Skip to contentSkip to conversation

Trivial code clean for procfs

PATCH Superseded yan 4 participants 13 messages Sep 3, 2012 – Sep 5, 2012 Proc filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

yan

author
#0 lore raw

cover Trivial code clean for procfs

yan (3):
proc: return -ENOMEM when inode allocation failed
proc: no need to initialize proc_inode->fd in proc_get_inode
proc: use kzalloc instead of kmalloc and memset

 fs/proc/generic.c |    5 ++---
 fs/proc/inode.c   |    1 -
 2 files changed, 2 insertions(+), 4 deletions(-)
Signature · 1 line
1.7.9.5
author
#1 lore raw

PATCH 1/3 proc: return -ENOMEM when inode allocation failed

---
 fs/proc/generic.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/proc/generic.c +1 -1
index b3647fe..9e8f631 100644
@@ -427,7 +427,7 @@ struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
 		if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
 			pde_get(de);
 			spin_unlock(&proc_subdir_lock);
-			error = -EINVAL;
+			error = -ENOMEM;
 			inode = proc_get_inode(dir->i_sb, de);
 			goto out_unlock;
 		}
Signature · 1 line
1.7.9.5
#2 lore raw

Re: [PATCH 1/3] proc: return -ENOMEM when inode allocation failed

Quoted 10 lines · yan
---
 fs/proc/generic.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/proc/generic.c +0 -0
index b3647fe..9e8f631 100644

fs/proc/generic.c struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir, · line 430

Quoted 5 lines
fs/proc/generic.c +1 -1
@@ -427,7 +427,7 @@ struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
 		if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
 			pde_get(de);
 			spin_unlock(&proc_subdir_lock);
-			error = -EINVAL;
+			error = -ENOMEM;

This seems incorrect. This function doesn't allocate anything, it looks up an inode. From my reading it looks like -EINVAL is returned here if the dentry name matches, but the inode cannot be found.

~Ryan

#3 lore raw

Re: [PATCH 1/3] proc: return -ENOMEM when inode allocation failed

In reply to yan
Quoted 1 line · yan

Please provide a changelog to explain why we need this patch.

Quoted 9 lines
---
  fs/proc/generic.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/proc/generic.c +0 -0
index b3647fe..9e8f631 100644

fs/proc/generic.c struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir, · line 430

Quoted 5 lines
fs/proc/generic.c +1 -1
@@ -427,7 +427,7 @@ struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
  		if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
  			pde_get(de);
  			spin_unlock(&proc_subdir_lock);
-			error = -EINVAL;
+			error = -ENOMEM;

Why the !memcmp() case is related with ENOMEM ??

author
#4 lore raw

Re: [PATCH 1/3] proc: return -ENOMEM when inode allocation failed

2012/9/4 Cong Wang <[email protected]>:

Quoted 6 lines · yan
Quoted 2 lines

Please provide a changelog to explain why we need this patch.

I think the title is self explained.

Quoted 4 lines
Quoted 4 lines
---
  fs/proc/generic.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

fs/proc/generic.c struct dentry *proc_lookup_de(struct proc_dir_entry · line 431

Quoted 11 lines
fs/proc/generic.c +1 -1
index b3647fe..9e8f631 100644
@@ -427,7 +427,7 @@ struct dentry *proc_lookup_de(struct proc_dir_entry
 *de, struct inode *dir,
                if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
                        pde_get(de);
                        spin_unlock(&proc_subdir_lock);
-                       error = -EINVAL;
+                       error = -ENOMEM;

Why the !memcmp() case is related with ENOMEM ??

We are presetting 'error' here. The following proc_get_inode() will try to get an inode, either from inode cache or allocate a new one (and fill it).

If we get a NULL inode, that means allocation failed. That's how ENOMEM involved.

Thank you for your reply.

#5 lore raw

Re: [PATCH 1/3] proc: return -ENOMEM when inode allocation failed

Quoted 9 lines · On Tue, 4 Sep 2012 17:22:15 +0800 yan yan <[email protected]> wrote:

2012/9/4 Cong Wang <[email protected]>:

Quoted 6 lines · yan
Quoted 2 lines

Please provide a changelog to explain why we need this patch.

I think the title is self explained.

Given this review feedback, the title alone was obviously insufficient!

I provided the following changelog text:

: If proc_get_inode() returns NULL then presumably it encountered memory : exhaustion. proc_lookup_de() should return -ENOMEM in this case, not : -EINVAL.

#6 lore raw

Re: [PATCH 1/3] proc: return -ENOMEM when inode allocation failed

In reply to yan yan
Quoted 30 lines · yan yan

2012/9/4 Cong Wang <[email protected]>:

Quoted 6 lines · yan
Quoted 2 lines

Please provide a changelog to explain why we need this patch.

I think the title is self explained.

Quoted 4 lines
Quoted 4 lines
---
   fs/proc/generic.c |    2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

fs/proc/generic.c struct dentry *proc_lookup_de(struct proc_dir_entry · line 431

Quoted 11 lines
fs/proc/generic.c +1 -1
index b3647fe..9e8f631 100644
@@ -427,7 +427,7 @@ struct dentry *proc_lookup_de(struct proc_dir_entry
 *de, struct inode *dir,
                 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
                         pde_get(de);
                         spin_unlock(&proc_subdir_lock);
-                       error = -EINVAL;
+                       error = -ENOMEM;

Why the !memcmp() case is related with ENOMEM ??

Quoted 5 lines

We are presetting 'error' here. The following proc_get_inode() will try to get an inode, either from inode cache or allocate a new one (and fill it).

If we get a NULL inode, that means allocation failed. That's how ENOMEM involved.

Then the following patch is probably better than yours:

fs/proc/generic.c +7 -1
index b3647fe..6b22913 100644
@@ -427,12 +427,16 @@ struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
                if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
                        pde_get(de);
                        spin_unlock(&proc_subdir_lock);
-                       error = -EINVAL;
                        inode = proc_get_inode(dir->i_sb, de);
+                       if (!inode) {
+                               error = -ENOMEM;
+                               goto out_put;
+                       }
                        goto out_unlock;
                }
        }
        spin_unlock(&proc_subdir_lock);
+
 out_unlock:
 
        if (inode) {
@@ -440,6 +444,8 @@ out_unlock:
                d_add(dentry, inode);
                return NULL;
        }
+out_put:
+
        if (de)
                pde_put(de);
        return ERR_PTR(error);
author
#7 lore raw

Re: [PATCH 1/3] proc: return -ENOMEM when inode allocation failed

2012/9/5 Cong Wang <[email protected]>:

Quoted 41 lines
Quoted 9 lines
Quoted 1 line

Why the !memcmp() case is related with ENOMEM ??

We are presetting 'error' here. The following proc_get_inode() will try to get an inode, either from inode cache or allocate a new one (and fill it).

If we get a NULL inode, that means allocation failed. That's how ENOMEM involved.

Then the following patch is probably better than yours:

fs/proc/generic.c +5 -1
index b3647fe..6b22913 100644
@@ -427,12 +427,16 @@ struct dentry *proc_lookup_de(struct proc_dir_entry
 *de, struct inode *dir,
 
                if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
                        pde_get(de);
                        spin_unlock(&proc_subdir_lock);
-                       error = -EINVAL;
                        inode = proc_get_inode(dir->i_sb, de);
+                       if (!inode) {
+                               error = -ENOMEM;
+                               goto out_put;
+                       }
                        goto out_unlock;
                }
        }
        spin_unlock(&proc_subdir_lock);
+
 out_unlock:
        if (inode) {

fs/proc/generic.c out_unlock: · line 451

Quoted 8 lines
fs/proc/generic.c +2 -0
@@ -440,6 +444,8 @@ out_unlock:
                d_add(dentry, inode);
                return NULL;
        }
+out_put:
+
        if (de)
                pde_put(de);
        return ERR_PTR(error);

Change so many lines to save a assignment to 'error' ...

That's a stye issue. I prefer a simple change, though your change seems OK to me.

Thanks

author
#8 lore raw

PATCH 2/3 proc : no need to initialize proc_inode->fd in proc_get_inode

In reply to yan

It has been initialized in proc_alloc_inode.

---
 fs/proc/inode.c |    1 -
 1 file changed, 1 deletion(-)
fs/proc/inode.c +0 -1
index 7ac817b..3b22bbd 100644
@@ -450,7 +450,6 @@ struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
 		return NULL;
 	if (inode->i_state & I_NEW) {
 		inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
-		PROC_I(inode)->fd = 0;
 		PROC_I(inode)->pde = de;
 
 		if (de->mode) {
Signature · 1 line
1.7.9.5
#9 lore raw

Re: [PATCH 2/3] proc : no need to initialize proc_inode->fd in proc_get_inode

Quoted 3 lines · On Mon, 3 Sep 2012 22:14:06 +0800 yan <[email protected]> wrote:

It has been initialized in proc_alloc_inode.

...

I think what you mean here is that the preceding call to iget_locked() will call alloc_inode() which will call proc_alloc_inode() which clears pro_inode.fd.

And if iget_locked() instead found the inode via find_inode_fast(), that inode will not have I_NEW set so this change has no effect.

Yes? Please do spell things out in this level of detail so that others can check your logic.

fs/proc/inode.c struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de) · line 455

Quoted 10 lines
fs/proc/inode.c +0 -1
@@ -450,7 +450,6 @@ struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
 		return NULL;
 	if (inode->i_state & I_NEW) {
 		inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
-		PROC_I(inode)->fd = 0;
 		PROC_I(inode)->pde = de;
 
 		if (de->mode) {

I provided this changelog:

: proc_get_inode() obtains the inode via a call to iget_locked(). : iget_locked() calls alloc_inode() which will call proc_alloc_inode() which : clears proc_inode.fd, so there is no need to clear this field in : proc_get_inode().
:
: If iget_locked() instead found the inode via find_inode_fast(), that inode : will not have I_NEW set so this change has no effect.

author
#10 lore raw

PATCH 3/3 proc: use kzalloc instead of kmalloc and memset

In reply to yan
---
 fs/proc/generic.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/proc/generic.c +1 -2
index 9e8f631..38de015 100644
@@ -616,10 +616,9 @@ static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
 
 	len = strlen(fn);
 
-	ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
+	ent = kzalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
 	if (!ent) goto out;
 
-	memset(ent, 0, sizeof(struct proc_dir_entry));
 	memcpy(ent->name, fn, len + 1);
 	ent->namelen = len;
 	ent->mode = mode;
Signature · 1 line
1.7.9.5
#11 lore raw

Re: [PATCH 3/3] proc: use kzalloc instead of kmalloc and memset

Quoted 11 lines · yan
---
 fs/proc/generic.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/proc/generic.c +0 -0
index 9e8f631..38de015 100644
@@ -616,10 +616,9 @@ static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
 

fs/proc/generic.c static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent, · line 624

Quoted 10 lines
fs/proc/generic.c +1 -2
 	len = strlen(fn);
 
-	ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
+	ent = kzalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
 	if (!ent) goto out;
 
-	memset(ent, 0, sizeof(struct proc_dir_entry));
 	memcpy(ent->name, fn, len + 1);
 	ent->namelen = len;
 	ent->mode = mode;

Note that this change results in slightly different behaviour. Before your change only sizeof(struct proc_dir_entry) is zero'ed by memset, and then the name is filled in (the len + 1 part). After your change the structure and the name field are both zeroed, so the name field is being written to twice. The cost is probably negligible though.

~Ryan

author
#12 lore raw

Re: [PATCH 3/3] proc: use kzalloc instead of kmalloc and memset

2012/9/4 Ryan Mallon <[email protected]>

Quoted 22 lines · yan
Quoted 20 lines
---
 fs/proc/generic.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/proc/generic.c +1 -1
index 9e8f631..38de015 100644
@@ -616,10 +616,9 @@ static struct proc_dir_entry *__proc_create(struct
 proc_dir_entry **parent,
 
      len = strlen(fn);
 
-     ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1,
 GFP_KERNEL);
+     ent = kzalloc(sizeof(struct proc_dir_entry) + len + 1,
 GFP_KERNEL);
      if (!ent) goto out;
 
Quoted 10 lines
Quoted 4 lines
fs/proc/generic.c +0 -1
-     memset(ent, 0, sizeof(struct proc_dir_entry));
      memcpy(ent->name, fn, len + 1);
      ent->namelen = len;
      ent->mode = mode;

Note that this change results in slightly different behaviour. Before your change only sizeof(struct proc_dir_entry) is zero'ed by memset, and then the name is filled in (the len + 1 part). After your change the structure and the name field are both zeroed, so the name field is being written to twice. The cost is probably negligible though.

Oh, I didn't notice that actually. Thank you.