Trivial code clean for procfs
v2 cover Trivial code clean for procfs
Changed from v1 :
Add detailed changelog (suggested by Andrew Morton)
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
PATCH v2 1/3 proc: return -ENOMEM when inode allocation failed
If proc_get_inode() returns NULL then presumably it encountered memory exhaustion. proc_lookup_de() should return -ENOMEM in this case, not -EINVAL.
--- fs/proc/generic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
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 ; inode =proc_get_inode (dir->i_sb, de); goto out_unlock; }
Signature · 1 line
1.7.9.5
PATCH v2 2/3 proc : no need to initialize proc_inode->fd in proc_get_inode
In reply to yanproc_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.
--- fs/proc/inode.c | 1 - 1 file changed, 1 deletion(-)
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) {
Signature · 1 line
1.7.9.5
PATCH v2 3/3 proc: use kzalloc instead of kmalloc and memset
In reply to yanPart of the memory will be written twice after this change, but that should be negligible.
--- fs/proc/generic.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/proc/generic.c +1 -2
@@ -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
Re: [PATCH 3/3 v2] proc: use kzalloc instead of kmalloc and memset
Quoted 9 lines · On Wed, 5 Sep 2012 20:17:17 +0800 yan <[email protected]> wrote:
Part of the memory will be written twice after this change, but that should be negligible.
...
fs/proc/generic.c +0
-0@@ -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
-2len =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;
I'm not sure that I really like the idea of adding this additional overhead. But sure, it won't matter to anyone at all.
While we're digging around in __proc_create(), how about we fix a few other things?
From: Andrew Morton <[email protected]>
Subject: proc-use-kzalloc-instead-of-kmalloc-and-memset-fix
fix __proc_create() coding-style issues, remove unneeded zero-initialisations
---
fs/proc/generic.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
fs/proc/generic.c renamed from fs/proc/generic.c~proc-use-kzalloc-instead-of-kmalloc-and-memset-fix +5 -5
@@ -605,7 +605,8 @@ static struct proc_dir_entry *__proc_creunsigned int len;/* make sure name is valid */ -if (!name || !strlen (name)) goto out; +if (!name || !strlen (name)) + goto out;if (xlate_proc_name (name, parent, &fn) !=0 ) goto out;
@@ -617,18 +618,17 @@ static struct proc_dir_entry *__proc_cre len =strlen (fn); ent =kzalloc (sizeof (struct proc_dir_entry ) + len +1 ,GFP_KERNEL ); -if (!ent) goto out; +if (!ent) + goto out;memcpy (ent->name, fn, len +1 ); ent->namelen = len; ent->mode = mode; ent->nlink = nlink;atomic_set (&ent->count,1 ); - ent->pde_users =0 ;spin_lock_init (&ent->pde_unload_lock); - ent->pde_unload_completion =NULL ;INIT_LIST_HEAD (&ent->pde_openers); - out: +out:return ent; }
_