Skip to contentSkip to conversation

ext4: Use struct_size() to improve ext4_htree_store_dirent()

PATCH Applied Thorsten Blum 3 participants 5 messages Nov 5, 2024 – Nov 14, 2024 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext4: Use struct_size() to improve ext4_htree_store_dirent()

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

---
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/[email protected]/

---
 fs/ext4/dir.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/ext4/dir.c +2 -3
index 233479647f1b..02d47a64e8d1 100644
@@ -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;
Signature · 1 line
2.47.0
#1 lore raw
Quoted 21 lines · Thorsten Blum

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

---
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/[email protected]/

---
 fs/ext4/dir.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/ext4/dir.c +0 -0
index 233479647f1b..02d47a64e8d1 100644
@@ -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;

fs/ext4/dir.c int ext4_htree_store_dirent(struct file *dir_file, __u32 hash, · line 480

Quoted 10 lines
fs/ext4/dir.c +2 -3
-	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

#2 lore raw
Quoted 35 lines · Greg KH
Quoted 21 lines · Thorsten Blum

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

---
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/[email protected]/
---
fs/ext4/dir.c | 5 ++---

1 file changed, 2 insertions(+), 3 deletions(-)
fs/ext4/dir.c +0 -0
index 233479647f1b..02d47a64e8d1 100644
@@ -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;

fs/ext4/dir.c int ext4_htree_store_dirent(struct file *dir_file, __u32 hash, · line 480

Quoted 10 lines
fs/ext4/dir.c +2 -3
- 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?

Quoted 2 lines

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

#3 lore raw
Quoted 36 lines · Thorsten Blum
Quoted 35 lines · Greg KH
Quoted 21 lines · Thorsten Blum

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

---
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/[email protected]/
---
fs/ext4/dir.c | 5 ++---

1 file changed, 2 insertions(+), 3 deletions(-)
fs/ext4/dir.c +0 -0
index 233479647f1b..02d47a64e8d1 100644
@@ -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;

fs/ext4/dir.c int ext4_htree_store_dirent(struct file *dir_file, __u32 hash, · line 480

Quoted 10 lines
fs/ext4/dir.c +2 -3
- 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?

Quoted 7 lines
Quoted 2 lines

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

maintainer Applied
#4 lore raw
In reply to Thorsten Blum
Quoted 4 lines · Thorsten Blum

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,

Signature · 1 line
Theodore Ts'o <[email protected]>