thp: allow mlocked THP again
[linux/fpc-iii.git] / mm / page_idle.c
blob1c245d9027e38f259028651f82ee47051aeffe73
1 #include <linux/init.h>
2 #include <linux/bootmem.h>
3 #include <linux/fs.h>
4 #include <linux/sysfs.h>
5 #include <linux/kobject.h>
6 #include <linux/mm.h>
7 #include <linux/mmzone.h>
8 #include <linux/pagemap.h>
9 #include <linux/rmap.h>
10 #include <linux/mmu_notifier.h>
11 #include <linux/page_ext.h>
12 #include <linux/page_idle.h>
14 #define BITMAP_CHUNK_SIZE sizeof(u64)
15 #define BITMAP_CHUNK_BITS (BITMAP_CHUNK_SIZE * BITS_PER_BYTE)
18 * Idle page tracking only considers user memory pages, for other types of
19 * pages the idle flag is always unset and an attempt to set it is silently
20 * ignored.
22 * We treat a page as a user memory page if it is on an LRU list, because it is
23 * always safe to pass such a page to rmap_walk(), which is essential for idle
24 * page tracking. With such an indicator of user pages we can skip isolated
25 * pages, but since there are not usually many of them, it will hardly affect
26 * the overall result.
28 * This function tries to get a user memory page by pfn as described above.
30 static struct page *page_idle_get_page(unsigned long pfn)
32 struct page *page;
33 struct zone *zone;
35 if (!pfn_valid(pfn))
36 return NULL;
38 page = pfn_to_page(pfn);
39 if (!page || !PageLRU(page) ||
40 !get_page_unless_zero(page))
41 return NULL;
43 zone = page_zone(page);
44 spin_lock_irq(&zone->lru_lock);
45 if (unlikely(!PageLRU(page))) {
46 put_page(page);
47 page = NULL;
49 spin_unlock_irq(&zone->lru_lock);
50 return page;
53 static int page_idle_clear_pte_refs_one(struct page *page,
54 struct vm_area_struct *vma,
55 unsigned long addr, void *arg)
57 struct mm_struct *mm = vma->vm_mm;
58 spinlock_t *ptl;
59 pmd_t *pmd;
60 pte_t *pte;
61 bool referenced = false;
63 if (unlikely(PageTransHuge(page))) {
64 pmd = page_check_address_pmd(page, mm, addr, &ptl);
65 if (pmd) {
66 referenced = pmdp_clear_young_notify(vma, addr, pmd);
67 spin_unlock(ptl);
69 } else {
70 pte = page_check_address(page, mm, addr, &ptl, 0);
71 if (pte) {
72 referenced = ptep_clear_young_notify(vma, addr, pte);
73 pte_unmap_unlock(pte, ptl);
76 if (referenced) {
77 clear_page_idle(page);
79 * We cleared the referenced bit in a mapping to this page. To
80 * avoid interference with page reclaim, mark it young so that
81 * page_referenced() will return > 0.
83 set_page_young(page);
85 return SWAP_AGAIN;
88 static void page_idle_clear_pte_refs(struct page *page)
91 * Since rwc.arg is unused, rwc is effectively immutable, so we
92 * can make it static const to save some cycles and stack.
94 static const struct rmap_walk_control rwc = {
95 .rmap_one = page_idle_clear_pte_refs_one,
96 .anon_lock = page_lock_anon_vma_read,
98 bool need_lock;
100 if (!page_mapped(page) ||
101 !page_rmapping(page))
102 return;
104 need_lock = !PageAnon(page) || PageKsm(page);
105 if (need_lock && !trylock_page(page))
106 return;
108 rmap_walk(page, (struct rmap_walk_control *)&rwc);
110 if (need_lock)
111 unlock_page(page);
114 static ssize_t page_idle_bitmap_read(struct file *file, struct kobject *kobj,
115 struct bin_attribute *attr, char *buf,
116 loff_t pos, size_t count)
118 u64 *out = (u64 *)buf;
119 struct page *page;
120 unsigned long pfn, end_pfn;
121 int bit;
123 if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
124 return -EINVAL;
126 pfn = pos * BITS_PER_BYTE;
127 if (pfn >= max_pfn)
128 return 0;
130 end_pfn = pfn + count * BITS_PER_BYTE;
131 if (end_pfn > max_pfn)
132 end_pfn = ALIGN(max_pfn, BITMAP_CHUNK_BITS);
134 for (; pfn < end_pfn; pfn++) {
135 bit = pfn % BITMAP_CHUNK_BITS;
136 if (!bit)
137 *out = 0ULL;
138 page = page_idle_get_page(pfn);
139 if (page) {
140 if (page_is_idle(page)) {
142 * The page might have been referenced via a
143 * pte, in which case it is not idle. Clear
144 * refs and recheck.
146 page_idle_clear_pte_refs(page);
147 if (page_is_idle(page))
148 *out |= 1ULL << bit;
150 put_page(page);
152 if (bit == BITMAP_CHUNK_BITS - 1)
153 out++;
154 cond_resched();
156 return (char *)out - buf;
159 static ssize_t page_idle_bitmap_write(struct file *file, struct kobject *kobj,
160 struct bin_attribute *attr, char *buf,
161 loff_t pos, size_t count)
163 const u64 *in = (u64 *)buf;
164 struct page *page;
165 unsigned long pfn, end_pfn;
166 int bit;
168 if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
169 return -EINVAL;
171 pfn = pos * BITS_PER_BYTE;
172 if (pfn >= max_pfn)
173 return -ENXIO;
175 end_pfn = pfn + count * BITS_PER_BYTE;
176 if (end_pfn > max_pfn)
177 end_pfn = ALIGN(max_pfn, BITMAP_CHUNK_BITS);
179 for (; pfn < end_pfn; pfn++) {
180 bit = pfn % BITMAP_CHUNK_BITS;
181 if ((*in >> bit) & 1) {
182 page = page_idle_get_page(pfn);
183 if (page) {
184 page_idle_clear_pte_refs(page);
185 set_page_idle(page);
186 put_page(page);
189 if (bit == BITMAP_CHUNK_BITS - 1)
190 in++;
191 cond_resched();
193 return (char *)in - buf;
196 static struct bin_attribute page_idle_bitmap_attr =
197 __BIN_ATTR(bitmap, S_IRUSR | S_IWUSR,
198 page_idle_bitmap_read, page_idle_bitmap_write, 0);
200 static struct bin_attribute *page_idle_bin_attrs[] = {
201 &page_idle_bitmap_attr,
202 NULL,
205 static struct attribute_group page_idle_attr_group = {
206 .bin_attrs = page_idle_bin_attrs,
207 .name = "page_idle",
210 #ifndef CONFIG_64BIT
211 static bool need_page_idle(void)
213 return true;
215 struct page_ext_operations page_idle_ops = {
216 .need = need_page_idle,
218 #endif
220 static int __init page_idle_init(void)
222 int err;
224 err = sysfs_create_group(mm_kobj, &page_idle_attr_group);
225 if (err) {
226 pr_err("page_idle: register sysfs failed\n");
227 return err;
229 return 0;
231 subsys_initcall(page_idle_init);