![Masahiro Yamada](/assets/img/avatar_default.png)
The use of config_enabled() against config options is ambiguous. In practical terms, config_enabled() is equivalent to IS_BUILTIN(), but the author might have used it for the meaning of IS_ENABLED(). Using IS_ENABLED(), IS_BUILTIN(), IS_MODULE() etc. makes the intention clearer. This commit replaces config_enabled() with IS_ENABLED() where possible. This commit is only touching bool config options. I noticed two cases where config_enabled() is used against a tristate option: - config_enabled(CONFIG_HWMON) [ drivers/net/wireless/ath/ath10k/thermal.c ] - config_enabled(CONFIG_BACKLIGHT_CLASS_DEVICE) [ drivers/gpu/drm/gma500/opregion.c ] I did not touch them because they should be converted to IS_BUILTIN() in order to keep the logic, but I was not sure it was the authors' intention. Link: http://lkml.kernel.org/r/1465215656-20569-1-git-send-email-yamada.masahiro@socionext.com Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com> Acked-by: Kees Cook <keescook@chromium.org> Cc: Stas Sergeev <stsp@list.ru> Cc: Matt Redfearn <matt.redfearn@imgtec.com> Cc: Joshua Kinard <kumba@gentoo.org> Cc: Jiri Slaby <jslaby@suse.com> Cc: Bjorn Helgaas <bhelgaas@google.com> Cc: Borislav Petkov <bp@suse.de> Cc: Markos Chandras <markos.chandras@imgtec.com> Cc: "Dmitry V. Levin" <ldv@altlinux.org> Cc: yu-cheng yu <yu-cheng.yu@intel.com> Cc: James Hogan <james.hogan@imgtec.com> Cc: Brian Gerst <brgerst@gmail.com> Cc: Johannes Berg <johannes@sipsolutions.net> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Will Drewry <wad@chromium.org> Cc: Nikolay Martynov <mar.kolya@gmail.com> Cc: Huacai Chen <chenhc@lemote.com> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Daniel Borkmann <daniel@iogearbox.net> Cc: Leonid Yegoshin <Leonid.Yegoshin@imgtec.com> Cc: Rafal Milecki <zajec5@gmail.com> Cc: James Cowgill <James.Cowgill@imgtec.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Ralf Baechle <ralf@linux-mips.org> Cc: Alex Smith <alex.smith@imgtec.com> Cc: Adam Buchbinder <adam.buchbinder@gmail.com> Cc: Qais Yousef <qais.yousef@imgtec.com> Cc: Jiang Liu <jiang.liu@linux.intel.com> Cc: Mikko Rapeli <mikko.rapeli@iki.fi> Cc: Paul Gortmaker <paul.gortmaker@windriver.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: Brian Norris <computersforpeace@gmail.com> Cc: Hidehiro Kawai <hidehiro.kawai.ez@hitachi.com> Cc: "Luis R. Rodriguez" <mcgrof@do-not-panic.com> Cc: Andy Lutomirski <luto@amacapital.net> Cc: Ingo Molnar <mingo@redhat.com> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> Cc: Roland McGrath <roland@hack.frob.com> Cc: Paul Burton <paul.burton@imgtec.com> Cc: Kalle Valo <kvalo@qca.qualcomm.com> Cc: Viresh Kumar <viresh.kumar@linaro.org> Cc: Tony Wu <tung7970@gmail.com> Cc: Huaitong Han <huaitong.han@intel.com> Cc: Sumit Semwal <sumit.semwal@linaro.org> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Juergen Gross <jgross@suse.com> Cc: Jason Cooper <jason@lakedaemon.net> Cc: "David S. Miller" <davem@davemloft.net> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Andrea Gelmini <andrea.gelmini@gelma.net> Cc: David Woodhouse <dwmw2@infradead.org> Cc: Marc Zyngier <marc.zyngier@arm.com> Cc: Rabin Vincent <rabin@rab.in> Cc: "Maciej W. Rozycki" <macro@imgtec.com> Cc: David Daney <david.daney@cavium.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
379 lines
13 KiB
C
379 lines
13 KiB
C
/*
|
|
* Wound/Wait Mutexes: blocking mutual exclusion locks with deadlock avoidance
|
|
*
|
|
* Original mutex implementation started by Ingo Molnar:
|
|
*
|
|
* Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
|
|
*
|
|
* Wound/wait implementation:
|
|
* Copyright (C) 2013 Canonical Ltd.
|
|
*
|
|
* This file contains the main data structure and API definitions.
|
|
*/
|
|
|
|
#ifndef __LINUX_WW_MUTEX_H
|
|
#define __LINUX_WW_MUTEX_H
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
struct ww_class {
|
|
atomic_long_t stamp;
|
|
struct lock_class_key acquire_key;
|
|
struct lock_class_key mutex_key;
|
|
const char *acquire_name;
|
|
const char *mutex_name;
|
|
};
|
|
|
|
struct ww_acquire_ctx {
|
|
struct task_struct *task;
|
|
unsigned long stamp;
|
|
unsigned acquired;
|
|
#ifdef CONFIG_DEBUG_MUTEXES
|
|
unsigned done_acquire;
|
|
struct ww_class *ww_class;
|
|
struct ww_mutex *contending_lock;
|
|
#endif
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
struct lockdep_map dep_map;
|
|
#endif
|
|
#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
|
|
unsigned deadlock_inject_interval;
|
|
unsigned deadlock_inject_countdown;
|
|
#endif
|
|
};
|
|
|
|
struct ww_mutex {
|
|
struct mutex base;
|
|
struct ww_acquire_ctx *ctx;
|
|
#ifdef CONFIG_DEBUG_MUTEXES
|
|
struct ww_class *ww_class;
|
|
#endif
|
|
};
|
|
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
# define __WW_CLASS_MUTEX_INITIALIZER(lockname, ww_class) \
|
|
, .ww_class = &ww_class
|
|
#else
|
|
# define __WW_CLASS_MUTEX_INITIALIZER(lockname, ww_class)
|
|
#endif
|
|
|
|
#define __WW_CLASS_INITIALIZER(ww_class) \
|
|
{ .stamp = ATOMIC_LONG_INIT(0) \
|
|
, .acquire_name = #ww_class "_acquire" \
|
|
, .mutex_name = #ww_class "_mutex" }
|
|
|
|
#define __WW_MUTEX_INITIALIZER(lockname, class) \
|
|
{ .base = { \__MUTEX_INITIALIZER(lockname) } \
|
|
__WW_CLASS_MUTEX_INITIALIZER(lockname, class) }
|
|
|
|
#define DEFINE_WW_CLASS(classname) \
|
|
struct ww_class classname = __WW_CLASS_INITIALIZER(classname)
|
|
|
|
#define DEFINE_WW_MUTEX(mutexname, ww_class) \
|
|
struct ww_mutex mutexname = __WW_MUTEX_INITIALIZER(mutexname, ww_class)
|
|
|
|
/**
|
|
* ww_mutex_init - initialize the w/w mutex
|
|
* @lock: the mutex to be initialized
|
|
* @ww_class: the w/w class the mutex should belong to
|
|
*
|
|
* Initialize the w/w mutex to unlocked state and associate it with the given
|
|
* class.
|
|
*
|
|
* It is not allowed to initialize an already locked mutex.
|
|
*/
|
|
static inline void ww_mutex_init(struct ww_mutex *lock,
|
|
struct ww_class *ww_class)
|
|
{
|
|
__mutex_init(&lock->base, ww_class->mutex_name, &ww_class->mutex_key);
|
|
lock->ctx = NULL;
|
|
#ifdef CONFIG_DEBUG_MUTEXES
|
|
lock->ww_class = ww_class;
|
|
#endif
|
|
}
|
|
|
|
/**
|
|
* ww_acquire_init - initialize a w/w acquire context
|
|
* @ctx: w/w acquire context to initialize
|
|
* @ww_class: w/w class of the context
|
|
*
|
|
* Initializes an context to acquire multiple mutexes of the given w/w class.
|
|
*
|
|
* Context-based w/w mutex acquiring can be done in any order whatsoever within
|
|
* a given lock class. Deadlocks will be detected and handled with the
|
|
* wait/wound logic.
|
|
*
|
|
* Mixing of context-based w/w mutex acquiring and single w/w mutex locking can
|
|
* result in undetected deadlocks and is so forbidden. Mixing different contexts
|
|
* for the same w/w class when acquiring mutexes can also result in undetected
|
|
* deadlocks, and is hence also forbidden. Both types of abuse will be caught by
|
|
* enabling CONFIG_PROVE_LOCKING.
|
|
*
|
|
* Nesting of acquire contexts for _different_ w/w classes is possible, subject
|
|
* to the usual locking rules between different lock classes.
|
|
*
|
|
* An acquire context must be released with ww_acquire_fini by the same task
|
|
* before the memory is freed. It is recommended to allocate the context itself
|
|
* on the stack.
|
|
*/
|
|
static inline void ww_acquire_init(struct ww_acquire_ctx *ctx,
|
|
struct ww_class *ww_class)
|
|
{
|
|
ctx->task = current;
|
|
ctx->stamp = atomic_long_inc_return(&ww_class->stamp);
|
|
ctx->acquired = 0;
|
|
#ifdef CONFIG_DEBUG_MUTEXES
|
|
ctx->ww_class = ww_class;
|
|
ctx->done_acquire = 0;
|
|
ctx->contending_lock = NULL;
|
|
#endif
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
debug_check_no_locks_freed((void *)ctx, sizeof(*ctx));
|
|
lockdep_init_map(&ctx->dep_map, ww_class->acquire_name,
|
|
&ww_class->acquire_key, 0);
|
|
mutex_acquire(&ctx->dep_map, 0, 0, _RET_IP_);
|
|
#endif
|
|
#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
|
|
ctx->deadlock_inject_interval = 1;
|
|
ctx->deadlock_inject_countdown = ctx->stamp & 0xf;
|
|
#endif
|
|
}
|
|
|
|
/**
|
|
* ww_acquire_done - marks the end of the acquire phase
|
|
* @ctx: the acquire context
|
|
*
|
|
* Marks the end of the acquire phase, any further w/w mutex lock calls using
|
|
* this context are forbidden.
|
|
*
|
|
* Calling this function is optional, it is just useful to document w/w mutex
|
|
* code and clearly designated the acquire phase from actually using the locked
|
|
* data structures.
|
|
*/
|
|
static inline void ww_acquire_done(struct ww_acquire_ctx *ctx)
|
|
{
|
|
#ifdef CONFIG_DEBUG_MUTEXES
|
|
lockdep_assert_held(ctx);
|
|
|
|
DEBUG_LOCKS_WARN_ON(ctx->done_acquire);
|
|
ctx->done_acquire = 1;
|
|
#endif
|
|
}
|
|
|
|
/**
|
|
* ww_acquire_fini - releases a w/w acquire context
|
|
* @ctx: the acquire context to free
|
|
*
|
|
* Releases a w/w acquire context. This must be called _after_ all acquired w/w
|
|
* mutexes have been released with ww_mutex_unlock.
|
|
*/
|
|
static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx)
|
|
{
|
|
#ifdef CONFIG_DEBUG_MUTEXES
|
|
mutex_release(&ctx->dep_map, 0, _THIS_IP_);
|
|
|
|
DEBUG_LOCKS_WARN_ON(ctx->acquired);
|
|
if (!IS_ENABLED(CONFIG_PROVE_LOCKING))
|
|
/*
|
|
* lockdep will normally handle this,
|
|
* but fail without anyway
|
|
*/
|
|
ctx->done_acquire = 1;
|
|
|
|
if (!IS_ENABLED(CONFIG_DEBUG_LOCK_ALLOC))
|
|
/* ensure ww_acquire_fini will still fail if called twice */
|
|
ctx->acquired = ~0U;
|
|
#endif
|
|
}
|
|
|
|
extern int __must_check __ww_mutex_lock(struct ww_mutex *lock,
|
|
struct ww_acquire_ctx *ctx);
|
|
extern int __must_check __ww_mutex_lock_interruptible(struct ww_mutex *lock,
|
|
struct ww_acquire_ctx *ctx);
|
|
|
|
/**
|
|
* ww_mutex_lock - acquire the w/w mutex
|
|
* @lock: the mutex to be acquired
|
|
* @ctx: w/w acquire context, or NULL to acquire only a single lock.
|
|
*
|
|
* Lock the w/w mutex exclusively for this task.
|
|
*
|
|
* Deadlocks within a given w/w class of locks are detected and handled with the
|
|
* wait/wound algorithm. If the lock isn't immediately avaiable this function
|
|
* will either sleep until it is (wait case). Or it selects the current context
|
|
* for backing off by returning -EDEADLK (wound case). Trying to acquire the
|
|
* same lock with the same context twice is also detected and signalled by
|
|
* returning -EALREADY. Returns 0 if the mutex was successfully acquired.
|
|
*
|
|
* In the wound case the caller must release all currently held w/w mutexes for
|
|
* the given context and then wait for this contending lock to be available by
|
|
* calling ww_mutex_lock_slow. Alternatively callers can opt to not acquire this
|
|
* lock and proceed with trying to acquire further w/w mutexes (e.g. when
|
|
* scanning through lru lists trying to free resources).
|
|
*
|
|
* The mutex must later on be released by the same task that
|
|
* acquired it. The task may not exit without first unlocking the mutex. Also,
|
|
* kernel memory where the mutex resides must not be freed with the mutex still
|
|
* locked. The mutex must first be initialized (or statically defined) before it
|
|
* can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
|
|
* of the same w/w lock class as was used to initialize the acquire context.
|
|
*
|
|
* A mutex acquired with this function must be released with ww_mutex_unlock.
|
|
*/
|
|
static inline int ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
|
|
{
|
|
if (ctx)
|
|
return __ww_mutex_lock(lock, ctx);
|
|
|
|
mutex_lock(&lock->base);
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* ww_mutex_lock_interruptible - acquire the w/w mutex, interruptible
|
|
* @lock: the mutex to be acquired
|
|
* @ctx: w/w acquire context
|
|
*
|
|
* Lock the w/w mutex exclusively for this task.
|
|
*
|
|
* Deadlocks within a given w/w class of locks are detected and handled with the
|
|
* wait/wound algorithm. If the lock isn't immediately avaiable this function
|
|
* will either sleep until it is (wait case). Or it selects the current context
|
|
* for backing off by returning -EDEADLK (wound case). Trying to acquire the
|
|
* same lock with the same context twice is also detected and signalled by
|
|
* returning -EALREADY. Returns 0 if the mutex was successfully acquired. If a
|
|
* signal arrives while waiting for the lock then this function returns -EINTR.
|
|
*
|
|
* In the wound case the caller must release all currently held w/w mutexes for
|
|
* the given context and then wait for this contending lock to be available by
|
|
* calling ww_mutex_lock_slow_interruptible. Alternatively callers can opt to
|
|
* not acquire this lock and proceed with trying to acquire further w/w mutexes
|
|
* (e.g. when scanning through lru lists trying to free resources).
|
|
*
|
|
* The mutex must later on be released by the same task that
|
|
* acquired it. The task may not exit without first unlocking the mutex. Also,
|
|
* kernel memory where the mutex resides must not be freed with the mutex still
|
|
* locked. The mutex must first be initialized (or statically defined) before it
|
|
* can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
|
|
* of the same w/w lock class as was used to initialize the acquire context.
|
|
*
|
|
* A mutex acquired with this function must be released with ww_mutex_unlock.
|
|
*/
|
|
static inline int __must_check ww_mutex_lock_interruptible(struct ww_mutex *lock,
|
|
struct ww_acquire_ctx *ctx)
|
|
{
|
|
if (ctx)
|
|
return __ww_mutex_lock_interruptible(lock, ctx);
|
|
else
|
|
return mutex_lock_interruptible(&lock->base);
|
|
}
|
|
|
|
/**
|
|
* ww_mutex_lock_slow - slowpath acquiring of the w/w mutex
|
|
* @lock: the mutex to be acquired
|
|
* @ctx: w/w acquire context
|
|
*
|
|
* Acquires a w/w mutex with the given context after a wound case. This function
|
|
* will sleep until the lock becomes available.
|
|
*
|
|
* The caller must have released all w/w mutexes already acquired with the
|
|
* context and then call this function on the contended lock.
|
|
*
|
|
* Afterwards the caller may continue to (re)acquire the other w/w mutexes it
|
|
* needs with ww_mutex_lock. Note that the -EALREADY return code from
|
|
* ww_mutex_lock can be used to avoid locking this contended mutex twice.
|
|
*
|
|
* It is forbidden to call this function with any other w/w mutexes associated
|
|
* with the context held. It is forbidden to call this on anything else than the
|
|
* contending mutex.
|
|
*
|
|
* Note that the slowpath lock acquiring can also be done by calling
|
|
* ww_mutex_lock directly. This function here is simply to help w/w mutex
|
|
* locking code readability by clearly denoting the slowpath.
|
|
*/
|
|
static inline void
|
|
ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
|
|
{
|
|
int ret;
|
|
#ifdef CONFIG_DEBUG_MUTEXES
|
|
DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
|
|
#endif
|
|
ret = ww_mutex_lock(lock, ctx);
|
|
(void)ret;
|
|
}
|
|
|
|
/**
|
|
* ww_mutex_lock_slow_interruptible - slowpath acquiring of the w/w mutex, interruptible
|
|
* @lock: the mutex to be acquired
|
|
* @ctx: w/w acquire context
|
|
*
|
|
* Acquires a w/w mutex with the given context after a wound case. This function
|
|
* will sleep until the lock becomes available and returns 0 when the lock has
|
|
* been acquired. If a signal arrives while waiting for the lock then this
|
|
* function returns -EINTR.
|
|
*
|
|
* The caller must have released all w/w mutexes already acquired with the
|
|
* context and then call this function on the contended lock.
|
|
*
|
|
* Afterwards the caller may continue to (re)acquire the other w/w mutexes it
|
|
* needs with ww_mutex_lock. Note that the -EALREADY return code from
|
|
* ww_mutex_lock can be used to avoid locking this contended mutex twice.
|
|
*
|
|
* It is forbidden to call this function with any other w/w mutexes associated
|
|
* with the given context held. It is forbidden to call this on anything else
|
|
* than the contending mutex.
|
|
*
|
|
* Note that the slowpath lock acquiring can also be done by calling
|
|
* ww_mutex_lock_interruptible directly. This function here is simply to help
|
|
* w/w mutex locking code readability by clearly denoting the slowpath.
|
|
*/
|
|
static inline int __must_check
|
|
ww_mutex_lock_slow_interruptible(struct ww_mutex *lock,
|
|
struct ww_acquire_ctx *ctx)
|
|
{
|
|
#ifdef CONFIG_DEBUG_MUTEXES
|
|
DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
|
|
#endif
|
|
return ww_mutex_lock_interruptible(lock, ctx);
|
|
}
|
|
|
|
extern void ww_mutex_unlock(struct ww_mutex *lock);
|
|
|
|
/**
|
|
* ww_mutex_trylock - tries to acquire the w/w mutex without acquire context
|
|
* @lock: mutex to lock
|
|
*
|
|
* Trylocks a mutex without acquire context, so no deadlock detection is
|
|
* possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise.
|
|
*/
|
|
static inline int __must_check ww_mutex_trylock(struct ww_mutex *lock)
|
|
{
|
|
return mutex_trylock(&lock->base);
|
|
}
|
|
|
|
/***
|
|
* ww_mutex_destroy - mark a w/w mutex unusable
|
|
* @lock: the mutex to be destroyed
|
|
*
|
|
* This function marks the mutex uninitialized, and any subsequent
|
|
* use of the mutex is forbidden. The mutex must not be locked when
|
|
* this function is called.
|
|
*/
|
|
static inline void ww_mutex_destroy(struct ww_mutex *lock)
|
|
{
|
|
mutex_destroy(&lock->base);
|
|
}
|
|
|
|
/**
|
|
* ww_mutex_is_locked - is the w/w mutex locked
|
|
* @lock: the mutex to be queried
|
|
*
|
|
* Returns 1 if the mutex is locked, 0 if unlocked.
|
|
*/
|
|
static inline bool ww_mutex_is_locked(struct ww_mutex *lock)
|
|
{
|
|
return mutex_is_locked(&lock->base);
|
|
}
|
|
|
|
#endif
|