Skip to contentSkip to conversation

tegra alloc_pdir

PATCH Superseded Chris Wright 4 participants 19 messages Jun 26, 2012 – Jul 2, 2012 15 messages not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

This looks buggy to me:

drivers/iommu/tegra-smmu.c::smmu_iommu_domain_init()

spin_lock_irqsave(&tmp->lock, flags); <-- lock held, interrupts off
alloc_pdir(as = tmp)

    devm_kzalloc(..., GFP_KERNEL);        <-- potentially sleeping allocation
    ...
    alloc_page(..., GFP_KERNEL | __GFP_DMA);   <-- same here

thanks,
-chris

#2 lore raw
In reply to a message not in the archive

Hi Chris,

Chris Wright <[email protected]> wrote @ Tue, 26 Jun 2012 20:15:55 +0200:

Quoted 9 lines

This looks buggy to me:

drivers/iommu/tegra-smmu.c::smmu_iommu_domain_init()

spin_lock_irqsave(&tmp->lock, flags); <-- lock held, interrupts off
alloc_pdir(as = tmp)

    devm_kzalloc(..., GFP_KERNEL);        <-- potentially sleeping allocation
    ...
    alloc_page(..., GFP_KERNEL | __GFP_DMA);   <-- same here

Right. I totally agree. Thank you for pointing this out.

The fix patch follows.

#4 lore raw

PATCH 1/1 iommu/tegra: smmu: Fix unsleepable memory allocation

In reply to a message not in the archive

allo_pdir() is called in smmu_iommu_domain_init() with spin_lock held. memory allocations in it have to be atomic/unsleepable.

---
 drivers/iommu/tegra-smmu.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/tegra-smmu.c +2 -2
index ecd6790..3f3d09d 100644
@@ -550,13 +550,13 @@ static int alloc_pdir(struct smmu_as *as)
 		return 0;
 
 	as->pte_count = devm_kzalloc(smmu->dev,
-		     sizeof(as->pte_count[0]) * SMMU_PDIR_COUNT, GFP_KERNEL);
+		     sizeof(as->pte_count[0]) * SMMU_PDIR_COUNT, GFP_ATOMIC);
 	if (!as->pte_count) {
 		dev_err(smmu->dev,
 			"failed to allocate smmu_device PTE cunters\n");
 		return -ENOMEM;
 	}
-	as->pdir_page = alloc_page(GFP_KERNEL | __GFP_DMA);
+	as->pdir_page = alloc_page(GFP_ATOMIC | __GFP_DMA);
 	if (!as->pdir_page) {
 		dev_err(smmu->dev,
 			"failed to allocate smmu_device page directory\n");
Signature · 1 line
1.7.5.4
#7 lore raw

Re: [PATCH 1/1] iommu/tegra: smmu: Fix unsleepable memory allocation

In reply to a message not in the archive
Quoted 2 lines · Hiroshi DOYU

allo_pdir() is called in smmu_iommu_domain_init() with spin_lock held. memory allocations in it have to be atomic/unsleepable.

Presumably this will cause allocation failures in situations with heavy memory pressure, rather than triggering swapping. Is it normal for IOMMU drivers to work that way? If so, I'm fine with it. Otherwise, can the allocations be performed before the spinlock is taken?

#9 lore raw

Re: [PATCH 1/1] iommu/tegra: smmu: Fix unsleepable memory allocation

In reply to a message not in the archive
Quoted 8 lines · On Wed, 27 Jun 2012 18:59:48 +0200 Stephen Warren…
Quoted 2 lines · Hiroshi DOYU

allo_pdir() is called in smmu_iommu_domain_init() with spin_lock held. memory allocations in it have to be atomic/unsleepable.

Presumably this will cause allocation failures in situations with heavy memory pressure, rather than triggering swapping. Is it normal for IOMMU drivers to work that way? If so, I'm fine with it. Otherwise, can the allocations be performed before the spinlock is taken?

There are some requirement for atomic allocation from DMA mapping API, but it's not normla for IOMMU, especiall not for iommu_domain_alloc(). It has already used GFP_KERNEL and then, calls ->iommu_domain_init() in it.

The update patch v2 follows.

#11 lore raw

PATCH v2 1/1 iommu/tegra: smmu: Fix unsleepable memory allocation at alloc_pdir()

In reply to a message not in the archive

alloc_pdir() is called from smmu_iommu_domain_init() with spin_lock held. memory allocations in alloc_pdir() had to be atomic/unsleepable. Instead of converting into atomic allocation, this patch once releases a lock, do the allocation, hold the lock again and then see if it's raced or not in order to avoid introducing mutex.

---
 drivers/iommu/tegra-smmu.c |   49 +++++++++++++++++++++++++++++--------------
 1 files changed, 33 insertions(+), 16 deletions(-)
drivers/iommu/tegra-smmu.c +33 -16
index ecd6790..ec656ec 100644
@@ -539,31 +539,42 @@ static inline void put_signature(struct smmu_as *as,
 /*
  * Caller must lock/unlock as
  */
-static int alloc_pdir(struct smmu_as *as)
+static int alloc_pdir(struct smmu_as *as, unsigned long *flags)
 {
 	unsigned long *pdir;
-	int pdn;
+	int pdn, err = 0;
 	u32 val;
 	struct smmu_device *smmu = as->smmu;
+	struct page *page;
+	unsigned int *cnt;
 
 	if (as->pdir_page)
 		return 0;
 
-	as->pte_count = devm_kzalloc(smmu->dev,
-		     sizeof(as->pte_count[0]) * SMMU_PDIR_COUNT, GFP_KERNEL);
-	if (!as->pte_count) {
-		dev_err(smmu->dev,
-			"failed to allocate smmu_device PTE cunters\n");
-		return -ENOMEM;
+	/*
+	 * do the allocation outside the as lock
+	 */
+	spin_unlock_irqrestore(&as->lock, *flags);
+	cnt = devm_kzalloc(smmu->dev,
+			   sizeof(cnt[0]) * SMMU_PDIR_COUNT, GFP_KERNEL);
+	page = alloc_page(GFP_KERNEL | __GFP_DMA);
+	spin_lock_irqsave(&as->lock, *flags);
+
+	if (as->pdir_page) {
+		/* We raced, free the redundant */
+		err = -ENODEV;
+		goto err_out;
 	}
-	as->pdir_page = alloc_page(GFP_KERNEL | __GFP_DMA);
-	if (!as->pdir_page) {
-		dev_err(smmu->dev,
-			"failed to allocate smmu_device page directory\n");
-		devm_kfree(smmu->dev, as->pte_count);
-		as->pte_count = NULL;
-		return -ENOMEM;
+
+	if (!page || !cnt) {
+		dev_err(smmu->dev, "failed to allocate at %s\n", __func__);
+		err = -ENOMEM;
+		goto err_out;
 	}
+
+	as->pdir_page = page;
+	as->pte_count = cnt;
+
 	SetPageReserved(as->pdir_page);
 	pdir = page_address(as->pdir_page);
 
@@ -580,6 +591,12 @@ static int alloc_pdir(struct smmu_as *as)
 	FLUSH_SMMU_REGS(as->smmu);
 
 	return 0;
+
+err_out:
+	devm_kfree(smmu->dev, cnt);
+	if (page)
+		__free_page(page);
+	return err;
 }
 
 static void __smmu_iommu_unmap(struct smmu_as *as, dma_addr_t iova)
@@ -791,7 +808,7 @@ static int smmu_iommu_domain_init(struct iommu_domain *domain)
 	return -ENODEV;
 
 found:
-	if (alloc_pdir(as) < 0)
+	if (alloc_pdir(as, &flags) < 0)
 		goto err_alloc_pdir;
 
 	spin_lock(&smmu->lock);
Signature · 1 line
1.7.5.4
#13 lore raw

Re: [v2 1/1] iommu/tegra: smmu: Fix unsleepable memory allocation at alloc_pdir()

In reply to a message not in the archive
Quoted 7 lines · Hiroshi DOYU

alloc_pdir() is called from smmu_iommu_domain_init() with spin_lock held. memory allocations in alloc_pdir() had to be atomic/unsleepable. Instead of converting into atomic allocation, this patch once releases a lock, do the allocation, hold the lock again and then see if it's raced or not in order to avoid introducing mutex.

---

You'd typically want to include a brief description of what changed from v1->v2 here, as a hint to reviewers re: what to concentrate on.

drivers/iommu/tegra-smmu.c static inline void put_signature(struct smmu_as *as, · line 567

Quoted 13 lines
drivers/iommu/tegra-smmu.c +11 -0
+static int alloc_pdir(struct smmu_as *as, unsigned long *flags)
 
+	spin_unlock_irqrestore(&as->lock, *flags);
+	cnt = devm_kzalloc(smmu->dev,
+			   sizeof(cnt[0]) * SMMU_PDIR_COUNT, GFP_KERNEL);
+	page = alloc_page(GFP_KERNEL | __GFP_DMA);
+	spin_lock_irqsave(&as->lock, *flags);
+
+	if (as->pdir_page) {
+		/* We raced, free the redundant */
+		err = -ENODEV;
+		goto err_out;
 	}

Is that really an error; it just means that something else allocated the same pdir already. Since the top of the function does:

if (as->pdir_page)
return 0;

I'd expect to s/err = -ENODEV/err = 0/ inside that if condition that I quoted above, but still of cause "goto err_out" to free the now unneeded allocations.

Aside from that, I think this looks reasonable.

#15 lore raw

Re: [v2 1/1] iommu/tegra: smmu: Fix unsleepable memory allocation at alloc_pdir()

In reply to a message not in the archive

Stephen Warren <[email protected]> wrote @ Thu, 28 Jun 2012 18:57:18 +0200:

Quoted 11 lines · Hiroshi DOYU
Quoted 7 lines

alloc_pdir() is called from smmu_iommu_domain_init() with spin_lock held. memory allocations in alloc_pdir() had to be atomic/unsleepable. Instead of converting into atomic allocation, this patch once releases a lock, do the allocation, hold the lock again and then see if it's raced or not in order to avoid introducing mutex.

---

You'd typically want to include a brief description of what changed from v1->v2 here, as a hint to reviewers re: what to concentrate on.

Ok, I'll.

Quoted 20 lines

Review

Quoted 13 lines
(quoted diff) +11 -0
+static int alloc_pdir(struct smmu_as *as, unsigned long *flags)
 
+	spin_unlock_irqrestore(&as->lock, *flags);
+	cnt = devm_kzalloc(smmu->dev,
+			   sizeof(cnt[0]) * SMMU_PDIR_COUNT, GFP_KERNEL);
+	page = alloc_page(GFP_KERNEL | __GFP_DMA);
+	spin_lock_irqsave(&as->lock, *flags);
+
+	if (as->pdir_page) {
+		/* We raced, free the redundant */
+		err = -ENODEV;
+		goto err_out;
 	}

Is that really an error; it just means that something else allocated the same pdir already. Since the top of the function does:

(quoted diff) +0 -0
 	if (as->pdir_page)
 		return 0;
Quoted 5 lines

I'd expect to s/err = -ENODEV/err = 0/ inside that if condition that I quoted above, but still of cause "goto err_out" to free the now unneeded allocations.

Aside from that, I think this looks reasonable.

I think that, in the case of race condition, the one which comes later, should retry with the another ASID, which is incremented as below. So I modified that the latter one returns with -EAGAIN, and try with another ASID.

The complete patch follows this mail.

Changes:
Modified drivers/iommu/tegra-smmu.c

drivers/iommu/tegra-smmu.c +10 -10
index ec656ec..f2c18fa 100644
@@ -562,7 +562,7 @@ static int alloc_pdir(struct smmu_as *as, unsigned long *flags)
 
 	if (as->pdir_page) {
 		/* We raced, free the redundant */
-		err = -ENODEV;
+		err = -EAGAIN;
 		goto err_out;
 	}
 
@@ -799,8 +799,15 @@ static int smmu_iommu_domain_init(struct iommu_domain *domain)
 
 		spin_lock_irqsave(&tmp->lock, flags);
 		if (!tmp->pdir_page) {
-			as = tmp;
-			goto found;
+			int err;
+
+			err = alloc_pdir(tmp, &flags);
+			if (!err) {
+				as = tmp;
+				goto found;
+			}
+			if (err == -EAGAIN)
+				continue;
 		}
 		spin_unlock_irqrestore(&tmp->lock, flags);
 	}
@@ -808,9 +815,6 @@ static int smmu_iommu_domain_init(struct iommu_domain *domain)
 	return -ENODEV;
 
 found:
-	if (alloc_pdir(as, &flags) < 0)
-		goto err_alloc_pdir;
-
 	spin_lock(&smmu->lock);
 
 	/* Update PDIR register */
@@ -826,10 +830,6 @@ found:
 
 	dev_dbg(smmu->dev, "smmu_as@%p\n", as);
 	return 0;
-
-err_alloc_pdir:
-	spin_unlock_irqrestore(&as->lock, flags);
-	return -ENODEV;
 }
 
 static void smmu_iommu_domain_destroy(struct iommu_domain *domain)
#17 lore raw

Re: [v2 1/1] iommu/tegra: smmu: Fix unsleepable memory allocation at alloc_pdir()

In reply to a message not in the archive
Quoted 32 lines · Hiroshi Doyu

Stephen Warren <[email protected]> wrote @ Thu, 28 Jun 2012 18:57:18 +0200:

Quoted 31 lines · Hiroshi DOYU
Quoted 9 lines

alloc_pdir() is called from smmu_iommu_domain_init() with spin_lock held. memory allocations in alloc_pdir() had to be atomic/unsleepable. Instead of converting into atomic allocation, this patch once releases a lock, do the allocation, hold the lock again and then see if it's raced or not in order to avoid introducing mutex.

(quoted diff) +2 -0
+static int alloc_pdir(struct smmu_as *as, unsigned long *flags)
 
+	spin_unlock_irqrestore(&as->lock, *flags);

Review

Quoted 10 lines
(quoted diff) +9 -0
+	cnt = devm_kzalloc(smmu->dev,
+			   sizeof(cnt[0]) * SMMU_PDIR_COUNT, GFP_KERNEL);
+	page = alloc_page(GFP_KERNEL | __GFP_DMA);
+	spin_lock_irqsave(&as->lock, *flags);
+
+	if (as->pdir_page) {
+		/* We raced, free the redundant */
+		err = -ENODEV;
+		goto err_out;
 	}

Is that really an error; it just means that something else allocated the same pdir already. Since the top of the function does:

(quoted diff) +0 -0
 	if (as->pdir_page)
 		return 0;

I'd expect to s/err = -ENODEV/err = 0/ inside that if condition that I quoted above, but still of cause "goto err_out" to free the now unneeded allocations.

Quoted 8 lines
Quoted 1 line

Aside from that, I think this looks reasonable.

I think that, in the case of race condition, the one which comes later, should retry with the another ASID, which is incremented as below. So I modified that the latter one returns with -EAGAIN, and try with another ASID.

The complete patch follows this mail.

incremental rather than complete, right?

Quoted 21 lines

Changes:
Modified drivers/iommu/tegra-smmu.c

drivers/iommu/tegra-smmu.c +1 -2
index ec656ec..f2c18fa 100644
@@ -562,7 +562,7 @@ static int alloc_pdir(struct smmu_as *as, unsigned long *flags)
 
 	if (as->pdir_page) {
 		/* We raced, free the redundant */
-		err = -ENODEV;
+		err = -EAGAIN;
 		goto err_out;
 	}
 
@@ -799,8 +799,15 @@ static int smmu_iommu_domain_init(struct iommu_domain *domain)
 
 		spin_lock_irqsave(&tmp->lock, flags);
 		if (!tmp->pdir_page) {
-			as = tmp;

drivers/iommu/tegra-smmu.c static int smmu_iommu_domain_init(struct iommu_domain *domain) · line 810

Quoted 10 lines
drivers/iommu/tegra-smmu.c +9 -1
-			goto found;
+			int err;
+
+			err = alloc_pdir(tmp, &flags);
+			if (!err) {
+				as = tmp;
+				goto found;
+			}
+			if (err == -EAGAIN)
+				continue;

That loop is going to continue anyway, since that code is right at the end of the loop. Don't you want to replace that if block with:

if (err != -EAGAIN)

    goto err_alloc_pdir;

?

Also, the first thinig that alloc_pdir does is:

        if (as->pdir_page)
                return 0;

It seems that should be removed completely, right? Since having the pdir_page already allocated is an error.

Quoted 22 lines
drivers/iommu/tegra-smmu.c +0 -7
 		}
 		spin_unlock_irqrestore(&tmp->lock, flags);
 	}
@@ -808,9 +815,6 @@ static int smmu_iommu_domain_init(struct iommu_domain *domain)
 	return -ENODEV;
 
 found:
-	if (alloc_pdir(as, &flags) < 0)
-		goto err_alloc_pdir;
-
 	spin_lock(&smmu->lock);
 
 	/* Update PDIR register */
@@ -826,10 +830,6 @@ found:
 
 	dev_dbg(smmu->dev, "smmu_as@%p\n", as);
 	return 0;
-
-err_alloc_pdir:
-	spin_unlock_irqrestore(&as->lock, flags);
-	return -ENODEV;
 }
#19 lore raw

Re: [v2 1/1] iommu/tegra: smmu: Fix unsleepable memory allocation at alloc_pdir()

In reply to a message not in the archive

Stephen Warren <[email protected]> wrote @ Thu, 28 Jun 2012 21:08:12 +0200:

Quoted 35 lines · Hiroshi Doyu
Quoted 34 lines

Stephen Warren <[email protected]> wrote @ Thu, 28 Jun 2012 18:57:18 +0200:

Quoted 32 lines · Hiroshi DOYU
Quoted 9 lines

alloc_pdir() is called from smmu_iommu_domain_init() with spin_lock held. memory allocations in alloc_pdir() had to be atomic/unsleepable. Instead of converting into atomic allocation, this patch once releases a lock, do the allocation, hold the lock again and then see if it's raced or not in order to avoid introducing mutex.

(quoted diff) +2 -0
+static int alloc_pdir(struct smmu_as *as, unsigned long *flags)
 
+	spin_unlock_irqrestore(&as->lock, *flags);

Review

Quoted 10 lines
(quoted diff) +9 -0
+	cnt = devm_kzalloc(smmu->dev,
+			   sizeof(cnt[0]) * SMMU_PDIR_COUNT, GFP_KERNEL);
+	page = alloc_page(GFP_KERNEL | __GFP_DMA);
+	spin_lock_irqsave(&as->lock, *flags);
+
+	if (as->pdir_page) {
+		/* We raced, free the redundant */
+		err = -ENODEV;
+		goto err_out;
 	}

Is that really an error; it just means that something else allocated the same pdir already. Since the top of the function does:

(quoted diff) +0 -0
 	if (as->pdir_page)
 		return 0;

I'd expect to s/err = -ENODEV/err = 0/ inside that if condition that I quoted above, but still of cause "goto err_out" to free the now unneeded allocations.

Aside from that, I think this looks reasonable.

Quoted 8 lines
Quoted 6 lines

I think that, in the case of race condition, the one which comes later, should retry with the another ASID, which is incremented as below. So I modified that the latter one returns with -EAGAIN, and try with another ASID.

The complete patch follows this mail.

incremental rather than complete, right?

Actually I was working on it.

Quoted 34 lines
Quoted 20 lines

Changes:
Modified drivers/iommu/tegra-smmu.c

drivers/iommu/tegra-smmu.c +1 -2
index ec656ec..f2c18fa 100644
@@ -562,7 +562,7 @@ static int alloc_pdir(struct smmu_as *as, unsigned long *flags)
 
 	if (as->pdir_page) {
 		/* We raced, free the redundant */
-		err = -ENODEV;
+		err = -EAGAIN;
 		goto err_out;
 	}
 
@@ -799,8 +799,15 @@ static int smmu_iommu_domain_init(struct iommu_domain *domain)
 
 		spin_lock_irqsave(&tmp->lock, flags);
 		if (!tmp->pdir_page) {
-			as = tmp;

drivers/iommu/tegra-smmu.c static int smmu_iommu_domain_init(struct iommu_domain *domain) · line 810

Quoted 10 lines
drivers/iommu/tegra-smmu.c +9 -1
-			goto found;
+			int err;
+
+			err = alloc_pdir(tmp, &flags);
+			if (!err) {
+				as = tmp;
+				goto found;
+			}
+			if (err == -EAGAIN)
+				continue;

That loop is going to continue anyway, since that code is right at the end of the loop. Don't you want to replace that if block with:

Quoted 4 lines

if (err != -EAGAIN)

    goto err_alloc_pdir;

?

Yes, I also found. I'll send the update one tomorrow.

Quoted 7 lines

Also, the first thinig that alloc_pdir does is:

        if (as->pdir_page)
                return 0;

It seems that should be removed completely, right? Since having the pdir_page already allocated is an error.

Yes. I thought that could be another patch. Same for the original code.

#21 lore raw

PATCH v3 1/2 iommu/tegra: smmu: Remove unnecessary sanity check at alloc_pdir()

In reply to a message not in the archive

alloc_pdir() is called with smmu->as[?].pdir_page == NULL. No need to check pdir_page again inside alloc_pdir().

Cc Stephen Warren <[email protected]>
---
 drivers/iommu/tegra-smmu.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
drivers/iommu/tegra-smmu.c +0 -3
index ecd6790..532c8a4 100644
@@ -546,9 +546,6 @@ static int alloc_pdir(struct smmu_as *as)
 	u32 val;
 	struct smmu_device *smmu = as->smmu;
 
-	if (as->pdir_page)
-		return 0;
-
 	as->pte_count = devm_kzalloc(smmu->dev,
 		     sizeof(as->pte_count[0]) * SMMU_PDIR_COUNT, GFP_KERNEL);
 	if (!as->pte_count) {
Signature · 1 line
1.7.5.4
#23 lore raw

PATCH v3 2/2 iommu/tegra: smmu: Fix unsleepable memory allocation at alloc_pdir()

In reply to a message not in the archive

alloc_pdir() is called from smmu_iommu_domain_init() with spin_lock held. memory allocations in alloc_pdir() had to be atomic. Instead of converting into atomic allocation, this patch once releases a lock, does the allocation, holds the lock again and then sees if it's raced or not in order to avoid introducing mutex and preallocation.

---
v3:
Added -EAGAIN in alloc_pdir when raced to retry.

---
 drivers/iommu/tegra-smmu.c |   77 +++++++++++++++++++++++++------------------
 1 files changed, 45 insertions(+), 32 deletions(-)
drivers/iommu/tegra-smmu.c +45 -32
index 532c8a4..dbba94c 100644
@@ -539,28 +539,39 @@ static inline void put_signature(struct smmu_as *as,
 /*
  * Caller must lock/unlock as
  */
-static int alloc_pdir(struct smmu_as *as)
+static int alloc_pdir(struct smmu_as *as, unsigned long *flags)
 {
 	unsigned long *pdir;
-	int pdn;
+	int pdn, err = 0;
 	u32 val;
 	struct smmu_device *smmu = as->smmu;
+	struct page *page;
+	unsigned int *cnt;
 
-	as->pte_count = devm_kzalloc(smmu->dev,
-		     sizeof(as->pte_count[0]) * SMMU_PDIR_COUNT, GFP_KERNEL);
-	if (!as->pte_count) {
-		dev_err(smmu->dev,
-			"failed to allocate smmu_device PTE cunters\n");
-		return -ENOMEM;
+	/*
+	 * do the allocation outside the as lock
+	 */
+	spin_unlock_irqrestore(&as->lock, *flags);
+	cnt = devm_kzalloc(smmu->dev,
+			   sizeof(cnt[0]) * SMMU_PDIR_COUNT, GFP_KERNEL);
+	page = alloc_page(GFP_KERNEL | __GFP_DMA);
+	spin_lock_irqsave(&as->lock, *flags);
+
+	if (as->pdir_page) {
+		/* We raced, free the redundant */
+		err = -EAGAIN;
+		goto err_out;
 	}
-	as->pdir_page = alloc_page(GFP_KERNEL | __GFP_DMA);
-	if (!as->pdir_page) {
-		dev_err(smmu->dev,
-			"failed to allocate smmu_device page directory\n");
-		devm_kfree(smmu->dev, as->pte_count);
-		as->pte_count = NULL;
-		return -ENOMEM;
+
+	if (!page || !cnt) {
+		dev_err(smmu->dev, "failed to allocate at %s\n", __func__);
+		err = -ENOMEM;
+		goto err_out;
 	}
+
+	as->pdir_page = page;
+	as->pte_count = cnt;
+
 	SetPageReserved(as->pdir_page);
 	pdir = page_address(as->pdir_page);
 
@@ -577,6 +588,12 @@ static int alloc_pdir(struct smmu_as *as)
 	FLUSH_SMMU_REGS(as->smmu);
 
 	return 0;
+
+err_out:
+	devm_kfree(smmu->dev, cnt);
+	if (page)
+		__free_page(page);
+	return err;
 }
 
 static void __smmu_iommu_unmap(struct smmu_as *as, dma_addr_t iova)
@@ -768,29 +785,29 @@ out:
 
 static int smmu_iommu_domain_init(struct iommu_domain *domain)
 {
-	int i;
+	int i, err = -ENODEV;
 	unsigned long flags;
 	struct smmu_as *as;
 	struct smmu_device *smmu = smmu_handle;
 
 	/* Look for a free AS with lock held */
 	for  (i = 0; i < smmu->num_as; i++) {
-		struct smmu_as *tmp = &smmu->as[i];
-
-		spin_lock_irqsave(&tmp->lock, flags);
-		if (!tmp->pdir_page) {
-			as = tmp;
-			goto found;
+		as = &smmu->as[i];
+		spin_lock_irqsave(&as->lock, flags);
+		if (!as->pdir_page) {
+			err = alloc_pdir(as, &flags);
+			if (!err)
+				goto found;
 		}
-		spin_unlock_irqrestore(&tmp->lock, flags);
+		spin_unlock_irqrestore(&as->lock, flags);
+		if (err != -EAGAIN)
+			break;
 	}
-	dev_err(smmu->dev, "no free AS\n");
-	return -ENODEV;
+	if (i == smmu->num_as)
+		dev_err(smmu->dev,  "no free AS\n");
+	return err;
 
 found:
-	if (alloc_pdir(as) < 0)
-		goto err_alloc_pdir;
-
 	spin_lock(&smmu->lock);
 
 	/* Update PDIR register */
@@ -806,10 +823,6 @@ found:
 
 	dev_dbg(smmu->dev, "smmu_as@%p\n", as);
 	return 0;
-
-err_alloc_pdir:
-	spin_unlock_irqrestore(&as->lock, flags);
-	return -ENODEV;
 }
 
 static void smmu_iommu_domain_destroy(struct iommu_domain *domain)
Signature · 1 line
1.7.5.4
#25 lore raw

Re: [PATCH 1/1] iommu/tegra: smmu: Fix unsleepable memory allocation

In reply to a message not in the archive
Quoted 19 lines · Hiroshi DOYU

allo_pdir() is called in smmu_iommu_domain_init() with spin_lock held. memory allocations in it have to be atomic/unsleepable.

---
 drivers/iommu/tegra-smmu.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/tegra-smmu.c +1 -1
index ecd6790..3f3d09d 100644
@@ -550,13 +550,13 @@ static int alloc_pdir(struct smmu_as *as)
 		return 0;
 
 	as->pte_count = devm_kzalloc(smmu->dev,
-		     sizeof(as->pte_count[0]) * SMMU_PDIR_COUNT, GFP_KERNEL);
+		     sizeof(as->pte_count[0]) * SMMU_PDIR_COUNT, GFP_ATOMIC);

drivers/iommu/tegra-smmu.c static int alloc_pdir(struct smmu_as *as) · line 562

Quoted 10 lines
drivers/iommu/tegra-smmu.c +1 -1
 	if (!as->pte_count) {
 		dev_err(smmu->dev,
 			"failed to allocate smmu_device PTE cunters\n");
 		return -ENOMEM;
 	}
-	as->pdir_page = alloc_page(GFP_KERNEL | __GFP_DMA);
+	as->pdir_page = alloc_page(GFP_ATOMIC | __GFP_DMA);
 	if (!as->pdir_page) {
 		dev_err(smmu->dev,
 			"failed to allocate smmu_device page directory\n");

Okay, I queued this one for v3.5. It is small enough so that it counts as a fix. The other two patches are 3.6 stuff.

Joerg

Signature · 5 lines
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#27 lore raw

Re: [PATCH 1/1] iommu/tegra: smmu: Fix unsleepable memory allocation

In reply to a message not in the archive

Hi Joerg,

Joerg Roedel <[email protected]> wrote @ Mon, 2 Jul 2012 11:57:47 +0200:

Quoted 17 lines · Hiroshi DOYU
Quoted 16 lines

allo_pdir() is called in smmu_iommu_domain_init() with spin_lock held. memory allocations in it have to be atomic/unsleepable.

---
 drivers/iommu/tegra-smmu.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/tegra-smmu.c +0 -0
index ecd6790..3f3d09d 100644
@@ -550,13 +550,13 @@ static int alloc_pdir(struct smmu_as *as)
 		return 0;
 
Quoted 16 lines

drivers/iommu/tegra-smmu.c static int alloc_pdir(struct smmu_as *as) · line 562

Quoted 13 lines
drivers/iommu/tegra-smmu.c +2 -2
 	as->pte_count = devm_kzalloc(smmu->dev,
-		     sizeof(as->pte_count[0]) * SMMU_PDIR_COUNT, GFP_KERNEL);
+		     sizeof(as->pte_count[0]) * SMMU_PDIR_COUNT, GFP_ATOMIC);
 	if (!as->pte_count) {
 		dev_err(smmu->dev,
 			"failed to allocate smmu_device PTE cunters\n");
 		return -ENOMEM;
 	}
-	as->pdir_page = alloc_page(GFP_KERNEL | __GFP_DMA);
+	as->pdir_page = alloc_page(GFP_ATOMIC | __GFP_DMA);
 	if (!as->pdir_page) {
 		dev_err(smmu->dev,
 			"failed to allocate smmu_device page directory\n");

Okay, I queued this one for v3.5. It is small enough so that it counts as a fix. The other two patches are 3.6 stuff.

The other 2 patehs are the replacement of this one. They are exclusive. Both of them are basically for the same fix.

#29 lore raw

Re: [PATCH 1/1] iommu/tegra: smmu: Fix unsleepable memory allocation

In reply to a message not in the archive

Hiroshi Doyu <[email protected]> wrote @ Mon, 2 Jul 2012 12:13:26 +0200:

Quoted 38 lines

Hi Joerg,

Joerg Roedel <[email protected]> wrote @ Mon, 2 Jul 2012 11:57:47 +0200:

Quoted 17 lines · Hiroshi DOYU
Quoted 16 lines

allo_pdir() is called in smmu_iommu_domain_init() with spin_lock held. memory allocations in it have to be atomic/unsleepable.

---
 drivers/iommu/tegra-smmu.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/tegra-smmu.c +0 -0
index ecd6790..3f3d09d 100644
@@ -550,13 +550,13 @@ static int alloc_pdir(struct smmu_as *as)
 		return 0;
 
Quoted 16 lines

drivers/iommu/tegra-smmu.c static int alloc_pdir(struct smmu_as *as) · line 562

Quoted 13 lines
drivers/iommu/tegra-smmu.c +2 -2
 	as->pte_count = devm_kzalloc(smmu->dev,
-		     sizeof(as->pte_count[0]) * SMMU_PDIR_COUNT, GFP_KERNEL);
+		     sizeof(as->pte_count[0]) * SMMU_PDIR_COUNT, GFP_ATOMIC);
 	if (!as->pte_count) {
 		dev_err(smmu->dev,
 			"failed to allocate smmu_device PTE cunters\n");
 		return -ENOMEM;
 	}
-	as->pdir_page = alloc_page(GFP_KERNEL | __GFP_DMA);
+	as->pdir_page = alloc_page(GFP_ATOMIC | __GFP_DMA);
 	if (!as->pdir_page) {
 		dev_err(smmu->dev,
 			"failed to allocate smmu_device page directory\n");

Okay, I queued this one for v3.5. It is small enough so that it counts as a fix. The other two patches are 3.6 stuff.

Quoted 2 lines

The other 2 patehs are the replacement of this one. They are exclusive. Both of them are basically for the same fix.

I might not get your point. If you plan to take the this(the original) as immediate fix for v3.5 you need tok take the others for v3.6 after *reverting* the original. This would also work.

#31 lore raw

Re: [PATCH 1/1] iommu/tegra: smmu: Fix unsleepable memory allocation

In reply to a message not in the archive
Quoted 7 lines · Hiroshi Doyu

Hiroshi Doyu <[email protected]> wrote @ Mon, 2 Jul 2012 12:13:26 +0200:

Quoted 2 lines

The other 2 patehs are the replacement of this one. They are exclusive. Both of them are basically for the same fix.

I might not get your point. If you plan to take the this(the original) as immediate fix for v3.5 you need tok take the others for v3.6 after *reverting* the original. This would also work.

Or you rebase the other two patches on-top of the first patch. Should not be too difficult and you can also include the Ack from Steven while at it :)

Joerg

Signature · 5 lines
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#33 lore raw

Re: [PATCH 1/1] iommu/tegra: smmu: Fix unsleepable memory allocation

In reply to a message not in the archive

"[email protected]" <[email protected]> wrote @ Mon, 2 Jul 2012 12:42:31 +0200:

Quoted 5 lines · Hiroshi Doyu
Quoted 4 lines

Hiroshi Doyu <[email protected]> wrote @ Mon, 2 Jul 2012 12:13:26 +0200:

Quoted 2 lines

The other 2 patehs are the replacement of this one. They are exclusive. Both of them are basically for the same fix.

Quoted 7 lines
Quoted 3 lines

I might not get your point. If you plan to take the this(the original) as immediate fix for v3.5 you need tok take the others for v3.6 after *reverting* the original. This would also work.

Or you rebase the other two patches on-top of the first patch. Should not be too difficult and you can also include the Ack from Steven while at it :)

I'm sending the following patch again. The first one is for v3.5. The rest goes to v3.6 as Joerg suggested.

v3.5
[1/1] iommu/tegra: smmu: Fix unsleepable memory allocation
v3.6
[1/3] Revert "iommu/tegra: smmu: Fix unsleepable memory allocation" [2/3] iommu/tegra: smmu: Remove unnecessary sanity check at alloc_pdir() [3/3] iommu/tegra: smmu: Fix unsleepable memory allocation at alloc_pdir()

Is this OK?