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;
31 module_param(enabled, bool, 0600);
34 * Time threshold for cold memory regions identification in microseconds.
36 * If a memory region is not accessed for this or longer time, DAMON_RECLAIM
37 * identifies the region as cold, and reclaims. 120 seconds by default.
39 static unsigned long min_age __read_mostly = 120000000;
40 module_param(min_age, ulong, 0600);
43 * Limit of time for trying the reclamation in milliseconds.
45 * DAMON_RECLAIM tries to use only up to this time within a time window
46 * (quota_reset_interval_ms) for trying reclamation of cold pages. This can be
47 * used for limiting CPU consumption of DAMON_RECLAIM. If the value is zero,
48 * the limit is disabled.
52 static unsigned long quota_ms __read_mostly = 10;
53 module_param(quota_ms, ulong, 0600);
56 * Limit of size of memory for the reclamation in bytes.
58 * DAMON_RECLAIM charges amount of memory which it tried to reclaim within a
59 * time window (quota_reset_interval_ms) and makes no more than this limit is
60 * tried. This can be used for limiting consumption of CPU and IO. If this
61 * value is zero, the limit is disabled.
65 static unsigned long quota_sz __read_mostly = 128 * 1024 * 1024;
66 module_param(quota_sz, ulong, 0600);
69 * The time/size quota charge reset interval in milliseconds.
71 * The charge reset interval for the quota of time (quota_ms) and size
72 * (quota_sz). That is, DAMON_RECLAIM does not try reclamation for more than
73 * quota_ms milliseconds or quota_sz bytes within quota_reset_interval_ms
76 * 1 second by default.
78 static unsigned long quota_reset_interval_ms __read_mostly = 1000;
79 module_param(quota_reset_interval_ms, ulong, 0600);
82 * The watermarks check time interval in microseconds.
84 * Minimal time to wait before checking the watermarks, when DAMON_RECLAIM is
85 * enabled but inactive due to its watermarks rule. 5 seconds by default.
87 static unsigned long wmarks_interval __read_mostly = 5000000;
88 module_param(wmarks_interval, ulong, 0600);
91 * Free memory rate (per thousand) for the high watermark.
93 * If free memory of the system in bytes per thousand bytes is higher than
94 * this, DAMON_RECLAIM becomes inactive, so it does nothing but periodically
95 * checks the watermarks. 500 (50%) by default.
97 static unsigned long wmarks_high __read_mostly = 500;
98 module_param(wmarks_high, ulong, 0600);
101 * Free memory rate (per thousand) for the middle watermark.
103 * If free memory of the system in bytes per thousand bytes is between this and
104 * the low watermark, DAMON_RECLAIM becomes active, so starts the monitoring
105 * and the reclaiming. 400 (40%) by default.
107 static unsigned long wmarks_mid __read_mostly = 400;
108 module_param(wmarks_mid, ulong, 0600);
111 * Free memory rate (per thousand) for the low watermark.
113 * If free memory of the system in bytes per thousand bytes is lower than this,
114 * DAMON_RECLAIM becomes inactive, so it does nothing but periodically checks
115 * the watermarks. In the case, the system falls back to the LRU-based page
116 * granularity reclamation logic. 200 (20%) by default.
118 static unsigned long wmarks_low __read_mostly = 200;
119 module_param(wmarks_low, ulong, 0600);
122 * Sampling interval for the monitoring in microseconds.
124 * The sampling interval of DAMON for the cold memory monitoring. Please refer
125 * to the DAMON documentation for more detail. 5 ms by default.
127 static unsigned long sample_interval __read_mostly = 5000;
128 module_param(sample_interval, ulong, 0600);
131 * Aggregation interval for the monitoring in microseconds.
133 * The aggregation interval of DAMON for the cold memory monitoring. Please
134 * refer to the DAMON documentation for more detail. 100 ms by default.
136 static unsigned long aggr_interval __read_mostly = 100000;
137 module_param(aggr_interval, ulong, 0600);
140 * Minimum number of monitoring regions.
142 * The minimal number of monitoring regions of DAMON for the cold memory
143 * monitoring. This can be used to set lower-bound of the monitoring quality.
144 * But, setting this too high could result in increased monitoring overhead.
145 * Please refer to the DAMON documentation for more detail. 10 by default.
147 static unsigned long min_nr_regions __read_mostly = 10;
148 module_param(min_nr_regions, ulong, 0600);
151 * Maximum number of monitoring regions.
153 * The maximum number of monitoring regions of DAMON for the cold memory
154 * monitoring. This can be used to set upper-bound of the monitoring overhead.
155 * However, setting this too low could result in bad monitoring quality.
156 * Please refer to the DAMON documentation for more detail. 1000 by default.
158 static unsigned long max_nr_regions __read_mostly = 1000;
159 module_param(max_nr_regions, ulong, 0600);
162 * Start of the target memory region in physical address.
164 * The start physical address of memory region that DAMON_RECLAIM will do work
165 * against. By default, biggest System RAM is used as the region.
167 static unsigned long monitor_region_start __read_mostly;
168 module_param(monitor_region_start, ulong, 0600);
171 * End of the target memory region in physical address.
173 * The end physical address of memory region that DAMON_RECLAIM will do work
174 * against. By default, biggest System RAM is used as the region.
176 static unsigned long monitor_region_end __read_mostly;
177 module_param(monitor_region_end, ulong, 0600);
180 * PID of the DAMON thread
182 * If DAMON_RECLAIM is enabled, this becomes the PID of the worker thread.
185 static int kdamond_pid __read_mostly = -1;
186 module_param(kdamond_pid, int, 0400);
188 static struct damon_ctx *ctx;
189 static struct damon_target *target;
191 struct damon_reclaim_ram_walk_arg {
196 static int walk_system_ram(struct resource *res, void *arg)
198 struct damon_reclaim_ram_walk_arg *a = arg;
200 if (a->end - a->start < res->end - res->start) {
201 a->start = res->start;
208 * Find biggest 'System RAM' resource and store its start and end address in
209 * @start and @end, respectively. If no System RAM is found, returns false.
211 static bool get_monitoring_region(unsigned long *start, unsigned long *end)
213 struct damon_reclaim_ram_walk_arg arg = {};
215 walk_system_ram_res(0, ULONG_MAX, &arg, walk_system_ram);
216 if (arg.end <= arg.start)
224 static struct damos *damon_reclaim_new_scheme(void)
226 struct damos_watermarks wmarks = {
227 .metric = DAMOS_WMARK_FREE_MEM_RATE,
228 .interval = wmarks_interval,
233 struct damos_quota quota = {
235 * Do not try reclamation for more than quota_ms milliseconds
236 * or quota_sz bytes within quota_reset_interval_ms.
240 .reset_interval = quota_reset_interval_ms,
241 /* Within the quota, page out older regions first. */
243 .weight_nr_accesses = 0,
246 struct damos *scheme = damon_new_scheme(
247 /* Find regions having PAGE_SIZE or larger size */
248 PAGE_SIZE, ULONG_MAX,
249 /* and not accessed at all */
251 /* for min_age or more micro-seconds, and */
252 min_age / aggr_interval, UINT_MAX,
253 /* page out those, as soon as found */
255 /* under the quota. */
257 /* (De)activate this according to the watermarks. */
263 static int damon_reclaim_turn(bool on)
265 struct damon_region *region;
266 struct damos *scheme;
270 err = damon_stop(&ctx, 1);
276 err = damon_set_attrs(ctx, sample_interval, aggr_interval, 0,
277 min_nr_regions, max_nr_regions);
281 if (monitor_region_start > monitor_region_end)
283 if (!monitor_region_start && !monitor_region_end &&
284 !get_monitoring_region(&monitor_region_start,
285 &monitor_region_end))
287 /* DAMON will free this on its own when finish monitoring */
288 region = damon_new_region(monitor_region_start, monitor_region_end);
291 damon_add_region(region, target);
293 /* Will be freed by 'damon_set_schemes()' below */
294 scheme = damon_reclaim_new_scheme();
297 goto free_region_out;
299 err = damon_set_schemes(ctx, &scheme, 1);
301 goto free_scheme_out;
303 err = damon_start(&ctx, 1);
305 kdamond_pid = ctx->kdamond->pid;
310 damon_destroy_scheme(scheme);
312 damon_destroy_region(region, target);
316 #define ENABLE_CHECK_INTERVAL_MS 1000
317 static struct delayed_work damon_reclaim_timer;
318 static void damon_reclaim_timer_fn(struct work_struct *work)
320 static bool last_enabled;
323 now_enabled = enabled;
324 if (last_enabled != now_enabled) {
325 if (!damon_reclaim_turn(now_enabled))
326 last_enabled = now_enabled;
328 enabled = last_enabled;
331 schedule_delayed_work(&damon_reclaim_timer,
332 msecs_to_jiffies(ENABLE_CHECK_INTERVAL_MS));
334 static DECLARE_DELAYED_WORK(damon_reclaim_timer, damon_reclaim_timer_fn);
336 static int __init damon_reclaim_init(void)
338 ctx = damon_new_ctx();
342 damon_pa_set_primitives(ctx);
344 /* 4242 means nothing but fun */
345 target = damon_new_target(4242);
347 damon_destroy_ctx(ctx);
350 damon_add_target(ctx, target);
352 schedule_delayed_work(&damon_reclaim_timer, 0);
356 module_init(damon_reclaim_init);