blob: d5dd79041484588cdfb409e7610d9ea23ee3c370 [file] [log] [blame]
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07001#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>
13
14#define BITMAP_CHUNK_SIZE sizeof(u64)
15#define BITMAP_CHUNK_BITS (BITMAP_CHUNK_SIZE * BITS_PER_BYTE)
16
17/*
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.
21 *
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.
27 *
28 * This function tries to get a user memory page by pfn as described above.
29 */
30static struct page *page_idle_get_page(unsigned long pfn)
31{
32 struct page *page;
33 struct zone *zone;
34
35 if (!pfn_valid(pfn))
36 return NULL;
37
38 page = pfn_to_page(pfn);
39 if (!page || !PageLRU(page) ||
40 !get_page_unless_zero(page))
41 return NULL;
42
43 zone = page_zone(page);
44 spin_lock_irq(&zone->lru_lock);
45 if (unlikely(!PageLRU(page))) {
46 put_page(page);
47 page = NULL;
48 }
49 spin_unlock_irq(&zone->lru_lock);
50 return page;
51}
52
53static int page_idle_clear_pte_refs_one(struct page *page,
54 struct vm_area_struct *vma,
55 unsigned long addr, void *arg)
56{
57 struct mm_struct *mm = vma->vm_mm;
58 spinlock_t *ptl;
59 pmd_t *pmd;
60 pte_t *pte;
61 bool referenced = false;
62
63 if (unlikely(PageTransHuge(page))) {
64 pmd = page_check_address_pmd(page, mm, addr,
65 PAGE_CHECK_ADDRESS_PMD_FLAG, &ptl);
66 if (pmd) {
67 referenced = pmdp_clear_young_notify(vma, addr, pmd);
68 spin_unlock(ptl);
69 }
70 } else {
71 pte = page_check_address(page, mm, addr, &ptl, 0);
72 if (pte) {
73 referenced = ptep_clear_young_notify(vma, addr, pte);
74 pte_unmap_unlock(pte, ptl);
75 }
76 }
77 if (referenced) {
78 clear_page_idle(page);
79 /*
80 * We cleared the referenced bit in a mapping to this page. To
81 * avoid interference with page reclaim, mark it young so that
82 * page_referenced() will return > 0.
83 */
84 set_page_young(page);
85 }
86 return SWAP_AGAIN;
87}
88
89static void page_idle_clear_pte_refs(struct page *page)
90{
91 /*
92 * Since rwc.arg is unused, rwc is effectively immutable, so we
93 * can make it static const to save some cycles and stack.
94 */
95 static const struct rmap_walk_control rwc = {
96 .rmap_one = page_idle_clear_pte_refs_one,
97 .anon_lock = page_lock_anon_vma_read,
98 };
99 bool need_lock;
100
101 if (!page_mapped(page) ||
102 !page_rmapping(page))
103 return;
104
105 need_lock = !PageAnon(page) || PageKsm(page);
106 if (need_lock && !trylock_page(page))
107 return;
108
109 rmap_walk(page, (struct rmap_walk_control *)&rwc);
110
111 if (need_lock)
112 unlock_page(page);
113}
114
115static ssize_t page_idle_bitmap_read(struct file *file, struct kobject *kobj,
116 struct bin_attribute *attr, char *buf,
117 loff_t pos, size_t count)
118{
119 u64 *out = (u64 *)buf;
120 struct page *page;
121 unsigned long pfn, end_pfn;
122 int bit;
123
124 if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
125 return -EINVAL;
126
127 pfn = pos * BITS_PER_BYTE;
128 if (pfn >= max_pfn)
129 return 0;
130
131 end_pfn = pfn + count * BITS_PER_BYTE;
132 if (end_pfn > max_pfn)
133 end_pfn = ALIGN(max_pfn, BITMAP_CHUNK_BITS);
134
135 for (; pfn < end_pfn; pfn++) {
136 bit = pfn % BITMAP_CHUNK_BITS;
137 if (!bit)
138 *out = 0ULL;
139 page = page_idle_get_page(pfn);
140 if (page) {
141 if (page_is_idle(page)) {
142 /*
143 * The page might have been referenced via a
144 * pte, in which case it is not idle. Clear
145 * refs and recheck.
146 */
147 page_idle_clear_pte_refs(page);
148 if (page_is_idle(page))
149 *out |= 1ULL << bit;
150 }
151 put_page(page);
152 }
153 if (bit == BITMAP_CHUNK_BITS - 1)
154 out++;
155 cond_resched();
156 }
157 return (char *)out - buf;
158}
159
160static ssize_t page_idle_bitmap_write(struct file *file, struct kobject *kobj,
161 struct bin_attribute *attr, char *buf,
162 loff_t pos, size_t count)
163{
164 const u64 *in = (u64 *)buf;
165 struct page *page;
166 unsigned long pfn, end_pfn;
167 int bit;
168
169 if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
170 return -EINVAL;
171
172 pfn = pos * BITS_PER_BYTE;
173 if (pfn >= max_pfn)
174 return -ENXIO;
175
176 end_pfn = pfn + count * BITS_PER_BYTE;
177 if (end_pfn > max_pfn)
178 end_pfn = ALIGN(max_pfn, BITMAP_CHUNK_BITS);
179
180 for (; pfn < end_pfn; pfn++) {
181 bit = pfn % BITMAP_CHUNK_BITS;
182 if ((*in >> bit) & 1) {
183 page = page_idle_get_page(pfn);
184 if (page) {
185 page_idle_clear_pte_refs(page);
186 set_page_idle(page);
187 put_page(page);
188 }
189 }
190 if (bit == BITMAP_CHUNK_BITS - 1)
191 in++;
192 cond_resched();
193 }
194 return (char *)in - buf;
195}
196
197static struct bin_attribute page_idle_bitmap_attr =
198 __BIN_ATTR(bitmap, S_IRUSR | S_IWUSR,
199 page_idle_bitmap_read, page_idle_bitmap_write, 0);
200
201static struct bin_attribute *page_idle_bin_attrs[] = {
202 &page_idle_bitmap_attr,
203 NULL,
204};
205
206static struct attribute_group page_idle_attr_group = {
207 .bin_attrs = page_idle_bin_attrs,
208 .name = "page_idle",
209};
210
211#ifndef CONFIG_64BIT
212static bool need_page_idle(void)
213{
214 return true;
215}
216struct page_ext_operations page_idle_ops = {
217 .need = need_page_idle,
218};
219#endif
220
221static int __init page_idle_init(void)
222{
223 int err;
224
225 err = sysfs_create_group(mm_kobj, &page_idle_attr_group);
226 if (err) {
227 pr_err("page_idle: register sysfs failed\n");
228 return err;
229 }
230 return 0;
231}
232subsys_initcall(page_idle_init);