All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: will@kernel.org, boqun.feng@gmail.com
Cc: linux-kernel@vger.kernel.org, x86@kernel.org,
	peterz@infradead.org, mark.rutland@arm.com, elver@google.com,
	keescook@chromium.org, hch@infradead.org,
	torvalds@linux-foundation.org, axboe@kernel.dk
Subject: [PATCH v2 7/9] refcount: Prepare for atomic_*_overflow() offsets
Date: Fri, 10 Dec 2021 17:16:25 +0100	[thread overview]
Message-ID: <20211210162313.774144401@infradead.org> (raw)
In-Reply-To: 20211210161618.645249719@infradead.org

In order to prepare for atomic_*_overflow() not strictly being in the
[0,INT_MAX] range, add ATOMIC_OVERFLOW_OFFSET to correct for the 0
point.

This makes the uncommon refcount ops a little awkward, but allows for
faster common ops.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 include/linux/atomic.h   |   16 ++++++++++++++++
 include/linux/refcount.h |   16 ++++++++++++----
 lib/refcount.c           |   10 +++++-----
 3 files changed, 33 insertions(+), 9 deletions(-)

--- a/include/linux/atomic.h
+++ b/include/linux/atomic.h
@@ -7,6 +7,14 @@
 #include <asm/atomic.h>
 #include <asm/barrier.h>
 
+#ifndef ATOMIC_OVERFLOW_OFFSET
+#define ATOMIC_OVERFLOW_OFFSET 0
+#endif
+
+#ifndef ATOMIC64_OVERFLOW_OFFSET
+#define ATOMIC64_OVERFLOW_OFFSET 0
+#endif
+
 /*
  * Relaxed variants of xchg, cmpxchg and some atomic operations.
  *
@@ -81,4 +89,12 @@
 #include <linux/atomic/atomic-long.h>
 #include <linux/atomic/atomic-instrumented.h>
 
+#ifndef ATOMIC_LONG_OVERFLOW_OFFSET
+#ifdef CONFIG_64BIT
+#define ATOMIC_LONG_OVERFLOW_OFFSET	ATOMIC64_OVERFLOW_OFFSET
+#else
+#define ATOMIC_LONG_OVERFLOW_OFFSET	ATOMIC_OVERFLOW_OFFSET
+#endif
+#endif
+
 #endif /* _LINUX_ATOMIC_H */
--- a/include/linux/refcount.h
+++ b/include/linux/refcount.h
@@ -133,7 +133,7 @@ void refcount_warn_saturate(refcount_t *
  */
 static inline void refcount_set(refcount_t *r, int n)
 {
-	atomic_set(&r->refs, n);
+	atomic_set(&r->refs, n - ATOMIC_OVERFLOW_OFFSET);
 }
 
 /**
@@ -144,18 +144,20 @@ static inline void refcount_set(refcount
  */
 static inline unsigned int refcount_read(const refcount_t *r)
 {
-	return atomic_read(&r->refs);
+	return atomic_read(&r->refs) + ATOMIC_OVERFLOW_OFFSET;
 }
 
 static inline __must_check bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp)
 {
-	int old = refcount_read(r);
+	int old = atomic_read(&r->refs);
 
 	do {
-		if (!old)
+		if (old == -ATOMIC_OVERFLOW_OFFSET)
 			break;
 	} while (!atomic_try_cmpxchg_relaxed(&r->refs, &old, old + i));
 
+	old += ATOMIC_OVERFLOW_OFFSET;
+
 	if (oldp)
 		*oldp = old;
 
@@ -192,6 +194,8 @@ static inline void __refcount_add(int i,
 {
 	int old = atomic_fetch_add_relaxed(i, &r->refs);
 
+	old += ATOMIC_OVERFLOW_OFFSET;
+
 	if (oldp)
 		*oldp = old;
 
@@ -274,6 +278,8 @@ static inline __must_check bool __refcou
 {
 	int old = atomic_fetch_sub_release(i, &r->refs);
 
+	old += ATOMIC_OVERFLOW_OFFSET;
+
 	if (oldp)
 		*oldp = old;
 
@@ -343,6 +349,8 @@ static inline void __refcount_dec(refcou
 {
 	int old = atomic_fetch_sub_release(1, &r->refs);
 
+	old += ATOMIC_OVERFLOW_OFFSET;
+
 	if (oldp)
 		*oldp = old;
 
--- a/lib/refcount.c
+++ b/lib/refcount.c
@@ -13,7 +13,7 @@
 void refcount_warn_saturate(refcount_t *r, enum refcount_saturation_type t)
 {
 	int old = refcount_read(r);
-	refcount_set(r, REFCOUNT_SATURATED);
+	atomic_set(&r->refs, REFCOUNT_SATURATED);
 
 	/* racy; who cares */
 	if (old == 1 && t == REFCOUNT_ADD_OVF)
@@ -59,9 +59,9 @@ EXPORT_SYMBOL(refcount_warn_saturate);
  */
 bool refcount_dec_if_one(refcount_t *r)
 {
-	int val = 1;
+	int val = 1 - ATOMIC_OVERFLOW_OFFSET;
 
-	return atomic_try_cmpxchg_release(&r->refs, &val, 0);
+	return atomic_try_cmpxchg_release(&r->refs, &val, 0 - ATOMIC_OVERFLOW_OFFSET);
 }
 EXPORT_SYMBOL(refcount_dec_if_one);
 
@@ -85,11 +85,11 @@ bool refcount_dec_not_one(refcount_t *r)
 		if (unlikely(val - (REFCOUNT_SATURATED + REFCOUNT_SATURATED/2) < -REFCOUNT_SATURATED))
 			return true;
 
-		if (val == 1)
+		if (val == 1 - ATOMIC_OVERFLOW_OFFSET)
 			return false;
 
 		new = val - 1;
-		if (new > val) {
+		if (new + ATOMIC_OVERFLOW_OFFSET > val + ATOMIC_OVERFLOW_OFFSET) {
 			refcount_warn_saturate(r, REFCOUNT_SUB_UAF);
 			return true;
 		}



  parent reply	other threads:[~2021-12-10 16:27 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-10 16:16 [PATCH v2 0/9] refcount: Improve code-gen Peter Zijlstra
2021-12-10 16:16 ` [PATCH v2 1/9] atomic: Prepare scripts for macro ops Peter Zijlstra
2021-12-10 17:27   ` Mark Rutland
2021-12-10 17:43   ` Marco Elver
2021-12-10 16:16 ` [PATCH v2 2/9] atomic: Add xchg.tbl Peter Zijlstra
2021-12-13  9:50   ` Mark Rutland
2021-12-10 16:16 ` [PATCH v2 3/9] atomic: Introduce atomic_{inc,dec,dec_and_test}_overflow() Peter Zijlstra
2021-12-13 10:06   ` Mark Rutland
2021-12-13 10:57     ` Peter Zijlstra
2021-12-13 10:59     ` Peter Zijlstra
2021-12-13 11:09       ` Mark Rutland
2021-12-10 16:16 ` [PATCH v2 4/9] refcount: Use atomic_*_overflow() Peter Zijlstra
2021-12-13 10:35   ` Mark Rutland
2021-12-10 16:16 ` [PATCH v2 5/9] atomic,x86: Implement atomic_dec_and_test_overflow() Peter Zijlstra
2021-12-13 11:04   ` Mark Rutland
2021-12-10 16:16 ` [PATCH v2 6/9] refcount: Fix refcount_dec_not_one() Peter Zijlstra
2021-12-10 16:16 ` Peter Zijlstra [this message]
2021-12-10 16:16 ` [PATCH v2 8/9] atomic,x86: Alternative atomic_*_overflow() scheme Peter Zijlstra
2021-12-10 16:53   ` Linus Torvalds
2021-12-10 17:27     ` Linus Torvalds
2021-12-17  3:38     ` Herbert Xu
2021-12-13 16:43   ` Peter Zijlstra
2021-12-13 17:29     ` Marco Elver
2021-12-13 18:11     ` Linus Torvalds
2021-12-13 18:18       ` Marco Elver
2021-12-13 18:24         ` Linus Torvalds
2021-12-13 19:35           ` Marco Elver
2021-12-13 18:21       ` Linus Torvalds
2021-12-10 16:16 ` [PATCH v2 9/9] refcount: Optimize __refcount_add_not_zero(.i=1) Peter Zijlstra
2021-12-10 19:37 ` [PATCH v2 0/9] refcount: Improve code-gen Peter Zijlstra
2021-12-13 12:15 ` [PATCH v2 10/9] atomic: Document the atomic_{}_overflow() functions Peter Zijlstra
2021-12-13 12:20 ` [PATCH v2 0/9] refcount: Improve code-gen Peter Zijlstra
2021-12-13 14:42   ` Marco Elver
2021-12-13 16:11     ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211210162313.774144401@infradead.org \
    --to=peterz@infradead.org \
    --cc=axboe@kernel.dk \
    --cc=boqun.feng@gmail.com \
    --cc=elver@google.com \
    --cc=hch@infradead.org \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=torvalds@linux-foundation.org \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.