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