Skip to contentSkip to conversation

Rework the gfs2 read and page fault locking

RFC PATCH In review Andreas Gruenbacher 3 participants 7 messages Nov 22, 2019 – Nov 26, 2019

Subsystems (5)
lore
Share & tools
lore

Andreas Gruenbacher

author maintainer
#0 lore raw

cover Rework the gfs2 read and page fault locking

Hello,

this patch series moves the glock lock taking in gfs2 from the ->readpage and ->readpages inode operations to the ->read_iter file and ->fault vm operations. To achieve that, we add flags to the generic_file_read_iter and filemap_fault generic helpers.

This proposal was triggered by the following discussion:

https://lore.kernel.org/linux-fsdevel/157225677483.3442.4227193290486305330.stgit at buzz/

In that thread, Linus argued that filesystems should make sure the inode size is sufficiently up-to-date before calling the generic helpers, and that filesystems can do it themselves if they want more than that. That's surely doable. However, implementing those operations properly at the filesystem level quickly becomes complicated when it gets to things like readahead. In addition, those slightly modified copies of those helpers would surely diverge from their originals over time, and maintaining them properly would become hard. So I hope the relatively small changes to make the original helpers slightly more flexible will be acceptable instead.

With the IOCB_CACHED flag added by one of the patches in this series, the code that Konstantin's initial patch adds to generic_file_buffered_read could be made conditional on the IOCB_CACHED flag being cleared. That way, it won't misfire on filesystems that allow a stale inode size. (I'm not sure if any filesystems other than gfs2 are actually affected.)

Some additional explanation:

The cache consistency model of filesystems like gfs2 is such that if pages are found in an inode's address space, those pages as well as the inode size are up to date and can be used without taking any filesystem locks. If a page is not cached, filesystem locks must be taken before the page can be read; this will also bring the inode size up to date.

Thus far, gfs2 has taken the filesystem locks inside the ->readpage and ->readpages address space operations. A better approach seems to be to take those locks earlier, in the ->read_iter file and ->fault vm operations. This would also avoid a lock inversion in ->readpages.

We obviously want to avoid taking the filesystem locks unnecessarily when the pages we are looking for are already cached; otherwise, we would cripple performance. So we need to check if those pages are present first. That's actually exactly what the generic_file_read_iter and filemap_fault helpers do already anyway, except that they will call into ->readpage and ->readpages when they find pages missing. Instead of that, we'd like those helpers to return with an error code that allows us to retry the operation after taking the filesystem locks.

Thanks,
Andreas

Andreas Gruenbacher (3):
fs: Add IOCB_CACHED flag for generic_file_read_iter
fs: Add FAULT_FLAG_CACHED flag for filemap_fault
gfs2: Rework read and page fault locking

 fs/gfs2/aops.c     | 36 +++++--------------------
 fs/gfs2/file.c     | 66 ++++++++++++++++++++++++++++++++++++++++++++--
 include/linux/fs.h |  1 +
 include/linux/mm.h |  4 ++-
 mm/filemap.c       | 60 ++++++++++++++++++++++++++++++-----------
 5 files changed, 119 insertions(+), 48 deletions(-)
Signature · 1 line
2.20.1
author maintainer
#1 lore raw

RFC PATCH 1/3 fs: Add IOCB_CACHED flag for generic_file_read_iter

Add an IOCB_CACHED flag which indicates to generic_file_read_iter that it should only look at the page cache, without triggering any filesystem I/O for the actual request or for readahead. When filesystem I/O would be triggered, an error code should be returned instead.

This allows the caller to perform a tentative read out of the page cache, and to retry the read after taking the necessary steps when the requested pages are not cached.

When readahead would be triggered, we return -ECANCELED instead of -EAGAIN. This allows to distinguish attempted readheads from attempted reads (with IOCB_NOWAIT).

---
 include/linux/fs.h |  1 +
 mm/filemap.c       | 17 ++++++++++++++---
 2 files changed, 15 insertions(+), 3 deletions(-)
include/linux/fs.h +1 -0
index e0d909d35763..4ca5e2885452 100644
@@ -314,6 +314,7 @@ enum rw_hint {
 #define IOCB_SYNC		(1 << 5)
 #define IOCB_WRITE		(1 << 6)
 #define IOCB_NOWAIT		(1 << 7)
+#define IOCB_CACHED		(1 << 8)
 
 struct kiocb {
 	struct file		*ki_filp;
mm/filemap.c +14 -3
index 85b7d087eb45..024ff0b5fcb6 100644
@@ -2046,7 +2046,7 @@ static ssize_t generic_file_buffered_read(struct kiocb *iocb,
 
 		page = find_get_page(mapping, index);
 		if (!page) {
-			if (iocb->ki_flags & IOCB_NOWAIT)
+			if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_CACHED))
 				goto would_block;
 			page_cache_sync_readahead(mapping,
 					ra, filp,
@@ -2056,12 +2056,16 @@ static ssize_t generic_file_buffered_read(struct kiocb *iocb,
 				goto no_cached_page;
 		}
 		if (PageReadahead(page)) {
+			if (iocb->ki_flags & IOCB_CACHED) {
+				error = -ECANCELED;
+				goto out;
+			}
 			page_cache_async_readahead(mapping,
 					ra, filp, page,
 					index, last_index - index);
 		}
 		if (!PageUptodate(page)) {
-			if (iocb->ki_flags & IOCB_NOWAIT) {
+			if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_CACHED)) {
 				put_page(page);
 				goto would_block;
 			}
@@ -2266,6 +2270,13 @@ static ssize_t generic_file_buffered_read(struct kiocb *iocb,
  *
  * This is the "read_iter()" routine for all filesystems
  * that can use the page cache directly.
+ *
+ * In the IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN should be
+ * returned if completing the request would require I/O; this does not prevent
+ * readahead.  The IOCB_CACHED flag indicates that -EAGAIN should be returned
+ * as under the IOCB_NOWAIT flag, and that -ECANCELED should be returned when
+ * readhead would be triggered.
+ *
  * Return:
  * * number of bytes copied, even for partial reads
  * * negative error code if nothing was read
@@ -2286,7 +2297,7 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
 		loff_t size;
 
 		size = i_size_read(inode);
-		if (iocb->ki_flags & IOCB_NOWAIT) {
+		if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_CACHED)) {
 			if (filemap_range_has_page(mapping, iocb->ki_pos,
 						   iocb->ki_pos + count - 1))
 				return -EAGAIN;
Signature · 1 line
2.20.1
author maintainer
#2 lore raw

RFC PATCH 2/3 fs: Add FAULT_FLAG_CACHED flag for filemap_fault

In reply to Andreas Gruenbacher

Add a FAULT_FLAG_CACHED flag which indicates to filemap_fault that it should only look at the page cache, without triggering filesystem I/O for the actual request or for readahead. When filesystem I/O would be triggered, VM_FAULT_RETRY should be returned instead.

This allows the caller to tentatively satisfy a minor page fault out of the page cache, and to retry the operation after taking the necessary steps when that isn't possible.

---
 include/linux/mm.h |  4 +++-
 mm/filemap.c       | 43 ++++++++++++++++++++++++++++++-------------
 2 files changed, 33 insertions(+), 14 deletions(-)
include/linux/mm.h +3 -1
index a2adf95b3f9c..b3317e4b2607 100644
@@ -392,6 +392,7 @@ extern pgprot_t protection_map[16];
 #define FAULT_FLAG_USER		0x40	/* The fault originated in userspace */
 #define FAULT_FLAG_REMOTE	0x80	/* faulting for non current tsk/mm */
 #define FAULT_FLAG_INSTRUCTION  0x100	/* The fault was during an instruction fetch */
+#define FAULT_FLAG_CACHED		0x200	/* Only look at the page cache */
 
 #define FAULT_FLAG_TRACE \
 	{ FAULT_FLAG_WRITE,		"WRITE" }, \
@@ -402,7 +403,8 @@ extern pgprot_t protection_map[16];
 	{ FAULT_FLAG_TRIED,		"TRIED" }, \
 	{ FAULT_FLAG_USER,		"USER" }, \
 	{ FAULT_FLAG_REMOTE,		"REMOTE" }, \
-	{ FAULT_FLAG_INSTRUCTION,	"INSTRUCTION" }
+	{ FAULT_FLAG_INSTRUCTION,	"INSTRUCTION" }, \
+	{ FAULT_FLAG_CACHED,		"CACHED" }
 
 /*
  * vm_fault is filled by the the pagefault handler and passed to the vma's
mm/filemap.c +30 -13

88 lines not on this page · raw message

Signature · 1 line
2.20.1
author maintainer
#3 lore raw

RFC PATCH 3/3 gfs2: Rework read and page fault locking

In reply to Andreas Gruenbacher

Move the glock lock taking code from the ->readpage and ->readpages address space operations to the ->read_iter file and ->fault vm operations.

To avoid taking the lock even when an operation can be satisfied out of the page cache, try the operation without taking the lock first. When that fails, take the lock and repeat the opeation.

---
 fs/gfs2/aops.c | 36 ++++++---------------------
 fs/gfs2/file.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 71 insertions(+), 31 deletions(-)
fs/gfs2/aops.c +7 -29
index b9fe975d7625..4aa6c952eb90 100644
@@ -515,26 +515,10 @@ static int __gfs2_readpage(void *file, struct page *page)
 
 static int gfs2_readpage(struct file *file, struct page *page)
 {
-	struct address_space *mapping = page->mapping;
-	struct gfs2_inode *ip = GFS2_I(mapping->host);
-	struct gfs2_holder gh;
 	int error;
 
-	unlock_page(page);
-	gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
-	error = gfs2_glock_nq(&gh);
-	if (unlikely(error))
-		goto out;
-	error = AOP_TRUNCATED_PAGE;
-	lock_page(page);
-	if (page->mapping == mapping && !PageUptodate(page))
-		error = __gfs2_readpage(file, page);
-	else
-		unlock_page(page);
-	gfs2_glock_dq(&gh);
-out:
-	gfs2_holder_uninit(&gh);
-	if (error && error != AOP_TRUNCATED_PAGE)
+	error = __gfs2_readpage(file, page);
+	if (error)
 		lock_page(page);
 	return error;
 }
@@ -602,18 +586,12 @@ static int gfs2_readpages(struct file *file, struct address_space *mapping,
 	struct inode *inode = mapping->host;
 	struct gfs2_inode *ip = GFS2_I(inode);
 	struct gfs2_sbd *sdp = GFS2_SB(inode);
-	struct gfs2_holder gh;
-	int ret;
+	int ret = 0;
 
-	gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
-	ret = gfs2_glock_nq(&gh);
-	if (unlikely(ret))
-		goto out_uninit;
-	if (!gfs2_is_stuffed(ip))
-		ret = mpage_readpages(mapping, pages, nr_pages, gfs2_block_map);
-	gfs2_glock_dq(&gh);
-out_uninit:
-	gfs2_holder_uninit(&gh);
+	if (gfs2_is_stuffed(ip))
+		goto out;
+	ret = mpage_readpages(mapping, pages, nr_pages, gfs2_block_map);
+out:
 	if (unlikely(test_bit(SDF_WITHDRAWN, &sdp->sd_flags)))
 		ret = -EIO;
 	return ret;
fs/gfs2/file.c +64 -2

87 lines not on this page · raw message

Signature · 1 line
2.20.1
#4 lore raw

Re: [RFC PATCH 3/3] gfs2: Rework read and page fault locking

fs/gfs2/file.c static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from) · line 809

Quoted 7 lines · Andreas Gruenbacher
fs/gfs2/file.c +3 -0
@@ -778,15 +804,51 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
 
 static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
 {
+	struct gfs2_inode *ip;
+	struct gfs2_holder gh;
+	size_t written = 0;

'written' in a read routine?

Signature · 1 line
 Kirill A. Shutemov
#5 lore raw

Re: [RFC PATCH 3/3] gfs2: Rework read and page fault locking

Am Mo., 25. Nov. 2019 um 10:16 Uhr schrieb Kirill A. Shutemov <[email protected]>:

Quoted 10 lines · Andreas Gruenbacher

fs/gfs2/file.c static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from) · line 809

Quoted 7 lines
fs/gfs2/file.c +3 -0
@@ -778,15 +804,51 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
 
 static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
 {
+     struct gfs2_inode *ip;
+     struct gfs2_holder gh;
+     size_t written = 0;

'written' in a read routine?

It's a bit weird, but it's the same as in generic_file_buffered_read.

Quoted 2 lines

--
Kirill A. Shutemov

Thanks,
Andreas

#6 lore raw
In reply to Andreas Gruenbacher
Quoted 42 lines · Andreas Gruenbacher

Hello,

this patch series moves the glock lock taking in gfs2 from the ->readpage and ->readpages inode operations to the ->read_iter file and ->fault vm operations. To achieve that, we add flags to the generic_file_read_iter and filemap_fault generic helpers.

This proposal was triggered by the following discussion:

https://lore.kernel.org/linux-fsdevel/157225677483.3442.4227193290486305330.stgit@buzz/

In that thread, Linus argued that filesystems should make sure the inode size is sufficiently up-to-date before calling the generic helpers, and that filesystems can do it themselves if they want more than that. That's surely doable. However, implementing those operations properly at the filesystem level quickly becomes complicated when it gets to things like readahead. In addition, those slightly modified copies of those helpers would surely diverge from their originals over time, and maintaining them properly would become hard. So I hope the relatively small changes to make the original helpers slightly more flexible will be acceptable instead.

With the IOCB_CACHED flag added by one of the patches in this series, the code that Konstantin's initial patch adds to generic_file_buffered_read could be made conditional on the IOCB_CACHED flag being cleared. That way, it won't misfire on filesystems that allow a stale inode size. (I'm not sure if any filesystems other than gfs2 are actually affected.)

Some additional explanation:

The cache consistency model of filesystems like gfs2 is such that if pages are found in an inode's address space, those pages as well as the inode size are up to date and can be used without taking any filesystem locks. If a page is not cached, filesystem locks must be taken before the page can be read; this will also bring the inode size up to date.

Thus far, gfs2 has taken the filesystem locks inside the ->readpage and ->readpages address space operations. A better approach seems to be to take those locks earlier, in the ->read_iter file and ->fault vm operations. This would also avoid a lock inversion in ->readpages.

Quoted 8 lines

We obviously want to avoid taking the filesystem locks unnecessarily when the pages we are looking for are already cached; otherwise, we would cripple performance. So we need to check if those pages are present first. That's actually exactly what the generic_file_read_iter and filemap_fault helpers do already anyway, except that they will call into ->readpage and ->readpages when they find pages missing. Instead of that, we'd like those helpers to return with an error code that allows us to retry the operation after taking the filesystem locks.

Do you see IOCB_CACHED/FAULT_FLAG_CACHED semantics being usable for anyting beyond gfs2?

Signature · 1 line
 Kirill A. Shutemov