public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Pavel Begunkov <asml.silence@gmail.com>
To: Markus Elfring <Markus.Elfring@web.de>,
	io-uring@vger.kernel.org, kernel-janitors@vger.kernel.org,
	Gabriel Krisman Bertazi <krisman@suse.de>,
	Jens Axboe <axboe@kernel.dk>
Cc: LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2 2/2] io_uring: Improve exception handling in io_ring_ctx_alloc()
Date: Thu, 11 Jan 2024 13:23:31 +0000	[thread overview]
Message-ID: <4b1deeeb-b5fd-481d-99b5-7d29c0edce2d@gmail.com> (raw)
In-Reply-To: <49ecda98-770d-455e-acd7-12d810280fdd@web.de>

On 1/10/24 20:50, Markus Elfring wrote:
> 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.

It's _much_ simpler the way it currently is, compare it with maintaining
a bunch of labels. That is the advantage of being able to distinguish
un-allocated state like NULL, just kfree them and don't care about
jumping to a wrong one or keeping them in order.

  
> 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
> 

-- 
Pavel Begunkov

  reply	other threads:[~2024-01-11 13:33 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <6cbcf640-55e5-2f11-4a09-716fe681c0d2@web.de>
     [not found] ` <d33ebeca-0900-8c6c-ac44-f301daf24a5b@web.de>
2023-04-09 23:15   ` [PATCH] apparmor: Return directly after a failed kzalloc() in two functions John Johansen
     [not found] ` <f1712777-97ff-d89c-0bdd-d72faed9a7f1@web.de>
2024-01-05 20:32   ` [PATCH] squashfs: Improve exception handling in squashfs_decompressor_create() Markus Elfring
     [not found] ` <33226beb-4fe2-3da5-5d69-a33e683dec57@web.de>
2024-01-10 11:40   ` [PATCH 0/3] lru_cache: Adjustments for lc_create() Markus Elfring
     [not found] ` <d9c673f9-2c32-282f-f261-b4d5762409bb@web.de>
2024-01-10 11:45   ` [PATCH 0/2] perf/x86/intel/pt: Adjustments for pt_pmu_hw_init() Markus Elfring
     [not found] ` <562a6f99-3f8e-9a77-e519-b668e24dced2@web.de>
2024-01-10 11:48   ` [PATCH 0/2] ARM: Adjustments for init_atags_procfs() Markus Elfring
2024-01-10 11:52     ` [0/2] " Markus Elfring
2024-01-10 12:24       ` Russell King (Oracle)
2024-01-10 12:44         ` Markus Elfring
2024-01-10 12:47           ` Russell King (Oracle)
2024-01-10 12:52             ` Markus Elfring
2024-01-10 13:50               ` Russell King (Oracle)
2024-01-10 14:00                 ` Markus Elfring
2024-01-10 14:07                   ` Russell King (Oracle)
2024-01-10 13:34             ` Christian Heusel
2024-01-10 13:46               ` Markus Elfring
     [not found] ` <87b65f8e-abde-2aff-4da8-df6e0b464677@web.de>
2024-01-10 12:24   ` [PATCH 0/4] overlayfs: Adjustments for ovl_fill_super() Markus Elfring
2024-01-10 12:49     ` Amir Goldstein
2024-01-10 13:01       ` [0/4] " Markus Elfring
2024-01-10 13:19         ` Amir Goldstein
2024-01-10 13:33           ` Markus Elfring
2024-01-10 13:45             ` Amir Goldstein
     [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       ` [PATCH v2 2/2] io_uring: Improve exception handling " Markus Elfring
2024-01-11 13:23         ` Pavel Begunkov [this message]
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=4b1deeeb-b5fd-481d-99b5-7d29c0edce2d@gmail.com \
    --to=asml.silence@gmail.com \
    --cc=Markus.Elfring@web.de \
    --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