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/kstrtox.h>
12 #include <linux/module.h>
14 #include "modules-common.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);
53 static struct damos_quota damon_reclaim_quota = {
54 /* use up to 10 ms time, reclaim up to 128 MiB per 1 sec by default */
56 .sz = 128 * 1024 * 1024,
57 .reset_interval = 1000,
58 /* Within the quota, page out older regions first. */
60 .weight_nr_accesses = 0,
63 DEFINE_DAMON_MODULES_DAMOS_QUOTAS(damon_reclaim_quota);
65 static struct damos_watermarks damon_reclaim_wmarks = {
66 .metric = DAMOS_WMARK_FREE_MEM_RATE,
67 .interval = 5000000, /* 5 seconds */
68 .high = 500, /* 50 percent */
69 .mid = 400, /* 40 percent */
70 .low = 200, /* 20 percent */
72 DEFINE_DAMON_MODULES_WMARKS_PARAMS(damon_reclaim_wmarks);
74 static struct damon_attrs damon_reclaim_mon_attrs = {
75 .sample_interval = 5000, /* 5 ms */
76 .aggr_interval = 100000, /* 100 ms */
77 .ops_update_interval = 0,
79 .max_nr_regions = 1000,
81 DEFINE_DAMON_MODULES_MON_ATTRS_PARAMS(damon_reclaim_mon_attrs);
84 * Start of the target memory region in physical address.
86 * The start physical address of memory region that DAMON_RECLAIM will do work
87 * against. By default, biggest System RAM is used as the region.
89 static unsigned long monitor_region_start __read_mostly;
90 module_param(monitor_region_start, ulong, 0600);
93 * End of the target memory region in physical address.
95 * The end physical address of memory region that DAMON_RECLAIM will do work
96 * against. By default, biggest System RAM is used as the region.
98 static unsigned long monitor_region_end __read_mostly;
99 module_param(monitor_region_end, ulong, 0600);
102 * Skip anonymous pages reclamation.
104 * If this parameter is set as ``Y``, DAMON_RECLAIM does not reclaim anonymous
105 * pages. By default, ``N``.
107 static bool skip_anon __read_mostly;
108 module_param(skip_anon, bool, 0600);
111 * PID of the DAMON thread
113 * If DAMON_RECLAIM is enabled, this becomes the PID of the worker thread.
116 static int kdamond_pid __read_mostly = -1;
117 module_param(kdamond_pid, int, 0400);
119 static struct damos_stat damon_reclaim_stat;
120 DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_reclaim_stat,
121 reclaim_tried_regions, reclaimed_regions, quota_exceeds);
123 static struct damon_ctx *ctx;
124 static struct damon_target *target;
126 static struct damos *damon_reclaim_new_scheme(void)
128 struct damos_access_pattern pattern = {
129 /* Find regions having PAGE_SIZE or larger size */
130 .min_sz_region = PAGE_SIZE,
131 .max_sz_region = ULONG_MAX,
132 /* and not accessed at all */
133 .min_nr_accesses = 0,
134 .max_nr_accesses = 0,
135 /* for min_age or more micro-seconds */
136 .min_age_region = min_age /
137 damon_reclaim_mon_attrs.aggr_interval,
138 .max_age_region = UINT_MAX,
141 return damon_new_scheme(
143 /* page out those, as soon as found */
145 /* under the quota. */
146 &damon_reclaim_quota,
147 /* (De)activate this according to the watermarks. */
148 &damon_reclaim_wmarks);
151 static int damon_reclaim_apply_parameters(void)
153 struct damos *scheme;
154 struct damos_filter *filter;
157 err = damon_set_attrs(ctx, &damon_reclaim_mon_attrs);
161 /* Will be freed by next 'damon_set_schemes()' below */
162 scheme = damon_reclaim_new_scheme();
166 filter = damos_new_filter(DAMOS_FILTER_TYPE_ANON, true);
168 /* Will be freed by next 'damon_set_schemes()' below */
169 damon_destroy_scheme(scheme);
172 damos_add_filter(scheme, filter);
174 damon_set_schemes(ctx, &scheme, 1);
176 return damon_set_region_biggest_system_ram_default(target,
177 &monitor_region_start,
178 &monitor_region_end);
181 static int damon_reclaim_turn(bool on)
186 err = damon_stop(&ctx, 1);
192 err = damon_reclaim_apply_parameters();
196 err = damon_start(&ctx, 1, true);
199 kdamond_pid = ctx->kdamond->pid;
203 static int damon_reclaim_enabled_store(const char *val,
204 const struct kernel_param *kp)
206 bool is_enabled = enabled;
210 err = kstrtobool(val, &enable);
214 if (is_enabled == enable)
217 /* Called before init function. The function will handle this. */
221 err = damon_reclaim_turn(enable);
230 static const struct kernel_param_ops enabled_param_ops = {
231 .set = damon_reclaim_enabled_store,
232 .get = param_get_bool,
235 module_param_cb(enabled, &enabled_param_ops, &enabled, 0600);
236 MODULE_PARM_DESC(enabled,
237 "Enable or disable DAMON_RECLAIM (default: disabled)");
239 static int damon_reclaim_handle_commit_inputs(void)
246 err = damon_reclaim_apply_parameters();
247 commit_inputs = false;
251 static int damon_reclaim_after_aggregation(struct damon_ctx *c)
255 /* update the stats parameter */
256 damon_for_each_scheme(s, c)
257 damon_reclaim_stat = s->stat;
259 return damon_reclaim_handle_commit_inputs();
262 static int damon_reclaim_after_wmarks_check(struct damon_ctx *c)
264 return damon_reclaim_handle_commit_inputs();
267 static int __init damon_reclaim_init(void)
269 int err = damon_modules_new_paddr_ctx_target(&ctx, &target);
274 ctx->callback.after_wmarks_check = damon_reclaim_after_wmarks_check;
275 ctx->callback.after_aggregation = damon_reclaim_after_aggregation;
277 /* 'enabled' has set before this function, probably via command line */
279 err = damon_reclaim_turn(true);
284 module_init(damon_reclaim_init);