From: Chandan Babu R <chandan.babu@oracle.com>
To: linux-xfs@vger.kernel.org
Cc: Dave Chinner <dchinner@redhat.com>,
david@fromorbit.com, sandeen@sandeen.net, djwong@kernel.org,
Chandan Babu R <chandan.babu@oracle.com>
Subject: [PATCH V2 3/5] atomic: convert to uatomic
Date: Fri, 24 Sep 2021 19:39:10 +0530 [thread overview]
Message-ID: <20210924140912.201481-4-chandan.babu@oracle.com> (raw)
In-Reply-To: <20210924140912.201481-1-chandan.babu@oracle.com>
From: Dave Chinner <dchinner@redhat.com>
Now we have liburcu, we can make use of it's atomic variable
implementation. It is almost identical to the kernel API - it's just
got a "uatomic" prefix. liburcu also provides all the same aomtic
variable memory barriers as the kernel, so if we pull memory barrier
dependent kernel code across, it will just work with the right
barrier wrappers.
This is preparation the addition of more extensive atomic operations
the that kernel buffer cache requires to function correctly.
Signed-off-by: Dave Chinner <dchinner@redhat.com>
[chandan.babu@oracle.com: Swap order of arguments provided to atomic[64]_[add|sub]()]
Signed-off-by: Chandan Babu R <chandan.babu@oracle.com>
---
include/atomic.h | 65 ++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 54 insertions(+), 11 deletions(-)
diff --git a/include/atomic.h b/include/atomic.h
index e0e1ba84..99cb85d3 100644
--- a/include/atomic.h
+++ b/include/atomic.h
@@ -7,21 +7,64 @@
#define __ATOMIC_H__
/*
- * Warning: These are not really atomic at all. They are wrappers around the
- * kernel atomic variable interface. If we do need these variables to be atomic
- * (due to multithreading of the code that uses them) we need to add some
- * pthreads magic here.
+ * Atomics are provided by liburcu.
+ *
+ * API and guidelines for which operations provide memory barriers is here:
+ *
+ * https://github.com/urcu/userspace-rcu/blob/master/doc/uatomic-api.md
+ *
+ * Unlike the kernel, the same interface supports 32 and 64 bit atomic integers.
*/
+#include <urcu/uatomic.h>
+#include "spinlock.h"
+
typedef int32_t atomic_t;
typedef int64_t atomic64_t;
-#define atomic_inc_return(x) (++(*(x)))
-#define atomic_dec_return(x) (--(*(x)))
+#define atomic_read(a) uatomic_read(a)
+#define atomic_set(a, v) uatomic_set(a, v)
+#define atomic_add(v, a) uatomic_add(a, v)
+#define atomic_sub(v, a) uatomic_sub(a, v)
+#define atomic_inc(a) uatomic_inc(a)
+#define atomic_dec(a) uatomic_dec(a)
+#define atomic_inc_return(a) uatomic_add_return(a, 1)
+#define atomic_dec_return(a) uatomic_sub_return(a, 1)
+#define atomic_dec_and_test(a) (atomic_dec_return(a) == 0)
+#define cmpxchg(a, o, n) uatomic_cmpxchg(a, o, n);
+
+static inline bool atomic_add_unless(atomic_t *a, int v, int u)
+{
+ int r = atomic_read(a);
+ int n, o;
+
+ do {
+ o = r;
+ if (o == u)
+ break;
+ n = o + v;
+ r = uatomic_cmpxchg(a, o, n);
+ } while (r != o);
+
+ return o != u;
+}
+
+static inline bool atomic_dec_and_lock(atomic_t *a, spinlock_t *lock)
+{
+ if (atomic_add_unless(a, -1, 1))
+ return 0;
+
+ spin_lock(lock);
+ if (atomic_dec_and_test(a))
+ return 1;
+ spin_unlock(lock);
+ return 0;
+}
-#define atomic64_read(x) *(x)
-#define atomic64_set(x, v) (*(x) = v)
-#define atomic64_add(v, x) (*(x) += v)
-#define atomic64_inc(x) ((*(x))++)
-#define atomic64_dec(x) ((*(x))--)
+#define atomic64_read(x) uatomic_read(x)
+#define atomic64_set(x, v) uatomic_set(x, v)
+#define atomic64_add(v, a) uatomic_add(a, v)
+#define atomic64_sub(v, a) uatomic_sub(a, v)
+#define atomic64_inc(a) uatomic_inc(a)
+#define atomic64_dec(a) uatomic_dec(a)
#endif /* __ATOMIC_H__ */
--
2.30.2
next prev parent reply other threads:[~2021-09-24 14:10 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-24 14:09 [PATCH V2 0/5] xfsprogs: generic serialisation primitives Chandan Babu R
2021-09-24 14:09 ` [PATCH V2 1/5] xfsprogs: introduce liburcu support Chandan Babu R
2021-09-24 21:51 ` Eric Sandeen
2021-09-25 10:24 ` Chandan Babu R
2021-09-25 23:05 ` Dave Chinner
2021-09-27 18:48 ` Eric Sandeen
2021-09-29 20:46 ` Eric Sandeen
2021-09-24 14:09 ` [PATCH V2 2/5] libxfs: add spinlock_t wrapper Chandan Babu R
2021-09-24 22:06 ` Eric Sandeen
2021-09-24 14:09 ` Chandan Babu R [this message]
2021-09-24 22:13 ` [PATCH V2 3/5] atomic: convert to uatomic Eric Sandeen
2021-09-25 10:26 ` Chandan Babu R
2021-09-25 23:15 ` Dave Chinner
2021-09-25 23:18 ` Eric Sandeen
2021-09-25 23:49 ` Dave Chinner
2021-09-24 14:09 ` [PATCH V2 4/5] libxfs: add kernel-compatible completion API Chandan Babu R
2021-09-24 23:02 ` Eric Sandeen
2021-09-25 10:29 ` Chandan Babu R
2021-09-27 20:33 ` Darrick J. Wong
2021-09-27 20:55 ` Dave Chinner
2021-09-24 14:09 ` [PATCH V2 5/5] libxfs: add wrappers for kernel semaphores Chandan Babu R
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=20210924140912.201481-4-chandan.babu@oracle.com \
--to=chandan.babu@oracle.com \
--cc=david@fromorbit.com \
--cc=dchinner@redhat.com \
--cc=djwong@kernel.org \
--cc=linux-xfs@vger.kernel.org \
--cc=sandeen@sandeen.net \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox