Skip to contentSkip to conversation

fs/ufs/super.c: remove unnecessary casting

PATCH Superseded Fabian Frederick 3 participants 6 messages Dec 28, 2014 – Dec 31, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/ufs/super.c: remove unnecessary casting

Fix the following coccinelle warning:
fs/ufs/super.c:1418:7-28: WARNING: casting value returned by memory allocation function to (struct ufs_inode_info *) is useless.

Cc Evgeniy Dushistov <[email protected]>
Cc Andrew Morton <[email protected]>
---
 fs/ufs/super.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/ufs/super.c +2 -2
index da73801..a15798f 100644
@@ -1414,8 +1414,8 @@ static struct kmem_cache * ufs_inode_cachep;
 
 static struct inode *ufs_alloc_inode(struct super_block *sb)
 {
-	struct ufs_inode_info *ei;
-	ei = (struct ufs_inode_info *)kmem_cache_alloc(ufs_inode_cachep, GFP_NOFS);
+	struct ufs_inode_info *ei = kmem_cache_alloc(ufs_inode_cachep,
+						     GFP_NOFS);
 	if (!ei)
 		return NULL;
 	ei->vfs_inode.i_version = 1;
Signature · 1 line
2.1.0
#1 lore raw
Quoted 3 lines · Fabian Frederick

Fix the following coccinelle warning:
fs/ufs/super.c:1418:7-28: WARNING: casting value returned by memory allocation function to (struct ufs_inode_info *) is useless.

... except that it makes allocations harder to grep for. IOW, it's not obviously an improvement and should be up to maintainer of the code in question.

#2 lore raw
Quoted 6 lines · Al Viro
Quoted 3 lines · Fabian Frederick

Fix the following coccinelle warning:
fs/ufs/super.c:1418:7-28: WARNING: casting value returned by memory allocation function to (struct ufs_inode_info *) is useless.

... except that it makes allocations harder to grep for.

How does it do that?

Quoted 3 lines

IOW, it's not
obviously an improvement and should be up to maintainer of the code in question.

That's always true in any case.

#3 lore raw
Quoted 9 lines · Joe Perches
Quoted 6 lines · Al Viro
Quoted 3 lines · Fabian Frederick

Fix the following coccinelle warning:
fs/ufs/super.c:1418:7-28: WARNING: casting value returned by memory allocation function to (struct ufs_inode_info *) is useless.

... except that it makes allocations harder to grep for.

How does it do that?

search for \<struct[ ]*$NAME[ ]*($|[^ *]|[*][ ]*[)])
generally gives a lot of interesting information (variables of that type, members of anything having that type, sizeof, container_of, explicit casts) with relatively little noise.

BTW, that's why I really don't like the stuff like

	struct foo *p = kmalloc(sizeof(*p), GFP_KERNEL);

It's bloody hard to spot.

In case of UFS we still catch "sizeof(struct ufs_inode_info)" in
ufs_inode_cachep = kmem_cache_create("ufs_inode_cache",
sizeof(struct ufs_inode_info), 0, (SLAB_RECLAIM_ACCOUNT|
SLAB_MEM_SPREAD),
init_once);
which gives the obvious secondary search pattern, so it's not _that_ terrible, but in general it's not something to do without thinking - such a cast could be placed there exactly to make it stand out on grep.

#4 lore raw
Quoted 25 lines · On 28 December 2014 at 19:21 Al Viro <[email protected]> wrote:
Quoted 9 lines · Joe Perches
Quoted 6 lines · Al Viro
Quoted 3 lines · Fabian Frederick

Fix the following coccinelle warning:
fs/ufs/super.c:1418:7-28: WARNING: casting value returned by memory allocation function to (struct ufs_inode_info *) is useless.

... except that it makes allocations harder to grep for.

How does it do that?

search for \<struct[  ]*$NAME[        ]*($|[^         *]|[*][         ]*[)]) generally gives a lot of interesting information (variables of that type, members of anything having that type, sizeof, container_of, explicit casts) with relatively little noise.

BTW, that's why I really don't like the stuff like struct foo *p = kmalloc(sizeof(*p), GFP_KERNEL); It's bloody hard to spot.

Quoted 9 lines

In case of UFS we still catch "sizeof(struct ufs_inode_info)" in ufs_inode_cachep = kmem_cache_create("ufs_inode_cache", sizeof(struct ufs_inode_info), 0, (SLAB_RECLAIM_ACCOUNT| SLAB_MEM_SPREAD), init_once);
which gives the obvious secondary search pattern, so it's not _that_ terrible, but in general it's not something to do without thinking - such a cast could be placed there exactly to make it stand out on grep.

Thanks a lot for taking the time to explain the problem Al. I'll be more careful next time.

Regards,
Fabian

#5 lore raw
In reply to Fabian Frederick
Quoted 3 lines · Fabian Frederick

Fix the following coccinelle warning:
fs/ufs/super.c:1418:7-28: WARNING: casting value returned by memory allocation function to (struct ufs_inode_info *) is useless.

[]

fs/ufs/super.c

Quoted 1 line
fs/ufs/super.c +0 -0

[]

fs/ufs/super.c static struct kmem_cache * ufs_inode_cachep; · line 1418

Quoted 8 lines
fs/ufs/super.c +2 -2
@@ -1414,8 +1414,8 @@ static struct kmem_cache * ufs_inode_cachep;
 
 static struct inode *ufs_alloc_inode(struct super_block *sb)
 {
-	struct ufs_inode_info *ei;
-	ei = (struct ufs_inode_info *)kmem_cache_alloc(ufs_inode_cachep, GFP_NOFS);
+	struct ufs_inode_info *ei = kmem_cache_alloc(ufs_inode_cachep,
+						     GFP_NOFS);

If you insist on the blank line after declaration style, perhaps it's better to separate the declaration from the alloc so the alloc is on a single line like:

static struct inode *ufs_alloc_inode(struct super_block *sb)

{
	struct ufs_inode_info *ei;

	ei = kmem_cache_alloc(ufs_inode_cachep, GFP_NOFS);
	if (!ei)
		return NULL;

	ei->vfs_inode.i_version = 1;
	etc...