erofs: avoid useless memory allocation
PATCH 1/1 erofs: avoid useless memory allocation
The variable 'vi->xattr_shared_count' could be ZERO.
--- fs/erofs/xattr.c | 2 ++ 1 file changed, 2 insertions(+)
fs/erofs/xattr.c +2 -0
@@ -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) {
Signature · 1 line
2.39.2
Quoted 3 lines · Noboru Asai
The variable '
vi->xattr_shared_count' could be ZERO.Signed-off-by Noboru Asai <[email protected]>
Thanks,
Gao Xiang
Quoted 17 lines
--- fs/erofs/xattr.c | 2 ++ 1 file changed, 2 insertions(+)fs/erofs/xattr.c +2
-0@@ -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) {
Yue Hu
reviewerQuoted 12 lines · On Fri, 3 Mar 2023 16:52:18 +0900 Noboru Asai <[email protected]> wrote:
The variable '
vi->xattr_shared_count' could be ZERO.Signed-off-by Noboru Asai <[email protected]>--- fs/erofs/xattr.c | 2 ++ 1 file changed, 2 insertions(+)fs/erofs/xattr.c +0
-0
fs/erofs/xattr.c static int init_inode_xattrs(struct inode *inode) · line 84
Quoted 5 lines
fs/erofs/xattr.c +2
-0@@ -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?
Quoted 3 lines
fs/erofs/xattr.c +0
-0vi->xattr_shared_xattrs =kmalloc_array (vi->xattr_shared_count,sizeof (uint),GFP_KERNEL );if (!vi->xattr_shared_xattrs) {
Quoted 23 lines · Yue Hu
Quoted 12 lines · On Fri, 3 Mar 2023 16:52:18 +0900 Noboru Asai <[email protected]> wrote:
The variable '
vi->xattr_shared_count' could be ZERO.Signed-off-by Noboru Asai <[email protected]>--- fs/erofs/xattr.c | 2 ++ 1 file changed, 2 insertions(+)fs/erofs/xattr.c +0
-0fs/erofs/xattr.c static int init_inode_xattrs(struct inode *inode) · line 84
Quoted 5 lines
fs/erofs/xattr.c +2
-0@@ -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?
Quoted 2 lines
I think we can keep current since
kmalloc_array()will check whether thesize(->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.
Quoted 4 lines
Quoted 4 lines
fs/erofs/xattr.c +0
-0vi->xattr_shared_xattrs =kmalloc_array (vi->xattr_shared_count,sizeof (uint),GFP_KERNEL );if (!vi->xattr_shared_xattrs) {
2023年3月3日(金) 18:39 Yue Hu <[email protected]>:
Quoted 16 lines
Quoted 13 lines · On Fri, 3 Mar 2023 16:52:18 +0900 Noboru Asai <[email protected]> wrote:
The variable '
vi->xattr_shared_count' could be ZERO.Signed-off-by Noboru Asai <[email protected]>--- fs/erofs/xattr.c | 2 ++ 1 file changed, 2 insertions(+)fs/erofs/xattr.c +0
-0@@ -80,6 +80,8 @@ static int init_inode_xattrs(struct inode *inode)
Quoted 7 lines
fs/erofs/xattr.c static int init_inode_xattrs(struct inode *inode) · line 84
Quoted 4 lines
fs/erofs/xattr.c +2
-0ih = (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.
Quoted 3 lines
I think we can keep current since
kmalloc_array()will check whether thesize(->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)
Quoted 5 lines
Quoted 3 lines
fs/erofs/xattr.c +0
-0vi->xattr_shared_xattrs =kmalloc_array (vi->xattr_shared_count,sizeof (uint),GFP_KERNEL );if (!vi->xattr_shared_xattrs) {