blob: 99f17cc8e16300e89c23a95403b2a0712939a771 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Waiman Long0f8f2aa2013-08-28 18:13:26 -07002#ifndef __LINUX_LOCKREF_H
3#define __LINUX_LOCKREF_H
4
5/*
6 * Locked reference counts.
7 *
8 * These are different from just plain atomic refcounts in that they
9 * are atomic with respect to the spinlock that goes with them. In
10 * particular, there can be implementations that don't actually get
11 * the spinlock for the common decrement/increment operations, but they
12 * still have to check that the operation is done semantically as if
13 * the spinlock had been taken (using a cmpxchg operation that covers
14 * both the lock and the count word, or using memory transactions, for
15 * example).
16 */
17
18#include <linux/spinlock.h>
Peter Zijlstra57f42572013-11-14 14:31:54 -080019#include <generated/bounds.h>
20
21#define USE_CMPXCHG_LOCKREF \
22 (IS_ENABLED(CONFIG_ARCH_USE_CMPXCHG_LOCKREF) && \
Kirill A. Shutemov597d7952013-12-20 13:35:58 +020023 IS_ENABLED(CONFIG_SMP) && SPINLOCK_SIZE <= 4)
Waiman Long0f8f2aa2013-08-28 18:13:26 -070024
25struct lockref {
Linus Torvaldsbc08b442013-09-02 12:12:15 -070026 union {
Peter Zijlstra57f42572013-11-14 14:31:54 -080027#if USE_CMPXCHG_LOCKREF
Linus Torvaldsbc08b442013-09-02 12:12:15 -070028 aligned_u64 lock_count;
29#endif
30 struct {
31 spinlock_t lock;
Linus Torvalds360f5472015-01-09 15:19:03 -080032 int count;
Linus Torvaldsbc08b442013-09-02 12:12:15 -070033 };
34 };
Waiman Long0f8f2aa2013-08-28 18:13:26 -070035};
36
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070037extern void lockref_get(struct lockref *);
Linus Torvalds360f5472015-01-09 15:19:03 -080038extern int lockref_put_return(struct lockref *);
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070039extern int lockref_get_not_zero(struct lockref *);
Andreas Gruenbacher450b1f62018-03-29 08:07:46 +010040extern int lockref_put_not_zero(struct lockref *);
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070041extern int lockref_get_or_lock(struct lockref *);
42extern int lockref_put_or_lock(struct lockref *);
Waiman Long0f8f2aa2013-08-28 18:13:26 -070043
Linus Torvaldse7d33bb2013-09-07 15:49:18 -070044extern void lockref_mark_dead(struct lockref *);
45extern int lockref_get_not_dead(struct lockref *);
46
Steven Whitehousee66cf162013-10-15 15:18:08 +010047/* Must be called under spinlock for reliable results */
Yaowei Bai11209f32018-02-06 15:41:21 -080048static inline bool __lockref_is_dead(const struct lockref *l)
Steven Whitehousee66cf162013-10-15 15:18:08 +010049{
50 return ((int)l->count < 0);
51}
52
Waiman Long0f8f2aa2013-08-28 18:13:26 -070053#endif /* __LINUX_LOCKREF_H */