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