mm/page_alloc: modify page_frag_alloc_align() to accept align as an argument
authorYunsheng Lin <linyunsheng@huawei.com>
Wed, 28 Feb 2024 09:30:08 +0000 (17:30 +0800)
committerPaolo Abeni <pabeni@redhat.com>
Tue, 5 Mar 2024 10:38:14 +0000 (11:38 +0100)
napi_alloc_frag_align() and netdev_alloc_frag_align() accept
align as an argument, and they are thin wrappers around the
__napi_alloc_frag_align() and __netdev_alloc_frag_align() APIs
doing the alignment checking and align mask conversion, in order
to call page_frag_alloc_align() directly. The intention here is
to keep the alignment checking and the alignmask conversion in
in-line wrapper to avoid those kind of operations during execution
time since it can usually be handled during compile time.

We are going to use page_frag_alloc_align() in vhost_net.c, it
need the same kind of alignment checking and alignmask conversion,
so split up page_frag_alloc_align into an inline wrapper doing the
above operation, and add __page_frag_alloc_align() which is passed
with the align mask the original function expected as suggested by
Alexander.

Signed-off-by: Yunsheng Lin <linyunsheng@huawei.com>
CC: Alexander Duyck <alexander.duyck@gmail.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
include/linux/gfp.h
mm/page_alloc.c
net/core/skbuff.c

index de292a0071389ed122a3540c4a98870fe30aa8d8..28aea17fa59b524f29d62b95d63929d021f6175b 100644 (file)
@@ -312,14 +312,21 @@ extern void free_pages(unsigned long addr, unsigned int order);
 
 struct page_frag_cache;
 extern void __page_frag_cache_drain(struct page *page, unsigned int count);
-extern void *page_frag_alloc_align(struct page_frag_cache *nc,
-                                  unsigned int fragsz, gfp_t gfp_mask,
-                                  unsigned int align_mask);
+void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz,
+                             gfp_t gfp_mask, unsigned int align_mask);
+
+static inline void *page_frag_alloc_align(struct page_frag_cache *nc,
+                                         unsigned int fragsz, gfp_t gfp_mask,
+                                         unsigned int align)
+{
+       WARN_ON_ONCE(!is_power_of_2(align));
+       return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align);
+}
 
 static inline void *page_frag_alloc(struct page_frag_cache *nc,
                             unsigned int fragsz, gfp_t gfp_mask)
 {
-       return page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u);
+       return __page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u);
 }
 
 extern void page_frag_free(void *addr);
index 150d4f23b01048ed7af53a74ec3e12a208fc17b5..c0f7e67c42500e2ba2d624ff934b52bfb22d1aea 100644 (file)
@@ -4708,9 +4708,9 @@ void __page_frag_cache_drain(struct page *page, unsigned int count)
 }
 EXPORT_SYMBOL(__page_frag_cache_drain);
 
-void *page_frag_alloc_align(struct page_frag_cache *nc,
-                     unsigned int fragsz, gfp_t gfp_mask,
-                     unsigned int align_mask)
+void *__page_frag_alloc_align(struct page_frag_cache *nc,
+                             unsigned int fragsz, gfp_t gfp_mask,
+                             unsigned int align_mask)
 {
        unsigned int size = PAGE_SIZE;
        struct page *page;
@@ -4779,7 +4779,7 @@ refill:
 
        return nc->va + offset;
 }
-EXPORT_SYMBOL(page_frag_alloc_align);
+EXPORT_SYMBOL(__page_frag_alloc_align);
 
 /*
  * Frees a page fragment allocated out of either a compound or order 0 page.
index 1f918e602bc4f61fc09aa7308f9c47e8cacae47b..43d7fc150acc9263760162c3f5778fa0a646bcc4 100644 (file)
@@ -315,7 +315,8 @@ void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
 
        fragsz = SKB_DATA_ALIGN(fragsz);
 
-       return page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask);
+       return __page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC,
+                                      align_mask);
 }
 EXPORT_SYMBOL(__napi_alloc_frag_align);
 
@@ -327,13 +328,15 @@ void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
        if (in_hardirq() || irqs_disabled()) {
                struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache);
 
-               data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask);
+               data = __page_frag_alloc_align(nc, fragsz, GFP_ATOMIC,
+                                              align_mask);
        } else {
                struct napi_alloc_cache *nc;
 
                local_bh_disable();
                nc = this_cpu_ptr(&napi_alloc_cache);
-               data = page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask);
+               data = __page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC,
+                                              align_mask);
                local_bh_enable();
        }
        return data;