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