Re: [PATCH v16 3/7] locking/mutex: Add p->blocked_on wrappers for correctness checks

From: John Stultz
Date: Wed Apr 16 2025 - 18:45:18 EST


On Mon, Apr 14, 2025 at 2:09 AM Juri Lelli <juri.lelli@xxxxxxxxxx> wrote:
> On 11/04/25 23:02, John Stultz wrote:
> > From: Valentin Schneider <valentin.schneider@xxxxxxx>
> >
> > This lets us assert mutex::wait_lock is held whenever we access
> > p->blocked_on, as well as warn us for unexpected state changes.
> >
>
> ...
>
> > +static inline struct mutex *__get_task_blocked_on(struct task_struct *p)
> > +{
> > + return READ_ONCE(p->blocked_on);
> > +}
> > +
>
> Shouldn't we check that wait_lock is held also when reading blocked_on?

Yeah, later in the series when I switch to the task->blocked lock we do:
https://github.com/johnstultz-work/linux-dev/blob/proxy-exec-v16-6.15-rc1/include/linux/sched.h#L2267

I think the difficulty here is just we don't have the mutex pointer in
this context to assert lock->wait_lock is held.

I guess we could assume blocked_on is the right mutex and assert that
we hold that one if it is non-null. I'll take a shot at that.

> And, if that's the case, why do we use READ_ONCE?

So, I was thinking this was because we pulled blocked_on in some
contexts without holding the lock and we wanted to avoid the compiler
rearranging things, but it's not used that way now, so I'll drop this.

Thanks for pointing this out!
-john