Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1306444pxa; Thu, 13 Aug 2020 06:02:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwBle7F0lK72P0uvRn5WJ9LKG3W6c6D4FIKD8fegKnBjD5VIBGnQmPQ2ff65+qNBErp0VU X-Received: by 2002:a17:906:fc26:: with SMTP id ov38mr4383006ejb.99.1597323748536; Thu, 13 Aug 2020 06:02:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597323748; cv=none; d=google.com; s=arc-20160816; b=WCDNkoTDUAg+1WRyoqpAXxyo304pm1cRIxNnkw/bXckdPil1FgHwkSNlFHZAWprjUE SjqpxJKGQ7yvE3SZZ58UGCNTR5mAo+REgHF6THf5RCZqSiJYflPDMLvs1kk5z6R2ntpW oHOlyFpY161XgyEbFFnYHICERF1XzsiOiujS3uCJmD6r9kKv5RtzJ9YU2pts07heQYV/ xJBit0IXO49XwzDs22wJWwtPvTiezd1k2uWXn5wEwb6fXB3MRpRUHORoc3ihQSnVJSG2 Y51HaT65jQ2iR7tDxD7UrcePfRZTdfsORmvKVyyIycXJ4A1otUlE42Pzbb85+qWT2ED1 jgRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:subject:cc:to:from:message-id:date; bh=SLtFGpfOPt6z2FHbPTsgkf92LM93LsoUTBpbVMkkycg=; b=wkzt5m6UrwYGoopsBhdnLit6RI963pXqDgnYUKTe/gChj4/t8tMZpvcUgNnWQGUzED Fd1yU0NVCyy/T/TVj4AMDVgaWIvqgMOvtcmHesRE6RuXDIvGV5v+13JG3nZeqDZnhfc0 MuiAA4mQRkPi1kFxPfEnrgleU2AUYqwasWsbX72pqoQL+fvg5nW3x/z3qmiKdGS8xDd9 Ihf/gHZWBuHLi2facyabEUSUV7dWwxHpsDlwJzVPQp5yE6Q21+W4TVdU3Pn3dsXwoTQX qYikGR5OrSac+kKeS1y2HdX0EMNJA8v5pTf+QrSC6ZIz6eCHcVs9GJHzLiI53T/8rtCU LN3A== 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 p8si3137751ejf.352.2020.08.13.06.02.04; Thu, 13 Aug 2020 06:02:28 -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 S1726631AbgHMM6j (ORCPT + 99 others); Thu, 13 Aug 2020 08:58:39 -0400 Received: from mx2.suse.de ([195.135.220.15]:41954 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726102AbgHMM6i (ORCPT ); Thu, 13 Aug 2020 08:58:38 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 28504AC55; Thu, 13 Aug 2020 12:58:59 +0000 (UTC) Date: Thu, 13 Aug 2020 14:58:36 +0200 Message-ID: From: Takashi Iwai To: Prateek Sood Cc: mcgrof@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] firmware_loader: fix memory leak for paged buffer In-Reply-To: <1597258819-14080-1-git-send-email-prsood@codeaurora.org> References: <1597258819-14080-1-git-send-email-prsood@codeaurora.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/25.3 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 12 Aug 2020 21:00:19 +0200, Prateek Sood wrote: > > vfree() is being called on paged buffer allocated > using alloc_page() and mapped using vmap(). > > Freeing of pages in vfree() relies on nr_pages of > struct vm_struct. vmap() does not update nr_pages. > It can lead to memory leaks. > > Signed-off-by: Prateek Sood Thanks for spotting this out! This is essentially a revert of the commit ddaf29fd9bb6 ("firmware: Free temporary page table after vmapping"), so better to mention it via Fixes tag as well as Cc to stable. About the changes: > --- a/drivers/base/firmware_loader/firmware.h > +++ b/drivers/base/firmware_loader/firmware.h > @@ -142,10 +142,12 @@ static inline void fw_state_done(struct fw_priv *fw_priv) > void fw_free_paged_buf(struct fw_priv *fw_priv); > int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed); > int fw_map_paged_buf(struct fw_priv *fw_priv); > +bool fw_is_paged_buf(struct fw_priv *fw_priv); I guess this isn't necessary if we just swap the call order of fw_free_paged_buf() and vfree(); then fw_priv->is_paged_buf is referred only in fw_free_paged_buf(). That is, something like below. In anyway, take my review tag: Reviewed-by: Takashi Iwai thanks, Takashi --- a/drivers/base/firmware_loader/main.c +++ b/drivers/base/firmware_loader/main.c @@ -252,9 +252,9 @@ static void __free_fw_priv(struct kref *ref) list_del(&fw_priv->list); spin_unlock(&fwc->lock); - fw_free_paged_buf(fw_priv); /* free leftover pages */ if (!fw_priv->allocated_size) vfree(fw_priv->data); + fw_free_paged_buf(fw_priv); /* free leftover pages */ kfree_const(fw_priv->fw_name); kfree(fw_priv); } @@ -272,7 +272,7 @@ void fw_free_paged_buf(struct fw_priv *fw_priv) { int i; - if (!fw_priv->pages) + if (!fw_priv->is_paged_buf) return; for (i = 0; i < fw_priv->nr_pages; i++) @@ -328,10 +328,6 @@ int fw_map_paged_buf(struct fw_priv *fw_priv) if (!fw_priv->data) return -ENOMEM; - /* page table is no longer needed after mapping, let's free */ - kvfree(fw_priv->pages); - fw_priv->pages = NULL; - return 0; } #endif