[PATCH 2/4] io_uring: convert to using atomic-ref

From: Jens Axboe
Date: Mon Dec 06 2021 - 13:11:00 EST


Signed-off-by: Jens Axboe <axboe@xxxxxxxxx>
---
fs/io_uring.c | 18 +++++-------------
1 file changed, 5 insertions(+), 13 deletions(-)

diff --git a/fs/io_uring.c b/fs/io_uring.c
index 59fd8b785262..2ce076fd85dc 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -81,6 +81,7 @@
#include <linux/tracehook.h>
#include <linux/audit.h>
#include <linux/security.h>
+#include <linux/atomic-ref.h>

#define CREATE_TRACE_POINTS
#include <trace/events/io_uring.h>
@@ -1170,17 +1171,10 @@ static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
#define io_for_each_link(pos, head) \
for (pos = (head); pos; pos = pos->link)

-/*
- * Shamelessly stolen from the mm implementation of page reference checking,
- * see commit f958d7b528b1 for details.
- */
-#define req_ref_zero_or_close_to_overflow(req) \
- ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
-
static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
{
WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
- return atomic_inc_not_zero(&req->refs);
+ return atomic_ref_inc_not_zero(&req->refs);
}

static inline bool req_ref_put_and_test(struct io_kiocb *req)
@@ -1188,21 +1182,19 @@ static inline bool req_ref_put_and_test(struct io_kiocb *req)
if (likely(!(req->flags & REQ_F_REFCOUNT)))
return true;

- WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
- return atomic_dec_and_test(&req->refs);
+ return atomic_ref_put_and_test(&req->refs);
}

static inline void req_ref_put(struct io_kiocb *req)
{
WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
- WARN_ON_ONCE(req_ref_put_and_test(req));
+ atomic_ref_put(&req->refs);
}

static inline void req_ref_get(struct io_kiocb *req)
{
WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
- WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
- atomic_inc(&req->refs);
+ atomic_ref_get(&req->refs);
}

static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
--
2.34.1


--------------5E8153C5F8A865ED65BA2BE8
Content-Type: text/x-patch; charset=UTF-8;
name="0001-atomic-ref-add-basic-infrastructure-for-atomic-refs-.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename*0="0001-atomic-ref-add-basic-infrastructure-for-atomic-refs-.pa";
filename*1="tch"