Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 2 | #include <linux/init.h> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 3 | #include <linux/memblock.h> |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 4 | #include <linux/fs.h> |
| 5 | #include <linux/sysfs.h> |
| 6 | #include <linux/kobject.h> |
SeongJae Park | 92fb1db | 2020-06-07 21:40:04 -0700 | [diff] [blame] | 7 | #include <linux/memory_hotplug.h> |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 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 | { |
SeongJae Park | 92fb1db | 2020-06-07 21:40:04 -0700 | [diff] [blame] | 34 | struct page *page = pfn_to_online_page(pfn); |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 35 | |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 36 | if (!page || !PageLRU(page) || |
| 37 | !get_page_unless_zero(page)) |
| 38 | return NULL; |
| 39 | |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 40 | if (unlikely(!PageLRU(page))) { |
| 41 | put_page(page); |
| 42 | page = NULL; |
| 43 | } |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 44 | return page; |
| 45 | } |
| 46 | |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 47 | static bool page_idle_clear_pte_refs_one(struct page *page, |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 48 | struct vm_area_struct *vma, |
| 49 | unsigned long addr, void *arg) |
| 50 | { |
Kirill A. Shutemov | 699fa21 | 2017-02-24 14:57:51 -0800 | [diff] [blame] | 51 | struct page_vma_mapped_walk pvmw = { |
| 52 | .page = page, |
| 53 | .vma = vma, |
| 54 | .address = addr, |
| 55 | }; |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 56 | bool referenced = false; |
| 57 | |
Kirill A. Shutemov | 699fa21 | 2017-02-24 14:57:51 -0800 | [diff] [blame] | 58 | while (page_vma_mapped_walk(&pvmw)) { |
| 59 | addr = pvmw.address; |
| 60 | if (pvmw.pte) { |
Yang Shi | f0849ac | 2018-04-05 16:22:35 -0700 | [diff] [blame] | 61 | /* |
| 62 | * For PTE-mapped THP, one sub page is referenced, |
| 63 | * the whole THP is referenced. |
| 64 | */ |
| 65 | if (ptep_clear_young_notify(vma, addr, pvmw.pte)) |
| 66 | referenced = true; |
Kirill A. Shutemov | 699fa21 | 2017-02-24 14:57:51 -0800 | [diff] [blame] | 67 | } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { |
Yang Shi | f0849ac | 2018-04-05 16:22:35 -0700 | [diff] [blame] | 68 | if (pmdp_clear_young_notify(vma, addr, pvmw.pmd)) |
| 69 | referenced = true; |
Kirill A. Shutemov | 699fa21 | 2017-02-24 14:57:51 -0800 | [diff] [blame] | 70 | } else { |
| 71 | /* unexpected pmd-mapped page? */ |
| 72 | WARN_ON_ONCE(1); |
| 73 | } |
Kirill A. Shutemov | b20ce5e | 2016-01-15 16:54:37 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 76 | if (referenced) { |
| 77 | clear_page_idle(page); |
| 78 | /* |
| 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. |
| 82 | */ |
| 83 | set_page_young(page); |
| 84 | } |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 85 | return true; |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static void page_idle_clear_pte_refs(struct page *page) |
| 89 | { |
| 90 | /* |
| 91 | * Since rwc.arg is unused, rwc is effectively immutable, so we |
| 92 | * can make it static const to save some cycles and stack. |
| 93 | */ |
| 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, |
| 97 | }; |
| 98 | bool need_lock; |
| 99 | |
| 100 | if (!page_mapped(page) || |
| 101 | !page_rmapping(page)) |
| 102 | return; |
| 103 | |
| 104 | need_lock = !PageAnon(page) || PageKsm(page); |
| 105 | if (need_lock && !trylock_page(page)) |
| 106 | return; |
| 107 | |
| 108 | rmap_walk(page, (struct rmap_walk_control *)&rwc); |
| 109 | |
| 110 | if (need_lock) |
| 111 | unlock_page(page); |
| 112 | } |
| 113 | |
| 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) |
| 117 | { |
| 118 | u64 *out = (u64 *)buf; |
| 119 | struct page *page; |
| 120 | unsigned long pfn, end_pfn; |
| 121 | int bit; |
| 122 | |
| 123 | if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE) |
| 124 | return -EINVAL; |
| 125 | |
| 126 | pfn = pos * BITS_PER_BYTE; |
| 127 | if (pfn >= max_pfn) |
| 128 | return 0; |
| 129 | |
| 130 | end_pfn = pfn + count * BITS_PER_BYTE; |
| 131 | if (end_pfn > max_pfn) |
Colin Ian King | 7298e3b | 2019-06-28 12:07:05 -0700 | [diff] [blame] | 132 | end_pfn = max_pfn; |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 133 | |
| 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)) { |
| 141 | /* |
| 142 | * The page might have been referenced via a |
| 143 | * pte, in which case it is not idle. Clear |
| 144 | * refs and recheck. |
| 145 | */ |
| 146 | page_idle_clear_pte_refs(page); |
| 147 | if (page_is_idle(page)) |
| 148 | *out |= 1ULL << bit; |
| 149 | } |
| 150 | put_page(page); |
| 151 | } |
| 152 | if (bit == BITMAP_CHUNK_BITS - 1) |
| 153 | out++; |
| 154 | cond_resched(); |
| 155 | } |
| 156 | return (char *)out - buf; |
| 157 | } |
| 158 | |
| 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) |
| 162 | { |
| 163 | const u64 *in = (u64 *)buf; |
| 164 | struct page *page; |
| 165 | unsigned long pfn, end_pfn; |
| 166 | int bit; |
| 167 | |
| 168 | if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | pfn = pos * BITS_PER_BYTE; |
| 172 | if (pfn >= max_pfn) |
| 173 | return -ENXIO; |
| 174 | |
| 175 | end_pfn = pfn + count * BITS_PER_BYTE; |
| 176 | if (end_pfn > max_pfn) |
Colin Ian King | 7298e3b | 2019-06-28 12:07:05 -0700 | [diff] [blame] | 177 | end_pfn = max_pfn; |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 178 | |
| 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); |
| 187 | } |
| 188 | } |
| 189 | if (bit == BITMAP_CHUNK_BITS - 1) |
| 190 | in++; |
| 191 | cond_resched(); |
| 192 | } |
| 193 | return (char *)in - buf; |
| 194 | } |
| 195 | |
| 196 | static struct bin_attribute page_idle_bitmap_attr = |
Joe Perches | 0825a6f | 2018-06-14 15:27:58 -0700 | [diff] [blame] | 197 | __BIN_ATTR(bitmap, 0600, |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 198 | page_idle_bitmap_read, page_idle_bitmap_write, 0); |
| 199 | |
| 200 | static struct bin_attribute *page_idle_bin_attrs[] = { |
| 201 | &page_idle_bitmap_attr, |
| 202 | NULL, |
| 203 | }; |
| 204 | |
Arvind Yadav | fd147cb | 2017-09-06 16:21:59 -0700 | [diff] [blame] | 205 | static const struct attribute_group page_idle_attr_group = { |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 206 | .bin_attrs = page_idle_bin_attrs, |
| 207 | .name = "page_idle", |
| 208 | }; |
| 209 | |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 210 | static int __init page_idle_init(void) |
| 211 | { |
| 212 | int err; |
| 213 | |
| 214 | err = sysfs_create_group(mm_kobj, &page_idle_attr_group); |
| 215 | if (err) { |
| 216 | pr_err("page_idle: register sysfs failed\n"); |
| 217 | return err; |
| 218 | } |
| 219 | return 0; |
| 220 | } |
| 221 | subsys_initcall(page_idle_init); |