xref: /openbmc/linux/mm/damon/ops-common.c (revision def85dce)
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 
8 #include <linux/mmu_notifier.h>
9 #include <linux/page_idle.h>
10 #include <linux/pagemap.h>
11 #include <linux/rmap.h>
12 
13 #include "ops-common.h"
14 
15 /*
16  * Get an online page for a pfn if it's in the LRU list.  Otherwise, returns
17  * NULL.
18  *
19  * The body of this function is stolen from the 'page_idle_get_folio()'.  We
20  * steal rather than reuse it because the code is quite simple.
21  */
22 struct folio *damon_get_folio(unsigned long pfn)
23 {
24 	struct page *page = pfn_to_online_page(pfn);
25 	struct folio *folio;
26 
27 	if (!page || PageTail(page))
28 		return NULL;
29 
30 	folio = page_folio(page);
31 	if (!folio_test_lru(folio) || !folio_try_get(folio))
32 		return NULL;
33 	if (unlikely(page_folio(page) != folio || !folio_test_lru(folio))) {
34 		folio_put(folio);
35 		folio = NULL;
36 	}
37 	return folio;
38 }
39 
40 void damon_ptep_mkold(pte_t *pte, struct mm_struct *mm, unsigned long addr)
41 {
42 	bool referenced = false;
43 	struct folio *folio = damon_get_folio(pte_pfn(*pte));
44 
45 	if (!folio)
46 		return;
47 
48 	if (pte_young(*pte)) {
49 		referenced = true;
50 		*pte = pte_mkold(*pte);
51 	}
52 
53 #ifdef CONFIG_MMU_NOTIFIER
54 	if (mmu_notifier_clear_young(mm, addr, addr + PAGE_SIZE))
55 		referenced = true;
56 #endif /* CONFIG_MMU_NOTIFIER */
57 
58 	if (referenced)
59 		folio_set_young(folio);
60 
61 	folio_set_idle(folio);
62 	folio_put(folio);
63 }
64 
65 void damon_pmdp_mkold(pmd_t *pmd, struct mm_struct *mm, unsigned long addr)
66 {
67 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
68 	bool referenced = false;
69 	struct folio *folio = damon_get_folio(pmd_pfn(*pmd));
70 
71 	if (!folio)
72 		return;
73 
74 	if (pmd_young(*pmd)) {
75 		referenced = true;
76 		*pmd = pmd_mkold(*pmd);
77 	}
78 
79 #ifdef CONFIG_MMU_NOTIFIER
80 	if (mmu_notifier_clear_young(mm, addr, addr + HPAGE_PMD_SIZE))
81 		referenced = true;
82 #endif /* CONFIG_MMU_NOTIFIER */
83 
84 	if (referenced)
85 		folio_set_young(folio);
86 
87 	folio_set_idle(folio);
88 	folio_put(folio);
89 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
90 }
91 
92 #define DAMON_MAX_SUBSCORE	(100)
93 #define DAMON_MAX_AGE_IN_LOG	(32)
94 
95 int damon_hot_score(struct damon_ctx *c, struct damon_region *r,
96 			struct damos *s)
97 {
98 	unsigned int max_nr_accesses;
99 	int freq_subscore;
100 	unsigned int age_in_sec;
101 	int age_in_log, age_subscore;
102 	unsigned int freq_weight = s->quota.weight_nr_accesses;
103 	unsigned int age_weight = s->quota.weight_age;
104 	int hotness;
105 
106 	max_nr_accesses = c->attrs.aggr_interval / c->attrs.sample_interval;
107 	freq_subscore = r->nr_accesses * DAMON_MAX_SUBSCORE / max_nr_accesses;
108 
109 	age_in_sec = (unsigned long)r->age * c->attrs.aggr_interval / 1000000;
110 	for (age_in_log = 0; age_in_log < DAMON_MAX_AGE_IN_LOG && age_in_sec;
111 			age_in_log++, age_in_sec >>= 1)
112 		;
113 
114 	/* If frequency is 0, higher age means it's colder */
115 	if (freq_subscore == 0)
116 		age_in_log *= -1;
117 
118 	/*
119 	 * Now age_in_log is in [-DAMON_MAX_AGE_IN_LOG, DAMON_MAX_AGE_IN_LOG].
120 	 * Scale it to be in [0, 100] and set it as age subscore.
121 	 */
122 	age_in_log += DAMON_MAX_AGE_IN_LOG;
123 	age_subscore = age_in_log * DAMON_MAX_SUBSCORE /
124 		DAMON_MAX_AGE_IN_LOG / 2;
125 
126 	hotness = (freq_weight * freq_subscore + age_weight * age_subscore);
127 	if (freq_weight + age_weight)
128 		hotness /= freq_weight + age_weight;
129 	/*
130 	 * Transform it to fit in [0, DAMOS_MAX_SCORE]
131 	 */
132 	hotness = hotness * DAMOS_MAX_SCORE / DAMON_MAX_SUBSCORE;
133 
134 	return hotness;
135 }
136 
137 int damon_cold_score(struct damon_ctx *c, struct damon_region *r,
138 			struct damos *s)
139 {
140 	int hotness = damon_hot_score(c, r, s);
141 
142 	/* Return coldness of the region */
143 	return DAMOS_MAX_SCORE - hotness;
144 }
145