linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [net-next PATCH V3 0/3] Fix page_pool API and dma address storage
@ 2019-02-13  1:55 Jesper Dangaard Brouer
  2019-02-13  1:55 ` [net-next PATCH V3 1/3] mm: add dma_addr_t to struct page Jesper Dangaard Brouer
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Jesper Dangaard Brouer @ 2019-02-13  1:55 UTC (permalink / raw)
  To: netdev, linux-mm
  Cc: Toke Høiland-Jørgensen, Ilias Apalodimas, willy,
	Saeed Mahameed, Alexander Duyck, Jesper Dangaard Brouer,
	Andrew Morton, mgorman, David S. Miller, Tariq Toukan

As pointed out by David Miller in [1] the current page_pool implementation
stores dma_addr_t in page->private. This won't work on 32-bit platforms with
64-bit DMA addresses since the page->private is an unsigned long and the
dma_addr_t a u64.

Since no driver is yet using the DMA mapping capabilities of the API let's
fix this by storing the information in 'struct page' and use that to store
and retrieve DMA addresses from network drivers.

As long as the addresses returned from dma_map_page() are aligned the first
bit, used by the compound pages code should not be set.

Ilias tested the first two patches on Espressobin driver mvneta, for which
we have patches for using the DMA API of page_pool.

[1]: https://lore.kernel.org/netdev/20181207.230655.1261252486319967024.davem@davemloft.net/

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---

Ilias Apalodimas (1):
      net: page_pool: don't use page->private to store dma_addr_t

Jesper Dangaard Brouer (2):
      mm: add dma_addr_t to struct page
      page_pool: use DMA_ATTR_SKIP_CPU_SYNC for DMA mappings


 include/linux/mm_types.h |    7 +++++++
 net/core/page_pool.c     |   22 ++++++++++++++--------
 2 files changed, 21 insertions(+), 8 deletions(-)

--


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

* [net-next PATCH V3 1/3] mm: add dma_addr_t to struct page
  2019-02-13  1:55 [net-next PATCH V3 0/3] Fix page_pool API and dma address storage Jesper Dangaard Brouer
@ 2019-02-13  1:55 ` Jesper Dangaard Brouer
  2020-04-29  0:38   ` Kirill A. Shutemov
  2019-02-13  1:55 ` [net-next PATCH V3 2/3] net: page_pool: don't use page->private to store dma_addr_t Jesper Dangaard Brouer
  2019-02-13  1:55 ` [net-next PATCH V3 3/3] page_pool: use DMA_ATTR_SKIP_CPU_SYNC for DMA mappings Jesper Dangaard Brouer
  2 siblings, 1 reply; 6+ messages in thread
From: Jesper Dangaard Brouer @ 2019-02-13  1:55 UTC (permalink / raw)
  To: netdev, linux-mm
  Cc: Toke Høiland-Jørgensen, Ilias Apalodimas, willy,
	Saeed Mahameed, Alexander Duyck, Jesper Dangaard Brouer,
	Andrew Morton, mgorman, David S. Miller, Tariq Toukan

The page_pool API is using page->private to store DMA addresses.
As pointed out by David Miller we can't use that on 32-bit architectures
with 64-bit DMA

This patch adds a new dma_addr_t struct to allow storing DMA addresses

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Acked-by: Andrew Morton <akpm@linux-foundation.org>
---
 include/linux/mm_types.h |    7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 2c471a2c43fa..0a36a22228e7 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -95,6 +95,13 @@ struct page {
 			 */
 			unsigned long private;
 		};
+		struct {	/* page_pool used by netstack */
+			/**
+			 * @dma_addr: might require a 64-bit value even on
+			 * 32-bit architectures.
+			 */
+			dma_addr_t dma_addr;
+		};
 		struct {	/* slab, slob and slub */
 			union {
 				struct list_head slab_list;	/* uses lru */


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

* [net-next PATCH V3 2/3] net: page_pool: don't use page->private to store dma_addr_t
  2019-02-13  1:55 [net-next PATCH V3 0/3] Fix page_pool API and dma address storage Jesper Dangaard Brouer
  2019-02-13  1:55 ` [net-next PATCH V3 1/3] mm: add dma_addr_t to struct page Jesper Dangaard Brouer
@ 2019-02-13  1:55 ` Jesper Dangaard Brouer
  2019-02-13  1:55 ` [net-next PATCH V3 3/3] page_pool: use DMA_ATTR_SKIP_CPU_SYNC for DMA mappings Jesper Dangaard Brouer
  2 siblings, 0 replies; 6+ messages in thread
From: Jesper Dangaard Brouer @ 2019-02-13  1:55 UTC (permalink / raw)
  To: netdev, linux-mm
  Cc: Toke Høiland-Jørgensen, Ilias Apalodimas, willy,
	Saeed Mahameed, Alexander Duyck, Jesper Dangaard Brouer,
	Andrew Morton, mgorman, David S. Miller, Tariq Toukan

From: Ilias Apalodimas <ilias.apalodimas@linaro.org>

As pointed out by David Miller the current page_pool implementation
stores dma_addr_t in page->private.
This won't work on 32-bit platforms with 64-bit DMA addresses since the
page->private is an unsigned long and the dma_addr_t a u64.

A previous patch is adding dma_addr_t on struct page to accommodate this.
This patch adapts the page_pool related functions to use the newly added
struct for storing and retrieving DMA addresses from network drivers.

Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
---
 net/core/page_pool.c |   13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/net/core/page_pool.c b/net/core/page_pool.c
index 43a932cb609b..897a69a1477e 100644
--- a/net/core/page_pool.c
+++ b/net/core/page_pool.c
@@ -136,7 +136,9 @@ static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
 	if (!(pool->p.flags & PP_FLAG_DMA_MAP))
 		goto skip_dma_map;
 
-	/* Setup DMA mapping: use page->private for DMA-addr
+	/* Setup DMA mapping: use 'struct page' area for storing DMA-addr
+	 * since dma_addr_t can be either 32 or 64 bits and does not always fit
+	 * into page private data (i.e 32bit cpu with 64bit DMA caps)
 	 * This mapping is kept for lifetime of page, until leaving pool.
 	 */
 	dma = dma_map_page(pool->p.dev, page, 0,
@@ -146,7 +148,7 @@ static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
 		put_page(page);
 		return NULL;
 	}
-	set_page_private(page, dma); /* page->private = dma; */
+	page->dma_addr = dma;
 
 skip_dma_map:
 	/* When page just alloc'ed is should/must have refcnt 1. */
@@ -175,13 +177,16 @@ EXPORT_SYMBOL(page_pool_alloc_pages);
 static void __page_pool_clean_page(struct page_pool *pool,
 				   struct page *page)
 {
+	dma_addr_t dma;
+
 	if (!(pool->p.flags & PP_FLAG_DMA_MAP))
 		return;
 
+	dma = page->dma_addr;
 	/* DMA unmap */
-	dma_unmap_page(pool->p.dev, page_private(page),
+	dma_unmap_page(pool->p.dev, dma,
 		       PAGE_SIZE << pool->p.order, pool->p.dma_dir);
-	set_page_private(page, 0);
+	page->dma_addr = 0;
 }
 
 /* Return a page to the page allocator, cleaning up our state */


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

* [net-next PATCH V3 3/3] page_pool: use DMA_ATTR_SKIP_CPU_SYNC for DMA mappings
  2019-02-13  1:55 [net-next PATCH V3 0/3] Fix page_pool API and dma address storage Jesper Dangaard Brouer
  2019-02-13  1:55 ` [net-next PATCH V3 1/3] mm: add dma_addr_t to struct page Jesper Dangaard Brouer
  2019-02-13  1:55 ` [net-next PATCH V3 2/3] net: page_pool: don't use page->private to store dma_addr_t Jesper Dangaard Brouer
@ 2019-02-13  1:55 ` Jesper Dangaard Brouer
  2 siblings, 0 replies; 6+ messages in thread
From: Jesper Dangaard Brouer @ 2019-02-13  1:55 UTC (permalink / raw)
  To: netdev, linux-mm
  Cc: Toke Høiland-Jørgensen, Ilias Apalodimas, willy,
	Saeed Mahameed, Alexander Duyck, Jesper Dangaard Brouer,
	Andrew Morton, mgorman, David S. Miller, Tariq Toukan

As pointed out by Alexander Duyck, the DMA mapping done in page_pool needs
to use the DMA attribute DMA_ATTR_SKIP_CPU_SYNC.

As the principle behind page_pool keeping the pages mapped is that the
driver takes over the DMA-sync steps.

Reported-by: Alexander Duyck <alexander.duyck@gmail.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---
 net/core/page_pool.c |   11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/net/core/page_pool.c b/net/core/page_pool.c
index 897a69a1477e..5b2252c6d49b 100644
--- a/net/core/page_pool.c
+++ b/net/core/page_pool.c
@@ -141,9 +141,9 @@ static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
 	 * into page private data (i.e 32bit cpu with 64bit DMA caps)
 	 * This mapping is kept for lifetime of page, until leaving pool.
 	 */
-	dma = dma_map_page(pool->p.dev, page, 0,
-			   (PAGE_SIZE << pool->p.order),
-			   pool->p.dma_dir);
+	dma = dma_map_page_attrs(pool->p.dev, page, 0,
+				 (PAGE_SIZE << pool->p.order),
+				 pool->p.dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
 	if (dma_mapping_error(pool->p.dev, dma)) {
 		put_page(page);
 		return NULL;
@@ -184,8 +184,9 @@ static void __page_pool_clean_page(struct page_pool *pool,
 
 	dma = page->dma_addr;
 	/* DMA unmap */
-	dma_unmap_page(pool->p.dev, dma,
-		       PAGE_SIZE << pool->p.order, pool->p.dma_dir);
+	dma_unmap_page_attrs(pool->p.dev, dma,
+			     PAGE_SIZE << pool->p.order, pool->p.dma_dir,
+			     DMA_ATTR_SKIP_CPU_SYNC);
 	page->dma_addr = 0;
 }
 


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

* Re: [net-next PATCH V3 1/3] mm: add dma_addr_t to struct page
  2019-02-13  1:55 ` [net-next PATCH V3 1/3] mm: add dma_addr_t to struct page Jesper Dangaard Brouer
@ 2020-04-29  0:38   ` Kirill A. Shutemov
  2020-04-29  1:54     ` Matthew Wilcox
  0 siblings, 1 reply; 6+ messages in thread
From: Kirill A. Shutemov @ 2020-04-29  0:38 UTC (permalink / raw)
  To: Jesper Dangaard Brouer
  Cc: netdev, linux-mm, Toke Høiland-Jørgensen,
	Ilias Apalodimas, willy, Saeed Mahameed, Alexander Duyck,
	Andrew Morton, mgorman, David S. Miller, Tariq Toukan

On Wed, Feb 13, 2019 at 02:55:40AM +0100, Jesper Dangaard Brouer wrote:
> The page_pool API is using page->private to store DMA addresses.
> As pointed out by David Miller we can't use that on 32-bit architectures
> with 64-bit DMA
> 
> This patch adds a new dma_addr_t struct to allow storing DMA addresses
> 
> Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
> Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> Acked-by: Andrew Morton <akpm@linux-foundation.org>
> ---
>  include/linux/mm_types.h |    7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
> index 2c471a2c43fa..0a36a22228e7 100644
> --- a/include/linux/mm_types.h
> +++ b/include/linux/mm_types.h
> @@ -95,6 +95,13 @@ struct page {
>  			 */
>  			unsigned long private;
>  		};
> +		struct {	/* page_pool used by netstack */
> +			/**
> +			 * @dma_addr: might require a 64-bit value even on
> +			 * 32-bit architectures.
> +			 */
> +			dma_addr_t dma_addr;
> +		};

[ I'm slow, but I've just noticed this change into struct page. ]

Is there a change that the dma_addr would have bit 0 set? If yes it may
lead to false-positive PageTail() and really strange behaviour.

I think it's better to put some padding into the struct to avoid aliasing
to compound_head.

See commit 1d798ca3f164 ("mm: make compound_head() robust") for context.

>  		struct {	/* slab, slob and slub */
>  			union {
>  				struct list_head slab_list;	/* uses lru */
> 

-- 
 Kirill A. Shutemov


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

* Re: [net-next PATCH V3 1/3] mm: add dma_addr_t to struct page
  2020-04-29  0:38   ` Kirill A. Shutemov
@ 2020-04-29  1:54     ` Matthew Wilcox
  0 siblings, 0 replies; 6+ messages in thread
From: Matthew Wilcox @ 2020-04-29  1:54 UTC (permalink / raw)
  To: Kirill A. Shutemov
  Cc: Jesper Dangaard Brouer, netdev, linux-mm,
	Toke Høiland-Jørgensen, Ilias Apalodimas,
	Saeed Mahameed, Alexander Duyck, Andrew Morton, mgorman,
	David S. Miller, Tariq Toukan

On Wed, Apr 29, 2020 at 03:38:43AM +0300, Kirill A. Shutemov wrote:
> On Wed, Feb 13, 2019 at 02:55:40AM +0100, Jesper Dangaard Brouer wrote:
> > The page_pool API is using page->private to store DMA addresses.
> > As pointed out by David Miller we can't use that on 32-bit architectures
> > with 64-bit DMA
> > +		struct {	/* page_pool used by netstack */
> > +			/**
> > +			 * @dma_addr: might require a 64-bit value even on
> > +			 * 32-bit architectures.
> > +			 */
> > +			dma_addr_t dma_addr;
> > +		};
> 
> [ I'm slow, but I've just noticed this change into struct page. ]
> 
> Is there a change that the dma_addr would have bit 0 set? If yes it may
> lead to false-positive PageTail() and really strange behaviour.

No.  It's the DMA address of the page, so it's going to be page aligned
and have the bottom 12 (or so) bits cleared.  It's not feasible for some
wacky IOMMU to use the bottom N bits for its own purposes because you can,
say, add three to the DMA address of the page and expect the device to
DMA to the third byte within the page.

Wacky IOMMUs use the top bits for storing "interesting" information.



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

end of thread, other threads:[~2020-04-29  1:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-13  1:55 [net-next PATCH V3 0/3] Fix page_pool API and dma address storage Jesper Dangaard Brouer
2019-02-13  1:55 ` [net-next PATCH V3 1/3] mm: add dma_addr_t to struct page Jesper Dangaard Brouer
2020-04-29  0:38   ` Kirill A. Shutemov
2020-04-29  1:54     ` Matthew Wilcox
2019-02-13  1:55 ` [net-next PATCH V3 2/3] net: page_pool: don't use page->private to store dma_addr_t Jesper Dangaard Brouer
2019-02-13  1:55 ` [net-next PATCH V3 3/3] page_pool: use DMA_ATTR_SKIP_CPU_SYNC for DMA mappings Jesper Dangaard Brouer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).