Skip to contentSkip to conversation

firmware_class: fix memory leak - free allocated pages

PATCH Superseded Tomas Winkler 2 participants 3 messages Apr 30, 2010 1 message not in the archive

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 firmware_class: fix memory leak - free allocated pages

fix memory leak introduced by 6e03a201bbe patch:
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 <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Ming Lei <[email protected]>
Cc Catalin Marinas <[email protected]>
---
 drivers/base/firmware_class.c |   28 ++++++++++++++++++++++------
 include/linux/firmware.h      |    1 +
 2 files changed, 23 insertions(+), 6 deletions(-)
drivers/base/firmware_class.c +22 -6
index 985da11..8200adc 100644
@@ -130,6 +130,20 @@ static ssize_t firmware_loading_show(struct device *dev,
 	return sprintf(buf, "%d\n", loading);
 }
 
+static void firmware_free_data(struct firmware *fw)
+{
+	int i;
+	vunmap(fw->data);
+	fw->data = NULL;
+	if (fw->pages) {
+		for (i = 0; i < PFN_UP(fw->size); i++)
+			__free_page(fw->pages[i]);
+		kfree(fw->pages);
+	}
+	fw->pages = NULL;
+	fw->size = 0;
+}
+
 /* Some architectures don't have PAGE_KERNEL_RO */
 #ifndef PAGE_KERNEL_RO
 #define PAGE_KERNEL_RO PAGE_KERNEL
@@ -162,21 +176,20 @@ 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);
+		/* 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 +197,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 +594,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);
 	}
include/linux/firmware.h +1 -0
index 043811f..53d1e6c 100644
@@ -12,6 +12,7 @@
 struct firmware {
 	size_t size;
 	const u8 *data;
+	struct page **pages;
 };
 
 struct device;
Signature · 9 lines
1.6.6.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 5 lines · Greg KH
Quoted 2 lines · Tomas Winkler

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

Who originally wrote this patch, Kay?

It's kind of a joint effort. I think it's probably mine by a fairly narrow majority.

Quoted 2 lines

If so, Tomas, please put a "From:" line at the top of the patch with
the author's name so we get it right when commiting it.

And don't make up Signed-off-by lines. Although you can have that one of mine now you've got it (since you've actually tested it).

Signature · 2 lines
David Woodhouse                            Open Source Technology Centre
[email protected]                              Intel Corporation
#3 lore raw
Quoted 17 lines

-----Original Message-----
From: David Woodhouse [mailto:[email protected]]
Sent: Thursday, April 29, 2010 8:54 PM
To: Greg KH
Cc: Winkler, Tomas; [email protected]; Johannes Berg; Ming Lei; Catalin Marinas
Subject: Re: [PATCH] firmware_class: fix memory leak - free allocated pages

Quoted 5 lines · Greg KH
Quoted 2 lines · Tomas Winkler

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

Who originally wrote this patch, Kay?

Quoted 8 lines

It's kind of a joint effort. I think it's probably mine by a fairly narrow majority.

Quoted 2 lines

If so, Tomas, please put a "From:" line at the top of the patch with
the author's name so we get it right when commiting it.

And don't make up Signed-off-by lines. Although you can have that one of mine now you've got it (since you've actually tested it).

I added firmware_free_data function to the original patch, would you mind to review it, please.

Thanks
Tomas

Quoted 4 lines

--
David Woodhouse Open Source Technology Centre
[email protected] Intel Corporation

---------------------------------------------------------------------Intel Israel (74) Limited
This e-mail and any attachments may contain confidential material forthe sole use of the intended recipient(s). Any review or distributionby others is strictly prohibited. If you are not the intendedrecipient, please contact the sender and delete all copies.���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�