Re: [PATCH 1/2] hung_task: Show the blocker task if the task is hung on mutex

From: Waiman Long
Date: Wed Feb 19 2025 - 21:15:57 EST



On 2/19/25 8:41 PM, Steven Rostedt wrote:
On Wed, 19 Feb 2025 20:36:13 -0500
Waiman Long <llong@xxxxxxxxxx> wrote:

this field, we don't need to take lock, though taking the wait_lock may
still be needed to examine other information inside the mutex.
Do we need to take it just for accessing owner, which is in an atomic?
Right. I forgot it is an atomic_long_t. In that case, no lock should be
needed.
Now if we have a two fields to read:

block_flags (for the type of lock) and blocked_on (for the lock)

We need a way to synchronize the two. What happens if we read the type, and
the task wakes up and and then blocks on a different type of lock?

Then the lock read from blocked_on could be a different type of lock than
what is expected.

That is different from reading the owner. In this case, we need to use smp_rmb()/wmb() to sequence the read and write operations unless it is guaranteed that they are in the same cacheline. One possible way is as follows:

Writer - setting them:

    WRITE_ONCE(lock)
    smp_wmb()
    WRITE_ONCE(type)

Clearing them:

    WRITE_ONCE(type, 0)
    smp_wmb()
    WRITE_ONCE(lock, NULL)

Reader:

    READ_ONCE(type)
again:
    smp_rmb()
    READ_ONCE(lock)
    smp_rmb()
    if (READ_ONCE(type) != type)
        goto again

Cheers,
Longman