Skip to contentSkip to conversation

iommufd: Fix missing update of domains_itree after splitting iopt_area

PATCH In review Koichiro Den 2 participants 3 messages Oct 27, 2023 – Oct 31, 2023

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

PATCH 1/1 iommufd: Fix missing update of domains_itree after splitting iopt_area

In iopt_area_split(), if the original iopt_area has filled a domain and is linked to domains_itree, pages_nodes have to be properly reinserted.

---
 drivers/iommu/iommufd/io_pagetable.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/iommu/iommufd/io_pagetable.c +7 -0
index 9f060abe53b6..5ba83296c91a 100644
@@ -1220,6 +1220,13 @@ static int iopt_area_split(struct iopt_area *area, unsigned long iova)
 	if (WARN_ON(rc))
 		goto err_remove_lhs;
 
+	/* If the original area has filled a domain, domains_itree has to be updated. */
+	if (!RB_EMPTY_NODE(&area->pages_node.rb)) {
+		interval_tree_remove(&area->pages_node, &pages->domains_itree);
+		interval_tree_insert(&lhs->pages_node, &pages->domains_itree);
+		interval_tree_insert(&rhs->pages_node, &pages->domains_itree);
+	}
+
 	lhs->storage_domain = area->storage_domain;
 	lhs->pages = area->pages;
 	rhs->storage_domain = area->storage_domain;
Signature · 1 line
2.41.0
#1 lore raw
Quoted 2 lines · Koichiro Den

In iopt_area_split(), if the original iopt_area has filled a domain and is linked to domains_itree, pages_nodes have to be properly reinserted.

Fixes 51fe6141f0f6 ("iommufd: Data structure to provide IOVA to PFN mapping")
Quoted 10 lines
---
 drivers/iommu/iommufd/io_pagetable.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/iommu/iommufd/io_pagetable.c +0 -0
index 9f060abe53b6..5ba83296c91a 100644

drivers/iommu/iommufd/io_pagetable.c static int iopt_area_split(struct iopt_area *area, unsigned long iova) · line 1224

Quoted 5 lines
drivers/iommu/iommufd/io_pagetable.c +2 -0
@@ -1220,6 +1220,13 @@ static int iopt_area_split(struct iopt_area *area, unsigned long iova)
 	if (WARN_ON(rc))
 		goto err_remove_lhs;
 
+	/* If the original area has filled a domain, domains_itree has to be updated. */
+	if (!RB_EMPTY_NODE(&area->pages_node.rb)) {

This isn't going to work entirely right because we never call RB_CLEAR_NODE.. Checking area->storage_domain would be appropriate

drivers/iommu/iommufd/io_pagetable.c static int iopt_area_split(struct iopt_area *area, unsigned long iova) · line 1227

Quoted 3 lines
drivers/iommu/iommufd/io_pagetable.c +3 -0
+		interval_tree_remove(&area->pages_node, &pages->domains_itree);
+		interval_tree_insert(&lhs->pages_node, &pages->domains_itree);
+		interval_tree_insert(&rhs->pages_node, &pages->domains_itree);

But yes, this is needed!!

I investigated for a while how come this doesn't crash already, and it seems to be a bit because of how rb tree works, that the test never does an access, and a bit because we never call RB_CLEAR_NODE.

Below improves things, and does make it crash without this change

Anyhow I picked this with the change and below as well, thanks!

From d906a485451dcce445f91aa207f5e724d1bf1e4e Mon Sep 17 00:00:00 2001
From: Jason Gunthorpe <[email protected]>
Date: Mon, 30 Oct 2023 11:26:33 -0300
Subject: [PATCH] iommufd: Add iopt_area_alloc()

We never initialize the two interval tree nodes, and zero fill is not the same as RB_CLEAR_NODE. This can hide issues where we missed adding the area to the trees. Factor out the allocation and clear the two nodes.

Fixes 51fe6141f0f6 ("iommufd: Data structure to provide IOVA to PFN mapping")
---
 drivers/iommu/iommufd/io_pagetable.c | 18 +++++++++++++++---
 drivers/iommu/iommufd/pages.c        |  2 ++
 2 files changed, 17 insertions(+), 3 deletions(-)
drivers/iommu/iommufd/io_pagetable.c +15 -3
index 9f060abe53b6c9..87075cbc2c630b 100644
@@ -222,6 +222,18 @@ static int iopt_insert_area(struct io_pagetable *iopt, struct iopt_area *area,
 	return 0;
 }
 
+static struct iopt_area *iopt_area_alloc(void)
+{
+	struct iopt_area *area;
+
+	area = kzalloc(sizeof(*area), GFP_KERNEL_ACCOUNT);
+	if (!area)
+		return NULL;
+	RB_CLEAR_NODE(&area->node.rb);
+	RB_CLEAR_NODE(&area->pages_node.rb);
+	return area;
+}
+
 static int iopt_alloc_area_pages(struct io_pagetable *iopt,
 				 struct list_head *pages_list,
 				 unsigned long length, unsigned long *dst_iova,
@@ -232,7 +244,7 @@ static int iopt_alloc_area_pages(struct io_pagetable *iopt,
 	int rc = 0;
 
 	list_for_each_entry(elm, pages_list, next) {
-		elm->area = kzalloc(sizeof(*elm->area), GFP_KERNEL_ACCOUNT);
+		elm->area = iopt_area_alloc();
 		if (!elm->area)
 			return -ENOMEM;
 	}
@@ -1177,11 +1189,11 @@ static int iopt_area_split(struct iopt_area *area, unsigned long iova)
 	    iopt_area_start_byte(area, new_start) & (alignment - 1))
 		return -EINVAL;
 
-	lhs = kzalloc(sizeof(*area), GFP_KERNEL_ACCOUNT);
+	lhs = iopt_area_alloc();
 	if (!lhs)
 		return -ENOMEM;
 
-	rhs = kzalloc(sizeof(*area), GFP_KERNEL_ACCOUNT);
+	rhs = iopt_area_alloc();
 	if (!rhs) {
 		rc = -ENOMEM;
 		goto err_free_lhs;
drivers/iommu/iommufd/pages.c +2 -0
index 8d9aa297c117e4..528f356238b343 100644
@@ -1507,6 +1507,8 @@ void iopt_area_unfill_domains(struct iopt_area *area, struct iopt_pages *pages)
 				area, domain, iopt_area_index(area),
 				iopt_area_last_index(area));
 
+	if (IS_ENABLED(CONFIG_IOMMUFD_TEST))
+		WARN_ON(RB_EMPTY_NODE(&area->pages_node.rb));
 	interval_tree_remove(&area->pages_node, &pages->domains_itree);
 	iopt_area_unfill_domain(area, pages, area->storage_domain);
 	area->storage_domain = NULL;
Signature · 1 line
2.42.0
#2 lore raw
Quoted 4 lines
Quoted 1 line

+ if (!RB_EMPTY_NODE(&area->pages_node.rb)) {

This isn't going to work entirely right because we never call RB_CLEAR_NODE.. Checking area->storage_domain would be appropriate

Right, thank you.

Quoted 1 line

I investigated for a while how come this doesn't crash already, and

it

Quoted 6 lines

seems to be a bit because of how rb tree works, that the test never does an access, and a bit because we never call RB_CLEAR_NODE.

Below improves things, and does make it crash without this change

Anyhow I picked this with the change and below as well, thanks!

I overlooked the nodes initialization part and your supplementary commit looks fine. Thank you. Before the patch, I initially thought that any subsequent operation after splitting that involves storage tier traversal, like adding another access, adding another domain or just resources releasing part etc. can potentially do us harm. But anyway after the fix, no need to worry and the test part you added may be sufficent. Thank you.