iommufd: Keep dmabuf MMIO attributes in domains added later
Subsystems (4)
v2 cover iommufd: Keep dmabuf MMIO attributes in domains added later
A VFIO PCI dmabuf mapped into an IOAS gets IOMMU_MMIO only in the domains that exist when it is mapped. A domain attached afterwards, or an IOMMU_IOAS_COPY of the area, reads the PFNs back from the existing domain and maps the BAR as cacheable CPU memory. Patch 1 always takes dmabuf PFNs from the recorded phys range instead.
Sashiko first reported this on patch 6 of David Woodhouse's RFC "KVM:
Allow alternative providers of guest_memfd backed by PFNMAP memory", which picks the batch kind in pfn_reader_fill_dmabuf() from the exporter's memory type. This fix does not depend on that series and combines with it: with both, a domain added later gets the kind the exporter reports.
The mock page table has no memory type bits, so the existing selftests cannot see the difference. Patch 2 makes the mock record, per domain, the pages mapped with IOMMU_MMIO, adds IOMMU_TEST_OP_MD_CHECK_MMIO, and checks the domain filled later in two new tests, dmabuf_mmio_new_domain and dmabuf_mmio_copy.
Tested on x86-64 under virtme-ng with CONFIG_IOMMUFD_TEST=y. Without patch 1 both new tests fail on the second domain in all three mock domain variants; in dmabuf_mmio_new_domain, a temporary print in batch_to_domain() showed that domain mapped with IOMMU_READ | IOMMU_WRITE | IOMMU_CACHE, where the first got IOMMU_READ | IOMMU_WRITE | IOMMU_MMIO. With patch 1 the dmabuf tests pass, and the other iommufd selftests give the same results as on the base tree. Patch 2 also builds for i386 with W=1.
Not tested on hardware that uses the memory type bits (ARM SMMUv3, AMD with SME, RISC-V).
Changes in v2:
- Validate the MMIO check range before converting 64-bit ioctl inputs to native-width page indices. Reject range overflow and values that cannot be represented on 32-bit kernels. (Sashiko)
- Patch 1 is unchanged.
V1: https://lore.kernel.org/all/[email protected]/
Andrea Parri (2):
iommufd: Keep dmabuf PFNs MMIO when filling another domain iommufd/selftest: Check dmabuf MMIO mappings filled from another
domain
drivers/iommu/iommufd/iommufd_test.h | 5 + drivers/iommu/iommufd/pages.c | 13 ++- drivers/iommu/iommufd/selftest.c | 92 +++++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 51 ++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 13 +++ 5 files changed, 170 insertions(+), 4 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/2 iommufd: Keep dmabuf PFNs MMIO when filling another domain
A VFIO PCI dmabuf mapped into an IOAS is mapped with IOMMU_MMIO only in the domains present when it is mapped. A domain added later, or a copy of the area into another IOAS, maps the same BAR as cacheable CPU
memory: IOMMU_CACHE set and IOMMU_MMIO clear.
Once the area is in pages->domains_itree, pfn_reader_fill_span() reads its PFNs back from the storage domain through batch_from_domain(), which adds them with batch_add_pfn() as BATCH_CPU_MEMORY. Only a hole in the span reaches pfn_reader_fill_dmabuf() and gets BATCH_MMIO, so batch_to_domain() programs the later domain with the wrong prot.
The effect depends on the page table format. io-pgtable-arm maps the BAR as Normal cacheable instead of Device memory, the AMDv1 and x86_64 generic_pt formats set the SME C-bit on it when the tables are encrypted, and the RISC-V format with Svpbmt maps it as normal memory instead of IO.
Read dmabuf PFNs from the recorded phys for every span, before the xarray and domain paths. The phys range is always available, and the read-back from a domain only serves to avoid re-pinning user pages.
Sashiko pointed this out while reviewing an RFC that plumbs the dma-buf memory type through pfn_reader_fill_dmabuf().
--- drivers/iommu/iommufd/pages.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/iommu/iommufd/pages.c +9 -4
@@ -1178,6 +1178,15 @@ static int pfn_reader_fill_span(struct pfn_reader *pfns) WARN_ON(span->last_used < start_index))return -EINVAL ; +/* +* Always read a dmabuf from its phys, even where a domain already +* maps it: a domain can only report CPU memory PFNs, losing +* BATCH_MMIO. last_hole aliases last_used, so this covers any span. +*/ +if (iopt_is_dmabuf (pfns->pages)) +return pfn_reader_fill_dmabuf (&pfns->dmabuf, &pfns->batch, + start_index, span->last_hole); +if (span->is_used ==1 ) {batch_from_xarray (&pfns->batch, &pfns->pages->pinned_pfns, start_index, span->last_used);
@@ -1201,10 +1210,6 @@ static int pfn_reader_fill_span(struct pfn_reader *pfns)return 0 ; } -if (iopt_is_dmabuf (pfns->pages)) -return pfn_reader_fill_dmabuf (&pfns->dmabuf, &pfns->batch, - start_index, span->last_hole); - user = &pfns->user;if (start_index >= user->upages_end) { rc = pfn_reader_user_pin(user, pfns->pages, start_index,
Signature · 1 line
2.53.0
PATCH v2 2/2 iommufd/selftest: Check dmabuf MMIO mappings filled from another domain
In reply to Andrea ParriAdd tests that a dmabuf keeps IOMMU_MMIO in a domain that is filled from an already mapped area: a second domain added to the IOAS after the dmabuf has been mapped, and the domain of another IOAS the dmabuf is copied into.
The mock page table has no memory type bits, so a mapping made with IOMMU_MMIO cannot be told apart from one made with IOMMU_CACHE. Wrap the mock's generic_pt map_range and unmap_range ops to record, per domain, the pages mapped with IOMMU_MMIO, and add IOMMU_TEST_OP_MD_CHECK_MMIO to check that a range is recorded. The check rejects ranges that wrap or do not fit in unsigned long.
dmabuf_mmio_new_domain maps a mock dmabuf, checks the existing domain, allocates a second HWPT on the same IOAS and checks it too. dmabuf_mmio_copy maps a mock dmabuf, copies it with IOMMU_IOAS_COPY into a second IOAS that has its own mock domain, and checks that domain. Without the previous patch the checks on the second domain fail in the three mock domain variants; with it all checks pass.
--- drivers/iommu/iommufd/iommufd_test.h | 5 + drivers/iommu/iommufd/selftest.c | 92 +++++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 51 ++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 13 +++ 4 files changed, 161 insertions(+)
drivers/iommu/iommufd/iommufd_test.h +5 -0
@@ -31,6 +31,7 @@ enum {IOMMU_TEST_OP_PASID_CHECK_HWPT ,IOMMU_TEST_OP_DMABUF_GET ,IOMMU_TEST_OP_DMABUF_REVOKE , + IOMMU_TEST_OP_MD_CHECK_MMIO, }; enum {
@@ -109,6 +110,10 @@ struct iommu_test_cmd {__aligned_u64 length;__aligned_u64 uptr; } check_map; +struct { +__aligned_u64 iova; +__aligned_u64 length; + } check_mmio;struct {__aligned_u64 length;__aligned_u64 uptr;
drivers/iommu/iommufd/selftest.c +92 -0
134 lines not on this page · raw message
tools/testing/selftests/iommu/iommufd.c +51 -0
57 lines not on this page · raw message
tools/testing/selftests/iommu/iommufd_utils.h +13 -0
@@ -560,6 +560,19 @@ static int _test_cmd_destroy_access_pages(int fd, unsigned int access_id,EXPECT_ERRNO (_errno,_test_cmd_destroy_access_pages ( \ self->fd, access_id, access_pages_id)) +#define test_cmd_md_check_mmio (_hwpt_id, _iova, _length) \ + ({ \ + struct iommu_test_cmd check_cmd = { \ + .size = sizeof(check_cmd), \ + .op = IOMMU_TEST_OP_MD_CHECK_MMIO, \ + .id = _hwpt_id, \ + .check_mmio = { .iova = _iova, .length = _length }, \ + }; \ + ASSERT_EQ(0, ioctl(self->fd, \ + _IOMMU_TEST_CMD(IOMMU_TEST_OP_MD_CHECK_MMIO), \ + &check_cmd)); \ + }) +static int _test_cmd_get_dmabuf (int fd,size_t len,int *out_fd) {struct iommu_test_cmd cmd = {
Signature · 1 line
2.53.0
Jason Gunthorpe
maintainerRe: [PATCH v2 2/2] iommufd/selftest: Check dmabuf MMIO mappings filled from another domain
Quoted 11 lines
Add tests that a dmabuf keeps
IOMMU_MMIOin a domain that is filled from an already mapped area: a second domain added to the IOAS after the dmabuf has been mapped, and the domain of another IOAS the dmabuf is copied into.The mock page table has no memory type bits, so a mapping made with
IOMMU_MMIOcannot be told apart from one made withIOMMU_CACHE. Wrap the mock'sgeneric_ptmap_rangeandunmap_rangeops to record, per domain, the pages mapped withIOMMU_MMIO, and addIOMMU_TEST_OP_MD_CHECK_MMIOto check that a range is recorded. The check rejects ranges that wrap or do not fit in unsigned long.
I'm not keen on this, the special mock amd domain is just for mock usage, we should probably just add a mmio bit if we want to do this test.
Signature · 1 line
Jason
Re: [PATCH v2 2/2] iommufd/selftest: Check dmabuf MMIO mappings filled from another domain
Quoted 10 lines · Jason Gunthorpe
Quoted 6 lines
The mock page table has no memory type bits, so a mapping made with
IOMMU_MMIOcannot be told apart from one made withIOMMU_CACHE. Wrap the mock'sgeneric_ptmap_rangeandunmap_rangeops to record, per domain, the pages mapped withIOMMU_MMIO, and addIOMMU_TEST_OP_MD_CHECK_MMIOto check that a range is recorded. The check rejects ranges that wrap or do not fit in unsigned long.I'm not keen on this, the special mock amd domain is just for mock usage, we should probably just add a mmio bit if we want to do this test.
Makes sense, thanks. For v3 I have:
- AMDV1PT_FMT_MOCK_MMIO (bit 52) in amdv1.h under AMDV1_IOMMUFD_SELFTEST, set by amdv1pt_iommu_set_prot() for IOMMU_MMIO and kept by amdv1pt_attr_from_entry();
- pt_iommu_amdv1_mock_iova_is_mmio() in fmt/iommu_mock.c, a walk like iova_to_phys() that returns the bit, exported in GENERIC_PT_IOMMU for IOMMU_TEST_OP_MD_CHECK_MMIO to use.
The op wrappers and the xarray in mock_iommu_domain are gone. Is the extra export from iommu_mock.c OK with you, or would you rather the selftest read the PTE some other way?
Patch 1 is unchanged; if it looks right to you, it could go in ahead of the test.
Thanks,
Andrea
Jason Gunthorpe
maintainerRe: [PATCH v2 2/2] iommufd/selftest: Check dmabuf MMIO mappings filled from another domain
Quoted 22 lines · Andrea Parri
Quoted 10 lines · Jason Gunthorpe
Quoted 6 lines
The mock page table has no memory type bits, so a mapping made with
IOMMU_MMIOcannot be told apart from one made withIOMMU_CACHE. Wrap the mock'sgeneric_ptmap_rangeandunmap_rangeops to record, per domain, the pages mapped withIOMMU_MMIO, and addIOMMU_TEST_OP_MD_CHECK_MMIOto check that a range is recorded. The check rejects ranges that wrap or do not fit in unsigned long.I'm not keen on this, the special mock amd domain is just for mock usage, we should probably just add a mmio bit if we want to do this test.
Makes sense, thanks. For v3 I have:
-
AMDV1PT_FMT_MOCK_MMIO(bit 52) inamdv1.hunderAMDV1_IOMMUFD_SELFTEST, set byamdv1pt_iommu_set_prot()forIOMMU_MMIOand kept byamdv1pt_attr_from_entry();-
pt_iommu_amdv1_mock_iova_is_mmio()infmt/iommu_mock.c, a walk likeiova_to_phys()that returns the bit, exported inGENERIC_PT_IOMMUforIOMMU_TEST_OP_MD_CHECK_MMIOto use.
Quoted 6 lines
The op wrappers and the xarray in
mock_iommu_domainare gone. Is the extra export fromiommu_mock.cOK with you, or would you rather the selftest read the PTE some other way?Patch 1 is unchanged; if it looks right to you, it could go in ahead of the test.
Yeah I will pick up patch 1, I am just stuck on all these dirty tracking patches right now
Jason
Jason Gunthorpe
maintainerRe: [PATCH v2 2/2] iommufd/selftest: Check dmabuf MMIO mappings filled from another domain
In reply to Andrea ParriQuoted 18 lines · Andrea Parri
Quoted 10 lines · Jason Gunthorpe
Quoted 6 lines
The mock page table has no memory type bits, so a mapping made with
IOMMU_MMIOcannot be told apart from one made withIOMMU_CACHE. Wrap the mock'sgeneric_ptmap_rangeandunmap_rangeops to record, per domain, the pages mapped withIOMMU_MMIO, and addIOMMU_TEST_OP_MD_CHECK_MMIOto check that a range is recorded. The check rejects ranges that wrap or do not fit in unsigned long.I'm not keen on this, the special mock amd domain is just for mock usage, we should probably just add a mmio bit if we want to do this test.
Makes sense, thanks. For v3 I have:
-
AMDV1PT_FMT_MOCK_MMIO(bit 52) inamdv1.hunderAMDV1_IOMMUFD_SELFTEST, set byamdv1pt_iommu_set_prot()forIOMMU_MMIOand kept byamdv1pt_attr_from_entry();
Quoted 7 lines
-
pt_iommu_amdv1_mock_iova_is_mmio()infmt/iommu_mock.c, a walk likeiova_to_phys()that returns the bit, exported inGENERIC_PT_IOMMUforIOMMU_TEST_OP_MD_CHECK_MMIOto use.The op wrappers and the xarray in
mock_iommu_domainare gone. Is the extra export fromiommu_mock.cOK with you, or would you rather the selftest read the PTE some other way?
Too much AI.
This sounds better, but I don't know if we need all this code just for a selftest.
Jason
Re: [PATCH v2 2/2] iommufd/selftest: Check dmabuf MMIO mappings filled from another domain
Quoted 2 lines · Jason Gunthorpe
This sounds better, but I don't know if we need all this code just for a selftest.
Fair enough, I'll drop the selftest. Thanks for picking up patch 1.
Andrea
Re: (subset) [PATCH v2 0/2] iommufd: Keep dmabuf MMIO attributes in domains added later
In reply to Andrea ParriQuoted 9 lines · Andrea Parri
iommufd: Keep dmabuf MMIO attributes in domains added later
A VFIO PCI dmabuf mapped into an IOAS gets
IOMMU_MMIOonly in the domains that exist when it is mapped. A domain attached afterwards, or anIOMMU_IOAS_COPYof the area, reads the PFNs back from the existing domain and maps the BAR as cacheable CPU memory. Patch 1 always takes dmabuf PFNs from the recorded phys range instead.[...]
Applied, thanks!
[1/2] iommufd: Keep dmabuf PFNs MMIO when filling another domain
https://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd.git/commit/?id=769f2cd9226c96619d77fa16af84de74cae41b6b
Best regards,
Signature · 1 line
Jason