| 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 | * Scale factor for DAMON_LRU_SORT to ops address conversion. |
| 116 | * |
| 117 | * This parameter must not be set to 0. |
| 118 | */ |
| 119 | static unsigned long addr_unit __read_mostly = 1; |
| 120 | |
| 121 | /* |
| 122 | * PID of the DAMON thread |
| 123 | * |
| 124 | * If DAMON_LRU_SORT is enabled, this becomes the PID of the worker thread. |
| 125 | * Else, -1. |
| 126 | */ |
| 127 | static int kdamond_pid __read_mostly = -1; |
| 128 | module_param(kdamond_pid, int, 0400); |
| 129 | |
| 130 | static struct damos_stat damon_lru_sort_hot_stat; |
| 131 | DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_hot_stat, |
| 132 | lru_sort_tried_hot_regions, lru_sorted_hot_regions, |
| 133 | hot_quota_exceeds); |
| 134 | |
| 135 | static struct damos_stat damon_lru_sort_cold_stat; |
| 136 | DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat, |
| 137 | lru_sort_tried_cold_regions, lru_sorted_cold_regions, |
| 138 | cold_quota_exceeds); |
| 139 | |
| 140 | static struct damos_access_pattern damon_lru_sort_stub_pattern = { |
| 141 | /* Find regions having PAGE_SIZE or larger size */ |
| 142 | .min_sz_region = PAGE_SIZE, |
| 143 | .max_sz_region = ULONG_MAX, |
| 144 | /* no matter its access frequency */ |
| 145 | .min_nr_accesses = 0, |
| 146 | .max_nr_accesses = UINT_MAX, |
| 147 | /* no matter its age */ |
| 148 | .min_age_region = 0, |
| 149 | .max_age_region = UINT_MAX, |
| 150 | }; |
| 151 | |
| 152 | static struct damon_ctx *ctx; |
| 153 | static struct damon_target *target; |
| 154 | |
| 155 | static struct damos *damon_lru_sort_new_scheme( |
| 156 | struct damos_access_pattern *pattern, enum damos_action action) |
| 157 | { |
| 158 | struct damos_quota quota = damon_lru_sort_quota; |
| 159 | |
| 160 | /* Use half of total quota for hot/cold pages sorting */ |
| 161 | quota.ms = quota.ms / 2; |
| 162 | |
| 163 | return damon_new_scheme( |
| 164 | /* find the pattern, and */ |
| 165 | pattern, |
| 166 | /* (de)prioritize on LRU-lists */ |
| 167 | action, |
| 168 | /* for each aggregation interval */ |
| 169 | apply_interval_us: 0, |
| 170 | /* under the quota. */ |
| 171 | quota: "a, |
| 172 | /* (De)activate this according to the watermarks. */ |
| 173 | wmarks: &damon_lru_sort_wmarks, |
| 174 | NUMA_NO_NODE); |
| 175 | } |
| 176 | |
| 177 | /* Create a DAMON-based operation scheme for hot memory regions */ |
| 178 | static struct damos *damon_lru_sort_new_hot_scheme(unsigned int hot_thres) |
| 179 | { |
| 180 | struct damos_access_pattern pattern = damon_lru_sort_stub_pattern; |
| 181 | |
| 182 | pattern.min_nr_accesses = hot_thres; |
| 183 | return damon_lru_sort_new_scheme(pattern: &pattern, action: DAMOS_LRU_PRIO); |
| 184 | } |
| 185 | |
| 186 | /* Create a DAMON-based operation scheme for cold memory regions */ |
| 187 | static struct damos *damon_lru_sort_new_cold_scheme(unsigned int cold_thres) |
| 188 | { |
| 189 | struct damos_access_pattern pattern = damon_lru_sort_stub_pattern; |
| 190 | |
| 191 | pattern.max_nr_accesses = 0; |
| 192 | pattern.min_age_region = cold_thres; |
| 193 | return damon_lru_sort_new_scheme(pattern: &pattern, action: DAMOS_LRU_DEPRIO); |
| 194 | } |
| 195 | |
| 196 | static int damon_lru_sort_apply_parameters(void) |
| 197 | { |
| 198 | struct damon_ctx *param_ctx; |
| 199 | struct damon_target *param_target; |
| 200 | struct damos *hot_scheme, *cold_scheme; |
| 201 | unsigned int hot_thres, cold_thres; |
| 202 | int err; |
| 203 | |
| 204 | err = damon_modules_new_paddr_ctx_target(ctxp: ¶m_ctx, targetp: ¶m_target); |
| 205 | if (err) |
| 206 | return err; |
| 207 | |
| 208 | /* |
| 209 | * If monitor_region_start/end are unset, always silently |
| 210 | * reset addr_unit to 1. |
| 211 | */ |
| 212 | if (!monitor_region_start && !monitor_region_end) |
| 213 | addr_unit = 1; |
| 214 | param_ctx->addr_unit = addr_unit; |
| 215 | param_ctx->min_sz_region = max(DAMON_MIN_REGION / addr_unit, 1); |
| 216 | |
| 217 | if (!damon_lru_sort_mon_attrs.sample_interval) { |
| 218 | err = -EINVAL; |
| 219 | goto out; |
| 220 | } |
| 221 | |
| 222 | err = damon_set_attrs(ctx: param_ctx, attrs: &damon_lru_sort_mon_attrs); |
| 223 | if (err) |
| 224 | goto out; |
| 225 | |
| 226 | err = -ENOMEM; |
| 227 | hot_thres = damon_max_nr_accesses(attrs: &damon_lru_sort_mon_attrs) * |
| 228 | hot_thres_access_freq / 1000; |
| 229 | hot_scheme = damon_lru_sort_new_hot_scheme(hot_thres); |
| 230 | if (!hot_scheme) |
| 231 | goto out; |
| 232 | |
| 233 | cold_thres = cold_min_age / damon_lru_sort_mon_attrs.aggr_interval; |
| 234 | cold_scheme = damon_lru_sort_new_cold_scheme(cold_thres); |
| 235 | if (!cold_scheme) { |
| 236 | damon_destroy_scheme(s: hot_scheme); |
| 237 | goto out; |
| 238 | } |
| 239 | |
| 240 | damon_set_schemes(ctx: param_ctx, schemes: &hot_scheme, nr_schemes: 1); |
| 241 | damon_add_scheme(ctx: param_ctx, s: cold_scheme); |
| 242 | |
| 243 | err = damon_set_region_biggest_system_ram_default(t: param_target, |
| 244 | start: &monitor_region_start, |
| 245 | end: &monitor_region_end, |
| 246 | min_sz_region: param_ctx->min_sz_region); |
| 247 | if (err) |
| 248 | goto out; |
| 249 | err = damon_commit_ctx(old_ctx: ctx, new_ctx: param_ctx); |
| 250 | out: |
| 251 | damon_destroy_ctx(ctx: param_ctx); |
| 252 | return err; |
| 253 | } |
| 254 | |
| 255 | static int damon_lru_sort_handle_commit_inputs(void) |
| 256 | { |
| 257 | int err; |
| 258 | |
| 259 | if (!commit_inputs) |
| 260 | return 0; |
| 261 | |
| 262 | err = damon_lru_sort_apply_parameters(); |
| 263 | commit_inputs = false; |
| 264 | return err; |
| 265 | } |
| 266 | |
| 267 | static int damon_lru_sort_damon_call_fn(void *arg) |
| 268 | { |
| 269 | struct damon_ctx *c = arg; |
| 270 | struct damos *s; |
| 271 | |
| 272 | /* update the stats parameter */ |
| 273 | damon_for_each_scheme(s, c) { |
| 274 | if (s->action == DAMOS_LRU_PRIO) |
| 275 | damon_lru_sort_hot_stat = s->stat; |
| 276 | else if (s->action == DAMOS_LRU_DEPRIO) |
| 277 | damon_lru_sort_cold_stat = s->stat; |
| 278 | } |
| 279 | |
| 280 | return damon_lru_sort_handle_commit_inputs(); |
| 281 | } |
| 282 | |
| 283 | static struct damon_call_control call_control = { |
| 284 | .fn = damon_lru_sort_damon_call_fn, |
| 285 | .repeat = true, |
| 286 | }; |
| 287 | |
| 288 | static int damon_lru_sort_turn(bool on) |
| 289 | { |
| 290 | int err; |
| 291 | |
| 292 | if (!on) { |
| 293 | err = damon_stop(ctxs: &ctx, nr_ctxs: 1); |
| 294 | if (!err) |
| 295 | kdamond_pid = -1; |
| 296 | return err; |
| 297 | } |
| 298 | |
| 299 | err = damon_lru_sort_apply_parameters(); |
| 300 | if (err) |
| 301 | return err; |
| 302 | |
| 303 | err = damon_start(ctxs: &ctx, nr_ctxs: 1, exclusive: true); |
| 304 | if (err) |
| 305 | return err; |
| 306 | kdamond_pid = ctx->kdamond->pid; |
| 307 | return damon_call(ctx, control: &call_control); |
| 308 | } |
| 309 | |
| 310 | static int damon_lru_sort_addr_unit_store(const char *val, |
| 311 | const struct kernel_param *kp) |
| 312 | { |
| 313 | unsigned long input_addr_unit; |
| 314 | int err = kstrtoul(s: val, base: 0, res: &input_addr_unit); |
| 315 | |
| 316 | if (err) |
| 317 | return err; |
| 318 | if (!input_addr_unit) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | addr_unit = input_addr_unit; |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static const struct kernel_param_ops addr_unit_param_ops = { |
| 326 | .set = damon_lru_sort_addr_unit_store, |
| 327 | .get = param_get_ulong, |
| 328 | }; |
| 329 | |
| 330 | module_param_cb(addr_unit, &addr_unit_param_ops, &addr_unit, 0600); |
| 331 | MODULE_PARM_DESC(addr_unit, |
| 332 | "Scale factor for DAMON_LRU_SORT to ops address conversion (default: 1)" ); |
| 333 | |
| 334 | static int damon_lru_sort_enabled_store(const char *val, |
| 335 | const struct kernel_param *kp) |
| 336 | { |
| 337 | bool is_enabled = enabled; |
| 338 | bool enable; |
| 339 | int err; |
| 340 | |
| 341 | err = kstrtobool(s: val, res: &enable); |
| 342 | if (err) |
| 343 | return err; |
| 344 | |
| 345 | if (is_enabled == enable) |
| 346 | return 0; |
| 347 | |
| 348 | /* Called before init function. The function will handle this. */ |
| 349 | if (!damon_initialized()) |
| 350 | goto set_param_out; |
| 351 | |
| 352 | err = damon_lru_sort_turn(on: enable); |
| 353 | if (err) |
| 354 | return err; |
| 355 | |
| 356 | set_param_out: |
| 357 | enabled = enable; |
| 358 | return err; |
| 359 | } |
| 360 | |
| 361 | static const struct kernel_param_ops enabled_param_ops = { |
| 362 | .set = damon_lru_sort_enabled_store, |
| 363 | .get = param_get_bool, |
| 364 | }; |
| 365 | |
| 366 | module_param_cb(enabled, &enabled_param_ops, &enabled, 0600); |
| 367 | MODULE_PARM_DESC(enabled, |
| 368 | "Enable or disable DAMON_LRU_SORT (default: disabled)" ); |
| 369 | |
| 370 | static int __init damon_lru_sort_init(void) |
| 371 | { |
| 372 | int err; |
| 373 | |
| 374 | if (!damon_initialized()) { |
| 375 | err = -ENOMEM; |
| 376 | goto out; |
| 377 | } |
| 378 | err = damon_modules_new_paddr_ctx_target(ctxp: &ctx, targetp: &target); |
| 379 | if (err) |
| 380 | goto out; |
| 381 | |
| 382 | call_control.data = ctx; |
| 383 | |
| 384 | /* 'enabled' has set before this function, probably via command line */ |
| 385 | if (enabled) |
| 386 | err = damon_lru_sort_turn(on: true); |
| 387 | |
| 388 | out: |
| 389 | if (err && enabled) |
| 390 | enabled = false; |
| 391 | return err; |
| 392 | } |
| 393 | |
| 394 | module_init(damon_lru_sort_init); |
| 395 | |