prmtv-common.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Common Primitives for Data Access Monitoring
  4. *
  5. * Author: SeongJae Park <sj@kernel.org>
  6. */
  7. #include <linux/mmu_notifier.h>
  8. #include <linux/page_idle.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/rmap.h>
  11. #include "prmtv-common.h"
  12. /*
  13. * Get an online page for a pfn if it's in the LRU list. Otherwise, returns
  14. * NULL.
  15. *
  16. * The body of this function is stolen from the 'page_idle_get_page()'. We
  17. * steal rather than reuse it because the code is quite simple.
  18. */
  19. struct page *damon_get_page(unsigned long pfn)
  20. {
  21. struct page *page = pfn_to_online_page(pfn);
  22. if (!page || !PageLRU(page) || !get_page_unless_zero(page))
  23. return NULL;
  24. if (unlikely(!PageLRU(page))) {
  25. put_page(page);
  26. page = NULL;
  27. }
  28. return page;
  29. }
  30. void damon_ptep_mkold(pte_t *pte, struct mm_struct *mm, unsigned long addr)
  31. {
  32. bool referenced = false;
  33. struct page *page = damon_get_page(pte_pfn(*pte));
  34. if (!page)
  35. return;
  36. if (pte_young(*pte)) {
  37. referenced = true;
  38. *pte = pte_mkold(*pte);
  39. }
  40. #ifdef CONFIG_MMU_NOTIFIER
  41. if (mmu_notifier_clear_young(mm, addr, addr + PAGE_SIZE))
  42. referenced = true;
  43. #endif /* CONFIG_MMU_NOTIFIER */
  44. if (referenced)
  45. set_page_young(page);
  46. set_page_idle(page);
  47. put_page(page);
  48. }
  49. void damon_pmdp_mkold(pmd_t *pmd, struct mm_struct *mm, unsigned long addr)
  50. {
  51. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  52. bool referenced = false;
  53. struct page *page = damon_get_page(pmd_pfn(*pmd));
  54. if (!page)
  55. return;
  56. if (pmd_young(*pmd)) {
  57. referenced = true;
  58. *pmd = pmd_mkold(*pmd);
  59. }
  60. #ifdef CONFIG_MMU_NOTIFIER
  61. if (mmu_notifier_clear_young(mm, addr,
  62. addr + ((1UL) << HPAGE_PMD_SHIFT)))
  63. referenced = true;
  64. #endif /* CONFIG_MMU_NOTIFIER */
  65. if (referenced)
  66. set_page_young(page);
  67. set_page_idle(page);
  68. put_page(page);
  69. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  70. }
  71. #define DAMON_MAX_SUBSCORE (100)
  72. #define DAMON_MAX_AGE_IN_LOG (32)
  73. int damon_pageout_score(struct damon_ctx *c, struct damon_region *r,
  74. struct damos *s)
  75. {
  76. unsigned int max_nr_accesses;
  77. int freq_subscore;
  78. unsigned int age_in_sec;
  79. int age_in_log, age_subscore;
  80. unsigned int freq_weight = s->quota.weight_nr_accesses;
  81. unsigned int age_weight = s->quota.weight_age;
  82. int hotness;
  83. max_nr_accesses = c->aggr_interval / c->sample_interval;
  84. freq_subscore = r->nr_accesses * DAMON_MAX_SUBSCORE / max_nr_accesses;
  85. age_in_sec = (unsigned long)r->age * c->aggr_interval / 1000000;
  86. for (age_in_log = 0; age_in_log < DAMON_MAX_AGE_IN_LOG && age_in_sec;
  87. age_in_log++, age_in_sec >>= 1)
  88. ;
  89. /* If frequency is 0, higher age means it's colder */
  90. if (freq_subscore == 0)
  91. age_in_log *= -1;
  92. /*
  93. * Now age_in_log is in [-DAMON_MAX_AGE_IN_LOG, DAMON_MAX_AGE_IN_LOG].
  94. * Scale it to be in [0, 100] and set it as age subscore.
  95. */
  96. age_in_log += DAMON_MAX_AGE_IN_LOG;
  97. age_subscore = age_in_log * DAMON_MAX_SUBSCORE /
  98. DAMON_MAX_AGE_IN_LOG / 2;
  99. hotness = (freq_weight * freq_subscore + age_weight * age_subscore);
  100. if (freq_weight + age_weight)
  101. hotness /= freq_weight + age_weight;
  102. /*
  103. * Transform it to fit in [0, DAMOS_MAX_SCORE]
  104. */
  105. hotness = hotness * DAMOS_MAX_SCORE / DAMON_MAX_SUBSCORE;
  106. /* Return coldness of the region */
  107. return DAMOS_MAX_SCORE - hotness;
  108. }