1 // SPDX-License-Identifier: GPL-2.0
3 * DAMON-based LRU-lists Sorting
5 * Author: SeongJae Park <sj@kernel.org>
8 #define pr_fmt(fmt) "damon-lru-sort: " fmt
10 #include <linux/damon.h>
11 #include <linux/ioport.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/workqueue.h>
16 #ifdef MODULE_PARAM_PREFIX
17 #undef MODULE_PARAM_PREFIX
19 #define MODULE_PARAM_PREFIX "damon_lru_sort."
22 * Enable or disable DAMON_LRU_SORT.
24 * You can enable DAMON_LRU_SORT by setting the value of this parameter as
25 * ``Y``. Setting it as ``N`` disables DAMON_LRU_SORT. Note that
26 * DAMON_LRU_SORT could do no real monitoring and LRU-lists sorting due to the
27 * watermarks-based activation condition. Refer to below descriptions for the
28 * watermarks parameter for this.
30 static bool enabled __read_mostly;
33 * Make DAMON_LRU_SORT reads the input parameters again, except ``enabled``.
35 * Input parameters that updated while DAMON_LRU_SORT is running are not
36 * applied by default. Once this parameter is set as ``Y``, DAMON_LRU_SORT
37 * reads values of parametrs except ``enabled`` again. Once the re-reading is
38 * done, this parameter is set as ``N``. If invalid parameters are found while
39 * the re-reading, DAMON_LRU_SORT will be disabled.
41 static bool commit_inputs __read_mostly;
42 module_param(commit_inputs, bool, 0600);
45 * Access frequency threshold for hot memory regions identification in permil.
47 * If a memory region is accessed in frequency of this or higher,
48 * DAMON_LRU_SORT identifies the region as hot, and mark it as accessed on the
49 * LRU list, so that it could not be reclaimed under memory pressure. 50% by
52 static unsigned long hot_thres_access_freq = 500;
53 module_param(hot_thres_access_freq, ulong, 0600);
56 * Time threshold for cold memory regions identification in microseconds.
58 * If a memory region is not accessed for this or longer time, DAMON_LRU_SORT
59 * identifies the region as cold, and mark it as unaccessed on the LRU list, so
60 * that it could be reclaimed first under memory pressure. 120 seconds by
63 static unsigned long cold_min_age __read_mostly = 120000000;
64 module_param(cold_min_age, ulong, 0600);
67 * Limit of time for trying the LRU lists sorting in milliseconds.
69 * DAMON_LRU_SORT tries to use only up to this time within a time window
70 * (quota_reset_interval_ms) for trying LRU lists sorting. This can be used
71 * for limiting CPU consumption of DAMON_LRU_SORT. If the value is zero, the
76 static unsigned long quota_ms __read_mostly = 10;
77 module_param(quota_ms, ulong, 0600);
80 * The time quota charge reset interval in milliseconds.
82 * The charge reset interval for the quota of time (quota_ms). That is,
83 * DAMON_LRU_SORT does not try LRU-lists sorting for more than quota_ms
84 * milliseconds or quota_sz bytes within quota_reset_interval_ms milliseconds.
86 * 1 second by default.
88 static unsigned long quota_reset_interval_ms __read_mostly = 1000;
89 module_param(quota_reset_interval_ms, ulong, 0600);
92 * The watermarks check time interval in microseconds.
94 * Minimal time to wait before checking the watermarks, when DAMON_LRU_SORT is
95 * enabled but inactive due to its watermarks rule. 5 seconds by default.
97 static unsigned long wmarks_interval __read_mostly = 5000000;
98 module_param(wmarks_interval, ulong, 0600);
101 * Free memory rate (per thousand) for the high watermark.
103 * If free memory of the system in bytes per thousand bytes is higher than
104 * this, DAMON_LRU_SORT becomes inactive, so it does nothing but periodically
105 * checks the watermarks. 200 (20%) by default.
107 static unsigned long wmarks_high __read_mostly = 200;
108 module_param(wmarks_high, ulong, 0600);
111 * Free memory rate (per thousand) for the middle watermark.
113 * If free memory of the system in bytes per thousand bytes is between this and
114 * the low watermark, DAMON_LRU_SORT becomes active, so starts the monitoring
115 * and the LRU-lists sorting. 150 (15%) by default.
117 static unsigned long wmarks_mid __read_mostly = 150;
118 module_param(wmarks_mid, ulong, 0600);
121 * Free memory rate (per thousand) for the low watermark.
123 * If free memory of the system in bytes per thousand bytes is lower than this,
124 * DAMON_LRU_SORT becomes inactive, so it does nothing but periodically checks
125 * the watermarks. 50 (5%) by default.
127 static unsigned long wmarks_low __read_mostly = 50;
128 module_param(wmarks_low, ulong, 0600);
131 * Sampling interval for the monitoring in microseconds.
133 * The sampling interval of DAMON for the hot/cold memory monitoring. Please
134 * refer to the DAMON documentation for more detail. 5 ms by default.
136 static unsigned long sample_interval __read_mostly = 5000;
137 module_param(sample_interval, ulong, 0600);
140 * Aggregation interval for the monitoring in microseconds.
142 * The aggregation interval of DAMON for the hot/cold memory monitoring.
143 * Please refer to the DAMON documentation for more detail. 100 ms by default.
145 static unsigned long aggr_interval __read_mostly = 100000;
146 module_param(aggr_interval, ulong, 0600);
149 * Minimum number of monitoring regions.
151 * The minimal number of monitoring regions of DAMON for the hot/cold memory
152 * monitoring. This can be used to set lower-bound of the monitoring quality.
153 * But, setting this too high could result in increased monitoring overhead.
154 * Please refer to the DAMON documentation for more detail. 10 by default.
156 static unsigned long min_nr_regions __read_mostly = 10;
157 module_param(min_nr_regions, ulong, 0600);
160 * Maximum number of monitoring regions.
162 * The maximum number of monitoring regions of DAMON for the hot/cold memory
163 * monitoring. This can be used to set upper-bound of the monitoring overhead.
164 * However, setting this too low could result in bad monitoring quality.
165 * Please refer to the DAMON documentation for more detail. 1000 by default.
167 static unsigned long max_nr_regions __read_mostly = 1000;
168 module_param(max_nr_regions, ulong, 0600);
171 * Start of the target memory region in physical address.
173 * The start physical address of memory region that DAMON_LRU_SORT will do work
174 * against. By default, biggest System RAM is used as the region.
176 static unsigned long monitor_region_start __read_mostly;
177 module_param(monitor_region_start, ulong, 0600);
180 * End of the target memory region in physical address.
182 * The end physical address of memory region that DAMON_LRU_SORT will do work
183 * against. By default, biggest System RAM is used as the region.
185 static unsigned long monitor_region_end __read_mostly;
186 module_param(monitor_region_end, ulong, 0600);
189 * PID of the DAMON thread
191 * If DAMON_LRU_SORT is enabled, this becomes the PID of the worker thread.
194 static int kdamond_pid __read_mostly = -1;
195 module_param(kdamond_pid, int, 0400);
198 * Number of hot memory regions that tried to be LRU-sorted.
200 static unsigned long nr_lru_sort_tried_hot_regions __read_mostly;
201 module_param(nr_lru_sort_tried_hot_regions, ulong, 0400);
204 * Total bytes of hot memory regions that tried to be LRU-sorted.
206 static unsigned long bytes_lru_sort_tried_hot_regions __read_mostly;
207 module_param(bytes_lru_sort_tried_hot_regions, ulong, 0400);
210 * Number of hot memory regions that successfully be LRU-sorted.
212 static unsigned long nr_lru_sorted_hot_regions __read_mostly;
213 module_param(nr_lru_sorted_hot_regions, ulong, 0400);
216 * Total bytes of hot memory regions that successfully be LRU-sorted.
218 static unsigned long bytes_lru_sorted_hot_regions __read_mostly;
219 module_param(bytes_lru_sorted_hot_regions, ulong, 0400);
222 * Number of times that the time quota limit for hot regions have exceeded
224 static unsigned long nr_hot_quota_exceeds __read_mostly;
225 module_param(nr_hot_quota_exceeds, ulong, 0400);
228 * Number of cold memory regions that tried to be LRU-sorted.
230 static unsigned long nr_lru_sort_tried_cold_regions __read_mostly;
231 module_param(nr_lru_sort_tried_cold_regions, ulong, 0400);
234 * Total bytes of cold memory regions that tried to be LRU-sorted.
236 static unsigned long bytes_lru_sort_tried_cold_regions __read_mostly;
237 module_param(bytes_lru_sort_tried_cold_regions, ulong, 0400);
240 * Number of cold memory regions that successfully be LRU-sorted.
242 static unsigned long nr_lru_sorted_cold_regions __read_mostly;
243 module_param(nr_lru_sorted_cold_regions, ulong, 0400);
246 * Total bytes of cold memory regions that successfully be LRU-sorted.
248 static unsigned long bytes_lru_sorted_cold_regions __read_mostly;
249 module_param(bytes_lru_sorted_cold_regions, ulong, 0400);
252 * Number of times that the time quota limit for cold regions have exceeded
254 static unsigned long nr_cold_quota_exceeds __read_mostly;
255 module_param(nr_cold_quota_exceeds, ulong, 0400);
257 static struct damon_ctx *ctx;
258 static struct damon_target *target;
260 struct damon_lru_sort_ram_walk_arg {
265 static int walk_system_ram(struct resource *res, void *arg)
267 struct damon_lru_sort_ram_walk_arg *a = arg;
269 if (a->end - a->start < resource_size(res)) {
270 a->start = res->start;
277 * Find biggest 'System RAM' resource and store its start and end address in
278 * @start and @end, respectively. If no System RAM is found, returns false.
280 static bool get_monitoring_region(unsigned long *start, unsigned long *end)
282 struct damon_lru_sort_ram_walk_arg arg = {};
284 walk_system_ram_res(0, ULONG_MAX, &arg, walk_system_ram);
285 if (arg.end <= arg.start)
293 /* Create a DAMON-based operation scheme for hot memory regions */
294 static struct damos *damon_lru_sort_new_hot_scheme(unsigned int hot_thres)
296 struct damos_watermarks wmarks = {
297 .metric = DAMOS_WMARK_FREE_MEM_RATE,
298 .interval = wmarks_interval,
303 struct damos_quota quota = {
305 * Do not try LRU-lists sorting of hot pages for more than half
306 * of quota_ms milliseconds within quota_reset_interval_ms.
310 .reset_interval = quota_reset_interval_ms,
311 /* Within the quota, mark hotter regions accessed first. */
313 .weight_nr_accesses = 1,
316 struct damos *scheme = damon_new_scheme(
317 /* Find regions having PAGE_SIZE or larger size */
318 PAGE_SIZE, ULONG_MAX,
319 /* and accessed for more than the threshold */
321 /* no matter its age */
323 /* prioritize those on LRU lists, as soon as found */
325 /* under the quota. */
327 /* (De)activate this according to the watermarks. */
333 /* Create a DAMON-based operation scheme for cold memory regions */
334 static struct damos *damon_lru_sort_new_cold_scheme(unsigned int cold_thres)
336 struct damos_watermarks wmarks = {
337 .metric = DAMOS_WMARK_FREE_MEM_RATE,
338 .interval = wmarks_interval,
343 struct damos_quota quota = {
345 * Do not try LRU-lists sorting of cold pages for more than
346 * half of quota_ms milliseconds within
347 * quota_reset_interval_ms.
351 .reset_interval = quota_reset_interval_ms,
352 /* Within the quota, mark colder regions not accessed first. */
354 .weight_nr_accesses = 0,
357 struct damos *scheme = damon_new_scheme(
358 /* Find regions having PAGE_SIZE or larger size */
359 PAGE_SIZE, ULONG_MAX,
360 /* and not accessed at all */
362 /* for cold_thres or more micro-seconds, and */
363 cold_thres, UINT_MAX,
364 /* mark those as not accessed, as soon as found */
366 /* under the quota. */
368 /* (De)activate this according to the watermarks. */
374 static int damon_lru_sort_apply_parameters(void)
376 struct damos *scheme, *next_scheme;
377 struct damon_addr_range addr_range;
378 unsigned int hot_thres, cold_thres;
381 err = damon_set_attrs(ctx, sample_interval, aggr_interval, 0,
382 min_nr_regions, max_nr_regions);
386 /* free previously set schemes */
387 damon_for_each_scheme_safe(scheme, next_scheme, ctx)
388 damon_destroy_scheme(scheme);
390 /* aggr_interval / sample_interval is the maximum nr_accesses */
391 hot_thres = aggr_interval / sample_interval * hot_thres_access_freq /
393 scheme = damon_lru_sort_new_hot_scheme(hot_thres);
396 damon_add_scheme(ctx, scheme);
398 cold_thres = cold_min_age / aggr_interval;
399 scheme = damon_lru_sort_new_cold_scheme(cold_thres);
402 damon_add_scheme(ctx, scheme);
404 if (monitor_region_start > monitor_region_end)
406 if (!monitor_region_start && !monitor_region_end &&
407 !get_monitoring_region(&monitor_region_start,
408 &monitor_region_end))
410 addr_range.start = monitor_region_start;
411 addr_range.end = monitor_region_end;
412 return damon_set_regions(target, &addr_range, 1);
415 static int damon_lru_sort_turn(bool on)
420 err = damon_stop(&ctx, 1);
426 err = damon_lru_sort_apply_parameters();
430 err = damon_start(&ctx, 1, true);
433 kdamond_pid = ctx->kdamond->pid;
437 static struct delayed_work damon_lru_sort_timer;
438 static void damon_lru_sort_timer_fn(struct work_struct *work)
440 static bool last_enabled;
443 now_enabled = enabled;
444 if (last_enabled != now_enabled) {
445 if (!damon_lru_sort_turn(now_enabled))
446 last_enabled = now_enabled;
448 enabled = last_enabled;
451 static DECLARE_DELAYED_WORK(damon_lru_sort_timer, damon_lru_sort_timer_fn);
453 static bool damon_lru_sort_initialized;
455 static int damon_lru_sort_enabled_store(const char *val,
456 const struct kernel_param *kp)
458 int rc = param_set_bool(val, kp);
463 if (!damon_lru_sort_initialized)
466 schedule_delayed_work(&damon_lru_sort_timer, 0);
471 static const struct kernel_param_ops enabled_param_ops = {
472 .set = damon_lru_sort_enabled_store,
473 .get = param_get_bool,
476 module_param_cb(enabled, &enabled_param_ops, &enabled, 0600);
477 MODULE_PARM_DESC(enabled,
478 "Enable or disable DAMON_LRU_SORT (default: disabled)");
480 static int damon_lru_sort_handle_commit_inputs(void)
487 err = damon_lru_sort_apply_parameters();
488 commit_inputs = false;
492 static int damon_lru_sort_after_aggregation(struct damon_ctx *c)
496 /* update the stats parameter */
497 damon_for_each_scheme(s, c) {
498 if (s->action == DAMOS_LRU_PRIO) {
499 nr_lru_sort_tried_hot_regions = s->stat.nr_tried;
500 bytes_lru_sort_tried_hot_regions = s->stat.sz_tried;
501 nr_lru_sorted_hot_regions = s->stat.nr_applied;
502 bytes_lru_sorted_hot_regions = s->stat.sz_applied;
503 nr_hot_quota_exceeds = s->stat.qt_exceeds;
504 } else if (s->action == DAMOS_LRU_DEPRIO) {
505 nr_lru_sort_tried_cold_regions = s->stat.nr_tried;
506 bytes_lru_sort_tried_cold_regions = s->stat.sz_tried;
507 nr_lru_sorted_cold_regions = s->stat.nr_applied;
508 bytes_lru_sorted_cold_regions = s->stat.sz_applied;
509 nr_cold_quota_exceeds = s->stat.qt_exceeds;
513 return damon_lru_sort_handle_commit_inputs();
516 static int damon_lru_sort_after_wmarks_check(struct damon_ctx *c)
518 return damon_lru_sort_handle_commit_inputs();
521 static int __init damon_lru_sort_init(void)
523 ctx = damon_new_ctx();
527 if (damon_select_ops(ctx, DAMON_OPS_PADDR)) {
528 damon_destroy_ctx(ctx);
532 ctx->callback.after_wmarks_check = damon_lru_sort_after_wmarks_check;
533 ctx->callback.after_aggregation = damon_lru_sort_after_aggregation;
535 target = damon_new_target();
537 damon_destroy_ctx(ctx);
540 damon_add_target(ctx, target);
542 schedule_delayed_work(&damon_lru_sort_timer, 0);
544 damon_lru_sort_initialized = true;
548 module_init(damon_lru_sort_init);