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