mm/damon/lru_sort: use quotas param generator
[platform/kernel/linux-starfive.git] / mm / damon / lru_sort.c
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/ioport.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/workqueue.h>
15
16 #include "modules-common.h"
17
18 #ifdef MODULE_PARAM_PREFIX
19 #undef MODULE_PARAM_PREFIX
20 #endif
21 #define MODULE_PARAM_PREFIX "damon_lru_sort."
22
23 /*
24  * Enable or disable DAMON_LRU_SORT.
25  *
26  * You can enable DAMON_LRU_SORT by setting the value of this parameter as
27  * ``Y``.  Setting it as ``N`` disables DAMON_LRU_SORT.  Note that
28  * DAMON_LRU_SORT could do no real monitoring and LRU-lists sorting due to the
29  * watermarks-based activation condition.  Refer to below descriptions for the
30  * watermarks parameter for this.
31  */
32 static bool enabled __read_mostly;
33
34 /*
35  * Make DAMON_LRU_SORT reads the input parameters again, except ``enabled``.
36  *
37  * Input parameters that updated while DAMON_LRU_SORT is running are not
38  * applied by default.  Once this parameter is set as ``Y``, DAMON_LRU_SORT
39  * reads values of parametrs except ``enabled`` again.  Once the re-reading is
40  * done, this parameter is set as ``N``.  If invalid parameters are found while
41  * the re-reading, DAMON_LRU_SORT will be disabled.
42  */
43 static bool commit_inputs __read_mostly;
44 module_param(commit_inputs, bool, 0600);
45
46 /*
47  * Access frequency threshold for hot memory regions identification in permil.
48  *
49  * If a memory region is accessed in frequency of this or higher,
50  * DAMON_LRU_SORT identifies the region as hot, and mark it as accessed on the
51  * LRU list, so that it could not be reclaimed under memory pressure.  50% by
52  * default.
53  */
54 static unsigned long hot_thres_access_freq = 500;
55 module_param(hot_thres_access_freq, ulong, 0600);
56
57 /*
58  * Time threshold for cold memory regions identification in microseconds.
59  *
60  * If a memory region is not accessed for this or longer time, DAMON_LRU_SORT
61  * identifies the region as cold, and mark it as unaccessed on the LRU list, so
62  * that it could be reclaimed first under memory pressure.  120 seconds by
63  * default.
64  */
65 static unsigned long cold_min_age __read_mostly = 120000000;
66 module_param(cold_min_age, ulong, 0600);
67
68 static struct damos_quota damon_lru_sort_quota = {
69         /* Use up to 10 ms per 1 sec, by default */
70         .ms = 10,
71         .sz = 0,
72         .reset_interval = 1000,
73         /* Within the quota, mark hotter regions accessed first. */
74         .weight_sz = 0,
75         .weight_nr_accesses = 1,
76         .weight_age = 0,
77 };
78 DEFINE_DAMON_MODULES_DAMOS_TIME_QUOTA(damon_lru_sort_quota);
79
80 struct damos_watermarks damon_lru_sort_wmarks = {
81         .metric = DAMOS_WMARK_FREE_MEM_RATE,
82         .interval = 5000000,    /* 5 seconds */
83         .high = 200,            /* 20 percent */
84         .mid = 150,             /* 15 percent */
85         .low = 50,              /* 5 percent */
86 };
87 DEFINE_DAMON_MODULES_WMARKS_PARAMS(damon_lru_sort_wmarks);
88
89 static struct damon_attrs damon_lru_sort_mon_attrs = {
90         .sample_interval = 5000,        /* 5 ms */
91         .aggr_interval = 100000,        /* 100 ms */
92         .ops_update_interval = 0,
93         .min_nr_regions = 10,
94         .max_nr_regions = 1000,
95 };
96 DEFINE_DAMON_MODULES_MON_ATTRS_PARAMS(damon_lru_sort_mon_attrs);
97
98 /*
99  * Start of the target memory region in physical address.
100  *
101  * The start physical address of memory region that DAMON_LRU_SORT will do work
102  * against.  By default, biggest System RAM is used as the region.
103  */
104 static unsigned long monitor_region_start __read_mostly;
105 module_param(monitor_region_start, ulong, 0600);
106
107 /*
108  * End of the target memory region in physical address.
109  *
110  * The end physical address of memory region that DAMON_LRU_SORT will do work
111  * against.  By default, biggest System RAM is used as the region.
112  */
113 static unsigned long monitor_region_end __read_mostly;
114 module_param(monitor_region_end, ulong, 0600);
115
116 /*
117  * PID of the DAMON thread
118  *
119  * If DAMON_LRU_SORT is enabled, this becomes the PID of the worker thread.
120  * Else, -1.
121  */
122 static int kdamond_pid __read_mostly = -1;
123 module_param(kdamond_pid, int, 0400);
124
125 static struct damos_stat damon_lru_sort_hot_stat;
126 DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_hot_stat,
127                 lru_sort_tried_hot_regions, lru_sorted_hot_regions,
128                 hot_quota_exceeds);
129
130 static struct damos_stat damon_lru_sort_cold_stat;
131 DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat,
132                 lru_sort_tried_cold_regions, lru_sorted_cold_regions,
133                 cold_quota_exceeds);
134
135 static struct damon_ctx *ctx;
136 static struct damon_target *target;
137
138 /* Create a DAMON-based operation scheme for hot memory regions */
139 static struct damos *damon_lru_sort_new_hot_scheme(unsigned int hot_thres)
140 {
141         struct damos_access_pattern pattern = {
142                 /* Find regions having PAGE_SIZE or larger size */
143                 .min_sz_region = PAGE_SIZE,
144                 .max_sz_region = ULONG_MAX,
145                 /* and accessed for more than the threshold */
146                 .min_nr_accesses = hot_thres,
147                 .max_nr_accesses = UINT_MAX,
148                 /* no matter its age */
149                 .min_age_region = 0,
150                 .max_age_region = UINT_MAX,
151         };
152         struct damos_quota quota = damon_lru_sort_quota;
153
154         /* Use half of total quota for hot pages sorting */
155         quota.ms = quota.ms / 2;
156
157         return damon_new_scheme(
158                         &pattern,
159                         /* prioritize those on LRU lists, as soon as found */
160                         DAMOS_LRU_PRIO,
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 cold memory regions */
168 static struct damos *damon_lru_sort_new_cold_scheme(unsigned int cold_thres)
169 {
170         struct damos_access_pattern pattern = {
171                 /* Find regions having PAGE_SIZE or larger size */
172                 .min_sz_region = PAGE_SIZE,
173                 .max_sz_region = ULONG_MAX,
174                 /* and not accessed at all */
175                 .min_nr_accesses = 0,
176                 .max_nr_accesses = 0,
177                 /* for min_age or more micro-seconds */
178                 .min_age_region = cold_thres,
179                 .max_age_region = UINT_MAX,
180         };
181         struct damos_quota quota = damon_lru_sort_quota;
182
183         /* Use half of total quota for cold pages sorting */
184         quota.ms = quota.ms / 2;
185
186         return damon_new_scheme(
187                         &pattern,
188                         /* mark those as not accessed, as soon as found */
189                         DAMOS_LRU_DEPRIO,
190                         /* under the quota. */
191                         &quota,
192                         /* (De)activate this according to the watermarks. */
193                         &damon_lru_sort_wmarks);
194 }
195
196 static int damon_lru_sort_apply_parameters(void)
197 {
198         struct damos *scheme;
199         struct damon_addr_range addr_range;
200         unsigned int hot_thres, cold_thres;
201         int err = 0;
202
203         err = damon_set_attrs(ctx, &damon_lru_sort_mon_attrs);
204         if (err)
205                 return err;
206
207         /* aggr_interval / sample_interval is the maximum nr_accesses */
208         hot_thres = damon_lru_sort_mon_attrs.aggr_interval /
209                 damon_lru_sort_mon_attrs.sample_interval *
210                 hot_thres_access_freq / 1000;
211         scheme = damon_lru_sort_new_hot_scheme(hot_thres);
212         if (!scheme)
213                 return -ENOMEM;
214         err = damon_set_schemes(ctx, &scheme, 1);
215         if (err)
216                 return err;
217
218         cold_thres = cold_min_age / damon_lru_sort_mon_attrs.aggr_interval;
219         scheme = damon_lru_sort_new_cold_scheme(cold_thres);
220         if (!scheme)
221                 return -ENOMEM;
222         damon_add_scheme(ctx, scheme);
223
224         if (monitor_region_start > monitor_region_end)
225                 return -EINVAL;
226         if (!monitor_region_start && !monitor_region_end &&
227             !damon_find_biggest_system_ram(&monitor_region_start,
228                                            &monitor_region_end))
229                 return -EINVAL;
230         addr_range.start = monitor_region_start;
231         addr_range.end = monitor_region_end;
232         return damon_set_regions(target, &addr_range, 1);
233 }
234
235 static int damon_lru_sort_turn(bool on)
236 {
237         int err;
238
239         if (!on) {
240                 err = damon_stop(&ctx, 1);
241                 if (!err)
242                         kdamond_pid = -1;
243                 return err;
244         }
245
246         err = damon_lru_sort_apply_parameters();
247         if (err)
248                 return err;
249
250         err = damon_start(&ctx, 1, true);
251         if (err)
252                 return err;
253         kdamond_pid = ctx->kdamond->pid;
254         return 0;
255 }
256
257 static struct delayed_work damon_lru_sort_timer;
258 static void damon_lru_sort_timer_fn(struct work_struct *work)
259 {
260         static bool last_enabled;
261         bool now_enabled;
262
263         now_enabled = enabled;
264         if (last_enabled != now_enabled) {
265                 if (!damon_lru_sort_turn(now_enabled))
266                         last_enabled = now_enabled;
267                 else
268                         enabled = last_enabled;
269         }
270 }
271 static DECLARE_DELAYED_WORK(damon_lru_sort_timer, damon_lru_sort_timer_fn);
272
273 static bool damon_lru_sort_initialized;
274
275 static int damon_lru_sort_enabled_store(const char *val,
276                 const struct kernel_param *kp)
277 {
278         int rc = param_set_bool(val, kp);
279
280         if (rc < 0)
281                 return rc;
282
283         if (!damon_lru_sort_initialized)
284                 return rc;
285
286         schedule_delayed_work(&damon_lru_sort_timer, 0);
287
288         return 0;
289 }
290
291 static const struct kernel_param_ops enabled_param_ops = {
292         .set = damon_lru_sort_enabled_store,
293         .get = param_get_bool,
294 };
295
296 module_param_cb(enabled, &enabled_param_ops, &enabled, 0600);
297 MODULE_PARM_DESC(enabled,
298         "Enable or disable DAMON_LRU_SORT (default: disabled)");
299
300 static int damon_lru_sort_handle_commit_inputs(void)
301 {
302         int err;
303
304         if (!commit_inputs)
305                 return 0;
306
307         err = damon_lru_sort_apply_parameters();
308         commit_inputs = false;
309         return err;
310 }
311
312 static int damon_lru_sort_after_aggregation(struct damon_ctx *c)
313 {
314         struct damos *s;
315
316         /* update the stats parameter */
317         damon_for_each_scheme(s, c) {
318                 if (s->action == DAMOS_LRU_PRIO)
319                         damon_lru_sort_hot_stat = s->stat;
320                 else if (s->action == DAMOS_LRU_DEPRIO)
321                         damon_lru_sort_cold_stat = s->stat;
322         }
323
324         return damon_lru_sort_handle_commit_inputs();
325 }
326
327 static int damon_lru_sort_after_wmarks_check(struct damon_ctx *c)
328 {
329         return damon_lru_sort_handle_commit_inputs();
330 }
331
332 static int __init damon_lru_sort_init(void)
333 {
334         ctx = damon_new_ctx();
335         if (!ctx)
336                 return -ENOMEM;
337
338         if (damon_select_ops(ctx, DAMON_OPS_PADDR)) {
339                 damon_destroy_ctx(ctx);
340                 return -EINVAL;
341         }
342
343         ctx->callback.after_wmarks_check = damon_lru_sort_after_wmarks_check;
344         ctx->callback.after_aggregation = damon_lru_sort_after_aggregation;
345
346         target = damon_new_target();
347         if (!target) {
348                 damon_destroy_ctx(ctx);
349                 return -ENOMEM;
350         }
351         damon_add_target(ctx, target);
352
353         schedule_delayed_work(&damon_lru_sort_timer, 0);
354
355         damon_lru_sort_initialized = true;
356         return 0;
357 }
358
359 module_init(damon_lru_sort_init);