blob: ef3c9342e11916266b15ebf001ab2c0943d2a85d [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 *);
40extern int lockref_get_or_lock(struct lockref *);
41extern int lockref_put_or_lock(struct lockref *);
Waiman Long0f8f2aa2013-08-28 18:13:26 -070042
Linus Torvaldse7d33bb2013-09-07 15:49:18 -070043extern void lockref_mark_dead(struct lockref *);
44extern int lockref_get_not_dead(struct lockref *);
45
Steven Whitehousee66cf162013-10-15 15:18:08 +010046/* Must be called under spinlock for reliable results */
47static inline int __lockref_is_dead(const struct lockref *l)
48{
49 return ((int)l->count < 0);
50}
51
Waiman Long0f8f2aa2013-08-28 18:13:26 -070052#endif /* __LINUX_LOCKREF_H */