Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1982440pxa; Mon, 24 Aug 2020 01:20:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyPrkL/hlfPJ8JCS/zcUQZXS6CgmavpNs0T4LU5KPxM4mBmfth9ClTUvod9UpxA/3Zj/xj9 X-Received: by 2002:a17:906:3459:: with SMTP id d25mr4304250ejb.553.1598257258835; Mon, 24 Aug 2020 01:20:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598257258; cv=none; d=google.com; s=arc-20160816; b=TXR/jHzafERicmrPdfVeCuCGYf/9rrcFJul8mWhqk1jKStXQHNDTG/gLI0G9HGjYuU 4e2mlESnH05eFBnxk55ikhKtDa1B56BptaPAo9ybOl6o0NwiJ+csQ6jPLLD/NhoYXKJK FKMT/YZkD8sDsmfG8J/b4lej+ysuyV5bx+WITihJiKq0v7WjkYAx/5HtsXub/MfV+8c8 IHmKLx4mV7EJHSNQglS2ovX4BZaVsyr1BMw9Xel/g3zWZKqRDGds4x6TCZcvDNppIK5n /oEHTfyUDpqLLUmRTdjvmw6vPR4RGlRxZtW09tZ84xby8QB3a5tXtNSM7Q2QJ3MM+5tx 3Hyg== 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=V5JZS4mXmo5x18ybiMbgN6GqhN0fR/BJ66VZ0CQMfdY=; b=LfapoG5M3BSKWUKqidTwFLGD6VRC65SKmlOnIHOGsP4TdQC3CQr3KVdWKQtR5tRAML js48QzGapWSzfkv0jxYT39y9NoDgwEVIfo+GOrVESdb4YFTBew+LjwSYbYsODLsSBKi4 0SNHhkJWicrSaF0G5fgjSC9R8nq6RLi10O7tJfnWNkQaSc4nxD6BDBPCaBFpNhSOhHMN EXmTpH/DNOJSfqOt9LK2F4iOC3Hg2PHMfAx7BEyZ8IPboiOknEAl3wR7zQmVLXjEAiwf 1EgU+FQ0EfAb6cwb2Pc80dfwpq9UUdByxioc8xmwwp89qcFmI2oVOR/kZEqFiS39h1JG +iCw== 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 j14si6414806ejd.406.2020.08.24.01.20.35; Mon, 24 Aug 2020 01:20:58 -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 S1726218AbgHXITx (ORCPT + 99 others); Mon, 24 Aug 2020 04:19:53 -0400 Received: from mx2.suse.de ([195.135.220.15]:49158 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725780AbgHXITx (ORCPT ); Mon, 24 Aug 2020 04:19:53 -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 B73DBAC58; Mon, 24 Aug 2020 08:20:20 +0000 (UTC) Date: Mon, 24 Aug 2020 10:19:50 +0200 Message-ID: From: Takashi Iwai To: gregkh@linuxfoundation.org Cc: Prateek Sood , mcgrof@kernel.org, rafael@kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH v3] firmware_loader: fix memory leak for paged buffer In-Reply-To: <1597957070-27185-1-git-send-email-prsood@codeaurora.org> References: <1597957070-27185-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 Thu, 20 Aug 2020 22:57:50 +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. > > Fixes: ddaf29fd9bb6 ("firmware: Free temporary page table after vmapping") > Signed-off-by: Prateek Sood > Reviewed-by: Takashi Iwai > Cc: stable@vger.kernel.org Greg, could you review and merge this one please? thanks, Takashi > --- > drivers/base/firmware_loader/firmware.h | 2 ++ > drivers/base/firmware_loader/main.c | 17 +++++++++++------ > 2 files changed, 13 insertions(+), 6 deletions(-) > > diff --git a/drivers/base/firmware_loader/firmware.h b/drivers/base/firmware_loader/firmware.h > index 933e2192..d08efc7 100644 > --- 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); > #else > static inline void fw_free_paged_buf(struct fw_priv *fw_priv) {} > static inline int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed) { return -ENXIO; } > static inline int fw_map_paged_buf(struct fw_priv *fw_priv) { return -ENXIO; } > +static inline bool fw_is_paged_buf(struct fw_priv *fw_priv) { return false; } > #endif > > #endif /* __FIRMWARE_LOADER_H */ > diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_loader/main.c > index ca871b1..36bf455 100644 > --- a/drivers/base/firmware_loader/main.c > +++ b/drivers/base/firmware_loader/main.c > @@ -252,9 +252,11 @@ 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) > + if (fw_is_paged_buf(fw_priv)) > + fw_free_paged_buf(fw_priv); > + else if (!fw_priv->allocated_size) > vfree(fw_priv->data); > + > kfree_const(fw_priv->fw_name); > kfree(fw_priv); > } > @@ -268,6 +270,11 @@ static void free_fw_priv(struct fw_priv *fw_priv) > } > > #ifdef CONFIG_FW_LOADER_PAGED_BUF > +bool fw_is_paged_buf(struct fw_priv *fw_priv) > +{ > + return fw_priv->is_paged_buf; > +} > + > void fw_free_paged_buf(struct fw_priv *fw_priv) > { > int i; > @@ -275,6 +282,8 @@ void fw_free_paged_buf(struct fw_priv *fw_priv) > if (!fw_priv->pages) > return; > > + vunmap(fw_priv->data); > + > for (i = 0; i < fw_priv->nr_pages; i++) > __free_page(fw_priv->pages[i]); > kvfree(fw_priv->pages); > @@ -328,10 +337,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 > -- > Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc., > is a member of Code Aurora Forum, a Linux Foundation Collaborative Project. >