Skip to contentSkip to conversation

Cleanup alloc cache init_once handling

PATCH Superseded Jens Axboe 2 participants 13 messages Jan 23, 2025 io_uring

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

cover Cleanup alloc cache init_once handling

Hi,

A minor prep patch cleaning up some confusion on types for uring_cmd, which don't matter now, but will after patch 2. Patch 2 gets rid of the init_once, and has the cache init functions pass in the number of bytes to clear for a fresh allocation.

 include/linux/io_uring/cmd.h   |  3 ++-
 include/linux/io_uring_types.h |  3 ++-
 io_uring/alloc_cache.h         | 30 +++++++++++++++++++++---------
 io_uring/futex.c               |  4 ++--
 io_uring/io_uring.c            | 13 ++++++++-----
 io_uring/io_uring.h            |  5 ++---
 io_uring/net.c                 | 11 +----------
 io_uring/net.h                 |  7 +++++--
 io_uring/poll.c                |  2 +-
 io_uring/rw.c                  | 10 +---------
 io_uring/rw.h                  |  5 ++++-
 io_uring/uring_cmd.c           | 16 ++++------------
 12 files changed, 53 insertions(+), 56 deletions(-)
Signature · 1 line
Jens Axboe
author maintainer
#1 lore raw

PATCH 1/2 io_uring/uring_cmd: cleanup struct io_uring_cmd_data layout

A few spots in uring_cmd assume that the SQEs copied are always at the start of the structure, and hence mix req->async_data and the struct itself.

Clean that up and use the proper indices.

---
 io_uring/uring_cmd.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
io_uring/uring_cmd.c +3 -3
index 3993c9339ac7..6a63ec4b5445 100644
@@ -192,8 +192,8 @@ static int io_uring_cmd_prep_setup(struct io_kiocb *req,
 		return 0;
 	}
 
-	memcpy(req->async_data, sqe, uring_sqe_size(req->ctx));
-	ioucmd->sqe = req->async_data;
+	memcpy(cache->sqes, sqe, uring_sqe_size(req->ctx));
+	ioucmd->sqe = cache->sqes;
 	return 0;
 }
 
@@ -260,7 +260,7 @@ int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
 		struct io_uring_cmd_data *cache = req->async_data;
 
 		if (ioucmd->sqe != (void *) cache)
-			memcpy(cache, ioucmd->sqe, uring_sqe_size(req->ctx));
+			memcpy(cache->sqes, ioucmd->sqe, uring_sqe_size(req->ctx));
 		return -EAGAIN;
 	} else if (ret == -EIOCBQUEUED) {
 		return -EIOCBQUEUED;
Signature · 1 line
2.47.2
#2 lore raw

Re: [PATCH 1/2] io_uring/uring_cmd: cleanup struct io_uring_cmd_data layout

Quoted 27 lines · Jens Axboe

A few spots in uring_cmd assume that the SQEs copied are always at the start of the structure, and hence mix req->async_data and the struct itself.

Clean that up and use the proper indices.

---
  io_uring/uring_cmd.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
io_uring/uring_cmd.c +2 -2
index 3993c9339ac7..6a63ec4b5445 100644
@@ -192,8 +192,8 @@ static int io_uring_cmd_prep_setup(struct io_kiocb *req,
  		return 0;
  	}
 
-	memcpy(req->async_data, sqe, uring_sqe_size(req->ctx));
-	ioucmd->sqe = req->async_data;
+	memcpy(cache->sqes, sqe, uring_sqe_size(req->ctx));
+	ioucmd->sqe = cache->sqes;
  	return 0;
  }
 

io_uring/uring_cmd.c int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags) · line 263

Quoted 5 lines
io_uring/uring_cmd.c +1 -1
@@ -260,7 +260,7 @@ int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
  		struct io_uring_cmd_data *cache = req->async_data;
 
  		if (ioucmd->sqe != (void *) cache)
-			memcpy(cache, ioucmd->sqe, uring_sqe_size(req->ctx));
+			memcpy(cache->sqes, ioucmd->sqe, uring_sqe_size(req->ctx));

3347fa658a1b ("io_uring/cmd: add per-op data to struct io_uring_cmd_data")

IIUC the patch above is queued for 6.14, and with that this patch looks like a fix? At least it feels pretty dangerous without.

Signature · 1 line
Pavel Begunkov
author maintainer
#3 lore raw

Re: [PATCH 1/2] io_uring/uring_cmd: cleanup struct io_uring_cmd_data layout

Quoted 33 lines · Pavel Begunkov
Quoted 19 lines · Jens Axboe

A few spots in uring_cmd assume that the SQEs copied are always at the start of the structure, and hence mix req->async_data and the struct itself.

Clean that up and use the proper indices.

---
  io_uring/uring_cmd.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
io_uring/uring_cmd.c +0 -0
index 3993c9339ac7..6a63ec4b5445 100644
@@ -192,8 +192,8 @@ static int io_uring_cmd_prep_setup(struct io_kiocb *req,
          return 0;
      }
  -    memcpy(req->async_data, sqe, uring_sqe_size(req->ctx));

io_uring/uring_cmd.c static int io_uring_cmd_prep_setup(struct io_kiocb *req,

Quoted 10 lines
io_uring/uring_cmd.c +3 -2
-    ioucmd->sqe = req->async_data;
+    memcpy(cache->sqes, sqe, uring_sqe_size(req->ctx));
+    ioucmd->sqe = cache->sqes;
      return 0;
  }
  @@ -260,7 +260,7 @@ int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
          struct io_uring_cmd_data *cache = req->async_data;
            if (ioucmd->sqe != (void *) cache)
-            memcpy(cache, ioucmd->sqe, uring_sqe_size(req->ctx));
+            memcpy(cache->sqes, ioucmd->sqe, uring_sqe_size(req->ctx));

3347fa658a1b ("io_uring/cmd: add per-op data to struct io_uring_cmd_data")

Quoted 2 lines

IIUC the patch above is queued for 6.14, and with that this patch looks like a fix? At least it feels pretty dangerous without.

It's not a fix, the sqes are first in the struct even with that patch. So I'd consider it a cleanup. In any case, targeting 6.14 for these alloc cache cleanups as it got introduced there as well.

Signature · 1 line
Jens Axboe
#4 lore raw

Re: [PATCH 1/2] io_uring/uring_cmd: cleanup struct io_uring_cmd_data layout

Quoted 34 lines · Jens Axboe
Quoted 33 lines · Pavel Begunkov
Quoted 19 lines · Jens Axboe

A few spots in uring_cmd assume that the SQEs copied are always at the start of the structure, and hence mix req->async_data and the struct itself.

Clean that up and use the proper indices.

---
   io_uring/uring_cmd.c | 6 +++---
   1 file changed, 3 insertions(+), 3 deletions(-)
io_uring/uring_cmd.c +0 -0
index 3993c9339ac7..6a63ec4b5445 100644
@@ -192,8 +192,8 @@ static int io_uring_cmd_prep_setup(struct io_kiocb *req,
           return 0;
       }
   -    memcpy(req->async_data, sqe, uring_sqe_size(req->ctx));

io_uring/uring_cmd.c static int io_uring_cmd_prep_setup(struct io_kiocb *req,

Quoted 10 lines
io_uring/uring_cmd.c +3 -2
-    ioucmd->sqe = req->async_data;
+    memcpy(cache->sqes, sqe, uring_sqe_size(req->ctx));
+    ioucmd->sqe = cache->sqes;
       return 0;
   }
   @@ -260,7 +260,7 @@ int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
           struct io_uring_cmd_data *cache = req->async_data;
             if (ioucmd->sqe != (void *) cache)
-            memcpy(cache, ioucmd->sqe, uring_sqe_size(req->ctx));
+            memcpy(cache->sqes, ioucmd->sqe, uring_sqe_size(req->ctx));

3347fa658a1b ("io_uring/cmd: add per-op data to struct io_uring_cmd_data")

Quoted 4 lines
Quoted 2 lines

IIUC the patch above is queued for 6.14, and with that this patch looks like a fix? At least it feels pretty dangerous without.

It's not a fix, the sqes are first in the struct even with that patch.

Ah yes

Quoted 2 lines

So I'd consider it a cleanup. In any case, targeting 6.14 for these alloc cache cleanups as it got introduced there as well.

That's good, makes it not that brittle

Signature · 1 line
Pavel Begunkov
author maintainer
#5 lore raw

Re: [PATCH 1/2] io_uring/uring_cmd: cleanup struct io_uring_cmd_data layout

Quoted 38 lines · Pavel Begunkov
Quoted 37 lines · Jens Axboe
Quoted 35 lines · Pavel Begunkov
Quoted 19 lines · Jens Axboe

A few spots in uring_cmd assume that the SQEs copied are always at the start of the structure, and hence mix req->async_data and the struct itself.

Clean that up and use the proper indices.

---
   io_uring/uring_cmd.c | 6 +++---
   1 file changed, 3 insertions(+), 3 deletions(-)
io_uring/uring_cmd.c +0 -0
index 3993c9339ac7..6a63ec4b5445 100644
@@ -192,8 +192,8 @@ static int io_uring_cmd_prep_setup(struct io_kiocb *req,
           return 0;
       }
   -    memcpy(req->async_data, sqe, uring_sqe_size(req->ctx));

io_uring/uring_cmd.c static int io_uring_cmd_prep_setup(struct io_kiocb *req,

Quoted 10 lines
io_uring/uring_cmd.c +3 -2
-    ioucmd->sqe = req->async_data;
+    memcpy(cache->sqes, sqe, uring_sqe_size(req->ctx));
+    ioucmd->sqe = cache->sqes;
       return 0;
   }
   @@ -260,7 +260,7 @@ int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
           struct io_uring_cmd_data *cache = req->async_data;
             if (ioucmd->sqe != (void *) cache)
-            memcpy(cache, ioucmd->sqe, uring_sqe_size(req->ctx));
+            memcpy(cache->sqes, ioucmd->sqe, uring_sqe_size(req->ctx));

3347fa658a1b ("io_uring/cmd: add per-op data to struct io_uring_cmd_data")

IIUC the patch above is queued for 6.14, and with that this patch looks like a fix? At least it feels pretty dangerous without.

Quoted 8 lines
Quoted 1 line

It's not a fix, the sqes are first in the struct even with that patch.

Ah yes

Quoted 2 lines

So I'd consider it a cleanup. In any case, targeting 6.14 for these alloc cache cleanups as it got introduced there as well.

That's good, makes it not that brittle

Yep it was too easy to miss, don't like them being aliased like that even if the usage was currently fine.

Signature · 1 line
Jens Axboe
author maintainer
#6 lore raw

PATCH 2/2 io_uring: get rid of alloc cache init_once handling

In reply to Jens Axboe

init_once is called when an object doesn't come from the cache, and hence needs initial clearing of certain members. While the whole struct could get cleared by memset() in that case, a few of the cache members are large enough that this may cause unnecessary overhead if the caches used aren't large enough to satisfy the workload. For those cases, some churn of kmalloc+kfree is to be expected.

Ensure that the 3 users that need clearing put the members they need cleared at the start of the struct, and place an empty placeholder 'init' member so that the cache initialization knows how much to clear.

---
 include/linux/io_uring/cmd.h   |  3 ++-
 include/linux/io_uring_types.h |  3 ++-
 io_uring/alloc_cache.h         | 30 +++++++++++++++++++++---------
 io_uring/futex.c               |  4 ++--
 io_uring/io_uring.c            | 13 ++++++++-----
 io_uring/io_uring.h            |  5 ++---
 io_uring/net.c                 | 11 +----------
 io_uring/net.h                 |  7 +++++--
 io_uring/poll.c                |  2 +-
 io_uring/rw.c                  | 10 +---------
 io_uring/rw.h                  |  5 ++++-
 io_uring/uring_cmd.c           | 10 +---------
 12 files changed, 50 insertions(+), 53 deletions(-)
include/linux/io_uring/cmd.h +2 -1
index a3ce553413de..8d7746d9fd23 100644
@@ -19,8 +19,9 @@ struct io_uring_cmd {
 };
 
 struct io_uring_cmd_data {
-	struct io_uring_sqe	sqes[2];
 	void			*op_data;
+	int			init[0];
+	struct io_uring_sqe	sqes[2];
 };
 
 static inline const void *io_uring_sqe_cmd(const struct io_uring_sqe *sqe)
include/linux/io_uring_types.h +2 -1
index 623d8e798a11..3def525a1da3 100644
@@ -222,7 +222,8 @@ struct io_alloc_cache {
 	void			**entries;
 	unsigned int		nr_cached;
 	unsigned int		max_cached;
-	size_t			elem_size;
+	unsigned int		elem_size;
+	unsigned int		init_clear;
 };
 
 struct io_ring_ctx {
io_uring/alloc_cache.h +21 -9
index a3a8cfec32ce..f57d5e13cbbb 100644
@@ -23,35 +23,47 @@ static inline void *io_alloc_cache_get(struct io_alloc_cache *cache)
 	if (cache->nr_cached) {
 		void *entry = cache->entries[--cache->nr_cached];
 
+		/*
+		 * If KASAN is enabled, always clear the initial bytes that
+		 * must be zeroed post alloc, in case any of them overlap
+		 * with KASAN storage.
+		 */
+#if defined(CONFIG_KASAN)
 		kasan_mempool_unpoison_object(entry, cache->elem_size);
+		if (cache->init_clear)
+			memset(entry, 0, cache->init_clear);
+#endif
 		return entry;
 	}
 
 	return NULL;
 }
 
-static inline void *io_cache_alloc(struct io_alloc_cache *cache, gfp_t gfp,
-				   void (*init_once)(void *obj))
+static inline void *io_cache_alloc(struct io_alloc_cache *cache, gfp_t gfp)
 {
-	if (unlikely(!cache->nr_cached)) {
-		void *obj = kmalloc(cache->elem_size, gfp);
+	void *obj;
 
-		if (obj && init_once)
-			init_once(obj);
+	obj = io_alloc_cache_get(cache);
+	if (obj)
 		return obj;
-	}
-	return io_alloc_cache_get(cache);
+
+	obj = kmalloc(cache->elem_size, gfp);
+	if (obj && cache->init_clear)
+		memset(obj, 0, cache->init_clear);
+	return obj;
 }
 
 /* returns false if the cache was initialized properly */
 static inline bool io_alloc_cache_init(struct io_alloc_cache *cache,
-				       unsigned max_nr, size_t size)
+				       unsigned max_nr, unsigned int size,
+				       unsigned int init_bytes)
 {
 	cache->entries = kvmalloc_array(max_nr, sizeof(void *), GFP_KERNEL);
 	if (cache->entries) {
 		cache->nr_cached = 0;
 		cache->max_cached = max_nr;
 		cache->elem_size = size;
+		cache->init_clear = init_bytes;
 		return false;
 	}
 	return true;
io_uring/futex.c +2 -2
index 30139cc150f2..3159a2b7eeca 100644
@@ -36,7 +36,7 @@ struct io_futex_data {
 bool io_futex_cache_init(struct io_ring_ctx *ctx)
 {
 	return io_alloc_cache_init(&ctx->futex_cache, IO_FUTEX_ALLOC_CACHE_MAX,
-				sizeof(struct io_futex_data));
+				sizeof(struct io_futex_data), 0);
 }
 
 void io_futex_cache_free(struct io_ring_ctx *ctx)
@@ -320,7 +320,7 @@ int io_futex_wait(struct io_kiocb *req, unsigned int issue_flags)
 	}
 
 	io_ring_submit_lock(ctx, issue_flags);
-	ifd = io_cache_alloc(&ctx->futex_cache, GFP_NOWAIT, NULL);
+	ifd = io_cache_alloc(&ctx->futex_cache, GFP_NOWAIT);
 	if (!ifd) {
 		ret = -ENOMEM;
 		goto done_unlock;
io_uring/io_uring.c +8 -5
index 7bfbc7c22367..7d38bdccbad8 100644
@@ -315,16 +315,19 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
 	INIT_LIST_HEAD(&ctx->cq_overflow_list);
 	INIT_LIST_HEAD(&ctx->io_buffers_cache);
 	ret = io_alloc_cache_init(&ctx->apoll_cache, IO_POLL_ALLOC_CACHE_MAX,
-			    sizeof(struct async_poll));
+			    sizeof(struct async_poll), 0);
 	ret |= io_alloc_cache_init(&ctx->netmsg_cache, IO_ALLOC_CACHE_MAX,
-			    sizeof(struct io_async_msghdr));
+			    sizeof(struct io_async_msghdr),
+			    offsetof(struct io_async_msghdr, init));
 	ret |= io_alloc_cache_init(&ctx->rw_cache, IO_ALLOC_CACHE_MAX,
-			    sizeof(struct io_async_rw));
+			    sizeof(struct io_async_rw),
+			    offsetof(struct io_async_rw, init));
 	ret |= io_alloc_cache_init(&ctx->uring_cache, IO_ALLOC_CACHE_MAX,
-			    sizeof(struct io_uring_cmd_data));
+			    sizeof(struct io_uring_cmd_data),
+			    offsetof(struct io_uring_cmd_data, init));
 	spin_lock_init(&ctx->msg_lock);
 	ret |= io_alloc_cache_init(&ctx->msg_cache, IO_ALLOC_CACHE_MAX,
-			    sizeof(struct io_kiocb));
+			    sizeof(struct io_kiocb), 0);
 	ret |= io_futex_cache_init(ctx);
 	if (ret)
 		goto free_ref;
io_uring/io_uring.h +2 -3
index f65e3f3ede51..67adbb3c1bf5 100644
@@ -226,10 +226,9 @@ static inline void io_req_set_res(struct io_kiocb *req, s32 res, u32 cflags)
 }
 
 static inline void *io_uring_alloc_async_data(struct io_alloc_cache *cache,
-					      struct io_kiocb *req,
-					      void (*init_once)(void *obj))
+					      struct io_kiocb *req)
 {
-	req->async_data = io_cache_alloc(cache, GFP_KERNEL, init_once);
+	req->async_data = io_cache_alloc(cache, GFP_KERNEL);
 	if (req->async_data)
 		req->flags |= REQ_F_ASYNC_DATA;
 	return req->async_data;
io_uring/net.c +1 -10
index 85f55fbc25c9..35bec4680fcc 100644
@@ -155,21 +155,12 @@ static void io_netmsg_recycle(struct io_kiocb *req, unsigned int issue_flags)
 	}
 }
 
-static void io_msg_async_data_init(void *obj)
-{
-	struct io_async_msghdr *hdr = (struct io_async_msghdr *)obj;
-
-	hdr->free_iov = NULL;
-	hdr->free_iov_nr = 0;
-}
-
 static struct io_async_msghdr *io_msg_alloc_async(struct io_kiocb *req)
 {
 	struct io_ring_ctx *ctx = req->ctx;
 	struct io_async_msghdr *hdr;
 
-	hdr = io_uring_alloc_async_data(&ctx->netmsg_cache, req,
-					io_msg_async_data_init);
+	hdr = io_uring_alloc_async_data(&ctx->netmsg_cache, req);
 	if (!hdr)
 		return NULL;
 
io_uring/net.h +5 -2
index 52bfee05f06a..22a1a34bca09 100644
@@ -5,16 +5,19 @@
 
 struct io_async_msghdr {
 #if defined(CONFIG_NET)
-	struct iovec			fast_iov;
-	/* points to an allocated iov, if NULL we use fast_iov instead */
 	struct iovec			*free_iov;
+	/* points to an allocated iov, if NULL we use fast_iov instead */
 	int				free_iov_nr;
+	int				init[0];
 	int				namelen;
+	struct iovec			fast_iov;
 	__kernel_size_t			controllen;
 	__kernel_size_t			payloadlen;
 	struct sockaddr __user		*uaddr;
 	struct msghdr			msg;
 	struct sockaddr_storage		addr;
+#else
+	int				init[0];
 #endif
 };
 
io_uring/poll.c +1 -1
index cc01c40b43d3..356474c66f32 100644
@@ -650,7 +650,7 @@ static struct async_poll *io_req_alloc_apoll(struct io_kiocb *req,
 		kfree(apoll->double_poll);
 	} else {
 		if (!(issue_flags & IO_URING_F_UNLOCKED))
-			apoll = io_cache_alloc(&ctx->apoll_cache, GFP_ATOMIC, NULL);
+			apoll = io_cache_alloc(&ctx->apoll_cache, GFP_ATOMIC);
 		else
 			apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
 		if (!apoll)
io_uring/rw.c +1 -9
index a9a2733be842..0a0ad8a2bc0d 100644
@@ -208,20 +208,12 @@ static void io_req_rw_cleanup(struct io_kiocb *req, unsigned int issue_flags)
 	}
 }
 
-static void io_rw_async_data_init(void *obj)
-{
-	struct io_async_rw *rw = (struct io_async_rw *)obj;
-
-	rw->free_iovec = NULL;
-	rw->bytes_done = 0;
-}
-
 static int io_rw_alloc_async(struct io_kiocb *req)
 {
 	struct io_ring_ctx *ctx = req->ctx;
 	struct io_async_rw *rw;
 
-	rw = io_uring_alloc_async_data(&ctx->rw_cache, req, io_rw_async_data_init);
+	rw = io_uring_alloc_async_data(&ctx->rw_cache, req);
 	if (!rw)
 		return -ENOMEM;
 	if (rw->free_iovec) {
io_uring/rw.h +4 -1
index 2d7656bd268d..ab9e4df2504a 100644
@@ -8,11 +8,14 @@ struct io_meta_state {
 };
 
 struct io_async_rw {
+	/* these must be cleared on initial alloc */
 	size_t				bytes_done;
+	struct iovec			*free_iovec;
+	int				init[0];
+	/* rest persists over caching */
 	struct iov_iter			iter;
 	struct iov_iter_state		iter_state;
 	struct iovec			fast_iov;
-	struct iovec			*free_iovec;
 	int				free_iov_nr;
 	/* wpq is for buffered io, while meta fields are used with direct io */
 	union {
io_uring/uring_cmd.c +1 -9
index 6a63ec4b5445..0da7dc20eca6 100644
@@ -168,21 +168,13 @@ void io_uring_cmd_done(struct io_uring_cmd *ioucmd, ssize_t ret, u64 res2,
 }
 EXPORT_SYMBOL_GPL(io_uring_cmd_done);
 
-static void io_uring_cmd_init_once(void *obj)
-{
-	struct io_uring_cmd_data *data = obj;
-
-	data->op_data = NULL;
-}	
-
 static int io_uring_cmd_prep_setup(struct io_kiocb *req,
 				   const struct io_uring_sqe *sqe)
 {
 	struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
 	struct io_uring_cmd_data *cache;
 
-	cache = io_uring_alloc_async_data(&req->ctx->uring_cache, req,
-			io_uring_cmd_init_once);
+	cache = io_uring_alloc_async_data(&req->ctx->uring_cache, req);
 	if (!cache)
 		return -ENOMEM;
 
Signature · 1 line
2.47.2
#7 lore raw

Re: [PATCH 2/2] io_uring: get rid of alloc cache init_once handling

Quoted 33 lines · Jens Axboe

init_once is called when an object doesn't come from the cache, and hence needs initial clearing of certain members. While the whole struct could get cleared by memset() in that case, a few of the cache members are large enough that this may cause unnecessary overhead if the caches used aren't large enough to satisfy the workload. For those cases, some churn of kmalloc+kfree is to be expected.

Ensure that the 3 users that need clearing put the members they need cleared at the start of the struct, and place an empty placeholder 'init' member so that the cache initialization knows how much to clear.

---
  include/linux/io_uring/cmd.h   |  3 ++-
  include/linux/io_uring_types.h |  3 ++-
  io_uring/alloc_cache.h         | 30 +++++++++++++++++++++---------
  io_uring/futex.c               |  4 ++--
  io_uring/io_uring.c            | 13 ++++++++-----
  io_uring/io_uring.h            |  5 ++---
  io_uring/net.c                 | 11 +----------
  io_uring/net.h                 |  7 +++++--
  io_uring/poll.c                |  2 +-
  io_uring/rw.c                  | 10 +---------
  io_uring/rw.h                  |  5 ++++-
  io_uring/uring_cmd.c           | 10 +---------
  12 files changed, 50 insertions(+), 53 deletions(-)
include/linux/io_uring/cmd.h +0 -0
index a3ce553413de..8d7746d9fd23 100644

include/linux/io_uring/cmd.h struct io_uring_cmd { · line 23

Quoted 6 lines
include/linux/io_uring/cmd.h +1 -1
@@ -19,8 +19,9 @@ struct io_uring_cmd {
  };
 
  struct io_uring_cmd_data {
-	struct io_uring_sqe	sqes[2];
  	void			*op_data;
+	int			init[0];

What do you think about using struct_group instead?

Signature · 1 line
Pavel Begunkov
#8 lore raw

Re: [PATCH 2/2] io_uring: get rid of alloc cache init_once handling

Quoted 29 lines · Pavel Begunkov
Quoted 28 lines · Jens Axboe

init_once is called when an object doesn't come from the cache, and hence needs initial clearing of certain members. While the whole struct could get cleared by memset() in that case, a few of the cache members are large enough that this may cause unnecessary overhead if the caches used aren't large enough to satisfy the workload. For those cases, some churn of kmalloc+kfree is to be expected.

Ensure that the 3 users that need clearing put the members they need cleared at the start of the struct, and place an empty placeholder 'init' member so that the cache initialization knows how much to clear.

---
  include/linux/io_uring/cmd.h   |  3 ++- include/linux/io_uring_types.h |  3 ++- io_uring/alloc_cache.h         | 30 +++++++++++++++++++++--------- io_uring/futex.c               |  4 ++-- io_uring/io_uring.c            | 13 ++++++++----- io_uring/io_uring.h            |  5 ++--- io_uring/net.c                 | 11 +---------- io_uring/net.h                 |  7 +++++-- io_uring/poll.c                |  2 +-
  io_uring/rw.c                  | 10 +--------- io_uring/rw.h                  |  5 ++++- io_uring/uring_cmd.c           | 10 +---------

  12 files changed, 50 insertions(+), 53 deletions(-)
Quoted 12 lines

include/linux/io_uring/cmd.h struct io_uring_cmd { · line 22

Quoted 10 lines
include/linux/io_uring/cmd.h +1 -1
index a3ce553413de..8d7746d9fd23 100644
@@ -19,8 +19,9 @@ struct io_uring_cmd {
   };
   struct io_uring_cmd_data {
-    struct io_uring_sqe    sqes[2];
       void            *op_data;
+    int            init[0];

What do you think about using struct_group instead?

And why do we care not clearing it all on initial alloc? If that's because of kasan, we can disable it until ("kasan, mempool: don't store free stacktrace in io_alloc_cache objects") lands.

Signature · 1 line
Pavel Begunkov
author maintainer
#9 lore raw

Re: [PATCH 2/2] io_uring: get rid of alloc cache init_once handling

Quoted 43 lines · Pavel Begunkov
Quoted 29 lines · Pavel Begunkov
Quoted 28 lines · Jens Axboe

init_once is called when an object doesn't come from the cache, and hence needs initial clearing of certain members. While the whole struct could get cleared by memset() in that case, a few of the cache members are large enough that this may cause unnecessary overhead if the caches used aren't large enough to satisfy the workload. For those cases, some churn of kmalloc+kfree is to be expected.

Ensure that the 3 users that need clearing put the members they need cleared at the start of the struct, and place an empty placeholder 'init' member so that the cache initialization knows how much to clear.

---
  include/linux/io_uring/cmd.h   |  3 ++-
  include/linux/io_uring_types.h |  3 ++-
  io_uring/alloc_cache.h         | 30 +++++++++++++++++++++---------
  io_uring/futex.c               |  4 ++--
  io_uring/io_uring.c            | 13 ++++++++-----
  io_uring/io_uring.h            |  5 ++---
  io_uring/net.c                 | 11 +----------
  io_uring/net.h                 |  7 +++++--
  io_uring/poll.c                |  2 +-
  io_uring/rw.c                  | 10 +---------
  io_uring/rw.h                  |  5 ++++-
  io_uring/uring_cmd.c           | 10 +---------
  12 files changed, 50 insertions(+), 53 deletions(-)
Quoted 12 lines

include/linux/io_uring/cmd.h struct io_uring_cmd { · line 22

Quoted 10 lines
include/linux/io_uring/cmd.h +1 -1
index a3ce553413de..8d7746d9fd23 100644
@@ -19,8 +19,9 @@ struct io_uring_cmd {
  };
  struct io_uring_cmd_data {
-    struct io_uring_sqe    sqes[2];
      void            *op_data;
+    int            init[0];

What do you think about using struct_group instead?

Quoted 3 lines

And why do we care not clearing it all on initial alloc? If that's because of kasan, we can disable it until ("kasan, mempool: don't store free stacktrace in io_alloc_cache objects") lands.

Not sure I follow - on initial alloc they do need clearing, that's when they need clearing. If they are coming from the cache, the state should be consistent.

Signature · 1 line
Jens Axboe
#10 lore raw

Re: [PATCH 2/2] io_uring: get rid of alloc cache init_once handling

Quoted 44 lines · Jens Axboe
Quoted 43 lines · Pavel Begunkov
Quoted 41 lines · Pavel Begunkov
Quoted 33 lines · Jens Axboe

init_once is called when an object doesn't come from the cache, and hence needs initial clearing of certain members. While the whole struct could get cleared by memset() in that case, a few of the cache members are large enough that this may cause unnecessary overhead if the caches used aren't large enough to satisfy the workload. For those cases, some churn of kmalloc+kfree is to be expected.

Ensure that the 3 users that need clearing put the members they need cleared at the start of the struct, and place an empty placeholder 'init' member so that the cache initialization knows how much to clear.

---
   include/linux/io_uring/cmd.h   |  3 ++-
   include/linux/io_uring_types.h |  3 ++-
   io_uring/alloc_cache.h         | 30 +++++++++++++++++++++---------
   io_uring/futex.c               |  4 ++--
   io_uring/io_uring.c            | 13 ++++++++-----
   io_uring/io_uring.h            |  5 ++---
   io_uring/net.c                 | 11 +----------
   io_uring/net.h                 |  7 +++++--
   io_uring/poll.c                |  2 +-
   io_uring/rw.c                  | 10 +---------
   io_uring/rw.h                  |  5 ++++-
   io_uring/uring_cmd.c           | 10 +---------
   12 files changed, 50 insertions(+), 53 deletions(-)
include/linux/io_uring/cmd.h +0 -0
index a3ce553413de..8d7746d9fd23 100644

include/linux/io_uring/cmd.h struct io_uring_cmd { · line 22

Quoted 5 lines
include/linux/io_uring/cmd.h +1 -1
@@ -19,8 +19,9 @@ struct io_uring_cmd {
   };
   struct io_uring_cmd_data {
-    struct io_uring_sqe    sqes[2];
       void            *op_data;
+    int            init[0];

What do you think about using struct_group instead?

Quoted 7 lines
Quoted 3 lines

And why do we care not clearing it all on initial alloc? If that's because of kasan, we can disable it until ("kasan, mempool: don't store free stacktrace in io_alloc_cache objects") lands.

Not sure I follow - on initial alloc they do need clearing, that's when they need clearing. If they are coming from the cache, the state should be consistent.

If we forget about kasan, ->init_clear is only really used right after allocation().

+ obj = kmalloc(cache->elem_size, gfp);
+ if (obj && cache->init_clear)
+ memset(obj, 0, cache->init_clear);

Why not kzalloc() it?

Signature · 1 line
Pavel Begunkov
author maintainer
#11 lore raw

Re: [PATCH 2/2] io_uring: get rid of alloc cache init_once handling

Quoted 60 lines · Pavel Begunkov
Quoted 44 lines · Jens Axboe
Quoted 43 lines · Pavel Begunkov
Quoted 41 lines · Pavel Begunkov
Quoted 33 lines · Jens Axboe

init_once is called when an object doesn't come from the cache, and hence needs initial clearing of certain members. While the whole struct could get cleared by memset() in that case, a few of the cache members are large enough that this may cause unnecessary overhead if the caches used aren't large enough to satisfy the workload. For those cases, some churn of kmalloc+kfree is to be expected.

Ensure that the 3 users that need clearing put the members they need cleared at the start of the struct, and place an empty placeholder 'init' member so that the cache initialization knows how much to clear.

---
   include/linux/io_uring/cmd.h   |  3 ++-
   include/linux/io_uring_types.h |  3 ++-
   io_uring/alloc_cache.h         | 30 +++++++++++++++++++++---------
   io_uring/futex.c               |  4 ++--
   io_uring/io_uring.c            | 13 ++++++++-----
   io_uring/io_uring.h            |  5 ++---
   io_uring/net.c                 | 11 +----------
   io_uring/net.h                 |  7 +++++--
   io_uring/poll.c                |  2 +-
   io_uring/rw.c                  | 10 +---------
   io_uring/rw.h                  |  5 ++++-
   io_uring/uring_cmd.c           | 10 +---------
   12 files changed, 50 insertions(+), 53 deletions(-)
include/linux/io_uring/cmd.h +0 -0
index a3ce553413de..8d7746d9fd23 100644

include/linux/io_uring/cmd.h struct io_uring_cmd { · line 22

Quoted 5 lines
include/linux/io_uring/cmd.h +1 -1
@@ -19,8 +19,9 @@ struct io_uring_cmd {
   };
   struct io_uring_cmd_data {
-    struct io_uring_sqe    sqes[2];
       void            *op_data;
+    int            init[0];

What do you think about using struct_group instead?

Quoted 7 lines
Quoted 3 lines

And why do we care not clearing it all on initial alloc? If that's because of kasan, we can disable it until ("kasan, mempool: don't store free stacktrace in io_alloc_cache objects") lands.

Not sure I follow - on initial alloc they do need clearing, that's when they need clearing. If they are coming from the cache, the state should be consistent.

If we forget about kasan, ->init_clear is only really used right after allocation().

io_uring/alloc_cache.h +3 -0
+    obj = kmalloc(cache->elem_size, gfp);
+    if (obj && cache->init_clear)
+        memset(obj, 0, cache->init_clear);
Quoted 1 line

Why not kzalloc() it?

We obviously could, but rw/net is 2-300b in size, and most of it we don't need to clear. Yes this is only for the "slower" path of hitting alloc rather than the cache, but it's not a given at all that any workload will fit within the cache, unfortunately. That's quite a lot of memset for those cases.

But maybe I'm overdoing it and we just kzalloc() it. It'd obviously be a lot simpler.

Signature · 1 line
Jens Axboe
author maintainer
#12 lore raw

Re: [PATCH 2/2] io_uring: get rid of alloc cache init_once handling

In reply to Pavel Begunkov
Quoted 29 lines · Pavel Begunkov
Quoted 28 lines · Jens Axboe

init_once is called when an object doesn't come from the cache, and hence needs initial clearing of certain members. While the whole struct could get cleared by memset() in that case, a few of the cache members are large enough that this may cause unnecessary overhead if the caches used aren't large enough to satisfy the workload. For those cases, some churn of kmalloc+kfree is to be expected.

Ensure that the 3 users that need clearing put the members they need cleared at the start of the struct, and place an empty placeholder 'init' member so that the cache initialization knows how much to clear.

---
  include/linux/io_uring/cmd.h   |  3 ++-
  include/linux/io_uring_types.h |  3 ++-
  io_uring/alloc_cache.h         | 30 +++++++++++++++++++++---------
  io_uring/futex.c               |  4 ++--
  io_uring/io_uring.c            | 13 ++++++++-----
  io_uring/io_uring.h            |  5 ++---
  io_uring/net.c                 | 11 +----------
  io_uring/net.h                 |  7 +++++--
  io_uring/poll.c                |  2 +-
  io_uring/rw.c                  | 10 +---------
  io_uring/rw.h                  |  5 ++++-
  io_uring/uring_cmd.c           | 10 +---------
  12 files changed, 50 insertions(+), 53 deletions(-)
Quoted 12 lines

include/linux/io_uring/cmd.h struct io_uring_cmd { · line 22

Quoted 10 lines
include/linux/io_uring/cmd.h +1 -1
index a3ce553413de..8d7746d9fd23 100644
@@ -19,8 +19,9 @@ struct io_uring_cmd {
  };
    struct io_uring_cmd_data {
-    struct io_uring_sqe    sqes[2];
      void            *op_data;
+    int            init[0];

What do you think about using struct_group instead?

That's a good idea, better than the magic empty variable. I'll make that change for v2, thanks!

Signature · 1 line
Jens Axboe