public inbox for linux-ext4@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ext4: Use struct_size() to improve ext4_htree_store_dirent()
@ 2024-11-05 10:33 Thorsten Blum
  2024-11-05 10:39 ` Greg KH
  2024-11-14 13:53 ` Theodore Ts'o
  0 siblings, 2 replies; 5+ messages in thread
From: Thorsten Blum @ 2024-11-05 10:33 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger
  Cc: Greg KH, Thorsten Blum, Jan Kara, linux-ext4, linux-kernel

Inline and use struct_size() to calculate the number of bytes to
allocate for new_fn and remove the local variable len.

Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
This change was originally part of another patch that was split into two
separate patches after feedback from Greg KH
- Link: https://lore.kernel.org/r/20241104234214.8094-2-thorsten.blum@linux.dev/
---
 fs/ext4/dir.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
index 233479647f1b..02d47a64e8d1 100644
--- a/fs/ext4/dir.c
+++ b/fs/ext4/dir.c
@@ -471,14 +471,13 @@ int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
 	struct rb_node **p, *parent = NULL;
 	struct fname *fname, *new_fn;
 	struct dir_private_info *info;
-	int len;
 
 	info = dir_file->private_data;
 	p = &info->root.rb_node;
 
 	/* Create and allocate the fname structure */
-	len = sizeof(struct fname) + ent_name->len + 1;
-	new_fn = kzalloc(len, GFP_KERNEL);
+	new_fn = kzalloc(struct_size(new_fn, name, ent_name->len + 1),
+			 GFP_KERNEL);
 	if (!new_fn)
 		return -ENOMEM;
 	new_fn->hash = hash;
-- 
2.47.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] ext4: Use struct_size() to improve ext4_htree_store_dirent()
  2024-11-05 10:33 [PATCH] ext4: Use struct_size() to improve ext4_htree_store_dirent() Thorsten Blum
@ 2024-11-05 10:39 ` Greg KH
  2024-11-05 11:06   ` Thorsten Blum
  2024-11-14 13:53 ` Theodore Ts'o
  1 sibling, 1 reply; 5+ messages in thread
From: Greg KH @ 2024-11-05 10:39 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Theodore Ts'o, Andreas Dilger, Jan Kara, linux-ext4,
	linux-kernel

On Tue, Nov 05, 2024 at 11:33:54AM +0100, Thorsten Blum wrote:
> Inline and use struct_size() to calculate the number of bytes to
> allocate for new_fn and remove the local variable len.
> 
> Reviewed-by: Jan Kara <jack@suse.cz>
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
> This change was originally part of another patch that was split into two
> separate patches after feedback from Greg KH
> - Link: https://lore.kernel.org/r/20241104234214.8094-2-thorsten.blum@linux.dev/
> ---
>  fs/ext4/dir.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
> index 233479647f1b..02d47a64e8d1 100644
> --- a/fs/ext4/dir.c
> +++ b/fs/ext4/dir.c
> @@ -471,14 +471,13 @@ int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
>  	struct rb_node **p, *parent = NULL;
>  	struct fname *fname, *new_fn;
>  	struct dir_private_info *info;
> -	int len;
>  
>  	info = dir_file->private_data;
>  	p = &info->root.rb_node;
>  
>  	/* Create and allocate the fname structure */
> -	len = sizeof(struct fname) + ent_name->len + 1;
> -	new_fn = kzalloc(len, GFP_KERNEL);
> +	new_fn = kzalloc(struct_size(new_fn, name, ent_name->len + 1),
> +			 GFP_KERNEL);

Does this actually matter and make the code any more robust or faster?

The original code here is easier to read and understand, why add
complexity if it is not required?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ext4: Use struct_size() to improve ext4_htree_store_dirent()
  2024-11-05 10:39 ` Greg KH
@ 2024-11-05 11:06   ` Thorsten Blum
  2024-11-05 12:11     ` Greg KH
  0 siblings, 1 reply; 5+ messages in thread
From: Thorsten Blum @ 2024-11-05 11:06 UTC (permalink / raw)
  To: Greg KH
  Cc: Theodore Ts'o, Andreas Dilger, Jan Kara, linux-ext4,
	linux-kernel

On 5. Nov 2024, at 11:39, Greg KH wrote:
> On Tue, Nov 05, 2024 at 11:33:54AM +0100, Thorsten Blum wrote:
>> Inline and use struct_size() to calculate the number of bytes to
>> allocate for new_fn and remove the local variable len.
>> 
>> Reviewed-by: Jan Kara <jack@suse.cz>
>> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
>> ---
>> This change was originally part of another patch that was split into two
>> separate patches after feedback from Greg KH
>> - Link: https://lore.kernel.org/r/20241104234214.8094-2-thorsten.blum@linux.dev/
>> ---
>> fs/ext4/dir.c | 5 ++---
>> 1 file changed, 2 insertions(+), 3 deletions(-)
>> 
>> diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
>> index 233479647f1b..02d47a64e8d1 100644
>> --- a/fs/ext4/dir.c
>> +++ b/fs/ext4/dir.c
>> @@ -471,14 +471,13 @@ int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
>> struct rb_node **p, *parent = NULL;
>> struct fname *fname, *new_fn;
>> struct dir_private_info *info;
>> - int len;
>> 
>> info = dir_file->private_data;
>> p = &info->root.rb_node;
>> 
>> /* Create and allocate the fname structure */
>> - len = sizeof(struct fname) + ent_name->len + 1;
>> - new_fn = kzalloc(len, GFP_KERNEL);
>> + new_fn = kzalloc(struct_size(new_fn, name, ent_name->len + 1),
>> + GFP_KERNEL);
> 
> Does this actually matter and make the code any more robust or faster?
> 
> The original code here is easier to read and understand, why add
> complexity if it is not required?

I find struct_size() to be more readable because it explicitly
communicates the relationship between the flexible array member name and
ent_name->len that the open-coded version doesn't. Plus, struct_size()
has some additional compile-time checks (e.g., __must_be_array()).

Thanks,
Thorsten


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ext4: Use struct_size() to improve ext4_htree_store_dirent()
  2024-11-05 11:06   ` Thorsten Blum
@ 2024-11-05 12:11     ` Greg KH
  0 siblings, 0 replies; 5+ messages in thread
From: Greg KH @ 2024-11-05 12:11 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Theodore Ts'o, Andreas Dilger, Jan Kara, linux-ext4,
	linux-kernel

On Tue, Nov 05, 2024 at 12:06:42PM +0100, Thorsten Blum wrote:
> On 5. Nov 2024, at 11:39, Greg KH wrote:
> > On Tue, Nov 05, 2024 at 11:33:54AM +0100, Thorsten Blum wrote:
> >> Inline and use struct_size() to calculate the number of bytes to
> >> allocate for new_fn and remove the local variable len.
> >> 
> >> Reviewed-by: Jan Kara <jack@suse.cz>
> >> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> >> ---
> >> This change was originally part of another patch that was split into two
> >> separate patches after feedback from Greg KH
> >> - Link: https://lore.kernel.org/r/20241104234214.8094-2-thorsten.blum@linux.dev/
> >> ---
> >> fs/ext4/dir.c | 5 ++---
> >> 1 file changed, 2 insertions(+), 3 deletions(-)
> >> 
> >> diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
> >> index 233479647f1b..02d47a64e8d1 100644
> >> --- a/fs/ext4/dir.c
> >> +++ b/fs/ext4/dir.c
> >> @@ -471,14 +471,13 @@ int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
> >> struct rb_node **p, *parent = NULL;
> >> struct fname *fname, *new_fn;
> >> struct dir_private_info *info;
> >> - int len;
> >> 
> >> info = dir_file->private_data;
> >> p = &info->root.rb_node;
> >> 
> >> /* Create and allocate the fname structure */
> >> - len = sizeof(struct fname) + ent_name->len + 1;
> >> - new_fn = kzalloc(len, GFP_KERNEL);
> >> + new_fn = kzalloc(struct_size(new_fn, name, ent_name->len + 1),
> >> + GFP_KERNEL);
> > 
> > Does this actually matter and make the code any more robust or faster?
> > 
> > The original code here is easier to read and understand, why add
> > complexity if it is not required?
> 
> I find struct_size() to be more readable because it explicitly
> communicates the relationship between the flexible array member name and
> ent_name->len that the open-coded version doesn't. Plus, struct_size()
> has some additional compile-time checks (e.g., __must_be_array()).

Ah, missed that, sure, that makes more sense, sorry for the noise.

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ext4: Use struct_size() to improve ext4_htree_store_dirent()
  2024-11-05 10:33 [PATCH] ext4: Use struct_size() to improve ext4_htree_store_dirent() Thorsten Blum
  2024-11-05 10:39 ` Greg KH
@ 2024-11-14 13:53 ` Theodore Ts'o
  1 sibling, 0 replies; 5+ messages in thread
From: Theodore Ts'o @ 2024-11-14 13:53 UTC (permalink / raw)
  To: Andreas Dilger, Thorsten Blum
  Cc: Theodore Ts'o, Greg KH, Jan Kara, linux-ext4, linux-kernel


On Tue, 05 Nov 2024 11:33:54 +0100, Thorsten Blum wrote:
> Inline and use struct_size() to calculate the number of bytes to
> allocate for new_fn and remove the local variable len.
> 
> 

Applied, thanks!

[1/1] ext4: Use struct_size() to improve ext4_htree_store_dirent()
      commit: d5e9836e13a53ef36af702d87ab20d1a126b0fb8

Best regards,
-- 
Theodore Ts'o <tytso@mit.edu>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-11-14 13:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-05 10:33 [PATCH] ext4: Use struct_size() to improve ext4_htree_store_dirent() Thorsten Blum
2024-11-05 10:39 ` Greg KH
2024-11-05 11:06   ` Thorsten Blum
2024-11-05 12:11     ` Greg KH
2024-11-14 13:53 ` Theodore Ts'o

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox