public inbox for linux-i2c@vger.kernel.org
 help / color / mirror / Atom feed
From: Wolfram Sang <wsa@kernel.org>
To: Yang Yingliang <yangyingliang@huawei.com>
Cc: linux-i2c@vger.kernel.org
Subject: Re: [PATCH] i2c: core: Fix possible memleak in i2c_new_client_device()
Date: Fri, 29 Sep 2023 11:15:59 +0200	[thread overview]
Message-ID: <ZRaVz4pj8zko8x7W@ninjato> (raw)
In-Reply-To: <20221124085448.3620240-1-yangyingliang@huawei.com>

[-- Attachment #1: Type: text/plain, Size: 1627 bytes --]

On Thu, Nov 24, 2022 at 04:54:48PM +0800, Yang Yingliang wrote:
> I got memory leak as follows when doing fault injection test:
> 
> unreferenced object 0xffff888014aec078 (size 8):
>   comm "xrun", pid 356, jiffies 4294910619 (age 16.332s)
>   hex dump (first 8 bytes):
>     31 2d 30 30 31 63 00 00                          1-001c..
>   backtrace:
>     [<00000000eb56c0a9>] __kmalloc_track_caller+0x1a6/0x300
>     [<000000000b220ea3>] kvasprintf+0xad/0x140
>     [<00000000b83203e5>] kvasprintf_const+0x62/0x190
>     [<000000002a5eab37>] kobject_set_name_vargs+0x56/0x140
>     [<00000000300ac279>] dev_set_name+0xb0/0xe0
>     [<00000000b66ebd6f>] i2c_new_client_device+0x7e4/0x9a0
> 
> If device_register() returns error in i2c_new_client_device(),
> the name allocated by i2c_dev_set_name() need be freed. As
> comment of device_register() says, it should use put_device()
> to give up the reference in the error path.
> 
> Moving i2c_dev_set_name() to front of device_register(), so
> it's managed by device core, then call put_device(), when the
> refcount is 0, the name will be freed in kobject_cleanup() and
> the 'client' will be freed in i2c_client_dev_release().
> 
> Because the 'client' is freed by calling put_device(), so it
> don't need goto error label, release other resources before
> put_device() and return directly.
> 
> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>

The report seems correct to me, yet the solution is copying code a tad
too much IMO. I'll send another approach I'd suggest to spark some
discussion. Thanks for the report in any case!


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2023-09-29  9:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-24  8:54 [PATCH] i2c: core: Fix possible memleak in i2c_new_client_device() Yang Yingliang
2023-09-29  9:15 ` Wolfram Sang [this message]
  -- strict thread matches above, loose matches on Subject: below --
2021-10-15  9:55 Yang Yingliang
2021-10-17 15:27 ` Wolfram Sang
2021-10-18  3:03   ` Yang Yingliang

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=ZRaVz4pj8zko8x7W@ninjato \
    --to=wsa@kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=yangyingliang@huawei.com \
    /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