Commit Graph

11 Commits

Author SHA1 Message Date
Joel Savitz baca3f37f7 locking/rtmutex: Fix task->pi_waiters integrity
JIRA: https://issues.redhat.com/browse/RHEL-5226

commit f7853c34241807bb97673a5e97719123be39a09e
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Fri Jul 7 16:19:09 2023 +0200

    locking/rtmutex: Fix task->pi_waiters integrity

    Henry reported that rt_mutex_adjust_prio_check() has an ordering
    problem and puts the lie to the comment in [7]. Sharing the sort key
    between lock->waiters and owner->pi_waiters *does* create problems,
    since unlike what the comment claims, holding [L] is insufficient.

    Notably, consider:

            A
          /   \
         M1   M2
         |     |
         B     C

    That is, task A owns both M1 and M2, B and C block on them. In this
    case a concurrent chain walk (B & C) will modify their resp. sort keys
    in [7] while holding M1->wait_lock and M2->wait_lock. So holding [L]
    is meaningless, they're different Ls.

    This then gives rise to a race condition between [7] and [11], where
    the requeue of pi_waiters will observe an inconsistent tree order.

            B                               C

      (holds M1->wait_lock,         (holds M2->wait_lock,
       holds B->pi_lock)             holds A->pi_lock)

      [7]
      waiter_update_prio();
      ...
      [8]
      raw_spin_unlock(B->pi_lock);
      ...
      [10]
      raw_spin_lock(A->pi_lock);

                                    [11]
                                    rt_mutex_enqueue_pi();
                                    // observes inconsistent A->pi_waiters
                                    // tree order

    Fixing this means either extending the range of the owner lock from
    [10-13] to [6-13], with the immediate problem that this means [6-8]
    hold both blocked and owner locks, or duplicating the sort key.

    Since the locking in chain walk is horrible enough without having to
    consider pi_lock nesting rules, duplicate the sort key instead.

    By giving each tree their own sort key, the above race becomes
    harmless, if C sees B at the old location, then B will correct things
    (if they need correcting) when it walks up the chain and reaches A.

    Fixes: fb00aca474 ("rtmutex: Turn the plist into an rb-tree")
    Reported-by: Henry Wu <triangletrap12@gmail.com>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Acked-by: Thomas Gleixner <tglx@linutronix.de>
    Tested-by: Henry Wu <triangletrap12@gmail.com>
    Link: https://lkml.kernel.org/r/20230707161052.GF2883469%40hirez.programming.kicks-ass.net"

Signed-off-by: Joel Savitz <jsavitz@redhat.com>
2024-01-15 10:10:43 -05:00
Waiman Long 6dd784cab0 locking/ww_mutex: Add rt_mutex based lock type and accessors
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit 2408f7a3782a6bfa69a573f5408b3a9666db78ca
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Sun, 15 Aug 2021 23:28:56 +0200

    locking/ww_mutex: Add rt_mutex based lock type and accessors

    Provide the defines for RT mutex based ww_mutexes and fix up the debug logic
    so it's either enabled by DEBUG_MUTEXES or DEBUG_RT_MUTEXES on RT kernels.

    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Ingo Molnar <mingo@kernel.org>
    Link: https://lore.kernel.org/r/20210815211304.908012566@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:12 -04:00
Waiman Long 0d8566cb72 locking/ww_mutex: Add RT priority to W/W order
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit 8850d773703f8114d7c8a2421fd20bde8a558f96
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Sun, 15 Aug 2021 23:28:55 +0200

    locking/ww_mutex: Add RT priority to W/W order

    RT mutex based ww_mutexes cannot order based on timestamps. They have to
    order based on priority. Add the necessary decision logic.

    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Ingo Molnar <mingo@kernel.org>
    Link: https://lore.kernel.org/r/20210815211304.847536630@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:11 -04:00
Waiman Long d1d9e03226 locking/ww_mutex: Implement rt_mutex accessors
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit dc4564f5dc2d4b11f3f3c8d3ac94012b1c7347d6
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Sun, 15 Aug 2021 23:28:53 +0200

    locking/ww_mutex: Implement rt_mutex accessors

    Provide the type defines and the helper inlines for rtmutex based ww_mutexes.

    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Ingo Molnar <mingo@kernel.org>
    Link: https://lore.kernel.org/r/20210815211304.790760545@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:10 -04:00
Waiman Long b6ad3ec119 locking/ww_mutex: Abstract out internal lock accesses
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit 653a5b0bd9b405db999d5f4bfe08d34691e2c55a
Author: Thomas Gleixner <tglx@linutronix.de>
Date:   Sun, 15 Aug 2021 23:28:52 +0200

    locking/ww_mutex: Abstract out internal lock accesses

    Accessing the internal wait_lock of mutex and rtmutex is slightly
    different. Provide helper functions for that.

    Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Ingo Molnar <mingo@kernel.org>
    Link: https://lore.kernel.org/r/20210815211304.734635961@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:10 -04:00
Waiman Long 6fb0a323ae locking/ww_mutex: Abstract out mutex types
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit bdb189148ded4ffa826a1387074c795fda43b3ba
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Sun, 15 Aug 2021 23:28:50 +0200

    locking/ww_mutex: Abstract out mutex types

    Some ww_mutex helper functions use pointers for the underlying mutex and
    mutex_waiter. The upcoming rtmutex based implementation needs to share
    these functions. Add and use defines for the types and replace the direct
    types in the affected functions.

    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Ingo Molnar <mingo@kernel.org>
    Link: https://lore.kernel.org/r/20210815211304.678720245@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:09 -04:00
Waiman Long e78f2d2e5e locking/ww_mutex: Abstract out mutex accessors
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit 9934ccc75cec2bafac552c2130835630530c4f7e
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Sun, 15 Aug 2021 23:28:49 +0200

    locking/ww_mutex: Abstract out mutex accessors

    Move the mutex related access from various ww_mutex functions into helper
    functions so they can be substituted for rtmutex based ww_mutex later.

    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Ingo Molnar <mingo@kernel.org>
    Link: https://lore.kernel.org/r/20210815211304.622477030@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:08 -04:00
Waiman Long 824fd5ac38 locking/ww_mutex: Abstract out waiter enqueueing
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit 843dac28f90ef80535b0aee0b78446f1770c8611
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Sun, 15 Aug 2021 23:28:47 +0200

    locking/ww_mutex: Abstract out waiter enqueueing

    The upcoming rtmutex based ww_mutex needs a different handling for
    enqueueing a waiter. Split it out into a helper function.

    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Ingo Molnar <mingo@kernel.org>
    Link: https://lore.kernel.org/r/20210815211304.566318143@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:08 -04:00
Waiman Long 9d8aa8d79a locking/ww_mutex: Abstract out the waiter iteration
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit 23d599eb2377404100d0d1508e12b0a2c40b49b1
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Sun, 15 Aug 2021 23:28:45 +0200

    locking/ww_mutex: Abstract out the waiter iteration

    Split out the waiter iteration functions so they can be substituted for a
    rtmutex based ww_mutex later.

    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Ingo Molnar <mingo@kernel.org>
    Link: https://lore.kernel.org/r/20210815211304.509186185@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:07 -04:00
Waiman Long e36fdce5bf locking/ww_mutex: Remove the __sched annotation from ww_mutex APIs
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit 5297ccb2c50916c59294a63fae79fe01a7fbb79a
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Sun, 15 Aug 2021 23:28:44 +0200

    locking/ww_mutex: Remove the __sched annotation from ww_mutex APIs

    None of these functions will be on the stack when blocking in
    schedule(), hence __sched is not needed.

    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Ingo Molnar <mingo@kernel.org>
    Link: https://lore.kernel.org/r/20210815211304.453235952@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:06 -04:00
Waiman Long 8120fa3bc7 locking/ww_mutex: Split out the W/W implementation logic into kernel/locking/ww_mutex.h
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit 2674bd181f3338dc2c58a59caa766dc9d5779784
Author: Peter Zijlstra (Intel) <peterz@infradead.org>
Date:   Tue, 17 Aug 2021 16:31:54 +0200

    locking/ww_mutex: Split out the W/W implementation logic into kernel/locking/ww_mutex.h

    Split the W/W mutex helper functions out into a separate header file, so
    they can be shared with a rtmutex based variant later.

    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Ingo Molnar <mingo@kernel.org>
    Link: https://lore.kernel.org/r/20210815211304.396893399@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:05 -04:00