Commit fa359552 authored by Jens Axboe's avatar Jens Axboe
Browse files

io_uring: get rid of alloc cache init_once handling



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 wrap the rest of the struct in
a struct group so the offset is known.

While at it, improve the interaction with KASAN such that when/if
KASAN writes to members inside the struct that should be retained over
caching, it won't trip over itself. For rw and net, the retaining of
the iovec over caching is disabled if KASAN is enabled. A helper will
free and clear those members in that case.

Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent eaf72f7b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -19,8 +19,8 @@ struct io_uring_cmd {
};

struct io_uring_cmd_data {
	struct io_uring_sqe	sqes[2];
	void			*op_data;
	struct io_uring_sqe	sqes[2];
};

static inline const void *io_uring_sqe_cmd(const struct io_uring_sqe *sqe)
+2 −1
Original line number Diff line number Diff line
@@ -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 {
+34 −9
Original line number Diff line number Diff line
@@ -6,6 +6,19 @@
 */
#define IO_ALLOC_CACHE_MAX	128

#if defined(CONFIG_KASAN)
static inline void io_alloc_cache_kasan(struct iovec **iov, int *nr)
{
	kfree(*iov);
	*iov = NULL;
	*nr = 0;
}
#else
static inline void io_alloc_cache_kasan(struct iovec **iov, int *nr)
{
}
#endif

static inline bool io_alloc_cache_put(struct io_alloc_cache *cache,
				      void *entry)
{
@@ -23,35 +36,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;

	obj = kmalloc(cache->elem_size, gfp);
	if (obj && cache->init_clear)
		memset(obj, 0, cache->init_clear);
	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)
				       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;
+2 −2
Original line number Diff line number Diff line
@@ -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;
+7 −5
Original line number Diff line number Diff line
@@ -315,16 +315,18 @@ 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, clear));
	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, clear));
	ret |= io_alloc_cache_init(&ctx->uring_cache, IO_ALLOC_CACHE_MAX,
			    sizeof(struct io_uring_cmd_data));
			    sizeof(struct io_uring_cmd_data), 0);
	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;
Loading