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 /* Walk all possible resources, with variants for only controls or monitors. */
30 #define for_each_rdt_resource(_r)						\
31 	for ((_r) = resctrl_arch_get_resource(0);				\
32 	     (_r) && (_r)->rid < RDT_NUM_RESOURCES;				\
33 	     (_r) = resctrl_arch_get_resource((_r)->rid + 1))
34 
35 #define for_each_capable_rdt_resource(r)				      \
36 	for_each_rdt_resource((r))					      \
37 		if ((r)->alloc_capable || (r)->mon_capable)
38 
39 #define for_each_alloc_capable_rdt_resource(r)				      \
40 	for_each_rdt_resource((r))					      \
41 		if ((r)->alloc_capable)
42 
43 #define for_each_mon_capable_rdt_resource(r)				      \
44 	for_each_rdt_resource((r))					      \
45 		if ((r)->mon_capable)
46 
47 /**
48  * enum resctrl_conf_type - The type of configuration.
49  * @CDP_NONE:	No prioritisation, both code and data are controlled or monitored.
50  * @CDP_CODE:	Configuration applies to instruction fetches.
51  * @CDP_DATA:	Configuration applies to reads and writes.
52  */
53 enum resctrl_conf_type {
54 	CDP_NONE,
55 	CDP_CODE,
56 	CDP_DATA,
57 };
58 
59 #define CDP_NUM_TYPES	(CDP_DATA + 1)
60 
61 /*
62  * struct pseudo_lock_region - pseudo-lock region information
63  * @s:			Resctrl schema for the resource to which this
64  *			pseudo-locked region belongs
65  * @closid:		The closid that this pseudo-locked region uses
66  * @d:			RDT domain to which this pseudo-locked region
67  *			belongs
68  * @cbm:		bitmask of the pseudo-locked region
69  * @lock_thread_wq:	waitqueue used to wait on the pseudo-locking thread
70  *			completion
71  * @thread_done:	variable used by waitqueue to test if pseudo-locking
72  *			thread completed
73  * @cpu:		core associated with the cache on which the setup code
74  *			will be run
75  * @line_size:		size of the cache lines
76  * @size:		size of pseudo-locked region in bytes
77  * @kmem:		the kernel memory associated with pseudo-locked region
78  * @minor:		minor number of character device associated with this
79  *			region
80  * @debugfs_dir:	pointer to this region's directory in the debugfs
81  *			filesystem
82  * @pm_reqs:		Power management QoS requests related to this region
83  */
84 struct pseudo_lock_region {
85 	struct resctrl_schema	*s;
86 	u32			closid;
87 	struct rdt_ctrl_domain	*d;
88 	u32			cbm;
89 	wait_queue_head_t	lock_thread_wq;
90 	int			thread_done;
91 	int			cpu;
92 	unsigned int		line_size;
93 	unsigned int		size;
94 	void			*kmem;
95 	unsigned int		minor;
96 	struct dentry		*debugfs_dir;
97 	struct list_head	pm_reqs;
98 };
99 
100 /**
101  * struct resctrl_staged_config - parsed configuration to be applied
102  * @new_ctrl:		new ctrl value to be loaded
103  * @have_new_ctrl:	whether the user provided new_ctrl is valid
104  */
105 struct resctrl_staged_config {
106 	u32			new_ctrl;
107 	bool			have_new_ctrl;
108 };
109 
110 enum resctrl_domain_type {
111 	RESCTRL_CTRL_DOMAIN,
112 	RESCTRL_MON_DOMAIN,
113 };
114 
115 /**
116  * struct rdt_domain_hdr - common header for different domain types
117  * @list:		all instances of this resource
118  * @id:			unique id for this instance
119  * @type:		type of this instance
120  * @cpu_mask:		which CPUs share this resource
121  */
122 struct rdt_domain_hdr {
123 	struct list_head		list;
124 	int				id;
125 	enum resctrl_domain_type	type;
126 	struct cpumask			cpu_mask;
127 };
128 
129 /**
130  * struct rdt_ctrl_domain - group of CPUs sharing a resctrl control resource
131  * @hdr:		common header for different domain types
132  * @plr:		pseudo-locked region (if any) associated with domain
133  * @staged_config:	parsed configuration to be applied
134  * @mbps_val:		When mba_sc is enabled, this holds the array of user
135  *			specified control values for mba_sc in MBps, indexed
136  *			by closid
137  */
138 struct rdt_ctrl_domain {
139 	struct rdt_domain_hdr		hdr;
140 	struct pseudo_lock_region	*plr;
141 	struct resctrl_staged_config	staged_config[CDP_NUM_TYPES];
142 	u32				*mbps_val;
143 };
144 
145 /**
146  * struct rdt_mon_domain - group of CPUs sharing a resctrl monitor resource
147  * @hdr:		common header for different domain types
148  * @ci:			cache info for this domain
149  * @rmid_busy_llc:	bitmap of which limbo RMIDs are above threshold
150  * @mbm_total:		saved state for MBM total bandwidth
151  * @mbm_local:		saved state for MBM local bandwidth
152  * @mbm_over:		worker to periodically read MBM h/w counters
153  * @cqm_limbo:		worker to periodically read CQM h/w counters
154  * @mbm_work_cpu:	worker CPU for MBM h/w counters
155  * @cqm_work_cpu:	worker CPU for CQM h/w counters
156  */
157 struct rdt_mon_domain {
158 	struct rdt_domain_hdr		hdr;
159 	struct cacheinfo		*ci;
160 	unsigned long			*rmid_busy_llc;
161 	struct mbm_state		*mbm_total;
162 	struct mbm_state		*mbm_local;
163 	struct delayed_work		mbm_over;
164 	struct delayed_work		cqm_limbo;
165 	int				mbm_work_cpu;
166 	int				cqm_work_cpu;
167 };
168 
169 /**
170  * struct resctrl_cache - Cache allocation related data
171  * @cbm_len:		Length of the cache bit mask
172  * @min_cbm_bits:	Minimum number of consecutive bits to be set.
173  *			The value 0 means the architecture can support
174  *			zero CBM.
175  * @shareable_bits:	Bitmask of shareable resource with other
176  *			executing entities
177  * @arch_has_sparse_bitmasks:	True if a bitmask like f00f is valid.
178  * @arch_has_per_cpu_cfg:	True if QOS_CFG register for this cache
179  *				level has CPU scope.
180  */
181 struct resctrl_cache {
182 	unsigned int	cbm_len;
183 	unsigned int	min_cbm_bits;
184 	unsigned int	shareable_bits;
185 	bool		arch_has_sparse_bitmasks;
186 	bool		arch_has_per_cpu_cfg;
187 };
188 
189 /**
190  * enum membw_throttle_mode - System's memory bandwidth throttling mode
191  * @THREAD_THROTTLE_UNDEFINED:	Not relevant to the system
192  * @THREAD_THROTTLE_MAX:	Memory bandwidth is throttled at the core
193  *				always using smallest bandwidth percentage
194  *				assigned to threads, aka "max throttling"
195  * @THREAD_THROTTLE_PER_THREAD:	Memory bandwidth is throttled at the thread
196  */
197 enum membw_throttle_mode {
198 	THREAD_THROTTLE_UNDEFINED = 0,
199 	THREAD_THROTTLE_MAX,
200 	THREAD_THROTTLE_PER_THREAD,
201 };
202 
203 /**
204  * struct resctrl_membw - Memory bandwidth allocation related data
205  * @min_bw:		Minimum memory bandwidth percentage user can request
206  * @max_bw:		Maximum memory bandwidth value, used as the reset value
207  * @bw_gran:		Granularity at which the memory bandwidth is allocated
208  * @delay_linear:	True if memory B/W delay is in linear scale
209  * @arch_needs_linear:	True if we can't configure non-linear resources
210  * @throttle_mode:	Bandwidth throttling mode when threads request
211  *			different memory bandwidths
212  * @mba_sc:		True if MBA software controller(mba_sc) is enabled
213  * @mb_map:		Mapping of memory B/W percentage to memory B/W delay
214  */
215 struct resctrl_membw {
216 	u32				min_bw;
217 	u32				max_bw;
218 	u32				bw_gran;
219 	u32				delay_linear;
220 	bool				arch_needs_linear;
221 	enum membw_throttle_mode	throttle_mode;
222 	bool				mba_sc;
223 	u32				*mb_map;
224 };
225 
226 struct resctrl_schema;
227 
228 enum resctrl_scope {
229 	RESCTRL_L2_CACHE = 2,
230 	RESCTRL_L3_CACHE = 3,
231 	RESCTRL_L3_NODE,
232 };
233 
234 /**
235  * enum resctrl_schema_fmt - The format user-space provides for a schema.
236  * @RESCTRL_SCHEMA_BITMAP:	The schema is a bitmap in hex.
237  * @RESCTRL_SCHEMA_RANGE:	The schema is a decimal number.
238  */
239 enum resctrl_schema_fmt {
240 	RESCTRL_SCHEMA_BITMAP,
241 	RESCTRL_SCHEMA_RANGE,
242 };
243 
244 /**
245  * struct rdt_resource - attributes of a resctrl resource
246  * @rid:		The index of the resource
247  * @alloc_capable:	Is allocation available on this machine
248  * @mon_capable:	Is monitor feature available on this machine
249  * @num_rmid:		Number of RMIDs available
250  * @ctrl_scope:		Scope of this resource for control functions
251  * @mon_scope:		Scope of this resource for monitor functions
252  * @cache:		Cache allocation related data
253  * @membw:		If the component has bandwidth controls, their properties.
254  * @ctrl_domains:	RCU list of all control domains for this resource
255  * @mon_domains:	RCU list of all monitor domains for this resource
256  * @name:		Name to use in "schemata" file.
257  * @schema_fmt:		Which format string and parser is used for this schema.
258  * @evt_list:		List of monitoring events
259  * @mbm_cfg_mask:	Bandwidth sources that can be tracked when bandwidth
260  *			monitoring events can be configured.
261  * @cdp_capable:	Is the CDP feature available on this resource
262  */
263 struct rdt_resource {
264 	int			rid;
265 	bool			alloc_capable;
266 	bool			mon_capable;
267 	int			num_rmid;
268 	enum resctrl_scope	ctrl_scope;
269 	enum resctrl_scope	mon_scope;
270 	struct resctrl_cache	cache;
271 	struct resctrl_membw	membw;
272 	struct list_head	ctrl_domains;
273 	struct list_head	mon_domains;
274 	char			*name;
275 	enum resctrl_schema_fmt	schema_fmt;
276 	struct list_head	evt_list;
277 	unsigned int		mbm_cfg_mask;
278 	bool			cdp_capable;
279 };
280 
281 /*
282  * Get the resource that exists at this level. If the level is not supported
283  * a dummy/not-capable resource can be returned. Levels >= RDT_NUM_RESOURCES
284  * will return NULL.
285  */
286 struct rdt_resource *resctrl_arch_get_resource(enum resctrl_res_level l);
287 
288 /**
289  * struct resctrl_schema - configuration abilities of a resource presented to
290  *			   user-space
291  * @list:	Member of resctrl_schema_all.
292  * @name:	The name to use in the "schemata" file.
293  * @fmt_str:	Format string to show domain value.
294  * @conf_type:	Whether this schema is specific to code/data.
295  * @res:	The resource structure exported by the architecture to describe
296  *		the hardware that is configured by this schema.
297  * @num_closid:	The number of closid that can be used with this schema. When
298  *		features like CDP are enabled, this will be lower than the
299  *		hardware supports for the resource.
300  */
301 struct resctrl_schema {
302 	struct list_head		list;
303 	char				name[8];
304 	const char			*fmt_str;
305 	enum resctrl_conf_type		conf_type;
306 	struct rdt_resource		*res;
307 	u32				num_closid;
308 };
309 
310 struct resctrl_cpu_defaults {
311 	u32 closid;
312 	u32 rmid;
313 };
314 
315 struct resctrl_mon_config_info {
316 	struct rdt_resource	*r;
317 	struct rdt_mon_domain	*d;
318 	u32			evtid;
319 	u32			mon_config;
320 };
321 
322 /**
323  * resctrl_arch_sync_cpu_closid_rmid() - Refresh this CPU's CLOSID and RMID.
324  *					 Call via IPI.
325  * @info:	If non-NULL, a pointer to a struct resctrl_cpu_defaults
326  *		specifying the new CLOSID and RMID for tasks in the default
327  *		resctrl ctrl and mon group when running on this CPU.  If NULL,
328  *		this CPU is not re-assigned to a different default group.
329  *
330  * Propagates reassignment of CPUs and/or tasks to different resctrl groups
331  * when requested by the resctrl core code.
332  *
333  * This function records the per-cpu defaults specified by @info (if any),
334  * and then reconfigures the CPU's hardware CLOSID and RMID for subsequent
335  * execution based on @current, in the same way as during a task switch.
336  */
337 void resctrl_arch_sync_cpu_closid_rmid(void *info);
338 
339 /**
340  * resctrl_get_default_ctrl() - Return the default control value for this
341  *                              resource.
342  * @r:		The resource whose default control type is queried.
343  */
resctrl_get_default_ctrl(struct rdt_resource * r)344 static inline u32 resctrl_get_default_ctrl(struct rdt_resource *r)
345 {
346 	switch (r->schema_fmt) {
347 	case RESCTRL_SCHEMA_BITMAP:
348 		return BIT_MASK(r->cache.cbm_len) - 1;
349 	case RESCTRL_SCHEMA_RANGE:
350 		return r->membw.max_bw;
351 	}
352 
353 	return WARN_ON_ONCE(1);
354 }
355 
356 /* The number of closid supported by this resource regardless of CDP */
357 u32 resctrl_arch_get_num_closid(struct rdt_resource *r);
358 u32 resctrl_arch_system_num_rmid_idx(void);
359 int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid);
360 
361 __init bool resctrl_arch_is_evt_configurable(enum resctrl_event_id evt);
362 
363 /**
364  * resctrl_arch_mon_event_config_write() - Write the config for an event.
365  * @config_info: struct resctrl_mon_config_info describing the resource, domain
366  *		 and event.
367  *
368  * Reads resource, domain and eventid from @config_info and writes the
369  * event config_info->mon_config into hardware.
370  *
371  * Called via IPI to reach a CPU that is a member of the specified domain.
372  */
373 void resctrl_arch_mon_event_config_write(void *config_info);
374 
375 /**
376  * resctrl_arch_mon_event_config_read() - Read the config for an event.
377  * @config_info: struct resctrl_mon_config_info describing the resource, domain
378  *		 and event.
379  *
380  * Reads resource, domain and eventid from @config_info and reads the
381  * hardware config value into config_info->mon_config.
382  *
383  * Called via IPI to reach a CPU that is a member of the specified domain.
384  */
385 void resctrl_arch_mon_event_config_read(void *config_info);
386 
387 /* For use by arch code to remap resctrl's smaller CDP CLOSID range */
resctrl_get_config_index(u32 closid,enum resctrl_conf_type type)388 static inline u32 resctrl_get_config_index(u32 closid,
389 					   enum resctrl_conf_type type)
390 {
391 	switch (type) {
392 	default:
393 	case CDP_NONE:
394 		return closid;
395 	case CDP_CODE:
396 		return closid * 2 + 1;
397 	case CDP_DATA:
398 		return closid * 2;
399 	}
400 }
401 
402 /*
403  * Update the ctrl_val and apply this config right now.
404  * Must be called on one of the domain's CPUs.
405  */
406 int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_ctrl_domain *d,
407 			    u32 closid, enum resctrl_conf_type t, u32 cfg_val);
408 
409 u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_ctrl_domain *d,
410 			    u32 closid, enum resctrl_conf_type type);
411 int resctrl_online_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d);
412 int resctrl_online_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d);
413 void resctrl_offline_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d);
414 void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d);
415 void resctrl_online_cpu(unsigned int cpu);
416 void resctrl_offline_cpu(unsigned int cpu);
417 
418 /**
419  * resctrl_arch_rmid_read() - Read the eventid counter corresponding to rmid
420  *			      for this resource and domain.
421  * @r:			resource that the counter should be read from.
422  * @d:			domain that the counter should be read from.
423  * @closid:		closid that matches the rmid. Depending on the architecture, the
424  *			counter may match traffic of both @closid and @rmid, or @rmid
425  *			only.
426  * @rmid:		rmid of the counter to read.
427  * @eventid:		eventid to read, e.g. L3 occupancy.
428  * @val:		result of the counter read in bytes.
429  * @arch_mon_ctx:	An architecture specific value from
430  *			resctrl_arch_mon_ctx_alloc(), for MPAM this identifies
431  *			the hardware monitor allocated for this read request.
432  *
433  * Some architectures need to sleep when first programming some of the counters.
434  * (specifically: arm64's MPAM cache occupancy counters can return 'not ready'
435  *  for a short period of time). Call from a non-migrateable process context on
436  * a CPU that belongs to domain @d. e.g. use smp_call_on_cpu() or
437  * schedule_work_on(). This function can be called with interrupts masked,
438  * e.g. using smp_call_function_any(), but may consistently return an error.
439  *
440  * Return:
441  * 0 on success, or -EIO, -EINVAL etc on error.
442  */
443 int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_mon_domain *d,
444 			   u32 closid, u32 rmid, enum resctrl_event_id eventid,
445 			   u64 *val, void *arch_mon_ctx);
446 
447 /**
448  * resctrl_arch_rmid_read_context_check()  - warn about invalid contexts
449  *
450  * When built with CONFIG_DEBUG_ATOMIC_SLEEP generate a warning when
451  * resctrl_arch_rmid_read() is called with preemption disabled.
452  *
453  * The contract with resctrl_arch_rmid_read() is that if interrupts
454  * are unmasked, it can sleep. This allows NOHZ_FULL systems to use an
455  * IPI, (and fail if the call needed to sleep), while most of the time
456  * the work is scheduled, allowing the call to sleep.
457  */
resctrl_arch_rmid_read_context_check(void)458 static inline void resctrl_arch_rmid_read_context_check(void)
459 {
460 	if (!irqs_disabled())
461 		might_sleep();
462 }
463 
464 /**
465  * resctrl_find_domain() - Search for a domain id in a resource domain list.
466  * @h:		The domain list to search.
467  * @id:		The domain id to search for.
468  * @pos:	A pointer to position in the list id should be inserted.
469  *
470  * Search the domain list to find the domain id. If the domain id is
471  * found, return the domain. NULL otherwise.  If the domain id is not
472  * found (and NULL returned) then the first domain with id bigger than
473  * the input id can be returned to the caller via @pos.
474  */
475 struct rdt_domain_hdr *resctrl_find_domain(struct list_head *h, int id,
476 					   struct list_head **pos);
477 
478 /**
479  * resctrl_arch_reset_rmid() - Reset any private state associated with rmid
480  *			       and eventid.
481  * @r:		The domain's resource.
482  * @d:		The rmid's domain.
483  * @closid:	closid that matches the rmid. Depending on the architecture, the
484  *		counter may match traffic of both @closid and @rmid, or @rmid only.
485  * @rmid:	The rmid whose counter values should be reset.
486  * @eventid:	The eventid whose counter values should be reset.
487  *
488  * This can be called from any CPU.
489  */
490 void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_mon_domain *d,
491 			     u32 closid, u32 rmid,
492 			     enum resctrl_event_id eventid);
493 
494 /**
495  * resctrl_arch_reset_rmid_all() - Reset all private state associated with
496  *				   all rmids and eventids.
497  * @r:		The resctrl resource.
498  * @d:		The domain for which all architectural counter state will
499  *		be cleared.
500  *
501  * This can be called from any CPU.
502  */
503 void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_mon_domain *d);
504 
505 /**
506  * resctrl_arch_reset_all_ctrls() - Reset the control for each CLOSID to its
507  *				    default.
508  * @r:		The resctrl resource to reset.
509  *
510  * This can be called from any CPU.
511  */
512 void resctrl_arch_reset_all_ctrls(struct rdt_resource *r);
513 
514 extern unsigned int resctrl_rmid_realloc_threshold;
515 extern unsigned int resctrl_rmid_realloc_limit;
516 
517 int __init resctrl_init(void);
518 void __exit resctrl_exit(void);
519 
520 #endif /* _RESCTRL_H */
521