* [PATCH net-next v3 1/4] mm: page_frag: Introduce page_frag_alloc_align()
2021-02-04 10:56 [PATCH net-next v3 0/4] net: Avoid the memory waste in some Ethernet drivers Kevin Hao
@ 2021-02-04 10:56 ` Kevin Hao
2021-02-04 16:11 ` Alexander Duyck
2021-02-06 20:20 ` [PATCH net-next v3 0/4] net: Avoid the memory waste in some Ethernet drivers patchwork-bot+netdevbpf
1 sibling, 1 reply; 4+ messages in thread
From: Kevin Hao @ 2021-02-04 10:56 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski, Andrew Morton
Cc: netdev, linux-mm, Vlastimil Babka, Eric Dumazet, Alexander Duyck
In the current implementation of page_frag_alloc(), it doesn't have
any align guarantee for the returned buffer address. But for some
hardwares they do require the DMA buffer to be aligned correctly,
so we would have to use some workarounds like below if the buffers
allocated by the page_frag_alloc() are used by these hardwares for
DMA.
buf = page_frag_alloc(really_needed_size + align);
buf = PTR_ALIGN(buf, align);
These codes seems ugly and would waste a lot of memories if the buffers
are used in a network driver for the TX/RX. So introduce
page_frag_alloc_align() to make sure that an aligned buffer address is
returned.
Signed-off-by: Kevin Hao <haokexin@gmail.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
---
v3: Use align mask as suggested by Alexander.
include/linux/gfp.h | 12 ++++++++++--
mm/page_alloc.c | 8 +++++---
2 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/include/linux/gfp.h b/include/linux/gfp.h
index 53caa9846854..52cd415b436c 100644
--- a/include/linux/gfp.h
+++ b/include/linux/gfp.h
@@ -583,8 +583,16 @@ 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(struct page_frag_cache *nc,
- unsigned int fragsz, gfp_t gfp_mask);
+extern 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(struct page_frag_cache *nc,
+ unsigned int fragsz, gfp_t gfp_mask)
+{
+ return page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u);
+}
+
extern void page_frag_free(void *addr);
#define __free_page(page) __free_pages((page), 0)
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index ad3ed3ec4dd5..3583c6accd88 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -5137,8 +5137,9 @@ void __page_frag_cache_drain(struct page *page, unsigned int count)
}
EXPORT_SYMBOL(__page_frag_cache_drain);
-void *page_frag_alloc(struct page_frag_cache *nc,
- unsigned int fragsz, gfp_t gfp_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;
@@ -5190,11 +5191,12 @@ void *page_frag_alloc(struct page_frag_cache *nc,
}
nc->pagecnt_bias--;
+ offset &= align_mask;
nc->offset = offset;
return nc->va + offset;
}
-EXPORT_SYMBOL(page_frag_alloc);
+EXPORT_SYMBOL(page_frag_alloc_align);
/*
* Frees a page fragment allocated out of either a compound or order 0 page.
--
2.29.2
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH net-next v3 0/4] net: Avoid the memory waste in some Ethernet drivers
2021-02-04 10:56 [PATCH net-next v3 0/4] net: Avoid the memory waste in some Ethernet drivers Kevin Hao
2021-02-04 10:56 ` [PATCH net-next v3 1/4] mm: page_frag: Introduce page_frag_alloc_align() Kevin Hao
@ 2021-02-06 20:20 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2021-02-06 20:20 UTC (permalink / raw)
To: Kevin Hao; +Cc: davem, kuba, akpm, netdev, linux-mm
Hello:
This series was applied to netdev/net-next.git (refs/heads/master):
On Thu, 4 Feb 2021 18:56:34 +0800 you wrote:
> Hi,
>
> v3:
> - Adjust patch 1 and 2 according to Alexander's suggestion.
> - Add Tested-by from Subbaraya.
> - Add Reviewed-by from Ioana.
>
> [...]
Here is the summary with links:
- [net-next,v3,1/4] mm: page_frag: Introduce page_frag_alloc_align()
https://git.kernel.org/netdev/net-next/c/b358e2122b9d
- [net-next,v3,2/4] net: Introduce {netdev,napi}_alloc_frag_align()
https://git.kernel.org/netdev/net-next/c/3f6e687dff39
- [net-next,v3,3/4] net: octeontx2: Use napi_alloc_frag_align() to avoid the memory waste
https://git.kernel.org/netdev/net-next/c/1b041601c798
- [net-next,v3,4/4] net: dpaa2: Use napi_alloc_frag_align() to avoid the memory waste
https://git.kernel.org/netdev/net-next/c/d0dfbb9912d9
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 4+ messages in thread