iommu.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RESEND v3] iommu/tegra-smmu: Add missing locks around mapping operations
@ 2020-08-14 16:22 Dmitry Osipenko
  2020-08-26 13:54 ` Sasha Levin
  2020-09-04  9:05 ` Joerg Roedel
  0 siblings, 2 replies; 6+ messages in thread
From: Dmitry Osipenko @ 2020-08-14 16:22 UTC (permalink / raw)
  To: Thierry Reding, Joerg Roedel, Jonathan Hunter
  Cc: linux-tegra, iommu, linux-kernel

The mapping operations of the Tegra SMMU driver are subjected to a race
condition issues because SMMU Address Space isn't allocated and freed
atomically, while it should be. This patch makes the mapping operations
atomic, it fixes an accidentally released Host1x Address Space problem
which happens while running multiple graphics tests in parallel on
Tegra30, i.e. by having multiple threads racing with each other in the
Host1x's submission and completion code paths, performing IOVA mappings
and unmappings in parallel.

Cc: <stable@vger.kernel.org>
Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
---

Changelog:

v3: - No changes. Resending for visibility, please apply.

v2: - Now using mutex instead of spinlock.

    - The _locked postfix is replaced with the underscores prefix.


 drivers/iommu/tegra-smmu.c | 38 ++++++++++++++++++++++++++++++++++----
 1 file changed, 34 insertions(+), 4 deletions(-)

diff --git a/drivers/iommu/tegra-smmu.c b/drivers/iommu/tegra-smmu.c
index 124c8848ab7e..4315b6381354 100644
--- a/drivers/iommu/tegra-smmu.c
+++ b/drivers/iommu/tegra-smmu.c
@@ -49,6 +49,7 @@ struct tegra_smmu_as {
 	struct iommu_domain domain;
 	struct tegra_smmu *smmu;
 	unsigned int use_count;
+	struct mutex lock;
 	u32 *count;
 	struct page **pts;
 	struct page *pd;
@@ -308,6 +309,8 @@ static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type)
 		return NULL;
 	}
 
+	mutex_init(&as->lock);
+
 	/* setup aperture */
 	as->domain.geometry.aperture_start = 0;
 	as->domain.geometry.aperture_end = 0xffffffff;
@@ -655,8 +658,9 @@ static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
 	smmu_flush(smmu);
 }
 
-static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
-			  phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
+static int
+__tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
+		 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
 {
 	struct tegra_smmu_as *as = to_smmu_as(domain);
 	dma_addr_t pte_dma;
@@ -685,8 +689,9 @@ static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
 	return 0;
 }
 
-static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
-			       size_t size, struct iommu_iotlb_gather *gather)
+static size_t
+__tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
+		   size_t size, struct iommu_iotlb_gather *gather)
 {
 	struct tegra_smmu_as *as = to_smmu_as(domain);
 	dma_addr_t pte_dma;
@@ -702,6 +707,31 @@ static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
 	return size;
 }
 
+static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
+			  phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
+{
+	struct tegra_smmu_as *as = to_smmu_as(domain);
+	int ret;
+
+	mutex_lock(&as->lock);
+	ret = __tegra_smmu_map(domain, iova, paddr, size, prot, gfp);
+	mutex_unlock(&as->lock);
+
+	return ret;
+}
+
+static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
+			       size_t size, struct iommu_iotlb_gather *gather)
+{
+	struct tegra_smmu_as *as = to_smmu_as(domain);
+
+	mutex_lock(&as->lock);
+	size = __tegra_smmu_unmap(domain, iova, size, gather);
+	mutex_unlock(&as->lock);
+
+	return size;
+}
+
 static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
 					   dma_addr_t iova)
 {
-- 
2.27.0

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* Re: [PATCH RESEND v3] iommu/tegra-smmu: Add missing locks around mapping operations
  2020-08-14 16:22 [PATCH RESEND v3] iommu/tegra-smmu: Add missing locks around mapping operations Dmitry Osipenko
@ 2020-08-26 13:54 ` Sasha Levin
  2020-09-04  9:05 ` Joerg Roedel
  1 sibling, 0 replies; 6+ messages in thread
From: Sasha Levin @ 2020-08-26 13:54 UTC (permalink / raw)
  To: Sasha Levin, Dmitry Osipenko, Thierry Reding; +Cc: linux-tegra, iommu, stable

Hi

[This is an automated email]

This commit has been processed because it contains a -stable tag.
The stable tag indicates that it's relevant for the following trees: all

The bot has tested the following trees: v5.8.2, v5.7.16, v5.4.59, v4.19.140, v4.14.193, v4.9.232, v4.4.232.

v5.8.2: Build OK!
v5.7.16: Build OK!
v5.4.59: Failed to apply! Possible dependencies:
    781ca2de89ba ("iommu: Add gfp parameter to iommu_ops::map")

v4.19.140: Failed to apply! Possible dependencies:
    06d60728ff5c ("iommu/dma: move the arm64 wrappers to common code")
    44f6876a00e8 ("iommu/arm-smmu: Support non-strict mode")
    46053c736854 ("dma-mapping: clear dev->dma_ops in arch_teardown_dma_ops")
    781ca2de89ba ("iommu: Add gfp parameter to iommu_ops::map")
    886643b76632 ("arm64: use the generic swiotlb_dma_ops")
    92aec09cc879 ("iommu/dma: Move __iommu_dma_map")
    96a299d24cfb ("iommu/arm-smmu: Add pm_runtime/sleep ops")
    c4dae366925f ("swiotlb: refactor swiotlb_map_page")
    d4a44f0750bb ("iommu/arm-smmu: Invoke pm_runtime across the driver")
    dff8d6c1ed58 ("swiotlb: remove the overflow buffer")
    fafadcd16595 ("swiotlb: don't dip into swiotlb pool for coherent allocations")

v4.14.193: Failed to apply! Possible dependencies:
    06d60728ff5c ("iommu/dma: move the arm64 wrappers to common code")
    10dac04c79b1 ("mips: fix an off-by-one in dma_capable")
    32b124492bdf ("iommu/io-pgtable-arm: Convert to IOMMU API TLB sync")
    32ce3862af3c ("powerpc/lib: Implement PMEM API")
    44f6876a00e8 ("iommu/arm-smmu: Support non-strict mode")
    781ca2de89ba ("iommu: Add gfp parameter to iommu_ops::map")
    92aec09cc879 ("iommu/dma: Move __iommu_dma_map")
    96a299d24cfb ("iommu/arm-smmu: Add pm_runtime/sleep ops")
    d4a44f0750bb ("iommu/arm-smmu: Invoke pm_runtime across the driver")
    ea8c64ace866 ("dma-mapping: move swiotlb arch helpers to a new header")

v4.9.232: Failed to apply! Possible dependencies:
    125458ab3aef ("iommu/arm-smmu: Fix 16-bit ASID configuration")
    280b683ceace ("iommu/arm-smmu: Simplify ASID/VMID handling")
    32b124492bdf ("iommu/io-pgtable-arm: Convert to IOMMU API TLB sync")
    3677a649a751 ("iommu/arm-smmu: Fix for ThunderX erratum #27704")
    44f6876a00e8 ("iommu/arm-smmu: Support non-strict mode")
    452107c79035 ("iommu/arm-smmu: Tidy up context bank indexing")
    523d7423e21b ("iommu/arm-smmu: Remove io-pgtable spinlock")
    58188afeb727 ("iommu/arm-smmu-v3: Remove io-pgtable spinlock")
    61bc671179f1 ("iommu/arm-smmu: Install bypass S2CRs for IOMMU_DOMAIN_IDENTITY domains")
    781ca2de89ba ("iommu: Add gfp parameter to iommu_ops::map")
    bdf95923086f ("iommu/arm-smmu: Return IOVA in iova_to_phys when SMMU is bypassed")
    d4a44f0750bb ("iommu/arm-smmu: Invoke pm_runtime across the driver")

v4.4.232: Failed to apply! Possible dependencies:
    267b62a96951 ("clk: tegra: pll: Update PLLM handling")
    287980e49ffc ("remove lots of IS_ERR_VALUE abuses")
    407254da291c ("clk: tegra: pll: Add logic for out-of-table rates for T210")
    56fd27b31f1a ("clk: tegra: pll: Change misc_reg count from 3 to 6")
    58188afeb727 ("iommu/arm-smmu-v3: Remove io-pgtable spinlock")
    6583a6309e83 ("clk: tegra: pll: Add tegra_pll_wait_for_lock to clk header")
    6929715cf6b9 ("clk: tegra: pll: Add support for PLLMB for Tegra210")
    6b301a059eb2 ("clk: tegra: Add support for Tegra210 clocks")
    781ca2de89ba ("iommu: Add gfp parameter to iommu_ops::map")
    7db864c9deb2 ("clk: tegra: pll: Simplify clk_enable_path")
    8cfb0cdf07e2 ("ACPI / debugger: Add IO interface to access debugger functionalities")
    8f78515425da ("iommu/arm-smmu: Implement of_xlate() for SMMUv3")
    9adb95949a34 ("iommu/arm-smmu: Support DMA-API domains")
    bc7f2ce0a7b5 ("iommu/arm-smmu: Don't fail device attach if already attached to a domain")
    bdf95923086f ("iommu/arm-smmu: Return IOVA in iova_to_phys when SMMU is bypassed")
    d907f4b4a178 ("clk: tegra: pll: Add logic for handling SDM data")
    f8d31489629c ("ACPICA: Debugger: Convert some mechanisms to OSPM specific")


NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

-- 
Thanks
Sasha
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* Re: [PATCH RESEND v3] iommu/tegra-smmu: Add missing locks around mapping operations
  2020-08-14 16:22 [PATCH RESEND v3] iommu/tegra-smmu: Add missing locks around mapping operations Dmitry Osipenko
  2020-08-26 13:54 ` Sasha Levin
@ 2020-09-04  9:05 ` Joerg Roedel
  2020-09-04  9:19   ` Dmitry Osipenko
  2020-09-04 12:20   ` Thierry Reding
  1 sibling, 2 replies; 6+ messages in thread
From: Joerg Roedel @ 2020-09-04  9:05 UTC (permalink / raw)
  To: Dmitry Osipenko
  Cc: linux-tegra, iommu, Thierry Reding, linux-kernel, Jonathan Hunter

On Fri, Aug 14, 2020 at 07:22:52PM +0300, Dmitry Osipenko wrote:
> The mapping operations of the Tegra SMMU driver are subjected to a race
> condition issues because SMMU Address Space isn't allocated and freed
> atomically, while it should be. This patch makes the mapping operations
> atomic, it fixes an accidentally released Host1x Address Space problem
> which happens while running multiple graphics tests in parallel on
> Tegra30, i.e. by having multiple threads racing with each other in the
> Host1x's submission and completion code paths, performing IOVA mappings
> and unmappings in parallel.
> 
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Dmitry Osipenko <digetx@gmail.com>

Thierry, does this change look good to you?

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* Re: [PATCH RESEND v3] iommu/tegra-smmu: Add missing locks around mapping operations
  2020-09-04  9:05 ` Joerg Roedel
@ 2020-09-04  9:19   ` Dmitry Osipenko
  2020-09-04  9:25     ` Dmitry Osipenko
  2020-09-04 12:20   ` Thierry Reding
  1 sibling, 1 reply; 6+ messages in thread
From: Dmitry Osipenko @ 2020-09-04  9:19 UTC (permalink / raw)
  To: Joerg Roedel
  Cc: linux-tegra, iommu, Thierry Reding, linux-kernel, Jonathan Hunter

04.09.2020 12:05, Joerg Roedel пишет:
> On Fri, Aug 14, 2020 at 07:22:52PM +0300, Dmitry Osipenko wrote:
>> The mapping operations of the Tegra SMMU driver are subjected to a race
>> condition issues because SMMU Address Space isn't allocated and freed
>> atomically, while it should be. This patch makes the mapping operations
>> atomic, it fixes an accidentally released Host1x Address Space problem
>> which happens while running multiple graphics tests in parallel on
>> Tegra30, i.e. by having multiple threads racing with each other in the
>> Host1x's submission and completion code paths, performing IOVA mappings
>> and unmappings in parallel.
>>
>> Cc: <stable@vger.kernel.org>
>> Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
> 
> Thierry, does this change look good to you?
> 

Hello Joerg and Thierry,

Please take into account that there is a v5 now that I sent out a day
ago, it's more optimized version and supports both atomic and non-atomic
GFP flags for the mapping operation.
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* Re: [PATCH RESEND v3] iommu/tegra-smmu: Add missing locks around mapping operations
  2020-09-04  9:19   ` Dmitry Osipenko
@ 2020-09-04  9:25     ` Dmitry Osipenko
  0 siblings, 0 replies; 6+ messages in thread
From: Dmitry Osipenko @ 2020-09-04  9:25 UTC (permalink / raw)
  To: Joerg Roedel
  Cc: linux-tegra, iommu, Thierry Reding, linux-kernel, Jonathan Hunter

04.09.2020 12:19, Dmitry Osipenko пишет:
> 04.09.2020 12:05, Joerg Roedel пишет:
>> On Fri, Aug 14, 2020 at 07:22:52PM +0300, Dmitry Osipenko wrote:
>>> The mapping operations of the Tegra SMMU driver are subjected to a race
>>> condition issues because SMMU Address Space isn't allocated and freed
>>> atomically, while it should be. This patch makes the mapping operations
>>> atomic, it fixes an accidentally released Host1x Address Space problem
>>> which happens while running multiple graphics tests in parallel on
>>> Tegra30, i.e. by having multiple threads racing with each other in the
>>> Host1x's submission and completion code paths, performing IOVA mappings
>>> and unmappings in parallel.
>>>
>>> Cc: <stable@vger.kernel.org>
>>> Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
>>
>> Thierry, does this change look good to you?
>>
> 
> Hello Joerg and Thierry,
> 
> Please take into account that there is a v5 now that I sent out a day
> ago, it's more optimized version and supports both atomic and non-atomic
> GFP flags for the mapping operation.
> 

https://patchwork.ozlabs.org/project/linux-tegra/patch/20200901203730.27865-1-digetx@gmail.com/
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* Re: [PATCH RESEND v3] iommu/tegra-smmu: Add missing locks around mapping operations
  2020-09-04  9:05 ` Joerg Roedel
  2020-09-04  9:19   ` Dmitry Osipenko
@ 2020-09-04 12:20   ` Thierry Reding
  1 sibling, 0 replies; 6+ messages in thread
From: Thierry Reding @ 2020-09-04 12:20 UTC (permalink / raw)
  To: Joerg Roedel
  Cc: linux-tegra, linux-kernel, Dmitry Osipenko, iommu, Jonathan Hunter


[-- Attachment #1.1: Type: text/plain, Size: 968 bytes --]

On Fri, Sep 04, 2020 at 11:05:19AM +0200, Joerg Roedel wrote:
> On Fri, Aug 14, 2020 at 07:22:52PM +0300, Dmitry Osipenko wrote:
> > The mapping operations of the Tegra SMMU driver are subjected to a race
> > condition issues because SMMU Address Space isn't allocated and freed
> > atomically, while it should be. This patch makes the mapping operations
> > atomic, it fixes an accidentally released Host1x Address Space problem
> > which happens while running multiple graphics tests in parallel on
> > Tegra30, i.e. by having multiple threads racing with each other in the
> > Host1x's submission and completion code paths, performing IOVA mappings
> > and unmappings in parallel.
> > 
> > Cc: <stable@vger.kernel.org>
> > Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
> 
> Thierry, does this change look good to you?

As Dmitry said, there's a new patch for this which is better. I've
replied with an Acked-by and Tested-by to v5.

Thierry

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

[-- Attachment #2: Type: text/plain, Size: 156 bytes --]

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

end of thread, other threads:[~2020-09-04 12:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-14 16:22 [PATCH RESEND v3] iommu/tegra-smmu: Add missing locks around mapping operations Dmitry Osipenko
2020-08-26 13:54 ` Sasha Levin
2020-09-04  9:05 ` Joerg Roedel
2020-09-04  9:19   ` Dmitry Osipenko
2020-09-04  9:25     ` Dmitry Osipenko
2020-09-04 12:20   ` Thierry Reding

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).