io-uring.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Markus Elfring <Markus.Elfring@web.de>
To: io-uring@vger.kernel.org, kernel-janitors@vger.kernel.org,
	Gabriel Krisman Bertazi <krisman@suse.de>,
	Jens Axboe <axboe@kernel.dk>,
	Pavel Begunkov <asml.silence@gmail.com>
Cc: LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH v2 2/2] io_uring: Improve exception handling in io_ring_ctx_alloc()
Date: Wed, 10 Jan 2024 21:50:15 +0100	[thread overview]
Message-ID: <49ecda98-770d-455e-acd7-12d810280fdd@web.de> (raw)
In-Reply-To: <b9c9ba9f-459e-40b5-ae4b-703dcc03871d@web.de>

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Wed, 10 Jan 2024 21:15:48 +0100

The label “err” was used to jump to a kfree() call despite of
the detail in the implementation of the function “io_ring_ctx_alloc”
that it was determined already that a corresponding variable contained
a null pointer because of a failed memory allocation.

1. Thus use more appropriate labels instead.

2. Reorder jump targets at the end.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---

See also:
https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consider+using+a+goto+chain+when+leaving+a+function+on+error+when+using+and+releasing+resources


 io_uring/io_uring.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index c9a63c39cdd0..7727cdd505ae 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -295,12 +295,14 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
 	hash_bits = ilog2(p->cq_entries) - 5;
 	hash_bits = clamp(hash_bits, 1, 8);
 	if (io_alloc_hash_table(&ctx->cancel_table, hash_bits))
-		goto err;
+		goto destroy_io_bl_xa;
+
 	if (io_alloc_hash_table(&ctx->cancel_table_locked, hash_bits))
-		goto err;
+		goto free_cancel_table_hbs;
+
 	if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
 			    0, GFP_KERNEL))
-		goto err;
+		goto free_cancel_table_locked_hbs;

 	ctx->flags = p->flags;
 	init_waitqueue_head(&ctx->sqo_sq_wait);
@@ -341,9 +343,12 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
 	INIT_WQ_LIST(&ctx->submit_state.compl_reqs);
 	INIT_HLIST_HEAD(&ctx->cancelable_uring_cmd);
 	return ctx;
-err:
-	kfree(ctx->cancel_table.hbs);
+
+free_cancel_table_locked_hbs:
 	kfree(ctx->cancel_table_locked.hbs);
+free_cancel_table_hbs:
+	kfree(ctx->cancel_table.hbs);
+destroy_io_bl_xa:
 	xa_destroy(&ctx->io_bl_xa);
 	kfree(ctx);
 	return NULL;
--
2.43.0


  parent reply	other threads:[~2024-01-10 20:50 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <6cbcf640-55e5-2f11-4a09-716fe681c0d2@web.de>
     [not found] ` <aa867594-e79d-6d08-a08e-8c9e952b4724@web.de>
2024-01-10 11:23   ` io_uring: Fix exception handling in io_ring_ctx_alloc() Markus Elfring
2024-01-10 16:55   ` [cocci] [PATCH] " Gabriel Krisman Bertazi
2024-01-10 20:45     ` [PATCH v2 0/2] io_uring: Adjustments for io_ring_ctx_alloc() Markus Elfring
2024-01-10 20:48       ` [PATCH v2 1/2] io_uring: Delete a redundant kfree() call in io_ring_ctx_alloc() Markus Elfring
2024-01-12 14:25         ` Gabriel Krisman Bertazi
2024-01-12 16:18           ` Jens Axboe
2024-01-12 17:15             ` Gabriel Krisman Bertazi
2024-01-12 17:50               ` [v2 " Markus Elfring
2024-01-10 20:50       ` Markus Elfring [this message]
2024-01-11 13:23         ` [PATCH v2 2/2] io_uring: Improve exception handling " Pavel Begunkov
2024-01-12 14:30         ` Gabriel Krisman Bertazi
2024-01-12 15:00           ` [v2 " Markus Elfring

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=49ecda98-770d-455e-acd7-12d810280fdd@web.de \
    --to=markus.elfring@web.de \
    --cc=asml.silence@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=io-uring@vger.kernel.org \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=krisman@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).