mirror of
https://github.com/linux-msm/laptops-kernel.git
synced 2026-08-13 14:19:53 -07:00
1. put the dead val into a macro so that it can be used in other places 2. __lockref_is_dead(): - drop the __ suffix, this is not an internal routine - drop the spurious cast, the value is already a signed int - use READ_ONCE to prevent any compile shenanigans 3. provide lockref_is_dead_or_zero() Signed-off-by: Mateusz Guzik <mjguzik@gmail.com> Link: https://patch.msgid.link/20260724171422.429284-2-mjguzik@gmail.com Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
72 lines
1.9 KiB
C
72 lines
1.9 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
#ifndef __LINUX_LOCKREF_H
|
|
#define __LINUX_LOCKREF_H
|
|
|
|
/*
|
|
* Locked reference counts.
|
|
*
|
|
* These are different from just plain atomic refcounts in that they
|
|
* are atomic with respect to the spinlock that goes with them. In
|
|
* particular, there can be implementations that don't actually get
|
|
* the spinlock for the common decrement/increment operations, but they
|
|
* still have to check that the operation is done semantically as if
|
|
* the spinlock had been taken (using a cmpxchg operation that covers
|
|
* both the lock and the count word, or using memory transactions, for
|
|
* example).
|
|
*/
|
|
|
|
#include <linux/spinlock.h>
|
|
#include <generated/bounds.h>
|
|
|
|
#define USE_CMPXCHG_LOCKREF \
|
|
(IS_ENABLED(CONFIG_ARCH_USE_CMPXCHG_LOCKREF) && \
|
|
IS_ENABLED(CONFIG_SMP) && SPINLOCK_SIZE <= 4)
|
|
|
|
struct lockref {
|
|
union {
|
|
#if USE_CMPXCHG_LOCKREF
|
|
aligned_u64 lock_count;
|
|
#endif
|
|
struct {
|
|
spinlock_t lock;
|
|
int count;
|
|
};
|
|
};
|
|
};
|
|
|
|
#define __LOCKREF_DEAD_VAL -128
|
|
|
|
/**
|
|
* lockref_init - Initialize a lockref
|
|
* @lockref: pointer to lockref structure
|
|
*
|
|
* Initializes @lockref->count to 1.
|
|
*/
|
|
static inline void lockref_init(struct lockref *lockref)
|
|
{
|
|
spin_lock_init(&lockref->lock);
|
|
lockref->count = 1;
|
|
}
|
|
|
|
void lockref_get(struct lockref *lockref);
|
|
int lockref_put_return(struct lockref *lockref);
|
|
bool lockref_get_not_zero(struct lockref *lockref);
|
|
bool lockref_put_or_lock(struct lockref *lockref) __cond_acquires(false, &lockref->lock);
|
|
|
|
void lockref_mark_dead(struct lockref *lockref);
|
|
bool lockref_get_not_dead(struct lockref *lockref);
|
|
|
|
/* Must be called under spinlock for reliable results */
|
|
static inline bool lockref_is_dead(const struct lockref *l)
|
|
{
|
|
return (READ_ONCE(l->count) == __LOCKREF_DEAD_VAL);
|
|
}
|
|
|
|
static inline bool lockref_is_dead_or_zero(const struct lockref *l)
|
|
{
|
|
int count = READ_ONCE(l->count);
|
|
return (count == __LOCKREF_DEAD_VAL || count == 0);
|
|
}
|
|
|
|
#endif /* __LINUX_LOCKREF_H */
|