blob: 0bbc872ba72b90aeceb4cc175e564a14acfaf551 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Ingo Molnar6053ee32006-01-09 15:59:19 -08002/*
3 * Mutexes: blocking mutual exclusion locks
4 *
5 * started by Ingo Molnar:
6 *
7 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <[email protected]>
8 *
9 * This file contains the main data structure and API definitions.
10 */
11#ifndef __LINUX_MUTEX_H
12#define __LINUX_MUTEX_H
13
Maarten Lankhorst040a0a32013-06-24 10:30:04 +020014#include <asm/current.h>
Ingo Molnar6053ee32006-01-09 15:59:19 -080015#include <linux/list.h>
16#include <linux/spinlock_types.h>
Ingo Molnaref5d4702006-07-03 00:24:55 -070017#include <linux/lockdep.h>
Arun Sharma600634972011-07-26 16:09:06 -070018#include <linux/atomic.h>
Heiko Carstens083986e2013-09-28 11:23:59 +020019#include <asm/processor.h>
Jason Low90631822014-07-14 10:27:49 -070020#include <linux/osq_lock.h>
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020021#include <linux/debug_locks.h>
Ingo Molnar6053ee32006-01-09 15:59:19 -080022
Ingo Molnar6053ee32006-01-09 15:59:19 -080023/*
24 * Simple, straightforward mutexes with strict semantics:
25 *
26 * - only one task can hold the mutex at a time
27 * - only the owner can unlock the mutex
28 * - multiple unlocks are not permitted
29 * - recursive locking is not permitted
30 * - a mutex object must be initialized via the API
31 * - a mutex object must not be initialized via memset or copying
32 * - task may not exit with mutex held
33 * - memory areas where held locks reside must not be freed
34 * - held mutexes must not be reinitialized
Matti Linnanvuorif20fda42007-10-16 23:29:41 -070035 * - mutexes may not be used in hardware or software interrupt
36 * contexts such as tasklets and timers
Ingo Molnar6053ee32006-01-09 15:59:19 -080037 *
38 * These semantics are fully enforced when DEBUG_MUTEXES is
39 * enabled. Furthermore, besides enforcing the above rules, the mutex
40 * debugging code also implements a number of additional features
41 * that make lock debugging easier and faster:
42 *
43 * - uses symbolic names of mutexes, whenever they are printed in debug output
44 * - point-of-acquire tracking, symbolic lookup of function names
45 * - list of all locks held in the system, printout of them
46 * - owner tracking
47 * - detects self-recursing locks and prints out all relevant info
48 * - detects multi-task circular deadlocks and prints out all affected
49 * locks and tasks (and only those tasks)
50 */
51struct mutex {
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020052 atomic_long_t owner;
Thomas Gleixnerebf4c552021-08-15 23:28:36 +020053 raw_spinlock_t wait_lock;
Waiman Long2bd2c922013-04-17 15:23:13 -040054#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
Jason Low90631822014-07-14 10:27:49 -070055 struct optimistic_spin_queue osq; /* Spinner MCS lock */
Waiman Long2bd2c922013-04-17 15:23:13 -040056#endif
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020057 struct list_head wait_list;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +010058#ifdef CONFIG_DEBUG_MUTEXES
Ingo Molnar6053ee32006-01-09 15:59:19 -080059 void *magic;
60#endif
Ingo Molnaref5d4702006-07-03 00:24:55 -070061#ifdef CONFIG_DEBUG_LOCK_ALLOC
62 struct lockdep_map dep_map;
63#endif
Ingo Molnar6053ee32006-01-09 15:59:19 -080064};
65
Ingo Molnar6053ee32006-01-09 15:59:19 -080066#ifdef CONFIG_DEBUG_MUTEXES
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020067
68#define __DEBUG_MUTEX_INITIALIZER(lockname) \
69 , .magic = &lockname
70
71extern void mutex_destroy(struct mutex *lock);
72
Ingo Molnar6053ee32006-01-09 15:59:19 -080073#else
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020074
Ingo Molnar6053ee32006-01-09 15:59:19 -080075# define __DEBUG_MUTEX_INITIALIZER(lockname)
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020076
77static inline void mutex_destroy(struct mutex *lock) {}
78
79#endif
80
Randy Dunlapef5dc122010-09-02 15:48:16 -070081/**
82 * mutex_init - initialize the mutex
83 * @mutex: the mutex to be initialized
84 *
85 * Initialize the mutex to unlocked state.
86 *
87 * It is not allowed to initialize an already locked mutex.
88 */
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020089#define mutex_init(mutex) \
90do { \
91 static struct lock_class_key __key; \
92 \
93 __mutex_init((mutex), #mutex, &__key); \
Ingo Molnaref5d4702006-07-03 00:24:55 -070094} while (0)
Ingo Molnar6053ee32006-01-09 15:59:19 -080095
Ingo Molnaref5d4702006-07-03 00:24:55 -070096#ifdef CONFIG_DEBUG_LOCK_ALLOC
Peter Zijlstrade8f5e42020-03-21 12:26:01 +010097# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
98 , .dep_map = { \
99 .name = #lockname, \
100 .wait_type_inner = LD_WAIT_SLEEP, \
101 }
Ingo Molnaref5d4702006-07-03 00:24:55 -0700102#else
103# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
104#endif
105
Ingo Molnar6053ee32006-01-09 15:59:19 -0800106#define __MUTEX_INITIALIZER(lockname) \
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200107 { .owner = ATOMIC_LONG_INIT(0) \
Thomas Gleixnerebf4c552021-08-15 23:28:36 +0200108 , .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
Ingo Molnar6053ee32006-01-09 15:59:19 -0800109 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
Ingo Molnaref5d4702006-07-03 00:24:55 -0700110 __DEBUG_MUTEX_INITIALIZER(lockname) \
111 __DEP_MAP_MUTEX_INITIALIZER(lockname) }
Ingo Molnar6053ee32006-01-09 15:59:19 -0800112
113#define DEFINE_MUTEX(mutexname) \
114 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
115
Ingo Molnaref5d4702006-07-03 00:24:55 -0700116extern void __mutex_init(struct mutex *lock, const char *name,
117 struct lock_class_key *key);
Ingo Molnar6053ee32006-01-09 15:59:19 -0800118
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800119/**
Ingo Molnar6053ee32006-01-09 15:59:19 -0800120 * mutex_is_locked - is the mutex locked
121 * @lock: the mutex to be queried
122 *
Yaowei Baidb076bef2018-02-06 15:41:35 -0800123 * Returns true if the mutex is locked, false if unlocked.
Ingo Molnar6053ee32006-01-09 15:59:19 -0800124 */
Mukesh Ojha5f35d5a2019-07-31 20:35:03 +0530125extern bool mutex_is_locked(struct mutex *lock);
Ingo Molnar6053ee32006-01-09 15:59:19 -0800126
127/*
Peter Zijlstra67a6de42013-11-08 08:26:39 +0100128 * See kernel/locking/mutex.c for detailed documentation of these APIs.
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -0300129 * Also see Documentation/locking/mutex-design.rst.
Ingo Molnar6053ee32006-01-09 15:59:19 -0800130 */
Ingo Molnaref5d4702006-07-03 00:24:55 -0700131#ifdef CONFIG_DEBUG_LOCK_ALLOC
132extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
Peter Zijlstrae4c70a62011-05-24 17:12:03 -0700133extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200134
Andrew Morton18d83622007-05-09 02:33:39 -0700135extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
136 unsigned int subclass);
Liam R. Howlettad776532007-12-06 17:37:59 -0500137extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
138 unsigned int subclass);
Tejun Heo1460cb62016-10-28 12:58:11 -0400139extern void mutex_lock_io_nested(struct mutex *lock, unsigned int subclass);
Peter Zijlstrae4564f72007-10-11 22:11:12 +0200140
141#define mutex_lock(lock) mutex_lock_nested(lock, 0)
142#define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
Liam R. Howlettad776532007-12-06 17:37:59 -0500143#define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
Tejun Heo1460cb62016-10-28 12:58:11 -0400144#define mutex_lock_io(lock) mutex_lock_io_nested(lock, 0)
Peter Zijlstrae4c70a62011-05-24 17:12:03 -0700145
146#define mutex_lock_nest_lock(lock, nest_lock) \
147do { \
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200148 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
Peter Zijlstrae4c70a62011-05-24 17:12:03 -0700149 _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
150} while (0)
151
Ingo Molnaref5d4702006-07-03 00:24:55 -0700152#else
Harvey Harrisonec701582008-02-08 04:19:55 -0800153extern void mutex_lock(struct mutex *lock);
154extern int __must_check mutex_lock_interruptible(struct mutex *lock);
155extern int __must_check mutex_lock_killable(struct mutex *lock);
Tejun Heo1460cb62016-10-28 12:58:11 -0400156extern void mutex_lock_io(struct mutex *lock);
Peter Zijlstrae4564f72007-10-11 22:11:12 +0200157
Ingo Molnaref5d4702006-07-03 00:24:55 -0700158# define mutex_lock_nested(lock, subclass) mutex_lock(lock)
NeilBrownd63a5a72006-12-08 02:36:17 -0800159# define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
Liam R. Howlettad776532007-12-06 17:37:59 -0500160# define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
Peter Zijlstrae4c70a62011-05-24 17:12:03 -0700161# define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
Thomas Gleixner291da9d2021-03-22 09:46:13 +0100162# define mutex_lock_io_nested(lock, subclass) mutex_lock_io(lock)
Ingo Molnaref5d4702006-07-03 00:24:55 -0700163#endif
164
Ingo Molnar6053ee32006-01-09 15:59:19 -0800165/*
166 * NOTE: mutex_trylock() follows the spin_trylock() convention,
167 * not the down_trylock() convention!
Arjan van de Vend98d38f2008-10-29 14:24:09 -0700168 *
169 * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
Ingo Molnar6053ee32006-01-09 15:59:19 -0800170 */
Harvey Harrisonec701582008-02-08 04:19:55 -0800171extern int mutex_trylock(struct mutex *lock);
172extern void mutex_unlock(struct mutex *lock);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200173
Andrew Mortona511e3f2009-04-29 15:59:58 -0700174extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
Eric Parisb1fca262009-03-23 18:22:09 +0100175
Tim Chene7224672014-01-21 15:36:00 -0800176#endif /* __LINUX_MUTEX_H */