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