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