diff options
author | David Woodhouse <David.Woodhouse@intel.com> | 2010-05-02 04:21:21 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2010-05-21 12:37:28 -0400 |
commit | dd336c554d8926c3348a2d5f2a5ef5597f6d1a06 (patch) | |
tree | 457178c0d3189d6c3e02922aca36e528f4fc50a9 /drivers/base/firmware_class.c | |
parent | cdc6e3d3968052cebb2f2ddcd742bff29fbd1a90 (diff) |
firmware_class: fix memory leak - free allocated pages
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>
Diffstat (limited to 'drivers/base/firmware_class.c')
-rw-r--r-- | drivers/base/firmware_class.c | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/drivers/base/firmware_class.c b/drivers/base/firmware_class.c index 985da11174e7..4c70b9148b28 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, | |||
130 | return sprintf(buf, "%d\n", loading); | 130 | return sprintf(buf, "%d\n", loading); |
131 | } | 131 | } |
132 | 132 | ||
133 | static void firmware_free_data(const struct firmware *fw) | ||
134 | { | ||
135 | int i; | ||
136 | vunmap(fw->data); | ||
137 | if (fw->pages) { | ||
138 | for (i = 0; i < PFN_UP(fw->size); i++) | ||
139 | __free_page(fw->pages[i]); | ||
140 | kfree(fw->pages); | ||
141 | } | ||
142 | } | ||
143 | |||
133 | /* Some architectures don't have PAGE_KERNEL_RO */ | 144 | /* Some architectures don't have PAGE_KERNEL_RO */ |
134 | #ifndef PAGE_KERNEL_RO | 145 | #ifndef PAGE_KERNEL_RO |
135 | #define PAGE_KERNEL_RO PAGE_KERNEL | 146 | #define PAGE_KERNEL_RO PAGE_KERNEL |
@@ -162,21 +173,21 @@ static ssize_t firmware_loading_store(struct device *dev, | |||
162 | mutex_unlock(&fw_lock); | 173 | mutex_unlock(&fw_lock); |
163 | break; | 174 | break; |
164 | } | 175 | } |
165 | vfree(fw_priv->fw->data); | 176 | firmware_free_data(fw_priv->fw); |
166 | fw_priv->fw->data = NULL; | 177 | memset(fw_priv->fw, 0, sizeof(struct firmware)); |
178 | /* If the pages are not owned by 'struct firmware' */ | ||
167 | for (i = 0; i < fw_priv->nr_pages; i++) | 179 | for (i = 0; i < fw_priv->nr_pages; i++) |
168 | __free_page(fw_priv->pages[i]); | 180 | __free_page(fw_priv->pages[i]); |
169 | kfree(fw_priv->pages); | 181 | kfree(fw_priv->pages); |
170 | fw_priv->pages = NULL; | 182 | fw_priv->pages = NULL; |
171 | fw_priv->page_array_size = 0; | 183 | fw_priv->page_array_size = 0; |
172 | fw_priv->nr_pages = 0; | 184 | fw_priv->nr_pages = 0; |
173 | fw_priv->fw->size = 0; | ||
174 | set_bit(FW_STATUS_LOADING, &fw_priv->status); | 185 | set_bit(FW_STATUS_LOADING, &fw_priv->status); |
175 | mutex_unlock(&fw_lock); | 186 | mutex_unlock(&fw_lock); |
176 | break; | 187 | break; |
177 | case 0: | 188 | case 0: |
178 | if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) { | 189 | if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) { |
179 | vfree(fw_priv->fw->data); | 190 | vunmap(fw_priv->fw->data); |
180 | fw_priv->fw->data = vmap(fw_priv->pages, | 191 | fw_priv->fw->data = vmap(fw_priv->pages, |
181 | fw_priv->nr_pages, | 192 | fw_priv->nr_pages, |
182 | 0, PAGE_KERNEL_RO); | 193 | 0, PAGE_KERNEL_RO); |
@@ -184,7 +195,10 @@ static ssize_t firmware_loading_store(struct device *dev, | |||
184 | dev_err(dev, "%s: vmap() failed\n", __func__); | 195 | dev_err(dev, "%s: vmap() failed\n", __func__); |
185 | goto err; | 196 | goto err; |
186 | } | 197 | } |
187 | /* Pages will be freed by vfree() */ | 198 | /* Pages are now owned by 'struct firmware' */ |
199 | fw_priv->fw->pages = fw_priv->pages; | ||
200 | fw_priv->pages = NULL; | ||
201 | |||
188 | fw_priv->page_array_size = 0; | 202 | fw_priv->page_array_size = 0; |
189 | fw_priv->nr_pages = 0; | 203 | fw_priv->nr_pages = 0; |
190 | complete(&fw_priv->completion); | 204 | complete(&fw_priv->completion); |
@@ -578,7 +592,7 @@ release_firmware(const struct firmware *fw) | |||
578 | if (fw->data == builtin->data) | 592 | if (fw->data == builtin->data) |
579 | goto free_fw; | 593 | goto free_fw; |
580 | } | 594 | } |
581 | vfree(fw->data); | 595 | firmware_free_data(fw); |
582 | free_fw: | 596 | free_fw: |
583 | kfree(fw); | 597 | kfree(fw); |
584 | } | 598 | } |