mm/gup.c: reorganize try_get_folio()

try_get_folio() takes in a page, then chooses to do some folio operations
based on the flags (either FOLL_GET or FOLL_PIN).  We can rewrite this
function to be more purpose oriented.

After calling try_get_folio(), if neither FOLL_GET nor FOLL_PIN are set,
warn and fail.  If FOLL_GET is set we can return the result.  If FOLL_GET
is not set then FOLL_PIN is set, so we pin the folio.

This change assists with folio conversions, and makes the function more
readable.

Link: https://lkml.kernel.org/r/20230614021312.34085-5-vishal.moola@gmail.com
Signed-off-by: Vishal Moola (Oracle) <vishal.moola@gmail.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
Vishal Moola (Oracle) 2023-06-13 19:13:11 -07:00 committed by Andrew Morton
parent c9223a4aed
commit 503670ee6d

View file

@ -124,13 +124,22 @@ static inline struct folio *try_get_folio(struct page *page, int refs)
*/ */
struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags) struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags)
{ {
struct folio *folio;
if (WARN_ON_ONCE((flags & (FOLL_GET | FOLL_PIN)) == 0))
return NULL;
if (unlikely(!(flags & FOLL_PCI_P2PDMA) && is_pci_p2pdma_page(page))) if (unlikely(!(flags & FOLL_PCI_P2PDMA) && is_pci_p2pdma_page(page)))
return NULL; return NULL;
folio = try_get_folio(page, refs);
if (flags & FOLL_GET) if (flags & FOLL_GET)
return try_get_folio(page, refs); return folio;
else if (flags & FOLL_PIN) {
struct folio *folio; /* FOLL_PIN is set */
if (!folio)
return NULL;
/* /*
* Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a * Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a
@ -138,16 +147,11 @@ struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags)
* path. * path.
*/ */
if (unlikely((flags & FOLL_LONGTERM) && if (unlikely((flags & FOLL_LONGTERM) &&
!is_longterm_pinnable_page(page))) !folio_is_longterm_pinnable(folio))) {
return NULL; if (!put_devmap_managed_page_refs(&folio->page, refs))
folio_put_refs(folio, refs);
/*
* CAUTION: Don't use compound_head() on the page before this
* point, the result won't be stable.
*/
folio = try_get_folio(page, refs);
if (!folio)
return NULL; return NULL;
}
/* /*
* When pinning a large folio, use an exact count to track it. * When pinning a large folio, use an exact count to track it.
@ -174,10 +178,6 @@ struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags)
return folio; return folio;
} }
WARN_ON_ONCE(1);
return NULL;
}
static void gup_put_folio(struct folio *folio, int refs, unsigned int flags) static void gup_put_folio(struct folio *folio, int refs, unsigned int flags)
{ {
if (flags & FOLL_PIN) { if (flags & FOLL_PIN) {