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