Skip to contentSkip to conversation

erofs: avoid useless memory allocation

PATCH Superseded Noboru Asai 3 participants 5 messages Mar 3, 2023 EROFS file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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
index 60729b1220b6..5164813a693b 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Noboru Asai

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

Thanks,
Gao Xiang

Quoted 17 lines
---
  fs/erofs/xattr.c | 2 ++
  1 file changed, 2 insertions(+)
fs/erofs/xattr.c +2 -0
index 60729b1220b6..5164813a693b 100644
@@ -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) {
reviewer
#2 lore raw
In reply to Noboru Asai
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.

---
 fs/erofs/xattr.c | 2 ++
 1 file changed, 2 insertions(+)
fs/erofs/xattr.c +0 -0
index 60729b1220b6..5164813a693b 100644

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 -0
 	vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
 						sizeof(uint), GFP_KERNEL);
 	if (!vi->xattr_shared_xattrs) {
#3 lore raw
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.

---
  fs/erofs/xattr.c | 2 ++
  1 file changed, 2 insertions(+)
fs/erofs/xattr.c +0 -0
index 60729b1220b6..5164813a693b 100644

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?

Quoted 2 lines

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.

Quoted 4 lines
Quoted 4 lines
fs/erofs/xattr.c +0 -0
  	vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
  						sizeof(uint), GFP_KERNEL);
  	if (!vi->xattr_shared_xattrs) {
author
#4 lore raw
In reply to Yue Hu

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.

---
 fs/erofs/xattr.c | 2 ++
 1 file changed, 2 insertions(+)
fs/erofs/xattr.c +0 -0
index 60729b1220b6..5164813a693b 100644
@@ -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 -0
      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.

Quoted 3 lines

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)

Quoted 5 lines
Quoted 3 lines
fs/erofs/xattr.c +0 -0
      vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
                                              sizeof(uint), GFP_KERNEL);
      if (!vi->xattr_shared_xattrs) {