2021-06-08 13:25:33

by John Garry

[permalink] [raw]
Subject: [PATCH v11 0/3] Enhance IOMMU default DMA mode build options

This is a reboot of Zhen Lei's series from a couple of years ago, which
never made it across the line.

I still think that it has some value, so taking up the mantle.

Motivation:
Allow lazy mode be default mode for DMA domains for all ARCHs, and not
only those who hardcode it (to be lazy). For ARM64, currently we must use
a kernel command line parameter to use lazy mode, which is less than
ideal.

Differences to v10:
- Rebase to v5.13-rc4
- Correct comment and typo in Kconfig (Randy)
- Make Kconfig choice depend on relevant architectures

Differences to v9:
https://lore.kernel.org/linux-iommu/[email protected]/#t
- Rebase to v5.13-rc2
- Remove CONFIG_IOMMU_DEFAULT_PASSTHROUGH from choice:
Since we can dynamically change default domain of group, lazy or strict and
passthrough are not mutually exclusive
- Drop ia64 patch, which I don't think was ever required
- Drop "x86/dma: use IS_ENABLED() to simplify the code", which is no
longer required
- Drop s390/pci patch, as this arch does not use CONFIG_IOMMU_API or even
already honour CONFIG_IOMMU_DEFAULT_PASSTHROUGH
https://lore.kernel.org/linux-iommu/[email protected]/
- Drop powernv/iommu patch, as I no longer think that it is relevant
https://lore.kernel.org/linux-iommu/[email protected]/
- Some tidying

Zhen Lei (3):
iommu: Enhance IOMMU default DMA mode build options
iommu/vt-d: Add support for IOMMU default DMA mode build options
iommu/amd: Add support for IOMMU default DMA mode build options

drivers/iommu/Kconfig | 36 ++++++++++++++++++++++++++++++++++++
drivers/iommu/amd/init.c | 3 ++-
drivers/iommu/intel/iommu.c | 2 +-
drivers/iommu/iommu.c | 3 ++-
4 files changed, 41 insertions(+), 3 deletions(-)

--
2.26.2


2021-06-08 13:27:48

by John Garry

[permalink] [raw]
Subject: [PATCH v11 1/3] iommu: Enhance IOMMU default DMA mode build options

From: Zhen Lei <[email protected]>

First, add build options IOMMU_DEFAULT_{LAZY|STRICT}, so that we have the
opportunity to set {lazy|strict} mode as default at build time. Then put
the two config options in a choice, as they are mutually exclusive.

[jpg: Make choice between strict and lazy only (and not passthrough)]
Signed-off-by: Zhen Lei <[email protected]>
Signed-off-by: John Garry <[email protected]>
---
drivers/iommu/Kconfig | 35 +++++++++++++++++++++++++++++++++++
drivers/iommu/iommu.c | 3 ++-
2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
index 1f111b399bca..369a3af9e5bf 100644
--- a/drivers/iommu/Kconfig
+++ b/drivers/iommu/Kconfig
@@ -90,6 +90,41 @@ config IOMMU_DEFAULT_PASSTHROUGH

If unsure, say N here.

+choice
+ prompt "IOMMU default DMA mode"
+ depends on IOMMU_API
+ depends on X86 || IA64 || X86_64 || ARM || ARM64
+
+ default IOMMU_DEFAULT_STRICT
+ help
+ This option allows an IOMMU DMA mode to be chosen at build time, to
+ override the default DMA mode of each ARCH, removing the need to
+ pass in kernel parameters through command line. It is still possible
+ to provide ARCH-specific or common boot options to override this
+ option.
+
+ If unsure, keep the default.
+
+config IOMMU_DEFAULT_LAZY
+ bool "lazy"
+ help
+ Support lazy mode, where for every IOMMU DMA unmap operation, the
+ flush operation of IOTLB and the free operation of IOVA are deferred.
+ They are only guaranteed to be done before the related IOVA will be
+ reused.
+
+config IOMMU_DEFAULT_STRICT
+ bool "strict"
+ help
+ For every IOMMU DMA unmap operation, the flush operation of IOTLB and
+ the free operation of IOVA are guaranteed to be done in the unmap
+ function.
+
+ This mode is safer than lazy mode, but it may be slower in some high
+ performance scenarios.
+
+endchoice
+
config OF_IOMMU
def_bool y
depends on OF && IOMMU_API
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 966426a96520..177b0dafc535 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -29,7 +29,8 @@ static struct kset *iommu_group_kset;
static DEFINE_IDA(iommu_group_ida);

static unsigned int iommu_def_domain_type __read_mostly;
-static bool iommu_dma_strict __read_mostly = true;
+static bool iommu_dma_strict __read_mostly =
+ IS_ENABLED(CONFIG_IOMMU_DEFAULT_STRICT);
static u32 iommu_cmd_line __read_mostly;

struct iommu_group {
--
2.26.2

2021-06-09 00:31:23

by John Garry

[permalink] [raw]
Subject: [PATCH v11 2/3] iommu/vt-d: Add support for IOMMU default DMA mode build options

From: Zhen Lei <[email protected]>

Make IOMMU_DEFAULT_LAZY default for when INTEL_IOMMU config is set,
and make intel_iommu_strict initially hold value of
IS_ENABLED(CONFIG_IOMMU_DEFAULT_STRICT).

Signed-off-by: Zhen Lei <[email protected]>
Signed-off-by: John Garry <[email protected]>
---
drivers/iommu/Kconfig | 1 +
drivers/iommu/intel/iommu.c | 2 +-
2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
index 369a3af9e5bf..b68ec7ed23c0 100644
--- a/drivers/iommu/Kconfig
+++ b/drivers/iommu/Kconfig
@@ -95,6 +95,7 @@ choice
depends on IOMMU_API
depends on X86 || IA64 || X86_64 || ARM || ARM64

+ default IOMMU_DEFAULT_LAZY if INTEL_IOMMU
default IOMMU_DEFAULT_STRICT
help
This option allows an IOMMU DMA mode to be chosen at build time, to
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index be35284a2016..fe33347f4a1e 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -360,7 +360,7 @@ int intel_iommu_enabled = 0;
EXPORT_SYMBOL_GPL(intel_iommu_enabled);

static int dmar_map_gfx = 1;
-static int intel_iommu_strict;
+static int intel_iommu_strict = IS_ENABLED(CONFIG_IOMMU_DEFAULT_STRICT);
static int intel_iommu_superpage = 1;
static int iommu_identity_mapping;
static int iommu_skip_te_disable;
--
2.26.2

2021-06-09 00:31:38

by John Garry

[permalink] [raw]
Subject: [PATCH v11 3/3] iommu/amd: Add support for IOMMU default DMA mode build options

From: Zhen Lei <[email protected]>

The default DMA mode lazy, but allow this to be set to strict mode at
build time. It may still be overridden by the relevant boot option.

Also make IOMMU_DEFAULT_LAZY default for when AMD_IOMMU config is set.

[jpg: Rebase for relocated file]
Signed-off-by: Zhen Lei <[email protected]>
Signed-off-by: John Garry <[email protected]>
---
drivers/iommu/Kconfig | 2 +-
drivers/iommu/amd/init.c | 3 ++-
2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
index b68ec7ed23c0..6d99150050b9 100644
--- a/drivers/iommu/Kconfig
+++ b/drivers/iommu/Kconfig
@@ -95,7 +95,7 @@ choice
depends on IOMMU_API
depends on X86 || IA64 || X86_64 || ARM || ARM64

- default IOMMU_DEFAULT_LAZY if INTEL_IOMMU
+ default IOMMU_DEFAULT_LAZY if (AMD_IOMMU || INTEL_IOMMU)
default IOMMU_DEFAULT_STRICT
help
This option allows an IOMMU DMA mode to be chosen at build time, to
diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c
index d006724f4dc2..af662fc37cbe 100644
--- a/drivers/iommu/amd/init.c
+++ b/drivers/iommu/amd/init.c
@@ -160,7 +160,8 @@ u16 amd_iommu_last_bdf; /* largest PCI device id we have
to handle */
LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
we find in ACPI */
-bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
+/* if true, flush on every unmap */
+bool amd_iommu_unmap_flush = IS_ENABLED(CONFIG_IOMMU_DEFAULT_STRICT);

LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
system */
--
2.26.2

2021-06-09 16:23:05

by John Garry

[permalink] [raw]
Subject: Re: [PATCH v11 1/3] iommu: Enhance IOMMU default DMA mode build options

On 09/06/2021 16:03, Robin Murphy wrote:
> On 2021-06-08 14:18, John Garry wrote:
>> From: Zhen Lei <[email protected]>
>>
>> First, add build options IOMMU_DEFAULT_{LAZY|STRICT}, so that we have the
>> opportunity to set {lazy|strict} mode as default at build time. Then put
>> the two config options in a choice, as they are mutually exclusive.
>>
>> [jpg: Make choice between strict and lazy only (and not passthrough)]
>> Signed-off-by: Zhen Lei <[email protected]>
>> Signed-off-by: John Garry <[email protected]>
>> ---
>>   drivers/iommu/Kconfig | 35 +++++++++++++++++++++++++++++++++++
>>   drivers/iommu/iommu.c |  3 ++-
>>   2 files changed, 37 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
>> index 1f111b399bca..369a3af9e5bf 100644
>> --- a/drivers/iommu/Kconfig
>> +++ b/drivers/iommu/Kconfig
>> @@ -90,6 +90,41 @@ config IOMMU_DEFAULT_PASSTHROUGH
>>         If unsure, say N here.
>> +choice
>> +    prompt "IOMMU default DMA mode"
>> +    depends on IOMMU_API
>> +    depends on X86 || IA64 || X86_64 || ARM || ARM64
>
> Simply "depends on IOMMU_DMA" should suffice, since that's now the only
> place where flush queues matter.

I suppose so.

Configs ARM64, AMD_IOMMU, and INTEL_IOMMU all select this.

>
>> +
>> +    default IOMMU_DEFAULT_STRICT
>> +    help
>> +      This option allows an IOMMU DMA mode to be chosen at build
>> time, to
>> +      override the default DMA mode of each ARCH, removing the need to
>> +      pass in kernel parameters through command line. It is still
>> possible
>> +      to provide ARCH-specific or common boot options to override this
>> +      option.
>> +
>> +      If unsure, keep the default.
>> +
>> +config IOMMU_DEFAULT_LAZY
>> +    bool "lazy"
>> +    help
>> +      Support lazy mode, where for every IOMMU DMA unmap operation, the
>> +      flush operation of IOTLB and the free operation of IOVA are
>> deferred.
>> +      They are only guaranteed to be done before the related IOVA
>> will be
>> +      reused.
>> +
>> +config IOMMU_DEFAULT_STRICT
>> +    bool "strict"
>> +    help
>> +      For every IOMMU DMA unmap operation, the flush operation of
>> IOTLB and
>> +      the free operation of IOVA are guaranteed to be done in the unmap
>> +      function.
>> +
>> +      This mode is safer than lazy mode, but it may be slower in some
>> high
>> +      performance scenarios.
>
> FWIW, as an end user who doesn't care much about the implementation
> details I'd probably appreciate the actual implications being clearer,
> i.e. what does "safer" mean in practice and what is it relative to?
>

Fine, I can mention that lazy mode means that we have reduced device
isolation and a dangerous window can be created between device driver
DMA unmap and zapping the mapping in the IOMMU; however still much safer
than passthrough/no IOMMU, which means no isolation at all.

Thanks,
John

2021-06-09 17:12:25

by Joerg Roedel

[permalink] [raw]
Subject: Re: [PATCH v11 0/3] Enhance IOMMU default DMA mode build options

Hi John,

On Tue, Jun 08, 2021 at 09:18:25PM +0800, John Garry wrote:
> Zhen Lei (3):
> iommu: Enhance IOMMU default DMA mode build options
> iommu/vt-d: Add support for IOMMU default DMA mode build options
> iommu/amd: Add support for IOMMU default DMA mode build options

So I like the idea, but can we go a step further and get (mostly) rid of
the driver-specific setup code for lazy/non-lazy mode? This can happen
in the dma-iommu code and the drivers only need to keep the support for
their legacy command line options.

Regards,

Joerg

2021-06-09 17:58:36

by Robin Murphy

[permalink] [raw]
Subject: Re: [PATCH v11 1/3] iommu: Enhance IOMMU default DMA mode build options

On 2021-06-08 14:18, John Garry wrote:
> From: Zhen Lei <[email protected]>
>
> First, add build options IOMMU_DEFAULT_{LAZY|STRICT}, so that we have the
> opportunity to set {lazy|strict} mode as default at build time. Then put
> the two config options in a choice, as they are mutually exclusive.
>
> [jpg: Make choice between strict and lazy only (and not passthrough)]
> Signed-off-by: Zhen Lei <[email protected]>
> Signed-off-by: John Garry <[email protected]>
> ---
> drivers/iommu/Kconfig | 35 +++++++++++++++++++++++++++++++++++
> drivers/iommu/iommu.c | 3 ++-
> 2 files changed, 37 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
> index 1f111b399bca..369a3af9e5bf 100644
> --- a/drivers/iommu/Kconfig
> +++ b/drivers/iommu/Kconfig
> @@ -90,6 +90,41 @@ config IOMMU_DEFAULT_PASSTHROUGH
>
> If unsure, say N here.
>
> +choice
> + prompt "IOMMU default DMA mode"
> + depends on IOMMU_API
> + depends on X86 || IA64 || X86_64 || ARM || ARM64

Simply "depends on IOMMU_DMA" should suffice, since that's now the only
place where flush queues matter.

> +
> + default IOMMU_DEFAULT_STRICT
> + help
> + This option allows an IOMMU DMA mode to be chosen at build time, to
> + override the default DMA mode of each ARCH, removing the need to
> + pass in kernel parameters through command line. It is still possible
> + to provide ARCH-specific or common boot options to override this
> + option.
> +
> + If unsure, keep the default.
> +
> +config IOMMU_DEFAULT_LAZY
> + bool "lazy"
> + help
> + Support lazy mode, where for every IOMMU DMA unmap operation, the
> + flush operation of IOTLB and the free operation of IOVA are deferred.
> + They are only guaranteed to be done before the related IOVA will be
> + reused.
> +
> +config IOMMU_DEFAULT_STRICT
> + bool "strict"
> + help
> + For every IOMMU DMA unmap operation, the flush operation of IOTLB and
> + the free operation of IOVA are guaranteed to be done in the unmap
> + function.
> +
> + This mode is safer than lazy mode, but it may be slower in some high
> + performance scenarios.

FWIW, as an end user who doesn't care much about the implementation
details I'd probably appreciate the actual implications being clearer,
i.e. what does "safer" mean in practice and what is it relative to?

Robin.

> +
> +endchoice
> +
> config OF_IOMMU
> def_bool y
> depends on OF && IOMMU_API
> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
> index 966426a96520..177b0dafc535 100644
> --- a/drivers/iommu/iommu.c
> +++ b/drivers/iommu/iommu.c
> @@ -29,7 +29,8 @@ static struct kset *iommu_group_kset;
> static DEFINE_IDA(iommu_group_ida);
>
> static unsigned int iommu_def_domain_type __read_mostly;
> -static bool iommu_dma_strict __read_mostly = true;
> +static bool iommu_dma_strict __read_mostly =
> + IS_ENABLED(CONFIG_IOMMU_DEFAULT_STRICT);
> static u32 iommu_cmd_line __read_mostly;
>
> struct iommu_group {
>

2021-06-10 16:33:05

by John Garry

[permalink] [raw]
Subject: Re: [PATCH v11 0/3] Enhance IOMMU default DMA mode build options

On 09/06/2021 09:15, Joerg Roedel wrote:
> On Tue, Jun 08, 2021 at 09:18:25PM +0800, John Garry wrote:
>> Zhen Lei (3):
>> iommu: Enhance IOMMU default DMA mode build options
>> iommu/vt-d: Add support for IOMMU default DMA mode build options
>> iommu/amd: Add support for IOMMU default DMA mode build options
> So I like the idea, but can we go a step further and get (mostly) rid of
> the driver-specific setup code for lazy/non-lazy mode? This can happen
> in the dma-iommu code and the drivers only need to keep the support for
> their legacy command line options.

The AMD driver just maintains a flag and a print for the strict mode
setting.

The Intel driver still maintains some lazy vs strict config, depending
on the platform:
- DMAR caching mode set means that we enforce strict mode globally
- workaround for ironlake gpu means that we enforce strict mode globally

So there isn't much driver-specific setup remaining, and I can't see the
intel stuff being moved into dma-iommu.c or asbtracted (for that).

We could prob replace the driver-specific logs with new logs in iommu.c,
and do away with maintaining the proprietary driver strict mode flags.
That's if we're ok with replacing the driver-specific logs, though.

Thanks,
John