xref: /openbmc/linux/mm/damon/lru_sort.c (revision 3c4441b2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * DAMON-based LRU-lists Sorting
4  *
5  * Author: SeongJae Park <sj@kernel.org>
6  */
7 
8 #define pr_fmt(fmt) "damon-lru-sort: " fmt
9 
10 #include <linux/damon.h>
11 #include <linux/kstrtox.h>
12 #include <linux/module.h>
13 
14 #include "modules-common.h"
15 
16 #ifdef MODULE_PARAM_PREFIX
17 #undef MODULE_PARAM_PREFIX
18 #endif
19 #define MODULE_PARAM_PREFIX "damon_lru_sort."
20 
21 /*
22  * Enable or disable DAMON_LRU_SORT.
23  *
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.
29  */
30 static bool enabled __read_mostly;
31 
32 /*
33  * Make DAMON_LRU_SORT reads the input parameters again, except ``enabled``.
34  *
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.
40  */
41 static bool commit_inputs __read_mostly;
42 module_param(commit_inputs, bool, 0600);
43 
44 /*
45  * Access frequency threshold for hot memory regions identification in permil.
46  *
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
50  * default.
51  */
52 static unsigned long hot_thres_access_freq = 500;
53 module_param(hot_thres_access_freq, ulong, 0600);
54 
55 /*
56  * Time threshold for cold memory regions identification in microseconds.
57  *
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
61  * default.
62  */
63 static unsigned long cold_min_age __read_mostly = 120000000;
64 module_param(cold_min_age, ulong, 0600);
65 
66 static struct damos_quota damon_lru_sort_quota = {
67 	/* Use up to 10 ms per 1 sec, by default */
68 	.ms = 10,
69 	.sz = 0,
70 	.reset_interval = 1000,
71 	/* Within the quota, mark hotter regions accessed first. */
72 	.weight_sz = 0,
73 	.weight_nr_accesses = 1,
74 	.weight_age = 0,
75 };
76 DEFINE_DAMON_MODULES_DAMOS_TIME_QUOTA(damon_lru_sort_quota);
77 
78 static struct damos_watermarks damon_lru_sort_wmarks = {
79 	.metric = DAMOS_WMARK_FREE_MEM_RATE,
80 	.interval = 5000000,	/* 5 seconds */
81 	.high = 200,		/* 20 percent */
82 	.mid = 150,		/* 15 percent */
83 	.low = 50,		/* 5 percent */
84 };
85 DEFINE_DAMON_MODULES_WMARKS_PARAMS(damon_lru_sort_wmarks);
86 
87 static struct damon_attrs damon_lru_sort_mon_attrs = {
88 	.sample_interval = 5000,	/* 5 ms */
89 	.aggr_interval = 100000,	/* 100 ms */
90 	.ops_update_interval = 0,
91 	.min_nr_regions = 10,
92 	.max_nr_regions = 1000,
93 };
94 DEFINE_DAMON_MODULES_MON_ATTRS_PARAMS(damon_lru_sort_mon_attrs);
95 
96 /*
97  * Start of the target memory region in physical address.
98  *
99  * The start physical address of memory region that DAMON_LRU_SORT will do work
100  * against.  By default, biggest System RAM is used as the region.
101  */
102 static unsigned long monitor_region_start __read_mostly;
103 module_param(monitor_region_start, ulong, 0600);
104 
105 /*
106  * End of the target memory region in physical address.
107  *
108  * The end physical address of memory region that DAMON_LRU_SORT will do work
109  * against.  By default, biggest System RAM is used as the region.
110  */
111 static unsigned long monitor_region_end __read_mostly;
112 module_param(monitor_region_end, ulong, 0600);
113 
114 /*
115  * PID of the DAMON thread
116  *
117  * If DAMON_LRU_SORT is enabled, this becomes the PID of the worker thread.
118  * Else, -1.
119  */
120 static int kdamond_pid __read_mostly = -1;
121 module_param(kdamond_pid, int, 0400);
122 
123 static struct damos_stat damon_lru_sort_hot_stat;
124 DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_hot_stat,
125 		lru_sort_tried_hot_regions, lru_sorted_hot_regions,
126 		hot_quota_exceeds);
127 
128 static struct damos_stat damon_lru_sort_cold_stat;
129 DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat,
130 		lru_sort_tried_cold_regions, lru_sorted_cold_regions,
131 		cold_quota_exceeds);
132 
133 static struct damos_access_pattern damon_lru_sort_stub_pattern = {
134 	/* Find regions having PAGE_SIZE or larger size */
135 	.min_sz_region = PAGE_SIZE,
136 	.max_sz_region = ULONG_MAX,
137 	/* no matter its access frequency */
138 	.min_nr_accesses = 0,
139 	.max_nr_accesses = UINT_MAX,
140 	/* no matter its age */
141 	.min_age_region = 0,
142 	.max_age_region = UINT_MAX,
143 };
144 
145 static struct damon_ctx *ctx;
146 static struct damon_target *target;
147 
damon_lru_sort_new_scheme(struct damos_access_pattern * pattern,enum damos_action action)148 static struct damos *damon_lru_sort_new_scheme(
149 		struct damos_access_pattern *pattern, enum damos_action action)
150 {
151 	struct damos_quota quota = damon_lru_sort_quota;
152 
153 	/* Use half of total quota for hot/cold pages sorting */
154 	quota.ms = quota.ms / 2;
155 
156 	return damon_new_scheme(
157 			/* find the pattern, and */
158 			pattern,
159 			/* (de)prioritize on LRU-lists */
160 			action,
161 			/* under the quota. */
162 			&quota,
163 			/* (De)activate this according to the watermarks. */
164 			&damon_lru_sort_wmarks);
165 }
166 
167 /* Create a DAMON-based operation scheme for hot memory regions */
damon_lru_sort_new_hot_scheme(unsigned int hot_thres)168 static struct damos *damon_lru_sort_new_hot_scheme(unsigned int hot_thres)
169 {
170 	struct damos_access_pattern pattern = damon_lru_sort_stub_pattern;
171 
172 	pattern.min_nr_accesses = hot_thres;
173 	return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_PRIO);
174 }
175 
176 /* Create a DAMON-based operation scheme for cold memory regions */
damon_lru_sort_new_cold_scheme(unsigned int cold_thres)177 static struct damos *damon_lru_sort_new_cold_scheme(unsigned int cold_thres)
178 {
179 	struct damos_access_pattern pattern = damon_lru_sort_stub_pattern;
180 
181 	pattern.max_nr_accesses = 0;
182 	pattern.min_age_region = cold_thres;
183 	return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_DEPRIO);
184 }
185 
damon_lru_sort_copy_quota_status(struct damos_quota * dst,struct damos_quota * src)186 static void damon_lru_sort_copy_quota_status(struct damos_quota *dst,
187 		struct damos_quota *src)
188 {
189 	dst->total_charged_sz = src->total_charged_sz;
190 	dst->total_charged_ns = src->total_charged_ns;
191 	dst->charged_sz = src->charged_sz;
192 	dst->charged_from = src->charged_from;
193 	dst->charge_target_from = src->charge_target_from;
194 	dst->charge_addr_from = src->charge_addr_from;
195 }
196 
damon_lru_sort_apply_parameters(void)197 static int damon_lru_sort_apply_parameters(void)
198 {
199 	struct damos *scheme, *hot_scheme, *cold_scheme;
200 	struct damos *old_hot_scheme = NULL, *old_cold_scheme = NULL;
201 	unsigned int hot_thres, cold_thres;
202 	int err = 0;
203 
204 	err = damon_set_attrs(ctx, &damon_lru_sort_mon_attrs);
205 	if (err)
206 		return err;
207 
208 	damon_for_each_scheme(scheme, ctx) {
209 		if (!old_hot_scheme) {
210 			old_hot_scheme = scheme;
211 			continue;
212 		}
213 		old_cold_scheme = scheme;
214 	}
215 
216 	hot_thres = damon_max_nr_accesses(&damon_lru_sort_mon_attrs) *
217 		hot_thres_access_freq / 1000;
218 	hot_scheme = damon_lru_sort_new_hot_scheme(hot_thres);
219 	if (!hot_scheme)
220 		return -ENOMEM;
221 	if (old_hot_scheme)
222 		damon_lru_sort_copy_quota_status(&hot_scheme->quota,
223 				&old_hot_scheme->quota);
224 
225 	cold_thres = cold_min_age / damon_lru_sort_mon_attrs.aggr_interval;
226 	cold_scheme = damon_lru_sort_new_cold_scheme(cold_thres);
227 	if (!cold_scheme) {
228 		damon_destroy_scheme(hot_scheme);
229 		return -ENOMEM;
230 	}
231 	if (old_cold_scheme)
232 		damon_lru_sort_copy_quota_status(&cold_scheme->quota,
233 				&old_cold_scheme->quota);
234 
235 	damon_set_schemes(ctx, &hot_scheme, 1);
236 	damon_add_scheme(ctx, cold_scheme);
237 
238 	return damon_set_region_biggest_system_ram_default(target,
239 					&monitor_region_start,
240 					&monitor_region_end);
241 }
242 
damon_lru_sort_turn(bool on)243 static int damon_lru_sort_turn(bool on)
244 {
245 	int err;
246 
247 	if (!on) {
248 		err = damon_stop(&ctx, 1);
249 		if (!err)
250 			kdamond_pid = -1;
251 		return err;
252 	}
253 
254 	err = damon_lru_sort_apply_parameters();
255 	if (err)
256 		return err;
257 
258 	err = damon_start(&ctx, 1, true);
259 	if (err)
260 		return err;
261 	kdamond_pid = ctx->kdamond->pid;
262 	return 0;
263 }
264 
damon_lru_sort_enabled_store(const char * val,const struct kernel_param * kp)265 static int damon_lru_sort_enabled_store(const char *val,
266 		const struct kernel_param *kp)
267 {
268 	bool is_enabled = enabled;
269 	bool enable;
270 	int err;
271 
272 	err = kstrtobool(val, &enable);
273 	if (err)
274 		return err;
275 
276 	if (is_enabled == enable)
277 		return 0;
278 
279 	/* Called before init function.  The function will handle this. */
280 	if (!ctx)
281 		goto set_param_out;
282 
283 	err = damon_lru_sort_turn(enable);
284 	if (err)
285 		return err;
286 
287 set_param_out:
288 	enabled = enable;
289 	return err;
290 }
291 
292 static const struct kernel_param_ops enabled_param_ops = {
293 	.set = damon_lru_sort_enabled_store,
294 	.get = param_get_bool,
295 };
296 
297 module_param_cb(enabled, &enabled_param_ops, &enabled, 0600);
298 MODULE_PARM_DESC(enabled,
299 	"Enable or disable DAMON_LRU_SORT (default: disabled)");
300 
damon_lru_sort_handle_commit_inputs(void)301 static int damon_lru_sort_handle_commit_inputs(void)
302 {
303 	int err;
304 
305 	if (!commit_inputs)
306 		return 0;
307 
308 	err = damon_lru_sort_apply_parameters();
309 	commit_inputs = false;
310 	return err;
311 }
312 
damon_lru_sort_after_aggregation(struct damon_ctx * c)313 static int damon_lru_sort_after_aggregation(struct damon_ctx *c)
314 {
315 	struct damos *s;
316 
317 	/* update the stats parameter */
318 	damon_for_each_scheme(s, c) {
319 		if (s->action == DAMOS_LRU_PRIO)
320 			damon_lru_sort_hot_stat = s->stat;
321 		else if (s->action == DAMOS_LRU_DEPRIO)
322 			damon_lru_sort_cold_stat = s->stat;
323 	}
324 
325 	return damon_lru_sort_handle_commit_inputs();
326 }
327 
damon_lru_sort_after_wmarks_check(struct damon_ctx * c)328 static int damon_lru_sort_after_wmarks_check(struct damon_ctx *c)
329 {
330 	return damon_lru_sort_handle_commit_inputs();
331 }
332 
damon_lru_sort_init(void)333 static int __init damon_lru_sort_init(void)
334 {
335 	int err = damon_modules_new_paddr_ctx_target(&ctx, &target);
336 
337 	if (err)
338 		return err;
339 
340 	ctx->callback.after_wmarks_check = damon_lru_sort_after_wmarks_check;
341 	ctx->callback.after_aggregation = damon_lru_sort_after_aggregation;
342 
343 	/* 'enabled' has set before this function, probably via command line */
344 	if (enabled)
345 		err = damon_lru_sort_turn(true);
346 
347 	return err;
348 }
349 
350 module_init(damon_lru_sort_init);
351