iommu.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/7] iommu/amd: Disable previously enabled IOMMUs at boot
@ 2017-06-16 14:09 Joerg Roedel
  2017-06-16 14:09 ` [PATCH 1/7] iommu/amd: Disable IOMMUs at boot if they are enabled Joerg Roedel
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Joerg Roedel @ 2017-06-16 14:09 UTC (permalink / raw)
  To: iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA
  Cc: linux-kernel-u79uwXL29TY76Z2rM5mHXA

Hi,

here is a patch-set refactoring the AMD IOMMU init code a
bit to make sure the IOMMUs get disabled at boot, even when
amd_iommu=off is specified on the kernel command line.

The patch-set is tested with various combinations of kernel
command-line parameters and with kexec. It applies to the
x86/amd branch of the iommu-tree.

Regards,

	Joerg

Joerg Roedel (7):
  iommu/amd: Disable IOMMUs at boot if they are enabled
  iommu/amd: Rename free_on_init_error()
  iommu/amd: Add new init-state IOMMU_CMDLINE_DISABLED
  iommu/amd: Check for error states first in iommu_go_to_state()
  iommu/amd: Set global pointers to NULL after freeing them
  iommu/amd: Free IOMMU resources when disabled on command line
  iommu/amd: Remove amd_iommu_disabled check from amd_iommu_detect()

 drivers/iommu/amd_iommu_init.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

-- 
2.7.4

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

* [PATCH 1/7] iommu/amd: Disable IOMMUs at boot if they are enabled
  2017-06-16 14:09 [PATCH 0/7] iommu/amd: Disable previously enabled IOMMUs at boot Joerg Roedel
@ 2017-06-16 14:09 ` Joerg Roedel
       [not found] ` <1497622200-18188-1-git-send-email-joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
  2017-06-16 14:10 ` [PATCH 7/7] iommu/amd: Remove amd_iommu_disabled check from amd_iommu_detect() Joerg Roedel
  2 siblings, 0 replies; 8+ messages in thread
From: Joerg Roedel @ 2017-06-16 14:09 UTC (permalink / raw)
  To: iommu; +Cc: linux-kernel, Joerg Roedel

From: Joerg Roedel <jroedel@suse.de>

When booting, make sure the IOMMUs are disabled. They could
be previously enabled if we boot into a kexec or kdump
kernel. So make sure they are off.

Signed-off-by: Joerg Roedel <jroedel@suse.de>
---
 drivers/iommu/amd_iommu_init.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/iommu/amd_iommu_init.c b/drivers/iommu/amd_iommu_init.c
index d9f5ddd..fb7035f 100644
--- a/drivers/iommu/amd_iommu_init.c
+++ b/drivers/iommu/amd_iommu_init.c
@@ -2316,6 +2316,9 @@ static int __init early_amd_iommu_init(void)
 	if (ret)
 		goto out;
 
+	/* Disable any previously enabled IOMMUs */
+	disable_iommus();
+
 	if (amd_iommu_irq_remap)
 		amd_iommu_irq_remap = check_ioapic_information();
 
-- 
2.7.4

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

* [PATCH 2/7] iommu/amd: Rename free_on_init_error()
       [not found] ` <1497622200-18188-1-git-send-email-joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
@ 2017-06-16 14:09   ` Joerg Roedel
  2017-06-16 14:09   ` [PATCH 3/7] iommu/amd: Add new init-state IOMMU_CMDLINE_DISABLED Joerg Roedel
                     ` (3 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Joerg Roedel @ 2017-06-16 14:09 UTC (permalink / raw)
  To: iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA
  Cc: Joerg Roedel, linux-kernel-u79uwXL29TY76Z2rM5mHXA

From: Joerg Roedel <jroedel-l3A5Bk7waGM@public.gmane.org>

The function will also be used to free iommu resources when
amd_iommu=off was specified on the kernel command line. So
rename the function to reflect that.

Signed-off-by: Joerg Roedel <jroedel-l3A5Bk7waGM@public.gmane.org>
---
 drivers/iommu/amd_iommu_init.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/amd_iommu_init.c b/drivers/iommu/amd_iommu_init.c
index fb7035f..28bf39c 100644
--- a/drivers/iommu/amd_iommu_init.c
+++ b/drivers/iommu/amd_iommu_init.c
@@ -2106,7 +2106,7 @@ static struct syscore_ops amd_iommu_syscore_ops = {
 	.resume = amd_iommu_resume,
 };
 
-static void __init free_on_init_error(void)
+static void __init free_iommu_resources(void)
 {
 	kmemleak_free(irq_lookup_table);
 	free_pages((unsigned long)irq_lookup_table,
@@ -2534,7 +2534,7 @@ static int __init amd_iommu_init(void)
 		free_dma_resources();
 		if (!irq_remapping_enabled) {
 			disable_iommus();
-			free_on_init_error();
+			free_iommu_resources();
 		} else {
 			struct amd_iommu *iommu;
 
-- 
2.7.4

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

* [PATCH 3/7] iommu/amd: Add new init-state IOMMU_CMDLINE_DISABLED
       [not found] ` <1497622200-18188-1-git-send-email-joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
  2017-06-16 14:09   ` [PATCH 2/7] iommu/amd: Rename free_on_init_error() Joerg Roedel
@ 2017-06-16 14:09   ` Joerg Roedel
  2017-06-16 14:09   ` [PATCH 4/7] iommu/amd: Check for error states first in iommu_go_to_state() Joerg Roedel
                     ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Joerg Roedel @ 2017-06-16 14:09 UTC (permalink / raw)
  To: iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA
  Cc: Joerg Roedel, linux-kernel-u79uwXL29TY76Z2rM5mHXA

From: Joerg Roedel <jroedel-l3A5Bk7waGM@public.gmane.org>

This will be used when during initialization we detect that
the iommu should be disabled.

Signed-off-by: Joerg Roedel <jroedel-l3A5Bk7waGM@public.gmane.org>
---
 drivers/iommu/amd_iommu_init.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/amd_iommu_init.c b/drivers/iommu/amd_iommu_init.c
index 28bf39c..c40adc8 100644
--- a/drivers/iommu/amd_iommu_init.c
+++ b/drivers/iommu/amd_iommu_init.c
@@ -237,6 +237,7 @@ enum iommu_init_state {
 	IOMMU_INITIALIZED,
 	IOMMU_NOT_FOUND,
 	IOMMU_INIT_ERROR,
+	IOMMU_CMDLINE_DISABLED,
 };
 
 /* Early ioapic and hpet maps from kernel command line */
@@ -2450,6 +2451,7 @@ static int __init state_next(void)
 		break;
 	case IOMMU_NOT_FOUND:
 	case IOMMU_INIT_ERROR:
+	case IOMMU_CMDLINE_DISABLED:
 		/* Error states => do nothing */
 		ret = -EINVAL;
 		break;
@@ -2467,8 +2469,9 @@ static int __init iommu_go_to_state(enum iommu_init_state state)
 
 	while (init_state != state) {
 		ret = state_next();
-		if (init_state == IOMMU_NOT_FOUND ||
-		    init_state == IOMMU_INIT_ERROR)
+		if (init_state == IOMMU_NOT_FOUND         ||
+		    init_state == IOMMU_INIT_ERROR        ||
+		    init_state == IOMMU_CMDLINE_DISABLED)
 			break;
 	}
 
-- 
2.7.4

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

* [PATCH 4/7] iommu/amd: Check for error states first in iommu_go_to_state()
       [not found] ` <1497622200-18188-1-git-send-email-joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
  2017-06-16 14:09   ` [PATCH 2/7] iommu/amd: Rename free_on_init_error() Joerg Roedel
  2017-06-16 14:09   ` [PATCH 3/7] iommu/amd: Add new init-state IOMMU_CMDLINE_DISABLED Joerg Roedel
@ 2017-06-16 14:09   ` Joerg Roedel
  2017-06-16 14:09   ` [PATCH 5/7] iommu/amd: Set global pointers to NULL after freeing them Joerg Roedel
  2017-06-16 14:09   ` [PATCH 6/7] iommu/amd: Free IOMMU resources when disabled on command line Joerg Roedel
  4 siblings, 0 replies; 8+ messages in thread
From: Joerg Roedel @ 2017-06-16 14:09 UTC (permalink / raw)
  To: iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA
  Cc: Joerg Roedel, linux-kernel-u79uwXL29TY76Z2rM5mHXA

From: Joerg Roedel <jroedel-l3A5Bk7waGM@public.gmane.org>

Check if we are in an error state already before calling
into state_next().

Signed-off-by: Joerg Roedel <jroedel-l3A5Bk7waGM@public.gmane.org>
---
 drivers/iommu/amd_iommu_init.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/amd_iommu_init.c b/drivers/iommu/amd_iommu_init.c
index c40adc8..a31f375 100644
--- a/drivers/iommu/amd_iommu_init.c
+++ b/drivers/iommu/amd_iommu_init.c
@@ -2465,14 +2465,14 @@ static int __init state_next(void)
 
 static int __init iommu_go_to_state(enum iommu_init_state state)
 {
-	int ret = 0;
+	int ret = -EINVAL;
 
 	while (init_state != state) {
-		ret = state_next();
 		if (init_state == IOMMU_NOT_FOUND         ||
 		    init_state == IOMMU_INIT_ERROR        ||
 		    init_state == IOMMU_CMDLINE_DISABLED)
 			break;
+		ret = state_next();
 	}
 
 	return ret;
-- 
2.7.4

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

* [PATCH 5/7] iommu/amd: Set global pointers to NULL after freeing them
       [not found] ` <1497622200-18188-1-git-send-email-joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
                     ` (2 preceding siblings ...)
  2017-06-16 14:09   ` [PATCH 4/7] iommu/amd: Check for error states first in iommu_go_to_state() Joerg Roedel
@ 2017-06-16 14:09   ` Joerg Roedel
  2017-06-16 14:09   ` [PATCH 6/7] iommu/amd: Free IOMMU resources when disabled on command line Joerg Roedel
  4 siblings, 0 replies; 8+ messages in thread
From: Joerg Roedel @ 2017-06-16 14:09 UTC (permalink / raw)
  To: iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA
  Cc: Joerg Roedel, linux-kernel-u79uwXL29TY76Z2rM5mHXA

From: Joerg Roedel <jroedel-l3A5Bk7waGM@public.gmane.org>

Avoid any tries to double-free these pointers.

Signed-off-by: Joerg Roedel <jroedel-l3A5Bk7waGM@public.gmane.org>
---
 drivers/iommu/amd_iommu_init.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/iommu/amd_iommu_init.c b/drivers/iommu/amd_iommu_init.c
index a31f375..d04898c 100644
--- a/drivers/iommu/amd_iommu_init.c
+++ b/drivers/iommu/amd_iommu_init.c
@@ -2112,18 +2112,22 @@ static void __init free_iommu_resources(void)
 	kmemleak_free(irq_lookup_table);
 	free_pages((unsigned long)irq_lookup_table,
 		   get_order(rlookup_table_size));
+	irq_lookup_table = NULL;
 
 	kmem_cache_destroy(amd_iommu_irq_cache);
 	amd_iommu_irq_cache = NULL;
 
 	free_pages((unsigned long)amd_iommu_rlookup_table,
 		   get_order(rlookup_table_size));
+	amd_iommu_rlookup_table = NULL;
 
 	free_pages((unsigned long)amd_iommu_alias_table,
 		   get_order(alias_table_size));
+	amd_iommu_alias_table = NULL;
 
 	free_pages((unsigned long)amd_iommu_dev_table,
 		   get_order(dev_table_size));
+	amd_iommu_dev_table = NULL;
 
 	free_iommu_all();
 
@@ -2193,6 +2197,7 @@ static void __init free_dma_resources(void)
 {
 	free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
 		   get_order(MAX_DOMAIN_ID/8));
+	amd_iommu_pd_alloc_bitmap = NULL;
 
 	free_unity_maps();
 }
-- 
2.7.4

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

* [PATCH 6/7] iommu/amd: Free IOMMU resources when disabled on command line
       [not found] ` <1497622200-18188-1-git-send-email-joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
                     ` (3 preceding siblings ...)
  2017-06-16 14:09   ` [PATCH 5/7] iommu/amd: Set global pointers to NULL after freeing them Joerg Roedel
@ 2017-06-16 14:09   ` Joerg Roedel
  4 siblings, 0 replies; 8+ messages in thread
From: Joerg Roedel @ 2017-06-16 14:09 UTC (permalink / raw)
  To: iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA
  Cc: Joerg Roedel, linux-kernel-u79uwXL29TY76Z2rM5mHXA

From: Joerg Roedel <jroedel-l3A5Bk7waGM@public.gmane.org>

After we made sure that all IOMMUs have been disabled we
need to make sure that all resources we allocated are
released again.

Signed-off-by: Joerg Roedel <jroedel-l3A5Bk7waGM@public.gmane.org>
---
 drivers/iommu/amd_iommu_init.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/iommu/amd_iommu_init.c b/drivers/iommu/amd_iommu_init.c
index d04898c..eedd3d3 100644
--- a/drivers/iommu/amd_iommu_init.c
+++ b/drivers/iommu/amd_iommu_init.c
@@ -2428,6 +2428,13 @@ static int __init state_next(void)
 	case IOMMU_IVRS_DETECTED:
 		ret = early_amd_iommu_init();
 		init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
+		if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
+			pr_info("AMD-Vi: AMD IOMMU disabled on kernel command-line\n");
+			free_dma_resources();
+			free_iommu_resources();
+			init_state = IOMMU_CMDLINE_DISABLED;
+			ret = -EINVAL;
+		}
 		break;
 	case IOMMU_ACPI_FINISHED:
 		early_enable_iommus();
-- 
2.7.4

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

* [PATCH 7/7] iommu/amd: Remove amd_iommu_disabled check from amd_iommu_detect()
  2017-06-16 14:09 [PATCH 0/7] iommu/amd: Disable previously enabled IOMMUs at boot Joerg Roedel
  2017-06-16 14:09 ` [PATCH 1/7] iommu/amd: Disable IOMMUs at boot if they are enabled Joerg Roedel
       [not found] ` <1497622200-18188-1-git-send-email-joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
@ 2017-06-16 14:10 ` Joerg Roedel
  2 siblings, 0 replies; 8+ messages in thread
From: Joerg Roedel @ 2017-06-16 14:10 UTC (permalink / raw)
  To: iommu; +Cc: linux-kernel, Joerg Roedel

From: Joerg Roedel <jroedel@suse.de>

This check needs to happens later now, when all previously
enabled IOMMUs have been disabled.

Signed-off-by: Joerg Roedel <jroedel@suse.de>
---
 drivers/iommu/amd_iommu_init.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/drivers/iommu/amd_iommu_init.c b/drivers/iommu/amd_iommu_init.c
index eedd3d3..bc9dfcf 100644
--- a/drivers/iommu/amd_iommu_init.c
+++ b/drivers/iommu/amd_iommu_init.c
@@ -2576,9 +2576,6 @@ int __init amd_iommu_detect(void)
 	if (no_iommu || (iommu_detected && !gart_iommu_aperture))
 		return -ENODEV;
 
-	if (amd_iommu_disabled)
-		return -ENODEV;
-
 	ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
 	if (ret)
 		return ret;
-- 
2.7.4

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

end of thread, other threads:[~2017-06-16 14:10 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-16 14:09 [PATCH 0/7] iommu/amd: Disable previously enabled IOMMUs at boot Joerg Roedel
2017-06-16 14:09 ` [PATCH 1/7] iommu/amd: Disable IOMMUs at boot if they are enabled Joerg Roedel
     [not found] ` <1497622200-18188-1-git-send-email-joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
2017-06-16 14:09   ` [PATCH 2/7] iommu/amd: Rename free_on_init_error() Joerg Roedel
2017-06-16 14:09   ` [PATCH 3/7] iommu/amd: Add new init-state IOMMU_CMDLINE_DISABLED Joerg Roedel
2017-06-16 14:09   ` [PATCH 4/7] iommu/amd: Check for error states first in iommu_go_to_state() Joerg Roedel
2017-06-16 14:09   ` [PATCH 5/7] iommu/amd: Set global pointers to NULL after freeing them Joerg Roedel
2017-06-16 14:09   ` [PATCH 6/7] iommu/amd: Free IOMMU resources when disabled on command line Joerg Roedel
2017-06-16 14:10 ` [PATCH 7/7] iommu/amd: Remove amd_iommu_disabled check from amd_iommu_detect() Joerg Roedel

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