binder: fix memory leak in binder_init()

Message ID 20230625154937.64316-1-qi.zheng@linux.dev
State New
Headers
Series binder: fix memory leak in binder_init() |

Commit Message

Qi Zheng June 25, 2023, 3:49 p.m. UTC
  From: Qi Zheng <zhengqi.arch@bytedance.com>

In binder_init(), the destruction of binder_alloc_shrinker_init() is not
performed in the wrong path, which will cause memory leaks. So this commit
introduces binder_alloc_shrinker_exit() and calls it in the wrong path to
fix that.

Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
---
 drivers/android/binder.c       | 1 +
 drivers/android/binder_alloc.c | 6 ++++++
 drivers/android/binder_alloc.h | 1 +
 3 files changed, 8 insertions(+)
  

Comments

Carlos Llamas June 25, 2023, 4:16 p.m. UTC | #1
On Sun, Jun 25, 2023 at 03:49:37PM +0000, Qi Zheng wrote:
> From: Qi Zheng <zhengqi.arch@bytedance.com>
> 
> In binder_init(), the destruction of binder_alloc_shrinker_init() is not
> performed in the wrong path, which will cause memory leaks. So this commit
> introduces binder_alloc_shrinker_exit() and calls it in the wrong path to
> fix that.
> 
> Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
> ---
>  drivers/android/binder.c       | 1 +
>  drivers/android/binder_alloc.c | 6 ++++++
>  drivers/android/binder_alloc.h | 1 +
>  3 files changed, 8 insertions(+)
> 
> diff --git a/drivers/android/binder.c b/drivers/android/binder.c
> index 486c8271cab7..d720f93d8b19 100644
> --- a/drivers/android/binder.c
> +++ b/drivers/android/binder.c
> @@ -6617,6 +6617,7 @@ static int __init binder_init(void)
>  
>  err_alloc_device_names_failed:
>  	debugfs_remove_recursive(binder_debugfs_dir_entry_root);
> +	binder_alloc_shrinker_exit();
>  
>  	return ret;
>  }
> diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c
> index 662a2a2e2e84..e3db8297095a 100644
> --- a/drivers/android/binder_alloc.c
> +++ b/drivers/android/binder_alloc.c
> @@ -1087,6 +1087,12 @@ int binder_alloc_shrinker_init(void)
>  	return ret;
>  }
>  
> +void binder_alloc_shrinker_exit(void)
> +{
> +	unregister_shrinker(&binder_shrinker);
> +	list_lru_destroy(&binder_alloc_lru);
> +}
> +
>  /**
>   * check_buffer() - verify that buffer/offset is safe to access
>   * @alloc: binder_alloc for this proc
> diff --git a/drivers/android/binder_alloc.h b/drivers/android/binder_alloc.h
> index 138d1d5af9ce..dc1e2b01dd64 100644
> --- a/drivers/android/binder_alloc.h
> +++ b/drivers/android/binder_alloc.h
> @@ -129,6 +129,7 @@ extern struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
>  						  int pid);
>  extern void binder_alloc_init(struct binder_alloc *alloc);
>  extern int binder_alloc_shrinker_init(void);
> +extern void binder_alloc_shrinker_exit(void);
>  extern void binder_alloc_vma_close(struct binder_alloc *alloc);
>  extern struct binder_buffer *
>  binder_alloc_prepare_to_free(struct binder_alloc *alloc,
> -- 
> 2.30.2
> 

Thanks for the fix Qi Zheng.

Acked-by: Carlos Llamas <cmllamas@google.com>
  
Qi Zheng June 26, 2023, 1:47 a.m. UTC | #2
On 2023/6/25 23:49, Qi Zheng wrote:
> From: Qi Zheng <zhengqi.arch@bytedance.com>
> 
> In binder_init(), the destruction of binder_alloc_shrinker_init() is not
> performed in the wrong path, which will cause memory leaks. So this commit
> introduces binder_alloc_shrinker_exit() and calls it in the wrong path to
> fix that.
> 
> Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>

Oh, I forgot to add Fixes tag:

Fixes: f2517eb76f1f ("android: binder: Add global lru shrinker to binder")

> ---
>   drivers/android/binder.c       | 1 +
>   drivers/android/binder_alloc.c | 6 ++++++
>   drivers/android/binder_alloc.h | 1 +
>   3 files changed, 8 insertions(+)
> 
> diff --git a/drivers/android/binder.c b/drivers/android/binder.c
> index 486c8271cab7..d720f93d8b19 100644
> --- a/drivers/android/binder.c
> +++ b/drivers/android/binder.c
> @@ -6617,6 +6617,7 @@ static int __init binder_init(void)
>   
>   err_alloc_device_names_failed:
>   	debugfs_remove_recursive(binder_debugfs_dir_entry_root);
> +	binder_alloc_shrinker_exit();
>   
>   	return ret;
>   }
> diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c
> index 662a2a2e2e84..e3db8297095a 100644
> --- a/drivers/android/binder_alloc.c
> +++ b/drivers/android/binder_alloc.c
> @@ -1087,6 +1087,12 @@ int binder_alloc_shrinker_init(void)
>   	return ret;
>   }
>   
> +void binder_alloc_shrinker_exit(void)
> +{
> +	unregister_shrinker(&binder_shrinker);
> +	list_lru_destroy(&binder_alloc_lru);
> +}
> +
>   /**
>    * check_buffer() - verify that buffer/offset is safe to access
>    * @alloc: binder_alloc for this proc
> diff --git a/drivers/android/binder_alloc.h b/drivers/android/binder_alloc.h
> index 138d1d5af9ce..dc1e2b01dd64 100644
> --- a/drivers/android/binder_alloc.h
> +++ b/drivers/android/binder_alloc.h
> @@ -129,6 +129,7 @@ extern struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
>   						  int pid);
>   extern void binder_alloc_init(struct binder_alloc *alloc);
>   extern int binder_alloc_shrinker_init(void);
> +extern void binder_alloc_shrinker_exit(void);
>   extern void binder_alloc_vma_close(struct binder_alloc *alloc);
>   extern struct binder_buffer *
>   binder_alloc_prepare_to_free(struct binder_alloc *alloc,
  
Qi Zheng July 21, 2023, 9:28 a.m. UTC | #3
On 2023/6/26 00:16, Carlos Llamas wrote:
> On Sun, Jun 25, 2023 at 03:49:37PM +0000, Qi Zheng wrote:
>> From: Qi Zheng <zhengqi.arch@bytedance.com>
>>
>> In binder_init(), the destruction of binder_alloc_shrinker_init() is not
>> performed in the wrong path, which will cause memory leaks. So this commit
>> introduces binder_alloc_shrinker_exit() and calls it in the wrong path to
>> fix that.
>>
>> Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
>> ---
>>   drivers/android/binder.c       | 1 +
>>   drivers/android/binder_alloc.c | 6 ++++++
>>   drivers/android/binder_alloc.h | 1 +
>>   3 files changed, 8 insertions(+)
>>
>> diff --git a/drivers/android/binder.c b/drivers/android/binder.c
>> index 486c8271cab7..d720f93d8b19 100644
>> --- a/drivers/android/binder.c
>> +++ b/drivers/android/binder.c
>> @@ -6617,6 +6617,7 @@ static int __init binder_init(void)
>>   
>>   err_alloc_device_names_failed:
>>   	debugfs_remove_recursive(binder_debugfs_dir_entry_root);
>> +	binder_alloc_shrinker_exit();
>>   
>>   	return ret;
>>   }
>> diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c
>> index 662a2a2e2e84..e3db8297095a 100644
>> --- a/drivers/android/binder_alloc.c
>> +++ b/drivers/android/binder_alloc.c
>> @@ -1087,6 +1087,12 @@ int binder_alloc_shrinker_init(void)
>>   	return ret;
>>   }
>>   
>> +void binder_alloc_shrinker_exit(void)
>> +{
>> +	unregister_shrinker(&binder_shrinker);
>> +	list_lru_destroy(&binder_alloc_lru);
>> +}
>> +
>>   /**
>>    * check_buffer() - verify that buffer/offset is safe to access
>>    * @alloc: binder_alloc for this proc
>> diff --git a/drivers/android/binder_alloc.h b/drivers/android/binder_alloc.h
>> index 138d1d5af9ce..dc1e2b01dd64 100644
>> --- a/drivers/android/binder_alloc.h
>> +++ b/drivers/android/binder_alloc.h
>> @@ -129,6 +129,7 @@ extern struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
>>   						  int pid);
>>   extern void binder_alloc_init(struct binder_alloc *alloc);
>>   extern int binder_alloc_shrinker_init(void);
>> +extern void binder_alloc_shrinker_exit(void);
>>   extern void binder_alloc_vma_close(struct binder_alloc *alloc);
>>   extern struct binder_buffer *
>>   binder_alloc_prepare_to_free(struct binder_alloc *alloc,
>> -- 
>> 2.30.2
>>
> 
> Thanks for the fix Qi Zheng.
> 
> Acked-by: Carlos Llamas <cmllamas@google.com>

Hi Greg,

Can this patch be merged? The Fixes tag is:

Fixes: f2517eb76f1f ("android: binder: Add global lru shrinker to binder")

Thanks,
Qi
  

Patch

diff --git a/drivers/android/binder.c b/drivers/android/binder.c
index 486c8271cab7..d720f93d8b19 100644
--- a/drivers/android/binder.c
+++ b/drivers/android/binder.c
@@ -6617,6 +6617,7 @@  static int __init binder_init(void)
 
 err_alloc_device_names_failed:
 	debugfs_remove_recursive(binder_debugfs_dir_entry_root);
+	binder_alloc_shrinker_exit();
 
 	return ret;
 }
diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c
index 662a2a2e2e84..e3db8297095a 100644
--- a/drivers/android/binder_alloc.c
+++ b/drivers/android/binder_alloc.c
@@ -1087,6 +1087,12 @@  int binder_alloc_shrinker_init(void)
 	return ret;
 }
 
+void binder_alloc_shrinker_exit(void)
+{
+	unregister_shrinker(&binder_shrinker);
+	list_lru_destroy(&binder_alloc_lru);
+}
+
 /**
  * check_buffer() - verify that buffer/offset is safe to access
  * @alloc: binder_alloc for this proc
diff --git a/drivers/android/binder_alloc.h b/drivers/android/binder_alloc.h
index 138d1d5af9ce..dc1e2b01dd64 100644
--- a/drivers/android/binder_alloc.h
+++ b/drivers/android/binder_alloc.h
@@ -129,6 +129,7 @@  extern struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
 						  int pid);
 extern void binder_alloc_init(struct binder_alloc *alloc);
 extern int binder_alloc_shrinker_init(void);
+extern void binder_alloc_shrinker_exit(void);
 extern void binder_alloc_vma_close(struct binder_alloc *alloc);
 extern struct binder_buffer *
 binder_alloc_prepare_to_free(struct binder_alloc *alloc,