iommu/arm-smmu-v3: Add support for hitless replace of CD entries
Subsystems (5)
cover iommu/arm-smmu-v3: Add support for hitless replace of CD entries
This patch series adds support for replacing IOMMU domains with S1 page tables hitlessly, that is without disrupting the ongoing DMAs. This is one of the dependencies of the Liveupdate IOMMU support for Arm SMMUv3.
S1 domains are programmed by CD entries in the CD table. During a new domain attach these are updated using arm_smmu_write_cd_entry(). Since ASID and TTB0 are in different 64-bit words, arm_smmu_write_cd_entry() considers it a non-hitless update and sets V=0. This means such an update is disruptive and would generate translation faults if there are ongoing DMAs.
Replace the CD entries by using an unused temporary ASID in the following sequence to allow hitless replacement of CD entry,
- Update the CD entry with temporary ASID.
- Update the CD entry with the target TTB0.
- Invalidate the temporary ASID.
- Update the CD entry with target ASID.
- Invalidate and release the temporary ASID.
The series adds a vfio selftest that triggers a continuous DMA and does the hwpt replace while the DMA is ongoing. Since domain replacement is a race with the translation requests, the test does it multiple times to capture the small window where IOMMU faults can occur.
Without this change we can observe the SMMU faults in the kernel logs and with it the SMMU faults do not occur.
This is only tested on qemu with the emulated SMMUv3 and an out of tree NVMe vfio selftest driver, not on real hardware. Following are not covered and can be done later,
- S2 domains have the same problem, S2VMID is in STE qword 2 and S2TTB is in qword 3. These need the same handling with a temporary VMID.
- ATS is not tested, the test environment runs with pci=noats.
Testing:
Without Fix:
TAP version 13
1..2
# Starting 2 tests from 2 test cases.
# RUN vfio_iommufd_replace_hwpt_test.domain_replace.memcpy ...
[ 33.267749] vfio-pci 0000:00:0a.0: enabling device (0000 -> 0002) [ 33.270709] vfio-pci 0000:00:0a.0: resetting [ 33.385682] vfio-pci 0000:00:0a.0: reset done [ 37.128021] arm-smmu-v3 9050000.smmuv3: event 0x0a received: [ 37.131885] arm-smmu-v3 9050000.smmuv3: 0x000000500000000a [ 37.134585] arm-smmu-v3 9050000.smmuv3: 0x0000000000000000 [ 37.136961] arm-smmu-v3 9050000.smmuv3: 0x0000000000000000 [ 37.139631] arm-smmu-v3 9050000.smmuv3: 0x0000000000000000 [ 37.142188] arm-smmu-v3 9050000.smmuv3: event: C_BAD_CD client: 0000:00:0a.0 sid: 0x50 ssid: 0x0
...
...
With Fix:
TAP version 13
1..2
# Starting 2 tests from 2 test cases.
# RUN vfio_iommufd_replace_hwpt_test.domain_replace.memcpy ...
[ 33.418253] vfio-pci 0000:00:0a.0: enabling device (0000 -> 0002) [ 33.421218] vfio-pci 0000:00:0a.0: resetting [ 33.539750] vfio-pci 0000:00:0a.0: reset done # OK vfio_iommufd_replace_hwpt_test.domain_replace.memcpy ok 1 vfio_iommufd_replace_hwpt_test.domain_replace.memcpy # RUN vfio_iommufd_replace_hwpt_test.noreplace.memcpy ... [ 88.416453] vfio-pci 0000:00:0a.0: resetting [ 88.531989] vfio-pci 0000:00:0a.0: reset done # OK vfio_iommufd_replace_hwpt_test.noreplace.memcpy ok 2 vfio_iommufd_replace_hwpt_test.noreplace.memcpy
Looking forward to your feedback on this.
Dependencies:
The patch series includes a patch from the vfio selftest multi iommu series as it is required for the hwpt_replace vfio selftest. It is part of this series since the original code doesn't apply cleanly on the base.
https://lore.kernel.org/all/[email protected]/
Related Work:
Liveupdate IOMMU patch series:
https://lore.kernel.org/all/[email protected]/
Liveupdate IOMMU feature patchset breakdown:
https://docs.google.com/document/d/1enDn-uPE9U77U-xHEnzn6HHGKiePSAtMIP8EDU3NO0M
Samiullah Khawaja (3):
vfio: selftests: Add support of creating iommus from iommufd
vfio: selftests: Add iommufd hwpt replace test iommu/arm-smmu-v3: Add support for hitless replace of S1 domains
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 66 ++++++- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/lib/include/libvfio/iommu.h | 2 + .../lib/include/libvfio/vfio_pci_device.h | 2 + tools/testing/selftests/vfio/lib/iommu.c | 60 +++++- .../selftests/vfio/lib/vfio_pci_device.c | 25 ++- .../vfio/vfio_iommufd_hwpt_replace_test.c | 171 ++++++++++++++++++ 7 files changed, 316 insertions(+), 11 deletions(-) create mode 100644 tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c
base-commit: 3d7783543c2646af69ad65825e810060494bea21
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 1/3 vfio: selftests: Add support of creating iommus from iommufd
Add API to init a struct iommu using an already opened iommufd instance and attach devices to it.
--- .../vfio/lib/include/libvfio/iommu.h | 2 + .../lib/include/libvfio/vfio_pci_device.h | 2 + tools/testing/selftests/vfio/lib/iommu.c | 60 +++++++++++++++++-- .../selftests/vfio/lib/vfio_pci_device.c | 25 ++++++-- 4 files changed, 80 insertions(+), 9 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +2 -0
@@ -29,10 +29,12 @@ struct iommu {int container_fd;int iommufd;u32 ioas_id; +u32 hwpt_id;struct list_head dma_regions; };struct iommu *iommu_init (const char *iommu_mode); +struct iommu *iommufd_iommu_init (int iommufd,u32 dev_id);void iommu_cleanup (struct iommu *iommu);int __iommu_map (struct iommu *iommu,struct dma_region *region);
tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +2 -0
@@ -19,6 +19,7 @@ struct vfio_pci_device {const char *bdf;int fd;int group_fd; +u32 dev_id;struct iommu *iommu;
@@ -82,6 +83,7 @@ static inline void vfio_pci_cmd_clear(struct vfio_pci_device *device, u16 bits)vfio_pci_config_writew (device,PCI_COMMAND , cmd & ~bits); } +void vfio_pci_device_attach_iommu (struct vfio_pci_device *device,struct iommu *iommu);void vfio_pci_irq_enable (struct vfio_pci_device *device,u32 index,u32 vector,int count);void vfio_pci_irq_disable (struct vfio_pci_device *device,u32 index);
tools/testing/selftests/vfio/lib/iommu.c +56 -4
89 lines not on this page · raw message
tools/testing/selftests/vfio/lib/vfio_pci_device.c +20 -5
@@ -469,15 +469,15 @@ int __vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_token)if (ioctl (device_fd,VFIO_DEVICE_BIND_IOMMUFD , &args))return -errno; -return 0 ; +return args.out_devid; } -static void vfio_device_bind_iommufd (int device_fd,int iommufd, -const char *vf_token) +int vfio_device_bind_iommufd (int device_fd,int iommufd,const char *vf_token) {int ret =__vfio_device_bind_iommufd (device_fd, iommufd, vf_token); -VFIO_ASSERT_EQ (ret,0 ,"Failed VFIO_DEVICE_BIND_IOMMUFD ioctl\n" ); +VFIO_ASSERT_GE (ret,0 ,"Failed VFIO_DEVICE_BIND_IOMMUFD ioctl\n" ); +return ret; }static void vfio_device_attach_iommufd_pt (int device_fd,u32 pt_id)
@@ -503,10 +503,25 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *bdf, const char *vf_token) {vfio_pci_cdev_open (device ,bdf ); -vfio_device_bind_iommufd (device->fd, device->iommu->iommufd, vf_token); + device->dev_id =vfio_device_bind_iommufd (device->fd, device->iommu->iommufd, vf_token);vfio_device_attach_iommufd_pt (device->fd, device->iommu->ioas_id); } +void vfio_pci_device_attach_iommu (struct vfio_pci_device *device,struct iommu *iommu) +{ +u32 pt_id = iommu->ioas_id; + +/* Only iommufd supports changing struct iommu attachments */ +VFIO_ASSERT_TRUE (iommu->iommufd); + +if (iommu->hwpt_id) + pt_id = iommu->hwpt_id; + +VFIO_ASSERT_NE (pt_id,0 ); +vfio_device_attach_iommufd_pt (device->fd, pt_id); + device->iommu = iommu; +} +struct vfio_pci_device *vfio_pci_device_alloc (const char *bdf,struct iommu *iommu) {struct vfio_pci_device *device;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
David Matlack
maintainerRe: [RFC PATCH 1/3] vfio: selftests: Add support of creating iommus from iommufd
Quoted 101 lines · Samiullah Khawaja
Add API to init a struct iommu using an already opened iommufd instance and attach devices to it.
Signed-off-by Samiullah Khawaja <[email protected]>--- .../vfio/lib/include/libvfio/iommu.h | 2 + .../lib/include/libvfio/vfio_pci_device.h | 2 + tools/testing/selftests/vfio/lib/iommu.c | 60 +++++++++++++++++-- .../selftests/vfio/lib/vfio_pci_device.c | 25 ++++++-- 4 files changed, 80 insertions(+), 9 deletions(-)tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +2
-0@@ -29,10 +29,12 @@ struct iommu {int container_fd;int iommufd;u32 ioas_id; +u32 hwpt_id;struct list_head dma_regions; };struct iommu *iommu_init (const char *iommu_mode); +struct iommu *iommufd_iommu_init (int iommufd,u32 dev_id);void iommu_cleanup (struct iommu *iommu);int __iommu_map (struct iommu *iommu,struct dma_region *region);tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +2
-0@@ -19,6 +19,7 @@ struct vfio_pci_device {const char *bdf;int fd;int group_fd; +u32 dev_id;struct iommu *iommu;@@ -82,6 +83,7 @@ static inline void vfio_pci_cmd_clear(struct vfio_pci_device *device, u16 bits)vfio_pci_config_writew (device,PCI_COMMAND , cmd & ~bits); } +void vfio_pci_device_attach_iommu (struct vfio_pci_device *device,struct iommu *iommu);void vfio_pci_irq_enable (struct vfio_pci_device *device,u32 index,u32 vector,int count);void vfio_pci_irq_disable (struct vfio_pci_device *device,u32 index);tools/testing/selftests/vfio/lib/iommu.c +23
-3@@ -405,6 +405,18 @@ struct iommu_iova_range *iommu_iova_ranges(struct iommu *iommu, u32 *nranges)return ranges; } +static u32 iommufd_hwpt_alloc (struct iommu *iommu,u32 dev_id) +{ +struct iommu_hwpt_alloc args = { + .size =sizeof (args), + .pt_id = iommu->ioas_id, + .dev_id = dev_id, + }; + +ioctl_assert (iommu->iommufd,IOMMU_HWPT_ALLOC , &args); +return args.out_hwpt_id; +} +static u32 iommufd_ioas_alloc (int iommufd) {struct iommu_ioas_alloc args = {@@ -415,17 +427,25 @@ static u32 iommufd_ioas_alloc(int iommufd)return args.out_ioas_id; } -struct iommu *iommu_init (const char *iommu_mode) +static struct iommu *iommu_alloc (const char *iommu_mode) { -const char *container_path;struct iommu *iommu; -int version; iommu =calloc_assert (1 ,sizeof (*iommu));INIT_LIST_HEAD (&iommu->dma_regions); iommu->mode =lookup_iommu_mode (iommu_mode); +return iommu; +} + +struct iommu *iommu_init (const char *iommu_mode) +{ +const char *container_path; +struct iommu *iommu; +int version; + + iommu =iommu_alloc (iommu_mode); container_path = iommu->mode->container_path;if (container_path) {
tools/testing/selftests/vfio/lib/iommu.c struct iommu *iommu_init(const char *iommu_mode) · line 472
Quoted 4 lines
tools/testing/selftests/vfio/lib/iommu.c +1
-0@@ -449,10 +469,42 @@ struct iommu *iommu_init(const char *iommu_mode)return iommu; } +struct iommu *iommufd_iommu_init (int iommufd,u32 dev_id)
I think this is a good opportunity to start adding kernel-doc comments. It's not very obvious how and when to use both iommufd_iommu_init() and vfio_pci_device_attach_iommu(). A kernel-doc comment would help clarify that.
Quoted 17 lines
tools/testing/selftests/vfio/lib/iommu.c +17
-0+{ +struct iommu *iommu; + + iommu =iommu_alloc ("iommufd" ); + + iommu->iommufd =dup (iommufd); +VFIO_ASSERT_GT (iommu->iommufd,0 ); + + iommu->ioas_id =iommufd_ioas_alloc (iommu->iommufd); + iommu->hwpt_id =iommufd_hwpt_alloc (iommu, dev_id); + +return iommu; +} + +static void iommufd_cleanup (struct iommu *iommu) +{ +struct iommu_destroy args = {
tools/testing/selftests/vfio/lib/iommu.c struct iommu *iommu_init(const char *iommu_mode) · line 499
Quoted 10 lines
tools/testing/selftests/vfio/lib/iommu.c +10
-0+ .size = sizeof(args), + }; + +if (iommu->hwpt_id) { + args.id = iommu->hwpt_id; +ioctl_assert (iommu->iommufd,IOMMU_DESTROY , &args); + } + + args.id = iommu->ioas_id; +ioctl_assert (iommu->iommufd,IOMMU_DESTROY , &args);
Please put IOMMU_DESTROY into its own helper so this can be:
if (iommu->hwpt_id)
iommufd_iommu_destroy(iommu, iommu->hwpt_id);
iommufd_iommu_destroy(iommu, iommu->ioas_id);
Quoted 17 lines
tools/testing/selftests/vfio/lib/iommu.c +5
-1+ +VFIO_ASSERT_EQ (close (iommu->iommufd),0 ); +} +void iommu_cleanup (struct iommu *iommu) {if (iommu->iommufd) -VFIO_ASSERT_EQ (close (iommu->iommufd),0 ); +iommufd_cleanup (iommu);else VFIO_ASSERT_EQ (close (iommu->container_fd),0 );tools/testing/selftests/vfio/lib/vfio_pci_device.c +0
-0
tools/testing/selftests/vfio/lib/vfio_pci_device.c int __vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_token) · line 472
Quoted 5 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +1
-1@@ -469,15 +469,15 @@ int __vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_token)if (ioctl (device_fd,VFIO_DEVICE_BIND_IOMMUFD , &args))return -errno; -return 0 ; +return args.out_devid;
This breaks vfio_pci_sriov_uapi_test for all iommufd variants (9/45 fail). iommufd_setup() in that test returns the result of __vfio_device_bind_iommufd() directly, and the fixture/ASSERT_COND_VF_CREATION() treat any non-zero value as failure.
Something like the following is needed to fix it:
tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c +0 -0
@@ -32,9 +32,14 @@ static int container_setup(struct vfio_pci_device *device, const char *bdf, static int iommufd_setup(struct vfio_pci_device *device, const char *bdf,
const char *vf_token)
{
+ int ret;
+
vfio_pci_cdev_open(device, bdf);
- return __vfio_device_bind_iommufd(device->fd,
- device->iommu->iommufd, vf_token);
+ ret = __vfio_device_bind_iommufd(device->fd,
+ device->iommu->iommufd, vf_token);
+
+ /* On success, __vfio_device_bind_iommufd() returns the device ID. */
+ return ret < 0 ? ret : 0;
}
static int device_init(const char *bdf, struct iommu *iommu,
Quoted 24 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +5
-4} -static void vfio_device_bind_iommufd (int device_fd,int iommufd, -const char *vf_token) +int vfio_device_bind_iommufd (int device_fd,int iommufd,const char *vf_token) {int ret =__vfio_device_bind_iommufd (device_fd, iommufd, vf_token); -VFIO_ASSERT_EQ (ret,0 ,"Failed VFIO_DEVICE_BIND_IOMMUFD ioctl\n" ); +VFIO_ASSERT_GE (ret,0 ,"Failed VFIO_DEVICE_BIND_IOMMUFD ioctl\n" ); +return ret; }static void vfio_device_attach_iommufd_pt (int device_fd,u32 pt_id)@@ -503,10 +503,25 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *bdf, const char *vf_token) {vfio_pci_cdev_open (device ,bdf ); -vfio_device_bind_iommufd (device->fd, device->iommu->iommufd, vf_token); + device->dev_id =vfio_device_bind_iommufd (device->fd, device->iommu->iommufd, vf_token);vfio_device_attach_iommufd_pt (device->fd, device->iommu->ioas_id); } +void vfio_pci_device_attach_iommu (struct vfio_pci_device *device,struct iommu *iommu)
tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, · line 520
Quoted 10 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +10
-0+{ +u32 pt_id = iommu->ioas_id; + +/* Only iommufd supports changing struct iommu attachments */ +VFIO_ASSERT_TRUE (iommu->iommufd); + +if (iommu->hwpt_id) + pt_id = iommu->hwpt_id; + +VFIO_ASSERT_NE (pt_id,0 );
Is this assert needed? (Will vfio_device_attach_iommufd_pt() not handle this appropriately?)
Quoted 10 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +4
-0+vfio_device_attach_iommufd_pt (device->fd, pt_id); + device->iommu = iommu; +} +struct vfio_pci_device *vfio_pci_device_alloc (const char *bdf,struct iommu *iommu) {struct vfio_pci_device *device;Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Re: [RFC PATCH 1/3] vfio: selftests: Add support of creating iommus from iommufd
Quoted 105 lines · David Matlack
Quoted 104 lines · Samiullah Khawaja
Add API to init a struct iommu using an already opened iommufd instance and attach devices to it.
Signed-off-by Samiullah Khawaja <[email protected]>--- .../vfio/lib/include/libvfio/iommu.h | 2 + .../lib/include/libvfio/vfio_pci_device.h | 2 + tools/testing/selftests/vfio/lib/iommu.c | 60 +++++++++++++++++-- .../selftests/vfio/lib/vfio_pci_device.c | 25 ++++++-- 4 files changed, 80 insertions(+), 9 deletions(-)tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +2
-0@@ -29,10 +29,12 @@ struct iommu {int container_fd;int iommufd;u32 ioas_id; +u32 hwpt_id;struct list_head dma_regions; };struct iommu *iommu_init (const char *iommu_mode); +struct iommu *iommufd_iommu_init (int iommufd,u32 dev_id);void iommu_cleanup (struct iommu *iommu);int __iommu_map (struct iommu *iommu,struct dma_region *region);tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +2
-0@@ -19,6 +19,7 @@ struct vfio_pci_device {const char *bdf;int fd;int group_fd; +u32 dev_id;struct iommu *iommu;@@ -82,6 +83,7 @@ static inline void vfio_pci_cmd_clear(struct vfio_pci_device *device, u16 bits)vfio_pci_config_writew (device,PCI_COMMAND , cmd & ~bits); } +void vfio_pci_device_attach_iommu (struct vfio_pci_device *device,struct iommu *iommu);void vfio_pci_irq_enable (struct vfio_pci_device *device,u32 index,u32 vector,int count);void vfio_pci_irq_disable (struct vfio_pci_device *device,u32 index);tools/testing/selftests/vfio/lib/iommu.c +23
-3@@ -405,6 +405,18 @@ struct iommu_iova_range *iommu_iova_ranges(struct iommu *iommu, u32 *nranges)return ranges; } +static u32 iommufd_hwpt_alloc (struct iommu *iommu,u32 dev_id) +{ +struct iommu_hwpt_alloc args = { + .size =sizeof (args), + .pt_id = iommu->ioas_id, + .dev_id = dev_id, + }; + +ioctl_assert (iommu->iommufd,IOMMU_HWPT_ALLOC , &args); +return args.out_hwpt_id; +} +static u32 iommufd_ioas_alloc (int iommufd) {struct iommu_ioas_alloc args = {@@ -415,17 +427,25 @@ static u32 iommufd_ioas_alloc(int iommufd)return args.out_ioas_id; } -struct iommu *iommu_init (const char *iommu_mode) +static struct iommu *iommu_alloc (const char *iommu_mode) { -const char *container_path;struct iommu *iommu; -int version; iommu =calloc_assert (1 ,sizeof (*iommu));INIT_LIST_HEAD (&iommu->dma_regions); iommu->mode =lookup_iommu_mode (iommu_mode); +return iommu; +} + +struct iommu *iommu_init (const char *iommu_mode) +{ +const char *container_path; +struct iommu *iommu; +int version; + + iommu =iommu_alloc (iommu_mode); container_path = iommu->mode->container_path;if (container_path) {@@ -449,10 +469,42 @@ struct iommu *iommu_init(const char *iommu_mode)return iommu; }
Quoted 6 lines
tools/testing/selftests/vfio/lib/iommu.c struct iommu *iommu_init(const char *iommu_mode) · line 472
Quoted 1 line
tools/testing/selftests/vfio/lib/iommu.c +1
-0+struct iommu *iommufd_iommu_init (int iommufd,u32 dev_id)I think this is a good opportunity to start adding kernel-doc comments. It's not very obvious how and when to use both
iommufd_iommu_init()andvfio_pci_device_attach_iommu(). A kernel-doc comment would help clarify that.
Agreed. I will add it.
Quoted 31 lines
Quoted 18 lines
tools/testing/selftests/vfio/lib/iommu.c +17
-0+{ +struct iommu *iommu; + + iommu =iommu_alloc ("iommufd" ); + + iommu->iommufd =dup (iommufd); +VFIO_ASSERT_GT (iommu->iommufd,0 ); + + iommu->ioas_id =iommufd_ioas_alloc (iommu->iommufd); + iommu->hwpt_id =iommufd_hwpt_alloc (iommu, dev_id); + +return iommu; +} + +static void iommufd_cleanup (struct iommu *iommu) +{ +struct iommu_destroy args = {tools/testing/selftests/vfio/lib/iommu.c struct iommu *iommu_init(const char *iommu_mode) · line 499
Quoted 10 lines
tools/testing/selftests/vfio/lib/iommu.c +10
-0+ .size = sizeof(args), + }; + +if (iommu->hwpt_id) { + args.id = iommu->hwpt_id; +ioctl_assert (iommu->iommufd,IOMMU_DESTROY , &args); + } + + args.id = iommu->ioas_id; +ioctl_assert (iommu->iommufd,IOMMU_DESTROY , &args);Please put
IOMMU_DESTROYinto its own helper so this can be:
Quoted 4 lines
if (
iommu->hwpt_id)iommufd_iommu_destroy(iommu, iommu->hwpt_id);
iommufd_iommu_destroy(iommu, iommu->ioas_id);
Will do
Quoted 41 lines
Quoted 18 lines
tools/testing/selftests/vfio/lib/iommu.c +5
-1+ +VFIO_ASSERT_EQ (close (iommu->iommufd),0 ); +} +void iommu_cleanup (struct iommu *iommu) {if (iommu->iommufd) -VFIO_ASSERT_EQ (close (iommu->iommufd),0 ); +iommufd_cleanup (iommu);else VFIO_ASSERT_EQ (close (iommu->container_fd),0 );tools/testing/selftests/vfio/lib/vfio_pci_device.c +0
-0tools/testing/selftests/vfio/lib/vfio_pci_device.c int __vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_token) · line 472
Quoted 5 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +1
-1@@ -469,15 +469,15 @@ int __vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_token)if (ioctl (device_fd,VFIO_DEVICE_BIND_IOMMUFD , &args))return -errno; -return 0 ; +return args.out_devid;This breaks
vfio_pci_sriov_uapi_testfor all iommufd variants (9/45 fail).iommufd_setup()in that test returns the result of__vfio_device_bind_iommufd()directly, and the fixture/ASSERT_COND_VF_CREATION() treat any non-zero value as failure.Something like the following is needed to fix it:
tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c +2
-0@@ -32,9 +32,14 @@ static int container_setup(struct vfio_pci_device *device, const char *bdf,static int iommufd_setup (struct vfio_pci_device *device,const char *bdf,const char *vf_token) { +int ret; +vfio_pci_cdev_open (device, bdf);
tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c static int container_setup(struct vfio_pci_device *device, const char *bdf, · line 45
Quoted 10 lines
tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c +5
-2-return __vfio_device_bind_iommufd (device->fd, - device->iommu->iommufd, vf_token); + ret =__vfio_device_bind_iommufd (device->fd, + device->iommu->iommufd, vf_token); + +/* On success, __vfio_device_bind_iommufd() returns the device ID. */ +return ret <0 ? ret :0 ; } static int device_init(const char *bdf, struct iommu *iommu,
Will update.
Quoted 24 lines
Quoted 24 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +4
-4} -static void vfio_device_bind_iommufd (int device_fd,int iommufd, -const char *vf_token) +int vfio_device_bind_iommufd (int device_fd,int iommufd,const char *vf_token) {int ret =__vfio_device_bind_iommufd (device_fd, iommufd, vf_token); -VFIO_ASSERT_EQ (ret,0 ,"Failed VFIO_DEVICE_BIND_IOMMUFD ioctl\n" ); +VFIO_ASSERT_GE (ret,0 ,"Failed VFIO_DEVICE_BIND_IOMMUFD ioctl\n" ); +return ret; }static void vfio_device_attach_iommufd_pt (int device_fd,u32 pt_id)@@ -503,10 +503,25 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *bdf, const char *vf_token) {vfio_pci_cdev_open (device ,bdf ); -vfio_device_bind_iommufd (device->fd, device->iommu->iommufd, vf_token); + device->dev_id =vfio_device_bind_iommufd (device->fd, device->iommu->iommufd, vf_token);vfio_device_attach_iommufd_pt (device->fd, device->iommu->ioas_id); }
Quoted 14 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, · line 520
Quoted 11 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +11
-0+void vfio_pci_device_attach_iommu (struct vfio_pci_device *device,struct iommu *iommu) +{ +u32 pt_id = iommu->ioas_id; + +/* Only iommufd supports changing struct iommu attachments */ +VFIO_ASSERT_TRUE (iommu->iommufd); + +if (iommu->hwpt_id) + pt_id = iommu->hwpt_id; + +VFIO_ASSERT_NE (pt_id,0 );Is this assert needed? (Will
vfio_device_attach_iommufd_pt()not handle this appropriately?)
It should be covered by the assert for the ioctl. I will remove this.
Quoted 11 lines
Quoted 11 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +4
-1+vfio_device_attach_iommufd_pt (device->fd, pt_id); + device->iommu = iommu; +} +struct vfio_pci_device *vfio_pci_device_alloc (const char *bdf,struct iommu *iommu) {struct vfio_pci_device *device; --2.56.0.rc1.315.gc6ed9934b7-goog
Thanks,
Sami
RFC PATCH 2/3 vfio: selftests: Add iommufd hwpt replace test
In reply to Samiullah KhawajaAdd a test that does iommufd hwpt replace while a DMA is ongoing. This verifies the hitless replace of IOMMU domain without disrupting the DMA.
Note that the new domain is attached after mapping the required DMA memory at the same IOVA in the new domain.
--- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/vfio_iommufd_hwpt_replace_test.c | 171 ++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c
tools/testing/selftests/vfio/Makefile +1 -0
@@ -8,6 +8,7 @@ else CFLAGS = $(KHDR_INCLUDES) TEST_GEN_PROGS += vfio_dma_mapping_test TEST_GEN_PROGS += vfio_dma_mapping_mmio_test +TEST_GEN_PROGS += vfio_iommufd_hwpt_replace_test TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test TEST_GEN_PROGS += vfio_pci_device_init_perf_test
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file +171 -0
171 lines not on this page · raw message
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Re: [RFC PATCH 2/3] vfio: selftests: Add iommufd hwpt replace test
Quoted 7 lines · Samiullah Khawaja
Add a test that does iommufd hwpt replace while a DMA is ongoing. This verifies the hitless replace of IOMMU domain without disrupting the DMA.
Note that the new domain is attached after mapping the required DMA memory at the same IOVA in the new domain.
Signed-off-by Samiullah Khawaja <[email protected]>
Aside from the MODE_IOMMUFD nit,
Quoted 70 lines
--- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/vfio_iommufd_hwpt_replace_test.c | 171 ++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.ctools/testing/selftests/vfio/Makefile +1
-0@@ -8,6 +8,7 @@ else CFLAGS = $(KHDR_INCLUDES) TEST_GEN_PROGS += vfio_dma_mapping_test TEST_GEN_PROGS += vfio_dma_mapping_mmio_test +TEST_GEN_PROGS += vfio_iommufd_hwpt_replace_test TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test TEST_GEN_PROGS += vfio_pci_device_init_perf_testtools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file +46
-0@@ -0,0 +1,171 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <string.h> +#include <sys/ioctl.h> +#include <sys/mman.h> + +#include <linux/sizes.h> +#include <linux/vfio.h> + +#include <libvfio.h> + +#include "kselftest_harness.h" + +static const char *device_bdf; + +static void region_setup (struct iommu *iommu, +struct iova_allocator *iova_allocator, +struct dma_region *region,u64 size) +{ +const int flags =MAP_SHARED |MAP_ANONYMOUS ; +const int prot =PROT_READ |PROT_WRITE ; +void *vaddr; + + vaddr =mmap (NULL , size, prot, flags,-1 ,0 ); +VFIO_ASSERT_NE (vaddr,MAP_FAILED ); + + region->vaddr = vaddr; + region->iova =iova_allocator_alloc (iova_allocator, size); + region->size = size; + +iommu_map (iommu, region); +} + +static void region_teardown (struct iommu *iommu,struct dma_region *region) +{ +iommu_unmap (iommu, region); +VFIO_ASSERT_EQ (munmap (region->vaddr, region->size),0 ); +} + +FIXTURE (vfio_iommufd_replace_hwpt_test) { +struct iommu *iommu; +struct vfio_pci_device *device; +struct iova_allocator *iova_allocator; +struct dma_region memcpy_region; + +u64 size; +void *src;
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c line 56
Quoted 10 lines
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file +10
-0+void *dst; +iova_t src_iova; +iova_t dst_iova; +}; + +FIXTURE_SETUP (vfio_iommufd_replace_hwpt_test) +{ +struct vfio_pci_driver *driver; + + self->iommu =iommu_init ("iommufd" );
nit: iommu_init(MODE_IOMMUFD)
This needs to be fixed in iommufd_iommu_init() in the previous patch as well.
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c line 63
Quoted 7 lines
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file +7
-0+ self->device =vfio_pci_device_init (device_bdf, self->iommu); + self->iova_allocator =iova_allocator_init (self->iommu); + + driver = &self->device->driver; + +region_setup (self->iommu, self->iova_allocator, &self->memcpy_region,SZ_2M ); +region_setup (self->iommu, self->iova_allocator, &driver->region,SZ_2M );
The driver->region.size is now populated automatically based on what the driver needs. So you can just pass in driver->region.size here instead of hard-coding SZ_2M.
Quoted 23 lines
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file +23
-0+ +if (driver->ops) +vfio_pci_driver_init (self->device); + + self->size = self->memcpy_region.size /2 ; + self->src = self->memcpy_region.vaddr; + self->dst = self->src + self->size; + + self->src_iova =to_iova (self->device, self->src); + self->dst_iova =to_iova (self->device, self->dst); +} + +FIXTURE_TEARDOWN (vfio_iommufd_replace_hwpt_test) +{ +struct vfio_pci_driver *driver = &self->device->driver; + +if (driver->ops) +vfio_pci_driver_remove (self->device); + +region_teardown (self->iommu, &self->memcpy_region); +region_teardown (self->iommu, &driver->region); + +iova_allocator_cleanup (self->iova_allocator);
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c line 96
Quoted 10 lines
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file +10
-0+vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu); +} + +FIXTURE_VARIANT (vfio_iommufd_replace_hwpt_test) { +u32 attaches; +}; + +FIXTURE_VARIANT_ADD(vfio_iommufd_replace_hwpt_test, domain_replace) { + .attaches = 1000,
Maybe we should replace memcpy_wait() with memcpy_poll() and move the wait loop out into the generic driver layer (vfio_pci_driver.c) repeatedly calling memcpy_poll(). The would let you do replaces until the memcpy is done instead of hard-coding a random number, and also lift more code out of the the drivers and into the generic core.
It should be easy enough to swap the drivers from wait() to poll() model if you are feeling up for it. Not a blocker for this series though.
Quoted 78 lines
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file +75
-0+}; + +FIXTURE_VARIANT_ADD(vfio_iommufd_replace_hwpt_test, noreplace) { + .attaches =0 , +}; + +TEST_F_TIMEOUT(vfio_iommufd_replace_hwpt_test, memcpy, 120) +{ +struct dma_region memcpy_region = {}, driver_region = {}; +struct vfio_pci_driver *driver = &self->device->driver; +struct iommu *iommu2 =NULL ; +u64 size =0 ; +int ret; +u32 i; + +if (variant->attaches) { + iommu2 =iommufd_iommu_init (self->iommu->iommufd, + self->device->dev_id); + + memcpy_region = self->memcpy_region; + driver_region = self->device->driver.region; + +iommu_map (iommu2, &memcpy_region); +iommu_map (iommu2, &driver_region); + } + +if (driver->ops) { +memset (self->src,'x' , self->size); +memset (self->dst,'y' , self->size); + + size =min_t (u64, driver->max_memcpy_size, self->size); + +/* +* Fill the device's queue. memcpy_start() returns with +* max_memcpy_count transfers still outstanding. +*/ +vfio_pci_driver_memcpy_start (self->device, + self->src_iova, + self->dst_iova, + size, driver->max_memcpy_count); + } + +/* +* Replace the domain repeatedly while that transfer is in flight. Since +* domain replacement is a race with the translation requests, it needs +* to be done multiple times to capture the small window where iommu +* faults can occur. +* +* The loop ends on the original domain, so the teardown below is safe. +*/ +for (i =0 ; i < variant->attaches; i++) { +vfio_pci_device_attach_iommu (self->device, iommu2); +vfio_pci_device_attach_iommu (self->device, self->iommu); + } + +if (driver->ops) { + ret =vfio_pci_driver_memcpy_wait (self->device); + +ASSERT_EQ (0 , ret); +ASSERT_EQ (0 ,memcmp (self->src, self->dst, size)); + } + +if (iommu2) { +iommu_unmap (iommu2, &memcpy_region); +iommu_unmap (iommu2, &driver_region); +iommu_cleanup (iommu2); + } +} + +int main (int argc,char *argv[]) +{ + device_bdf =vfio_selftests_get_bdf (&argc, argv); + +return test_harness_run (argc, argv); +}Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Re: [RFC PATCH 2/3] vfio: selftests: Add iommufd hwpt replace test
Quoted 142 lines · David Matlack
Quoted 7 lines · Samiullah Khawaja
Add a test that does iommufd hwpt replace while a DMA is ongoing. This verifies the hitless replace of IOMMU domain without disrupting the DMA.
Note that the new domain is attached after mapping the required DMA memory at the same IOVA in the new domain.
Signed-off-by Samiullah Khawaja <[email protected]>Aside from the
MODE_IOMMUFDnit,Reviewed-by David Matlack <[email protected]>Quoted 70 lines
--- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/vfio_iommufd_hwpt_replace_test.c | 171 ++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.ctools/testing/selftests/vfio/Makefile +1
-0@@ -8,6 +8,7 @@ else CFLAGS = $(KHDR_INCLUDES) TEST_GEN_PROGS += vfio_dma_mapping_test TEST_GEN_PROGS += vfio_dma_mapping_mmio_test +TEST_GEN_PROGS += vfio_iommufd_hwpt_replace_test TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test TEST_GEN_PROGS += vfio_pci_device_init_perf_testtools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file +46
-0@@ -0,0 +1,171 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <string.h> +#include <sys/ioctl.h> +#include <sys/mman.h> + +#include <linux/sizes.h> +#include <linux/vfio.h> + +#include <libvfio.h> + +#include "kselftest_harness.h" + +static const char *device_bdf; + +static void region_setup (struct iommu *iommu, +struct iova_allocator *iova_allocator, +struct dma_region *region,u64 size) +{ +const int flags =MAP_SHARED |MAP_ANONYMOUS ; +const int prot =PROT_READ |PROT_WRITE ; +void *vaddr; + + vaddr =mmap (NULL , size, prot, flags,-1 ,0 ); +VFIO_ASSERT_NE (vaddr,MAP_FAILED ); + + region->vaddr = vaddr; + region->iova =iova_allocator_alloc (iova_allocator, size); + region->size = size; + +iommu_map (iommu, region); +} + +static void region_teardown (struct iommu *iommu,struct dma_region *region) +{ +iommu_unmap (iommu, region); +VFIO_ASSERT_EQ (munmap (region->vaddr, region->size),0 ); +} + +FIXTURE (vfio_iommufd_replace_hwpt_test) { +struct iommu *iommu; +struct vfio_pci_device *device; +struct iova_allocator *iova_allocator; +struct dma_region memcpy_region; + +u64 size; +void *src;tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c line 56
Quoted 10 lines
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file +10
-0+void *dst; +iova_t src_iova; +iova_t dst_iova; +}; + +FIXTURE_SETUP (vfio_iommufd_replace_hwpt_test) +{ +struct vfio_pci_driver *driver; + + self->iommu =iommu_init ("iommufd" );nit:
iommu_init(MODE_IOMMUFD)This needs to be fixed in
iommufd_iommu_init()in the previous patch as well.tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c line 63
Quoted 7 lines
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file +7
-0+ self->device =vfio_pci_device_init (device_bdf, self->iommu); + self->iova_allocator =iova_allocator_init (self->iommu); + + driver = &self->device->driver; + +region_setup (self->iommu, self->iova_allocator, &self->memcpy_region,SZ_2M ); +region_setup (self->iommu, self->iova_allocator, &driver->region,SZ_2M );The
driver->region.sizeis now populated automatically based on what the driver needs. So you can just pass indriver->region.sizehere instead of hard-codingSZ_2M.Quoted 31 lines
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file +31
-0+ +if (driver->ops) +vfio_pci_driver_init (self->device); + + self->size = self->memcpy_region.size /2 ; + self->src = self->memcpy_region.vaddr; + self->dst = self->src + self->size; + + self->src_iova =to_iova (self->device, self->src); + self->dst_iova =to_iova (self->device, self->dst); +} + +FIXTURE_TEARDOWN (vfio_iommufd_replace_hwpt_test) +{ +struct vfio_pci_driver *driver = &self->device->driver; + +if (driver->ops) +vfio_pci_driver_remove (self->device); + +region_teardown (self->iommu, &self->memcpy_region); +region_teardown (self->iommu, &driver->region); + +iova_allocator_cleanup (self->iova_allocator); +vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu); +} + +FIXTURE_VARIANT (vfio_iommufd_replace_hwpt_test) { +u32 attaches; +}; +
Quoted 8 lines
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c line 96
Quoted 2 lines
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file +2
-0+FIXTURE_VARIANT_ADD(vfio_iommufd_replace_hwpt_test, domain_replace) { + .attaches = 1000,Maybe we should replace
memcpy_wait()withmemcpy_poll()and move the wait loop out into the generic driver layer (vfio_pci_driver.c) repeatedly callingmemcpy_poll(). The would let you do replaces until the memcpy is done instead of hard-coding a random number, and also lift more code out of the the drivers and into the generic core.
The purpose of this being here was to a N number of attaches/replaces as it might have chance to produce faults, as this is basically race. While I agree poll() is a useful op to have, but in the context of this test maybe it is an overkill.
Thanks,
Sami
Quoted 82 lines
It should be easy enough to swap the drivers from
wait()topoll()model if you are feeling up for it. Not a blocker for this series though.Quoted 78 lines
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file +75
-1+}; + +FIXTURE_VARIANT_ADD(vfio_iommufd_replace_hwpt_test, noreplace) { + .attaches =0 , +}; + +TEST_F_TIMEOUT(vfio_iommufd_replace_hwpt_test, memcpy, 120) +{ +struct dma_region memcpy_region = {}, driver_region = {}; +struct vfio_pci_driver *driver = &self->device->driver; +struct iommu *iommu2 =NULL ; +u64 size =0 ; +int ret; +u32 i; + +if (variant->attaches) { + iommu2 =iommufd_iommu_init (self->iommu->iommufd, + self->device->dev_id); + + memcpy_region = self->memcpy_region; + driver_region = self->device->driver.region; + +iommu_map (iommu2, &memcpy_region); +iommu_map (iommu2, &driver_region); + } + +if (driver->ops) { +memset (self->src,'x' , self->size); +memset (self->dst,'y' , self->size); + + size =min_t (u64, driver->max_memcpy_size, self->size); + +/* +* Fill the device's queue. memcpy_start() returns with +* max_memcpy_count transfers still outstanding. +*/ +vfio_pci_driver_memcpy_start (self->device, + self->src_iova, + self->dst_iova, + size, driver->max_memcpy_count); + } + +/* +* Replace the domain repeatedly while that transfer is in flight. Since +* domain replacement is a race with the translation requests, it needs +* to be done multiple times to capture the small window where iommu +* faults can occur. +* +* The loop ends on the original domain, so the teardown below is safe. +*/ +for (i =0 ; i < variant->attaches; i++) { +vfio_pci_device_attach_iommu (self->device, iommu2); +vfio_pci_device_attach_iommu (self->device, self->iommu); + } + +if (driver->ops) { + ret =vfio_pci_driver_memcpy_wait (self->device); + +ASSERT_EQ (0 , ret); +ASSERT_EQ (0 ,memcmp (self->src, self->dst, size)); + } + +if (iommu2) { +iommu_unmap (iommu2, &memcpy_region); +iommu_unmap (iommu2, &driver_region); +iommu_cleanup (iommu2); + } +} + +int main (int argc,char *argv[]) +{ + device_bdf =vfio_selftests_get_bdf (&argc, argv); + +return test_harness_run (argc, argv); +} --2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 3/3 iommu/arm-smmu-v3: Add support for hitless replace of S1 domains
In reply to Samiullah KhawajaS1 domains are programmed by CD entries in the CD table. During a new domain attach these are updated using write_cd_entry. Since ASID and TTB0 are in different 64-bit words, the write_cd_entry considers it a non-hitless update and sets V=0. This means such an update is disruptive and would generate translation faults if there are ongoing DMAs.
Replace the CD entries by using an unused temporary ASID in following sequence to allow hitless replacement of CD entry,
- Update the CD entry with temporary ASID.
- Update the CD entry with the target TTB0.
- Update the CD entry with the target ASID.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 66 ++++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +64 -2
@@ -1641,6 +1641,68 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid,arm_smmu_write_entry (&cd_writer.writer, cdptr->data, target->data); } +/* Invalidate every stage-1 TLB entry tagged with @asid. */ +static void arm_smmu_tlb_inv_asid (struct arm_smmu_device *smmu,u16 asid) +{ +enum arm_smmu_cmdq_opcode op = (smmu->features &ARM_SMMU_FEAT_E2H ) ? +CMDQ_OP_TLBI_EL2_ASID : +CMDQ_OP_TLBI_NH_ASID ; + +arm_smmu_cmdq_issue_cmd_with_sync (smmu, +arm_smmu_make_cmd_tlbi (op, asid,0 )); +} + +static void arm_smmu_cd_set_asid (struct arm_smmu_cd *cd,u16 asid) +{ + cd->data[0 ] &= ~cpu_to_le64 (CTXDESC_CD_0_ASID ); + cd->data[0 ] |=cpu_to_le64 (FIELD_PREP (CTXDESC_CD_0_ASID , asid)); +} + +static void arm_smmu_replace_cd_entry (struct arm_smmu_master *master,int ssid, +struct arm_smmu_cd *cdptr, +const struct arm_smmu_cd *target) +{ +struct arm_smmu_device *smmu = master->smmu; +struct arm_smmu_cd stage; +u32 tmp_asid; + +lockdep_assert_held (&arm_smmu_asid_lock); + +/* +* Nothing is translating through an invalid CD, and an update that +* invalidates one cannot be made hitless anyway. +*/ +if (!(cdptr->data[0 ] &cpu_to_le64 (CTXDESC_CD_0_V )) || + !(target->data[0 ] &cpu_to_le64 (CTXDESC_CD_0_V ))) + goto write_directly; + +if (xa_alloc (&arm_smmu_asid_xa, &tmp_asid,XA_ZERO_ENTRY , +XA_LIMIT (1 , (1 << smmu->asid_bits) -1 ),GFP_KERNEL )) + goto write_directly; + +/* Copy the existing cd entry and update only the ASID. */ + stage = *cdptr; +arm_smmu_cd_set_asid (&stage, tmp_asid); +arm_smmu_write_cd_entry (master, ssid, cdptr, &stage); + +/* Copy the target cd entry and update ASID to the temporary ASID. */ + stage = *target; +arm_smmu_cd_set_asid (&stage, tmp_asid); +arm_smmu_write_cd_entry (master, ssid, cdptr, &stage); + +arm_smmu_tlb_inv_asid (smmu, tmp_asid); + +/* Update to the target cd entry as it should be hitless now. */ +arm_smmu_write_cd_entry (master, ssid, cdptr, target); + +arm_smmu_tlb_inv_asid (smmu, tmp_asid); +xa_erase (&arm_smmu_asid_xa, tmp_asid); +return ; + +write_directly: +arm_smmu_write_cd_entry (master, ssid, cdptr, target); +} +void arm_smmu_make_s1_cd (struct arm_smmu_cd *target,struct arm_smmu_master *master,struct arm_smmu_domain *smmu_domain)
@@ -3664,8 +3726,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev,struct arm_smmu_cd target_cd;arm_smmu_make_s1_cd (&target_cd, master, smmu_domain); -arm_smmu_write_cd_entry (master,IOMMU_NO_PASID , cdptr, - &target_cd); +arm_smmu_replace_cd_entry (master,IOMMU_NO_PASID , cdptr, + &target_cd);arm_smmu_make_cdtable_ste (&target, master, state.ats_enabled,STRTAB_STE_1_S1DSS_SSID0 );arm_smmu_install_ste_for_dev (master, &target);
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Robin Murphy
reviewerRe: [RFC PATCH 3/3] iommu/arm-smmu-v3: Add support for hitless replace of S1 domains
Quoted 26 lines · Samiullah Khawaja
S1 domains are programmed by CD entries in the CD table. During a new domain attach these are updated using
write_cd_entry. Since ASID and TTB0 are in different 64-bit words, thewrite_cd_entryconsiders it a non-hitless update and sets V=0. This means such an update is disruptive and would generate translation faults if there are ongoing DMAs.Replace the CD entries by using an unused temporary ASID in following sequence to allow hitless replacement of CD entry,
- Update the CD entry with temporary ASID.
- Update the CD entry with the target TTB0.
- Update the CD entry with the target ASID.Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 66 ++++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-)drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0
-0@@ -1641,6 +1641,68 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid,arm_smmu_write_entry (&cd_writer.writer, cdptr->data, target->data); }
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, · line 1653
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+/* Invalidate every stage-1 TLB entry tagged with @asid. */ +static void arm_smmu_tlb_inv_asid (struct arm_smmu_device *smmu,u16 asid) +{ +enum arm_smmu_cmdq_opcode op = (smmu->features &ARM_SMMU_FEAT_E2H ) ? +CMDQ_OP_TLBI_EL2_ASID : +CMDQ_OP_TLBI_NH_ASID ; + +arm_smmu_cmdq_issue_cmd_with_sync (smmu, +arm_smmu_make_cmd_tlbi (op, asid,0 )); +}
Feels like this should probably use the existing invalidation machinery...
Quoted 56 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +52
-0+ +static void arm_smmu_cd_set_asid (struct arm_smmu_cd *cd,u16 asid) +{ + cd->data[0 ] &= ~cpu_to_le64 (CTXDESC_CD_0_ASID ); + cd->data[0 ] |=cpu_to_le64 (FIELD_PREP (CTXDESC_CD_0_ASID , asid)); +} + +static void arm_smmu_replace_cd_entry (struct arm_smmu_master *master,int ssid, +struct arm_smmu_cd *cdptr, +const struct arm_smmu_cd *target) +{ +struct arm_smmu_device *smmu = master->smmu; +struct arm_smmu_cd stage; +u32 tmp_asid; + +lockdep_assert_held (&arm_smmu_asid_lock); + +/* +* Nothing is translating through an invalid CD, and an update that +* invalidates one cannot be made hitless anyway. +*/ +if (!(cdptr->data[0 ] &cpu_to_le64 (CTXDESC_CD_0_V )) || + !(target->data[0 ] &cpu_to_le64 (CTXDESC_CD_0_V ))) + goto write_directly; + +if (xa_alloc (&arm_smmu_asid_xa, &tmp_asid,XA_ZERO_ENTRY , +XA_LIMIT (1 , (1 << smmu->asid_bits) -1 ),GFP_KERNEL )) + goto write_directly; + +/* Copy the existing cd entry and update only the ASID. */ + stage = *cdptr; +arm_smmu_cd_set_asid (&stage, tmp_asid); +arm_smmu_write_cd_entry (master, ssid, cdptr, &stage); + +/* Copy the target cd entry and update ASID to the temporary ASID. */ + stage = *target; +arm_smmu_cd_set_asid (&stage, tmp_asid); +arm_smmu_write_cd_entry (master, ssid, cdptr, &stage); + +arm_smmu_tlb_inv_asid (smmu, tmp_asid); + +/* Update to the target cd entry as it should be hitless now. */ +arm_smmu_write_cd_entry (master, ssid, cdptr, target); + +arm_smmu_tlb_inv_asid (smmu, tmp_asid); +xa_erase (&arm_smmu_asid_xa, tmp_asid); +return ; + +write_directly: +arm_smmu_write_cd_entry (master, ssid, cdptr, target); +} +void arm_smmu_make_s1_cd (struct arm_smmu_cd *target,struct arm_smmu_master *master,struct arm_smmu_domain *smmu_domain)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev, · line 3730
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-2@@ -3664,8 +3726,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev,struct arm_smmu_cd target_cd;arm_smmu_make_s1_cd (&target_cd, master, smmu_domain); -arm_smmu_write_cd_entry (master,IOMMU_NO_PASID , cdptr, - &target_cd); +arm_smmu_replace_cd_entry (master,IOMMU_NO_PASID , cdptr, + &target_cd);
No, we cannot blindly do this for arbitrary ->attach_dev calls - the majority of S1->S1 transitions are going to be moving back and forth between the default domain and user domains, where there is zero expectation, let alone guarantee, that the translations would be equivalent and thus not liable to lead to TLB conflict aborts or amalgamation hell.
This needs to be in a dedicated "I know what I'm doing and I fully accept the consequences if I get it wrong" call path.
Thanks,
Robin.
Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0
-0arm_smmu_make_cdtable_ste (&target, master, state.ats_enabled,STRTAB_STE_1_S1DSS_SSID0 );arm_smmu_install_ste_for_dev (master, &target);
Re: [RFC PATCH 3/3] iommu/arm-smmu-v3: Add support for hitless replace of S1 domains
Quoted 5 lines · Robin Murphy
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0
-2@@ -3664,8 +3726,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev,struct arm_smmu_cd target_cd;arm_smmu_make_s1_cd (&target_cd, master, smmu_domain); -arm_smmu_write_cd_entry (master,IOMMU_NO_PASID , cdptr, - &target_cd);
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev, · line 3730
Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-0+arm_smmu_replace_cd_entry (master,IOMMU_NO_PASID , cdptr, + &target_cd);No, we cannot blindly do this for arbitrary ->attach_dev calls - the majority of
S1->S1transitions are going to be moving back and forth between the default domain and user domains, where there is zero expectation, let alone guarantee, that the translations would be equivalent and thus not liable to lead to TLB conflict aborts or amalgamation hell.
Heh, yeah this probably only works properly with BBML2 or something so the temporary ASID doesn't explode.
Jason
Robin Murphy
reviewerRe: [RFC PATCH 3/3] iommu/arm-smmu-v3: Add support for hitless replace of S1 domains
Quoted 15 lines · Jason Gunthorpe
Quoted 5 lines · Robin Murphy
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0
-2@@ -3664,8 +3726,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev,struct arm_smmu_cd target_cd;arm_smmu_make_s1_cd (&target_cd, master, smmu_domain); -arm_smmu_write_cd_entry (master,IOMMU_NO_PASID , cdptr, - &target_cd);Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev, · line 3730
Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-0+arm_smmu_replace_cd_entry (master,IOMMU_NO_PASID , cdptr, + &target_cd);No, we cannot blindly do this for arbitrary ->attach_dev calls - the majority of
S1->S1transitions are going to be moving back and forth between the default domain and user domains, where there is zero expectation, let alone guarantee, that the translations would be equivalent and thus not liable to lead to TLB conflict aborts or amalgamation hell.
Quoted 2 lines
Heh, yeah this probably only works properly with BBML2 or something so the temporary ASID doesn't explode.
This is fundamental VMSA behaviour - see R_FVQCK in the Arm ARM. BBML only applies to the one specific case of identical VA->PA translations with identical attributes _only_ differing in the leaf level at which they are described.
Thanks,
Robin.
Re: [RFC PATCH 3/3] iommu/arm-smmu-v3: Add support for hitless replace of S1 domains
Quoted 16 lines · Robin Murphy
Quoted 15 lines · Jason Gunthorpe
Quoted 13 lines · Robin Murphy
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev, · line 3729
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-2@@ -3664,8 +3726,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev,struct arm_smmu_cd target_cd;arm_smmu_make_s1_cd (&target_cd, master, smmu_domain); -arm_smmu_write_cd_entry (master,IOMMU_NO_PASID , cdptr, - &target_cd); +arm_smmu_replace_cd_entry (master,IOMMU_NO_PASID , cdptr, + &target_cd);No, we cannot blindly do this for arbitrary ->attach_dev calls - the majority of
S1->S1transitions are going to be moving back and forth between the default domain and user domains, where there is zero expectation, let alone guarantee, that the translations would be equivalent and thus not liable to lead to TLB conflict aborts or amalgamation hell.
Quoted 7 lines
Quoted 2 lines
Heh, yeah this probably only works properly with BBML2 or something so the temporary ASID doesn't explode.
This is fundamental VMSA behaviour - see
R_FVQCKin the Arm ARM. BBML only applies to the one specific case of identicalVA->PAtranslations with identical attributes _only_ differing in the leaf level at which they are described.
Sigh, I thought ARM got past that 'tlb conflicts catch fire'.
So, yeah we need a flag that says "I pinky swear these are identical translations". And it has to be a kernel only flag since we can't let userspace trigger this.
Jason
Re: [RFC PATCH 3/3] iommu/arm-smmu-v3: Add support for hitless replace of S1 domains
Quoted 17 lines · Jason Gunthorpe
Quoted 16 lines · Robin Murphy
Quoted 15 lines · Jason Gunthorpe
Quoted 13 lines · Robin Murphy
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev, · line 3729
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-2@@ -3664,8 +3726,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev,struct arm_smmu_cd target_cd;arm_smmu_make_s1_cd (&target_cd, master, smmu_domain); -arm_smmu_write_cd_entry (master,IOMMU_NO_PASID , cdptr, - &target_cd); +arm_smmu_replace_cd_entry (master,IOMMU_NO_PASID , cdptr, + &target_cd);No, we cannot blindly do this for arbitrary ->attach_dev calls - the majority of
S1->S1transitions are going to be moving back and forth between the default domain and user domains, where there is zero expectation, let alone guarantee, that the translations would be equivalent and thus not liable to lead to TLB conflict aborts or amalgamation hell.
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Heh, yeah this probably only works properly with BBML2 or something so the temporary ASID doesn't explode.
This is fundamental VMSA behaviour - see
R_FVQCKin the Arm ARM. BBML only applies to the one specific case of identicalVA->PAtranslations with identical attributes _only_ differing in the leaf level at which they are described.
Thinking about this more, in case of a conflict, what is the expected behaviour? Does it result in a DMA fault for the affected IOVA, or is it a catastrophic event for the SMMU itself, affecting other devices and the whole system?
Quoted 6 lines
Sigh, I thought ARM got past that 'tlb conflicts catch fire'.
So, yeah we need a flag that says "I pinky swear these are identical translations". And it has to be a kernel only flag since we can't let userspace trigger this.
Agreed, this should only be requestable through a kernel-only flag. From the live update side, we would set it only when replacing the restored (preserved) domain with the new one, where the translations are identical by construction, including the page sizes used.
Quoted 2 lines
Jason
Thanks,
Sami
Re: [RFC PATCH 3/3] iommu/arm-smmu-v3: Add support for hitless replace of S1 domains
In reply to Robin MurphyQuoted 30 lines · Robin Murphy
Quoted 29 lines · Samiullah Khawaja
S1 domains are programmed by CD entries in the CD table. During a new domain attach these are updated using
write_cd_entry. Since ASID and TTB0 are in different 64-bit words, thewrite_cd_entryconsiders it a non-hitless update and sets V=0. This means such an update is disruptive and would generate translation faults if there are ongoing DMAs.Replace the CD entries by using an unused temporary ASID in following sequence to allow hitless replacement of CD entry,
- Update the CD entry with temporary ASID.
- Update the CD entry with the target TTB0.
- Update the CD entry with the target ASID.Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 66 ++++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-)drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0@@ -1641,6 +1641,68 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid,arm_smmu_write_entry (&cd_writer.writer, cdptr->data, target->data); } +/* Invalidate every stage-1 TLB entry tagged with @asid. */ +static void arm_smmu_tlb_inv_asid (struct arm_smmu_device *smmu,u16 asid) +{ + enum arm_smmu_cmdq_opcode op = (smmu->features & ARM_SMMU_FEAT_E2H) ?
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, · line 1653
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +6
-0+ CMDQ_OP_TLBI_EL2_ASID : +CMDQ_OP_TLBI_NH_ASID ; + +arm_smmu_cmdq_issue_cmd_with_sync (smmu, +arm_smmu_make_cmd_tlbi (op, asid,0 )); +}Feels like this should probably use the existing invalidation machinery...
I see, I can use the arm_smmu_inv_flush_iotlb_tag() to issue invalidation for the ASID.
Quoted 66 lines
Quoted 57 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +52
-0+ +static void arm_smmu_cd_set_asid (struct arm_smmu_cd *cd,u16 asid) +{ + cd->data[0 ] &= ~cpu_to_le64 (CTXDESC_CD_0_ASID ); + cd->data[0 ] |=cpu_to_le64 (FIELD_PREP (CTXDESC_CD_0_ASID , asid)); +} + +static void arm_smmu_replace_cd_entry (struct arm_smmu_master *master,int ssid, +struct arm_smmu_cd *cdptr, +const struct arm_smmu_cd *target) +{ +struct arm_smmu_device *smmu = master->smmu; +struct arm_smmu_cd stage; +u32 tmp_asid; + +lockdep_assert_held (&arm_smmu_asid_lock); + +/* +* Nothing is translating through an invalid CD, and an update that +* invalidates one cannot be made hitless anyway. +*/ +if (!(cdptr->data[0 ] &cpu_to_le64 (CTXDESC_CD_0_V )) || + !(target->data[0 ] &cpu_to_le64 (CTXDESC_CD_0_V ))) + goto write_directly; + +if (xa_alloc (&arm_smmu_asid_xa, &tmp_asid,XA_ZERO_ENTRY , +XA_LIMIT (1 , (1 << smmu->asid_bits) -1 ),GFP_KERNEL )) + goto write_directly; + +/* Copy the existing cd entry and update only the ASID. */ + stage = *cdptr; +arm_smmu_cd_set_asid (&stage, tmp_asid); +arm_smmu_write_cd_entry (master, ssid, cdptr, &stage); + +/* Copy the target cd entry and update ASID to the temporary ASID. */ + stage = *target; +arm_smmu_cd_set_asid (&stage, tmp_asid); +arm_smmu_write_cd_entry (master, ssid, cdptr, &stage); + +arm_smmu_tlb_inv_asid (smmu, tmp_asid); + +/* Update to the target cd entry as it should be hitless now. */ +arm_smmu_write_cd_entry (master, ssid, cdptr, target); + +arm_smmu_tlb_inv_asid (smmu, tmp_asid); +xa_erase (&arm_smmu_asid_xa, tmp_asid); +return ; + +write_directly: +arm_smmu_write_cd_entry (master, ssid, cdptr, target); +} +void arm_smmu_make_s1_cd (struct arm_smmu_cd *target,struct arm_smmu_master *master,struct arm_smmu_domain *smmu_domain)drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev, · line 3729
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-2@@ -3664,8 +3726,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev,struct arm_smmu_cd target_cd;arm_smmu_make_s1_cd (&target_cd, master, smmu_domain); -arm_smmu_write_cd_entry (master,IOMMU_NO_PASID , cdptr, - &target_cd); +arm_smmu_replace_cd_entry (master,IOMMU_NO_PASID , cdptr, + &target_cd);No, we cannot blindly do this for arbitrary ->attach_dev calls - the majority of
S1->S1transitions are going to be moving back and forth between the default domain and user domains, where there is zero expectation, let alone guarantee, that the translations would be equivalent and thus not liable to lead to TLB conflict aborts or amalgamation hell.
Quoted 1 line
This needs to be in a dedicated "I know what I'm doing and I fully accept the consequences if I get it wrong" call path.
Agreed.
I will update this to only be triggered with explicit request from the Core to satisfy my live update use case where a preserved iommu domain is being replaced by a new domain setup by the user.
Thanks,
Sami
Quoted 8 lines
Thanks,
Robin.Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0
-0arm_smmu_make_cdtable_ste (&target, master, state.ats_enabled,STRTAB_STE_1_S1DSS_SSID0 );arm_smmu_install_ste_for_dev (master, &target);
David Matlack
maintainerQuoted 25 lines · Samiullah Khawaja
This patch series adds support for replacing IOMMU domains with S1 page tables hitlessly, that is without disrupting the ongoing DMAs. This is one of the dependencies of the Liveupdate IOMMU support for Arm SMMUv3.
S1 domains are programmed by CD entries in the CD table. During a new domain attach these are updated using
arm_smmu_write_cd_entry(). Since ASID and TTB0 are in different 64-bit words,arm_smmu_write_cd_entry()considers it a non-hitless update and sets V=0. This means such an update is disruptive and would generate translation faults if there are ongoing DMAs.Replace the CD entries by using an unused temporary ASID in the following sequence to allow hitless replacement of CD entry,
- Update the CD entry with temporary ASID.
- Update the CD entry with the target TTB0.
- Invalidate the temporary ASID.
- Update the CD entry with target ASID.
- Invalidate and release the temporary ASID.The series adds a vfio selftest that triggers a continuous DMA and does the hwpt replace while the DMA is ongoing. Since domain replacement is a race with the translation requests, the test does it multiple times to capture the small window where IOMMU faults can occur.
Quoted 6 lines
Without this change we can observe the SMMU faults in the kernel logs and with it the SMMU faults do not occur.
This is only tested on qemu with the emulated SMMUv3 and an out of tree NVMe vfio selftest driver, not on real hardware. Following are not covered and can be done later,
Out of curiousity, does the in-tree VFIO selftests driver for IGB not work for this testing scenario?
Quoted 29 lines · David Matlack
Quoted 28 lines · Samiullah Khawaja
This patch series adds support for replacing IOMMU domains with S1 page tables hitlessly, that is without disrupting the ongoing DMAs. This is one of the dependencies of the Liveupdate IOMMU support for Arm SMMUv3.
S1 domains are programmed by CD entries in the CD table. During a new domain attach these are updated using
arm_smmu_write_cd_entry(). Since ASID and TTB0 are in different 64-bit words,arm_smmu_write_cd_entry()considers it a non-hitless update and sets V=0. This means such an update is disruptive and would generate translation faults if there are ongoing DMAs.Replace the CD entries by using an unused temporary ASID in the following sequence to allow hitless replacement of CD entry,
- Update the CD entry with temporary ASID.
- Update the CD entry with the target TTB0.
- Invalidate the temporary ASID.
- Update the CD entry with target ASID.
- Invalidate and release the temporary ASID.The series adds a vfio selftest that triggers a continuous DMA and does the hwpt replace while the DMA is ongoing. Since domain replacement is a race with the translation requests, the test does it multiple times to capture the small window where IOMMU faults can occur.
Without this change we can observe the SMMU faults in the kernel logs and with it the SMMU faults do not occur.
Quoted 6 lines
Quoted 3 lines
This is only tested on qemu with the emulated SMMUv3 and an out of tree NVMe vfio selftest driver, not on real hardware. Following are not covered and can be done later,
Out of curiousity, does the in-tree VFIO selftests driver for IGB not work for this testing scenario?
I tried with the IGB driver first, but I couldn't get the DMAs to work. Its probably my setup, will recheck.
Sami
Robin Murphy
reviewerQuoted 11 lines · Samiullah Khawaja
This patch series adds support for replacing IOMMU domains with S1 page tables hitlessly, that is without disrupting the ongoing DMAs. This is one of the dependencies of the Liveupdate IOMMU support for Arm SMMUv3.
S1 domains are programmed by CD entries in the CD table. During a new domain attach these are updated using
arm_smmu_write_cd_entry(). Since ASID and TTB0 are in different 64-bit words,arm_smmu_write_cd_entry()considers it a non-hitless update and sets V=0. This means such an update is disruptive and would generate translation faults if there are ongoing DMAs.
Quoted 8 lines
Replace the CD entries by using an unused temporary ASID in the following sequence to allow hitless replacement of CD entry,
- Update the CD entry with temporary ASID.
- Update the CD entry with the target TTB0.
- Invalidate the temporary ASID.
- Update the CD entry with target ASID.
- Invalidate and release the temporary ASID.
Why bother transitioning through a 3rd ASID? The old and new pagetables must contain the same translations anyway, so why not just: switch ASID, switch TTB, invalidate old ASID?
It's perhaps worth noting that for correctness either way this also very much depends on no concurrent map/unmap operations being able to happen in either hwpt while the switch is ongoing, but then I guess it already depends on the caller guaranteeing that the hwpts hold identical translations, which we can't realistically verify at the driver level either...
Thanks,
Robin.
Quoted 86 lines
The series adds a vfio selftest that triggers a continuous DMA and does the hwpt replace while the DMA is ongoing. Since domain replacement is a race with the translation requests, the test does it multiple times to capture the small window where IOMMU faults can occur.
Without this change we can observe the SMMU faults in the kernel logs and with it the SMMU faults do not occur.
This is only tested on qemu with the emulated SMMUv3 and an out of tree NVMe vfio selftest driver, not on real hardware. Following are not covered and can be done later,
- S2 domains have the same problem, S2VMID is in STE qword 2 and S2TTB is in qword 3. These need the same handling with a temporary VMID.
- ATS is not tested, the test environment runs with pci=noats.Testing:
Without Fix:
TAP version 13 1..2 # Starting 2 tests from 2 test cases. # RUN vfio_iommufd_replace_hwpt_test.domain_replace.memcpy ... [ 33.267749] vfio-pci 0000:00:0a.0: enabling device (0000 -> 0002) [ 33.270709] vfio-pci 0000:00:0a.0: resetting [ 33.385682] vfio-pci 0000:00:0a.0: reset done [ 37.128021] arm-smmu-v3 9050000.smmuv3: event 0x0a received: [ 37.131885] arm-smmu-v3 9050000.smmuv3: 0x000000500000000a [ 37.134585] arm-smmu-v3 9050000.smmuv3: 0x0000000000000000 [ 37.136961] arm-smmu-v3 9050000.smmuv3: 0x0000000000000000 [ 37.139631] arm-smmu-v3 9050000.smmuv3: 0x0000000000000000 [ 37.142188] arm-smmu-v3 9050000.smmuv3: event: C_BAD_CD client: 0000:00:0a.0 sid: 0x50 ssid: 0x0 ... ...With Fix:
TAP version 13
1..2
# Starting 2 tests from 2 test cases.
# RUN vfio_iommufd_replace_hwpt_test.domain_replace.memcpy ...[ 33.418253] vfio-pci 0000:00:0a.0: enabling device (0000 -> 0002) [ 33.421218] vfio-pci 0000:00:0a.0: resetting [ 33.539750] vfio-pci 0000:00:0a.0: reset done # OK vfio_iommufd_replace_hwpt_test.domain_replace.memcpy ok 1 vfio_iommufd_replace_hwpt_test.domain_replace.memcpy # RUN vfio_iommufd_replace_hwpt_test.noreplace.memcpy ... [ 88.416453] vfio-pci 0000:00:0a.0: resetting [ 88.531989] vfio-pci 0000:00:0a.0: reset done # OK vfio_iommufd_replace_hwpt_test.noreplace.memcpy ok 2 vfio_iommufd_replace_hwpt_test.noreplace.memcpyLooking forward to your feedback on this.
Dependencies:
The patch series includes a patch from the vfio selftest multi iommu series as it is required for thehwpt_replacevfio selftest. It is part of this series since the original code doesn't apply cleanly on the base.
https://lore.kernel.org/all/[email protected]/Related Work:
Liveupdate IOMMU patch series:
https://lore.kernel.org/all/[email protected]/
Liveupdate IOMMU feature patchset breakdown:
https://docs.google.com/document/d/1enDn-uPE9U77U-xHEnzn6HHGKiePSAtMIP8EDU3NO0MSamiullah Khawaja (3):
vfio: selftests: Add support of creating iommus from iommufd
vfio: selftests: Add iommufd hwpt replace test iommu/arm-smmu-v3: Add support for hitless replace of S1 domainsdrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 66 ++++++- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/lib/include/libvfio/iommu.h | 2 + .../lib/include/libvfio/vfio_pci_device.h | 2 + tools/testing/selftests/vfio/lib/iommu.c | 60 +++++- .../selftests/vfio/lib/vfio_pci_device.c | 25 ++- .../vfio/vfio_iommufd_hwpt_replace_test.c | 171 ++++++++++++++++++ 7 files changed, 316 insertions(+), 11 deletions(-)create mode 100644
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.cbase-commit:
3d7783543c2646af69ad65825e810060494bea21
Quoted 21 lines · Robin Murphy
Quoted 11 lines · Samiullah Khawaja
This patch series adds support for replacing IOMMU domains with S1 page tables hitlessly, that is without disrupting the ongoing DMAs. This is one of the dependencies of the Liveupdate IOMMU support for Arm SMMUv3.
S1 domains are programmed by CD entries in the CD table. During a new domain attach these are updated using
arm_smmu_write_cd_entry(). Since ASID and TTB0 are in different 64-bit words,arm_smmu_write_cd_entry()considers it a non-hitless update and sets V=0. This means such an update is disruptive and would generate translation faults if there are ongoing DMAs.Quoted 8 lines
Replace the CD entries by using an unused temporary ASID in the following sequence to allow hitless replacement of CD entry,
- Update the CD entry with temporary ASID.
- Update the CD entry with the target TTB0.
- Invalidate the temporary ASID.
- Update the CD entry with target ASID.
- Invalidate and release the temporary ASID.
Quoted 3 lines
Why bother transitioning through a 3rd ASID? The old and new pagetables must contain the same translations anyway, so why not just: switch ASID, switch TTB, invalidate old ASID?
The target ASID may be in use by another CD someplace else. Doing it without the temporary could contaminate it with wrong translations from the original TTB0.
From a driver perspective we don't know why the domain replace operation was triggered. iommufd can trigger it for domains that are not identical. Corrupting the target ASID when we don't know for sure what is happening is not safe.
Obviously Samiullah's use case is hitless between the same translation so it doesn't need the extra ASID, but the driver can't know this.
Samiullah please include this explanation in a comment..
Jason
Robin Murphy
reviewerQuoted 26 lines · Jason Gunthorpe
Quoted 21 lines · Robin Murphy
Quoted 11 lines · Samiullah Khawaja
This patch series adds support for replacing IOMMU domains with S1 page tables hitlessly, that is without disrupting the ongoing DMAs. This is one of the dependencies of the Liveupdate IOMMU support for Arm SMMUv3.
S1 domains are programmed by CD entries in the CD table. During a new domain attach these are updated using
arm_smmu_write_cd_entry(). Since ASID and TTB0 are in different 64-bit words,arm_smmu_write_cd_entry()considers it a non-hitless update and sets V=0. This means such an update is disruptive and would generate translation faults if there are ongoing DMAs.Quoted 8 lines
Replace the CD entries by using an unused temporary ASID in the following sequence to allow hitless replacement of CD entry,
- Update the CD entry with temporary ASID.
- Update the CD entry with the target TTB0.
- Invalidate the temporary ASID.
- Update the CD entry with target ASID.
- Invalidate and release the temporary ASID.Quoted 3 lines
Why bother transitioning through a 3rd ASID? The old and new pagetables must contain the same translations anyway, so why not just: switch ASID, switch TTB, invalidate old ASID?
Quoted 8 lines
The target ASID may be in use by another CD someplace else. Doing it without the temporary could contaminate it with wrong translations from the original TTB0.
From a driver perspective we don't know why the domain replace
operation was triggered. iommufd can trigger it for domains that are not identical. Corrupting the target ASID when we don't know for sure what is happening is not safe.
If there can be "wrong translations" then the whole thing is fundamentally not safe to begin with.
Quoted 2 lines
Obviously Samiullah's use case is hitless between the same translation so it doesn't need the extra ASID, but the driver can't know this.
Huh? If it doesn't need to be "hitless" then there is no need to do this at all; break-before-make via an invalid CD is fine. If it does, then the translations must be identical. Doesn't matter how many intermediate ASIDs you go through, any time *one* of them could have fetched from two different pagetables, you can't say for sure that an amalgamation of TLB entries won't create something which ends up looking like a global translation, that then "corrupts" *all* ASIDs.
Thanks,
Robin.
Quoted 22 lines · Jason Gunthorpe
Quoted 21 lines · Robin Murphy
Quoted 19 lines · Samiullah Khawaja
This patch series adds support for replacing IOMMU domains with S1 page tables hitlessly, that is without disrupting the ongoing DMAs. This is one of the dependencies of the Liveupdate IOMMU support for Arm SMMUv3.
S1 domains are programmed by CD entries in the CD table. During a new domain attach these are updated using
arm_smmu_write_cd_entry(). Since ASID and TTB0 are in different 64-bit words,arm_smmu_write_cd_entry()considers it a non-hitless update and sets V=0. This means such an update is disruptive and would generate translation faults if there are ongoing DMAs.Replace the CD entries by using an unused temporary ASID in the following sequence to allow hitless replacement of CD entry,
- Update the CD entry with temporary ASID.
- Update the CD entry with the target TTB0.
- Invalidate the temporary ASID.
- Update the CD entry with target ASID.
- Invalidate and release the temporary ASID.
Quoted 7 lines
Quoted 3 lines
Why bother transitioning through a 3rd ASID? The old and new pagetables must contain the same translations anyway, so why not just: switch ASID, switch TTB, invalidate old ASID?
The target ASID may be in use by another CD someplace else. Doing it without the temporary could contaminate it with wrong translations from the original TTB0.
+1
Quoted 10 lines
From a driver perspective we don't know why the domain replace operation was triggered. iommufd can trigger it for domains that are not identical. Corrupting the target ASID when we don't know for sure what is happening is not safe.
Obviously Samiullah's use case is hitless between the same translation so it doesn't need the extra ASID, but the driver can't know this.
Samiullah please include this explanation in a comment..
I will add this in the next version.
Thanks,
Sami
Quoted 2 lines
Jason