diff mbox series

[v2,1/3] mm/shmem: check return value of shmem_init_inodecache

Message ID 20220605035557.3957759-2-chenwandun@huawei.com (mailing list archive)
State New
Headers show
Series a few cleanup and bugfixes about shmem | expand

Commit Message

Chen Wandun June 5, 2022, 3:55 a.m. UTC
It will result in null pointer access if shmem_init_inodecache fail,
so check return value of shmem_init_inodecache

Signed-off-by: Chen Wandun <chenwandun@huawei.com>
---
 mm/shmem.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

Comments

Matthew Wilcox June 5, 2022, 11:49 a.m. UTC | #1
On Sun, Jun 05, 2022 at 11:55:55AM +0800, Chen Wandun wrote:
> It will result in null pointer access if shmem_init_inodecache fail,
> so check return value of shmem_init_inodecache

You ignored my suggestion from v1.  Here, let me write it out for you.

+static int shmem_init_inodecache(void)
 {
  	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  				sizeof(struct shmem_inode_info),
  				0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
+	if (!shmem_inode_cachep)
+		return -ENOMEM;
+	return 0;
 }

...

+	error = shmem_init_inodecache();
+	if (error)
+		goto out2;
Chen Wandun June 6, 2022, 1:34 a.m. UTC | #2
On 2022/6/5 19:49, Matthew Wilcox wrote:
> On Sun, Jun 05, 2022 at 11:55:55AM +0800, Chen Wandun wrote:
>> It will result in null pointer access if shmem_init_inodecache fail,
>> so check return value of shmem_init_inodecache
> You ignored my suggestion from v1.  Here, let me write it out for you.
Hi Matthew,
I didn't ignore your suggestion,  some explanation is needed, sorry for 
that.

In V1, Kefeng point:
"kmem_cache_create return a pointer to the cache on success, NULL on 
failure,
so error = -ENOMEM; is right :)"

so, I look some similar code such as init_inodecache in kinds of file 
system,  they all
return -ENOMEM on failure, so is it OK to return -ENOMEM on failure :)

Besides,  kmem_cache_create return NULL on failure, maybe returning 
error code
on failure is more proper, but it is another job.
>
> +static int shmem_init_inodecache(void)
>   {
>    	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
>    				sizeof(struct shmem_inode_info),
>    				0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
> +	if (!shmem_inode_cachep)
> +		return -ENOMEM;
> +	return 0;
>   }
>
> ...
>
> +	error = shmem_init_inodecache();
> +	if (error)
> +		goto out2;
>
>
> .
Matthew Wilcox June 6, 2022, 2:08 a.m. UTC | #3
On Mon, Jun 06, 2022 at 09:34:13AM +0800, Chen Wandun wrote:
> On 2022/6/5 19:49, Matthew Wilcox wrote:
> > On Sun, Jun 05, 2022 at 11:55:55AM +0800, Chen Wandun wrote:
> > > It will result in null pointer access if shmem_init_inodecache fail,
> > > so check return value of shmem_init_inodecache
> > You ignored my suggestion from v1.  Here, let me write it out for you.
> Hi Matthew,
> I didn't ignore your suggestion,  some explanation is needed, sorry for
> that.
> 
> In V1, Kefeng point:
> "kmem_cache_create return a pointer to the cache on success, NULL on
> failure,
> so error = -ENOMEM; is right :)"
> 
> so, I look some similar code such as init_inodecache in kinds of file
> system,  they all
> return -ENOMEM on failure, so is it OK to return -ENOMEM on failure :)
> 
> Besides,  kmem_cache_create return NULL on failure, maybe returning error
> code
> on failure is more proper, but it is another job.

I literally wrote out what I think you should do instead.  Stop arguing.

> > +static int shmem_init_inodecache(void)
> >   {
> >    	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
> >    				sizeof(struct shmem_inode_info),
> >    				0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
> > +	if (!shmem_inode_cachep)
> > +		return -ENOMEM;
> > +	return 0;
> >   }
> > 
> > ...
> > 
> > +	error = shmem_init_inodecache();
> > +	if (error)
> > +		goto out2;
> > 
> > 
> > .
>
Chen Wandun June 6, 2022, 2:58 a.m. UTC | #4
在 2022/6/6 10:08, Matthew Wilcox 写道:
> On Mon, Jun 06, 2022 at 09:34:13AM +0800, Chen Wandun wrote:
>> On 2022/6/5 19:49, Matthew Wilcox wrote:
>>> On Sun, Jun 05, 2022 at 11:55:55AM +0800, Chen Wandun wrote:
>>>> It will result in null pointer access if shmem_init_inodecache fail,
>>>> so check return value of shmem_init_inodecache
>>> You ignored my suggestion from v1.  Here, let me write it out for you.
>> Hi Matthew,
>> I didn't ignore your suggestion,  some explanation is needed, sorry for
>> that.
>>
>> In V1, Kefeng point:
>> "kmem_cache_create return a pointer to the cache on success, NULL on
>> failure,
>> so error = -ENOMEM; is right :)"
>>
>> so, I look some similar code such as init_inodecache in kinds of file
>> system,  they all
>> return -ENOMEM on failure, so is it OK to return -ENOMEM on failure :)
>>
>> Besides,  kmem_cache_create return NULL on failure, maybe returning error
>> code
>> on failure is more proper, but it is another job.
> I literally wrote out what I think you should do instead.  Stop arguing.
>
>>> +static int shmem_init_inodecache(void)
>>>    {
>>>     	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
>>>     				sizeof(struct shmem_inode_info),
>>>     				0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
>>> +	if (!shmem_inode_cachep)
>>> +		return -ENOMEM;
>>> +	return 0;
>>>    }
>>>
>>> ...
>>>
>>> +	error = shmem_init_inodecache();
>>> +	if (error)
>>> +		goto out2;
Oh, I misunderstood what you said, feel so sorry.
I will send a new version.

Thanks.
>>>
>>>
>>> .
> .
diff mbox series

Patch

diff --git a/mm/shmem.c b/mm/shmem.c
index 12d45a03f7fc..17f8297ece29 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -3775,11 +3775,13 @@  static void shmem_init_inode(void *foo)
 	inode_init_once(&info->vfs_inode);
 }
 
-static void shmem_init_inodecache(void)
+static struct kmem_cache *shmem_init_inodecache(void)
 {
 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
 				sizeof(struct shmem_inode_info),
 				0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
+
+	return shmem_inode_cachep;
 }
 
 static void shmem_destroy_inodecache(void)
@@ -3923,7 +3925,10 @@  void __init shmem_init(void)
 {
 	int error;
 
-	shmem_init_inodecache();
+	if (!shmem_init_inodecache()) {
+		error = -ENOMEM;
+		goto out2;
+	}
 
 	error = register_filesystem(&shmem_fs_type);
 	if (error) {