Linux-EROFS Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] erofs: avoid useless memory allocation
@ 2023-03-03  7:52 Noboru Asai
  2023-03-03  7:59 ` Gao Xiang
  2023-03-03  9:45 ` Yue Hu
  0 siblings, 2 replies; 5+ messages in thread
From: Noboru Asai @ 2023-03-03  7:52 UTC (permalink / raw)
  To: xiang, chao, huyue2; +Cc: linux-erofs

The variable 'vi->xattr_shared_count' could be ZERO.

Signed-off-by: Noboru Asai <asai@sijam.com>
---
 fs/erofs/xattr.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c
index 60729b1220b6..5164813a693b 100644
--- a/fs/erofs/xattr.c
+++ b/fs/erofs/xattr.c
@@ -80,6 +80,8 @@ static int init_inode_xattrs(struct inode *inode)
 
 	ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
 	vi->xattr_shared_count = ih->h_shared_count;
+	if (!vi->xattr_shared_count)
+		goto out_unlock;
 	vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
 						sizeof(uint), GFP_KERNEL);
 	if (!vi->xattr_shared_xattrs) {
-- 
2.39.2


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

* Re: [PATCH] erofs: avoid useless memory allocation
  2023-03-03  7:52 [PATCH] erofs: avoid useless memory allocation Noboru Asai
@ 2023-03-03  7:59 ` Gao Xiang
  2023-03-03  9:45 ` Yue Hu
  1 sibling, 0 replies; 5+ messages in thread
From: Gao Xiang @ 2023-03-03  7:59 UTC (permalink / raw)
  To: Noboru Asai, xiang, chao, huyue2; +Cc: linux-erofs



On 2023/3/3 15:52, Noboru Asai wrote:
> The variable 'vi->xattr_shared_count' could be ZERO.
> 
> Signed-off-by: Noboru Asai <asai@sijam.com>

Reviewed-by: Gao Xiang <hsiangkao@linux.alibaba.com>

Thanks,
Gao Xiang

> ---
>   fs/erofs/xattr.c | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c
> index 60729b1220b6..5164813a693b 100644
> --- a/fs/erofs/xattr.c
> +++ b/fs/erofs/xattr.c
> @@ -80,6 +80,8 @@ static int init_inode_xattrs(struct inode *inode)
>   
>   	ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
>   	vi->xattr_shared_count = ih->h_shared_count;
> +	if (!vi->xattr_shared_count)
> +		goto out_unlock;
>   	vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
>   						sizeof(uint), GFP_KERNEL);
>   	if (!vi->xattr_shared_xattrs) {

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

* Re: [PATCH] erofs: avoid useless memory allocation
  2023-03-03  7:52 [PATCH] erofs: avoid useless memory allocation Noboru Asai
  2023-03-03  7:59 ` Gao Xiang
@ 2023-03-03  9:45 ` Yue Hu
  2023-03-03 10:06   ` Gao Xiang
  2023-03-03 10:14   ` 浅井登
  1 sibling, 2 replies; 5+ messages in thread
From: Yue Hu @ 2023-03-03  9:45 UTC (permalink / raw)
  To: Noboru Asai; +Cc: linux-erofs, huyue2, zhangwen

On Fri,  3 Mar 2023 16:52:18 +0900
Noboru Asai <asai@sijam.com> wrote:

> The variable 'vi->xattr_shared_count' could be ZERO.
> 
> Signed-off-by: Noboru Asai <asai@sijam.com>
> ---
>  fs/erofs/xattr.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c
> index 60729b1220b6..5164813a693b 100644
> --- a/fs/erofs/xattr.c
> +++ b/fs/erofs/xattr.c
> @@ -80,6 +80,8 @@ static int init_inode_xattrs(struct inode *inode)
>  
>  	ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
>  	vi->xattr_shared_count = ih->h_shared_count;
> +	if (!vi->xattr_shared_count)
> +		goto out_unlock;

Questions: ret = 0? no need to erofs_put_metabuf?

I think we can keep current since kmalloc_array() will check whether the
size(->xattr_shared_count) is zero size or not. rt?

>  	vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
>  						sizeof(uint), GFP_KERNEL);
>  	if (!vi->xattr_shared_xattrs) {


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

* Re: [PATCH] erofs: avoid useless memory allocation
  2023-03-03  9:45 ` Yue Hu
@ 2023-03-03 10:06   ` Gao Xiang
  2023-03-03 10:14   ` 浅井登
  1 sibling, 0 replies; 5+ messages in thread
From: Gao Xiang @ 2023-03-03 10:06 UTC (permalink / raw)
  To: Yue Hu, Noboru Asai; +Cc: linux-erofs, huyue2, zhangwen



On 2023/3/3 17:45, Yue Hu wrote:
> On Fri,  3 Mar 2023 16:52:18 +0900
> Noboru Asai <asai@sijam.com> wrote:
> 
>> The variable 'vi->xattr_shared_count' could be ZERO.
>>
>> Signed-off-by: Noboru Asai <asai@sijam.com>
>> ---
>>   fs/erofs/xattr.c | 2 ++
>>   1 file changed, 2 insertions(+)
>>
>> diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c
>> index 60729b1220b6..5164813a693b 100644
>> --- a/fs/erofs/xattr.c
>> +++ b/fs/erofs/xattr.c
>> @@ -80,6 +80,8 @@ static int init_inode_xattrs(struct inode *inode)
>>   
>>   	ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
>>   	vi->xattr_shared_count = ih->h_shared_count;
>> +	if (!vi->xattr_shared_count)
>> +		goto out_unlock;
> 
> Questions: ret = 0? no need to erofs_put_metabuf?
> 
> I think we can keep current since kmalloc_array() will check whether the
> size(->xattr_shared_count) is zero size or not. rt?

Personally I'd like to avoid 0-sized kmalloc, but you're right,
we have to do erofs_put_metabuf() here and it needs to be fixed.

> 
>>   	vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
>>   						sizeof(uint), GFP_KERNEL);
>>   	if (!vi->xattr_shared_xattrs) {

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

* Re: [PATCH] erofs: avoid useless memory allocation
  2023-03-03  9:45 ` Yue Hu
  2023-03-03 10:06   ` Gao Xiang
@ 2023-03-03 10:14   ` 浅井登
  1 sibling, 0 replies; 5+ messages in thread
From: 浅井登 @ 2023-03-03 10:14 UTC (permalink / raw)
  To: Yue Hu; +Cc: linux-erofs, huyue2, zhangwen

[-- Attachment #1: Type: text/plain, Size: 1422 bytes --]

2023年3月3日(金) 18:39 Yue Hu <zbestahu@gmail.com>:

> On Fri,  3 Mar 2023 16:52:18 +0900
> Noboru Asai <asai@sijam.com> wrote:
>
> > The variable 'vi->xattr_shared_count' could be ZERO.
> >
> > Signed-off-by: Noboru Asai <asai@sijam.com>
> > ---
> >  fs/erofs/xattr.c | 2 ++
> >  1 file changed, 2 insertions(+)
> >
> > diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c
> > index 60729b1220b6..5164813a693b 100644
> > --- a/fs/erofs/xattr.c
> > +++ b/fs/erofs/xattr.c
> > @@ -80,6 +80,8 @@ static int init_inode_xattrs(struct inode *inode)
> >
> >       ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
> >       vi->xattr_shared_count = ih->h_shared_count;
> > +     if (!vi->xattr_shared_count)
> > +             goto out_unlock;
>
> Questions: ret = 0? no need to erofs_put_metabuf?
>

You are right. this patch needs to call erofs_put_metabuf() before goto
out_unlock.


> I think we can keep current since kmalloc_array() will check whether the
> size(->xattr_shared_count) is zero size or not. rt?
>

In case of xattr_shared_count == 0, kmalloc_array() return ZERO_SIZE_PTR.
And kfree() do noting in this case (safe).

#define ZERO_SIZE_PTR ((void *)16)



> >       vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
> >                                               sizeof(uint), GFP_KERNEL);
> >       if (!vi->xattr_shared_xattrs) {
>
>

[-- Attachment #2: Type: text/html, Size: 3138 bytes --]

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

end of thread, other threads:[~2023-03-03 10:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-03  7:52 [PATCH] erofs: avoid useless memory allocation Noboru Asai
2023-03-03  7:59 ` Gao Xiang
2023-03-03  9:45 ` Yue Hu
2023-03-03 10:06   ` Gao Xiang
2023-03-03 10:14   ` 浅井登

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox