Commit Graph

109 Commits

Author SHA1 Message Date
Waiman Long 9ae7ca4ed6 locking/mutex: Document that mutex_unlock() is non-atomic
JIRA: https://issues.redhat.com/browse/RHEL-35759

commit a51749ab34d9e5dec548fe38ede7e01e8bb26454
Author: Jann Horn <jannh@google.com>
Date:   Thu, 30 Nov 2023 21:48:17 +0100

    locking/mutex: Document that mutex_unlock() is non-atomic

    I have seen several cases of attempts to use mutex_unlock() to release an
    object such that the object can then be freed by another task.

    This is not safe because mutex_unlock(), in the
    MUTEX_FLAG_WAITERS && !MUTEX_FLAG_HANDOFF case, accesses the mutex
    structure after having marked it as unlocked; so mutex_unlock() requires
    its caller to ensure that the mutex stays alive until mutex_unlock()
    returns.

    If MUTEX_FLAG_WAITERS is set and there are real waiters, those waiters
    have to keep the mutex alive, but we could have a spurious
    MUTEX_FLAG_WAITERS left if an interruptible/killable waiter bailed
    between the points where __mutex_unlock_slowpath() did the cmpxchg
    reading the flags and where it acquired the wait_lock.

    ( With spinlocks, that kind of code pattern is allowed and, from what I
      remember, used in several places in the kernel. )

    Document this, such a semantic difference between mutexes and spinlocks
    is fairly unintuitive.

    [ mingo: Made the changelog a bit more assertive, refined the comments. ]

    Signed-off-by: Jann Horn <jannh@google.com>
    Signed-off-by: Ingo Molnar <mingo@kernel.org>
    Link: https://lore.kernel.org/r/20231130204817.2031407-1-jannh@google.com

Signed-off-by: Waiman Long <longman@redhat.com>
2024-05-22 19:52:16 -04:00
Waiman Long b347b2fbc1 locking: export contention tracepoints for bcachefs six locks
JIRA: https://issues.redhat.com/browse/RHEL-35759

commit 957e48087dfa38c976407f82e7525277d17a27ae
Author: Brian Foster <bfoster@redhat.com>
Date:   Mon, 14 Aug 2023 09:04:50 -0400

    locking: export contention tracepoints for bcachefs six locks

    The bcachefs implementation of six locks is intended to land in
    generic locking code in the long term, but has been pulled into the
    bcachefs subsystem for internal use for the time being. This code
    lift breaks the bcachefs module build as six locks depend a couple
    of the generic locking tracepoints. Export these tracepoint symbols
    for bcachefs.

    Signed-off-by: Brian Foster <bfoster@redhat.com>
    Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>

Signed-off-by: Waiman Long <longman@redhat.com>
2024-05-22 19:52:14 -04:00
Joel Savitz 1cc444bf94 locking/mutex: Make contention tracepoints more consistent wrt adaptive spinning
commit dc1f7893a70fe403983bd8492f177bf993940e2c
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Wed Mar 30 13:06:54 2022 +0200

    locking/mutex: Make contention tracepoints more consistent wrt adaptive spinning

    Have the trace_contention_*() tracepoints consistently include
    adaptive spinning. In order to differentiate between the spinning and
    non-spinning states add LCB_F_MUTEX and combine with LCB_F_SPIN.

    The consequence is that a mutex contention can now triggler multiple
    _begin() tracepoints before triggering an _end().

    Additionally, this fixes one path where mutex would trigger _end()
    without ever seeing a _begin().

    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2176147
Signed-off-by: Joel Savitz <jsavitz@redhat.com>
2023-03-07 15:26:28 -05:00
Joel Savitz 2d216f7bd8 locking: Apply contention tracepoints in the slow path
conflict in kernel/locking/rtmutex.c
	detail: c9s commit c3a495f437 ("rtmutex: Add acquire semantics for rtmutex lock acquisition slow path"), backport of upstream commit 1c0908d8e441, adds a second parameter to fixup_rt_mutex_waiters(), which is not present in upstream commit ee042be16cb4.
	action: keep new call to fixup_rt_mutex_waiters()

commit ee042be16cb455116d0fe99b77c6bc8baf87c8c6
Author: Namhyung Kim <namhyung@kernel.org>
Date:   Tue Mar 22 11:57:09 2022 -0700

    locking: Apply contention tracepoints in the slow path

    Adding the lock contention tracepoints in various lock function slow
    paths.  Note that each arch can define spinlock differently, I only
    added it only to the generic qspinlock for now.

    Signed-off-by: Namhyung Kim <namhyung@kernel.org>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Tested-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
    Link: https://lkml.kernel.org/r/20220322185709.141236-3-namhyung@kernel.org

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2176147
Signed-off-by: Joel Savitz <jsavitz@redhat.com>
2023-03-07 15:26:28 -05:00
Waiman Long b54489e6b0 locking: Add lock contention tracepoints
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2141431

commit 16edd9b511a13e7760ed4b92ba4e39bacda5c86f
Author: Namhyung Kim <namhyung@kernel.org>
Date:   Tue, 22 Mar 2022 11:57:08 -0700

    locking: Add lock contention tracepoints

    This adds two new lock contention tracepoints like below:

     * lock:contention_begin
     * lock:contention_end

    The lock:contention_begin takes a flags argument to classify locks.  I
    found it useful to identify what kind of locks it's tracing like if
    it's spinning or sleeping, reader-writer lock, real-time, and per-cpu.

    Move tracepoint definitions into mutex.c so that we can use them
    without lockdep.

    Signed-off-by: Namhyung Kim <namhyung@kernel.org>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Tested-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
    Link: https://lkml.kernel.org/r/20220322185709.141236-2-namhyung@kernel.org

Signed-off-by: Waiman Long <longman@redhat.com>
2022-11-10 11:38:06 -05:00
Waiman Long cf476291f3 locking: Make owner_on_cpu() into <linux/sched.h>
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2076713

commit c0bed69daf4b67809b58cc7cd81a8fa4f45bc161
Author: Kefeng Wang <wangkefeng.wang@huawei.com>
Date:   Fri, 3 Dec 2021 15:59:34 +0800

    locking: Make owner_on_cpu() into <linux/sched.h>

    Move the owner_on_cpu() from kernel/locking/rwsem.c into
    include/linux/sched.h with under CONFIG_SMP, then use it
    in the mutex/rwsem/rtmutex to simplify the code.

    Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Link: https://lore.kernel.org/r/20211203075935.136808-2-wangkefeng.wang@huawei.com

Signed-off-by: Waiman Long <longman@redhat.com>
2022-05-12 08:34:03 -04:00
Waiman Long cc1b561b58 locking: Remove rcu_read_{,un}lock() for preempt_{dis,en}able()
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2076713

commit 6c2787f2a20ceb49c98bd06f7dad1589eed1c951
Author: Yanfei Xu <yanfei.xu@windriver.com>
Date:   Wed, 13 Oct 2021 21:41:52 +0800

    locking: Remove rcu_read_{,un}lock() for preempt_{dis,en}able()

    preempt_disable/enable() is equal to RCU read-side crital section, and
    the spinning codes in mutex and rwsem could ensure that the preemption
    is disabled. So let's remove the unnecessary rcu_read_lock/unlock for
    saving some cycles in hot codes.

    Signed-off-by: Yanfei Xu <yanfei.xu@windriver.com>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Acked-by: Waiman Long <longman@redhat.com>
    Link: https://lore.kernel.org/r/20211013134154.1085649-2-yanfei.xu@windriver.com

Signed-off-by: Waiman Long <longman@redhat.com>
2022-05-12 08:32:19 -04:00
Karol Herbst 2394c9bf48 kernel/locking: Add context to ww_mutex_trylock()
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2072020
Upstream Status: v5.16-rc1

commit 12235da8c80a1f9909008e4ca6036d5772b81192
Author:     Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
AuthorDate: Thu Sep  9 11:32:18 2021 +0200
Commit:     Peter Zijlstra <peterz@infradead.org>
CommitDate: Fri Sep 17 15:08:41 2021 +0200

    i915 will soon gain an eviction path that trylock a whole lot of locks
    for eviction, getting dmesg failures like below:

      BUG: MAX_LOCK_DEPTH too low!
      turning off the locking correctness validator.
      depth: 48  max: 48!
      48 locks held by i915_selftest/5776:
       #0: ffff888101a79240 (&dev->mutex){....}-{3:3}, at: __driver_attach+0x88/0x160
       #1: ffffc900009778c0 (reservation_ww_class_acquire){+.+.}-{0:0}, at: i915_vma_pin.constprop.63+0x39/0x1b0 [i915]
       #2: ffff88800cf74de8 (reservation_ww_class_mutex){+.+.}-{3:3}, at: i915_vma_pin.constprop.63+0x5f/0x1b0 [i915]
       #3: ffff88810c7f9e38 (&vm->mutex/1){+.+.}-{3:3}, at: i915_vma_pin_ww+0x1c4/0x9d0 [i915]
       #4: ffff88810bad5768 (reservation_ww_class_mutex){+.+.}-{3:3}, at: i915_gem_evict_something+0x110/0x860 [i915]
       #5: ffff88810bad60e8 (reservation_ww_class_mutex){+.+.}-{3:3}, at: i915_gem_evict_something+0x110/0x860 [i915]
      ...
       #46: ffff88811964d768 (reservation_ww_class_mutex){+.+.}-{3:3}, at: i915_gem_evict_something+0x110/0x860 [i915]
       #47: ffff88811964e0e8 (reservation_ww_class_mutex){+.+.}-{3:3}, at: i915_gem_evict_something+0x110/0x860 [i915]
      INFO: lockdep is turned off.

    Fixing eviction to nest into ww_class_acquire is a high priority, but
    it requires a rework of the entire driver, which can only be done one
    step at a time.

    As an intermediate solution, add an acquire context to
    ww_mutex_trylock, which allows us to do proper nesting annotations on
    the trylocks, making the above lockdep splat disappear.

    This is also useful in regulator_lock_nested, which may avoid dropping
    regulator_nesting_mutex in the uncontended path, so use it there.

    TTM may be another user for this, where we could lock a buffer in a
    fastpath with list locks held, without dropping all locks we hold.

    [peterz: rework actual ww_mutex_trylock() implementations]
    Signed-off-by: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Link: https://lkml.kernel.org/r/YUBGPdDDjKlxAuXJ@hirez.programming.kicks-ass.net

Signed-off-by: Karol Herbst <kherbst@redhat.com>
2022-04-19 15:52:59 +02:00
Waiman Long c433197478 locking/ww_mutex: Initialize waiter.ww_ctx properly
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit b857174e68e26f9c4f0796971e11eb63ad5a3eb6
Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Date:   Thu, 19 Aug 2021 21:30:30 +0200

    locking/ww_mutex: Initialize waiter.ww_ctx properly

    The consolidation of the debug code for mutex waiter intialization sets
    waiter::ww_ctx to a poison value unconditionally. For regular mutexes this
    is intended to catch the case where waiter_ww_ctx is dereferenced
    accidentally.

    For ww_mutex the poison value has to be overwritten either with a context
    pointer or NULL for ww_mutexes without context.

    The rework broke this as it made the store conditional on the context
    pointer instead of the argument which signals whether ww_mutex code should
    be compiled in or optiized out. As a result waiter::ww_ctx ends up with the
    poison pointer for contextless ww_mutexes which causes a later dereference of
    the poison pointer because it is != NULL.

    Use the build argument instead so for ww_mutex the poison value is always
    overwritten.

    Fixes: c0afb0ffc06e6 ("locking/ww_mutex: Gather mutex_waiter initialization")
    Reported-by: Guenter Roeck <linux@roeck-us.net>
    Suggested-by: Peter Zijlstra <peterz@infradead.org>
    Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
    Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
    Link: https://lore.kernel.org/r/20210819193030.zpwrpvvrmy7xxxiy@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 19:45:08 -04:00
Waiman Long e3bd2040f4 locking/rtmutex: Add mutex variant for RT
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit bb630f9f7a7d43869e4e7f5e4c002207396aea59
Author: Thomas Gleixner <tglx@linutronix.de>
Date:   Sun, 15 Aug 2021 23:29:01 +0200

    locking/rtmutex: Add mutex variant for RT

    Add the necessary defines, helpers and API functions for replacing struct mutex on
    a PREEMPT_RT enabled kernel with an rtmutex based variant.

    No functional change when CONFIG_PREEMPT_RT=n

    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/20210815211305.081517417@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:14 -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
Waiman Long 1c17ad896d locking/ww_mutex: Split up ww_mutex_unlock()
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit aaa77de10b7c86fa779b2108802fa9e785fbe2e9
Author: Peter Zijlstra (Intel) <peterz@infradead.org>
Date:   Tue, 17 Aug 2021 16:19:04 +0200

    locking/ww_mutex: Split up ww_mutex_unlock()

    Split the ww related part out into a helper function so it can be reused
    for a rtmutex based ww_mutex implementation.

    [ mingo: Fixed bisection failure. ]

    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.340166556@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:05 -04:00
Waiman Long 58ff7cb466 locking/ww_mutex: Gather mutex_waiter initialization
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

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

    locking/ww_mutex: Gather mutex_waiter initialization

    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.281927514@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:04 -04:00
Waiman Long a3d7109c09 locking/ww_mutex: Simplify lockdep annotations
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

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

    locking/ww_mutex: Simplify lockdep annotations

    No functional change.

    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.222921634@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:03 -04:00
Waiman Long 058c7d27c7 locking/mutex: Make mutex::wait_lock raw
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

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

    locking/mutex: Make mutex::wait_lock raw

    The wait_lock of mutex is really a low level lock. Convert it to a
    raw_spinlock like the wait_lock of rtmutex.

    [ mingo: backmerged the test_lockup.c build fix by bigeasy. ]

    Co-developed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
    Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
    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.166863404@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:03 -04:00
Waiman Long 96b72be9c9 locking/mutex: Consolidate core headers, remove kernel/locking/mutex-debug.h
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit a321fb9038b335f3c447d1810b97d5f7eec152ac
Author: Thomas Gleixner <tglx@linutronix.de>
Date:   Tue, 17 Aug 2021 16:17:38 +0200

    locking/mutex: Consolidate core headers, remove kernel/locking/mutex-debug.h

    Having two header files which contain just the non-debug and debug variants
    is mostly waste of disc space and has no real value. Stick the debug
    variants into the common mutex.h file as counterpart to the stubs for the
    non-debug case.

    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/20210815211303.995350521@linutronix.de

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:19:00 -04:00
Waiman Long 3b7a71adcf locking/mutex: Add MUTEX_WARN_ON
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit e6b4457b05f36bb9e371f29ab1dd2d97272a1540
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Wed, 30 Jun 2021 17:35:20 +0200

    locking/mutex: Add MUTEX_WARN_ON

    Cleanup some #ifdef'fery.

    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Reviewed-by: Waiman Long <longman@redhat.com>
    Reviewed-by: Yanfei Xu <yanfei.xu@windriver.com>
    Link: https://lore.kernel.org/r/20210630154115.020298650@infradead.org

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:18:34 -04:00
Waiman Long 1bffed4612 locking/mutex: Introduce __mutex_trylock_or_handoff()
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit ad90880dc9625682a58897cba2ecff657a2aa60b
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Wed, 30 Jun 2021 17:35:19 +0200

    locking/mutex: Introduce __mutex_trylock_or_handoff()

    Yanfei reported that it is possible to loose HANDOFF when we race with
    mutex_unlock() and end up setting HANDOFF on an unlocked mutex. At
    that point anybody can steal it, losing HANDOFF in the process.

    If this happens often enough, we can in fact starve the top waiter.

    Solve this by folding the 'set HANDOFF' operation into the trylock
    operation, such that either we acquire the lock, or it gets HANDOFF
    set. This avoids having HANDOFF set on an unlocked mutex.

    Reported-by: Yanfei Xu <yanfei.xu@windriver.com>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Reviewed-by: Waiman Long <longman@redhat.com>
    Reviewed-by: Yanfei Xu <yanfei.xu@windriver.com>
    Link: https://lore.kernel.org/r/20210630154114.958507900@infradead.org

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:18:34 -04:00
Waiman Long a740cb4841 locking/mutex: Fix HANDOFF condition
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit 048661a1f963e9517630f080687d48af79ed784c
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Wed, 30 Jun 2021 17:35:18 +0200

    locking/mutex: Fix HANDOFF condition

    Yanfei reported that setting HANDOFF should not depend on recomputing
    @first, only on @first state. Which would then give:

      if (ww_ctx || !first)
        first = __mutex_waiter_is_first(lock, &waiter);
      if (first)
        __mutex_set_flag(lock, MUTEX_FLAG_HANDOFF);

    But because 'ww_ctx || !first' is basically 'always' and the test for
    first is relatively cheap, omit that first branch entirely.

    Reported-by: Yanfei Xu <yanfei.xu@windriver.com>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Reviewed-by: Waiman Long <longman@redhat.com>
    Reviewed-by: Yanfei Xu <yanfei.xu@windriver.com>
    Link: https://lore.kernel.org/r/20210630154114.896786297@infradead.org

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:18:33 -04:00
Waiman Long a4b898bf42 locking/mutex: Use try_cmpxchg()
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2007032

commit ab4e4d9f79b2c95ef268985d2a9625a03a73c49a
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Wed, 30 Jun 2021 17:35:17 +0200

    locking/mutex: Use try_cmpxchg()

    For simpler and better code.

    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Reviewed-by: Waiman Long <longman@redhat.com>
    Reviewed-by: Yanfei Xu <yanfei.xu@windriver.com>
    Link: https://lore.kernel.org/r/20210630154114.834438545@infradead.org

Signed-off-by: Waiman Long <longman@redhat.com>
2021-09-27 16:18:32 -04:00
Peter Zijlstra 2f064a59a1 sched: Change task_struct::state
Change the type and name of task_struct::state. Drop the volatile and
shrink it to an 'unsigned int'. Rename it in order to find all uses
such that we can use READ_ONCE/WRITE_ONCE as appropriate.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Daniel Bristot de Oliveira <bristot@redhat.com>
Acked-by: Will Deacon <will@kernel.org>
Acked-by: Daniel Thompson <daniel.thompson@linaro.org>
Link: https://lore.kernel.org/r/20210611082838.550736351@infradead.org
2021-06-18 11:43:09 +02:00
Zqiang 3a010c4932 locking/mutex: clear MUTEX_FLAGS if wait_list is empty due to signal
When a interruptible mutex locker is interrupted by a signal
without acquiring this lock and removed from the wait queue.
if the mutex isn't contended enough to have a waiter
put into the wait queue again, the setting of the WAITER
bit will force mutex locker to go into the slowpath to
acquire the lock every time, so if the wait queue is empty,
the WAITER bit need to be clear.

Fixes: 040a0a3710 ("mutex: Add support for wound/wait style locks")
Suggested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Zqiang <qiang.zhang@windriver.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20210517034005.30828-1-qiang.zhang@windriver.com
2021-05-18 12:53:51 +02:00
Ingo Molnar e2db7592be locking: Fix typos in comments
Fix ~16 single-word typos in locking code comments.

Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Paul E. McKenney <paulmck@kernel.org>
Cc: Will Deacon <will.deacon@arm.com>
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2021-03-22 02:45:52 +01:00
Waiman Long 5de2055d31 locking/ww_mutex: Simplify use_ww_ctx & ww_ctx handling
The use_ww_ctx flag is passed to mutex_optimistic_spin(), but the
function doesn't use it. The frequent use of the (use_ww_ctx && ww_ctx)
combination is repetitive.

In fact, ww_ctx should not be used at all if !use_ww_ctx.  Simplify
ww_mutex code by dropping use_ww_ctx from mutex_optimistic_spin() an
clear ww_ctx if !use_ww_ctx. In this way, we can replace (use_ww_ctx &&
ww_ctx) by just (ww_ctx).

Signed-off-by: Waiman Long <longman@redhat.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Davidlohr Bueso <dbueso@suse.de>
Link: https://lore.kernel.org/r/20210316153119.13802-2-longman@redhat.com
2021-03-17 09:56:44 +01:00
Sebastian Andrzej Siewior 0f319d49a4 locking/mutex: Kill mutex_trylock_recursive()
There are not users of mutex_trylock_recursive() in tree as of
v5.11-rc7.

Remove it.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20210210085248.219210-2-bigeasy@linutronix.de
2021-02-10 14:44:40 +01:00
Davidlohr Bueso c571b72e2b Revert "locking/mutex: Complain upon mutex API misuse in IRQ contexts"
This ended up causing some noise in places such as rxrpc running in softirq.

The warning is misleading in this case as the mutex trylock and unlock
operations are done within the same context; and therefore we need not
worry about the PI-boosting issues that comes along with no single-owner
lock guarantees.

While we don't want to support this in mutexes, there is no way out of
this yet; so lets get rid of the WARNs for now, as it is only fair to
code that has historically relied on non-preemptible softirq guarantees.
In addition, changing the lock type is also unviable: exclusive rwsems
have the same issue (just not the WARN_ON) and counting semaphores
would introduce a performance hit as mutexes are a lot more optimized.

This reverts:

    a0855d24fc22: ("locking/mutex: Complain upon mutex API misuse in IRQ contexts")

Fixes: a0855d24fc22: ("locking/mutex: Complain upon mutex API misuse in IRQ contexts")
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Tested-by: David Howells <dhowells@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-afs@lists.infradead.org
Cc: linux-fsdevel@vger.kernel.org
Cc: will@kernel.org
Link: https://lkml.kernel.org/r/20191210220523.28540-1-dave@stgolabs.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2019-12-11 00:27:43 +01:00
Davidlohr Bueso a0855d24fc locking/mutex: Complain upon mutex API misuse in IRQ contexts
Add warning checks if mutex_trylock() or mutex_unlock() are used in
IRQ contexts, under CONFIG_DEBUG_MUTEXES=y.

While the mutex rules and semantics are explicitly documented, this allows
to expose any abusers and robustifies the whole thing.

While trylock and unlock are non-blocking, calling from IRQ context
is still forbidden (lock must be within the same context as unlock).

Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: dave@stgolabs.net
Link: https://lkml.kernel.org/r/20191025033634.3330-1-dave@stgolabs.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2019-10-29 12:22:52 +01:00
Qian Cai 5facae4f35 locking/lockdep: Remove unused @nested argument from lock_release()
Since the following commit:

  b4adfe8e05 ("locking/lockdep: Remove unused argument in __lock_release")

@nested is no longer used in lock_release(), so remove it from all
lock_release() calls and friends.

Signed-off-by: Qian Cai <cai@lca.pw>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Will Deacon <will@kernel.org>
Acked-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: airlied@linux.ie
Cc: akpm@linux-foundation.org
Cc: alexander.levin@microsoft.com
Cc: daniel@iogearbox.net
Cc: davem@davemloft.net
Cc: dri-devel@lists.freedesktop.org
Cc: duyuyang@gmail.com
Cc: gregkh@linuxfoundation.org
Cc: hannes@cmpxchg.org
Cc: intel-gfx@lists.freedesktop.org
Cc: jack@suse.com
Cc: jlbec@evilplan.or
Cc: joonas.lahtinen@linux.intel.com
Cc: joseph.qi@linux.alibaba.com
Cc: jslaby@suse.com
Cc: juri.lelli@redhat.com
Cc: maarten.lankhorst@linux.intel.com
Cc: mark@fasheh.com
Cc: mhocko@kernel.org
Cc: mripard@kernel.org
Cc: ocfs2-devel@oss.oracle.com
Cc: rodrigo.vivi@intel.com
Cc: sean@poorly.run
Cc: st@kernel.org
Cc: tj@kernel.org
Cc: tytso@mit.edu
Cc: vdavydov.dev@gmail.com
Cc: vincent.guittot@linaro.org
Cc: viro@zeniv.linux.org.uk
Link: https://lkml.kernel.org/r/1568909380-32199-1-git-send-email-cai@lca.pw
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2019-10-09 12:46:10 +02:00
Peter Zijlstra e57d143091 mutex: Fix up mutex_waiter usage
The patch moving bits into mutex.c was a little too much; by also
moving struct mutex_waiter a few less common CONFIGs would no longer
build.

Fixes: 5f35d5a66b ("locking/mutex: Make __mutex_owner static to mutex.c")
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
2019-08-08 09:09:25 +02:00
Mukesh Ojha a037d26922 locking/mutex: Use mutex flags macro instead of hard code
Use the mutex flag macro instead of hard code value inside
__mutex_owner().

Signed-off-by: Mukesh Ojha <mojha@codeaurora.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: mingo@redhat.com
Cc: will@kernel.org
Link: https://lkml.kernel.org/r/1564585504-3543-2-git-send-email-mojha@codeaurora.org
2019-08-06 12:49:16 +02:00
Mukesh Ojha 5f35d5a66b locking/mutex: Make __mutex_owner static to mutex.c
__mutex_owner() should only be used by the mutex api's.
So, to put this restiction let's move the __mutex_owner()
function definition from linux/mutex.h to mutex.c file.

There exist functions that uses __mutex_owner() like
mutex_is_locked() and mutex_trylock_recursive(), So
to keep legacy thing intact move them as well and
export them.

Move mutex_waiter structure also to keep it private to the
file.

Signed-off-by: Mukesh Ojha <mojha@codeaurora.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: mingo@redhat.com
Cc: will@kernel.org
Link: https://lkml.kernel.org/r/1564585504-3543-1-git-send-email-mojha@codeaurora.org
2019-08-06 12:49:16 +02:00
Sebastian Andrzej Siewior 6c11c6e3d5 locking/mutex: Test for initialized mutex
An uninitialized/ zeroed mutex will go unnoticed because there is no
check for it. There is a magic check in the unlock's slowpath path which
might go unnoticed if the unlock happens in the fastpath.

Add a ->magic check early in the mutex_lock() and mutex_trylock() path.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Will Deacon <will@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: https://lkml.kernel.org/r/20190703092125.lsdf4gpsh2plhavb@linutronix.de
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2019-07-25 15:39:27 +02:00
Mauro Carvalho Chehab 387b14684f docs: locking: convert docs to ReST and rename to *.rst
Convert the locking documents to ReST and add them to the
kernel development book where it belongs.

Most of the stuff here is just to make Sphinx to properly
parse the text file, as they're already in good shape,
not requiring massive changes in order to be parsed.

The conversion is actually:
  - add blank lines and identation in order to identify paragraphs;
  - fix tables markups;
  - add some lists markups;
  - mark literal blocks;
  - adjust title markups.

At its new index.rst, let's add a :orphan: while this is not linked to
the main index.rst file, in order to avoid build warnings.

Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
Acked-by: Federico Vaga <federico.vaga@vaga.pv.it>
2019-07-15 08:53:27 -03:00
Thomas Gleixner 457c899653 treewide: Add SPDX license identifier for missed files
Add SPDX license identifiers to all files which:

 - Have no license information of any form

 - Have EXPORT_.*_SYMBOL_GPL inside which was used in the
   initial scan/conversion to ignore the file

These files fall under the project license, GPL v2 only. The resulting SPDX
license identifier is:

  GPL-2.0-only

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-05-21 10:50:45 +02:00
Davidlohr Bueso 3bb5f4ac55 kernel/locking/mutex.c: remove caller signal_pending branch predictions
This is already done for us internally by the signal machinery.

Link: http://lkml.kernel.org/r/20181116002713.8474-2-dave@stgolabs.net
Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-01-04 13:13:48 -08:00
Thomas Hellstrom e13e2366d8 locking/mutex: Fix mutex debug call and ww_mutex documentation
The following commit:

  08295b3b5b ("Implement an algorithm choice for Wound-Wait mutexes")

introduced a reference in the documentation to a function that was
removed in an earlier commit.

It also forgot to remove a call to debug_mutex_add_waiter() which is now
unconditionally called by __mutex_add_waiter().

Fix those bugs.

Signed-off-by: Thomas Hellstrom <thellstrom@vmware.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: dri-devel@lists.freedesktop.org
Fixes: 08295b3b5b ("Implement an algorithm choice for Wound-Wait mutexes")
Link: http://lkml.kernel.org/r/20180903140708.2401-1-thellstrom@vmware.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2018-09-10 10:05:10 +02:00
Thomas Hellstrom 08295b3b5b locking: Implement an algorithm choice for Wound-Wait mutexes
The current Wound-Wait mutex algorithm is actually not Wound-Wait but
Wait-Die. Implement also Wound-Wait as a per-ww-class choice. Wound-Wait
is, contrary to Wait-Die a preemptive algorithm and is known to generate
fewer backoffs. Testing reveals that this is true if the
number of simultaneous contending transactions is small.
As the number of simultaneous contending threads increases, Wait-Wound
becomes inferior to Wait-Die in terms of elapsed time.
Possibly due to the larger number of held locks of sleeping transactions.

Update documentation and callers.

Timings using git://people.freedesktop.org/~thomash/ww_mutex_test
tag patch-18-06-15

Each thread runs 100000 batches of lock / unlock 800 ww mutexes randomly
chosen out of 100000. Four core Intel x86_64:

Algorithm    #threads       Rollbacks  time
Wound-Wait   4              ~100       ~17s.
Wait-Die     4              ~150000    ~19s.
Wound-Wait   16             ~360000    ~109s.
Wait-Die     16             ~450000    ~82s.

Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Gustavo Padovan <gustavo@padovan.org>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Sean Paul <seanpaul@chromium.org>
Cc: David Airlie <airlied@linux.ie>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Cc: Josh Triplett <josh@joshtriplett.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Kate Stewart <kstewart@linuxfoundation.org>
Cc: Philippe Ombredanne <pombredanne@nexb.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-doc@vger.kernel.org
Cc: linux-media@vger.kernel.org
Cc: linaro-mm-sig@lists.linaro.org
Co-authored-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Thomas Hellstrom <thellstrom@vmware.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Ingo Molnar <mingo@kernel.org>
2018-07-03 09:44:36 +02:00
Peter Ziljstra 55f036ca7e locking: WW mutex cleanup
Make the WW mutex code more readable by adding comments, splitting up
functions and pointing out that we're actually using the Wait-Die
algorithm.

Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Gustavo Padovan <gustavo@padovan.org>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Sean Paul <seanpaul@chromium.org>
Cc: David Airlie <airlied@linux.ie>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Cc: Josh Triplett <josh@joshtriplett.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Kate Stewart <kstewart@linuxfoundation.org>
Cc: Philippe Ombredanne <pombredanne@nexb.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-doc@vger.kernel.org
Cc: linux-media@vger.kernel.org
Cc: linaro-mm-sig@lists.linaro.org
Co-authored-by: Thomas Hellstrom <thellstrom@vmware.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Thomas Hellstrom <thellstrom@vmware.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
2018-07-03 09:42:40 +02:00
Peter Zijlstra c427f69564 locking/mutex: Optimize __mutex_trylock_fast()
Use try_cmpxchg to avoid the pointless TEST instruction..
And add the (missing) atomic_long_try_cmpxchg*() wrappery.

On x86_64 this gives:

0000000000000710 <mutex_lock>:						0000000000000710 <mutex_lock>:
 710:   65 48 8b 14 25 00 00    mov    %gs:0x0,%rdx                      710:   65 48 8b 14 25 00 00    mov    %gs:0x0,%rdx
 717:   00 00                                                            717:   00 00
                        715: R_X86_64_32S       current_task                                    715: R_X86_64_32S       current_task
 719:   31 c0                   xor    %eax,%eax                         719:   31 c0                   xor    %eax,%eax
 71b:   f0 48 0f b1 17          lock cmpxchg %rdx,(%rdi)                 71b:   f0 48 0f b1 17          lock cmpxchg %rdx,(%rdi)
 720:   48 85 c0                test   %rax,%rax                         720:   75 02                   jne    724 <mutex_lock+0x14>
 723:   75 02                   jne    727 <mutex_lock+0x17>             722:   f3 c3                   repz retq
 725:   f3 c3                   repz retq                                724:   eb da                   jmp    700 <__mutex_lock_slowpath>
 727:   eb d7                   jmp    700 <__mutex_lock_slowpath>       726:   66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
 729:   0f 1f 80 00 00 00 00    nopl   0x0(%rax)                         72d:   00 00 00

On ARM64 this gives:

000000000000638 <mutex_lock>:						0000000000000638 <mutex_lock>:
     638:       d5384101        mrs     x1, sp_el0                           638:       d5384101        mrs     x1, sp_el0
     63c:       d2800002        mov     x2, #0x0                             63c:       d2800002        mov     x2, #0x0
     640:       f9800011        prfm    pstl1strm, [x0]                      640:       f9800011        prfm    pstl1strm, [x0]
     644:       c85ffc03        ldaxr   x3, [x0]                             644:       c85ffc03        ldaxr   x3, [x0]
     648:       ca020064        eor     x4, x3, x2                           648:       ca020064        eor     x4, x3, x2
     64c:       b5000064        cbnz    x4, 658 <mutex_lock+0x20>            64c:       b5000064        cbnz    x4, 658 <mutex_lock+0x20>
     650:       c8047c01        stxr    w4, x1, [x0]                         650:       c8047c01        stxr    w4, x1, [x0]
     654:       35ffff84        cbnz    w4, 644 <mutex_lock+0xc>             654:       35ffff84        cbnz    w4, 644 <mutex_lock+0xc>
     658:       b40000c3        cbz     x3, 670 <mutex_lock+0x38>            658:       b5000043        cbnz    x3, 660 <mutex_lock+0x28>
     65c:       a9bf7bfd        stp     x29, x30, [sp,#-16]!                 65c:       d65f03c0        ret
     660:       910003fd        mov     x29, sp                              660:       a9bf7bfd        stp     x29, x30, [sp,#-16]!
     664:       97ffffef        bl      620 <__mutex_lock_slowpath>          664:       910003fd        mov     x29, sp
     668:       a8c17bfd        ldp     x29, x30, [sp],#16                   668:       97ffffee        bl      620 <__mutex_lock_slowpath>
     66c:       d65f03c0        ret                                          66c:       a8c17bfd        ldp     x29, x30, [sp],#16
     670:       d65f03c0        ret                                          670:       d65f03c0        ret

Reported-by: Matthew Wilcox <mawilcox@microsoft.com>
Acked-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2018-05-04 10:02:39 +02:00
Matthew Wilcox 45dbac0e28 locking/mutex: Improve documentation
On Wed, Mar 14, 2018 at 01:56:31PM -0700, Andrew Morton wrote:

> My memory is weak and our documentation is awful.  What does
> mutex_lock_killable() actually do and how does it differ from
> mutex_lock_interruptible()?

Add kernel-doc for mutex_lock_killable() and mutex_lock_io().  Reword the
kernel-doc for mutex_lock_interruptible().

Signed-off-by: Matthew Wilcox <mawilcox@microsoft.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Kirill Tkhai <ktkhai@virtuozzo.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: cl@linux.com
Cc: tj@kernel.org
Link: http://lkml.kernel.org/r/20180315115812.GA9949@bombadil.infradead.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2018-03-20 08:07:41 +01:00
Mauro Carvalho Chehab 7b4ff1adb5 mutex, futex: adjust kernel-doc markups to generate ReST
There are a few issues on some kernel-doc markups that was
causing troubles with kernel-doc output on ReST format:

./kernel/futex.c:492: WARNING: Inline emphasis start-string without end-string.
./kernel/futex.c:1264: WARNING: Block quote ends without a blank line; unexpected unindent.
./kernel/futex.c:1721: WARNING: Block quote ends without a blank line; unexpected unindent.
./kernel/futex.c:2338: WARNING: Block quote ends without a blank line; unexpected unindent.
./kernel/futex.c:2426: WARNING: Block quote ends without a blank line; unexpected unindent.
./kernel/futex.c:2899: WARNING: Block quote ends without a blank line; unexpected unindent.
./kernel/futex.c:2972: WARNING: Block quote ends without a blank line; unexpected unindent.

Fix them.

No functional changes.

Acked-by: Darren Hart (VMware) <dvhart@infradead.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
2017-05-16 08:43:25 -03:00
Ingo Molnar b17b01533b sched/headers: Prepare for new header dependencies before moving code to <linux/sched/debug.h>
We are going to split <linux/sched/debug.h> out of <linux/sched.h>, which
will have to be picked up from other headers and a couple of .c files.

Create a trivial placeholder <linux/sched/debug.h> file that just
maps to <linux/sched.h> to make this patch obviously correct and
bisectable.

Include the new header in the files that are going to need it.

Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-03-02 08:42:34 +01:00
Ingo Molnar 174cd4b1e5 sched/headers: Prepare to move signal wakeup & sigpending methods from <linux/sched.h> into <linux/sched/signal.h>
Fix up affected files that include this signal functionality via sched.h.

Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-03-02 08:42:32 +01:00
Ingo Molnar 84f001e157 sched/headers: Prepare for new header dependencies before moving code to <linux/sched/wake_q.h>
We are going to split <linux/sched/wake_q.h> out of <linux/sched.h>, which
will have to be picked up from other headers and a couple of .c files.

Create a trivial placeholder <linux/sched/wake_q.h> file that just
maps to <linux/sched.h> to make this patch obviously correct and
bisectable.

Include the new header in the files that are going to need it.

Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-03-02 08:42:26 +01:00
Linus Torvalds 42e1b14b6e Merge branch 'locking-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull locking updates from Ingo Molnar:
 "The main changes in this cycle were:

   - Implement wraparound-safe refcount_t and kref_t types based on
     generic atomic primitives (Peter Zijlstra)

   - Improve and fix the ww_mutex code (Nicolai Hähnle)

   - Add self-tests to the ww_mutex code (Chris Wilson)

   - Optimize percpu-rwsems with the 'rcuwait' mechanism (Davidlohr
     Bueso)

   - Micro-optimize the current-task logic all around the core kernel
     (Davidlohr Bueso)

   - Tidy up after recent optimizations: remove stale code and APIs,
     clean up the code (Waiman Long)

   - ... plus misc fixes, updates and cleanups"

* 'locking-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (50 commits)
  fork: Fix task_struct alignment
  locking/spinlock/debug: Remove spinlock lockup detection code
  lockdep: Fix incorrect condition to print bug msgs for MAX_LOCKDEP_CHAIN_HLOCKS
  lkdtm: Convert to refcount_t testing
  kref: Implement 'struct kref' using refcount_t
  refcount_t: Introduce a special purpose refcount type
  sched/wake_q: Clarify queue reinit comment
  sched/wait, rcuwait: Fix typo in comment
  locking/mutex: Fix lockdep_assert_held() fail
  locking/rtmutex: Flip unlikely() branch to likely() in __rt_mutex_slowlock()
  locking/rwsem: Reinit wake_q after use
  locking/rwsem: Remove unnecessary atomic_long_t casts
  jump_labels: Move header guard #endif down where it belongs
  locking/atomic, kref: Implement kref_put_lock()
  locking/ww_mutex: Turn off __must_check for now
  locking/atomic, kref: Avoid more abuse
  locking/atomic, kref: Use kref_get_unless_zero() more
  locking/atomic, kref: Kill kref_sub()
  locking/atomic, kref: Add kref_read()
  locking/atomic, kref: Add KREF_INIT()
  ...
2017-02-20 13:23:30 -08:00
Peter Zijlstra b9c16a0e1f locking/mutex: Fix lockdep_assert_held() fail
In commit:

  659cf9f582 ("locking/ww_mutex: Optimize ww-mutexes by waking at most one waiter for backoff when acquiring the lock")

I replaced a comment with a lockdep_assert_held(). However it turns out
we hide that lock from lockdep for hysterical raisins, which results
in the assertion always firing.

Remove the old debug code as lockdep will easily spot the abuse it was
meant to catch, which will make the lock visible to lockdep and make
the assertion work as intended.

Reported-by: Mike Galbraith <efault@gmx.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Nicolai Haehnle <Nicolai.Haehnle@amd.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Fixes: 659cf9f582 ("locking/ww_mutex: Optimize ww-mutexes by waking at most one waiter for backoff when acquiring the lock")
Link: http://lkml.kernel.org/r/20170117150609.GB32474@worktop
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-01-30 11:42:59 +01:00
Tejun Heo 1460cb65a1 locking/mutex, sched/wait: Add mutex_lock_io()
We sometimes end up propagating IO blocking through mutexes; however,
because there currently is no way of annotating mutex sleeps as
iowait, there are cases where iowait and /proc/stat:procs_blocked
report misleading numbers obscuring the actual state of the system.

This patch adds mutex_lock_io() so that mutex sleeps can be marked as
iowait in those cases.

Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: adilger.kernel@dilger.ca
Cc: jack@suse.com
Cc: kernel-team@fb.com
Cc: mingbo@fb.com
Cc: tytso@mit.edu
Link: http://lkml.kernel.org/r/1477673892-28940-4-git-send-email-tj@kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-01-14 11:30:05 +01:00
Nicolai Hähnle 977625a693 locking/mutex: Initialize mutex_waiter::ww_ctx with poison when debugging
Help catch cases where mutex_lock is used directly on w/w mutexes, which
otherwise result in the w/w tasks reading uninitialized data.

Signed-off-by: Nicolai Hähnle <Nicolai.Haehnle@amd.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Maarten Lankhorst <dev@mblankhorst.nl>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: dri-devel@lists.freedesktop.org
Link: http://lkml.kernel.org/r/1482346000-9927-12-git-send-email-nhaehnle@gmail.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-01-14 11:14:53 +01:00
Nicolai Hähnle c516df978d locking/ww_mutex: Optimize ww-mutexes by yielding to other waiters from optimistic spin
Lock stealing is less beneficial for w/w mutexes since we may just end up
backing off if we stole from a thread with an earlier acquire stamp that
already holds another w/w mutex that we also need. So don't spin
optimistically unless we are sure that there is no other waiter that might
cause us to back off.

Median timings taken of a contention-heavy GPU workload:

Before:

  real    0m52.946s
  user    0m7.272s
  sys     1m55.964s

After:

  real    0m53.086s
  user    0m7.360s
  sys     1m46.204s

This particular workload still spends 20%-25% of CPU in mutex_spin_on_owner
according to perf, but my attempts to further reduce this spinning based on
various heuristics all lead to an increase in measured wall time despite
the decrease in sys time.

Signed-off-by: Nicolai Hähnle <Nicolai.Haehnle@amd.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Maarten Lankhorst <dev@mblankhorst.nl>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: dri-devel@lists.freedesktop.org
Link: http://lkml.kernel.org/r/1482346000-9927-11-git-send-email-nhaehnle@gmail.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-01-14 11:14:52 +01:00
Nicolai Hähnle 25f13b4040 locking/ww_mutex: Re-check ww->ctx in the inner optimistic spin loop
In the following scenario, thread #1 should back off its attempt to lock
ww1 and unlock ww2 (assuming the acquire context stamps are ordered
accordingly).

    Thread #0               Thread #1
    ---------               ---------
                            successfully lock ww2
    set ww1->base.owner
                            attempt to lock ww1
                            confirm ww1->ctx == NULL
                            enter mutex_spin_on_owner
    set ww1->ctx

What was likely to happen previously is:

    attempt to lock ww2
    refuse to spin because
      ww2->ctx != NULL
    schedule()
                            detect thread #0 is off CPU
                            stop optimistic spin
                            return -EDEADLK
                            unlock ww2
                            wakeup thread #0
    lock ww2

Now, we are more likely to see:

                            detect ww1->ctx != NULL
                            stop optimistic spin
                            return -EDEADLK
                            unlock ww2
    successfully lock ww2

... because thread #1 will stop its optimistic spin as soon as possible.

The whole scenario is quite unlikely, since it requires thread #1 to get
between thread #0 setting the owner and setting the ctx. But since we're
idling here anyway, the additional check is basically free.

Found by inspection.

Signed-off-by: Nicolai Hähnle <Nicolai.Haehnle@amd.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Maarten Lankhorst <dev@mblankhorst.nl>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: dri-devel@lists.freedesktop.org
Link: http://lkml.kernel.org/r/1482346000-9927-10-git-send-email-nhaehnle@gmail.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-01-14 11:14:51 +01:00