mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-27 19:29:37 +00:00
![David Hildenbrand](/assets/img/avatar_default.png)
I. Background: Sparse Memory Mappings When we manage sparse memory mappings dynamically in user space - also sometimes involving MAP_NORESERVE - we want to dynamically populate/ discard memory inside such a sparse memory region. Example users are hypervisors (especially implementing memory ballooning or similar technologies like virtio-mem) and memory allocators. In addition, we want to fail in a nice way (instead of generating SIGBUS) if populating does not succeed because we are out of backend memory (which can happen easily with file-based mappings, especially tmpfs and hugetlbfs). While MADV_DONTNEED, MADV_REMOVE and FALLOC_FL_PUNCH_HOLE allow for reliably discarding memory for most mapping types, there is no generic approach to populate page tables and preallocate memory. Although mmap() supports MAP_POPULATE, it is not applicable to the concept of sparse memory mappings, where we want to populate/discard dynamically and avoid expensive/problematic remappings. In addition, we never actually report errors during the final populate phase - it is best-effort only. fallocate() can be used to preallocate file-based memory and fail in a safe way. However, it cannot really be used for any private mappings on anonymous files via memfd due to COW semantics. In addition, fallocate() does not actually populate page tables, so we still always get pagefaults on first access - which is sometimes undesired (i.e., real-time workloads) and requires real prefaulting of page tables, not just a preallocation of backend storage. There might be interesting use cases for sparse memory regions along with mlockall(MCL_ONFAULT) which fallocate() cannot satisfy as it does not prefault page tables. II. On preallcoation/prefaulting from user space Because we don't have a proper interface, what applications (like QEMU and databases) end up doing is touching (i.e., reading+writing one byte to not overwrite existing data) all individual pages. However, that approach 1) Can result in wear on storage backing, because we end up reading/writing each page; this is especially a problem for dax/pmem. 2) Can result in mmap_sem contention when prefaulting via multiple threads. 3) Requires expensive signal handling, especially to catch SIGBUS in case of hugetlbfs/shmem/file-backed memory. For example, this is problematic in hypervisors like QEMU where SIGBUS handlers might already be used by other subsystems concurrently to e.g, handle hardware errors. "Simply" doing preallocation concurrently from other thread is not that easy. III. On MADV_WILLNEED Extending MADV_WILLNEED is not an option because 1. It would change the semantics: "Expect access in the near future." and "might be a good idea to read some pages" vs. "Definitely populate/ preallocate all memory and definitely fail on errors.". 2. Existing users (like virtio-balloon in QEMU when deflating the balloon) don't want populate/prealloc semantics. They treat this rather as a hint to give a little performance boost without too much overhead - and don't expect that a lot of memory might get consumed or a lot of time might be spent. IV. MADV_POPULATE_READ and MADV_POPULATE_WRITE Let's introduce MADV_POPULATE_READ and MADV_POPULATE_WRITE, inspired by MAP_POPULATE, with the following semantics: 1. MADV_POPULATE_READ can be used to prefault page tables just like manually reading each individual page. This will not break any COW mappings. The shared zero page might get mapped and no backend storage might get preallocated -- allocation might be deferred to write-fault time. Especially shared file mappings require an explicit fallocate() upfront to actually preallocate backend memory (blocks in the file system) in case the file might have holes. 2. If MADV_POPULATE_READ succeeds, all page tables have been populated (prefaulted) readable once. 3. MADV_POPULATE_WRITE can be used to preallocate backend memory and prefault page tables just like manually writing (or reading+writing) each individual page. This will break any COW mappings -- e.g., the shared zeropage is never populated. 4. If MADV_POPULATE_WRITE succeeds, all page tables have been populated (prefaulted) writable once. 5. MADV_POPULATE_READ and MADV_POPULATE_WRITE cannot be applied to special mappings marked with VM_PFNMAP and VM_IO. Also, proper access permissions (e.g., PROT_READ, PROT_WRITE) are required. If any such mapping is encountered, madvise() fails with -EINVAL. 6. If MADV_POPULATE_READ or MADV_POPULATE_WRITE fails, some page tables might have been populated. 7. MADV_POPULATE_READ and MADV_POPULATE_WRITE will return -EHWPOISON when encountering a HW poisoned page in the range. 8. Similar to MAP_POPULATE, MADV_POPULATE_READ and MADV_POPULATE_WRITE cannot protect from the OOM (Out Of Memory) handler killing the process. While the use case for MADV_POPULATE_WRITE is fairly obvious (i.e., preallocate memory and prefault page tables for VMs), one issue is that whenever we prefault pages writable, the pages have to be marked dirty, because the CPU could dirty them any time. while not a real problem for hugetlbfs or dax/pmem, it can be a problem for shared file mappings: each page will be marked dirty and has to be written back later when evicting. MADV_POPULATE_READ allows for optimizing this scenario: Pre-read a whole mapping from backend storage without marking it dirty, such that eviction won't have to write it back. As discussed above, shared file mappings might require an explciit fallocate() upfront to achieve preallcoation+prepopulation. Although sparse memory mappings are the primary use case, this will also be useful for other preallocate/prefault use cases where MAP_POPULATE is not desired or the semantics of MAP_POPULATE are not sufficient: as one example, QEMU users can trigger preallocation/prefaulting of guest RAM after the mapping was created -- and don't want errors to be silently suppressed. Looking at the history, MADV_POPULATE was already proposed in 2013 [1], however, the main motivation back than was performance improvements -- which should also still be the case. V. Single-threaded performance comparison I did a short experiment, prefaulting page tables on completely *empty mappings/files* and repeated the experiment 10 times. The results correspond to the shortest execution time. In general, the performance benefit for huge pages is negligible with small mappings. V.1: Private mappings POPULATE_READ and POPULATE_WRITE is fastest. Note that Reading/POPULATE_READ will populate the shared zeropage where applicable -- which result in short population times. The fastest way to allocate backend storage (here: swap or huge pages) and prefault page tables is POPULATE_WRITE. V.2: Shared mappings fallocate() is fastest, however, doesn't prefault page tables. POPULATE_WRITE is faster than simple writes and read/writes. POPULATE_READ is faster than simple reads. Without a fd, the fastest way to allocate backend storage and prefault page tables is POPULATE_WRITE. With an fd, the fastest way is usually FALLOCATE+POPULATE_READ or FALLOCATE+POPULATE_WRITE respectively; one exception are actual files: FALLOCATE+Read is slightly faster than FALLOCATE+POPULATE_READ. The fastest way to allocate backend storage prefault page tables is FALLOCATE+POPULATE_WRITE -- except when dealing with actual files; then, FALLOCATE+POPULATE_READ is fastest and won't directly mark all pages as dirty. v.3: Detailed results ================================================== 2 MiB MAP_PRIVATE: ************************************************** Anon 4 KiB : Read : 0.119 ms Anon 4 KiB : Write : 0.222 ms Anon 4 KiB : Read/Write : 0.380 ms Anon 4 KiB : POPULATE_READ : 0.060 ms Anon 4 KiB : POPULATE_WRITE : 0.158 ms Memfd 4 KiB : Read : 0.034 ms Memfd 4 KiB : Write : 0.310 ms Memfd 4 KiB : Read/Write : 0.362 ms Memfd 4 KiB : POPULATE_READ : 0.039 ms Memfd 4 KiB : POPULATE_WRITE : 0.229 ms Memfd 2 MiB : Read : 0.030 ms Memfd 2 MiB : Write : 0.030 ms Memfd 2 MiB : Read/Write : 0.030 ms Memfd 2 MiB : POPULATE_READ : 0.030 ms Memfd 2 MiB : POPULATE_WRITE : 0.030 ms tmpfs : Read : 0.033 ms tmpfs : Write : 0.313 ms tmpfs : Read/Write : 0.406 ms tmpfs : POPULATE_READ : 0.039 ms tmpfs : POPULATE_WRITE : 0.285 ms file : Read : 0.033 ms file : Write : 0.351 ms file : Read/Write : 0.408 ms file : POPULATE_READ : 0.039 ms file : POPULATE_WRITE : 0.290 ms hugetlbfs : Read : 0.030 ms hugetlbfs : Write : 0.030 ms hugetlbfs : Read/Write : 0.030 ms hugetlbfs : POPULATE_READ : 0.030 ms hugetlbfs : POPULATE_WRITE : 0.030 ms ************************************************** 4096 MiB MAP_PRIVATE: ************************************************** Anon 4 KiB : Read : 237.940 ms Anon 4 KiB : Write : 708.409 ms Anon 4 KiB : Read/Write : 1054.041 ms Anon 4 KiB : POPULATE_READ : 124.310 ms Anon 4 KiB : POPULATE_WRITE : 572.582 ms Memfd 4 KiB : Read : 136.928 ms Memfd 4 KiB : Write : 963.898 ms Memfd 4 KiB : Read/Write : 1106.561 ms Memfd 4 KiB : POPULATE_READ : 78.450 ms Memfd 4 KiB : POPULATE_WRITE : 805.881 ms Memfd 2 MiB : Read : 357.116 ms Memfd 2 MiB : Write : 357.210 ms Memfd 2 MiB : Read/Write : 357.606 ms Memfd 2 MiB : POPULATE_READ : 356.094 ms Memfd 2 MiB : POPULATE_WRITE : 356.937 ms tmpfs : Read : 137.536 ms tmpfs : Write : 954.362 ms tmpfs : Read/Write : 1105.954 ms tmpfs : POPULATE_READ : 80.289 ms tmpfs : POPULATE_WRITE : 822.826 ms file : Read : 137.874 ms file : Write : 987.025 ms file : Read/Write : 1107.439 ms file : POPULATE_READ : 80.413 ms file : POPULATE_WRITE : 857.622 ms hugetlbfs : Read : 355.607 ms hugetlbfs : Write : 355.729 ms hugetlbfs : Read/Write : 356.127 ms hugetlbfs : POPULATE_READ : 354.585 ms hugetlbfs : POPULATE_WRITE : 355.138 ms ************************************************** 2 MiB MAP_SHARED: ************************************************** Anon 4 KiB : Read : 0.394 ms Anon 4 KiB : Write : 0.348 ms Anon 4 KiB : Read/Write : 0.400 ms Anon 4 KiB : POPULATE_READ : 0.326 ms Anon 4 KiB : POPULATE_WRITE : 0.273 ms Anon 2 MiB : Read : 0.030 ms Anon 2 MiB : Write : 0.030 ms Anon 2 MiB : Read/Write : 0.030 ms Anon 2 MiB : POPULATE_READ : 0.030 ms Anon 2 MiB : POPULATE_WRITE : 0.030 ms Memfd 4 KiB : Read : 0.412 ms Memfd 4 KiB : Write : 0.372 ms Memfd 4 KiB : Read/Write : 0.419 ms Memfd 4 KiB : POPULATE_READ : 0.343 ms Memfd 4 KiB : POPULATE_WRITE : 0.288 ms Memfd 4 KiB : FALLOCATE : 0.137 ms Memfd 4 KiB : FALLOCATE+Read : 0.446 ms Memfd 4 KiB : FALLOCATE+Write : 0.330 ms Memfd 4 KiB : FALLOCATE+Read/Write : 0.454 ms Memfd 4 KiB : FALLOCATE+POPULATE_READ : 0.379 ms Memfd 4 KiB : FALLOCATE+POPULATE_WRITE : 0.268 ms Memfd 2 MiB : Read : 0.030 ms Memfd 2 MiB : Write : 0.030 ms Memfd 2 MiB : Read/Write : 0.030 ms Memfd 2 MiB : POPULATE_READ : 0.030 ms Memfd 2 MiB : POPULATE_WRITE : 0.030 ms Memfd 2 MiB : FALLOCATE : 0.030 ms Memfd 2 MiB : FALLOCATE+Read : 0.031 ms Memfd 2 MiB : FALLOCATE+Write : 0.031 ms Memfd 2 MiB : FALLOCATE+Read/Write : 0.031 ms Memfd 2 MiB : FALLOCATE+POPULATE_READ : 0.030 ms Memfd 2 MiB : FALLOCATE+POPULATE_WRITE : 0.030 ms tmpfs : Read : 0.416 ms tmpfs : Write : 0.369 ms tmpfs : Read/Write : 0.425 ms tmpfs : POPULATE_READ : 0.346 ms tmpfs : POPULATE_WRITE : 0.295 ms tmpfs : FALLOCATE : 0.139 ms tmpfs : FALLOCATE+Read : 0.447 ms tmpfs : FALLOCATE+Write : 0.333 ms tmpfs : FALLOCATE+Read/Write : 0.454 ms tmpfs : FALLOCATE+POPULATE_READ : 0.380 ms tmpfs : FALLOCATE+POPULATE_WRITE : 0.272 ms file : Read : 0.191 ms file : Write : 0.511 ms file : Read/Write : 0.524 ms file : POPULATE_READ : 0.196 ms file : POPULATE_WRITE : 0.434 ms file : FALLOCATE : 0.004 ms file : FALLOCATE+Read : 0.197 ms file : FALLOCATE+Write : 0.554 ms file : FALLOCATE+Read/Write : 0.480 ms file : FALLOCATE+POPULATE_READ : 0.201 ms file : FALLOCATE+POPULATE_WRITE : 0.381 ms hugetlbfs : Read : 0.030 ms hugetlbfs : Write : 0.030 ms hugetlbfs : Read/Write : 0.030 ms hugetlbfs : POPULATE_READ : 0.030 ms hugetlbfs : POPULATE_WRITE : 0.030 ms hugetlbfs : FALLOCATE : 0.030 ms hugetlbfs : FALLOCATE+Read : 0.031 ms hugetlbfs : FALLOCATE+Write : 0.031 ms hugetlbfs : FALLOCATE+Read/Write : 0.030 ms hugetlbfs : FALLOCATE+POPULATE_READ : 0.030 ms hugetlbfs : FALLOCATE+POPULATE_WRITE : 0.030 ms ************************************************** 4096 MiB MAP_SHARED: ************************************************** Anon 4 KiB : Read : 1053.090 ms Anon 4 KiB : Write : 913.642 ms Anon 4 KiB : Read/Write : 1060.350 ms Anon 4 KiB : POPULATE_READ : 893.691 ms Anon 4 KiB : POPULATE_WRITE : 782.885 ms Anon 2 MiB : Read : 358.553 ms Anon 2 MiB : Write : 358.419 ms Anon 2 MiB : Read/Write : 357.992 ms Anon 2 MiB : POPULATE_READ : 357.533 ms Anon 2 MiB : POPULATE_WRITE : 357.808 ms Memfd 4 KiB : Read : 1078.144 ms Memfd 4 KiB : Write : 942.036 ms Memfd 4 KiB : Read/Write : 1100.391 ms Memfd 4 KiB : POPULATE_READ : 925.829 ms Memfd 4 KiB : POPULATE_WRITE : 804.394 ms Memfd 4 KiB : FALLOCATE : 304.632 ms Memfd 4 KiB : FALLOCATE+Read : 1163.359 ms Memfd 4 KiB : FALLOCATE+Write : 933.186 ms Memfd 4 KiB : FALLOCATE+Read/Write : 1187.304 ms Memfd 4 KiB : FALLOCATE+POPULATE_READ : 1013.660 ms Memfd 4 KiB : FALLOCATE+POPULATE_WRITE : 794.560 ms Memfd 2 MiB : Read : 358.131 ms Memfd 2 MiB : Write : 358.099 ms Memfd 2 MiB : Read/Write : 358.250 ms Memfd 2 MiB : POPULATE_READ : 357.563 ms Memfd 2 MiB : POPULATE_WRITE : 357.334 ms Memfd 2 MiB : FALLOCATE : 356.735 ms Memfd 2 MiB : FALLOCATE+Read : 358.152 ms Memfd 2 MiB : FALLOCATE+Write : 358.331 ms Memfd 2 MiB : FALLOCATE+Read/Write : 358.018 ms Memfd 2 MiB : FALLOCATE+POPULATE_READ : 357.286 ms Memfd 2 MiB : FALLOCATE+POPULATE_WRITE : 357.523 ms tmpfs : Read : 1087.265 ms tmpfs : Write : 950.840 ms tmpfs : Read/Write : 1107.567 ms tmpfs : POPULATE_READ : 922.605 ms tmpfs : POPULATE_WRITE : 810.094 ms tmpfs : FALLOCATE : 306.320 ms tmpfs : FALLOCATE+Read : 1169.796 ms tmpfs : FALLOCATE+Write : 933.730 ms tmpfs : FALLOCATE+Read/Write : 1191.610 ms tmpfs : FALLOCATE+POPULATE_READ : 1020.474 ms tmpfs : FALLOCATE+POPULATE_WRITE : 798.945 ms file : Read : 654.101 ms file : Write : 1259.142 ms file : Read/Write : 1289.509 ms file : POPULATE_READ : 661.642 ms file : POPULATE_WRITE : 1106.816 ms file : FALLOCATE : 1.864 ms file : FALLOCATE+Read : 656.328 ms file : FALLOCATE+Write : 1153.300 ms file : FALLOCATE+Read/Write : 1180.613 ms file : FALLOCATE+POPULATE_READ : 668.347 ms file : FALLOCATE+POPULATE_WRITE : 996.143 ms hugetlbfs : Read : 357.245 ms hugetlbfs : Write : 357.413 ms hugetlbfs : Read/Write : 357.120 ms hugetlbfs : POPULATE_READ : 356.321 ms hugetlbfs : POPULATE_WRITE : 356.693 ms hugetlbfs : FALLOCATE : 355.927 ms hugetlbfs : FALLOCATE+Read : 357.074 ms hugetlbfs : FALLOCATE+Write : 357.120 ms hugetlbfs : FALLOCATE+Read/Write : 356.983 ms hugetlbfs : FALLOCATE+POPULATE_READ : 356.413 ms hugetlbfs : FALLOCATE+POPULATE_WRITE : 356.266 ms ************************************************** [1] https://lkml.org/lkml/2013/6/27/698 [akpm@linux-foundation.org: coding style fixes] Link: https://lkml.kernel.org/r/20210419135443.12822-3-david@redhat.com Signed-off-by: David Hildenbrand <david@redhat.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Michal Hocko <mhocko@suse.com> Cc: Oscar Salvador <osalvador@suse.de> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Minchan Kim <minchan@kernel.org> Cc: Jann Horn <jannh@google.com> Cc: Jason Gunthorpe <jgg@ziepe.ca> Cc: Dave Hansen <dave.hansen@intel.com> Cc: Hugh Dickins <hughd@google.com> Cc: Rik van Riel <riel@surriel.com> Cc: Michael S. Tsirkin <mst@redhat.com> Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Richard Henderson <rth@twiddle.net> Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru> Cc: Matt Turner <mattst88@gmail.com> Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de> Cc: "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com> Cc: Helge Deller <deller@gmx.de> Cc: Chris Zankel <chris@zankel.net> Cc: Max Filippov <jcmvbkbc@gmail.com> Cc: Mike Kravetz <mike.kravetz@oracle.com> Cc: Peter Xu <peterx@redhat.com> Cc: Rolf Eike Beer <eike-kernel@sf-tec.de> Cc: Ram Pai <linuxram@us.ibm.com> Cc: Shuah Khan <shuah@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1307 lines
32 KiB
C
1307 lines
32 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* linux/mm/madvise.c
|
|
*
|
|
* Copyright (C) 1999 Linus Torvalds
|
|
* Copyright (C) 2002 Christoph Hellwig
|
|
*/
|
|
|
|
#include <linux/mman.h>
|
|
#include <linux/pagemap.h>
|
|
#include <linux/syscalls.h>
|
|
#include <linux/mempolicy.h>
|
|
#include <linux/page-isolation.h>
|
|
#include <linux/page_idle.h>
|
|
#include <linux/userfaultfd_k.h>
|
|
#include <linux/hugetlb.h>
|
|
#include <linux/falloc.h>
|
|
#include <linux/fadvise.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/sched/mm.h>
|
|
#include <linux/uio.h>
|
|
#include <linux/ksm.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/file.h>
|
|
#include <linux/blkdev.h>
|
|
#include <linux/backing-dev.h>
|
|
#include <linux/pagewalk.h>
|
|
#include <linux/swap.h>
|
|
#include <linux/swapops.h>
|
|
#include <linux/shmem_fs.h>
|
|
#include <linux/mmu_notifier.h>
|
|
|
|
#include <asm/tlb.h>
|
|
|
|
#include "internal.h"
|
|
|
|
struct madvise_walk_private {
|
|
struct mmu_gather *tlb;
|
|
bool pageout;
|
|
};
|
|
|
|
/*
|
|
* Any behaviour which results in changes to the vma->vm_flags needs to
|
|
* take mmap_lock for writing. Others, which simply traverse vmas, need
|
|
* to only take it for reading.
|
|
*/
|
|
static int madvise_need_mmap_write(int behavior)
|
|
{
|
|
switch (behavior) {
|
|
case MADV_REMOVE:
|
|
case MADV_WILLNEED:
|
|
case MADV_DONTNEED:
|
|
case MADV_COLD:
|
|
case MADV_PAGEOUT:
|
|
case MADV_FREE:
|
|
case MADV_POPULATE_READ:
|
|
case MADV_POPULATE_WRITE:
|
|
return 0;
|
|
default:
|
|
/* be safe, default to 1. list exceptions explicitly */
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* We can potentially split a vm area into separate
|
|
* areas, each area with its own behavior.
|
|
*/
|
|
static long madvise_behavior(struct vm_area_struct *vma,
|
|
struct vm_area_struct **prev,
|
|
unsigned long start, unsigned long end, int behavior)
|
|
{
|
|
struct mm_struct *mm = vma->vm_mm;
|
|
int error = 0;
|
|
pgoff_t pgoff;
|
|
unsigned long new_flags = vma->vm_flags;
|
|
|
|
switch (behavior) {
|
|
case MADV_NORMAL:
|
|
new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
|
|
break;
|
|
case MADV_SEQUENTIAL:
|
|
new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
|
|
break;
|
|
case MADV_RANDOM:
|
|
new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
|
|
break;
|
|
case MADV_DONTFORK:
|
|
new_flags |= VM_DONTCOPY;
|
|
break;
|
|
case MADV_DOFORK:
|
|
if (vma->vm_flags & VM_IO) {
|
|
error = -EINVAL;
|
|
goto out;
|
|
}
|
|
new_flags &= ~VM_DONTCOPY;
|
|
break;
|
|
case MADV_WIPEONFORK:
|
|
/* MADV_WIPEONFORK is only supported on anonymous memory. */
|
|
if (vma->vm_file || vma->vm_flags & VM_SHARED) {
|
|
error = -EINVAL;
|
|
goto out;
|
|
}
|
|
new_flags |= VM_WIPEONFORK;
|
|
break;
|
|
case MADV_KEEPONFORK:
|
|
new_flags &= ~VM_WIPEONFORK;
|
|
break;
|
|
case MADV_DONTDUMP:
|
|
new_flags |= VM_DONTDUMP;
|
|
break;
|
|
case MADV_DODUMP:
|
|
if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) {
|
|
error = -EINVAL;
|
|
goto out;
|
|
}
|
|
new_flags &= ~VM_DONTDUMP;
|
|
break;
|
|
case MADV_MERGEABLE:
|
|
case MADV_UNMERGEABLE:
|
|
error = ksm_madvise(vma, start, end, behavior, &new_flags);
|
|
if (error)
|
|
goto out_convert_errno;
|
|
break;
|
|
case MADV_HUGEPAGE:
|
|
case MADV_NOHUGEPAGE:
|
|
error = hugepage_madvise(vma, &new_flags, behavior);
|
|
if (error)
|
|
goto out_convert_errno;
|
|
break;
|
|
}
|
|
|
|
if (new_flags == vma->vm_flags) {
|
|
*prev = vma;
|
|
goto out;
|
|
}
|
|
|
|
pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
|
|
*prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
|
|
vma->vm_file, pgoff, vma_policy(vma),
|
|
vma->vm_userfaultfd_ctx);
|
|
if (*prev) {
|
|
vma = *prev;
|
|
goto success;
|
|
}
|
|
|
|
*prev = vma;
|
|
|
|
if (start != vma->vm_start) {
|
|
if (unlikely(mm->map_count >= sysctl_max_map_count)) {
|
|
error = -ENOMEM;
|
|
goto out;
|
|
}
|
|
error = __split_vma(mm, vma, start, 1);
|
|
if (error)
|
|
goto out_convert_errno;
|
|
}
|
|
|
|
if (end != vma->vm_end) {
|
|
if (unlikely(mm->map_count >= sysctl_max_map_count)) {
|
|
error = -ENOMEM;
|
|
goto out;
|
|
}
|
|
error = __split_vma(mm, vma, end, 0);
|
|
if (error)
|
|
goto out_convert_errno;
|
|
}
|
|
|
|
success:
|
|
/*
|
|
* vm_flags is protected by the mmap_lock held in write mode.
|
|
*/
|
|
vma->vm_flags = new_flags;
|
|
|
|
out_convert_errno:
|
|
/*
|
|
* madvise() returns EAGAIN if kernel resources, such as
|
|
* slab, are temporarily unavailable.
|
|
*/
|
|
if (error == -ENOMEM)
|
|
error = -EAGAIN;
|
|
out:
|
|
return error;
|
|
}
|
|
|
|
#ifdef CONFIG_SWAP
|
|
static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
|
|
unsigned long end, struct mm_walk *walk)
|
|
{
|
|
pte_t *orig_pte;
|
|
struct vm_area_struct *vma = walk->private;
|
|
unsigned long index;
|
|
|
|
if (pmd_none_or_trans_huge_or_clear_bad(pmd))
|
|
return 0;
|
|
|
|
for (index = start; index != end; index += PAGE_SIZE) {
|
|
pte_t pte;
|
|
swp_entry_t entry;
|
|
struct page *page;
|
|
spinlock_t *ptl;
|
|
|
|
orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl);
|
|
pte = *(orig_pte + ((index - start) / PAGE_SIZE));
|
|
pte_unmap_unlock(orig_pte, ptl);
|
|
|
|
if (pte_present(pte) || pte_none(pte))
|
|
continue;
|
|
entry = pte_to_swp_entry(pte);
|
|
if (unlikely(non_swap_entry(entry)))
|
|
continue;
|
|
|
|
page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE,
|
|
vma, index, false);
|
|
if (page)
|
|
put_page(page);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct mm_walk_ops swapin_walk_ops = {
|
|
.pmd_entry = swapin_walk_pmd_entry,
|
|
};
|
|
|
|
static void force_shm_swapin_readahead(struct vm_area_struct *vma,
|
|
unsigned long start, unsigned long end,
|
|
struct address_space *mapping)
|
|
{
|
|
XA_STATE(xas, &mapping->i_pages, linear_page_index(vma, start));
|
|
pgoff_t end_index = linear_page_index(vma, end + PAGE_SIZE - 1);
|
|
struct page *page;
|
|
|
|
rcu_read_lock();
|
|
xas_for_each(&xas, page, end_index) {
|
|
swp_entry_t swap;
|
|
|
|
if (!xa_is_value(page))
|
|
continue;
|
|
xas_pause(&xas);
|
|
rcu_read_unlock();
|
|
|
|
swap = radix_to_swp_entry(page);
|
|
page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
|
|
NULL, 0, false);
|
|
if (page)
|
|
put_page(page);
|
|
|
|
rcu_read_lock();
|
|
}
|
|
rcu_read_unlock();
|
|
|
|
lru_add_drain(); /* Push any new pages onto the LRU now */
|
|
}
|
|
#endif /* CONFIG_SWAP */
|
|
|
|
/*
|
|
* Schedule all required I/O operations. Do not wait for completion.
|
|
*/
|
|
static long madvise_willneed(struct vm_area_struct *vma,
|
|
struct vm_area_struct **prev,
|
|
unsigned long start, unsigned long end)
|
|
{
|
|
struct mm_struct *mm = vma->vm_mm;
|
|
struct file *file = vma->vm_file;
|
|
loff_t offset;
|
|
|
|
*prev = vma;
|
|
#ifdef CONFIG_SWAP
|
|
if (!file) {
|
|
walk_page_range(vma->vm_mm, start, end, &swapin_walk_ops, vma);
|
|
lru_add_drain(); /* Push any new pages onto the LRU now */
|
|
return 0;
|
|
}
|
|
|
|
if (shmem_mapping(file->f_mapping)) {
|
|
force_shm_swapin_readahead(vma, start, end,
|
|
file->f_mapping);
|
|
return 0;
|
|
}
|
|
#else
|
|
if (!file)
|
|
return -EBADF;
|
|
#endif
|
|
|
|
if (IS_DAX(file_inode(file))) {
|
|
/* no bad return value, but ignore advice */
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Filesystem's fadvise may need to take various locks. We need to
|
|
* explicitly grab a reference because the vma (and hence the
|
|
* vma's reference to the file) can go away as soon as we drop
|
|
* mmap_lock.
|
|
*/
|
|
*prev = NULL; /* tell sys_madvise we drop mmap_lock */
|
|
get_file(file);
|
|
offset = (loff_t)(start - vma->vm_start)
|
|
+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
|
|
mmap_read_unlock(mm);
|
|
vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
|
|
fput(file);
|
|
mmap_read_lock(mm);
|
|
return 0;
|
|
}
|
|
|
|
static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
|
|
unsigned long addr, unsigned long end,
|
|
struct mm_walk *walk)
|
|
{
|
|
struct madvise_walk_private *private = walk->private;
|
|
struct mmu_gather *tlb = private->tlb;
|
|
bool pageout = private->pageout;
|
|
struct mm_struct *mm = tlb->mm;
|
|
struct vm_area_struct *vma = walk->vma;
|
|
pte_t *orig_pte, *pte, ptent;
|
|
spinlock_t *ptl;
|
|
struct page *page = NULL;
|
|
LIST_HEAD(page_list);
|
|
|
|
if (fatal_signal_pending(current))
|
|
return -EINTR;
|
|
|
|
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
|
|
if (pmd_trans_huge(*pmd)) {
|
|
pmd_t orig_pmd;
|
|
unsigned long next = pmd_addr_end(addr, end);
|
|
|
|
tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
|
|
ptl = pmd_trans_huge_lock(pmd, vma);
|
|
if (!ptl)
|
|
return 0;
|
|
|
|
orig_pmd = *pmd;
|
|
if (is_huge_zero_pmd(orig_pmd))
|
|
goto huge_unlock;
|
|
|
|
if (unlikely(!pmd_present(orig_pmd))) {
|
|
VM_BUG_ON(thp_migration_supported() &&
|
|
!is_pmd_migration_entry(orig_pmd));
|
|
goto huge_unlock;
|
|
}
|
|
|
|
page = pmd_page(orig_pmd);
|
|
|
|
/* Do not interfere with other mappings of this page */
|
|
if (page_mapcount(page) != 1)
|
|
goto huge_unlock;
|
|
|
|
if (next - addr != HPAGE_PMD_SIZE) {
|
|
int err;
|
|
|
|
get_page(page);
|
|
spin_unlock(ptl);
|
|
lock_page(page);
|
|
err = split_huge_page(page);
|
|
unlock_page(page);
|
|
put_page(page);
|
|
if (!err)
|
|
goto regular_page;
|
|
return 0;
|
|
}
|
|
|
|
if (pmd_young(orig_pmd)) {
|
|
pmdp_invalidate(vma, addr, pmd);
|
|
orig_pmd = pmd_mkold(orig_pmd);
|
|
|
|
set_pmd_at(mm, addr, pmd, orig_pmd);
|
|
tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
|
|
}
|
|
|
|
ClearPageReferenced(page);
|
|
test_and_clear_page_young(page);
|
|
if (pageout) {
|
|
if (!isolate_lru_page(page)) {
|
|
if (PageUnevictable(page))
|
|
putback_lru_page(page);
|
|
else
|
|
list_add(&page->lru, &page_list);
|
|
}
|
|
} else
|
|
deactivate_page(page);
|
|
huge_unlock:
|
|
spin_unlock(ptl);
|
|
if (pageout)
|
|
reclaim_pages(&page_list);
|
|
return 0;
|
|
}
|
|
|
|
regular_page:
|
|
if (pmd_trans_unstable(pmd))
|
|
return 0;
|
|
#endif
|
|
tlb_change_page_size(tlb, PAGE_SIZE);
|
|
orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
|
|
flush_tlb_batched_pending(mm);
|
|
arch_enter_lazy_mmu_mode();
|
|
for (; addr < end; pte++, addr += PAGE_SIZE) {
|
|
ptent = *pte;
|
|
|
|
if (pte_none(ptent))
|
|
continue;
|
|
|
|
if (!pte_present(ptent))
|
|
continue;
|
|
|
|
page = vm_normal_page(vma, addr, ptent);
|
|
if (!page)
|
|
continue;
|
|
|
|
/*
|
|
* Creating a THP page is expensive so split it only if we
|
|
* are sure it's worth. Split it if we are only owner.
|
|
*/
|
|
if (PageTransCompound(page)) {
|
|
if (page_mapcount(page) != 1)
|
|
break;
|
|
get_page(page);
|
|
if (!trylock_page(page)) {
|
|
put_page(page);
|
|
break;
|
|
}
|
|
pte_unmap_unlock(orig_pte, ptl);
|
|
if (split_huge_page(page)) {
|
|
unlock_page(page);
|
|
put_page(page);
|
|
pte_offset_map_lock(mm, pmd, addr, &ptl);
|
|
break;
|
|
}
|
|
unlock_page(page);
|
|
put_page(page);
|
|
pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
|
|
pte--;
|
|
addr -= PAGE_SIZE;
|
|
continue;
|
|
}
|
|
|
|
/* Do not interfere with other mappings of this page */
|
|
if (page_mapcount(page) != 1)
|
|
continue;
|
|
|
|
VM_BUG_ON_PAGE(PageTransCompound(page), page);
|
|
|
|
if (pte_young(ptent)) {
|
|
ptent = ptep_get_and_clear_full(mm, addr, pte,
|
|
tlb->fullmm);
|
|
ptent = pte_mkold(ptent);
|
|
set_pte_at(mm, addr, pte, ptent);
|
|
tlb_remove_tlb_entry(tlb, pte, addr);
|
|
}
|
|
|
|
/*
|
|
* We are deactivating a page for accelerating reclaiming.
|
|
* VM couldn't reclaim the page unless we clear PG_young.
|
|
* As a side effect, it makes confuse idle-page tracking
|
|
* because they will miss recent referenced history.
|
|
*/
|
|
ClearPageReferenced(page);
|
|
test_and_clear_page_young(page);
|
|
if (pageout) {
|
|
if (!isolate_lru_page(page)) {
|
|
if (PageUnevictable(page))
|
|
putback_lru_page(page);
|
|
else
|
|
list_add(&page->lru, &page_list);
|
|
}
|
|
} else
|
|
deactivate_page(page);
|
|
}
|
|
|
|
arch_leave_lazy_mmu_mode();
|
|
pte_unmap_unlock(orig_pte, ptl);
|
|
if (pageout)
|
|
reclaim_pages(&page_list);
|
|
cond_resched();
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct mm_walk_ops cold_walk_ops = {
|
|
.pmd_entry = madvise_cold_or_pageout_pte_range,
|
|
};
|
|
|
|
static void madvise_cold_page_range(struct mmu_gather *tlb,
|
|
struct vm_area_struct *vma,
|
|
unsigned long addr, unsigned long end)
|
|
{
|
|
struct madvise_walk_private walk_private = {
|
|
.pageout = false,
|
|
.tlb = tlb,
|
|
};
|
|
|
|
tlb_start_vma(tlb, vma);
|
|
walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
|
|
tlb_end_vma(tlb, vma);
|
|
}
|
|
|
|
static long madvise_cold(struct vm_area_struct *vma,
|
|
struct vm_area_struct **prev,
|
|
unsigned long start_addr, unsigned long end_addr)
|
|
{
|
|
struct mm_struct *mm = vma->vm_mm;
|
|
struct mmu_gather tlb;
|
|
|
|
*prev = vma;
|
|
if (!can_madv_lru_vma(vma))
|
|
return -EINVAL;
|
|
|
|
lru_add_drain();
|
|
tlb_gather_mmu(&tlb, mm);
|
|
madvise_cold_page_range(&tlb, vma, start_addr, end_addr);
|
|
tlb_finish_mmu(&tlb);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void madvise_pageout_page_range(struct mmu_gather *tlb,
|
|
struct vm_area_struct *vma,
|
|
unsigned long addr, unsigned long end)
|
|
{
|
|
struct madvise_walk_private walk_private = {
|
|
.pageout = true,
|
|
.tlb = tlb,
|
|
};
|
|
|
|
tlb_start_vma(tlb, vma);
|
|
walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
|
|
tlb_end_vma(tlb, vma);
|
|
}
|
|
|
|
static inline bool can_do_pageout(struct vm_area_struct *vma)
|
|
{
|
|
if (vma_is_anonymous(vma))
|
|
return true;
|
|
if (!vma->vm_file)
|
|
return false;
|
|
/*
|
|
* paging out pagecache only for non-anonymous mappings that correspond
|
|
* to the files the calling process could (if tried) open for writing;
|
|
* otherwise we'd be including shared non-exclusive mappings, which
|
|
* opens a side channel.
|
|
*/
|
|
return inode_owner_or_capable(&init_user_ns,
|
|
file_inode(vma->vm_file)) ||
|
|
file_permission(vma->vm_file, MAY_WRITE) == 0;
|
|
}
|
|
|
|
static long madvise_pageout(struct vm_area_struct *vma,
|
|
struct vm_area_struct **prev,
|
|
unsigned long start_addr, unsigned long end_addr)
|
|
{
|
|
struct mm_struct *mm = vma->vm_mm;
|
|
struct mmu_gather tlb;
|
|
|
|
*prev = vma;
|
|
if (!can_madv_lru_vma(vma))
|
|
return -EINVAL;
|
|
|
|
if (!can_do_pageout(vma))
|
|
return 0;
|
|
|
|
lru_add_drain();
|
|
tlb_gather_mmu(&tlb, mm);
|
|
madvise_pageout_page_range(&tlb, vma, start_addr, end_addr);
|
|
tlb_finish_mmu(&tlb);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
|
|
unsigned long end, struct mm_walk *walk)
|
|
|
|
{
|
|
struct mmu_gather *tlb = walk->private;
|
|
struct mm_struct *mm = tlb->mm;
|
|
struct vm_area_struct *vma = walk->vma;
|
|
spinlock_t *ptl;
|
|
pte_t *orig_pte, *pte, ptent;
|
|
struct page *page;
|
|
int nr_swap = 0;
|
|
unsigned long next;
|
|
|
|
next = pmd_addr_end(addr, end);
|
|
if (pmd_trans_huge(*pmd))
|
|
if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
|
|
goto next;
|
|
|
|
if (pmd_trans_unstable(pmd))
|
|
return 0;
|
|
|
|
tlb_change_page_size(tlb, PAGE_SIZE);
|
|
orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
|
|
flush_tlb_batched_pending(mm);
|
|
arch_enter_lazy_mmu_mode();
|
|
for (; addr != end; pte++, addr += PAGE_SIZE) {
|
|
ptent = *pte;
|
|
|
|
if (pte_none(ptent))
|
|
continue;
|
|
/*
|
|
* If the pte has swp_entry, just clear page table to
|
|
* prevent swap-in which is more expensive rather than
|
|
* (page allocation + zeroing).
|
|
*/
|
|
if (!pte_present(ptent)) {
|
|
swp_entry_t entry;
|
|
|
|
entry = pte_to_swp_entry(ptent);
|
|
if (non_swap_entry(entry))
|
|
continue;
|
|
nr_swap--;
|
|
free_swap_and_cache(entry);
|
|
pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
|
|
continue;
|
|
}
|
|
|
|
page = vm_normal_page(vma, addr, ptent);
|
|
if (!page)
|
|
continue;
|
|
|
|
/*
|
|
* If pmd isn't transhuge but the page is THP and
|
|
* is owned by only this process, split it and
|
|
* deactivate all pages.
|
|
*/
|
|
if (PageTransCompound(page)) {
|
|
if (page_mapcount(page) != 1)
|
|
goto out;
|
|
get_page(page);
|
|
if (!trylock_page(page)) {
|
|
put_page(page);
|
|
goto out;
|
|
}
|
|
pte_unmap_unlock(orig_pte, ptl);
|
|
if (split_huge_page(page)) {
|
|
unlock_page(page);
|
|
put_page(page);
|
|
pte_offset_map_lock(mm, pmd, addr, &ptl);
|
|
goto out;
|
|
}
|
|
unlock_page(page);
|
|
put_page(page);
|
|
pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
|
|
pte--;
|
|
addr -= PAGE_SIZE;
|
|
continue;
|
|
}
|
|
|
|
VM_BUG_ON_PAGE(PageTransCompound(page), page);
|
|
|
|
if (PageSwapCache(page) || PageDirty(page)) {
|
|
if (!trylock_page(page))
|
|
continue;
|
|
/*
|
|
* If page is shared with others, we couldn't clear
|
|
* PG_dirty of the page.
|
|
*/
|
|
if (page_mapcount(page) != 1) {
|
|
unlock_page(page);
|
|
continue;
|
|
}
|
|
|
|
if (PageSwapCache(page) && !try_to_free_swap(page)) {
|
|
unlock_page(page);
|
|
continue;
|
|
}
|
|
|
|
ClearPageDirty(page);
|
|
unlock_page(page);
|
|
}
|
|
|
|
if (pte_young(ptent) || pte_dirty(ptent)) {
|
|
/*
|
|
* Some of architecture(ex, PPC) don't update TLB
|
|
* with set_pte_at and tlb_remove_tlb_entry so for
|
|
* the portability, remap the pte with old|clean
|
|
* after pte clearing.
|
|
*/
|
|
ptent = ptep_get_and_clear_full(mm, addr, pte,
|
|
tlb->fullmm);
|
|
|
|
ptent = pte_mkold(ptent);
|
|
ptent = pte_mkclean(ptent);
|
|
set_pte_at(mm, addr, pte, ptent);
|
|
tlb_remove_tlb_entry(tlb, pte, addr);
|
|
}
|
|
mark_page_lazyfree(page);
|
|
}
|
|
out:
|
|
if (nr_swap) {
|
|
if (current->mm == mm)
|
|
sync_mm_rss(mm);
|
|
|
|
add_mm_counter(mm, MM_SWAPENTS, nr_swap);
|
|
}
|
|
arch_leave_lazy_mmu_mode();
|
|
pte_unmap_unlock(orig_pte, ptl);
|
|
cond_resched();
|
|
next:
|
|
return 0;
|
|
}
|
|
|
|
static const struct mm_walk_ops madvise_free_walk_ops = {
|
|
.pmd_entry = madvise_free_pte_range,
|
|
};
|
|
|
|
static int madvise_free_single_vma(struct vm_area_struct *vma,
|
|
unsigned long start_addr, unsigned long end_addr)
|
|
{
|
|
struct mm_struct *mm = vma->vm_mm;
|
|
struct mmu_notifier_range range;
|
|
struct mmu_gather tlb;
|
|
|
|
/* MADV_FREE works for only anon vma at the moment */
|
|
if (!vma_is_anonymous(vma))
|
|
return -EINVAL;
|
|
|
|
range.start = max(vma->vm_start, start_addr);
|
|
if (range.start >= vma->vm_end)
|
|
return -EINVAL;
|
|
range.end = min(vma->vm_end, end_addr);
|
|
if (range.end <= vma->vm_start)
|
|
return -EINVAL;
|
|
mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
|
|
range.start, range.end);
|
|
|
|
lru_add_drain();
|
|
tlb_gather_mmu(&tlb, mm);
|
|
update_hiwater_rss(mm);
|
|
|
|
mmu_notifier_invalidate_range_start(&range);
|
|
tlb_start_vma(&tlb, vma);
|
|
walk_page_range(vma->vm_mm, range.start, range.end,
|
|
&madvise_free_walk_ops, &tlb);
|
|
tlb_end_vma(&tlb, vma);
|
|
mmu_notifier_invalidate_range_end(&range);
|
|
tlb_finish_mmu(&tlb);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Application no longer needs these pages. If the pages are dirty,
|
|
* it's OK to just throw them away. The app will be more careful about
|
|
* data it wants to keep. Be sure to free swap resources too. The
|
|
* zap_page_range call sets things up for shrink_active_list to actually free
|
|
* these pages later if no one else has touched them in the meantime,
|
|
* although we could add these pages to a global reuse list for
|
|
* shrink_active_list to pick up before reclaiming other pages.
|
|
*
|
|
* NB: This interface discards data rather than pushes it out to swap,
|
|
* as some implementations do. This has performance implications for
|
|
* applications like large transactional databases which want to discard
|
|
* pages in anonymous maps after committing to backing store the data
|
|
* that was kept in them. There is no reason to write this data out to
|
|
* the swap area if the application is discarding it.
|
|
*
|
|
* An interface that causes the system to free clean pages and flush
|
|
* dirty pages is already available as msync(MS_INVALIDATE).
|
|
*/
|
|
static long madvise_dontneed_single_vma(struct vm_area_struct *vma,
|
|
unsigned long start, unsigned long end)
|
|
{
|
|
zap_page_range(vma, start, end - start);
|
|
return 0;
|
|
}
|
|
|
|
static long madvise_dontneed_free(struct vm_area_struct *vma,
|
|
struct vm_area_struct **prev,
|
|
unsigned long start, unsigned long end,
|
|
int behavior)
|
|
{
|
|
struct mm_struct *mm = vma->vm_mm;
|
|
|
|
*prev = vma;
|
|
if (!can_madv_lru_vma(vma))
|
|
return -EINVAL;
|
|
|
|
if (!userfaultfd_remove(vma, start, end)) {
|
|
*prev = NULL; /* mmap_lock has been dropped, prev is stale */
|
|
|
|
mmap_read_lock(mm);
|
|
vma = find_vma(mm, start);
|
|
if (!vma)
|
|
return -ENOMEM;
|
|
if (start < vma->vm_start) {
|
|
/*
|
|
* This "vma" under revalidation is the one
|
|
* with the lowest vma->vm_start where start
|
|
* is also < vma->vm_end. If start <
|
|
* vma->vm_start it means an hole materialized
|
|
* in the user address space within the
|
|
* virtual range passed to MADV_DONTNEED
|
|
* or MADV_FREE.
|
|
*/
|
|
return -ENOMEM;
|
|
}
|
|
if (!can_madv_lru_vma(vma))
|
|
return -EINVAL;
|
|
if (end > vma->vm_end) {
|
|
/*
|
|
* Don't fail if end > vma->vm_end. If the old
|
|
* vma was split while the mmap_lock was
|
|
* released the effect of the concurrent
|
|
* operation may not cause madvise() to
|
|
* have an undefined result. There may be an
|
|
* adjacent next vma that we'll walk
|
|
* next. userfaultfd_remove() will generate an
|
|
* UFFD_EVENT_REMOVE repetition on the
|
|
* end-vma->vm_end range, but the manager can
|
|
* handle a repetition fine.
|
|
*/
|
|
end = vma->vm_end;
|
|
}
|
|
VM_WARN_ON(start >= end);
|
|
}
|
|
|
|
if (behavior == MADV_DONTNEED)
|
|
return madvise_dontneed_single_vma(vma, start, end);
|
|
else if (behavior == MADV_FREE)
|
|
return madvise_free_single_vma(vma, start, end);
|
|
else
|
|
return -EINVAL;
|
|
}
|
|
|
|
static long madvise_populate(struct vm_area_struct *vma,
|
|
struct vm_area_struct **prev,
|
|
unsigned long start, unsigned long end,
|
|
int behavior)
|
|
{
|
|
const bool write = behavior == MADV_POPULATE_WRITE;
|
|
struct mm_struct *mm = vma->vm_mm;
|
|
unsigned long tmp_end;
|
|
int locked = 1;
|
|
long pages;
|
|
|
|
*prev = vma;
|
|
|
|
while (start < end) {
|
|
/*
|
|
* We might have temporarily dropped the lock. For example,
|
|
* our VMA might have been split.
|
|
*/
|
|
if (!vma || start >= vma->vm_end) {
|
|
vma = find_vma(mm, start);
|
|
if (!vma || start < vma->vm_start)
|
|
return -ENOMEM;
|
|
}
|
|
|
|
tmp_end = min_t(unsigned long, end, vma->vm_end);
|
|
/* Populate (prefault) page tables readable/writable. */
|
|
pages = faultin_vma_page_range(vma, start, tmp_end, write,
|
|
&locked);
|
|
if (!locked) {
|
|
mmap_read_lock(mm);
|
|
locked = 1;
|
|
*prev = NULL;
|
|
vma = NULL;
|
|
}
|
|
if (pages < 0) {
|
|
switch (pages) {
|
|
case -EINTR:
|
|
return -EINTR;
|
|
case -EFAULT: /* Incompatible mappings / permissions. */
|
|
return -EINVAL;
|
|
case -EHWPOISON:
|
|
return -EHWPOISON;
|
|
default:
|
|
pr_warn_once("%s: unhandled return value: %ld\n",
|
|
__func__, pages);
|
|
fallthrough;
|
|
case -ENOMEM:
|
|
return -ENOMEM;
|
|
}
|
|
}
|
|
start += pages * PAGE_SIZE;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Application wants to free up the pages and associated backing store.
|
|
* This is effectively punching a hole into the middle of a file.
|
|
*/
|
|
static long madvise_remove(struct vm_area_struct *vma,
|
|
struct vm_area_struct **prev,
|
|
unsigned long start, unsigned long end)
|
|
{
|
|
loff_t offset;
|
|
int error;
|
|
struct file *f;
|
|
struct mm_struct *mm = vma->vm_mm;
|
|
|
|
*prev = NULL; /* tell sys_madvise we drop mmap_lock */
|
|
|
|
if (vma->vm_flags & VM_LOCKED)
|
|
return -EINVAL;
|
|
|
|
f = vma->vm_file;
|
|
|
|
if (!f || !f->f_mapping || !f->f_mapping->host) {
|
|
return -EINVAL;
|
|
}
|
|
|
|
if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
|
|
return -EACCES;
|
|
|
|
offset = (loff_t)(start - vma->vm_start)
|
|
+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
|
|
|
|
/*
|
|
* Filesystem's fallocate may need to take i_mutex. We need to
|
|
* explicitly grab a reference because the vma (and hence the
|
|
* vma's reference to the file) can go away as soon as we drop
|
|
* mmap_lock.
|
|
*/
|
|
get_file(f);
|
|
if (userfaultfd_remove(vma, start, end)) {
|
|
/* mmap_lock was not released by userfaultfd_remove() */
|
|
mmap_read_unlock(mm);
|
|
}
|
|
error = vfs_fallocate(f,
|
|
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
|
|
offset, end - start);
|
|
fput(f);
|
|
mmap_read_lock(mm);
|
|
return error;
|
|
}
|
|
|
|
#ifdef CONFIG_MEMORY_FAILURE
|
|
/*
|
|
* Error injection support for memory error handling.
|
|
*/
|
|
static int madvise_inject_error(int behavior,
|
|
unsigned long start, unsigned long end)
|
|
{
|
|
unsigned long size;
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
return -EPERM;
|
|
|
|
|
|
for (; start < end; start += size) {
|
|
unsigned long pfn;
|
|
struct page *page;
|
|
int ret;
|
|
|
|
ret = get_user_pages_fast(start, 1, 0, &page);
|
|
if (ret != 1)
|
|
return ret;
|
|
pfn = page_to_pfn(page);
|
|
|
|
/*
|
|
* When soft offlining hugepages, after migrating the page
|
|
* we dissolve it, therefore in the second loop "page" will
|
|
* no longer be a compound page.
|
|
*/
|
|
size = page_size(compound_head(page));
|
|
|
|
if (behavior == MADV_SOFT_OFFLINE) {
|
|
pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
|
|
pfn, start);
|
|
ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
|
|
} else {
|
|
pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
|
|
pfn, start);
|
|
ret = memory_failure(pfn, MF_COUNT_INCREASED);
|
|
}
|
|
|
|
if (ret)
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
static long
|
|
madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
|
|
unsigned long start, unsigned long end, int behavior)
|
|
{
|
|
switch (behavior) {
|
|
case MADV_REMOVE:
|
|
return madvise_remove(vma, prev, start, end);
|
|
case MADV_WILLNEED:
|
|
return madvise_willneed(vma, prev, start, end);
|
|
case MADV_COLD:
|
|
return madvise_cold(vma, prev, start, end);
|
|
case MADV_PAGEOUT:
|
|
return madvise_pageout(vma, prev, start, end);
|
|
case MADV_FREE:
|
|
case MADV_DONTNEED:
|
|
return madvise_dontneed_free(vma, prev, start, end, behavior);
|
|
case MADV_POPULATE_READ:
|
|
case MADV_POPULATE_WRITE:
|
|
return madvise_populate(vma, prev, start, end, behavior);
|
|
default:
|
|
return madvise_behavior(vma, prev, start, end, behavior);
|
|
}
|
|
}
|
|
|
|
static bool
|
|
madvise_behavior_valid(int behavior)
|
|
{
|
|
switch (behavior) {
|
|
case MADV_DOFORK:
|
|
case MADV_DONTFORK:
|
|
case MADV_NORMAL:
|
|
case MADV_SEQUENTIAL:
|
|
case MADV_RANDOM:
|
|
case MADV_REMOVE:
|
|
case MADV_WILLNEED:
|
|
case MADV_DONTNEED:
|
|
case MADV_FREE:
|
|
case MADV_COLD:
|
|
case MADV_PAGEOUT:
|
|
case MADV_POPULATE_READ:
|
|
case MADV_POPULATE_WRITE:
|
|
#ifdef CONFIG_KSM
|
|
case MADV_MERGEABLE:
|
|
case MADV_UNMERGEABLE:
|
|
#endif
|
|
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
|
|
case MADV_HUGEPAGE:
|
|
case MADV_NOHUGEPAGE:
|
|
#endif
|
|
case MADV_DONTDUMP:
|
|
case MADV_DODUMP:
|
|
case MADV_WIPEONFORK:
|
|
case MADV_KEEPONFORK:
|
|
#ifdef CONFIG_MEMORY_FAILURE
|
|
case MADV_SOFT_OFFLINE:
|
|
case MADV_HWPOISON:
|
|
#endif
|
|
return true;
|
|
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static bool
|
|
process_madvise_behavior_valid(int behavior)
|
|
{
|
|
switch (behavior) {
|
|
case MADV_COLD:
|
|
case MADV_PAGEOUT:
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* The madvise(2) system call.
|
|
*
|
|
* Applications can use madvise() to advise the kernel how it should
|
|
* handle paging I/O in this VM area. The idea is to help the kernel
|
|
* use appropriate read-ahead and caching techniques. The information
|
|
* provided is advisory only, and can be safely disregarded by the
|
|
* kernel without affecting the correct operation of the application.
|
|
*
|
|
* behavior values:
|
|
* MADV_NORMAL - the default behavior is to read clusters. This
|
|
* results in some read-ahead and read-behind.
|
|
* MADV_RANDOM - the system should read the minimum amount of data
|
|
* on any access, since it is unlikely that the appli-
|
|
* cation will need more than what it asks for.
|
|
* MADV_SEQUENTIAL - pages in the given range will probably be accessed
|
|
* once, so they can be aggressively read ahead, and
|
|
* can be freed soon after they are accessed.
|
|
* MADV_WILLNEED - the application is notifying the system to read
|
|
* some pages ahead.
|
|
* MADV_DONTNEED - the application is finished with the given range,
|
|
* so the kernel can free resources associated with it.
|
|
* MADV_FREE - the application marks pages in the given range as lazy free,
|
|
* where actual purges are postponed until memory pressure happens.
|
|
* MADV_REMOVE - the application wants to free up the given range of
|
|
* pages and associated backing store.
|
|
* MADV_DONTFORK - omit this area from child's address space when forking:
|
|
* typically, to avoid COWing pages pinned by get_user_pages().
|
|
* MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
|
|
* MADV_WIPEONFORK - present the child process with zero-filled memory in this
|
|
* range after a fork.
|
|
* MADV_KEEPONFORK - undo the effect of MADV_WIPEONFORK
|
|
* MADV_HWPOISON - trigger memory error handler as if the given memory range
|
|
* were corrupted by unrecoverable hardware memory failure.
|
|
* MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
|
|
* MADV_MERGEABLE - the application recommends that KSM try to merge pages in
|
|
* this area with pages of identical content from other such areas.
|
|
* MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
|
|
* MADV_HUGEPAGE - the application wants to back the given range by transparent
|
|
* huge pages in the future. Existing pages might be coalesced and
|
|
* new pages might be allocated as THP.
|
|
* MADV_NOHUGEPAGE - mark the given range as not worth being backed by
|
|
* transparent huge pages so the existing pages will not be
|
|
* coalesced into THP and new pages will not be allocated as THP.
|
|
* MADV_DONTDUMP - the application wants to prevent pages in the given range
|
|
* from being included in its core dump.
|
|
* MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
|
|
* MADV_COLD - the application is not expected to use this memory soon,
|
|
* deactivate pages in this range so that they can be reclaimed
|
|
* easily if memory pressure happens.
|
|
* MADV_PAGEOUT - the application is not expected to use this memory soon,
|
|
* page out the pages in this range immediately.
|
|
* MADV_POPULATE_READ - populate (prefault) page tables readable by
|
|
* triggering read faults if required
|
|
* MADV_POPULATE_WRITE - populate (prefault) page tables writable by
|
|
* triggering write faults if required
|
|
*
|
|
* return values:
|
|
* zero - success
|
|
* -EINVAL - start + len < 0, start is not page-aligned,
|
|
* "behavior" is not a valid value, or application
|
|
* is attempting to release locked or shared pages,
|
|
* or the specified address range includes file, Huge TLB,
|
|
* MAP_SHARED or VMPFNMAP range.
|
|
* -ENOMEM - addresses in the specified range are not currently
|
|
* mapped, or are outside the AS of the process.
|
|
* -EIO - an I/O error occurred while paging in data.
|
|
* -EBADF - map exists, but area maps something that isn't a file.
|
|
* -EAGAIN - a kernel resource was temporarily unavailable.
|
|
*/
|
|
int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior)
|
|
{
|
|
unsigned long end, tmp;
|
|
struct vm_area_struct *vma, *prev;
|
|
int unmapped_error = 0;
|
|
int error = -EINVAL;
|
|
int write;
|
|
size_t len;
|
|
struct blk_plug plug;
|
|
|
|
start = untagged_addr(start);
|
|
|
|
if (!madvise_behavior_valid(behavior))
|
|
return error;
|
|
|
|
if (!PAGE_ALIGNED(start))
|
|
return error;
|
|
len = PAGE_ALIGN(len_in);
|
|
|
|
/* Check to see whether len was rounded up from small -ve to zero */
|
|
if (len_in && !len)
|
|
return error;
|
|
|
|
end = start + len;
|
|
if (end < start)
|
|
return error;
|
|
|
|
error = 0;
|
|
if (end == start)
|
|
return error;
|
|
|
|
#ifdef CONFIG_MEMORY_FAILURE
|
|
if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
|
|
return madvise_inject_error(behavior, start, start + len_in);
|
|
#endif
|
|
|
|
write = madvise_need_mmap_write(behavior);
|
|
if (write) {
|
|
if (mmap_write_lock_killable(mm))
|
|
return -EINTR;
|
|
} else {
|
|
mmap_read_lock(mm);
|
|
}
|
|
|
|
/*
|
|
* If the interval [start,end) covers some unmapped address
|
|
* ranges, just ignore them, but return -ENOMEM at the end.
|
|
* - different from the way of handling in mlock etc.
|
|
*/
|
|
vma = find_vma_prev(mm, start, &prev);
|
|
if (vma && start > vma->vm_start)
|
|
prev = vma;
|
|
|
|
blk_start_plug(&plug);
|
|
for (;;) {
|
|
/* Still start < end. */
|
|
error = -ENOMEM;
|
|
if (!vma)
|
|
goto out;
|
|
|
|
/* Here start < (end|vma->vm_end). */
|
|
if (start < vma->vm_start) {
|
|
unmapped_error = -ENOMEM;
|
|
start = vma->vm_start;
|
|
if (start >= end)
|
|
goto out;
|
|
}
|
|
|
|
/* Here vma->vm_start <= start < (end|vma->vm_end) */
|
|
tmp = vma->vm_end;
|
|
if (end < tmp)
|
|
tmp = end;
|
|
|
|
/* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
|
|
error = madvise_vma(vma, &prev, start, tmp, behavior);
|
|
if (error)
|
|
goto out;
|
|
start = tmp;
|
|
if (prev && start < prev->vm_end)
|
|
start = prev->vm_end;
|
|
error = unmapped_error;
|
|
if (start >= end)
|
|
goto out;
|
|
if (prev)
|
|
vma = prev->vm_next;
|
|
else /* madvise_remove dropped mmap_lock */
|
|
vma = find_vma(mm, start);
|
|
}
|
|
out:
|
|
blk_finish_plug(&plug);
|
|
if (write)
|
|
mmap_write_unlock(mm);
|
|
else
|
|
mmap_read_unlock(mm);
|
|
|
|
return error;
|
|
}
|
|
|
|
SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
|
|
{
|
|
return do_madvise(current->mm, start, len_in, behavior);
|
|
}
|
|
|
|
SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
|
|
size_t, vlen, int, behavior, unsigned int, flags)
|
|
{
|
|
ssize_t ret;
|
|
struct iovec iovstack[UIO_FASTIOV], iovec;
|
|
struct iovec *iov = iovstack;
|
|
struct iov_iter iter;
|
|
struct pid *pid;
|
|
struct task_struct *task;
|
|
struct mm_struct *mm;
|
|
size_t total_len;
|
|
unsigned int f_flags;
|
|
|
|
if (flags != 0) {
|
|
ret = -EINVAL;
|
|
goto out;
|
|
}
|
|
|
|
ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
|
|
if (ret < 0)
|
|
goto out;
|
|
|
|
pid = pidfd_get_pid(pidfd, &f_flags);
|
|
if (IS_ERR(pid)) {
|
|
ret = PTR_ERR(pid);
|
|
goto free_iov;
|
|
}
|
|
|
|
task = get_pid_task(pid, PIDTYPE_PID);
|
|
if (!task) {
|
|
ret = -ESRCH;
|
|
goto put_pid;
|
|
}
|
|
|
|
if (!process_madvise_behavior_valid(behavior)) {
|
|
ret = -EINVAL;
|
|
goto release_task;
|
|
}
|
|
|
|
/* Require PTRACE_MODE_READ to avoid leaking ASLR metadata. */
|
|
mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
|
|
if (IS_ERR_OR_NULL(mm)) {
|
|
ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
|
|
goto release_task;
|
|
}
|
|
|
|
/*
|
|
* Require CAP_SYS_NICE for influencing process performance. Note that
|
|
* only non-destructive hints are currently supported.
|
|
*/
|
|
if (!capable(CAP_SYS_NICE)) {
|
|
ret = -EPERM;
|
|
goto release_mm;
|
|
}
|
|
|
|
total_len = iov_iter_count(&iter);
|
|
|
|
while (iov_iter_count(&iter)) {
|
|
iovec = iov_iter_iovec(&iter);
|
|
ret = do_madvise(mm, (unsigned long)iovec.iov_base,
|
|
iovec.iov_len, behavior);
|
|
if (ret < 0)
|
|
break;
|
|
iov_iter_advance(&iter, iovec.iov_len);
|
|
}
|
|
|
|
if (ret == 0)
|
|
ret = total_len - iov_iter_count(&iter);
|
|
|
|
release_mm:
|
|
mmput(mm);
|
|
release_task:
|
|
put_task_struct(task);
|
|
put_pid:
|
|
put_pid(pid);
|
|
free_iov:
|
|
kfree(iov);
|
|
out:
|
|
return ret;
|
|
}
|