Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1636400pxa; Thu, 20 Aug 2020 16:57:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzGkwVBB7yXbcCcWAidtFUjs7O7kftytmsYzF77Qlfi0870HAysoUmhj5AHoE1PPDOEpNRT X-Received: by 2002:a17:906:c7d3:: with SMTP id dc19mr214633ejb.495.1597967841813; Thu, 20 Aug 2020 16:57:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597967841; cv=none; d=google.com; s=arc-20160816; b=vM1cxDTDnzqiZWSQkF+cbl6Rll7cWojPj+mDzJVHn3TR1DpZbnW35dtomL6wvxJP9/ GovOrX509TLjNngm/nf/QpXwTvd7JMqlZEMi8MTQZiuoevLWqw+T4xeSxeFqUQ6/0Zjj AMYEQqRcEjCeTAq1MZR75aVRuIbxIhaS90Kg7ZTiwqL+lkeWeEQYNRA4qp7Pfgo7TQZX AQMnZqynoUtlVe3rGbIVV0wKXh8msRicFVb8WqDjwUdbdI3DoaWRVabE+XoMZ49qOOCR YpYYm9xwnqJBYkLfIHEmTwLafAIYKD6BKh4noDNvqHaJEC+gprjV30iRm/SHPE2HVila qguA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dmarc-filter:dkim-signature; bh=FJnUZ8hSOEwfDswhjf6p3X1DWgepHwpYrB7MhBJr5Ig=; b=ao9zDhMipVdHl8VQi4f7jbkOUhMzli71/xcVzbMFfMbOiHDKKdd331qIeXq4gLp3NE mu3sApvRbMWuXJVxEXFOLZ2tt+cZ7a/I+myM/1nvOpFIiD8mWXwobTfPSRLP7m8ytda4 wUa6NvRUFQRzR98bJ4HJ6Yq0qF15HBdSJo+KSMocEGMZNyjGcyM21EmtGA50uZtHOxkK mi5chqgO3OQ1O2KNXtVQwB5CLGcSZIv9jE/HYH6O7c64W1aC3nUBgyZkHV7/8zILmq3/ I6GPLORE78xPq4yQg54bixLzrxTkjDztvzwH4Za4Qlj8RU+DEtA3295F0mxq60ly6hkd 6jRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mg.codeaurora.org header.s=smtp header.b=GEbXAnjy; 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 gq5si20295ejb.162.2020.08.20.16.56.58; Thu, 20 Aug 2020 16:57:21 -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; dkim=fail header.i=@mg.codeaurora.org header.s=smtp header.b=GEbXAnjy; 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 S1728594AbgHTU7B (ORCPT + 99 others); Thu, 20 Aug 2020 16:59:01 -0400 Received: from m43-7.mailgun.net ([69.72.43.7]:19712 "EHLO m43-7.mailgun.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728255AbgHTU7A (ORCPT ); Thu, 20 Aug 2020 16:59:00 -0400 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1597957140; h=References: In-Reply-To: Message-Id: Date: Subject: Cc: To: From: Sender; bh=FJnUZ8hSOEwfDswhjf6p3X1DWgepHwpYrB7MhBJr5Ig=; b=GEbXAnjy8S9iqjwWM9XTXLkzF0xuYd9pvOFwSL+TbQsNIwZT1gcYOvSNbRJjzUEj1ICys0Lk he/7MntKdFS81M+G6zj7L8WtF5HZQw3WrLCHydaLLFqO6v4fJLlnyb93amcYsRTTNr1IzMlK voLDzi9dPgwqG560IC+K8cvoh7k= X-Mailgun-Sending-Ip: 69.72.43.7 X-Mailgun-Sid: WyI0MWYwYSIsICJsaW51eC1rZXJuZWxAdmdlci5rZXJuZWwub3JnIiwgImJlOWU0YSJd Received: from smtp.codeaurora.org (ec2-35-166-182-171.us-west-2.compute.amazonaws.com [35.166.182.171]) by smtp-out-n02.prod.us-east-1.postgun.com with SMTP id 5f3ee3d6f37da9fb0e87a338 (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Thu, 20 Aug 2020 20:57:58 GMT Received: by smtp.codeaurora.org (Postfix, from userid 1001) id 5E9DDC4339C; Thu, 20 Aug 2020 20:57:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-caf-mail-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=2.0 tests=ALL_TRUSTED,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.0 Received: from localhost (unknown [202.46.22.19]) (using TLSv1.2 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: prsood) by smtp.codeaurora.org (Postfix) with ESMTPSA id 9FBB1C433CB; Thu, 20 Aug 2020 20:57:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 9FBB1C433CB Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; spf=none smtp.mailfrom=prsood@codeaurora.org From: Prateek Sood To: tiwai@suse.de, mcgrof@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org, Prateek Sood Subject: [PATCH v3] firmware_loader: fix memory leak for paged buffer Date: Fri, 21 Aug 2020 02:27:50 +0530 Message-Id: <1597957070-27185-1-git-send-email-prsood@codeaurora.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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.