1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _RESCTRL_H 3 #define _RESCTRL_H 4 5 #include <linux/cacheinfo.h> 6 #include <linux/kernel.h> 7 #include <linux/list.h> 8 #include <linux/pid.h> 9 #include <linux/resctrl_types.h> 10 11 /* CLOSID, RMID value used by the default control group */ 12 #define RESCTRL_RESERVED_CLOSID 0 13 #define RESCTRL_RESERVED_RMID 0 14 15 #define RESCTRL_PICK_ANY_CPU -1 16 17 #ifdef CONFIG_PROC_CPU_RESCTRL 18 19 int proc_resctrl_show(struct seq_file *m, 20 struct pid_namespace *ns, 21 struct pid *pid, 22 struct task_struct *tsk); 23 24 #endif 25 26 /* max value for struct rdt_domain's mbps_val */ 27 #define MBA_MAX_MBPS U32_MAX 28 29 /** 30 * enum resctrl_conf_type - The type of configuration. 31 * @CDP_NONE: No prioritisation, both code and data are controlled or monitored. 32 * @CDP_CODE: Configuration applies to instruction fetches. 33 * @CDP_DATA: Configuration applies to reads and writes. 34 */ 35 enum resctrl_conf_type { 36 CDP_NONE, 37 CDP_CODE, 38 CDP_DATA, 39 }; 40 41 #define CDP_NUM_TYPES (CDP_DATA + 1) 42 43 /** 44 * struct resctrl_staged_config - parsed configuration to be applied 45 * @new_ctrl: new ctrl value to be loaded 46 * @have_new_ctrl: whether the user provided new_ctrl is valid 47 */ 48 struct resctrl_staged_config { 49 u32 new_ctrl; 50 bool have_new_ctrl; 51 }; 52 53 enum resctrl_domain_type { 54 RESCTRL_CTRL_DOMAIN, 55 RESCTRL_MON_DOMAIN, 56 }; 57 58 /** 59 * struct rdt_domain_hdr - common header for different domain types 60 * @list: all instances of this resource 61 * @id: unique id for this instance 62 * @type: type of this instance 63 * @cpu_mask: which CPUs share this resource 64 */ 65 struct rdt_domain_hdr { 66 struct list_head list; 67 int id; 68 enum resctrl_domain_type type; 69 struct cpumask cpu_mask; 70 }; 71 72 /** 73 * struct rdt_ctrl_domain - group of CPUs sharing a resctrl control resource 74 * @hdr: common header for different domain types 75 * @plr: pseudo-locked region (if any) associated with domain 76 * @staged_config: parsed configuration to be applied 77 * @mbps_val: When mba_sc is enabled, this holds the array of user 78 * specified control values for mba_sc in MBps, indexed 79 * by closid 80 */ 81 struct rdt_ctrl_domain { 82 struct rdt_domain_hdr hdr; 83 struct pseudo_lock_region *plr; 84 struct resctrl_staged_config staged_config[CDP_NUM_TYPES]; 85 u32 *mbps_val; 86 }; 87 88 /** 89 * struct rdt_mon_domain - group of CPUs sharing a resctrl monitor resource 90 * @hdr: common header for different domain types 91 * @ci: cache info for this domain 92 * @rmid_busy_llc: bitmap of which limbo RMIDs are above threshold 93 * @mbm_total: saved state for MBM total bandwidth 94 * @mbm_local: saved state for MBM local bandwidth 95 * @mbm_over: worker to periodically read MBM h/w counters 96 * @cqm_limbo: worker to periodically read CQM h/w counters 97 * @mbm_work_cpu: worker CPU for MBM h/w counters 98 * @cqm_work_cpu: worker CPU for CQM h/w counters 99 */ 100 struct rdt_mon_domain { 101 struct rdt_domain_hdr hdr; 102 struct cacheinfo *ci; 103 unsigned long *rmid_busy_llc; 104 struct mbm_state *mbm_total; 105 struct mbm_state *mbm_local; 106 struct delayed_work mbm_over; 107 struct delayed_work cqm_limbo; 108 int mbm_work_cpu; 109 int cqm_work_cpu; 110 }; 111 112 /** 113 * struct resctrl_cache - Cache allocation related data 114 * @cbm_len: Length of the cache bit mask 115 * @min_cbm_bits: Minimum number of consecutive bits to be set. 116 * The value 0 means the architecture can support 117 * zero CBM. 118 * @shareable_bits: Bitmask of shareable resource with other 119 * executing entities 120 * @arch_has_sparse_bitmasks: True if a bitmask like f00f is valid. 121 * @arch_has_per_cpu_cfg: True if QOS_CFG register for this cache 122 * level has CPU scope. 123 */ 124 struct resctrl_cache { 125 unsigned int cbm_len; 126 unsigned int min_cbm_bits; 127 unsigned int shareable_bits; 128 bool arch_has_sparse_bitmasks; 129 bool arch_has_per_cpu_cfg; 130 }; 131 132 /** 133 * enum membw_throttle_mode - System's memory bandwidth throttling mode 134 * @THREAD_THROTTLE_UNDEFINED: Not relevant to the system 135 * @THREAD_THROTTLE_MAX: Memory bandwidth is throttled at the core 136 * always using smallest bandwidth percentage 137 * assigned to threads, aka "max throttling" 138 * @THREAD_THROTTLE_PER_THREAD: Memory bandwidth is throttled at the thread 139 */ 140 enum membw_throttle_mode { 141 THREAD_THROTTLE_UNDEFINED = 0, 142 THREAD_THROTTLE_MAX, 143 THREAD_THROTTLE_PER_THREAD, 144 }; 145 146 /** 147 * struct resctrl_membw - Memory bandwidth allocation related data 148 * @min_bw: Minimum memory bandwidth percentage user can request 149 * @max_bw: Maximum memory bandwidth value, used as the reset value 150 * @bw_gran: Granularity at which the memory bandwidth is allocated 151 * @delay_linear: True if memory B/W delay is in linear scale 152 * @arch_needs_linear: True if we can't configure non-linear resources 153 * @throttle_mode: Bandwidth throttling mode when threads request 154 * different memory bandwidths 155 * @mba_sc: True if MBA software controller(mba_sc) is enabled 156 * @mb_map: Mapping of memory B/W percentage to memory B/W delay 157 */ 158 struct resctrl_membw { 159 u32 min_bw; 160 u32 max_bw; 161 u32 bw_gran; 162 u32 delay_linear; 163 bool arch_needs_linear; 164 enum membw_throttle_mode throttle_mode; 165 bool mba_sc; 166 u32 *mb_map; 167 }; 168 169 struct resctrl_schema; 170 171 enum resctrl_scope { 172 RESCTRL_L2_CACHE = 2, 173 RESCTRL_L3_CACHE = 3, 174 RESCTRL_L3_NODE, 175 }; 176 177 /** 178 * enum resctrl_schema_fmt - The format user-space provides for a schema. 179 * @RESCTRL_SCHEMA_BITMAP: The schema is a bitmap in hex. 180 * @RESCTRL_SCHEMA_RANGE: The schema is a decimal number. 181 */ 182 enum resctrl_schema_fmt { 183 RESCTRL_SCHEMA_BITMAP, 184 RESCTRL_SCHEMA_RANGE, 185 }; 186 187 /** 188 * struct rdt_resource - attributes of a resctrl resource 189 * @rid: The index of the resource 190 * @alloc_capable: Is allocation available on this machine 191 * @mon_capable: Is monitor feature available on this machine 192 * @num_rmid: Number of RMIDs available 193 * @ctrl_scope: Scope of this resource for control functions 194 * @mon_scope: Scope of this resource for monitor functions 195 * @cache: Cache allocation related data 196 * @membw: If the component has bandwidth controls, their properties. 197 * @ctrl_domains: RCU list of all control domains for this resource 198 * @mon_domains: RCU list of all monitor domains for this resource 199 * @name: Name to use in "schemata" file. 200 * @schema_fmt: Which format string and parser is used for this schema. 201 * @evt_list: List of monitoring events 202 * @cdp_capable: Is the CDP feature available on this resource 203 */ 204 struct rdt_resource { 205 int rid; 206 bool alloc_capable; 207 bool mon_capable; 208 int num_rmid; 209 enum resctrl_scope ctrl_scope; 210 enum resctrl_scope mon_scope; 211 struct resctrl_cache cache; 212 struct resctrl_membw membw; 213 struct list_head ctrl_domains; 214 struct list_head mon_domains; 215 char *name; 216 enum resctrl_schema_fmt schema_fmt; 217 struct list_head evt_list; 218 bool cdp_capable; 219 }; 220 221 /* 222 * Get the resource that exists at this level. If the level is not supported 223 * a dummy/not-capable resource can be returned. Levels >= RDT_NUM_RESOURCES 224 * will return NULL. 225 */ 226 struct rdt_resource *resctrl_arch_get_resource(enum resctrl_res_level l); 227 228 /** 229 * struct resctrl_schema - configuration abilities of a resource presented to 230 * user-space 231 * @list: Member of resctrl_schema_all. 232 * @name: The name to use in the "schemata" file. 233 * @fmt_str: Format string to show domain value. 234 * @conf_type: Whether this schema is specific to code/data. 235 * @res: The resource structure exported by the architecture to describe 236 * the hardware that is configured by this schema. 237 * @num_closid: The number of closid that can be used with this schema. When 238 * features like CDP are enabled, this will be lower than the 239 * hardware supports for the resource. 240 */ 241 struct resctrl_schema { 242 struct list_head list; 243 char name[8]; 244 const char *fmt_str; 245 enum resctrl_conf_type conf_type; 246 struct rdt_resource *res; 247 u32 num_closid; 248 }; 249 250 struct resctrl_cpu_defaults { 251 u32 closid; 252 u32 rmid; 253 }; 254 255 /** 256 * resctrl_arch_sync_cpu_closid_rmid() - Refresh this CPU's CLOSID and RMID. 257 * Call via IPI. 258 * @info: If non-NULL, a pointer to a struct resctrl_cpu_defaults 259 * specifying the new CLOSID and RMID for tasks in the default 260 * resctrl ctrl and mon group when running on this CPU. If NULL, 261 * this CPU is not re-assigned to a different default group. 262 * 263 * Propagates reassignment of CPUs and/or tasks to different resctrl groups 264 * when requested by the resctrl core code. 265 * 266 * This function records the per-cpu defaults specified by @info (if any), 267 * and then reconfigures the CPU's hardware CLOSID and RMID for subsequent 268 * execution based on @current, in the same way as during a task switch. 269 */ 270 void resctrl_arch_sync_cpu_closid_rmid(void *info); 271 272 /** 273 * resctrl_get_default_ctrl() - Return the default control value for this 274 * resource. 275 * @r: The resource whose default control type is queried. 276 */ 277 static inline u32 resctrl_get_default_ctrl(struct rdt_resource *r) 278 { 279 switch (r->schema_fmt) { 280 case RESCTRL_SCHEMA_BITMAP: 281 return BIT_MASK(r->cache.cbm_len) - 1; 282 case RESCTRL_SCHEMA_RANGE: 283 return r->membw.max_bw; 284 } 285 286 return WARN_ON_ONCE(1); 287 } 288 289 /* The number of closid supported by this resource regardless of CDP */ 290 u32 resctrl_arch_get_num_closid(struct rdt_resource *r); 291 u32 resctrl_arch_system_num_rmid_idx(void); 292 int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid); 293 294 /* 295 * Update the ctrl_val and apply this config right now. 296 * Must be called on one of the domain's CPUs. 297 */ 298 int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_ctrl_domain *d, 299 u32 closid, enum resctrl_conf_type t, u32 cfg_val); 300 301 u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_ctrl_domain *d, 302 u32 closid, enum resctrl_conf_type type); 303 int resctrl_online_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d); 304 int resctrl_online_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d); 305 void resctrl_offline_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d); 306 void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d); 307 void resctrl_online_cpu(unsigned int cpu); 308 void resctrl_offline_cpu(unsigned int cpu); 309 310 /** 311 * resctrl_arch_rmid_read() - Read the eventid counter corresponding to rmid 312 * for this resource and domain. 313 * @r: resource that the counter should be read from. 314 * @d: domain that the counter should be read from. 315 * @closid: closid that matches the rmid. Depending on the architecture, the 316 * counter may match traffic of both @closid and @rmid, or @rmid 317 * only. 318 * @rmid: rmid of the counter to read. 319 * @eventid: eventid to read, e.g. L3 occupancy. 320 * @val: result of the counter read in bytes. 321 * @arch_mon_ctx: An architecture specific value from 322 * resctrl_arch_mon_ctx_alloc(), for MPAM this identifies 323 * the hardware monitor allocated for this read request. 324 * 325 * Some architectures need to sleep when first programming some of the counters. 326 * (specifically: arm64's MPAM cache occupancy counters can return 'not ready' 327 * for a short period of time). Call from a non-migrateable process context on 328 * a CPU that belongs to domain @d. e.g. use smp_call_on_cpu() or 329 * schedule_work_on(). This function can be called with interrupts masked, 330 * e.g. using smp_call_function_any(), but may consistently return an error. 331 * 332 * Return: 333 * 0 on success, or -EIO, -EINVAL etc on error. 334 */ 335 int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_mon_domain *d, 336 u32 closid, u32 rmid, enum resctrl_event_id eventid, 337 u64 *val, void *arch_mon_ctx); 338 339 /** 340 * resctrl_arch_rmid_read_context_check() - warn about invalid contexts 341 * 342 * When built with CONFIG_DEBUG_ATOMIC_SLEEP generate a warning when 343 * resctrl_arch_rmid_read() is called with preemption disabled. 344 * 345 * The contract with resctrl_arch_rmid_read() is that if interrupts 346 * are unmasked, it can sleep. This allows NOHZ_FULL systems to use an 347 * IPI, (and fail if the call needed to sleep), while most of the time 348 * the work is scheduled, allowing the call to sleep. 349 */ 350 static inline void resctrl_arch_rmid_read_context_check(void) 351 { 352 if (!irqs_disabled()) 353 might_sleep(); 354 } 355 356 /** 357 * resctrl_find_domain() - Search for a domain id in a resource domain list. 358 * @h: The domain list to search. 359 * @id: The domain id to search for. 360 * @pos: A pointer to position in the list id should be inserted. 361 * 362 * Search the domain list to find the domain id. If the domain id is 363 * found, return the domain. NULL otherwise. If the domain id is not 364 * found (and NULL returned) then the first domain with id bigger than 365 * the input id can be returned to the caller via @pos. 366 */ 367 struct rdt_domain_hdr *resctrl_find_domain(struct list_head *h, int id, 368 struct list_head **pos); 369 370 /** 371 * resctrl_arch_reset_rmid() - Reset any private state associated with rmid 372 * and eventid. 373 * @r: The domain's resource. 374 * @d: The rmid's domain. 375 * @closid: closid that matches the rmid. Depending on the architecture, the 376 * counter may match traffic of both @closid and @rmid, or @rmid only. 377 * @rmid: The rmid whose counter values should be reset. 378 * @eventid: The eventid whose counter values should be reset. 379 * 380 * This can be called from any CPU. 381 */ 382 void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_mon_domain *d, 383 u32 closid, u32 rmid, 384 enum resctrl_event_id eventid); 385 386 /** 387 * resctrl_arch_reset_rmid_all() - Reset all private state associated with 388 * all rmids and eventids. 389 * @r: The resctrl resource. 390 * @d: The domain for which all architectural counter state will 391 * be cleared. 392 * 393 * This can be called from any CPU. 394 */ 395 void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_mon_domain *d); 396 397 extern unsigned int resctrl_rmid_realloc_threshold; 398 extern unsigned int resctrl_rmid_realloc_limit; 399 400 int __init resctrl_init(void); 401 void __exit resctrl_exit(void); 402 403 #endif /* _RESCTRL_H */ 404