page_pool.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =============
  3. Page Pool API
  4. =============
  5. .. kernel-doc:: include/net/page_pool/helpers.h
  6. :doc: page_pool allocator
  7. Architecture overview
  8. =====================
  9. .. code-block:: none
  10. +------------------+
  11. | Driver |
  12. +------------------+
  13. ^
  14. |
  15. |
  16. |
  17. v
  18. +--------------------------------------------+
  19. | request memory |
  20. +--------------------------------------------+
  21. ^ ^
  22. | |
  23. | Pool empty | Pool has entries
  24. | |
  25. v v
  26. +-----------------------+ +------------------------+
  27. | alloc (and map) pages | | get page from cache |
  28. +-----------------------+ +------------------------+
  29. ^ ^
  30. | |
  31. | cache available | No entries, refill
  32. | | from ptr-ring
  33. | |
  34. v v
  35. +-----------------+ +------------------+
  36. | Fast cache | | ptr-ring cache |
  37. +-----------------+ +------------------+
  38. Monitoring
  39. ==========
  40. Information about page pools on the system can be accessed via the netdev
  41. genetlink family (see Documentation/netlink/specs/netdev.yaml).
  42. API interface
  43. =============
  44. The number of pools created **must** match the number of hardware queues
  45. unless hardware restrictions make that impossible. This would otherwise beat the
  46. purpose of page pool, which is allocate pages fast from cache without locking.
  47. This lockless guarantee naturally comes from running under a NAPI softirq.
  48. The protection doesn't strictly have to be NAPI, any guarantee that allocating
  49. a page will cause no race conditions is enough.
  50. .. kernel-doc:: net/core/page_pool.c
  51. :identifiers: page_pool_create
  52. .. kernel-doc:: include/net/page_pool/types.h
  53. :identifiers: struct page_pool_params
  54. .. kernel-doc:: include/net/page_pool/helpers.h
  55. :identifiers: page_pool_put_page page_pool_put_full_page
  56. page_pool_recycle_direct page_pool_free_va
  57. page_pool_dev_alloc_pages page_pool_dev_alloc_frag
  58. page_pool_dev_alloc page_pool_dev_alloc_va
  59. page_pool_get_dma_addr page_pool_get_dma_dir
  60. .. kernel-doc:: net/core/page_pool.c
  61. :identifiers: page_pool_put_page_bulk page_pool_get_stats
  62. DMA sync
  63. --------
  64. Driver is always responsible for syncing the pages for the CPU.
  65. Drivers may choose to take care of syncing for the device as well
  66. or set the ``PP_FLAG_DMA_SYNC_DEV`` flag to request that pages
  67. allocated from the page pool are already synced for the device.
  68. If ``PP_FLAG_DMA_SYNC_DEV`` is set, the driver must inform the core what portion
  69. of the buffer has to be synced. This allows the core to avoid syncing the entire
  70. page when the drivers knows that the device only accessed a portion of the page.
  71. Most drivers will reserve headroom in front of the frame. This part
  72. of the buffer is not touched by the device, so to avoid syncing
  73. it drivers can set the ``offset`` field in struct page_pool_params
  74. appropriately.
  75. For pages recycled on the XDP xmit and skb paths the page pool will
  76. use the ``max_len`` member of struct page_pool_params to decide how
  77. much of the page needs to be synced (starting at ``offset``).
  78. When directly freeing pages in the driver (page_pool_put_page())
  79. the ``dma_sync_size`` argument specifies how much of the buffer needs
  80. to be synced.
  81. If in doubt set ``offset`` to 0, ``max_len`` to ``PAGE_SIZE`` and
  82. pass -1 as ``dma_sync_size``. That combination of arguments is always
  83. correct.
  84. Note that the syncing parameters are for the entire page.
  85. This is important to remember when using fragments (``PP_FLAG_PAGE_FRAG``),
  86. where allocated buffers may be smaller than a full page.
  87. Unless the driver author really understands page pool internals
  88. it's recommended to always use ``offset = 0``, ``max_len = PAGE_SIZE``
  89. with fragmented page pools.
  90. Stats API and structures
  91. ------------------------
  92. If the kernel is configured with ``CONFIG_PAGE_POOL_STATS=y``, the API
  93. page_pool_get_stats() and structures described below are available.
  94. It takes a pointer to a ``struct page_pool`` and a pointer to a struct
  95. page_pool_stats allocated by the caller.
  96. Older drivers expose page pool statistics via ethtool or debugfs.
  97. The same statistics are accessible via the netlink netdev family
  98. in a driver-independent fashion.
  99. .. kernel-doc:: include/net/page_pool/types.h
  100. :identifiers: struct page_pool_recycle_stats
  101. struct page_pool_alloc_stats
  102. struct page_pool_stats
  103. Coding examples
  104. ===============
  105. Registration
  106. ------------
  107. .. code-block:: c
  108. /* Page pool registration */
  109. struct page_pool_params pp_params = { 0 };
  110. struct xdp_rxq_info xdp_rxq;
  111. int err;
  112. pp_params.order = 0;
  113. /* internal DMA mapping in page_pool */
  114. pp_params.flags = PP_FLAG_DMA_MAP;
  115. pp_params.pool_size = DESC_NUM;
  116. pp_params.nid = NUMA_NO_NODE;
  117. pp_params.dev = priv->dev;
  118. pp_params.napi = napi; /* only if locking is tied to NAPI */
  119. pp_params.dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
  120. page_pool = page_pool_create(&pp_params);
  121. err = xdp_rxq_info_reg(&xdp_rxq, ndev, 0);
  122. if (err)
  123. goto err_out;
  124. err = xdp_rxq_info_reg_mem_model(&xdp_rxq, MEM_TYPE_PAGE_POOL, page_pool);
  125. if (err)
  126. goto err_out;
  127. NAPI poller
  128. -----------
  129. .. code-block:: c
  130. /* NAPI Rx poller */
  131. enum dma_data_direction dma_dir;
  132. dma_dir = page_pool_get_dma_dir(dring->page_pool);
  133. while (done < budget) {
  134. if (some error)
  135. page_pool_recycle_direct(page_pool, page);
  136. if (packet_is_xdp) {
  137. if XDP_DROP:
  138. page_pool_recycle_direct(page_pool, page);
  139. } else (packet_is_skb) {
  140. skb_mark_for_recycle(skb);
  141. new_page = page_pool_dev_alloc_pages(page_pool);
  142. }
  143. }
  144. Stats
  145. -----
  146. .. code-block:: c
  147. #ifdef CONFIG_PAGE_POOL_STATS
  148. /* retrieve stats */
  149. struct page_pool_stats stats = { 0 };
  150. if (page_pool_get_stats(page_pool, &stats)) {
  151. /* perhaps the driver reports statistics with ethool */
  152. ethtool_print_allocation_stats(&stats.alloc_stats);
  153. ethtool_print_recycle_stats(&stats.recycle_stats);
  154. }
  155. #endif
  156. Driver unload
  157. -------------
  158. .. code-block:: c
  159. /* Driver unload */
  160. page_pool_put_full_page(page_pool, page, false);
  161. xdp_rxq_info_unreg(&xdp_rxq);