Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/mm/madvise.c |
| 4 | * |
| 5 | * Copyright (C) 1999 Linus Torvalds |
| 6 | * Copyright (C) 2002 Christoph Hellwig |
| 7 | */ |
| 8 | |
| 9 | #include <linux/mman.h> |
| 10 | #include <linux/pagemap.h> |
| 11 | #include <linux/syscalls.h> |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 12 | #include <linux/mempolicy.h> |
Andi Kleen | afcf938 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 13 | #include <linux/page-isolation.h> |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 14 | #include <linux/page_idle.h> |
Pavel Emelyanov | 05ce772 | 2017-02-22 15:42:40 -0800 | [diff] [blame] | 15 | #include <linux/userfaultfd_k.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/hugetlb.h> |
Hugh Dickins | 3f31d07 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 17 | #include <linux/falloc.h> |
Jan Kara | 692fe62 | 2019-08-29 09:04:11 -0700 | [diff] [blame] | 18 | #include <linux/fadvise.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 19 | #include <linux/sched.h> |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 20 | #include <linux/sched/mm.h> |
Arnd Bergmann | 17fca13 | 2022-01-14 14:06:07 -0800 | [diff] [blame^] | 21 | #include <linux/mm_inline.h> |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 22 | #include <linux/string.h> |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 23 | #include <linux/uio.h> |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 24 | #include <linux/ksm.h> |
Hugh Dickins | 3f31d07 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 25 | #include <linux/fs.h> |
Andy Lutomirski | 9ab4233 | 2012-07-05 16:00:11 -0700 | [diff] [blame] | 26 | #include <linux/file.h> |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 27 | #include <linux/blkdev.h> |
Tejun Heo | 66114ca | 2015-05-22 17:13:32 -0400 | [diff] [blame] | 28 | #include <linux/backing-dev.h> |
Christoph Hellwig | a520110 | 2019-08-28 16:19:53 +0200 | [diff] [blame] | 29 | #include <linux/pagewalk.h> |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 30 | #include <linux/swap.h> |
| 31 | #include <linux/swapops.h> |
Hugh Dickins | 3a4f8a0 | 2017-02-24 14:59:36 -0800 | [diff] [blame] | 32 | #include <linux/shmem_fs.h> |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 33 | #include <linux/mmu_notifier.h> |
| 34 | |
| 35 | #include <asm/tlb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Kirill A. Shutemov | 2351907 | 2017-02-22 15:46:39 -0800 | [diff] [blame] | 37 | #include "internal.h" |
| 38 | |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 39 | struct madvise_walk_private { |
| 40 | struct mmu_gather *tlb; |
| 41 | bool pageout; |
| 42 | }; |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | /* |
Nick Piggin | 0a27a14 | 2007-05-06 14:49:53 -0700 | [diff] [blame] | 45 | * Any behaviour which results in changes to the vma->vm_flags needs to |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 46 | * take mmap_lock for writing. Others, which simply traverse vmas, need |
Nick Piggin | 0a27a14 | 2007-05-06 14:49:53 -0700 | [diff] [blame] | 47 | * to only take it for reading. |
| 48 | */ |
| 49 | static int madvise_need_mmap_write(int behavior) |
| 50 | { |
| 51 | switch (behavior) { |
| 52 | case MADV_REMOVE: |
| 53 | case MADV_WILLNEED: |
| 54 | case MADV_DONTNEED: |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 55 | case MADV_COLD: |
Minchan Kim | 1a4e58c | 2019-09-25 16:49:15 -0700 | [diff] [blame] | 56 | case MADV_PAGEOUT: |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 57 | case MADV_FREE: |
David Hildenbrand | 4ca9b385 | 2021-06-30 18:52:28 -0700 | [diff] [blame] | 58 | case MADV_POPULATE_READ: |
| 59 | case MADV_POPULATE_WRITE: |
Nick Piggin | 0a27a14 | 2007-05-06 14:49:53 -0700 | [diff] [blame] | 60 | return 0; |
| 61 | default: |
| 62 | /* be safe, default to 1. list exceptions explicitly */ |
| 63 | return 1; |
| 64 | } |
| 65 | } |
| 66 | |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 67 | #ifdef CONFIG_ANON_VMA_NAME |
Suren Baghdasaryan | 78db341 | 2022-01-14 14:06:03 -0800 | [diff] [blame] | 68 | static struct anon_vma_name *anon_vma_name_alloc(const char *name) |
| 69 | { |
| 70 | struct anon_vma_name *anon_name; |
| 71 | size_t count; |
| 72 | |
| 73 | /* Add 1 for NUL terminator at the end of the anon_name->name */ |
| 74 | count = strlen(name) + 1; |
| 75 | anon_name = kmalloc(struct_size(anon_name, name, count), GFP_KERNEL); |
| 76 | if (anon_name) { |
| 77 | kref_init(&anon_name->kref); |
| 78 | memcpy(anon_name->name, name, count); |
| 79 | } |
| 80 | |
| 81 | return anon_name; |
| 82 | } |
| 83 | |
| 84 | static void vma_anon_name_free(struct kref *kref) |
| 85 | { |
| 86 | struct anon_vma_name *anon_name = |
| 87 | container_of(kref, struct anon_vma_name, kref); |
| 88 | kfree(anon_name); |
| 89 | } |
| 90 | |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 91 | static inline bool has_vma_anon_name(struct vm_area_struct *vma) |
| 92 | { |
| 93 | return !vma->vm_file && vma->anon_name; |
| 94 | } |
| 95 | |
| 96 | const char *vma_anon_name(struct vm_area_struct *vma) |
| 97 | { |
| 98 | if (!has_vma_anon_name(vma)) |
| 99 | return NULL; |
| 100 | |
| 101 | mmap_assert_locked(vma->vm_mm); |
| 102 | |
Suren Baghdasaryan | 78db341 | 2022-01-14 14:06:03 -0800 | [diff] [blame] | 103 | return vma->anon_name->name; |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void dup_vma_anon_name(struct vm_area_struct *orig_vma, |
| 107 | struct vm_area_struct *new_vma) |
| 108 | { |
| 109 | if (!has_vma_anon_name(orig_vma)) |
| 110 | return; |
| 111 | |
Suren Baghdasaryan | 78db341 | 2022-01-14 14:06:03 -0800 | [diff] [blame] | 112 | kref_get(&orig_vma->anon_name->kref); |
| 113 | new_vma->anon_name = orig_vma->anon_name; |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void free_vma_anon_name(struct vm_area_struct *vma) |
| 117 | { |
Suren Baghdasaryan | 78db341 | 2022-01-14 14:06:03 -0800 | [diff] [blame] | 118 | struct anon_vma_name *anon_name; |
| 119 | |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 120 | if (!has_vma_anon_name(vma)) |
| 121 | return; |
| 122 | |
Suren Baghdasaryan | 78db341 | 2022-01-14 14:06:03 -0800 | [diff] [blame] | 123 | anon_name = vma->anon_name; |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 124 | vma->anon_name = NULL; |
Suren Baghdasaryan | 78db341 | 2022-01-14 14:06:03 -0800 | [diff] [blame] | 125 | kref_put(&anon_name->kref, vma_anon_name_free); |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* mmap_lock should be write-locked */ |
| 129 | static int replace_vma_anon_name(struct vm_area_struct *vma, const char *name) |
| 130 | { |
Suren Baghdasaryan | 78db341 | 2022-01-14 14:06:03 -0800 | [diff] [blame] | 131 | const char *anon_name; |
| 132 | |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 133 | if (!name) { |
| 134 | free_vma_anon_name(vma); |
| 135 | return 0; |
| 136 | } |
| 137 | |
Suren Baghdasaryan | 78db341 | 2022-01-14 14:06:03 -0800 | [diff] [blame] | 138 | anon_name = vma_anon_name(vma); |
| 139 | if (anon_name) { |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 140 | /* Same name, nothing to do here */ |
Suren Baghdasaryan | 78db341 | 2022-01-14 14:06:03 -0800 | [diff] [blame] | 141 | if (!strcmp(name, anon_name)) |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 142 | return 0; |
| 143 | |
| 144 | free_vma_anon_name(vma); |
| 145 | } |
Suren Baghdasaryan | 78db341 | 2022-01-14 14:06:03 -0800 | [diff] [blame] | 146 | vma->anon_name = anon_vma_name_alloc(name); |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 147 | if (!vma->anon_name) |
| 148 | return -ENOMEM; |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | #else /* CONFIG_ANON_VMA_NAME */ |
| 153 | static int replace_vma_anon_name(struct vm_area_struct *vma, const char *name) |
| 154 | { |
| 155 | if (name) |
| 156 | return -EINVAL; |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | #endif /* CONFIG_ANON_VMA_NAME */ |
Nick Piggin | 0a27a14 | 2007-05-06 14:49:53 -0700 | [diff] [blame] | 161 | /* |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 162 | * Update the vm_flags on region of a vma, splitting it or merging it as |
| 163 | * necessary. Must be called with mmap_sem held for writing; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | */ |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 165 | static int madvise_update_vma(struct vm_area_struct *vma, |
| 166 | struct vm_area_struct **prev, unsigned long start, |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 167 | unsigned long end, unsigned long new_flags, |
| 168 | const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
Vladimir Cernov | ec9bed9 | 2013-09-11 14:20:15 -0700 | [diff] [blame] | 170 | struct mm_struct *mm = vma->vm_mm; |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 171 | int error; |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 172 | pgoff_t pgoff; |
Prasanna Meda | e798c6e | 2005-06-21 17:14:36 -0700 | [diff] [blame] | 173 | |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 174 | if (new_flags == vma->vm_flags && is_same_vma_anon_name(vma, name)) { |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 175 | *prev = vma; |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 176 | return 0; |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); |
| 180 | *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma, |
Andrea Arcangeli | 19a809a | 2015-09-04 15:46:24 -0700 | [diff] [blame] | 181 | vma->vm_file, pgoff, vma_policy(vma), |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 182 | vma->vm_userfaultfd_ctx, name); |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 183 | if (*prev) { |
| 184 | vma = *prev; |
| 185 | goto success; |
| 186 | } |
| 187 | |
| 188 | *prev = vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | if (start != vma->vm_start) { |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 191 | if (unlikely(mm->map_count >= sysctl_max_map_count)) |
| 192 | return -ENOMEM; |
David Rientjes | def5efe | 2017-02-24 14:58:47 -0800 | [diff] [blame] | 193 | error = __split_vma(mm, vma, start, 1); |
Mike Rapoport | f3bc0db | 2019-09-23 15:39:31 -0700 | [diff] [blame] | 194 | if (error) |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 195 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (end != vma->vm_end) { |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 199 | if (unlikely(mm->map_count >= sysctl_max_map_count)) |
| 200 | return -ENOMEM; |
David Rientjes | def5efe | 2017-02-24 14:58:47 -0800 | [diff] [blame] | 201 | error = __split_vma(mm, vma, end, 0); |
Mike Rapoport | f3bc0db | 2019-09-23 15:39:31 -0700 | [diff] [blame] | 202 | if (error) |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 203 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Hugh Dickins | 836d5ff | 2005-09-03 15:54:53 -0700 | [diff] [blame] | 206 | success: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 208 | * vm_flags is protected by the mmap_lock held in write mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | */ |
Prasanna Meda | e798c6e | 2005-06-21 17:14:36 -0700 | [diff] [blame] | 210 | vma->vm_flags = new_flags; |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 211 | if (!vma->vm_file) { |
| 212 | error = replace_vma_anon_name(vma, name); |
| 213 | if (error) |
| 214 | return error; |
| 215 | } |
Mike Rapoport | f3bc0db | 2019-09-23 15:39:31 -0700 | [diff] [blame] | 216 | |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 217 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 220 | #ifdef CONFIG_SWAP |
| 221 | static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start, |
| 222 | unsigned long end, struct mm_walk *walk) |
| 223 | { |
| 224 | pte_t *orig_pte; |
| 225 | struct vm_area_struct *vma = walk->private; |
| 226 | unsigned long index; |
| 227 | |
| 228 | if (pmd_none_or_trans_huge_or_clear_bad(pmd)) |
| 229 | return 0; |
| 230 | |
| 231 | for (index = start; index != end; index += PAGE_SIZE) { |
| 232 | pte_t pte; |
| 233 | swp_entry_t entry; |
| 234 | struct page *page; |
| 235 | spinlock_t *ptl; |
| 236 | |
| 237 | orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl); |
| 238 | pte = *(orig_pte + ((index - start) / PAGE_SIZE)); |
| 239 | pte_unmap_unlock(orig_pte, ptl); |
| 240 | |
Kirill A. Shutemov | 0661a33 | 2015-02-10 14:10:04 -0800 | [diff] [blame] | 241 | if (pte_present(pte) || pte_none(pte)) |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 242 | continue; |
| 243 | entry = pte_to_swp_entry(pte); |
| 244 | if (unlikely(non_swap_entry(entry))) |
| 245 | continue; |
| 246 | |
| 247 | page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE, |
Shaohua Li | 2395562 | 2017-07-10 15:47:11 -0700 | [diff] [blame] | 248 | vma, index, false); |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 249 | if (page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 250 | put_page(page); |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 256 | static const struct mm_walk_ops swapin_walk_ops = { |
| 257 | .pmd_entry = swapin_walk_pmd_entry, |
| 258 | }; |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 259 | |
| 260 | static void force_shm_swapin_readahead(struct vm_area_struct *vma, |
| 261 | unsigned long start, unsigned long end, |
| 262 | struct address_space *mapping) |
| 263 | { |
Matthew Wilcox (Oracle) | e6e8871 | 2020-10-13 16:51:24 -0700 | [diff] [blame] | 264 | XA_STATE(xas, &mapping->i_pages, linear_page_index(vma, start)); |
Matthew Wilcox (Oracle) | 6638380 | 2020-11-21 22:17:22 -0800 | [diff] [blame] | 265 | pgoff_t end_index = linear_page_index(vma, end + PAGE_SIZE - 1); |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 266 | struct page *page; |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 267 | |
Matthew Wilcox (Oracle) | e6e8871 | 2020-10-13 16:51:24 -0700 | [diff] [blame] | 268 | rcu_read_lock(); |
| 269 | xas_for_each(&xas, page, end_index) { |
| 270 | swp_entry_t swap; |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 271 | |
Matthew Wilcox (Oracle) | e6e8871 | 2020-10-13 16:51:24 -0700 | [diff] [blame] | 272 | if (!xa_is_value(page)) |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 273 | continue; |
Matthew Wilcox (Oracle) | e6e8871 | 2020-10-13 16:51:24 -0700 | [diff] [blame] | 274 | xas_pause(&xas); |
| 275 | rcu_read_unlock(); |
| 276 | |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 277 | swap = radix_to_swp_entry(page); |
| 278 | page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE, |
Shaohua Li | 2395562 | 2017-07-10 15:47:11 -0700 | [diff] [blame] | 279 | NULL, 0, false); |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 280 | if (page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 281 | put_page(page); |
Matthew Wilcox (Oracle) | e6e8871 | 2020-10-13 16:51:24 -0700 | [diff] [blame] | 282 | |
| 283 | rcu_read_lock(); |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 284 | } |
Matthew Wilcox (Oracle) | e6e8871 | 2020-10-13 16:51:24 -0700 | [diff] [blame] | 285 | rcu_read_unlock(); |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 286 | |
| 287 | lru_add_drain(); /* Push any new pages onto the LRU now */ |
| 288 | } |
| 289 | #endif /* CONFIG_SWAP */ |
| 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | /* |
| 292 | * Schedule all required I/O operations. Do not wait for completion. |
| 293 | */ |
Vladimir Cernov | ec9bed9 | 2013-09-11 14:20:15 -0700 | [diff] [blame] | 294 | static long madvise_willneed(struct vm_area_struct *vma, |
| 295 | struct vm_area_struct **prev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | unsigned long start, unsigned long end) |
| 297 | { |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 298 | struct mm_struct *mm = vma->vm_mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | struct file *file = vma->vm_file; |
Jan Kara | 692fe62 | 2019-08-29 09:04:11 -0700 | [diff] [blame] | 300 | loff_t offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
chenjie | 6ea8d95 | 2017-11-29 16:10:54 -0800 | [diff] [blame] | 302 | *prev = vma; |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 303 | #ifdef CONFIG_SWAP |
Christoph Hellwig | 97b713b | 2015-01-14 10:42:31 +0100 | [diff] [blame] | 304 | if (!file) { |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 305 | walk_page_range(vma->vm_mm, start, end, &swapin_walk_ops, vma); |
| 306 | lru_add_drain(); /* Push any new pages onto the LRU now */ |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 307 | return 0; |
| 308 | } |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 309 | |
Christoph Hellwig | 97b713b | 2015-01-14 10:42:31 +0100 | [diff] [blame] | 310 | if (shmem_mapping(file->f_mapping)) { |
Christoph Hellwig | 97b713b | 2015-01-14 10:42:31 +0100 | [diff] [blame] | 311 | force_shm_swapin_readahead(vma, start, end, |
| 312 | file->f_mapping); |
| 313 | return 0; |
| 314 | } |
| 315 | #else |
Suzuki | 1bef400 | 2005-10-11 08:29:06 -0700 | [diff] [blame] | 316 | if (!file) |
| 317 | return -EBADF; |
Christoph Hellwig | 97b713b | 2015-01-14 10:42:31 +0100 | [diff] [blame] | 318 | #endif |
Suzuki | 1bef400 | 2005-10-11 08:29:06 -0700 | [diff] [blame] | 319 | |
Matthew Wilcox | e748dcd | 2015-02-16 15:59:12 -0800 | [diff] [blame] | 320 | if (IS_DAX(file_inode(file))) { |
Carsten Otte | fe77ba6 | 2005-06-23 22:05:29 -0700 | [diff] [blame] | 321 | /* no bad return value, but ignore advice */ |
| 322 | return 0; |
| 323 | } |
| 324 | |
Jan Kara | 692fe62 | 2019-08-29 09:04:11 -0700 | [diff] [blame] | 325 | /* |
| 326 | * Filesystem's fadvise may need to take various locks. We need to |
| 327 | * explicitly grab a reference because the vma (and hence the |
| 328 | * vma's reference to the file) can go away as soon as we drop |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 329 | * mmap_lock. |
Jan Kara | 692fe62 | 2019-08-29 09:04:11 -0700 | [diff] [blame] | 330 | */ |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 331 | *prev = NULL; /* tell sys_madvise we drop mmap_lock */ |
Jan Kara | 692fe62 | 2019-08-29 09:04:11 -0700 | [diff] [blame] | 332 | get_file(file); |
Jan Kara | 692fe62 | 2019-08-29 09:04:11 -0700 | [diff] [blame] | 333 | offset = (loff_t)(start - vma->vm_start) |
| 334 | + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 335 | mmap_read_unlock(mm); |
Jan Kara | 692fe62 | 2019-08-29 09:04:11 -0700 | [diff] [blame] | 336 | vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED); |
| 337 | fput(file); |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 338 | mmap_read_lock(mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 342 | static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, |
| 343 | unsigned long addr, unsigned long end, |
| 344 | struct mm_walk *walk) |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 345 | { |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 346 | struct madvise_walk_private *private = walk->private; |
| 347 | struct mmu_gather *tlb = private->tlb; |
| 348 | bool pageout = private->pageout; |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 349 | struct mm_struct *mm = tlb->mm; |
| 350 | struct vm_area_struct *vma = walk->vma; |
| 351 | pte_t *orig_pte, *pte, ptent; |
| 352 | spinlock_t *ptl; |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 353 | struct page *page = NULL; |
| 354 | LIST_HEAD(page_list); |
| 355 | |
| 356 | if (fatal_signal_pending(current)) |
| 357 | return -EINTR; |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 358 | |
| 359 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 360 | if (pmd_trans_huge(*pmd)) { |
| 361 | pmd_t orig_pmd; |
| 362 | unsigned long next = pmd_addr_end(addr, end); |
| 363 | |
| 364 | tlb_change_page_size(tlb, HPAGE_PMD_SIZE); |
| 365 | ptl = pmd_trans_huge_lock(pmd, vma); |
| 366 | if (!ptl) |
| 367 | return 0; |
| 368 | |
| 369 | orig_pmd = *pmd; |
| 370 | if (is_huge_zero_pmd(orig_pmd)) |
| 371 | goto huge_unlock; |
| 372 | |
| 373 | if (unlikely(!pmd_present(orig_pmd))) { |
| 374 | VM_BUG_ON(thp_migration_supported() && |
| 375 | !is_pmd_migration_entry(orig_pmd)); |
| 376 | goto huge_unlock; |
| 377 | } |
| 378 | |
| 379 | page = pmd_page(orig_pmd); |
Michal Hocko | 12e967f | 2020-03-21 18:22:26 -0700 | [diff] [blame] | 380 | |
| 381 | /* Do not interfere with other mappings of this page */ |
| 382 | if (page_mapcount(page) != 1) |
| 383 | goto huge_unlock; |
| 384 | |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 385 | if (next - addr != HPAGE_PMD_SIZE) { |
| 386 | int err; |
| 387 | |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 388 | get_page(page); |
| 389 | spin_unlock(ptl); |
| 390 | lock_page(page); |
| 391 | err = split_huge_page(page); |
| 392 | unlock_page(page); |
| 393 | put_page(page); |
| 394 | if (!err) |
| 395 | goto regular_page; |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | if (pmd_young(orig_pmd)) { |
| 400 | pmdp_invalidate(vma, addr, pmd); |
| 401 | orig_pmd = pmd_mkold(orig_pmd); |
| 402 | |
| 403 | set_pmd_at(mm, addr, pmd, orig_pmd); |
| 404 | tlb_remove_pmd_tlb_entry(tlb, pmd, addr); |
| 405 | } |
| 406 | |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 407 | ClearPageReferenced(page); |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 408 | test_and_clear_page_young(page); |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 409 | if (pageout) { |
zhong jiang | 8207296 | 2019-11-15 17:34:36 -0800 | [diff] [blame] | 410 | if (!isolate_lru_page(page)) { |
| 411 | if (PageUnevictable(page)) |
| 412 | putback_lru_page(page); |
| 413 | else |
| 414 | list_add(&page->lru, &page_list); |
| 415 | } |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 416 | } else |
| 417 | deactivate_page(page); |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 418 | huge_unlock: |
| 419 | spin_unlock(ptl); |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 420 | if (pageout) |
| 421 | reclaim_pages(&page_list); |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
Minchan Kim | ce26842 | 2020-09-14 23:32:15 -0700 | [diff] [blame] | 425 | regular_page: |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 426 | if (pmd_trans_unstable(pmd)) |
| 427 | return 0; |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 428 | #endif |
| 429 | tlb_change_page_size(tlb, PAGE_SIZE); |
| 430 | orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 431 | flush_tlb_batched_pending(mm); |
| 432 | arch_enter_lazy_mmu_mode(); |
| 433 | for (; addr < end; pte++, addr += PAGE_SIZE) { |
| 434 | ptent = *pte; |
| 435 | |
| 436 | if (pte_none(ptent)) |
| 437 | continue; |
| 438 | |
| 439 | if (!pte_present(ptent)) |
| 440 | continue; |
| 441 | |
| 442 | page = vm_normal_page(vma, addr, ptent); |
| 443 | if (!page) |
| 444 | continue; |
| 445 | |
| 446 | /* |
| 447 | * Creating a THP page is expensive so split it only if we |
| 448 | * are sure it's worth. Split it if we are only owner. |
| 449 | */ |
| 450 | if (PageTransCompound(page)) { |
| 451 | if (page_mapcount(page) != 1) |
| 452 | break; |
| 453 | get_page(page); |
| 454 | if (!trylock_page(page)) { |
| 455 | put_page(page); |
| 456 | break; |
| 457 | } |
| 458 | pte_unmap_unlock(orig_pte, ptl); |
| 459 | if (split_huge_page(page)) { |
| 460 | unlock_page(page); |
| 461 | put_page(page); |
| 462 | pte_offset_map_lock(mm, pmd, addr, &ptl); |
| 463 | break; |
| 464 | } |
| 465 | unlock_page(page); |
| 466 | put_page(page); |
| 467 | pte = pte_offset_map_lock(mm, pmd, addr, &ptl); |
| 468 | pte--; |
| 469 | addr -= PAGE_SIZE; |
| 470 | continue; |
| 471 | } |
| 472 | |
Michal Hocko | 12e967f | 2020-03-21 18:22:26 -0700 | [diff] [blame] | 473 | /* Do not interfere with other mappings of this page */ |
| 474 | if (page_mapcount(page) != 1) |
| 475 | continue; |
| 476 | |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 477 | VM_BUG_ON_PAGE(PageTransCompound(page), page); |
| 478 | |
| 479 | if (pte_young(ptent)) { |
| 480 | ptent = ptep_get_and_clear_full(mm, addr, pte, |
| 481 | tlb->fullmm); |
| 482 | ptent = pte_mkold(ptent); |
| 483 | set_pte_at(mm, addr, pte, ptent); |
| 484 | tlb_remove_tlb_entry(tlb, pte, addr); |
| 485 | } |
| 486 | |
| 487 | /* |
| 488 | * We are deactivating a page for accelerating reclaiming. |
| 489 | * VM couldn't reclaim the page unless we clear PG_young. |
| 490 | * As a side effect, it makes confuse idle-page tracking |
| 491 | * because they will miss recent referenced history. |
| 492 | */ |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 493 | ClearPageReferenced(page); |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 494 | test_and_clear_page_young(page); |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 495 | if (pageout) { |
zhong jiang | 8207296 | 2019-11-15 17:34:36 -0800 | [diff] [blame] | 496 | if (!isolate_lru_page(page)) { |
| 497 | if (PageUnevictable(page)) |
| 498 | putback_lru_page(page); |
| 499 | else |
| 500 | list_add(&page->lru, &page_list); |
| 501 | } |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 502 | } else |
| 503 | deactivate_page(page); |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | arch_leave_lazy_mmu_mode(); |
| 507 | pte_unmap_unlock(orig_pte, ptl); |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 508 | if (pageout) |
| 509 | reclaim_pages(&page_list); |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 510 | cond_resched(); |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | static const struct mm_walk_ops cold_walk_ops = { |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 516 | .pmd_entry = madvise_cold_or_pageout_pte_range, |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 517 | }; |
| 518 | |
| 519 | static void madvise_cold_page_range(struct mmu_gather *tlb, |
| 520 | struct vm_area_struct *vma, |
| 521 | unsigned long addr, unsigned long end) |
| 522 | { |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 523 | struct madvise_walk_private walk_private = { |
| 524 | .pageout = false, |
| 525 | .tlb = tlb, |
| 526 | }; |
| 527 | |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 528 | tlb_start_vma(tlb, vma); |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 529 | walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private); |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 530 | tlb_end_vma(tlb, vma); |
| 531 | } |
| 532 | |
| 533 | static long madvise_cold(struct vm_area_struct *vma, |
| 534 | struct vm_area_struct **prev, |
| 535 | unsigned long start_addr, unsigned long end_addr) |
| 536 | { |
| 537 | struct mm_struct *mm = vma->vm_mm; |
| 538 | struct mmu_gather tlb; |
| 539 | |
| 540 | *prev = vma; |
| 541 | if (!can_madv_lru_vma(vma)) |
| 542 | return -EINVAL; |
| 543 | |
| 544 | lru_add_drain(); |
Will Deacon | a72afd8 | 2021-01-27 23:53:45 +0000 | [diff] [blame] | 545 | tlb_gather_mmu(&tlb, mm); |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 546 | madvise_cold_page_range(&tlb, vma, start_addr, end_addr); |
Will Deacon | ae8eba8 | 2021-01-27 23:53:43 +0000 | [diff] [blame] | 547 | tlb_finish_mmu(&tlb); |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
Minchan Kim | 1a4e58c | 2019-09-25 16:49:15 -0700 | [diff] [blame] | 552 | static void madvise_pageout_page_range(struct mmu_gather *tlb, |
| 553 | struct vm_area_struct *vma, |
| 554 | unsigned long addr, unsigned long end) |
| 555 | { |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 556 | struct madvise_walk_private walk_private = { |
| 557 | .pageout = true, |
| 558 | .tlb = tlb, |
| 559 | }; |
| 560 | |
Minchan Kim | 1a4e58c | 2019-09-25 16:49:15 -0700 | [diff] [blame] | 561 | tlb_start_vma(tlb, vma); |
Minchan Kim | d616d51 | 2019-09-25 16:49:19 -0700 | [diff] [blame] | 562 | walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private); |
Minchan Kim | 1a4e58c | 2019-09-25 16:49:15 -0700 | [diff] [blame] | 563 | tlb_end_vma(tlb, vma); |
| 564 | } |
| 565 | |
| 566 | static inline bool can_do_pageout(struct vm_area_struct *vma) |
| 567 | { |
| 568 | if (vma_is_anonymous(vma)) |
| 569 | return true; |
| 570 | if (!vma->vm_file) |
| 571 | return false; |
| 572 | /* |
| 573 | * paging out pagecache only for non-anonymous mappings that correspond |
| 574 | * to the files the calling process could (if tried) open for writing; |
| 575 | * otherwise we'd be including shared non-exclusive mappings, which |
| 576 | * opens a side channel. |
| 577 | */ |
Christian Brauner | 21cb47b | 2021-01-21 14:19:25 +0100 | [diff] [blame] | 578 | return inode_owner_or_capable(&init_user_ns, |
| 579 | file_inode(vma->vm_file)) || |
Christian Brauner | 02f92b3 | 2021-01-21 14:19:22 +0100 | [diff] [blame] | 580 | file_permission(vma->vm_file, MAY_WRITE) == 0; |
Minchan Kim | 1a4e58c | 2019-09-25 16:49:15 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | static long madvise_pageout(struct vm_area_struct *vma, |
| 584 | struct vm_area_struct **prev, |
| 585 | unsigned long start_addr, unsigned long end_addr) |
| 586 | { |
| 587 | struct mm_struct *mm = vma->vm_mm; |
| 588 | struct mmu_gather tlb; |
| 589 | |
| 590 | *prev = vma; |
| 591 | if (!can_madv_lru_vma(vma)) |
| 592 | return -EINVAL; |
| 593 | |
| 594 | if (!can_do_pageout(vma)) |
| 595 | return 0; |
| 596 | |
| 597 | lru_add_drain(); |
Will Deacon | a72afd8 | 2021-01-27 23:53:45 +0000 | [diff] [blame] | 598 | tlb_gather_mmu(&tlb, mm); |
Minchan Kim | 1a4e58c | 2019-09-25 16:49:15 -0700 | [diff] [blame] | 599 | madvise_pageout_page_range(&tlb, vma, start_addr, end_addr); |
Will Deacon | ae8eba8 | 2021-01-27 23:53:43 +0000 | [diff] [blame] | 600 | tlb_finish_mmu(&tlb); |
Minchan Kim | 1a4e58c | 2019-09-25 16:49:15 -0700 | [diff] [blame] | 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 605 | static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, |
| 606 | unsigned long end, struct mm_walk *walk) |
| 607 | |
| 608 | { |
| 609 | struct mmu_gather *tlb = walk->private; |
| 610 | struct mm_struct *mm = tlb->mm; |
| 611 | struct vm_area_struct *vma = walk->vma; |
| 612 | spinlock_t *ptl; |
| 613 | pte_t *orig_pte, *pte, ptent; |
| 614 | struct page *page; |
Minchan Kim | 64b42bc | 2016-01-15 16:55:06 -0800 | [diff] [blame] | 615 | int nr_swap = 0; |
Minchan Kim | b8d3c4c | 2016-01-15 16:55:42 -0800 | [diff] [blame] | 616 | unsigned long next; |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 617 | |
Minchan Kim | b8d3c4c | 2016-01-15 16:55:42 -0800 | [diff] [blame] | 618 | next = pmd_addr_end(addr, end); |
| 619 | if (pmd_trans_huge(*pmd)) |
| 620 | if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next)) |
| 621 | goto next; |
| 622 | |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 623 | if (pmd_trans_unstable(pmd)) |
| 624 | return 0; |
| 625 | |
Peter Zijlstra | ed6a793 | 2018-08-31 14:46:08 +0200 | [diff] [blame] | 626 | tlb_change_page_size(tlb, PAGE_SIZE); |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 627 | orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl); |
Mel Gorman | 3ea2771 | 2017-08-02 13:31:52 -0700 | [diff] [blame] | 628 | flush_tlb_batched_pending(mm); |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 629 | arch_enter_lazy_mmu_mode(); |
| 630 | for (; addr != end; pte++, addr += PAGE_SIZE) { |
| 631 | ptent = *pte; |
| 632 | |
Minchan Kim | 64b42bc | 2016-01-15 16:55:06 -0800 | [diff] [blame] | 633 | if (pte_none(ptent)) |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 634 | continue; |
Minchan Kim | 64b42bc | 2016-01-15 16:55:06 -0800 | [diff] [blame] | 635 | /* |
| 636 | * If the pte has swp_entry, just clear page table to |
| 637 | * prevent swap-in which is more expensive rather than |
| 638 | * (page allocation + zeroing). |
| 639 | */ |
| 640 | if (!pte_present(ptent)) { |
| 641 | swp_entry_t entry; |
| 642 | |
| 643 | entry = pte_to_swp_entry(ptent); |
| 644 | if (non_swap_entry(entry)) |
| 645 | continue; |
| 646 | nr_swap--; |
| 647 | free_swap_and_cache(entry); |
| 648 | pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); |
| 649 | continue; |
| 650 | } |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 651 | |
Christoph Hellwig | 25b2995 | 2019-06-13 22:50:49 +0200 | [diff] [blame] | 652 | page = vm_normal_page(vma, addr, ptent); |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 653 | if (!page) |
| 654 | continue; |
| 655 | |
| 656 | /* |
| 657 | * If pmd isn't transhuge but the page is THP and |
| 658 | * is owned by only this process, split it and |
| 659 | * deactivate all pages. |
| 660 | */ |
| 661 | if (PageTransCompound(page)) { |
| 662 | if (page_mapcount(page) != 1) |
| 663 | goto out; |
| 664 | get_page(page); |
| 665 | if (!trylock_page(page)) { |
| 666 | put_page(page); |
| 667 | goto out; |
| 668 | } |
| 669 | pte_unmap_unlock(orig_pte, ptl); |
| 670 | if (split_huge_page(page)) { |
| 671 | unlock_page(page); |
| 672 | put_page(page); |
| 673 | pte_offset_map_lock(mm, pmd, addr, &ptl); |
| 674 | goto out; |
| 675 | } |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 676 | unlock_page(page); |
Eric Biggers | 263630e | 2017-08-25 15:55:39 -0700 | [diff] [blame] | 677 | put_page(page); |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 678 | pte = pte_offset_map_lock(mm, pmd, addr, &ptl); |
| 679 | pte--; |
| 680 | addr -= PAGE_SIZE; |
| 681 | continue; |
| 682 | } |
| 683 | |
| 684 | VM_BUG_ON_PAGE(PageTransCompound(page), page); |
| 685 | |
| 686 | if (PageSwapCache(page) || PageDirty(page)) { |
| 687 | if (!trylock_page(page)) |
| 688 | continue; |
| 689 | /* |
| 690 | * If page is shared with others, we couldn't clear |
| 691 | * PG_dirty of the page. |
| 692 | */ |
| 693 | if (page_mapcount(page) != 1) { |
| 694 | unlock_page(page); |
| 695 | continue; |
| 696 | } |
| 697 | |
| 698 | if (PageSwapCache(page) && !try_to_free_swap(page)) { |
| 699 | unlock_page(page); |
| 700 | continue; |
| 701 | } |
| 702 | |
| 703 | ClearPageDirty(page); |
| 704 | unlock_page(page); |
| 705 | } |
| 706 | |
| 707 | if (pte_young(ptent) || pte_dirty(ptent)) { |
| 708 | /* |
| 709 | * Some of architecture(ex, PPC) don't update TLB |
| 710 | * with set_pte_at and tlb_remove_tlb_entry so for |
| 711 | * the portability, remap the pte with old|clean |
| 712 | * after pte clearing. |
| 713 | */ |
| 714 | ptent = ptep_get_and_clear_full(mm, addr, pte, |
| 715 | tlb->fullmm); |
| 716 | |
| 717 | ptent = pte_mkold(ptent); |
| 718 | ptent = pte_mkclean(ptent); |
| 719 | set_pte_at(mm, addr, pte, ptent); |
| 720 | tlb_remove_tlb_entry(tlb, pte, addr); |
| 721 | } |
Shaohua Li | 802a3a9 | 2017-05-03 14:52:32 -0700 | [diff] [blame] | 722 | mark_page_lazyfree(page); |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 723 | } |
| 724 | out: |
Minchan Kim | 64b42bc | 2016-01-15 16:55:06 -0800 | [diff] [blame] | 725 | if (nr_swap) { |
| 726 | if (current->mm == mm) |
| 727 | sync_mm_rss(mm); |
| 728 | |
| 729 | add_mm_counter(mm, MM_SWAPENTS, nr_swap); |
| 730 | } |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 731 | arch_leave_lazy_mmu_mode(); |
| 732 | pte_unmap_unlock(orig_pte, ptl); |
| 733 | cond_resched(); |
Minchan Kim | b8d3c4c | 2016-01-15 16:55:42 -0800 | [diff] [blame] | 734 | next: |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 738 | static const struct mm_walk_ops madvise_free_walk_ops = { |
| 739 | .pmd_entry = madvise_free_pte_range, |
| 740 | }; |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 741 | |
| 742 | static int madvise_free_single_vma(struct vm_area_struct *vma, |
| 743 | unsigned long start_addr, unsigned long end_addr) |
| 744 | { |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 745 | struct mm_struct *mm = vma->vm_mm; |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 746 | struct mmu_notifier_range range; |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 747 | struct mmu_gather tlb; |
| 748 | |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 749 | /* MADV_FREE works for only anon vma at the moment */ |
| 750 | if (!vma_is_anonymous(vma)) |
| 751 | return -EINVAL; |
| 752 | |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 753 | range.start = max(vma->vm_start, start_addr); |
| 754 | if (range.start >= vma->vm_end) |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 755 | return -EINVAL; |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 756 | range.end = min(vma->vm_end, end_addr); |
| 757 | if (range.end <= vma->vm_start) |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 758 | return -EINVAL; |
Jérôme Glisse | 7269f99 | 2019-05-13 17:20:53 -0700 | [diff] [blame] | 759 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, |
Jérôme Glisse | 6f4f13e | 2019-05-13 17:20:49 -0700 | [diff] [blame] | 760 | range.start, range.end); |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 761 | |
| 762 | lru_add_drain(); |
Will Deacon | a72afd8 | 2021-01-27 23:53:45 +0000 | [diff] [blame] | 763 | tlb_gather_mmu(&tlb, mm); |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 764 | update_hiwater_rss(mm); |
| 765 | |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 766 | mmu_notifier_invalidate_range_start(&range); |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 767 | tlb_start_vma(&tlb, vma); |
| 768 | walk_page_range(vma->vm_mm, range.start, range.end, |
| 769 | &madvise_free_walk_ops, &tlb); |
| 770 | tlb_end_vma(&tlb, vma); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 771 | mmu_notifier_invalidate_range_end(&range); |
Will Deacon | ae8eba8 | 2021-01-27 23:53:43 +0000 | [diff] [blame] | 772 | tlb_finish_mmu(&tlb); |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 773 | |
| 774 | return 0; |
| 775 | } |
| 776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | /* |
| 778 | * Application no longer needs these pages. If the pages are dirty, |
| 779 | * it's OK to just throw them away. The app will be more careful about |
| 780 | * data it wants to keep. Be sure to free swap resources too. The |
Fernando Luis Vazquez Cao | 7e6cbea | 2008-07-29 22:33:39 -0700 | [diff] [blame] | 781 | * zap_page_range call sets things up for shrink_active_list to actually free |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | * these pages later if no one else has touched them in the meantime, |
| 783 | * although we could add these pages to a global reuse list for |
Fernando Luis Vazquez Cao | 7e6cbea | 2008-07-29 22:33:39 -0700 | [diff] [blame] | 784 | * shrink_active_list to pick up before reclaiming other pages. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | * |
| 786 | * NB: This interface discards data rather than pushes it out to swap, |
| 787 | * as some implementations do. This has performance implications for |
| 788 | * applications like large transactional databases which want to discard |
| 789 | * pages in anonymous maps after committing to backing store the data |
| 790 | * that was kept in them. There is no reason to write this data out to |
| 791 | * the swap area if the application is discarding it. |
| 792 | * |
| 793 | * An interface that causes the system to free clean pages and flush |
| 794 | * dirty pages is already available as msync(MS_INVALIDATE). |
| 795 | */ |
Mike Rapoport | 230ca98 | 2017-07-10 15:49:02 -0700 | [diff] [blame] | 796 | static long madvise_dontneed_single_vma(struct vm_area_struct *vma, |
| 797 | unsigned long start, unsigned long end) |
| 798 | { |
| 799 | zap_page_range(vma, start, end - start); |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | static long madvise_dontneed_free(struct vm_area_struct *vma, |
| 804 | struct vm_area_struct **prev, |
| 805 | unsigned long start, unsigned long end, |
| 806 | int behavior) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | { |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 808 | struct mm_struct *mm = vma->vm_mm; |
| 809 | |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 810 | *prev = vma; |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 811 | if (!can_madv_lru_vma(vma)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | return -EINVAL; |
| 813 | |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 814 | if (!userfaultfd_remove(vma, start, end)) { |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 815 | *prev = NULL; /* mmap_lock has been dropped, prev is stale */ |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 816 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 817 | mmap_read_lock(mm); |
| 818 | vma = find_vma(mm, start); |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 819 | if (!vma) |
| 820 | return -ENOMEM; |
| 821 | if (start < vma->vm_start) { |
| 822 | /* |
| 823 | * This "vma" under revalidation is the one |
| 824 | * with the lowest vma->vm_start where start |
| 825 | * is also < vma->vm_end. If start < |
| 826 | * vma->vm_start it means an hole materialized |
| 827 | * in the user address space within the |
Mike Rapoport | 230ca98 | 2017-07-10 15:49:02 -0700 | [diff] [blame] | 828 | * virtual range passed to MADV_DONTNEED |
| 829 | * or MADV_FREE. |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 830 | */ |
| 831 | return -ENOMEM; |
| 832 | } |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 833 | if (!can_madv_lru_vma(vma)) |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 834 | return -EINVAL; |
| 835 | if (end > vma->vm_end) { |
| 836 | /* |
| 837 | * Don't fail if end > vma->vm_end. If the old |
Ingo Molnar | f0953a1 | 2021-05-06 18:06:47 -0700 | [diff] [blame] | 838 | * vma was split while the mmap_lock was |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 839 | * released the effect of the concurrent |
Mike Rapoport | 230ca98 | 2017-07-10 15:49:02 -0700 | [diff] [blame] | 840 | * operation may not cause madvise() to |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 841 | * have an undefined result. There may be an |
| 842 | * adjacent next vma that we'll walk |
| 843 | * next. userfaultfd_remove() will generate an |
| 844 | * UFFD_EVENT_REMOVE repetition on the |
| 845 | * end-vma->vm_end range, but the manager can |
| 846 | * handle a repetition fine. |
| 847 | */ |
| 848 | end = vma->vm_end; |
| 849 | } |
| 850 | VM_WARN_ON(start >= end); |
| 851 | } |
Mike Rapoport | 230ca98 | 2017-07-10 15:49:02 -0700 | [diff] [blame] | 852 | |
| 853 | if (behavior == MADV_DONTNEED) |
| 854 | return madvise_dontneed_single_vma(vma, start, end); |
| 855 | else if (behavior == MADV_FREE) |
| 856 | return madvise_free_single_vma(vma, start, end); |
| 857 | else |
| 858 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
| 860 | |
David Hildenbrand | 4ca9b385 | 2021-06-30 18:52:28 -0700 | [diff] [blame] | 861 | static long madvise_populate(struct vm_area_struct *vma, |
| 862 | struct vm_area_struct **prev, |
| 863 | unsigned long start, unsigned long end, |
| 864 | int behavior) |
| 865 | { |
| 866 | const bool write = behavior == MADV_POPULATE_WRITE; |
| 867 | struct mm_struct *mm = vma->vm_mm; |
| 868 | unsigned long tmp_end; |
| 869 | int locked = 1; |
| 870 | long pages; |
| 871 | |
| 872 | *prev = vma; |
| 873 | |
| 874 | while (start < end) { |
| 875 | /* |
| 876 | * We might have temporarily dropped the lock. For example, |
| 877 | * our VMA might have been split. |
| 878 | */ |
| 879 | if (!vma || start >= vma->vm_end) { |
| 880 | vma = find_vma(mm, start); |
| 881 | if (!vma || start < vma->vm_start) |
| 882 | return -ENOMEM; |
| 883 | } |
| 884 | |
| 885 | tmp_end = min_t(unsigned long, end, vma->vm_end); |
| 886 | /* Populate (prefault) page tables readable/writable. */ |
| 887 | pages = faultin_vma_page_range(vma, start, tmp_end, write, |
| 888 | &locked); |
| 889 | if (!locked) { |
| 890 | mmap_read_lock(mm); |
| 891 | locked = 1; |
| 892 | *prev = NULL; |
| 893 | vma = NULL; |
| 894 | } |
| 895 | if (pages < 0) { |
| 896 | switch (pages) { |
| 897 | case -EINTR: |
| 898 | return -EINTR; |
David Hildenbrand | eb2faa5 | 2021-08-13 16:54:37 -0700 | [diff] [blame] | 899 | case -EINVAL: /* Incompatible mappings / permissions. */ |
David Hildenbrand | 4ca9b385 | 2021-06-30 18:52:28 -0700 | [diff] [blame] | 900 | return -EINVAL; |
| 901 | case -EHWPOISON: |
| 902 | return -EHWPOISON; |
David Hildenbrand | eb2faa5 | 2021-08-13 16:54:37 -0700 | [diff] [blame] | 903 | case -EFAULT: /* VM_FAULT_SIGBUS or VM_FAULT_SIGSEGV */ |
| 904 | return -EFAULT; |
David Hildenbrand | 4ca9b385 | 2021-06-30 18:52:28 -0700 | [diff] [blame] | 905 | default: |
| 906 | pr_warn_once("%s: unhandled return value: %ld\n", |
| 907 | __func__, pages); |
| 908 | fallthrough; |
| 909 | case -ENOMEM: |
| 910 | return -ENOMEM; |
| 911 | } |
| 912 | } |
| 913 | start += pages * PAGE_SIZE; |
| 914 | } |
| 915 | return 0; |
| 916 | } |
| 917 | |
Badari Pulavarty | f6b3ec2 | 2006-01-06 00:10:38 -0800 | [diff] [blame] | 918 | /* |
| 919 | * Application wants to free up the pages and associated backing store. |
| 920 | * This is effectively punching a hole into the middle of a file. |
Badari Pulavarty | f6b3ec2 | 2006-01-06 00:10:38 -0800 | [diff] [blame] | 921 | */ |
| 922 | static long madvise_remove(struct vm_area_struct *vma, |
Nick Piggin | 00e9fa2 | 2007-03-16 13:38:10 -0800 | [diff] [blame] | 923 | struct vm_area_struct **prev, |
Badari Pulavarty | f6b3ec2 | 2006-01-06 00:10:38 -0800 | [diff] [blame] | 924 | unsigned long start, unsigned long end) |
| 925 | { |
Hugh Dickins | 3f31d07 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 926 | loff_t offset; |
Hugh Dickins | 90ed52e | 2007-03-29 01:20:38 -0700 | [diff] [blame] | 927 | int error; |
Andy Lutomirski | 9ab4233 | 2012-07-05 16:00:11 -0700 | [diff] [blame] | 928 | struct file *f; |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 929 | struct mm_struct *mm = vma->vm_mm; |
Badari Pulavarty | f6b3ec2 | 2006-01-06 00:10:38 -0800 | [diff] [blame] | 930 | |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 931 | *prev = NULL; /* tell sys_madvise we drop mmap_lock */ |
Nick Piggin | 00e9fa2 | 2007-03-16 13:38:10 -0800 | [diff] [blame] | 932 | |
Mike Kravetz | 72079ba | 2015-09-08 15:01:57 -0700 | [diff] [blame] | 933 | if (vma->vm_flags & VM_LOCKED) |
Badari Pulavarty | f6b3ec2 | 2006-01-06 00:10:38 -0800 | [diff] [blame] | 934 | return -EINVAL; |
| 935 | |
Andy Lutomirski | 9ab4233 | 2012-07-05 16:00:11 -0700 | [diff] [blame] | 936 | f = vma->vm_file; |
| 937 | |
| 938 | if (!f || !f->f_mapping || !f->f_mapping->host) { |
Badari Pulavarty | f6b3ec2 | 2006-01-06 00:10:38 -0800 | [diff] [blame] | 939 | return -EINVAL; |
| 940 | } |
| 941 | |
Hugh Dickins | 69cf0fa | 2006-04-17 22:46:32 +0100 | [diff] [blame] | 942 | if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE)) |
| 943 | return -EACCES; |
| 944 | |
Badari Pulavarty | f6b3ec2 | 2006-01-06 00:10:38 -0800 | [diff] [blame] | 945 | offset = (loff_t)(start - vma->vm_start) |
| 946 | + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); |
Hugh Dickins | 90ed52e | 2007-03-29 01:20:38 -0700 | [diff] [blame] | 947 | |
Andy Lutomirski | 9ab4233 | 2012-07-05 16:00:11 -0700 | [diff] [blame] | 948 | /* |
Jan Kara | 9608703 | 2021-04-12 15:50:21 +0200 | [diff] [blame] | 949 | * Filesystem's fallocate may need to take i_rwsem. We need to |
Andy Lutomirski | 9ab4233 | 2012-07-05 16:00:11 -0700 | [diff] [blame] | 950 | * explicitly grab a reference because the vma (and hence the |
| 951 | * vma's reference to the file) can go away as soon as we drop |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 952 | * mmap_lock. |
Andy Lutomirski | 9ab4233 | 2012-07-05 16:00:11 -0700 | [diff] [blame] | 953 | */ |
| 954 | get_file(f); |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 955 | if (userfaultfd_remove(vma, start, end)) { |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 956 | /* mmap_lock was not released by userfaultfd_remove() */ |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 957 | mmap_read_unlock(mm); |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 958 | } |
Anna Schumaker | 72c72bd | 2014-11-07 14:44:25 -0500 | [diff] [blame] | 959 | error = vfs_fallocate(f, |
Hugh Dickins | 3f31d07 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 960 | FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, |
| 961 | offset, end - start); |
Andy Lutomirski | 9ab4233 | 2012-07-05 16:00:11 -0700 | [diff] [blame] | 962 | fput(f); |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 963 | mmap_read_lock(mm); |
Hugh Dickins | 90ed52e | 2007-03-29 01:20:38 -0700 | [diff] [blame] | 964 | return error; |
Badari Pulavarty | f6b3ec2 | 2006-01-06 00:10:38 -0800 | [diff] [blame] | 965 | } |
| 966 | |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 967 | /* |
| 968 | * Apply an madvise behavior to a region of a vma. madvise_update_vma |
| 969 | * will handle splitting a vm area into separate areas, each area with its own |
| 970 | * behavior. |
| 971 | */ |
| 972 | static int madvise_vma_behavior(struct vm_area_struct *vma, |
| 973 | struct vm_area_struct **prev, |
| 974 | unsigned long start, unsigned long end, |
| 975 | unsigned long behavior) |
| 976 | { |
| 977 | int error; |
| 978 | unsigned long new_flags = vma->vm_flags; |
| 979 | |
| 980 | switch (behavior) { |
| 981 | case MADV_REMOVE: |
| 982 | return madvise_remove(vma, prev, start, end); |
| 983 | case MADV_WILLNEED: |
| 984 | return madvise_willneed(vma, prev, start, end); |
| 985 | case MADV_COLD: |
| 986 | return madvise_cold(vma, prev, start, end); |
| 987 | case MADV_PAGEOUT: |
| 988 | return madvise_pageout(vma, prev, start, end); |
| 989 | case MADV_FREE: |
| 990 | case MADV_DONTNEED: |
| 991 | return madvise_dontneed_free(vma, prev, start, end, behavior); |
| 992 | case MADV_POPULATE_READ: |
| 993 | case MADV_POPULATE_WRITE: |
| 994 | return madvise_populate(vma, prev, start, end, behavior); |
| 995 | case MADV_NORMAL: |
| 996 | new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ; |
| 997 | break; |
| 998 | case MADV_SEQUENTIAL: |
| 999 | new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ; |
| 1000 | break; |
| 1001 | case MADV_RANDOM: |
| 1002 | new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ; |
| 1003 | break; |
| 1004 | case MADV_DONTFORK: |
| 1005 | new_flags |= VM_DONTCOPY; |
| 1006 | break; |
| 1007 | case MADV_DOFORK: |
| 1008 | if (vma->vm_flags & VM_IO) |
| 1009 | return -EINVAL; |
| 1010 | new_flags &= ~VM_DONTCOPY; |
| 1011 | break; |
| 1012 | case MADV_WIPEONFORK: |
| 1013 | /* MADV_WIPEONFORK is only supported on anonymous memory. */ |
| 1014 | if (vma->vm_file || vma->vm_flags & VM_SHARED) |
| 1015 | return -EINVAL; |
| 1016 | new_flags |= VM_WIPEONFORK; |
| 1017 | break; |
| 1018 | case MADV_KEEPONFORK: |
| 1019 | new_flags &= ~VM_WIPEONFORK; |
| 1020 | break; |
| 1021 | case MADV_DONTDUMP: |
| 1022 | new_flags |= VM_DONTDUMP; |
| 1023 | break; |
| 1024 | case MADV_DODUMP: |
| 1025 | if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) |
| 1026 | return -EINVAL; |
| 1027 | new_flags &= ~VM_DONTDUMP; |
| 1028 | break; |
| 1029 | case MADV_MERGEABLE: |
| 1030 | case MADV_UNMERGEABLE: |
| 1031 | error = ksm_madvise(vma, start, end, behavior, &new_flags); |
| 1032 | if (error) |
| 1033 | goto out; |
| 1034 | break; |
| 1035 | case MADV_HUGEPAGE: |
| 1036 | case MADV_NOHUGEPAGE: |
| 1037 | error = hugepage_madvise(vma, &new_flags, behavior); |
| 1038 | if (error) |
| 1039 | goto out; |
| 1040 | break; |
| 1041 | } |
| 1042 | |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 1043 | error = madvise_update_vma(vma, prev, start, end, new_flags, |
| 1044 | vma_anon_name(vma)); |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 1045 | |
| 1046 | out: |
| 1047 | /* |
| 1048 | * madvise() returns EAGAIN if kernel resources, such as |
| 1049 | * slab, are temporarily unavailable. |
| 1050 | */ |
| 1051 | if (error == -ENOMEM) |
| 1052 | error = -EAGAIN; |
| 1053 | return error; |
| 1054 | } |
| 1055 | |
Andi Kleen | 9893e49 | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 1056 | #ifdef CONFIG_MEMORY_FAILURE |
| 1057 | /* |
| 1058 | * Error injection support for memory error handling. |
| 1059 | */ |
Anshuman Khandual | 97167a7 | 2017-05-03 14:55:25 -0700 | [diff] [blame] | 1060 | static int madvise_inject_error(int behavior, |
| 1061 | unsigned long start, unsigned long end) |
Andi Kleen | 9893e49 | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 1062 | { |
Yunfeng Ye | d3cd257 | 2019-11-30 17:57:42 -0800 | [diff] [blame] | 1063 | unsigned long size; |
Anshuman Khandual | 97167a7 | 2017-05-03 14:55:25 -0700 | [diff] [blame] | 1064 | |
Andi Kleen | 9893e49 | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 1065 | if (!capable(CAP_SYS_ADMIN)) |
| 1066 | return -EPERM; |
Anshuman Khandual | 97167a7 | 2017-05-03 14:55:25 -0700 | [diff] [blame] | 1067 | |
Alexandru Moise | 19bfbe2 | 2017-10-03 16:14:31 -0700 | [diff] [blame] | 1068 | |
Yunfeng Ye | d3cd257 | 2019-11-30 17:57:42 -0800 | [diff] [blame] | 1069 | for (; start < end; start += size) { |
Dan Williams | 23e7b5c | 2018-07-13 21:50:06 -0700 | [diff] [blame] | 1070 | unsigned long pfn; |
Oscar Salvador | dc7560b | 2020-10-15 20:06:53 -0700 | [diff] [blame] | 1071 | struct page *page; |
Andrew Morton | 325c4ef | 2013-09-11 14:23:03 -0700 | [diff] [blame] | 1072 | int ret; |
| 1073 | |
Anshuman Khandual | 97167a7 | 2017-05-03 14:55:25 -0700 | [diff] [blame] | 1074 | ret = get_user_pages_fast(start, 1, 0, &page); |
Andi Kleen | 9893e49 | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 1075 | if (ret != 1) |
| 1076 | return ret; |
Dan Williams | 23e7b5c | 2018-07-13 21:50:06 -0700 | [diff] [blame] | 1077 | pfn = page_to_pfn(page); |
Andrew Morton | 325c4ef | 2013-09-11 14:23:03 -0700 | [diff] [blame] | 1078 | |
Alexandru Moise | 19bfbe2 | 2017-10-03 16:14:31 -0700 | [diff] [blame] | 1079 | /* |
| 1080 | * When soft offlining hugepages, after migrating the page |
| 1081 | * we dissolve it, therefore in the second loop "page" will |
Yunfeng Ye | d3cd257 | 2019-11-30 17:57:42 -0800 | [diff] [blame] | 1082 | * no longer be a compound page. |
Alexandru Moise | 19bfbe2 | 2017-10-03 16:14:31 -0700 | [diff] [blame] | 1083 | */ |
Yunfeng Ye | d3cd257 | 2019-11-30 17:57:42 -0800 | [diff] [blame] | 1084 | size = page_size(compound_head(page)); |
Alexandru Moise | 19bfbe2 | 2017-10-03 16:14:31 -0700 | [diff] [blame] | 1085 | |
Anshuman Khandual | 97167a7 | 2017-05-03 14:55:25 -0700 | [diff] [blame] | 1086 | if (behavior == MADV_SOFT_OFFLINE) { |
| 1087 | pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n", |
Oscar Salvador | dc7560b | 2020-10-15 20:06:53 -0700 | [diff] [blame] | 1088 | pfn, start); |
Naoya Horiguchi | feec24a | 2019-11-30 17:53:38 -0800 | [diff] [blame] | 1089 | ret = soft_offline_page(pfn, MF_COUNT_INCREASED); |
Oscar Salvador | dc7560b | 2020-10-15 20:06:53 -0700 | [diff] [blame] | 1090 | } else { |
| 1091 | pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n", |
| 1092 | pfn, start); |
Oscar Salvador | 1e8aaed | 2020-12-14 19:11:48 -0800 | [diff] [blame] | 1093 | ret = memory_failure(pfn, MF_COUNT_INCREASED); |
Andi Kleen | afcf938 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 1094 | } |
Anshuman Khandual | 97167a7 | 2017-05-03 14:55:25 -0700 | [diff] [blame] | 1095 | |
Naoya Horiguchi | 23a003b | 2016-03-15 14:56:36 -0700 | [diff] [blame] | 1096 | if (ret) |
| 1097 | return ret; |
Andi Kleen | 9893e49 | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 1098 | } |
Mel Gorman | c461ad6 | 2017-08-31 16:15:30 -0700 | [diff] [blame] | 1099 | |
Andrew Morton | 325c4ef | 2013-09-11 14:23:03 -0700 | [diff] [blame] | 1100 | return 0; |
Andi Kleen | 9893e49 | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 1101 | } |
| 1102 | #endif |
| 1103 | |
Nicholas Krause | 1ecef9e | 2015-09-04 15:48:24 -0700 | [diff] [blame] | 1104 | static bool |
Nick Piggin | 75927af | 2009-06-16 15:32:38 -0700 | [diff] [blame] | 1105 | madvise_behavior_valid(int behavior) |
| 1106 | { |
| 1107 | switch (behavior) { |
| 1108 | case MADV_DOFORK: |
| 1109 | case MADV_DONTFORK: |
| 1110 | case MADV_NORMAL: |
| 1111 | case MADV_SEQUENTIAL: |
| 1112 | case MADV_RANDOM: |
| 1113 | case MADV_REMOVE: |
| 1114 | case MADV_WILLNEED: |
| 1115 | case MADV_DONTNEED: |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 1116 | case MADV_FREE: |
Minchan Kim | 9c276cc | 2019-09-25 16:49:08 -0700 | [diff] [blame] | 1117 | case MADV_COLD: |
Minchan Kim | 1a4e58c | 2019-09-25 16:49:15 -0700 | [diff] [blame] | 1118 | case MADV_PAGEOUT: |
David Hildenbrand | 4ca9b385 | 2021-06-30 18:52:28 -0700 | [diff] [blame] | 1119 | case MADV_POPULATE_READ: |
| 1120 | case MADV_POPULATE_WRITE: |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 1121 | #ifdef CONFIG_KSM |
| 1122 | case MADV_MERGEABLE: |
| 1123 | case MADV_UNMERGEABLE: |
| 1124 | #endif |
Andrea Arcangeli | 0af4e98 | 2011-01-13 15:46:55 -0800 | [diff] [blame] | 1125 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 1126 | case MADV_HUGEPAGE: |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 1127 | case MADV_NOHUGEPAGE: |
Andrea Arcangeli | 0af4e98 | 2011-01-13 15:46:55 -0800 | [diff] [blame] | 1128 | #endif |
Jason Baron | accb61f | 2012-03-23 15:02:51 -0700 | [diff] [blame] | 1129 | case MADV_DONTDUMP: |
| 1130 | case MADV_DODUMP: |
Rik van Riel | d2cd9ed | 2017-09-06 16:25:15 -0700 | [diff] [blame] | 1131 | case MADV_WIPEONFORK: |
| 1132 | case MADV_KEEPONFORK: |
Anshuman Khandual | 5e451be | 2017-05-03 14:55:28 -0700 | [diff] [blame] | 1133 | #ifdef CONFIG_MEMORY_FAILURE |
| 1134 | case MADV_SOFT_OFFLINE: |
| 1135 | case MADV_HWPOISON: |
| 1136 | #endif |
Nicholas Krause | 1ecef9e | 2015-09-04 15:48:24 -0700 | [diff] [blame] | 1137 | return true; |
Nick Piggin | 75927af | 2009-06-16 15:32:38 -0700 | [diff] [blame] | 1138 | |
| 1139 | default: |
Nicholas Krause | 1ecef9e | 2015-09-04 15:48:24 -0700 | [diff] [blame] | 1140 | return false; |
Nick Piggin | 75927af | 2009-06-16 15:32:38 -0700 | [diff] [blame] | 1141 | } |
| 1142 | } |
Hugh Dickins | 3866ea9 | 2009-09-21 17:01:52 -0700 | [diff] [blame] | 1143 | |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 1144 | static bool |
| 1145 | process_madvise_behavior_valid(int behavior) |
| 1146 | { |
| 1147 | switch (behavior) { |
| 1148 | case MADV_COLD: |
| 1149 | case MADV_PAGEOUT: |
zhangkui | d5fffc5 | 2021-09-02 15:01:11 -0700 | [diff] [blame] | 1150 | case MADV_WILLNEED: |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 1151 | return true; |
| 1152 | default: |
| 1153 | return false; |
| 1154 | } |
| 1155 | } |
| 1156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | /* |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 1158 | * Walk the vmas in range [start,end), and call the visit function on each one. |
| 1159 | * The visit function will get start and end parameters that cover the overlap |
| 1160 | * between the current vma and the original range. Any unmapped regions in the |
| 1161 | * original range will result in this function returning -ENOMEM while still |
| 1162 | * calling the visit function on all of the existing vmas in the range. |
| 1163 | * Must be called with the mmap_lock held for reading or writing. |
| 1164 | */ |
| 1165 | static |
| 1166 | int madvise_walk_vmas(struct mm_struct *mm, unsigned long start, |
| 1167 | unsigned long end, unsigned long arg, |
| 1168 | int (*visit)(struct vm_area_struct *vma, |
| 1169 | struct vm_area_struct **prev, unsigned long start, |
| 1170 | unsigned long end, unsigned long arg)) |
| 1171 | { |
| 1172 | struct vm_area_struct *vma; |
| 1173 | struct vm_area_struct *prev; |
| 1174 | unsigned long tmp; |
| 1175 | int unmapped_error = 0; |
| 1176 | |
| 1177 | /* |
| 1178 | * If the interval [start,end) covers some unmapped address |
| 1179 | * ranges, just ignore them, but return -ENOMEM at the end. |
| 1180 | * - different from the way of handling in mlock etc. |
| 1181 | */ |
| 1182 | vma = find_vma_prev(mm, start, &prev); |
| 1183 | if (vma && start > vma->vm_start) |
| 1184 | prev = vma; |
| 1185 | |
| 1186 | for (;;) { |
| 1187 | int error; |
| 1188 | |
| 1189 | /* Still start < end. */ |
| 1190 | if (!vma) |
| 1191 | return -ENOMEM; |
| 1192 | |
| 1193 | /* Here start < (end|vma->vm_end). */ |
| 1194 | if (start < vma->vm_start) { |
| 1195 | unmapped_error = -ENOMEM; |
| 1196 | start = vma->vm_start; |
| 1197 | if (start >= end) |
| 1198 | break; |
| 1199 | } |
| 1200 | |
| 1201 | /* Here vma->vm_start <= start < (end|vma->vm_end) */ |
| 1202 | tmp = vma->vm_end; |
| 1203 | if (end < tmp) |
| 1204 | tmp = end; |
| 1205 | |
| 1206 | /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */ |
| 1207 | error = visit(vma, &prev, start, tmp, arg); |
| 1208 | if (error) |
| 1209 | return error; |
| 1210 | start = tmp; |
| 1211 | if (prev && start < prev->vm_end) |
| 1212 | start = prev->vm_end; |
| 1213 | if (start >= end) |
| 1214 | break; |
| 1215 | if (prev) |
| 1216 | vma = prev->vm_next; |
| 1217 | else /* madvise_remove dropped mmap_lock */ |
| 1218 | vma = find_vma(mm, start); |
| 1219 | } |
| 1220 | |
| 1221 | return unmapped_error; |
| 1222 | } |
| 1223 | |
Colin Cross | 9a10064 | 2022-01-14 14:05:59 -0800 | [diff] [blame] | 1224 | #ifdef CONFIG_ANON_VMA_NAME |
| 1225 | static int madvise_vma_anon_name(struct vm_area_struct *vma, |
| 1226 | struct vm_area_struct **prev, |
| 1227 | unsigned long start, unsigned long end, |
| 1228 | unsigned long name) |
| 1229 | { |
| 1230 | int error; |
| 1231 | |
| 1232 | /* Only anonymous mappings can be named */ |
| 1233 | if (vma->vm_file) |
| 1234 | return -EBADF; |
| 1235 | |
| 1236 | error = madvise_update_vma(vma, prev, start, end, vma->vm_flags, |
| 1237 | (const char *)name); |
| 1238 | |
| 1239 | /* |
| 1240 | * madvise() returns EAGAIN if kernel resources, such as |
| 1241 | * slab, are temporarily unavailable. |
| 1242 | */ |
| 1243 | if (error == -ENOMEM) |
| 1244 | error = -EAGAIN; |
| 1245 | return error; |
| 1246 | } |
| 1247 | |
| 1248 | int madvise_set_anon_name(struct mm_struct *mm, unsigned long start, |
| 1249 | unsigned long len_in, const char *name) |
| 1250 | { |
| 1251 | unsigned long end; |
| 1252 | unsigned long len; |
| 1253 | |
| 1254 | if (start & ~PAGE_MASK) |
| 1255 | return -EINVAL; |
| 1256 | len = (len_in + ~PAGE_MASK) & PAGE_MASK; |
| 1257 | |
| 1258 | /* Check to see whether len was rounded up from small -ve to zero */ |
| 1259 | if (len_in && !len) |
| 1260 | return -EINVAL; |
| 1261 | |
| 1262 | end = start + len; |
| 1263 | if (end < start) |
| 1264 | return -EINVAL; |
| 1265 | |
| 1266 | if (end == start) |
| 1267 | return 0; |
| 1268 | |
| 1269 | return madvise_walk_vmas(mm, start, end, (unsigned long)name, |
| 1270 | madvise_vma_anon_name); |
| 1271 | } |
| 1272 | #endif /* CONFIG_ANON_VMA_NAME */ |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 1273 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | * The madvise(2) system call. |
| 1275 | * |
| 1276 | * Applications can use madvise() to advise the kernel how it should |
| 1277 | * handle paging I/O in this VM area. The idea is to help the kernel |
| 1278 | * use appropriate read-ahead and caching techniques. The information |
| 1279 | * provided is advisory only, and can be safely disregarded by the |
| 1280 | * kernel without affecting the correct operation of the application. |
| 1281 | * |
| 1282 | * behavior values: |
| 1283 | * MADV_NORMAL - the default behavior is to read clusters. This |
| 1284 | * results in some read-ahead and read-behind. |
| 1285 | * MADV_RANDOM - the system should read the minimum amount of data |
| 1286 | * on any access, since it is unlikely that the appli- |
| 1287 | * cation will need more than what it asks for. |
| 1288 | * MADV_SEQUENTIAL - pages in the given range will probably be accessed |
| 1289 | * once, so they can be aggressively read ahead, and |
| 1290 | * can be freed soon after they are accessed. |
| 1291 | * MADV_WILLNEED - the application is notifying the system to read |
| 1292 | * some pages ahead. |
| 1293 | * MADV_DONTNEED - the application is finished with the given range, |
| 1294 | * so the kernel can free resources associated with it. |
Naoya Horiguchi | d7206a7 | 2016-03-15 14:56:58 -0700 | [diff] [blame] | 1295 | * MADV_FREE - the application marks pages in the given range as lazy free, |
| 1296 | * where actual purges are postponed until memory pressure happens. |
Badari Pulavarty | f6b3ec2 | 2006-01-06 00:10:38 -0800 | [diff] [blame] | 1297 | * MADV_REMOVE - the application wants to free up the given range of |
| 1298 | * pages and associated backing store. |
Hugh Dickins | 3866ea9 | 2009-09-21 17:01:52 -0700 | [diff] [blame] | 1299 | * MADV_DONTFORK - omit this area from child's address space when forking: |
| 1300 | * typically, to avoid COWing pages pinned by get_user_pages(). |
| 1301 | * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking. |
Yang Shi | c02c300 | 2017-10-13 15:57:37 -0700 | [diff] [blame] | 1302 | * MADV_WIPEONFORK - present the child process with zero-filled memory in this |
| 1303 | * range after a fork. |
| 1304 | * MADV_KEEPONFORK - undo the effect of MADV_WIPEONFORK |
Naoya Horiguchi | d7206a7 | 2016-03-15 14:56:58 -0700 | [diff] [blame] | 1305 | * MADV_HWPOISON - trigger memory error handler as if the given memory range |
| 1306 | * were corrupted by unrecoverable hardware memory failure. |
| 1307 | * MADV_SOFT_OFFLINE - try to soft-offline the given range of memory. |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 1308 | * MADV_MERGEABLE - the application recommends that KSM try to merge pages in |
| 1309 | * this area with pages of identical content from other such areas. |
| 1310 | * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others. |
Naoya Horiguchi | d7206a7 | 2016-03-15 14:56:58 -0700 | [diff] [blame] | 1311 | * MADV_HUGEPAGE - the application wants to back the given range by transparent |
| 1312 | * huge pages in the future. Existing pages might be coalesced and |
| 1313 | * new pages might be allocated as THP. |
| 1314 | * MADV_NOHUGEPAGE - mark the given range as not worth being backed by |
| 1315 | * transparent huge pages so the existing pages will not be |
| 1316 | * coalesced into THP and new pages will not be allocated as THP. |
| 1317 | * MADV_DONTDUMP - the application wants to prevent pages in the given range |
| 1318 | * from being included in its core dump. |
| 1319 | * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump. |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 1320 | * MADV_COLD - the application is not expected to use this memory soon, |
| 1321 | * deactivate pages in this range so that they can be reclaimed |
Ingo Molnar | f0953a1 | 2021-05-06 18:06:47 -0700 | [diff] [blame] | 1322 | * easily if memory pressure happens. |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 1323 | * MADV_PAGEOUT - the application is not expected to use this memory soon, |
| 1324 | * page out the pages in this range immediately. |
David Hildenbrand | 4ca9b385 | 2021-06-30 18:52:28 -0700 | [diff] [blame] | 1325 | * MADV_POPULATE_READ - populate (prefault) page tables readable by |
| 1326 | * triggering read faults if required |
| 1327 | * MADV_POPULATE_WRITE - populate (prefault) page tables writable by |
| 1328 | * triggering write faults if required |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | * |
| 1330 | * return values: |
| 1331 | * zero - success |
| 1332 | * -EINVAL - start + len < 0, start is not page-aligned, |
| 1333 | * "behavior" is not a valid value, or application |
Yang Shi | c02c300 | 2017-10-13 15:57:37 -0700 | [diff] [blame] | 1334 | * is attempting to release locked or shared pages, |
| 1335 | * or the specified address range includes file, Huge TLB, |
| 1336 | * MAP_SHARED or VMPFNMAP range. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | * -ENOMEM - addresses in the specified range are not currently |
| 1338 | * mapped, or are outside the AS of the process. |
| 1339 | * -EIO - an I/O error occurred while paging in data. |
| 1340 | * -EBADF - map exists, but area maps something that isn't a file. |
| 1341 | * -EAGAIN - a kernel resource was temporarily unavailable. |
| 1342 | */ |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 1343 | int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | { |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 1345 | unsigned long end; |
| 1346 | int error; |
Jason Baron | f797779 | 2007-07-15 23:38:21 -0700 | [diff] [blame] | 1347 | int write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | size_t len; |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 1349 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
Andrey Konovalov | 057d3389 | 2019-09-25 16:48:30 -0700 | [diff] [blame] | 1351 | start = untagged_addr(start); |
| 1352 | |
Nick Piggin | 75927af | 2009-06-16 15:32:38 -0700 | [diff] [blame] | 1353 | if (!madvise_behavior_valid(behavior)) |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 1354 | return -EINVAL; |
Nick Piggin | 75927af | 2009-06-16 15:32:38 -0700 | [diff] [blame] | 1355 | |
Wei Yang | df6c650 | 2019-11-30 17:57:46 -0800 | [diff] [blame] | 1356 | if (!PAGE_ALIGNED(start)) |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 1357 | return -EINVAL; |
Wei Yang | df6c650 | 2019-11-30 17:57:46 -0800 | [diff] [blame] | 1358 | len = PAGE_ALIGN(len_in); |
Rasmus Villemoes | 84d96d8 | 2013-04-29 15:08:23 -0700 | [diff] [blame] | 1359 | |
| 1360 | /* Check to see whether len was rounded up from small -ve to zero */ |
| 1361 | if (len_in && !len) |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 1362 | return -EINVAL; |
Rasmus Villemoes | 84d96d8 | 2013-04-29 15:08:23 -0700 | [diff] [blame] | 1363 | |
| 1364 | end = start + len; |
| 1365 | if (end < start) |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 1366 | return -EINVAL; |
Rasmus Villemoes | 84d96d8 | 2013-04-29 15:08:23 -0700 | [diff] [blame] | 1367 | |
Rasmus Villemoes | 84d96d8 | 2013-04-29 15:08:23 -0700 | [diff] [blame] | 1368 | if (end == start) |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 1369 | return 0; |
Rasmus Villemoes | 84d96d8 | 2013-04-29 15:08:23 -0700 | [diff] [blame] | 1370 | |
Anshuman Khandual | 5e451be | 2017-05-03 14:55:28 -0700 | [diff] [blame] | 1371 | #ifdef CONFIG_MEMORY_FAILURE |
| 1372 | if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE) |
| 1373 | return madvise_inject_error(behavior, start, start + len_in); |
| 1374 | #endif |
| 1375 | |
Jason Baron | f797779 | 2007-07-15 23:38:21 -0700 | [diff] [blame] | 1376 | write = madvise_need_mmap_write(behavior); |
Michal Hocko | dc0ef0d | 2016-05-23 16:25:27 -0700 | [diff] [blame] | 1377 | if (write) { |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 1378 | if (mmap_write_lock_killable(mm)) |
Michal Hocko | dc0ef0d | 2016-05-23 16:25:27 -0700 | [diff] [blame] | 1379 | return -EINTR; |
| 1380 | } else { |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 1381 | mmap_read_lock(mm); |
Michal Hocko | dc0ef0d | 2016-05-23 16:25:27 -0700 | [diff] [blame] | 1382 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | |
Shaohua Li | 1998cc0 | 2013-02-22 16:32:31 -0800 | [diff] [blame] | 1384 | blk_start_plug(&plug); |
Colin Cross | ac1e9ac | 2022-01-14 14:05:55 -0800 | [diff] [blame] | 1385 | error = madvise_walk_vmas(mm, start, end, behavior, |
| 1386 | madvise_vma_behavior); |
Rasmus Villemoes | 84d96d8 | 2013-04-29 15:08:23 -0700 | [diff] [blame] | 1387 | blk_finish_plug(&plug); |
Jason Baron | f797779 | 2007-07-15 23:38:21 -0700 | [diff] [blame] | 1388 | if (write) |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 1389 | mmap_write_unlock(mm); |
Nick Piggin | 0a27a14 | 2007-05-06 14:49:53 -0700 | [diff] [blame] | 1390 | else |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 1391 | mmap_read_unlock(mm); |
Nick Piggin | 0a27a14 | 2007-05-06 14:49:53 -0700 | [diff] [blame] | 1392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | return error; |
| 1394 | } |
Jens Axboe | db08ca2 | 2019-12-25 22:14:54 -0700 | [diff] [blame] | 1395 | |
| 1396 | SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior) |
| 1397 | { |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 1398 | return do_madvise(current->mm, start, len_in, behavior); |
Jens Axboe | db08ca2 | 2019-12-25 22:14:54 -0700 | [diff] [blame] | 1399 | } |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 1400 | |
| 1401 | SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec, |
| 1402 | size_t, vlen, int, behavior, unsigned int, flags) |
| 1403 | { |
| 1404 | ssize_t ret; |
| 1405 | struct iovec iovstack[UIO_FASTIOV], iovec; |
| 1406 | struct iovec *iov = iovstack; |
| 1407 | struct iov_iter iter; |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 1408 | struct task_struct *task; |
| 1409 | struct mm_struct *mm; |
| 1410 | size_t total_len; |
| 1411 | unsigned int f_flags; |
| 1412 | |
| 1413 | if (flags != 0) { |
| 1414 | ret = -EINVAL; |
| 1415 | goto out; |
| 1416 | } |
| 1417 | |
| 1418 | ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter); |
| 1419 | if (ret < 0) |
| 1420 | goto out; |
| 1421 | |
Christian Brauner | ee9955d | 2021-10-11 15:32:45 +0200 | [diff] [blame] | 1422 | task = pidfd_get_task(pidfd, &f_flags); |
| 1423 | if (IS_ERR(task)) { |
| 1424 | ret = PTR_ERR(task); |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 1425 | goto free_iov; |
| 1426 | } |
| 1427 | |
Minchan Kim | a68a026 | 2020-12-08 20:57:18 -0800 | [diff] [blame] | 1428 | if (!process_madvise_behavior_valid(behavior)) { |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 1429 | ret = -EINVAL; |
| 1430 | goto release_task; |
| 1431 | } |
| 1432 | |
Suren Baghdasaryan | 96cfe2c | 2021-03-12 21:08:06 -0800 | [diff] [blame] | 1433 | /* Require PTRACE_MODE_READ to avoid leaking ASLR metadata. */ |
| 1434 | mm = mm_access(task, PTRACE_MODE_READ_FSCREDS); |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 1435 | if (IS_ERR_OR_NULL(mm)) { |
| 1436 | ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH; |
| 1437 | goto release_task; |
| 1438 | } |
| 1439 | |
Suren Baghdasaryan | 96cfe2c | 2021-03-12 21:08:06 -0800 | [diff] [blame] | 1440 | /* |
| 1441 | * Require CAP_SYS_NICE for influencing process performance. Note that |
| 1442 | * only non-destructive hints are currently supported. |
| 1443 | */ |
| 1444 | if (!capable(CAP_SYS_NICE)) { |
| 1445 | ret = -EPERM; |
| 1446 | goto release_mm; |
| 1447 | } |
| 1448 | |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 1449 | total_len = iov_iter_count(&iter); |
| 1450 | |
| 1451 | while (iov_iter_count(&iter)) { |
| 1452 | iovec = iov_iter_iovec(&iter); |
| 1453 | ret = do_madvise(mm, (unsigned long)iovec.iov_base, |
| 1454 | iovec.iov_len, behavior); |
| 1455 | if (ret < 0) |
| 1456 | break; |
| 1457 | iov_iter_advance(&iter, iovec.iov_len); |
| 1458 | } |
| 1459 | |
| 1460 | if (ret == 0) |
| 1461 | ret = total_len - iov_iter_count(&iter); |
| 1462 | |
Suren Baghdasaryan | 96cfe2c | 2021-03-12 21:08:06 -0800 | [diff] [blame] | 1463 | release_mm: |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 1464 | mmput(mm); |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 1465 | release_task: |
| 1466 | put_task_struct(task); |
Minchan Kim | ecb8ac8 | 2020-10-17 16:14:59 -0700 | [diff] [blame] | 1467 | free_iov: |
| 1468 | kfree(iov); |
| 1469 | out: |
| 1470 | return ret; |
| 1471 | } |