public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH V3 0/2] Add reattaching support and fix memory leak issue
@ 2023-03-13  7:50 Chunyan Zhang
  2023-03-13  7:50 ` [PATCH V3 1/2] iommu: sprd: release dma buffer to avoid memory leak Chunyan Zhang
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Chunyan Zhang @ 2023-03-13  7:50 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon, Robin Murphy
  Cc: iommu, Baolu Lu, Baolin Wang, Orson Zhai, Chunyan Zhang,
	Chunyan Zhang, LKML

V3:
* Split into two patches;
* Added support reattching an existing domain;
* Release DMA buffer only when domain freed.

V2: https://lkml.org/lkml/2023/3/7/1717
* Added some comment in sprd_iommu_attach_device() for the reason
  of calling sprd_iommu_cleanup().

V1: https://lkml.org/lkml/2023/2/10/198

Chunyan Zhang (2):
  iommu: sprd: release dma buffer to avoid memory leak
  iommu: sprd: Add support for reattaching an existing domain

 drivers/iommu/sprd-iommu.c | 54 +++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 13 deletions(-)

-- 
2.25.1


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

* [PATCH V3 1/2] iommu: sprd: release dma buffer to avoid memory leak
  2023-03-13  7:50 [PATCH V3 0/2] Add reattaching support and fix memory leak issue Chunyan Zhang
@ 2023-03-13  7:50 ` Chunyan Zhang
  2023-03-13  7:50 ` [PATCH V3 2/2] iommu: sprd: Add support for reattaching an existing domain Chunyan Zhang
  2023-03-27  2:47 ` [PATCH V3 0/2] Add reattaching support and fix memory leak issue Chunyan Zhang
  2 siblings, 0 replies; 7+ messages in thread
From: Chunyan Zhang @ 2023-03-13  7:50 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon, Robin Murphy
  Cc: iommu, Baolu Lu, Baolin Wang, Orson Zhai, Chunyan Zhang,
	Chunyan Zhang, LKML

When attaching to a domain, the driver would alloc a DMA buffer which
is used to store address mapping table, and it need to be released
when the IOMMU domain is freed.

Signed-off-by: Chunyan Zhang <chunyan.zhang@unisoc.com>
---
 drivers/iommu/sprd-iommu.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/drivers/iommu/sprd-iommu.c b/drivers/iommu/sprd-iommu.c
index ae94d74b73f4..7df1f730c778 100644
--- a/drivers/iommu/sprd-iommu.c
+++ b/drivers/iommu/sprd-iommu.c
@@ -151,13 +151,6 @@ static struct iommu_domain *sprd_iommu_domain_alloc(unsigned int domain_type)
 	return &dom->domain;
 }
 
-static void sprd_iommu_domain_free(struct iommu_domain *domain)
-{
-	struct sprd_iommu_domain *dom = to_sprd_domain(domain);
-
-	kfree(dom);
-}
-
 static void sprd_iommu_first_vpn(struct sprd_iommu_domain *dom)
 {
 	struct sprd_iommu_device *sdev = dom->sdev;
@@ -230,6 +223,28 @@ static void sprd_iommu_hw_en(struct sprd_iommu_device *sdev, bool en)
 	sprd_iommu_update_bits(sdev, reg_cfg, mask, 0, val);
 }
 
+static void sprd_iommu_cleanup(struct sprd_iommu_domain *dom)
+{
+	size_t pgt_size;
+
+	/* Nothing need to do if the domain hasn't been attached */
+	if (!dom->sdev)
+		return;
+
+	pgt_size = sprd_iommu_pgt_size(&dom->domain);
+	dma_free_coherent(dom->sdev->dev, pgt_size, dom->pgt_va, dom->pgt_pa);
+	dom->sdev = NULL;
+	sprd_iommu_hw_en(dom->sdev, false);
+}
+
+static void sprd_iommu_domain_free(struct iommu_domain *domain)
+{
+	struct sprd_iommu_domain *dom = to_sprd_domain(domain);
+
+	sprd_iommu_cleanup(dom);
+	kfree(dom);
+}
+
 static int sprd_iommu_attach_device(struct iommu_domain *domain,
 				    struct device *dev)
 {
-- 
2.25.1


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

* [PATCH V3 2/2] iommu: sprd: Add support for reattaching an existing domain
  2023-03-13  7:50 [PATCH V3 0/2] Add reattaching support and fix memory leak issue Chunyan Zhang
  2023-03-13  7:50 ` [PATCH V3 1/2] iommu: sprd: release dma buffer to avoid memory leak Chunyan Zhang
@ 2023-03-13  7:50 ` Chunyan Zhang
  2023-03-27  2:47 ` [PATCH V3 0/2] Add reattaching support and fix memory leak issue Chunyan Zhang
  2 siblings, 0 replies; 7+ messages in thread
From: Chunyan Zhang @ 2023-03-13  7:50 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon, Robin Murphy
  Cc: iommu, Baolu Lu, Baolin Wang, Orson Zhai, Chunyan Zhang,
	Chunyan Zhang, LKML

This IOMMU driver should allow a domain to be attached more than once.

If IOMMU is reattaching to the same domain which is attached, there's
nothing to be done.

If reattching to a previously-used domain, do not alloc DMA buffer
again which stores address mapping table to avoid memory leak.

Signed-off-by: Chunyan Zhang <chunyan.zhang@unisoc.com>
---
 drivers/iommu/sprd-iommu.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/drivers/iommu/sprd-iommu.c b/drivers/iommu/sprd-iommu.c
index 7df1f730c778..3513b2b108bf 100644
--- a/drivers/iommu/sprd-iommu.c
+++ b/drivers/iommu/sprd-iommu.c
@@ -62,6 +62,7 @@ enum sprd_iommu_version {
  * @eb: gate clock which controls IOMMU access
  */
 struct sprd_iommu_device {
+	struct sprd_iommu_domain	*dom;
 	enum sprd_iommu_version	ver;
 	u32			*prot_page_va;
 	dma_addr_t		prot_page_pa;
@@ -252,15 +253,27 @@ static int sprd_iommu_attach_device(struct iommu_domain *domain,
 	struct sprd_iommu_domain *dom = to_sprd_domain(domain);
 	size_t pgt_size = sprd_iommu_pgt_size(domain);
 
-	if (dom->sdev)
-		return -EINVAL;
+	/* The device is attached to this domain */
+	if (sdev->dom == dom)
+		return 0;
 
-	dom->pgt_va = dma_alloc_coherent(sdev->dev, pgt_size, &dom->pgt_pa, GFP_KERNEL);
-	if (!dom->pgt_va)
-		return -ENOMEM;
+	/* The first time that domain is attaching to a device */
+	if (!dom->pgt_va) {
+		dom->pgt_va = dma_alloc_coherent(sdev->dev, pgt_size, &dom->pgt_pa, GFP_KERNEL);
+		if (!dom->pgt_va)
+			return -ENOMEM;
+
+		dom->sdev = sdev;
+	}
 
-	dom->sdev = sdev;
+	sdev->dom = dom;
 
+	/*
+	 * One sprd IOMMU serves one client device only, disabled it before
+	 * configure mapping table to avoid access conflict in case other
+	 * mapping table is stored in.
+	 */
+	sprd_iommu_hw_en(sdev, false);
 	sprd_iommu_first_ppn(dom);
 	sprd_iommu_first_vpn(dom);
 	sprd_iommu_vpn_range(dom);
-- 
2.25.1


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

* Re: [PATCH V3 0/2] Add reattaching support and fix memory leak issue
  2023-03-13  7:50 [PATCH V3 0/2] Add reattaching support and fix memory leak issue Chunyan Zhang
  2023-03-13  7:50 ` [PATCH V3 1/2] iommu: sprd: release dma buffer to avoid memory leak Chunyan Zhang
  2023-03-13  7:50 ` [PATCH V3 2/2] iommu: sprd: Add support for reattaching an existing domain Chunyan Zhang
@ 2023-03-27  2:47 ` Chunyan Zhang
  2023-03-27  8:01   ` Joerg Roedel
  2 siblings, 1 reply; 7+ messages in thread
From: Chunyan Zhang @ 2023-03-27  2:47 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon, Robin Murphy
  Cc: iommu, Baolu Lu, Baolin Wang, Orson Zhai, LKML, Chunyan Zhang

Hi Joerg,

On Mon, 13 Mar 2023 at 15:51, Chunyan Zhang <chunyan.zhang@unisoc.com> wrote:
>
> V3:
> * Split into two patches;
> * Added support reattching an existing domain;
> * Release DMA buffer only when domain freed.
>
> V2: https://lkml.org/lkml/2023/3/7/1717
> * Added some comment in sprd_iommu_attach_device() for the reason
>   of calling sprd_iommu_cleanup().
>
> V1: https://lkml.org/lkml/2023/2/10/198
>
> Chunyan Zhang (2):
>   iommu: sprd: release dma buffer to avoid memory leak
>   iommu: sprd: Add support for reattaching an existing domain

Not sure if you received this patchset, since I received a rejection
letter from mail.8bytes.org.

If you didn't receive it, I can resend it with another email address.

Otherwise, would you please pick up this series if there are no more comments.

Thanks,
Chunyan


>
>  drivers/iommu/sprd-iommu.c | 54 +++++++++++++++++++++++++++++---------
>  1 file changed, 41 insertions(+), 13 deletions(-)
>
> --
> 2.25.1
>

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

* Re: [PATCH V3 0/2] Add reattaching support and fix memory leak issue
  2023-03-27  2:47 ` [PATCH V3 0/2] Add reattaching support and fix memory leak issue Chunyan Zhang
@ 2023-03-27  8:01   ` Joerg Roedel
  2023-03-28  2:57     ` Chunyan Zhang
  0 siblings, 1 reply; 7+ messages in thread
From: Joerg Roedel @ 2023-03-27  8:01 UTC (permalink / raw)
  To: Chunyan Zhang
  Cc: Will Deacon, Robin Murphy, iommu, Baolu Lu, Baolin Wang,
	Orson Zhai, LKML, Chunyan Zhang


Hi Chunyan,

On Mon, Mar 27, 2023 at 10:47:18AM +0800, Chunyan Zhang wrote:
> Not sure if you received this patchset, since I received a rejection
> letter from mail.8bytes.org.
> 
> If you didn't receive it, I can resend it with another email address.
> 
> Otherwise, would you please pick up this series if there are no more comments.

I didn't receive it, can you please send me the error message you got
from my mail server?

Regards,

	Joerg

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

* Re: [PATCH V3 0/2] Add reattaching support and fix memory leak issue
  2023-03-27  8:01   ` Joerg Roedel
@ 2023-03-28  2:57     ` Chunyan Zhang
  2023-03-28 13:13       ` Joerg Roedel
  0 siblings, 1 reply; 7+ messages in thread
From: Chunyan Zhang @ 2023-03-28  2:57 UTC (permalink / raw)
  To: Joerg Roedel
  Cc: Will Deacon, Robin Murphy, iommu, Baolu Lu, Baolin Wang,
	Orson Zhai, LKML, Chunyan Zhang

On Mon, 27 Mar 2023 at 16:01, Joerg Roedel <joro@8bytes.org> wrote:
>
>
> Hi Chunyan,
>
> On Mon, Mar 27, 2023 at 10:47:18AM +0800, Chunyan Zhang wrote:
> > Not sure if you received this patchset, since I received a rejection
> > letter from mail.8bytes.org.
> >
> > If you didn't receive it, I can resend it with another email address.
> >
> > Otherwise, would you please pick up this series if there are no more comments.
>
> I didn't receive it, can you please send me the error message you got
> from my mail server?

Have sent it to you.

>
> Regards,
>
>         Joerg

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

* Re: [PATCH V3 0/2] Add reattaching support and fix memory leak issue
  2023-03-28  2:57     ` Chunyan Zhang
@ 2023-03-28 13:13       ` Joerg Roedel
  0 siblings, 0 replies; 7+ messages in thread
From: Joerg Roedel @ 2023-03-28 13:13 UTC (permalink / raw)
  To: Chunyan Zhang
  Cc: Will Deacon, Robin Murphy, iommu, Baolu Lu, Baolin Wang,
	Orson Zhai, LKML, Chunyan Zhang

On Tue, Mar 28, 2023 at 10:57:30AM +0800, Chunyan Zhang wrote:
> Have sent it to you.

I see, can you please re-send the patch-set to me from a properly
configures email domain?

Regards,

	Joerg

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

end of thread, other threads:[~2023-03-28 13:14 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-13  7:50 [PATCH V3 0/2] Add reattaching support and fix memory leak issue Chunyan Zhang
2023-03-13  7:50 ` [PATCH V3 1/2] iommu: sprd: release dma buffer to avoid memory leak Chunyan Zhang
2023-03-13  7:50 ` [PATCH V3 2/2] iommu: sprd: Add support for reattaching an existing domain Chunyan Zhang
2023-03-27  2:47 ` [PATCH V3 0/2] Add reattaching support and fix memory leak issue Chunyan Zhang
2023-03-27  8:01   ` Joerg Roedel
2023-03-28  2:57     ` Chunyan Zhang
2023-03-28 13:13       ` Joerg Roedel

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