diff mbox series

[1/4] mm/shmem: check return value of shmem_init_inodecache

Message ID 20220601124417.2872001-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 1, 2022, 12:44 p.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 1, 2022, 12:54 p.m. UTC | #1
On Wed, Jun 01, 2022 at 08:44:14PM +0800, Chen Wandun wrote:
> -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)
> @@ -3924,7 +3926,10 @@ void __init shmem_init(void)
>  {
>  	int error;
>  
> -	shmem_init_inodecache();
> +	if (!shmem_init_inodecache()) {
> +		error = -ENOMEM;
> +		goto out2;
> +	}

better to return the errno from shmem_init_inodecache():

	error = shmem_init_inodecache();
	if (error)
		goto out2;
Kefeng Wang June 1, 2022, 1:34 p.m. UTC | #2
On 2022/6/1 20:54, Matthew Wilcox wrote:
> On Wed, Jun 01, 2022 at 08:44:14PM +0800, Chen Wandun wrote:
>> -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)
>> @@ -3924,7 +3926,10 @@ void __init shmem_init(void)
>>   {
>>   	int error;
>>   
>> -	shmem_init_inodecache();
>> +	if (!shmem_init_inodecache()) {
>> +		error = -ENOMEM;
>> +		goto out2;
>> +	}
> better to return the errno from shmem_init_inodecache():
>
> 	error = shmem_init_inodecache();

kmem_cache_create return a pointer to the cache on success, NULL on failureļ¼Œ
so error = -ENOMEM; is right  :)

> 	if (error)
> 		goto out2;
>
> .
diff mbox series

Patch

diff --git a/mm/shmem.c b/mm/shmem.c
index d55dd972023a..80c361c3d82c 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -3776,11 +3776,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)
@@ -3924,7 +3926,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) {