2023-12-29 11:36:45

by Markus Elfring

[permalink] [raw]
Subject: [PATCH 0/2] 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



2023-12-29 11:38:10

by Markus Elfring

[permalink] [raw]
Subject: [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.

Signed-off-by: Markus Elfring <[email protected]>
---
fs/hugetlbfs/inode.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index ea5b8e57d904..24401a5046dd 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -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


2023-12-29 11:40:38

by Markus Elfring

[permalink] [raw]
Subject: [PATCH 2/2] hugetlbfs: Improve exception handling in hugetlbfs_fill_super()

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.

Signed-off-by: Markus Elfring <[email protected]>
---
fs/hugetlbfs/inode.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 24401a5046dd..5687ec574dc4 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -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


2023-12-29 14:52:21

by Matthew Wilcox

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

On Fri, Dec 29, 2023 at 12:40:12PM +0100, Markus Elfring wrote:
> diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
> index 24401a5046dd..5687ec574dc4 100644
> --- a/fs/hugetlbfs/inode.c
> +++ b/fs/hugetlbfs/inode.c
> @@ -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;
> }

This is more complex. NACK.

2023-12-30 06:46:39

by Markus Elfring

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

>> +++ b/fs/hugetlbfs/inode.c
>> @@ -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;
>> }
>
> 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

2024-01-02 02:58:28

by Muchun Song

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



> On Dec 29, 2023, at 19:37, Markus Elfring <[email protected]> wrote:
>
> 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.
>
> Signed-off-by: Markus Elfring <[email protected]>

Reviewed-by: Muchun Song <[email protected]>

Thanks.