Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp2042185rdb; Sun, 21 Jan 2024 05:09:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IFC52E3A5+5DvQbtprCiTwd3muayA1lH5S40Rh/D3ut7OjUb2BNsHRxMNgh+8v4KKJ0u83g X-Received: by 2002:a05:600c:4f96:b0:40e:4800:c91f with SMTP id n22-20020a05600c4f9600b0040e4800c91fmr1581857wmq.9.1705842595800; Sun, 21 Jan 2024 05:09:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705842595; cv=pass; d=google.com; s=arc-20160816; b=JzF7GshuDr36p/atSC94XX9IWczwfdOYhkKEn18jgy+9FH/AErnyQhMMxnQzUdp+eZ ISzlPkKQWWfpYsM2H1sqYAaSzHd5kM8lZ7FIaxsjYJo7348KsPHG8l3yB3qy1kmtLrpB GvrqrcTrf8egHzonDQ/oCa02c26e4Zu4qYkjxTSZVCBz5TwLy6Qr/g3kOmnqU5TpbyLk BBp2AeMCRZTqLZ/2vZA1yBE94R0rS6KZVe9ZrGsA29RmPq6EQdnB3X4anwN9nDvZbWxL LlXtdxatQbw3P33a6F10jGLU1BtQyk+fvZxnl7Gs8SSF6kWqZtaKIm0HO02jAS7mD22r N2lA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=AmAPYbUn8EDCZoHPZYtqCC6P1cRbechGMrzCmLCdLyw=; fh=AOcBsVm7+GzU/LQIAjmCtmnv6xbEvV527kX50E8cTCg=; b=XVdj5Nk9+N0DI3PdKzTzlQk2nlnDv9TzPuhhG9FNN1IHN7pGKG5iCdJ9W+DnXYydR6 LZAY3J+lDFt4ZkskytH4rhV3WJSIrfnYhuLxy9y/5Bi95cve7jesPu7p9fkKHVqBjgEG RNXYhPgPYVOXdgEliRXT14SKbiibLVAI368g24gz4upfmyWjWfletdIyiauFJ3+bIMM7 z6P7DA/v+guUaa2ix9XwzATGrseRZZr3qGYImjJDXSI/5M+zZ8Fo+vuOwvk/+TKmPdou Yt9oItE/51JwMSmGB3lbsBXr/tErsZR0/Mo3qTqfvQNXUcN3eHEYq9UfLyq9Q78aPGjQ VMGQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Yd3FOdmS; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-32062-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32062-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id m6-20020a056402510600b0055a55a806f8si3015553edd.478.2024.01.21.05.09.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 05:09:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-32062-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Yd3FOdmS; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-32062-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32062-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 877CA1F22C60 for ; Sun, 21 Jan 2024 13:09:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 759BB37170; Sun, 21 Jan 2024 13:09:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Yd3FOdmS" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9080C273FB; Sun, 21 Jan 2024 13:09:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705842585; cv=none; b=jHYO4Nn+MMNPizsCstC9YnVrwRg427CzR3A1/5Zzu8w3q6oyHQimBvyXdoInfEamCnoUgd75MjyMOU8dviFeZK1nDY5eDg3j2V/lACCvjLVd+6HxBIOjJiySLZysZ+NyQdO3L82NbWm5LdL95f8jfGqxZfkKsXTqnmRKakj5F+E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705842585; c=relaxed/simple; bh=9cyDx1F/dYPJrf8RZbUvqLxyHay+cmGJp31p+JZ2tBc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=NiDCQsa+Ox3ysxwcPsbDDurH0zqqDe+V72QHOzurHimcgFp3mJRW0x5V4XZaKMpZAzc6M0mdWoNBclarzxNiD7ktXzWsG/eJZa/VFht2TC+kSXKVn2xkIasyT3gkuQAY7yGyXn4e4g5GgtyAoN234Yi5upmK3Oo62G/gLLpE4dc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Yd3FOdmS; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 92A0FC433C7; Sun, 21 Jan 2024 13:09:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1705842585; bh=9cyDx1F/dYPJrf8RZbUvqLxyHay+cmGJp31p+JZ2tBc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Yd3FOdmSElZt7EwwlOyHijJCPPd8/8JTm/iNfsZcAFaJlWSUC7ysZMyGRPvUmMxsv L60jypo5eBKcMvi5iEl9IvUIWaAxmP3x6M3r+d4XUnlT/RMWeK93dvrHosPkKROUUb ZOkcpxfL2xXss9El/SxQTuICc1VV9/hvl9m9JHYYWjFkWujdXhT+ZPZozsH6rx5nEW U+3bqCJ1BRYppcvn76KfHmELqQgyUGAYmE1FA1W5u0P91BEASJ383RwsKMEXxet6D9 S/w3yQhIZwS0mrJEgkIJRIsCtZUvFufDAVmkWvUQfRRH769U6ggcFVOygT+/r5/P0A megt6AA8IgTwQ== Date: Sun, 21 Jan 2024 15:09:40 +0200 From: Leon Romanovsky To: Dennis Dalessandro Cc: Zhipeng Lu , Jason Gunthorpe , Ravi Krishnaswamy , Harish Chegondi , Brendan Cunningham , linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] IB/hfi1: fix a memleak in init_credit_return Message-ID: <20240121130940.GA7547@unreal> References: <20240112085523.3731720-1-alexious@zju.edu.cn> <20240114090434.GD6404@unreal> <28aeb877-c0b4-4236-87d5-0bbaeb185656@cornelisnetworks.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <28aeb877-c0b4-4236-87d5-0bbaeb185656@cornelisnetworks.com> On Thu, Jan 18, 2024 at 06:14:11PM -0500, Dennis Dalessandro wrote: > On 1/14/24 4:04 AM, Leon Romanovsky wrote: > > On Fri, Jan 12, 2024 at 04:55:23PM +0800, Zhipeng Lu wrote: > >> When dma_alloc_coherent fails to allocate dd->cr_base[i].va, > >> init_credit_return should deallocate dd->cr_base and > >> dd->cr_base[i] that allocated before. Or those resources > >> would be never freed and a memleak is triggered. > >> > >> Fixes: 7724105686e7 ("IB/hfi1: add driver files") > >> Signed-off-by: Zhipeng Lu > >> --- > >> drivers/infiniband/hw/hfi1/pio.c | 6 +++++- > >> 1 file changed, 5 insertions(+), 1 deletion(-) > >> > >> diff --git a/drivers/infiniband/hw/hfi1/pio.c b/drivers/infiniband/hw/hfi1/pio.c > >> index 68c621ff59d0..5a91cbda4aee 100644 > >> --- a/drivers/infiniband/hw/hfi1/pio.c > >> +++ b/drivers/infiniband/hw/hfi1/pio.c > >> @@ -2086,7 +2086,7 @@ int init_credit_return(struct hfi1_devdata *dd) > >> "Unable to allocate credit return DMA range for NUMA %d\n", > >> i); > >> ret = -ENOMEM; > >> - goto done; > >> + goto free_cr_base; > >> } > >> } > >> set_dev_node(&dd->pcidev->dev, dd->node); > >> @@ -2094,6 +2094,10 @@ int init_credit_return(struct hfi1_devdata *dd) > >> ret = 0; > >> done: > >> return ret; > >> + > >> +free_cr_base: > >> + free_credit_return(dd); > > > > Dennis, > > > > The idea of this patch is right, but it made me wonder, if > > free_credit_return() is correct. > > Yes, I've double checked the call path and if init_credit_return() fails we do > not call the free_credit_return(). > > So this patch: > > Acked-by: Dennis Dalessandro > > > > > > init_credit_return() iterates with help of for_each_node_with_cpus(): > > > > 2062 int init_credit_return(struct hfi1_devdata *dd) > > 2063 { > > ... > > 2075 for_each_node_with_cpus(i) { > > 2076 int bytes = TXE_NUM_CONTEXTS * sizeof(struct credit_return); > > 2077 > > > > But free_credit_return uses something else: > > 2099 void free_credit_return(struct hfi1_devdata *dd) > > 2100 { > > ... > > 2105 for (i = 0; i < node_affinity.num_possible_nodes; i++) { > > 2106 if (dd->cr_base[i].va) { > > > > Thanks > > > >> + goto done; > >> } > >> > >> void free_credit_return(struct hfi1_devdata *dd) > > I think we are OK because the allocation uses node_affinity.num_possible_nodes > and in free_credit_return() we walk that entire array and if something is > allocated we free it. > > Now why do we use for_each_node_with_cpus() at all? I believe that is because it > produces a subset of what is represented by num_possible_nodes(), which is OK > and doesn't leak anything. You are right, let's wait till merge window ends and we will apply this patch to rdma-rc. Thanks > > -Denny >