Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1241398ybt; Sat, 20 Jun 2020 05:10:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzCFVtW9ydmLwU7S1KhoRK5mh7i9SfVQ+d06dok1RadNk5dQF7H60iyQOyKZJYB9MMD5LUU X-Received: by 2002:a17:907:94cf:: with SMTP id dn15mr4175233ejc.457.1592655036343; Sat, 20 Jun 2020 05:10:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592655036; cv=none; d=google.com; s=arc-20160816; b=01lZB0jIoLq4fWKlcOrCCVkyWmRSqRXUMkdbqgCx39lInIrx30ZoRphutMmglKzqmL uUGdoqR/ehb6wDUoCiBFmQe/TDnJhaapbfyhAaYr15fWvOJKro5A8rMqbF3xiHbIAXho NrDrreUMdnQ/inPbFDbIz4H5v5qufScrNsP2pbnVxM1chn06c9ZvmNx+kIqjvig0MWYk hgyRp4+Gf16Rmgamn9f+IKg1YufimeDMi6Gv4W3YQs//nvYe90VGkrUbB5tQeirnkH0i +KW1E4s4ZHAULR7dj6/SRFYuakcb2Xiy0JRgvWVcFq8a68HQamsZWmCtoXRLNPruZZ2W 6q6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:mime-version:user-agent:date:message-id:autocrypt :from:subject:cc:to:dkim-signature; bh=Dp7tTvN4MlDBXH29iWX+uIPDgw7B0N/mp76c+PQibAE=; b=gByUFnuw5r7a1uFI3KEL/wFM45iKBlhM2oKn3obkpffKhJkp28n76PqathEwRScEv6 oAkznpTIM3LjZErIZSYEgUsGNup316OGlH12dLEGJKtLLBBgNUuc3N6YgGHkxuTuBLyJ ApkhjOhJMXo2tcxz4unyUhJG6gqmsiKFAsZSX7s0PGtVn60tniyrtlBpM5uQFrhx8dux ZeV9rkuN0Cndx15He4zv4ekP0jK8o3QnQOeUDdwjpXxSeiOwGxNudpk+0JdQS1Y51haG xTwotuBtNfE3R1Ac4xXkPkxjlFYqmu+BkkDJQkAXCghq3RM4e7M0Y5kG34bC15KwIyKI nETw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@web.de header.s=dbaedf251592 header.b=PSMdyJdi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v13si5660586eju.21.2020.06.20.05.10.12; Sat, 20 Jun 2020 05:10:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@web.de header.s=dbaedf251592 header.b=PSMdyJdi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728096AbgFTMG0 (ORCPT + 99 others); Sat, 20 Jun 2020 08:06:26 -0400 Received: from mout.web.de ([212.227.15.3]:38855 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728028AbgFTMGY (ORCPT ); Sat, 20 Jun 2020 08:06:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1592654756; bh=42kox52x5HpeaYmlS8iN/4a3O5nbkPDAEP7S2unRXk0=; h=X-UI-Sender-Class:To:Cc:Subject:From:Date; b=PSMdyJdiPSNfETdpp0pchjC8ho/A2mDDmrynvsKLDlYdU2vdAQP/JqUunJm57Fusc A3EqqVFEJR6MiYjmP81EKsAAaFOOmGm+DMNTf1qvK8zjW7RO4sApwg2Haq1itshsOA 1R+6e9fRxos4TBXh/Iwn7LX95Y7iLBqBZQjR/CV8= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.1.2] ([2.243.139.185]) by smtp.web.de (mrweb004 [213.165.67.108]) with ESMTPSA (Nemesis) id 0LpQjF-1jGI7724lp-00f8bk; Sat, 20 Jun 2020 14:05:56 +0200 To: Zheng Bin , linux-block@vger.kernel.org Cc: nbd@other.debian.org, Navid Emamdoost , Navid Emamdoost , Kangjie Lu , Stephen McCamant , Qiushi Wu , kernel-janitors@vger.kernel.org, linux-kernel@vger.kernel.org, Jens Axboe , Josef Bacik , Tuomas Tynkkynen , Yi Zhang Subject: Re: [PATCH v2] nbd: Fix memory leak in nbd_add_socket From: Markus Elfring Autocrypt: addr=Markus.Elfring@web.de; prefer-encrypt=mutual; keydata= mQINBFg2+xABEADBJW2hoUoFXVFWTeKbqqif8VjszdMkriilx90WB5c0ddWQX14h6w5bT/A8 +v43YoGpDNyhgA0w9CEhuwfZrE91GocMtjLO67TAc2i2nxMc/FJRDI0OemO4VJ9RwID6ltwt mpVJgXGKkNJ1ey+QOXouzlErVvE2fRh+KXXN1Q7fSmTJlAW9XJYHS3BDHb0uRpymRSX3O+E2 lA87C7R8qAigPDZi6Z7UmwIA83ZMKXQ5stA0lhPyYgQcM7fh7V4ZYhnR0I5/qkUoxKpqaYLp YHBczVP+Zx/zHOM0KQphOMbU7X3c1pmMruoe6ti9uZzqZSLsF+NKXFEPBS665tQr66HJvZvY GMDlntZFAZ6xQvCC1r3MGoxEC1tuEa24vPCC9RZ9wk2sY5Csbva0WwYv3WKRZZBv8eIhGMxs rcpeGShRFyZ/0BYO53wZAPV1pEhGLLxd8eLN/nEWjJE0ejakPC1H/mt5F+yQBJAzz9JzbToU 5jKLu0SugNI18MspJut8AiA1M44CIWrNHXvWsQ+nnBKHDHHYZu7MoXlOmB32ndsfPthR3GSv jN7YD4Ad724H8fhRijmC1+RpuSce7w2JLj5cYj4MlccmNb8YUxsE8brY2WkXQYS8Ivse39MX BE66MQN0r5DQ6oqgoJ4gHIVBUv/ZwgcmUNS5g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vp+Orcz5KixHQmc5tbkLWf x0n1QFc1xxJhbzN+r9djSGGN/5IBDfUqSANC8cWzHpWaHmSuU3JSAMB/N+yQjIad2ztTckZY pwT6oxng29LzZspTYUEzMz3wK2jQHw+U66qBFk8whA7B2uAU1QdGyPgahLYSOa4XAEGb6wbI FEE= Message-ID: Date: Sat, 20 Jun 2020 14:05:53 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:fx4SRyXo0jNr5mPJylsMhZlfteu2CS304HYyK3aGfdL2nAhOOjd yE5x41KuAhq5SF9sIsrNOtbWuKG1BsIkG79e6LeM72s/gFk6bIBNBZ7G0dIhh++jOih2seU /RBx3U0LOrLoHdw/0NtM3WUcgJflhbDlQtcjCDxcDJydEUv9yz+fzEB7URgaJyx64p71xvg MvjIlyaoxEoxP8e8paWjg== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:AJbsK0XqvpM=:nVOkrWqzJ9B0Waj5yxBjBF UfQJNSKo7nIf0S3OfOJyLw8d9T8lKI6gq2lEbyHBzdgWEjdhiGnnewBtZjPjtpXUAHYNTWlvU Rb1t+dgbtp+MAipRuuJLCZ56cTgVkiRPG4+EYKw6NYu1Y14KRw/X5kQocOds4jovJVjFyC+uq tmlzZ4evfq4EMHn4Vcr9EKRfDe44oeMz2hpr224UvaftvjLHAM+AdA8DE2L92Xf7ebl2e8HuF oV4B5h/bhLIanTMSdJp6DBS1GImXqiTlxTDvt/2lrrqv+bdBiMjo/4WBAbE5b6jL5Ru2nu3x/ fR6eWAF/hEO43fz6BPwFAJY3yYZKhzI6c9pXbUmJHfo4es0xP+FZSEVrTDA9TnldUkRwrT3yj 4UZeV3ZoYvv/yfaPzapJbeK8r1zW5XErRbf7OazbfE3y14q+5L2vSPJU5ato3cY9dX0Nf7IpF ViokwvAhr1X5AcsBSsriqscwbhfrdpDOfFJSWxbI+PMqsBcdl37j20o4eozIcXlyZkmJmdrgj r0YmJ/r/pY4l9wv7/JnubBkgYUlJfrPysUDbYsIgsSmalOXlng7d8Nq1FLosojQhNRV8OPD/0 cQPXrtX6dkpXjH3SYQbJQHBgyOkOHa4+zsOuXkfeBvTxZRB6F/b4tXPLlEecaAwN3t027CDdY DXyybiqlghL0vgpiIwkPpfnYvNtcWEMuWIvrF1FZCjQ12asKSdib+zQ7MN50v3o2Tyxu9bJ95 wq4AJ+4imyBSrmTtSWo2HrsKVxOBCzivt1E2kuzChFskpESbTFUaeDd1csLamGW8hBqbULBVm ckwoZqxlSwD0Gj8ucJsrZoi1jc8Ud14hUtiVM4zeJO1PbAO2VlHvOIvg8rFK0RNkcSFI8mvPH OrxKBVRtRm6lnIcma1wKvQc+fm2CqupBxI/OYWZkfxV00UCtmKGIqhtysgo8IaRS6QXLYexhs WKY/umZKPa4CIvGQscQywds4TggE45k89qE5ES4+uUBa2ZhT24VlhMYWIuPFy6XrgQabMX9rY K8hhleekQgxgwj7d4UKZBNCPd795w7u6mwg+c9HlfBMvTQHixqhmnXPXvRmFMMCeA+jRypKeR 4fQXbL+p1RwJs1FELwwAEFOJD2/U7Zs+k7t3d8+YRXj3t6AGR+sjTRwVBe2XIIwElWcc7aQfg jUDDVY9IizI6KoFHZ7lw66/rD+YcxraQuFHEv21M6oBWzcxJhojAJwty3l/mIlR1g8SsTPaJk 7kWz7g8VJE8YYaANi Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > 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? =E2=80=A6 > +++ b/drivers/block/nbd.c > @@ -1037,21 +1037,22 @@ static int nbd_add_socket(struct nbd_device *nb= d, unsigned long arg, > return -EBUSY; > } > > + nsock =3D 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/Do= cumentation/process/coding-style.rst?id=3D4333a9b0b67bb4e8bcd91bdd80da80b0= ec151162#n854 + nsock =3D kzalloc(sizeof(*nsock), GFP_KERNEL); =E2=80=A6 > 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