Commit 4965b8cd authored by Prateek Sood's avatar Prateek Sood Committed by Greg Kroah-Hartman

firmware_loader: fix memory leak for paged buffer

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: ddaf29fd ("firmware: Free temporary page table after vmapping")
Signed-off-by: default avatarPrateek Sood <prsood@codeaurora.org>
Reviewed-by: default avatarTakashi Iwai <tiwai@suse.de>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/1597957070-27185-1-git-send-email-prsood@codeaurora.orgSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent d012a719
...@@ -142,10 +142,12 @@ int assign_fw(struct firmware *fw, struct device *device, u32 opt_flags); ...@@ -142,10 +142,12 @@ int assign_fw(struct firmware *fw, struct device *device, u32 opt_flags);
void fw_free_paged_buf(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_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed);
int fw_map_paged_buf(struct fw_priv *fw_priv); int fw_map_paged_buf(struct fw_priv *fw_priv);
bool fw_is_paged_buf(struct fw_priv *fw_priv);
#else #else
static inline void fw_free_paged_buf(struct fw_priv *fw_priv) {} 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_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 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
#endif /* __FIRMWARE_LOADER_H */ #endif /* __FIRMWARE_LOADER_H */
...@@ -252,9 +252,11 @@ static void __free_fw_priv(struct kref *ref) ...@@ -252,9 +252,11 @@ static void __free_fw_priv(struct kref *ref)
list_del(&fw_priv->list); list_del(&fw_priv->list);
spin_unlock(&fwc->lock); spin_unlock(&fwc->lock);
fw_free_paged_buf(fw_priv); /* free leftover pages */ if (fw_is_paged_buf(fw_priv))
if (!fw_priv->allocated_size) fw_free_paged_buf(fw_priv);
else if (!fw_priv->allocated_size)
vfree(fw_priv->data); vfree(fw_priv->data);
kfree_const(fw_priv->fw_name); kfree_const(fw_priv->fw_name);
kfree(fw_priv); kfree(fw_priv);
} }
...@@ -268,6 +270,11 @@ static void free_fw_priv(struct fw_priv *fw_priv) ...@@ -268,6 +270,11 @@ static void free_fw_priv(struct fw_priv *fw_priv)
} }
#ifdef CONFIG_FW_LOADER_PAGED_BUF #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) void fw_free_paged_buf(struct fw_priv *fw_priv)
{ {
int i; int i;
...@@ -275,6 +282,8 @@ void fw_free_paged_buf(struct fw_priv *fw_priv) ...@@ -275,6 +282,8 @@ void fw_free_paged_buf(struct fw_priv *fw_priv)
if (!fw_priv->pages) if (!fw_priv->pages)
return; return;
vunmap(fw_priv->data);
for (i = 0; i < fw_priv->nr_pages; i++) for (i = 0; i < fw_priv->nr_pages; i++)
__free_page(fw_priv->pages[i]); __free_page(fw_priv->pages[i]);
kvfree(fw_priv->pages); kvfree(fw_priv->pages);
...@@ -328,10 +337,6 @@ int fw_map_paged_buf(struct fw_priv *fw_priv) ...@@ -328,10 +337,6 @@ int fw_map_paged_buf(struct fw_priv *fw_priv)
if (!fw_priv->data) if (!fw_priv->data)
return -ENOMEM; return -ENOMEM;
/* page table is no longer needed after mapping, let's free */
kvfree(fw_priv->pages);
fw_priv->pages = NULL;
return 0; return 0;
} }
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment