page_frag_cache.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Page fragment allocator
  3. *
  4. * Page Fragment:
  5. * An arbitrary-length arbitrary-offset area of memory which resides within a
  6. * 0 or higher order page. Multiple fragments within that page are
  7. * individually refcounted, in the page's reference counter.
  8. *
  9. * The page_frag functions provide a simple allocation framework for page
  10. * fragments. This is used by the network stack and network device drivers to
  11. * provide a backing region of memory for use as either an sk_buff->head, or to
  12. * be used in the "frags" portion of skb_shared_info.
  13. */
  14. #include <linux/build_bug.h>
  15. #include <linux/export.h>
  16. #include <linux/gfp_types.h>
  17. #include <linux/init.h>
  18. #include <linux/mm.h>
  19. #include <linux/page_frag_cache.h>
  20. #include "internal.h"
  21. static unsigned long encoded_page_create(struct page *page, unsigned int order,
  22. bool pfmemalloc)
  23. {
  24. BUILD_BUG_ON(PAGE_FRAG_CACHE_MAX_ORDER > PAGE_FRAG_CACHE_ORDER_MASK);
  25. BUILD_BUG_ON(PAGE_FRAG_CACHE_PFMEMALLOC_BIT >= PAGE_SIZE);
  26. return (unsigned long)page_address(page) |
  27. (order & PAGE_FRAG_CACHE_ORDER_MASK) |
  28. ((unsigned long)pfmemalloc * PAGE_FRAG_CACHE_PFMEMALLOC_BIT);
  29. }
  30. static unsigned long encoded_page_decode_order(unsigned long encoded_page)
  31. {
  32. return encoded_page & PAGE_FRAG_CACHE_ORDER_MASK;
  33. }
  34. static void *encoded_page_decode_virt(unsigned long encoded_page)
  35. {
  36. return (void *)(encoded_page & PAGE_MASK);
  37. }
  38. static struct page *encoded_page_decode_page(unsigned long encoded_page)
  39. {
  40. return virt_to_page((void *)encoded_page);
  41. }
  42. static struct page *__page_frag_cache_refill(struct page_frag_cache *nc,
  43. gfp_t gfp_mask)
  44. {
  45. unsigned long order = PAGE_FRAG_CACHE_MAX_ORDER;
  46. struct page *page = NULL;
  47. gfp_t gfp = gfp_mask;
  48. #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
  49. gfp_mask = (gfp_mask & ~__GFP_DIRECT_RECLAIM) | __GFP_COMP |
  50. __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC;
  51. page = __alloc_pages(gfp_mask, PAGE_FRAG_CACHE_MAX_ORDER,
  52. numa_mem_id(), NULL);
  53. #endif
  54. if (unlikely(!page)) {
  55. page = __alloc_pages(gfp, 0, numa_mem_id(), NULL);
  56. order = 0;
  57. }
  58. nc->encoded_page = page ?
  59. encoded_page_create(page, order, page_is_pfmemalloc(page)) : 0;
  60. return page;
  61. }
  62. void page_frag_cache_drain(struct page_frag_cache *nc)
  63. {
  64. if (!nc->encoded_page)
  65. return;
  66. __page_frag_cache_drain(encoded_page_decode_page(nc->encoded_page),
  67. nc->pagecnt_bias);
  68. nc->encoded_page = 0;
  69. }
  70. EXPORT_SYMBOL(page_frag_cache_drain);
  71. void __page_frag_cache_drain(struct page *page, unsigned int count)
  72. {
  73. VM_BUG_ON_PAGE(page_ref_count(page) == 0, page);
  74. if (page_ref_sub_and_test(page, count))
  75. free_frozen_pages(page, compound_order(page));
  76. }
  77. EXPORT_SYMBOL(__page_frag_cache_drain);
  78. void *__page_frag_alloc_align(struct page_frag_cache *nc,
  79. unsigned int fragsz, gfp_t gfp_mask,
  80. unsigned int align_mask)
  81. {
  82. unsigned long encoded_page = nc->encoded_page;
  83. unsigned int size, offset;
  84. struct page *page;
  85. if (unlikely(!encoded_page)) {
  86. refill:
  87. page = __page_frag_cache_refill(nc, gfp_mask);
  88. if (!page)
  89. return NULL;
  90. encoded_page = nc->encoded_page;
  91. /* Even if we own the page, we do not use atomic_set().
  92. * This would break get_page_unless_zero() users.
  93. */
  94. page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE);
  95. /* reset page count bias and offset to start of new frag */
  96. nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1;
  97. nc->offset = 0;
  98. }
  99. size = PAGE_SIZE << encoded_page_decode_order(encoded_page);
  100. offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask);
  101. if (unlikely(offset + fragsz > size)) {
  102. if (unlikely(fragsz > PAGE_SIZE)) {
  103. /*
  104. * The caller is trying to allocate a fragment
  105. * with fragsz > PAGE_SIZE but the cache isn't big
  106. * enough to satisfy the request, this may
  107. * happen in low memory conditions.
  108. * We don't release the cache page because
  109. * it could make memory pressure worse
  110. * so we simply return NULL here.
  111. */
  112. return NULL;
  113. }
  114. page = encoded_page_decode_page(encoded_page);
  115. if (!page_ref_sub_and_test(page, nc->pagecnt_bias))
  116. goto refill;
  117. if (unlikely(encoded_page_decode_pfmemalloc(encoded_page))) {
  118. free_frozen_pages(page,
  119. encoded_page_decode_order(encoded_page));
  120. goto refill;
  121. }
  122. /* OK, page count is 0, we can safely set it */
  123. set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1);
  124. /* reset page count bias and offset to start of new frag */
  125. nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1;
  126. offset = 0;
  127. }
  128. nc->pagecnt_bias--;
  129. nc->offset = offset + fragsz;
  130. return encoded_page_decode_virt(encoded_page) + offset;
  131. }
  132. EXPORT_SYMBOL(__page_frag_alloc_align);
  133. /*
  134. * Frees a page fragment allocated out of either a compound or order 0 page.
  135. */
  136. void page_frag_free(void *addr)
  137. {
  138. struct page *page = virt_to_head_page(addr);
  139. if (unlikely(put_page_testzero(page)))
  140. free_frozen_pages(page, compound_order(page));
  141. }
  142. EXPORT_SYMBOL(page_frag_free);