Skip to contentSkip to conversation

iommu: Enable per-device SSID space for SVA

RFC PATCH In review Joonwon Kang 5 participants 19 messages Apr 24 – May 14

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

RFC PATCH 1/1 iommu: Enable per-device SSID space for SVA

For SVA, the IOMMU core always allocates PASID from the global PASID space. The use of this global PASID space comes from the limitation of the ENQCMD instruction in Intel CPUs that it fetches its PASID operand from IA32_PASID, which is per-task.

Due to this nature, SVA with ARM SMMU v3 has been found not working in our environment when other modules/devices compete for PASID. The environment looks as follows:

- The device is not a PCIe device.
- The device is to use SVA.
- The supported SSID/PASID space is very small for the device; only 1 to 3 SSIDs are supported.
- There is a custom way of transmitting the SSID from the kernel to the device.

With this setup, when other modules have allocated all the PASIDs that our device is expected to use from the global PASID space via APIs like iommu_alloc_global_pasid() or iommu_sva_bind_device(), SVA binding to our device fails due to the lack of available PASIDs.

Since SSID/PASID is supported per-SID in ARM SMMU v3, this commit leverages the fact and lifts the use of the global PASID space if possible. What it does includes:

- Introduce a new IOMMU capability IOMMU_CAP_PER_DEV_PASID_SPACE, which represents whether the IOMMU supports an independent PASID space per- device, not shared across devices. ARM SMMU v3 is the case.
- Open a new API iommu_attach_device_pasid_any() to allocate any available PASID and attach an IOMMU domain to it.
- Opt out the use of the global PASID space for SVA if the IOMMU has that capability, and use the new API to allocate a PASID in that case.

---
v1: Request comments for this approach, other possible approaches and/or
other aspects to consider more. Code is not sanitized and commits are not separated appropriately in this version.

 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c |  2 +
 drivers/iommu/iommu-sva.c                   | 44 +++++++----
 drivers/iommu/iommu.c                       | 85 ++++++++++++++++++++-
 include/linux/iommu.h                       |  5 ++
 4 files changed, 121 insertions(+), 15 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -0
index 4d00d796f078..3a700ab0b5c7 100644
@@ -2494,6 +2494,8 @@ static bool arm_smmu_capable(struct device *dev, enum iommu_cap cap)
 		return true;
 	case IOMMU_CAP_DIRTY_TRACKING:
 		return arm_smmu_dbm_capable(master->smmu);
+	case IOMMU_CAP_PER_DEV_PASID_SPACE:
+		return true;
 	default:
 		return false;
 	}
drivers/iommu/iommu-sva.c +30 -14

75 lines not on this page · raw message

drivers/iommu/iommu.c +84 -1

97 lines not on this page · raw message

include/linux/iommu.h +5 -0
index 54b8b48c762e..1665f9fe1d8a 100644
@@ -271,6 +271,7 @@ enum iommu_cap {
 	 */
 	IOMMU_CAP_DEFERRED_FLUSH,
 	IOMMU_CAP_DIRTY_TRACKING,	/* IOMMU supports dirty tracking */
+	IOMMU_CAP_PER_DEV_PASID_SPACE,	/* IOMMU supports per-device PASID space */
 };
 
 /* These are the possible reserved region types */
@@ -1136,6 +1137,7 @@ struct iommu_sva {
 
 struct iommu_mm_data {
 	u32			pasid;
+	bool			pasid_global;
 	struct mm_struct	*mm;
 	struct list_head	sva_domains;
 	struct list_head	mm_list_elm;
@@ -1184,6 +1186,9 @@ void iommu_device_release_dma_owner(struct device *dev);
 int iommu_attach_device_pasid(struct iommu_domain *domain,
 			      struct device *dev, ioasid_t pasid,
 			      struct iommu_attach_handle *handle);
+int iommu_attach_device_pasid_any(struct iommu_domain *domain,
+				  struct device *dev, ioasid_t *pasid,
+				  struct iommu_attach_handle *handle);
 void iommu_detach_device_pasid(struct iommu_domain *domain,
 			       struct device *dev, ioasid_t pasid);
 ioasid_t iommu_alloc_global_pasid(struct device *dev);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#1 lore raw
Quoted 4 lines · Joonwon Kang

For SVA, the IOMMU core always allocates PASID from the global PASID space. The use of this global PASID space comes from the limitation of the ENQCMD instruction in Intel CPUs that it fetches its PASID operand from IA32_PASID, which is per-task.

That's right, and all the iommu drivers should have no issue with per-device pasid or they are not following the API contract.. I believe that has been taking care of already.

So, I don't think this is an iommu driver capability.

Instead, you have to decide if the PASID is per device or not based on if the system will use ENQCMD or any similar instruction. I understand ARM has introduced a similar instruction.

So you may be better off with some kind of 'arch has enqcmd like instruction' to control this instead of involving the iommu driver.

Quoted 4 lines

- The device is not a PCIe device.
- The device is to use SVA.
- The supported SSID/PASID space is very small for the device; only 1 to 3 SSIDs are supported.

Yuk

Quoted 4 lines

With this setup, when other modules have allocated all the PASIDs that our device is expected to use from the global PASID space via APIs like iommu_alloc_global_pasid() or iommu_sva_bind_device(), SVA binding to our device fails due to the lack of available PASIDs.

So you have multiple SVA using devices as well? Or multiple instances of the same device?

Jason

#2 lore raw
Quoted 13 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, April 24, 2026 9:40 PM

Quoted 4 lines · Joonwon Kang

For SVA, the IOMMU core always allocates PASID from the global PASID space. The use of this global PASID space comes from the limitation of the ENQCMD instruction in Intel CPUs that it fetches its PASID operand from IA32_PASID, which is per-task.

That's right, and all the iommu drivers should have no issue with per-device pasid or they are not following the API contract.. I believe that has been taking care of already.

Quoted 9 lines

So, I don't think this is an iommu driver capability.

Instead, you have to decide if the PASID is per device or not based on if the system will use ENQCMD or any similar instruction. I understand ARM has introduced a similar instruction.

So you may be better off with some kind of 'arch has enqcmd like instruction' to control this instead of involving the iommu driver.

if both arch and device support enqcmd-like insn...

I recalled this was discussed years ago. For devices like this, just let driver manage its own pasid space then have a new interface e.g. iommu_sva_bind_device_pasid(dev, mm, pasid) to use the specified pasid.

#3 lore raw
Quoted 6 lines · Kevin

if both arch and device support enqcmd-like insn...

I recalled this was discussed years ago. For devices like this, just let driver manage its own pasid space then have a new interface e.g. iommu_sva_bind_device_pasid(dev, mm, pasid) to use the specified pasid.

Yeah, that makes sense. If the driver knows it doesn't use an ENQCMD like programming model at all then it can use this API and it should also avoid programming the MSRs/etc.

Jason

#4 lore raw
In reply to Jason Gunthorpe

Hi Jason, thank you for your review and sorry for the late reply.

Quoted 10 lines · Joonwon Kang
Quoted 4 lines

For SVA, the IOMMU core always allocates PASID from the global PASID space. The use of this global PASID space comes from the limitation of the ENQCMD instruction in Intel CPUs that it fetches its PASID operand from IA32_PASID, which is per-task.

That's right, and all the iommu drivers should have no issue with per-device pasid or they are not following the API contract.. I believe that has been taking care of already.

Thanks for this info that every IOMMU should support per-device PASID space already, i.e. each device behind the IOMMU can have its own PASID space.

Let me clarify my understanding first to prevent future confusion.

The reason of using the global PASID space in the first place, i.e. iommu_global_pasid_ida, is to support the case where a userspace driver wants to communicate with multiple devices using the ENQCMD instruction without kernel's intervention. Since the ENQCMD instruction fetches PASID from the per-process IA32_PASID, the userspace driver could not use a different PASID for each device. If a syscall had been provided to change the process' current PASID, however, we might have been able to get rid of the use of the global PASID space, although it may cause other issues and require research on feasibility and effectiveness.

Please let me know if there is any other reason of the global PASID space use that the team considered back then.

Quoted 6 lines

So, I don't think this is an iommu driver capability.

Instead, you have to decide if the PASID is per device or not based on if the system will use ENQCMD or any similar instruction. I understand ARM has introduced a similar instruction.

By "similar instruction" on ARM, I guess you mean ST64BV0, which fetches the bottom 32 bits data from ACCDATA_EL1. Please let me know if you meant others as it will matter. If ST64BV0 is supported on ARM, however, it would mean that ST64B and ST64BV are also supported already according to the ID_AA64ISAR1_EL1's LS64 field. The latter 2 instructions are just to atomically store whatever user wants to a memory location without referring to ACCDATA_EL1 and all the 3 instructions can be run at EL0. So, the userspace driver would have enough capability to designate arbitrary PASID as it wants via the latter 2 instructions when communicating with multiple devices.

Quoted 3 lines

So you may be better off with some kind of 'arch has enqcmd like instruction' to control this instead of involving the iommu driver.

If the above makes sense, I guess we could lift the use of the global PASID space on ARM unconditionally. What do you think?

Quoted 7 lines
Quoted 4 lines

- The device is not a PCIe device.
- The device is to use SVA.
- The supported SSID/PASID space is very small for the device; only 1 to 3 SSIDs are supported.

Yuk

Quoted 7 lines
Quoted 4 lines

With this setup, when other modules have allocated all the PASIDs that our device is expected to use from the global PASID space via APIs like iommu_alloc_global_pasid() or iommu_sva_bind_device(), SVA binding to our device fails due to the lack of available PASIDs.

So you have multiple SVA using devices as well? Or multiple instances of the same device?

We have multiple processes and a single device, those processes want to do SVA with the same device, and only one process will do SVA with the device at a time. Though, the problem occurs even when irrelevant processes allocate the PASIDs from the global PASID space for their own irrelevant purposes.

Thanks,
Joonwon Kang

#5 lore raw
Quoted 10 lines · Joonwon Kang

By "similar instruction" on ARM, I guess you mean ST64BV0, which fetches the bottom 32 bits data from ACCDATA_EL1. Please let me know if you meant others as it will matter. If ST64BV0 is supported on ARM, however, it would mean that ST64B and ST64BV are also supported already according to the ID_AA64ISAR1_EL1's LS64 field. The latter 2 instructions are just to atomically store whatever user wants to a memory location without referring to ACCDATA_EL1 and all the 3 instructions can be run at EL0. So, the userspace driver would have enough capability to designate arbitrary PASID as it wants via the latter 2 instructions when communicating with multiple devices.

IDK exactly what ARM did. IIRC on Intel ENQCMD forms a special non-posted write TLP and the device can tell the TLP came from ENQCMD and so it trusts the encoded PASID. ARM has to have done the same thing - allowing anyone to forge the PASID by using a different instruction misses the point of the Intel design.

Honestly, I'm not sure why they even implemented it. SMMUv3 can't do the translation scheme required to use ENQCMD from a VM anyhow, so it is pretty useless.

Quoted 5 lines

We have multiple processes and a single device, those processes want to do SVA with the same device, and only one process will do SVA with the device at a time. Though, the problem occurs even when irrelevant processes allocate the PASIDs from the global PASID space for their own irrelevant purposes.

The only way to allocate a PASID from the global PASID space is to establish another SVA, so you have multiple devices doing SVA?

Jason

reviewer
#6 lore raw
Quoted 13 lines · Jason Gunthorpe
Quoted 10 lines · Joonwon Kang

By "similar instruction" on ARM, I guess you mean ST64BV0, which fetches the bottom 32 bits data from ACCDATA_EL1. Please let me know if you meant others as it will matter. If ST64BV0 is supported on ARM, however, it would mean that ST64B and ST64BV are also supported already according to the ID_AA64ISAR1_EL1's LS64 field. The latter 2 instructions are just to atomically store whatever user wants to a memory location without referring to ACCDATA_EL1 and all the 3 instructions can be run at EL0. So, the userspace driver would have enough capability to designate arbitrary PASID as it wants via the latter 2 instructions when communicating with multiple devices.

Quoted 5 lines

IDK exactly what ARM did. IIRC on Intel ENQCMD forms a special non-posted write TLP and the device can tell the TLP came from ENQCMD and so it trusts the encoded PASID. ARM has to have done the same thing - allowing anyone to forge the PASID by using a different instruction misses the point of the Intel design.

Yes, ACCDATA_EL1 is a privileged register neither writeable nor readable by userspace[1], so it should be functionally equivalent from an SVA point of view.

Quoted 3 lines

Honestly, I'm not sure why they even implemented it. SMMUv3 can't do the translation scheme required to use ENQCMD from a VM anyhow, so it is pretty useless.

Not sure what you mean there - indeed you can't do the SIOV thing of assigning individual ADIs to _different_ VMs, but there's still no reason you couldn't give the whole accelerator device to one VM, and run the "full" kernel driver in that VM to hand out ADIs to processes, same as for non-virtualised ST64BV0/ENQCMD usage. It's entirely usable, just not so "scalable".

Thanks,
Robin.

[1] https://developer.arm.com/documentation/ddi0601/2026-03/AArch64-Registers/ACCDATA-EL1--Accelerator-Data

Quoted 11 lines
Quoted 6 lines

We have multiple processes and a single device, those processes want to do SVA with the same device, and only one process will do SVA with the device at a time. Though, the problem occurs even when irrelevant processes allocate the PASIDs from the global PASID space for their own irrelevant purposes.

The only way to allocate a PASID from the global PASID space is to establish another SVA, so you have multiple devices doing SVA?

Jason

#7 lore raw
Quoted 20 lines · Robin Murphy
Quoted 13 lines · Jason Gunthorpe
Quoted 10 lines · Joonwon Kang

By "similar instruction" on ARM, I guess you mean ST64BV0, which fetches the bottom 32 bits data from ACCDATA_EL1. Please let me know if you meant others as it will matter. If ST64BV0 is supported on ARM, however, it would mean that ST64B and ST64BV are also supported already according to the ID_AA64ISAR1_EL1's LS64 field. The latter 2 instructions are just to atomically store whatever user wants to a memory location without referring to ACCDATA_EL1 and all the 3 instructions can be run at EL0. So, the userspace driver would have enough capability to designate arbitrary PASID as it wants via the latter 2 instructions when communicating with multiple devices.

Quoted 5 lines

IDK exactly what ARM did. IIRC on Intel ENQCMD forms a special non-posted write TLP and the device can tell the TLP came from ENQCMD and so it trusts the encoded PASID. ARM has to have done the same thing - allowing anyone to forge the PASID by using a different instruction misses the point of the Intel design.

Quoted 3 lines

Yes, ACCDATA_EL1 is a privileged register neither writeable nor readable by userspace[1], so it should be functionally equivalent from an SVA point of view.

There is a bit more going on though, I think that is what Joonwon is mentioning by asking about ST64B and ST64BV. I *think* the answer is:

- ST64B uses a posted write
- ST64BV can be restricted so EL0 cannot execute it, it uses a non-posted write (AI tells me via EnASR)
- ST64BV0 can be used by EL0, always uses a non-posted write, and always uses ACCDATA_EL1

Which is similar to Intel. The device only processes the PASID from a non-posted write, and the CPU prevents userspace from forming non-posted writes except through ST64BV0

Quoted 10 lines
Quoted 3 lines

Honestly, I'm not sure why they even implemented it. SMMUv3 can't do the translation scheme required to use ENQCMD from a VM anyhow, so it is pretty useless.

Not sure what you mean there - indeed you can't do the SIOV thing of assigning individual ADIs to _different_ VMs, but there's still no reason you couldn't give the whole accelerator device to one VM, and run the "full" kernel driver in that VM to hand out ADIs to processes, same as for non-virtualised ST64BV0/ENQCMD usage. It's entirely usable, just not so "scalable".

Well yes, technically, but I'm not sure this is attractive in practice.

The value of ENQCMD on Intel was it can eliminate any HW side per-context state for simple HW like DMA engines, including for virtualization.

You pay for that value with some performance loss, but it can be attractive because of the universal scalability.

However complex devices don't seem to want to use it, once you have to have per-context state for any other reason the performance downsides of ENQCMD make it unappealing.

So, IDK, maybe some embedded on-chip device will find a way to make good use of it, but also I'm not aware of any adoption on x86..

Jason

#8 lore raw

Hi Jason and Robin, thanks a lot for sharing your insights! Could you help to answer the further questions below? or just let me know if it is better to use other channels for them like ARM support.

Quoted 28 lines · Robin Murphy
Quoted 20 lines · Jason Gunthorpe
Quoted 13 lines · Joonwon Kang
Quoted 11 lines

By "similar instruction" on ARM, I guess you mean ST64BV0, which fetches the bottom 32 bits data from ACCDATA_EL1. Please let me know if you meant others as it will matter. If ST64BV0 is supported on ARM, however, it would mean that ST64B and ST64BV are also supported already according to the ID_AA64ISAR1_EL1's LS64 field. The latter 2 instructions are just to atomically store whatever user wants to a memory location without referring to ACCDATA_EL1 and all the 3 instructions can be run at EL0. So, the userspace driver would have enough capability to designate arbitrary PASID as it wants via the latter 2 instructions when communicating with multiple devices.

Quoted 5 lines

IDK exactly what ARM did. IIRC on Intel ENQCMD forms a special non-posted write TLP and the device can tell the TLP came from ENQCMD and so it trusts the encoded PASID. ARM has to have done the same thing - allowing anyone to forge the PASID by using a different instruction misses the point of the Intel design.

Quoted 3 lines

Yes, ACCDATA_EL1 is a privileged register neither writeable nor readable by userspace[1], so it should be functionally equivalent from an SVA point of view.

There is a bit more going on though, I think that is what Joonwon is mentioning by asking about ST64B and ST64BV. I *think* the answer is:

Quoted 7 lines

- ST64B uses a posted write
- ST64BV can be restricted so EL0 cannot execute it, it uses a non-posted write (AI tells me via EnASR)
- ST64BV0 can be used by EL0, always uses a non-posted write, and always uses ACCDATA_EL1

Which is similar to Intel.

Ah, I missed that ST64BV is currently being trapped to EL1 while ST64B is not [1]. However, I am not sure if the trap is to disallow EL0 to use it. Can it be instead to pass the response value of the non-posted write to EL0 while using the EL0-given PASID as-is? If so, I believe EL0 still can specify arbitrary PASID as it wants via ST64BV.

Since I guess ST64B* instructions are to serve generic purposes not only for communication with accelerators with SIOV but also with any memory location or device without SIOV, I am not sure if it is always okay to make those instructions work the way Jason mentioned.

Quoted 2 lines

The device only processes the PASID from a non-posted write,

Regarding ST64B, are the ARM devices behind ARM SMMU v3 supposed to work this way too? If not, EL0 can specify arbitrary PASID via ST64B with the kernel today [1].

[1] https://github.com/torvalds/linux/blob/50897c955902c93ae71c38698abb910525ebdc89/arch/arm64/kernel/cpufeature.c#L3166-L3181

Thanks,
Joonwon Kang

#9 lore raw
Quoted 9 lines · Joonwon Kang
Quoted 9 lines

There is a bit more going on though, I think that is what Joonwon is mentioning by asking about ST64B and ST64BV. I *think* the answer is:

- ST64B uses a posted write
- ST64BV can be restricted so EL0 cannot execute it, it uses a non-posted write (AI tells me via EnASR)
- ST64BV0 can be used by EL0, always uses a non-posted write, and always uses ACCDATA_EL1

Quoted 7 lines
Quoted 1 line

Which is similar to Intel.

Ah, I missed that ST64BV is currently being trapped to EL1 while ST64B is not [1]. However, I am not sure if the trap is to disallow EL0 to use it. Can it be instead to pass the response value of the non-posted write to EL0 while using the EL0-given PASID as-is? If so, I believe EL0 still can specify arbitrary PASID as it wants via ST64BV.

I think if an OS implements things this way it is would security broken as far as ENQCMD compatible HW goes.

Quoted 4 lines

Since I guess ST64B* instructions are to serve generic purposes not only for communication with accelerators with SIOV but also with any memory location or device without SIOV, I am not sure if it is always okay to make those instructions work the way Jason mentioned.

The end point has to use the posted vs non-posted write distinction for security.

Quoted 5 lines
Quoted 1 line

The device only processes the PASID from a non-posted write,

Regarding ST64B, are the ARM devices behind ARM SMMU v3 supposed to work this way too? If not, EL0 can specify arbitrary PASID via ST64B with the kernel today [1].

If you want ENQCMD compatible semantics then yes you have to do all of these things, it is part of the security design.

Jason

reviewer
#10 lore raw
Quoted 12 lines · Jason Gunthorpe
Quoted 11 lines · Joonwon Kang
Quoted 10 lines

There is a bit more going on though, I think that is what Joonwon is mentioning by asking about ST64B and ST64BV. I *think* the answer is:

- ST64B uses a posted write
- ST64BV can be restricted so EL0 cannot execute it, it uses a non-posted write (AI tells me via EnASR)
- ST64BV0 can be used by EL0, always uses a non-posted write, and always uses ACCDATA_EL1

Which is similar to Intel.

Quoted 8 lines
Quoted 5 lines

Ah, I missed that ST64BV is currently being trapped to EL1 while ST64B is not [1]. However, I am not sure if the trap is to disallow EL0 to use it. Can it be instead to pass the response value of the non-posted write to EL0 while using the EL0-given PASID as-is? If so, I believe EL0 still can specify arbitrary PASID as it wants via ST64BV.

I think if an OS implements things this way it is would security broken as far as ENQCMD compatible HW goes.

Yes, I think it's rather the point that the EnALS/EnASR traps to EL1 allow EL1 to sanitise the data that ST64B/ST64BV are sending, and do exactly things like substituting a valid PASID. ST64BV0 offers a way of doing so _without_ needing the overhead of trapping, but conversely that needs the EnAS0 opt-in all the way down to indicate both EL1's awareness of programming ACCDATA_EL1 appropriately and EL2/3's awareness of context-switching it.

I've not looked closely at what exactly the arm64 arch code is doing today and how well it actually fits the expected ENQCMD usage model, but I can well believe it might need a bit of tweaking.

Thanks,
Robin.

Quoted 18 lines
Quoted 4 lines

Since I guess ST64B* instructions are to serve generic purposes not only for communication with accelerators with SIOV but also with any memory location or device without SIOV, I am not sure if it is always okay to make those instructions work the way Jason mentioned.

The end point has to use the posted vs non-posted write distinction for security.

Quoted 5 lines
Quoted 1 line

The device only processes the PASID from a non-posted write,

Regarding ST64B, are the ARM devices behind ARM SMMU v3 supposed to work this way too? If not, EL0 can specify arbitrary PASID via ST64B with the kernel today [1].

If you want ENQCMD compatible semantics then yes you have to do all of these things, it is part of the security design.

Jason

#11 lore raw
Quoted 30 lines · Jason Gunthorpe
Quoted 12 lines · Joonwon Kang
Quoted 11 lines
Quoted 10 lines

There is a bit more going on though, I think that is what Joonwon is mentioning by asking about ST64B and ST64BV. I *think* the answer is:

- ST64B uses a posted write
- ST64BV can be restricted so EL0 cannot execute it, it uses a non-posted write (AI tells me via EnASR)
- ST64BV0 can be used by EL0, always uses a non-posted write, and always uses ACCDATA_EL1

Which is similar to Intel.

Quoted 8 lines
Quoted 5 lines

Ah, I missed that ST64BV is currently being trapped to EL1 while ST64B is not [1]. However, I am not sure if the trap is to disallow EL0 to use it. Can it be instead to pass the response value of the non-posted write to EL0 while using the EL0-given PASID as-is? If so, I believe EL0 still can specify arbitrary PASID as it wants via ST64BV.

I think if an OS implements things this way it is would security broken as far as ENQCMD compatible HW goes.

Yes, I think it's rather the point that the EnALS/EnASR traps to EL1 allow EL1 to sanitise the data that ST64B/ST64BV are sending, and do exactly things like substituting a valid PASID. ST64BV0 offers a way of doing so _without_ needing the overhead of trapping, but conversely that needs the EnAS0 opt-in all the way down to indicate both EL1's awareness of programming ACCDATA_EL1 appropriately and EL2/3's awareness of context-switching it.

Quoted 25 lines

I've not looked closely at what exactly the arm64 arch code is doing today and how well it actually fits the expected ENQCMD usage model, but I can well believe it might need a bit of tweaking.

Thanks,
Robin.

Quoted 18 lines
Quoted 4 lines

Since I guess ST64B* instructions are to serve generic purposes not only for communication with accelerators with SIOV but also with any memory location or device without SIOV, I am not sure if it is always okay to make those instructions work the way Jason mentioned.

The end point has to use the posted vs non-posted write distinction for security.

Quoted 5 lines
Quoted 1 line

The device only processes the PASID from a non-posted write,

Regarding ST64B, are the ARM devices behind ARM SMMU v3 supposed to work this way too? If not, EL0 can specify arbitrary PASID via ST64B with the kernel today [1].

If you want ENQCMD compatible semantics then yes you have to do all of these things, it is part of the security design.

Jason

Appreciate all your clarifications here. So, my understanding is that if our system does not support ST64BV and ST64BV0 or if our device does not distinguish between the posted write and the non-posted write regarding PASID, then we can lift the use of the global PASID space. Can I say this?

If yes, I will create a new patch which checks for that case. It might as well add a new device tree property to represent if the device supports the posted write vs. non-posted write distinction.

Thanks,
Joonwon Kang

#12 lore raw
Quoted 4 lines · Joonwon Kang

Appreciate all your clarifications here. So, my understanding is that if our system does not support ST64BV and ST64BV0 or if our device does not distinguish between the posted write and the non-posted write regarding PASID, then we can lift the use of the global PASID space. Can I say this?

You should do what Robin said - just have your driver use a per-device PASID that it allocates and never use the global pasid allocator.

To do this lightly re-organize the SVA code so the driver can supply its own PASID, and in this mode we wouldn't activate the ENQCMD features in the mm.

Jason

#13 lore raw
Quoted 7 lines · Joonwon Kang
Quoted 5 lines

Appreciate all your clarifications here. So, my understanding is that if our system does not support ST64BV and ST64BV0 or if our device does not distinguish between the posted write and the non-posted write regarding PASID, then we can lift the use of the global PASID space. Can I say this?

Quoted 6 lines

You should do what Robin said - just have your driver use a per-device PASID that it allocates and never use the global pasid allocator.

To do this lightly re-organize the SVA code so the driver can supply its own PASID, and in this mode we wouldn't activate the ENQCMD features in the mm.

Ah, we could actively disallow EL0 to execute ENQCMD-like instructions when the device driver explicitly shows the intention via a new API like iommu_sva_bind_device_pasid() that Tian mentioned earlier. And the new API only uses the per-device PASID space. It makes a lot of sense. It also means that ENQCMD-like instructions are only allowed when the PASID is allocated from the global PASID space.

If a process communicates with only one device with the PASID allocated from the per-device PASID space, however, there should be no blocker for the process to execute ENQCMD-like instructions, technically speaking. In this case, should we allow the process to execute them? and later if the process tries to allocate another PASID for another device, should we disallow the instruction execution then? I guess this way may complicate the implementation without much benefit, though.

To allocate a per-device PASID, I think we should do it using dev->iommu_group->pasid_array instead of making the device driver create its own PASID set since all the devices in the same iommu_group are supposed to share the same PASID space.

Will create a new patch with the establishment so far.

Thanks,
Joonwon Kang

#14 lore raw
Quoted 10 lines · Joonwon Kang
Quoted 10 lines · Joonwon Kang
Quoted 5 lines

Appreciate all your clarifications here. So, my understanding is that if our system does not support ST64BV and ST64BV0 or if our device does not distinguish between the posted write and the non-posted write regarding PASID, then we can lift the use of the global PASID space. Can I say this?

You should do what Robin said - just have your driver use a per-device PASID that it allocates and never use the global pasid allocator.

Quoted 7 lines
Quoted 3 lines

To do this lightly re-organize the SVA code so the driver can supply its own PASID, and in this mode we wouldn't activate the ENQCMD features in the mm.

Ah, we could actively disallow EL0 to execute ENQCMD-like instructions when the device driver explicitly shows the intention via a new API like iommu_sva_bind_device_pasid() that Tian mentioned earlier.

You shouldn't need to do anything like this.

All you need is to ensure that mm_get_enqcmd_pasid() returns IOMMU_PASID_INVALID so long as a the normal iommu_sva_bind_device() hasn't been called. Once it is called it is fine to allow the ENQCMD.

Your new iommu_sva_bind_device_pasid() needs to establish the SVA and attach it without triggering mm_get_enqcmd_pasid().

The arch code is required to block the ENQCMD like instructions when IOMMU_PASID_INVALID.

Devices that can mmap an ENQCMD sensitive BAR region must not do so unless iommu_sva_bind_device() has been called.

Quoted 2 lines

To allocate a per-device PASID, I think we should do it using dev->iommu_group->pasid_array instead of making the device driver

No, make the driver manage this, don't mess with the core code. PASID isn't supported with multi-device groups already.

Jason

#15 lore raw
Quoted 25 lines · Joonwon Kang
Quoted 10 lines
Quoted 10 lines · Joonwon Kang
Quoted 5 lines

Appreciate all your clarifications here. So, my understanding is that if our system does not support ST64BV and ST64BV0 or if our device does not distinguish between the posted write and the non-posted write regarding PASID, then we can lift the use of the global PASID space. Can I say this?

You should do what Robin said - just have your driver use a per-device PASID that it allocates and never use the global pasid allocator.

Quoted 7 lines
Quoted 3 lines

To do this lightly re-organize the SVA code so the driver can supply its own PASID, and in this mode we wouldn't activate the ENQCMD features in the mm.

Ah, we could actively disallow EL0 to execute ENQCMD-like instructions when the device driver explicitly shows the intention via a new API like iommu_sva_bind_device_pasid() that Tian mentioned earlier.

You shouldn't need to do anything like this.

All you need is to ensure that mm_get_enqcmd_pasid() returns IOMMU_PASID_INVALID so long as a the normal iommu_sva_bind_device() hasn't been called. Once it is called it is fine to allow the ENQCMD.

Quoted 9 lines

Your new iommu_sva_bind_device_pasid() needs to establish the SVA and attach it without triggering mm_get_enqcmd_pasid().

The arch code is required to block the ENQCMD like instructions when IOMMU_PASID_INVALID.

Devices that can mmap an ENQCMD sensitive BAR region must not do so unless iommu_sva_bind_device() has been called.

Yes, the basic idea I meant is the same as this: use mm->iommu to deactivate the instructions. The arch code for ARM may block them later as it does not seem to support FEAT_LS64_ACCDATA for now. Thanks for the details. I think I am pretty much aligned. Let me try it.

Quoted 5 lines
Quoted 2 lines

To allocate a per-device PASID, I think we should do it using dev->iommu_group->pasid_array instead of making the device driver

No, make the driver manage this, don't mess with the core code. PASID isn't supported with multi-device groups already.

Understood. Thanks for this info.

Thanks,
Joonwon Kang

#16 lore raw
In reply to Jason Gunthorpe
Quoted 8 lines
Quoted 5 lines

We have multiple processes and a single device, those processes want to do SVA with the same device, and only one process will do SVA with the device at a time. Though, the problem occurs even when irrelevant processes allocate the PASIDs from the global PASID space for their own irrelevant purposes.

The only way to allocate a PASID from the global PASID space is to establish another SVA, so you have multiple devices doing SVA?

Another way is via iommu_alloc_global_pasid() [1] and it does not require SVA. No, we currently do not have multiple devices doing SVA. But, the problem is that it is out of our control when any device is to allocate a PASID or do SVA for its own purpose.

[1] https://lore.kernel.org/all/[email protected]/

Thanks,
Joonwon Kang

#17 lore raw
In reply to Joonwon Kang
Quoted 33 lines · Joonwon Kang

For SVA, the IOMMU core always allocates PASID from the global PASID space. The use of this global PASID space comes from the limitation of the ENQCMD instruction in Intel CPUs that it fetches its PASID operand from IA32_PASID, which is per-task.

Due to this nature, SVA with ARM SMMU v3 has been found not working in our environment when other modules/devices compete for PASID. The environment looks as follows:

- The device is not a PCIe device.
- The device is to use SVA.
- The supported SSID/PASID space is very small for the device; only 1 to 3 SSIDs are supported.
- There is a custom way of transmitting the SSID from the kernel to the device.

With this setup, when other modules have allocated all the PASIDs that our device is expected to use from the global PASID space via APIs like iommu_alloc_global_pasid() or iommu_sva_bind_device(), SVA binding to our device fails due to the lack of available PASIDs.

Since SSID/PASID is supported per-SID in ARM SMMU v3, this commit leverages the fact and lifts the use of the global PASID space if possible. What it does includes:

- Introduce a new IOMMU capability IOMMU_CAP_PER_DEV_PASID_SPACE, which represents whether the IOMMU supports an independent PASID space per- device, not shared across devices. ARM SMMU v3 is the case.
- Open a new API iommu_attach_device_pasid_any() to allocate any available PASID and attach an IOMMU domain to it.
- Opt out the use of the global PASID space for SVA if the IOMMU has that capability, and use the new API to allocate a PASID in that case.

Quoted 6 lines

---
v1: Request comments for this approach, other possible approaches and/or
other aspects to consider more. Code is not sanitized and commits are not separated appropriately in this version.

<snip>

This may be a a basic question, but how does this reconcile with the fact that the process ID space is global? Even with PID namespacing, I understand that each process in a PID namespace has a "parent" PID in the parent namespace, unless I'm grossly mistaken.

Also, with the per-device PASID space, different SVA-capable devices being used by the same process would have different PASIDs referring to the same process address space, and would break the DSA<->IAA kind of interaction where the device drivers can communicate the PASID among each other to operate on the same process address space. Is that a scenario that does not matter to your use case?

Thanks,
Easwar (he/him)

#18 lore raw
Quoted 3 lines · Easwar Hariharan

process address space, and would break the DSA<->IAA kind of interaction where the device drivers can communicate the PASID among each other to operate on the same process address space.

That is not part of the Linux model...

Each device has to get its own SVA and it must use the returned PASID, not just invent one from someplace else.

Jason