x86/pat: fix memory leak in free_memtype
PATCH 1/1 x86/pat: fix memory leak in free_memtype
reserve_memtype will allocate memory for new memtype, but in free_memtype, after the memtype erased from rbtree, the memory is not freed.
--- arch/x86/mm/pat_rbtree.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/mm/pat_rbtree.c +2 -0
@@ -15,6 +15,7 @@#include <linux/rbtree.h> #include <linux/sched.h> #include <linux/gfp.h> +#include <linux/slab.h> #include <asm/pgtable.h> #include <asm/pat.h>
@@ -240,6 +241,7 @@ int rbt_memtype_erase(u64 start, u64 end)return -EINVAL ;rb_erase (&data->rb, &memtype_rbroot); +kfree (data);return 0 ; }
Signature · 1 line
1.7.0.1
Quoted 28 lines · Xiaotian Feng
reserve_memtypewill allocate memory for new memtype, but infree_memtype, after the memtype erased from rbtree, the memory is not freed.Signed-off-by Xiaotian Feng <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Venkatesh Pallipadi <[email protected]>Cc Jack Steiner <[email protected]>Cc Suresh Siddha <[email protected]>--- arch/x86/mm/pat_rbtree.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)arch/x86/mm/pat_rbtree.c +1
-0@@ -15,6 +15,7 @@#include <linux/rbtree.h> #include <linux/sched.h> #include <linux/gfp.h> +#include <linux/slab.h> #include <asm/pgtable.h> #include <asm/pat.h>
arch/x86/mm/pat_rbtree.c int rbt_memtype_erase(u64 start, u64 end) · line 246
Quoted 6 lines
arch/x86/mm/pat_rbtree.c +1
-0@@ -240,6 +241,7 @@ int rbt_memtype_erase(u64 start, u64 end)return -EINVAL ;rb_erase (&data->rb, &memtype_rbroot); +kfree (data);return 0 ; }
Peter, Please apply this for x86/urgent, this is introduced in the post 2.6.34 changes.
thanks,
suresh
Quoted 38 lines · Suresh Siddha
Quoted 28 lines · Xiaotian Feng
reserve_memtypewill allocate memory for new memtype, but infree_memtype, after the memtype erased from rbtree, the memory is not freed.Signed-off-by Xiaotian Feng <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Venkatesh Pallipadi <[email protected]>Cc Jack Steiner <[email protected]>Cc Suresh Siddha <[email protected]>--- arch/x86/mm/pat_rbtree.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)arch/x86/mm/pat_rbtree.c +1
-0@@ -15,6 +15,7 @@#include <linux/rbtree.h> #include <linux/sched.h> #include <linux/gfp.h> +#include <linux/slab.h> #include <asm/pgtable.h> #include <asm/pat.h> arch/x86/mm/pat_rbtree.c int rbt_memtype_erase(u64 start, u64 end) · line 246
Quoted 6 lines
arch/x86/mm/pat_rbtree.c +1
-0@@ -240,6 +241,7 @@ int rbt_memtype_erase(u64 start, u64 end)return -EINVAL ;rb_erase (&data->rb, &memtype_rbroot); +kfree (data);return 0 ; }Acked-by Suresh Siddha <[email protected]>
Quoted 2 lines
Peter, Please apply this for x86/urgent, this is introduced in the post 2.6.34 changes.
Xiaotian, Also I think more cleaner fix is to do this in free_memtype(). Can you please resend the patch with this change?
thanks,
suresh
PATCH v2 1/1 x86/pat: fix memory leak in free_memtype
In reply to Suresh Siddhareserve_memtype will allocate memory for new memtype, but in free_memtype, after the memtype erased from rbtree, the memory is not freed.
Changes since V1:
make rbt_memtype_erase return erased memtype so that it can be freed in free_memtype.
--- arch/x86/mm/pat.c | 10 +++++++--- arch/x86/mm/pat_internal.h | 6 +++--- arch/x86/mm/pat_rbtree.c | 7 ++++--- 3 files changed, 14 insertions(+), 9 deletions(-)
arch/x86/mm/pat.c +7 -3
@@ -336,6 +336,7 @@ int free_memtype(u64 start, u64 end) {int err = -EINVAL ;int is_range_ram; +struct memtype *entry;if (!pat_enabled)return 0 ;
@@ -355,17 +356,20 @@ int free_memtype(u64 start, u64 end) }spin_lock (&memtype_lock); - err =rbt_memtype_erase (start, end); + entry =rbt_memtype_erase (start, end);spin_unlock (&memtype_lock); -if (err) { +if (!entry) {printk (KERN_INFO "%s:%d freeing invalid memtype %Lx-%Lx\n" , current->comm, current->pid, start, end); +return -EINVAL ; } + +kfree (entry);dprintk ("free_memtype request 0x%Lx-0x%Lx\n" , start, end); -return err; +return 0 ; }
arch/x86/mm/pat_internal.h +3 -3
@@ -28,15 +28,15 @@ static inline char *cattr_name(unsigned long flags)#ifdef CONFIG_X86_PAT extern int rbt_memtype_check_insert (struct memtype *new,unsigned long *new_type); -extern int rbt_memtype_erase (u64 start,u64 end); +extern struct memtype *rbt_memtype_erase (u64 start,u64 end);extern struct memtype *rbt_memtype_lookup (u64 addr);extern int rbt_memtype_copy_nth_element (struct memtype *out,loff_t pos);#else static inline int rbt_memtype_check_insert (struct memtype *new,unsigned long *new_type) {return 0 ; } -static inline int rbt_memtype_erase (u64 start,u64 end) -{return 0 ; } +static inline struct memtype *rbt_memtype_erase (u64 start,u64 end) +{return NULL ; }static inline struct memtype *rbt_memtype_lookup (u64 addr) {return NULL ; }static inline int rbt_memtype_copy_nth_element (struct memtype *out,loff_t pos)
arch/x86/mm/pat_rbtree.c +4 -3
@@ -231,16 +231,17 @@ int rbt_memtype_check_insert(struct memtype *new, unsigned long *ret_type)return err; } -int rbt_memtype_erase (u64 start,u64 end) +struct memtype *rbt_memtype_erase (u64 start,u64 end) {struct memtype *data; data =memtype_rb_exact_match (&memtype_rbroot, start, end);if (!data) -return -EINVAL ; + goto out;rb_erase (&data->rb, &memtype_rbroot); -return 0 ; +out: +return data; }struct memtype *rbt_memtype_lookup (u64 addr)
Signature · 1 line
1.6.6.1
Quoted 8 lines · Xiaotian Feng
reserve_memtypewill allocate memory for new memtype, but infree_memtype, after the memtype erased from rbtree, the memory is not freed.Changes since V1:
makerbt_memtype_erasereturn erased memtype so that it can be freed infree_memtype.
Quoted 7 lines
Signed-off-by Xiaotian Feng <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Venkatesh Pallipadi <[email protected]>Cc Jack Steiner <[email protected]>Cc Suresh Siddha <[email protected]>
It looks bigger than I expected. But it is a bit cleaner (as both allocation/free happens in pat.c API)
Quoted 90 lines
--- arch/x86/mm/pat.c | 10 +++++++--- arch/x86/mm/pat_internal.h | 6 +++--- arch/x86/mm/pat_rbtree.c | 7 ++++--- 3 files changed, 14 insertions(+), 9 deletions(-)arch/x86/mm/pat.c +7
-3@@ -336,6 +336,7 @@ int free_memtype(u64 start, u64 end) {int err = -EINVAL ;int is_range_ram; +struct memtype *entry;if (!pat_enabled)return 0 ;@@ -355,17 +356,20 @@ int free_memtype(u64 start, u64 end) }spin_lock (&memtype_lock); - err =rbt_memtype_erase (start, end); + entry =rbt_memtype_erase (start, end);spin_unlock (&memtype_lock); -if (err) { +if (!entry) {printk (KERN_INFO "%s:%d freeing invalid memtype %Lx-%Lx\n" , current->comm, current->pid, start, end); +return -EINVAL ; } + +kfree (entry);dprintk ("free_memtype request 0x%Lx-0x%Lx\n" , start, end); -return err; +return 0 ; }arch/x86/mm/pat_internal.h +3
-3@@ -28,15 +28,15 @@ static inline char *cattr_name(unsigned long flags)#ifdef CONFIG_X86_PAT extern int rbt_memtype_check_insert (struct memtype *new,unsigned long *new_type); -extern int rbt_memtype_erase (u64 start,u64 end); +extern struct memtype *rbt_memtype_erase (u64 start,u64 end);extern struct memtype *rbt_memtype_lookup (u64 addr);extern int rbt_memtype_copy_nth_element (struct memtype *out,loff_t pos);#else static inline int rbt_memtype_check_insert (struct memtype *new,unsigned long *new_type) {return 0 ; } -static inline int rbt_memtype_erase (u64 start,u64 end) -{return 0 ; } +static inline struct memtype *rbt_memtype_erase (u64 start,u64 end) +{return NULL ; }static inline struct memtype *rbt_memtype_lookup (u64 addr) {return NULL ; }static inline int rbt_memtype_copy_nth_element (struct memtype *out,loff_t pos)arch/x86/mm/pat_rbtree.c +4
-3@@ -231,16 +231,17 @@ int rbt_memtype_check_insert(struct memtype *new, unsigned long *ret_type)return err; } -int rbt_memtype_erase (u64 start,u64 end) +struct memtype *rbt_memtype_erase (u64 start,u64 end) {struct memtype *data; data =memtype_rb_exact_match (&memtype_rbroot, start, end);if (!data) -return -EINVAL ; + goto out;rb_erase (&data->rb, &memtype_rbroot); -return 0 ; +out: +return data; }struct memtype *rbt_memtype_lookup (u64 addr)
[tip:x86/urgent] x86, pat: Fix memory leak in free_memtype
In reply to Xiaotian FengCommit-ID: 20413f27163fb1b8b806c0c219dc95eae67c633a Gitweb: http://git.kernel.org/tip/20413f27163fb1b8b806c0c219dc95eae67c633a Author: Xiaotian Feng <[email protected]> AuthorDate: Wed, 26 May 2010 09:51:10 +0800 Committer: H. Peter Anvin <[email protected]> CommitDate: Wed, 26 May 2010 11:26:04 -0700
x86, pat: Fix memory leak in free_memtype
Reserve_memtype will allocate memory for new memtype, but in free_memtype, after the memtype erased from rbtree, the memory is not freed.
Changes since V1:
make rbt_memtype_erase return erased memtype so that it can be freed in free_memtype.
[ hpa: not for -stable: 2.6.34 and earlier not affected ]
LKML-Reference: <[email protected]>
--- arch/x86/mm/pat.c | 10 +++++++--- arch/x86/mm/pat_internal.h | 6 +++--- arch/x86/mm/pat_rbtree.c | 7 ++++--- 3 files changed, 14 insertions(+), 9 deletions(-)
arch/x86/mm/pat.c +7 -3
@@ -336,6 +336,7 @@ int free_memtype(u64 start, u64 end) {int err = -EINVAL ;int is_range_ram; +struct memtype *entry;if (!pat_enabled)return 0 ;
@@ -355,17 +356,20 @@ int free_memtype(u64 start, u64 end) }spin_lock (&memtype_lock); - err =rbt_memtype_erase (start, end); + entry =rbt_memtype_erase (start, end);spin_unlock (&memtype_lock); -if (err) { +if (!entry) {printk (KERN_INFO "%s:%d freeing invalid memtype %Lx-%Lx\n" , current->comm, current->pid, start, end); +return -EINVAL ; } +kfree (entry); +dprintk ("free_memtype request 0x%Lx-0x%Lx\n" , start, end); -return err; +return 0 ; }
arch/x86/mm/pat_internal.h +3 -3
@@ -28,15 +28,15 @@ static inline char *cattr_name(unsigned long flags)#ifdef CONFIG_X86_PAT extern int rbt_memtype_check_insert (struct memtype *new,unsigned long *new_type); -extern int rbt_memtype_erase (u64 start,u64 end); +extern struct memtype *rbt_memtype_erase (u64 start,u64 end);extern struct memtype *rbt_memtype_lookup (u64 addr);extern int rbt_memtype_copy_nth_element (struct memtype *out,loff_t pos);#else static inline int rbt_memtype_check_insert (struct memtype *new,unsigned long *new_type) {return 0 ; } -static inline int rbt_memtype_erase (u64 start,u64 end) -{return 0 ; } +static inline struct memtype *rbt_memtype_erase (u64 start,u64 end) +{return NULL ; }static inline struct memtype *rbt_memtype_lookup (u64 addr) {return NULL ; }static inline int rbt_memtype_copy_nth_element (struct memtype *out,loff_t pos)
arch/x86/mm/pat_rbtree.c +4 -3
@@ -231,16 +231,17 @@ int rbt_memtype_check_insert(struct memtype *new, unsigned long *ret_type)return err; } -int rbt_memtype_erase (u64 start,u64 end) +struct memtype *rbt_memtype_erase (u64 start,u64 end) {struct memtype *data; data =memtype_rb_exact_match (&memtype_rbroot, start, end);if (!data) -return -EINVAL ; + goto out;rb_erase (&data->rb, &memtype_rbroot); -return 0 ; +out: +return data; }struct memtype *rbt_memtype_lookup (u64 addr)