blob: 2ea18a3def045b4f71ccea1cf428a92d8d50734b [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
Waiman Long364f7842019-04-04 13:43:20 -040023/*
24 * For an uncontended rwsem, count and owner are the only fields a task
25 * needs to touch when acquiring the rwsem. So they are put next to each
26 * other to increase the chance that they will share the same cacheline.
27 *
28 * In a contended rwsem, the owner is likely the most frequently accessed
29 * field in the structure as the optimistic waiter that holds the osq lock
30 * will spin on owner. For an embedded rwsem, other hot fields in the
31 * containing structure should be moved further away from the rwsem to
32 * reduce the chance that they will share the same cacheline causing
33 * cacheline bouncing problem.
34 */
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000035struct rw_semaphore {
Jason Low8ee62b12016-06-03 22:26:02 -070036 atomic_long_t count;
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070037#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070038 /*
39 * Write owner. Used as a speculative check to see
40 * if the owner is running on the cpu.
41 */
42 struct task_struct *owner;
Waiman Long364f7842019-04-04 13:43:20 -040043 struct optimistic_spin_queue osq; /* spinner MCS lock */
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070044#endif
Waiman Long364f7842019-04-04 13:43:20 -040045 raw_spinlock_t wait_lock;
46 struct list_head wait_list;
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000047#ifdef CONFIG_DEBUG_LOCK_ALLOC
48 struct lockdep_map dep_map;
49#endif
50};
51
Waiman Long5a817642018-05-15 17:49:51 -040052/*
Waiman Long925b9cd2018-09-06 16:18:34 -040053 * Setting bit 1 of the owner field but not bit 0 will indicate
Waiman Long5a817642018-05-15 17:49:51 -040054 * that the rwsem is writer-owned with an unknown owner.
55 */
Waiman Long925b9cd2018-09-06 16:18:34 -040056#define RWSEM_OWNER_UNKNOWN ((struct task_struct *)-2L)
Waiman Long5a817642018-05-15 17:49:51 -040057
Thomas Gleixner41e58872011-01-26 20:06:03 +000058/* In all implementations count != 0 means locked */
59static inline int rwsem_is_locked(struct rw_semaphore *sem)
60{
Jason Low8ee62b12016-06-03 22:26:02 -070061 return atomic_long_read(&sem->count) != 0;
Thomas Gleixner41e58872011-01-26 20:06:03 +000062}
63
Waiman Long46ad0842019-03-22 10:30:06 -040064#define RWSEM_UNLOCKED_VALUE 0L
Jason Low8ee62b12016-06-03 22:26:02 -070065#define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Thomas Gleixner12249b32011-01-26 20:06:00 +000067/* Common initializer macros and functions */
68
69#ifdef CONFIG_DEBUG_LOCK_ALLOC
70# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
71#else
72# define __RWSEM_DEP_MAP_INIT(lockname)
73#endif
74
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070075#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Jason Lowce069fc2014-07-14 10:27:52 -070076#define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070077#else
Jason Lowce069fc2014-07-14 10:27:52 -070078#define __RWSEM_OPT_INIT(lockname)
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070079#endif
Thomas Gleixner12249b32011-01-26 20:06:00 +000080
Jason Lowce069fc2014-07-14 10:27:52 -070081#define __RWSEM_INITIALIZER(name) \
Jason Low8ee62b12016-06-03 22:26:02 -070082 { __RWSEM_INIT_COUNT(name), \
Jason Lowce069fc2014-07-14 10:27:52 -070083 .wait_list = LIST_HEAD_INIT((name).wait_list), \
84 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \
85 __RWSEM_OPT_INIT(name) \
86 __RWSEM_DEP_MAP_INIT(name) }
87
Thomas Gleixner12249b32011-01-26 20:06:00 +000088#define DECLARE_RWSEM(name) \
89 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
90
91extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
92 struct lock_class_key *key);
93
94#define init_rwsem(sem) \
95do { \
96 static struct lock_class_key __key; \
97 \
98 __init_rwsem((sem), #sem, &__key); \
99} while (0)
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
Josef Bacik4a444b12013-08-30 10:05:22 -0400102 * This is the same regardless of which rwsem implementation that is being used.
103 * It is just a heuristic meant to be called by somebody alreadying holding the
104 * rwsem to see if somebody from an incompatible type is wanting access to the
105 * lock.
106 */
107static inline int rwsem_is_contended(struct rw_semaphore *sem)
108{
109 return !list_empty(&sem->wait_list);
110}
111
112/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * lock for reading
114 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700115extern void down_read(struct rw_semaphore *sem);
Kirill Tkhai76f85072017-09-29 19:06:38 +0300116extern int __must_check down_read_killable(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/*
119 * trylock for reading -- returns 1 if successful, 0 if contention
120 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700121extern int down_read_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/*
124 * lock for writing
125 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700126extern void down_write(struct rw_semaphore *sem);
Michal Hocko916633a2016-04-07 17:12:31 +0200127extern int __must_check down_write_killable(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/*
130 * trylock for writing -- returns 1 if successful, 0 if contention
131 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700132extern int down_write_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/*
135 * release a read lock
136 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700137extern void up_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139/*
140 * release a write lock
141 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700142extern void up_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144/*
145 * downgrade write lock to read lock
146 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700147extern void downgrade_write(struct rw_semaphore *sem);
148
149#ifdef CONFIG_DEBUG_LOCK_ALLOC
150/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -0700151 * nested locking. NOTE: rwsems are not allowed to recurse
152 * (which occurs if the same task tries to acquire the same
153 * lock instance multiple times), but multiple locks of the
154 * same lock class might be taken, if the order of the locks
155 * is always the same. This ordering rule can be expressed
156 * to lockdep via the _nested() APIs, but enumerating the
157 * subclasses that are used. (If the nesting relationship is
158 * static then another method for expressing nested locking is
159 * the explicit definition of lock class keys and the use of
160 * lockdep_set_class() at lock initialization time.
Davidlohr Bueso214e0ae2014-07-30 13:41:55 -0700161 * See Documentation/locking/lockdep-design.txt for more details.)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700162 */
163extern void down_read_nested(struct rw_semaphore *sem, int subclass);
164extern void down_write_nested(struct rw_semaphore *sem, int subclass);
Al Viro887bddf2016-05-26 00:04:58 -0400165extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
Jiri Kosina1b963c82013-01-11 14:31:56 -0800166extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
167
168# define down_write_nest_lock(sem, nest_lock) \
169do { \
170 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
171 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
172} while (0);
173
Kent Overstreet84759c62011-09-21 21:43:05 -0700174/*
175 * Take/release a lock when not the owner will release it.
176 *
177 * [ This API should be avoided as much as possible - the
178 * proper abstraction for this case is completions. ]
179 */
180extern void down_read_non_owner(struct rw_semaphore *sem);
181extern void up_read_non_owner(struct rw_semaphore *sem);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700182#else
183# define down_read_nested(sem, subclass) down_read(sem)
Jiri Kosinae65b9ad2013-01-15 20:12:37 +0100184# define down_write_nest_lock(sem, nest_lock) down_write(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700185# define down_write_nested(sem, subclass) down_write(sem)
Al Viro887bddf2016-05-26 00:04:58 -0400186# define down_write_killable_nested(sem, subclass) down_write_killable(sem)
Kent Overstreet84759c62011-09-21 21:43:05 -0700187# define down_read_non_owner(sem) down_read(sem)
188# define up_read_non_owner(sem) up_read(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700189#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#endif /* _LINUX_RWSEM_H */