From: Qihang <q.h.hack.winter@gmail.com>
To: Jason Gunthorpe <jgg@ziepe.ca>, Kevin Tian <kevin.tian@intel.com>
Cc: Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>,
Robin Murphy <robin.murphy@arm.com>,
iommu@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [PATCH] iommufd: Unmap the failing batch before releasing its pins
Date: Thu, 19 Nov 2026 00:40:26 +0800 [thread overview]
Message-ID: <20261118164026.54455-1-q.h.hack.winter@gmail.com> (raw)
iopt_area_fill_domains() maps the PFNs of one batch into every domain
and only then moves on to the next batch. If batch_to_domain() fails
for one domain after an earlier domain has already installed the batch,
the unwind path calls pfn_reader_release_pins() at the top of
out_unmap. That unpins the pages of the failing batch while the
domains that did install it still translate those IOVAs, and only the
loop below removes those mappings. For the duration of that loop a
device can DMA to a page that is no longer pinned, so it can be freed
and reused while the translation remains live.
The release also cannot simply be delayed to pfn_reader_destroy():
the last domain in the unwind loop is the only one that unpins, and
the unpin ends in update_unpinned(). When an earlier batch was fully
installed before the failure, pages->npinned still counts the pins
the reader holds, while pages->last_npinned still has its pre-fill
value, so update_unpinned() warns about npinned being above
last_npinned.
Release the pins inside the else branch of the last domain instead,
right before iopt_area_unfill_partial_domain(). No domain maps the
failing batch by then: the earlier domains were just unmapped by this
loop, the failing domain rolled back its partial mapping in
batch_to_domain(), and the later domains never installed the batch.
The release also precedes the last domain's unpin and its
update_unpinned(), so the pin accounting stays consistent. The
single-domain variant iopt_area_fill_domain() already unwinds in this
order.
Fixes: 8d160cd4d506 ("iommufd: Algorithms for PFN storage")
Cc: stable@vger.kernel.org
Signed-off-by: Qihang <q.h.hack.winter@gmail.com>
---
drivers/iommu/iommufd/pages.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c
index 2f6153108add..a99f4efbffd9 100644
--- a/drivers/iommu/iommufd/pages.c
+++ b/drivers/iommu/iommufd/pages.c
@@ -1962,7 +1962,6 @@
return 0;
out_unmap:
- pfn_reader_release_pins(&pfns);
xa_for_each(&area->iopt->domains, unmap_index, domain) {
unsigned long end_index;
@@ -1982,6 +1981,18 @@
area, domain, iopt_area_index(area),
end_index - 1);
} else {
+ /*
+ * The last domain is the only one that unpins, so the
+ * reader's pins must be released before the unpin and
+ * its update_unpinned(): npinned still counts them
+ * while last_npinned predates the fill. No domain maps
+ * the failing batch any more: the earlier domains
+ * were just unmapped by this loop, the failing domain
+ * rolled back its partial mapping in
+ * batch_to_domain(), and the later domains never
+ * installed it.
+ */
+ pfn_reader_release_pins(&pfns);
iopt_area_unfill_partial_domain(area, pages, domain,
end_index);
}
--
2.54.0 (Apple Git-157)
reply other threads:[~2026-11-18 16:40 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261118164026.54455-1-q.h.hack.winter@gmail.com \
--to=q.h.hack.winter@gmail.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=robin.murphy@arm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®