2023-12-14 09:16:57

by Joakim Zhang

[permalink] [raw]
Subject: [PATCH V2] dma-mapping: Set dma_mem pointer as NULL after it's freed

From: Joakim Zhang <[email protected]>

Reproduced with below sequence:
dma_declare_coherent_memory()->dma_release_coherent_memory()
->dma_declare_coherent_memory()->"return -EBUSY" error

It will return -EBUSY from the dma_assign_coherent_memory()
in dma_declare_coherent_memory(), the reason is that dev->dma_mem
pointer has not been set to NULL after it's freed.

Fixes: cf65a0f6f6ff ("dma-mapping: move all DMA mapping code to kernel/dma")
Signed-off-by: Joakim Zhang <[email protected]>
---
ChangeLogs:
V1->V2:
* remove _dma_release_coherent_memory() from
rmem_dma_device_release()
* update commit message
---
kernel/dma/coherent.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/kernel/dma/coherent.c b/kernel/dma/coherent.c
index c21abc77c53e..ff5683a57f77 100644
--- a/kernel/dma/coherent.c
+++ b/kernel/dma/coherent.c
@@ -132,8 +132,10 @@ int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,

void dma_release_coherent_memory(struct device *dev)
{
- if (dev)
+ if (dev) {
_dma_release_coherent_memory(dev->dma_mem);
+ dev->dma_mem = NULL;
+ }
}

static void *__dma_alloc_from_coherent(struct device *dev,
--
2.25.1


2023-12-15 11:32:47

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH V2] dma-mapping: Set dma_mem pointer as NULL after it's freed

I've applied this after fixing the whitespace errors.

But we really need to get remoteporc off messing with the reserved
regions itself and retire this API entirely.

2023-12-15 14:25:11

by Joakim Zhang

[permalink] [raw]
Subject: RE: [PATCH V2] dma-mapping: Set dma_mem pointer as NULL after it's freed


Hi Christoph Hellwig,

> -----Original Message-----
> From: Christoph Hellwig <[email protected]>
> Sent: Friday, December 15, 2023 7:32 PM
> To: Joakim Zhang <[email protected]>
> Cc: [email protected]; [email protected]; [email protected];
> [email protected]; [email protected]; cix-kernel-upstream
> <[email protected]>
> Subject: Re: [PATCH V2] dma-mapping: Set dma_mem pointer as NULL after
> it's freed
>
> I've applied this after fixing the whitespace errors.
>
> But we really need to get remoteporc off messing with the reserved regions
> itself and retire this API entirely.

Indeed, I met this issue when debugging remoteproc, this API maybe useful since some system would not support device tree, but still want to reserve memory in the driver.