Re: [PATCH 2/2] wifi: mac80211_hwsim: serialize radio removal with frame commands

From: enderaoelyther

Date: Thu Nov 12 2026 - 23:39:44 EST


> hwsim_tx_info_frame_received_nl() just doesn't do anything that sleeps,
> so it could just use RCU protection directly?

Agreed, and the same applies to the inner transmitter lookup (txdata) in
hwsim_cloned_frame_received_nl(). Extending rcu_read_lock() across the
full use works there, since teardown unpublishes the radio before
ieee80211_unregister_hw() and wiphy_unregister() unconditionally runs
synchronize_rcu() before ieee80211_free_hw().

> hwsim_cloned_frame_received_nl() uses it twice - the inner use is not an
> issue as above, and the outer one is just acquiring the mutex for
> synchronization with ->started, but given that mac80211 will sync RCU in
> stop that's perhaps just not needed at all? Or maybe hwsim just needs to
> synchronize_net() on stop.

The existing mac80211 grace period is too early to replace the mutex
during an ordinary stop. It runs in ieee80211_do_stop() before
drv_stop() invokes mac80211_hwsim_stop(). Since the hwsim radio remains
published, a new reader can start after that grace period, observe
->started as true, and then be delayed until after the driver has
stopped. That is the window the current data->mutex closes.

Setting ->started to false and then calling synchronize_net() in
mac80211_hwsim_stop() could replace that mutex. That would be a
different synchronization protocol, with the grace-period cost moved to
every radio stop instead of keeping the existing per-radio exclusion.

> hwsim_pmsr_report_nl() uses the mutex for pmsr_request access/swap and
> GFP_KERNEL, but both of those could change? might need some more work
> for abort though, not sure.

The abort ordering is the difficult part. cfg80211 moves the request out
of its list, calls the driver's abort callback, and frees the request as
soon as that callback returns. The existing data->mutex therefore does
more than protect the pointer swap: it prevents abort from returning
while the report path is still using the request.

Changing the allocation flags or atomically clearing pmsr_request would
not provide that exclusion. Abort could observe the cleared pointer,
return, and let cfg80211 free the request while the report path was
still using its local copy. Avoiding the mutex would require a new
request-ownership protocol that abort also waits for.

So I agree that direct RCU can cover the two non-sleeping lookup uses,
and it could cover the outer cloned-frame use with the stop change
above. It does not cover the PMSR path without redesigning the request
lifetime.

A hybrid design is possible in principle: use RCU for the frame and
TX-status paths and retain a per-radio pin for PMSR. The reference
version instead keeps one lookup-lifetime rule and leaves both the
existing stop synchronization and PMSR ownership protocol unchanged.
Its trade-off is the short hwsim_radio_lock critical section and the
reference operations on every lookup.

I still think the single reference-based rule is the smaller and less
fragile change overall, but I agree the RCU approach avoids that
lookup-side cost on the packet paths.

Thanks,
Zhao

On Wed, 11 Nov 2026 21:34:12 +0100, Johannes Berg
<johannes@xxxxxxxxxxxxxxxx> wrote:
> On Wed, 2026-11-11 at 14:45 -0500, enderaoelyther@xxxxxxxxx wrote:
> >
> >
> > The underlying problem is per-radio object lifetime, not command
> > serialization. get_hwsim_data_ref_from_addr() returns a radio pointer
> > after rhashtable_lookup_fast() has already ended its RCU read-side
> > protection. A concurrent deletion can then unpublish and free that radio
> > while a frame, TX-status, or PMSR command is still using the pointer.
> >
> > I reworked the fix around a per-radio reference count.
>
> I'm not convinced that's necessary.
>
> There are three users of get_hwsim_data_ref_from_addr:
>
> hwsim_tx_info_frame_received_nl() just doesn't do anything that sleeps,
> so it could just use RCU protection directly?
>
> hwsim_cloned_frame_received_nl() uses it twice - the inner use is not an
> issue as above, and the outer one is just acquiring the mutex for
> synchronization with ->started, but given that mac80211 will sync RCU in
> stop that's perhaps just not needed at all? Or maybe hwsim just needs to
> synchronize_net() on stop.
>
> hwsim_pmsr_report_nl() uses the mutex for pmsr_request access/swap and
> GFP_KERNEL, but both of those could change? might need some more work
> for abort though, not sure.
>
> Dunno if that's better overall? Maybe, maybe not.
>
> johannes