Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp98199ybb; Tue, 14 Apr 2020 20:03:07 -0700 (PDT) X-Google-Smtp-Source: APiQypJhb84A0DM4ZAP1lc+tqanL1cNlZ2YnduNFGWQJhDq2Dvx276NUCaaZwLaoI7QBxgu6Hfoy X-Received: by 2002:a17:906:1e42:: with SMTP id i2mr2943982ejj.317.1586919787045; Tue, 14 Apr 2020 20:03:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586919787; cv=none; d=google.com; s=arc-20160816; b=tSIGSpk3wM9hM4bwN/91hVNq/nuJHWrWIzIIeys75oIpFrJOwHpIFp9bJ1KkzI7P/U Y4hw/yaczix3O28vkhQCxei2hLM8+rjHxJXw1qtprt3ZVoe19p4NG1iFepKwr5+q3AAz OvzfF0m+c+dAelXokEaJ2qYMD2ZmYx1mnQe6V1DX3lBBbRr5DWwHrG15SSlLuNep+JI/ DfoP0I5kFHSFxvgwaR2CQjwPOuCBEOcTEQCG1exBcVXebA5i5JIAKjK32F72Hq0oexuT kdAmqvjfkaP7Vdmtocox7Z670akDEmBXg2ijqInZFKhEVtg6tOPra0EVthr6wZ24QoyL 14XA== 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:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:reply-to; bh=UV3b21WSy27QOtX21x5hxRqZO9Iauj0+DH+5U+L+0zU=; b=zWp7EvkPub0Isf+wZ8GRZPmk0Bfb8PEOtbeU/JwOddAT73QbdUvvDP7erKTwjCGsqj RQhNVLgBnbAeEWjrQ3Zm3vERV6R+n14n8Rxp3WEp1s1bJw31S+nHViF2d73D5cZu0CV+ mhp+EMtlA05TJLtB+IfMpQpNke/ZBZm4Q3Ss4V8RW2PVfbtJJxy63c5gY+gjgTVcArAX B3vj4EN2UMrxDxNaxU5C2CZ2Xl+MHVYOVojvzKrfGizIGM0ZqYjOZH0o8SSjWd6fakyQ +zwBSu3CgHwrVAB8iG6TU6qXf0vzcXSdvyzVK6gLU/m0sgGCmiqZj6H89ArjvHbQwjfN 2gXA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for 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 hh22si9674783ejb.375.2020.04.14.20.02.43; Tue, 14 Apr 2020 20:03:07 -0700 (PDT) Received-SPF: pass (google.com: best guess record for 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: best guess record for 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 S2389515AbgDMWU5 (ORCPT + 99 others); Mon, 13 Apr 2020 18:20:57 -0400 Received: from smtp.infotech.no ([82.134.31.41]:42141 "EHLO smtp.infotech.no" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389509AbgDMWU5 (ORCPT ); Mon, 13 Apr 2020 18:20:57 -0400 X-Greylist: delayed 308 seconds by postgrey-1.27 at vger.kernel.org; Mon, 13 Apr 2020 18:20:56 EDT Received: from localhost (localhost [127.0.0.1]) by smtp.infotech.no (Postfix) with ESMTP id D5E8520425A; Tue, 14 Apr 2020 00:15:45 +0200 (CEST) X-Virus-Scanned: by amavisd-new-2.6.6 (20110518) (Debian) at infotech.no Received: from smtp.infotech.no ([127.0.0.1]) by localhost (smtp.infotech.no [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id bXtObVqnq5wF; Tue, 14 Apr 2020 00:15:40 +0200 (CEST) Received: from [192.168.48.23] (host-23-251-188-50.dyn.295.ca [23.251.188.50]) by smtp.infotech.no (Postfix) with ESMTPA id 0C172204155; Tue, 14 Apr 2020 00:15:38 +0200 (CEST) Reply-To: dgilbert@interlog.com Subject: Re: [PATCH] scsi: sg: fix memory leak in sg_build_indirect To: Li Bin , jejb@linux.ibm.com, martin.petersen@oracle.com Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, xiexiuqi@huawei.com References: <1586777552-17524-1-git-send-email-huawei.libin@huawei.com> From: Douglas Gilbert Message-ID: <8a11ba5c-3836-0d95-7f70-7dc32bda95c1@interlog.com> Date: Mon, 13 Apr 2020 18:15:29 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: <1586777552-17524-1-git-send-email-huawei.libin@huawei.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-CA Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020-04-13 7:32 a.m., Li Bin wrote: > Fix a memory leak when there have failed, that we should free the pages > under the condition rem_sz > 0. May I paraphrase the above: "Fix a memory leak that occurs when alloc_pages() succeeds several times before failing. This condition is noticed when rem_sz > 0." > > Signed-off-by: Li Bin > --- > drivers/scsi/sg.c | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c > index 4e6af592..8441ac5 100644 > --- a/drivers/scsi/sg.c > +++ b/drivers/scsi/sg.c > @@ -1959,8 +1959,12 @@ static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned lon It is the sg_build_indirect() function not sg_compat_ioctl() as suggested above by git. Can be get a replacement for git :-) > k, rem_sz)); > > schp->bufflen = blk_size; > - if (rem_sz > 0) /* must have failed */ > + if (rem_sz > 0) { /* must have failed */ > + for (i = 0; i < k; i++) > + __free_pages(schp->pages[i], order); > + > return -ENOMEM; It is easier, and less code, to replace 'return -ENOMEM'; with 'goto out'. Or even simpler: if (likely(rem_sz == 0)) return 0; out: ........ Doug Gilbert BTW I spotted this one during the sg driver rewrite and fixed it. Note that this bug and several others like it won't be fixed by me while the sg driver rewrite is pending. > + } > return 0; > out: > for (i = 0; i < k; i++) >