601-v5.18-page_pool-Add-recycle-stats.patch 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. From ad6fa1e1ab1b8164f1ba296b1b4dc556a483bcad Mon Sep 17 00:00:00 2001
  2. From: Joe Damato <[email protected]>
  3. Date: Tue, 1 Mar 2022 23:55:48 -0800
  4. Subject: [PATCH 2/3] page_pool: Add recycle stats
  5. Add per-cpu stats tracking page pool recycling events:
  6. - cached: recycling placed page in the page pool cache
  7. - cache_full: page pool cache was full
  8. - ring: page placed into the ptr ring
  9. - ring_full: page released from page pool because the ptr ring was full
  10. - released_refcnt: page released (and not recycled) because refcnt > 1
  11. Signed-off-by: Joe Damato <[email protected]>
  12. Acked-by: Jesper Dangaard Brouer <[email protected]>
  13. Reviewed-by: Ilias Apalodimas <[email protected]>
  14. Signed-off-by: David S. Miller <[email protected]>
  15. ---
  16. include/net/page_pool.h | 16 ++++++++++++++++
  17. net/core/page_pool.c | 30 ++++++++++++++++++++++++++++--
  18. 2 files changed, 44 insertions(+), 2 deletions(-)
  19. --- a/include/net/page_pool.h
  20. +++ b/include/net/page_pool.h
  21. @@ -93,6 +93,18 @@ struct page_pool_alloc_stats {
  22. u64 refill; /* allocations via successful refill */
  23. u64 waive; /* failed refills due to numa zone mismatch */
  24. };
  25. +
  26. +struct page_pool_recycle_stats {
  27. + u64 cached; /* recycling placed page in the cache. */
  28. + u64 cache_full; /* cache was full */
  29. + u64 ring; /* recycling placed page back into ptr ring */
  30. + u64 ring_full; /* page was released from page-pool because
  31. + * PTR ring was full.
  32. + */
  33. + u64 released_refcnt; /* page released because of elevated
  34. + * refcnt
  35. + */
  36. +};
  37. #endif
  38. struct page_pool {
  39. @@ -136,6 +148,10 @@ struct page_pool {
  40. */
  41. struct ptr_ring ring;
  42. +#ifdef CONFIG_PAGE_POOL_STATS
  43. + /* recycle stats are per-cpu to avoid locking */
  44. + struct page_pool_recycle_stats __percpu *recycle_stats;
  45. +#endif
  46. atomic_t pages_state_release_cnt;
  47. /* A page_pool is strictly tied to a single RX-queue being
  48. --- a/net/core/page_pool.c
  49. +++ b/net/core/page_pool.c
  50. @@ -29,8 +29,15 @@
  51. #ifdef CONFIG_PAGE_POOL_STATS
  52. /* alloc_stat_inc is intended to be used in softirq context */
  53. #define alloc_stat_inc(pool, __stat) (pool->alloc_stats.__stat++)
  54. +/* recycle_stat_inc is safe to use when preemption is possible. */
  55. +#define recycle_stat_inc(pool, __stat) \
  56. + do { \
  57. + struct page_pool_recycle_stats __percpu *s = pool->recycle_stats; \
  58. + this_cpu_inc(s->__stat); \
  59. + } while (0)
  60. #else
  61. #define alloc_stat_inc(pool, __stat)
  62. +#define recycle_stat_inc(pool, __stat)
  63. #endif
  64. static int page_pool_init(struct page_pool *pool,
  65. @@ -80,6 +87,12 @@ static int page_pool_init(struct page_po
  66. pool->p.flags & PP_FLAG_PAGE_FRAG)
  67. return -EINVAL;
  68. +#ifdef CONFIG_PAGE_POOL_STATS
  69. + pool->recycle_stats = alloc_percpu(struct page_pool_recycle_stats);
  70. + if (!pool->recycle_stats)
  71. + return -ENOMEM;
  72. +#endif
  73. +
  74. if (ptr_ring_init(&pool->ring, ring_qsize, GFP_KERNEL) < 0)
  75. return -ENOMEM;
  76. @@ -412,7 +425,12 @@ static bool page_pool_recycle_in_ring(st
  77. else
  78. ret = ptr_ring_produce_bh(&pool->ring, page);
  79. - return (ret == 0) ? true : false;
  80. + if (!ret) {
  81. + recycle_stat_inc(pool, ring);
  82. + return true;
  83. + }
  84. +
  85. + return false;
  86. }
  87. /* Only allow direct recycling in special circumstances, into the
  88. @@ -423,11 +441,14 @@ static bool page_pool_recycle_in_ring(st
  89. static bool page_pool_recycle_in_cache(struct page *page,
  90. struct page_pool *pool)
  91. {
  92. - if (unlikely(pool->alloc.count == PP_ALLOC_CACHE_SIZE))
  93. + if (unlikely(pool->alloc.count == PP_ALLOC_CACHE_SIZE)) {
  94. + recycle_stat_inc(pool, cache_full);
  95. return false;
  96. + }
  97. /* Caller MUST have verified/know (page_ref_count(page) == 1) */
  98. pool->alloc.cache[pool->alloc.count++] = page;
  99. + recycle_stat_inc(pool, cached);
  100. return true;
  101. }
  102. @@ -482,6 +503,7 @@ __page_pool_put_page(struct page_pool *p
  103. * doing refcnt based recycle tricks, meaning another process
  104. * will be invoking put_page.
  105. */
  106. + recycle_stat_inc(pool, released_refcnt);
  107. /* Do not replace this with page_pool_return_page() */
  108. page_pool_release_page(pool, page);
  109. put_page(page);
  110. @@ -495,6 +517,7 @@ void page_pool_put_page(struct page_pool
  111. page = __page_pool_put_page(pool, page, dma_sync_size, allow_direct);
  112. if (page && !page_pool_recycle_in_ring(pool, page)) {
  113. /* Cache full, fallback to free pages */
  114. + recycle_stat_inc(pool, ring_full);
  115. page_pool_return_page(pool, page);
  116. }
  117. }
  118. @@ -641,6 +664,9 @@ static void page_pool_free(struct page_p
  119. if (pool->p.flags & PP_FLAG_DMA_MAP)
  120. put_device(pool->p.dev);
  121. +#ifdef CONFIG_PAGE_POOL_STATS
  122. + free_percpu(pool->recycle_stats);
  123. +#endif
  124. kfree(pool);
  125. }