blob: 6e56006b2cb6b7017c712cc76fe6e2cbeb61e247 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* rwsem.h: R/W semaphores, public interface
3 *
4 * Written by David Howells (dhowells@redhat.com).
5 * Derived from asm-i386/semaphore.h
6 */
7
8#ifndef _LINUX_RWSEM_H
9#define _LINUX_RWSEM_H
10
11#include <linux/linkage.h>
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/types.h>
14#include <linux/kernel.h>
Thomas Gleixnerc16a87c2011-01-26 20:05:50 +000015#include <linux/list.h>
16#include <linux/spinlock.h>
Arun Sharma600634972011-07-26 16:09:06 -070017#include <linux/atomic.h>
Michal Hockod47996082016-04-07 17:12:26 +020018#include <linux/err.h>
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070019#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Jason Low90631822014-07-14 10:27:49 -070020#include <linux/osq_lock.h>
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070021#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23struct rw_semaphore;
24
25#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
26#include <linux/rwsem-spinlock.h> /* use a generic implementation */
Jason Low8ee62b12016-06-03 22:26:02 -070027#define __RWSEM_INIT_COUNT(name) .count = RWSEM_UNLOCKED_VALUE
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#else
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000029/* All arch specific implementations share the same struct */
30struct rw_semaphore {
Jason Low8ee62b12016-06-03 22:26:02 -070031 atomic_long_t count;
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070032 struct list_head wait_list;
Jason Lowce069fc2014-07-14 10:27:52 -070033 raw_spinlock_t wait_lock;
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070034#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Jason Lowce069fc2014-07-14 10:27:52 -070035 struct optimistic_spin_queue osq; /* spinner MCS lock */
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070036 /*
37 * Write owner. Used as a speculative check to see
38 * if the owner is running on the cpu.
39 */
40 struct task_struct *owner;
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070041#endif
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000042#ifdef CONFIG_DEBUG_LOCK_ALLOC
43 struct lockdep_map dep_map;
44#endif
45};
46
Waiman Long5a817642018-05-15 17:49:51 -040047/*
Waiman Long925b9cd2018-09-06 16:18:34 -040048 * Setting bit 1 of the owner field but not bit 0 will indicate
Waiman Long5a817642018-05-15 17:49:51 -040049 * that the rwsem is writer-owned with an unknown owner.
50 */
Waiman Long925b9cd2018-09-06 16:18:34 -040051#define RWSEM_OWNER_UNKNOWN ((struct task_struct *)-2L)
Waiman Long5a817642018-05-15 17:49:51 -040052
Thomas Gleixnerd1233752011-01-26 21:32:01 +010053extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
Kirill Tkhai83ced162017-06-19 21:02:26 +030054extern struct rw_semaphore *rwsem_down_read_failed_killable(struct rw_semaphore *sem);
Thomas Gleixnerd1233752011-01-26 21:32:01 +010055extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
Michal Hockod47996082016-04-07 17:12:26 +020056extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem);
Thomas Gleixnerd1233752011-01-26 21:32:01 +010057extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
58extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
Thomas Gleixneraac72272011-01-26 20:06:06 +000059
Thomas Gleixner41e58872011-01-26 20:06:03 +000060/* In all implementations count != 0 means locked */
61static inline int rwsem_is_locked(struct rw_semaphore *sem)
62{
Jason Low8ee62b12016-06-03 22:26:02 -070063 return atomic_long_read(&sem->count) != 0;
Thomas Gleixner41e58872011-01-26 20:06:03 +000064}
65
Waiman Long46ad0842019-03-22 10:30:06 -040066#define RWSEM_UNLOCKED_VALUE 0L
Jason Low8ee62b12016-06-03 22:26:02 -070067#define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#endif
69
Thomas Gleixner12249b32011-01-26 20:06:00 +000070/* Common initializer macros and functions */
71
72#ifdef CONFIG_DEBUG_LOCK_ALLOC
73# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
74#else
75# define __RWSEM_DEP_MAP_INIT(lockname)
76#endif
77
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070078#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Jason Lowce069fc2014-07-14 10:27:52 -070079#define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070080#else
Jason Lowce069fc2014-07-14 10:27:52 -070081#define __RWSEM_OPT_INIT(lockname)
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070082#endif
Thomas Gleixner12249b32011-01-26 20:06:00 +000083
Jason Lowce069fc2014-07-14 10:27:52 -070084#define __RWSEM_INITIALIZER(name) \
Jason Low8ee62b12016-06-03 22:26:02 -070085 { __RWSEM_INIT_COUNT(name), \
Jason Lowce069fc2014-07-14 10:27:52 -070086 .wait_list = LIST_HEAD_INIT((name).wait_list), \
87 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \
88 __RWSEM_OPT_INIT(name) \
89 __RWSEM_DEP_MAP_INIT(name) }
90
Thomas Gleixner12249b32011-01-26 20:06:00 +000091#define DECLARE_RWSEM(name) \
92 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
93
94extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
95 struct lock_class_key *key);
96
97#define init_rwsem(sem) \
98do { \
99 static struct lock_class_key __key; \
100 \
101 __init_rwsem((sem), #sem, &__key); \
102} while (0)
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
Josef Bacik4a444b12013-08-30 10:05:22 -0400105 * This is the same regardless of which rwsem implementation that is being used.
106 * It is just a heuristic meant to be called by somebody alreadying holding the
107 * rwsem to see if somebody from an incompatible type is wanting access to the
108 * lock.
109 */
110static inline int rwsem_is_contended(struct rw_semaphore *sem)
111{
112 return !list_empty(&sem->wait_list);
113}
114
115/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * lock for reading
117 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700118extern void down_read(struct rw_semaphore *sem);
Kirill Tkhai76f85072017-09-29 19:06:38 +0300119extern int __must_check down_read_killable(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121/*
122 * trylock for reading -- returns 1 if successful, 0 if contention
123 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700124extern int down_read_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/*
127 * lock for writing
128 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700129extern void down_write(struct rw_semaphore *sem);
Michal Hocko916633a2016-04-07 17:12:31 +0200130extern int __must_check down_write_killable(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132/*
133 * trylock for writing -- returns 1 if successful, 0 if contention
134 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700135extern int down_write_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/*
138 * release a read lock
139 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700140extern void up_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142/*
143 * release a write lock
144 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700145extern void up_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/*
148 * downgrade write lock to read lock
149 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700150extern void downgrade_write(struct rw_semaphore *sem);
151
152#ifdef CONFIG_DEBUG_LOCK_ALLOC
153/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -0700154 * nested locking. NOTE: rwsems are not allowed to recurse
155 * (which occurs if the same task tries to acquire the same
156 * lock instance multiple times), but multiple locks of the
157 * same lock class might be taken, if the order of the locks
158 * is always the same. This ordering rule can be expressed
159 * to lockdep via the _nested() APIs, but enumerating the
160 * subclasses that are used. (If the nesting relationship is
161 * static then another method for expressing nested locking is
162 * the explicit definition of lock class keys and the use of
163 * lockdep_set_class() at lock initialization time.
Davidlohr Bueso214e0ae2014-07-30 13:41:55 -0700164 * See Documentation/locking/lockdep-design.txt for more details.)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700165 */
166extern void down_read_nested(struct rw_semaphore *sem, int subclass);
167extern void down_write_nested(struct rw_semaphore *sem, int subclass);
Al Viro887bddf2016-05-26 00:04:58 -0400168extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
Jiri Kosina1b963c82013-01-11 14:31:56 -0800169extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
170
171# define down_write_nest_lock(sem, nest_lock) \
172do { \
173 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
174 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
175} while (0);
176
Kent Overstreet84759c62011-09-21 21:43:05 -0700177/*
178 * Take/release a lock when not the owner will release it.
179 *
180 * [ This API should be avoided as much as possible - the
181 * proper abstraction for this case is completions. ]
182 */
183extern void down_read_non_owner(struct rw_semaphore *sem);
184extern void up_read_non_owner(struct rw_semaphore *sem);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700185#else
186# define down_read_nested(sem, subclass) down_read(sem)
Jiri Kosinae65b9ad2013-01-15 20:12:37 +0100187# define down_write_nest_lock(sem, nest_lock) down_write(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700188# define down_write_nested(sem, subclass) down_write(sem)
Al Viro887bddf2016-05-26 00:04:58 -0400189# define down_write_killable_nested(sem, subclass) down_write_killable(sem)
Kent Overstreet84759c62011-09-21 21:43:05 -0700190# define down_read_non_owner(sem) down_read(sem)
191# define up_read_non_owner(sem) up_read(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#endif /* _LINUX_RWSEM_H */