Skip to content

Commit

Permalink
mm/madvise: pass mm to do_madvise
Browse files Browse the repository at this point in the history
Patch series "introduce memory hinting API for external process", v9.

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.

This patch (of 3):

In upcoming patches, do_madvise will be called from external process
context so we shouldn't asssume "current" is always hinted process's
task_struct.

Furthermore, we must not access mm_struct via task->mm, but obtain it via
access_mm() once (in the following patch) and only use that pointer [1],
so pass it to do_madvise() as well.  Note the vma->vm_mm pointers are
safe, so we can use them further down the call stack.

And let's pass current->mm as arguments of do_madvise so it shouldn't
change existing behavior but prepare next patch to make review easy.

[[email protected]: changelog tweak]
[[email protected]: use current->mm for io_uring]
  Link: http://lkml.kernel.org/r/[email protected]
[[email protected]: fix it for upstream changes]
[[email protected]: whoops]
[[email protected]: add missing includes]

Signed-off-by: Minchan Kim <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Reviewed-by: Suren Baghdasaryan <[email protected]>
Reviewed-by: Vlastimil Babka <[email protected]>
Acked-by: David Rientjes <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: Jann Horn <[email protected]>
Cc: Tim Murray <[email protected]>
Cc: Daniel Colascione <[email protected]>
Cc: Sandeep Patil <[email protected]>
Cc: Sonny Rao <[email protected]>
Cc: Brian Geffon <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Shakeel Butt <[email protected]>
Cc: John Dias <[email protected]>
Cc: Joel Fernandes <[email protected]>
Cc: Alexander Duyck <[email protected]>
Cc: SeongJae Park <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Kirill Tkhai <[email protected]>
Cc: Oleksandr Natalenko <[email protected]>
Cc: SeongJae Park <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Florian Weimer <[email protected]>
Cc: <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
Link: http://lkml.kernel.org/r/[email protected]
Link: http://lkml.kernel.org/r/[email protected]
Link: http://lkml.kernel.org/r/[email protected]
Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
minchank authored and torvalds committed Oct 18, 2020
1 parent 2559653 commit 0726b01
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion fs/io_uring.c
Original file line number Diff line number Diff line change
Expand Up @@ -3989,7 +3989,7 @@ static int io_madvise(struct io_kiocb *req, bool force_nonblock)
if (force_nonblock)
return -EAGAIN;

ret = do_madvise(ma->addr, ma->len, ma->advice);
ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
if (ret < 0)
req_set_fail_links(req);
io_req_complete(req, ret);
Expand Down
2 changes: 1 addition & 1 deletion include/linux/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2579,7 +2579,7 @@ 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 mm_struct *mm, unsigned long start, size_t len_in, int behavior);

#ifdef CONFIG_MMU
extern int __mm_populate(unsigned long addr, unsigned long len,
Expand Down
32 changes: 18 additions & 14 deletions mm/madvise.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ 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;

Expand Down Expand Up @@ -294,10 +295,10 @@ static long madvise_willneed(struct vm_area_struct *vma,
get_file(file);
offset = (loff_t)(start - vma->vm_start)
+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
mmap_read_unlock(current->mm);
mmap_read_unlock(mm);
vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
fput(file);
mmap_read_lock(current->mm);
mmap_read_lock(mm);
return 0;
}

Expand Down Expand Up @@ -766,15 +767,17 @@ 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;

if (!userfaultfd_remove(vma, start, end)) {
*prev = NULL; /* mmap_lock has been dropped, prev is stale */

mmap_read_lock(current->mm);
vma = find_vma(current->mm, start);
mmap_read_lock(mm);
vma = find_vma(mm, start);
if (!vma)
return -ENOMEM;
if (start < vma->vm_start) {
Expand Down Expand Up @@ -828,6 +831,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_lock */

Expand Down Expand Up @@ -855,13 +859,13 @@ static long madvise_remove(struct vm_area_struct *vma,
get_file(f);
if (userfaultfd_remove(vma, start, end)) {
/* mmap_lock was not released by userfaultfd_remove() */
mmap_read_unlock(current->mm);
mmap_read_unlock(mm);
}
error = vfs_fallocate(f,
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
offset, end - start);
fput(f);
mmap_read_lock(current->mm);
mmap_read_lock(mm);
return error;
}

Expand Down Expand Up @@ -1045,7 +1049,7 @@ 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 mm_struct *mm, unsigned long start, size_t len_in, int behavior)
{
unsigned long end, tmp;
struct vm_area_struct *vma, *prev;
Expand Down Expand Up @@ -1083,18 +1087,18 @@ int do_madvise(unsigned long start, size_t len_in, int behavior)

write = madvise_need_mmap_write(behavior);
if (write) {
if (mmap_write_lock_killable(current->mm))
if (mmap_write_lock_killable(mm))
return -EINTR;
} else {
mmap_read_lock(current->mm);
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(current->mm, start, &prev);
vma = find_vma_prev(mm, start, &prev);
if (vma && start > vma->vm_start)
prev = vma;

Expand Down Expand Up @@ -1131,19 +1135,19 @@ int do_madvise(unsigned long start, size_t len_in, int behavior)
if (prev)
vma = prev->vm_next;
else /* madvise_remove dropped mmap_lock */
vma = find_vma(current->mm, start);
vma = find_vma(mm, start);
}
out:
blk_finish_plug(&plug);
if (write)
mmap_write_unlock(current->mm);
mmap_write_unlock(mm);
else
mmap_read_unlock(current->mm);
mmap_read_unlock(mm);

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->mm, start, len_in, behavior);
}

0 comments on commit 0726b01

Please sign in to comment.