Skip to contentSkip to conversation

AMD/IOMMU: assorted corrections (leftover)

PATCH In review Jan Beulich 4 participants 14 messages Feb 4, 2025 – Feb 10, 2025

lore
Share & tools
lore
#0 lore raw

v3 cover AMD/IOMMU: assorted corrections (leftover)

What previously was the main bug fix in this series was dropped for v3. It turns out what is now patch 2 is sufficient to address the issue, while doing the requested tidying. The latter two patches are for 4.21 only, with the final one being up for debate altogether.

1: radix-tree: purge node allocation override hooks
2: radix-tree: introduce RADIX_TREE{,_INIT}()
3: radix-tree: drop "root" parameters from radix_tree_node_{alloc,free}() 4: PCI: drop pci_segments_init()

Jan

#1 lore raw

PATCH v3 1/4 radix-tree: purge node allocation override hooks

These were needed by TMEM only, which is long gone. The Linux original doesn't have such either. This effectively reverts one of the "Other changes" from 8dc6738dbb3c ("Update radix-tree.[ch] from upstream Linux to gain RCU awareness").

Positive side effect: Two cf_check go away.

While there also convert xmalloc()+memset() to xzalloc(). (Don't convert to xvzalloc(), as that would require touching the freeing side, too.)

---
v3: Add a missing blank line.
v2: New.

xen/common/radix-tree.c +6 -31
@@ -53,12 +53,6 @@ struct rcu_node {
 	struct rcu_head rcu_head;
 };
 
-static struct radix_tree_node *cf_check rcu_node_alloc(void *arg)
-{
-	struct rcu_node *rcu_node = xmalloc(struct rcu_node);
-	return rcu_node ? &rcu_node->node : NULL;
-}
-
 static void cf_check _rcu_node_free(struct rcu_head *head)
 {
 	struct rcu_node *rcu_node =
@@ -66,26 +60,20 @@ static void cf_check _rcu_node_free(stru
 	xfree(rcu_node);
 }
 
-static void cf_check rcu_node_free(struct radix_tree_node *node, void *arg)
-{
-	struct rcu_node *rcu_node = container_of(node, struct rcu_node, node);
-	call_rcu(&rcu_node->rcu_head, _rcu_node_free);
-}
-
 static struct radix_tree_node *radix_tree_node_alloc(
 	struct radix_tree_root *root)
 {
-	struct radix_tree_node *ret;
-	ret = root->node_alloc(root->node_alloc_free_arg);
-	if (ret)
-		memset(ret, 0, sizeof(*ret));
-	return ret;
+	struct rcu_node *rcu_node = xzalloc(struct rcu_node);
+
+	return rcu_node ? &rcu_node->node : NULL;
 }
 
 static void radix_tree_node_free(
 	struct radix_tree_root *root, struct radix_tree_node *node)
 {
-	root->node_free(node, root->node_alloc_free_arg);
+	struct rcu_node *rcu_node = container_of(node, struct rcu_node, node);
+
+	call_rcu(&rcu_node->rcu_head, _rcu_node_free);
 }
 
 /*
@@ -718,19 +706,6 @@ void radix_tree_destroy(
 void radix_tree_init(struct radix_tree_root *root)
 {
 	memset(root, 0, sizeof(*root));
-	root->node_alloc = rcu_node_alloc;
-	root->node_free = rcu_node_free;
-}
-
-void radix_tree_set_alloc_callbacks(
-	struct radix_tree_root *root,
-	radix_tree_alloc_fn_t *node_alloc,
-	radix_tree_free_fn_t *node_free,
-	void *node_alloc_free_arg)
-{
-	root->node_alloc = node_alloc;
-	root->node_free = node_free;
-	root->node_alloc_free_arg = node_alloc_free_arg;
 }
 
 static __init unsigned long __maxindex(unsigned int height)
xen/include/xen/radix-tree.h +0 -10
@@ -66,11 +66,6 @@ typedef void radix_tree_free_fn_t(struct
 struct radix_tree_root {
 	unsigned int		height;
 	struct radix_tree_node	__rcu *rnode;
-
-	/* Allow to specify custom node alloc/dealloc routines. */
-	radix_tree_alloc_fn_t *node_alloc;
-	radix_tree_free_fn_t *node_free;
-	void *node_alloc_free_arg;
 };
 
 /*
@@ -78,11 +73,6 @@ struct radix_tree_root {
  */
 
 void radix_tree_init(struct radix_tree_root *root);
-void radix_tree_set_alloc_callbacks(
-	struct radix_tree_root *root,
-	radix_tree_alloc_fn_t *node_alloc,
-	radix_tree_free_fn_t *node_free,
-	void *node_alloc_free_arg);
 
 void radix_tree_destroy(
 	struct radix_tree_root *root,
#2 lore raw

PATCH v3 2/4 radix-tree: introduce RADIX_TREE{,_INIT}()

In reply to Jan Beulich

... now that static initialization is possible. Use RADIX_TREE() for pci_segments and ivrs_maps.

This then fixes an ordering issue on x86: With the call to radix_tree_init(), acpi_mmcfg_init()'s invocation of pci_segments_init() will zap the possible earlier introduction of segment 0 by amd_iommu_detect_one_acpi()'s call to pci_ro_device(), and thus the write-protection of the PCI devices representing AMD IOMMUs.

Fixes 3950f2485bbc ("x86/x2APIC: defer probe until after IOMMU ACPI table parsing")

---
Sadly gcc below 5.x doesn't support compound literals in static initializers (Clang 3.5 does). Hence the request in response to v2 has to remain un-addressed.
---
v3: Extend description and add Fixes: tag.
v2: New.

xen/drivers/passthrough/amd/iommu_init.c +1 -2
@@ -31,7 +31,7 @@ static struct tasklet amd_iommu_irq_task
 unsigned int __read_mostly amd_iommu_acpi_info;
 unsigned int __read_mostly ivrs_bdf_entries;
 u8 __read_mostly ivhd_type;
-static struct radix_tree_root ivrs_maps;
+static RADIX_TREE(ivrs_maps);
 LIST_HEAD_RO_AFTER_INIT(amd_iommu_head);
 bool iommuv2_enabled;
 
@@ -1408,7 +1408,6 @@ int __init amd_iommu_prepare(bool xt)
         goto error_out;
     ivrs_bdf_entries = rc;
 
-    radix_tree_init(&ivrs_maps);
     for_each_amd_iommu ( iommu )
     {
         rc = amd_iommu_prepare_one(iommu);
xen/drivers/passthrough/pci.c +1 -2
@@ -68,7 +68,7 @@ bool pcidevs_locked(void)
     return rspin_is_locked(&_pcidevs_lock);
 }
 
-static struct radix_tree_root pci_segments;
+static RADIX_TREE(pci_segments);
 
 static inline struct pci_seg *get_pseg(u16 seg)
 {
@@ -124,7 +124,6 @@ static int pci_segments_iterate(
 
 void __init pci_segments_init(void)
 {
-    radix_tree_init(&pci_segments);
     if ( !alloc_pseg(0) )
         panic("Could not initialize PCI segment 0\n");
 }
xen/include/xen/radix-tree.h +3 -0
@@ -72,6 +72,9 @@ struct radix_tree_root {
  *** radix-tree API starts here **
  */
 
+#define RADIX_TREE_INIT() {}
+#define RADIX_TREE(name) struct radix_tree_root name = RADIX_TREE_INIT()
+
 void radix_tree_init(struct radix_tree_root *root);
 
 void radix_tree_destroy(
#3 lore raw

Re: [PATCH v3 for-4.20 2/4] radix-tree: introduce RADIX_TREE{,_INIT}()

Quoted 62 lines · Jan Beulich

... now that static initialization is possible. Use RADIX_TREE() for pci_segments and ivrs_maps.

This then fixes an ordering issue on x86: With the call to radix_tree_init(), acpi_mmcfg_init()'s invocation of pci_segments_init() will zap the possible earlier introduction of segment 0 by amd_iommu_detect_one_acpi()'s call to pci_ro_device(), and thus the write-protection of the PCI devices representing AMD IOMMUs.

Fixes 3950f2485bbc ("x86/x2APIC: defer probe until after IOMMU ACPI table parsing")

---
Sadly gcc below 5.x doesn't support compound literals in static initializers (Clang 3.5 does). Hence the request in response to v2 has to remain un-addressed.
---
v3: Extend description and add Fixes: tag.
v2: New.

xen/drivers/passthrough/amd/iommu_init.c +1 -2
@@ -31,7 +31,7 @@ static struct tasklet amd_iommu_irq_task
 unsigned int __read_mostly amd_iommu_acpi_info;
 unsigned int __read_mostly ivrs_bdf_entries;
 u8 __read_mostly ivhd_type;
-static struct radix_tree_root ivrs_maps;
+static RADIX_TREE(ivrs_maps);
 LIST_HEAD_RO_AFTER_INIT(amd_iommu_head);
 bool iommuv2_enabled;
 
@@ -1408,7 +1408,6 @@ int __init amd_iommu_prepare(bool xt)
         goto error_out;
     ivrs_bdf_entries = rc;
 
-    radix_tree_init(&ivrs_maps);
     for_each_amd_iommu ( iommu )
     {
         rc = amd_iommu_prepare_one(iommu);
xen/drivers/passthrough/pci.c +1 -2
@@ -68,7 +68,7 @@ bool pcidevs_locked(void)
     return rspin_is_locked(&_pcidevs_lock);
 }
 
-static struct radix_tree_root pci_segments;
+static RADIX_TREE(pci_segments);
 
 static inline struct pci_seg *get_pseg(u16 seg)
 {
@@ -124,7 +124,6 @@ static int pci_segments_iterate(
 
 void __init pci_segments_init(void)
 {
-    radix_tree_init(&pci_segments);
     if ( !alloc_pseg(0) )
         panic("Could not initialize PCI segment 0\n");
 }

xen/include/xen/radix-tree.h struct radix_tree_root { · line 76

Quoted 5 lines
xen/include/xen/radix-tree.h +2 -0
@@ -72,6 +72,9 @@ struct radix_tree_root {
  *** radix-tree API starts here **
  */
 
+#define RADIX_TREE_INIT() {}
+#define RADIX_TREE(name) struct radix_tree_root name = RADIX_TREE_INIT()

We can still use this form in radix_tree_init(), can't we?

~Andrew

#4 lore raw

Re: [PATCH v3 for-4.20 2/4] radix-tree: introduce RADIX_TREE{,_INIT}()

Quoted 11 lines · Andrew Cooper

xen/include/xen/radix-tree.h struct radix_tree_root { · line 76

Quoted 8 lines · Jan Beulich
xen/include/xen/radix-tree.h +2 -0
@@ -72,6 +72,9 @@ struct radix_tree_root {
  *** radix-tree API starts here **
  */
 
+#define RADIX_TREE_INIT() {}
+#define RADIX_TREE(name) struct radix_tree_root name = RADIX_TREE_INIT()

We can still use this form in radix_tree_init(), can't we?

Only indirectly, and that's imo ugly:

void radix_tree_init(struct radix_tree_root *root)

{
	RADIX_TREE(init);

	*root = init;
}

RADIX_TREE_INIT() cannot (directly) be used because {} isn't an rvalue.

Jan

#5 lore raw

Re: [PATCH v3 for-4.20 2/4] radix-tree: introduce RADIX_TREE{,_INIT}()

Quoted 13 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper

xen/include/xen/radix-tree.h struct radix_tree_root { · line 76

Quoted 8 lines · Jan Beulich
xen/include/xen/radix-tree.h +2 -0
@@ -72,6 +72,9 @@ struct radix_tree_root {
  *** radix-tree API starts here **
  */
 
+#define RADIX_TREE_INIT() {}
+#define RADIX_TREE(name) struct radix_tree_root name = RADIX_TREE_INIT()

We can still use this form in radix_tree_init(), can't we?

Only indirectly, and that's imo ugly:

Quoted 8 lines

void radix_tree_init(struct radix_tree_root *root)

{
	RADIX_TREE(init);

	*root = init;
}

RADIX_TREE_INIT() cannot (directly) be used because {} isn't an rvalue.

Even if it's not ideal,

    *root = *(struct radix_tree_root)RADIX_TREE_INIT();

works.  We use this pattern elsewhere in Xen, so it surely will be fine even on ancient compilers.

~Andrew

#6 lore raw

Re: [PATCH v3 for-4.20 2/4] radix-tree: introduce RADIX_TREE{,_INIT}()

Quoted 21 lines · Andrew Cooper
Quoted 20 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper

xen/include/xen/radix-tree.h struct radix_tree_root { · line 76

Quoted 8 lines · Jan Beulich
xen/include/xen/radix-tree.h +2 -0
@@ -72,6 +72,9 @@ struct radix_tree_root {
  *** radix-tree API starts here **
  */
 
+#define RADIX_TREE_INIT() {}
+#define RADIX_TREE(name) struct radix_tree_root name = RADIX_TREE_INIT()

We can still use this form in radix_tree_init(), can't we?

Only indirectly, and that's imo ugly:

void radix_tree_init(struct radix_tree_root *root)

{
	RADIX_TREE(init);

	*root = init;
}
Quoted 8 lines
Quoted 1 line

RADIX_TREE_INIT() cannot (directly) be used because {} isn't an rvalue.

Even if it's not ideal,

    *root = *(struct radix_tree_root)RADIX_TREE_INIT();

works.  We use this pattern elsewhere in Xen, so it surely will be fine even on ancient compilers.

Hmm, yes, with the * on the rhs dropped this ought to work. I'll switch, yet at the same time I have to admit that I don't recall having seen this pattern anywhere in our tree.

Jan

#7 lore raw

PATCH v3 3/4 radix-tree: drop "root" parameters from radix_tree_node_{alloc,free}()

In reply to Jan Beulich

They aren't used anymore.

---
v2: New.

xen/common/radix-tree.c +8 -10
@@ -60,16 +60,14 @@ static void cf_check _rcu_node_free(stru
 	xfree(rcu_node);
 }
 
-static struct radix_tree_node *radix_tree_node_alloc(
-	struct radix_tree_root *root)
+static struct radix_tree_node *radix_tree_node_alloc(void)
 {
 	struct rcu_node *rcu_node = xzalloc(struct rcu_node);
 
 	return rcu_node ? &rcu_node->node : NULL;
 }
 
-static void radix_tree_node_free(
-	struct radix_tree_root *root, struct radix_tree_node *node)
+static void radix_tree_node_free(struct radix_tree_node *node)
 {
 	struct rcu_node *rcu_node = container_of(node, struct rcu_node, node);
 
@@ -105,7 +103,7 @@ static int radix_tree_extend(struct radi
 
 	do {
 		unsigned int newheight;
-		if (!(node = radix_tree_node_alloc(root)))
+		if (!(node = radix_tree_node_alloc()))
 			return -ENOMEM;
 
 		/* Increase the height.  */
@@ -156,7 +154,7 @@ int radix_tree_insert(struct radix_tree_
 	while (height > 0) {
 		if (slot == NULL) {
 			/* Have to add a child node.  */
-			if (!(slot = radix_tree_node_alloc(root)))
+			if (!(slot = radix_tree_node_alloc()))
 				return -ENOMEM;
 			slot->height = height;
 			if (node) {
@@ -575,7 +573,7 @@ static inline void radix_tree_shrink(str
 			*((unsigned long *)&to_free->slots[0]) |=
 						RADIX_TREE_INDIRECT_PTR;
 
-		radix_tree_node_free(root, to_free);
+		radix_tree_node_free(to_free);
 	}
 }
 
@@ -640,7 +638,7 @@ void *radix_tree_delete(struct radix_tre
 		 * last reference to it disappears (set NULL, above).
 		 */
 		if (to_free)
-			radix_tree_node_free(root, to_free);
+			radix_tree_node_free(to_free);
 
 		if (pathp->node->count) {
 			if (pathp->node == indirect_to_ptr(root->rnode))
@@ -656,7 +654,7 @@ void *radix_tree_delete(struct radix_tre
 	root->height = 0;
 	root->rnode = NULL;
 	if (to_free)
-		radix_tree_node_free(root, to_free);
+		radix_tree_node_free(to_free);
 
 out:
 	return slot;
@@ -683,7 +681,7 @@ radix_tree_node_destroy(
 		}
 	}
 
-	radix_tree_node_free(root, node);
+	radix_tree_node_free(node);
 }
 
 void radix_tree_destroy(
#8 lore raw

PATCH v3 4/4 PCI: drop pci_segments_init()

In reply to Jan Beulich

Have callers invoke pci_add_segment() directly instead: With radix tree initialization moved out of the function, its name isn't quite describing anymore what it actually does.

---
This is entirely optional and up for discussion. There certainly also is an argument towards keeping the function.
---
v3: Adjust description to accont for and re-base over dropped earlier
patch.
v2: New.

xen/arch/arm/pci/pci.c +2 -1
@@ -88,7 +88,8 @@ static int __init pci_init(void)
     if ( !pci_passthrough_enabled )
         return 0;
 
-    pci_segments_init();
+    if ( pci_add_segment(0) )
+        panic("Could not initialize PCI segment 0\n");
 
     if ( acpi_disabled )
         return dt_pci_init();
xen/arch/x86/x86_64/mmconfig-shared.c +2 -1
@@ -402,7 +402,8 @@ void __init acpi_mmcfg_init(void)
 {
     bool valid = true;
 
-    pci_segments_init();
+    if ( pci_add_segment(0) )
+        panic("Could not initialize PCI segment 0\n");
 
     /* MMCONFIG disabled */
     if ((pci_probe & PCI_PROBE_MMCONF) == 0)
xen/drivers/passthrough/pci.c +0 -6
@@ -122,12 +122,6 @@ static int pci_segments_iterate(
     return rc;
 }
 
-void __init pci_segments_init(void)
-{
-    if ( !alloc_pseg(0) )
-        panic("Could not initialize PCI segment 0\n");
-}
-
 int __init pci_add_segment(u16 seg)
 {
     return alloc_pseg(seg) ? 0 : -ENOMEM;
xen/include/xen/pci.h +0 -1
@@ -214,7 +214,6 @@ void setup_hwdom_pci_devices(struct doma
                              int (*handler)(uint8_t devfn,
                                             struct pci_dev *pdev));
 int pci_release_devices(struct domain *d);
-void pci_segments_init(void);
 int pci_add_segment(u16 seg);
 const unsigned long *pci_get_ro_map(u16 seg);
 int pci_add_device(u16 seg, u8 bus, u8 devfn,
#9 lore raw

Re: [PATCH v3 for-4.21 4/4] PCI: drop pci_segments_init()

Quoted 5 lines · Jan Beulich

Have callers invoke pci_add_segment() directly instead: With radix tree initialization moved out of the function, its name isn't quite describing anymore what it actually does.

IMO I would rather not add the segment here, and just make sure that all callers that add segments have proper error reporting when it fails. Maybe alloc_pseg() should gain a printk on failure?

Thanks, Roger.

#10 lore raw

Re: [PATCH v3 for-4.21 4/4] PCI: drop pci_segments_init()

Quoted 10 lines · Roger Pau Monné
Quoted 5 lines · Jan Beulich

Have callers invoke pci_add_segment() directly instead: With radix tree initialization moved out of the function, its name isn't quite describing anymore what it actually does.

IMO I would rather not add the segment here, and just make sure that all callers that add segments have proper error reporting when it fails.

Maybe. Yet then things may (on x86) work fine with secondary segments not properly working. A log from one of the few multi-segment systems that I had seen data from suggested that none of the devices on the secondary segment were actually used by anything. This was, if I'm not mistaken, the underlying reason why (on x86) we demand segment 0 to have proper representation, but do things best effort only for other segments. Which isn't to say that we can't change things and do better.

Quoted 1 line

Maybe alloc_pseg() should gain a printk on failure?

Not sure that would buy us much, especially if (on x86) it's seg 0 which is affected.

For the patch at hand - do you then suggest to simply drop it? The plan here wasn't to re-work what we do, just tidy slightly how we do things.

Jan

#11 lore raw

Re: [PATCH v3 for-4.21 4/4] PCI: drop pci_segments_init()

Quoted 20 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monné
Quoted 5 lines · Jan Beulich

Have callers invoke pci_add_segment() directly instead: With radix tree initialization moved out of the function, its name isn't quite describing anymore what it actually does.

IMO I would rather not add the segment here, and just make sure that all callers that add segments have proper error reporting when it fails.

Maybe. Yet then things may (on x86) work fine with secondary segments not properly working. A log from one of the few multi-segment systems that I had seen data from suggested that none of the devices on the secondary segment were actually used by anything. This was, if I'm not mistaken, the underlying reason why (on x86) we demand segment 0 to have proper representation, but do things best effort only for other segments. Which isn't to say that we can't change things and do better.

Quoted 7 lines
Quoted 1 line

Maybe alloc_pseg() should gain a printk on failure?

Not sure that would buy us much, especially if (on x86) it's seg 0 which is affected.

For the patch at hand - do you then suggest to simply drop it? The plan here wasn't to re-work what we do, just tidy slightly how we do things.

I feel like acpi_mmcfg_init() is an obscure place to do this. It won't be strange to shuffle that call around and forgot it's also adding segment 0.

I would prefer if such allocation of segment 0 was done in __start_xen(), as that makes it much easier to identify dependencies and prevent such kind of re-ordering mistakes.

Thanks, Roger.

#12 lore raw
In reply to Jan Beulich

Oleksii,

Quoted 9 lines · Jan Beulich

What previously was the main bug fix in this series was dropped for v3. It turns out what is now patch 2 is sufficient to address the issue, while doing the requested tidying. The latter two patches are for 4.21 only, with the final one being up for debate altogether.

1: radix-tree: purge node allocation override hooks
2: radix-tree: introduce RADIX_TREE{,_INIT}()
3: radix-tree: drop "root" parameters from radix_tree_node_{alloc,free}() 4: PCI: drop pci_segments_init()

any verdict one way or the other for the first two patches?

Thanks, Jan

#13 lore raw
Quoted 8 lines · Jan Beulich

Oleksii,

Quoted 5 lines · Jan Beulich

What previously was the main bug fix in this series was dropped for v3. It turns out what is now patch 2 is sufficient to address the issue, while doing the requested tidying. The latter two patches are for 4.21 only, with the final one being up for debate altogether.

Quoted 5 lines
Quoted 4 lines

1: radix-tree: purge node allocation override hooks
2: radix-tree: introduce RADIX_TREE{,_INIT}()
3: radix-tree: drop "root" parameters from radix_tree_node_{alloc,free}() 4: PCI: drop pci_segments_init()

any verdict one way or the other for the first two patches?

Sorry, I missed the fixes tag so I thought that they are just refactoring.

For first two patches:

~ Oleksii