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