xref: /linux/include/linux/damon.h (revision 021bc4b9)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * DAMON api
4  *
5  * Author: SeongJae Park <sj@kernel.org>
6  */
7 
8 #ifndef _DAMON_H_
9 #define _DAMON_H_
10 
11 #include <linux/memcontrol.h>
12 #include <linux/mutex.h>
13 #include <linux/time64.h>
14 #include <linux/types.h>
15 #include <linux/random.h>
16 
17 /* Minimal region size.  Every damon_region is aligned by this. */
18 #define DAMON_MIN_REGION	PAGE_SIZE
19 /* Max priority score for DAMON-based operation schemes */
20 #define DAMOS_MAX_SCORE		(99)
21 
22 /* Get a random number in [l, r) */
23 static inline unsigned long damon_rand(unsigned long l, unsigned long r)
24 {
25 	return l + get_random_u32_below(r - l);
26 }
27 
28 /**
29  * struct damon_addr_range - Represents an address region of [@start, @end).
30  * @start:	Start address of the region (inclusive).
31  * @end:	End address of the region (exclusive).
32  */
33 struct damon_addr_range {
34 	unsigned long start;
35 	unsigned long end;
36 };
37 
38 /**
39  * struct damon_region - Represents a monitoring target region.
40  * @ar:			The address range of the region.
41  * @sampling_addr:	Address of the sample for the next access check.
42  * @nr_accesses:	Access frequency of this region.
43  * @nr_accesses_bp:	@nr_accesses in basis point (0.01%) that updated for
44  *			each sampling interval.
45  * @list:		List head for siblings.
46  * @age:		Age of this region.
47  *
48  * @nr_accesses is reset to zero for every &damon_attrs->aggr_interval and be
49  * increased for every &damon_attrs->sample_interval if an access to the region
50  * during the last sampling interval is found.  The update of this field should
51  * not be done with direct access but with the helper function,
52  * damon_update_region_access_rate().
53  *
54  * @nr_accesses_bp is another representation of @nr_accesses in basis point
55  * (1 in 10,000) that updated for every &damon_attrs->sample_interval in a
56  * manner similar to moving sum.  By the algorithm, this value becomes
57  * @nr_accesses * 10000 for every &struct damon_attrs->aggr_interval.  This can
58  * be used when the aggregation interval is too huge and therefore cannot wait
59  * for it before getting the access monitoring results.
60  *
61  * @age is initially zero, increased for each aggregation interval, and reset
62  * to zero again if the access frequency is significantly changed.  If two
63  * regions are merged into a new region, both @nr_accesses and @age of the new
64  * region are set as region size-weighted average of those of the two regions.
65  */
66 struct damon_region {
67 	struct damon_addr_range ar;
68 	unsigned long sampling_addr;
69 	unsigned int nr_accesses;
70 	unsigned int nr_accesses_bp;
71 	struct list_head list;
72 
73 	unsigned int age;
74 /* private: Internal value for age calculation. */
75 	unsigned int last_nr_accesses;
76 };
77 
78 /**
79  * struct damon_target - Represents a monitoring target.
80  * @pid:		The PID of the virtual address space to monitor.
81  * @nr_regions:		Number of monitoring target regions of this target.
82  * @regions_list:	Head of the monitoring target regions of this target.
83  * @list:		List head for siblings.
84  *
85  * Each monitoring context could have multiple targets.  For example, a context
86  * for virtual memory address spaces could have multiple target processes.  The
87  * @pid should be set for appropriate &struct damon_operations including the
88  * virtual address spaces monitoring operations.
89  */
90 struct damon_target {
91 	struct pid *pid;
92 	unsigned int nr_regions;
93 	struct list_head regions_list;
94 	struct list_head list;
95 };
96 
97 /**
98  * enum damos_action - Represents an action of a Data Access Monitoring-based
99  * Operation Scheme.
100  *
101  * @DAMOS_WILLNEED:	Call ``madvise()`` for the region with MADV_WILLNEED.
102  * @DAMOS_COLD:		Call ``madvise()`` for the region with MADV_COLD.
103  * @DAMOS_PAGEOUT:	Call ``madvise()`` for the region with MADV_PAGEOUT.
104  * @DAMOS_HUGEPAGE:	Call ``madvise()`` for the region with MADV_HUGEPAGE.
105  * @DAMOS_NOHUGEPAGE:	Call ``madvise()`` for the region with MADV_NOHUGEPAGE.
106  * @DAMOS_LRU_PRIO:	Prioritize the region on its LRU lists.
107  * @DAMOS_LRU_DEPRIO:	Deprioritize the region on its LRU lists.
108  * @DAMOS_STAT:		Do nothing but count the stat.
109  * @NR_DAMOS_ACTIONS:	Total number of DAMOS actions
110  *
111  * The support of each action is up to running &struct damon_operations.
112  * &enum DAMON_OPS_VADDR and &enum DAMON_OPS_FVADDR supports all actions except
113  * &enum DAMOS_LRU_PRIO and &enum DAMOS_LRU_DEPRIO.  &enum DAMON_OPS_PADDR
114  * supports only &enum DAMOS_PAGEOUT, &enum DAMOS_LRU_PRIO, &enum
115  * DAMOS_LRU_DEPRIO, and &DAMOS_STAT.
116  */
117 enum damos_action {
118 	DAMOS_WILLNEED,
119 	DAMOS_COLD,
120 	DAMOS_PAGEOUT,
121 	DAMOS_HUGEPAGE,
122 	DAMOS_NOHUGEPAGE,
123 	DAMOS_LRU_PRIO,
124 	DAMOS_LRU_DEPRIO,
125 	DAMOS_STAT,		/* Do nothing but only record the stat */
126 	NR_DAMOS_ACTIONS,
127 };
128 
129 /**
130  * struct damos_quota - Controls the aggressiveness of the given scheme.
131  * @ms:			Maximum milliseconds that the scheme can use.
132  * @sz:			Maximum bytes of memory that the action can be applied.
133  * @reset_interval:	Charge reset interval in milliseconds.
134  *
135  * @weight_sz:		Weight of the region's size for prioritization.
136  * @weight_nr_accesses:	Weight of the region's nr_accesses for prioritization.
137  * @weight_age:		Weight of the region's age for prioritization.
138  *
139  * @get_score:		Feedback function for self-tuning quota.
140  * @get_score_arg:	Parameter for @get_score
141  *
142  * To avoid consuming too much CPU time or IO resources for applying the
143  * &struct damos->action to large memory, DAMON allows users to set time and/or
144  * size quotas.  The quotas can be set by writing non-zero values to &ms and
145  * &sz, respectively.  If the time quota is set, DAMON tries to use only up to
146  * &ms milliseconds within &reset_interval for applying the action.  If the
147  * size quota is set, DAMON tries to apply the action only up to &sz bytes
148  * within &reset_interval.
149  *
150  * Internally, the time quota is transformed to a size quota using estimated
151  * throughput of the scheme's action.  DAMON then compares it against &sz and
152  * uses smaller one as the effective quota.
153  *
154  * For selecting regions within the quota, DAMON prioritizes current scheme's
155  * target memory regions using the &struct damon_operations->get_scheme_score.
156  * You could customize the prioritization logic by setting &weight_sz,
157  * &weight_nr_accesses, and &weight_age, because monitoring operations are
158  * encouraged to respect those.
159  *
160  * If @get_score function pointer is set, DAMON calls it back with
161  * @get_score_arg and get the return value of it for every @reset_interval.
162  * Then, DAMON adjusts the effective quota using the return value as a feedback
163  * score to the current quota, using its internal feedback loop algorithm.
164  *
165  * The feedback loop algorithem assumes the quota input and the feedback score
166  * output are in a positive proportional relationship, and the goal of the
167  * tuning is getting the feedback screo value of 10,000.  If @ms and/or @sz are
168  * set together, those work as a hard limit quota.  If neither @ms nor @sz are
169  * set, the mechanism starts from the quota of one byte.
170  */
171 struct damos_quota {
172 	unsigned long ms;
173 	unsigned long sz;
174 	unsigned long reset_interval;
175 
176 	unsigned int weight_sz;
177 	unsigned int weight_nr_accesses;
178 	unsigned int weight_age;
179 
180 	unsigned long (*get_score)(void *arg);
181 	void *get_score_arg;
182 
183 /* private: */
184 	/* For throughput estimation */
185 	unsigned long total_charged_sz;
186 	unsigned long total_charged_ns;
187 
188 	unsigned long esz;	/* Effective size quota in bytes */
189 
190 	/* For charging the quota */
191 	unsigned long charged_sz;
192 	unsigned long charged_from;
193 	struct damon_target *charge_target_from;
194 	unsigned long charge_addr_from;
195 
196 	/* For prioritization */
197 	unsigned long histogram[DAMOS_MAX_SCORE + 1];
198 	unsigned int min_score;
199 
200 	/* For feedback loop */
201 	unsigned long esz_bp;
202 };
203 
204 /**
205  * enum damos_wmark_metric - Represents the watermark metric.
206  *
207  * @DAMOS_WMARK_NONE:		Ignore the watermarks of the given scheme.
208  * @DAMOS_WMARK_FREE_MEM_RATE:	Free memory rate of the system in [0,1000].
209  * @NR_DAMOS_WMARK_METRICS:	Total number of DAMOS watermark metrics
210  */
211 enum damos_wmark_metric {
212 	DAMOS_WMARK_NONE,
213 	DAMOS_WMARK_FREE_MEM_RATE,
214 	NR_DAMOS_WMARK_METRICS,
215 };
216 
217 /**
218  * struct damos_watermarks - Controls when a given scheme should be activated.
219  * @metric:	Metric for the watermarks.
220  * @interval:	Watermarks check time interval in microseconds.
221  * @high:	High watermark.
222  * @mid:	Middle watermark.
223  * @low:	Low watermark.
224  *
225  * If &metric is &DAMOS_WMARK_NONE, the scheme is always active.  Being active
226  * means DAMON does monitoring and applying the action of the scheme to
227  * appropriate memory regions.  Else, DAMON checks &metric of the system for at
228  * least every &interval microseconds and works as below.
229  *
230  * If &metric is higher than &high, the scheme is inactivated.  If &metric is
231  * between &mid and &low, the scheme is activated.  If &metric is lower than
232  * &low, the scheme is inactivated.
233  */
234 struct damos_watermarks {
235 	enum damos_wmark_metric metric;
236 	unsigned long interval;
237 	unsigned long high;
238 	unsigned long mid;
239 	unsigned long low;
240 
241 /* private: */
242 	bool activated;
243 };
244 
245 /**
246  * struct damos_stat - Statistics on a given scheme.
247  * @nr_tried:	Total number of regions that the scheme is tried to be applied.
248  * @sz_tried:	Total size of regions that the scheme is tried to be applied.
249  * @nr_applied:	Total number of regions that the scheme is applied.
250  * @sz_applied:	Total size of regions that the scheme is applied.
251  * @qt_exceeds: Total number of times the quota of the scheme has exceeded.
252  */
253 struct damos_stat {
254 	unsigned long nr_tried;
255 	unsigned long sz_tried;
256 	unsigned long nr_applied;
257 	unsigned long sz_applied;
258 	unsigned long qt_exceeds;
259 };
260 
261 /**
262  * enum damos_filter_type - Type of memory for &struct damos_filter
263  * @DAMOS_FILTER_TYPE_ANON:	Anonymous pages.
264  * @DAMOS_FILTER_TYPE_MEMCG:	Specific memcg's pages.
265  * @DAMOS_FILTER_TYPE_ADDR:	Address range.
266  * @DAMOS_FILTER_TYPE_TARGET:	Data Access Monitoring target.
267  * @NR_DAMOS_FILTER_TYPES:	Number of filter types.
268  *
269  * The anon pages type and memcg type filters are handled by underlying
270  * &struct damon_operations as a part of scheme action trying, and therefore
271  * accounted as 'tried'.  In contrast, other types are handled by core layer
272  * before trying of the action and therefore not accounted as 'tried'.
273  *
274  * The support of the filters that handled by &struct damon_operations depend
275  * on the running &struct damon_operations.
276  * &enum DAMON_OPS_PADDR supports both anon pages type and memcg type filters,
277  * while &enum DAMON_OPS_VADDR and &enum DAMON_OPS_FVADDR don't support any of
278  * the two types.
279  */
280 enum damos_filter_type {
281 	DAMOS_FILTER_TYPE_ANON,
282 	DAMOS_FILTER_TYPE_MEMCG,
283 	DAMOS_FILTER_TYPE_ADDR,
284 	DAMOS_FILTER_TYPE_TARGET,
285 	NR_DAMOS_FILTER_TYPES,
286 };
287 
288 /**
289  * struct damos_filter - DAMOS action target memory filter.
290  * @type:	Type of the page.
291  * @matching:	If the matching page should filtered out or in.
292  * @memcg_id:	Memcg id of the question if @type is DAMOS_FILTER_MEMCG.
293  * @addr_range:	Address range if @type is DAMOS_FILTER_TYPE_ADDR.
294  * @target_idx:	Index of the &struct damon_target of
295  *		&damon_ctx->adaptive_targets if @type is
296  *		DAMOS_FILTER_TYPE_TARGET.
297  * @list:	List head for siblings.
298  *
299  * Before applying the &damos->action to a memory region, DAMOS checks if each
300  * page of the region matches to this and avoid applying the action if so.
301  * Support of each filter type depends on the running &struct damon_operations
302  * and the type.  Refer to &enum damos_filter_type for more detai.
303  */
304 struct damos_filter {
305 	enum damos_filter_type type;
306 	bool matching;
307 	union {
308 		unsigned short memcg_id;
309 		struct damon_addr_range addr_range;
310 		int target_idx;
311 	};
312 	struct list_head list;
313 };
314 
315 /**
316  * struct damos_access_pattern - Target access pattern of the given scheme.
317  * @min_sz_region:	Minimum size of target regions.
318  * @max_sz_region:	Maximum size of target regions.
319  * @min_nr_accesses:	Minimum ``->nr_accesses`` of target regions.
320  * @max_nr_accesses:	Maximum ``->nr_accesses`` of target regions.
321  * @min_age_region:	Minimum age of target regions.
322  * @max_age_region:	Maximum age of target regions.
323  */
324 struct damos_access_pattern {
325 	unsigned long min_sz_region;
326 	unsigned long max_sz_region;
327 	unsigned int min_nr_accesses;
328 	unsigned int max_nr_accesses;
329 	unsigned int min_age_region;
330 	unsigned int max_age_region;
331 };
332 
333 /**
334  * struct damos - Represents a Data Access Monitoring-based Operation Scheme.
335  * @pattern:		Access pattern of target regions.
336  * @action:		&damo_action to be applied to the target regions.
337  * @apply_interval_us:	The time between applying the @action.
338  * @quota:		Control the aggressiveness of this scheme.
339  * @wmarks:		Watermarks for automated (in)activation of this scheme.
340  * @filters:		Additional set of &struct damos_filter for &action.
341  * @stat:		Statistics of this scheme.
342  * @list:		List head for siblings.
343  *
344  * For each @apply_interval_us, DAMON finds regions which fit in the
345  * &pattern and applies &action to those. To avoid consuming too much
346  * CPU time or IO resources for the &action, &quota is used.
347  *
348  * If @apply_interval_us is zero, &damon_attrs->aggr_interval is used instead.
349  *
350  * To do the work only when needed, schemes can be activated for specific
351  * system situations using &wmarks.  If all schemes that registered to the
352  * monitoring context are inactive, DAMON stops monitoring either, and just
353  * repeatedly checks the watermarks.
354  *
355  * Before applying the &action to a memory region, &struct damon_operations
356  * implementation could check pages of the region and skip &action to respect
357  * &filters
358  *
359  * After applying the &action to each region, &stat_count and &stat_sz is
360  * updated to reflect the number of regions and total size of regions that the
361  * &action is applied.
362  */
363 struct damos {
364 	struct damos_access_pattern pattern;
365 	enum damos_action action;
366 	unsigned long apply_interval_us;
367 /* private: internal use only */
368 	/*
369 	 * number of sample intervals that should be passed before applying
370 	 * @action
371 	 */
372 	unsigned long next_apply_sis;
373 /* public: */
374 	struct damos_quota quota;
375 	struct damos_watermarks wmarks;
376 	struct list_head filters;
377 	struct damos_stat stat;
378 	struct list_head list;
379 };
380 
381 /**
382  * enum damon_ops_id - Identifier for each monitoring operations implementation
383  *
384  * @DAMON_OPS_VADDR:	Monitoring operations for virtual address spaces
385  * @DAMON_OPS_FVADDR:	Monitoring operations for only fixed ranges of virtual
386  *			address spaces
387  * @DAMON_OPS_PADDR:	Monitoring operations for the physical address space
388  * @NR_DAMON_OPS:	Number of monitoring operations implementations
389  */
390 enum damon_ops_id {
391 	DAMON_OPS_VADDR,
392 	DAMON_OPS_FVADDR,
393 	DAMON_OPS_PADDR,
394 	NR_DAMON_OPS,
395 };
396 
397 struct damon_ctx;
398 
399 /**
400  * struct damon_operations - Monitoring operations for given use cases.
401  *
402  * @id:				Identifier of this operations set.
403  * @init:			Initialize operations-related data structures.
404  * @update:			Update operations-related data structures.
405  * @prepare_access_checks:	Prepare next access check of target regions.
406  * @check_accesses:		Check the accesses to target regions.
407  * @reset_aggregated:		Reset aggregated accesses monitoring results.
408  * @get_scheme_score:		Get the score of a region for a scheme.
409  * @apply_scheme:		Apply a DAMON-based operation scheme.
410  * @target_valid:		Determine if the target is valid.
411  * @cleanup:			Clean up the context.
412  *
413  * DAMON can be extended for various address spaces and usages.  For this,
414  * users should register the low level operations for their target address
415  * space and usecase via the &damon_ctx.ops.  Then, the monitoring thread
416  * (&damon_ctx.kdamond) calls @init and @prepare_access_checks before starting
417  * the monitoring, @update after each &damon_attrs.ops_update_interval, and
418  * @check_accesses, @target_valid and @prepare_access_checks after each
419  * &damon_attrs.sample_interval.  Finally, @reset_aggregated is called after
420  * each &damon_attrs.aggr_interval.
421  *
422  * Each &struct damon_operations instance having valid @id can be registered
423  * via damon_register_ops() and selected by damon_select_ops() later.
424  * @init should initialize operations-related data structures.  For example,
425  * this could be used to construct proper monitoring target regions and link
426  * those to @damon_ctx.adaptive_targets.
427  * @update should update the operations-related data structures.  For example,
428  * this could be used to update monitoring target regions for current status.
429  * @prepare_access_checks should manipulate the monitoring regions to be
430  * prepared for the next access check.
431  * @check_accesses should check the accesses to each region that made after the
432  * last preparation and update the number of observed accesses of each region.
433  * It should also return max number of observed accesses that made as a result
434  * of its update.  The value will be used for regions adjustment threshold.
435  * @reset_aggregated should reset the access monitoring results that aggregated
436  * by @check_accesses.
437  * @get_scheme_score should return the priority score of a region for a scheme
438  * as an integer in [0, &DAMOS_MAX_SCORE].
439  * @apply_scheme is called from @kdamond when a region for user provided
440  * DAMON-based operation scheme is found.  It should apply the scheme's action
441  * to the region and return bytes of the region that the action is successfully
442  * applied.
443  * @target_valid should check whether the target is still valid for the
444  * monitoring.
445  * @cleanup is called from @kdamond just before its termination.
446  */
447 struct damon_operations {
448 	enum damon_ops_id id;
449 	void (*init)(struct damon_ctx *context);
450 	void (*update)(struct damon_ctx *context);
451 	void (*prepare_access_checks)(struct damon_ctx *context);
452 	unsigned int (*check_accesses)(struct damon_ctx *context);
453 	void (*reset_aggregated)(struct damon_ctx *context);
454 	int (*get_scheme_score)(struct damon_ctx *context,
455 			struct damon_target *t, struct damon_region *r,
456 			struct damos *scheme);
457 	unsigned long (*apply_scheme)(struct damon_ctx *context,
458 			struct damon_target *t, struct damon_region *r,
459 			struct damos *scheme);
460 	bool (*target_valid)(struct damon_target *t);
461 	void (*cleanup)(struct damon_ctx *context);
462 };
463 
464 /**
465  * struct damon_callback - Monitoring events notification callbacks.
466  *
467  * @before_start:	Called before starting the monitoring.
468  * @after_wmarks_check:	Called after each schemes' watermarks check.
469  * @after_sampling:	Called after each sampling.
470  * @after_aggregation:	Called after each aggregation.
471  * @before_damos_apply:	Called before applying DAMOS action.
472  * @before_terminate:	Called before terminating the monitoring.
473  * @private:		User private data.
474  *
475  * The monitoring thread (&damon_ctx.kdamond) calls @before_start and
476  * @before_terminate just before starting and finishing the monitoring,
477  * respectively.  Therefore, those are good places for installing and cleaning
478  * @private.
479  *
480  * The monitoring thread calls @after_wmarks_check after each DAMON-based
481  * operation schemes' watermarks check.  If users need to make changes to the
482  * attributes of the monitoring context while it's deactivated due to the
483  * watermarks, this is the good place to do.
484  *
485  * The monitoring thread calls @after_sampling and @after_aggregation for each
486  * of the sampling intervals and aggregation intervals, respectively.
487  * Therefore, users can safely access the monitoring results without additional
488  * protection.  For the reason, users are recommended to use these callback for
489  * the accesses to the results.
490  *
491  * If any callback returns non-zero, monitoring stops.
492  */
493 struct damon_callback {
494 	void *private;
495 
496 	int (*before_start)(struct damon_ctx *context);
497 	int (*after_wmarks_check)(struct damon_ctx *context);
498 	int (*after_sampling)(struct damon_ctx *context);
499 	int (*after_aggregation)(struct damon_ctx *context);
500 	int (*before_damos_apply)(struct damon_ctx *context,
501 			struct damon_target *target,
502 			struct damon_region *region,
503 			struct damos *scheme);
504 	void (*before_terminate)(struct damon_ctx *context);
505 };
506 
507 /**
508  * struct damon_attrs - Monitoring attributes for accuracy/overhead control.
509  *
510  * @sample_interval:		The time between access samplings.
511  * @aggr_interval:		The time between monitor results aggregations.
512  * @ops_update_interval:	The time between monitoring operations updates.
513  * @min_nr_regions:		The minimum number of adaptive monitoring
514  *				regions.
515  * @max_nr_regions:		The maximum number of adaptive monitoring
516  *				regions.
517  *
518  * For each @sample_interval, DAMON checks whether each region is accessed or
519  * not during the last @sample_interval.  If such access is found, DAMON
520  * aggregates the information by increasing &damon_region->nr_accesses for
521  * @aggr_interval time.  For each @aggr_interval, the count is reset.  DAMON
522  * also checks whether the target memory regions need update (e.g., by
523  * ``mmap()`` calls from the application, in case of virtual memory monitoring)
524  * and applies the changes for each @ops_update_interval.  All time intervals
525  * are in micro-seconds.  Please refer to &struct damon_operations and &struct
526  * damon_callback for more detail.
527  */
528 struct damon_attrs {
529 	unsigned long sample_interval;
530 	unsigned long aggr_interval;
531 	unsigned long ops_update_interval;
532 	unsigned long min_nr_regions;
533 	unsigned long max_nr_regions;
534 };
535 
536 /**
537  * struct damon_ctx - Represents a context for each monitoring.  This is the
538  * main interface that allows users to set the attributes and get the results
539  * of the monitoring.
540  *
541  * @attrs:		Monitoring attributes for accuracy/overhead control.
542  * @kdamond:		Kernel thread who does the monitoring.
543  * @kdamond_lock:	Mutex for the synchronizations with @kdamond.
544  *
545  * For each monitoring context, one kernel thread for the monitoring is
546  * created.  The pointer to the thread is stored in @kdamond.
547  *
548  * Once started, the monitoring thread runs until explicitly required to be
549  * terminated or every monitoring target is invalid.  The validity of the
550  * targets is checked via the &damon_operations.target_valid of @ops.  The
551  * termination can also be explicitly requested by calling damon_stop().
552  * The thread sets @kdamond to NULL when it terminates. Therefore, users can
553  * know whether the monitoring is ongoing or terminated by reading @kdamond.
554  * Reads and writes to @kdamond from outside of the monitoring thread must
555  * be protected by @kdamond_lock.
556  *
557  * Note that the monitoring thread protects only @kdamond via @kdamond_lock.
558  * Accesses to other fields must be protected by themselves.
559  *
560  * @ops:	Set of monitoring operations for given use cases.
561  * @callback:	Set of callbacks for monitoring events notifications.
562  *
563  * @adaptive_targets:	Head of monitoring targets (&damon_target) list.
564  * @schemes:		Head of schemes (&damos) list.
565  */
566 struct damon_ctx {
567 	struct damon_attrs attrs;
568 
569 /* private: internal use only */
570 	/* number of sample intervals that passed since this context started */
571 	unsigned long passed_sample_intervals;
572 	/*
573 	 * number of sample intervals that should be passed before next
574 	 * aggregation
575 	 */
576 	unsigned long next_aggregation_sis;
577 	/*
578 	 * number of sample intervals that should be passed before next ops
579 	 * update
580 	 */
581 	unsigned long next_ops_update_sis;
582 	/* for waiting until the execution of the kdamond_fn is started */
583 	struct completion kdamond_started;
584 
585 /* public: */
586 	struct task_struct *kdamond;
587 	struct mutex kdamond_lock;
588 
589 	struct damon_operations ops;
590 	struct damon_callback callback;
591 
592 	struct list_head adaptive_targets;
593 	struct list_head schemes;
594 };
595 
596 static inline struct damon_region *damon_next_region(struct damon_region *r)
597 {
598 	return container_of(r->list.next, struct damon_region, list);
599 }
600 
601 static inline struct damon_region *damon_prev_region(struct damon_region *r)
602 {
603 	return container_of(r->list.prev, struct damon_region, list);
604 }
605 
606 static inline struct damon_region *damon_last_region(struct damon_target *t)
607 {
608 	return list_last_entry(&t->regions_list, struct damon_region, list);
609 }
610 
611 static inline struct damon_region *damon_first_region(struct damon_target *t)
612 {
613 	return list_first_entry(&t->regions_list, struct damon_region, list);
614 }
615 
616 static inline unsigned long damon_sz_region(struct damon_region *r)
617 {
618 	return r->ar.end - r->ar.start;
619 }
620 
621 
622 #define damon_for_each_region(r, t) \
623 	list_for_each_entry(r, &t->regions_list, list)
624 
625 #define damon_for_each_region_from(r, t) \
626 	list_for_each_entry_from(r, &t->regions_list, list)
627 
628 #define damon_for_each_region_safe(r, next, t) \
629 	list_for_each_entry_safe(r, next, &t->regions_list, list)
630 
631 #define damon_for_each_target(t, ctx) \
632 	list_for_each_entry(t, &(ctx)->adaptive_targets, list)
633 
634 #define damon_for_each_target_safe(t, next, ctx)	\
635 	list_for_each_entry_safe(t, next, &(ctx)->adaptive_targets, list)
636 
637 #define damon_for_each_scheme(s, ctx) \
638 	list_for_each_entry(s, &(ctx)->schemes, list)
639 
640 #define damon_for_each_scheme_safe(s, next, ctx) \
641 	list_for_each_entry_safe(s, next, &(ctx)->schemes, list)
642 
643 #define damos_for_each_filter(f, scheme) \
644 	list_for_each_entry(f, &(scheme)->filters, list)
645 
646 #define damos_for_each_filter_safe(f, next, scheme) \
647 	list_for_each_entry_safe(f, next, &(scheme)->filters, list)
648 
649 #ifdef CONFIG_DAMON
650 
651 struct damon_region *damon_new_region(unsigned long start, unsigned long end);
652 
653 /*
654  * Add a region between two other regions
655  */
656 static inline void damon_insert_region(struct damon_region *r,
657 		struct damon_region *prev, struct damon_region *next,
658 		struct damon_target *t)
659 {
660 	__list_add(&r->list, &prev->list, &next->list);
661 	t->nr_regions++;
662 }
663 
664 void damon_add_region(struct damon_region *r, struct damon_target *t);
665 void damon_destroy_region(struct damon_region *r, struct damon_target *t);
666 int damon_set_regions(struct damon_target *t, struct damon_addr_range *ranges,
667 		unsigned int nr_ranges);
668 void damon_update_region_access_rate(struct damon_region *r, bool accessed,
669 		struct damon_attrs *attrs);
670 
671 struct damos_filter *damos_new_filter(enum damos_filter_type type,
672 		bool matching);
673 void damos_add_filter(struct damos *s, struct damos_filter *f);
674 void damos_destroy_filter(struct damos_filter *f);
675 
676 struct damos *damon_new_scheme(struct damos_access_pattern *pattern,
677 			enum damos_action action,
678 			unsigned long apply_interval_us,
679 			struct damos_quota *quota,
680 			struct damos_watermarks *wmarks);
681 void damon_add_scheme(struct damon_ctx *ctx, struct damos *s);
682 void damon_destroy_scheme(struct damos *s);
683 
684 struct damon_target *damon_new_target(void);
685 void damon_add_target(struct damon_ctx *ctx, struct damon_target *t);
686 bool damon_targets_empty(struct damon_ctx *ctx);
687 void damon_free_target(struct damon_target *t);
688 void damon_destroy_target(struct damon_target *t);
689 unsigned int damon_nr_regions(struct damon_target *t);
690 
691 struct damon_ctx *damon_new_ctx(void);
692 void damon_destroy_ctx(struct damon_ctx *ctx);
693 int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs);
694 void damon_set_schemes(struct damon_ctx *ctx,
695 			struct damos **schemes, ssize_t nr_schemes);
696 int damon_nr_running_ctxs(void);
697 bool damon_is_registered_ops(enum damon_ops_id id);
698 int damon_register_ops(struct damon_operations *ops);
699 int damon_select_ops(struct damon_ctx *ctx, enum damon_ops_id id);
700 
701 static inline bool damon_target_has_pid(const struct damon_ctx *ctx)
702 {
703 	return ctx->ops.id == DAMON_OPS_VADDR || ctx->ops.id == DAMON_OPS_FVADDR;
704 }
705 
706 static inline unsigned int damon_max_nr_accesses(const struct damon_attrs *attrs)
707 {
708 	/* {aggr,sample}_interval are unsigned long, hence could overflow */
709 	return min(attrs->aggr_interval / attrs->sample_interval,
710 			(unsigned long)UINT_MAX);
711 }
712 
713 
714 int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive);
715 int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
716 
717 int damon_set_region_biggest_system_ram_default(struct damon_target *t,
718 				unsigned long *start, unsigned long *end);
719 
720 #endif	/* CONFIG_DAMON */
721 
722 #endif	/* _DAMON_H */
723