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