public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: Malaya Kumar Rout <mrout@redhat.com>
Cc: linux-kernel@vger.kernel.org, lyude@redhat.com,
	malayarout91@gmail.com, John Stultz <jstultz@google.com>,
	Stephen Boyd <sboyd@kernel.org>
Subject: Re: [PATCH] timekeeping: Fix resource leak in tk_aux_sysfs_init() error paths
Date: Sun, 16 Nov 2025 18:14:02 +0100	[thread overview]
Message-ID: <87y0o599yd.ffs@tglx> (raw)
In-Reply-To: <CADD9qegc7VsPFnWgdhRQU-gSHnRtcU=M3+aQBjx5=6FB0FGv8w@mail.gmail.com>

On Sun, Nov 16 2025 at 21:19, Malaya Kumar Rout wrote:
> On Sat, Nov 15, 2025 at 12:11 AM Thomas Gleixner <tglx@linutronix.de> wrote:
>> >       auxo = kobject_create_and_add("aux_clocks", tko);
>> >       if (!auxo) {
>> > -             kobject_put(tko);
>> > -             return -ENOMEM;
>> > +             ret = -ENOMEM;
>> > +             goto err_put_tko;
>>
>> This ret variable is completely pointless as it is set to -ENOMEM in
>> every error path. Just make the error path do 'return -ENOMEM;', no?
>>
> While it's true that most error paths in this function return -ENOMEM, the
> sysfs_create_group() call can return different error codes depending on the
> failure mode:
>                 - -ENOMEM: Memory allocation failure
>                 - -EEXIST: Attribute group already exists
>                 - -EINVAL: Invalid arguments
>   By preserving the 'ret' variable, we ensure that the actual error code from
>   sysfs_create_group() is propagated to the caller. This provides more accurate
>   error information for debugging and allows the caller to handle different
>   error conditions appropriately.

Fair enough

>> >       }
>> >
>> >       for (int i = 0; i < MAX_AUX_CLOCKS; i++) {
>> >               char id[2] = { [0] = '0' + i, };
>> >               struct kobject *clk = kobject_create_and_add(id, auxo);
>> >
>> > -             if (!clk)
>> > -                     return -ENOMEM;
>> > -
>> > -             int ret = sysfs_create_group(clk, &aux_clock_enable_attr_group);
>> > +             if (!clk) {
>> > +                     ret = -ENOMEM;
>> > +                     goto err_put_auxo;
>> > +             }
>> >
>> > +             ret = sysfs_create_group(clk, &aux_clock_enable_attr_group);
>> >               if (ret)
>> > -                     return ret;
>> > +                     goto err_put_auxo;
>> >       }
>> >       return 0;
>> > +
>> > +err_put_auxo:
>> > +     kobject_put(auxo);
>> > +err_put_tko:
>> > +     kobject_put(tko);
>> > +     return ret;
>>
>> You can simplify that with _one_ error label:
>>
>> err:
>>         kobject_put(auxo);
>>         kobject_put(tko);
>>         return -ENOMEM;
>>
>> because kobject_put() is NULL pointer safe.
> I agree with your suggestion to use a single error label since
> kobject_put() is NULL-safe.
>
> Thank you for reviewing the patch and providing valuable feedback.
> I will incorporate your suggestion for the single error label in the
> next version while retaining the 'ret' variable for proper error
> propagation.

To avoid this ENOMEM nonsense you can split the stuff and do:

static int __init tk_aux_sysfs_init(void)
{
	struct kobject *auxo, *tko = kobject_create_and_add("time", kernel_kobj);
        int ret = -ENOMEM;

	if (!tko)
		return -ENOMEM;

	auxo = kobject_create_and_add("aux_clocks", tko);
	if (auxo)
        	ret = __tk_aux_sysfs_init(auxo);

	if (ret) {
		kobject_put(auxo);
		kobject_put(tko);
        }
	return ret;
}

which spares all the extra 'ret = -ENOMEM;' completely.

Thanks,

        tglx


  reply	other threads:[~2025-11-16 17:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-10  7:00 [PATCH] timekeeping: Fix resource leak in tk_aux_sysfs_init() error paths Malaya Kumar Rout
2025-11-14 18:41 ` Thomas Gleixner
2025-11-16 15:49   ` Malaya Kumar Rout
2025-11-16 17:14     ` Thomas Gleixner [this message]
2025-11-20 15:02       ` [PATCH v2] " Malaya Kumar Rout
2025-11-20 15:44         ` [tip: timers/urgent] " tip-bot2 for Malaya Kumar Rout

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=87y0o599yd.ffs@tglx \
    --to=tglx@linutronix.de \
    --cc=jstultz@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lyude@redhat.com \
    --cc=malayarout91@gmail.com \
    --cc=mrout@redhat.com \
    --cc=sboyd@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