Skip to content

Commit 3faa52c

Browse files
johnhubbardtorvalds
authored andcommitted
mm/gup: track FOLL_PIN pages
Add tracking of pages that were pinned via FOLL_PIN. This tracking is implemented via overloading of page->_refcount: pins are added by adding GUP_PIN_COUNTING_BIAS (1024) to the refcount. This provides a fuzzy indication of pinning, and it can have false positives (and that's OK). Please see the pre-existing Documentation/core-api/pin_user_pages.rst for details. As mentioned in pin_user_pages.rst, callers who effectively set FOLL_PIN (typically via pin_user_pages*()) are required to ultimately free such pages via unpin_user_page(). Please also note the limitation, discussed in pin_user_pages.rst under the "TODO: for 1GB and larger huge pages" section. (That limitation will be removed in a following patch.) The effect of a FOLL_PIN flag is similar to that of FOLL_GET, and may be thought of as "FOLL_GET for DIO and/or RDMA use". Pages that have been pinned via FOLL_PIN are identifiable via a new function call: bool page_maybe_dma_pinned(struct page *page); What to do in response to encountering such a page, is left to later patchsets. There is discussion about this in [1], [2], [3], and [4]. This also changes a BUG_ON(), to a WARN_ON(), in follow_page_mask(). [1] Some slow progress on get_user_pages() (Apr 2, 2019): https://lwn.net/Articles/784574/ [2] DMA and get_user_pages() (LPC: Dec 12, 2018): https://lwn.net/Articles/774411/ [3] The trouble with get_user_pages() (Apr 30, 2018): https://lwn.net/Articles/753027/ [4] LWN kernel index: get_user_pages(): https://lwn.net/Kernel/Index/#Memory_management-get_user_pages [jhubbard@nvidia.com: add kerneldoc] Link: http://lkml.kernel.org/r/20200307021157.235726-1-jhubbard@nvidia.com [imbrenda@linux.ibm.com: if pin fails, we need to unpin, a simple put_page will not be enough] Link: http://lkml.kernel.org/r/20200306132537.783769-2-imbrenda@linux.ibm.com [akpm@linux-foundation.org: fix put_compound_head defined but not used] Suggested-by: Jan Kara <jack@suse.cz> Suggested-by: Jérôme Glisse <jglisse@redhat.com> Signed-off-by: John Hubbard <jhubbard@nvidia.com> Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Reviewed-by: Jan Kara <jack@suse.cz> Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Ira Weiny <ira.weiny@intel.com> Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Christoph Hellwig <hch@infradead.org> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Dave Chinner <david@fromorbit.com> Cc: Jason Gunthorpe <jgg@ziepe.ca> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Michal Hocko <mhocko@suse.com> Cc: Mike Kravetz <mike.kravetz@oracle.com> Cc: Shuah Khan <shuah@kernel.org> Cc: Vlastimil Babka <vbabka@suse.cz> Link: http://lkml.kernel.org/r/20200211001536.1027652-7-jhubbard@nvidia.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 94202f1 commit 3faa52c

5 files changed

Lines changed: 379 additions & 104 deletions

File tree

‎Documentation/core-api/pin_user_pages.rst‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ CASE 4: Pinning for struct page manipulation only
173173
-------------------------------------------------
174174
Here, normal GUP calls are sufficient, so neither flag needs to be set.
175175

176-
page_dma_pinned(): the whole point of pinning
177-
=============================================
176+
page_maybe_dma_pinned(): the whole point of pinning
177+
===================================================
178178

179179
The whole point of marking pages as "DMA-pinned" or "gup-pinned" is to be able
180180
to query, "is this page DMA-pinned?" That allows code such as page_mkclean()
@@ -186,7 +186,7 @@ and debates (see the References at the end of this document). It's a TODO item
186186
here: fill in the details once that's worked out. Meanwhile, it's safe to say
187187
that having this available: ::
188188

189-
static inline bool page_dma_pinned(struct page *page)
189+
static inline bool page_maybe_dma_pinned(struct page *page)
190190

191191
...is a prerequisite to solving the long-running gup+DMA problem.
192192

‎include/linux/mm.h‎

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,8 @@ static inline void get_page(struct page *page)
10011001
page_ref_inc(page);
10021002
}
10031003

1004+
bool __must_check try_grab_page(struct page *page, unsigned int flags);
1005+
10041006
static inline __must_check bool try_get_page(struct page *page)
10051007
{
10061008
page = compound_head(page);
@@ -1029,29 +1031,79 @@ static inline void put_page(struct page *page)
10291031
__put_page(page);
10301032
}
10311033

1032-
/**
1033-
* unpin_user_page() - release a gup-pinned page
1034-
* @page: pointer to page to be released
1034+
/*
1035+
* GUP_PIN_COUNTING_BIAS, and the associated functions that use it, overload
1036+
* the page's refcount so that two separate items are tracked: the original page
1037+
* reference count, and also a new count of how many pin_user_pages() calls were
1038+
* made against the page. ("gup-pinned" is another term for the latter).
1039+
*
1040+
* With this scheme, pin_user_pages() becomes special: such pages are marked as
1041+
* distinct from normal pages. As such, the unpin_user_page() call (and its
1042+
* variants) must be used in order to release gup-pinned pages.
1043+
*
1044+
* Choice of value:
1045+
*
1046+
* By making GUP_PIN_COUNTING_BIAS a power of two, debugging of page reference
1047+
* counts with respect to pin_user_pages() and unpin_user_page() becomes
1048+
* simpler, due to the fact that adding an even power of two to the page
1049+
* refcount has the effect of using only the upper N bits, for the code that
1050+
* counts up using the bias value. This means that the lower bits are left for
1051+
* the exclusive use of the original code that increments and decrements by one
1052+
* (or at least, by much smaller values than the bias value).
10351053
*
1036-
* Pages that were pinned via pin_user_pages*() must be released via either
1037-
* unpin_user_page(), or one of the unpin_user_pages*() routines. This is so
1038-
* that eventually such pages can be separately tracked and uniquely handled. In
1039-
* particular, interactions with RDMA and filesystems need special handling.
1054+
* Of course, once the lower bits overflow into the upper bits (and this is
1055+
* OK, because subtraction recovers the original values), then visual inspection
1056+
* no longer suffices to directly view the separate counts. However, for normal
1057+
* applications that don't have huge page reference counts, this won't be an
1058+
* issue.
10401059
*
1041-
* unpin_user_page() and put_page() are not interchangeable, despite this early
1042-
* implementation that makes them look the same. unpin_user_page() calls must
1043-
* be perfectly matched up with pin*() calls.
1060+
* Locking: the lockless algorithm described in page_cache_get_speculative()
1061+
* and page_cache_gup_pin_speculative() provides safe operation for
1062+
* get_user_pages and page_mkclean and other calls that race to set up page
1063+
* table entries.
10441064
*/
1045-
static inline void unpin_user_page(struct page *page)
1046-
{
1047-
put_page(page);
1048-
}
1065+
#define GUP_PIN_COUNTING_BIAS (1U << 10)
10491066

1067+
void unpin_user_page(struct page *page);
10501068
void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,
10511069
bool make_dirty);
1052-
10531070
void unpin_user_pages(struct page **pages, unsigned long npages);
10541071

1072+
/**
1073+
* page_maybe_dma_pinned() - report if a page is pinned for DMA.
1074+
*
1075+
* This function checks if a page has been pinned via a call to
1076+
* pin_user_pages*().
1077+
*
1078+
* For non-huge pages, the return value is partially fuzzy: false is not fuzzy,
1079+
* because it means "definitely not pinned for DMA", but true means "probably
1080+
* pinned for DMA, but possibly a false positive due to having at least
1081+
* GUP_PIN_COUNTING_BIAS worth of normal page references".
1082+
*
1083+
* False positives are OK, because: a) it's unlikely for a page to get that many
1084+
* refcounts, and b) all the callers of this routine are expected to be able to
1085+
* deal gracefully with a false positive.
1086+
*
1087+
* For more information, please see Documentation/vm/pin_user_pages.rst.
1088+
*
1089+
* @page: pointer to page to be queried.
1090+
* @Return: True, if it is likely that the page has been "dma-pinned".
1091+
* False, if the page is definitely not dma-pinned.
1092+
*/
1093+
static inline bool page_maybe_dma_pinned(struct page *page)
1094+
{
1095+
/*
1096+
* page_ref_count() is signed. If that refcount overflows, then
1097+
* page_ref_count() returns a negative value, and callers will avoid
1098+
* further incrementing the refcount.
1099+
*
1100+
* Here, for that overflow case, use the signed bit to count a little
1101+
* bit higher via unsigned math, and thus still get an accurate result.
1102+
*/
1103+
return ((unsigned int)page_ref_count(compound_head(page))) >=
1104+
GUP_PIN_COUNTING_BIAS;
1105+
}
1106+
10551107
#if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP)
10561108
#define SECTION_IN_PAGE_FLAGS
10571109
#endif

0 commit comments

Comments
 (0)