linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next v3 0/4] net: Avoid the memory waste in some Ethernet drivers
@ 2021-02-04 10:56 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 ` [PATCH net-next v3 0/4] net: Avoid the memory waste in some Ethernet drivers patchwork-bot+netdevbpf
  0 siblings, 2 replies; 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

Hi,

v3:
  - Adjust patch 1 and 2 according to Alexander's suggestion.
  - Add Tested-by from Subbaraya.
  - Add Reviewed-by from Ioana.

v2:
  - Inline page_frag_alloc() and {netdev,napi}_alloc_frag()
  - Adopt Vlastimil's suggestion and add his Acked-by

In the current implementation of napi_alloc_frag(), it doesn't have any
align guarantee for the returned buffer address. We would have to use
some ugly workarounds to make sure that we can get a align buffer
address for some Ethernet drivers. This patch series tries to introduce
some helper functions to make sure that an align buffer is returned.
Then we can drop the ugly workarounds and avoid the unnecessary memory
waste.

Kevin Hao (4):
  mm: page_frag: Introduce page_frag_alloc_align()
  net: Introduce {netdev,napi}_alloc_frag_align()
  net: octeontx2: Use napi_alloc_frag_align() to avoid the memory waste
  net: dpaa2: Use napi_alloc_frag_align() to avoid the memory waste

 .../net/ethernet/freescale/dpaa2/dpaa2-eth.c  |  3 +-
 .../marvell/octeontx2/nic/otx2_common.c       |  3 +-
 include/linux/gfp.h                           | 12 +++++--
 include/linux/skbuff.h                        | 36 +++++++++++++++++--
 mm/page_alloc.c                               |  8 +++--
 net/core/skbuff.c                             | 26 ++++++--------
 6 files changed, 61 insertions(+), 27 deletions(-)

-- 
2.29.2



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [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 1/4] mm: page_frag: Introduce page_frag_alloc_align()
  2021-02-04 10:56 ` [PATCH net-next v3 1/4] mm: page_frag: Introduce page_frag_alloc_align() Kevin Hao
@ 2021-02-04 16:11   ` Alexander Duyck
  0 siblings, 0 replies; 4+ messages in thread
From: Alexander Duyck @ 2021-02-04 16:11 UTC (permalink / raw)
  To: Kevin Hao
  Cc: David S . Miller, Jakub Kicinski, Andrew Morton, Netdev,
	linux-mm, Vlastimil Babka, Eric Dumazet

On Thu, Feb 4, 2021 at 3:06 AM Kevin Hao <haokexin@gmail.com> wrote:
>
> 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(-)

Looks good to me.

Reviewed-by: Alexander Duyck <alexanderduyck@fb.com>


^ 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

end of thread, other threads:[~2021-02-06 20:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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-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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox