mm: Add DEFINE_PAGE_VMA_WALK and DEFINE_FOLIO_VMA_WALK
[linux-block.git] / mm / page_idle.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
3 #include <linux/memblock.h>
4 #include <linux/fs.h>
5 #include <linux/sysfs.h>
6 #include <linux/kobject.h>
7 #include <linux/memory_hotplug.h>
8 #include <linux/mm.h>
9 #include <linux/mmzone.h>
10 #include <linux/pagemap.h>
11 #include <linux/rmap.h>
12 #include <linux/mmu_notifier.h>
13 #include <linux/page_ext.h>
14 #include <linux/page_idle.h>
15
16 #define BITMAP_CHUNK_SIZE       sizeof(u64)
17 #define BITMAP_CHUNK_BITS       (BITMAP_CHUNK_SIZE * BITS_PER_BYTE)
18
19 /*
20  * Idle page tracking only considers user memory pages, for other types of
21  * pages the idle flag is always unset and an attempt to set it is silently
22  * ignored.
23  *
24  * We treat a page as a user memory page if it is on an LRU list, because it is
25  * always safe to pass such a page to rmap_walk(), which is essential for idle
26  * page tracking. With such an indicator of user pages we can skip isolated
27  * pages, but since there are not usually many of them, it will hardly affect
28  * the overall result.
29  *
30  * This function tries to get a user memory page by pfn as described above.
31  */
32 static struct page *page_idle_get_page(unsigned long pfn)
33 {
34         struct page *page = pfn_to_online_page(pfn);
35
36         if (!page || !PageLRU(page) ||
37             !get_page_unless_zero(page))
38                 return NULL;
39
40         if (unlikely(!PageLRU(page))) {
41                 put_page(page);
42                 page = NULL;
43         }
44         return page;
45 }
46
47 static bool page_idle_clear_pte_refs_one(struct page *page,
48                                         struct vm_area_struct *vma,
49                                         unsigned long addr, void *arg)
50 {
51         DEFINE_PAGE_VMA_WALK(pvmw, page, vma, addr, 0);
52         bool referenced = false;
53
54         while (page_vma_mapped_walk(&pvmw)) {
55                 addr = pvmw.address;
56                 if (pvmw.pte) {
57                         /*
58                          * For PTE-mapped THP, one sub page is referenced,
59                          * the whole THP is referenced.
60                          */
61                         if (ptep_clear_young_notify(vma, addr, pvmw.pte))
62                                 referenced = true;
63                 } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
64                         if (pmdp_clear_young_notify(vma, addr, pvmw.pmd))
65                                 referenced = true;
66                 } else {
67                         /* unexpected pmd-mapped page? */
68                         WARN_ON_ONCE(1);
69                 }
70         }
71
72         if (referenced) {
73                 clear_page_idle(page);
74                 /*
75                  * We cleared the referenced bit in a mapping to this page. To
76                  * avoid interference with page reclaim, mark it young so that
77                  * page_referenced() will return > 0.
78                  */
79                 set_page_young(page);
80         }
81         return true;
82 }
83
84 static void page_idle_clear_pte_refs(struct page *page)
85 {
86         /*
87          * Since rwc.arg is unused, rwc is effectively immutable, so we
88          * can make it static const to save some cycles and stack.
89          */
90         static const struct rmap_walk_control rwc = {
91                 .rmap_one = page_idle_clear_pte_refs_one,
92                 .anon_lock = page_lock_anon_vma_read,
93         };
94         bool need_lock;
95
96         if (!page_mapped(page) ||
97             !page_rmapping(page))
98                 return;
99
100         need_lock = !PageAnon(page) || PageKsm(page);
101         if (need_lock && !trylock_page(page))
102                 return;
103
104         rmap_walk(page, (struct rmap_walk_control *)&rwc);
105
106         if (need_lock)
107                 unlock_page(page);
108 }
109
110 static ssize_t page_idle_bitmap_read(struct file *file, struct kobject *kobj,
111                                      struct bin_attribute *attr, char *buf,
112                                      loff_t pos, size_t count)
113 {
114         u64 *out = (u64 *)buf;
115         struct page *page;
116         unsigned long pfn, end_pfn;
117         int bit;
118
119         if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
120                 return -EINVAL;
121
122         pfn = pos * BITS_PER_BYTE;
123         if (pfn >= max_pfn)
124                 return 0;
125
126         end_pfn = pfn + count * BITS_PER_BYTE;
127         if (end_pfn > max_pfn)
128                 end_pfn = max_pfn;
129
130         for (; pfn < end_pfn; pfn++) {
131                 bit = pfn % BITMAP_CHUNK_BITS;
132                 if (!bit)
133                         *out = 0ULL;
134                 page = page_idle_get_page(pfn);
135                 if (page) {
136                         if (page_is_idle(page)) {
137                                 /*
138                                  * The page might have been referenced via a
139                                  * pte, in which case it is not idle. Clear
140                                  * refs and recheck.
141                                  */
142                                 page_idle_clear_pte_refs(page);
143                                 if (page_is_idle(page))
144                                         *out |= 1ULL << bit;
145                         }
146                         put_page(page);
147                 }
148                 if (bit == BITMAP_CHUNK_BITS - 1)
149                         out++;
150                 cond_resched();
151         }
152         return (char *)out - buf;
153 }
154
155 static ssize_t page_idle_bitmap_write(struct file *file, struct kobject *kobj,
156                                       struct bin_attribute *attr, char *buf,
157                                       loff_t pos, size_t count)
158 {
159         const u64 *in = (u64 *)buf;
160         struct page *page;
161         unsigned long pfn, end_pfn;
162         int bit;
163
164         if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
165                 return -EINVAL;
166
167         pfn = pos * BITS_PER_BYTE;
168         if (pfn >= max_pfn)
169                 return -ENXIO;
170
171         end_pfn = pfn + count * BITS_PER_BYTE;
172         if (end_pfn > max_pfn)
173                 end_pfn = max_pfn;
174
175         for (; pfn < end_pfn; pfn++) {
176                 bit = pfn % BITMAP_CHUNK_BITS;
177                 if ((*in >> bit) & 1) {
178                         page = page_idle_get_page(pfn);
179                         if (page) {
180                                 page_idle_clear_pte_refs(page);
181                                 set_page_idle(page);
182                                 put_page(page);
183                         }
184                 }
185                 if (bit == BITMAP_CHUNK_BITS - 1)
186                         in++;
187                 cond_resched();
188         }
189         return (char *)in - buf;
190 }
191
192 static struct bin_attribute page_idle_bitmap_attr =
193                 __BIN_ATTR(bitmap, 0600,
194                            page_idle_bitmap_read, page_idle_bitmap_write, 0);
195
196 static struct bin_attribute *page_idle_bin_attrs[] = {
197         &page_idle_bitmap_attr,
198         NULL,
199 };
200
201 static const struct attribute_group page_idle_attr_group = {
202         .bin_attrs = page_idle_bin_attrs,
203         .name = "page_idle",
204 };
205
206 static int __init page_idle_init(void)
207 {
208         int err;
209
210         err = sysfs_create_group(mm_kobj, &page_idle_attr_group);
211         if (err) {
212                 pr_err("page_idle: register sysfs failed\n");
213                 return err;
214         }
215         return 0;
216 }
217 subsys_initcall(page_idle_init);