Skip to contentSkip to conversation

xfs: Fix races between readahead and hole punching

PATCH Superseded Jan Kara 5 participants 13 messages Jul 11, 2019 – Jul 23, 2019

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

cover xfs: Fix races between readahead and hole punching

Hello,

this is a patch series that addresses a possible race between readahead and hole punching Amir has discovered [1]. The first patch makes madvise(2) to handle readahead requests through fadvise infrastructure, the third patch then adds necessary locking to XFS to protect against the race. Note that other filesystems need similar protections but e.g. in case of ext4 it isn't so simple without seriously regressing mixed rw workload performance so I'm pushing just xfs fix at this moment which is simple.

Honza

[1] https://lore.kernel.org/linux-fsdevel/CAOQ4uxjQNmxqmtA_VbYW0Su9rKRk2zobJmahcyeaEVOFKVQ5dw@mail.gmail.com/

author reviewer
#8 lore raw

PATCH 3/3 xfs: Fix stale data exposure when readahead races with hole punch

Hole puching currently evicts pages from page cache and then goes on to remove blocks from the inode. This happens under both XFS_IOLOCK_EXCL and XFS_MMAPLOCK_EXCL which provides appropriate serialization with racing reads or page faults. However there is currently nothing that prevents readahead triggered by fadvise() or madvise() from racing with the hole punch and instantiating page cache page after hole punching has evicted page cache in xfs_flush_unmap_range() but before it has removed blocks from the inode. This page cache page will be mapping soon to be freed block and that can lead to returning stale data to userspace or even filesystem corruption.

Fix the problem by protecting handling of readahead requests by XFS_IOLOCK_SHARED similarly as we protect reads.

---
 fs/xfs/xfs_file.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
fs/xfs/xfs_file.c +20 -0
index 76748255f843..88fe3dbb3ba2 100644
@@ -33,6 +33,7 @@
 #include <linux/pagevec.h>
 #include <linux/backing-dev.h>
 #include <linux/mman.h>
+#include <linux/fadvise.h>
 
 static const struct vm_operations_struct xfs_file_vm_ops;
 
@@ -939,6 +940,24 @@ xfs_file_fallocate(
 	return error;
 }
 
+STATIC int
+xfs_file_fadvise(
+	struct file *file,
+	loff_t start,
+	loff_t end,
+	int advice)
+{
+	struct xfs_inode *ip = XFS_I(file_inode(file));
+	int ret;
+
+	/* Readahead needs protection from hole punching and similar ops */
+	if (advice == POSIX_FADV_WILLNEED)
+		xfs_ilock(ip, XFS_IOLOCK_SHARED);
+	ret = generic_fadvise(file, start, end, advice);
+	if (advice == POSIX_FADV_WILLNEED)
+		xfs_iunlock(ip, XFS_IOLOCK_SHARED);
+	return ret;
+}
 
 STATIC loff_t
 xfs_file_remap_range(
@@ -1235,6 +1254,7 @@ const struct file_operations xfs_file_operations = {
 	.fsync		= xfs_file_fsync,
 	.get_unmapped_area = thp_get_unmapped_area,
 	.fallocate	= xfs_file_fallocate,
+	.fadvise	= xfs_file_fadvise,
 	.remap_file_range = xfs_file_remap_range,
 };
 
Signature · 1 line
2.16.4
author reviewer
#1 lore raw

PATCH 1/3 mm: Handle MADV_WILLNEED through vfs_fadvise()

In reply to Jan Kara

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.

CC [email protected] # Needed by "xfs: Fix stale data exposure

when readahead races with hole punch"

---
 mm/madvise.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
mm/madvise.c +16 -6
index 628022e674a7..ae56d0ef337d 100644
@@ -14,6 +14,7 @@
 #include <linux/userfaultfd_k.h>
 #include <linux/hugetlb.h>
 #include <linux/falloc.h>
+#include <linux/fadvise.h>
 #include <linux/sched.h>
 #include <linux/ksm.h>
 #include <linux/fs.h>
@@ -275,6 +276,7 @@ static long madvise_willneed(struct vm_area_struct *vma,
 			     unsigned long start, unsigned long end)
 {
 	struct file *file = vma->vm_file;
+	loff_t offset;
 
 	*prev = vma;
 #ifdef CONFIG_SWAP
@@ -298,12 +300,20 @@ static long madvise_willneed(struct vm_area_struct *vma,
 		return 0;
 	}
 
-	start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
-	if (end > vma->vm_end)
-		end = vma->vm_end;
-	end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
-
-	force_page_cache_readahead(file->f_mapping, file, start, end - start);
+	/*
+	 * 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_sem.
+	 */
+	*prev = NULL;	/* tell sys_madvise we drop mmap_sem */
+	get_file(file);
+	up_read(&current->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(&current->mm->mmap_sem);
 	return 0;
 }
 
Signature · 1 line
2.16.4
author reviewer
#5 lore raw

PATCH 2/3 fs: Export generic_fadvise()

In reply to Jan Kara

Filesystems will need to call this function from their fadvise handlers.

CC: [email protected] # Needed by "xfs: Fix stale data exposure when
readahead races with hole punch"

---
 include/linux/fs.h | 2 ++
 mm/fadvise.c       | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)
include/linux/fs.h +2 -0
index f7fdfe93e25d..2666862ff00d 100644
@@ -3536,6 +3536,8 @@ extern void inode_nohighmem(struct inode *inode);
 /* mm/fadvise.c */
 extern int vfs_fadvise(struct file *file, loff_t offset, loff_t len,
 		       int advice);
+extern int generic_fadvise(struct file *file, loff_t offset, loff_t len,
+			   int advice);
 
 #if defined(CONFIG_IO_URING)
 extern struct sock *io_uring_get_socket(struct file *file);
mm/fadvise.c +2 -2
index 467bcd032037..4f17c83db575 100644
@@ -27,8 +27,7 @@
  * deactivate the pages and clear PG_Referenced.
  */
 
-static int generic_fadvise(struct file *file, loff_t offset, loff_t len,
-			   int advice)
+int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
 {
 	struct inode *inode;
 	struct address_space *mapping;
@@ -178,6 +177,7 @@ static int generic_fadvise(struct file *file, loff_t offset, loff_t len,
 	}
 	return 0;
 }
+EXPORT_SYMBOL(generic_fadvise);
 
 int vfs_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
 {
Signature · 1 line
2.16.4
#9 lore raw

Re: [PATCH 3/3] xfs: Fix stale data exposure when readahead races with hole punch

In reply to Jan Kara
Quoted 12 lines · Jan Kara

Hole puching currently evicts pages from page cache and then goes on to remove blocks from the inode. This happens under both XFS_IOLOCK_EXCL and XFS_MMAPLOCK_EXCL which provides appropriate serialization with racing reads or page faults. However there is currently nothing that prevents readahead triggered by fadvise() or madvise() from racing with the hole punch and instantiating page cache page after hole punching has evicted page cache in xfs_flush_unmap_range() but before it has removed blocks from the inode. This page cache page will be mapping soon to be freed block and that can lead to returning stale data to userspace or even filesystem corruption.

Quoted 8 lines

Fix the problem by protecting handling of readahead requests by XFS_IOLOCK_SHARED similarly as we protect reads.

---

Looks sane. (I'll let xfs developers offer reviewed-by tags)

Quoted 51 lines
 fs/xfs/xfs_file.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
fs/xfs/xfs_file.c +20 -1
index 76748255f843..88fe3dbb3ba2 100644
@@ -33,6 +33,7 @@
 #include <linux/pagevec.h>
 #include <linux/backing-dev.h>
 #include <linux/mman.h>
+#include <linux/fadvise.h>
 
 static const struct vm_operations_struct xfs_file_vm_ops;
 
@@ -939,6 +940,24 @@ xfs_file_fallocate(
        return error;
 }
 
+STATIC int
+xfs_file_fadvise(
+       struct file *file,
+       loff_t start,
+       loff_t end,
+       int advice)
+{
+       struct xfs_inode *ip = XFS_I(file_inode(file));
+       int ret;
+
+       /* Readahead needs protection from hole punching and similar ops */
+       if (advice == POSIX_FADV_WILLNEED)
+               xfs_ilock(ip, XFS_IOLOCK_SHARED);
+       ret = generic_fadvise(file, start, end, advice);
+       if (advice == POSIX_FADV_WILLNEED)
+               xfs_iunlock(ip, XFS_IOLOCK_SHARED);
+       return ret;
+}
 
 STATIC loff_t
 xfs_file_remap_range(
@@ -1235,6 +1254,7 @@ const struct file_operations xfs_file_operations = {
        .fsync          = xfs_file_fsync,
        .get_unmapped_area = thp_get_unmapped_area,
        .fallocate      = xfs_file_fallocate,
+       .fadvise        = xfs_file_fadvise,
        .remap_file_range = xfs_file_remap_range,
 };
 
--

2.16.4

#10 lore raw

Re: [PATCH 3/3] xfs: Fix stale data exposure when readahead races with hole punch

Quoted 43 lines · Amir Goldstein
Quoted 12 lines · Jan Kara

Hole puching currently evicts pages from page cache and then goes on to remove blocks from the inode. This happens under both XFS_IOLOCK_EXCL and XFS_MMAPLOCK_EXCL which provides appropriate serialization with racing reads or page faults. However there is currently nothing that prevents readahead triggered by fadvise() or madvise() from racing with the hole punch and instantiating page cache page after hole punching has evicted page cache in xfs_flush_unmap_range() but before it has removed blocks from the inode. This page cache page will be mapping soon to be freed block and that can lead to returning stale data to userspace or even filesystem corruption.

Quoted 8 lines

Fix the problem by protecting handling of readahead requests by XFS_IOLOCK_SHARED similarly as we protect reads.

---

Looks sane. (I'll let xfs developers offer reviewed-by tags)

Quoted 19 lines
 fs/xfs/xfs_file.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
fs/xfs/xfs_file.c +1 -0
index 76748255f843..88fe3dbb3ba2 100644
@@ -33,6 +33,7 @@
 #include <linux/pagevec.h>
 #include <linux/backing-dev.h>
 #include <linux/mman.h>
+#include <linux/fadvise.h>
 
 static const struct vm_operations_struct xfs_file_vm_ops;
 
@@ -939,6 +940,24 @@ xfs_file_fallocate(
        return error;
 }
 
Quoted 6 lines

fs/xfs/xfs_file.c xfs_file_fallocate( · line 948

Quoted 6 lines
fs/xfs/xfs_file.c +6 -0
+STATIC int
+xfs_file_fadvise(
+       struct file *file,
+       loff_t start,
+       loff_t end,
+       int advice)

Indentation needs fixing here.

Quoted 7 lines

fs/xfs/xfs_file.c xfs_file_fallocate( · line 955

Quoted 7 lines
fs/xfs/xfs_file.c +7 -0
+{
+       struct xfs_inode *ip = XFS_I(file_inode(file));
+       int ret;
+
+       /* Readahead needs protection from hole punching and similar ops */
+       if (advice == POSIX_FADV_WILLNEED)
+               xfs_ilock(ip, XFS_IOLOCK_SHARED);

It's good to fix this race, but at the same time I wonder what's the impact to processes writing to one part of a file waiting on IOLOCK_EXCL while readahead holds IOLOCK_SHARED?

(bluh bluh range locks ftw bluh bluh)

Do we need a lock for DONTNEED? I think the answer is that you have to lock the page to drop it and that will protect us from <myriad punch and truncate spaghetti> ... ?

Quoted 3 lines

fs/xfs/xfs_file.c xfs_file_fallocate( · line 958

Quoted 3 lines
fs/xfs/xfs_file.c +3 -0
+       ret = generic_fadvise(file, start, end, advice);
+       if (advice == POSIX_FADV_WILLNEED)
+               xfs_iunlock(ip, XFS_IOLOCK_SHARED);

Maybe it'd be better to do:

	int	lockflags = 0;

	if (advice == POSIX_FADV_WILLNEED) {
		lockflags = XFS_IOLOCK_SHARED;
		xfs_ilock(ip, lockflags);
	}

	ret = generic_fadvise(file, start, end, advice);

	if (lockflags)
		xfs_iunlock(ip, lockflags);

Just in case we some day want more or different types of inode locks?

--D

Quoted 16 lines
Quoted 16 lines
fs/xfs/xfs_file.c +3 -1
+       return ret;
+}
 
 STATIC loff_t
 xfs_file_remap_range(
@@ -1235,6 +1254,7 @@ const struct file_operations xfs_file_operations = {
        .fsync          = xfs_file_fsync,
        .get_unmapped_area = thp_get_unmapped_area,
        .fallocate      = xfs_file_fallocate,
+       .fadvise        = xfs_file_fadvise,
        .remap_file_range = xfs_file_remap_range,
 };
 
--

2.16.4

author reviewer
#11 lore raw

Re: [PATCH 3/3] xfs: Fix stale data exposure when readahead races with hole punch

Quoted 13 lines · Darrick J. Wong
Quoted 7 lines · Amir Goldstein

fs/xfs/xfs_file.c xfs_file_fallocate( · line 955

Quoted 7 lines
fs/xfs/xfs_file.c +7 -0
+{
+       struct xfs_inode *ip = XFS_I(file_inode(file));
+       int ret;
+
+       /* Readahead needs protection from hole punching and similar ops */
+       if (advice == POSIX_FADV_WILLNEED)
+               xfs_ilock(ip, XFS_IOLOCK_SHARED);

It's good to fix this race, but at the same time I wonder what's the impact to processes writing to one part of a file waiting on IOLOCK_EXCL while readahead holds IOLOCK_SHARED?

Quoted 1 line

(bluh bluh range locks ftw bluh bluh)

Yeah, with range locks this would have less impact. Also note that we hold the lock only during page setup and IO submission. IO itself will already happen without IOLOCK, only under page lock. But that's enough to stop the race.

Quoted 3 lines

Do we need a lock for DONTNEED? I think the answer is that you have to lock the page to drop it and that will protect us from <myriad punch and truncate spaghetti> ... ?

Yeah, DONTNEED is just page writeback + invalidate. So page lock is enough to protect from anything bad. Essentially we need IOLOCK only to protect the places that creates new pages in page cache.

Quoted 18 lines
Quoted 3 lines

fs/xfs/xfs_file.c xfs_file_fallocate( · line 958

Quoted 3 lines
fs/xfs/xfs_file.c +3 -0
+       ret = generic_fadvise(file, start, end, advice);
+       if (advice == POSIX_FADV_WILLNEED)
+               xfs_iunlock(ip, XFS_IOLOCK_SHARED);

Maybe it'd be better to do:

	int	lockflags = 0;

	if (advice == POSIX_FADV_WILLNEED) {
		lockflags = XFS_IOLOCK_SHARED;
		xfs_ilock(ip, lockflags);
	}

	ret = generic_fadvise(file, start, end, advice);

	if (lockflags)
		xfs_iunlock(ip, lockflags);
Quoted 1 line

Just in case we some day want more or different types of inode locks?

OK, will do. Just I'll get to testing this only after I return from vacation.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#2 lore raw

Re: [PATCH 1/3] mm: Handle MADV_WILLNEED through vfs_fadvise()

In reply to Jan Kara
Quoted 9 lines · Jan Kara

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.

CC [email protected] # Needed by "xfs: Fix stale data exposure

when readahead races with hole punch"

Looks reasonable to me, though is this race between readahead and truncate severe enough to try to push it as a fix for 5.3 or are you targetting 5.4?

--D

Quoted 54 lines
---
 mm/madvise.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
mm/madvise.c +16 -6
index 628022e674a7..ae56d0ef337d 100644
@@ -14,6 +14,7 @@
 #include <linux/userfaultfd_k.h>
 #include <linux/hugetlb.h>
 #include <linux/falloc.h>
+#include <linux/fadvise.h>
 #include <linux/sched.h>
 #include <linux/ksm.h>
 #include <linux/fs.h>
@@ -275,6 +276,7 @@ static long madvise_willneed(struct vm_area_struct *vma,
 			     unsigned long start, unsigned long end)
 {
 	struct file *file = vma->vm_file;
+	loff_t offset;
 
 	*prev = vma;
 #ifdef CONFIG_SWAP
@@ -298,12 +300,20 @@ static long madvise_willneed(struct vm_area_struct *vma,
 		return 0;
 	}
 
-	start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
-	if (end > vma->vm_end)
-		end = vma->vm_end;
-	end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
-
-	force_page_cache_readahead(file->f_mapping, file, start, end - start);
+	/*
+	 * 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_sem.
+	 */
+	*prev = NULL;	/* tell sys_madvise we drop mmap_sem */
+	get_file(file);
+	up_read(&current->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(&current->mm->mmap_sem);
 	return 0;
 }
 
Signature · 1 line
2.16.4
Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] fs: Export generic_fadvise()

In reply to Jan Kara
Quoted 5 lines · Jan Kara

Filesystems will need to call this function from their fadvise handlers.

CC: [email protected] # Needed by "xfs: Fix stale data exposure when
readahead races with hole punch"

Looks ok,

--D

Quoted 43 lines
---
 include/linux/fs.h | 2 ++
 mm/fadvise.c       | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)
include/linux/fs.h +2 -0
index f7fdfe93e25d..2666862ff00d 100644
@@ -3536,6 +3536,8 @@ extern void inode_nohighmem(struct inode *inode);
 /* mm/fadvise.c */
 extern int vfs_fadvise(struct file *file, loff_t offset, loff_t len,
 		       int advice);
+extern int generic_fadvise(struct file *file, loff_t offset, loff_t len,
+			   int advice);
 
 #if defined(CONFIG_IO_URING)
 extern struct sock *io_uring_get_socket(struct file *file);
mm/fadvise.c +2 -2
index 467bcd032037..4f17c83db575 100644
@@ -27,8 +27,7 @@
  * deactivate the pages and clear PG_Referenced.
  */
 
-static int generic_fadvise(struct file *file, loff_t offset, loff_t len,
-			   int advice)
+int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
 {
 	struct inode *inode;
 	struct address_space *mapping;
@@ -178,6 +177,7 @@ static int generic_fadvise(struct file *file, loff_t offset, loff_t len,
 	}
 	return 0;
 }
+EXPORT_SYMBOL(generic_fadvise);
 
 int vfs_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
 {
Signature · 1 line
2.16.4
#12 lore raw

Re: [PATCH 3/3] xfs: Fix stale data exposure when readahead races with hole punch

In reply to Jan Kara
Quoted 44 lines · Jan Kara
Quoted 13 lines · Darrick J. Wong
Quoted 7 lines · Amir Goldstein

fs/xfs/xfs_file.c xfs_file_fallocate( · line 955

Quoted 7 lines
fs/xfs/xfs_file.c +7 -0
+{
+       struct xfs_inode *ip = XFS_I(file_inode(file));
+       int ret;
+
+       /* Readahead needs protection from hole punching and similar ops */
+       if (advice == POSIX_FADV_WILLNEED)
+               xfs_ilock(ip, XFS_IOLOCK_SHARED);

It's good to fix this race, but at the same time I wonder what's the impact to processes writing to one part of a file waiting on IOLOCK_EXCL while readahead holds IOLOCK_SHARED?

Quoted 1 line

(bluh bluh range locks ftw bluh bluh)

Yeah, with range locks this would have less impact. Also note that we hold the lock only during page setup and IO submission. IO itself will already happen without IOLOCK, only under page lock. But that's enough to stop the race.

Quoted 3 lines

Do we need a lock for DONTNEED? I think the answer is that you have to lock the page to drop it and that will protect us from <myriad punch and truncate spaghetti> ... ?

Yeah, DONTNEED is just page writeback + invalidate. So page lock is enough to protect from anything bad. Essentially we need IOLOCK only to protect the places that creates new pages in page cache.

Quoted 15 lines
Quoted 3 lines

fs/xfs/xfs_file.c xfs_file_fallocate( · line 958

Quoted 3 lines
fs/xfs/xfs_file.c +3 -0
+       ret = generic_fadvise(file, start, end, advice);
+       if (advice == POSIX_FADV_WILLNEED)
+               xfs_iunlock(ip, XFS_IOLOCK_SHARED);

Maybe it'd be better to do:

	int	lockflags = 0;

	if (advice == POSIX_FADV_WILLNEED) {
		lockflags = XFS_IOLOCK_SHARED;
		xfs_ilock(ip, lockflags);
	}

	ret = generic_fadvise(file, start, end, advice);
Quoted 7 lines
Quoted 4 lines
	if (lockflags)
		xfs_iunlock(ip, lockflags);

Just in case we some day want more or different types of inode locks?

OK, will do. Just I'll get to testing this only after I return from vacation.

<nod>

--D

Quoted 6 lines

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#7 lore raw

Re: [PATCH 2/3] fs: Export generic_fadvise()

In reply to Jan Kara

Hi,

[This is an automated email]

This commit has been processed because it contains a -stable tag. The stable tag indicates that it's relevant for the following trees: all

The bot has tested the following trees: v5.2, v5.1.17, v4.19.58, v4.14.133, v4.9.185, v4.4.185.

v5.2: Build OK!
v5.1.17: Build OK!
v4.19.58: Build OK!
v4.14.133: Failed to apply! Possible dependencies:
17ef445f9bef ("Documentation/filesystems: update documentation of file_operations") 312db1aa1dc7 ("fs: add ksys_mount() helper; remove in-kernel calls to sys_mount()") 36028d5dd711 ("fs: add ksys_p{read,write}64() helpers; remove in-kernel calls to syscalls") 3a18ef5c1b39 ("fs: add ksys_umount() helper; remove in-kernel call to sys_umount()") 3ce4a7bf6626 ("fs: add ksys_read() helper; remove in-kernel calls to sys_read()") 45cd0faae371 ("vfs: add the fadvise() file operation")
6e8b704df584 ("fs: update documentation to mention __poll_t and match the code") 70f68ee81e2e ("fs: add ksys_sync() helper; remove in-kernel calls to sys_sync()") 806cbae1228c ("fs: add ksys_sync_file_range helper(); remove in-kernel calls to syscall") 819671ff849b ("syscalls: define and explain goal to not call syscalls in the kernel") 9b32105ec6b1 ("kernel: add ksys_unshare() helper; remove in-kernel calls to sys_unshare()") 9d5b7c956b09 ("mm: add ksys_fadvise64_64() helper; remove in-kernel call to sys_fadvise64_64()") a16fe33ab557 ("fs: add ksys_chroot() helper; remove-in kernel calls to sys_chroot()") c7248321a3d4 ("fs: add ksys_dup{,3}() helper; remove in-kernel calls to sys_dup{,3}()") e2aaa9f42336 ("kernel: add ksys_setsid() helper; remove in-kernel call to sys_setsid()") e7a3e8b2edf5 ("fs: add ksys_write() helper; remove in-kernel calls to sys_write()") edf292c76b88 ("fs: add ksys_fallocate() wrapper; remove in-kernel calls to sys_fallocate()")

v4.9.185: Failed to apply! Possible dependencies:
17ef445f9bef ("Documentation/filesystems: update documentation of file_operations")
3859a271a003 ("randstruct: Mark various structs for randomization")
45cd0faae371 ("vfs: add the fadvise() file operation")
5613fda9a503 ("sched/cputime: Convert task/group cputime to nsecs")
60f3e00d25b4 ("sysv,ipc: cacheline align kern_ipc_perm")
6e8b704df584 ("fs: update documentation to mention __poll_t and match the code")
8c8b73c4811f ("sched/cputime, powerpc: Prepare accounting structure for cputime flush on tick") a19ff1a2cc92 ("sched/cputime, powerpc/vtime: Accumulate cputime and account only on tick/task switch") b18b6a9cef7f ("timers: Omit POSIX timer stuff from task_struct when disabled")
baa73d9e478f ("posix-timers: Make them configurable")
c3edc4010e9d ("sched/headers: Move task_struct::signal and task_struct::sighand types and accessors into <linux/sched/signal.h>") d69dece5f5b6 ("LSM: Add /sys/kernel/security/lsm")
f828c3d0aeba ("sched/cputime, powerpc: Migrate stolen_time field to the accounting structure")

v4.4.185: Failed to apply! Possible dependencies:
04b38d601239 ("vfs: pull btrfs clone API to vfs layer")
17ef445f9bef ("Documentation/filesystems: update documentation of file_operations") 29732938a628 ("vfs: add copy_file_range syscall and vfs helper") 3859a271a003 ("randstruct: Mark various structs for randomization") 3db11b2eecc0 ("btrfs: add .copy_file_range file operation")
45cd0faae371 ("vfs: add the fadvise() file operation")
54dbc1517237 ("vfs: hoist the btrfs deduplication ioctl to the vfs") 6e8b704df584 ("fs: update documentation to mention __poll_t and match the code") 75ba1d07fd6a ("seq/proc: modify seq_put_decimal_[u]ll to take a const char *, not char") 81243eacfa40 ("cred: simpler, 1D supplementary groups")
d79bdd52d8be ("vfs: wire up compat ioctl for CLONE/CLONE_RANGE") f7a5f132b447 ("proc: faster /proc/*/status")

NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

--
Thanks,
Sasha

maintainer
#3 lore raw

Re: [PATCH 1/3] mm: Handle MADV_WILLNEED through vfs_fadvise()

In reply to Jan Kara

Hi,

[This is an automated email]

This commit has been processed because it contains a -stable tag. The stable tag indicates that it's relevant for the following trees: all

The bot has tested the following trees: v5.2, v5.1.17, v4.19.58, v4.14.133, v4.9.185, v4.4.185.

v5.2: Build OK!
v5.1.17: Build OK!
v4.19.58: Build OK!
v4.14.133: Build failed! Errors:
mm/madvise.c:314:2: error: implicit declaration of function ‘vfs_fadvise’; did you mean ‘sys_madvise’? [-Werror=implicit-function-declaration]

v4.9.185: Build failed! Errors:
mm/madvise.c:266:2: error: implicit declaration of function ‘vfs_fadvise’; did you mean ‘sys_madvise’? [-Werror=implicit-function-declaration]

v4.4.185: Build failed! Errors:
mm/madvise.c:261:2: error: implicit declaration of function ‘vfs_fadvise’; did you mean ‘sys_madvise’? [-Werror=implicit-function-declaration]

NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

--
Thanks,
Sasha

Reviewed-by on v1 1/3
#4 lore raw

Re: [PATCH 1/3] mm: Handle MADV_WILLNEED through vfs_fadvise()

In reply to Jan Kara
Quoted 9 lines · Jan Kara

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.

CC [email protected] # Needed by "xfs: Fix stale data exposure

when readahead races with hole punch"

I had a similar patch for my needs. But did not drop the mmap_sem when calling into the FS. This one is much better.

I tested this patch, Works perfect for my needs.

Thank you for this patch
Boaz

Quoted 52 lines
---
 mm/madvise.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
mm/madvise.c +16 -6
index 628022e674a7..ae56d0ef337d 100644
@@ -14,6 +14,7 @@
 #include <linux/userfaultfd_k.h>
 #include <linux/hugetlb.h>
 #include <linux/falloc.h>
+#include <linux/fadvise.h>
 #include <linux/sched.h>
 #include <linux/ksm.h>
 #include <linux/fs.h>
@@ -275,6 +276,7 @@ static long madvise_willneed(struct vm_area_struct *vma,
 			     unsigned long start, unsigned long end)
 {
 	struct file *file = vma->vm_file;
+	loff_t offset;
 
 	*prev = vma;
 #ifdef CONFIG_SWAP
@@ -298,12 +300,20 @@ static long madvise_willneed(struct vm_area_struct *vma,
 		return 0;
 	}
 
-	start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
-	if (end > vma->vm_end)
-		end = vma->vm_end;
-	end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
-
-	force_page_cache_readahead(file->f_mapping, file, start, end - start);
+	/*
+	 * 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_sem.
+	 */
+	*prev = NULL;	/* tell sys_madvise we drop mmap_sem */
+	get_file(file);
+	up_read(&current->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(&current->mm->mmap_sem);
 	return 0;
 }