1 // SPDX-License-Identifier: GPL-2.0
3 * DAMON-based page reclamation
5 * Author: SeongJae Park <sj@kernel.org>
8 #define pr_fmt(fmt) "damon-reclaim: " 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_reclaim."
22 * Enable or disable DAMON_RECLAIM.
24 * You can enable DAMON_RCLAIM by setting the value of this parameter as ``Y``.
25 * Setting it as ``N`` disables DAMON_RECLAIM. Note that DAMON_RECLAIM could
26 * do no real monitoring and reclamation due to the watermarks-based activation
27 * condition. Refer to below descriptions for the watermarks parameter for
30 static bool enabled __read_mostly;
33 * Make DAMON_RECLAIM reads the input parameters again, except ``enabled``.
35 * Input parameters that updated while DAMON_RECLAIM is running are not applied
36 * by default. Once this parameter is set as ``Y``, DAMON_RECLAIM reads values
37 * of parametrs except ``enabled`` again. Once the re-reading is done, this
38 * parameter is set as ``N``. If invalid parameters are found while the
39 * re-reading, DAMON_RECLAIM will be disabled.
41 static bool commit_inputs __read_mostly;
42 module_param(commit_inputs, bool, 0600);
45 * Time threshold for cold memory regions identification in microseconds.
47 * If a memory region is not accessed for this or longer time, DAMON_RECLAIM
48 * identifies the region as cold, and reclaims. 120 seconds by default.
50 static unsigned long min_age __read_mostly = 120000000;
51 module_param(min_age, ulong, 0600);
54 * Limit of time for trying the reclamation in milliseconds.
56 * DAMON_RECLAIM tries to use only up to this time within a time window
57 * (quota_reset_interval_ms) for trying reclamation of cold pages. This can be
58 * used for limiting CPU consumption of DAMON_RECLAIM. If the value is zero,
59 * the limit is disabled.
63 static unsigned long quota_ms __read_mostly = 10;
64 module_param(quota_ms, ulong, 0600);
67 * Limit of size of memory for the reclamation in bytes.
69 * DAMON_RECLAIM charges amount of memory which it tried to reclaim within a
70 * time window (quota_reset_interval_ms) and makes no more than this limit is
71 * tried. This can be used for limiting consumption of CPU and IO. If this
72 * value is zero, the limit is disabled.
76 static unsigned long quota_sz __read_mostly = 128 * 1024 * 1024;
77 module_param(quota_sz, ulong, 0600);
80 * The time/size quota charge reset interval in milliseconds.
82 * The charge reset interval for the quota of time (quota_ms) and size
83 * (quota_sz). That is, DAMON_RECLAIM does not try reclamation for more than
84 * quota_ms milliseconds or quota_sz bytes within quota_reset_interval_ms
87 * 1 second by default.
89 static unsigned long quota_reset_interval_ms __read_mostly = 1000;
90 module_param(quota_reset_interval_ms, ulong, 0600);
93 * The watermarks check time interval in microseconds.
95 * Minimal time to wait before checking the watermarks, when DAMON_RECLAIM is
96 * enabled but inactive due to its watermarks rule. 5 seconds by default.
98 static unsigned long wmarks_interval __read_mostly = 5000000;
99 module_param(wmarks_interval, ulong, 0600);
102 * Free memory rate (per thousand) for the high watermark.
104 * If free memory of the system in bytes per thousand bytes is higher than
105 * this, DAMON_RECLAIM becomes inactive, so it does nothing but periodically
106 * checks the watermarks. 500 (50%) by default.
108 static unsigned long wmarks_high __read_mostly = 500;
109 module_param(wmarks_high, ulong, 0600);
112 * Free memory rate (per thousand) for the middle watermark.
114 * If free memory of the system in bytes per thousand bytes is between this and
115 * the low watermark, DAMON_RECLAIM becomes active, so starts the monitoring
116 * and the reclaiming. 400 (40%) by default.
118 static unsigned long wmarks_mid __read_mostly = 400;
119 module_param(wmarks_mid, ulong, 0600);
122 * Free memory rate (per thousand) for the low watermark.
124 * If free memory of the system in bytes per thousand bytes is lower than this,
125 * DAMON_RECLAIM becomes inactive, so it does nothing but periodically checks
126 * the watermarks. In the case, the system falls back to the LRU-based page
127 * granularity reclamation logic. 200 (20%) by default.
129 static unsigned long wmarks_low __read_mostly = 200;
130 module_param(wmarks_low, ulong, 0600);
133 * Sampling interval for the monitoring in microseconds.
135 * The sampling interval of DAMON for the cold memory monitoring. Please refer
136 * to the DAMON documentation for more detail. 5 ms by default.
138 static unsigned long sample_interval __read_mostly = 5000;
139 module_param(sample_interval, ulong, 0600);
142 * Aggregation interval for the monitoring in microseconds.
144 * The aggregation interval of DAMON for the cold memory monitoring. Please
145 * refer to the DAMON documentation for more detail. 100 ms by default.
147 static unsigned long aggr_interval __read_mostly = 100000;
148 module_param(aggr_interval, ulong, 0600);
151 * Minimum number of monitoring regions.
153 * The minimal number of monitoring regions of DAMON for the cold memory
154 * monitoring. This can be used to set lower-bound of the monitoring quality.
155 * But, setting this too high could result in increased monitoring overhead.
156 * Please refer to the DAMON documentation for more detail. 10 by default.
158 static unsigned long min_nr_regions __read_mostly = 10;
159 module_param(min_nr_regions, ulong, 0600);
162 * Maximum number of monitoring regions.
164 * The maximum number of monitoring regions of DAMON for the cold memory
165 * monitoring. This can be used to set upper-bound of the monitoring overhead.
166 * However, setting this too low could result in bad monitoring quality.
167 * Please refer to the DAMON documentation for more detail. 1000 by default.
169 static unsigned long max_nr_regions __read_mostly = 1000;
170 module_param(max_nr_regions, ulong, 0600);
173 * Start of the target memory region in physical address.
175 * The start physical address of memory region that DAMON_RECLAIM will do work
176 * against. By default, biggest System RAM is used as the region.
178 static unsigned long monitor_region_start __read_mostly;
179 module_param(monitor_region_start, ulong, 0600);
182 * End of the target memory region in physical address.
184 * The end physical address of memory region that DAMON_RECLAIM will do work
185 * against. By default, biggest System RAM is used as the region.
187 static unsigned long monitor_region_end __read_mostly;
188 module_param(monitor_region_end, ulong, 0600);
191 * PID of the DAMON thread
193 * If DAMON_RECLAIM is enabled, this becomes the PID of the worker thread.
196 static int kdamond_pid __read_mostly = -1;
197 module_param(kdamond_pid, int, 0400);
200 * Number of memory regions that tried to be reclaimed.
202 static unsigned long nr_reclaim_tried_regions __read_mostly;
203 module_param(nr_reclaim_tried_regions, ulong, 0400);
206 * Total bytes of memory regions that tried to be reclaimed.
208 static unsigned long bytes_reclaim_tried_regions __read_mostly;
209 module_param(bytes_reclaim_tried_regions, ulong, 0400);
212 * Number of memory regions that successfully be reclaimed.
214 static unsigned long nr_reclaimed_regions __read_mostly;
215 module_param(nr_reclaimed_regions, ulong, 0400);
218 * Total bytes of memory regions that successfully be reclaimed.
220 static unsigned long bytes_reclaimed_regions __read_mostly;
221 module_param(bytes_reclaimed_regions, ulong, 0400);
224 * Number of times that the time/space quota limits have exceeded
226 static unsigned long nr_quota_exceeds __read_mostly;
227 module_param(nr_quota_exceeds, ulong, 0400);
229 static struct damon_ctx *ctx;
230 static struct damon_target *target;
232 struct damon_reclaim_ram_walk_arg {
237 static int walk_system_ram(struct resource *res, void *arg)
239 struct damon_reclaim_ram_walk_arg *a = arg;
241 if (a->end - a->start < resource_size(res)) {
242 a->start = res->start;
249 * Find biggest 'System RAM' resource and store its start and end address in
250 * @start and @end, respectively. If no System RAM is found, returns false.
252 static bool get_monitoring_region(unsigned long *start, unsigned long *end)
254 struct damon_reclaim_ram_walk_arg arg = {};
256 walk_system_ram_res(0, ULONG_MAX, &arg, walk_system_ram);
257 if (arg.end <= arg.start)
265 static struct damos *damon_reclaim_new_scheme(void)
267 struct damos_watermarks wmarks = {
268 .metric = DAMOS_WMARK_FREE_MEM_RATE,
269 .interval = wmarks_interval,
274 struct damos_quota quota = {
276 * Do not try reclamation for more than quota_ms milliseconds
277 * or quota_sz bytes within quota_reset_interval_ms.
281 .reset_interval = quota_reset_interval_ms,
282 /* Within the quota, page out older regions first. */
284 .weight_nr_accesses = 0,
287 struct damos *scheme = damon_new_scheme(
288 /* Find regions having PAGE_SIZE or larger size */
289 PAGE_SIZE, ULONG_MAX,
290 /* and not accessed at all */
292 /* for min_age or more micro-seconds, and */
293 min_age / aggr_interval, UINT_MAX,
294 /* page out those, as soon as found */
296 /* under the quota. */
298 /* (De)activate this according to the watermarks. */
304 static int damon_reclaim_apply_parameters(void)
306 struct damos *scheme;
307 struct damon_addr_range addr_range;
310 err = damon_set_attrs(ctx, sample_interval, aggr_interval, 0,
311 min_nr_regions, max_nr_regions);
315 /* Will be freed by next 'damon_set_schemes()' below */
316 scheme = damon_reclaim_new_scheme();
319 err = damon_set_schemes(ctx, &scheme, 1);
323 if (monitor_region_start > monitor_region_end)
325 if (!monitor_region_start && !monitor_region_end &&
326 !get_monitoring_region(&monitor_region_start,
327 &monitor_region_end))
329 addr_range.start = monitor_region_start;
330 addr_range.end = monitor_region_end;
331 return damon_set_regions(target, &addr_range, 1);
334 static int damon_reclaim_turn(bool on)
339 err = damon_stop(&ctx, 1);
345 err = damon_reclaim_apply_parameters();
349 err = damon_start(&ctx, 1, true);
352 kdamond_pid = ctx->kdamond->pid;
356 static struct delayed_work damon_reclaim_timer;
357 static void damon_reclaim_timer_fn(struct work_struct *work)
359 static bool last_enabled;
362 now_enabled = enabled;
363 if (last_enabled != now_enabled) {
364 if (!damon_reclaim_turn(now_enabled))
365 last_enabled = now_enabled;
367 enabled = last_enabled;
370 static DECLARE_DELAYED_WORK(damon_reclaim_timer, damon_reclaim_timer_fn);
372 static bool damon_reclaim_initialized;
374 static int enabled_store(const char *val,
375 const struct kernel_param *kp)
377 int rc = param_set_bool(val, kp);
382 /* system_wq might not initialized yet */
383 if (!damon_reclaim_initialized)
386 schedule_delayed_work(&damon_reclaim_timer, 0);
390 static const struct kernel_param_ops enabled_param_ops = {
391 .set = enabled_store,
392 .get = param_get_bool,
395 module_param_cb(enabled, &enabled_param_ops, &enabled, 0600);
396 MODULE_PARM_DESC(enabled,
397 "Enable or disable DAMON_RECLAIM (default: disabled)");
399 static int damon_reclaim_handle_commit_inputs(void)
406 err = damon_reclaim_apply_parameters();
407 commit_inputs = false;
411 static int damon_reclaim_after_aggregation(struct damon_ctx *c)
415 /* update the stats parameter */
416 damon_for_each_scheme(s, c) {
417 nr_reclaim_tried_regions = s->stat.nr_tried;
418 bytes_reclaim_tried_regions = s->stat.sz_tried;
419 nr_reclaimed_regions = s->stat.nr_applied;
420 bytes_reclaimed_regions = s->stat.sz_applied;
421 nr_quota_exceeds = s->stat.qt_exceeds;
424 return damon_reclaim_handle_commit_inputs();
427 static int damon_reclaim_after_wmarks_check(struct damon_ctx *c)
429 return damon_reclaim_handle_commit_inputs();
432 static int __init damon_reclaim_init(void)
434 ctx = damon_new_ctx();
438 if (damon_select_ops(ctx, DAMON_OPS_PADDR))
441 ctx->callback.after_wmarks_check = damon_reclaim_after_wmarks_check;
442 ctx->callback.after_aggregation = damon_reclaim_after_aggregation;
444 target = damon_new_target();
446 damon_destroy_ctx(ctx);
449 damon_add_target(ctx, target);
451 schedule_delayed_work(&damon_reclaim_timer, 0);
453 damon_reclaim_initialized = true;
457 module_init(damon_reclaim_init);