Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2249495pxa; Mon, 17 Aug 2020 05:13:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxo26dwODcMUMYgL5Z8O5+w9v/K/iLrSdFgn8icntzBKqY+Vns55Xu82NRz6jXIle+n6pql X-Received: by 2002:a17:906:cb91:: with SMTP id mf17mr14553100ejb.527.1597666411884; Mon, 17 Aug 2020 05:13:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597666411; cv=none; d=google.com; s=arc-20160816; b=FUcCwhh3vWK3uDOSaJ+F0Yyyr92Y85w0EVqiam3uahYGkN2lMA57G1aUS4UJBMuC9/ FmLd49zV+jBhzwIsW90xM5XutOI+RdhyGxOxS2ncFm/OeStvdJ9O58SkOd/S5qOlFuyr aaKLh99LZkRHUh0SCWbi0ICIEcsdpr4KUSDZKpwUcYbYchu0FjDvOQR/pirqQjkWsOeG rSXfJny+srDPSra0ebL3Ab1ZvOYHbnbWyO3QWgEkvvqG9ilj0e78Fg5LyfG1mWQRI03D t7fAU4ZexzD88KhsDXX0AGECh6ZtfI4Qm+cFBTjg7PEAoRN8Wfxz0zQoL19aCbQ870+I s69A== 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=qMM+xDpirpnMiVO6HOp8Cjz2DHSi1YRqRwYPLKtjruM=; b=J56C8RHdX4FvQy66CkSUcSjfboHl+bJy1LEXC/BGGD9nc58BeP7Z8fUwyQ5EVg5sqg q2ZL9BcJdXqLkeEEGI1D8qq+UqM6wFgmt+r7l4TlONcP/eB7W6fOPg0B4/n6O6Il09D7 y5nGJH1PnXlgLxKc6zpWZM1OVXq1V5d69YuyJLdp+s30TICbUf5L6SPxIF11sjQWpFLO z9nKyKdhDoGlUaIr/+yGXJXWtJTlIrUUfLYv6pqsLli8Hc2NKZB/of0cnM1XkROhcGKy fiH0d0ubyp3VetC7wzjO7Ie8q+19g0qdjxrrJ5bKFIKwCXPwBq0aBDdrfNlx0GgWe6Ti N+aQ== 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 v18si11208617ejj.270.2020.08.17.05.13.08; Mon, 17 Aug 2020 05:13:31 -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 S1726980AbgHQMMK (ORCPT + 99 others); Mon, 17 Aug 2020 08:12:10 -0400 Received: from mx2.suse.de ([195.135.220.15]:56870 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726151AbgHQMMJ (ORCPT ); Mon, 17 Aug 2020 08:12:09 -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 2C782AD60; Mon, 17 Aug 2020 12:12:32 +0000 (UTC) Date: Mon, 17 Aug 2020 14:12:06 +0200 Message-ID: From: Takashi Iwai To: Prateek Sood Cc: mcgrof@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH v2] firmware_loader: fix memory leak for paged buffer In-Reply-To: <1597348822-17762-1-git-send-email-prsood@codeaurora.org> References: <3f9dac61-1ba5-1a44-1d08-75666c9398bc@codeaurora.org> <1597348822-17762-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, 13 Aug 2020 22:00:22 +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 Note that, for including to stable kernels properly, you'd need to put a Cc-to-stable line around the signed-off-by section in the patch itself. OTOH, you don't have to send the patch itself to stable when you submit it. 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. >