^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) .. _page_frags:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) ==============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) Page fragments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) ==============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) A page fragment is an arbitrary-length arbitrary-offset area of memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) which resides within a 0 or higher order compound page. Multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) fragments within that page are individually refcounted, in the page's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) reference counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) The page_frag functions, page_frag_alloc and page_frag_free, provide a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) simple allocation framework for page fragments. This is used by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) network stack and network device drivers to provide a backing region of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) memory for use as either an sk_buff->head, or to be used in the "frags"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) portion of skb_shared_info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) In order to make use of the page fragment APIs a backing page fragment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) cache is needed. This provides a central point for the fragment allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) and tracks allows multiple calls to make use of a cached page. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) advantage to doing this is that multiple calls to get_page can be avoided
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) which can be expensive at allocation time. However due to the nature of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) this caching it is required that any calls to the cache be protected by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) either a per-cpu limitation, or a per-cpu limitation and forcing interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) to be disabled when executing the fragment allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) The network stack uses two separate caches per CPU to handle fragment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) allocation. The netdev_alloc_cache is used by callers making use of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) netdev_alloc_frag and __netdev_alloc_skb calls. The napi_alloc_cache is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) used by callers of the __napi_alloc_frag and __napi_alloc_skb calls. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) main difference between these two calls is the context in which they may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) called. The "netdev" prefixed functions are usable in any context as these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) functions will disable interrupts, while the "napi" prefixed functions are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) only usable within the softirq context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) Many network device drivers use a similar methodology for allocating page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) fragments, but the page fragments are cached at the ring or descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) level. In order to enable these cases it is necessary to provide a generic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) way of tearing down a page cache. For this reason __page_frag_cache_drain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) was implemented. It allows for freeing multiple references from a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) page via a single call. The advantage to doing this is that it allows for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) cleaning up the multiple references that were added to a page in order to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) avoid calling get_page per allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) Alexander Duyck, Nov 29, 2016.