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