Skip to contentSkip to conversation

vfio/spapr_tce: Get rid of possible infinite loop

RFC PATCH Applied Alexey Kardashevskiy 6 participants 7 messages Oct 2, 2018 – Dec 23, 2018

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

RFC PATCH 1/1 vfio/spapr_tce: Get rid of possible infinite loop

As a part of cleanup, the SPAPR TCE IOMMU subdriver releases preregistered memory. If there is a bug in memory release, the loop in tce_iommu_release() becomes infinite; this actually happened to me.

This makes the loop finite and prints a warning on every failure to make the code more bug prone.

---
 drivers/vfio/vfio_iommu_spapr_tce.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/vfio/vfio_iommu_spapr_tce.c +3 -7
index b1a8ab3..ece0651 100644
@@ -371,6 +371,7 @@ static void tce_iommu_release(void *iommu_data)
 {
 	struct tce_container *container = iommu_data;
 	struct tce_iommu_group *tcegrp;
+	struct tce_iommu_prereg *tcemem, *tmtmp;
 	long i;
 
 	while (tce_groups_attached(container)) {
@@ -393,13 +394,8 @@ static void tce_iommu_release(void *iommu_data)
 		tce_iommu_free_table(container, tbl);
 	}
 
-	while (!list_empty(&container->prereg_list)) {
-		struct tce_iommu_prereg *tcemem;
-
-		tcemem = list_first_entry(&container->prereg_list,
-				struct tce_iommu_prereg, next);
-		WARN_ON_ONCE(tce_iommu_prereg_free(container, tcemem));
-	}
+	list_for_each_entry_safe(tcemem, tmtmp, &container->prereg_list, next)
+		WARN_ON(tce_iommu_prereg_free(container, tcemem));
 
 	tce_iommu_disable(container);
 	if (container->mm)
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Alexey Kardashevskiy

As a part of cleanup, the SPAPR TCE IOMMU subdriver releases preregistered memory. If there is a bug in memory release, the loop in tce_iommu_release() becomes infinite; this actually happened to me.

This makes the loop finite and prints a warning on every failure to make the code more bug prone.

It does improve the current behaviour. I do suspect, however, that leaving the failed regions in the list will probably cause another failure later on.

Quoted 32 lines
---
 drivers/vfio/vfio_iommu_spapr_tce.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/vfio/vfio_iommu_spapr_tce.c +3 -7
index b1a8ab3..ece0651 100644
@@ -371,6 +371,7 @@ static void tce_iommu_release(void *iommu_data)
 {
 	struct tce_container *container = iommu_data;
 	struct tce_iommu_group *tcegrp;
+	struct tce_iommu_prereg *tcemem, *tmtmp;
 	long i;
 
 	while (tce_groups_attached(container)) {
@@ -393,13 +394,8 @@ static void tce_iommu_release(void *iommu_data)
 		tce_iommu_free_table(container, tbl);
 	}
 
-	while (!list_empty(&container->prereg_list)) {
-		struct tce_iommu_prereg *tcemem;
-
-		tcemem = list_first_entry(&container->prereg_list,
-				struct tce_iommu_prereg, next);
-		WARN_ON_ONCE(tce_iommu_prereg_free(container, tcemem));
-	}
+	list_for_each_entry_safe(tcemem, tmtmp, &container->prereg_list, next)
+		WARN_ON(tce_iommu_prereg_free(container, tcemem));
 
 	tce_iommu_disable(container);
 	if (container->mm)
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
Acked-by on v1 1/1
#2 lore raw
In reply to Alexey Kardashevskiy
Quoted 11 lines · On Tue, 2 Oct 2018 13:22:31 +1000 Alexey Kardashevskiy <[email protected]> wrote:

As a part of cleanup, the SPAPR TCE IOMMU subdriver releases preregistered memory. If there is a bug in memory release, the loop in tce_iommu_release() becomes infinite; this actually happened to me.

This makes the loop finite and prints a warning on every failure to make the code more bug prone.

---
 drivers/vfio/vfio_iommu_spapr_tce.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

Should this have a stable/fixes tag? Looks like it's relevant to:

4b6fad7097f8 powerpc/mm/iommu, vfio/spapr: Put pages on VFIO container shutdown

Also, not sure who you're wanting to take this since it was sent to ppc lists. If it's me, let me know, otherwise

Thanks,
Alex

Quoted 28 lines
drivers/vfio/vfio_iommu_spapr_tce.c +3 -7
index b1a8ab3..ece0651 100644
@@ -371,6 +371,7 @@ static void tce_iommu_release(void *iommu_data)
 {
 	struct tce_container *container = iommu_data;
 	struct tce_iommu_group *tcegrp;
+	struct tce_iommu_prereg *tcemem, *tmtmp;
 	long i;
 
 	while (tce_groups_attached(container)) {
@@ -393,13 +394,8 @@ static void tce_iommu_release(void *iommu_data)
 		tce_iommu_free_table(container, tbl);
 	}
 
-	while (!list_empty(&container->prereg_list)) {
-		struct tce_iommu_prereg *tcemem;
-
-		tcemem = list_first_entry(&container->prereg_list,
-				struct tce_iommu_prereg, next);
-		WARN_ON_ONCE(tce_iommu_prereg_free(container, tcemem));
-	}
+	list_for_each_entry_safe(tcemem, tmtmp, &container->prereg_list, next)
+		WARN_ON(tce_iommu_prereg_free(container, tcemem));
 
 	tce_iommu_disable(container);
 	if (container->mm)
#3 lore raw
In reply to Alexey Kardashevskiy
Quoted 27 lines · Alexey Kardashevskiy

As a part of cleanup, the SPAPR TCE IOMMU subdriver releases preregistered memory. If there is a bug in memory release, the loop in tce_iommu_release() becomes infinite; this actually happened to me.

This makes the loop finite and prints a warning on every failure to make the code more bug prone.

---
 drivers/vfio/vfio_iommu_spapr_tce.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/vfio/vfio_iommu_spapr_tce.c +1 -0
index b1a8ab3..ece0651 100644
@@ -371,6 +371,7 @@ static void tce_iommu_release(void *iommu_data)
 {
 	struct tce_container *container = iommu_data;
 	struct tce_iommu_group *tcegrp;
+	struct tce_iommu_prereg *tcemem, *tmtmp;
 	long i;
 
 	while (tce_groups_attached(container)) {
@@ -393,13 +394,8 @@ static void tce_iommu_release(void *iommu_data)
 		tce_iommu_free_table(container, tbl);
 	}

drivers/vfio/vfio_iommu_spapr_tce.c static void tce_iommu_release(void *iommu_data) · line 398

Quoted 10 lines
drivers/vfio/vfio_iommu_spapr_tce.c +2 -7
 
-	while (!list_empty(&container->prereg_list)) {
-		struct tce_iommu_prereg *tcemem;
-
-		tcemem = list_first_entry(&container->prereg_list,
-				struct tce_iommu_prereg, next);
-		WARN_ON_ONCE(tce_iommu_prereg_free(container, tcemem));
-	}
+	list_for_each_entry_safe(tcemem, tmtmp, &container->prereg_list, next)
+		WARN_ON(tce_iommu_prereg_free(container, tcemem));

I'm not sure that tce_iommu_prereg_free() call under WARN_ON() is good idea because WARN_ON() is a preprocessor macro:

if CONFIG_WARN=n is added by the analogy with CONFIG_BUG=n defining WARN_ON() as empty we will loose call to tce_iommu_prereg_free() leaking resources.

There is no problem at the moment: WARN_ON() defined for PPC in arch/powerpc/include/asm/bug.h unconditionally.

So your first version with intermediate variable looks better to me.

Quoted 4 lines
drivers/vfio/vfio_iommu_spapr_tce.c +0 -0
 	tce_iommu_disable(container);
 	if (container->mm)
 
Signature · 2 lines
Thanks,
Serhii
#4 lore raw
Quoted 34 lines · Serhii Popovych
Quoted 19 lines · Alexey Kardashevskiy

As a part of cleanup, the SPAPR TCE IOMMU subdriver releases preregistered memory. If there is a bug in memory release, the loop in tce_iommu_release() becomes infinite; this actually happened to me.

This makes the loop finite and prints a warning on every failure to make the code more bug prone.

---
 drivers/vfio/vfio_iommu_spapr_tce.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/vfio/vfio_iommu_spapr_tce.c +0 -0
index b1a8ab3..ece0651 100644
@@ -393,13 +394,8 @@ static void tce_iommu_release(void *iommu_data)
 		tce_iommu_free_table(container, tbl);
 	}

drivers/vfio/vfio_iommu_spapr_tce.c static void tce_iommu_release(void *iommu_data) · line 398

Quoted 10 lines
drivers/vfio/vfio_iommu_spapr_tce.c +2 -7
 
-	while (!list_empty(&container->prereg_list)) {
-		struct tce_iommu_prereg *tcemem;
-
-		tcemem = list_first_entry(&container->prereg_list,
-				struct tce_iommu_prereg, next);
-		WARN_ON_ONCE(tce_iommu_prereg_free(container, tcemem));
-	}
+	list_for_each_entry_safe(tcemem, tmtmp, &container->prereg_list, next)
+		WARN_ON(tce_iommu_prereg_free(container, tcemem));

I'm not sure that tce_iommu_prereg_free() call under WARN_ON() is good idea because WARN_ON() is a preprocessor macro:

Quoted 3 lines

if CONFIG_WARN=n is added by the analogy with CONFIG_BUG=n defining WARN_ON() as empty we will loose call to tce_iommu_prereg_free() leaking resources.

I don't think that's likely to ever happen though, we have a large number of uses that would need to be checked one-by-one:

  $ git grep "if (WARN_ON(" | wc -l
  2853

So if we ever did add CONFIG_WARN, I think it would still need to evaluate the condition, just not emit a warning.

cheers

#5 lore raw
Quoted 42 lines · Michael Ellerman
Quoted 34 lines · Serhii Popovych
Quoted 19 lines · Alexey Kardashevskiy

As a part of cleanup, the SPAPR TCE IOMMU subdriver releases preregistered memory. If there is a bug in memory release, the loop in tce_iommu_release() becomes infinite; this actually happened to me.

This makes the loop finite and prints a warning on every failure to make the code more bug prone.

---
 drivers/vfio/vfio_iommu_spapr_tce.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/vfio/vfio_iommu_spapr_tce.c +0 -0
index b1a8ab3..ece0651 100644
@@ -393,13 +394,8 @@ static void tce_iommu_release(void *iommu_data)
 		tce_iommu_free_table(container, tbl);
 	}

drivers/vfio/vfio_iommu_spapr_tce.c static void tce_iommu_release(void *iommu_data) · line 398

Quoted 10 lines
drivers/vfio/vfio_iommu_spapr_tce.c +2 -7
 
-	while (!list_empty(&container->prereg_list)) {
-		struct tce_iommu_prereg *tcemem;
-
-		tcemem = list_first_entry(&container->prereg_list,
-				struct tce_iommu_prereg, next);
-		WARN_ON_ONCE(tce_iommu_prereg_free(container, tcemem));
-	}
+	list_for_each_entry_safe(tcemem, tmtmp, &container->prereg_list, next)
+		WARN_ON(tce_iommu_prereg_free(container, tcemem));

I'm not sure that tce_iommu_prereg_free() call under WARN_ON() is good idea because WARN_ON() is a preprocessor macro:

Quoted 3 lines

if CONFIG_WARN=n is added by the analogy with CONFIG_BUG=n defining WARN_ON() as empty we will loose call to tce_iommu_prereg_free() leaking resources.

I don't think that's likely to ever happen though, we have a large number of uses that would need to be checked one-by-one:

Quoted 6 lines
  $ git grep "if (WARN_ON(" | wc -l
  2853

So if we ever did add CONFIG_WARN, I think it would still need to evaluate the condition, just not emit a warning.

Is anyone taking this?

Signature · 1 line
Alexey
#6 lore raw
In reply to Alexey Kardashevskiy
Quoted 10 lines · Alexey Kardashevskiy

As a part of cleanup, the SPAPR TCE IOMMU subdriver releases preregistered memory. If there is a bug in memory release, the loop in tce_iommu_release() becomes infinite; this actually happened to me.

This makes the loop finite and prints a warning on every failure to make the code more bug prone.

Applied to powerpc next, thanks.

https://git.kernel.org/powerpc/c/517ad4ae8aa93dccdb9a88c27257ec

cheers