Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2429411ybt; Sun, 21 Jun 2020 20:28:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwUkTGXFanSN4HQVERFc94NRGXDVFo49mj0nf3TP6KOiHQptJKhWI5U1sOQ+GBwIoPuL8ze X-Received: by 2002:a17:906:fcae:: with SMTP id qw14mr14766794ejb.166.1592796483947; Sun, 21 Jun 2020 20:28:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592796483; cv=none; d=google.com; s=arc-20160816; b=XGafiAo/zlSRc/ZRn13L7mfpuWZT6MEgkK6xqInrqfbemTdKcIl5+K/1JwqvmRUxiO 6tcACQOK3ZNaZdVtev3NMlCSpa+ANijXcXfSjDXHso8uzY0khahBS8JqZ2aaFbYeXY3+ gNKsZXDYAGDTWZYTqZAXzTPxPZXUJTnRiy08+yO0CFn2iqOmk71oTWQcWnuq11LsYvt7 pVAo04Jlw3xTdzYUQi+Yhd9gAJoC9pid9ra03x/Q7FAUQM4aDz/dYYEr+dltPSqVJXsB ZKI5nRF2V6i8QVRKFe4sKv+31Xa1iPI0WjqXIqVAMDSfUzqHM53wapyVhGdGyeiPT6z6 4rEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=+v0TB/9miMioMRizEh9FS57njOgVVOwHBRCkQP+tLGE=; b=CCxu18gQZ58gJ1aos6BO+M2PRLb7az1dvih5z4fvSHytMDvy72HFsrEcLQSaRFhlLo eKDCuB6tJgtiEn/zKYeHqFO9QHTAH9hM8Qv2LSyoqH2zIDGjoP+an7YIz9oKW4u9B4C9 WwQJtp1whUu4MAcLKl2Fv+sHBbeaOHUpYf3jPC21Raliv1AzBVzFzSXHzpiI+/cFQeB2 PykCwJOQ+qdEH+oJD1LJDrEOnGWtqk+U7y2rfw0ak7IVW9anNE8hz7pM6i/+v26FqfJR Ql6MMZYBCedr0JYTnMFnQbQvi9+53OyaJjEMg58i8Rb0ME8srO9S89UeiG2LWjKEPnzj 1e9A== ARC-Authentication-Results: i=1; mx.google.com; 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 y5si8514488edw.121.2020.06.21.20.27.41; Sun, 21 Jun 2020 20:28:03 -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; 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 S1726724AbgFVD0F (ORCPT + 99 others); Sun, 21 Jun 2020 23:26:05 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:40284 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726559AbgFVD0E (ORCPT ); Sun, 21 Jun 2020 23:26:04 -0400 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 9449AD0E39D143FD9445; Mon, 22 Jun 2020 11:26:01 +0800 (CST) Received: from [127.0.0.1] (10.166.215.138) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.487.0; Mon, 22 Jun 2020 11:25:54 +0800 Subject: Re: [PATCH v2] nbd: Fix memory leak in nbd_add_socket To: Markus Elfring , CC: , Navid Emamdoost , "Navid Emamdoost" , Kangjie Lu , Stephen McCamant , Qiushi Wu , , , Jens Axboe , Josef Bacik , Tuomas Tynkkynen , Yi Zhang References: From: "Zhengbin (OSKernel)" Message-ID: Date: Mon, 22 Jun 2020 11:25:52 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Originating-IP: [10.166.215.138] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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?id=4333a9b0b67bb4e8bcd91bdd80da80b0ec151162#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 > > . >