GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing docs.kernel.org — pin_user_pages() and related calls — The Linux Kernel documentation pin_user_pages() and related calls¶Overview¶This document describes the following functions: pin_user_pages() pin_user_pages_fast() pin_user_pages_remote() Basic description of FOLL_PIN¶FOLL_PIN and FOLL_LONGTERM are flags that can be passed to the get_user_pages*() (“gup”) family of functions. FOLL_PIN has significant interactions and interdependencies with FOLL_LONGTERM, so both are covered here. FOLL_PIN is internal to gup, meaning that it should not appear at the gup call sites. This allows the associated wrapper functions (pin_user_pages*() and others) to set the correct combination of these flags, and to check for problems as well. FOLL_LONGTERM, on the other hand, is allowed to be set at the gup call sites. This is in order to avoid creating a large number of wrapper functions to cover all combinations of get*(), pin*(), FOLL_LONGTERM, and more. Also, the pin_user_pages*() APIs are clearly distinct from the get_user_pages*() APIs, so that’s a natural dividing line, and a good point to make separate wrapper calls. In other words, use pin_user_pages*() for DMA-pinned pages, and get_user_pages*() for other cases. There are five cases described later on in this document, to further clarify that concept. FOLL_PIN and FOLL_GET are mutually exclusive for a given gup call. However,
multiple threads and call sites are free to pin the same The FOLL_PIN implementation is nearly the same as FOLL_GET, except that FOLL_PIN uses a different reference counting technique. FOLL_PIN is a prerequisite to FOLL_LONGTERM. Another way of saying that is, FOLL_LONGTERM is a specific case, more restrictive case of FOLL_PIN. Which flags are set by each wrapper¶For these pin_user_pages*() functions, FOLL_PIN is OR’d in with whatever gup
flags the caller provides. The caller is required to pass in a non-null For large folios, the GUP_PIN_COUNTING_BIAS scheme is not used. Instead,
the extra space available in the This approach for large folios avoids the counting upper limit problems that are discussed below. Those limitations would have been aggravated severely by huge pages, because each tail page adds a refcount to the head page. And in fact, testing revealed that, without a separate pincount field, refcount overflows were seen in some huge page stress tests. This also means that huge pages and large folios do not suffer from the false positives problem that is mentioned below.: Function -------- pin_user_pages FOLL_PIN is always set internally by this function. pin_user_pages_fast FOLL_PIN is always set internally by this function. pin_user_pages_remote FOLL_PIN is always set internally by this function. For these get_user_pages*() functions, FOLL_GET might not even be specified.
Behavior is a little more complex than above. If FOLL_GET was not specified,
but the caller passed in a non-null Function -------- get_user_pages FOLL_GET is sometimes set internally by this function. get_user_pages_fast FOLL_GET is sometimes set internally by this function. get_user_pages_remote FOLL_GET is sometimes set internally by this function. Tracking dma-pinned pages¶Some of the key design constraints, and solutions, for tracking dma-pinned pages:
This also leads to limitations: there are only 31-10==21 bits available for a counter that increments 10 bits at a time.
FOLL_PIN, FOLL_GET, FOLL_LONGTERM: when to use which flags¶Thanks to Jan Kara, Vlastimil Babka and several other -mm people, for describing these categories: CASE 1: Direct IO (DIO)¶There are GUP references to pages that are serving
as DIO buffers. These buffers are needed for a relatively short time (so they
are not “long term”). No special synchronization with FOLL_PIN ...but rather than setting FOLL_PIN directly, call sites should use one of the pin_user_pages*() routines that set FOLL_PIN. CASE 2: RDMA¶There are GUP references to pages that are serving as DMA
buffers. These buffers are needed for a long time (“long term”). No special
synchronization with FOLL_PIN | FOLL_LONGTERM NOTE: Some pages, such as DAX pages, cannot be pinned with longterm pins. That’s because DAX pages do not have a separate page cache, and so “pinning” implies locking down file system blocks, which is not (yet) supported in that way. CASE 3: MMU notifier registration, with or without page faulting hardware¶Device drivers can pin pages via get_user_pages*(), and register for mmu notifier callbacks for the memory range. Then, upon receiving a notifier “invalidate range” callback , stop the device from using the range, and unpin the pages. There may be other possible schemes, such as for example explicitly synchronizing against pending IO, that accomplish approximately the same thing. Or, if the hardware supports replayable page faults, then the device driver can avoid pinning entirely (this is ideal), as follows: register for mmu notifier callbacks as above, but instead of stopping the device and unpinning in the callback, simply remove the range from the device’s page tables. Either way, as long as the driver unpins the pages upon mmu notifier callback,
then there is proper synchronization with both filesystem and mm
( CASE 4: Pinning for struct page manipulation only¶If only CASE 5: Pinning in order to write to the data within the page¶Even though neither DMA nor Direct IO is involved, just a simple case of “pin, write to a page’s data, unpin” can cause a problem. Case 5 may be considered a superset of Case 1, plus Case 2, plus anything that invokes that pattern. In other words, if the code is neither Case 1 nor Case 2, it may still require FOLL_PIN, for patterns like this:
folio_maybe_dma_pinned(): the whole point of pinning¶The whole point of marking folios as “DMA-pinned” or “gup-pinned” is to be able
to query, “is this folio DMA-pinned?” That allows code such as What to do in those cases is the subject of a years-long series of discussions and debates (see the References at the end of this document). It’s a TODO item here: fill in the details once that’s worked out. Meanwhile, it’s safe to say that having this available: static inline bool folio_maybe_dma_pinned(struct folio *folio) ...is a prerequisite to solving the long-running gup+DMA problem. Another way of thinking about FOLL_GET, FOLL_PIN, and FOLL_LONGTERM¶Another way of thinking about these flags is as a progression of restrictions:
FOLL_GET is for Unit testing¶You can monitor how many total dma-pinned pages have been acquired and released since the system was booted, via two new /proc/vmstat entries: /proc/vmstat/nr_foll_pin_acquired /proc/vmstat/nr_foll_pin_released Under normal conditions, these two values will be equal unless there are any long-term [R]DMA pins in place, or during pin/unpin transitions.
...the following is expected: nr_foll_pin_released == nr_foll_pin_acquired (...unless it was already out of balance due to a long-term RDMA pin being in place.) Other diagnostics¶
References¶John Hubbard, October, 2019 |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |