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