Skip to contentSkip to conversation

firmware_class: fix memory leak - free allocated pages

PATCH In review Tomas Winkler 3 participants 7 messages May 2, 2010 – May 4, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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

From: David Woodhouse <[email protected]>

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 <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Ming Lei <[email protected]>
Cc Catalin Marinas <[email protected]>

---
V2: fix authorship of the patch
V3: fix const struct firmware breakage

 drivers/base/firmware_class.c |   26 ++++++++++++++++++++------
 include/linux/firmware.h      |    1 +
 2 files changed, 21 insertions(+), 6 deletions(-)
drivers/base/firmware_class.c +20 -6
index 985da11..4c70b91 100644
@@ -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);
 	}
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.
#1 lore raw
Quoted 22 lines · Tomas Winkler

From: David Woodhouse <[email protected]>

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 <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Ming Lei <[email protected]>
Cc Catalin Marinas <[email protected]>
Quoted 3 lines

---
V2: fix authorship of the patch
V3: fix const struct firmware breakage

Thanks, this looks much better, I've applied it now.

greg k-h

#2 lore raw
Quoted 36 lines · Greg KH

From: Greg KH <[email protected]>
Subject: Re: [PATCH v3] firmware_class: fix memory leak - free allocated pages
To: "Tomas Winkler" <[email protected]>
Cc: "Greg Kroah-Hartman" <[email protected]>, [email protected], "David Woodhouse" <[email protected]>, "Kay Sievers" <[email protected]>, "David Woodhouse" <[email protected]>, "Johannes Berg" <[email protected]>, "Ming Lei" <[email protected]>, "Catalin Marinas" <[email protected]>
Date: Monday, 3 May, 2010, 10:34 PM

Quoted 6 lines · Tomas Winkler

From: David Woodhouse <[email protected]>

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

Quoted 1 line

using vmap. The memory has to be vunmap-ed and the

pages needs

Quoted 4 lines

to be freed explicitly

2. page array is moved into the 'struct
firmware' so that we can free it from

release_firmware()

Quoted 11 lines

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]>
Quoted 3 lines

---
V2: fix authorship of the patch
V3: fix const struct firmware breakage

Quoted 1 line

Thanks, this looks much better, I've applied it now.

Sorry to bump-in the middle.
But as a user of firmware class, what should now my fw_entry structure be? const struct firmware *fw_entry or,
struct firmware *fw_entry - without the const ..

Quoted 8 lines

greg k-h
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

#3 lore raw
Quoted 40 lines · Pavan Savoy
Quoted 36 lines · Greg KH

From: Greg KH <[email protected]>
Subject: Re: [PATCH v3] firmware_class: fix memory leak - free allocated pages
To: "Tomas Winkler" <[email protected]>
Cc: "Greg Kroah-Hartman" <[email protected]>, [email protected], "David Woodhouse" <[email protected]>, "Kay Sievers" <[email protected]>, "David Woodhouse" <[email protected]>, "Johannes Berg" <[email protected]>, "Ming Lei" <[email protected]>, "Catalin Marinas" <[email protected]>
Date: Monday, 3 May, 2010, 10:34 PM

Quoted 6 lines · Tomas Winkler

From: David Woodhouse <[email protected]>

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

Quoted 1 line

using vmap. The memory has to be vunmap-ed and the

pages needs

Quoted 4 lines

to be freed explicitly

2. page array is moved into the 'struct
firmware' so that we can free it from

release_firmware()

Quoted 14 lines

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]>

---
V2: fix authorship of the patch
V3: fix const struct firmware breakage

Quoted 6 lines
Quoted 1 line

Thanks, this looks much better, I've applied it now.

Sorry to bump-in the middle.
But as a user of firmware class, what should now my fw_entry structure be? const struct firmware *fw_entry or,
struct firmware *fw_entry - without the const ..

You should leave it to be whatever it was before this patch, it has not changed anything now.

thanks,

greg k-h

#4 lore raw
Quoted 65 lines · Greg KH

From: Greg KH <[email protected]>
Subject: Re: [PATCH v3] firmware_class: fix memory leak - free allocated pages
To: "Pavan Savoy" <[email protected]>
Cc: "Tomas Winkler" <[email protected]>, [email protected]
Date: Tuesday, 4 May, 2010, 7:33 PM

Quoted 3 lines · Pavan Savoy

--- On Mon, 3/5/10, Greg KH <[email protected]>

Quoted 3 lines · wrote:
Quoted 3 lines

From: Greg KH <[email protected]>
Subject: Re: [PATCH v3] firmware_class: fix

memory leak - free allocated pages

Quoted 2 lines
Quoted 2 lines

To: "Tomas Winkler" <[email protected]>
Cc: "Greg Kroah-Hartman" <[email protected]>,

[email protected],
"David Woodhouse" <[email protected]>,
"Kay Sievers" <[email protected]>,
"David Woodhouse" <[email protected]>,
"Johannes Berg" <[email protected]>,
"Ming Lei" <[email protected]>,
"Catalin Marinas" <[email protected]>

Quoted 6 lines
Quoted 6 lines

Date: Monday, 3 May, 2010, 10:34 PM

Quoted 3 lines · Tomas Winkler

From: David Woodhouse <[email protected]>

fix memory leak introduced by the patch

6e03a201bbe:

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

firmware: speed up request_firmware()

1. vfree won't release pages there were

allocated

Quoted 2 lines
Quoted 2 lines

explicitly and mapped

Quoted 1 line

using vmap. The memory has to be vunmap-ed

and the

Quoted 9 lines
Quoted 9 lines

pages needs

Quoted 4 lines

to be freed explicitly

2. page array is moved into the 'struct
firmware' so that we can free it from

release_firmware()

Quoted 3 lines

and not only in fw_dev_release()

The fix doesn't break the firmware load

speed.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Cc Johannes Berg <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Ming Lei <[email protected]>
Cc Catalin Marinas <[email protected]>

---
V2: fix authorship of the patch
V3: fix const struct firmware breakage

Quoted 1 line
Quoted 1 line

Thanks, this looks much better, I've applied it

now.

Quoted 3 lines

Sorry to bump-in the middle.
But as a user of firmware class, what should now my

Quoted 7 lines

fw_entry structure be?

Quoted 2 lines

const struct firmware *fw_entry or,
struct firmware *fw_entry - without the const ..

You should leave it to be whatever it was before this patch, it has not
changed anything now.

Ok, However this patch has changed the release_firmware, -release_firmware(const struct firmware *fw) +release_firmware(struct firmware *fw)

Quoted 4 lines

thanks,

greg k-h

#5 lore raw
Quoted 77 lines · Pavan Savoy
Quoted 65 lines · Greg KH

From: Greg KH <[email protected]>
Subject: Re: [PATCH v3] firmware_class: fix memory leak - free allocated pages
To: "Pavan Savoy" <[email protected]>
Cc: "Tomas Winkler" <[email protected]>, [email protected]
Date: Tuesday, 4 May, 2010, 7:33 PM

Quoted 3 lines · Pavan Savoy

--- On Mon, 3/5/10, Greg KH <[email protected]>

Quoted 3 lines · wrote:
Quoted 3 lines

From: Greg KH <[email protected]>
Subject: Re: [PATCH v3] firmware_class: fix

memory leak - free allocated pages

Quoted 2 lines
Quoted 2 lines

To: "Tomas Winkler" <[email protected]>
Cc: "Greg Kroah-Hartman" <[email protected]>,

[email protected],
"David Woodhouse" <[email protected]>,
"Kay Sievers" <[email protected]>,
"David Woodhouse" <[email protected]>,
"Johannes Berg" <[email protected]>,
"Ming Lei" <[email protected]>,
"Catalin Marinas" <[email protected]>

Quoted 6 lines
Quoted 6 lines

Date: Monday, 3 May, 2010, 10:34 PM

Quoted 3 lines · Tomas Winkler

From: David Woodhouse <[email protected]>

fix memory leak introduced by the patch

6e03a201bbe:

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

firmware: speed up request_firmware()

1. vfree won't release pages there were

allocated

Quoted 2 lines
Quoted 2 lines

explicitly and mapped

Quoted 1 line

using vmap. The memory has to be vunmap-ed

and the

Quoted 9 lines
Quoted 9 lines

pages needs

Quoted 4 lines

to be freed explicitly

2. page array is moved into the 'struct
firmware' so that we can free it from

release_firmware()

Quoted 3 lines

and not only in fw_dev_release()

The fix doesn't break the firmware load

speed.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Cc Johannes Berg <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Ming Lei <[email protected]>
Cc Catalin Marinas <[email protected]>

---
V2: fix authorship of the patch
V3: fix const struct firmware breakage

Quoted 1 line
Quoted 1 line

Thanks, this looks much better, I've applied it

now.

Quoted 3 lines

Sorry to bump-in the middle.
But as a user of firmware class, what should now my

Quoted 7 lines

fw_entry structure be?

Quoted 2 lines

const struct firmware *fw_entry or,
struct firmware *fw_entry - without the const ..

You should leave it to be whatever it was before this patch, it has not
changed anything now.

Ok, However this patch has changed the release_firmware,

Review

Quoted 2 lines
(quoted diff) +1 -1
-release_firmware(const struct firmware *fw)
+release_firmware(struct firmware *fw)

Does your code now complain about a warning? If so, do you have a pointer to it?

thanks,

greg k-h

#6 lore raw
Quoted 101 lines · Greg KH

From: Greg KH <[email protected]>
Subject: Re: [PATCH v3] firmware_class: fix memory leak - free allocated pages
To: "Pavan Savoy" <[email protected]>
Cc: "Tomas Winkler" <[email protected]>, [email protected]
Date: Tuesday, 4 May, 2010, 9:17 PM

Quoted 2 lines · Pavan Savoy

--- On Tue, 4/5/10, Greg KH <[email protected]>

Quoted 3 lines · wrote:
Quoted 3 lines

From: Greg KH <[email protected]>
Subject: Re: [PATCH v3] firmware_class: fix

memory leak - free allocated pages

Quoted 2 lines
Quoted 2 lines

To: "Pavan Savoy" <[email protected]>
Cc: "Tomas Winkler" <[email protected]>,

[email protected]

Quoted 10 lines
Quoted 10 lines

Date: Tuesday, 4 May, 2010, 7:33 PM

Quoted 3 lines · Pavan Savoy

--- On Mon, 3/5/10, Greg KH <[email protected]>

Quoted 3 lines · wrote:
Quoted 3 lines

From: Greg KH <[email protected]>
Subject: Re: [PATCH v3] firmware_class:

fix

Quoted 10 lines
Quoted 10 lines

memory leak - free allocated pages

Quoted 2 lines
Quoted 2 lines

To: "Tomas Winkler" <[email protected]>
Cc: "Greg Kroah-Hartman" <[email protected]>,

[email protected],
"David Woodhouse" <[email protected]>, "Kay Sievers" <[email protected]>, "David Woodhouse" <[email protected]>, "Johannes Berg" <[email protected]>, "Ming Lei" <[email protected]>, "Catalin Marinas" <[email protected]>

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Date: Monday, 3 May, 2010, 10:34 PM

Quoted 3 lines · Tomas Winkler

From: David Woodhouse <[email protected]>

fix memory leak introduced by the

patch

Quoted 2 lines
Quoted 2 lines

6e03a201bbe:

Quoted 1 line
Quoted 1 line
Quoted 1 line

firmware: speed up

request_firmware()

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

1. vfree won't release pages there

were

Quoted 3 lines
Quoted 3 lines

allocated

Quoted 2 lines
Quoted 2 lines

explicitly and mapped

Quoted 1 line

using vmap. The memory has to be

vunmap-ed

Quoted 5 lines
Quoted 5 lines

and the

Quoted 4 lines
Quoted 4 lines

pages needs

Quoted 3 lines

to be freed explicitly

2. page array is moved into the

'struct

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

firmware' so that we can free it

from

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

release_firmware()

Quoted 3 lines

and not only in fw_dev_release()

The fix doesn't break the firmware

load

Quoted 8 lines
Quoted 8 lines

speed.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Cc Johannes Berg <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Ming Lei <[email protected]>
Cc Catalin Marinas <[email protected]>
Signed-off-by David Woodhouse

<[email protected]>

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Signed-off-by Tomas Winkler

<[email protected]>

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

---
V2: fix authorship of the patch
V3: fix const struct firmware

breakage

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Thanks, this looks much better, I've

applied it

Quoted 4 lines
Quoted 4 lines

now.

Quoted 3 lines

Sorry to bump-in the middle.
But as a user of firmware class, what should

now my

Quoted 3 lines
Quoted 3 lines

fw_entry structure be?

Quoted 2 lines

const struct firmware *fw_entry or,
struct firmware *fw_entry - without the

const ..

Quoted 2 lines
Quoted 2 lines

You should leave it to be whatever it was before

this

Quoted 3 lines
Quoted 2 lines

patch, it has not
changed anything now.

Quoted 8 lines
Quoted 1 line

Ok, However this patch has changed the

release_firmware,

Review

Quoted 2 lines
(quoted diff) +1 -1
-release_firmware(const struct firmware *fw)
+release_firmware(struct firmware *fw)

Does your code now complain about a warning?  If so, do you have a
pointer to it?

Yes, code now has warnings, I have something like,
const struct firmware *fw_entry; declared in my header file,
and use it as,

            request_firmware(&kim_gdata->fw_entry, bts_scr_name,
                             &kim_gdata->kim_pdev->dev);
and then I get warnings at,
        release_firmware(kim_gdata->fw_entry);

You want me to have a pointer to &kw_gdata->fw_entry which is const ? as in
const struct firmware **fw_const_ptr;
fw_const_ptr = &kim_gdata->fw_entry; ?

Quoted 10 lines

thanks,

greg k-h
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/