Skip to content

Commit b56335c

Browse files
jankaragregkh
authored andcommitted
mm: Handle MADV_WILLNEED through vfs_fadvise()
commit 692fe62 upstream. Currently handling of MADV_WILLNEED hint calls directly into readahead code. Handle it by calling vfs_fadvise() instead so that filesystem can use its ->fadvise() callback to acquire necessary locks or otherwise prepare for the request. Suggested-by: Amir Goldstein <amir73il@gmail.com> Reviewed-by: Boaz Harrosh <boazh@netapp.com> CC: stable@vger.kernel.org Signed-off-by: Jan Kara <jack@suse.cz> Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a775ad9 commit b56335c

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

‎mm/madvise.c‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/userfaultfd_k.h>
1515
#include <linux/hugetlb.h>
1616
#include <linux/falloc.h>
17+
#include <linux/fadvise.h>
1718
#include <linux/sched.h>
1819
#include <linux/ksm.h>
1920
#include <linux/fs.h>
@@ -275,6 +276,7 @@ static long madvise_willneed(struct vm_area_struct *vma,
275276
unsigned long start, unsigned long end)
276277
{
277278
struct file *file = vma->vm_file;
279+
loff_t offset;
278280

279281
*prev = vma;
280282
#ifdef CONFIG_SWAP
@@ -298,12 +300,20 @@ static long madvise_willneed(struct vm_area_struct *vma,
298300
return 0;
299301
}
300302

301-
start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
302-
if (end > vma->vm_end)
303-
end = vma->vm_end;
304-
end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
305-
306-
force_page_cache_readahead(file->f_mapping, file, start, end - start);
303+
/*
304+
* Filesystem's fadvise may need to take various locks. We need to
305+
* explicitly grab a reference because the vma (and hence the
306+
* vma's reference to the file) can go away as soon as we drop
307+
* mmap_sem.
308+
*/
309+
*prev = NULL; /* tell sys_madvise we drop mmap_sem */
310+
get_file(file);
311+
up_read(&current->mm->mmap_sem);
312+
offset = (loff_t)(start - vma->vm_start)
313+
+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
314+
vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
315+
fput(file);
316+
down_read(&current->mm->mmap_sem);
307317
return 0;
308318
}
309319

0 commit comments

Comments
 (0)