Re: [PATCH v11 11/16] iommu/arm-smmu-v3: Add CMDQ_PROD_STOP_FLAG to gate CMDQ submissions

From: Pranjal Shrivastava

Date: Wed Nov 25 2026 - 08:42:24 EST


On Wed, Nov 25, 2026 at 09:37:02AM +0000, John Garry wrote:
> >
> > > And it would be nice to not add extra logic in this loop. It is very timing
> > > sensitive, and adding extra checks can increase the chance that the cmpxchg
> > > fails (bad for performance).
> > >
> > That's a fair concern. A couple of things here:
> >
> > - The check is on our local llq copy, which we've already loaded, so
> > it's a bit test + a (not-taken) branch, with no extra memory access
> > before the cmpxchg.
> >
> > - Per Will's suggestion, I'm making arm_smmu_cmdq_poll_until_not_full()
> > bail out on the STOP flag, which lets me drop the check at the top of
> > the loop and the extra READ_ONCE(), leaving a single unlikely() check
> > right before the cmpxchg.
> >
> > It does need to stay in the loop though, since the cmpxchg is the only
> > point where checking the flag and reserving the slots is atomic.. a
> > check any earlier (e.g. in arm_smmu_get_cmdq()) could race with the
> > suspend path setting it.
>
> I just find it odd that we try to elide commands here (at the point of
> command submission). I would assume that any commands would not get this far
> - or even be submitted to the queue - if at the point of entering sleep.
>

They never actually reach the queue: the check is right before we
reserve any slots, so nothing gets submitted.

As for why the check sits here and not further up, we did try that in
the earlier versions and have converged on the current design:

- v1/v2 [1] elided at the call sites (TLBI/CFGI) using
pm_runtime_get_if_active(). That raced with the RPM state bouncing,
i.e. a caller could see the SMMU active and the suspend could kick in
before it actually submitted (as Daniel pointed out).
- v3 [2] tried to fix that with a new pm_runtime_get_if_not_suspended()
API, and v4/v5 [3] replaced it with a driver-side biased counter
(nr_cmdq_users). That worked, but it meant an extra atomic on a shared
cache line in every command submission, which isn't great on high
core count.
- v6 [4] replaced the counter with the STOP flag in prod, and v7-v9
[5] dropped the call-site checks entirely, leaving the CMDQ as the
single place that decides.

The callers themselves legitimately run while the SMMU is suspended,
e.g. a client unmapping (or a deferred unmap) while it's off, and we
don't want to wake the SMMU up just to invalidate TLBs that'll be fully
invalidated on resume anyway. So these commands do get this far, and the
only point where "is the SMMU on?" can be answered atomically with "I'm
committing commands" is the slot reservation itself.. any earlier check
is racy against the suspend path.

[1] https://lore.kernel.org/all/20250418233409.3926715-1-praan@xxxxxxxxxx/
[2] https://lore.kernel.org/all/20250616203149.2649118-1-praan@xxxxxxxxxx/
[3] https://lore.kernel.org/all/20251117191433.3360130-1-praan@xxxxxxxxxx/
[4] https://lore.kernel.org/all/20260414194702.1229094-1-praan@xxxxxxxxxx/
[5] https://lore.kernel.org/all/20260527221407.1756491-1-praan@xxxxxxxxxx/

Thanks,
Praan