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