public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@suse.de>
To: linux-kernel@vger.kernel.org
Cc: David Woodhouse <David.Woodhouse@intel.com>,
	Johannes Berg <johannes@sipsolutions.net>,
	Ming Lei <tom.leiming@gmail.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Tomas Winkler <tomas.winkler@intel.com>,
	Greg Kroah-Hartman <gregkh@suse.de>
Subject: [PATCH 02/38] firmware_class: fix memory leak - free allocated pages
Date: Fri, 21 May 2010 09:53:32 -0700	[thread overview]
Message-ID: <1274460848-11377-2-git-send-email-gregkh@suse.de> (raw)
In-Reply-To: <20100521165106.GA11216@kroah.com>

From: David Woodhouse <David.Woodhouse@intel.com>

fix memory leak introduced by the patch 6e03a201bbe:
firmware: speed up request_firmware()

1. vfree won't release pages there were allocated explicitly and mapped
using vmap. The memory has to be vunmap-ed and the pages needs
to be freed explicitly

2. page array is moved into the 'struct
firmware' so that we can free it from release_firmware()
and not only in fw_dev_release()

The fix doesn't break the firmware load speed.

Cc: Johannes Berg <johannes@sipsolutions.net>
Cc: Ming Lei <tom.leiming@gmail.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Singed-off-by: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
---
 drivers/base/firmware_class.c |   26 ++++++++++++++++++++------
 include/linux/firmware.h      |    1 +
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/drivers/base/firmware_class.c b/drivers/base/firmware_class.c
index 985da11..4c70b91 100644
--- a/drivers/base/firmware_class.c
+++ b/drivers/base/firmware_class.c
@@ -130,6 +130,17 @@ static ssize_t firmware_loading_show(struct device *dev,
 	return sprintf(buf, "%d\n", loading);
 }
 
+static void firmware_free_data(const struct firmware *fw)
+{
+	int i;
+	vunmap(fw->data);
+	if (fw->pages) {
+		for (i = 0; i < PFN_UP(fw->size); i++)
+			__free_page(fw->pages[i]);
+		kfree(fw->pages);
+	}
+}
+
 /* Some architectures don't have PAGE_KERNEL_RO */
 #ifndef PAGE_KERNEL_RO
 #define PAGE_KERNEL_RO PAGE_KERNEL
@@ -162,21 +173,21 @@ static ssize_t firmware_loading_store(struct device *dev,
 			mutex_unlock(&fw_lock);
 			break;
 		}
-		vfree(fw_priv->fw->data);
-		fw_priv->fw->data = NULL;
+		firmware_free_data(fw_priv->fw);
+		memset(fw_priv->fw, 0, sizeof(struct firmware));
+		/* If the pages are not owned by 'struct firmware' */
 		for (i = 0; i < fw_priv->nr_pages; i++)
 			__free_page(fw_priv->pages[i]);
 		kfree(fw_priv->pages);
 		fw_priv->pages = NULL;
 		fw_priv->page_array_size = 0;
 		fw_priv->nr_pages = 0;
-		fw_priv->fw->size = 0;
 		set_bit(FW_STATUS_LOADING, &fw_priv->status);
 		mutex_unlock(&fw_lock);
 		break;
 	case 0:
 		if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
-			vfree(fw_priv->fw->data);
+			vunmap(fw_priv->fw->data);
 			fw_priv->fw->data = vmap(fw_priv->pages,
 						 fw_priv->nr_pages,
 						 0, PAGE_KERNEL_RO);
@@ -184,7 +195,10 @@ static ssize_t firmware_loading_store(struct device *dev,
 				dev_err(dev, "%s: vmap() failed\n", __func__);
 				goto err;
 			}
-			/* Pages will be freed by vfree() */
+			/* Pages are now owned by 'struct firmware' */
+			fw_priv->fw->pages = fw_priv->pages;
+			fw_priv->pages = NULL;
+
 			fw_priv->page_array_size = 0;
 			fw_priv->nr_pages = 0;
 			complete(&fw_priv->completion);
@@ -578,7 +592,7 @@ release_firmware(const struct firmware *fw)
 			if (fw->data == builtin->data)
 				goto free_fw;
 		}
-		vfree(fw->data);
+		firmware_free_data(fw);
 	free_fw:
 		kfree(fw);
 	}
diff --git a/include/linux/firmware.h b/include/linux/firmware.h
index 043811f..53d1e6c 100644
--- a/include/linux/firmware.h
+++ b/include/linux/firmware.h
@@ -12,6 +12,7 @@
 struct firmware {
 	size_t size;
 	const u8 *data;
+	struct page **pages;
 };
 
 struct device;
-- 
1.7.0.3


  parent reply	other threads:[~2010-05-21 16:54 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-21 16:51 [GIT PATCH] driver core patches for .35 Greg KH
2010-05-21 16:53 ` [PATCH 01/38] drivers/base/cpu.c: fix the output from /sys/devices/system/cpu/offline Greg Kroah-Hartman
2010-05-21 16:53 ` Greg Kroah-Hartman [this message]
2010-05-21 16:53 ` [PATCH 03/38] kref: remove kref_set Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 04/38] Driver core: Reduce the level of request_firmware() messages Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 05/38] driver-core: fix potential race condition in drivers/base/dd.c Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 06/38] Driver core: don't initialize wakeup flags Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 07/38] driver core: module.c: Use kasprintf Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 08/38] devtmpfs: support !CONFIG_TMPFS Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 09/38] platform_bus: allow custom extensions to system PM methods Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 10/38] drivers/base: Convert dev->sem to mutex Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 11/38] lockdep: Add novalidate class for dev->mutex conversion Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 12/38] firmware class: export nowait to userspace Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 13/38] firmware loader: rely on driver core to create class attribute Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 14/38] firmware loader: split out builtin firmware handling Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 15/38] firmware loader: do not allocate firmare id separately Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 16/38] Driver core: Protect device shutdown from hot unplug events Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 17/38] generate "change" uevent for loop device Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 18/38] sysfs: Basic support for multiple super blocks Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 19/38] sysfs: Remove double free sysfs_get_sb Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 20/38] kobj: Add basic infrastructure for dealing with namespaces Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 21/38] sysfs: Implement sysfs tagged directory support Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 22/38] sysfs: Add support for tagged directories with untagged members Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 23/38] sysfs: Implement sysfs_delete_link Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 24/38] driver core: Implement ns directory support for device classes Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 25/38] sysfs: Comment sysfs directory tagging logic Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 26/38] sysfs-namespaces: add a high-level Documentation file Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 27/38] sysfs: Don't use enums in inline function declaration Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 28/38] sysfs: Remove usage of S_BIAS to avoid merge conflict with the vfs tree Greg Kroah-Hartman
2010-05-21 16:53 ` [PATCH 29/38] sysfs: add struct file* to bin_attr callbacks Greg Kroah-Hartman
2010-05-21 16:54 ` [PATCH 30/38] pci: check caps from sysfs file open to read device dependent config space Greg Kroah-Hartman
2010-05-21 16:54 ` [PATCH 31/38] driver-core: fix Typo in drivers/base/core.c for CONFIG_MODULE Greg Kroah-Hartman
2010-05-21 16:54 ` [PATCH 32/38] kobject: Send hotplug events in all network namespaces Greg Kroah-Hartman
2010-05-21 16:54 ` [PATCH 33/38] netns: Teach network device kobjects which namespace they are in Greg Kroah-Hartman
2010-05-21 16:54 ` [PATCH 34/38] net/sysfs: Fix the bitrot in network device kobject namespace support Greg Kroah-Hartman
2010-05-21 16:54 ` [PATCH 35/38] netlink: Implment netlink_broadcast_filtered Greg Kroah-Hartman
2010-05-21 16:54 ` [PATCH 36/38] kobj: Send hotplug events in the proper namespace Greg Kroah-Hartman
2010-05-21 16:54 ` [PATCH 37/38] hotplug: netns aware uevent_helper Greg Kroah-Hartman
2010-05-21 16:54 ` [PATCH 38/38] net: Expose all network devices in a namespaces in sysfs Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1274460848-11377-2-git-send-email-gregkh@suse.de \
    --to=gregkh@suse.de \
    --cc=David.Woodhouse@intel.com \
    --cc=catalin.marinas@arm.com \
    --cc=johannes@sipsolutions.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tom.leiming@gmail.com \
    --cc=tomas.winkler@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox