All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rcu: Handle failure of memory allocation functions
@ 2022-06-11  9:30 Li Qiong
  2022-06-11 16:34 ` Paul E. McKenney
  2022-06-12  6:48 ` [PATCH v2] " Li Qiong
  0 siblings, 2 replies; 5+ messages in thread
From: Li Qiong @ 2022-06-11  9:30 UTC (permalink / raw)
  To: Davidlohr Bueso, Paul E . McKenney, Josh Triplett,
	Frederic Weisbecker, Neeraj Upadhyay, Steven Rostedt,
	Mathieu Desnoyers, Lai Jiangshan, Joel Fernandes
  Cc: linux-kernel, rcu, hukun, qixu, yuzhe, renyu, Li Qiong

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(+)

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);
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] rcu: Handle failure of memory allocation functions
  2022-06-11  9:30 [PATCH] rcu: Handle failure of memory allocation functions Li Qiong
@ 2022-06-11 16:34 ` Paul E. McKenney
  2022-06-12  6:11   ` liqiong
  2022-06-12  6:48 ` [PATCH v2] " Li Qiong
  1 sibling, 1 reply; 5+ messages in thread
From: Paul E. McKenney @ 2022-06-11 16:34 UTC (permalink / raw)
  To: Li Qiong
  Cc: Davidlohr Bueso, Josh Triplett, Frederic Weisbecker,
	Neeraj Upadhyay, Steven Rostedt, Mathieu Desnoyers,
	Lai Jiangshan, Joel Fernandes, linux-kernel, rcu, hukun, qixu,
	yuzhe, renyu

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
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rcu: Handle failure of memory allocation functions
  2022-06-11 16:34 ` Paul E. McKenney
@ 2022-06-12  6:11   ` liqiong
  0 siblings, 0 replies; 5+ messages in thread
From: liqiong @ 2022-06-12  6:11 UTC (permalink / raw)
  To: paulmck
  Cc: Davidlohr Bueso, Josh Triplett, Frederic Weisbecker,
	Neeraj Upadhyay, Steven Rostedt, Mathieu Desnoyers,
	Lai Jiangshan, Joel Fernandes, linux-kernel, rcu, hukun, qixu,
	yuzhe, renyu



在 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
>>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] rcu: Handle failure of memory allocation functions
  2022-06-11  9:30 [PATCH] rcu: Handle failure of memory allocation functions Li Qiong
  2022-06-11 16:34 ` Paul E. McKenney
@ 2022-06-12  6:48 ` Li Qiong
  2022-06-13 23:47   ` Paul E. McKenney
  1 sibling, 1 reply; 5+ messages in thread
From: Li Qiong @ 2022-06-12  6:48 UTC (permalink / raw)
  To: Davidlohr Bueso, Josh Triplett, Frederic Weisbecker,
	Neeraj Upadhyay, Steven Rostedt, Lai Jiangshan, Joel Fernandes,
	paulmck
  Cc: linux-kernel, rcu, hukun, qixu, yuzhe, renyu, Li Qiong

Add warning when these functions (eg:kmalloc,vmalloc) fail, handle the
failure.

Signed-off-by: Li Qiong <liqiong@nfschina.com>
---
v2:
- check return value of kmem_cache_create().
---
 kernel/rcu/rcutorture.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
index 7120165a9342..104d2658a366 100644
--- a/kernel/rcu/rcutorture.c
+++ b/kernel/rcu/rcutorture.c
@@ -1990,7 +1990,13 @@ static void rcu_torture_mem_dump_obj(void)
 	static int z;
 
 	kcp = kmem_cache_create("rcuscale", 136, 8, SLAB_STORE_USER, NULL);
+	if (WARN_ON_ONCE(!kcp))
+		return;
 	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 +2013,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 +2022,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.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] rcu: Handle failure of memory allocation functions
  2022-06-12  6:48 ` [PATCH v2] " Li Qiong
@ 2022-06-13 23:47   ` Paul E. McKenney
  0 siblings, 0 replies; 5+ messages in thread
From: Paul E. McKenney @ 2022-06-13 23:47 UTC (permalink / raw)
  To: Li Qiong
  Cc: Davidlohr Bueso, Josh Triplett, Frederic Weisbecker,
	Neeraj Upadhyay, Steven Rostedt, Lai Jiangshan, Joel Fernandes,
	linux-kernel, rcu, hukun, qixu, yuzhe, renyu

On Sun, Jun 12, 2022 at 02:48:25PM +0800, Li Qiong wrote:
> Add warning when these functions (eg:kmalloc,vmalloc) fail, handle the
> failure.
> 
> Signed-off-by: Li Qiong <liqiong@nfschina.com>

Queued for review and testing with a bit of wordsmithing, thank you!

Please see below to check whether I messed something up.

							Thanx, Paul

------------------------------------------------------------------------

commit 19deffcffe66991c9a9e435ae90b16b97b83e4c1
Author: Li Qiong <liqiong@nfschina.com>
Date:   Sun Jun 12 14:48:25 2022 +0800

    rcu: Handle failure of memory allocation functions
    
    This commit adds warnings and ends the mem_dump_obj() test when allocation
    functions (kmalloc(), vmalloc(), ...) fail.
    
    Signed-off-by: Li Qiong <liqiong@nfschina.com>
    Signed-off-by: Paul E. McKenney <paulmck@kernel.org>

diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
index 21470ebb15eb4..ccc1af23dc1f2 100644
--- a/kernel/rcu/rcutorture.c
+++ b/kernel/rcu/rcutorture.c
@@ -2051,7 +2051,13 @@ static void rcu_torture_mem_dump_obj(void)
 	static int z;
 
 	kcp = kmem_cache_create("rcuscale", 136, 8, SLAB_STORE_USER, NULL);
+	if (WARN_ON_ONCE(!kcp))
+		return;
 	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);
@@ -2068,6 +2074,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);
@@ -2075,6 +2083,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);

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-06-13 23:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-11  9:30 [PATCH] rcu: Handle failure of memory allocation functions Li Qiong
2022-06-11 16:34 ` Paul E. McKenney
2022-06-12  6:11   ` liqiong
2022-06-12  6:48 ` [PATCH v2] " Li Qiong
2022-06-13 23:47   ` Paul E. McKenney

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.