Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Red Hat, Inc. |
| 3 | * |
| 4 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 5 | * the COPYING file in the top-level directory. |
| 6 | */ |
| 7 | |
Andrew Morton | ae3a8c1 | 2014-06-04 16:06:58 -0700 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 10 | #include <linux/mm.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/highmem.h> |
| 13 | #include <linux/hugetlb.h> |
| 14 | #include <linux/mmu_notifier.h> |
| 15 | #include <linux/rmap.h> |
| 16 | #include <linux/swap.h> |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 17 | #include <linux/shrinker.h> |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 18 | #include <linux/mm_inline.h> |
Matthew Wilcox | 4897c76 | 2015-09-08 14:58:45 -0700 | [diff] [blame] | 19 | #include <linux/dax.h> |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 20 | #include <linux/kthread.h> |
| 21 | #include <linux/khugepaged.h> |
Andrea Arcangeli | 878aee7 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 22 | #include <linux/freezer.h> |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 23 | #include <linux/mman.h> |
Ralf Baechle | 325adeb | 2012-10-15 13:44:56 +0200 | [diff] [blame] | 24 | #include <linux/pagemap.h> |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 25 | #include <linux/migrate.h> |
Sasha Levin | 43b5fbb | 2013-02-22 16:32:27 -0800 | [diff] [blame] | 26 | #include <linux/hashtable.h> |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 27 | #include <linux/userfaultfd_k.h> |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 28 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 29 | #include <asm/tlb.h> |
| 30 | #include <asm/pgalloc.h> |
| 31 | #include "internal.h" |
| 32 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 33 | /* |
Jianguo Wu | 8bfa3f9 | 2013-11-12 15:07:16 -0800 | [diff] [blame] | 34 | * By default transparent hugepage support is disabled in order that avoid |
| 35 | * to risk increase the memory footprint of applications without a guaranteed |
| 36 | * benefit. When transparent hugepage support is enabled, is for all mappings, |
| 37 | * and khugepaged scans all mappings. |
| 38 | * Defrag is invoked by khugepaged hugepage allocations and by page faults |
| 39 | * for all hugepage allocations. |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 40 | */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 41 | unsigned long transparent_hugepage_flags __read_mostly = |
Andrea Arcangeli | 13ece88 | 2011-01-13 15:47:07 -0800 | [diff] [blame] | 42 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 43 | (1<<TRANSPARENT_HUGEPAGE_FLAG)| |
Andrea Arcangeli | 13ece88 | 2011-01-13 15:47:07 -0800 | [diff] [blame] | 44 | #endif |
| 45 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE |
| 46 | (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)| |
| 47 | #endif |
Andrea Arcangeli | d39d33c | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 48 | (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)| |
Kirill A. Shutemov | 79da540 | 2012-12-12 13:51:12 -0800 | [diff] [blame] | 49 | (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)| |
| 50 | (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 51 | |
| 52 | /* default scan 8*512 pte (or vmas) every 30 second */ |
| 53 | static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8; |
| 54 | static unsigned int khugepaged_pages_collapsed; |
| 55 | static unsigned int khugepaged_full_scans; |
| 56 | static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000; |
| 57 | /* during fragmentation poll the hugepage allocator once every minute */ |
| 58 | static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000; |
| 59 | static struct task_struct *khugepaged_thread __read_mostly; |
| 60 | static DEFINE_MUTEX(khugepaged_mutex); |
| 61 | static DEFINE_SPINLOCK(khugepaged_mm_lock); |
| 62 | static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait); |
| 63 | /* |
| 64 | * default collapse hugepages if there is at least one pte mapped like |
| 65 | * it would have happened if the vma was large enough during page |
| 66 | * fault. |
| 67 | */ |
| 68 | static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1; |
| 69 | |
| 70 | static int khugepaged(void *none); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 71 | static int khugepaged_slab_init(void); |
Kirill A. Shutemov | 65ebb64 | 2015-04-15 16:14:20 -0700 | [diff] [blame] | 72 | static void khugepaged_slab_exit(void); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 73 | |
Sasha Levin | 43b5fbb | 2013-02-22 16:32:27 -0800 | [diff] [blame] | 74 | #define MM_SLOTS_HASH_BITS 10 |
| 75 | static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS); |
| 76 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 77 | static struct kmem_cache *mm_slot_cache __read_mostly; |
| 78 | |
| 79 | /** |
| 80 | * struct mm_slot - hash lookup from mm to mm_slot |
| 81 | * @hash: hash collision list |
| 82 | * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head |
| 83 | * @mm: the mm that this information is valid for |
| 84 | */ |
| 85 | struct mm_slot { |
| 86 | struct hlist_node hash; |
| 87 | struct list_head mm_node; |
| 88 | struct mm_struct *mm; |
| 89 | }; |
| 90 | |
| 91 | /** |
| 92 | * struct khugepaged_scan - cursor for scanning |
| 93 | * @mm_head: the head of the mm list to scan |
| 94 | * @mm_slot: the current mm_slot we are scanning |
| 95 | * @address: the next address inside that to be scanned |
| 96 | * |
| 97 | * There is only the one khugepaged_scan instance of this cursor structure. |
| 98 | */ |
| 99 | struct khugepaged_scan { |
| 100 | struct list_head mm_head; |
| 101 | struct mm_slot *mm_slot; |
| 102 | unsigned long address; |
H Hartley Sweeten | 2f1da64 | 2011-10-31 17:09:25 -0700 | [diff] [blame] | 103 | }; |
| 104 | static struct khugepaged_scan khugepaged_scan = { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 105 | .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head), |
| 106 | }; |
| 107 | |
Andrea Arcangeli | f000565 | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 108 | |
| 109 | static int set_recommended_min_free_kbytes(void) |
| 110 | { |
| 111 | struct zone *zone; |
| 112 | int nr_zones = 0; |
| 113 | unsigned long recommended_min; |
Andrea Arcangeli | f000565 | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 114 | |
Andrea Arcangeli | f000565 | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 115 | for_each_populated_zone(zone) |
| 116 | nr_zones++; |
| 117 | |
| 118 | /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */ |
| 119 | recommended_min = pageblock_nr_pages * nr_zones * 2; |
| 120 | |
| 121 | /* |
| 122 | * Make sure that on average at least two pageblocks are almost free |
| 123 | * of another type, one for a migratetype to fall back to and a |
| 124 | * second to avoid subsequent fallbacks of other types There are 3 |
| 125 | * MIGRATE_TYPES we care about. |
| 126 | */ |
| 127 | recommended_min += pageblock_nr_pages * nr_zones * |
| 128 | MIGRATE_PCPTYPES * MIGRATE_PCPTYPES; |
| 129 | |
| 130 | /* don't ever allow to reserve more than 5% of the lowmem */ |
| 131 | recommended_min = min(recommended_min, |
| 132 | (unsigned long) nr_free_buffer_pages() / 20); |
| 133 | recommended_min <<= (PAGE_SHIFT-10); |
| 134 | |
Han Pingtian | 42aa83c | 2014-01-23 15:53:28 -0800 | [diff] [blame] | 135 | if (recommended_min > min_free_kbytes) { |
| 136 | if (user_min_free_kbytes >= 0) |
| 137 | pr_info("raising min_free_kbytes from %d to %lu " |
| 138 | "to help transparent hugepage allocations\n", |
| 139 | min_free_kbytes, recommended_min); |
| 140 | |
Andrea Arcangeli | f000565 | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 141 | min_free_kbytes = recommended_min; |
Han Pingtian | 42aa83c | 2014-01-23 15:53:28 -0800 | [diff] [blame] | 142 | } |
Andrea Arcangeli | f000565 | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 143 | setup_per_zone_wmarks(); |
| 144 | return 0; |
| 145 | } |
Andrea Arcangeli | f000565 | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 146 | |
Kirill A. Shutemov | 79553da | 2015-04-15 16:14:56 -0700 | [diff] [blame] | 147 | static int start_stop_khugepaged(void) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 148 | { |
| 149 | int err = 0; |
| 150 | if (khugepaged_enabled()) { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 151 | if (!khugepaged_thread) |
| 152 | khugepaged_thread = kthread_run(khugepaged, NULL, |
| 153 | "khugepaged"); |
| 154 | if (unlikely(IS_ERR(khugepaged_thread))) { |
Andrew Morton | ae3a8c1 | 2014-06-04 16:06:58 -0700 | [diff] [blame] | 155 | pr_err("khugepaged: kthread_run(khugepaged) failed\n"); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 156 | err = PTR_ERR(khugepaged_thread); |
| 157 | khugepaged_thread = NULL; |
Kirill A. Shutemov | 79553da | 2015-04-15 16:14:56 -0700 | [diff] [blame] | 158 | goto fail; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 159 | } |
Xiao Guangrong | 911891a | 2012-10-08 16:29:41 -0700 | [diff] [blame] | 160 | |
| 161 | if (!list_empty(&khugepaged_scan.mm_head)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 162 | wake_up_interruptible(&khugepaged_wait); |
Andrea Arcangeli | f000565 | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 163 | |
| 164 | set_recommended_min_free_kbytes(); |
Xiao Guangrong | 911891a | 2012-10-08 16:29:41 -0700 | [diff] [blame] | 165 | } else if (khugepaged_thread) { |
Xiao Guangrong | 911891a | 2012-10-08 16:29:41 -0700 | [diff] [blame] | 166 | kthread_stop(khugepaged_thread); |
| 167 | khugepaged_thread = NULL; |
| 168 | } |
Kirill A. Shutemov | 79553da | 2015-04-15 16:14:56 -0700 | [diff] [blame] | 169 | fail: |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 170 | return err; |
| 171 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 172 | |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 173 | static atomic_t huge_zero_refcount; |
Wang, Yalin | 56873f4 | 2015-02-11 15:24:51 -0800 | [diff] [blame] | 174 | struct page *huge_zero_page __read_mostly; |
Kirill A. Shutemov | 4a6c129 | 2012-12-12 13:50:47 -0800 | [diff] [blame] | 175 | |
Matthew Wilcox | fc43704 | 2015-09-08 14:58:51 -0700 | [diff] [blame] | 176 | struct page *get_huge_zero_page(void) |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 177 | { |
| 178 | struct page *zero_page; |
| 179 | retry: |
| 180 | if (likely(atomic_inc_not_zero(&huge_zero_refcount))) |
Jason Low | 4db0c3c | 2015-04-15 16:14:08 -0700 | [diff] [blame] | 181 | return READ_ONCE(huge_zero_page); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 182 | |
| 183 | zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE, |
| 184 | HPAGE_PMD_ORDER); |
Kirill A. Shutemov | d8a8e1f | 2012-12-12 13:51:09 -0800 | [diff] [blame] | 185 | if (!zero_page) { |
| 186 | count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED); |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 187 | return NULL; |
Kirill A. Shutemov | d8a8e1f | 2012-12-12 13:51:09 -0800 | [diff] [blame] | 188 | } |
| 189 | count_vm_event(THP_ZERO_PAGE_ALLOC); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 190 | preempt_disable(); |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 191 | if (cmpxchg(&huge_zero_page, NULL, zero_page)) { |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 192 | preempt_enable(); |
Yu Zhao | 5ddacbe | 2014-10-29 14:50:26 -0700 | [diff] [blame] | 193 | __free_pages(zero_page, compound_order(zero_page)); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 194 | goto retry; |
| 195 | } |
| 196 | |
| 197 | /* We take additional reference here. It will be put back by shrinker */ |
| 198 | atomic_set(&huge_zero_refcount, 2); |
| 199 | preempt_enable(); |
Jason Low | 4db0c3c | 2015-04-15 16:14:08 -0700 | [diff] [blame] | 200 | return READ_ONCE(huge_zero_page); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static void put_huge_zero_page(void) |
| 204 | { |
| 205 | /* |
| 206 | * Counter should never go to zero here. Only shrinker can put |
| 207 | * last reference. |
| 208 | */ |
| 209 | BUG_ON(atomic_dec_and_test(&huge_zero_refcount)); |
| 210 | } |
| 211 | |
Glauber Costa | 4889646 | 2013-08-28 10:18:15 +1000 | [diff] [blame] | 212 | static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink, |
| 213 | struct shrink_control *sc) |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 214 | { |
Glauber Costa | 4889646 | 2013-08-28 10:18:15 +1000 | [diff] [blame] | 215 | /* we can free zero page only if last reference remains */ |
| 216 | return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0; |
| 217 | } |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 218 | |
Glauber Costa | 4889646 | 2013-08-28 10:18:15 +1000 | [diff] [blame] | 219 | static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink, |
| 220 | struct shrink_control *sc) |
| 221 | { |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 222 | if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) { |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 223 | struct page *zero_page = xchg(&huge_zero_page, NULL); |
| 224 | BUG_ON(zero_page == NULL); |
Yu Zhao | 5ddacbe | 2014-10-29 14:50:26 -0700 | [diff] [blame] | 225 | __free_pages(zero_page, compound_order(zero_page)); |
Glauber Costa | 4889646 | 2013-08-28 10:18:15 +1000 | [diff] [blame] | 226 | return HPAGE_PMD_NR; |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static struct shrinker huge_zero_page_shrinker = { |
Glauber Costa | 4889646 | 2013-08-28 10:18:15 +1000 | [diff] [blame] | 233 | .count_objects = shrink_huge_zero_page_count, |
| 234 | .scan_objects = shrink_huge_zero_page_scan, |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 235 | .seeks = DEFAULT_SEEKS, |
| 236 | }; |
| 237 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 238 | #ifdef CONFIG_SYSFS |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 239 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 240 | static ssize_t double_flag_show(struct kobject *kobj, |
| 241 | struct kobj_attribute *attr, char *buf, |
| 242 | enum transparent_hugepage_flag enabled, |
| 243 | enum transparent_hugepage_flag req_madv) |
| 244 | { |
| 245 | if (test_bit(enabled, &transparent_hugepage_flags)) { |
| 246 | VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags)); |
| 247 | return sprintf(buf, "[always] madvise never\n"); |
| 248 | } else if (test_bit(req_madv, &transparent_hugepage_flags)) |
| 249 | return sprintf(buf, "always [madvise] never\n"); |
| 250 | else |
| 251 | return sprintf(buf, "always madvise [never]\n"); |
| 252 | } |
| 253 | static ssize_t double_flag_store(struct kobject *kobj, |
| 254 | struct kobj_attribute *attr, |
| 255 | const char *buf, size_t count, |
| 256 | enum transparent_hugepage_flag enabled, |
| 257 | enum transparent_hugepage_flag req_madv) |
| 258 | { |
| 259 | if (!memcmp("always", buf, |
| 260 | min(sizeof("always")-1, count))) { |
| 261 | set_bit(enabled, &transparent_hugepage_flags); |
| 262 | clear_bit(req_madv, &transparent_hugepage_flags); |
| 263 | } else if (!memcmp("madvise", buf, |
| 264 | min(sizeof("madvise")-1, count))) { |
| 265 | clear_bit(enabled, &transparent_hugepage_flags); |
| 266 | set_bit(req_madv, &transparent_hugepage_flags); |
| 267 | } else if (!memcmp("never", buf, |
| 268 | min(sizeof("never")-1, count))) { |
| 269 | clear_bit(enabled, &transparent_hugepage_flags); |
| 270 | clear_bit(req_madv, &transparent_hugepage_flags); |
| 271 | } else |
| 272 | return -EINVAL; |
| 273 | |
| 274 | return count; |
| 275 | } |
| 276 | |
| 277 | static ssize_t enabled_show(struct kobject *kobj, |
| 278 | struct kobj_attribute *attr, char *buf) |
| 279 | { |
| 280 | return double_flag_show(kobj, attr, buf, |
| 281 | TRANSPARENT_HUGEPAGE_FLAG, |
| 282 | TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG); |
| 283 | } |
| 284 | static ssize_t enabled_store(struct kobject *kobj, |
| 285 | struct kobj_attribute *attr, |
| 286 | const char *buf, size_t count) |
| 287 | { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 288 | ssize_t ret; |
| 289 | |
| 290 | ret = double_flag_store(kobj, attr, buf, count, |
| 291 | TRANSPARENT_HUGEPAGE_FLAG, |
| 292 | TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG); |
| 293 | |
| 294 | if (ret > 0) { |
Xiao Guangrong | 911891a | 2012-10-08 16:29:41 -0700 | [diff] [blame] | 295 | int err; |
| 296 | |
| 297 | mutex_lock(&khugepaged_mutex); |
Kirill A. Shutemov | 79553da | 2015-04-15 16:14:56 -0700 | [diff] [blame] | 298 | err = start_stop_khugepaged(); |
Xiao Guangrong | 911891a | 2012-10-08 16:29:41 -0700 | [diff] [blame] | 299 | mutex_unlock(&khugepaged_mutex); |
| 300 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 301 | if (err) |
| 302 | ret = err; |
| 303 | } |
| 304 | |
| 305 | return ret; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 306 | } |
| 307 | static struct kobj_attribute enabled_attr = |
| 308 | __ATTR(enabled, 0644, enabled_show, enabled_store); |
| 309 | |
| 310 | static ssize_t single_flag_show(struct kobject *kobj, |
| 311 | struct kobj_attribute *attr, char *buf, |
| 312 | enum transparent_hugepage_flag flag) |
| 313 | { |
Ben Hutchings | e27e615 | 2011-04-14 15:22:21 -0700 | [diff] [blame] | 314 | return sprintf(buf, "%d\n", |
| 315 | !!test_bit(flag, &transparent_hugepage_flags)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 316 | } |
Ben Hutchings | e27e615 | 2011-04-14 15:22:21 -0700 | [diff] [blame] | 317 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 318 | static ssize_t single_flag_store(struct kobject *kobj, |
| 319 | struct kobj_attribute *attr, |
| 320 | const char *buf, size_t count, |
| 321 | enum transparent_hugepage_flag flag) |
| 322 | { |
Ben Hutchings | e27e615 | 2011-04-14 15:22:21 -0700 | [diff] [blame] | 323 | unsigned long value; |
| 324 | int ret; |
| 325 | |
| 326 | ret = kstrtoul(buf, 10, &value); |
| 327 | if (ret < 0) |
| 328 | return ret; |
| 329 | if (value > 1) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 330 | return -EINVAL; |
| 331 | |
Ben Hutchings | e27e615 | 2011-04-14 15:22:21 -0700 | [diff] [blame] | 332 | if (value) |
| 333 | set_bit(flag, &transparent_hugepage_flags); |
| 334 | else |
| 335 | clear_bit(flag, &transparent_hugepage_flags); |
| 336 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 337 | return count; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Currently defrag only disables __GFP_NOWAIT for allocation. A blind |
| 342 | * __GFP_REPEAT is too aggressive, it's never worth swapping tons of |
| 343 | * memory just to allocate one more hugepage. |
| 344 | */ |
| 345 | static ssize_t defrag_show(struct kobject *kobj, |
| 346 | struct kobj_attribute *attr, char *buf) |
| 347 | { |
| 348 | return double_flag_show(kobj, attr, buf, |
| 349 | TRANSPARENT_HUGEPAGE_DEFRAG_FLAG, |
| 350 | TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG); |
| 351 | } |
| 352 | static ssize_t defrag_store(struct kobject *kobj, |
| 353 | struct kobj_attribute *attr, |
| 354 | const char *buf, size_t count) |
| 355 | { |
| 356 | return double_flag_store(kobj, attr, buf, count, |
| 357 | TRANSPARENT_HUGEPAGE_DEFRAG_FLAG, |
| 358 | TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG); |
| 359 | } |
| 360 | static struct kobj_attribute defrag_attr = |
| 361 | __ATTR(defrag, 0644, defrag_show, defrag_store); |
| 362 | |
Kirill A. Shutemov | 79da540 | 2012-12-12 13:51:12 -0800 | [diff] [blame] | 363 | static ssize_t use_zero_page_show(struct kobject *kobj, |
| 364 | struct kobj_attribute *attr, char *buf) |
| 365 | { |
| 366 | return single_flag_show(kobj, attr, buf, |
| 367 | TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); |
| 368 | } |
| 369 | static ssize_t use_zero_page_store(struct kobject *kobj, |
| 370 | struct kobj_attribute *attr, const char *buf, size_t count) |
| 371 | { |
| 372 | return single_flag_store(kobj, attr, buf, count, |
| 373 | TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); |
| 374 | } |
| 375 | static struct kobj_attribute use_zero_page_attr = |
| 376 | __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 377 | #ifdef CONFIG_DEBUG_VM |
| 378 | static ssize_t debug_cow_show(struct kobject *kobj, |
| 379 | struct kobj_attribute *attr, char *buf) |
| 380 | { |
| 381 | return single_flag_show(kobj, attr, buf, |
| 382 | TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG); |
| 383 | } |
| 384 | static ssize_t debug_cow_store(struct kobject *kobj, |
| 385 | struct kobj_attribute *attr, |
| 386 | const char *buf, size_t count) |
| 387 | { |
| 388 | return single_flag_store(kobj, attr, buf, count, |
| 389 | TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG); |
| 390 | } |
| 391 | static struct kobj_attribute debug_cow_attr = |
| 392 | __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store); |
| 393 | #endif /* CONFIG_DEBUG_VM */ |
| 394 | |
| 395 | static struct attribute *hugepage_attr[] = { |
| 396 | &enabled_attr.attr, |
| 397 | &defrag_attr.attr, |
Kirill A. Shutemov | 79da540 | 2012-12-12 13:51:12 -0800 | [diff] [blame] | 398 | &use_zero_page_attr.attr, |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 399 | #ifdef CONFIG_DEBUG_VM |
| 400 | &debug_cow_attr.attr, |
| 401 | #endif |
| 402 | NULL, |
| 403 | }; |
| 404 | |
| 405 | static struct attribute_group hugepage_attr_group = { |
| 406 | .attrs = hugepage_attr, |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 407 | }; |
| 408 | |
| 409 | static ssize_t scan_sleep_millisecs_show(struct kobject *kobj, |
| 410 | struct kobj_attribute *attr, |
| 411 | char *buf) |
| 412 | { |
| 413 | return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs); |
| 414 | } |
| 415 | |
| 416 | static ssize_t scan_sleep_millisecs_store(struct kobject *kobj, |
| 417 | struct kobj_attribute *attr, |
| 418 | const char *buf, size_t count) |
| 419 | { |
| 420 | unsigned long msecs; |
| 421 | int err; |
| 422 | |
Jingoo Han | 3dbb95f | 2013-09-11 14:20:25 -0700 | [diff] [blame] | 423 | err = kstrtoul(buf, 10, &msecs); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 424 | if (err || msecs > UINT_MAX) |
| 425 | return -EINVAL; |
| 426 | |
| 427 | khugepaged_scan_sleep_millisecs = msecs; |
| 428 | wake_up_interruptible(&khugepaged_wait); |
| 429 | |
| 430 | return count; |
| 431 | } |
| 432 | static struct kobj_attribute scan_sleep_millisecs_attr = |
| 433 | __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show, |
| 434 | scan_sleep_millisecs_store); |
| 435 | |
| 436 | static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj, |
| 437 | struct kobj_attribute *attr, |
| 438 | char *buf) |
| 439 | { |
| 440 | return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs); |
| 441 | } |
| 442 | |
| 443 | static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj, |
| 444 | struct kobj_attribute *attr, |
| 445 | const char *buf, size_t count) |
| 446 | { |
| 447 | unsigned long msecs; |
| 448 | int err; |
| 449 | |
Jingoo Han | 3dbb95f | 2013-09-11 14:20:25 -0700 | [diff] [blame] | 450 | err = kstrtoul(buf, 10, &msecs); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 451 | if (err || msecs > UINT_MAX) |
| 452 | return -EINVAL; |
| 453 | |
| 454 | khugepaged_alloc_sleep_millisecs = msecs; |
| 455 | wake_up_interruptible(&khugepaged_wait); |
| 456 | |
| 457 | return count; |
| 458 | } |
| 459 | static struct kobj_attribute alloc_sleep_millisecs_attr = |
| 460 | __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show, |
| 461 | alloc_sleep_millisecs_store); |
| 462 | |
| 463 | static ssize_t pages_to_scan_show(struct kobject *kobj, |
| 464 | struct kobj_attribute *attr, |
| 465 | char *buf) |
| 466 | { |
| 467 | return sprintf(buf, "%u\n", khugepaged_pages_to_scan); |
| 468 | } |
| 469 | static ssize_t pages_to_scan_store(struct kobject *kobj, |
| 470 | struct kobj_attribute *attr, |
| 471 | const char *buf, size_t count) |
| 472 | { |
| 473 | int err; |
| 474 | unsigned long pages; |
| 475 | |
Jingoo Han | 3dbb95f | 2013-09-11 14:20:25 -0700 | [diff] [blame] | 476 | err = kstrtoul(buf, 10, &pages); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 477 | if (err || !pages || pages > UINT_MAX) |
| 478 | return -EINVAL; |
| 479 | |
| 480 | khugepaged_pages_to_scan = pages; |
| 481 | |
| 482 | return count; |
| 483 | } |
| 484 | static struct kobj_attribute pages_to_scan_attr = |
| 485 | __ATTR(pages_to_scan, 0644, pages_to_scan_show, |
| 486 | pages_to_scan_store); |
| 487 | |
| 488 | static ssize_t pages_collapsed_show(struct kobject *kobj, |
| 489 | struct kobj_attribute *attr, |
| 490 | char *buf) |
| 491 | { |
| 492 | return sprintf(buf, "%u\n", khugepaged_pages_collapsed); |
| 493 | } |
| 494 | static struct kobj_attribute pages_collapsed_attr = |
| 495 | __ATTR_RO(pages_collapsed); |
| 496 | |
| 497 | static ssize_t full_scans_show(struct kobject *kobj, |
| 498 | struct kobj_attribute *attr, |
| 499 | char *buf) |
| 500 | { |
| 501 | return sprintf(buf, "%u\n", khugepaged_full_scans); |
| 502 | } |
| 503 | static struct kobj_attribute full_scans_attr = |
| 504 | __ATTR_RO(full_scans); |
| 505 | |
| 506 | static ssize_t khugepaged_defrag_show(struct kobject *kobj, |
| 507 | struct kobj_attribute *attr, char *buf) |
| 508 | { |
| 509 | return single_flag_show(kobj, attr, buf, |
| 510 | TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG); |
| 511 | } |
| 512 | static ssize_t khugepaged_defrag_store(struct kobject *kobj, |
| 513 | struct kobj_attribute *attr, |
| 514 | const char *buf, size_t count) |
| 515 | { |
| 516 | return single_flag_store(kobj, attr, buf, count, |
| 517 | TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG); |
| 518 | } |
| 519 | static struct kobj_attribute khugepaged_defrag_attr = |
| 520 | __ATTR(defrag, 0644, khugepaged_defrag_show, |
| 521 | khugepaged_defrag_store); |
| 522 | |
| 523 | /* |
| 524 | * max_ptes_none controls if khugepaged should collapse hugepages over |
| 525 | * any unmapped ptes in turn potentially increasing the memory |
| 526 | * footprint of the vmas. When max_ptes_none is 0 khugepaged will not |
| 527 | * reduce the available free memory in the system as it |
| 528 | * runs. Increasing max_ptes_none will instead potentially reduce the |
| 529 | * free memory in the system during the khugepaged scan. |
| 530 | */ |
| 531 | static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj, |
| 532 | struct kobj_attribute *attr, |
| 533 | char *buf) |
| 534 | { |
| 535 | return sprintf(buf, "%u\n", khugepaged_max_ptes_none); |
| 536 | } |
| 537 | static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj, |
| 538 | struct kobj_attribute *attr, |
| 539 | const char *buf, size_t count) |
| 540 | { |
| 541 | int err; |
| 542 | unsigned long max_ptes_none; |
| 543 | |
Jingoo Han | 3dbb95f | 2013-09-11 14:20:25 -0700 | [diff] [blame] | 544 | err = kstrtoul(buf, 10, &max_ptes_none); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 545 | if (err || max_ptes_none > HPAGE_PMD_NR-1) |
| 546 | return -EINVAL; |
| 547 | |
| 548 | khugepaged_max_ptes_none = max_ptes_none; |
| 549 | |
| 550 | return count; |
| 551 | } |
| 552 | static struct kobj_attribute khugepaged_max_ptes_none_attr = |
| 553 | __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show, |
| 554 | khugepaged_max_ptes_none_store); |
| 555 | |
| 556 | static struct attribute *khugepaged_attr[] = { |
| 557 | &khugepaged_defrag_attr.attr, |
| 558 | &khugepaged_max_ptes_none_attr.attr, |
| 559 | &pages_to_scan_attr.attr, |
| 560 | &pages_collapsed_attr.attr, |
| 561 | &full_scans_attr.attr, |
| 562 | &scan_sleep_millisecs_attr.attr, |
| 563 | &alloc_sleep_millisecs_attr.attr, |
| 564 | NULL, |
| 565 | }; |
| 566 | |
| 567 | static struct attribute_group khugepaged_attr_group = { |
| 568 | .attrs = khugepaged_attr, |
| 569 | .name = "khugepaged", |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 570 | }; |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 571 | |
| 572 | static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj) |
| 573 | { |
| 574 | int err; |
| 575 | |
| 576 | *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj); |
| 577 | if (unlikely(!*hugepage_kobj)) { |
Andrew Morton | ae3a8c1 | 2014-06-04 16:06:58 -0700 | [diff] [blame] | 578 | pr_err("failed to create transparent hugepage kobject\n"); |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 579 | return -ENOMEM; |
| 580 | } |
| 581 | |
| 582 | err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group); |
| 583 | if (err) { |
Andrew Morton | ae3a8c1 | 2014-06-04 16:06:58 -0700 | [diff] [blame] | 584 | pr_err("failed to register transparent hugepage group\n"); |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 585 | goto delete_obj; |
| 586 | } |
| 587 | |
| 588 | err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group); |
| 589 | if (err) { |
Andrew Morton | ae3a8c1 | 2014-06-04 16:06:58 -0700 | [diff] [blame] | 590 | pr_err("failed to register transparent hugepage group\n"); |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 591 | goto remove_hp_group; |
| 592 | } |
| 593 | |
| 594 | return 0; |
| 595 | |
| 596 | remove_hp_group: |
| 597 | sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group); |
| 598 | delete_obj: |
| 599 | kobject_put(*hugepage_kobj); |
| 600 | return err; |
| 601 | } |
| 602 | |
| 603 | static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj) |
| 604 | { |
| 605 | sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group); |
| 606 | sysfs_remove_group(hugepage_kobj, &hugepage_attr_group); |
| 607 | kobject_put(hugepage_kobj); |
| 608 | } |
| 609 | #else |
| 610 | static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj) |
| 611 | { |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj) |
| 616 | { |
| 617 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 618 | #endif /* CONFIG_SYSFS */ |
| 619 | |
| 620 | static int __init hugepage_init(void) |
| 621 | { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 622 | int err; |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 623 | struct kobject *hugepage_kobj; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 624 | |
Andrea Arcangeli | 4b7167b | 2011-01-13 15:47:09 -0800 | [diff] [blame] | 625 | if (!has_transparent_hugepage()) { |
| 626 | transparent_hugepage_flags = 0; |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 627 | return -EINVAL; |
Andrea Arcangeli | 4b7167b | 2011-01-13 15:47:09 -0800 | [diff] [blame] | 628 | } |
| 629 | |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 630 | err = hugepage_init_sysfs(&hugepage_kobj); |
| 631 | if (err) |
Kirill A. Shutemov | 65ebb64 | 2015-04-15 16:14:20 -0700 | [diff] [blame] | 632 | goto err_sysfs; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 633 | |
| 634 | err = khugepaged_slab_init(); |
| 635 | if (err) |
Kirill A. Shutemov | 65ebb64 | 2015-04-15 16:14:20 -0700 | [diff] [blame] | 636 | goto err_slab; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 637 | |
Kirill A. Shutemov | 65ebb64 | 2015-04-15 16:14:20 -0700 | [diff] [blame] | 638 | err = register_shrinker(&huge_zero_page_shrinker); |
| 639 | if (err) |
| 640 | goto err_hzp_shrinker; |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 641 | |
Rik van Riel | 97562cd | 2011-01-13 15:47:12 -0800 | [diff] [blame] | 642 | /* |
| 643 | * By default disable transparent hugepages on smaller systems, |
| 644 | * where the extra memory used could hurt more than TLB overhead |
| 645 | * is likely to save. The admin can still enable it through /sys. |
| 646 | */ |
Kirill A. Shutemov | 79553da | 2015-04-15 16:14:56 -0700 | [diff] [blame] | 647 | if (totalram_pages < (512 << (20 - PAGE_SHIFT))) { |
Rik van Riel | 97562cd | 2011-01-13 15:47:12 -0800 | [diff] [blame] | 648 | transparent_hugepage_flags = 0; |
Kirill A. Shutemov | 79553da | 2015-04-15 16:14:56 -0700 | [diff] [blame] | 649 | return 0; |
| 650 | } |
Rik van Riel | 97562cd | 2011-01-13 15:47:12 -0800 | [diff] [blame] | 651 | |
Kirill A. Shutemov | 79553da | 2015-04-15 16:14:56 -0700 | [diff] [blame] | 652 | err = start_stop_khugepaged(); |
Kirill A. Shutemov | 65ebb64 | 2015-04-15 16:14:20 -0700 | [diff] [blame] | 653 | if (err) |
| 654 | goto err_khugepaged; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 655 | |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 656 | return 0; |
Kirill A. Shutemov | 65ebb64 | 2015-04-15 16:14:20 -0700 | [diff] [blame] | 657 | err_khugepaged: |
| 658 | unregister_shrinker(&huge_zero_page_shrinker); |
| 659 | err_hzp_shrinker: |
| 660 | khugepaged_slab_exit(); |
| 661 | err_slab: |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 662 | hugepage_exit_sysfs(hugepage_kobj); |
Kirill A. Shutemov | 65ebb64 | 2015-04-15 16:14:20 -0700 | [diff] [blame] | 663 | err_sysfs: |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 664 | return err; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 665 | } |
Paul Gortmaker | a64fb3c | 2014-01-23 15:53:30 -0800 | [diff] [blame] | 666 | subsys_initcall(hugepage_init); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 667 | |
| 668 | static int __init setup_transparent_hugepage(char *str) |
| 669 | { |
| 670 | int ret = 0; |
| 671 | if (!str) |
| 672 | goto out; |
| 673 | if (!strcmp(str, "always")) { |
| 674 | set_bit(TRANSPARENT_HUGEPAGE_FLAG, |
| 675 | &transparent_hugepage_flags); |
| 676 | clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, |
| 677 | &transparent_hugepage_flags); |
| 678 | ret = 1; |
| 679 | } else if (!strcmp(str, "madvise")) { |
| 680 | clear_bit(TRANSPARENT_HUGEPAGE_FLAG, |
| 681 | &transparent_hugepage_flags); |
| 682 | set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, |
| 683 | &transparent_hugepage_flags); |
| 684 | ret = 1; |
| 685 | } else if (!strcmp(str, "never")) { |
| 686 | clear_bit(TRANSPARENT_HUGEPAGE_FLAG, |
| 687 | &transparent_hugepage_flags); |
| 688 | clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, |
| 689 | &transparent_hugepage_flags); |
| 690 | ret = 1; |
| 691 | } |
| 692 | out: |
| 693 | if (!ret) |
Andrew Morton | ae3a8c1 | 2014-06-04 16:06:58 -0700 | [diff] [blame] | 694 | pr_warn("transparent_hugepage= cannot parse, ignored\n"); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 695 | return ret; |
| 696 | } |
| 697 | __setup("transparent_hugepage=", setup_transparent_hugepage); |
| 698 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 699 | pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 700 | { |
| 701 | if (likely(vma->vm_flags & VM_WRITE)) |
| 702 | pmd = pmd_mkwrite(pmd); |
| 703 | return pmd; |
| 704 | } |
| 705 | |
Kirill A. Shutemov | 3122359 | 2013-09-12 15:14:01 -0700 | [diff] [blame] | 706 | static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot) |
Bob Liu | b3092b3 | 2012-12-11 16:00:41 -0800 | [diff] [blame] | 707 | { |
| 708 | pmd_t entry; |
Kirill A. Shutemov | 3122359 | 2013-09-12 15:14:01 -0700 | [diff] [blame] | 709 | entry = mk_pmd(page, prot); |
Bob Liu | b3092b3 | 2012-12-11 16:00:41 -0800 | [diff] [blame] | 710 | entry = pmd_mkhuge(entry); |
| 711 | return entry; |
| 712 | } |
| 713 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 714 | static int __do_huge_pmd_anonymous_page(struct mm_struct *mm, |
| 715 | struct vm_area_struct *vma, |
Andrea Arcangeli | 230c92a | 2015-09-04 15:47:20 -0700 | [diff] [blame] | 716 | unsigned long address, pmd_t *pmd, |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 717 | struct page *page, gfp_t gfp, |
| 718 | unsigned int flags) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 719 | { |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 720 | struct mem_cgroup *memcg; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 721 | pgtable_t pgtable; |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 722 | spinlock_t *ptl; |
Andrea Arcangeli | 230c92a | 2015-09-04 15:47:20 -0700 | [diff] [blame] | 723 | unsigned long haddr = address & HPAGE_PMD_MASK; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 724 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 725 | VM_BUG_ON_PAGE(!PageCompound(page), page); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 726 | |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 727 | if (mem_cgroup_try_charge(page, mm, gfp, &memcg)) { |
| 728 | put_page(page); |
| 729 | count_vm_event(THP_FAULT_FALLBACK); |
| 730 | return VM_FAULT_FALLBACK; |
| 731 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 732 | |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 733 | pgtable = pte_alloc_one(mm, haddr); |
| 734 | if (unlikely(!pgtable)) { |
| 735 | mem_cgroup_cancel_charge(page, memcg); |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 736 | put_page(page); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 737 | return VM_FAULT_OOM; |
| 738 | } |
| 739 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 740 | clear_huge_page(page, haddr, HPAGE_PMD_NR); |
Minchan Kim | 52f3762 | 2013-04-29 15:08:15 -0700 | [diff] [blame] | 741 | /* |
| 742 | * The memory barrier inside __SetPageUptodate makes sure that |
| 743 | * clear_huge_page writes become visible before the set_pmd_at() |
| 744 | * write. |
| 745 | */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 746 | __SetPageUptodate(page); |
| 747 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 748 | ptl = pmd_lock(mm, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 749 | if (unlikely(!pmd_none(*pmd))) { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 750 | spin_unlock(ptl); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 751 | mem_cgroup_cancel_charge(page, memcg); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 752 | put_page(page); |
| 753 | pte_free(mm, pgtable); |
| 754 | } else { |
| 755 | pmd_t entry; |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 756 | |
| 757 | /* Deliver the page fault to userland */ |
| 758 | if (userfaultfd_missing(vma)) { |
| 759 | int ret; |
| 760 | |
| 761 | spin_unlock(ptl); |
| 762 | mem_cgroup_cancel_charge(page, memcg); |
| 763 | put_page(page); |
| 764 | pte_free(mm, pgtable); |
Andrea Arcangeli | 230c92a | 2015-09-04 15:47:20 -0700 | [diff] [blame] | 765 | ret = handle_userfault(vma, address, flags, |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 766 | VM_UFFD_MISSING); |
| 767 | VM_BUG_ON(ret & VM_FAULT_FALLBACK); |
| 768 | return ret; |
| 769 | } |
| 770 | |
Kirill A. Shutemov | 3122359 | 2013-09-12 15:14:01 -0700 | [diff] [blame] | 771 | entry = mk_huge_pmd(page, vma->vm_page_prot); |
| 772 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 773 | page_add_new_anon_rmap(page, vma, haddr); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 774 | mem_cgroup_commit_charge(page, memcg, false); |
| 775 | lru_cache_add_active_or_unevictable(page, vma); |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 776 | pgtable_trans_huge_deposit(mm, pmd, pgtable); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 777 | set_pmd_at(mm, haddr, pmd, entry); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 778 | add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR); |
Kirill A. Shutemov | e1f56c8 | 2013-11-14 14:30:48 -0800 | [diff] [blame] | 779 | atomic_long_inc(&mm->nr_ptes); |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 780 | spin_unlock(ptl); |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 781 | count_vm_event(THP_FAULT_ALLOC); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 782 | } |
| 783 | |
David Rientjes | aa2e878 | 2012-05-29 15:06:17 -0700 | [diff] [blame] | 784 | return 0; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 785 | } |
| 786 | |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 787 | static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp) |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 788 | { |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 789 | return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp; |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 790 | } |
| 791 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 792 | /* Caller must hold page table lock. */ |
Matthew Wilcox | 4897c76 | 2015-09-08 14:58:45 -0700 | [diff] [blame] | 793 | bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm, |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 794 | struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd, |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 795 | struct page *zero_page) |
Kirill A. Shutemov | fc9fe82 | 2012-12-12 13:50:51 -0800 | [diff] [blame] | 796 | { |
| 797 | pmd_t entry; |
Andrew Morton | 7c41416 | 2015-09-08 14:58:43 -0700 | [diff] [blame] | 798 | if (!pmd_none(*pmd)) |
| 799 | return false; |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 800 | entry = mk_pmd(zero_page, vma->vm_page_prot); |
Kirill A. Shutemov | fc9fe82 | 2012-12-12 13:50:51 -0800 | [diff] [blame] | 801 | entry = pmd_mkhuge(entry); |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 802 | pgtable_trans_huge_deposit(mm, pmd, pgtable); |
Kirill A. Shutemov | fc9fe82 | 2012-12-12 13:50:51 -0800 | [diff] [blame] | 803 | set_pmd_at(mm, haddr, pmd, entry); |
Kirill A. Shutemov | e1f56c8 | 2013-11-14 14:30:48 -0800 | [diff] [blame] | 804 | atomic_long_inc(&mm->nr_ptes); |
Andrew Morton | 7c41416 | 2015-09-08 14:58:43 -0700 | [diff] [blame] | 805 | return true; |
Kirill A. Shutemov | fc9fe82 | 2012-12-12 13:50:51 -0800 | [diff] [blame] | 806 | } |
| 807 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 808 | int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, |
| 809 | unsigned long address, pmd_t *pmd, |
| 810 | unsigned int flags) |
| 811 | { |
Aneesh Kumar K.V | 077fcf1 | 2015-02-11 15:27:12 -0800 | [diff] [blame] | 812 | gfp_t gfp; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 813 | struct page *page; |
| 814 | unsigned long haddr = address & HPAGE_PMD_MASK; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 815 | |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 816 | if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end) |
Kirill A. Shutemov | c029255 | 2013-09-12 15:14:05 -0700 | [diff] [blame] | 817 | return VM_FAULT_FALLBACK; |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 818 | if (unlikely(anon_vma_prepare(vma))) |
| 819 | return VM_FAULT_OOM; |
David Rientjes | 6d50e60 | 2014-10-29 14:50:31 -0700 | [diff] [blame] | 820 | if (unlikely(khugepaged_enter(vma, vma->vm_flags))) |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 821 | return VM_FAULT_OOM; |
Dominik Dingel | 593befa | 2014-10-23 12:07:44 +0200 | [diff] [blame] | 822 | if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) && |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 823 | transparent_hugepage_use_zero_page()) { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 824 | spinlock_t *ptl; |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 825 | pgtable_t pgtable; |
| 826 | struct page *zero_page; |
| 827 | bool set; |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 828 | int ret; |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 829 | pgtable = pte_alloc_one(mm, haddr); |
| 830 | if (unlikely(!pgtable)) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 831 | return VM_FAULT_OOM; |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 832 | zero_page = get_huge_zero_page(); |
| 833 | if (unlikely(!zero_page)) { |
| 834 | pte_free(mm, pgtable); |
Andi Kleen | 81ab420 | 2011-04-14 15:22:06 -0700 | [diff] [blame] | 835 | count_vm_event(THP_FAULT_FALLBACK); |
Kirill A. Shutemov | c029255 | 2013-09-12 15:14:05 -0700 | [diff] [blame] | 836 | return VM_FAULT_FALLBACK; |
Andi Kleen | 81ab420 | 2011-04-14 15:22:06 -0700 | [diff] [blame] | 837 | } |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 838 | ptl = pmd_lock(mm, pmd); |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 839 | ret = 0; |
| 840 | set = false; |
| 841 | if (pmd_none(*pmd)) { |
| 842 | if (userfaultfd_missing(vma)) { |
| 843 | spin_unlock(ptl); |
Andrea Arcangeli | 230c92a | 2015-09-04 15:47:20 -0700 | [diff] [blame] | 844 | ret = handle_userfault(vma, address, flags, |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 845 | VM_UFFD_MISSING); |
| 846 | VM_BUG_ON(ret & VM_FAULT_FALLBACK); |
| 847 | } else { |
| 848 | set_huge_zero_page(pgtable, mm, vma, |
| 849 | haddr, pmd, |
| 850 | zero_page); |
| 851 | spin_unlock(ptl); |
| 852 | set = true; |
| 853 | } |
| 854 | } else |
| 855 | spin_unlock(ptl); |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 856 | if (!set) { |
| 857 | pte_free(mm, pgtable); |
| 858 | put_huge_zero_page(); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 859 | } |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 860 | return ret; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 861 | } |
Aneesh Kumar K.V | 077fcf1 | 2015-02-11 15:27:12 -0800 | [diff] [blame] | 862 | gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0); |
| 863 | page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER); |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 864 | if (unlikely(!page)) { |
| 865 | count_vm_event(THP_FAULT_FALLBACK); |
Kirill A. Shutemov | c029255 | 2013-09-12 15:14:05 -0700 | [diff] [blame] | 866 | return VM_FAULT_FALLBACK; |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 867 | } |
Andrea Arcangeli | 230c92a | 2015-09-04 15:47:20 -0700 | [diff] [blame] | 868 | return __do_huge_pmd_anonymous_page(mm, vma, address, pmd, page, gfp, |
| 869 | flags); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 870 | } |
| 871 | |
Matthew Wilcox | ae18d6d | 2015-09-08 14:59:14 -0700 | [diff] [blame] | 872 | static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr, |
Matthew Wilcox | 5cad465 | 2015-09-08 14:58:54 -0700 | [diff] [blame] | 873 | pmd_t *pmd, unsigned long pfn, pgprot_t prot, bool write) |
| 874 | { |
| 875 | struct mm_struct *mm = vma->vm_mm; |
| 876 | pmd_t entry; |
| 877 | spinlock_t *ptl; |
| 878 | |
| 879 | ptl = pmd_lock(mm, pmd); |
| 880 | if (pmd_none(*pmd)) { |
| 881 | entry = pmd_mkhuge(pfn_pmd(pfn, prot)); |
| 882 | if (write) { |
| 883 | entry = pmd_mkyoung(pmd_mkdirty(entry)); |
| 884 | entry = maybe_pmd_mkwrite(entry, vma); |
| 885 | } |
| 886 | set_pmd_at(mm, addr, pmd, entry); |
| 887 | update_mmu_cache_pmd(vma, addr, pmd); |
| 888 | } |
| 889 | spin_unlock(ptl); |
Matthew Wilcox | 5cad465 | 2015-09-08 14:58:54 -0700 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr, |
| 893 | pmd_t *pmd, unsigned long pfn, bool write) |
| 894 | { |
| 895 | pgprot_t pgprot = vma->vm_page_prot; |
| 896 | /* |
| 897 | * If we had pmd_special, we could avoid all these restrictions, |
| 898 | * but we need to be consistent with PTEs and architectures that |
| 899 | * can't support a 'special' bit. |
| 900 | */ |
| 901 | BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); |
| 902 | BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == |
| 903 | (VM_PFNMAP|VM_MIXEDMAP)); |
| 904 | BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); |
| 905 | BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); |
| 906 | |
| 907 | if (addr < vma->vm_start || addr >= vma->vm_end) |
| 908 | return VM_FAULT_SIGBUS; |
| 909 | if (track_pfn_insert(vma, &pgprot, pfn)) |
| 910 | return VM_FAULT_SIGBUS; |
Matthew Wilcox | ae18d6d | 2015-09-08 14:59:14 -0700 | [diff] [blame] | 911 | insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write); |
| 912 | return VM_FAULT_NOPAGE; |
Matthew Wilcox | 5cad465 | 2015-09-08 14:58:54 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 915 | int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, |
| 916 | pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, |
| 917 | struct vm_area_struct *vma) |
| 918 | { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 919 | spinlock_t *dst_ptl, *src_ptl; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 920 | struct page *src_page; |
| 921 | pmd_t pmd; |
| 922 | pgtable_t pgtable; |
| 923 | int ret; |
| 924 | |
| 925 | ret = -ENOMEM; |
| 926 | pgtable = pte_alloc_one(dst_mm, addr); |
| 927 | if (unlikely(!pgtable)) |
| 928 | goto out; |
| 929 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 930 | dst_ptl = pmd_lock(dst_mm, dst_pmd); |
| 931 | src_ptl = pmd_lockptr(src_mm, src_pmd); |
| 932 | spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 933 | |
| 934 | ret = -EAGAIN; |
| 935 | pmd = *src_pmd; |
| 936 | if (unlikely(!pmd_trans_huge(pmd))) { |
| 937 | pte_free(dst_mm, pgtable); |
| 938 | goto out_unlock; |
| 939 | } |
Kirill A. Shutemov | fc9fe82 | 2012-12-12 13:50:51 -0800 | [diff] [blame] | 940 | /* |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 941 | * When page table lock is held, the huge zero pmd should not be |
Kirill A. Shutemov | fc9fe82 | 2012-12-12 13:50:51 -0800 | [diff] [blame] | 942 | * under splitting since we don't split the page itself, only pmd to |
| 943 | * a page table. |
| 944 | */ |
| 945 | if (is_huge_zero_pmd(pmd)) { |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 946 | struct page *zero_page; |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 947 | /* |
| 948 | * get_huge_zero_page() will never allocate a new page here, |
| 949 | * since we already have a zero page to copy. It just takes a |
| 950 | * reference. |
| 951 | */ |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 952 | zero_page = get_huge_zero_page(); |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 953 | set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd, |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 954 | zero_page); |
Kirill A. Shutemov | fc9fe82 | 2012-12-12 13:50:51 -0800 | [diff] [blame] | 955 | ret = 0; |
| 956 | goto out_unlock; |
| 957 | } |
Mel Gorman | de466bd | 2013-12-18 17:08:42 -0800 | [diff] [blame] | 958 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 959 | if (unlikely(pmd_trans_splitting(pmd))) { |
| 960 | /* split huge page running from under us */ |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 961 | spin_unlock(src_ptl); |
| 962 | spin_unlock(dst_ptl); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 963 | pte_free(dst_mm, pgtable); |
| 964 | |
| 965 | wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */ |
| 966 | goto out; |
| 967 | } |
| 968 | src_page = pmd_page(pmd); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 969 | VM_BUG_ON_PAGE(!PageHead(src_page), src_page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 970 | get_page(src_page); |
| 971 | page_dup_rmap(src_page); |
| 972 | add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR); |
| 973 | |
| 974 | pmdp_set_wrprotect(src_mm, addr, src_pmd); |
| 975 | pmd = pmd_mkold(pmd_wrprotect(pmd)); |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 976 | pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 977 | set_pmd_at(dst_mm, addr, dst_pmd, pmd); |
Kirill A. Shutemov | e1f56c8 | 2013-11-14 14:30:48 -0800 | [diff] [blame] | 978 | atomic_long_inc(&dst_mm->nr_ptes); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 979 | |
| 980 | ret = 0; |
| 981 | out_unlock: |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 982 | spin_unlock(src_ptl); |
| 983 | spin_unlock(dst_ptl); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 984 | out: |
| 985 | return ret; |
| 986 | } |
| 987 | |
Will Deacon | a1dd450 | 2012-12-11 16:01:27 -0800 | [diff] [blame] | 988 | void huge_pmd_set_accessed(struct mm_struct *mm, |
| 989 | struct vm_area_struct *vma, |
| 990 | unsigned long address, |
| 991 | pmd_t *pmd, pmd_t orig_pmd, |
| 992 | int dirty) |
| 993 | { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 994 | spinlock_t *ptl; |
Will Deacon | a1dd450 | 2012-12-11 16:01:27 -0800 | [diff] [blame] | 995 | pmd_t entry; |
| 996 | unsigned long haddr; |
| 997 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 998 | ptl = pmd_lock(mm, pmd); |
Will Deacon | a1dd450 | 2012-12-11 16:01:27 -0800 | [diff] [blame] | 999 | if (unlikely(!pmd_same(*pmd, orig_pmd))) |
| 1000 | goto unlock; |
| 1001 | |
| 1002 | entry = pmd_mkyoung(orig_pmd); |
| 1003 | haddr = address & HPAGE_PMD_MASK; |
| 1004 | if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty)) |
| 1005 | update_mmu_cache_pmd(vma, address, pmd); |
| 1006 | |
| 1007 | unlock: |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1008 | spin_unlock(ptl); |
Will Deacon | a1dd450 | 2012-12-11 16:01:27 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
Hugh Dickins | 5338a93 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 1011 | /* |
| 1012 | * Save CONFIG_DEBUG_PAGEALLOC from faulting falsely on tail pages |
| 1013 | * during copy_user_huge_page()'s copy_page_rep(): in the case when |
| 1014 | * the source page gets split and a tail freed before copy completes. |
| 1015 | * Called under pmd_lock of checked pmd, so safe from splitting itself. |
| 1016 | */ |
| 1017 | static void get_user_huge_page(struct page *page) |
| 1018 | { |
| 1019 | if (IS_ENABLED(CONFIG_DEBUG_PAGEALLOC)) { |
| 1020 | struct page *endpage = page + HPAGE_PMD_NR; |
| 1021 | |
| 1022 | atomic_add(HPAGE_PMD_NR, &page->_count); |
| 1023 | while (++page < endpage) |
| 1024 | get_huge_page_tail(page); |
| 1025 | } else { |
| 1026 | get_page(page); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | static void put_user_huge_page(struct page *page) |
| 1031 | { |
| 1032 | if (IS_ENABLED(CONFIG_DEBUG_PAGEALLOC)) { |
| 1033 | struct page *endpage = page + HPAGE_PMD_NR; |
| 1034 | |
| 1035 | while (page < endpage) |
| 1036 | put_page(page++); |
| 1037 | } else { |
| 1038 | put_page(page); |
| 1039 | } |
| 1040 | } |
| 1041 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1042 | static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm, |
| 1043 | struct vm_area_struct *vma, |
| 1044 | unsigned long address, |
| 1045 | pmd_t *pmd, pmd_t orig_pmd, |
| 1046 | struct page *page, |
| 1047 | unsigned long haddr) |
| 1048 | { |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1049 | struct mem_cgroup *memcg; |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1050 | spinlock_t *ptl; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1051 | pgtable_t pgtable; |
| 1052 | pmd_t _pmd; |
| 1053 | int ret = 0, i; |
| 1054 | struct page **pages; |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1055 | unsigned long mmun_start; /* For mmu_notifiers */ |
| 1056 | unsigned long mmun_end; /* For mmu_notifiers */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1057 | |
| 1058 | pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR, |
| 1059 | GFP_KERNEL); |
| 1060 | if (unlikely(!pages)) { |
| 1061 | ret |= VM_FAULT_OOM; |
| 1062 | goto out; |
| 1063 | } |
| 1064 | |
| 1065 | for (i = 0; i < HPAGE_PMD_NR; i++) { |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 1066 | pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE | |
| 1067 | __GFP_OTHER_NODE, |
Andi Kleen | 19ee151 | 2011-03-04 17:36:31 -0800 | [diff] [blame] | 1068 | vma, address, page_to_nid(page)); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1069 | if (unlikely(!pages[i] || |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1070 | mem_cgroup_try_charge(pages[i], mm, GFP_KERNEL, |
| 1071 | &memcg))) { |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1072 | if (pages[i]) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1073 | put_page(pages[i]); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1074 | while (--i >= 0) { |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1075 | memcg = (void *)page_private(pages[i]); |
| 1076 | set_page_private(pages[i], 0); |
| 1077 | mem_cgroup_cancel_charge(pages[i], memcg); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1078 | put_page(pages[i]); |
| 1079 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1080 | kfree(pages); |
| 1081 | ret |= VM_FAULT_OOM; |
| 1082 | goto out; |
| 1083 | } |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1084 | set_page_private(pages[i], (unsigned long)memcg); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | for (i = 0; i < HPAGE_PMD_NR; i++) { |
| 1088 | copy_user_highpage(pages[i], page + i, |
Hillf Danton | 0089e48 | 2011-10-31 17:09:38 -0700 | [diff] [blame] | 1089 | haddr + PAGE_SIZE * i, vma); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1090 | __SetPageUptodate(pages[i]); |
| 1091 | cond_resched(); |
| 1092 | } |
| 1093 | |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1094 | mmun_start = haddr; |
| 1095 | mmun_end = haddr + HPAGE_PMD_SIZE; |
| 1096 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
| 1097 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1098 | ptl = pmd_lock(mm, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1099 | if (unlikely(!pmd_same(*pmd, orig_pmd))) |
| 1100 | goto out_free_pages; |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1101 | VM_BUG_ON_PAGE(!PageHead(page), page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1102 | |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 1103 | pmdp_huge_clear_flush_notify(vma, haddr, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1104 | /* leave pmd empty until pte is filled */ |
| 1105 | |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 1106 | pgtable = pgtable_trans_huge_withdraw(mm, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1107 | pmd_populate(mm, &_pmd, pgtable); |
| 1108 | |
| 1109 | for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) { |
| 1110 | pte_t *pte, entry; |
| 1111 | entry = mk_pte(pages[i], vma->vm_page_prot); |
| 1112 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1113 | memcg = (void *)page_private(pages[i]); |
| 1114 | set_page_private(pages[i], 0); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1115 | page_add_new_anon_rmap(pages[i], vma, haddr); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1116 | mem_cgroup_commit_charge(pages[i], memcg, false); |
| 1117 | lru_cache_add_active_or_unevictable(pages[i], vma); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1118 | pte = pte_offset_map(&_pmd, haddr); |
| 1119 | VM_BUG_ON(!pte_none(*pte)); |
| 1120 | set_pte_at(mm, haddr, pte, entry); |
| 1121 | pte_unmap(pte); |
| 1122 | } |
| 1123 | kfree(pages); |
| 1124 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1125 | smp_wmb(); /* make pte visible before pmd */ |
| 1126 | pmd_populate(mm, pmd, pgtable); |
| 1127 | page_remove_rmap(page); |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1128 | spin_unlock(ptl); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1129 | |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1130 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
| 1131 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1132 | ret |= VM_FAULT_WRITE; |
| 1133 | put_page(page); |
| 1134 | |
| 1135 | out: |
| 1136 | return ret; |
| 1137 | |
| 1138 | out_free_pages: |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1139 | spin_unlock(ptl); |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1140 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1141 | for (i = 0; i < HPAGE_PMD_NR; i++) { |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1142 | memcg = (void *)page_private(pages[i]); |
| 1143 | set_page_private(pages[i], 0); |
| 1144 | mem_cgroup_cancel_charge(pages[i], memcg); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1145 | put_page(pages[i]); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1146 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1147 | kfree(pages); |
| 1148 | goto out; |
| 1149 | } |
| 1150 | |
| 1151 | int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, |
| 1152 | unsigned long address, pmd_t *pmd, pmd_t orig_pmd) |
| 1153 | { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1154 | spinlock_t *ptl; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1155 | int ret = 0; |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1156 | struct page *page = NULL, *new_page; |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1157 | struct mem_cgroup *memcg; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1158 | unsigned long haddr; |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1159 | unsigned long mmun_start; /* For mmu_notifiers */ |
| 1160 | unsigned long mmun_end; /* For mmu_notifiers */ |
Michal Hocko | 3b36369 | 2015-04-15 16:13:29 -0700 | [diff] [blame] | 1161 | gfp_t huge_gfp; /* for allocation and charge */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1162 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1163 | ptl = pmd_lockptr(mm, pmd); |
Sasha Levin | 81d1b09 | 2014-10-09 15:28:10 -0700 | [diff] [blame] | 1164 | VM_BUG_ON_VMA(!vma->anon_vma, vma); |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1165 | haddr = address & HPAGE_PMD_MASK; |
| 1166 | if (is_huge_zero_pmd(orig_pmd)) |
| 1167 | goto alloc; |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1168 | spin_lock(ptl); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1169 | if (unlikely(!pmd_same(*pmd, orig_pmd))) |
| 1170 | goto out_unlock; |
| 1171 | |
| 1172 | page = pmd_page(orig_pmd); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1173 | VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1174 | if (page_mapcount(page) == 1) { |
| 1175 | pmd_t entry; |
| 1176 | entry = pmd_mkyoung(orig_pmd); |
| 1177 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
| 1178 | if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1)) |
David Miller | b113da6 | 2012-10-08 16:34:25 -0700 | [diff] [blame] | 1179 | update_mmu_cache_pmd(vma, address, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1180 | ret |= VM_FAULT_WRITE; |
| 1181 | goto out_unlock; |
| 1182 | } |
Hugh Dickins | 5338a93 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 1183 | get_user_huge_page(page); |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1184 | spin_unlock(ptl); |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1185 | alloc: |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1186 | if (transparent_hugepage_enabled(vma) && |
Aneesh Kumar K.V | 077fcf1 | 2015-02-11 15:27:12 -0800 | [diff] [blame] | 1187 | !transparent_hugepage_debug_cow()) { |
Michal Hocko | 3b36369 | 2015-04-15 16:13:29 -0700 | [diff] [blame] | 1188 | huge_gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0); |
| 1189 | new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER); |
Aneesh Kumar K.V | 077fcf1 | 2015-02-11 15:27:12 -0800 | [diff] [blame] | 1190 | } else |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1191 | new_page = NULL; |
| 1192 | |
| 1193 | if (unlikely(!new_page)) { |
Hugh Dickins | eecc1e4 | 2014-01-12 01:25:21 -0800 | [diff] [blame] | 1194 | if (!page) { |
Kirill A. Shutemov | e9b71ca | 2014-04-03 14:48:17 -0700 | [diff] [blame] | 1195 | split_huge_page_pmd(vma, address, pmd); |
| 1196 | ret |= VM_FAULT_FALLBACK; |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1197 | } else { |
| 1198 | ret = do_huge_pmd_wp_page_fallback(mm, vma, address, |
| 1199 | pmd, orig_pmd, page, haddr); |
Kirill A. Shutemov | 9845cbb | 2014-02-25 15:01:42 -0800 | [diff] [blame] | 1200 | if (ret & VM_FAULT_OOM) { |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1201 | split_huge_page(page); |
Kirill A. Shutemov | 9845cbb | 2014-02-25 15:01:42 -0800 | [diff] [blame] | 1202 | ret |= VM_FAULT_FALLBACK; |
| 1203 | } |
Hugh Dickins | 5338a93 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 1204 | put_user_huge_page(page); |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1205 | } |
David Rientjes | 17766dd | 2013-09-12 15:14:06 -0700 | [diff] [blame] | 1206 | count_vm_event(THP_FAULT_FALLBACK); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1207 | goto out; |
| 1208 | } |
| 1209 | |
Michal Hocko | 3b36369 | 2015-04-15 16:13:29 -0700 | [diff] [blame] | 1210 | if (unlikely(mem_cgroup_try_charge(new_page, mm, huge_gfp, &memcg))) { |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1211 | put_page(new_page); |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1212 | if (page) { |
| 1213 | split_huge_page(page); |
Hugh Dickins | 5338a93 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 1214 | put_user_huge_page(page); |
Kirill A. Shutemov | 9845cbb | 2014-02-25 15:01:42 -0800 | [diff] [blame] | 1215 | } else |
| 1216 | split_huge_page_pmd(vma, address, pmd); |
| 1217 | ret |= VM_FAULT_FALLBACK; |
David Rientjes | 17766dd | 2013-09-12 15:14:06 -0700 | [diff] [blame] | 1218 | count_vm_event(THP_FAULT_FALLBACK); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1219 | goto out; |
| 1220 | } |
| 1221 | |
David Rientjes | 17766dd | 2013-09-12 15:14:06 -0700 | [diff] [blame] | 1222 | count_vm_event(THP_FAULT_ALLOC); |
| 1223 | |
Hugh Dickins | eecc1e4 | 2014-01-12 01:25:21 -0800 | [diff] [blame] | 1224 | if (!page) |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1225 | clear_huge_page(new_page, haddr, HPAGE_PMD_NR); |
| 1226 | else |
| 1227 | copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1228 | __SetPageUptodate(new_page); |
| 1229 | |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1230 | mmun_start = haddr; |
| 1231 | mmun_end = haddr + HPAGE_PMD_SIZE; |
| 1232 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
| 1233 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1234 | spin_lock(ptl); |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1235 | if (page) |
Hugh Dickins | 5338a93 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 1236 | put_user_huge_page(page); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1237 | if (unlikely(!pmd_same(*pmd, orig_pmd))) { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1238 | spin_unlock(ptl); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1239 | mem_cgroup_cancel_charge(new_page, memcg); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1240 | put_page(new_page); |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1241 | goto out_mn; |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1242 | } else { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1243 | pmd_t entry; |
Kirill A. Shutemov | 3122359 | 2013-09-12 15:14:01 -0700 | [diff] [blame] | 1244 | entry = mk_huge_pmd(new_page, vma->vm_page_prot); |
| 1245 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 1246 | pmdp_huge_clear_flush_notify(vma, haddr, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1247 | page_add_new_anon_rmap(new_page, vma, haddr); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1248 | mem_cgroup_commit_charge(new_page, memcg, false); |
| 1249 | lru_cache_add_active_or_unevictable(new_page, vma); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1250 | set_pmd_at(mm, haddr, pmd, entry); |
David Miller | b113da6 | 2012-10-08 16:34:25 -0700 | [diff] [blame] | 1251 | update_mmu_cache_pmd(vma, address, pmd); |
Hugh Dickins | eecc1e4 | 2014-01-12 01:25:21 -0800 | [diff] [blame] | 1252 | if (!page) { |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1253 | add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 1254 | put_huge_zero_page(); |
| 1255 | } else { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1256 | VM_BUG_ON_PAGE(!PageHead(page), page); |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1257 | page_remove_rmap(page); |
| 1258 | put_page(page); |
| 1259 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1260 | ret |= VM_FAULT_WRITE; |
| 1261 | } |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1262 | spin_unlock(ptl); |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1263 | out_mn: |
| 1264 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
| 1265 | out: |
| 1266 | return ret; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1267 | out_unlock: |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1268 | spin_unlock(ptl); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1269 | return ret; |
| 1270 | } |
| 1271 | |
David Rientjes | b676b29 | 2012-10-08 16:34:03 -0700 | [diff] [blame] | 1272 | struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1273 | unsigned long addr, |
| 1274 | pmd_t *pmd, |
| 1275 | unsigned int flags) |
| 1276 | { |
David Rientjes | b676b29 | 2012-10-08 16:34:03 -0700 | [diff] [blame] | 1277 | struct mm_struct *mm = vma->vm_mm; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1278 | struct page *page = NULL; |
| 1279 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1280 | assert_spin_locked(pmd_lockptr(mm, pmd)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1281 | |
| 1282 | if (flags & FOLL_WRITE && !pmd_write(*pmd)) |
| 1283 | goto out; |
| 1284 | |
Kirill A. Shutemov | 85facf2 | 2013-02-04 14:28:42 -0800 | [diff] [blame] | 1285 | /* Avoid dumping huge zero page */ |
| 1286 | if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd)) |
| 1287 | return ERR_PTR(-EFAULT); |
| 1288 | |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 1289 | /* Full NUMA hinting faults to serialise migration in fault paths */ |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 1290 | if ((flags & FOLL_NUMA) && pmd_protnone(*pmd)) |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 1291 | goto out; |
| 1292 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1293 | page = pmd_page(*pmd); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1294 | VM_BUG_ON_PAGE(!PageHead(page), page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1295 | if (flags & FOLL_TOUCH) { |
| 1296 | pmd_t _pmd; |
| 1297 | /* |
| 1298 | * We should set the dirty bit only for FOLL_WRITE but |
| 1299 | * for now the dirty bit in the pmd is meaningless. |
| 1300 | * And if the dirty bit will become meaningful and |
| 1301 | * we'll only set it with FOLL_WRITE, an atomic |
| 1302 | * set_bit will be required on the pmd to set the |
| 1303 | * young bit, instead of the current set_pmd_at. |
| 1304 | */ |
| 1305 | _pmd = pmd_mkyoung(pmd_mkdirty(*pmd)); |
Aneesh Kumar K.V | 8663890a | 2013-06-06 00:20:34 -0700 | [diff] [blame] | 1306 | if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK, |
| 1307 | pmd, _pmd, 1)) |
| 1308 | update_mmu_cache_pmd(vma, addr, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1309 | } |
Kirill A. Shutemov | 84d33df | 2015-04-14 15:44:37 -0700 | [diff] [blame] | 1310 | if ((flags & FOLL_POPULATE) && (vma->vm_flags & VM_LOCKED)) { |
David Rientjes | b676b29 | 2012-10-08 16:34:03 -0700 | [diff] [blame] | 1311 | if (page->mapping && trylock_page(page)) { |
| 1312 | lru_add_drain(); |
| 1313 | if (page->mapping) |
| 1314 | mlock_vma_page(page); |
| 1315 | unlock_page(page); |
| 1316 | } |
| 1317 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1318 | page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT; |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1319 | VM_BUG_ON_PAGE(!PageCompound(page), page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1320 | if (flags & FOLL_GET) |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 1321 | get_page_foll(page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1322 | |
| 1323 | out: |
| 1324 | return page; |
| 1325 | } |
| 1326 | |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1327 | /* NUMA hinting page fault entry point for trans huge pmds */ |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 1328 | int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma, |
| 1329 | unsigned long addr, pmd_t pmd, pmd_t *pmdp) |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1330 | { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1331 | spinlock_t *ptl; |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1332 | struct anon_vma *anon_vma = NULL; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1333 | struct page *page; |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1334 | unsigned long haddr = addr & HPAGE_PMD_MASK; |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 1335 | int page_nid = -1, this_nid = numa_node_id(); |
Peter Zijlstra | 9057289 | 2013-10-07 11:29:20 +0100 | [diff] [blame] | 1336 | int target_nid, last_cpupid = -1; |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 1337 | bool page_locked; |
| 1338 | bool migrated = false; |
Mel Gorman | b191f9b | 2015-03-25 15:55:40 -0700 | [diff] [blame] | 1339 | bool was_writable; |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 1340 | int flags = 0; |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1341 | |
Mel Gorman | c0e7cad | 2015-02-12 14:58:41 -0800 | [diff] [blame] | 1342 | /* A PROT_NONE fault should not end up here */ |
| 1343 | BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))); |
| 1344 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1345 | ptl = pmd_lock(mm, pmdp); |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1346 | if (unlikely(!pmd_same(pmd, *pmdp))) |
| 1347 | goto out_unlock; |
| 1348 | |
Mel Gorman | de466bd | 2013-12-18 17:08:42 -0800 | [diff] [blame] | 1349 | /* |
| 1350 | * If there are potential migrations, wait for completion and retry |
| 1351 | * without disrupting NUMA hinting information. Do not relock and |
| 1352 | * check_same as the page may no longer be mapped. |
| 1353 | */ |
| 1354 | if (unlikely(pmd_trans_migrating(*pmdp))) { |
Mel Gorman | 5d83306 | 2015-02-12 14:58:16 -0800 | [diff] [blame] | 1355 | page = pmd_page(*pmdp); |
Mel Gorman | de466bd | 2013-12-18 17:08:42 -0800 | [diff] [blame] | 1356 | spin_unlock(ptl); |
Mel Gorman | 5d83306 | 2015-02-12 14:58:16 -0800 | [diff] [blame] | 1357 | wait_on_page_locked(page); |
Mel Gorman | de466bd | 2013-12-18 17:08:42 -0800 | [diff] [blame] | 1358 | goto out; |
| 1359 | } |
| 1360 | |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1361 | page = pmd_page(pmd); |
Mel Gorman | a1a4618 | 2013-10-07 11:28:50 +0100 | [diff] [blame] | 1362 | BUG_ON(is_huge_zero_page(page)); |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 1363 | page_nid = page_to_nid(page); |
Peter Zijlstra | 9057289 | 2013-10-07 11:29:20 +0100 | [diff] [blame] | 1364 | last_cpupid = page_cpupid_last(page); |
Mel Gorman | 03c5a6e | 2012-11-02 14:52:48 +0000 | [diff] [blame] | 1365 | count_vm_numa_event(NUMA_HINT_FAULTS); |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1366 | if (page_nid == this_nid) { |
Mel Gorman | 03c5a6e | 2012-11-02 14:52:48 +0000 | [diff] [blame] | 1367 | count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1368 | flags |= TNF_FAULT_LOCAL; |
| 1369 | } |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 1370 | |
Mel Gorman | bea66fb | 2015-03-25 15:55:37 -0700 | [diff] [blame] | 1371 | /* See similar comment in do_numa_page for explanation */ |
| 1372 | if (!(vma->vm_flags & VM_WRITE)) |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 1373 | flags |= TNF_NO_GROUP; |
| 1374 | |
| 1375 | /* |
Mel Gorman | ff9042b | 2013-10-07 11:28:43 +0100 | [diff] [blame] | 1376 | * Acquire the page lock to serialise THP migrations but avoid dropping |
| 1377 | * page_table_lock if at all possible |
| 1378 | */ |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1379 | page_locked = trylock_page(page); |
| 1380 | target_nid = mpol_misplaced(page, vma, haddr); |
| 1381 | if (target_nid == -1) { |
| 1382 | /* If the page was locked, there are no parallel migrations */ |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1383 | if (page_locked) |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1384 | goto clear_pmdnuma; |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 1385 | } |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 1386 | |
Mel Gorman | de466bd | 2013-12-18 17:08:42 -0800 | [diff] [blame] | 1387 | /* Migration could have started since the pmd_trans_migrating check */ |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 1388 | if (!page_locked) { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1389 | spin_unlock(ptl); |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1390 | wait_on_page_locked(page); |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1391 | page_nid = -1; |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1392 | goto out; |
| 1393 | } |
| 1394 | |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 1395 | /* |
| 1396 | * Page is misplaced. Page lock serialises migrations. Acquire anon_vma |
| 1397 | * to serialises splits |
| 1398 | */ |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1399 | get_page(page); |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1400 | spin_unlock(ptl); |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1401 | anon_vma = page_lock_anon_vma_read(page); |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1402 | |
Peter Zijlstra | c69307d | 2013-10-07 11:28:41 +0100 | [diff] [blame] | 1403 | /* Confirm the PMD did not change while page_table_lock was released */ |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1404 | spin_lock(ptl); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1405 | if (unlikely(!pmd_same(pmd, *pmdp))) { |
| 1406 | unlock_page(page); |
| 1407 | put_page(page); |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1408 | page_nid = -1; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1409 | goto out_unlock; |
| 1410 | } |
Mel Gorman | ff9042b | 2013-10-07 11:28:43 +0100 | [diff] [blame] | 1411 | |
Mel Gorman | c3a489c | 2013-12-18 17:08:38 -0800 | [diff] [blame] | 1412 | /* Bail if we fail to protect against THP splits for any reason */ |
| 1413 | if (unlikely(!anon_vma)) { |
| 1414 | put_page(page); |
| 1415 | page_nid = -1; |
| 1416 | goto clear_pmdnuma; |
| 1417 | } |
| 1418 | |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1419 | /* |
| 1420 | * Migrate the THP to the requested node, returns with page unlocked |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 1421 | * and access rights restored. |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1422 | */ |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1423 | spin_unlock(ptl); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1424 | migrated = migrate_misplaced_transhuge_page(mm, vma, |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1425 | pmdp, pmd, addr, page, target_nid); |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 1426 | if (migrated) { |
| 1427 | flags |= TNF_MIGRATED; |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 1428 | page_nid = target_nid; |
Mel Gorman | 074c238 | 2015-03-25 15:55:42 -0700 | [diff] [blame] | 1429 | } else |
| 1430 | flags |= TNF_MIGRATE_FAIL; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1431 | |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 1432 | goto out; |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 1433 | clear_pmdnuma: |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1434 | BUG_ON(!PageLocked(page)); |
Mel Gorman | b191f9b | 2015-03-25 15:55:40 -0700 | [diff] [blame] | 1435 | was_writable = pmd_write(pmd); |
Mel Gorman | 4d94246 | 2015-02-12 14:58:28 -0800 | [diff] [blame] | 1436 | pmd = pmd_modify(pmd, vma->vm_page_prot); |
Mel Gorman | b7b0400 | 2015-03-25 15:55:45 -0700 | [diff] [blame] | 1437 | pmd = pmd_mkyoung(pmd); |
Mel Gorman | b191f9b | 2015-03-25 15:55:40 -0700 | [diff] [blame] | 1438 | if (was_writable) |
| 1439 | pmd = pmd_mkwrite(pmd); |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1440 | set_pmd_at(mm, haddr, pmdp, pmd); |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1441 | update_mmu_cache_pmd(vma, addr, pmdp); |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1442 | unlock_page(page); |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1443 | out_unlock: |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1444 | spin_unlock(ptl); |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1445 | |
| 1446 | out: |
| 1447 | if (anon_vma) |
| 1448 | page_unlock_anon_vma_read(anon_vma); |
| 1449 | |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 1450 | if (page_nid != -1) |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 1451 | task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags); |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 1452 | |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
| 1455 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1456 | int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, |
Shaohua Li | f21760b | 2012-01-12 17:19:16 -0800 | [diff] [blame] | 1457 | pmd_t *pmd, unsigned long addr) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1458 | { |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1459 | spinlock_t *ptl; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1460 | int ret = 0; |
| 1461 | |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1462 | if (__pmd_trans_huge_lock(pmd, vma, &ptl) == 1) { |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1463 | pgtable_t pgtable; |
David Miller | f5c8ad4 | 2012-10-08 16:34:26 -0700 | [diff] [blame] | 1464 | pmd_t orig_pmd; |
Aneesh Kumar K.V | a6bf2bb | 2013-06-05 17:14:04 -0700 | [diff] [blame] | 1465 | /* |
| 1466 | * For architectures like ppc64 we look at deposited pgtable |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 1467 | * when calling pmdp_huge_get_and_clear. So do the |
Aneesh Kumar K.V | a6bf2bb | 2013-06-05 17:14:04 -0700 | [diff] [blame] | 1468 | * pgtable_trans_huge_withdraw after finishing pmdp related |
| 1469 | * operations. |
| 1470 | */ |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 1471 | orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd, |
| 1472 | tlb->fullmm); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1473 | tlb_remove_pmd_tlb_entry(tlb, pmd, addr); |
Matthew Wilcox | 4897c76 | 2015-09-08 14:58:45 -0700 | [diff] [blame] | 1474 | if (vma_is_dax(vma)) { |
| 1475 | if (is_huge_zero_pmd(orig_pmd)) { |
| 1476 | pgtable = NULL; |
| 1477 | } else { |
| 1478 | spin_unlock(ptl); |
| 1479 | return 1; |
| 1480 | } |
| 1481 | } else { |
| 1482 | pgtable = pgtable_trans_huge_withdraw(tlb->mm, pmd); |
| 1483 | } |
Kirill A. Shutemov | 479f0ab | 2012-12-12 13:50:50 -0800 | [diff] [blame] | 1484 | if (is_huge_zero_pmd(orig_pmd)) { |
Kirill A. Shutemov | e1f56c8 | 2013-11-14 14:30:48 -0800 | [diff] [blame] | 1485 | atomic_long_dec(&tlb->mm->nr_ptes); |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1486 | spin_unlock(ptl); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 1487 | put_huge_zero_page(); |
Kirill A. Shutemov | 479f0ab | 2012-12-12 13:50:50 -0800 | [diff] [blame] | 1488 | } else { |
Matthew Wilcox | 4897c76 | 2015-09-08 14:58:45 -0700 | [diff] [blame] | 1489 | struct page *page = pmd_page(orig_pmd); |
Kirill A. Shutemov | 479f0ab | 2012-12-12 13:50:50 -0800 | [diff] [blame] | 1490 | page_remove_rmap(page); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1491 | VM_BUG_ON_PAGE(page_mapcount(page) < 0, page); |
Kirill A. Shutemov | 479f0ab | 2012-12-12 13:50:50 -0800 | [diff] [blame] | 1492 | add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1493 | VM_BUG_ON_PAGE(!PageHead(page), page); |
Kirill A. Shutemov | e1f56c8 | 2013-11-14 14:30:48 -0800 | [diff] [blame] | 1494 | atomic_long_dec(&tlb->mm->nr_ptes); |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1495 | spin_unlock(ptl); |
Kirill A. Shutemov | 479f0ab | 2012-12-12 13:50:50 -0800 | [diff] [blame] | 1496 | tlb_remove_page(tlb, page); |
| 1497 | } |
Matthew Wilcox | 4897c76 | 2015-09-08 14:58:45 -0700 | [diff] [blame] | 1498 | if (pgtable) |
| 1499 | pte_free(tlb->mm, pgtable); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1500 | ret = 1; |
| 1501 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1502 | return ret; |
| 1503 | } |
| 1504 | |
Andrea Arcangeli | 37a1c49 | 2011-10-31 17:08:30 -0700 | [diff] [blame] | 1505 | int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma, |
| 1506 | unsigned long old_addr, |
| 1507 | unsigned long new_addr, unsigned long old_end, |
| 1508 | pmd_t *old_pmd, pmd_t *new_pmd) |
| 1509 | { |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1510 | spinlock_t *old_ptl, *new_ptl; |
Andrea Arcangeli | 37a1c49 | 2011-10-31 17:08:30 -0700 | [diff] [blame] | 1511 | int ret = 0; |
| 1512 | pmd_t pmd; |
| 1513 | |
| 1514 | struct mm_struct *mm = vma->vm_mm; |
| 1515 | |
| 1516 | if ((old_addr & ~HPAGE_PMD_MASK) || |
| 1517 | (new_addr & ~HPAGE_PMD_MASK) || |
| 1518 | old_end - old_addr < HPAGE_PMD_SIZE || |
| 1519 | (new_vma->vm_flags & VM_NOHUGEPAGE)) |
| 1520 | goto out; |
| 1521 | |
| 1522 | /* |
| 1523 | * The destination pmd shouldn't be established, free_pgtables() |
| 1524 | * should have release it. |
| 1525 | */ |
| 1526 | if (WARN_ON(!pmd_none(*new_pmd))) { |
| 1527 | VM_BUG_ON(pmd_trans_huge(*new_pmd)); |
| 1528 | goto out; |
| 1529 | } |
| 1530 | |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1531 | /* |
| 1532 | * We don't have to worry about the ordering of src and dst |
| 1533 | * ptlocks because exclusive mmap_sem prevents deadlock. |
| 1534 | */ |
| 1535 | ret = __pmd_trans_huge_lock(old_pmd, vma, &old_ptl); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1536 | if (ret == 1) { |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1537 | new_ptl = pmd_lockptr(mm, new_pmd); |
| 1538 | if (new_ptl != old_ptl) |
| 1539 | spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 1540 | pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1541 | VM_BUG_ON(!pmd_none(*new_pmd)); |
Kirill A. Shutemov | 3592806 | 2013-12-12 17:12:33 -0800 | [diff] [blame] | 1542 | |
Aneesh Kumar K.V | b3084f4 | 2014-01-13 11:34:24 +0530 | [diff] [blame] | 1543 | if (pmd_move_must_withdraw(new_ptl, old_ptl)) { |
| 1544 | pgtable_t pgtable; |
Kirill A. Shutemov | 3592806 | 2013-12-12 17:12:33 -0800 | [diff] [blame] | 1545 | pgtable = pgtable_trans_huge_withdraw(mm, old_pmd); |
| 1546 | pgtable_trans_huge_deposit(mm, new_pmd, pgtable); |
Kirill A. Shutemov | 3592806 | 2013-12-12 17:12:33 -0800 | [diff] [blame] | 1547 | } |
Aneesh Kumar K.V | b3084f4 | 2014-01-13 11:34:24 +0530 | [diff] [blame] | 1548 | set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd)); |
| 1549 | if (new_ptl != old_ptl) |
| 1550 | spin_unlock(new_ptl); |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1551 | spin_unlock(old_ptl); |
Andrea Arcangeli | 37a1c49 | 2011-10-31 17:08:30 -0700 | [diff] [blame] | 1552 | } |
| 1553 | out: |
| 1554 | return ret; |
| 1555 | } |
| 1556 | |
Mel Gorman | f123d74 | 2013-10-07 11:28:49 +0100 | [diff] [blame] | 1557 | /* |
| 1558 | * Returns |
| 1559 | * - 0 if PMD could not be locked |
| 1560 | * - 1 if PMD was locked but protections unchange and TLB flush unnecessary |
| 1561 | * - HPAGE_PMD_NR is protections changed and TLB flush necessary |
| 1562 | */ |
Johannes Weiner | cd7548a | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 1563 | int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, |
Mel Gorman | e944fd6 | 2015-02-12 14:58:35 -0800 | [diff] [blame] | 1564 | unsigned long addr, pgprot_t newprot, int prot_numa) |
Johannes Weiner | cd7548a | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 1565 | { |
| 1566 | struct mm_struct *mm = vma->vm_mm; |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1567 | spinlock_t *ptl; |
Johannes Weiner | cd7548a | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 1568 | int ret = 0; |
| 1569 | |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1570 | if (__pmd_trans_huge_lock(pmd, vma, &ptl) == 1) { |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1571 | pmd_t entry; |
Mel Gorman | b191f9b | 2015-03-25 15:55:40 -0700 | [diff] [blame] | 1572 | bool preserve_write = prot_numa && pmd_write(*pmd); |
Mel Gorman | ba68bc0 | 2015-03-07 15:20:48 +0000 | [diff] [blame] | 1573 | ret = 1; |
Mel Gorman | e944fd6 | 2015-02-12 14:58:35 -0800 | [diff] [blame] | 1574 | |
| 1575 | /* |
| 1576 | * Avoid trapping faults against the zero page. The read-only |
| 1577 | * data is likely to be read-cached on the local CPU and |
| 1578 | * local/remote hits to the zero page are not interesting. |
| 1579 | */ |
| 1580 | if (prot_numa && is_huge_zero_pmd(*pmd)) { |
| 1581 | spin_unlock(ptl); |
Mel Gorman | ba68bc0 | 2015-03-07 15:20:48 +0000 | [diff] [blame] | 1582 | return ret; |
Mel Gorman | e944fd6 | 2015-02-12 14:58:35 -0800 | [diff] [blame] | 1583 | } |
| 1584 | |
Mel Gorman | 10c1045 | 2015-02-12 14:58:44 -0800 | [diff] [blame] | 1585 | if (!prot_numa || !pmd_protnone(*pmd)) { |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 1586 | entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd); |
Mel Gorman | 10c1045 | 2015-02-12 14:58:44 -0800 | [diff] [blame] | 1587 | entry = pmd_modify(entry, newprot); |
Mel Gorman | b191f9b | 2015-03-25 15:55:40 -0700 | [diff] [blame] | 1588 | if (preserve_write) |
| 1589 | entry = pmd_mkwrite(entry); |
Mel Gorman | 10c1045 | 2015-02-12 14:58:44 -0800 | [diff] [blame] | 1590 | ret = HPAGE_PMD_NR; |
| 1591 | set_pmd_at(mm, addr, pmd, entry); |
Mel Gorman | b191f9b | 2015-03-25 15:55:40 -0700 | [diff] [blame] | 1592 | BUG_ON(!preserve_write && pmd_write(entry)); |
Mel Gorman | 10c1045 | 2015-02-12 14:58:44 -0800 | [diff] [blame] | 1593 | } |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1594 | spin_unlock(ptl); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1595 | } |
Johannes Weiner | cd7548a | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 1596 | |
| 1597 | return ret; |
| 1598 | } |
| 1599 | |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1600 | /* |
| 1601 | * Returns 1 if a given pmd maps a stable (not under splitting) thp. |
| 1602 | * Returns -1 if it maps a thp under splitting. Returns 0 otherwise. |
| 1603 | * |
| 1604 | * Note that if it returns 1, this routine returns without unlocking page |
| 1605 | * table locks. So callers must unlock them. |
| 1606 | */ |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1607 | int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma, |
| 1608 | spinlock_t **ptl) |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1609 | { |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1610 | *ptl = pmd_lock(vma->vm_mm, pmd); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1611 | if (likely(pmd_trans_huge(*pmd))) { |
| 1612 | if (unlikely(pmd_trans_splitting(*pmd))) { |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1613 | spin_unlock(*ptl); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1614 | wait_split_huge_page(vma->anon_vma, pmd); |
| 1615 | return -1; |
| 1616 | } else { |
| 1617 | /* Thp mapped by 'pmd' is stable, so we can |
| 1618 | * handle it as it is. */ |
| 1619 | return 1; |
| 1620 | } |
| 1621 | } |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1622 | spin_unlock(*ptl); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1623 | return 0; |
| 1624 | } |
| 1625 | |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1626 | /* |
| 1627 | * This function returns whether a given @page is mapped onto the @address |
| 1628 | * in the virtual space of @mm. |
| 1629 | * |
| 1630 | * When it's true, this function returns *pmd with holding the page table lock |
| 1631 | * and passing it back to the caller via @ptl. |
| 1632 | * If it's false, returns NULL without holding the page table lock. |
| 1633 | */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1634 | pmd_t *page_check_address_pmd(struct page *page, |
| 1635 | struct mm_struct *mm, |
| 1636 | unsigned long address, |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1637 | enum page_check_address_pmd_flag flag, |
| 1638 | spinlock_t **ptl) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1639 | { |
Kirill A. Shutemov | b5a8cad | 2014-04-18 15:07:25 -0700 | [diff] [blame] | 1640 | pgd_t *pgd; |
| 1641 | pud_t *pud; |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1642 | pmd_t *pmd; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1643 | |
| 1644 | if (address & ~HPAGE_PMD_MASK) |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1645 | return NULL; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1646 | |
Kirill A. Shutemov | b5a8cad | 2014-04-18 15:07:25 -0700 | [diff] [blame] | 1647 | pgd = pgd_offset(mm, address); |
| 1648 | if (!pgd_present(*pgd)) |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1649 | return NULL; |
Kirill A. Shutemov | b5a8cad | 2014-04-18 15:07:25 -0700 | [diff] [blame] | 1650 | pud = pud_offset(pgd, address); |
| 1651 | if (!pud_present(*pud)) |
| 1652 | return NULL; |
| 1653 | pmd = pmd_offset(pud, address); |
| 1654 | |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1655 | *ptl = pmd_lock(mm, pmd); |
Kirill A. Shutemov | b5a8cad | 2014-04-18 15:07:25 -0700 | [diff] [blame] | 1656 | if (!pmd_present(*pmd)) |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1657 | goto unlock; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1658 | if (pmd_page(*pmd) != page) |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1659 | goto unlock; |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 1660 | /* |
| 1661 | * split_vma() may create temporary aliased mappings. There is |
| 1662 | * no risk as long as all huge pmd are found and have their |
| 1663 | * splitting bit set before __split_huge_page_refcount |
| 1664 | * runs. Finding the same huge pmd more than once during the |
| 1665 | * same rmap walk is not a problem. |
| 1666 | */ |
| 1667 | if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG && |
| 1668 | pmd_trans_splitting(*pmd)) |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1669 | goto unlock; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1670 | if (pmd_trans_huge(*pmd)) { |
| 1671 | VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG && |
| 1672 | !pmd_trans_splitting(*pmd)); |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1673 | return pmd; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1674 | } |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1675 | unlock: |
| 1676 | spin_unlock(*ptl); |
| 1677 | return NULL; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | static int __split_huge_page_splitting(struct page *page, |
| 1681 | struct vm_area_struct *vma, |
| 1682 | unsigned long address) |
| 1683 | { |
| 1684 | struct mm_struct *mm = vma->vm_mm; |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1685 | spinlock_t *ptl; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1686 | pmd_t *pmd; |
| 1687 | int ret = 0; |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1688 | /* For mmu_notifiers */ |
| 1689 | const unsigned long mmun_start = address; |
| 1690 | const unsigned long mmun_end = address + HPAGE_PMD_SIZE; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1691 | |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1692 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1693 | pmd = page_check_address_pmd(page, mm, address, |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1694 | PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG, &ptl); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1695 | if (pmd) { |
| 1696 | /* |
| 1697 | * We can't temporarily set the pmd to null in order |
| 1698 | * to split it, the pmd must remain marked huge at all |
| 1699 | * times or the VM won't take the pmd_trans_huge paths |
Ingo Molnar | 5a50508 | 2012-12-02 19:56:46 +0000 | [diff] [blame] | 1700 | * and it won't wait on the anon_vma->root->rwsem to |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1701 | * serialize against split_huge_page*. |
| 1702 | */ |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1703 | pmdp_splitting_flush(vma, address, pmd); |
Joerg Roedel | 34ee645 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 1704 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1705 | ret = 1; |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1706 | spin_unlock(ptl); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1707 | } |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1708 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1709 | |
| 1710 | return ret; |
| 1711 | } |
| 1712 | |
Shaohua Li | 5bc7b8a | 2013-04-29 15:08:36 -0700 | [diff] [blame] | 1713 | static void __split_huge_page_refcount(struct page *page, |
| 1714 | struct list_head *list) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1715 | { |
| 1716 | int i; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1717 | struct zone *zone = page_zone(page); |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1718 | struct lruvec *lruvec; |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 1719 | int tail_count = 0; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1720 | |
| 1721 | /* prevent PageLRU to go away from under us, and freeze lru stats */ |
| 1722 | spin_lock_irq(&zone->lru_lock); |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1723 | lruvec = mem_cgroup_page_lruvec(page, zone); |
| 1724 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1725 | compound_lock(page); |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 1726 | /* complete memcg works before add pages to LRU */ |
| 1727 | mem_cgroup_split_huge_fixup(page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1728 | |
Shaohua Li | 4567688 | 2012-01-12 17:19:18 -0800 | [diff] [blame] | 1729 | for (i = HPAGE_PMD_NR - 1; i >= 1; i--) { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1730 | struct page *page_tail = page + i; |
| 1731 | |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 1732 | /* tail_page->_mapcount cannot change */ |
| 1733 | BUG_ON(page_mapcount(page_tail) < 0); |
| 1734 | tail_count += page_mapcount(page_tail); |
| 1735 | /* check for overflow */ |
| 1736 | BUG_ON(tail_count < 0); |
| 1737 | BUG_ON(atomic_read(&page_tail->_count) != 0); |
| 1738 | /* |
| 1739 | * tail_page->_count is zero and not changing from |
| 1740 | * under us. But get_page_unless_zero() may be running |
| 1741 | * from under us on the tail_page. If we used |
| 1742 | * atomic_set() below instead of atomic_add(), we |
| 1743 | * would then run atomic_set() concurrently with |
| 1744 | * get_page_unless_zero(), and atomic_set() is |
| 1745 | * implemented in C not using locked ops. spin_unlock |
| 1746 | * on x86 sometime uses locked ops because of PPro |
| 1747 | * errata 66, 92, so unless somebody can guarantee |
| 1748 | * atomic_set() here would be safe on all archs (and |
| 1749 | * not only on x86), it's safer to use atomic_add(). |
| 1750 | */ |
| 1751 | atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1, |
| 1752 | &page_tail->_count); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1753 | |
| 1754 | /* after clearing PageTail the gup refcount can be released */ |
Waiman Long | 3a79d52 | 2014-08-06 16:05:38 -0700 | [diff] [blame] | 1755 | smp_mb__after_atomic(); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1756 | |
Naoya Horiguchi | f4c18e6 | 2015-08-06 15:47:08 -0700 | [diff] [blame] | 1757 | page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1758 | page_tail->flags |= (page->flags & |
| 1759 | ((1L << PG_referenced) | |
| 1760 | (1L << PG_swapbacked) | |
| 1761 | (1L << PG_mlocked) | |
Kirill A. Shutemov | e180cf8 | 2013-07-31 13:53:39 -0700 | [diff] [blame] | 1762 | (1L << PG_uptodate) | |
| 1763 | (1L << PG_active) | |
| 1764 | (1L << PG_unevictable))); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1765 | page_tail->flags |= (1L << PG_dirty); |
| 1766 | |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 1767 | /* clear PageTail before overwriting first_page */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1768 | smp_wmb(); |
| 1769 | |
| 1770 | /* |
| 1771 | * __split_huge_page_splitting() already set the |
| 1772 | * splitting bit in all pmd that could map this |
| 1773 | * hugepage, that will ensure no CPU can alter the |
| 1774 | * mapcount on the head page. The mapcount is only |
| 1775 | * accounted in the head page and it has to be |
| 1776 | * transferred to all tail pages in the below code. So |
| 1777 | * for this code to be safe, the split the mapcount |
| 1778 | * can't change. But that doesn't mean userland can't |
| 1779 | * keep changing and reading the page contents while |
| 1780 | * we transfer the mapcount, so the pmd splitting |
| 1781 | * status is achieved setting a reserved bit in the |
| 1782 | * pmd, not by clearing the present bit. |
| 1783 | */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1784 | page_tail->_mapcount = page->_mapcount; |
| 1785 | |
| 1786 | BUG_ON(page_tail->mapping); |
| 1787 | page_tail->mapping = page->mapping; |
| 1788 | |
Shaohua Li | 4567688 | 2012-01-12 17:19:18 -0800 | [diff] [blame] | 1789 | page_tail->index = page->index + i; |
Peter Zijlstra | 9057289 | 2013-10-07 11:29:20 +0100 | [diff] [blame] | 1790 | page_cpupid_xchg_last(page_tail, page_cpupid_last(page)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1791 | |
| 1792 | BUG_ON(!PageAnon(page_tail)); |
| 1793 | BUG_ON(!PageUptodate(page_tail)); |
| 1794 | BUG_ON(!PageDirty(page_tail)); |
| 1795 | BUG_ON(!PageSwapBacked(page_tail)); |
| 1796 | |
Shaohua Li | 5bc7b8a | 2013-04-29 15:08:36 -0700 | [diff] [blame] | 1797 | lru_add_page_tail(page, page_tail, lruvec, list); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1798 | } |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 1799 | atomic_sub(tail_count, &page->_count); |
| 1800 | BUG_ON(atomic_read(&page->_count) <= 0); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1801 | |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1802 | __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1); |
Andrea Arcangeli | 7913417 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1803 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1804 | ClearPageCompound(page); |
| 1805 | compound_unlock(page); |
| 1806 | spin_unlock_irq(&zone->lru_lock); |
| 1807 | |
| 1808 | for (i = 1; i < HPAGE_PMD_NR; i++) { |
| 1809 | struct page *page_tail = page + i; |
| 1810 | BUG_ON(page_count(page_tail) <= 0); |
| 1811 | /* |
| 1812 | * Tail pages may be freed if there wasn't any mapping |
| 1813 | * like if add_to_swap() is running on a lru page that |
| 1814 | * had its mapping zapped. And freeing these pages |
| 1815 | * requires taking the lru_lock so we do the put_page |
| 1816 | * of the tail pages after the split is complete. |
| 1817 | */ |
| 1818 | put_page(page_tail); |
| 1819 | } |
| 1820 | |
| 1821 | /* |
| 1822 | * Only the head page (now become a regular page) is required |
| 1823 | * to be pinned by the caller. |
| 1824 | */ |
| 1825 | BUG_ON(page_count(page) <= 0); |
| 1826 | } |
| 1827 | |
| 1828 | static int __split_huge_page_map(struct page *page, |
| 1829 | struct vm_area_struct *vma, |
| 1830 | unsigned long address) |
| 1831 | { |
| 1832 | struct mm_struct *mm = vma->vm_mm; |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1833 | spinlock_t *ptl; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1834 | pmd_t *pmd, _pmd; |
| 1835 | int ret = 0, i; |
| 1836 | pgtable_t pgtable; |
| 1837 | unsigned long haddr; |
| 1838 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1839 | pmd = page_check_address_pmd(page, mm, address, |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1840 | PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG, &ptl); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1841 | if (pmd) { |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 1842 | pgtable = pgtable_trans_huge_withdraw(mm, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1843 | pmd_populate(mm, &_pmd, pgtable); |
Waiman Long | f8303c2 | 2014-08-06 16:05:36 -0700 | [diff] [blame] | 1844 | if (pmd_write(*pmd)) |
| 1845 | BUG_ON(page_mapcount(page) != 1); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1846 | |
Gerald Schaefer | e3ebcf6 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 1847 | haddr = address; |
| 1848 | for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1849 | pte_t *pte, entry; |
| 1850 | BUG_ON(PageCompound(page+i)); |
Mel Gorman | abc40bd | 2014-10-02 19:47:42 +0100 | [diff] [blame] | 1851 | /* |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 1852 | * Note that NUMA hinting access restrictions are not |
| 1853 | * transferred to avoid any possibility of altering |
| 1854 | * permissions across VMAs. |
Mel Gorman | abc40bd | 2014-10-02 19:47:42 +0100 | [diff] [blame] | 1855 | */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1856 | entry = mk_pte(page + i, vma->vm_page_prot); |
| 1857 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); |
| 1858 | if (!pmd_write(*pmd)) |
| 1859 | entry = pte_wrprotect(entry); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1860 | if (!pmd_young(*pmd)) |
| 1861 | entry = pte_mkold(entry); |
| 1862 | pte = pte_offset_map(&_pmd, haddr); |
| 1863 | BUG_ON(!pte_none(*pte)); |
| 1864 | set_pte_at(mm, haddr, pte, entry); |
| 1865 | pte_unmap(pte); |
| 1866 | } |
| 1867 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1868 | smp_wmb(); /* make pte visible before pmd */ |
| 1869 | /* |
| 1870 | * Up to this point the pmd is present and huge and |
| 1871 | * userland has the whole access to the hugepage |
| 1872 | * during the split (which happens in place). If we |
| 1873 | * overwrite the pmd with the not-huge version |
| 1874 | * pointing to the pte here (which of course we could |
| 1875 | * if all CPUs were bug free), userland could trigger |
| 1876 | * a small page size TLB miss on the small sized TLB |
| 1877 | * while the hugepage TLB entry is still established |
| 1878 | * in the huge TLB. Some CPU doesn't like that. See |
| 1879 | * http://support.amd.com/us/Processor_TechDocs/41322.pdf, |
| 1880 | * Erratum 383 on page 93. Intel should be safe but is |
| 1881 | * also warns that it's only safe if the permission |
| 1882 | * and cache attributes of the two entries loaded in |
| 1883 | * the two TLB is identical (which should be the case |
| 1884 | * here). But it is generally safer to never allow |
| 1885 | * small and huge TLB entries for the same virtual |
| 1886 | * address to be loaded simultaneously. So instead of |
| 1887 | * doing "pmd_populate(); flush_tlb_range();" we first |
| 1888 | * mark the current pmd notpresent (atomically because |
| 1889 | * here the pmd_trans_huge and pmd_trans_splitting |
| 1890 | * must remain set at all times on the pmd until the |
| 1891 | * split is complete for this pmd), then we flush the |
| 1892 | * SMP TLB and finally we write the non-huge version |
| 1893 | * of the pmd entry with pmd_populate. |
| 1894 | */ |
Gerald Schaefer | 46dcde7 | 2012-10-08 16:30:09 -0700 | [diff] [blame] | 1895 | pmdp_invalidate(vma, address, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1896 | pmd_populate(mm, pmd, pgtable); |
| 1897 | ret = 1; |
Kirill A. Shutemov | 117b079 | 2013-11-14 14:30:56 -0800 | [diff] [blame] | 1898 | spin_unlock(ptl); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1899 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1900 | |
| 1901 | return ret; |
| 1902 | } |
| 1903 | |
Ingo Molnar | 5a50508 | 2012-12-02 19:56:46 +0000 | [diff] [blame] | 1904 | /* must be called with anon_vma->root->rwsem held */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1905 | static void __split_huge_page(struct page *page, |
Shaohua Li | 5bc7b8a | 2013-04-29 15:08:36 -0700 | [diff] [blame] | 1906 | struct anon_vma *anon_vma, |
| 1907 | struct list_head *list) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1908 | { |
| 1909 | int mapcount, mapcount2; |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 1910 | pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1911 | struct anon_vma_chain *avc; |
| 1912 | |
| 1913 | BUG_ON(!PageHead(page)); |
| 1914 | BUG_ON(PageTail(page)); |
| 1915 | |
| 1916 | mapcount = 0; |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 1917 | anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1918 | struct vm_area_struct *vma = avc->vma; |
| 1919 | unsigned long addr = vma_address(page, vma); |
| 1920 | BUG_ON(is_vma_temporary_stack(vma)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1921 | mapcount += __split_huge_page_splitting(page, vma, addr); |
| 1922 | } |
Andrea Arcangeli | 05759d3 | 2011-01-13 15:46:53 -0800 | [diff] [blame] | 1923 | /* |
| 1924 | * It is critical that new vmas are added to the tail of the |
| 1925 | * anon_vma list. This guarantes that if copy_huge_pmd() runs |
| 1926 | * and establishes a child pmd before |
| 1927 | * __split_huge_page_splitting() freezes the parent pmd (so if |
| 1928 | * we fail to prevent copy_huge_pmd() from running until the |
| 1929 | * whole __split_huge_page() is complete), we will still see |
| 1930 | * the newly established pmd of the child later during the |
| 1931 | * walk, to be able to set it as pmd_trans_splitting too. |
| 1932 | */ |
Kirill A. Shutemov | ff9e43e | 2014-06-04 16:06:57 -0700 | [diff] [blame] | 1933 | if (mapcount != page_mapcount(page)) { |
Andrew Morton | ae3a8c1 | 2014-06-04 16:06:58 -0700 | [diff] [blame] | 1934 | pr_err("mapcount %d page_mapcount %d\n", |
| 1935 | mapcount, page_mapcount(page)); |
Kirill A. Shutemov | ff9e43e | 2014-06-04 16:06:57 -0700 | [diff] [blame] | 1936 | BUG(); |
| 1937 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1938 | |
Shaohua Li | 5bc7b8a | 2013-04-29 15:08:36 -0700 | [diff] [blame] | 1939 | __split_huge_page_refcount(page, list); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1940 | |
| 1941 | mapcount2 = 0; |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 1942 | anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1943 | struct vm_area_struct *vma = avc->vma; |
| 1944 | unsigned long addr = vma_address(page, vma); |
| 1945 | BUG_ON(is_vma_temporary_stack(vma)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1946 | mapcount2 += __split_huge_page_map(page, vma, addr); |
| 1947 | } |
Kirill A. Shutemov | ff9e43e | 2014-06-04 16:06:57 -0700 | [diff] [blame] | 1948 | if (mapcount != mapcount2) { |
Andrew Morton | ae3a8c1 | 2014-06-04 16:06:58 -0700 | [diff] [blame] | 1949 | pr_err("mapcount %d mapcount2 %d page_mapcount %d\n", |
| 1950 | mapcount, mapcount2, page_mapcount(page)); |
Kirill A. Shutemov | ff9e43e | 2014-06-04 16:06:57 -0700 | [diff] [blame] | 1951 | BUG(); |
| 1952 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1953 | } |
| 1954 | |
Shaohua Li | 5bc7b8a | 2013-04-29 15:08:36 -0700 | [diff] [blame] | 1955 | /* |
| 1956 | * Split a hugepage into normal pages. This doesn't change the position of head |
| 1957 | * page. If @list is null, tail pages will be added to LRU list, otherwise, to |
| 1958 | * @list. Both head page and tail pages will inherit mapping, flags, and so on |
| 1959 | * from the hugepage. |
| 1960 | * Return 0 if the hugepage is split successfully otherwise return 1. |
| 1961 | */ |
| 1962 | int split_huge_page_to_list(struct page *page, struct list_head *list) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1963 | { |
| 1964 | struct anon_vma *anon_vma; |
| 1965 | int ret = 1; |
| 1966 | |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 1967 | BUG_ON(is_huge_zero_page(page)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1968 | BUG_ON(!PageAnon(page)); |
Mel Gorman | 062f1af | 2013-01-11 14:32:02 -0800 | [diff] [blame] | 1969 | |
| 1970 | /* |
| 1971 | * The caller does not necessarily hold an mmap_sem that would prevent |
| 1972 | * the anon_vma disappearing so we first we take a reference to it |
| 1973 | * and then lock the anon_vma for write. This is similar to |
| 1974 | * page_lock_anon_vma_read except the write lock is taken to serialise |
| 1975 | * against parallel split or collapse operations. |
| 1976 | */ |
| 1977 | anon_vma = page_get_anon_vma(page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1978 | if (!anon_vma) |
| 1979 | goto out; |
Mel Gorman | 062f1af | 2013-01-11 14:32:02 -0800 | [diff] [blame] | 1980 | anon_vma_lock_write(anon_vma); |
| 1981 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1982 | ret = 0; |
| 1983 | if (!PageCompound(page)) |
| 1984 | goto out_unlock; |
| 1985 | |
| 1986 | BUG_ON(!PageSwapBacked(page)); |
Shaohua Li | 5bc7b8a | 2013-04-29 15:08:36 -0700 | [diff] [blame] | 1987 | __split_huge_page(page, anon_vma, list); |
Andi Kleen | 81ab420 | 2011-04-14 15:22:06 -0700 | [diff] [blame] | 1988 | count_vm_event(THP_SPLIT); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1989 | |
| 1990 | BUG_ON(PageCompound(page)); |
| 1991 | out_unlock: |
Konstantin Khlebnikov | 08b5270 | 2013-02-22 16:34:40 -0800 | [diff] [blame] | 1992 | anon_vma_unlock_write(anon_vma); |
Mel Gorman | 062f1af | 2013-01-11 14:32:02 -0800 | [diff] [blame] | 1993 | put_anon_vma(anon_vma); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1994 | out: |
| 1995 | return ret; |
| 1996 | } |
| 1997 | |
Vlastimil Babka | 9050d7e | 2014-03-03 15:38:27 -0800 | [diff] [blame] | 1998 | #define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE) |
Andrea Arcangeli | 78f11a2 | 2011-04-27 15:26:45 -0700 | [diff] [blame] | 1999 | |
Andrea Arcangeli | 60ab324 | 2011-01-13 15:47:18 -0800 | [diff] [blame] | 2000 | int hugepage_madvise(struct vm_area_struct *vma, |
| 2001 | unsigned long *vm_flags, int advice) |
Andrea Arcangeli | 0af4e98 | 2011-01-13 15:46:55 -0800 | [diff] [blame] | 2002 | { |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 2003 | switch (advice) { |
| 2004 | case MADV_HUGEPAGE: |
Alex Thorlton | 1e1836e | 2014-04-07 15:37:09 -0700 | [diff] [blame] | 2005 | #ifdef CONFIG_S390 |
| 2006 | /* |
| 2007 | * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390 |
| 2008 | * can't handle this properly after s390_enable_sie, so we simply |
| 2009 | * ignore the madvise to prevent qemu from causing a SIGSEGV. |
| 2010 | */ |
| 2011 | if (mm_has_pgste(vma->vm_mm)) |
| 2012 | return 0; |
| 2013 | #endif |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 2014 | /* |
| 2015 | * Be somewhat over-protective like KSM for now! |
| 2016 | */ |
Andrea Arcangeli | 78f11a2 | 2011-04-27 15:26:45 -0700 | [diff] [blame] | 2017 | if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP)) |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 2018 | return -EINVAL; |
| 2019 | *vm_flags &= ~VM_NOHUGEPAGE; |
| 2020 | *vm_flags |= VM_HUGEPAGE; |
Andrea Arcangeli | 60ab324 | 2011-01-13 15:47:18 -0800 | [diff] [blame] | 2021 | /* |
| 2022 | * If the vma become good for khugepaged to scan, |
| 2023 | * register it here without waiting a page fault that |
| 2024 | * may not happen any time soon. |
| 2025 | */ |
David Rientjes | 6d50e60 | 2014-10-29 14:50:31 -0700 | [diff] [blame] | 2026 | if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags))) |
Andrea Arcangeli | 60ab324 | 2011-01-13 15:47:18 -0800 | [diff] [blame] | 2027 | return -ENOMEM; |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 2028 | break; |
| 2029 | case MADV_NOHUGEPAGE: |
| 2030 | /* |
| 2031 | * Be somewhat over-protective like KSM for now! |
| 2032 | */ |
Andrea Arcangeli | 78f11a2 | 2011-04-27 15:26:45 -0700 | [diff] [blame] | 2033 | if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP)) |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 2034 | return -EINVAL; |
| 2035 | *vm_flags &= ~VM_HUGEPAGE; |
| 2036 | *vm_flags |= VM_NOHUGEPAGE; |
Andrea Arcangeli | 60ab324 | 2011-01-13 15:47:18 -0800 | [diff] [blame] | 2037 | /* |
| 2038 | * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning |
| 2039 | * this vma even if we leave the mm registered in khugepaged if |
| 2040 | * it got registered before VM_NOHUGEPAGE was set. |
| 2041 | */ |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 2042 | break; |
| 2043 | } |
Andrea Arcangeli | 0af4e98 | 2011-01-13 15:46:55 -0800 | [diff] [blame] | 2044 | |
| 2045 | return 0; |
| 2046 | } |
| 2047 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2048 | static int __init khugepaged_slab_init(void) |
| 2049 | { |
| 2050 | mm_slot_cache = kmem_cache_create("khugepaged_mm_slot", |
| 2051 | sizeof(struct mm_slot), |
| 2052 | __alignof__(struct mm_slot), 0, NULL); |
| 2053 | if (!mm_slot_cache) |
| 2054 | return -ENOMEM; |
| 2055 | |
| 2056 | return 0; |
| 2057 | } |
| 2058 | |
Kirill A. Shutemov | 65ebb64 | 2015-04-15 16:14:20 -0700 | [diff] [blame] | 2059 | static void __init khugepaged_slab_exit(void) |
| 2060 | { |
| 2061 | kmem_cache_destroy(mm_slot_cache); |
| 2062 | } |
| 2063 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2064 | static inline struct mm_slot *alloc_mm_slot(void) |
| 2065 | { |
| 2066 | if (!mm_slot_cache) /* initialization failed */ |
| 2067 | return NULL; |
| 2068 | return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL); |
| 2069 | } |
| 2070 | |
| 2071 | static inline void free_mm_slot(struct mm_slot *mm_slot) |
| 2072 | { |
| 2073 | kmem_cache_free(mm_slot_cache, mm_slot); |
| 2074 | } |
| 2075 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2076 | static struct mm_slot *get_mm_slot(struct mm_struct *mm) |
| 2077 | { |
| 2078 | struct mm_slot *mm_slot; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2079 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2080 | hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2081 | if (mm == mm_slot->mm) |
| 2082 | return mm_slot; |
Sasha Levin | 43b5fbb | 2013-02-22 16:32:27 -0800 | [diff] [blame] | 2083 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2084 | return NULL; |
| 2085 | } |
| 2086 | |
| 2087 | static void insert_to_mm_slots_hash(struct mm_struct *mm, |
| 2088 | struct mm_slot *mm_slot) |
| 2089 | { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2090 | mm_slot->mm = mm; |
Sasha Levin | 43b5fbb | 2013-02-22 16:32:27 -0800 | [diff] [blame] | 2091 | hash_add(mm_slots_hash, &mm_slot->hash, (long)mm); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | static inline int khugepaged_test_exit(struct mm_struct *mm) |
| 2095 | { |
| 2096 | return atomic_read(&mm->mm_users) == 0; |
| 2097 | } |
| 2098 | |
| 2099 | int __khugepaged_enter(struct mm_struct *mm) |
| 2100 | { |
| 2101 | struct mm_slot *mm_slot; |
| 2102 | int wakeup; |
| 2103 | |
| 2104 | mm_slot = alloc_mm_slot(); |
| 2105 | if (!mm_slot) |
| 2106 | return -ENOMEM; |
| 2107 | |
| 2108 | /* __khugepaged_exit() must not run from under us */ |
Sasha Levin | 96dad67 | 2014-10-09 15:28:39 -0700 | [diff] [blame] | 2109 | VM_BUG_ON_MM(khugepaged_test_exit(mm), mm); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2110 | if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) { |
| 2111 | free_mm_slot(mm_slot); |
| 2112 | return 0; |
| 2113 | } |
| 2114 | |
| 2115 | spin_lock(&khugepaged_mm_lock); |
| 2116 | insert_to_mm_slots_hash(mm, mm_slot); |
| 2117 | /* |
| 2118 | * Insert just behind the scanning cursor, to let the area settle |
| 2119 | * down a little. |
| 2120 | */ |
| 2121 | wakeup = list_empty(&khugepaged_scan.mm_head); |
| 2122 | list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head); |
| 2123 | spin_unlock(&khugepaged_mm_lock); |
| 2124 | |
| 2125 | atomic_inc(&mm->mm_count); |
| 2126 | if (wakeup) |
| 2127 | wake_up_interruptible(&khugepaged_wait); |
| 2128 | |
| 2129 | return 0; |
| 2130 | } |
| 2131 | |
David Rientjes | 6d50e60 | 2014-10-29 14:50:31 -0700 | [diff] [blame] | 2132 | int khugepaged_enter_vma_merge(struct vm_area_struct *vma, |
| 2133 | unsigned long vm_flags) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2134 | { |
| 2135 | unsigned long hstart, hend; |
| 2136 | if (!vma->anon_vma) |
| 2137 | /* |
| 2138 | * Not yet faulted in so we will register later in the |
| 2139 | * page fault if needed. |
| 2140 | */ |
| 2141 | return 0; |
Andrea Arcangeli | 78f11a2 | 2011-04-27 15:26:45 -0700 | [diff] [blame] | 2142 | if (vma->vm_ops) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2143 | /* khugepaged not yet working on file or special mappings */ |
| 2144 | return 0; |
David Rientjes | 6d50e60 | 2014-10-29 14:50:31 -0700 | [diff] [blame] | 2145 | VM_BUG_ON_VMA(vm_flags & VM_NO_THP, vma); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2146 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; |
| 2147 | hend = vma->vm_end & HPAGE_PMD_MASK; |
| 2148 | if (hstart < hend) |
David Rientjes | 6d50e60 | 2014-10-29 14:50:31 -0700 | [diff] [blame] | 2149 | return khugepaged_enter(vma, vm_flags); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2150 | return 0; |
| 2151 | } |
| 2152 | |
| 2153 | void __khugepaged_exit(struct mm_struct *mm) |
| 2154 | { |
| 2155 | struct mm_slot *mm_slot; |
| 2156 | int free = 0; |
| 2157 | |
| 2158 | spin_lock(&khugepaged_mm_lock); |
| 2159 | mm_slot = get_mm_slot(mm); |
| 2160 | if (mm_slot && khugepaged_scan.mm_slot != mm_slot) { |
Sasha Levin | 43b5fbb | 2013-02-22 16:32:27 -0800 | [diff] [blame] | 2161 | hash_del(&mm_slot->hash); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2162 | list_del(&mm_slot->mm_node); |
| 2163 | free = 1; |
| 2164 | } |
Chris Wright | d788e80 | 2011-07-25 17:12:14 -0700 | [diff] [blame] | 2165 | spin_unlock(&khugepaged_mm_lock); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2166 | |
| 2167 | if (free) { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2168 | clear_bit(MMF_VM_HUGEPAGE, &mm->flags); |
| 2169 | free_mm_slot(mm_slot); |
| 2170 | mmdrop(mm); |
| 2171 | } else if (mm_slot) { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2172 | /* |
| 2173 | * This is required to serialize against |
| 2174 | * khugepaged_test_exit() (which is guaranteed to run |
| 2175 | * under mmap sem read mode). Stop here (after we |
| 2176 | * return all pagetables will be destroyed) until |
| 2177 | * khugepaged has finished working on the pagetables |
| 2178 | * under the mmap_sem. |
| 2179 | */ |
| 2180 | down_write(&mm->mmap_sem); |
| 2181 | up_write(&mm->mmap_sem); |
Chris Wright | d788e80 | 2011-07-25 17:12:14 -0700 | [diff] [blame] | 2182 | } |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | static void release_pte_page(struct page *page) |
| 2186 | { |
| 2187 | /* 0 stands for page_is_file_cache(page) == false */ |
| 2188 | dec_zone_page_state(page, NR_ISOLATED_ANON + 0); |
| 2189 | unlock_page(page); |
| 2190 | putback_lru_page(page); |
| 2191 | } |
| 2192 | |
| 2193 | static void release_pte_pages(pte_t *pte, pte_t *_pte) |
| 2194 | { |
| 2195 | while (--_pte >= pte) { |
| 2196 | pte_t pteval = *_pte; |
Ebru Akagunduz | ca0984c | 2015-04-14 15:45:24 -0700 | [diff] [blame] | 2197 | if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval))) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2198 | release_pte_page(pte_page(pteval)); |
| 2199 | } |
| 2200 | } |
| 2201 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2202 | static int __collapse_huge_page_isolate(struct vm_area_struct *vma, |
| 2203 | unsigned long address, |
| 2204 | pte_t *pte) |
| 2205 | { |
| 2206 | struct page *page; |
| 2207 | pte_t *_pte; |
Ebru Akagunduz | ca0984c | 2015-04-14 15:45:24 -0700 | [diff] [blame] | 2208 | int none_or_zero = 0; |
Ebru Akagunduz | 1035921 | 2015-02-11 15:28:28 -0800 | [diff] [blame] | 2209 | bool referenced = false, writable = false; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2210 | for (_pte = pte; _pte < pte+HPAGE_PMD_NR; |
| 2211 | _pte++, address += PAGE_SIZE) { |
| 2212 | pte_t pteval = *_pte; |
Ebru Akagunduz | ca0984c | 2015-04-14 15:45:24 -0700 | [diff] [blame] | 2213 | if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { |
Andrea Arcangeli | c1294d0 | 2015-09-04 15:46:27 -0700 | [diff] [blame] | 2214 | if (!userfaultfd_armed(vma) && |
| 2215 | ++none_or_zero <= khugepaged_max_ptes_none) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2216 | continue; |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2217 | else |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2218 | goto out; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2219 | } |
Ebru Akagunduz | 1035921 | 2015-02-11 15:28:28 -0800 | [diff] [blame] | 2220 | if (!pte_present(pteval)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2221 | goto out; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2222 | page = vm_normal_page(vma, address, pteval); |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2223 | if (unlikely(!page)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2224 | goto out; |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2225 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 2226 | VM_BUG_ON_PAGE(PageCompound(page), page); |
| 2227 | VM_BUG_ON_PAGE(!PageAnon(page), page); |
| 2228 | VM_BUG_ON_PAGE(!PageSwapBacked(page), page); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2229 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2230 | /* |
| 2231 | * We can do it before isolate_lru_page because the |
| 2232 | * page can't be freed from under us. NOTE: PG_lock |
| 2233 | * is needed to serialize against split_huge_page |
| 2234 | * when invoked from the VM. |
| 2235 | */ |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2236 | if (!trylock_page(page)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2237 | goto out; |
Ebru Akagunduz | 1035921 | 2015-02-11 15:28:28 -0800 | [diff] [blame] | 2238 | |
| 2239 | /* |
| 2240 | * cannot use mapcount: can't collapse if there's a gup pin. |
| 2241 | * The page must only be referenced by the scanned process |
| 2242 | * and page swap cache. |
| 2243 | */ |
| 2244 | if (page_count(page) != 1 + !!PageSwapCache(page)) { |
| 2245 | unlock_page(page); |
| 2246 | goto out; |
| 2247 | } |
| 2248 | if (pte_write(pteval)) { |
| 2249 | writable = true; |
| 2250 | } else { |
| 2251 | if (PageSwapCache(page) && !reuse_swap_page(page)) { |
| 2252 | unlock_page(page); |
| 2253 | goto out; |
| 2254 | } |
| 2255 | /* |
| 2256 | * Page is not in the swap cache. It can be collapsed |
| 2257 | * into a THP. |
| 2258 | */ |
| 2259 | } |
| 2260 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2261 | /* |
| 2262 | * Isolate the page to avoid collapsing an hugepage |
| 2263 | * currently in use by the VM. |
| 2264 | */ |
| 2265 | if (isolate_lru_page(page)) { |
| 2266 | unlock_page(page); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2267 | goto out; |
| 2268 | } |
| 2269 | /* 0 stands for page_is_file_cache(page) == false */ |
| 2270 | inc_zone_page_state(page, NR_ISOLATED_ANON + 0); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 2271 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 2272 | VM_BUG_ON_PAGE(PageLRU(page), page); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2273 | |
| 2274 | /* If there is no mapped pte young don't collapse the page */ |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 2275 | if (pte_young(pteval) || PageReferenced(page) || |
| 2276 | mmu_notifier_test_young(vma->vm_mm, address)) |
Ebru Akagunduz | 1035921 | 2015-02-11 15:28:28 -0800 | [diff] [blame] | 2277 | referenced = true; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2278 | } |
Ebru Akagunduz | 1035921 | 2015-02-11 15:28:28 -0800 | [diff] [blame] | 2279 | if (likely(referenced && writable)) |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2280 | return 1; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2281 | out: |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2282 | release_pte_pages(pte, _pte); |
| 2283 | return 0; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | static void __collapse_huge_page_copy(pte_t *pte, struct page *page, |
| 2287 | struct vm_area_struct *vma, |
| 2288 | unsigned long address, |
| 2289 | spinlock_t *ptl) |
| 2290 | { |
| 2291 | pte_t *_pte; |
| 2292 | for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) { |
| 2293 | pte_t pteval = *_pte; |
| 2294 | struct page *src_page; |
| 2295 | |
Ebru Akagunduz | ca0984c | 2015-04-14 15:45:24 -0700 | [diff] [blame] | 2296 | if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2297 | clear_user_highpage(page, address); |
| 2298 | add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1); |
Ebru Akagunduz | ca0984c | 2015-04-14 15:45:24 -0700 | [diff] [blame] | 2299 | if (is_zero_pfn(pte_pfn(pteval))) { |
| 2300 | /* |
| 2301 | * ptl mostly unnecessary. |
| 2302 | */ |
| 2303 | spin_lock(ptl); |
| 2304 | /* |
| 2305 | * paravirt calls inside pte_clear here are |
| 2306 | * superfluous. |
| 2307 | */ |
| 2308 | pte_clear(vma->vm_mm, address, _pte); |
| 2309 | spin_unlock(ptl); |
| 2310 | } |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2311 | } else { |
| 2312 | src_page = pte_page(pteval); |
| 2313 | copy_user_highpage(page, src_page, address, vma); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 2314 | VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2315 | release_pte_page(src_page); |
| 2316 | /* |
| 2317 | * ptl mostly unnecessary, but preempt has to |
| 2318 | * be disabled to update the per-cpu stats |
| 2319 | * inside page_remove_rmap(). |
| 2320 | */ |
| 2321 | spin_lock(ptl); |
| 2322 | /* |
| 2323 | * paravirt calls inside pte_clear here are |
| 2324 | * superfluous. |
| 2325 | */ |
| 2326 | pte_clear(vma->vm_mm, address, _pte); |
| 2327 | page_remove_rmap(src_page); |
| 2328 | spin_unlock(ptl); |
| 2329 | free_page_and_swap_cache(src_page); |
| 2330 | } |
| 2331 | |
| 2332 | address += PAGE_SIZE; |
| 2333 | page++; |
| 2334 | } |
| 2335 | } |
| 2336 | |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2337 | static void khugepaged_alloc_sleep(void) |
| 2338 | { |
| 2339 | wait_event_freezable_timeout(khugepaged_wait, false, |
| 2340 | msecs_to_jiffies(khugepaged_alloc_sleep_millisecs)); |
| 2341 | } |
| 2342 | |
Bob Liu | 9f1b868 | 2013-11-12 15:07:37 -0800 | [diff] [blame] | 2343 | static int khugepaged_node_load[MAX_NUMNODES]; |
| 2344 | |
David Rientjes | 14a4e21 | 2014-08-06 16:07:29 -0700 | [diff] [blame] | 2345 | static bool khugepaged_scan_abort(int nid) |
| 2346 | { |
| 2347 | int i; |
| 2348 | |
| 2349 | /* |
| 2350 | * If zone_reclaim_mode is disabled, then no extra effort is made to |
| 2351 | * allocate memory locally. |
| 2352 | */ |
| 2353 | if (!zone_reclaim_mode) |
| 2354 | return false; |
| 2355 | |
| 2356 | /* If there is a count for this node already, it must be acceptable */ |
| 2357 | if (khugepaged_node_load[nid]) |
| 2358 | return false; |
| 2359 | |
| 2360 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 2361 | if (!khugepaged_node_load[i]) |
| 2362 | continue; |
| 2363 | if (node_distance(nid, i) > RECLAIM_DISTANCE) |
| 2364 | return true; |
| 2365 | } |
| 2366 | return false; |
| 2367 | } |
| 2368 | |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2369 | #ifdef CONFIG_NUMA |
Bob Liu | 9f1b868 | 2013-11-12 15:07:37 -0800 | [diff] [blame] | 2370 | static int khugepaged_find_target_node(void) |
| 2371 | { |
| 2372 | static int last_khugepaged_target_node = NUMA_NO_NODE; |
| 2373 | int nid, target_node = 0, max_value = 0; |
| 2374 | |
| 2375 | /* find first node with max normal pages hit */ |
| 2376 | for (nid = 0; nid < MAX_NUMNODES; nid++) |
| 2377 | if (khugepaged_node_load[nid] > max_value) { |
| 2378 | max_value = khugepaged_node_load[nid]; |
| 2379 | target_node = nid; |
| 2380 | } |
| 2381 | |
| 2382 | /* do some balance if several nodes have the same hit record */ |
| 2383 | if (target_node <= last_khugepaged_target_node) |
| 2384 | for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES; |
| 2385 | nid++) |
| 2386 | if (max_value == khugepaged_node_load[nid]) { |
| 2387 | target_node = nid; |
| 2388 | break; |
| 2389 | } |
| 2390 | |
| 2391 | last_khugepaged_target_node = target_node; |
| 2392 | return target_node; |
| 2393 | } |
| 2394 | |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2395 | static bool khugepaged_prealloc_page(struct page **hpage, bool *wait) |
| 2396 | { |
| 2397 | if (IS_ERR(*hpage)) { |
| 2398 | if (!*wait) |
| 2399 | return false; |
| 2400 | |
| 2401 | *wait = false; |
Xiao Guangrong | e3b4126 | 2012-10-08 16:32:57 -0700 | [diff] [blame] | 2402 | *hpage = NULL; |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2403 | khugepaged_alloc_sleep(); |
| 2404 | } else if (*hpage) { |
| 2405 | put_page(*hpage); |
| 2406 | *hpage = NULL; |
| 2407 | } |
| 2408 | |
| 2409 | return true; |
| 2410 | } |
| 2411 | |
Michal Hocko | 3b36369 | 2015-04-15 16:13:29 -0700 | [diff] [blame] | 2412 | static struct page * |
| 2413 | khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm, |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2414 | struct vm_area_struct *vma, unsigned long address, |
| 2415 | int node) |
| 2416 | { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 2417 | VM_BUG_ON_PAGE(*hpage, *hpage); |
Vlastimil Babka | 8b16456 | 2014-10-09 15:27:00 -0700 | [diff] [blame] | 2418 | |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2419 | /* |
Vlastimil Babka | 8b16456 | 2014-10-09 15:27:00 -0700 | [diff] [blame] | 2420 | * Before allocating the hugepage, release the mmap_sem read lock. |
| 2421 | * The allocation can take potentially a long time if it involves |
| 2422 | * sync compaction, and we do not need to hold the mmap_sem during |
| 2423 | * that. We will recheck the vma after taking it again in write mode. |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2424 | */ |
| 2425 | up_read(&mm->mmap_sem); |
Vlastimil Babka | 8b16456 | 2014-10-09 15:27:00 -0700 | [diff] [blame] | 2426 | |
Michal Hocko | 3b36369 | 2015-04-15 16:13:29 -0700 | [diff] [blame] | 2427 | *hpage = alloc_pages_exact_node(node, gfp, HPAGE_PMD_ORDER); |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2428 | if (unlikely(!*hpage)) { |
| 2429 | count_vm_event(THP_COLLAPSE_ALLOC_FAILED); |
| 2430 | *hpage = ERR_PTR(-ENOMEM); |
| 2431 | return NULL; |
| 2432 | } |
| 2433 | |
| 2434 | count_vm_event(THP_COLLAPSE_ALLOC); |
| 2435 | return *hpage; |
| 2436 | } |
| 2437 | #else |
Bob Liu | 9f1b868 | 2013-11-12 15:07:37 -0800 | [diff] [blame] | 2438 | static int khugepaged_find_target_node(void) |
| 2439 | { |
| 2440 | return 0; |
| 2441 | } |
| 2442 | |
Bob Liu | 10dc415 | 2013-11-12 15:07:35 -0800 | [diff] [blame] | 2443 | static inline struct page *alloc_hugepage(int defrag) |
| 2444 | { |
| 2445 | return alloc_pages(alloc_hugepage_gfpmask(defrag, 0), |
| 2446 | HPAGE_PMD_ORDER); |
| 2447 | } |
| 2448 | |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2449 | static struct page *khugepaged_alloc_hugepage(bool *wait) |
| 2450 | { |
| 2451 | struct page *hpage; |
| 2452 | |
| 2453 | do { |
| 2454 | hpage = alloc_hugepage(khugepaged_defrag()); |
| 2455 | if (!hpage) { |
| 2456 | count_vm_event(THP_COLLAPSE_ALLOC_FAILED); |
| 2457 | if (!*wait) |
| 2458 | return NULL; |
| 2459 | |
| 2460 | *wait = false; |
| 2461 | khugepaged_alloc_sleep(); |
| 2462 | } else |
| 2463 | count_vm_event(THP_COLLAPSE_ALLOC); |
| 2464 | } while (unlikely(!hpage) && likely(khugepaged_enabled())); |
| 2465 | |
| 2466 | return hpage; |
| 2467 | } |
| 2468 | |
| 2469 | static bool khugepaged_prealloc_page(struct page **hpage, bool *wait) |
| 2470 | { |
| 2471 | if (!*hpage) |
| 2472 | *hpage = khugepaged_alloc_hugepage(wait); |
| 2473 | |
| 2474 | if (unlikely(!*hpage)) |
| 2475 | return false; |
| 2476 | |
| 2477 | return true; |
| 2478 | } |
| 2479 | |
Michal Hocko | 3b36369 | 2015-04-15 16:13:29 -0700 | [diff] [blame] | 2480 | static struct page * |
| 2481 | khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm, |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2482 | struct vm_area_struct *vma, unsigned long address, |
| 2483 | int node) |
| 2484 | { |
| 2485 | up_read(&mm->mmap_sem); |
| 2486 | VM_BUG_ON(!*hpage); |
Michal Hocko | 3b36369 | 2015-04-15 16:13:29 -0700 | [diff] [blame] | 2487 | |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2488 | return *hpage; |
| 2489 | } |
| 2490 | #endif |
| 2491 | |
Bob Liu | fa475e5 | 2012-12-11 16:00:39 -0800 | [diff] [blame] | 2492 | static bool hugepage_vma_check(struct vm_area_struct *vma) |
| 2493 | { |
| 2494 | if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) || |
| 2495 | (vma->vm_flags & VM_NOHUGEPAGE)) |
| 2496 | return false; |
| 2497 | |
| 2498 | if (!vma->anon_vma || vma->vm_ops) |
| 2499 | return false; |
| 2500 | if (is_vma_temporary_stack(vma)) |
| 2501 | return false; |
Sasha Levin | 81d1b09 | 2014-10-09 15:28:10 -0700 | [diff] [blame] | 2502 | VM_BUG_ON_VMA(vma->vm_flags & VM_NO_THP, vma); |
Bob Liu | fa475e5 | 2012-12-11 16:00:39 -0800 | [diff] [blame] | 2503 | return true; |
| 2504 | } |
| 2505 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2506 | static void collapse_huge_page(struct mm_struct *mm, |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2507 | unsigned long address, |
| 2508 | struct page **hpage, |
| 2509 | struct vm_area_struct *vma, |
| 2510 | int node) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2511 | { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2512 | pmd_t *pmd, _pmd; |
| 2513 | pte_t *pte; |
| 2514 | pgtable_t pgtable; |
| 2515 | struct page *new_page; |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2516 | spinlock_t *pmd_ptl, *pte_ptl; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2517 | int isolated; |
| 2518 | unsigned long hstart, hend; |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 2519 | struct mem_cgroup *memcg; |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 2520 | unsigned long mmun_start; /* For mmu_notifiers */ |
| 2521 | unsigned long mmun_end; /* For mmu_notifiers */ |
Michal Hocko | 3b36369 | 2015-04-15 16:13:29 -0700 | [diff] [blame] | 2522 | gfp_t gfp; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2523 | |
| 2524 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
Andrea Arcangeli | 692e0b3 | 2011-05-24 17:12:14 -0700 | [diff] [blame] | 2525 | |
Michal Hocko | 3b36369 | 2015-04-15 16:13:29 -0700 | [diff] [blame] | 2526 | /* Only allocate from the target node */ |
| 2527 | gfp = alloc_hugepage_gfpmask(khugepaged_defrag(), __GFP_OTHER_NODE) | |
| 2528 | __GFP_THISNODE; |
| 2529 | |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2530 | /* release the mmap_sem read lock. */ |
Michal Hocko | 3b36369 | 2015-04-15 16:13:29 -0700 | [diff] [blame] | 2531 | new_page = khugepaged_alloc_page(hpage, gfp, mm, vma, address, node); |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2532 | if (!new_page) |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2533 | return; |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2534 | |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 2535 | if (unlikely(mem_cgroup_try_charge(new_page, mm, |
Michal Hocko | 3b36369 | 2015-04-15 16:13:29 -0700 | [diff] [blame] | 2536 | gfp, &memcg))) |
Andrea Arcangeli | 692e0b3 | 2011-05-24 17:12:14 -0700 | [diff] [blame] | 2537 | return; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2538 | |
| 2539 | /* |
| 2540 | * Prevent all access to pagetables with the exception of |
| 2541 | * gup_fast later hanlded by the ptep_clear_flush and the VM |
| 2542 | * handled by the anon_vma lock + PG_lock. |
| 2543 | */ |
| 2544 | down_write(&mm->mmap_sem); |
| 2545 | if (unlikely(khugepaged_test_exit(mm))) |
| 2546 | goto out; |
| 2547 | |
| 2548 | vma = find_vma(mm, address); |
Libin | a8f531eb | 2013-09-11 14:20:38 -0700 | [diff] [blame] | 2549 | if (!vma) |
| 2550 | goto out; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2551 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; |
| 2552 | hend = vma->vm_end & HPAGE_PMD_MASK; |
| 2553 | if (address < hstart || address + HPAGE_PMD_SIZE > hend) |
| 2554 | goto out; |
Bob Liu | fa475e5 | 2012-12-11 16:00:39 -0800 | [diff] [blame] | 2555 | if (!hugepage_vma_check(vma)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2556 | goto out; |
Bob Liu | 6219049 | 2012-12-11 16:00:37 -0800 | [diff] [blame] | 2557 | pmd = mm_find_pmd(mm, address); |
| 2558 | if (!pmd) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2559 | goto out; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2560 | |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 2561 | anon_vma_lock_write(vma->anon_vma); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2562 | |
| 2563 | pte = pte_offset_map(pmd, address); |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2564 | pte_ptl = pte_lockptr(mm, pmd); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2565 | |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 2566 | mmun_start = address; |
| 2567 | mmun_end = address + HPAGE_PMD_SIZE; |
| 2568 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2569 | pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */ |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2570 | /* |
| 2571 | * After this gup_fast can't run anymore. This also removes |
| 2572 | * any huge TLB entry from the CPU so we won't allow |
| 2573 | * huge and small TLB entries for the same virtual address |
| 2574 | * to avoid the risk of CPU bugs in that area. |
| 2575 | */ |
Aneesh Kumar K.V | 15a25b2 | 2015-06-24 16:57:39 -0700 | [diff] [blame] | 2576 | _pmd = pmdp_collapse_flush(vma, address, pmd); |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2577 | spin_unlock(pmd_ptl); |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 2578 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2579 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2580 | spin_lock(pte_ptl); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2581 | isolated = __collapse_huge_page_isolate(vma, address, pte); |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2582 | spin_unlock(pte_ptl); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2583 | |
| 2584 | if (unlikely(!isolated)) { |
Johannes Weiner | 453c719 | 2011-01-20 14:44:18 -0800 | [diff] [blame] | 2585 | pte_unmap(pte); |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2586 | spin_lock(pmd_ptl); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2587 | BUG_ON(!pmd_none(*pmd)); |
Aneesh Kumar K.V | 7c34251 | 2013-05-24 15:55:21 -0700 | [diff] [blame] | 2588 | /* |
| 2589 | * We can only use set_pmd_at when establishing |
| 2590 | * hugepmds and never for establishing regular pmds that |
| 2591 | * points to regular pagetables. Use pmd_populate for that |
| 2592 | */ |
| 2593 | pmd_populate(mm, pmd, pmd_pgtable(_pmd)); |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2594 | spin_unlock(pmd_ptl); |
Konstantin Khlebnikov | 08b5270 | 2013-02-22 16:34:40 -0800 | [diff] [blame] | 2595 | anon_vma_unlock_write(vma->anon_vma); |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2596 | goto out; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | /* |
| 2600 | * All pages are isolated and locked so anon_vma rmap |
| 2601 | * can't run anymore. |
| 2602 | */ |
Konstantin Khlebnikov | 08b5270 | 2013-02-22 16:34:40 -0800 | [diff] [blame] | 2603 | anon_vma_unlock_write(vma->anon_vma); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2604 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2605 | __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl); |
Johannes Weiner | 453c719 | 2011-01-20 14:44:18 -0800 | [diff] [blame] | 2606 | pte_unmap(pte); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2607 | __SetPageUptodate(new_page); |
| 2608 | pgtable = pmd_pgtable(_pmd); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2609 | |
Kirill A. Shutemov | 3122359 | 2013-09-12 15:14:01 -0700 | [diff] [blame] | 2610 | _pmd = mk_huge_pmd(new_page, vma->vm_page_prot); |
| 2611 | _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2612 | |
| 2613 | /* |
| 2614 | * spin_lock() below is not the equivalent of smp_wmb(), so |
| 2615 | * this is needed to avoid the copy_huge_page writes to become |
| 2616 | * visible after the set_pmd_at() write. |
| 2617 | */ |
| 2618 | smp_wmb(); |
| 2619 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2620 | spin_lock(pmd_ptl); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2621 | BUG_ON(!pmd_none(*pmd)); |
| 2622 | page_add_new_anon_rmap(new_page, vma, address); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 2623 | mem_cgroup_commit_charge(new_page, memcg, false); |
| 2624 | lru_cache_add_active_or_unevictable(new_page, vma); |
Aneesh Kumar K.V | fce144b | 2013-06-05 17:14:06 -0700 | [diff] [blame] | 2625 | pgtable_trans_huge_deposit(mm, pmd, pgtable); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2626 | set_pmd_at(mm, address, pmd, _pmd); |
David Miller | b113da6 | 2012-10-08 16:34:25 -0700 | [diff] [blame] | 2627 | update_mmu_cache_pmd(vma, address, pmd); |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2628 | spin_unlock(pmd_ptl); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2629 | |
| 2630 | *hpage = NULL; |
Xiao Guangrong | 420256ef | 2012-10-08 16:29:49 -0700 | [diff] [blame] | 2631 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2632 | khugepaged_pages_collapsed++; |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2633 | out_up_write: |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2634 | up_write(&mm->mmap_sem); |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 2635 | return; |
| 2636 | |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2637 | out: |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 2638 | mem_cgroup_cancel_charge(new_page, memcg); |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2639 | goto out_up_write; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2640 | } |
| 2641 | |
| 2642 | static int khugepaged_scan_pmd(struct mm_struct *mm, |
| 2643 | struct vm_area_struct *vma, |
| 2644 | unsigned long address, |
| 2645 | struct page **hpage) |
| 2646 | { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2647 | pmd_t *pmd; |
| 2648 | pte_t *pte, *_pte; |
Ebru Akagunduz | ca0984c | 2015-04-14 15:45:24 -0700 | [diff] [blame] | 2649 | int ret = 0, none_or_zero = 0; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2650 | struct page *page; |
| 2651 | unsigned long _address; |
| 2652 | spinlock_t *ptl; |
David Rientjes | 00ef2d2 | 2013-02-22 16:35:36 -0800 | [diff] [blame] | 2653 | int node = NUMA_NO_NODE; |
Ebru Akagunduz | 1035921 | 2015-02-11 15:28:28 -0800 | [diff] [blame] | 2654 | bool writable = false, referenced = false; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2655 | |
| 2656 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
| 2657 | |
Bob Liu | 6219049 | 2012-12-11 16:00:37 -0800 | [diff] [blame] | 2658 | pmd = mm_find_pmd(mm, address); |
| 2659 | if (!pmd) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2660 | goto out; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2661 | |
Bob Liu | 9f1b868 | 2013-11-12 15:07:37 -0800 | [diff] [blame] | 2662 | memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load)); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2663 | pte = pte_offset_map_lock(mm, pmd, address, &ptl); |
| 2664 | for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR; |
| 2665 | _pte++, _address += PAGE_SIZE) { |
| 2666 | pte_t pteval = *_pte; |
Ebru Akagunduz | ca0984c | 2015-04-14 15:45:24 -0700 | [diff] [blame] | 2667 | if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { |
Andrea Arcangeli | c1294d0 | 2015-09-04 15:46:27 -0700 | [diff] [blame] | 2668 | if (!userfaultfd_armed(vma) && |
| 2669 | ++none_or_zero <= khugepaged_max_ptes_none) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2670 | continue; |
| 2671 | else |
| 2672 | goto out_unmap; |
| 2673 | } |
Ebru Akagunduz | 1035921 | 2015-02-11 15:28:28 -0800 | [diff] [blame] | 2674 | if (!pte_present(pteval)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2675 | goto out_unmap; |
Ebru Akagunduz | 1035921 | 2015-02-11 15:28:28 -0800 | [diff] [blame] | 2676 | if (pte_write(pteval)) |
| 2677 | writable = true; |
| 2678 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2679 | page = vm_normal_page(vma, _address, pteval); |
| 2680 | if (unlikely(!page)) |
| 2681 | goto out_unmap; |
Andi Kleen | 5c4b4be | 2011-03-04 17:36:32 -0800 | [diff] [blame] | 2682 | /* |
Bob Liu | 9f1b868 | 2013-11-12 15:07:37 -0800 | [diff] [blame] | 2683 | * Record which node the original page is from and save this |
| 2684 | * information to khugepaged_node_load[]. |
| 2685 | * Khupaged will allocate hugepage from the node has the max |
| 2686 | * hit record. |
Andi Kleen | 5c4b4be | 2011-03-04 17:36:32 -0800 | [diff] [blame] | 2687 | */ |
Bob Liu | 9f1b868 | 2013-11-12 15:07:37 -0800 | [diff] [blame] | 2688 | node = page_to_nid(page); |
David Rientjes | 14a4e21 | 2014-08-06 16:07:29 -0700 | [diff] [blame] | 2689 | if (khugepaged_scan_abort(node)) |
| 2690 | goto out_unmap; |
Bob Liu | 9f1b868 | 2013-11-12 15:07:37 -0800 | [diff] [blame] | 2691 | khugepaged_node_load[node]++; |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 2692 | VM_BUG_ON_PAGE(PageCompound(page), page); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2693 | if (!PageLRU(page) || PageLocked(page) || !PageAnon(page)) |
| 2694 | goto out_unmap; |
Ebru Akagunduz | 1035921 | 2015-02-11 15:28:28 -0800 | [diff] [blame] | 2695 | /* |
| 2696 | * cannot use mapcount: can't collapse if there's a gup pin. |
| 2697 | * The page must only be referenced by the scanned process |
| 2698 | * and page swap cache. |
| 2699 | */ |
| 2700 | if (page_count(page) != 1 + !!PageSwapCache(page)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2701 | goto out_unmap; |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 2702 | if (pte_young(pteval) || PageReferenced(page) || |
| 2703 | mmu_notifier_test_young(vma->vm_mm, address)) |
Ebru Akagunduz | 1035921 | 2015-02-11 15:28:28 -0800 | [diff] [blame] | 2704 | referenced = true; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2705 | } |
Ebru Akagunduz | 1035921 | 2015-02-11 15:28:28 -0800 | [diff] [blame] | 2706 | if (referenced && writable) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2707 | ret = 1; |
| 2708 | out_unmap: |
| 2709 | pte_unmap_unlock(pte, ptl); |
Bob Liu | 9f1b868 | 2013-11-12 15:07:37 -0800 | [diff] [blame] | 2710 | if (ret) { |
| 2711 | node = khugepaged_find_target_node(); |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2712 | /* collapse_huge_page will return with the mmap_sem released */ |
Andi Kleen | 5c4b4be | 2011-03-04 17:36:32 -0800 | [diff] [blame] | 2713 | collapse_huge_page(mm, address, hpage, vma, node); |
Bob Liu | 9f1b868 | 2013-11-12 15:07:37 -0800 | [diff] [blame] | 2714 | } |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2715 | out: |
| 2716 | return ret; |
| 2717 | } |
| 2718 | |
| 2719 | static void collect_mm_slot(struct mm_slot *mm_slot) |
| 2720 | { |
| 2721 | struct mm_struct *mm = mm_slot->mm; |
| 2722 | |
Hugh Dickins | b9980cd | 2012-02-08 17:13:40 -0800 | [diff] [blame] | 2723 | VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock)); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2724 | |
| 2725 | if (khugepaged_test_exit(mm)) { |
| 2726 | /* free mm_slot */ |
Sasha Levin | 43b5fbb | 2013-02-22 16:32:27 -0800 | [diff] [blame] | 2727 | hash_del(&mm_slot->hash); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2728 | list_del(&mm_slot->mm_node); |
| 2729 | |
| 2730 | /* |
| 2731 | * Not strictly needed because the mm exited already. |
| 2732 | * |
| 2733 | * clear_bit(MMF_VM_HUGEPAGE, &mm->flags); |
| 2734 | */ |
| 2735 | |
| 2736 | /* khugepaged_mm_lock actually not necessary for the below */ |
| 2737 | free_mm_slot(mm_slot); |
| 2738 | mmdrop(mm); |
| 2739 | } |
| 2740 | } |
| 2741 | |
| 2742 | static unsigned int khugepaged_scan_mm_slot(unsigned int pages, |
| 2743 | struct page **hpage) |
H Hartley Sweeten | 2f1da64 | 2011-10-31 17:09:25 -0700 | [diff] [blame] | 2744 | __releases(&khugepaged_mm_lock) |
| 2745 | __acquires(&khugepaged_mm_lock) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2746 | { |
| 2747 | struct mm_slot *mm_slot; |
| 2748 | struct mm_struct *mm; |
| 2749 | struct vm_area_struct *vma; |
| 2750 | int progress = 0; |
| 2751 | |
| 2752 | VM_BUG_ON(!pages); |
Hugh Dickins | b9980cd | 2012-02-08 17:13:40 -0800 | [diff] [blame] | 2753 | VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock)); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2754 | |
| 2755 | if (khugepaged_scan.mm_slot) |
| 2756 | mm_slot = khugepaged_scan.mm_slot; |
| 2757 | else { |
| 2758 | mm_slot = list_entry(khugepaged_scan.mm_head.next, |
| 2759 | struct mm_slot, mm_node); |
| 2760 | khugepaged_scan.address = 0; |
| 2761 | khugepaged_scan.mm_slot = mm_slot; |
| 2762 | } |
| 2763 | spin_unlock(&khugepaged_mm_lock); |
| 2764 | |
| 2765 | mm = mm_slot->mm; |
| 2766 | down_read(&mm->mmap_sem); |
| 2767 | if (unlikely(khugepaged_test_exit(mm))) |
| 2768 | vma = NULL; |
| 2769 | else |
| 2770 | vma = find_vma(mm, khugepaged_scan.address); |
| 2771 | |
| 2772 | progress++; |
| 2773 | for (; vma; vma = vma->vm_next) { |
| 2774 | unsigned long hstart, hend; |
| 2775 | |
| 2776 | cond_resched(); |
| 2777 | if (unlikely(khugepaged_test_exit(mm))) { |
| 2778 | progress++; |
| 2779 | break; |
| 2780 | } |
Bob Liu | fa475e5 | 2012-12-11 16:00:39 -0800 | [diff] [blame] | 2781 | if (!hugepage_vma_check(vma)) { |
| 2782 | skip: |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2783 | progress++; |
| 2784 | continue; |
| 2785 | } |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2786 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; |
| 2787 | hend = vma->vm_end & HPAGE_PMD_MASK; |
Andrea Arcangeli | a7d6e4e | 2011-02-15 19:02:45 +0100 | [diff] [blame] | 2788 | if (hstart >= hend) |
| 2789 | goto skip; |
| 2790 | if (khugepaged_scan.address > hend) |
| 2791 | goto skip; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2792 | if (khugepaged_scan.address < hstart) |
| 2793 | khugepaged_scan.address = hstart; |
Andrea Arcangeli | a7d6e4e | 2011-02-15 19:02:45 +0100 | [diff] [blame] | 2794 | VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2795 | |
| 2796 | while (khugepaged_scan.address < hend) { |
| 2797 | int ret; |
| 2798 | cond_resched(); |
| 2799 | if (unlikely(khugepaged_test_exit(mm))) |
| 2800 | goto breakouterloop; |
| 2801 | |
| 2802 | VM_BUG_ON(khugepaged_scan.address < hstart || |
| 2803 | khugepaged_scan.address + HPAGE_PMD_SIZE > |
| 2804 | hend); |
| 2805 | ret = khugepaged_scan_pmd(mm, vma, |
| 2806 | khugepaged_scan.address, |
| 2807 | hpage); |
| 2808 | /* move to next address */ |
| 2809 | khugepaged_scan.address += HPAGE_PMD_SIZE; |
| 2810 | progress += HPAGE_PMD_NR; |
| 2811 | if (ret) |
| 2812 | /* we released mmap_sem so break loop */ |
| 2813 | goto breakouterloop_mmap_sem; |
| 2814 | if (progress >= pages) |
| 2815 | goto breakouterloop; |
| 2816 | } |
| 2817 | } |
| 2818 | breakouterloop: |
| 2819 | up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */ |
| 2820 | breakouterloop_mmap_sem: |
| 2821 | |
| 2822 | spin_lock(&khugepaged_mm_lock); |
Andrea Arcangeli | a7d6e4e | 2011-02-15 19:02:45 +0100 | [diff] [blame] | 2823 | VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2824 | /* |
| 2825 | * Release the current mm_slot if this mm is about to die, or |
| 2826 | * if we scanned all vmas of this mm. |
| 2827 | */ |
| 2828 | if (khugepaged_test_exit(mm) || !vma) { |
| 2829 | /* |
| 2830 | * Make sure that if mm_users is reaching zero while |
| 2831 | * khugepaged runs here, khugepaged_exit will find |
| 2832 | * mm_slot not pointing to the exiting mm. |
| 2833 | */ |
| 2834 | if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) { |
| 2835 | khugepaged_scan.mm_slot = list_entry( |
| 2836 | mm_slot->mm_node.next, |
| 2837 | struct mm_slot, mm_node); |
| 2838 | khugepaged_scan.address = 0; |
| 2839 | } else { |
| 2840 | khugepaged_scan.mm_slot = NULL; |
| 2841 | khugepaged_full_scans++; |
| 2842 | } |
| 2843 | |
| 2844 | collect_mm_slot(mm_slot); |
| 2845 | } |
| 2846 | |
| 2847 | return progress; |
| 2848 | } |
| 2849 | |
| 2850 | static int khugepaged_has_work(void) |
| 2851 | { |
| 2852 | return !list_empty(&khugepaged_scan.mm_head) && |
| 2853 | khugepaged_enabled(); |
| 2854 | } |
| 2855 | |
| 2856 | static int khugepaged_wait_event(void) |
| 2857 | { |
| 2858 | return !list_empty(&khugepaged_scan.mm_head) || |
Xiao Guangrong | 2017c0b | 2012-10-08 16:29:44 -0700 | [diff] [blame] | 2859 | kthread_should_stop(); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2860 | } |
| 2861 | |
Xiao Guangrong | d516904 | 2012-10-08 16:29:48 -0700 | [diff] [blame] | 2862 | static void khugepaged_do_scan(void) |
| 2863 | { |
| 2864 | struct page *hpage = NULL; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2865 | unsigned int progress = 0, pass_through_head = 0; |
| 2866 | unsigned int pages = khugepaged_pages_to_scan; |
Xiao Guangrong | d516904 | 2012-10-08 16:29:48 -0700 | [diff] [blame] | 2867 | bool wait = true; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2868 | |
| 2869 | barrier(); /* write khugepaged_pages_to_scan to local stack */ |
| 2870 | |
| 2871 | while (progress < pages) { |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2872 | if (!khugepaged_prealloc_page(&hpage, &wait)) |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 2873 | break; |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2874 | |
Xiao Guangrong | 420256ef | 2012-10-08 16:29:49 -0700 | [diff] [blame] | 2875 | cond_resched(); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2876 | |
Jiri Kosina | cd09241 | 2015-06-24 16:56:07 -0700 | [diff] [blame] | 2877 | if (unlikely(kthread_should_stop() || try_to_freeze())) |
Andrea Arcangeli | 878aee7 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 2878 | break; |
| 2879 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2880 | spin_lock(&khugepaged_mm_lock); |
| 2881 | if (!khugepaged_scan.mm_slot) |
| 2882 | pass_through_head++; |
| 2883 | if (khugepaged_has_work() && |
| 2884 | pass_through_head < 2) |
| 2885 | progress += khugepaged_scan_mm_slot(pages - progress, |
Xiao Guangrong | d516904 | 2012-10-08 16:29:48 -0700 | [diff] [blame] | 2886 | &hpage); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2887 | else |
| 2888 | progress = pages; |
| 2889 | spin_unlock(&khugepaged_mm_lock); |
| 2890 | } |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2891 | |
Xiao Guangrong | d516904 | 2012-10-08 16:29:48 -0700 | [diff] [blame] | 2892 | if (!IS_ERR_OR_NULL(hpage)) |
| 2893 | put_page(hpage); |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 2894 | } |
| 2895 | |
Xiao Guangrong | 2017c0b | 2012-10-08 16:29:44 -0700 | [diff] [blame] | 2896 | static void khugepaged_wait_work(void) |
| 2897 | { |
Xiao Guangrong | 2017c0b | 2012-10-08 16:29:44 -0700 | [diff] [blame] | 2898 | if (khugepaged_has_work()) { |
| 2899 | if (!khugepaged_scan_sleep_millisecs) |
| 2900 | return; |
| 2901 | |
| 2902 | wait_event_freezable_timeout(khugepaged_wait, |
| 2903 | kthread_should_stop(), |
| 2904 | msecs_to_jiffies(khugepaged_scan_sleep_millisecs)); |
| 2905 | return; |
| 2906 | } |
| 2907 | |
| 2908 | if (khugepaged_enabled()) |
| 2909 | wait_event_freezable(khugepaged_wait, khugepaged_wait_event()); |
| 2910 | } |
| 2911 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2912 | static int khugepaged(void *none) |
| 2913 | { |
| 2914 | struct mm_slot *mm_slot; |
| 2915 | |
Andrea Arcangeli | 878aee7 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 2916 | set_freezable(); |
Dongsheng Yang | 8698a74 | 2014-03-11 18:09:12 +0800 | [diff] [blame] | 2917 | set_user_nice(current, MAX_NICE); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2918 | |
Xiao Guangrong | b723178 | 2012-10-08 16:29:54 -0700 | [diff] [blame] | 2919 | while (!kthread_should_stop()) { |
| 2920 | khugepaged_do_scan(); |
| 2921 | khugepaged_wait_work(); |
| 2922 | } |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2923 | |
| 2924 | spin_lock(&khugepaged_mm_lock); |
| 2925 | mm_slot = khugepaged_scan.mm_slot; |
| 2926 | khugepaged_scan.mm_slot = NULL; |
| 2927 | if (mm_slot) |
| 2928 | collect_mm_slot(mm_slot); |
| 2929 | spin_unlock(&khugepaged_mm_lock); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2930 | return 0; |
| 2931 | } |
| 2932 | |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2933 | static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, |
| 2934 | unsigned long haddr, pmd_t *pmd) |
| 2935 | { |
| 2936 | struct mm_struct *mm = vma->vm_mm; |
| 2937 | pgtable_t pgtable; |
| 2938 | pmd_t _pmd; |
| 2939 | int i; |
| 2940 | |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 2941 | pmdp_huge_clear_flush_notify(vma, haddr, pmd); |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2942 | /* leave pmd empty until pte is filled */ |
| 2943 | |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 2944 | pgtable = pgtable_trans_huge_withdraw(mm, pmd); |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2945 | pmd_populate(mm, &_pmd, pgtable); |
| 2946 | |
| 2947 | for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) { |
| 2948 | pte_t *pte, entry; |
| 2949 | entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot); |
| 2950 | entry = pte_mkspecial(entry); |
| 2951 | pte = pte_offset_map(&_pmd, haddr); |
| 2952 | VM_BUG_ON(!pte_none(*pte)); |
| 2953 | set_pte_at(mm, haddr, pte, entry); |
| 2954 | pte_unmap(pte); |
| 2955 | } |
| 2956 | smp_wmb(); /* make pte visible before pmd */ |
| 2957 | pmd_populate(mm, pmd, pgtable); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 2958 | put_huge_zero_page(); |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2959 | } |
| 2960 | |
Kirill A. Shutemov | e180377 | 2012-12-12 13:50:59 -0800 | [diff] [blame] | 2961 | void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address, |
| 2962 | pmd_t *pmd) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 2963 | { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2964 | spinlock_t *ptl; |
Matthew Wilcox | 4897c76 | 2015-09-08 14:58:45 -0700 | [diff] [blame] | 2965 | struct page *page = NULL; |
Kirill A. Shutemov | e180377 | 2012-12-12 13:50:59 -0800 | [diff] [blame] | 2966 | struct mm_struct *mm = vma->vm_mm; |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2967 | unsigned long haddr = address & HPAGE_PMD_MASK; |
| 2968 | unsigned long mmun_start; /* For mmu_notifiers */ |
| 2969 | unsigned long mmun_end; /* For mmu_notifiers */ |
Kirill A. Shutemov | e180377 | 2012-12-12 13:50:59 -0800 | [diff] [blame] | 2970 | |
| 2971 | BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 2972 | |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2973 | mmun_start = haddr; |
| 2974 | mmun_end = haddr + HPAGE_PMD_SIZE; |
Hugh Dickins | 750e816 | 2013-10-16 13:47:08 -0700 | [diff] [blame] | 2975 | again: |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2976 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2977 | ptl = pmd_lock(mm, pmd); |
Matthew Wilcox | 4897c76 | 2015-09-08 14:58:45 -0700 | [diff] [blame] | 2978 | if (unlikely(!pmd_trans_huge(*pmd))) |
| 2979 | goto unlock; |
| 2980 | if (vma_is_dax(vma)) { |
Kirill A. Shutemov | 5b701b8 | 2015-09-08 14:59:28 -0700 | [diff] [blame^] | 2981 | pmd_t _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd); |
| 2982 | if (is_huge_zero_pmd(_pmd)) |
| 2983 | put_huge_zero_page(); |
Matthew Wilcox | 4897c76 | 2015-09-08 14:58:45 -0700 | [diff] [blame] | 2984 | } else if (is_huge_zero_pmd(*pmd)) { |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2985 | __split_huge_zero_page_pmd(vma, haddr, pmd); |
Matthew Wilcox | 4897c76 | 2015-09-08 14:58:45 -0700 | [diff] [blame] | 2986 | } else { |
| 2987 | page = pmd_page(*pmd); |
| 2988 | VM_BUG_ON_PAGE(!page_count(page), page); |
| 2989 | get_page(page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 2990 | } |
Matthew Wilcox | 4897c76 | 2015-09-08 14:58:45 -0700 | [diff] [blame] | 2991 | unlock: |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2992 | spin_unlock(ptl); |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2993 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 2994 | |
Matthew Wilcox | 4897c76 | 2015-09-08 14:58:45 -0700 | [diff] [blame] | 2995 | if (!page) |
| 2996 | return; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 2997 | |
Matthew Wilcox | 4897c76 | 2015-09-08 14:58:45 -0700 | [diff] [blame] | 2998 | split_huge_page(page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 2999 | put_page(page); |
Hugh Dickins | 750e816 | 2013-10-16 13:47:08 -0700 | [diff] [blame] | 3000 | |
| 3001 | /* |
| 3002 | * We don't always have down_write of mmap_sem here: a racing |
| 3003 | * do_huge_pmd_wp_page() might have copied-on-write to another |
| 3004 | * huge page before our split_huge_page() got the anon_vma lock. |
| 3005 | */ |
| 3006 | if (unlikely(pmd_trans_huge(*pmd))) |
| 3007 | goto again; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 3008 | } |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 3009 | |
Kirill A. Shutemov | e180377 | 2012-12-12 13:50:59 -0800 | [diff] [blame] | 3010 | void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address, |
| 3011 | pmd_t *pmd) |
| 3012 | { |
| 3013 | struct vm_area_struct *vma; |
| 3014 | |
| 3015 | vma = find_vma(mm, address); |
| 3016 | BUG_ON(vma == NULL); |
| 3017 | split_huge_page_pmd(vma, address, pmd); |
| 3018 | } |
| 3019 | |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 3020 | static void split_huge_page_address(struct mm_struct *mm, |
| 3021 | unsigned long address) |
| 3022 | { |
Hugh Dickins | f72e7dc | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 3023 | pgd_t *pgd; |
| 3024 | pud_t *pud; |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 3025 | pmd_t *pmd; |
| 3026 | |
| 3027 | VM_BUG_ON(!(address & ~HPAGE_PMD_MASK)); |
| 3028 | |
Hugh Dickins | f72e7dc | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 3029 | pgd = pgd_offset(mm, address); |
| 3030 | if (!pgd_present(*pgd)) |
| 3031 | return; |
| 3032 | |
| 3033 | pud = pud_offset(pgd, address); |
| 3034 | if (!pud_present(*pud)) |
| 3035 | return; |
| 3036 | |
| 3037 | pmd = pmd_offset(pud, address); |
| 3038 | if (!pmd_present(*pmd)) |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 3039 | return; |
| 3040 | /* |
| 3041 | * Caller holds the mmap_sem write mode, so a huge pmd cannot |
| 3042 | * materialize from under us. |
| 3043 | */ |
Kirill A. Shutemov | e180377 | 2012-12-12 13:50:59 -0800 | [diff] [blame] | 3044 | split_huge_page_pmd_mm(mm, address, pmd); |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 3045 | } |
| 3046 | |
Kirill A. Shutemov | e1b9996 | 2015-09-08 14:58:37 -0700 | [diff] [blame] | 3047 | void vma_adjust_trans_huge(struct vm_area_struct *vma, |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 3048 | unsigned long start, |
| 3049 | unsigned long end, |
| 3050 | long adjust_next) |
| 3051 | { |
| 3052 | /* |
| 3053 | * If the new start address isn't hpage aligned and it could |
| 3054 | * previously contain an hugepage: check if we need to split |
| 3055 | * an huge pmd. |
| 3056 | */ |
| 3057 | if (start & ~HPAGE_PMD_MASK && |
| 3058 | (start & HPAGE_PMD_MASK) >= vma->vm_start && |
| 3059 | (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end) |
| 3060 | split_huge_page_address(vma->vm_mm, start); |
| 3061 | |
| 3062 | /* |
| 3063 | * If the new end address isn't hpage aligned and it could |
| 3064 | * previously contain an hugepage: check if we need to split |
| 3065 | * an huge pmd. |
| 3066 | */ |
| 3067 | if (end & ~HPAGE_PMD_MASK && |
| 3068 | (end & HPAGE_PMD_MASK) >= vma->vm_start && |
| 3069 | (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end) |
| 3070 | split_huge_page_address(vma->vm_mm, end); |
| 3071 | |
| 3072 | /* |
| 3073 | * If we're also updating the vma->vm_next->vm_start, if the new |
| 3074 | * vm_next->vm_start isn't page aligned and it could previously |
| 3075 | * contain an hugepage: check if we need to split an huge pmd. |
| 3076 | */ |
| 3077 | if (adjust_next > 0) { |
| 3078 | struct vm_area_struct *next = vma->vm_next; |
| 3079 | unsigned long nstart = next->vm_start; |
| 3080 | nstart += adjust_next << PAGE_SHIFT; |
| 3081 | if (nstart & ~HPAGE_PMD_MASK && |
| 3082 | (nstart & HPAGE_PMD_MASK) >= next->vm_start && |
| 3083 | (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end) |
| 3084 | split_huge_page_address(next->vm_mm, nstart); |
| 3085 | } |
| 3086 | } |