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