lru_sort.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 parametrs 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. * Access frequency threshold for hot memory regions identification in permil.
  39. *
  40. * If a memory region is accessed in frequency of this or higher,
  41. * DAMON_LRU_SORT identifies the region as hot, and mark it as accessed on the
  42. * LRU list, so that it could not be reclaimed under memory pressure. 50% by
  43. * default.
  44. */
  45. static unsigned long hot_thres_access_freq = 500;
  46. module_param(hot_thres_access_freq, ulong, 0600);
  47. /*
  48. * Time threshold for cold memory regions identification in microseconds.
  49. *
  50. * If a memory region is not accessed for this or longer time, DAMON_LRU_SORT
  51. * identifies the region as cold, and mark it as unaccessed on the LRU list, so
  52. * that it could be reclaimed first under memory pressure. 120 seconds by
  53. * default.
  54. */
  55. static unsigned long cold_min_age __read_mostly = 120000000;
  56. module_param(cold_min_age, ulong, 0600);
  57. static struct damos_quota damon_lru_sort_quota = {
  58. /* Use up to 10 ms per 1 sec, by default */
  59. .ms = 10,
  60. .sz = 0,
  61. .reset_interval = 1000,
  62. /* Within the quota, mark hotter regions accessed first. */
  63. .weight_sz = 0,
  64. .weight_nr_accesses = 1,
  65. .weight_age = 0,
  66. };
  67. DEFINE_DAMON_MODULES_DAMOS_TIME_QUOTA(damon_lru_sort_quota);
  68. static struct damos_watermarks damon_lru_sort_wmarks = {
  69. .metric = DAMOS_WMARK_FREE_MEM_RATE,
  70. .interval = 5000000, /* 5 seconds */
  71. .high = 200, /* 20 percent */
  72. .mid = 150, /* 15 percent */
  73. .low = 50, /* 5 percent */
  74. };
  75. DEFINE_DAMON_MODULES_WMARKS_PARAMS(damon_lru_sort_wmarks);
  76. static struct damon_attrs damon_lru_sort_mon_attrs = {
  77. .sample_interval = 5000, /* 5 ms */
  78. .aggr_interval = 100000, /* 100 ms */
  79. .ops_update_interval = 0,
  80. .min_nr_regions = 10,
  81. .max_nr_regions = 1000,
  82. };
  83. DEFINE_DAMON_MODULES_MON_ATTRS_PARAMS(damon_lru_sort_mon_attrs);
  84. /*
  85. * Start of the target memory region in physical address.
  86. *
  87. * The start physical address of memory region that DAMON_LRU_SORT will do work
  88. * against. By default, biggest System RAM is used as the region.
  89. */
  90. static unsigned long monitor_region_start __read_mostly;
  91. module_param(monitor_region_start, ulong, 0600);
  92. /*
  93. * End of the target memory region in physical address.
  94. *
  95. * The end physical address of memory region that DAMON_LRU_SORT will do work
  96. * against. By default, biggest System RAM is used as the region.
  97. */
  98. static unsigned long monitor_region_end __read_mostly;
  99. module_param(monitor_region_end, ulong, 0600);
  100. /*
  101. * PID of the DAMON thread
  102. *
  103. * If DAMON_LRU_SORT is enabled, this becomes the PID of the worker thread.
  104. * Else, -1.
  105. */
  106. static int kdamond_pid __read_mostly = -1;
  107. module_param(kdamond_pid, int, 0400);
  108. static struct damos_stat damon_lru_sort_hot_stat;
  109. DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_hot_stat,
  110. lru_sort_tried_hot_regions, lru_sorted_hot_regions,
  111. hot_quota_exceeds);
  112. static struct damos_stat damon_lru_sort_cold_stat;
  113. DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat,
  114. lru_sort_tried_cold_regions, lru_sorted_cold_regions,
  115. cold_quota_exceeds);
  116. static struct damos_access_pattern damon_lru_sort_stub_pattern = {
  117. /* Find regions having PAGE_SIZE or larger size */
  118. .min_sz_region = PAGE_SIZE,
  119. .max_sz_region = ULONG_MAX,
  120. /* no matter its access frequency */
  121. .min_nr_accesses = 0,
  122. .max_nr_accesses = UINT_MAX,
  123. /* no matter its age */
  124. .min_age_region = 0,
  125. .max_age_region = UINT_MAX,
  126. };
  127. static struct damon_ctx *ctx;
  128. static struct damon_target *target;
  129. static struct damos *damon_lru_sort_new_scheme(
  130. struct damos_access_pattern *pattern, enum damos_action action)
  131. {
  132. struct damos_quota quota = damon_lru_sort_quota;
  133. /* Use half of total quota for hot/cold pages sorting */
  134. quota.ms = quota.ms / 2;
  135. return damon_new_scheme(
  136. /* find the pattern, and */
  137. pattern,
  138. /* (de)prioritize on LRU-lists */
  139. action,
  140. /* for each aggregation interval */
  141. 0,
  142. /* under the quota. */
  143. &quota,
  144. /* (De)activate this according to the watermarks. */
  145. &damon_lru_sort_wmarks,
  146. NUMA_NO_NODE);
  147. }
  148. /* Create a DAMON-based operation scheme for hot memory regions */
  149. static struct damos *damon_lru_sort_new_hot_scheme(unsigned int hot_thres)
  150. {
  151. struct damos_access_pattern pattern = damon_lru_sort_stub_pattern;
  152. pattern.min_nr_accesses = hot_thres;
  153. return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_PRIO);
  154. }
  155. /* Create a DAMON-based operation scheme for cold memory regions */
  156. static struct damos *damon_lru_sort_new_cold_scheme(unsigned int cold_thres)
  157. {
  158. struct damos_access_pattern pattern = damon_lru_sort_stub_pattern;
  159. pattern.max_nr_accesses = 0;
  160. pattern.min_age_region = cold_thres;
  161. return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_DEPRIO);
  162. }
  163. static int damon_lru_sort_apply_parameters(void)
  164. {
  165. struct damon_ctx *param_ctx;
  166. struct damon_target *param_target;
  167. struct damos *hot_scheme, *cold_scheme;
  168. unsigned int hot_thres, cold_thres;
  169. int err;
  170. err = damon_modules_new_paddr_ctx_target(&param_ctx, &param_target);
  171. if (err)
  172. return err;
  173. err = damon_set_attrs(ctx, &damon_lru_sort_mon_attrs);
  174. if (err)
  175. goto out;
  176. err = -ENOMEM;
  177. hot_thres = damon_max_nr_accesses(&damon_lru_sort_mon_attrs) *
  178. hot_thres_access_freq / 1000;
  179. hot_scheme = damon_lru_sort_new_hot_scheme(hot_thres);
  180. if (!hot_scheme)
  181. goto out;
  182. cold_thres = cold_min_age / damon_lru_sort_mon_attrs.aggr_interval;
  183. cold_scheme = damon_lru_sort_new_cold_scheme(cold_thres);
  184. if (!cold_scheme) {
  185. damon_destroy_scheme(hot_scheme);
  186. goto out;
  187. }
  188. damon_set_schemes(param_ctx, &hot_scheme, 1);
  189. damon_add_scheme(param_ctx, cold_scheme);
  190. err = damon_set_region_biggest_system_ram_default(param_target,
  191. &monitor_region_start,
  192. &monitor_region_end);
  193. if (err)
  194. goto out;
  195. err = damon_commit_ctx(ctx, param_ctx);
  196. out:
  197. damon_destroy_ctx(param_ctx);
  198. return err;
  199. }
  200. static int damon_lru_sort_turn(bool on)
  201. {
  202. int err;
  203. if (!on) {
  204. err = damon_stop(&ctx, 1);
  205. if (!err)
  206. kdamond_pid = -1;
  207. return err;
  208. }
  209. err = damon_lru_sort_apply_parameters();
  210. if (err)
  211. return err;
  212. err = damon_start(&ctx, 1, true);
  213. if (err)
  214. return err;
  215. kdamond_pid = ctx->kdamond->pid;
  216. return 0;
  217. }
  218. static int damon_lru_sort_enabled_store(const char *val,
  219. const struct kernel_param *kp)
  220. {
  221. bool is_enabled = enabled;
  222. bool enable;
  223. int err;
  224. err = kstrtobool(val, &enable);
  225. if (err)
  226. return err;
  227. if (is_enabled == enable)
  228. return 0;
  229. /* Called before init function. The function will handle this. */
  230. if (!ctx)
  231. goto set_param_out;
  232. err = damon_lru_sort_turn(enable);
  233. if (err)
  234. return err;
  235. set_param_out:
  236. enabled = enable;
  237. return err;
  238. }
  239. static const struct kernel_param_ops enabled_param_ops = {
  240. .set = damon_lru_sort_enabled_store,
  241. .get = param_get_bool,
  242. };
  243. module_param_cb(enabled, &enabled_param_ops, &enabled, 0600);
  244. MODULE_PARM_DESC(enabled,
  245. "Enable or disable DAMON_LRU_SORT (default: disabled)");
  246. static int damon_lru_sort_handle_commit_inputs(void)
  247. {
  248. int err;
  249. if (!commit_inputs)
  250. return 0;
  251. err = damon_lru_sort_apply_parameters();
  252. commit_inputs = false;
  253. return err;
  254. }
  255. static int damon_lru_sort_after_aggregation(struct damon_ctx *c)
  256. {
  257. struct damos *s;
  258. /* update the stats parameter */
  259. damon_for_each_scheme(s, c) {
  260. if (s->action == DAMOS_LRU_PRIO)
  261. damon_lru_sort_hot_stat = s->stat;
  262. else if (s->action == DAMOS_LRU_DEPRIO)
  263. damon_lru_sort_cold_stat = s->stat;
  264. }
  265. return damon_lru_sort_handle_commit_inputs();
  266. }
  267. static int damon_lru_sort_after_wmarks_check(struct damon_ctx *c)
  268. {
  269. return damon_lru_sort_handle_commit_inputs();
  270. }
  271. static int __init damon_lru_sort_init(void)
  272. {
  273. int err = damon_modules_new_paddr_ctx_target(&ctx, &target);
  274. if (err)
  275. return err;
  276. ctx->callback.after_wmarks_check = damon_lru_sort_after_wmarks_check;
  277. ctx->callback.after_aggregation = damon_lru_sort_after_aggregation;
  278. /* 'enabled' has set before this function, probably via command line */
  279. if (enabled)
  280. err = damon_lru_sort_turn(true);
  281. return err;
  282. }
  283. module_init(damon_lru_sort_init);