From: "Zhengbin (OSKernel)" <zhengbin13@huawei.com>
To: Markus Elfring <Markus.Elfring@web.de>, linux-block@vger.kernel.org
Cc: nbd@other.debian.org, Navid Emamdoost <navid.emamdoost@gmail.com>,
Navid Emamdoost <emamd001@umn.edu>, Kangjie Lu <kjlu@umn.edu>,
Stephen McCamant <mccamant@cs.umn.edu>,
Qiushi Wu <wu000273@umn.edu>,
kernel-janitors@vger.kernel.org, linux-kernel@vger.kernel.org,
Jens Axboe <axboe@kernel.dk>, Josef Bacik <josef@toxicpanda.com>,
Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>,
Yi Zhang <yi.zhang@huawei.com>
Subject: Re: [PATCH v2] nbd: Fix memory leak in nbd_add_socket
Date: Mon, 22 Jun 2020 03:25:52 +0000 [thread overview]
Message-ID: <db17d022-a53a-3781-63d4-c38d5cf5b1e4@huawei.com> (raw)
In-Reply-To: <b55f8af2-b20c-214f-90f0-9b6efcb7273c@web.de>
On 2020/6/20 20:05, Markus Elfring wrote:
>> If we add first socket to nbd, config->socks is malloced but
>> num_connections does not update(nsock's allocation fail), the memory
>> is leaked. Cause in later nbd_config_put(), will only free config->socks
>> when num_connections is not 0.
>>
>> Let nsock's allocation first to avoid this.
> I suggest to improve this change description.
> Can an other wording variant be nicer?
em, how about this?
When adding first socket to nbd, if nsock's allocation fails, config->socks
is malloced but num_connections does not update, memory leak will
occur(Function
nbd_config_put will only free config->socks when num_connections is not 0).
>
>
> …
>> +++ b/drivers/block/nbd.c
>> @@ -1037,21 +1037,22 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
>> return -EBUSY;
>> }
>>
>> + nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
> Please use the following code variant.
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?idC33a9b0b67bb4e8bcd91bdd80da80b0ec151162#n854
>
> + nsock = kzalloc(sizeof(*nsock), GFP_KERNEL);
>
>
> …
>> if (!socks) {
>> sockfd_put(sock);
>> + kfree(nsock);
>> return -ENOMEM;
>> }
> Please take another software design possibility into account.
>
> if (!socks) {
> - sockfd_put(sock);
> - return -ENOMEM;
> + kfree(nsock);
> + goto put_socket;
> }
>
>
> Regards,
> Markus
>
> .
>
next prev parent reply other threads:[~2020-06-22 3:25 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-20 12:05 [PATCH v2] nbd: Fix memory leak in nbd_add_socket Markus Elfring
2020-06-22 3:25 ` Zhengbin (OSKernel) [this message]
2020-06-22 7:52 ` 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=db17d022-a53a-3781-63d4-c38d5cf5b1e4@huawei.com \
--to=zhengbin13@huawei.com \
--cc=Markus.Elfring@web.de \
--cc=axboe@kernel.dk \
--cc=emamd001@umn.edu \
--cc=josef@toxicpanda.com \
--cc=kernel-janitors@vger.kernel.org \
--cc=kjlu@umn.edu \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mccamant@cs.umn.edu \
--cc=navid.emamdoost@gmail.com \
--cc=nbd@other.debian.org \
--cc=tuomas.tynkkynen@iki.fi \
--cc=wu000273@umn.edu \
--cc=yi.zhang@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;
as well as URLs for NNTP newsgroup(s).