Skip to contentSkip to conversation

dcache: use kmalloc_flex() in __d_alloc

PATCH Applied Thorsten Blum 4 participants 4 messages Apr 17 – Apr 21 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dcache: use kmalloc_flex() in __d_alloc

Use kmalloc_flex() when allocating a new 'struct external_name' in __d_alloc() to replace offsetof() and the open-coded size arithmetic, and to keep the size type-safe.

---
 fs/dcache.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/dcache.c +4 -4
index dbcbd0affb26..4568f9530166 100644
@@ -1753,10 +1753,10 @@ static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
 		name = &slash_name;
 		dname = dentry->d_shortname.string;
 	} else if (name->len > DNAME_INLINE_LEN-1) {
-		size_t size = offsetof(struct external_name, name[1]);
-		struct external_name *p = kmalloc(size + name->len,
-						  GFP_KERNEL_ACCOUNT |
-						  __GFP_RECLAIMABLE);
+		struct external_name *p;
+
+		p = kmalloc_flex(*p, name, name->len + 1,
+				 GFP_KERNEL_ACCOUNT | __GFP_RECLAIMABLE);
 		if (!p) {
 			kmem_cache_free(dentry_cache, dentry); 
 			return NULL;
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Thorsten Blum

Use kmalloc_flex() when allocating a new 'struct external_name' in __d_alloc() to replace offsetof() and the open-coded size arithmetic, and to keep the size type-safe.

---
 fs/dcache.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/dcache.c +0 -1
index dbcbd0affb26..4568f9530166 100644
@@ -1753,10 +1753,10 @@ static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
 		name = &slash_name;
 		dname = dentry->d_shortname.string;
 	} else if (name->len > DNAME_INLINE_LEN-1) {
-		size_t size = offsetof(struct external_name, name[1]);

fs/dcache.c static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name) · line 1762

Quoted 10 lines
fs/dcache.c +4 -3
-		struct external_name *p = kmalloc(size + name->len,
-						  GFP_KERNEL_ACCOUNT |
-						  __GFP_RECLAIMABLE);
+		struct external_name *p;
+
+		p = kmalloc_flex(*p, name, name->len + 1,
+				 GFP_KERNEL_ACCOUNT | __GFP_RECLAIMABLE);
 		if (!p) {
 			kmem_cache_free(dentry_cache, dentry); 
 			return NULL;

Looks reasonable to me. Is the idea to completely outphase this old way of requesting memory for structs with flexible array members?

Feel free to add:

reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Thorsten Blum
Quoted 5 lines · Thorsten Blum

Use kmalloc_flex() when allocating a new 'struct external_name' in __d_alloc() to replace offsetof() and the open-coded size arithmetic, and to keep the size type-safe.

Looks good. Feel free to add:

Honza

Quoted 23 lines
---
 fs/dcache.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/dcache.c +4 -4
index dbcbd0affb26..4568f9530166 100644
@@ -1753,10 +1753,10 @@ static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
 		name = &slash_name;
 		dname = dentry->d_shortname.string;
 	} else if (name->len > DNAME_INLINE_LEN-1) {
-		size_t size = offsetof(struct external_name, name[1]);
-		struct external_name *p = kmalloc(size + name->len,
-						  GFP_KERNEL_ACCOUNT |
-						  __GFP_RECLAIMABLE);
+		struct external_name *p;
+
+		p = kmalloc_flex(*p, name, name->len + 1,
+				 GFP_KERNEL_ACCOUNT | __GFP_RECLAIMABLE);
 		if (!p) {
 			kmem_cache_free(dentry_cache, dentry); 
 			return NULL;
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#3 lore raw
In reply to Thorsten Blum
Quoted 3 lines · Thorsten Blum

Use kmalloc_flex() when allocating a new 'struct external_name' in __d_alloc() to replace offsetof() and the open-coded size arithmetic, and to keep the size type-safe.

Applied to the vfs-7.2.misc branch of the vfs/vfs.git tree. Patches in the vfs-7.2.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-7.2.misc

[1/1] dcache: use kmalloc_flex() in __d_alloc

      https://git.kernel.org/vfs/vfs/c/4f521e4554a7