Skip to contentSkip to conversation

iomap: fix readahead folio refcounting race

PATCH Superseded Joanne Koong 2 participants 5 messages Jan 14 – Jan 15

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iomap: fix readahead folio refcounting race

readahead_folio() returns the next folio from the readahead control (rac) but it also drops the refcount on the folio that had been held by the rac. As such, there is only one refcount remaining on the folio (which is held by the page cache) after this returns.

This is problematic because this opens a race where if the folio does not have an iomap_folio_state struct attached to it and the folio gets read in by the filesystem's IO helper, folio_end_read() may have already been called on the folio (which will unlock the folio) which allows the page cache to evict the folio (dropping the refcount and leading to the folio being freed) by the time iomap_read_end() runs.

Switch to __readahead_folio(), which returns the folio with a reference held for the caller, and add explicit folio_put() calls when done with the folio.

Fixes d43558ae6729 ("iomap: track pending read bytes more optimally")
---
 fs/iomap/buffered-io.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/iomap/buffered-io.c +5 -2
index fd9a2cf95620..96fab015371b 100644
@@ -588,10 +588,11 @@ static int iomap_readahead_iter(struct iomap_iter *iter,
 		if (ctx->cur_folio &&
 		    offset_in_folio(ctx->cur_folio, iter->pos) == 0) {
 			iomap_read_end(ctx->cur_folio, *cur_bytes_submitted);
+			folio_put(ctx->cur_folio);
 			ctx->cur_folio = NULL;
 		}
 		if (!ctx->cur_folio) {
-			ctx->cur_folio = readahead_folio(ctx->rac);
+			ctx->cur_folio = __readahead_folio(ctx->rac);
 			if (WARN_ON_ONCE(!ctx->cur_folio))
 				return -EINVAL;
 			*cur_bytes_submitted = 0;
@@ -639,8 +640,10 @@ void iomap_readahead(const struct iomap_ops *ops,
 	if (ctx->ops->submit_read)
 		ctx->ops->submit_read(ctx);
 
-	if (ctx->cur_folio)
+	if (ctx->cur_folio) {
 		iomap_read_end(ctx->cur_folio, cur_bytes_submitted);
+		folio_put(ctx->cur_folio);
+	}
 }
 EXPORT_SYMBOL_GPL(iomap_readahead);
 
Signature · 1 line
2.47.3
#1 lore raw
Quoted 12 lines · Joanne Koong

readahead_folio() returns the next folio from the readahead control (rac) but it also drops the refcount on the folio that had been held by the rac. As such, there is only one refcount remaining on the folio (which is held by the page cache) after this returns.

This is problematic because this opens a race where if the folio does not have an iomap_folio_state struct attached to it and the folio gets read in by the filesystem's IO helper, folio_end_read() may have already been called on the folio (which will unlock the folio) which allows the page cache to evict the folio (dropping the refcount and leading to the folio being freed) by the time iomap_read_end() runs.

Quoted 3 lines

Switch to __readahead_folio(), which returns the folio with a reference held for the caller, and add explicit folio_put() calls when done with the folio.

No. The direction we're going in is that there's no refcount held at this point. I just want to get this ANCK out before Christian applies the patch; I'll send a followup with a better fix imminently.

Quoted 39 lines
Fixes d43558ae6729 ("iomap: track pending read bytes more optimally")
---
 fs/iomap/buffered-io.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/iomap/buffered-io.c +5 -2
index fd9a2cf95620..96fab015371b 100644
@@ -588,10 +588,11 @@ static int iomap_readahead_iter(struct iomap_iter *iter,
 		if (ctx->cur_folio &&
 		    offset_in_folio(ctx->cur_folio, iter->pos) == 0) {
 			iomap_read_end(ctx->cur_folio, *cur_bytes_submitted);
+			folio_put(ctx->cur_folio);
 			ctx->cur_folio = NULL;
 		}
 		if (!ctx->cur_folio) {
-			ctx->cur_folio = readahead_folio(ctx->rac);
+			ctx->cur_folio = __readahead_folio(ctx->rac);
 			if (WARN_ON_ONCE(!ctx->cur_folio))
 				return -EINVAL;
 			*cur_bytes_submitted = 0;
@@ -639,8 +640,10 @@ void iomap_readahead(const struct iomap_ops *ops,
 	if (ctx->ops->submit_read)
 		ctx->ops->submit_read(ctx);
 
-	if (ctx->cur_folio)
+	if (ctx->cur_folio) {
 		iomap_read_end(ctx->cur_folio, cur_bytes_submitted);
+		folio_put(ctx->cur_folio);
+	}
 }
 EXPORT_SYMBOL_GPL(iomap_readahead);
 
Signature · 1 line
2.47.3
#2 lore raw
Quoted 14 lines · Matthew Wilcox
Quoted 12 lines · Joanne Koong

readahead_folio() returns the next folio from the readahead control (rac) but it also drops the refcount on the folio that had been held by the rac. As such, there is only one refcount remaining on the folio (which is held by the page cache) after this returns.

This is problematic because this opens a race where if the folio does not have an iomap_folio_state struct attached to it and the folio gets read in by the filesystem's IO helper, folio_end_read() may have already been called on the folio (which will unlock the folio) which allows the page cache to evict the folio (dropping the refcount and leading to the folio being freed) by the time iomap_read_end() runs.

Quoted 7 lines
Quoted 3 lines

Switch to __readahead_folio(), which returns the folio with a reference held for the caller, and add explicit folio_put() calls when done with the folio.

No. The direction we're going in is that there's no refcount held at this point. I just want to get this ANCK out before Christian applies the patch; I'll send a followup with a better fix imminently.

Sounds good, thanks for taking a look. I'll keep an eye out for your patch.

Thanks,
Joanne

Quoted 40 lines
Quoted 40 lines
Fixes d43558ae6729 ("iomap: track pending read bytes more optimally")
---
 fs/iomap/buffered-io.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/iomap/buffered-io.c +5 -3
index fd9a2cf95620..96fab015371b 100644
@@ -588,10 +588,11 @@ static int iomap_readahead_iter(struct iomap_iter *iter,
              if (ctx->cur_folio &&
                  offset_in_folio(ctx->cur_folio, iter->pos) == 0) {
                      iomap_read_end(ctx->cur_folio, *cur_bytes_submitted);
+                     folio_put(ctx->cur_folio);
                      ctx->cur_folio = NULL;
              }
              if (!ctx->cur_folio) {
-                     ctx->cur_folio = readahead_folio(ctx->rac);
+                     ctx->cur_folio = __readahead_folio(ctx->rac);
                      if (WARN_ON_ONCE(!ctx->cur_folio))
                              return -EINVAL;
                      *cur_bytes_submitted = 0;
@@ -639,8 +640,10 @@ void iomap_readahead(const struct iomap_ops *ops,
      if (ctx->ops->submit_read)
              ctx->ops->submit_read(ctx);
 
-     if (ctx->cur_folio)
+     if (ctx->cur_folio) {
              iomap_read_end(ctx->cur_folio, cur_bytes_submitted);
+             folio_put(ctx->cur_folio);
+     }
 }
 EXPORT_SYMBOL_GPL(iomap_readahead);
 
--

2.47.3

#3 lore raw
Quoted 19 lines · Joanne Koong
Quoted 18 lines · Matthew Wilcox
Quoted 15 lines · Joanne Koong

readahead_folio() returns the next folio from the readahead control (rac) but it also drops the refcount on the folio that had been held by the rac. As such, there is only one refcount remaining on the folio (which is held by the page cache) after this returns.

This is problematic because this opens a race where if the folio does not have an iomap_folio_state struct attached to it and the folio gets read in by the filesystem's IO helper, folio_end_read() may have already been called on the folio (which will unlock the folio) which allows the page cache to evict the folio (dropping the refcount and leading to the folio being freed) by the time iomap_read_end() runs.

Switch to __readahead_folio(), which returns the folio with a reference held for the caller, and add explicit folio_put() calls when done with the folio.

Quoted 5 lines
Quoted 3 lines

No. The direction we're going in is that there's no refcount held at this point. I just want to get this ANCK out before Christian applies the patch; I'll send a followup with a better fix imminently.

Sounds good, thanks for taking a look. I'll keep an eye out for your patch.

I can't quite figure out what's going on. If things are happening as you describe, then with your patch we'll end up calling folio_end_read() twice, which is not allowed anyway (and we have debugging assertions to catch!) It feels like something needs to be clearing ctx->cur_folio if it knows it's going to call / already has called folio_end_read().

But then I can't see anywhere in Linus' current tree that's calling iomap_finish_folio_read() outside of fs/iomap/bio.c. Should I be looking at a different tree to see the problem you're experiencing?

#4 lore raw
Quoted 21 lines · Matthew Wilcox
Quoted 19 lines · Joanne Koong
Quoted 18 lines · Matthew Wilcox
Quoted 15 lines · Joanne Koong

readahead_folio() returns the next folio from the readahead control (rac) but it also drops the refcount on the folio that had been held by the rac. As such, there is only one refcount remaining on the folio (which is held by the page cache) after this returns.

This is problematic because this opens a race where if the folio does not have an iomap_folio_state struct attached to it and the folio gets read in by the filesystem's IO helper, folio_end_read() may have already been called on the folio (which will unlock the folio) which allows the page cache to evict the folio (dropping the refcount and leading to the folio being freed) by the time iomap_read_end() runs.

Switch to __readahead_folio(), which returns the folio with a reference held for the caller, and add explicit folio_put() calls when done with the folio.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

No. The direction we're going in is that there's no refcount held at this point. I just want to get this ANCK out before Christian applies the patch; I'll send a followup with a better fix imminently.

Sounds good, thanks for taking a look. I'll keep an eye out for your patch.

Hi Matthew,

Quoted 5 lines

I can't quite figure out what's going on. If things are happening as you describe, then with your patch we'll end up calling folio_end_read() twice, which is not allowed anyway (and we have debugging assertions to catch!) It feels like something needs to be clearing ctx->cur_folio if it knows it's going to call / already has called folio_end_read().

I'm not seeing how the patch ends up calling folio_end_read() twice. The issue is that without the patch, after the readahead folio is sent off to be read in by the filesystem in this line in iomap_read_folio_iter()

   ret = ctx->ops->read_folio_range(iter, ctx, plen);

the filesystem may have called iomap_finish_folio_read() -> folio_end_read() on the folio, which then allows the possibility of the folio being evicted from the page cache, whereupon it'll be freed because only the page cache holds the reference. That problem is that subsequent readahead logic (eg iomap_readahead_iter(), iomap_read_end()) still accesses into that folio. This only happens for folios that don't have ifs metadata attached, since for folios with ifs metadata, the bias we add to ifs->read_bytes_pending prevents this.

My thought process behind this patch was that if we still retain the caller reference for the readahead folio, then when the folio is evicted from the page cache, the folio won't be freed since we have a ref on it.

Looking at this some more, I think you're right that the better solution is to just clear ctx->cur_folio if the folio could have been freed.
I'll submit a v2 that fixes it this way.

Quoted 4 lines

But then I can't see anywhere in Linus' current tree that's calling iomap_finish_folio_read() outside of fs/iomap/bio.c. Should I be looking at a different tree to see the problem you're experiencing?

Fuse is the other caller of iomap_finish_folio_read() (in fs/fuse/file.c). The tree I'm using is Christian's vfs tree (on the branch vfs.fixes).

Thanks for looking at this, I appreciate your thoughts.