filemap: Fix error propagation in do_read_cache_page()
Subsystems (3)
PATCH 1/1 filemap: Fix error propagation in do_read_cache_page()
When do_read_cache_folio() returns an error pointer the code was dereferencing it rather than forwarding the error via ERR_CAST().
Found during code review.
--- mm/filemap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/filemap.c +1 -1
@@ -3560,7 +3560,7 @@ static struct page *do_read_cache_page(struct address_space *mapping, folio =do_read_cache_folio (mapping, index, filler, file, gfp);if (IS_ERR (folio)) -return &folio->page; +return ERR_CAST (folio);return folio_file_page (folio, index); }
Signature · 1 line
2.37.3
Al Viro
maintainerQuoted 17 lines · Alexander Larsson
When
do_read_cache_folio()returns an error pointer the code was dereferencing it rather than forwarding the error viaERR_CAST().Found during code review.
Fixes 539a3322f208 ("filemap: Add read_cache_folio and read_mapping_folio")Signed-off-by Alexander Larsson <[email protected]>--- mm/filemap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/filemap.c +0
-0
mm/filemap.c static struct page *do_read_cache_page(struct address_space *mapping, · line 3563
Quoted 5 lines
mm/filemap.c +1
-1@@ -3560,7 +3560,7 @@ static struct page *do_read_cache_page(struct address_space *mapping, folio =do_read_cache_folio (mapping, index, filler, file, gfp);if (IS_ERR (folio)) -return &folio->page; +return ERR_CAST (folio);
Where do you see a dereference? I agree that your variant is cleaner, but &folio->page does *NOT* dereference anything - it's an equivalent of
(struct page *)((unsigned long)folio + offsetof(struct folio, page))
and the reason it happens to work is that page is the first member in struct folio, so the offsetof ends up being 0 and we are left with a cast from struct folio * to struct page *, i.e. the same thing ERR_CAST() variant end up with (it casts to void *, which is converted to struct page * since return acts as assignment wrt type conversions).
It *is* brittle and misguiding, and your patch is a much more clear way to spell that thing, no arguments about it; just that your patch is not changing behaviour.
Quoted 36 lines · Al Viro
Quoted 17 lines · Alexander Larsson
When
do_read_cache_folio()returns an error pointer the code was dereferencing it rather than forwarding the error viaERR_CAST().Found during code review.
Fixes 539a3322f208 ("filemap: Add read_cache_folio and read_mapping_folio")Signed-off-by Alexander Larsson <[email protected]>--- mm/filemap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/filemap.c +0
-0mm/filemap.c static struct page *do_read_cache_page(struct address_space *mapping, · line 3563
Quoted 5 lines
mm/filemap.c +1
-1@@ -3560,7 +3560,7 @@ static struct page *do_read_cache_page(struct address_space *mapping, folio =do_read_cache_folio (mapping, index, filler, file, gfp);if (IS_ERR (folio)) -return &folio->page; +return ERR_CAST (folio);Where do you see a dereference? I agree that your variant is cleaner, but
&folio->pagedoes *NOT* dereference anything - it's an equivalent of(struct page *)((unsigned long)folio +
offsetof(struct folio, page))and the reason it happens to work is that page is the first member in struct folio, so the offsetof ends up being 0 and we are left with a cast from struct folio * to struct page *, i.e. the same thing
ERR_CAST()variant end up with (it casts to void *, which is converted to struct page * since return acts as assignment wrt type conversions).
Quoted 3 lines
It *is* brittle and misguiding, and your patch is a much more clear way to spell that thing, no arguments about it; just that your patch is not changing behaviour.
Yeah, it doesn't actually dereference, but what I was thinking is that the caller could dereference it, if the addition made it not an error. However, I didn't look at the actual offset of page in folio, so you're right, this is actually fine.
Still, better change this to avoid confusing people.
Signature · 3 lines
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Alexander Larsson Red Hat, Inc
[email protected] [email protected]Matthew Wilcox
maintainerQuoted 35 lines · Al Viro
Quoted 17 lines · Alexander Larsson
When
do_read_cache_folio()returns an error pointer the code was dereferencing it rather than forwarding the error viaERR_CAST().Found during code review.
Fixes 539a3322f208 ("filemap: Add read_cache_folio and read_mapping_folio")Signed-off-by Alexander Larsson <[email protected]>--- mm/filemap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/filemap.c +0
-0mm/filemap.c static struct page *do_read_cache_page(struct address_space *mapping, · line 3563
Quoted 5 lines
mm/filemap.c +1
-1@@ -3560,7 +3560,7 @@ static struct page *do_read_cache_page(struct address_space *mapping, folio =do_read_cache_folio (mapping, index, filler, file, gfp);if (IS_ERR (folio)) -return &folio->page; +return ERR_CAST (folio);Where do you see a dereference? I agree that your variant is cleaner, but
&folio->pagedoes *NOT* dereference anything - it's an equivalent of(struct page *)((unsigned long)folio +
offsetof(struct folio, page))and the reason it happens to work is that page is the first member in struct folio, so the offsetof ends up being 0 and we are left with a cast from struct folio * to struct page *, i.e. the same thing
ERR_CAST()variant end up with (it casts to void *, which is converted to struct page * since return acts as assignment wrt type conversions).
Quoted 3 lines
It *is* brittle and misguiding, and your patch is a much more clear way to spell that thing, no arguments about it; just that your patch is not changing behaviour.
I don't see it that way. &folio->page is the idiomatic way to do this. What it really is, is an indicator that code needs to be converted from calling do_read_cache_page() and friends to calling the folio equivalents. Also, I should have moved this code to folio-compat.c where it would be with all the other code that uses this idiom.