Skip to content

Commit

Permalink
mm: fix missing wake-up event for FSDAX pages
Browse files Browse the repository at this point in the history
FSDAX page refcounts are 1-based, rather than 0-based: if refcount is
1, then the page is freed.  The FSDAX pages can be pinned through GUP,
then they will be unpinned via unpin_user_page() using a folio variant
to put the page, however, folio variants did not consider this special
case, the result will be to miss a wakeup event (like the user of
__fuse_dax_break_layouts()).  This results in a task being permanently
stuck in TASK_INTERRUPTIBLE state.

Since FSDAX pages are only possibly obtained by GUP users, so fix GUP
instead of folio_put() to lower overhead.

Link: https://lkml.kernel.org/r/[email protected]
Fixes: d8ddc09 ("mm/gup: Add gup_put_folio()")
Signed-off-by: Muchun Song <[email protected]>
Suggested-by: Matthew Wilcox <[email protected]>
Cc: Jason Gunthorpe <[email protected]>
Cc: John Hubbard <[email protected]>
Cc: William Kucharski <[email protected]>
Cc: Dan Williams <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
Muchun Song authored and akpm00 committed Jul 18, 2022
1 parent 3fe2895 commit f4f451a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
14 changes: 9 additions & 5 deletions include/linux/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1130,23 +1130,27 @@ static inline bool is_zone_movable_page(const struct page *page)
#if defined(CONFIG_ZONE_DEVICE) && defined(CONFIG_FS_DAX)
DECLARE_STATIC_KEY_FALSE(devmap_managed_key);

bool __put_devmap_managed_page(struct page *page);
static inline bool put_devmap_managed_page(struct page *page)
bool __put_devmap_managed_page_refs(struct page *page, int refs);
static inline bool put_devmap_managed_page_refs(struct page *page, int refs)
{
if (!static_branch_unlikely(&devmap_managed_key))
return false;
if (!is_zone_device_page(page))
return false;
return __put_devmap_managed_page(page);
return __put_devmap_managed_page_refs(page, refs);
}

#else /* CONFIG_ZONE_DEVICE && CONFIG_FS_DAX */
static inline bool put_devmap_managed_page(struct page *page)
static inline bool put_devmap_managed_page_refs(struct page *page, int refs)
{
return false;
}
#endif /* CONFIG_ZONE_DEVICE && CONFIG_FS_DAX */

static inline bool put_devmap_managed_page(struct page *page)
{
return put_devmap_managed_page_refs(page, 1);
}

/* 127: arbitrary random number, small enough to assemble well */
#define folio_ref_zero_or_close_to_overflow(folio) \
((unsigned int) folio_ref_count(folio) + 127u <= 127u)
Expand Down
6 changes: 4 additions & 2 deletions mm/gup.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ static inline struct folio *try_get_folio(struct page *page, int refs)
* belongs to this folio.
*/
if (unlikely(page_folio(page) != folio)) {
folio_put_refs(folio, refs);
if (!put_devmap_managed_page_refs(&folio->page, refs))
folio_put_refs(folio, refs);
goto retry;
}

Expand Down Expand Up @@ -176,7 +177,8 @@ static void gup_put_folio(struct folio *folio, int refs, unsigned int flags)
refs *= GUP_PIN_COUNTING_BIAS;
}

folio_put_refs(folio, refs);
if (!put_devmap_managed_page_refs(&folio->page, refs))
folio_put_refs(folio, refs);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions mm/memremap.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ void free_zone_device_page(struct page *page)
}

#ifdef CONFIG_FS_DAX
bool __put_devmap_managed_page(struct page *page)
bool __put_devmap_managed_page_refs(struct page *page, int refs)
{
if (page->pgmap->type != MEMORY_DEVICE_FS_DAX)
return false;
Expand All @@ -509,9 +509,9 @@ bool __put_devmap_managed_page(struct page *page)
* refcount is 1, then the page is free and the refcount is
* stable because nobody holds a reference on the page.
*/
if (page_ref_dec_return(page) == 1)
if (page_ref_sub_return(page, refs) == 1)
wake_up_var(&page->_refcount);
return true;
}
EXPORT_SYMBOL(__put_devmap_managed_page);
EXPORT_SYMBOL(__put_devmap_managed_page_refs);
#endif /* CONFIG_FS_DAX */

0 comments on commit f4f451a

Please sign in to comment.