2022-06-11 09:44:07

by Li Qiong

[permalink] [raw]
Subject: [PATCH] rcu: Handle failure of memory allocation functions

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

Signed-off-by: Li Qiong <[email protected]>
---
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


2022-06-11 16:50:43

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH] rcu: Handle failure of memory allocation functions

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 <[email protected]>

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
>

2022-06-12 06:41:24

by Li Qiong

[permalink] [raw]
Subject: Re: [PATCH] rcu: Handle failure of memory allocation functions



在 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 <[email protected]>
> 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
>>

2022-06-12 16:15:27

by Li Qiong

[permalink] [raw]
Subject: [PATCH v2] rcu: Handle failure of memory allocation functions

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

Signed-off-by: Li Qiong <[email protected]>
---
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

2022-06-14 00:12:15

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH v2] rcu: Handle failure of memory allocation functions

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 <[email protected]>

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 <[email protected]>
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 <[email protected]>
Signed-off-by: Paul E. McKenney <[email protected]>

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