iommufd: harden DMA-BUF PAL setup and release
Subsystems (4)
cover iommufd: harden DMA-BUF PAL setup and release
This is a narrow companion to Jason's unmerged DMA-BUF mapping-type RFC. It fixes setup and release problems in the IOMMUFD PAL consumer and adds fault- injection selftests. It is not standalone and requires this exact base:
c37cf2824740d ("cover-letter: Add DMA-buf mapping types and convert
vfio/iommufd to use them")
I used LLM assistance while inspecting, testing and drafting this series.
I also tested DMA-BUF paths on a Raspberry Pi 400 and Pi 500+, using their stock Raspberry Pi kernels (6.18.39+rpt-rpi-v8 and 6.18.34+rpt-rpi-2712). Both passed offscreen CPU/V3D byte checks. The Pi 400 also passed five raw-MJPG payload-prefix comparisons through V3D; this was not direct or zero-copy camera DMA. Neither board ran this RFC or IOMMUFD PAL. The Pi 500+ connector state and mode remained unchanged; the tested buffers were not scanned out.
RFC:
https://lore.kernel.org/r/[email protected]/
Jason's branch:
https://github.com/jgunthorpe/linux/commits/dmabuf_map_type
This series:
https://github.com/kmehltretter82/linux/commits/dmabuf-pal-lifecycle-rfc-v1
Diederik's RK3588 testing produced a useful trace through rockchip_gem_iommu_map() and iommu_map_sg():
https://lore.kernel.org/r/[email protected]/
Jason noted that this showed the same private-IOMMU-domain mapping need as IOMMUFD:
https://lore.kernel.org/r/[email protected]/
That trace is motivation only. This series neither revives my warn-only mechanism nor converts Rockchip. Jason's feedback on that mechanism is here:
https://lore.kernel.org/r/[email protected]/
Compared with Jason's branch, this series:
- preserves dma_buf_pin() errors;
- publishes the attachment and successful PAL list together under resv;
- unmaps and clears the published PAL pointer under resv before unlocking;
- keeps callback state alive until dma_buf_detach() returns;
- calls dma_buf_unpin() while holding resv; and
- tests pin/map failures, cleanup counters and retry on the same DMA-BUF.
Patch 3 follows an earlier forced-window arm64 QEMU/KASAN double-free report:
https://lore.kernel.org/r/[email protected]/
The proposed release order is:
resv lock -> unmap and clear PAL -> unpin -> unlock -> detach -> put -> destroy the pages mutex
Questions:
1. Is that publication and lifetime rule correct?
2. Is c37cf2824740d still the right base for this companion series? If not, which tree should I use?
3. Can these lifecycle fixes stand independently of moving final mapping selection closer to map time?
https://lore.kernel.org/r/[email protected]/
https://lore.kernel.org/r/[email protected]/
4. Is the per-buffer selftest control acceptable, or would deterministic setup/release race hooks be more useful?
Testing:
- arm64 QEMU virt: two boots, 24 selected cases per boot, zero skips;
- x86-64 KVM q35: two 24-case candidate boots plus one focused four-case
pin-failure boot;
- a one-defect x86 control failed all four pin-failure variants on the
expected erroneous successful ioctl return; - x86-64 GCC 13.3 built the exact head, headers and IOMMUFD selftests; this build was not booted; and
- KASAN and lockdep initialized with no diagnostics in candidate runs.
The QEMU/KVM campaigns use mock IOMMU domains and the selftest exporter. They do not cover a non-selftest PAL exporter, physical DMA/IOMMU, forced invalidation races, or the complete IOMMUFD selftest suite. The x86 negative control validates patch 1 only; the earlier patch-3 report is separate.
Testing with a non-selftest PAL exporter would be welcome, especially failure followed by retry, invalidation during setup/release, repeated revoke, and multi-range buffers. Please include the base, config, architecture, exporter/device and full dmesg. A full IOMMUFD selftest run on another configuration would also help.
Thanks,
Karl
Karl Mehltretter (6):
iommufd: Preserve DMA-buf pin errors
iommufd: Publish successful PAL mappings under resv
iommufd: Clear the PAL list before dropping resv
iommufd: Keep the pages mutex alive until DMA-buf detach
iommufd: Unpin the DMA-buf while holding resv iommufd/selftest: Check DMA-buf pin and map failure cleanup
drivers/iommu/iommufd/iommufd_test.h | 24 ++++ drivers/iommu/iommufd/pages.c | 24 ++-- drivers/iommu/iommufd/selftest.c | 111 ++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 121 ++++++++++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 30 +++++ 5 files changed, 298 insertions(+), 12 deletions(-)
base-commit: c37cf2824740d833b21eb816e21f94836100291f
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH 1/6 iommufd: Preserve DMA-buf pin errors
If dma_buf_pin() fails, iopt_map_dmabuf() replaces its error with PTR_ERR(pages->dmabuf.exp_phys). The newly allocated pages object still has a NULL exp_phys, so the function detaches and returns zero. Its caller then accepts a DMA-buf pages object with no attachment.
Keep the error returned by dma_buf_pin() when unwinding the attachment.
--- drivers/iommu/iommufd/pages.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iommu/iommufd/pages.c +1 -3
@@ -1510,10 +1510,8 @@ static int iopt_map_dmabuf(struct iommufd_ctx *ictx, struct iopt_pages *pages, } rc =dma_buf_pin (attach); -if (rc) { - rc =PTR_ERR (pages->dmabuf.exp_phys); +if (rc) goto err_detach; - } pages->dmabuf.exp_phys =dma_buf_pal_map_phys (attach);if (IS_ERR (pages->dmabuf.exp_phys)) {
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH 2/6 iommufd: Publish successful PAL mappings under resv
In reply to Karl Mehltretteriopt_map_dmabuf() stores the PAL map result in exp_phys before checking for errors and publishes the attachment after dropping resv. The attachment is already linked to the DMA-buf. An invalidation after the unlock can therefore pass a NULL attachment to dma_buf_pal_unmap_phys(). On map failure, the non-NULL error pointer also looks like a live mapping to iopt_dmabuf_revoked().
Keep the map result local until it succeeds. Publish the attachment and physical list together before dropping resv, leaving exp_phys NULL on failure.
--- drivers/iommu/iommufd/pages.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/iommu/iommufd/pages.c +6 -5
@@ -1491,6 +1491,7 @@ static int iopt_map_dmabuf(struct iommufd_ctx *ictx, struct iopt_pages *pages, {struct dma_buf_mapping_match pal_match[] = {DMA_BUF_IMAPPING_PAL () };struct dma_buf_attachment *attach; +struct dma_buf_phys_list *phys;int rc; attach = dma_buf_mapping_attach(dmabuf, pal_match,
@@ -1513,16 +1514,16 @@ static int iopt_map_dmabuf(struct iommufd_ctx *ictx, struct iopt_pages *pages,if (rc) goto err_detach; - pages->dmabuf.exp_phys =dma_buf_pal_map_phys (attach); -if (IS_ERR (pages->dmabuf.exp_phys)) { - rc =PTR_ERR (pages->dmabuf.exp_phys); + phys =dma_buf_pal_map_phys (attach); +if (IS_ERR (phys)) { + rc =PTR_ERR (phys); goto err_unpin; } -dma_resv_unlock (dmabuf->resv); -/* On success iopt_release_pages() will detach and put the dmabuf. */ pages->dmabuf.attach = attach; + pages->dmabuf.exp_phys = phys; +dma_resv_unlock (dmabuf->resv);return 0 ; err_unpin:
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH 3/6 iommufd: Clear the PAL list before dropping resv
In reply to Karl Mehltretteriopt_release_pages() frees the PAL physical list while holding resv but leaves exp_phys pointing to it. Between the reservation unlock and dma_buf_detach(), an invalidation can see that pointer and unmap the same list again.
Clear exp_phys before dropping resv so a later invalidation sees that there is no live PAL mapping.
--- drivers/iommu/iommufd/pages.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iommu/iommufd/pages.c +3 -1
@@ -1674,9 +1674,11 @@ void iopt_release_pages(struct kref *kref)struct dma_buf *dmabuf = pages->dmabuf.attach->dmabuf;dma_resv_lock (dmabuf->resv,NULL ); -if (pages->dmabuf.exp_phys) +if (pages->dmabuf.exp_phys) {dma_buf_pal_unmap_phys (pages->dmabuf.attach, pages->dmabuf.exp_phys); + pages->dmabuf.exp_phys =NULL ; + }dma_resv_unlock (dmabuf->resv);dma_buf_unpin (pages->dmabuf.attach);
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH 4/6 iommufd: Keep the pages mutex alive until DMA-buf detach
In reply to Karl Mehltretteriopt_release_pages() destroys pages->mutex before removing the DMA-buf attachment. The linked attachment still exposes pages through importer_priv. An invalidation can enter iopt_revoke_notify() and lock the destroyed mutex, including when exp_phys is already NULL.
Destroy pages->mutex after dma_buf_detach() has removed the attachment and excluded further invalidation callbacks.
--- drivers/iommu/iommufd/pages.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/iommufd/pages.c +1 -1
@@ -1667,7 +1667,6 @@ void iopt_release_pages(struct kref *kref)WARN_ON (pages->npinned);WARN_ON (!xa_empty (&pages->pinned_pfns));mmdrop (pages->source_mm); -mutex_destroy (&pages->mutex);put_task_struct (pages->source_task);free_uid (pages->source_user);if (iopt_is_dmabuf (pages) && pages->dmabuf.attach) {
@@ -1689,6 +1688,7 @@ void iopt_release_pages(struct kref *kref) }else if (pages->type ==IOPT_ADDRESS_FILE ) {fput (pages->file); } +mutex_destroy (&pages->mutex);kfree (pages); }
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH 5/6 iommufd: Unpin the DMA-buf while holding resv
In reply to Karl Mehltretteriopt_release_pages() calls dma_buf_unpin() after releasing the DMA-buf reservation lock. The unpin interface requires the reservation to be held while it invokes the exporter callback.
Move the unpin into the reservation critical section used to release the PAL physical list.
--- drivers/iommu/iommufd/pages.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iommu/iommufd/pages.c +1 -2
@@ -1678,9 +1678,8 @@ void iopt_release_pages(struct kref *kref) pages->dmabuf.exp_phys); pages->dmabuf.exp_phys =NULL ; } -dma_resv_unlock (dmabuf->resv); -dma_buf_unpin (pages->dmabuf.attach); +dma_resv_unlock (dmabuf->resv);dma_buf_detach (dmabuf, pages->dmabuf.attach);dma_buf_put (dmabuf);
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH 6/6 iommufd/selftest: Check DMA-buf pin and map failure cleanup
In reply to Karl MehltretterThe PAL selftest exporter has no pin callback, so it cannot exercise the importer's dma_buf_pin() error path. An error there must reach userspace without mapping the buffer or unpinning an unsuccessful pin.
Add per-buffer error injection and reservation-protected lifecycle counters. Check pin and map failure cleanup, then clear each fault and retry the same buffer to verify balanced successful cleanup.
Also reject unknown control flags, nonzero reserved fields, invalid errno values and configuration changes while an attachment exists.
--- drivers/iommu/iommufd/iommufd_test.h | 24 ++++ drivers/iommu/iommufd/selftest.c | 111 ++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 121 ++++++++++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 30 +++++ 4 files changed, 286 insertions(+)
drivers/iommu/iommufd/iommufd_test.h +24 -0
@@ -32,6 +32,7 @@ enum {IOMMU_TEST_OP_DMABUF_GET ,IOMMU_TEST_OP_DMABUF_REVOKE ,IOMMU_TEST_OP_MD_CHECK_DMABUF , + IOMMU_TEST_OP_DMABUF_CONTROL, }; enum {
@@ -80,6 +81,21 @@ enum {/* Reserved for special pasid replace test */ #define IOMMU_TEST_PASID_RESERVED 1024 +enum { +IOMMU_TEST_DMABUF_SET_ERRORS =1 <<0 , +}; + +struct iommu_test_dmabuf_stats { +__u32 attach_calls; +__u32 detach_calls; +__u32 pin_calls; +__u32 pin_successes; +__u32 unpin_calls; +__u32 map_calls; +__u32 map_successes; +__u32 unmap_calls; +}; +struct iommu_test_cmd {__u32 size;__u32 op;
@@ -201,6 +217,14 @@ struct iommu_test_cmd {__aligned_u64 length;__aligned_u64 offset; } check_dmabuf; +struct { +__s32 dmabuf_fd; +__u32 flags; +/* Positive errno, or zero to disable injection. */ +__s32 pin_errno; +__s32 map_errno; +struct iommu_test_dmabuf_stats out_stats; + } dmabuf_control; };__u32 last; };
drivers/iommu/iommufd/selftest.c +111 -0
159 lines not on this page · raw message
tools/testing/selftests/iommu/iommufd.c +121 -0
127 lines not on this page · raw message
tools/testing/selftests/iommu/iommufd_utils.h +30 -0
@@ -593,6 +593,36 @@ static int _test_cmd_revoke_dmabuf(int fd, int dmabuf_fd, bool revoked)#define test_cmd_revoke_dmabuf (dmabuf_fd, revoke) \ ASSERT_EQ(0, _test_cmd_revoke_dmabuf(self->fd, dmabuf_fd, revoke)) +static int _test_cmd_dmabuf_control (int fd,int dmabuf_fd,unsigned int flags, +int pin_errno,int map_errno, +struct iommu_test_dmabuf_stats *stats) +{ +struct iommu_test_cmd cmd = { + .size =sizeof (cmd), + .op =IOMMU_TEST_OP_DMABUF_CONTROL , + .dmabuf_control = { + .dmabuf_fd = dmabuf_fd, + .flags = flags, + .pin_errno = pin_errno, + .map_errno = map_errno, + }, + }; +int rc; + + rc =ioctl (fd,IOMMU_TEST_CMD , &cmd); +if (!rc && stats) + *stats = cmd.dmabuf_control.out_stats; +return rc; +} + +#define test_cmd_dmabuf_errors (dfd, pin_error, map_error) \ + ASSERT_EQ(0, _test_cmd_dmabuf_control( \ + self->fd, dfd, IOMMU_TEST_DMABUF_SET_ERRORS, pin_error, \ + map_error, NULL)) + +#define test_cmd_dmabuf_stats (dfd, stats) \ + ASSERT_EQ(0, _test_cmd_dmabuf_control(self->fd, dfd, 0, 0, 0, stats)) +#define test_cmd_check_dmabuf (_hwpt_id, _dmabuf_fd, _iova, _length, _offset) \ ({ \ struct iommu_test_cmd check_cmd = { \
Signature · 1 line
2.39.5 (Apple Git-154)
On 2026-10-20 at 17:37 +1100, Karl Mehltretter <[email protected]> wrote...
Quoted 58 lines
This is a narrow companion to Jason's unmerged DMA-BUF mapping-type RFC. It fixes setup and release problems in the IOMMUFD PAL consumer and adds fault- injection selftests. It is not standalone and requires this exact base:
c37cf2824740d("cover-letter: Add DMA-buf mapping types and convert
vfio/iommufd to use them")I used LLM assistance while inspecting, testing and drafting this series.
I also tested DMA-BUF paths on a Raspberry Pi 400 and Pi 500+, using their stock Raspberry Pi kernels (6.18.39+rpt-rpi-v8 and 6.18.34+rpt-rpi-2712). Both passed offscreen CPU/V3D byte checks. The Pi 400 also passed five raw-MJPG payload-prefix comparisons through V3D; this was not direct or zero-copy camera DMA. Neither board ran this RFC or IOMMUFD PAL. The Pi 500+ connector state and mode remained unchanged; the tested buffers were not scanned out.
RFC:
https://lore.kernel.org/r/[email protected]/Jason's branch:
https://github.com/jgunthorpe/linux/commits/dmabuf_map_typeThis series:
https://github.com/kmehltretter82/linux/commits/dmabuf-pal-lifecycle-rfc-v1Diederik's RK3588 testing produced a useful trace through
rockchip_gem_iommu_map()andiommu_map_sg():https://lore.kernel.org/r/[email protected]/
Jason noted that this showed the same private-IOMMU-domain mapping need as IOMMUFD:
https://lore.kernel.org/r/[email protected]/
That trace is motivation only. This series neither revives my warn-only mechanism nor converts Rockchip. Jason's feedback on that mechanism is here:
https://lore.kernel.org/r/[email protected]/
Compared with Jason's branch, this series:
- preserves
dma_buf_pin()errors;
- publishes the attachment and successful PAL list together under resv;
- unmaps and clears the published PAL pointer under resv before unlocking;
- keeps callback state alive untildma_buf_detach()returns;
- callsdma_buf_unpin()while holding resv; and
- tests pin/map failures, cleanup counters and retry on the same DMA-BUF.Patch 3 follows an earlier forced-window arm64 QEMU/KASAN double-free report:
https://lore.kernel.org/r/[email protected]/
The proposed release order is:
resv lock -> unmap and clear PAL -> unpin -> unlock -> detach -> put -> destroy the pages mutex
Quoted 6 lines
Questions:
1. Is that publication and lifetime rule correct?
2. Is
c37cf2824740dstill the right base for this companion series? If not, which tree should I use?
The discussion upstream on whether the PAL approach is acceptable still hasn't settled AFAICT. I'm starting to look at alternate approaches but don't have anything concrete yet. However as part of that work I have rebased Jason's branch onto a more recent upstream (v7.3-rc5) so if you wanted to rebase this work onto that it could be useful.
I haven't had a chance to look at these patches yet, but if some of them are fixing bugs in Jason's series it might be best for us to fold them into that series, with correct attribution of course. Anyway I will take a closer look in the morning.
Thanks.
- Alistair
Quoted 54 lines
3. Can these lifecycle fixes stand independently of moving final mapping selection closer to map time?
https://lore.kernel.org/r/[email protected]/ https://lore.kernel.org/r/[email protected]/4. Is the per-buffer selftest control acceptable, or would deterministic setup/release race hooks be more useful?
Testing:
- arm64 QEMU virt: two boots, 24 selected cases per boot, zero skips;
- x86-64 KVM q35: two 24-case candidate boots plus one focused four-casepin-failure boot; - a one-defect x86 control failed all four pin-failure variants on the expected erroneous successful ioctl return;- x86-64 GCC 13.3 built the exact head, headers and IOMMUFD selftests; this build was not booted; and
- KASAN and lockdep initialized with no diagnostics in candidate runs.The QEMU/KVM campaigns use mock IOMMU domains and the selftest exporter. They do not cover a non-selftest PAL exporter, physical DMA/IOMMU, forced invalidation races, or the complete IOMMUFD selftest suite. The x86 negative control validates patch 1 only; the earlier patch-3 report is separate.
Testing with a non-selftest PAL exporter would be welcome, especially failure followed by retry, invalidation during setup/release, repeated revoke, and multi-range buffers. Please include the base, config, architecture, exporter/device and full dmesg. A full IOMMUFD selftest run on another configuration would also help.
Thanks,
KarlKarl Mehltretter (6):
iommufd: Preserve DMA-buf pin errors
iommufd: Publish successful PAL mappings under resv
iommufd: Clear the PAL list before dropping resv
iommufd: Keep the pages mutex alive until DMA-buf detach
iommufd: Unpin the DMA-buf while holding resv iommufd/selftest: Check DMA-buf pin and map failure cleanupdrivers/iommu/iommufd/iommufd_test.h | 24 ++++ drivers/iommu/iommufd/pages.c | 24 ++-- drivers/iommu/iommufd/selftest.c | 111 ++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 121 ++++++++++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 30 +++++ 5 files changed, 298 insertions(+), 12 deletions(-)base-commit:
c37cf2824740d833b21eb816e21f94836100291fSignature · 1 line
2.39.5 (Apple Git-154)
Jason Gunthorpe
maintainerQuoted 4 lines · Alistair Popple
I haven't had a chance to look at these patches yet, but if some of them are fixing bugs in Jason's series it might be best for us to fold them into that series, with correct attribution of course. Anyway I will take a closer look in the morning.
Are any fixing bugs in tree right now? I think one of them is already applied from someone else..
Jason
Quoted 10 lines · Jason Gunthorpe
Quoted 4 lines · Alistair Popple
I haven't had a chance to look at these patches yet, but if some of them are fixing bugs in Jason's series it might be best for us to fold them into that series, with correct attribution of course. Anyway I will take a closer look in the morning.
Are any fixing bugs in tree right now? I think one of them is already applied from someone else..
Good catch. None of these fixes a bug in the current mainline tree. I based the series on the stale c37cf2824740d topic and missed two fixes that landed in mainline afterward:
- patch 4 is already covered more broadly by f2d70dbd3dce ("iommufd: Destroy the pages content after detaching from dmabuf");
- patch 5 duplicates Ankit Soni's e745cd2c749e
("iommufd: Take dma_resv lock before dma_buf_unpin() in release path").
Patches 1-3 address lifecycle issues introduced by the PAL conversion, and patch 6 tests those paths.
My main goal with this RFC was to put the concrete lifecycle issues and tests on the table and get the discussion moving.
I am not concerned about preserving these as separate commits for attribution purposes. Please fold or rework any useful pieces into the mapping-type series as makes sense.
Thanks,
Karl
On 2026-10-21 at 12:45 +1100, Karl Mehltretter <[email protected]> wrote...
Quoted 24 lines · Jason Gunthorpe
Quoted 10 lines · Alistair Popple
Quoted 5 lines
I haven't had a chance to look at these patches yet, but if some of them are fixing bugs in Jason's series it might be best for us to fold them into that series, with correct attribution of course. Anyway I will take a closer look in the morning.
Are any fixing bugs in tree right now? I think one of them is already applied from someone else..
Good catch. None of these fixes a bug in the current mainline tree. I based the series on the stale
c37cf2824740dtopic and missed two fixes that landed in mainline afterward:- patch 4 is already covered more broadly by
f2d70dbd3dce("iommufd: Destroy the pages content after detaching from dmabuf");
- patch 5 duplicates Ankit Soni'se745cd2c749e
("iommufd: Takedma_resvlock beforedma_buf_unpin()in release path").Patches 1-3 address lifecycle issues introduced by the PAL conversion, and patch 6 tests those paths.
Quoted 6 lines
My main goal with this RFC was to put the concrete lifecycle issues and tests on the table and get the discussion moving.
I am not concerned about preserving these as separate commits for attribution purposes. Please fold or rework any useful pieces into the mapping-type series as makes sense.
Will do. I hope to post a v2 later this week so will fold them into that. Thanks.
- Alistair
Quoted 2 lines
Thanks,
Karl