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