From: Maoyi Xie <maoyixie.tju@gmail.com>
To: joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, jpb@kernel.org
Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org,
virtualization@lists.linux.dev,
Maoyi Xie <maoyixie.tju@gmail.com>
Subject: [PATCH 1/2] iommu: Avoid using the list iterator past the loop in iommu_insert_resv_region()
Date: Thu, 4 Jun 2026 13:18:15 +0800 [thread overview]
Message-ID: <20260604051816.2976221-2-maoyixie.tju@gmail.com> (raw)
In-Reply-To: <20260604051816.2976221-1-maoyixie.tju@gmail.com>
iommu_insert_resv_region() walks @regions to find the insertion point.
When no element compares greater, the list_for_each_entry() iterator ends
up one past the last entry, so &iter->list aliases the list head through
container_of() offset cancellation and the following list_add_tail() still
targets the tail. The result is correct, but using the iterator after the
loop is undefined per the list_for_each_entry() contract.
The loop only needs a list_head to use as the insertion point, not the
entry itself, so iterate with list_for_each() and keep the typed
list_entry() dereference inside the loop body. No functional change.
Suggested-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Maoyi Xie <maoyixie.tju@gmail.com>
---
drivers/iommu/iommu.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 61c12ba78206..f9f53db9696f 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -862,6 +862,7 @@ static int iommu_insert_resv_region(struct iommu_resv_region *new,
struct list_head *regions)
{
struct iommu_resv_region *iter, *tmp, *nr, *top;
+ struct list_head *pos;
LIST_HEAD(stack);
nr = iommu_alloc_resv_region(new->start, new->length,
@@ -870,12 +871,14 @@ static int iommu_insert_resv_region(struct iommu_resv_region *new,
return -ENOMEM;
/* First add the new element based on start address sorting */
- list_for_each_entry(iter, regions, list) {
+ list_for_each(pos, regions) {
+ iter = list_entry(pos, struct iommu_resv_region, list);
+
if (nr->start < iter->start ||
(nr->start == iter->start && nr->type <= iter->type))
break;
}
- list_add_tail(&nr->list, &iter->list);
+ list_add_tail(&nr->list, pos);
/* Merge overlapping segments of type nr->type in @regions, if any */
list_for_each_entry_safe(iter, tmp, regions, list) {
--
2.34.1
next prev parent reply other threads:[~2026-06-04 5:18 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-18 18:49 iommu: iterator used after loop end in resv region insertion? Maoyi Xie
2026-06-02 10:28 ` Will Deacon
2026-06-02 10:44 ` Robin Murphy
2026-06-04 5:18 ` [PATCH 0/2] iommu: avoid using list iterators past the loop in resv region insertion Maoyi Xie
2026-06-04 5:18 ` Maoyi Xie [this message]
2026-06-04 5:18 ` [PATCH 2/2] iommu/virtio: Avoid using the list iterator past the loop in viommu_add_resv_mem() Maoyi Xie
2026-06-04 16:04 ` Jean-Philippe Brucker
2026-06-05 8:59 ` Maoyi Xie
2026-06-05 10:01 ` Jean-Philippe Brucker
2026-06-05 10:05 ` Robin Murphy
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=20260604051816.2976221-2-maoyixie.tju@gmail.com \
--to=maoyixie.tju@gmail.com \
--cc=iommu@lists.linux.dev \
--cc=joro@8bytes.org \
--cc=jpb@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=robin.murphy@arm.com \
--cc=virtualization@lists.linux.dev \
--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