public inbox for [email protected]
 help / color / mirror / Atom feed
From: Gabriel Krisman Bertazi <[email protected]>
To: [email protected], [email protected]
Cc: [email protected], Gabriel Krisman Bertazi <[email protected]>
Subject: [PATCH RESEND v2 1/9] io_uring: Fold allocation into alloc_cache helper
Date: Mon, 16 Dec 2024 15:46:07 -0500	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

The allocation paths that use alloc_cache duplicate the same code
pattern, sometimes in a quite convoluted way.  Fold the allocation into
the cache code itself, making it just an allocator function, and keeping
the cache policy invisible to callers.  Another justification for doing
this, beyond code simplicity, is that it makes it trivial to test the
impact of disabling the cache and using slab directly, which I've used
for slab improvement experiments.

One relevant detail is that we provide a callback to optionally
initialize memory only when we actually reach slab.  This allows us to
avoid blindly executing the allocation with GFP_ZERO and only clean
fields when they matter.

Signed-off-by: Gabriel Krisman Bertazi <[email protected]>

---
since v1:
  - add a callback to initialize objects coming from slab
  - rename io_alloc_cache_alloc -> io_cache_alloc
---
 io_uring/alloc_cache.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/io_uring/alloc_cache.h b/io_uring/alloc_cache.h
index b7a38a2069cf..a3a8cfec32ce 100644
--- a/io_uring/alloc_cache.h
+++ b/io_uring/alloc_cache.h
@@ -30,6 +30,19 @@ static inline void *io_alloc_cache_get(struct io_alloc_cache *cache)
 	return NULL;
 }
 
+static inline void *io_cache_alloc(struct io_alloc_cache *cache, gfp_t gfp,
+				   void (*init_once)(void *obj))
+{
+	if (unlikely(!cache->nr_cached)) {
+		void *obj = kmalloc(cache->elem_size, gfp);
+
+		if (obj && init_once)
+			init_once(obj);
+		return obj;
+	}
+	return io_alloc_cache_get(cache);
+}
+
 /* 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)
-- 
2.47.0


  reply	other threads:[~2024-12-16 20:46 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-16 20:46 [PATCH RESEND v2 0/9] Clean up alloc_cache allocations Gabriel Krisman Bertazi
2024-12-16 20:46 ` Gabriel Krisman Bertazi [this message]
2024-12-16 20:46 ` [PATCH RESEND v2 2/9] io_uring: Add generic helper to allocate async data Gabriel Krisman Bertazi
2024-12-16 20:46 ` [PATCH RESEND v2 3/9] io_uring/futex: Allocate ifd with generic alloc_cache helper Gabriel Krisman Bertazi
2024-12-16 20:46 ` [PATCH RESEND v2 4/9] io_uring/poll: Allocate apoll " Gabriel Krisman Bertazi
2024-12-16 20:46 ` [PATCH RESEND v2 5/9] io_uring/uring_cmd: Allocate async data through generic helper Gabriel Krisman Bertazi
2024-12-16 20:46 ` [PATCH RESEND v2 6/9] io_uring/net: Allocate msghdr async data through helper Gabriel Krisman Bertazi
2024-12-16 20:46 ` [PATCH RESEND v2 7/9] io_uring/rw: Allocate " Gabriel Krisman Bertazi
2024-12-16 20:46 ` [PATCH RESEND v2 8/9] io_uring: Move old async data allocation helper to header Gabriel Krisman Bertazi
2024-12-16 20:46 ` [PATCH RESEND v2 9/9] io_uring/msg_ring: Drop custom destructor Gabriel Krisman Bertazi
2024-12-17  0:05 ` [PATCH RESEND v2 0/9] Clean up alloc_cache allocations Jens Axboe

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 \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /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