lru_sort.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DAMON-based LRU-lists Sorting
  4. *
  5. * Author: SeongJae Park <sj@kernel.org>
  6. */
  7. #define pr_fmt(fmt) "damon-lru-sort: " fmt
  8. #include <linux/damon.h>
  9. #include <linux/kstrtox.h>
  10. #include <linux/module.h>
  11. #include "modules-common.h"
  12. #ifdef MODULE_PARAM_PREFIX
  13. #undef MODULE_PARAM_PREFIX
  14. #endif
  15. #define MODULE_PARAM_PREFIX "damon_lru_sort."
  16. /*
  17. * Enable or disable DAMON_LRU_SORT.
  18. *
  19. * You can enable DAMON_LRU_SORT by setting the value of this parameter as
  20. * ``Y``. Setting it as ``N`` disables DAMON_LRU_SORT. Note that
  21. * DAMON_LRU_SORT could do no real monitoring and LRU-lists sorting due to the
  22. * watermarks-based activation condition. Refer to below descriptions for the
  23. * watermarks parameter for this.
  24. */
  25. static bool enabled __read_mostly;
  26. /*
  27. * Make DAMON_LRU_SORT reads the input parameters again, except ``enabled``.
  28. *
  29. * Input parameters that updated while DAMON_LRU_SORT is running are not
  30. * applied by default. Once this parameter is set as ``Y``, DAMON_LRU_SORT
  31. * reads values of parameters except ``enabled`` again. Once the re-reading is
  32. * done, this parameter is set as ``N``. If invalid parameters are found while
  33. * the re-reading, DAMON_LRU_SORT will be disabled.
  34. */
  35. static bool commit_inputs __read_mostly;
  36. module_param(commit_inputs, bool, 0600);
  37. /*
  38. * Desired active to [in]active memory ratio in bp (1/10,000).
  39. *
  40. * While keeping the caps that set by other quotas, DAMON_LRU_SORT
  41. * automatically increases and decreases the effective level of the quota
  42. * aiming the LRU [de]prioritizations of the hot and cold memory resulting in
  43. * this active to [in]active memory ratio. Value zero means disabling this
  44. * auto-tuning feature.
  45. *
  46. * Disabled by default.
  47. */
  48. static unsigned long active_mem_bp __read_mostly;
  49. module_param(active_mem_bp, ulong, 0600);
  50. /*
  51. * Auto-tune monitoring intervals.
  52. *
  53. * If this parameter is set as ``Y``, DAMON_LRU_SORT automatically tunes
  54. * DAMON's sampling and aggregation intervals. The auto-tuning aims to capture
  55. * meaningful amount of access events in each DAMON-snapshot, while keeping the
  56. * sampling interval 5 milliseconds in minimum, and 10 seconds in maximum.
  57. * Setting this as ``N`` disables the auto-tuning.
  58. *
  59. * Disabled by default.
  60. */
  61. static bool autotune_monitoring_intervals __read_mostly;
  62. module_param(autotune_monitoring_intervals, bool, 0600);
  63. /*
  64. * Filter [non-]young pages accordingly for LRU [de]prioritizations.
  65. *
  66. * If this is set, check page level access (youngness) once again before each
  67. * LRU [de]prioritization operation. LRU prioritization operation is skipped
  68. * if the page has not accessed since the last check (not young). LRU
  69. * deprioritization operation is skipped if the page has accessed since the
  70. * last check (young). The feature is enabled or disabled if this parameter is
  71. * set as ``Y`` or ``N``, respectively.
  72. *
  73. * Disabled by default.
  74. */
  75. static bool filter_young_pages __read_mostly;
  76. module_param(filter_young_pages, bool, 0600);
  77. /*
  78. * Access frequency threshold for hot memory regions identification in permil.
  79. *
  80. * If a memory region is accessed in frequency of this or higher,
  81. * DAMON_LRU_SORT identifies the region as hot, and mark it as accessed on the
  82. * LRU list, so that it could not be reclaimed under memory pressure. 50% by
  83. * default.
  84. */
  85. static unsigned long hot_thres_access_freq = 500;
  86. module_param(hot_thres_access_freq, ulong, 0600);
  87. /*
  88. * Time threshold for cold memory regions identification in microseconds.
  89. *
  90. * If a memory region is not accessed for this or longer time, DAMON_LRU_SORT
  91. * identifies the region as cold, and mark it as unaccessed on the LRU list, so
  92. * that it could be reclaimed first under memory pressure. 120 seconds by
  93. * default.
  94. */
  95. static unsigned long cold_min_age __read_mostly = 120000000;
  96. module_param(cold_min_age, ulong, 0600);
  97. static struct damos_quota damon_lru_sort_quota = {
  98. /* Use up to 10 ms per 1 sec, by default */
  99. .ms = 10,
  100. .sz = 0,
  101. .reset_interval = 1000,
  102. /* Within the quota, mark hotter regions accessed first. */
  103. .weight_sz = 0,
  104. .weight_nr_accesses = 1,
  105. .weight_age = 1,
  106. };
  107. DEFINE_DAMON_MODULES_DAMOS_TIME_QUOTA(damon_lru_sort_quota);
  108. static struct damos_watermarks damon_lru_sort_wmarks = {
  109. .metric = DAMOS_WMARK_FREE_MEM_RATE,
  110. .interval = 5000000, /* 5 seconds */
  111. .high = 200, /* 20 percent */
  112. .mid = 150, /* 15 percent */
  113. .low = 50, /* 5 percent */
  114. };
  115. DEFINE_DAMON_MODULES_WMARKS_PARAMS(damon_lru_sort_wmarks);
  116. static struct damon_attrs damon_lru_sort_mon_attrs = {
  117. .sample_interval = 5000, /* 5 ms */
  118. .aggr_interval = 100000, /* 100 ms */
  119. .ops_update_interval = 0,
  120. .min_nr_regions = 10,
  121. .max_nr_regions = 1000,
  122. };
  123. DEFINE_DAMON_MODULES_MON_ATTRS_PARAMS(damon_lru_sort_mon_attrs);
  124. /*
  125. * Start of the target memory region in physical address.
  126. *
  127. * The start physical address of memory region that DAMON_LRU_SORT will do work
  128. * against. By default, biggest System RAM is used as the region.
  129. */
  130. static unsigned long monitor_region_start __read_mostly;
  131. module_param(monitor_region_start, ulong, 0600);
  132. /*
  133. * End of the target memory region in physical address.
  134. *
  135. * The end physical address of memory region that DAMON_LRU_SORT will do work
  136. * against. By default, biggest System RAM is used as the region.
  137. */
  138. static unsigned long monitor_region_end __read_mostly;
  139. module_param(monitor_region_end, ulong, 0600);
  140. /*
  141. * Scale factor for DAMON_LRU_SORT to ops address conversion.
  142. *
  143. * This parameter must not be set to 0.
  144. */
  145. static unsigned long addr_unit __read_mostly = 1;
  146. /*
  147. * PID of the DAMON thread
  148. *
  149. * If DAMON_LRU_SORT is enabled, this becomes the PID of the worker thread.
  150. * Else, -1.
  151. */
  152. static int kdamond_pid __read_mostly = -1;
  153. module_param(kdamond_pid, int, 0400);
  154. static struct damos_stat damon_lru_sort_hot_stat;
  155. DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_hot_stat,
  156. lru_sort_tried_hot_regions, lru_sorted_hot_regions,
  157. hot_quota_exceeds);
  158. static struct damos_stat damon_lru_sort_cold_stat;
  159. DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat,
  160. lru_sort_tried_cold_regions, lru_sorted_cold_regions,
  161. cold_quota_exceeds);
  162. static struct damos_access_pattern damon_lru_sort_stub_pattern = {
  163. /* Find regions having PAGE_SIZE or larger size */
  164. .min_sz_region = PAGE_SIZE,
  165. .max_sz_region = ULONG_MAX,
  166. /* no matter its access frequency */
  167. .min_nr_accesses = 0,
  168. .max_nr_accesses = UINT_MAX,
  169. /* no matter its age */
  170. .min_age_region = 0,
  171. .max_age_region = UINT_MAX,
  172. };
  173. static struct damon_ctx *ctx;
  174. static struct damon_target *target;
  175. static struct damos *damon_lru_sort_new_scheme(
  176. struct damos_access_pattern *pattern, enum damos_action action)
  177. {
  178. struct damos_quota quota = damon_lru_sort_quota;
  179. /* Use half of total quota for hot/cold pages sorting */
  180. quota.ms = quota.ms / 2;
  181. return damon_new_scheme(
  182. /* find the pattern, and */
  183. pattern,
  184. /* (de)prioritize on LRU-lists */
  185. action,
  186. /* for each aggregation interval */
  187. 0,
  188. /* under the quota. */
  189. &quota,
  190. /* (De)activate this according to the watermarks. */
  191. &damon_lru_sort_wmarks,
  192. NUMA_NO_NODE);
  193. }
  194. /* Create a DAMON-based operation scheme for hot memory regions */
  195. static struct damos *damon_lru_sort_new_hot_scheme(unsigned int hot_thres)
  196. {
  197. struct damos_access_pattern pattern = damon_lru_sort_stub_pattern;
  198. pattern.min_nr_accesses = hot_thres;
  199. return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_PRIO);
  200. }
  201. /* Create a DAMON-based operation scheme for cold memory regions */
  202. static struct damos *damon_lru_sort_new_cold_scheme(unsigned int cold_thres)
  203. {
  204. struct damos_access_pattern pattern = damon_lru_sort_stub_pattern;
  205. pattern.max_nr_accesses = 0;
  206. pattern.min_age_region = cold_thres;
  207. return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_DEPRIO);
  208. }
  209. static int damon_lru_sort_add_quota_goals(struct damos *hot_scheme,
  210. struct damos *cold_scheme)
  211. {
  212. struct damos_quota_goal *goal;
  213. if (!active_mem_bp)
  214. return 0;
  215. goal = damos_new_quota_goal(DAMOS_QUOTA_ACTIVE_MEM_BP, active_mem_bp);
  216. if (!goal)
  217. return -ENOMEM;
  218. damos_add_quota_goal(&hot_scheme->quota, goal);
  219. /* aim 0.2 % goal conflict, to keep little ping pong */
  220. goal = damos_new_quota_goal(DAMOS_QUOTA_INACTIVE_MEM_BP,
  221. 10000 - active_mem_bp + 2);
  222. if (!goal)
  223. return -ENOMEM;
  224. damos_add_quota_goal(&cold_scheme->quota, goal);
  225. return 0;
  226. }
  227. static int damon_lru_sort_add_filters(struct damos *hot_scheme,
  228. struct damos *cold_scheme)
  229. {
  230. struct damos_filter *filter;
  231. if (!filter_young_pages)
  232. return 0;
  233. /* disallow prioritizing not-young pages */
  234. filter = damos_new_filter(DAMOS_FILTER_TYPE_YOUNG, false, false);
  235. if (!filter)
  236. return -ENOMEM;
  237. damos_add_filter(hot_scheme, filter);
  238. /* disabllow de-prioritizing young pages */
  239. filter = damos_new_filter(DAMOS_FILTER_TYPE_YOUNG, true, false);
  240. if (!filter)
  241. return -ENOMEM;
  242. damos_add_filter(cold_scheme, filter);
  243. return 0;
  244. }
  245. static int damon_lru_sort_apply_parameters(void)
  246. {
  247. struct damon_ctx *param_ctx;
  248. struct damon_target *param_target;
  249. struct damon_attrs attrs;
  250. struct damos *hot_scheme, *cold_scheme;
  251. unsigned int hot_thres, cold_thres;
  252. int err;
  253. err = damon_modules_new_paddr_ctx_target(&param_ctx, &param_target);
  254. if (err)
  255. return err;
  256. /*
  257. * If monitor_region_start/end are unset, always silently
  258. * reset addr_unit to 1.
  259. */
  260. if (!monitor_region_start && !monitor_region_end)
  261. addr_unit = 1;
  262. param_ctx->addr_unit = addr_unit;
  263. param_ctx->min_region_sz = max(DAMON_MIN_REGION_SZ / addr_unit, 1);
  264. if (!damon_lru_sort_mon_attrs.sample_interval) {
  265. err = -EINVAL;
  266. goto out;
  267. }
  268. attrs = damon_lru_sort_mon_attrs;
  269. if (autotune_monitoring_intervals) {
  270. attrs.sample_interval = 5000;
  271. attrs.aggr_interval = 100000;
  272. attrs.intervals_goal.access_bp = 40;
  273. attrs.intervals_goal.aggrs = 3;
  274. attrs.intervals_goal.min_sample_us = 5000;
  275. attrs.intervals_goal.max_sample_us = 10 * 1000 * 1000;
  276. }
  277. err = damon_set_attrs(param_ctx, &attrs);
  278. if (err)
  279. goto out;
  280. err = -ENOMEM;
  281. hot_thres = damon_max_nr_accesses(&attrs) *
  282. hot_thres_access_freq / 1000;
  283. hot_scheme = damon_lru_sort_new_hot_scheme(hot_thres);
  284. if (!hot_scheme)
  285. goto out;
  286. cold_thres = cold_min_age / attrs.aggr_interval;
  287. cold_scheme = damon_lru_sort_new_cold_scheme(cold_thres);
  288. if (!cold_scheme) {
  289. damon_destroy_scheme(hot_scheme);
  290. goto out;
  291. }
  292. damon_set_schemes(param_ctx, &hot_scheme, 1);
  293. damon_add_scheme(param_ctx, cold_scheme);
  294. err = damon_lru_sort_add_quota_goals(hot_scheme, cold_scheme);
  295. if (err)
  296. goto out;
  297. err = damon_lru_sort_add_filters(hot_scheme, cold_scheme);
  298. if (err)
  299. goto out;
  300. err = damon_set_region_biggest_system_ram_default(param_target,
  301. &monitor_region_start,
  302. &monitor_region_end,
  303. param_ctx->min_region_sz);
  304. if (err)
  305. goto out;
  306. err = damon_commit_ctx(ctx, param_ctx);
  307. out:
  308. damon_destroy_ctx(param_ctx);
  309. return err;
  310. }
  311. static int damon_lru_sort_handle_commit_inputs(void)
  312. {
  313. int err;
  314. if (!commit_inputs)
  315. return 0;
  316. err = damon_lru_sort_apply_parameters();
  317. commit_inputs = false;
  318. return err;
  319. }
  320. static int damon_lru_sort_damon_call_fn(void *arg)
  321. {
  322. struct damon_ctx *c = arg;
  323. struct damos *s;
  324. /* update the stats parameter */
  325. damon_for_each_scheme(s, c) {
  326. if (s->action == DAMOS_LRU_PRIO)
  327. damon_lru_sort_hot_stat = s->stat;
  328. else if (s->action == DAMOS_LRU_DEPRIO)
  329. damon_lru_sort_cold_stat = s->stat;
  330. }
  331. return damon_lru_sort_handle_commit_inputs();
  332. }
  333. static struct damon_call_control call_control = {
  334. .fn = damon_lru_sort_damon_call_fn,
  335. .repeat = true,
  336. };
  337. static int damon_lru_sort_turn(bool on)
  338. {
  339. int err;
  340. if (!on) {
  341. err = damon_stop(&ctx, 1);
  342. if (!err)
  343. kdamond_pid = -1;
  344. return err;
  345. }
  346. err = damon_lru_sort_apply_parameters();
  347. if (err)
  348. return err;
  349. err = damon_start(&ctx, 1, true);
  350. if (err)
  351. return err;
  352. kdamond_pid = damon_kdamond_pid(ctx);
  353. if (kdamond_pid < 0)
  354. return kdamond_pid;
  355. return damon_call(ctx, &call_control);
  356. }
  357. static int damon_lru_sort_addr_unit_store(const char *val,
  358. const struct kernel_param *kp)
  359. {
  360. unsigned long input_addr_unit;
  361. int err = kstrtoul(val, 0, &input_addr_unit);
  362. if (err)
  363. return err;
  364. if (!input_addr_unit)
  365. return -EINVAL;
  366. addr_unit = input_addr_unit;
  367. return 0;
  368. }
  369. static const struct kernel_param_ops addr_unit_param_ops = {
  370. .set = damon_lru_sort_addr_unit_store,
  371. .get = param_get_ulong,
  372. };
  373. module_param_cb(addr_unit, &addr_unit_param_ops, &addr_unit, 0600);
  374. MODULE_PARM_DESC(addr_unit,
  375. "Scale factor for DAMON_LRU_SORT to ops address conversion (default: 1)");
  376. static int damon_lru_sort_enabled_store(const char *val,
  377. const struct kernel_param *kp)
  378. {
  379. bool is_enabled = enabled;
  380. bool enable;
  381. int err;
  382. err = kstrtobool(val, &enable);
  383. if (err)
  384. return err;
  385. if (is_enabled == enable)
  386. return 0;
  387. /* Called before init function. The function will handle this. */
  388. if (!damon_initialized())
  389. goto set_param_out;
  390. err = damon_lru_sort_turn(enable);
  391. if (err)
  392. return err;
  393. set_param_out:
  394. enabled = enable;
  395. return err;
  396. }
  397. static const struct kernel_param_ops enabled_param_ops = {
  398. .set = damon_lru_sort_enabled_store,
  399. .get = param_get_bool,
  400. };
  401. module_param_cb(enabled, &enabled_param_ops, &enabled, 0600);
  402. MODULE_PARM_DESC(enabled,
  403. "Enable or disable DAMON_LRU_SORT (default: disabled)");
  404. static int __init damon_lru_sort_init(void)
  405. {
  406. int err;
  407. if (!damon_initialized()) {
  408. err = -ENOMEM;
  409. goto out;
  410. }
  411. err = damon_modules_new_paddr_ctx_target(&ctx, &target);
  412. if (err)
  413. goto out;
  414. call_control.data = ctx;
  415. /* 'enabled' has set before this function, probably via command line */
  416. if (enabled)
  417. err = damon_lru_sort_turn(true);
  418. out:
  419. if (err && enabled)
  420. enabled = false;
  421. return err;
  422. }
  423. module_init(damon_lru_sort_init);