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