* [PATCH] dax: Fix ENOMEM handling in grab_mapping_entry()
@ 2021-06-22 16:00 Jan Kara
2021-06-25 22:54 ` Dan Williams
0 siblings, 1 reply; 2+ messages in thread
From: Jan Kara @ 2021-06-22 16:00 UTC (permalink / raw)
To: linux-fsdevel
Cc: Matthew Wilcox, Aneesh Kumar K.V, Dan Williams, Andrew Morton, Jan Kara
grab_mapping_entry() has a bug in handling of ENOMEM condition. Suppose
we have a PMD entry at index I which we are downgrading to a PTE entry.
grab_mapping_entry() will set pmd_downgrade to true, lock the entry,
clear the entry in xarray, and decrement mapping->nrpages. The it will
call:
entry = dax_make_entry(pfn_to_pfn_t(0), flags);
dax_lock_entry(xas, entry);
which inserts new PTE entry into xarray. However this may fail
allocating the new node. We handle this by:
if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM))
goto retry;
however pmd_downgrade stays set to true even though 'entry' returned
from get_unlocked_entry() will be NULL now. And we will go again through
the downgrade branch. This is mostly harmless except that
mapping->nrpages is decremented again and we temporarily have invalid
entry stored in xarray. Fix the problem by setting pmd_downgrade to
false each time we lookup the entry we work with so that it matches
the entry we found.
Fixes: b15cd800682f ("dax: Convert page fault handlers to XArray")
Signed-off-by: Jan Kara <jack@suse.cz>
---
fs/dax.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/fs/dax.c b/fs/dax.c
index 62352cbcf0f4..da41f9363568 100644
--- a/fs/dax.c
+++ b/fs/dax.c
@@ -488,10 +488,11 @@ static void *grab_mapping_entry(struct xa_state *xas,
struct address_space *mapping, unsigned int order)
{
unsigned long index = xas->xa_index;
- bool pmd_downgrade = false; /* splitting PMD entry into PTE entries? */
+ bool pmd_downgrade; /* splitting PMD entry into PTE entries? */
void *entry;
retry:
+ pmd_downgrade = false;
xas_lock_irq(xas);
entry = get_unlocked_entry(xas, order);
--
2.26.2
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] dax: Fix ENOMEM handling in grab_mapping_entry()
2021-06-22 16:00 [PATCH] dax: Fix ENOMEM handling in grab_mapping_entry() Jan Kara
@ 2021-06-25 22:54 ` Dan Williams
0 siblings, 0 replies; 2+ messages in thread
From: Dan Williams @ 2021-06-25 22:54 UTC (permalink / raw)
To: Jan Kara; +Cc: linux-fsdevel, Matthew Wilcox, Aneesh Kumar K.V, Andrew Morton
Hi Jan,
I was out for a few days...
On Tue, Jun 22, 2021 at 9:00 AM Jan Kara <jack@suse.cz> wrote:
>
> grab_mapping_entry() has a bug in handling of ENOMEM condition. Suppose
> we have a PMD entry at index I which we are downgrading to a PTE entry.
> grab_mapping_entry() will set pmd_downgrade to true, lock the entry,
> clear the entry in xarray, and decrement mapping->nrpages. The it will
> call:
>
> entry = dax_make_entry(pfn_to_pfn_t(0), flags);
> dax_lock_entry(xas, entry);
>
> which inserts new PTE entry into xarray. However this may fail
> allocating the new node. We handle this by:
>
> if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM))
> goto retry;
>
> however pmd_downgrade stays set to true even though 'entry' returned
> from get_unlocked_entry() will be NULL now. And we will go again through
> the downgrade branch. This is mostly harmless except that
> mapping->nrpages is decremented again and we temporarily have invalid
> entry stored in xarray. Fix the problem by setting pmd_downgrade to
> false each time we lookup the entry we work with so that it matches
> the entry we found.
>
> Fixes: b15cd800682f ("dax: Convert page fault handlers to XArray")
> Signed-off-by: Jan Kara <jack@suse.cz>
Looks good to me:
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
I notice that Andrew already picked this up and I'm ok to let it
percolate up to Linus through -mm.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-06-25 22:54 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-22 16:00 [PATCH] dax: Fix ENOMEM handling in grab_mapping_entry() Jan Kara
2021-06-25 22:54 ` Dan Williams
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).