linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: check the function kmalloc_slab return value
@ 2022-06-13 10:24 Ren Yu
  2022-06-13 13:37 ` kernel test robot
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Ren Yu @ 2022-06-13 10:24 UTC (permalink / raw)
  To: cl
  Cc: penberg, rientjes, iamjoonsoo.kim, akpm, vbabka, roman.gushchin,
	42.hyeyoo, linux-mm, linux-kernel, liqiong, qixu, hukun, yuzhe,
	Ren Yu

As the possible failure of the kmalloc_slab,
it should be better to check it.

Signed-off-by: Ren Yu <renyu@nfschina.com>
---
 mm/slab.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/mm/slab.c b/mm/slab.c
index f8cd00f4ba13..72135e555827 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -2064,6 +2064,8 @@ int __kmem_cache_create(struct kmem_cache *cachep, slab_flags_t flags)
 	if (OFF_SLAB(cachep)) {
 		cachep->freelist_cache =
 			kmalloc_slab(cachep->freelist_size, 0u);
+		if (unlikely(ZERO_OR_NULL_PTR(cachep->freelist_cache)))
+			return cachep->freelist_cache;
 	}
 
 	err = setup_cpu_cache(cachep, gfp);
-- 
2.11.0


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

* Re: [PATCH] mm: check the function kmalloc_slab return value
  2022-06-13 10:24 [PATCH] mm: check the function kmalloc_slab return value Ren Yu
@ 2022-06-13 13:37 ` kernel test robot
  2022-06-14  8:39 ` Ren Yu
  2022-06-14  9:23 ` [PATCH v2] mm, slab: " Ren Yu
  2 siblings, 0 replies; 7+ messages in thread
From: kernel test robot @ 2022-06-13 13:37 UTC (permalink / raw)
  To: Ren Yu, cl
  Cc: kbuild-all, penberg, rientjes, iamjoonsoo.kim, akpm, vbabka,
	roman.gushchin, 42.hyeyoo, linux-mm, linux-kernel, liqiong, qixu,
	hukun, yuzhe, Ren Yu

Hi Ren,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Ren-Yu/mm-check-the-function-kmalloc_slab-return-value/20220613-182849
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
config: um-i386_defconfig (https://download.01.org/0day-ci/archive/20220613/202206132134.VtqdhH9v-lkp@intel.com/config)
compiler: gcc-11 (Debian 11.3.0-3) 11.3.0
reproduce (this is a W=1 build):
        # https://github.com/intel-lab-lkp/linux/commit/5ba2024be9e85177c986e9078e903798cac72f74
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Ren-Yu/mm-check-the-function-kmalloc_slab-return-value/20220613-182849
        git checkout 5ba2024be9e85177c986e9078e903798cac72f74
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 O=build_dir ARCH=um SUBARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

   mm/slab.c: In function '__kmem_cache_create':
>> mm/slab.c:2068:38: warning: returning 'struct kmem_cache *' from a function with return type 'int' makes integer from pointer without a cast [-Wint-conversion]
    2068 |                         return cachep->freelist_cache;
         |                                ~~~~~~^~~~~~~~~~~~~~~~


vim +2068 mm/slab.c

  2063	
  2064		if (OFF_SLAB(cachep)) {
  2065			cachep->freelist_cache =
  2066				kmalloc_slab(cachep->freelist_size, 0u);
  2067			if (unlikely(ZERO_OR_NULL_PTR(cachep->freelist_cache)))
> 2068				return cachep->freelist_cache;
  2069		}
  2070	
  2071		err = setup_cpu_cache(cachep, gfp);
  2072		if (err) {
  2073			__kmem_cache_release(cachep);
  2074			return err;
  2075		}
  2076	
  2077		return 0;
  2078	}
  2079	

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp

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

* [PATCH] mm: check the function kmalloc_slab return value
  2022-06-13 10:24 [PATCH] mm: check the function kmalloc_slab return value Ren Yu
  2022-06-13 13:37 ` kernel test robot
@ 2022-06-14  8:39 ` Ren Yu
  2022-06-14  8:48   ` Vlastimil Babka
  2022-06-14  9:23 ` [PATCH v2] mm, slab: " Ren Yu
  2 siblings, 1 reply; 7+ messages in thread
From: Ren Yu @ 2022-06-14  8:39 UTC (permalink / raw)
  To: cl
  Cc: penberg, rientjes, iamjoonsoo.kim, akpm, vbabka, roman.gushchin,
	42.hyeyoo, linux-mm, linux-kernel, liqiong, qixu, hukun, yuzhe,
	Ren Yu

As the possible failure of the kmalloc_slab,
it should be better to check it.

Signed-off-by: Ren Yu <renyu@nfschina.com>
Reported-by: kernel test robot <lkp@intel.com>
---
v2:
- fix build waring integer from pointer without a cast
---
---
 mm/slab.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/mm/slab.c b/mm/slab.c
index f8cd00f4ba13..72135e555827 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -2064,6 +2064,8 @@ int __kmem_cache_create(struct kmem_cache *cachep, slab_flags_t flags)
 	if (OFF_SLAB(cachep)) {
 		cachep->freelist_cache =
 			kmalloc_slab(cachep->freelist_size, 0u);
+		if (unlikely(ZERO_OR_NULL_PTR(cachep->freelist_cache)))
+			return cachep->freelist_cache;
 	}
 
 	err = setup_cpu_cache(cachep, gfp);
-- 
2.11.0


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

* Re: [PATCH] mm: check the function kmalloc_slab return value
  2022-06-14  8:39 ` Ren Yu
@ 2022-06-14  8:48   ` Vlastimil Babka
  2022-06-14  9:26     ` tury
  0 siblings, 1 reply; 7+ messages in thread
From: Vlastimil Babka @ 2022-06-14  8:48 UTC (permalink / raw)
  To: Ren Yu, cl
  Cc: penberg, rientjes, iamjoonsoo.kim, akpm, roman.gushchin,
	42.hyeyoo, linux-mm, linux-kernel, liqiong, qixu, hukun, yuzhe

On 6/14/22 10:39, Ren Yu wrote:
> As the possible failure of the kmalloc_slab,
> it should be better to check it.

AFAIK failure is not possible, kmalloc_slab() is not an allocation function,
it just returns a member of kmalloc_caches array, which is initialized
elsewhere and shouldn't contain NULLs. So the patch seems unnecessary to me.

> Signed-off-by: Ren Yu <renyu@nfschina.com>
> Reported-by: kernel test robot <lkp@intel.com>
> ---
> v2:
> - fix build waring integer from pointer without a cast
> ---
> ---
>  mm/slab.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/mm/slab.c b/mm/slab.c
> index f8cd00f4ba13..72135e555827 100644
> --- a/mm/slab.c
> +++ b/mm/slab.c
> @@ -2064,6 +2064,8 @@ int __kmem_cache_create(struct kmem_cache *cachep, slab_flags_t flags)
>  	if (OFF_SLAB(cachep)) {
>  		cachep->freelist_cache =
>  			kmalloc_slab(cachep->freelist_size, 0u);
> +		if (unlikely(ZERO_OR_NULL_PTR(cachep->freelist_cache)))

The usual way is "if (!cachep->freelist_cache)". Not sure why check for ZERO.

> +			return cachep->freelist_cache;

So in case of NULL this would return NULL, thus 0, but __kmem_cache_create()
return 0 on success, so it's wrong. You would have to return e.g. -ENOMEM.

>  	}
>  
>  	err = setup_cpu_cache(cachep, gfp);


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

* [PATCH v2] mm, slab: check the function kmalloc_slab return value
  2022-06-13 10:24 [PATCH] mm: check the function kmalloc_slab return value Ren Yu
  2022-06-13 13:37 ` kernel test robot
  2022-06-14  8:39 ` Ren Yu
@ 2022-06-14  9:23 ` Ren Yu
  2 siblings, 0 replies; 7+ messages in thread
From: Ren Yu @ 2022-06-14  9:23 UTC (permalink / raw)
  To: cl
  Cc: penberg, rientjes, iamjoonsoo.kim, akpm, vbabka, roman.gushchin,
	42.hyeyoo, linux-mm, linux-kernel, liqiong, qixu, hukun, yuzhe,
	Ren Yu

As the possible failure of the kmalloc_slab,
it should be better to check it.

Signed-off-by: Ren Yu <renyu@nfschina.com>
Reported-by: kernel test robot <lkp@intel.com>
---
v2:
- change return value of error path to '-ENOMEM'
- not check for zero
---
 mm/slab.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/mm/slab.c b/mm/slab.c
index f8cd00f4ba13..eb3fb042f4f4 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -2064,6 +2064,8 @@ int __kmem_cache_create(struct kmem_cache *cachep, slab_flags_t flags)
 	if (OFF_SLAB(cachep)) {
 		cachep->freelist_cache =
 			kmalloc_slab(cachep->freelist_size, 0u);
+		if (!cachep->freelist_cache)
+			return -ENOMEM;
 	}
 
 	err = setup_cpu_cache(cachep, gfp);
-- 
2.11.0


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

* Re: [PATCH] mm: check the function kmalloc_slab return value
  2022-06-14  8:48   ` Vlastimil Babka
@ 2022-06-14  9:26     ` tury
  2022-06-14 11:47       ` Vlastimil Babka
  0 siblings, 1 reply; 7+ messages in thread
From: tury @ 2022-06-14  9:26 UTC (permalink / raw)
  To: Vlastimil Babka, cl
  Cc: penberg, rientjes, iamjoonsoo.kim, akpm, roman.gushchin,
	42.hyeyoo, linux-mm, linux-kernel, liqiong, qixu, hukun, yuzhe



在 2022年06月14日 16:48, Vlastimil Babka 写道:
> On 6/14/22 10:39, Ren Yu wrote:
>> As the possible failure of the kmalloc_slab,
>> it should be better to check it.
> AFAIK failure is not possible, kmalloc_slab() is not an allocation function,
> it just returns a member of kmalloc_caches array, which is initialized
> elsewhere and shouldn't contain NULLs. So the patch seems unnecessary to me.
>
>> Signed-off-by: Ren Yu <renyu@nfschina.com>
>> Reported-by: kernel test robot <lkp@intel.com>
>> ---
>> v2:
>> - fix build waring integer from pointer without a cast
>> ---
>> ---
>>   mm/slab.c | 2 ++
>>   1 file changed, 2 insertions(+)
>>
>> diff --git a/mm/slab.c b/mm/slab.c
>> index f8cd00f4ba13..72135e555827 100644
>> --- a/mm/slab.c
>> +++ b/mm/slab.c
>> @@ -2064,6 +2064,8 @@ int __kmem_cache_create(struct kmem_cache *cachep, slab_flags_t flags)
>>   	if (OFF_SLAB(cachep)) {
>>   		cachep->freelist_cache =
>>   			kmalloc_slab(cachep->freelist_size, 0u);
>> +		if (unlikely(ZERO_OR_NULL_PTR(cachep->freelist_cache)))
> The usual way is "if (!cachep->freelist_cache)". Not sure why check for ZERO.
>
>> +			return cachep->freelist_cache;
> So in case of NULL this would return NULL, thus 0, but __kmem_cache_create()
> return 0 on success, so it's wrong. You would have to return e.g. -ENOMEM.
Thanks for the advice ,I'll be re-patching
>
>>   	}
>>   
>>   	err = setup_cpu_cache(cachep, gfp);
>


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

* Re: [PATCH] mm: check the function kmalloc_slab return value
  2022-06-14  9:26     ` tury
@ 2022-06-14 11:47       ` Vlastimil Babka
  0 siblings, 0 replies; 7+ messages in thread
From: Vlastimil Babka @ 2022-06-14 11:47 UTC (permalink / raw)
  To: tury, cl
  Cc: penberg, rientjes, iamjoonsoo.kim, akpm, roman.gushchin,
	42.hyeyoo, linux-mm, linux-kernel, liqiong, qixu, hukun, yuzhe

On 6/14/22 11:26, tury wrote:
> 
> 
> 在 2022年06月14日 16:48, Vlastimil Babka 写道:
>> On 6/14/22 10:39, Ren Yu wrote:
>>> As the possible failure of the kmalloc_slab,
>>> it should be better to check it.
>> AFAIK failure is not possible, kmalloc_slab() is not an allocation function,
>> it just returns a member of kmalloc_caches array, which is initialized
>> elsewhere and shouldn't contain NULLs. So the patch seems unnecessary to me.
>>
>>> Signed-off-by: Ren Yu <renyu@nfschina.com>
>>> Reported-by: kernel test robot <lkp@intel.com>
>>> ---
>>> v2:
>>> - fix build waring integer from pointer without a cast
>>> ---
>>> ---
>>>   mm/slab.c | 2 ++
>>>   1 file changed, 2 insertions(+)
>>>
>>> diff --git a/mm/slab.c b/mm/slab.c
>>> index f8cd00f4ba13..72135e555827 100644
>>> --- a/mm/slab.c
>>> +++ b/mm/slab.c
>>> @@ -2064,6 +2064,8 @@ int __kmem_cache_create(struct kmem_cache *cachep,
>>> slab_flags_t flags)
>>>       if (OFF_SLAB(cachep)) {
>>>           cachep->freelist_cache =
>>>               kmalloc_slab(cachep->freelist_size, 0u);
>>> +        if (unlikely(ZERO_OR_NULL_PTR(cachep->freelist_cache)))
>> The usual way is "if (!cachep->freelist_cache)". Not sure why check for ZERO.
>>
>>> +            return cachep->freelist_cache;
>> So in case of NULL this would return NULL, thus 0, but __kmem_cache_create()
>> return 0 on success, so it's wrong. You would have to return e.g. -ENOMEM.
> Thanks for the advice ,I'll be re-patching

However that was meant just for your information/learning, the patch is
still unecessary as I wrote above, so I will not merge it so we don't
complicate the code needlessly.

>>
>>>       }
>>>         err = setup_cpu_cache(cachep, gfp);
>>
> 


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

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

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-13 10:24 [PATCH] mm: check the function kmalloc_slab return value Ren Yu
2022-06-13 13:37 ` kernel test robot
2022-06-14  8:39 ` Ren Yu
2022-06-14  8:48   ` Vlastimil Babka
2022-06-14  9:26     ` tury
2022-06-14 11:47       ` Vlastimil Babka
2022-06-14  9:23 ` [PATCH v2] mm, slab: " Ren Yu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).