return an ERR_PTR from __filemap_get_folio
Subsystems (17)
Hi all,
__filemap_get_folio and its wrappers can return NULL for three different conditions, which in some cases requires the caller to reverse engineer the decision making. This is fixed by returning an ERR_PTR instead of NULL and thus transporting the reason for the failure. But to make that work we first need to ensure that no xa_value special case is returned and thus return the FGP_ENTRY flag. It turns out that flag is barely used and can usually be deal with in a better way.
Note that the shmem patches in here are non-trivial and need some careful review and testing.
Diffstat
fs/afs/dir.c | 10 +++---- fs/afs/dir_edit.c | 2 - fs/afs/write.c | 4 +- fs/btrfs/disk-io.c | 2 - fs/btrfs/extent_io.c | 2 + fs/ext4/inode.c | 2 - fs/ext4/move_extent.c | 8 ++--- fs/gfs2/lops.c | 2 + fs/hugetlbfs/inode.c | 2 - fs/iomap/buffered-io.c | 6 ++-- fs/netfs/buffered_read.c | 4 +- fs/nilfs2/page.c | 6 ++-- include/linux/pagemap.h | 4 +- include/linux/shmem_fs.h | 1 mm/filemap.c | 27 ++++++++----------- mm/folio-compat.c | 4 +- mm/huge_memory.c | 5 +-- mm/memcontrol.c | 2 - mm/mincore.c | 2 - mm/shmem.c | 64 +++++++++++++++++++---------------------------- mm/swap_state.c | 17 ++++++------ mm/swapfile.c | 4 +- mm/truncate.c | 15 +++++------ 23 files changed, 93 insertions(+), 102 deletions(-)
PATCH 1/9 mm: don't look at xarray value entries in split_huge_pages_in_file
split_huge_pages_in_file never wants to do anything with the special value enties. Switch to using filemap_get_folio to not even see them.
--- mm/huge_memory.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
mm/huge_memory.c +2 -3
@@ -3100,11 +3100,10 @@ static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start, mapping = candidate->f_mapping;for (index = off_start; index < off_end; index += nr_pages) { -struct folio *folio =__filemap_get_folio (mapping, index, -FGP_ENTRY ,0 ); +struct folio *folio =filemap_get_folio (mapping, index); nr_pages =1 ; -if (xa_is_value (folio) || !folio) +if (!folio)continue ;if (!folio_test_large (folio))
Signature · 1 line
2.39.0
Re: [PATCH 1/9] mm: don't look at xarray value entries in split_huge_pages_in_file
Quoted 4 lines · Christoph Hellwig
split_huge_pages_in_filenever wants to do anything with the special value enties. Switch to usingfilemap_get_folioto not even see them.Signed-off-by Christoph Hellwig <[email protected]>
PATCH 2/9 mm: make mapping_get_entry available outside of filemap.c
In reply to Christoph Hellwigmapping_get_entry is useful for page cache API users that need to know about xa_value internals. Rename it and make it available in pagemap.h.
--- include/linux/pagemap.h | 1 + mm/filemap.c | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-)
include/linux/pagemap.h +1 -0
@@ -507,6 +507,7 @@ pgoff_t page_cache_prev_miss(struct address_space *mapping,#define FGP_ENTRY 0x00000080#define FGP_STABLE 0x00000100 +void *filemap_get_entry (struct address_space *mapping,pgoff_t index);struct folio *__filemap_get_folio (struct address_space *mapping,pgoff_t index,int fgp_flags,gfp_t gfp); struct page *pagecache_get_page(struct address_space *mapping, pgoff_t index,
mm/filemap.c +3 -3
@@ -1834,7 +1834,7 @@ EXPORT_SYMBOL(page_cache_prev_miss);*/ /* -* mapping_get_entry - Get a page cache entry. +* filemap_get_entry - Get a page cache entry. * @mapping: the address_space to search * @index: The page cache index. *
@@ -1845,7 +1845,7 @@ EXPORT_SYMBOL(page_cache_prev_miss);* * Return: The folio, swap or shadow entry, %NULL if nothing is found. */ -static void *mapping_get_entry (struct address_space *mapping,pgoff_t index) +void *filemap_get_entry (struct address_space *mapping,pgoff_t index) {XA_STATE (xas, &mapping->i_pages, index);struct folio *folio;
@@ -1915,7 +1915,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,struct folio *folio; repeat: - folio =mapping_get_entry (mapping, index); + folio =filemap_get_entry (mapping, index);if (xa_is_value (folio)) {if (fgp_flags &FGP_ENTRY )return folio;
Signature · 1 line
2.39.0
Re: [PATCH 2/9] mm: make mapping_get_entry available outside of filemap.c
Quoted 4 lines · Christoph Hellwig
mapping_get_entryis useful for page cache API users that need to know aboutxa_valueinternals. Rename it and make it available inpagemap.h.Signed-off-by Christoph Hellwig <[email protected]>
PATCH 3/9 mm: use filemap_get_entry in filemap_get_incore_folio
In reply to Christoph Hellwigfilemap_get_incore_folio wants to look at the details of xa_is_value entries, but doesn't need any of the other logic in filemap_get_folio. Switch it to use the lower-level filemap_get_entry interface.
--- mm/swap_state.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/swap_state.c +1 -1
@@ -380,7 +380,7 @@ struct folio *filemap_get_incore_folio(struct address_space *mapping, {swp_entry_t swp;struct swap_info_struct *si; -struct folio *folio =__filemap_get_folio (mapping, index,FGP_ENTRY ,0 ); +struct folio *folio =filemap_get_entry (mapping, index);if (!xa_is_value (folio)) goto out;
Signature · 1 line
2.39.0
Re: [PATCH 3/9] mm: use filemap_get_entry in filemap_get_incore_folio
Quoted 5 lines · Christoph Hellwig
filemap_get_incore_foliowants to look at the details ofxa_is_valueentries, but doesn't need any of the other logic infilemap_get_folio. Switch it to use the lower-levelfilemap_get_entryinterface.Signed-off-by Christoph Hellwig <[email protected]>
PATCH 4/9 shmem: remove shmem_get_partial_folio
In reply to Christoph HellwigAdd a new SGP_FIND mode for shmem_get_partial_folio that works like SGP_READ, but does not check i_size. Use that instead of open coding the page cache lookup in shmem_get_partial_folio. Note that this is a behavior change in that it reads in swap cache entries for offsets outside i_size, possibly causing a little bit of extra work.
--- include/linux/shmem_fs.h | 1 + mm/shmem.c | 46 ++++++++++++---------------------------- 2 files changed, 15 insertions(+), 32 deletions(-)
include/linux/shmem_fs.h +1 -0
@@ -105,6 +105,7 @@ enum sgp_type {SGP_CACHE ,/* don't exceed i_size, may allocate page */ SGP_WRITE ,/* may exceed i_size, may allocate !Uptodate page */ SGP_FALLOC ,/* like SGP_WRITE, but make existing page Uptodate */ + SGP_FIND, /* like SGP_READ, but also read outside i_size */ }; int shmem_get_folio(struct inode *inode, pgoff_t index, struct folio **foliop,
mm/shmem.c +14 -32
76 lines not on this page · raw message
Signature · 1 line
2.39.0
Re: [PATCH 4/9] shmem: remove shmem_get_partial_folio
Quoted 32 lines · Christoph Hellwig
Add a new
SGP_FINDmode forshmem_get_partial_foliothat works likeSGP_READ, but does not checki_size. Use that instead of open coding the page cache lookup inshmem_get_partial_folio. Note that this is a behavior change in that it reads in swap cache entries for offsets outsidei_size, possibly causing a little bit of extra work.Signed-off-by Christoph Hellwig <[email protected]>--- include/linux/shmem_fs.h | 1 + mm/shmem.c | 46 ++++++++++++---------------------------- 2 files changed, 15 insertions(+), 32 deletions(-)include/linux/shmem_fs.h +1
-0@@ -105,6 +105,7 @@ enum sgp_type {SGP_CACHE ,/* don't exceed i_size, may allocate page */ SGP_WRITE ,/* may exceed i_size, may allocate !Uptodate page */ SGP_FALLOC ,/* like SGP_WRITE, but make existing page Uptodate */ + SGP_FIND, /* like SGP_READ, but also read outside i_size */ }; int shmem_get_folio(struct inode *inode, pgoff_t index, struct folio **foliop,mm/shmem.c +0
-0@@ -877,27 +877,6 @@ void shmem_unlock_mapping(struct address_space *mapping) } }
mm/shmem.c void shmem_unlock_mapping(struct address_space *mapping) · line 889
Quoted 10 lines
mm/shmem.c +0
-10-static struct folio *shmem_get_partial_folio (struct inode *inode,pgoff_t index) -{ -struct folio *folio; - -/* -* At first avoid shmem_get_folio(,,,SGP_READ): that fails -* beyond i_size, and reports fallocated pages as holes. -*/ - folio =__filemap_get_folio (inode->i_mapping, index, -FGP_ENTRY |FGP_LOCK ,0 );
This all seems reasonable to me at a glance, FWIW, but I am a little curious why this wouldn't split up into two changes. I.e., switch this over to filemap_get_entry() to minimally remove the FGP_ENTRY dependency without a behavior change, then (perhaps after the next patch) introduce SGP_FIND in a separate patch. That makes it easier to review and potentially undo if it happens to pose a problem in the future. Hm?
Brian
Quoted 71 lines
mm/shmem.c +14
-22-if (!xa_is_value (folio)) -return folio; -/* -* But read a page back from swap if any of it is within i_size -* (although in some cases this is just a waste of time). -*/ - folio =NULL ; -shmem_get_folio (inode, index, &folio,SGP_READ ); -return folio; -} -/* * Remove range of pages and swap entries from page cache, and free them. * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. @@ -957,7 +936,8 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, goto whole_folios; same_folio = (lstart >>PAGE_SHIFT ) == (lend >>PAGE_SHIFT ); - folio =shmem_get_partial_folio (inode, lstart >>PAGE_SHIFT ); + folio =NULL ; +shmem_get_folio (inode, lstart >>PAGE_SHIFT , &folio,SGP_FIND );if (folio) { same_folio = lend <folio_pos (folio) +folio_size (folio);folio_mark_dirty (folio);@@ -971,14 +951,16 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, folio =NULL ; } -if (!same_folio) - folio =shmem_get_partial_folio (inode, lend >>PAGE_SHIFT ); -if (folio) { -folio_mark_dirty (folio); -if (!truncate_inode_partial_folio (folio, lstart, lend)) - end = folio->index; -folio_unlock (folio); -folio_put (folio); +if (!same_folio) { + folio =NULL ; +shmem_get_folio (inode, lend >>PAGE_SHIFT , &folio,SGP_FIND ); +if (folio) { +folio_mark_dirty (folio); +if (!truncate_inode_partial_folio (folio, lstart, lend)) + end = folio->index; +folio_unlock (folio); +folio_put (folio); + } } whole_folios:@@ -1900,7 +1882,7 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index,if (folio_test_uptodate (folio)) goto out;/* fallocated folio */ -if (sgp !=SGP_READ ) +if (sgp !=SGP_READ && sgp !=SGP_FIND ) goto clear;folio_unlock (folio);folio_put (folio);@@ -1911,7 +1893,7 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index,* SGP_NOALLOC: fail on hole, with NULL folio, letting caller fail. */ *foliop =NULL ; -if (sgp ==SGP_READ ) +if (sgp ==SGP_READ || sgp ==SGP_FIND )return 0 ;if (sgp ==SGP_NOALLOC )return -ENOENT ;Signature · 1 line
2.39.0
Re: [PATCH 4/9] shmem: remove shmem_get_partial_folio
Quoted 6 lines · Brian Foster
This all seems reasonable to me at a glance, FWIW, but I am a little curious why this wouldn't split up into two changes. I.e., switch this over to
filemap_get_entry()to minimally remove theFGP_ENTRYdependency without a behavior change, then (perhaps after the next patch) introduceSGP_FINDin a separate patch. That makes it easier to review and potentially undo if it happens to pose a problem in the future. Hm?
The minimal change to filemap_get_entry would require to add the lock, check mapping and retry loop and thus add a fair amount of code. So I looked for ways to avoid that and came up with this version. But if there is a strong preference to first open code the logic and then later consolidate it I could do that.
Re: [PATCH 4/9] shmem: remove shmem_get_partial_folio
Quoted 8 lines · Christoph Hellwig
Quoted 6 lines · Brian Foster
This all seems reasonable to me at a glance, FWIW, but I am a little curious why this wouldn't split up into two changes. I.e., switch this over to
filemap_get_entry()to minimally remove theFGP_ENTRYdependency without a behavior change, then (perhaps after the next patch) introduceSGP_FINDin a separate patch. That makes it easier to review and potentially undo if it happens to pose a problem in the future. Hm?
Quoted 6 lines
The minimal change to
filemap_get_entrywould require to add the lock, check mapping and retry loop and thus add a fair amount of code. So I looked for ways to avoid that and came up with this version. But if there is a strong preference to first open code the logic and then later consolidate it I could do that.
Ok. Not a strong preference from me. I don't think it's worth complicating that much just to split up.
Brian
PATCH 5/9 shmem: open code the page cache lookup in shmem_get_folio_gfp
In reply to Christoph HellwigUse the very low level filemap_get_entry helper to look up the entry in the xarray, and then:
- don't bother locking the folio if only doing a userfault notification
- open code locking the page and checking for truncation in a related code block
This will allow to eventually remove the FGP_ENTRY flag.
--- mm/shmem.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
mm/shmem.c +10 -4
@@ -1856,12 +1856,10 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, sbinfo =SHMEM_SB (inode->i_sb); charge_mm = vma ? vma->vm_mm :NULL ; - folio =__filemap_get_folio (mapping, index,FGP_ENTRY |FGP_LOCK ,0 ); + folio =filemap_get_entry (mapping, index);if (folio && vma &&userfaultfd_minor (vma)) { -if (!xa_is_value (folio)) { -folio_unlock (folio); +if (!xa_is_value (folio))folio_put (folio); - } *fault_type =handle_userfault (vmf,VM_UFFD_MINOR );return 0 ; }
@@ -1877,6 +1875,14 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, }if (folio) { +folio_lock (folio); + +/* Has the page been truncated? */ +if (unlikely (folio->mapping != mapping)) { +folio_unlock (folio); +folio_put (folio); + goto repeat; + }if (sgp ==SGP_WRITE )folio_mark_accessed (folio);if (folio_test_uptodate (folio))
Signature · 1 line
2.39.0
PATCH 6/9 mm: remove FGP_ENTRY
In reply to Christoph HellwigFGP_ENTRY is unused now, so remove it.
--- include/linux/pagemap.h | 3 +-- mm/filemap.c | 7 +------ mm/folio-compat.c | 4 ++-- 3 files changed, 4 insertions(+), 10 deletions(-)
include/linux/pagemap.h +1 -2
@@ -504,8 +504,7 @@ pgoff_t page_cache_prev_miss(struct address_space *mapping,#define FGP_NOFS 0x00000010#define FGP_NOWAIT 0x00000020#define FGP_FOR_MMAP 0x00000040 -#define FGP_ENTRY 0x00000080 -#define FGP_STABLE 0x00000100 +#define FGP_STABLE 0x00000080void *filemap_get_entry (struct address_space *mapping,pgoff_t index); struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
mm/filemap.c +1 -6
@@ -1889,8 +1889,6 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index)* * * %FGP_ACCESSED - The folio will be marked accessed. * * %FGP_LOCK - The folio is returned locked. -* * %FGP_ENTRY - If there is a shadow / swap / DAX entry, return it -* instead of allocating a new folio to replace it. * * %FGP_CREAT - If no page is present then a new page is allocated using * @gfp and added to the page cache and the VM's LRU list. * The page is returned locked and with an increased refcount.
@@ -1916,11 +1914,8 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, repeat: folio =filemap_get_entry (mapping, index); -if (xa_is_value (folio)) { -if (fgp_flags &FGP_ENTRY ) -return folio; +if (xa_is_value (folio)) folio =NULL ; - }if (!folio) goto no_page;
mm/folio-compat.c +2 -2
@@ -97,8 +97,8 @@ struct page *pagecache_get_page(struct address_space *mapping, pgoff_t index,struct folio *folio; folio =__filemap_get_folio (mapping, index, fgp_flags, gfp); -if (!folio ||xa_is_value (folio)) -return &folio->page; +if (!folio) +return NULL ;return folio_file_page (folio, index); }EXPORT_SYMBOL (pagecache_get_page);
Signature · 1 line
2.39.0
PATCH 7/9 gfs2: handle a NULL folio in gfs2_jhead_process_page
In reply to Christoph Hellwigfilemap_get_folio can return NULL, so exit early for that case.
--- fs/gfs2/lops.c | 2 ++ 1 file changed, 2 insertions(+)
fs/gfs2/lops.c +2 -0
@@ -472,6 +472,8 @@ static void gfs2_jhead_process_page(struct gfs2_jdesc *jd, unsigned long index,struct folio *folio; folio =filemap_get_folio (jd->jd_inode->i_mapping, index); +if (!folio) +return ;folio_wait_locked (folio);if (folio_test_error (folio))
Signature · 1 line
2.39.0
Matthew Wilcox
maintainerRe: [PATCH 7/9] gfs2: handle a NULL folio in gfs2_jhead_process_page
Quoted 1 line · Christoph Hellwig
filemap_get_foliocan return NULL, so exit early for that case.
I'm not sure it can return NULL in this specific case. As I understand this code, we're scanning the journal looking for the log head. We've just submitted the bio to read this page. I suppose memory pressure could theoretically push the page out, but if it does, we're doing the wrong thing by just returning here; we need to retry reading the page.
Assuming we're not willing to do the work to add that case, I think I'd rather see the crash in folio_wait_locked() than get data corruption from failing to find the head of the log.
Quoted 21 lines
Signed-off-by Christoph Hellwig <[email protected]>--- fs/gfs2/lops.c | 2 ++ 1 file changed, 2 insertions(+)fs/gfs2/lops.c +2
-0@@ -472,6 +472,8 @@ static void gfs2_jhead_process_page(struct gfs2_jdesc *jd, unsigned long index,struct folio *folio; folio =filemap_get_folio (jd->jd_inode->i_mapping, index); +if (!folio) +return ;folio_wait_locked (folio);if (folio_test_error (folio))Signature · 1 line
2.39.0
Andreas Gruenbacher
maintainerRe: [Cluster-devel] [PATCH 7/9] gfs2: handle a NULL folio in gfs2_jhead_process_page
[Christoph's email ended up in my spam folder; I hope that was a one-time-only occurrence.]
Quoted 25 lines · Matthew Wilcox
Quoted 1 line · Christoph Hellwig
filemap_get_foliocan return NULL, so exit early for that case.I'm not sure it can return NULL in this specific case. As I understand this code, we're scanning the journal looking for the log head. We've just submitted the bio to read this page. I suppose memory pressure could theoretically push the page out, but if it does, we're doing the wrong thing by just returning here; we need to retry reading the page.
Assuming we're not willing to do the work to add that case, I think I'd rather see the crash in
folio_wait_locked()than get data corruption from failing to find the head of the log.Quoted 12 lines
Signed-off-by Christoph Hellwig <[email protected]>--- fs/gfs2/lops.c | 2 ++ 1 file changed, 2 insertions(+)fs/gfs2/lops.c +0
-0@@ -472,6 +472,8 @@ static void gfs2_jhead_process_page(struct gfs2_jdesc *jd, unsigned long index,struct folio *folio;
Quoted 3 lines
fs/gfs2/lops.c static void gfs2_jhead_process_page(struct gfs2_jdesc *jd, unsigned long index, · line 476
Quoted 3 lines
fs/gfs2/lops.c +2
-0folio =filemap_get_folio (jd->jd_inode->i_mapping, index); +if (!folio) +return ;
We're actually still holding a reference to the folio from the find_or_create_page() in gfs2_find_jhead() here, so we know that memory pressure can't push the page out and filemap_get_folio() won't return NULL.
Quoted 7 lines
Quoted 6 lines
fs/gfs2/lops.c +0
-1folio_wait_locked (folio);if (folio_test_error (folio)) --2.39.0
Thanks,
Andreas
Re: [Cluster-devel] [PATCH 7/9] gfs2: handle a NULL folio in gfs2_jhead_process_page
Quoted 4 lines · Andreas Gruenbacher
We're actually still holding a reference to the folio from the
find_or_create_page()ingfs2_find_jhead()here, so we know that memory pressure can't push the page out andfilemap_get_folio()won't return NULL.
Ok, I'll drop this patch.
PATCH 8/9 btrfs: handle a NULL folio in extent_range_redirty_for_io
In reply to Christoph Hellwigfilemap_get_folio can return NULL, skip those cases.
--- fs/btrfs/extent_io.c | 2 ++ 1 file changed, 2 insertions(+)
fs/btrfs/extent_io.c +2 -0
@@ -230,6 +230,8 @@ void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)while (index <= end_index) { folio =filemap_get_folio (mapping, index); +if (!folio) +continue ;filemap_dirty_folio (mapping, folio);folio_account_redirty (folio); index +=folio_nr_pages (folio);
Signature · 1 line
2.39.0
Matthew Wilcox
maintainerRe: [PATCH 8/9] btrfs: handle a NULL folio in extent_range_redirty_for_io
Quoted 1 line · Christoph Hellwig
filemap_get_foliocan return NULL, skip those cases.
Hmm, I'm not sure that's true. We have one place that calls extent_range_redirty_for_io(), and it previously calls extent_range_clear_dirty_for_io() which has an explicit
BUG_ON(!page); /* Pages should be in the extent_io_tree */
so I'm going to say this one can't happen either. I haven't delved far enough into btrfs to figure out why it can't happen.
Quoted 21 lines
Signed-off-by Christoph Hellwig <[email protected]>--- fs/btrfs/extent_io.c | 2 ++ 1 file changed, 2 insertions(+)fs/btrfs/extent_io.c +2
-0@@ -230,6 +230,8 @@ void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)while (index <= end_index) { folio =filemap_get_folio (mapping, index); +if (!folio) +continue ;filemap_dirty_folio (mapping, folio);folio_account_redirty (folio); index +=folio_nr_pages (folio);Signature · 1 line
2.39.0
Re: [PATCH 8/9] btrfs: handle a NULL folio in extent_range_redirty_for_io
Quoted 11 lines · Matthew Wilcox
Quoted 1 line · Christoph Hellwig
filemap_get_foliocan return NULL, skip those cases.Hmm, I'm not sure that's true. We have one place that calls
extent_range_redirty_for_io(), and it previously callsextent_range_clear_dirty_for_io()which has an explicit
BUG_ON(!page); /* Pages should be in theextent_io_tree*/so I'm going to say this one can't happen either. I haven't delved far enough into btrfs to figure out why it can't happen.
I'll drop this patch for now.
PATCH 9/9 mm: return an ERR_PTR from __filemap_get_folio
In reply to Christoph HellwigInstead of returning NULL for all errors, distinguish between:
- no entry found and not asked to allocated (-ENOENT)
- failed to allocate memory (-ENOMEM)
- would block (-EAGAIN)
so that callers don't have to guess the error based on the passed in flags.
Also pass through the error through the direct callers:filemap_get_folio, filemap_lock_folio filemap_grab_folio and filemap_get_incore_folio.
--- fs/afs/dir.c | 10 +++++----- fs/afs/dir_edit.c | 2 +- fs/afs/write.c | 4 ++-- fs/btrfs/disk-io.c | 2 +- fs/btrfs/extent_io.c | 2 +- fs/ext4/inode.c | 2 +- fs/ext4/move_extent.c | 8 ++++---- fs/gfs2/lops.c | 2 +- fs/hugetlbfs/inode.c | 2 +- fs/iomap/buffered-io.c | 6 +++--- fs/netfs/buffered_read.c | 4 ++-- fs/nilfs2/page.c | 6 +++--- mm/filemap.c | 14 ++++++++------ mm/folio-compat.c | 2 +- mm/huge_memory.c | 2 +- mm/memcontrol.c | 2 +- mm/mincore.c | 2 +- mm/shmem.c | 4 ++-- mm/swap_state.c | 15 ++++++++------- mm/swapfile.c | 4 ++-- mm/truncate.c | 15 ++++++++------- 21 files changed, 57 insertions(+), 53 deletions(-)
fs/afs/dir.c +5 -5
@@ -319,16 +319,16 @@ static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)struct folio *folio; folio =filemap_get_folio (mapping, i); -if (!folio) { +if (IS_ERR (folio)) {if (test_and_clear_bit (AFS_VNODE_DIR_VALID , &dvnode->flags))afs_stat_v (dvnode, n_inval); - - ret = -ENOMEM ; folio =__filemap_get_folio (mapping, i,FGP_LOCK |FGP_CREAT , mapping->gfp_mask); -if (!folio) +if (IS_ERR (folio)) { + ret =PTR_ERR (folio); goto error; + }folio_attach_private (folio, (void *)1 );folio_unlock (folio); }
@@ -524,7 +524,7 @@ static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,*/ folio =__filemap_get_folio (dir->i_mapping, ctx->pos /PAGE_SIZE ,FGP_ACCESSED ,0 ); -if (!folio) { +if (IS_ERR (folio)) { ret =afs_bad (dvnode, afs_file_error_dir_missing_page);break ; }
fs/afs/dir_edit.c +1 -1
@@ -115,7 +115,7 @@ static struct folio *afs_dir_get_folio(struct afs_vnode *vnode, pgoff_t index) folio =__filemap_get_folio (mapping, index,FGP_LOCK |FGP_ACCESSED |FGP_CREAT , mapping->gfp_mask); -if (!folio) +if (IS_ERR (folio))clear_bit (AFS_VNODE_DIR_VALID , &vnode->flags);else if (folio && !folio_test_private (folio))folio_attach_private (folio, (void *)1 );
fs/afs/write.c +2 -2
@@ -232,7 +232,7 @@ static void afs_kill_pages(struct address_space *mapping,_debug ("kill %lx (to %lx)" , index, last); folio =filemap_get_folio (mapping, index); -if (!folio) { +if (IS_ERR (folio)) { next = index +1 ;continue ; }
@@ -270,7 +270,7 @@ static void afs_redirty_pages(struct writeback_control *wbc,_debug ("redirty %llx @%llx" , len, start); folio =filemap_get_folio (mapping, index); -if (!folio) { +if (IS_ERR (folio)) { next = index +1 ;continue ; }
fs/btrfs/disk-io.c +1 -1
@@ -4034,7 +4034,7 @@ static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) folio =filemap_get_folio (device->bdev->bd_inode->i_mapping, bytenr >>PAGE_SHIFT ); -if (!folio) { +if (IS_ERR (folio)) { errors++;if (i ==0 ) primary_failed = true;
fs/btrfs/extent_io.c +1 -1
@@ -230,7 +230,7 @@ void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)while (index <= end_index) { folio =filemap_get_folio (mapping, index); -if (!folio) +if (IS_ERR (folio))continue ;filemap_dirty_folio (mapping, folio);folio_account_redirty (folio);
fs/ext4/inode.c +1 -1
@@ -5391,7 +5391,7 @@ static void ext4_wait_for_tail_page_commit(struct inode *inode)while (1 ) {struct folio *folio =filemap_lock_folio (inode->i_mapping, inode->i_size >>PAGE_SHIFT ); -if (!folio) +if (IS_ERR (folio))return ; ret =__ext4_journalled_invalidate_folio (folio, offset,folio_size (folio) - offset);
fs/ext4/move_extent.c +4 -4
@@ -141,18 +141,18 @@ mext_folio_double_lock(struct inode *inode1, struct inode *inode2, flags =memalloc_nofs_save (); folio[0 ] =__filemap_get_folio (mapping[0 ], index1, fgp_flags,mapping_gfp_mask (mapping[0 ])); -if (!folio[0 ]) { +if (IS_ERR (folio[0 ])) {memalloc_nofs_restore (flags); -return -ENOMEM ; +return PTR_ERR (folio[0 ]); } folio[1 ] =__filemap_get_folio (mapping[1 ], index2, fgp_flags,mapping_gfp_mask (mapping[1 ]));memalloc_nofs_restore (flags); -if (!folio[1 ]) { +if (IS_ERR (folio[1 ])) {folio_unlock (folio[0 ]);folio_put (folio[0 ]); -return -ENOMEM ; +return PTR_ERR (folio[1 ]); }/* * __filemap_get_folio() may not wait on folio's writeback if
fs/gfs2/lops.c +1 -1
@@ -472,7 +472,7 @@ static void gfs2_jhead_process_page(struct gfs2_jdesc *jd, unsigned long index,struct folio *folio; folio =filemap_get_folio (jd->jd_inode->i_mapping, index); -if (!folio) +if (IS_ERR (folio))return ;folio_wait_locked (folio);
fs/hugetlbfs/inode.c +1 -1
@@ -697,7 +697,7 @@ static void hugetlbfs_zero_partial_page(struct hstate *h,struct folio *folio; folio =filemap_lock_folio (mapping, idx); -if (!folio) +if (IS_ERR (folio))return ; start = start & ~huge_page_mask (h);
fs/iomap/buffered-io.c +3 -3
@@ -614,8 +614,8 @@ static int iomap_write_begin(struct iomap_iter *iter, loff_t pos, folio =__filemap_get_folio (iter->inode->i_mapping, pos >>PAGE_SHIFT , fgp,mapping_gfp_mask (iter->inode->i_mapping)); -if (!folio) { - status = (iter->flags &IOMAP_NOWAIT ) ? -EAGAIN : -ENOMEM ; +if (IS_ERR (folio)) { + status =PTR_ERR (folio); goto out_no_page; }
@@ -882,7 +882,7 @@ static int iomap_write_delalloc_scan(struct inode *inode,/* grab locked page */ folio =filemap_lock_folio (inode->i_mapping, start_byte >>PAGE_SHIFT ); -if (!folio) { +if (IS_ERR (folio)) { start_byte =ALIGN_DOWN (start_byte,PAGE_SIZE ) +PAGE_SIZE ;continue ;
fs/netfs/buffered_read.c +2 -2
@@ -350,8 +350,8 @@ int netfs_write_begin(struct netfs_inode *ctx, retry: folio =__filemap_get_folio (mapping, index, fgp_flags,mapping_gfp_mask (mapping)); -if (!folio) -return -ENOMEM ; +if (IS_ERR (folio)) +return PTR_ERR (folio);if (ctx->ops->check_write_begin) {/* Allow the netfs (eg. ceph) to flush conflicts. */
fs/nilfs2/page.c +3 -3
@@ -259,10 +259,10 @@ int nilfs_copy_dirty_pages(struct address_space *dmap,NILFS_PAGE_BUG (&folio->page,"inconsistent dirty state" ); dfolio =filemap_grab_folio (dmap, folio->index); -if (unlikely (!dfolio)) { +if (unlikely (IS_ERR (dfolio))) {/* No empty page is added to the page cache */ - err = -ENOMEM ;folio_unlock (folio); + err =PTR_ERR (dfolio);break ; }if (unlikely (!folio_buffers (folio)))
@@ -311,7 +311,7 @@ void nilfs_copy_back_pages(struct address_space *dmap,folio_lock (folio); dfolio =filemap_lock_folio (dmap, index); -if (dfolio) { +if (!IS_ERR (dfolio)) {/* overwrite existing folio in the destination cache */ WARN_ON (folio_test_dirty (dfolio));nilfs_copy_page (&dfolio->page, &folio->page,0 );
mm/filemap.c +8 -6
@@ -1905,7 +1905,7 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index)* * If there is a page cache page, it is returned with an increased refcount. * -* Return: The found folio or %NULL otherwise. +* Return: The found folio or an ERR_PTR() otherwise. */ struct folio *__filemap_get_folio (struct address_space *mapping,pgoff_t index,int fgp_flags,gfp_t gfp)
@@ -1923,7 +1923,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,if (fgp_flags &FGP_NOWAIT ) {if (!folio_trylock (folio)) {folio_put (folio); -return NULL ; +return ERR_PTR (-EAGAIN ); } }else {folio_lock (folio);
@@ -1962,7 +1962,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, folio =filemap_alloc_folio (gfp,0 );if (!folio) -return NULL ; +return ERR_PTR (-ENOMEM );if (WARN_ON_ONCE (!(fgp_flags & (FGP_LOCK |FGP_FOR_MMAP )))) fgp_flags |=FGP_LOCK ;
@@ -1987,6 +1987,8 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,folio_unlock (folio); } +if (!folio) +return ERR_PTR (-ENOENT );return folio; }EXPORT_SYMBOL (__filemap_get_folio);
@@ -3126,7 +3128,7 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)* Do we have something in the page cache already? */ folio =filemap_get_folio (mapping, index); -if (likely (folio)) { +if (likely (!IS_ERR (folio))) {/* * We found the page, so try async readahead before waiting for * the lock.
@@ -3155,7 +3157,7 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) folio =__filemap_get_folio (mapping, index,FGP_CREAT |FGP_FOR_MMAP , vmf->gfp_mask); -if (!folio) { +if (IS_ERR (folio)) {if (fpin) goto out_retry;filemap_invalidate_unlock_shared (mapping);
@@ -3506,7 +3508,7 @@ static struct folio *do_read_cache_folio(struct address_space *mapping, filler = mapping->a_ops->read_folio; repeat: folio =filemap_get_folio (mapping, index); -if (!folio) { +if (IS_ERR (folio)) { folio =filemap_alloc_folio (gfp,0 );if (!folio)return ERR_PTR (-ENOMEM );
mm/folio-compat.c +1 -1
@@ -97,7 +97,7 @@ struct page *pagecache_get_page(struct address_space *mapping, pgoff_t index,struct folio *folio; folio =__filemap_get_folio (mapping, index, fgp_flags, gfp); -if (!folio) +if (IS_ERR (folio))return NULL ;return folio_file_page (folio, index); }
mm/huge_memory.c +1 -1
@@ -3103,7 +3103,7 @@ static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start,struct folio *folio =filemap_get_folio (mapping, index); nr_pages =1 ; -if (!folio) +if (IS_ERR (folio))continue ;if (!folio_test_large (folio))
mm/memcontrol.c +1 -1
@@ -5693,7 +5693,7 @@ static struct page *mc_handle_file_pte(struct vm_area_struct *vma,/* shmem/tmpfs may report page out on swap: account for that too. */ index =linear_page_index (vma, addr); folio =filemap_get_incore_folio (vma->vm_file->f_mapping, index); -if (!folio) +if (IS_ERR (folio))return NULL ;return folio_file_page (folio, index); }
mm/mincore.c +1 -1
@@ -61,7 +61,7 @@ static unsigned char mincore_page(struct address_space *mapping, pgoff_t index)* tmpfs's .fault). So swapped out tmpfs mappings are tested here. */ folio =filemap_get_incore_folio (mapping, index); -if (folio) { +if (!IS_ERR (folio)) { present =folio_test_uptodate (folio);folio_put (folio); }
mm/shmem.c +2 -2
@@ -603,7 +603,7 @@ static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, index = (inode->i_size &HPAGE_PMD_MASK ) >>PAGE_SHIFT ; folio =filemap_get_folio (inode->i_mapping, index); -if (!folio) +if (IS_ERR (folio)) goto drop;/* No huge page at the end of the file: nothing to split */
@@ -3187,7 +3187,7 @@ static const char *shmem_get_link(struct dentry *dentry,if (!dentry) { folio =filemap_get_folio (inode->i_mapping,0 ); -if (!folio) +if (IS_ERR (folio))return ERR_PTR (-ECHILD );if (PageHWPoison (folio_page (folio,0 )) || !folio_test_uptodate (folio)) {
mm/swap_state.c +8 -7
@@ -330,7 +330,7 @@ struct folio *swap_cache_get_folio(swp_entry_t entry,struct folio *folio; folio =filemap_get_folio (swap_address_space (entry),swp_offset (entry)); -if (folio) { +if (!IS_ERR (folio)) {bool vma_ra =swap_use_vma_readahead ();bool readahead;
@@ -360,6 +360,8 @@ struct folio *swap_cache_get_folio(swp_entry_t entry,if (!vma || !vma_ra)atomic_inc (&swapin_readahead_hits); } + }else { + folio =NULL ; }return folio;
@@ -383,22 +385,21 @@ struct folio *filemap_get_incore_folio(struct address_space *mapping,struct folio *folio =filemap_get_entry (mapping, index);if (!xa_is_value (folio)) - goto out; +return folio;if (!shmem_mapping (mapping)) -return NULL ; +return ERR_PTR (-ENOENT ); swp =radix_to_swp_entry (folio);/* There might be swapin error entries in shmem mapping. */ if (non_swap_entry (swp)) -return NULL ; +return ERR_PTR (-ENOENT );/* Prevent swapoff from happening to us */ si =get_swap_device (swp);if (!si) -return NULL ; +return ERR_PTR (-ENOENT ); index =swp_offset (swp); folio =filemap_get_folio (swap_address_space (swp), index);put_swap_device (si); -out:return folio; }
@@ -425,7 +426,7 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, folio =filemap_get_folio (swap_address_space (entry),swp_offset (entry));put_swap_device (si); -if (folio) +if (!IS_ERR (folio))return folio_file_page (folio,swp_offset (entry));/*
mm/swapfile.c +2 -2
@@ -136,7 +136,7 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si,int ret =0 ; folio =filemap_get_folio (swap_address_space (entry), offset); -if (!folio) +if (IS_ERR (folio))return 0 ;/* * When this function is called from scan_swap_map_slots() and it's
@@ -2096,7 +2096,7 @@ static int try_to_unuse(unsigned int type) entry =swp_entry (type, i); folio =filemap_get_folio (swap_address_space (entry), i); -if (!folio) +if (IS_ERR (folio))continue ;/*
mm/truncate.c +8 -7
@@ -375,7 +375,7 @@ void truncate_inode_pages_range(struct address_space *mapping, same_folio = (lstart >>PAGE_SHIFT ) == (lend >>PAGE_SHIFT ); folio =__filemap_get_folio (mapping, lstart >>PAGE_SHIFT ,FGP_LOCK ,0 ); -if (folio) { +if (!IS_ERR (folio)) { same_folio = lend <folio_pos (folio) +folio_size (folio);if (!truncate_inode_partial_folio (folio, lstart, lend)) { start = folio->index +folio_nr_pages (folio);
@@ -387,14 +387,15 @@ void truncate_inode_pages_range(struct address_space *mapping, folio =NULL ; } -if (!same_folio) +if (!same_folio) { folio =__filemap_get_folio (mapping, lend >>PAGE_SHIFT ,FGP_LOCK ,0 ); -if (folio) { -if (!truncate_inode_partial_folio (folio, lstart, lend)) - end = folio->index; -folio_unlock (folio); -folio_put (folio); +if (!IS_ERR (folio)) { +if (!truncate_inode_partial_folio (folio, lstart, lend)) + end = folio->index; +folio_unlock (folio); +folio_put (folio); + } } index = start;
Signature · 1 line
2.39.0
Ryusuke Konishi
maintainerRe: [PATCH 9/9] mm: return an ERR_PTR from __filemap_get_folio
Quoted 7 lines · Christoph Hellwig
Instead of returning NULL for all errors, distinguish between:
- no entry found and not asked to allocated (
-ENOENT)
- failed to allocate memory (-ENOMEM)
- would block (-EAGAIN)
Quoted 7 lines
so that callers don't have to guess the error based on the passed in flags.
Also pass through the error through the direct callers:
filemap_get_folio,filemap_lock_foliofilemap_grab_folioandfilemap_get_incore_folio.
As for the comments describing the return values of these callers, isn't it necessary to rewrite the value from NULL in case of errors ?
Regards,
Ryusuke Konishi
Re: [PATCH 9/9] mm: return an ERR_PTR from __filemap_get_folio
Quoted 7 lines · Ryusuke Konishi
Quoted 4 lines
Also pass through the error through the direct callers:
filemap_get_folio,filemap_lock_foliofilemap_grab_folioandfilemap_get_incore_folio.As for the comments describing the return values of these callers, isn't it necessary to rewrite the value from NULL in case of errors ?
Yes, thanks for catching this.