Skip to contentSkip to conversation

iommu/amd: Modify set_dte_entry() to use 128-bit cmpxchg operation

PATCH Superseded Suravee Suthikulpanit 3 participants 5 messages Aug 19, 2024 – Aug 29, 2024

Subsystems (2)

v1 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Suravee Suthikulpanit

author reviewer
#0 lore raw

PATCH 1/1 iommu/amd: Modify set_dte_entry() to use 128-bit cmpxchg operation

The current implementation does not follow the 128-bit write requirement to update DTE as specified in the AMD I/O Virtualization Techonology (IOMMU) Specification.

In addition, the function is used to program several DTE fields (e.g. stage1 table, stage2 table, domain id, and etc.), which is difficult to keep track with current implementation.

Therefore, introduce new a new dte256_t data type and a helper function update_dte_256(), which uses two try_cmpxchg128 operations to update 256-bit DTE.

Also, separate logic for setting up the GCR3 Table Root Pointer, GIOV, GV, GLX, and GuestPagingMode into another helper function set_dte_gcr3_table().

---
 drivers/iommu/amd/amd_iommu_types.h |  17 ++++
 drivers/iommu/amd/iommu.c           | 143 +++++++++++++++++-----------
 2 files changed, 107 insertions(+), 53 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +17 -0
index c9f9a598eb82..295138447476 100644
@@ -886,6 +886,23 @@ struct dev_table_entry {
 	u64 data[4];
 };
 
+struct dte256 {
+	union {
+		struct {
+			u64 lo;
+			u64 hi;
+		};
+		u128 data;
+	} qw_lo;
+	union {
+		struct {
+			u64 lo;
+			u64 hi;
+		};
+		u128 data;
+	} qw_hi;
+};
+
 /*
  * One entry for unity mappings parsed out of the ACPI table.
  */
drivers/iommu/amd/iommu.c +90 -53

180 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw
Quoted 1 line · Suravee Suthikulpanit

+struct dte256 {

[..]

This would be alot better as just

struct dte {

 union {
     u64 data[4];
     u128 data128[2];

};
};

And don't make a new type. IIRC the cmpxchg likes to have alignment and the u128 provides that..

Quoted 9 lines
drivers/iommu/amd/iommu.c +9 -0
+static void update_dte256(struct amd_iommu *iommu, u16 devid, struct dte256 *new)
+{
+	struct dev_table_entry *dev_table = get_dev_table(iommu);
+	struct dte256 *ptr = (struct dte256 *)&dev_table[devid];
+	struct dte256 old = {
+		.qw_lo.data = ptr->qw_lo.data,
+		.qw_hi.data = ptr->qw_hi.data,
+	};
+

drivers/iommu/amd/iommu.c int amd_iommu_clear_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid) · line 1915

Quoted 10 lines
drivers/iommu/amd/iommu.c +10 -0
+	/* Update qw_lo */
+	if (!try_cmpxchg128(&ptr->qw_lo.data, &old.qw_lo.data, new->qw_lo.data))
+		goto err_out;
+
+	/* Update qw_hi */
+	if (!try_cmpxchg128(&ptr->qw_hi.data, &old.qw_hi.data, new->qw_hi.data)) {
+		/* Restore qw_lo */
+		try_cmpxchg128(&ptr->qw_lo.data, &new->qw_lo.data, old.qw_lo.data);
+		goto err_out;
+	}

I don't think this is going to work like this, the interrupt remapping code can asynchronously change the values (it is an existing race bug) and if it races that will cause these to fail too, and this doesn't handle that case at all.

IMHO the simple solution would hold the spinlock and transfer the interrupt remapping fields then directly write them without a failable cmpxchg. Maybe you could also use a cmpxchg loop to transfer the interrupt remapping bits without a lock..

Can you assume cmpxchg 128 is available on all CPUs that have the iommu? I see google says some early AMD 64 bit CPUs don't have it? Do they have iommu's? I had wondered if the doc intended that some 128 bit SSE/MMX store store would be used here??

If cmpxchg128 is safe checking that cap and refusing to probe the iommu driver would be appropriate. Otherwise a 64 bit flow still has to work?

Finally, the order of programming the two 128s depends on what you are programming. The first 128 contains the valid bit so you shouldn't be writing the second 128 after, and vice versa. This will make undesirable races around Guest Paging Mode and eventually viommuen at least.

If you have to solve the 64 bit case too, then you really should use the programmer from SMMUv3. Everyone seems to need this logic and it should be generalized. I can help you do it.. I have a patch to make that logic use 128 bit stores too someplace.

Otherwise you can possibly open code the 128 bit path with some more care to check the valid bits and set the order, plus the interrupt remapping locking.

Jason

author reviewer
#2 lore raw

Hi Jason,

Quoted 9 lines · Jason Gunthorpe
Quoted 1 line · Suravee Suthikulpanit

+struct dte256 {

[..]

This would be alot better as just

struct dte {

Quoted 8 lines
  union {
      u64 data[4];
      u128 data128[2];

};
};

And don't make a new type. IIRC the cmpxchg likes to have alignment and the u128 provides that..

OK.

Quoted 25 lines
Quoted 9 lines
drivers/iommu/amd/iommu.c +9 -0
+static void update_dte256(struct amd_iommu *iommu, u16 devid, struct dte256 *new)
+{
+	struct dev_table_entry *dev_table = get_dev_table(iommu);
+	struct dte256 *ptr = (struct dte256 *)&dev_table[devid];
+	struct dte256 old = {
+		.qw_lo.data = ptr->qw_lo.data,
+		.qw_hi.data = ptr->qw_hi.data,
+	};
+

drivers/iommu/amd/iommu.c int amd_iommu_clear_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid) · line 1915

Quoted 10 lines
drivers/iommu/amd/iommu.c +10 -0
+	/* Update qw_lo */
+	if (!try_cmpxchg128(&ptr->qw_lo.data, &old.qw_lo.data, new->qw_lo.data))
+		goto err_out;
+
+	/* Update qw_hi */
+	if (!try_cmpxchg128(&ptr->qw_hi.data, &old.qw_hi.data, new->qw_hi.data)) {
+		/* Restore qw_lo */
+		try_cmpxchg128(&ptr->qw_lo.data, &new->qw_lo.data, old.qw_lo.data);
+		goto err_out;
+	}

I don't think this is going to work like this, the interrupt remapping code can asynchronously change the values (it is an existing race bug) and if it races that will cause these to fail too, and this doesn't handle that case at all.

Quoted 4 lines

IMHO the simple solution would hold the spinlock and transfer the interrupt remapping fields then directly write them without a failable cmpxchg. Maybe you could also use a cmpxchg loop to transfer the interrupt remapping bits without a lock..

OK, I'll as synchronization when updating DTE.

Quoted 8 lines

Can you assume cmpxchg 128 is available on all CPUs that have the iommu? I see google says some early AMD 64 bit CPUs don't have it? Do they have iommu's? I had wondered if the doc intended that some 128 bit SSE/MMX store store would be used here??

If cmpxchg128 is safe checking that cap and refusing to probe the iommu driver would be appropriate. Otherwise a 64 bit flow still has to work?

According to the AMD BIOS and Kernel Developer's Guide (BDKG) dated back since family 10h Processor (https://www.amd.com/content/dam/amd/en/documents/archived-tech-docs/programmer-references/31116.pdf), which is the first introduction of AMD IOMMU, AMD processor always support for CPUID Fn0000_0001_ECX[CMPXCHG16B]=1.

So, it should be safe to assume that cmpxchg128 is available with all AMD processor w/ IOMMU.

Quoted 5 lines

Finally, the order of programming the two 128s depends on what you are programming. The first 128 contains the valid bit so you shouldn't be writing the second 128 after, and vice versa. This will make undesirable races around Guest Paging Mode and eventually viommuen at least.

Ok, I'll program the higher 128-bit first.

Quoted 8 lines

If you have to solve the 64 bit case too, then you really should use the programmer from SMMUv3. Everyone seems to need this logic and it should be generalized. I can help you do it.. I have a patch to make that logic use 128 bit stores too someplace.

Otherwise you can possibly open code the 128 bit path with some more care to check the valid bits and set the order, plus the interrupt remapping locking.

I think the cmpxchg128 should still work and would be a simpler solution. I am sending out v2 shortly.

Thanks,
Suravee

#3 lore raw
In reply to Suravee Suthikulpanit
Quoted 16 lines · Suravee Suthikulpanit

The current implementation does not follow the 128-bit write requirement to update DTE as specified in the AMD I/O Virtualization Techonology (IOMMU) Specification.

In addition, the function is used to program several DTE fields (e.g. stage1 table, stage2 table, domain id, and etc.), which is difficult to keep track with current implementation.

Therefore, introduce new a new dte256_t data type and a helper function update_dte_256(), which uses two try_cmpxchg128 operations to update 256-bit DTE.

Also, separate logic for setting up the GCR3 Table Root Pointer, GIOV, GV, GLX, and GuestPagingMode into another helper function set_dte_gcr3_table().

[...]

Quoted 7 lines
drivers/iommu/amd/iommu.c +7 -0
+static void update_dte256(struct amd_iommu *iommu, u16 devid, struct dte256 *new)
+{
+	struct dev_table_entry *dev_table = get_dev_table(iommu);
+	struct dte256 *ptr = (struct dte256 *)&dev_table[devid];
+	struct dte256 old = {
+		.qw_lo.data = ptr->qw_lo.data,
+		.qw_hi.data = ptr->qw_hi.data,

drivers/iommu/amd/iommu.c int amd_iommu_clear_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid) · line 1913

Quoted 10 lines
drivers/iommu/amd/iommu.c +10 -0
+	};
+
+	/* Update qw_lo */
+	if (!try_cmpxchg128(&ptr->qw_lo.data, &old.qw_lo.data, new->qw_lo.data))
+		goto err_out;
+
+	/* Update qw_hi */
+	if (!try_cmpxchg128(&ptr->qw_hi.data, &old.qw_hi.data, new->qw_hi.data)) {
+		/* Restore qw_lo */
+		try_cmpxchg128(&ptr->qw_lo.data, &new->qw_lo.data, old.qw_lo.data);

You should use plain cmpxchg128() when the result is unused.

Uros.

author reviewer
#4 lore raw
Quoted 46 lines · Uros Bizjak
Quoted 16 lines · Suravee Suthikulpanit

The current implementation does not follow the 128-bit write requirement to update DTE as specified in the AMD I/O Virtualization Techonology (IOMMU) Specification.

In addition, the function is used to program several DTE fields (e.g. stage1 table, stage2 table, domain id, and etc.), which is difficult to keep track with current implementation.

Therefore, introduce new a new dte256_t data type and a helper function update_dte_256(), which uses two try_cmpxchg128 operations to update 256-bit DTE.

Also, separate logic for setting up the GCR3 Table Root Pointer, GIOV, GV, GLX, and GuestPagingMode into another helper function set_dte_gcr3_table().

[...]

Quoted 13 lines

+static void update_dte256(struct amd_iommu *iommu, u16 devid, struct dte256 *new)

drivers/iommu/amd/iommu.c +10 -0
+{
+    struct dev_table_entry *dev_table = get_dev_table(iommu);
+    struct dte256 *ptr = (struct dte256 *)&dev_table[devid];
+    struct dte256 old = {
+        .qw_lo.data = ptr->qw_lo.data,
+        .qw_hi.data = ptr->qw_hi.data,
+    };
+
+    /* Update qw_lo */
+    if (!try_cmpxchg128(&ptr->qw_lo.data, &old.qw_lo.data, 

new->qw_lo.data))

Quoted 8 lines

+        goto err_out;
+
+    /* Update qw_hi */
+    if (!try_cmpxchg128(&ptr->qw_hi.data, &old.qw_hi.data, new->qw_hi.data)) {
+        /* Restore qw_lo */
+        try_cmpxchg128(&ptr->qw_lo.data, &new->qw_lo.data, old.qw_lo.data);

You should use plain cmpxchg128() when the result is unused.

Quoted 1 line

Uros.

I will update in v2.

Thanks,
Suravee