diff mbox series

rcu: Handle failure of memory allocation functions

Message ID 20220611093055.1473-1-liqiong@nfschina.com (mailing list archive)
State Superseded
Headers show
Series rcu: Handle failure of memory allocation functions | expand

Commit Message

Li Qiong June 11, 2022, 9:30 a.m. UTC
Add warning when these functions (eg:kmalloc,vmalloc) fail, handle the
failure.

Signed-off-by: Li Qiong <liqiong@nfschina.com>
---
 kernel/rcu/rcutorture.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Comments

Paul E. McKenney June 11, 2022, 4:34 p.m. UTC | #1
On Sat, Jun 11, 2022 at 05:30:55PM +0800, Li Qiong wrote:
> Add warning when these functions (eg:kmalloc,vmalloc) fail, handle the
> failure.
> 
> Signed-off-by: Li Qiong <liqiong@nfschina.com>

Good catch, thank you!  However...

> ---
>  kernel/rcu/rcutorture.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
> index 7120165a9342..97f90e304ae3 100644
> --- a/kernel/rcu/rcutorture.c
> +++ b/kernel/rcu/rcutorture.c
> @@ -1991,6 +1991,10 @@ static void rcu_torture_mem_dump_obj(void)
>  
>  	kcp = kmem_cache_create("rcuscale", 136, 8, SLAB_STORE_USER, NULL);

As long as we are checking, why not also check this one?

							Thanx, Paul

>  	rhp = kmem_cache_alloc(kcp, GFP_KERNEL);
> +	if (WARN_ON_ONCE(!rhp)) {
> +		kmem_cache_destroy(kcp);
> +		return;
> +	}
>  	pr_alert("mem_dump_obj() slab test: rcu_torture_stats = %px, &rhp = %px, rhp = %px, &z = %px\n", stats_task, &rhp, rhp, &z);
>  	pr_alert("mem_dump_obj(ZERO_SIZE_PTR):");
>  	mem_dump_obj(ZERO_SIZE_PTR);
> @@ -2007,6 +2011,8 @@ static void rcu_torture_mem_dump_obj(void)
>  	kmem_cache_free(kcp, rhp);
>  	kmem_cache_destroy(kcp);
>  	rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
> +	if (WARN_ON_ONCE(!rhp))
> +		return;
>  	pr_alert("mem_dump_obj() kmalloc test: rcu_torture_stats = %px, &rhp = %px, rhp = %px\n", stats_task, &rhp, rhp);
>  	pr_alert("mem_dump_obj(kmalloc %px):", rhp);
>  	mem_dump_obj(rhp);
> @@ -2014,6 +2020,8 @@ static void rcu_torture_mem_dump_obj(void)
>  	mem_dump_obj(&rhp->func);
>  	kfree(rhp);
>  	rhp = vmalloc(4096);
> +	if (WARN_ON_ONCE(!rhp))
> +		return;
>  	pr_alert("mem_dump_obj() vmalloc test: rcu_torture_stats = %px, &rhp = %px, rhp = %px\n", stats_task, &rhp, rhp);
>  	pr_alert("mem_dump_obj(vmalloc %px):", rhp);
>  	mem_dump_obj(rhp);
> -- 
> 2.11.0
>
Li Qiong June 12, 2022, 6:11 a.m. UTC | #2
在 2022年06月12日 00:34, Paul E. McKenney 写道:
> On Sat, Jun 11, 2022 at 05:30:55PM +0800, Li Qiong wrote:
>> Add warning when these functions (eg:kmalloc,vmalloc) fail, handle the
>> failure.
>>
>> Signed-off-by: Li Qiong <liqiong@nfschina.com>
> Good catch, thank you!  However...
>
>> ---
>>  kernel/rcu/rcutorture.c | 8 ++++++++
>>  1 file changed, 8 insertions(+)
>>
>> diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
>> index 7120165a9342..97f90e304ae3 100644
>> --- a/kernel/rcu/rcutorture.c
>> +++ b/kernel/rcu/rcutorture.c
>> @@ -1991,6 +1991,10 @@ static void rcu_torture_mem_dump_obj(void)
>>  
>>  	kcp = kmem_cache_create("rcuscale", 136, 8, SLAB_STORE_USER, NULL);
> As long as we are checking, why not also check this one?
>
> 							Thanx, Paul

Hi Paul,
Yes,  the kmem_cache_create  would fail too. 
I searched  "kernel" directory, found that It seems  all the code just check kmem_cache_alloc(),
So, I ignored   kmem_cache_create() .  I will submit a v2 patch.

Thanks,
Li Qiong


>
>>  	rhp = kmem_cache_alloc(kcp, GFP_KERNEL);
>> +	if (WARN_ON_ONCE(!rhp)) {
>> +		kmem_cache_destroy(kcp);
>> +		return;
>> +	}
>>  	pr_alert("mem_dump_obj() slab test: rcu_torture_stats = %px, &rhp = %px, rhp = %px, &z = %px\n", stats_task, &rhp, rhp, &z);
>>  	pr_alert("mem_dump_obj(ZERO_SIZE_PTR):");
>>  	mem_dump_obj(ZERO_SIZE_PTR);
>> @@ -2007,6 +2011,8 @@ static void rcu_torture_mem_dump_obj(void)
>>  	kmem_cache_free(kcp, rhp);
>>  	kmem_cache_destroy(kcp);
>>  	rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
>> +	if (WARN_ON_ONCE(!rhp))
>> +		return;
>>  	pr_alert("mem_dump_obj() kmalloc test: rcu_torture_stats = %px, &rhp = %px, rhp = %px\n", stats_task, &rhp, rhp);
>>  	pr_alert("mem_dump_obj(kmalloc %px):", rhp);
>>  	mem_dump_obj(rhp);
>> @@ -2014,6 +2020,8 @@ static void rcu_torture_mem_dump_obj(void)
>>  	mem_dump_obj(&rhp->func);
>>  	kfree(rhp);
>>  	rhp = vmalloc(4096);
>> +	if (WARN_ON_ONCE(!rhp))
>> +		return;
>>  	pr_alert("mem_dump_obj() vmalloc test: rcu_torture_stats = %px, &rhp = %px, rhp = %px\n", stats_task, &rhp, rhp);
>>  	pr_alert("mem_dump_obj(vmalloc %px):", rhp);
>>  	mem_dump_obj(rhp);
>> -- 
>> 2.11.0
>>
diff mbox series

Patch

diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
index 7120165a9342..97f90e304ae3 100644
--- a/kernel/rcu/rcutorture.c
+++ b/kernel/rcu/rcutorture.c
@@ -1991,6 +1991,10 @@  static void rcu_torture_mem_dump_obj(void)
 
 	kcp = kmem_cache_create("rcuscale", 136, 8, SLAB_STORE_USER, NULL);
 	rhp = kmem_cache_alloc(kcp, GFP_KERNEL);
+	if (WARN_ON_ONCE(!rhp)) {
+		kmem_cache_destroy(kcp);
+		return;
+	}
 	pr_alert("mem_dump_obj() slab test: rcu_torture_stats = %px, &rhp = %px, rhp = %px, &z = %px\n", stats_task, &rhp, rhp, &z);
 	pr_alert("mem_dump_obj(ZERO_SIZE_PTR):");
 	mem_dump_obj(ZERO_SIZE_PTR);
@@ -2007,6 +2011,8 @@  static void rcu_torture_mem_dump_obj(void)
 	kmem_cache_free(kcp, rhp);
 	kmem_cache_destroy(kcp);
 	rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
+	if (WARN_ON_ONCE(!rhp))
+		return;
 	pr_alert("mem_dump_obj() kmalloc test: rcu_torture_stats = %px, &rhp = %px, rhp = %px\n", stats_task, &rhp, rhp);
 	pr_alert("mem_dump_obj(kmalloc %px):", rhp);
 	mem_dump_obj(rhp);
@@ -2014,6 +2020,8 @@  static void rcu_torture_mem_dump_obj(void)
 	mem_dump_obj(&rhp->func);
 	kfree(rhp);
 	rhp = vmalloc(4096);
+	if (WARN_ON_ONCE(!rhp))
+		return;
 	pr_alert("mem_dump_obj() vmalloc test: rcu_torture_stats = %px, &rhp = %px, rhp = %px\n", stats_task, &rhp, rhp);
 	pr_alert("mem_dump_obj(vmalloc %px):", rhp);
 	mem_dump_obj(rhp);