From: Robin Murphy <robin.murphy@arm.com>
To: Maoyi Xie <maoyixie.tju@gmail.com>,
Jean-Philippe Brucker <jpb@kernel.org>
Cc: joro@8bytes.org, will@kernel.org, iommu@lists.linux.dev,
linux-kernel@vger.kernel.org, virtualization@lists.linux.dev
Subject: Re: [PATCH 2/2] iommu/virtio: Avoid using the list iterator past the loop in viommu_add_resv_mem()
Date: Fri, 5 Jun 2026 11:05:09 +0100 [thread overview]
Message-ID: <3852f5fd-3e25-4ea1-b818-80d56a081eba@arm.com> (raw)
In-Reply-To: <CAHPEe=HV+wpgg3YMQcOxN5M7c+ew6MQRgY0YgW-3KgB91gn_sw@mail.gmail.com>
On 2026-06-05 9:59 am, Maoyi Xie wrote:
> Hi Jean-Philippe,
>
> On Fri, Jun 5, 2026 at 12:04 AM Jean-Philippe Brucker <jpb@kernel.org> wrote:
>> Thank you for removing this hack, though I don't find a contract in the
>> list_for_each_entry() doc, and the fix still accesses a cursor outside the
>> loop. Since you mentioned C11 UB in another email, do you have more info
>> on the precise operation which is undefined in the kernel (container_of
>> into an invalid object or the &next->list addition)? Just so I can avoid
>> it in the future.
>
>> Anyway, thanks for the patch. If this is just general cleanup that's fine
>> too.
>
> Thanks for the review. You're right.
>
> There is no such contract in the docs. I overstated it. And no undefined
> pointer arithmetic happens here either.
>
> iommu_resv_region has list as its first member, so offsetof is 0. That
> makes container_of(&vdev->resv_regions, struct iommu_resv_region, list)
> just (char *)&vdev->resv_regions - 0, i.e. the head with a different
> type. &next->list is the head again. Nothing reads next as an
> iommu_resv_region, so no pointer leaves its object.
>
> The container_of would be undefined only if list was not the first
> member. Then the subtraction lands before the real object (C11 6.5.6).
> That is not the case here.
If that were a real issue, then I think list_entry_is_head() in the
list_for_each_entry() iteration itself would suffer from it too. It's
probably safe to say that while the C language in general leaves this
open, Linux relies on GCC using pointer representations where the
arithmetic will always work out, same as we depend on two's complement
representation of integers all over the place.
I believe the concern is more just that if the pattern of using a
list_entry iterator variable outside the scope of the loop isn't
discouraged, then it's all too easy for subsequent code to inadvertently
dereference fields *other* than the list_head without checking that it
is a valid entry, which definitely would then be the worst kind of UB.
Thanks,
Robin.
> So this is cleanup, not a UB fix. The typed cursor points at the head but
> reads like an entry. That becomes a real bug the day someone reorders the
> struct or touches another field. The new pos stays a struct list_head *,
> which is just the head, so it avoids all of that. I should have said that
> in the message.
>
> If you or Joerg prefer, I can resend the series with the wording fixed.
>
> Thanks,
> Maoyi
prev parent reply other threads:[~2026-06-05 10:05 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 ` [PATCH 1/2] iommu: Avoid using the list iterator past the loop in iommu_insert_resv_region() Maoyi Xie
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 [this message]
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=3852f5fd-3e25-4ea1-b818-80d56a081eba@arm.com \
--to=robin.murphy@arm.com \
--cc=iommu@lists.linux.dev \
--cc=joro@8bytes.org \
--cc=jpb@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=maoyixie.tju@gmail.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