Skip to contentSkip to conversation

hugetlbfs: Adjustments for two function implementations

PATCH In review Markus Elfring 3 participants 6 messages Dec 29, 2023 – Jan 2, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover hugetlbfs: Adjustments for two function implementations

From: Markus Elfring <[email protected]>
Date: Fri, 29 Dec 2023 12:32:12 +0100

A few update suggestions were taken into account
from static source code analysis.

Markus Elfring (2):
Improve a size determination in two functions Improve exception handling in hugetlbfs_fill_super()

 fs/hugetlbfs/inode.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

--
2.43.0

#1 lore raw

PATCH 1/2 hugetlbfs: Improve a size determination in two functions

From: Markus Elfring <[email protected]>
Date: Fri, 29 Dec 2023 11:32:07 +0100

Replace the specification of data structures by pointer dereferences as the parameter for the operator “sizeof” to make the corresponding size determination a bit safer according to the Linux coding style convention.

This issue was detected by using the Coccinelle software.

---
 fs/hugetlbfs/inode.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/hugetlbfs/inode.c +2 -2
index ea5b8e57d904..24401a5046dd 100644
@@ -1460,7 +1460,7 @@ hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	struct hugetlbfs_fs_context *ctx = fc->fs_private;
 	struct hugetlbfs_sb_info *sbinfo;
 
-	sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
+	sbinfo = kmalloc(sizeof(*sbinfo), GFP_KERNEL);
 	if (!sbinfo)
 		return -ENOMEM;
 	sb->s_fs_info = sbinfo;
@@ -1530,7 +1530,7 @@ static int hugetlbfs_init_fs_context(struct fs_context *fc)
 {
 	struct hugetlbfs_fs_context *ctx;
 
-	ctx = kzalloc(sizeof(struct hugetlbfs_fs_context), GFP_KERNEL);
+	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)
 		return -ENOMEM;
 

--
2.43.0

maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] hugetlbfs: Improve a size determination in two functions

Quoted 7 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Fri, 29 Dec 2023 11:32:07 +0100

Quoted 7 lines

Replace the specification of data structures by pointer dereferences as the parameter for the operator “sizeof” to make the corresponding size determination a bit safer according to the Linux coding style convention.

This issue was detected by using the Coccinelle software.

Thanks.

#3 lore raw

PATCH 2/2 hugetlbfs: Improve exception handling in hugetlbfs_fill_super()

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Fri, 29 Dec 2023 11:46:32 +0100

The kfree() function was called in one case by the hugetlbfs_fill_super() function during error handling even if the passed variable contained a null pointer. This issue was detected by using the Coccinelle software.

Thus adjust jump targets.

---
 fs/hugetlbfs/inode.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/hugetlbfs/inode.c +5 -3
index 24401a5046dd..5687ec574dc4 100644
@@ -1483,7 +1483,7 @@ hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
 						     ctx->max_hpages,
 						     ctx->min_hpages);
 		if (!sbinfo->spool)
-			goto out_free;
+			goto free_sbinfo;
 	}
 	sb->s_maxbytes = MAX_LFS_FILESIZE;
 	sb->s_blocksize = huge_page_size(ctx->hstate);
@@ -1499,10 +1499,12 @@ hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	sb->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
 	sb->s_root = d_make_root(hugetlbfs_get_root(sb, ctx));
 	if (!sb->s_root)
-		goto out_free;
+		goto free_spool;
 	return 0;
-out_free:
+
+free_spool:
 	kfree(sbinfo->spool);
+free_sbinfo:
 	kfree(sbinfo);
 	return -ENOMEM;
 }

--
2.43.0

#4 lore raw

Re: [PATCH 2/2] hugetlbfs: Improve exception handling in hugetlbfs_fill_super()

Quoted 18 lines · Markus Elfring
fs/hugetlbfs/inode.c +1 -2
index 24401a5046dd..5687ec574dc4 100644
@@ -1483,7 +1483,7 @@ hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
 						     ctx->max_hpages,
 						     ctx->min_hpages);
 		if (!sbinfo->spool)
-			goto out_free;
+			goto free_sbinfo;
 	}
 	sb->s_maxbytes = MAX_LFS_FILESIZE;
 	sb->s_blocksize = huge_page_size(ctx->hstate);
@@ -1499,10 +1499,12 @@ hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	sb->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
 	sb->s_root = d_make_root(hugetlbfs_get_root(sb, ctx));
 	if (!sb->s_root)
-		goto out_free;

fs/hugetlbfs/inode.c hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc) · line 1510

Quoted 10 lines
fs/hugetlbfs/inode.c +4 -1
+		goto free_spool;
 	return 0;
-out_free:
+
+free_spool:
 	kfree(sbinfo->spool);
+free_sbinfo:
 	kfree(sbinfo);
 	return -ENOMEM;
 }

This is more complex. NACK.

#5 lore raw

Re: [PATCH 2/2] hugetlbfs: Improve exception handling in hugetlbfs_fill_super()

Quoted 19 lines
Quoted 19 lines

+++ b/fs/hugetlbfs/inode.c

fs/hugetlbfs/inode.c +3 -3
@@ -1483,7 +1483,7 @@ hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
 						     ctx->max_hpages,
 						     ctx->min_hpages);
 		if (!sbinfo->spool)
-			goto out_free;
+			goto free_sbinfo;
 	}
 	sb->s_maxbytes = MAX_LFS_FILESIZE;
 	sb->s_blocksize = huge_page_size(ctx->hstate);
@@ -1499,10 +1499,12 @@ hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	sb->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
 	sb->s_root = d_make_root(hugetlbfs_get_root(sb, ctx));
 	if (!sb->s_root)
-		goto out_free;
+		goto free_spool;
 	return 0;
-out_free:
+
Quoted 8 lines

fs/hugetlbfs/inode.c hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc) · line 1510

Quoted 6 lines
fs/hugetlbfs/inode.c +2 -0
+free_spool:
 	kfree(sbinfo->spool);
+free_sbinfo:
 	kfree(sbinfo);
 	return -ENOMEM;
 }

This is more complex. NACK.

I am curious how coding style preferences will evolve further.

See also:
https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consider+using+a+goto+chain+when+leaving+a+function+on+error+when+using+and+releasing+resources

Regards,
Markus