dma-buf: replace "#if" with just "if"
Subsystems (2)
PATCH 1/2 dma-buf: replace "#if" with just "if"
No need to conditional compile that code, let the compilers dead code elimination handle it instead.
--- drivers/dma-buf/dma-buf.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/dma-buf/dma-buf.c +2 -3
@@ -1141,8 +1141,7 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, }mangle_sg_table (sg_table); -#ifdef CONFIG_DMA_API_DEBUG - { +if (IS_ENABLED (CONFIG_DMA_API_DEBUG )) {struct scatterlist *sg;u64 addr;int len;
@@ -1154,10 +1153,10 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,if (!PAGE_ALIGNED (addr) || !PAGE_ALIGNED (len)) {pr_debug ("%s: addr %llx or len %x is not page aligned!\n" , __func__, addr, len); +break ; } } } -#endif /* CONFIG_DMA_API_DEBUG */ return sg_table; error_unmap:
Signature · 1 line
2.43.0
PATCH 2/2 dma-buf: improve sg_table debugging hack
Instead of just mangling the page link create a copy of the sg_table but only copy over the DMA addresses and not the pages.
Still quite a hack but this at least allows the exporter to properly keeps it's sg_table intact.
This is important for example for the system DMA-heap which needs it's sg_table to sync CPU writes with device accesses.
--- drivers/dma-buf/dma-buf.c | 68 +++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 14 deletions(-)
drivers/dma-buf/dma-buf.c +54 -14
@@ -828,21 +828,59 @@ void dma_buf_put(struct dma_buf *dmabuf) }EXPORT_SYMBOL_NS_GPL (dma_buf_put,"DMA_BUF" ); -static void mangle_sg_table (struct sg_table *sg_table) +static int dma_buf_mangle_sg_table (struct sg_table **sg_table) { -#ifdef CONFIG_DMABUF_DEBUG -int i; -struct scatterlist *sg; - -/* To catch abuse of the underlying struct page by importers mix -* up the bits, but take care to preserve the low SG_ bits to -* not corrupt the sgt. The mixing is undone on unmap -* before passing the sgt back to the exporter. +struct sg_table *to, *from = *sg_table; +struct scatterlist *to_sg, *from_sg; +int i, ret; + +if (!IS_ENABLED (CONFIG_DMABUF_DEBUG )) +return 0 ; + +/* +* To catch abuse of the underlying struct page by importers copy the +* sg_table without copying the page_link and give only the copy back to +* the importer. */ -for_each_sgtable_sg (sg_table, sg, i) - sg->page_link ^= ~0xffUL ; -#endif + to =kzalloc (sizeof (*to),GFP_KERNEL ); +if (!to) +return -ENOMEM ; + ret =sg_alloc_table (to, from->nents,GFP_KERNEL ); +if (ret) + goto free_to; + + to_sg = to->sgl; +for_each_sgtable_dma_sg (from, from_sg, i) { +sg_set_page (to_sg,NULL ,0 ,0 ); +sg_dma_address (to_sg) =sg_dma_address (from_sg); +sg_dma_len (to_sg) =sg_dma_len (from_sg); + to_sg =sg_next (to_sg); + } + +/* +* Store the original sg_table in the first page_link of the copy so +* that we can revert everything back again on unmap. +*/ + to->sgl[0 ].page_link = (unsigned long )from; + *sg_table = to; +return 0 ; + +free_to: +kfree (to); +return ret; +} + +static void dma_buf_demangle_sg_table (struct sg_table **sg_table) +{ +struct sg_table *copy = *sg_table; + +if (!IS_ENABLED (CONFIG_DMABUF_DEBUG )) +return ; + + *sg_table = (void *)copy->sgl[0 ].page_link; +sg_free_table (copy); +kfree (copy); } static inline bool
@@ -1139,7 +1177,9 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,if (ret <0 ) goto error_unmap; } -mangle_sg_table (sg_table); + ret =dma_buf_mangle_sg_table (&sg_table); +if (ret) + goto error_unmap;if (IS_ENABLED (CONFIG_DMA_API_DEBUG )) {struct scatterlist *sg;
@@ -1220,7 +1260,7 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,dma_resv_assert_held (attach->dmabuf->resv); -mangle_sg_table (sg_table); +dma_buf_demangle_sg_table (&sg_table); attach->dmabuf->ops->unmap_dma_buf (attach, sg_table, direction);if (dma_buf_pin_on_map (attach))
Signature · 1 line
2.43.0
RE: [PATCH 2/2] dma-buf: improve sg_table debugging hack
Quoted 11 lines
-----Original Message-----
From: dri-devel <[email protected]> On Behalf Of Christian König
Sent: Monday, October 6, 2025 9:47 AM
To: [email protected]; [email protected]; dri- [email protected]; [email protected]; [email protected]
Subject: [PATCH 2/2] dma-buf: improvesg_tabledebugging hackInstead of just mangling the page link create a copy of the
sg_tablebut only copy over the DMA addresses and not the pages.
This made the issue obvious. If I abuse this now how will I know I am doing the wrong thing?
Quoted 5 lines
Still quite a hack but this at least allows the exporter to properly keeps it's
sg_tableintact.This is important for example for the system DMA-heap which needs it's
sg_tableto sync CPU writes with device accesses.
So the mangling actually breaks a usage? I thought that the usage was incorrect...is the dma-heap using this incorrectly?
Quoted 44 lines
Signed-off-by Christian König <[email protected]>---
drivers/dma-buf/dma-buf.c| 68 +++++++++++++++++++++++++++++++-----
---1 file changed, 54 insertions(+), 14 deletions(-)drivers/dma-buf/dma-buf.c +11
-9@@ -828,21 +828,59 @@ void dma_buf_put(struct dma_buf *dmabuf) }EXPORT_SYMBOL_NS_GPL (dma_buf_put,"DMA_BUF" ); -static void mangle_sg_table (struct sg_table *sg_table) +static int dma_buf_mangle_sg_table (struct sg_table **sg_table) { -#ifdef CONFIG_DMABUF_DEBUG -int i; -struct scatterlist *sg; - -/* To catch abuse of the underlying struct page by importers mix -* up the bits, but take care to preserve the low SG_ bits to -* not corrupt the sgt. The mixing is undone on unmap -* before passing the sgt back to the exporter. +struct sg_table *to, *from = *sg_table; +struct scatterlist *to_sg, *from_sg; +int i, ret; + +if (!IS_ENABLED (CONFIG_DMABUF_DEBUG )) +return 0 ; + +/* +* To catch abuse of the underlying struct page by importers copy the +* sg_table without copying the page_link and give only the copy back to
drivers/dma-buf/dma-buf.c +1
-0+* the importer. */drivers/dma-buf/dma-buf.c +3
-3-for_each_sgtable_sg (sg_table, sg, i) - sg->page_link ^= ~0xffUL ; -#endif + to =kzalloc (sizeof (*to),GFP_KERNEL ); +if (!to) +return -ENOMEM ;
drivers/dma-buf/dma-buf.c void dma_buf_put(struct dma_buf *dmabuf) · line 857
Quoted 10 lines
drivers/dma-buf/dma-buf.c +9
-0+ ret =sg_alloc_table (to, from->nents,GFP_KERNEL ); +if (ret) + goto free_to; + + to_sg = to->sgl; +for_each_sgtable_dma_sg (from, from_sg, i) { +sg_set_page (to_sg,NULL ,0 ,0 ); +sg_dma_address (to_sg) =sg_dma_address (from_sg); +sg_dma_len (to_sg) =sg_dma_len (from_sg);
Is the indentation correct here?
M
Quoted 53 lines
drivers/dma-buf/dma-buf.c +26
-0+ to_sg =sg_next (to_sg); + } + +/* +* Store the original sg_table in the first page_link of the copy so +* that we can revert everything back again on unmap. +*/ + to->sgl[0 ].page_link = (unsigned long )from; + *sg_table = to; +return 0 ; + +free_to: +kfree (to); +return ret; +} + +static void dma_buf_demangle_sg_table (struct sg_table **sg_table) +{ +struct sg_table *copy = *sg_table; + +if (!IS_ENABLED (CONFIG_DMABUF_DEBUG )) +return ; + + *sg_table = (void *)copy->sgl[0 ].page_link; +sg_free_table (copy); +kfree (copy);}
static inline bool
drivers/dma-buf/dma-buf.c +3
-1@@ -1139,7 +1177,9 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, if (ret < 0) goto error_unmap; } -mangle_sg_table (sg_table); + ret =dma_buf_mangle_sg_table (&sg_table); +if (ret) + goto error_unmap;if (IS_ENABLED (CONFIG_DMA_API_DEBUG )) {struct scatterlist *sg;drivers/dma-buf/dma-buf.c +1
-1@@ -1220,7 +1260,7 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,dma_resv_assert_held (attach->dmabuf->resv); -mangle_sg_table (sg_table); +dma_buf_demangle_sg_table (&sg_table); attach->dmabuf->ops->unmap_dma_buf (attach, sg_table, direction);if (dma_buf_pin_on_map(attach))--
2.43.0
Christian König
maintainerRe: [PATCH 2/2] dma-buf: improve sg_table debugging hack
Quoted 9 lines · Michael J
Quoted 9 lines
-----Original Message-----
From: dri-devel <[email protected]> On Behalf Of Christian König
Sent: Monday, October 6, 2025 9:47 AM
To: [email protected]; [email protected]; dri- [email protected]; [email protected]; [email protected]
Subject: [PATCH 2/2] dma-buf: improvesg_tabledebugging hack
Quoted 5 lines
Quoted 2 lines
Instead of just mangling the page link create a copy of the
sg_tablebut only copy over the DMA addresses and not the pages.This made the issue obvious. If I abuse this now how will I know I am doing the wrong thing?
You get a crash when you try to to convert a page link into a struct page pointer and then access fields in that struct page (the pointer is NULL for most entries now).
Quoted 8 lines
Quoted 5 lines
Still quite a hack but this at least allows the exporter to properly keeps it's
sg_tableintact.This is important for example for the system DMA-heap which needs it's
sg_tableto sync CPU writes with device accesses.So the mangling actually breaks a usage? I thought that the usage was incorrect...is the dma-heap using this incorrectly?
No, dma-heap as an exporter is using it perfectly correctly.
The problem was (or rather is) that some importers turned the page link into a struct page again and then tried to modify that struct page, e.g. grabbing references to it or similar.
That turned into tons of problems when the exporter used device private pages and didn't expect that somebody messing with it.
The only property importers are allowed to access in the sg_tables they get from dma_buf are the DMA-addresses.
This patch here is a first step to replace using sg_tables with something else like xarray of DMA-addresses or similar.
Regards,
Christian.
Quoted 114 lines
Quoted 45 lines
Signed-off-by Christian König <[email protected]>---
drivers/dma-buf/dma-buf.c| 68 +++++++++++++++++++++++++++++++-----
---1 file changed, 54 insertions(+), 14 deletions(-)drivers/dma-buf/dma-buf.c +11
-9@@ -828,21 +828,59 @@ void dma_buf_put(struct dma_buf *dmabuf) }EXPORT_SYMBOL_NS_GPL (dma_buf_put,"DMA_BUF" ); -static void mangle_sg_table (struct sg_table *sg_table) +static int dma_buf_mangle_sg_table (struct sg_table **sg_table) { -#ifdef CONFIG_DMABUF_DEBUG -int i; -struct scatterlist *sg; - -/* To catch abuse of the underlying struct page by importers mix -* up the bits, but take care to preserve the low SG_ bits to -* not corrupt the sgt. The mixing is undone on unmap -* before passing the sgt back to the exporter. +struct sg_table *to, *from = *sg_table; +struct scatterlist *to_sg, *from_sg; +int i, ret; + +if (!IS_ENABLED (CONFIG_DMABUF_DEBUG )) +return 0 ; + +/* +* To catch abuse of the underlying struct page by importers copy the +* sg_table without copying the page_link and give only the copy back to
drivers/dma-buf/dma-buf.c +1
-0+* the importer. */drivers/dma-buf/dma-buf.c +3
-3-for_each_sgtable_sg (sg_table, sg, i) - sg->page_link ^= ~0xffUL ; -#endif + to =kzalloc (sizeof (*to),GFP_KERNEL ); +if (!to) +return -ENOMEM ;drivers/dma-buf/dma-buf.c void dma_buf_put(struct dma_buf *dmabuf) · line 857
Quoted 10 lines
drivers/dma-buf/dma-buf.c +9
-0+ ret =sg_alloc_table (to, from->nents,GFP_KERNEL ); +if (ret) + goto free_to; + + to_sg = to->sgl; +for_each_sgtable_dma_sg (from, from_sg, i) { +sg_set_page (to_sg,NULL ,0 ,0 ); +sg_dma_address (to_sg) =sg_dma_address (from_sg); +sg_dma_len (to_sg) =sg_dma_len (from_sg);Is the indentation correct here?
M
Quoted 53 lines
drivers/dma-buf/dma-buf.c +26
-0+ to_sg =sg_next (to_sg); + } + +/* +* Store the original sg_table in the first page_link of the copy so +* that we can revert everything back again on unmap. +*/ + to->sgl[0 ].page_link = (unsigned long )from; + *sg_table = to; +return 0 ; + +free_to: +kfree (to); +return ret; +} + +static void dma_buf_demangle_sg_table (struct sg_table **sg_table) +{ +struct sg_table *copy = *sg_table; + +if (!IS_ENABLED (CONFIG_DMABUF_DEBUG )) +return ; + + *sg_table = (void *)copy->sgl[0 ].page_link; +sg_free_table (copy); +kfree (copy);}
static inline bool
drivers/dma-buf/dma-buf.c +3
-1@@ -1139,7 +1177,9 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, if (ret < 0) goto error_unmap; } -mangle_sg_table (sg_table); + ret =dma_buf_mangle_sg_table (&sg_table); +if (ret) + goto error_unmap;if (IS_ENABLED (CONFIG_DMA_API_DEBUG )) {struct scatterlist *sg;drivers/dma-buf/dma-buf.c +1
-1@@ -1220,7 +1260,7 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,dma_resv_assert_held (attach->dmabuf->resv); -mangle_sg_table (sg_table); +dma_buf_demangle_sg_table (&sg_table); attach->dmabuf->ops->unmap_dma_buf (attach, sg_table, direction);if (dma_buf_pin_on_map(attach))--
2.43.0
Ruhl, Michael J
Reviewed-byRE: [PATCH 2/2] dma-buf: improve sg_table debugging hack
Quoted 45 lines
-----Original Message-----
From: Christian König <[email protected]>
Sent: Monday, October 13, 2025 11:02 AM
To: Ruhl, Michael J <[email protected]>; [email protected]; [email protected]; [email protected]; linaro-mm- [email protected]; [email protected]
Subject: Re: [PATCH 2/2] dma-buf: improvesg_tabledebugging hackQuoted 9 lines · Michael J
Quoted 9 lines
-----Original Message-----
From: dri-devel <[email protected]> On Behalf Of Christian König
Sent: Monday, October 6, 2025 9:47 AM
To: [email protected]; [email protected]; dri- [email protected]; [email protected]; [email protected]
Subject: [PATCH 2/2] dma-buf: improvesg_tabledebugging hackQuoted 5 lines
Quoted 2 lines
Instead of just mangling the page link create a copy of the
sg_tablebut only copy over the DMA addresses and not the pages.This made the issue obvious. If I abuse this now how will I know I am doing the wrong thing?
You get a crash when you try to to convert a page link into a struct page pointer and then access fields in that struct page (the pointer is NULL for most entries now).
Quoted 7 lines
Quoted 5 lines
Still quite a hack but this at least allows the exporter to properly keeps it's
sg_tableintact.This is important for example for the system DMA-heap which needs it's
sg_tableto sync CPU writes with device accesses.So the mangling actually breaks a usage? I thought that the usage was
incorrect...is
Quoted 1 line
the dma-heap using this incorrectly?
No, dma-heap as an exporter is using it perfectly correctly.
The problem was (or rather is) that some importers turned the page link into a struct page again and then tried to modify that struct page, e.g. grabbing references to it or similar.
Quoted 8 lines
That turned into tons of problems when the exporter used device private pages and didn't expect that somebody messing with it.
The only property importers are allowed to access in the
sg_tablesthey get fromdma_bufare the DMA-addresses.This patch here is a first step to replace using
sg_tableswith something else like xarray of DMA-addresses or similar.
Ok got it.
Some further comments below...
Quoted 64 lines
Regards,
Christian.Quoted 4 lines
Quoted 4 lines
Signed-off-by Christian König <[email protected]>---
drivers/dma-buf/dma-buf.c| 68 +++++++++++++++++++++++++++++++-----
Quoted 56 lines
Quoted 41 lines
---
1 file changed, 54 insertions(+), 14 deletions(-)drivers/dma-buf/dma-buf.c +11
-9@@ -828,21 +828,59 @@ void dma_buf_put(struct dma_buf *dmabuf) }EXPORT_SYMBOL_NS_GPL (dma_buf_put,"DMA_BUF" ); -static void mangle_sg_table (struct sg_table *sg_table) +static int dma_buf_mangle_sg_table (struct sg_table **sg_table) { -#ifdef CONFIG_DMABUF_DEBUG -int i; -struct scatterlist *sg; - -/* To catch abuse of the underlying struct page by importers mix -* up the bits, but take care to preserve the low SG_ bits to -* not corrupt the sgt. The mixing is undone on unmap -* before passing the sgt back to the exporter. +struct sg_table *to, *from = *sg_table; +struct scatterlist *to_sg, *from_sg; +int i, ret; + +if (!IS_ENABLED (CONFIG_DMABUF_DEBUG )) +return 0 ; + +/* +* To catch abuse of the underlying struct page by importers copy the +* sg_table without copying the page_link and give only the copy back to
drivers/dma-buf/dma-buf.c +1
-0+* the importer. */drivers/dma-buf/dma-buf.c +3
-3-for_each_sgtable_sg (sg_table, sg, i) - sg->page_link ^= ~0xffUL ; -#endif + to =kzalloc (sizeof (*to),GFP_KERNEL ); +if (!to) +return -ENOMEM ;drivers/dma-buf/dma-buf.c void dma_buf_put(struct dma_buf *dmabuf) · line 857
Quoted 10 lines
drivers/dma-buf/dma-buf.c +9
-0+ ret =sg_alloc_table (to, from->nents,GFP_KERNEL ); +if (ret) + goto free_to; + + to_sg = to->sgl; +for_each_sgtable_dma_sg (from, from_sg, i) { +sg_set_page (to_sg,NULL ,0 ,0 ); +sg_dma_address (to_sg) =sg_dma_address (from_sg); +sg_dma_len (to_sg) =sg_dma_len (from_sg);Is the indentation correct here?
M
Quoted 8 lines
Quoted 8 lines
drivers/dma-buf/dma-buf.c void dma_buf_put(struct dma_buf *dmabuf) · line 865
Quoted 8 lines
drivers/dma-buf/dma-buf.c +8
-0+ to_sg =sg_next (to_sg); + } + +/* +* Store the original sg_table in the first page_link of the copy so +* that we can revert everything back again on unmap. +*/ + to->sgl[0 ].page_link = (unsigned long )from;
If the concern is the importer touching the table when it shouldn't...why is this ok?
I understand that you are only copying the dma info...but if I do something like for_each_sg()... will that take me to the original?
I kinda think that keeping this info inaccessible to the importer would make more sense? (however I see the problem that we can't make it part of the dma-buf struct because there could be multiple importers...)
Hmm. You do say that this is "still a hack"... so doing this is for the debug purposes and will go away with the future steps?
If that is the plan, (and with the indentation issue fixed):
Mike
Quoted 47 lines
Quoted 20 lines
Quoted 20 lines
drivers/dma-buf/dma-buf.c +18
-0+ *sg_table = to; +return 0 ; + +free_to: +kfree (to); +return ret; +} + +static void dma_buf_demangle_sg_table (struct sg_table **sg_table) +{ +struct sg_table *copy = *sg_table; + +if (!IS_ENABLED (CONFIG_DMABUF_DEBUG )) +return ; + + *sg_table = (void *)copy->sgl[0 ].page_link; +sg_free_table (copy); +kfree (copy);}
Quoted 2 lines
drivers/dma-buf/dma-buf.c struct sg_table
Quoted 2 lines
static inline bool
drivers/dma-buf/dma-buf.c +0
-0@@ -1139,7 +1177,9 @@ struct sg_table*dma_buf_map_attachment(struct
Quoted 24 lines
Quoted 23 lines
drivers/dma-buf/dma-buf.c +3
-1dma_buf_attachment *attach, if (ret < 0) goto error_unmap; } -mangle_sg_table (sg_table); + ret =dma_buf_mangle_sg_table (&sg_table); +if (ret) + goto error_unmap;if (IS_ENABLED (CONFIG_DMA_API_DEBUG )) {struct scatterlist *sg;drivers/dma-buf/dma-buf.c +1
-1@@ -1220,7 +1260,7 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,dma_resv_assert_held (attach->dmabuf->resv); -mangle_sg_table (sg_table); +dma_buf_demangle_sg_table (&sg_table); attach->dmabuf->ops->unmap_dma_buf (attach, sg_table, direction);if (dma_buf_pin_on_map(attach))--
2.43.0
Quoted 11 lines
-----Original Message-----
From: dri-devel <[email protected]> On Behalf Of Christian König
Sent: Monday, October 6, 2025 9:47 AM
To: [email protected]; [email protected]; dri- [email protected]; [email protected]; [email protected]
Subject: [PATCH 1/2] dma-buf: replace "#if" with just "if"No need to conditional compile that code, let the compilers dead code elimination handle it instead.
Makes sense to me.
Quoted 36 lines
Signed-off-by Christian König <[email protected]>---
drivers/dma-buf/dma-buf.c| 5 ++---1 file changed, 2 insertions(+), 3 deletions(-)drivers/dma-buf/dma-buf.c +1
-2@@ -1141,8 +1141,7 @@ struct sg_table *dma_buf_map_attachment(structdma_buf_attachment *attach, }mangle_sg_table (sg_table ); -#ifdef CONFIG_DMA_API_DEBUG - { +if (IS_ENABLED (CONFIG_DMA_API_DEBUG )) {struct scatterlist *sg;u64 addr;int len;drivers/dma-buf/dma-buf.c +1
-1@@ -1154,10 +1153,10 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, if (!PAGE_ALIGNED(addr) || !PAGE_ALIGNED(len)) { pr_debug("%s: addr %llx or len %x is not page aligned!\n", __func__, addr, len); + break; } } } -#endif /* CONFIG_DMA_API_DEBUG */ return sg_table;
error_unmap:
--
2.43.0