introduce memory hinting API for external process
Subsystems (20)
v4 cover introduce memory hinting API for external process
Now, we have MADV_PAGEOUT and MADV_COLD as madvise hinting API. With that, application could give hints to kernel what memory range are preferred to be reclaimed. However, in some platform(e.g., Android), the information required to make the hinting decision is not known to the app.
Instead, it is known to a centralized userspace daemon(e.g., ActivityManagerService), and that daemon must be able to initiate reclaim on its own without any app involvement.
To solve the concern, this patch introduces new syscall - process_madvise(2). Bascially, it's same with madvise(2) syscall but it has some differences.
1. It needs pidfd of target process to provide the hint
2. It supports only MADV_{COLD|PAGEOUT|MERGEABLE|UNMEREABLE} at this moment. Other hints in madvise will be opened when there are explicit requests from community to prevent unexpected bugs we couldn't support.
3. Only privileged processes can do something for other process's address space.
For more detail of the new API, please see "mm: introduce external memory hinting API" description in this patchset.
Minchan Kim (6):
mm: pass task to do_madvise
mm: introduce external memory hinting API
mm: validate mm in do_madvise
mm: check fatal signal pending of target process
pid: export pidfd_get_pid
mm: support both pid and pidfd for process_madvise
Oleksandr Natalenko (2):
mm/madvise: employ mmget_still_valid for write lock mm/madvise: allow KSM hints for remote API
* from v3 - https://lore.kernel.org/linux-mm/20200128001641.5086-1-minchan-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org/
* verify task->mm aftere access_mm - Oleg
* split some patches for easy review - Alexander
* clean up fatal signal checking - Suren
* from v2 - https://lore.kernel.org/linux-mm/20200116235953.163318-1-minchan-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org/
* check signal callee and caller to bail out - Kirill Tkhai
* put more clarification for justification of new API
* from v1 - https://lore.kernel.org/linux-mm/20200110213433.94739-1-minchan-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org/
* fix syscall number - SeongJae
* use get_pid_task - Kirill Tkhai
* extend API to support pid as well as pidfd - Kirill Tkhai
arch/alpha/kernel/syscalls/syscall.tbl | 1 + arch/arm/tools/syscall.tbl | 1 + arch/arm64/include/asm/unistd.h | 2 +- arch/arm64/include/asm/unistd32.h | 2 + arch/ia64/kernel/syscalls/syscall.tbl | 1 + arch/m68k/kernel/syscalls/syscall.tbl | 1 + arch/microblaze/kernel/syscalls/syscall.tbl | 1 + arch/mips/kernel/syscalls/syscall_n32.tbl | 1 + arch/mips/kernel/syscalls/syscall_n64.tbl | 1 + arch/parisc/kernel/syscalls/syscall.tbl | 1 + arch/powerpc/kernel/syscalls/syscall.tbl | 1 + arch/s390/kernel/syscalls/syscall.tbl | 1 + arch/sh/kernel/syscalls/syscall.tbl | 1 + arch/sparc/kernel/syscalls/syscall.tbl | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/xtensa/kernel/syscalls/syscall.tbl | 1 + fs/io_uring.c | 2 +- include/linux/mm.h | 3 +- include/linux/pid.h | 1 + include/linux/syscalls.h | 3 + include/uapi/asm-generic/unistd.h | 4 +- kernel/exit.c | 17 --- kernel/pid.c | 17 +++ kernel/sys_ni.c | 1 + mm/madvise.c | 149 ++++++++++++++++---- 26 files changed, 170 insertions(+), 46 deletions(-)
Signature · 1 line
2.25.0.225.g125e21ebc7-goog
PATCH v4 1/8 mm: pass task to do_madvise
In upcoming patches, do_madvise will be called from external process context so it shouldn't asssume "current" is always hinted process's task_struct. Thus, let's get the mm_struct from vma->vm_mm, not current because vma is always hinted process's one. And let's pass *current* as new task argument of do_madvise so it shouldn't change existing behavior.
--- fs/io_uring.c | 2 +- include/linux/mm.h | 3 ++- mm/madvise.c | 37 ++++++++++++++++++++----------------- 3 files changed, 23 insertions(+), 19 deletions(-)
fs/io_uring.c +1 -1
@@ -2736,7 +2736,7 @@ static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,if (force_nonblock)return -EAGAIN ; - ret =do_madvise (ma->addr, ma->len, ma->advice); + ret =do_madvise (current, ma->addr, ma->len, ma->advice);if (ret <0 )req_set_fail_links (req);io_cqring_add_event (req, ret);
include/linux/mm.h +2 -1
@@ -2323,7 +2323,8 @@ extern int __do_munmap(struct mm_struct *, unsigned long, size_t, struct list_head *uf, bool downgrade);extern int do_munmap (struct mm_struct *,unsigned long ,size_t ,struct list_head *uf); -extern int do_madvise (unsigned long start,size_t len_in,int behavior); +extern int do_madvise (struct task_struct *task,unsigned long start, +size_t len_in,int behavior); static inline unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
mm/madvise.c +20 -17
@@ -256,6 +256,7 @@ static long madvise_willneed(struct vm_area_struct *vma, {struct file *file = vma->vm_file;loff_t offset; +struct mm_struct *mm = vma->vm_mm; *prev = vma;#ifdef CONFIG_SWAP
@@ -288,12 +289,12 @@ static long madvise_willneed(struct vm_area_struct *vma,*/ *prev =NULL ;/* tell sys_madvise we drop mmap_sem */ get_file (file); -up_read (¤t->mm->mmap_sem); +up_read (&mm->mmap_sem); offset = (loff_t)(start - vma->vm_start) + ((loff_t )vma->vm_pgoff <<PAGE_SHIFT );vfs_fadvise (file, offset, end - start,POSIX_FADV_WILLNEED );fput (file); -down_read (¤t->mm->mmap_sem); +down_read (&mm->mmap_sem);return 0 ; }
@@ -674,9 +675,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, } out:if (nr_swap) { -if (current->mm == mm) -sync_mm_rss (mm); - +sync_mm_rss (mm);add_mm_counter (mm,MM_SWAPENTS , nr_swap); }arch_leave_lazy_mmu_mode ();
@@ -756,6 +755,7 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, 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 ;
@@ -763,8 +763,8 @@ static long madvise_dontneed_free(struct vm_area_struct *vma,if (!userfaultfd_remove (vma, start, end)) { *prev =NULL ;/* mmap_sem has been dropped, prev is stale */ -down_read (¤t->mm->mmap_sem); - vma =find_vma (current->mm, start); +down_read (&mm->mmap_sem); + vma =find_vma (mm, start);if (!vma)return -ENOMEM ;if (start < vma->vm_start) {
@@ -818,6 +818,7 @@ static long madvise_remove(struct vm_area_struct *vma,loff_t offset;int error;struct file *f; +struct mm_struct *mm = vma->vm_mm; *prev =NULL ;/* tell sys_madvise we drop mmap_sem */
@@ -845,13 +846,13 @@ static long madvise_remove(struct vm_area_struct *vma,get_file (f);if (userfaultfd_remove (vma, start, end)) {/* mmap_sem was not released by userfaultfd_remove() */ -up_read (¤t->mm->mmap_sem); +up_read (&mm->mmap_sem); } error =vfs_fallocate (f,FALLOC_FL_PUNCH_HOLE |FALLOC_FL_KEEP_SIZE , offset, end - start);fput (f); -down_read (¤t->mm->mmap_sem); +down_read (&mm->mmap_sem);return error; }
@@ -1044,7 +1045,8 @@ madvise_behavior_valid(int behavior)* -EBADF - map exists, but area maps something that isn't a file. * -EAGAIN - a kernel resource was temporarily unavailable. */ -int do_madvise (unsigned long start,size_t len_in,int behavior) +int do_madvise (struct task_struct *task,unsigned long start, +size_t len_in,int behavior) {unsigned long end, tmp;struct vm_area_struct *vma, *prev;
@@ -1053,6 +1055,7 @@ int do_madvise(unsigned long start, size_t len_in, int behavior)int write;size_t len;struct blk_plug plug; +struct mm_struct *mm = task->mm; start =untagged_addr (start);
@@ -1082,10 +1085,10 @@ int do_madvise(unsigned long start, size_t len_in, int behavior) write =madvise_need_mmap_write (behavior);if (write) { -if (down_write_killable (¤t->mm->mmap_sem)) +if (down_write_killable (&mm->mmap_sem))return -EINTR ; }else { -down_read (¤t->mm->mmap_sem); +down_read (&mm->mmap_sem); }/*
@@ -1093,7 +1096,7 @@ int do_madvise(unsigned long start, size_t len_in, int behavior)* ranges, just ignore them, but return -ENOMEM at the end. * - different from the way of handling in mlock etc. */ - vma =find_vma_prev (current->mm, start, &prev); + vma =find_vma_prev (mm, start, &prev);if (vma && start > vma->vm_start) prev = vma;
@@ -1130,19 +1133,19 @@ int do_madvise(unsigned long start, size_t len_in, int behavior)if (prev) vma = prev->vm_next;else /* madvise_remove dropped mmap_sem */ - vma =find_vma (current->mm, start); + vma =find_vma (mm, start); } out:blk_finish_plug (&plug);if (write) -up_write (¤t->mm->mmap_sem); +up_write (&mm->mmap_sem);else -up_read (¤t->mm->mmap_sem); +up_read (&mm->mmap_sem);return error; } SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior) { -return do_madvise (start, len_in, behavior); +return do_madvise (current, start, len_in, behavior); }
Signature · 1 line
2.25.0.225.g125e21ebc7-goog
PATCH v4 2/8 mm: introduce external memory hinting API
In reply to Minchan KimThere is usecase that System Management Software(SMS) want to give a memory hint like MADV_[COLD|PAGEEOUT] to other processes and in the case of Android, it is the ActivityManagerService.
It's similar in spirit to madvise(MADV_WONTNEED), but the information required to make the reclaim decision is not known to the app. Instead, it is known to the centralized userspace daemon(ActivityManagerService), and that daemon must be able to initiate reclaim on its own without any app involvement.
To solve the issue, this patch introduces a new syscall process_madvise(2). It uses pidfd of an external process to give the hint.
int process_madvise(int pidfd, void *addr, size_t length, int advise,
unsigned long flag);
Since it could affect other process's address range, only privileged process(CAP_SYS_PTRACE) or something else(e.g., being the same UID) gives it the right to ptrace the process could use it successfully. The flag argument is reserved for future use if we need to extend the API.
I think supporting all hints madvise has/will supported/support to process_madvise is rather risky. Because we are not sure all hints make sense from external process and implementation for the hint may rely on the caller being in the current context so it could be error-prone. Thus, I just limited hints as MADV_[COLD|PAGEOUT] in this patch.
If someone want to add other hints, we could hear hear the usecase and review it for each hint. It's safer for maintenance rather than introducing a buggy syscall but hard to fix it later.
Q.1 - Why does any external entity have better knowledge?
Quote from Sandeep
"For Android, every application (including the special SystemServer) are forked from Zygote. The reason of course is to share as many libraries and classes between the two as possible to benefit from the preloading during boot.
After applications start, (almost) all of the APIs end up calling into this SystemServer process over IPC (binder) and back to the application.
In a fully running system, the SystemServer monitors every single process periodically to calculate their PSS / RSS and also decides which process is "important" to the user for interactivity.
So, because of how these processes start _and_ the fact that the SystemServer is looping to monitor each process, it does tend to *know* which address range of the application is not used / useful.
Besides, we can never rely on applications to clean things up themselves. We've had the "hey app1, the system is low on memory, please trim your memory usage down" notifications for a long time[1]. They rely on applications honoring the broadcasts and very few do.
So, if we want to avoid the inevitable killing of the application and restarting it, some way to be able to tell the OS about unimportant memory in these applications will be useful.
- ssp
Q.2 - How to guarantee the race(i.e., object validation) between when giving a hint from an external process and get the hint from the target process?
process_madvise operates on the target process's address space as it exists at the instant that process_madvise is called. If the space target process can run between the time the process_madvise process inspects the target process address space and the time that process_madvise is actually called, process_madvise may operate on memory regions that the calling process does not expect. It's the responsibility of the process calling process_madvise to close this race condition. For example, the calling process can suspend the target process with ptrace, SIGSTOP, or the freezer cgroup so that it doesn't have an opportunity to change its own address space before process_madvise is called. Another option is to operate on memory regions that the caller knows a priori will be unchanged in the target process. Yet another option is to accept the race for certain process_madvise calls after reasoning that mistargeting will do no harm. The suggested API itself does not provide synchronization. It also apply other APIs like move_pages, process_vm_write.
The race isn't really a problem though. Why is it so wrong to require that callers do their own synchronization in some manner? Nobody objects to write(2) merely because it's possible for two processes to open the same file and clobber each other's writes --- instead, we tell people to use flock or something. Think about mmap. It never guarantees newly allocated address space is still valid when the user tries to access it because other threads could unmap the memory right before. That's where we need synchronization by using other API or design from userside. It shouldn't be part of API itself. If someone needs more fine-grained synchronization rather than process level, there were two ideas suggested - cookie[2] and anon-fd[3]. Both are applicable via using last reserved argument of the API but I don't think it's necessary right now since we have already ways to prevent the race so don't want to add additional complexity with more fine-grained optimization model.
To make the API extend, it reserved an unsigned long as last argument so we could support it in future if someone really needs it.
Q.3 - Why doesn't ptrace work?
Injecting an madvise in the target process using ptrace would not work for us because such injected madvise would have to be executed by the target process, which means that process would have to be runnable and that creates the risk of the abovementioned race and hinting a wrong VMA. Furthermore, we want to act the hint in caller's context, not calle because calle is usually limited in cpuset/cgroups or even freezed state so they can't act by themselves quick enough, which causes more thrashing/kill. It doesn't work if the target process are ptraced(e.g., strace, debugger, minidump) because a process can have at most one ptracer.
[1] https://developer.android.com/topic/performance/memory"
[2] process_getinfo for getting the cookie which is updated whenever
vma of process address layout are changed - Daniel Colascione
- https://lore.kernel.org/lkml/[email protected]/T/#m7694416fd179b2066a2c62b5b139b14e3894e224
[3] anonymous fd which is used for the object(i.e., address range)
validation - Michal Hocko
- https://lore.kernel.org/lkml/[email protected]/
--- arch/alpha/kernel/syscalls/syscall.tbl | 1 + arch/arm/tools/syscall.tbl | 1 + arch/arm64/include/asm/unistd.h | 2 +- arch/arm64/include/asm/unistd32.h | 2 + arch/ia64/kernel/syscalls/syscall.tbl | 1 + arch/m68k/kernel/syscalls/syscall.tbl | 1 + arch/microblaze/kernel/syscalls/syscall.tbl | 1 + arch/mips/kernel/syscalls/syscall_n32.tbl | 1 + arch/mips/kernel/syscalls/syscall_n64.tbl | 1 + arch/parisc/kernel/syscalls/syscall.tbl | 1 + arch/powerpc/kernel/syscalls/syscall.tbl | 1 + arch/s390/kernel/syscalls/syscall.tbl | 1 + arch/sh/kernel/syscalls/syscall.tbl | 1 + arch/sparc/kernel/syscalls/syscall.tbl | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/xtensa/kernel/syscalls/syscall.tbl | 1 + include/linux/syscalls.h | 2 + include/uapi/asm-generic/unistd.h | 4 +- kernel/sys_ni.c | 1 + mm/madvise.c | 64 +++++++++++++++++++++ 21 files changed, 88 insertions(+), 2 deletions(-)
arch/alpha/kernel/syscalls/syscall.tbl +1 -0
@@ -477,3 +477,4 @@ # 545 reserved for clone3 547 common openat2 sys_openat2 548 common pidfd_getfd sys_pidfd_getfd +549 common process_madvise sys_process_madvise
arch/arm/tools/syscall.tbl +1 -0
@@ -451,3 +451,4 @@ 435 common clone3 sys_clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise
arch/arm64/include/asm/unistd.h +1 -1
@@ -38,7 +38,7 @@#define __ARM_NR_compat_set_tls (__ARM_NR_COMPAT_BASE + 5)#define __ARM_NR_COMPAT_END (__ARM_NR_COMPAT_BASE + 0x800) -#define __NR_compat_syscalls 439 +#define __NR_compat_syscalls 440#endif #define __ARCH_WANT_SYS_CLONE
arch/arm64/include/asm/unistd32.h +2 -0
@@ -883,6 +883,8 @@ __SYSCALL(__NR_clone3, sys_clone3) __SYSCALL(__NR_openat2, sys_openat2)#define __NR_pidfd_getfd 438 __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd) +#define __NR_process_madvise 439 +__SYSCALL (__NR_process_madvise, process_madvise)/* * Please add new compat syscalls above this comment and update
arch/ia64/kernel/syscalls/syscall.tbl +1 -0
@@ -358,3 +358,4 @@ # 435 reserved for clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise
arch/m68k/kernel/syscalls/syscall.tbl +1 -0
@@ -437,3 +437,4 @@ 435 common clone3 __sys_clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise
arch/microblaze/kernel/syscalls/syscall.tbl +1 -0
@@ -443,3 +443,4 @@ 435 common clone3 sys_clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise
arch/mips/kernel/syscalls/syscall_n32.tbl +1 -0
@@ -376,3 +376,4 @@ 435 n32 clone3 __sys_clone3 437 n32 openat2 sys_openat2 438 n32 pidfd_getfd sys_pidfd_getfd +439 n32 process_madvise sys_process_madvise
arch/mips/kernel/syscalls/syscall_n64.tbl +1 -0
@@ -352,3 +352,4 @@ 435 n64 clone3 __sys_clone3 437 n64 openat2 sys_openat2 438 n64 pidfd_getfd sys_pidfd_getfd +439 n64 process_madvise sys_process_madvise
arch/parisc/kernel/syscalls/syscall.tbl +1 -0
@@ -435,3 +435,4 @@ 435 common clone3 sys_clone3_wrapper 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise
arch/powerpc/kernel/syscalls/syscall.tbl +1 -0
@@ -519,3 +519,4 @@ 435 nospu clone3 ppc_clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise
arch/s390/kernel/syscalls/syscall.tbl +1 -0
@@ -440,3 +440,4 @@ 435 common clone3 sys_clone3 sys_clone3 437 common openat2 sys_openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise sys_process_madvise
arch/sh/kernel/syscalls/syscall.tbl +1 -0
@@ -440,3 +440,4 @@ # 435 reserved for clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise
arch/sparc/kernel/syscalls/syscall.tbl +1 -0
@@ -483,3 +483,4 @@ # 435 reserved for clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
@@ -442,3 +442,4 @@ 435 i386 clone3 sys_clone3 __ia32_sys_clone3 437 i386 openat2 sys_openat2 __ia32_sys_openat2 438 i386 pidfd_getfd sys_pidfd_getfd __ia32_sys_pidfd_getfd +439 i386 process_madvise sys_process_madvise __ia32_sys_process_madvise
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -359,6 +359,7 @@ 435 common clone3 __x64_sys_clone3/ptregs 437 common openat2 __x64_sys_openat2 438 common pidfd_getfd __x64_sys_pidfd_getfd +439 common process_madvise __x64_sys_process_madvise # # x32-specific system call numbers start at 512 to avoid cache impact
arch/xtensa/kernel/syscalls/syscall.tbl +1 -0
@@ -408,3 +408,4 @@ 435 common clone3 sys_clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise
include/linux/syscalls.h +2 -0
@@ -876,6 +876,8 @@ asmlinkage long sys_munlockall(void);asmlinkage long sys_mincore (unsigned long start,size_t len,unsigned char __user * vec);asmlinkage long sys_madvise (unsigned long start,size_t len,int behavior); +asmlinkage long sys_process_madvise (int pidfd,unsigned long start, +size_t len,int behavior,unsigned long flags);asmlinkage long sys_remap_file_pages (unsigned long start,unsigned long size,unsigned long prot,unsigned long pgoff,unsigned long flags);
include/uapi/asm-generic/unistd.h +3 -1
@@ -855,9 +855,11 @@ __SYSCALL(__NR_clone3, sys_clone3) __SYSCALL(__NR_openat2, sys_openat2)#define __NR_pidfd_getfd 438 __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd) +#define __NR_pidfd_getfd 439 +__SYSCALL(__NR_process_madvise, sys_process_madvise)#undef __NR_syscalls -#define __NR_syscalls 439 +#define __NR_syscalls 440/* * 32 bit systems traditionally used different
kernel/sys_ni.c +1 -0
@@ -280,6 +280,7 @@ COND_SYSCALL(mlockall);COND_SYSCALL (munlockall);COND_SYSCALL (mincore);COND_SYSCALL (madvise); +COND_SYSCALL (process_madvise);COND_SYSCALL (remap_file_pages);COND_SYSCALL (mbind);COND_SYSCALL_COMPAT (mbind);
mm/madvise.c +64 -0
85 lines not on this page · raw message
Signature · 1 line
2.25.0.225.g125e21ebc7-goog
PATCH v4 3/8 mm: validate mm in do_madvise
In reply to Minchan KimOleg pointed out mm could be nulllified right after mm_access succeeds. This patch validates it before the using.
--- mm/madvise.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
mm/madvise.c +5 -1
@@ -1073,7 +1073,11 @@ int do_madvise(struct task_struct *task, unsigned long start,int write;size_t len;struct blk_plug plug; -struct mm_struct *mm = task->mm; +struct mm_struct *mm =READ_ONCE (task->mm); + +/* task can exit and nullify its ->mm right after mm_access() */ +if (!mm) +return -ESRCH ; start =untagged_addr (start);
Signature · 1 line
2.25.0.225.g125e21ebc7-goog
PATCH v4 4/8 mm: check fatal signal pending of target process
In reply to Minchan KimBail out to prevent unnecessary CPU overhead if target process ha pending fatal signal during MADV_COLD| MADV_PAGEOUT operation.
--- mm/madvise.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)
mm/madvise.c +19 -8
@@ -36,6 +36,7 @@struct madvise_walk_private {struct mmu_gather *tlb;bool pageout; +struct task_struct *task; };/*
@@ -316,6 +317,9 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,if (fatal_signal_pending (current))return -EINTR ; +if (fatal_signal_pending (private->task)) +return -EINTR ; +#ifdef CONFIG_TRANSPARENT_HUGEPAGE if (pmd_trans_huge (*pmd)) {pmd_t orig_pmd;
@@ -471,12 +475,14 @@ static const struct mm_walk_ops cold_walk_ops = { };static void madvise_cold_page_range (struct mmu_gather *tlb, +struct task_struct *task,struct vm_area_struct *vma,unsigned long addr,unsigned long end) {struct madvise_walk_private walk_private = { .pageout = false, .tlb = tlb, + .task = task, };tlb_start_vma (tlb, vma);
@@ -484,7 +490,8 @@ static void madvise_cold_page_range(struct mmu_gather *tlb,tlb_end_vma (tlb, vma); } -static long madvise_cold (struct vm_area_struct *vma, +static long madvise_cold (struct task_struct *task, +struct vm_area_struct *vma,struct vm_area_struct **prev,unsigned long start_addr,unsigned long end_addr) {
@@ -497,19 +504,21 @@ static long madvise_cold(struct vm_area_struct *vma,lru_add_drain ();tlb_gather_mmu (&tlb, mm, start_addr, end_addr); -madvise_cold_page_range (&tlb, vma, start_addr, end_addr); +madvise_cold_page_range (&tlb, task, vma, start_addr, end_addr);tlb_finish_mmu (&tlb, start_addr, end_addr);return 0 ; }static void madvise_pageout_page_range (struct mmu_gather *tlb, +struct task_struct *task,struct vm_area_struct *vma,unsigned long addr,unsigned long end) {struct madvise_walk_private walk_private = { .pageout = true, .tlb = tlb, + .task = task, };tlb_start_vma (tlb, vma);
@@ -533,7 +542,8 @@ static inline bool can_do_pageout(struct vm_area_struct *vma)inode_permission (file_inode (vma->vm_file),MAY_WRITE ) ==0 ; } -static long madvise_pageout (struct vm_area_struct *vma, +static long madvise_pageout (struct task_struct *task, +struct vm_area_struct *vma,struct vm_area_struct **prev,unsigned long start_addr,unsigned long end_addr) {
@@ -549,7 +559,7 @@ static long madvise_pageout(struct vm_area_struct *vma,lru_add_drain ();tlb_gather_mmu (&tlb, mm, start_addr, end_addr); -madvise_pageout_page_range (&tlb, vma, start_addr, end_addr); +madvise_pageout_page_range (&tlb, task, vma, start_addr, end_addr);tlb_finish_mmu (&tlb, start_addr, end_addr);return 0 ;
@@ -927,7 +937,8 @@ static int madvise_inject_error(int behavior,#endif static long -madvise_vma (struct vm_area_struct *vma,struct vm_area_struct **prev, +madvise_vma (struct task_struct *task,struct vm_area_struct *vma, +struct vm_area_struct **prev,unsigned long start,unsigned long end,int behavior) {switch (behavior) {
@@ -936,9 +947,9 @@ madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,case MADV_WILLNEED :return madvise_willneed (vma, prev, start, end);case MADV_COLD : -return madvise_cold (vma, prev, start, end); +return madvise_cold (task, vma, prev, start, end);case MADV_PAGEOUT : -return madvise_pageout (vma, prev, start, end); +return madvise_pageout (task, vma, prev, start, end);case MADV_FREE :case MADV_DONTNEED :return madvise_dontneed_free (vma, prev, start, end, behavior);
@@ -1143,7 +1154,7 @@ int do_madvise(struct task_struct *task, unsigned long start, tmp = end;/* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */ - error =madvise_vma (vma, &prev, start, tmp, behavior); + error =madvise_vma (task, vma, &prev, start, tmp, behavior);if (error) goto out; start = tmp;
Signature · 1 line
2.25.0.225.g125e21ebc7-goog
PATCH v4 5/8 mm/madvise: employ mmget_still_valid for write lock
In reply to Minchan KimFrom: Oleksandr Natalenko <[email protected]>
Do the very same trick as we already do since 04f5866e41fb. KSM hints will require locking mmap_sem for write since they modify vm_flags, so for remote KSM hinting this additional check is needed.
--- mm/madvise.c | 3 +++ 1 file changed, 3 insertions(+)
mm/madvise.c +3 -0
@@ -1120,6 +1120,8 @@ int do_madvise(struct task_struct *task, unsigned long start,if (write) {if (down_write_killable (&mm->mmap_sem))return -EINTR ; +if (current->mm != mm && !mmget_still_valid (mm)) + goto skip_mm; }else {down_read (&mm->mmap_sem); }
@@ -1170,6 +1172,7 @@ int do_madvise(struct task_struct *task, unsigned long start, } out:blk_finish_plug (&plug); +skip_mm:if (write)up_write (&mm->mmap_sem);else
Signature · 1 line
2.25.0.225.g125e21ebc7-goog
PATCH v4 6/8 mm/madvise: allow KSM hints for remote API
In reply to Minchan KimFrom: Oleksandr Natalenko <[email protected]>
It all began with the fact that KSM works only on memory that is marked by madvise(). And the only way to get around that is to either:
* use LD_PRELOAD; or
* patch the kernel with something like UKSM or PKSM.
(i skip ptrace can of worms here intentionally)
To overcome this restriction, lets employ a new remote madvise API. This can be used by some small userspace helper daemon that will do auto-KSM job for us.
I think of two major consumers of remote KSM hints:
* hosts, that run containers, especially similar ones and especially in
a trusted environment, sharing the same runtime like Node.js;
* heavy applications, that can be run in multiple instances, not limited to opensource ones like Firefox, but also those that cannot be modified since they are binary-only and, maybe, statically linked.
Speaking of statistics, more numbers can be found in the very first submission, that is related to this one [1]. For my current setup with two Firefox instances I get 100 to 200 MiB saved for the second instance depending on the amount of tabs.
1 FF instance with 15 tabs:
$ echo "$(cat /sys/kernel/mm/ksm/pages_sharing) * 4 / 1024" | bc 410
2 FF instances, second one has 12 tabs (all the tabs are different):
$ echo "$(cat /sys/kernel/mm/ksm/pages_sharing) * 4 / 1024" | bc 592
At the very moment I do not have specific numbers for containerised workload, but those should be comparable in case the containers share similar/same runtime.
[1] https://lore.kernel.org/patchwork/patch/1012142/
--- mm/madvise.c | 4 ++++ 1 file changed, 4 insertions(+)
mm/madvise.c +4 -0
@@ -1002,6 +1002,10 @@ process_madvise_behavior_valid(int behavior)switch (behavior) {case MADV_COLD :case MADV_PAGEOUT : +#ifdef CONFIG_KSM +case MADV_MERGEABLE : +case MADV_UNMERGEABLE : +#endif return true;default :return false;
Signature · 1 line
2.25.0.225.g125e21ebc7-goog
PATCH v4 7/8 pid: export pidfd_get_pid
In reply to Minchan Kimprocess_madvise syscall needs pidfd_get_pid function to translate pidfd to pid so this patch exports the function.
--- include/linux/pid.h | 1 + kernel/exit.c | 17 ----------------- kernel/pid.c | 17 +++++++++++++++++ 3 files changed, 18 insertions(+), 17 deletions(-)
include/linux/pid.h +1 -0
@@ -75,6 +75,7 @@ extern const struct file_operations pidfd_fops;struct file ;extern struct pid *pidfd_pid (const struct file *file); +extern struct pid *pidfd_get_pid (unsigned int fd);static inline struct pid *get_pid (struct pid *pid) {
kernel/exit.c +0 -17
@@ -1470,23 +1470,6 @@ static long do_wait(struct wait_opts *wo)return retval; } -static struct pid *pidfd_get_pid (unsigned int fd) -{ -struct fd f; -struct pid *pid; - - f =fdget (fd); -if (!f.file) -return ERR_PTR (-EBADF ); - - pid =pidfd_pid (f.file); -if (!IS_ERR (pid)) -get_pid (pid); - -fdput (f); -return pid; -} -static long kernel_waitid (int which,pid_t upid,struct waitid_info *infop,int options,struct rusage *ru) {
kernel/pid.c +17 -0
@@ -496,6 +496,23 @@ struct pid *find_ge_pid(int nr, struct pid_namespace *ns)return idr_get_next (&ns->idr, &nr); } +struct pid *pidfd_get_pid (unsigned int fd) +{ +struct fd f; +struct pid *pid; + + f =fdget (fd); +if (!f.file) +return ERR_PTR (-EBADF ); + + pid =pidfd_pid (f.file); +if (!IS_ERR (pid)) +get_pid (pid); + +fdput (f); +return pid; +} +/** * pidfd_create() - Create a new pid file descriptor. *
Signature · 1 line
2.25.0.225.g125e21ebc7-goog
PATCH v4 8/8 mm: support both pid and pidfd for process_madvise
In reply to Minchan KimThere is a demand[1] to support pid as well pidfd for process_madvise to reduce unnecessary syscall to get pidfd if the user has control of the target process(ie, they could guarantee the process is not gone or pid is not reused. Or, it might be okay to give a hint to wrong process).
This patch aims for supporting both options like waitid(2). So, the syscall is currently,
int process_madvise(int which, pid_t pid, void *addr, size_t length, int advise, unsigned long flag);
@which is actually idtype_t for userspace libray and currently, it supports P_PID and P_PIDFD.
[1] https://lore.kernel.org/linux-mm/[email protected]/
--- include/linux/syscalls.h | 3 ++- mm/madvise.c | 34 ++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-)
include/linux/syscalls.h +2 -1
@@ -876,7 +876,8 @@ asmlinkage long sys_munlockall(void);asmlinkage long sys_mincore (unsigned long start,size_t len,unsigned char __user * vec);asmlinkage long sys_madvise (unsigned long start,size_t len,int behavior); -asmlinkage long sys_process_madvise (int pidfd,unsigned long start, + +asmlinkage long sys_process_madvise (int which,pid_t pid,unsigned long start,size_t len,int behavior,unsigned long flags); asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size, unsigned long prot, unsigned long pgoff,
mm/madvise.c +22 -12
@@ -1190,11 +1190,10 @@ SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)return do_madvise (current, start, len_in, behavior); } -SYSCALL_DEFINE5(process_madvise, int, pidfd, unsigned long, start, +SYSCALL_DEFINE6(process_madvise, int, which, pid_t, upid, unsigned long, start, size_t, len_in, int, behavior, unsigned long, flags) {int ret; -struct fd f;struct pid *pid;struct task_struct *task;struct mm_struct *mm;
@@ -1205,20 +1204,31 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, unsigned long, start,if (!process_madvise_behavior_valid (behavior))return -EINVAL ; - f =fdget (pidfd); -if (!f.file) -return -EBADF ; +switch (which) { +case P_PID : +if (upid <=0 ) +return -EINVAL ; + + pid =find_get_pid (upid); +if (!pid) +return -ESRCH ; +break ; +case P_PIDFD : +if (upid <0 ) +return -EINVAL ; - pid =pidfd_pid (f.file); -if (IS_ERR (pid)) { - ret =PTR_ERR (pid); - goto fdput; + pid =pidfd_get_pid (upid); +if (IS_ERR (pid)) +return PTR_ERR (pid); +break ; +default : +return -EINVAL ; } task =get_pid_task (pid,PIDTYPE_PID );if (!task) { ret = -ESRCH ; - goto fdput; + goto put_pid; } mm =mm_access (task,PTRACE_MODE_ATTACH_FSCREDS );
@@ -1231,7 +1241,7 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, unsigned long, start,mmput (mm); release_task:put_task_struct (task); -fdput: -fdput (f); +put_pid: +put_pid (pid);return ret; }
Signature · 1 line
2.25.0.225.g125e21ebc7-goog
Re: [PATCH v4 1/8] mm: pass task to do_madvise
In reply to Minchan KimQuoted 46 lines · Minchan Kim
In upcoming patches,
do_madvisewill be called from external process context so it shouldn't asssume "current" is always hinted process'stask_struct. Thus, let's get themm_structfromvma->vm_mm, not current because vma is always hinted process's one. And let's pass *current* as new task argument ofdo_madviseso it shouldn't change existing behavior.Signed-off-by Minchan Kim <[email protected]>--- fs/io_uring.c | 2 +- include/linux/mm.h | 3 ++- mm/madvise.c | 37 ++++++++++++++++++++----------------- 3 files changed, 23 insertions(+), 19 deletions(-)fs/io_uring.c +1
-1@@ -2736,7 +2736,7 @@ static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,if (force_nonblock)return -EAGAIN ; - ret =do_madvise (ma->addr, ma->len, ma->advice); + ret =do_madvise (current, ma->addr, ma->len, ma->advice);if (ret <0 )req_set_fail_links (req);io_cqring_add_event (req, ret);include/linux/mm.h +2
-1@@ -2323,7 +2323,8 @@ extern int __do_munmap(struct mm_struct *, unsigned long, size_t, struct list_head *uf, bool downgrade);extern int do_munmap (struct mm_struct *,unsigned long ,size_t ,struct list_head *uf); -extern int do_madvise (unsigned long start,size_t len_in,int behavior); +extern int do_madvise (struct task_struct *task,unsigned long start, +size_t len_in,int behavior); static inline unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,mm/madvise.c +0
-0
mm/madvise.c static long madvise_willneed(struct vm_area_struct *vma, · line 262
Quoted 7 lines
mm/madvise.c +1
-0@@ -256,6 +256,7 @@ static long madvise_willneed(struct vm_area_struct *vma, {struct file *file = vma->vm_file;loff_t offset; +struct mm_struct *mm = vma->vm_mm; *prev = vma;#ifdef CONFIG_SWAP
I would probably move the declaration of the mm variable to the top just so you don't have the large "offset" valley between the two long variable declarations.
Quoted 16 lines
mm/madvise.c +2
-2@@ -288,12 +289,12 @@ static long madvise_willneed(struct vm_area_struct *vma,*/ *prev =NULL ;/* tell sys_madvise we drop mmap_sem */ get_file (file); -up_read (¤t->mm->mmap_sem); +up_read (&mm->mmap_sem); offset = (loff_t)(start - vma->vm_start) + ((loff_t )vma->vm_pgoff <<PAGE_SHIFT );vfs_fadvise (file, offset, end - start,POSIX_FADV_WILLNEED );fput (file); -down_read (¤t->mm->mmap_sem); +down_read (&mm->mmap_sem);return 0 ; }
mm/madvise.c static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, · line 681
Quoted 10 lines
mm/madvise.c +1
-3@@ -674,9 +675,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, } out:if (nr_swap) { -if (current->mm == mm) -sync_mm_rss (mm); - +sync_mm_rss (mm);add_mm_counter (mm,MM_SWAPENTS , nr_swap); }arch_leave_lazy_mmu_mode ();
This seems like it is taking things in the opposite direction of the other changes. sync_mm_rss will operate on current if I am not mistaken. I don't think you would want to add the stats from current to the stats of the task you are updating.
It might make sense to add a new function that would allow you to sync the remote task stats by creaing a version of sync_mm_rss that also takes a task pointer.
Quoted 9 lines
mm/madvise.c +1
-0@@ -756,6 +755,7 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, 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 ;
mm/madvise.c static long madvise_dontneed_free(struct vm_area_struct *vma, · line 770
Quoted 10 lines
mm/madvise.c +2
-2@@ -763,8 +763,8 @@ static long madvise_dontneed_free(struct vm_area_struct *vma,if (!userfaultfd_remove (vma, start, end)) { *prev =NULL ;/* mmap_sem has been dropped, prev is stale */ -down_read (¤t->mm->mmap_sem); - vma =find_vma (current->mm, start); +down_read (&mm->mmap_sem); + vma =find_vma (mm, start);if (!vma)return -ENOMEM ;if (start < vma->vm_start) {
This piece of code has me wondering if it is valid to be using vma->mm at the start of the function. I assume we are probably safe since we read the mm value before the semaphore was released in userfaultfd_remove. It might make more sense to just pass the task to the function and use task->mm-
Quoted 1 line
mmap_seminstead.
It might be simpler, safer, and easier to review to just go through and add the task struct as needed and then simply replace references to current->mm with task->mm.
Quoted 88 lines
mm/madvise.c +13
-10@@ -818,6 +818,7 @@ static long madvise_remove(struct vm_area_struct *vma,loff_t offset;int error;struct file *f; +struct mm_struct *mm = vma->vm_mm; *prev =NULL ;/* tell sys_madvise we drop mmap_sem */ @@ -845,13 +846,13 @@ static long madvise_remove(struct vm_area_struct *vma,get_file (f);if (userfaultfd_remove (vma, start, end)) {/* mmap_sem was not released by userfaultfd_remove() */ -up_read (¤t->mm->mmap_sem); +up_read (&mm->mmap_sem); } error =vfs_fallocate (f,FALLOC_FL_PUNCH_HOLE |FALLOC_FL_KEEP_SIZE , offset, end - start);fput (f); -down_read (¤t->mm->mmap_sem); +down_read (&mm->mmap_sem);return error; }@@ -1044,7 +1045,8 @@ madvise_behavior_valid(int behavior)* -EBADF - map exists, but area maps something that isn't a file. * -EAGAIN - a kernel resource was temporarily unavailable. */ -int do_madvise (unsigned long start,size_t len_in,int behavior) +int do_madvise (struct task_struct *task,unsigned long start, +size_t len_in,int behavior) {unsigned long end, tmp;struct vm_area_struct *vma, *prev;@@ -1053,6 +1055,7 @@ int do_madvise(unsigned long start, size_t len_in, int behavior)int write;size_t len;struct blk_plug plug; +struct mm_struct *mm = task->mm; start =untagged_addr (start);@@ -1082,10 +1085,10 @@ int do_madvise(unsigned long start, size_t len_in, int behavior) write =madvise_need_mmap_write (behavior);if (write) { -if (down_write_killable (¤t->mm->mmap_sem)) +if (down_write_killable (&mm->mmap_sem))return -EINTR ; }else { -down_read (¤t->mm->mmap_sem); +down_read (&mm->mmap_sem); }/* @@ -1093,7 +1096,7 @@ int do_madvise(unsigned long start, size_t len_in, int behavior)* ranges, just ignore them, but return -ENOMEM at the end. * - different from the way of handling in mlock etc. */ - vma =find_vma_prev (current->mm, start, &prev); + vma =find_vma_prev (mm, start, &prev);if (vma && start > vma->vm_start) prev = vma;@@ -1130,19 +1133,19 @@ int do_madvise(unsigned long start, size_t len_in, int behavior)if (prev) vma = prev->vm_next;else /* madvise_remove dropped mmap_sem */ - vma =find_vma (current->mm, start); + vma =find_vma (mm, start); } out:blk_finish_plug (&plug);if (write) -up_write (¤t->mm->mmap_sem); +up_write (&mm->mmap_sem);else -up_read (¤t->mm->mmap_sem); +up_read (&mm->mmap_sem);return error; } SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior) { -return do_madvise (start, len_in, behavior); +return do_madvise (current, start, len_in, behavior); }
Re: [PATCH v4 7/8] pid: export pidfd_get_pid
In reply to Minchan KimQuoted 5 lines · Minchan Kim
process_madvisesyscall needspidfd_get_pidfunction to translate pidfd to pid so this patch exports the function.Cc Christian Brauner <[email protected]>Signed-off-by Minchan Kim <[email protected]>
I think you might have misunderstood my earlier comments. This should be patch 2 in your set. What is patch 8 should be folded into you existing patch 2 and become patch 3 with the rest of your patches shifted by 1 since you are reordering them.
Otherwise the code itself appears to not have changed anything so it looks fine to me.
Quoted 74 lines
--- include/linux/pid.h | 1 + kernel/exit.c | 17 ----------------- kernel/pid.c | 17 +++++++++++++++++ 3 files changed, 18 insertions(+), 17 deletions(-)include/linux/pid.h +1
-0@@ -75,6 +75,7 @@ extern const struct file_operations pidfd_fops;struct file ;extern struct pid *pidfd_pid (const struct file *file); +extern struct pid *pidfd_get_pid (unsigned int fd);static inline struct pid *get_pid (struct pid *pid) {kernel/exit.c +0
-17@@ -1470,23 +1470,6 @@ static long do_wait(struct wait_opts *wo)return retval; } -static struct pid *pidfd_get_pid (unsigned int fd) -{ -struct fd f; -struct pid *pid; - - f =fdget (fd); -if (!f.file) -return ERR_PTR (-EBADF ); - - pid =pidfd_pid (f.file); -if (!IS_ERR (pid)) -get_pid (pid); - -fdput (f); -return pid; -} -static long kernel_waitid (int which,pid_t upid,struct waitid_info *infop,int options,struct rusage *ru) {kernel/pid.c +17
-0@@ -496,6 +496,23 @@ struct pid *find_ge_pid(int nr, struct pid_namespace *ns)return idr_get_next (&ns->idr, &nr); } +struct pid *pidfd_get_pid (unsigned int fd) +{ +struct fd f; +struct pid *pid; + + f =fdget (fd); +if (!f.file) +return ERR_PTR (-EBADF ); + + pid =pidfd_pid (f.file); +if (!IS_ERR (pid)) +get_pid (pid); + +fdput (f); +return pid; +} +/** * pidfd_create() - Create a new pid file descriptor. *
Re: [PATCH v4 8/8] mm: support both pid and pidfd for process_madvise
In reply to Minchan KimQuoted 12 lines · Minchan Kim
There is a demand[1] to support pid as well pidfd for
process_madviseto reduce unnecessary syscall to get pidfd if the user has control of the target process(ie, they could guarantee the process is not gone or pid is not reused. Or, it might be okay to give a hint to wrong process).This patch aims for supporting both options like waitid(2). So, the syscall is currently,
int process_madvise(int which, pid_t pid, void *addr, size_t length, int advise, unsigned long flag);
Quoted 8 lines
@which is actually
idtype_tfor userspace libray and currently, it supportsP_PIDandP_PIDFD.Cc Christian Brauner <[email protected]>Suggested-by Kirill Tkhai <[email protected]>Signed-off-by Minchan Kim <[email protected]>
So if you move patch 7 up before patch 2 you could squash this patch with your current patch 2 and drop one patch from your series. It would probably help to reduce the review overhead as well.
Jann Horn
reviewerRe: [PATCH v4 2/8] mm: introduce external memory hinting API
In reply to Minchan KimQuoted 2 lines · Minchan Kim
To solve the issue, this patch introduces a new syscall
process_madvise(2). It uses pidfd of an external process to give the hint.
[...]
mm/madvise.c SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior) · line 1208
Quoted 7 lines
mm/madvise.c +7
-0+ mm =mm_access (task,PTRACE_MODE_ATTACH_FSCREDS ); +if (IS_ERR_OR_NULL (mm)) { + ret =IS_ERR (mm) ?PTR_ERR (mm) : -ESRCH ; + goto release_task; + } + + ret =do_madvise (task, start, len_in, behavior);
When you're accessing another task, you should ensure that the other task doesn't gain new privileges by executing a setuid binary in the middle of being accessed. mm_access() does that for you; it holds the ->cred_guard_mutex while it is looking up the task's ->mm and doing the security check. mm_access() then returns you an mm pointer that you're allowed to access without worrying about such things; an mm_struct never gains privileges, since a setuid execution creates a fresh mm_struct. However, the task may still execute setuid binaries and such things.
This means that after you've looked up the mm with mm_access(), you have to actually *use* that pointer. You're not allowed to simply read task->mm yourself.
Therefore, I think you should:
- change patch 1/8 ("mm: pass task to do_madvise") to also pass an
mm_struct* to do_madvise (but keep the task_struct* for patch 4/8)
- in this patch, pass the mm_struct* from mm_access() into do_madvise()
- drop patch 3/8 ("mm: validate mm in do_madvise"); it just papers
over a symptom without addressing the underlying problem
Re: [PATCH v4 2/8] mm: introduce external memory hinting API
Hi Jann,
Quoted 26 lines · Jann Horn
Quoted 2 lines · Minchan Kim
To solve the issue, this patch introduces a new syscall
process_madvise(2). It uses pidfd of an external process to give the hint.[...]
mm/madvise.c SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior) · line 1208
Quoted 7 lines
mm/madvise.c +7
-0+ mm =mm_access (task,PTRACE_MODE_ATTACH_FSCREDS ); +if (IS_ERR_OR_NULL (mm)) { + ret =IS_ERR (mm) ?PTR_ERR (mm) : -ESRCH ; + goto release_task; + } + + ret =do_madvise (task, start, len_in, behavior);When you're accessing another task, you should ensure that the other task doesn't gain new privileges by executing a setuid binary in the middle of being accessed.
mm_access()does that for you; it holds the ->cred_guard_mutex while it is looking up the task's ->mm and doing the security check.mm_access()then returns you an mm pointer that you're allowed to access without worrying about such things; anmm_structnever gains privileges, since a setuid execution creates a freshmm_struct. However, the task may still execute setuid binaries and such things.This means that after you've looked up the mm with
mm_access(), you have to actually *use* that pointer. You're not allowed to simply readtask->mmyourself.
Quoted 7 lines
Therefore, I think you should:
- change patch 1/8 ("mm: pass task to
do_madvise") to also pass an
mm_struct* todo_madvise(but keep the task_struct* for patch 4/8)
- in this patch, pass the mm_struct* frommm_access()intodo_madvise()
- drop patch 3/8 ("mm: validate mm indo_madvise"); it just papers
over a symptom without addressing the underlying problem
Actually, it was what this patch series was doing until last version but I changed it to reduce just *a parameter* to do_madvise. And then, this time, I got a good advise I was not familiar. I will fix it again.
Thanks for the review!
Re: [PATCH v4 1/8] mm: pass task to do_madvise
In reply to Alexander DuyckQuoted 52 lines · Alexander Duyck
Quoted 51 lines · Minchan Kim
In upcoming patches,
do_madvisewill be called from external process context so it shouldn't asssume "current" is always hinted process'stask_struct. Thus, let's get themm_structfromvma->vm_mm, not current because vma is always hinted process's one. And let's pass *current* as new task argument ofdo_madviseso it shouldn't change existing behavior.Signed-off-by Minchan Kim <[email protected]>--- fs/io_uring.c | 2 +- include/linux/mm.h | 3 ++- mm/madvise.c | 37 ++++++++++++++++++++----------------- 3 files changed, 23 insertions(+), 19 deletions(-)fs/io_uring.c +1
-1@@ -2736,7 +2736,7 @@ static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,if (force_nonblock)return -EAGAIN ; - ret =do_madvise (ma->addr, ma->len, ma->advice); + ret =do_madvise (current, ma->addr, ma->len, ma->advice);if (ret <0 )req_set_fail_links (req);io_cqring_add_event (req, ret);include/linux/mm.h +2
-1@@ -2323,7 +2323,8 @@ extern int __do_munmap(struct mm_struct *, unsigned long, size_t, struct list_head *uf, bool downgrade);extern int do_munmap (struct mm_struct *,unsigned long ,size_t ,struct list_head *uf); -extern int do_madvise (unsigned long start,size_t len_in,int behavior); +extern int do_madvise (struct task_struct *task,unsigned long start, +size_t len_in,int behavior); static inline unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,mm/madvise.c +1
-0@@ -256,6 +256,7 @@ static long madvise_willneed(struct vm_area_struct *vma, {struct file *file = vma->vm_file;loff_t offset; +struct mm_struct *mm = vma->vm_mm;
Quoted 6 lines
mm/madvise.c static long madvise_willneed(struct vm_area_struct *vma, · line 262
Quoted 2 lines
mm/madvise.c +0
-0*prev = vma;#ifdef CONFIG_SWAP I would probably move the declaration of the mm variable to the top just so you don't have the large "offset" valley between the two long variable declarations.
No problem.
Quoted 33 lines
Quoted 17 lines
mm/madvise.c +2
-2@@ -288,12 +289,12 @@ static long madvise_willneed(struct vm_area_struct *vma,*/ *prev =NULL ;/* tell sys_madvise we drop mmap_sem */ get_file (file); -up_read (¤t->mm->mmap_sem); +up_read (&mm->mmap_sem); offset = (loff_t)(start - vma->vm_start) + ((loff_t )vma->vm_pgoff <<PAGE_SHIFT );vfs_fadvise (file, offset, end - start,POSIX_FADV_WILLNEED );fput (file); -down_read (¤t->mm->mmap_sem); +down_read (&mm->mmap_sem);return 0 ; }mm/madvise.c static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, · line 681
Quoted 10 lines
mm/madvise.c +1
-3@@ -674,9 +675,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, } out:if (nr_swap) { -if (current->mm == mm) -sync_mm_rss (mm); - +sync_mm_rss (mm);add_mm_counter (mm,MM_SWAPENTS , nr_swap); }arch_leave_lazy_mmu_mode ();This seems like it is taking things in the opposite direction of the other changes.
sync_mm_rsswill operate on current if I am not mistaken. I don't think you would want to add the stats from current to the stats of the task you are updating.
Quoted 3 lines
It might make sense to add a new function that would allow you to sync the remote task stats by creaing a version of
sync_mm_rssthat also takes a task pointer.
Sorry, that part was intended for other patch regardless of this patchset but squeezed into this version by my mistake. I wanted to remove those part from madvise_free because currently madvise_free is always done in current context. I will drop it in next revision since it's not related to this patchset.
Quoted 13 lines
Quoted 13 lines
mm/madvise.c +1
-0@@ -756,6 +755,7 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, 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 ;@@ -763,8 +763,8 @@ static long madvise_dontneed_free(struct vm_area_struct *vma,if (!userfaultfd_remove (vma, start, end)) { *prev =NULL ;/* mmap_sem has been dropped, prev is stale */
Quoted 13 lines
mm/madvise.c static long madvise_dontneed_free(struct vm_area_struct *vma, · line 770
Quoted 7 lines
mm/madvise.c +2
-2-down_read (¤t->mm->mmap_sem); - vma =find_vma (current->mm, start); +down_read (&mm->mmap_sem); + vma =find_vma (mm, start);if (!vma)return -ENOMEM ;if (start < vma->vm_start) {This piece of code has me wondering if it is valid to be using
vma->mmat the start of the function. I assume we are probably safe since we read the mm value before the semaphore was released inuserfaultfd_remove. It might make more sense to just pass the task to the function and use task->mm-Quoted 1 line
mmap_seminstead.
As Jann pointed out, we couldn't use task->mm once we verified it via access_mm. However, I believe vma->vm_mm is safe(Ccing Jann for double check).
Quoted 95 lines
It might be simpler, safer, and easier to review to just go through and add the task struct as needed and then simply replace references to
current->mmwithtask->mm.Quoted 88 lines
mm/madvise.c +13
-10@@ -818,6 +818,7 @@ static long madvise_remove(struct vm_area_struct *vma,loff_t offset;int error;struct file *f; +struct mm_struct *mm = vma->vm_mm; *prev =NULL ;/* tell sys_madvise we drop mmap_sem */ @@ -845,13 +846,13 @@ static long madvise_remove(struct vm_area_struct *vma,get_file (f);if (userfaultfd_remove (vma, start, end)) {/* mmap_sem was not released by userfaultfd_remove() */ -up_read (¤t->mm->mmap_sem); +up_read (&mm->mmap_sem); } error =vfs_fallocate (f,FALLOC_FL_PUNCH_HOLE |FALLOC_FL_KEEP_SIZE , offset, end - start);fput (f); -down_read (¤t->mm->mmap_sem); +down_read (&mm->mmap_sem);return error; }@@ -1044,7 +1045,8 @@ madvise_behavior_valid(int behavior)* -EBADF - map exists, but area maps something that isn't a file. * -EAGAIN - a kernel resource was temporarily unavailable. */ -int do_madvise (unsigned long start,size_t len_in,int behavior) +int do_madvise (struct task_struct *task,unsigned long start, +size_t len_in,int behavior) {unsigned long end, tmp;struct vm_area_struct *vma, *prev;@@ -1053,6 +1055,7 @@ int do_madvise(unsigned long start, size_t len_in, int behavior)int write;size_t len;struct blk_plug plug; +struct mm_struct *mm = task->mm; start =untagged_addr (start);@@ -1082,10 +1085,10 @@ int do_madvise(unsigned long start, size_t len_in, int behavior) write =madvise_need_mmap_write (behavior);if (write) { -if (down_write_killable (¤t->mm->mmap_sem)) +if (down_write_killable (&mm->mmap_sem))return -EINTR ; }else { -down_read (¤t->mm->mmap_sem); +down_read (&mm->mmap_sem); }/* @@ -1093,7 +1096,7 @@ int do_madvise(unsigned long start, size_t len_in, int behavior)* ranges, just ignore them, but return -ENOMEM at the end. * - different from the way of handling in mlock etc. */ - vma =find_vma_prev (current->mm, start, &prev); + vma =find_vma_prev (mm, start, &prev);if (vma && start > vma->vm_start) prev = vma;@@ -1130,19 +1133,19 @@ int do_madvise(unsigned long start, size_t len_in, int behavior)if (prev) vma = prev->vm_next;else /* madvise_remove dropped mmap_sem */ - vma =find_vma (current->mm, start); + vma =find_vma (mm, start); } out:blk_finish_plug (&plug);if (write) -up_write (¤t->mm->mmap_sem); +up_write (&mm->mmap_sem);else -up_read (¤t->mm->mmap_sem); +up_read (&mm->mmap_sem);return error; } SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior) { -return do_madvise (start, len_in, behavior); +return do_madvise (current, start, len_in, behavior); }
Re: [PATCH v4 7/8] pid: export pidfd_get_pid
In reply to Alexander DuyckHi Alexander,
Quoted 12 lines · Alexander Duyck
Quoted 5 lines · Minchan Kim
process_madvisesyscall needspidfd_get_pidfunction to translate pidfd to pid so this patch exports the function.Cc Christian Brauner <[email protected]>Signed-off-by Minchan Kim <[email protected]>I think you might have misunderstood my earlier comments. This should be patch 2 in your set. What is patch 8 should be folded into you existing patch 2 and become patch 3 with the rest of your patches shifted by 1 since you are reordering them.
Quoted 4 lines
Otherwise the code itself appears to not have changed anything so it looks fine to me.
Reviewed-by Alexander Duyck <[email protected]>
It was my intention because I expect supporting both pid and pidfd would be controversial. It would make easy to revert.
Thanks for the review!
Jann Horn
reviewerRe: [PATCH v4 1/8] mm: pass task to do_madvise
In reply to Minchan KimQuoted 8 lines · Minchan Kim
Quoted 7 lines · Alexander Duyck
Quoted 6 lines · Minchan Kim
In upcoming patches,
do_madvisewill be called from external process context so it shouldn't asssume "current" is always hinted process'stask_struct. Thus, let's get themm_structfromvma->vm_mm, not current because vma is always hinted process's one. And let's pass *current* as new task argument ofdo_madviseso it shouldn't change existing behavior.
[...]
Quoted 16 lines
Quoted 16 lines
mm/madvise.c static long madvise_dontneed_free(struct vm_area_struct *vma, · line 770
Quoted 11 lines
mm/madvise.c +2
-2@@ -763,8 +763,8 @@ static long madvise_dontneed_free(struct vm_area_struct *vma,if (!userfaultfd_remove (vma, start, end)) { *prev =NULL ;/* mmap_sem has been dropped, prev is stale */ -down_read (¤t->mm->mmap_sem); - vma =find_vma (current->mm, start); +down_read (&mm->mmap_sem); + vma =find_vma (mm, start);if (!vma)return -ENOMEM ;if (start < vma->vm_start) {This piece of code has me wondering if it is valid to be using
vma->mmat the start of the function. I assume we are probably safe since we read the mm value before the semaphore was released inuserfaultfd_remove. It might make more sense to just pass the task to the function and use task->mm-
Quoted 5 lines
Quoted 1 line
Quoted 1 line
mmap_seminstead.As Jann pointed out, we couldn't use
task->mmonce we verified it viaaccess_mm. However, I believevma->vm_mmis safe(Ccing Jann for double check).
Looks safe to me, too.