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