mm/gup: Add try_get_folio() and try_grab_folio()

Convert try_get_compound_head() into try_get_folio() and convert
try_grab_compound_head() into try_grab_folio().  Add a temporary
try_grab_compound_head() wrapper around try_grab_folio() to let us
convert callers individually.
Signed-off-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
Reviewed-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Reviewed-by: default avatarWilliam Kucharski <william.kucharski@oracle.com>
parent 0b90ddae
...@@ -47,75 +47,70 @@ static void put_page_refs(struct page *page, int refs) ...@@ -47,75 +47,70 @@ static void put_page_refs(struct page *page, int refs)
} }
/* /*
* Return the compound head page with ref appropriately incremented, * Return the folio with ref appropriately incremented,
* or NULL if that failed. * or NULL if that failed.
*/ */
static inline struct page *try_get_compound_head(struct page *page, int refs) static inline struct folio *try_get_folio(struct page *page, int refs)
{ {
struct page *head; struct folio *folio;
retry: retry:
head = compound_head(page); folio = page_folio(page);
if (WARN_ON_ONCE(folio_ref_count(folio) < 0))
if (WARN_ON_ONCE(page_ref_count(head) < 0))
return NULL; return NULL;
if (unlikely(!page_cache_add_speculative(head, refs))) if (unlikely(!folio_ref_try_add_rcu(folio, refs)))
return NULL; return NULL;
/* /*
* At this point we have a stable reference to the head page; but it * At this point we have a stable reference to the folio; but it
* could be that between the compound_head() lookup and the refcount * could be that between calling page_folio() and the refcount
* increment, the compound page was split, in which case we'd end up * increment, the folio was split, in which case we'd end up
* holding a reference on a page that has nothing to do with the page * holding a reference on a folio that has nothing to do with the page
* we were given anymore. * we were given anymore.
* So now that the head page is stable, recheck that the pages still * So now that the folio is stable, recheck that the page still
* belong together. * belongs to this folio.
*/ */
if (unlikely(compound_head(page) != head)) { if (unlikely(page_folio(page) != folio)) {
put_page_refs(head, refs); folio_put_refs(folio, refs);
goto retry; goto retry;
} }
return head; return folio;
} }
/** /**
* try_grab_compound_head() - attempt to elevate a page's refcount, by a * try_grab_folio() - Attempt to get or pin a folio.
* flags-dependent amount.
*
* Even though the name includes "compound_head", this function is still
* appropriate for callers that have a non-compound @page to get.
*
* @page: pointer to page to be grabbed * @page: pointer to page to be grabbed
* @refs: the value to (effectively) add to the page's refcount * @refs: the value to (effectively) add to the folio's refcount
* @flags: gup flags: these are the FOLL_* flag values. * @flags: gup flags: these are the FOLL_* flag values.
* *
* "grab" names in this file mean, "look at flags to decide whether to use * "grab" names in this file mean, "look at flags to decide whether to use
* FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount. * FOLL_PIN or FOLL_GET behavior, when incrementing the folio's refcount.
* *
* Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the
* same time. (That's true throughout the get_user_pages*() and * same time. (That's true throughout the get_user_pages*() and
* pin_user_pages*() APIs.) Cases: * pin_user_pages*() APIs.) Cases:
* *
* FOLL_GET: page's refcount will be incremented by @refs. * FOLL_GET: folio's refcount will be incremented by @refs.
* *
* FOLL_PIN on compound pages: page's refcount will be incremented by * FOLL_PIN on large folios: folio's refcount will be incremented by
* @refs, and page[1].compound_pincount will be incremented by @refs. * @refs, and its compound_pincount will be incremented by @refs.
* *
* FOLL_PIN on normal pages: page's refcount will be incremented by * FOLL_PIN on single-page folios: folio's refcount will be incremented by
* @refs * GUP_PIN_COUNTING_BIAS. * @refs * GUP_PIN_COUNTING_BIAS.
* *
* Return: head page (with refcount appropriately incremented) for success, or * Return: The folio containing @page (with refcount appropriately
* NULL upon failure. If neither FOLL_GET nor FOLL_PIN was set, that's * incremented) for success, or NULL upon failure. If neither FOLL_GET
* considered failure, and furthermore, a likely bug in the caller, so a warning * nor FOLL_PIN was set, that's considered failure, and furthermore,
* is also emitted. * a likely bug in the caller, so a warning is also emitted.
*/ */
__maybe_unused struct page *try_grab_compound_head(struct page *page, struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags)
int refs, unsigned int flags)
{ {
if (flags & FOLL_GET) if (flags & FOLL_GET)
return try_get_compound_head(page, refs); return try_get_folio(page, refs);
else if (flags & FOLL_PIN) { else if (flags & FOLL_PIN) {
struct folio *folio;
/* /*
* 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
* right zone, so fail and let the caller fall back to the slow * right zone, so fail and let the caller fall back to the slow
...@@ -129,34 +124,38 @@ __maybe_unused struct page *try_grab_compound_head(struct page *page, ...@@ -129,34 +124,38 @@ __maybe_unused struct page *try_grab_compound_head(struct page *page,
* CAUTION: Don't use compound_head() on the page before this * CAUTION: Don't use compound_head() on the page before this
* point, the result won't be stable. * point, the result won't be stable.
*/ */
page = try_get_compound_head(page, refs); folio = try_get_folio(page, refs);
if (!page) if (!folio)
return NULL; return NULL;
/* /*
* When pinning a compound page, use an exact count to * When pinning a large folio, use an exact count to track it.
* track it.
* *
* However, be sure to *also* increment the normal page * However, be sure to *also* increment the normal folio
* refcount field at least once, so that the page really * refcount field at least once, so that the folio really
* is pinned. That's why the refcount from the earlier * is pinned. That's why the refcount from the earlier
* try_get_compound_head() is left intact. * try_get_folio() is left intact.
*/ */
if (PageHead(page)) if (folio_test_large(folio))
atomic_add(refs, compound_pincount_ptr(page)); atomic_add(refs, folio_pincount_ptr(folio));
else else
page_ref_add(page, refs * (GUP_PIN_COUNTING_BIAS - 1)); folio_ref_add(folio,
refs * (GUP_PIN_COUNTING_BIAS - 1));
node_stat_mod_folio(folio, NR_FOLL_PIN_ACQUIRED, refs);
mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_ACQUIRED, return folio;
refs);
return page;
} }
WARN_ON_ONCE(1); WARN_ON_ONCE(1);
return NULL; return NULL;
} }
struct page *try_grab_compound_head(struct page *page,
int refs, unsigned int flags)
{
return &try_grab_folio(page, refs, flags)->page;
}
static void put_compound_head(struct page *page, int refs, unsigned int flags) static void put_compound_head(struct page *page, int refs, unsigned int flags)
{ {
VM_BUG_ON_PAGE(PageTail(page), page); VM_BUG_ON_PAGE(PageTail(page), page);
...@@ -185,7 +184,7 @@ static void put_compound_head(struct page *page, int refs, unsigned int flags) ...@@ -185,7 +184,7 @@ static void put_compound_head(struct page *page, int refs, unsigned int flags)
* @flags: gup flags: these are the FOLL_* flag values. * @flags: gup flags: these are the FOLL_* flag values.
* *
* Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same
* time. Cases: please see the try_grab_compound_head() documentation, with * time. Cases: please see the try_grab_folio() documentation, with
* "refs=1". * "refs=1".
* *
* Return: true for success, or if no action was required (if neither FOLL_PIN * Return: true for success, or if no action was required (if neither FOLL_PIN
......
...@@ -737,4 +737,9 @@ int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, ...@@ -737,4 +737,9 @@ int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
void free_zone_device_page(struct page *page); void free_zone_device_page(struct page *page);
/*
* mm/gup.c
*/
struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags);
#endif /* __MM_INTERNAL_H */ #endif /* __MM_INTERNAL_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment