>From cfec670a6240b84173f3b3719a24df9a4c7424e5 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Mon, 6 Dec 2021 10:54:55 -0700 Subject: [PATCH 1/4] atomic-ref: add basic infrastructure for atomic refs based on atomic_t Make the atomic_t reference counting from commit f958d7b528b1 generic and available for other users. Signed-off-by: Jens Axboe --- include/linux/atomic-ref.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 include/linux/atomic-ref.h diff --git a/include/linux/atomic-ref.h b/include/linux/atomic-ref.h new file mode 100644 index 000000000000..dfba69dd9d37 --- /dev/null +++ b/include/linux/atomic-ref.h @@ -0,0 +1,33 @@ +#ifndef LINUX_ATOMIC_REF_H +#define LINUX_ATOMIC_REF_H + +/* + * Shamelessly stolen from the mm implementation of page reference checking, + * see commit f958d7b528b1 for details. + */ +#define atomic_ref_zero_or_close_to_overflow(ref) \ + ((unsigned int) atomic_read(ref) + 127u <= 127u) + +static inline bool atomic_ref_inc_not_zero(atomic_t *ref) +{ + return atomic_inc_not_zero(ref); +} + +static inline bool atomic_ref_put_and_test(atomic_t *ref) +{ + WARN_ON_ONCE(atomic_ref_zero_or_close_to_overflow(ref)); + return atomic_dec_and_test(ref); +} + +static inline void atomic_ref_put(atomic_t *ref) +{ + WARN_ON_ONCE(atomic_ref_put_and_test(ref)); +} + +static inline void atomic_ref_get(atomic_t *ref) +{ + WARN_ON_ONCE(atomic_ref_zero_or_close_to_overflow(ref)); + atomic_inc(ref); +} + +#endif -- 2.34.1