Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp4101585imw; Mon, 18 Jul 2022 22:44:08 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v0xGErlcosje2H4N0GPDGyKPZPefDhI7RWfXz31vwuHyL4HZi0fLsU/CRkBOi79aWROPn2 X-Received: by 2002:a17:907:7ba2:b0:72b:57da:a76e with SMTP id ne34-20020a1709077ba200b0072b57daa76emr28791679ejc.210.1658209448029; Mon, 18 Jul 2022 22:44:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658209448; cv=none; d=google.com; s=arc-20160816; b=YI17BFFoi5m/3emdrZqBk0QCCVDVkSJnEGkNFl+WIM8H8nYAv5Jh+dJpDNX4/lvKYa KSpp/ssv2TvpmGuxPIm7G/09HwYWYeZutZgMdt3VbgyBdHAIhhcFSjW12ZSAVkGtGy8f 0AehDYmS1zi12UaHcf0IGHWbyuMQIRgy2Y2LZR4FanSIEyy9bg/hEFklcfyrU2+ohteK IosB53m9Xnory1JPqORKkzG+IeR1HMn+rHOdubAdDbUemKb4KPd6XAN89gaGJLLRXHxP JzJ/2u9YICOJY/Qj9vKBGkOVxUdD/l5cYoBgQwKfgABjWPAC/7KfWAoXQd2w8aCkZLF9 Kv8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=olkGm6k7Mtxy3t1B47pdkVSdX9gcJkl0iHy4dDT9LjE=; b=oToANX5ekzOQ82T33jyw147TnTF3F/UviaraT1AYOYuxFupO+X34TVvXswzlpYWa1L 6xzVJb/2HcoNHyOiDFllRNPqrVCI0leY/nhqdmkXFWdSRumiTPOyOuf7RhQwOseBT5Xu /rc9ai8bWXY6fc2PlAphkeZzRjSyT/scU2ocKZsi3FjLnSeWa1FxVAEp338AmKuQnEgr LzPMTM6142HWesx7bdsX6yJU0RP4xSg12JyITjmU8FP4jzNYQ3xE0TLUmO1zYFATIrgK 7jPMu3FQEUwWGPiEaZLjwl/UKPyL35pXmd2JJ2iYdhX0lr0bu+wthgjWr2EWF3roZWtT fXRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="nIYNUuc/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z14-20020a05640235ce00b0043b27dc3b46si2164399edc.389.2022.07.18.22.43.43; Mon, 18 Jul 2022 22:44:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="nIYNUuc/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235686AbiGSF1R (ORCPT + 99 others); Tue, 19 Jul 2022 01:27:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235983AbiGSF0p (ORCPT ); Tue, 19 Jul 2022 01:26:45 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2DB1324BF1; Mon, 18 Jul 2022 22:26:45 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C092661591; Tue, 19 Jul 2022 05:26:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 772D4C341C6; Tue, 19 Jul 2022 05:26:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1658208404; bh=bkOVs9bpZgSJNv5tbjkfFp659Okff01K/tFuY0jID1o=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=nIYNUuc/R/Z+xsQEKAo39MfAzQ6+WyipoaD8wCW7zOgWZF1Ljh6IiOs5VtjKMeHUT ci1yg205y+mZIOK2phhu6a4T+5xtCWNb4pAQkJQNQhIT6cQxYXa3pzOxlk6kLbejYC o9DPyPc2htsQ5x6EPep0OLJWgcNfKYBk1pYvgAi4MJvEBmMqQ41zHciqd5KSJKCnQy rNH/6RRMJGxM1TgcTKN4JfUkKYhssnSszIuhkNaUlPikDyQZiwALSOKLhE3NmohQQT OYQuGHyyncAGHQV+2eIn7r01M8aMTifIq90B1RYzyRRJyr4ROr+VKTjyH8kwCShZfw CqVIslXwZt7Jg== Date: Tue, 19 Jul 2022 08:26:39 +0300 From: Leon Romanovsky To: Dennis Dalessandro Cc: Jianglei Nie , jgg@ziepe.ca, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] RDMA/hfi1: fix potential memory leak in setup_base_ctxt() Message-ID: References: <20220711070718.2318320-1-niejianglei2021@163.com> <1038e814-5f0d-17a3-1331-8ed24a64d597@cornelisnetworks.com> <62c8d684-6587-e560-6029-18fbe76ad8c4@cornelisnetworks.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <62c8d684-6587-e560-6029-18fbe76ad8c4@cornelisnetworks.com> X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 18, 2022 at 09:56:48AM -0400, Dennis Dalessandro wrote: > On 7/18/22 8:30 AM, Leon Romanovsky wrote: > > On Mon, Jul 18, 2022 at 08:11:59AM -0400, Dennis Dalessandro wrote: > >> On 7/18/22 6:39 AM, Leon Romanovsky wrote: > >>> On Mon, Jul 11, 2022 at 07:52:25AM -0400, Dennis Dalessandro wrote: > >>>> On 7/11/22 3:07 AM, Jianglei Nie wrote: > >>>>> setup_base_ctxt() allocates a memory chunk for uctxt->groups with > >>>>> hfi1_alloc_ctxt_rcv_groups(). When init_user_ctxt() fails, uctxt->groups > >>>>> is not released, which will lead to a memory leak. > >>>>> > >>>>> We should release the uctxt->groups with hfi1_free_ctxt_rcv_groups() > >>>>> when init_user_ctxt() fails. > >>>>> > >>>>> Signed-off-by: Jianglei Nie > >>>>> --- > >>>>> drivers/infiniband/hw/hfi1/file_ops.c | 4 +++- > >>>>> 1 file changed, 3 insertions(+), 1 deletion(-) > >>>>> > >>>>> diff --git a/drivers/infiniband/hw/hfi1/file_ops.c b/drivers/infiniband/hw/hfi1/file_ops.c > >>>>> index 2e4cf2b11653..629beff053ad 100644 > >>>>> --- a/drivers/infiniband/hw/hfi1/file_ops.c > >>>>> +++ b/drivers/infiniband/hw/hfi1/file_ops.c > >>>>> @@ -1179,8 +1179,10 @@ static int setup_base_ctxt(struct hfi1_filedata *fd, > >>>>> goto done; > >>>>> > >>>>> ret = init_user_ctxt(fd, uctxt); > >>>>> - if (ret) > >>>>> + if (ret) { > >>>>> + hfi1_free_ctxt_rcv_groups(uctxt); > >>>>> goto done; > >>>>> + } > >>>>> > >>>>> user_init(uctxt); > >>>>> > >>>> > >>>> Doesn't seem like this patch is correct. The free is done when the file is > >>>> closed, along with other clean up stuff. See hfi1_file_close(). > >>> > >>> Can setup_base_ctxt() be called twice for same uctxt? > >>> You are allocating rcd->groups and not releasing. > >> > >> The first thing assign_ctxt() does is a check of the fd->uctxt and it bails with > >> -EINVAL. So effectively only once. > > > > I'm slightly confused. How will you release rcd->groups? > > > > assign_ctxt() > > -> setup_base_ctxt() > > -> hfi1_alloc_ctxt_rcv_groups() > > ,,, > > rcd->groups = kzalloc... > > ... > > -> init_user_ctxt() <-- fails and leaves fd->uctx == NULL > > > > > > ... > > hfi1_file_close() > > struct hfi1_ctxtdata *uctxt = fdata->uctxt; > > ... > > if (!uctxt) <-- This is our case > > goto done; > > ... > > > > done: > > if (refcount_dec_and_test(&dd->user_refcount)) > > complete(&dd->user_comp); > > > > cleanup_srcu_struct(&fdata->pq_srcu); > > kfree(fdata); > > return 0; > > > > Looks like this may have been broken with: > > e87473bc1b6c ("IB/hfi1: Only set fd pointer when base context is completely > initialized") > > The question is does it make more sense to just move the fd->uctxt assignment > up, or call the free directly. I think that might be opening a bigger can of > worms though, as this was part of a larger patch set. Maybe it is best after all > to go with this patch. > > Let's add the above as a fixes line and tack on: > > Acked-by: Dennis Dalessandro > > It's been like this since 4.14, so no rush to get it in for the ultra late RC. > I'll get it tested as part of the next cycle. > Thanks, applied.