[PATCH V5 3/8] ext4: specify the free pointer offset for ext4_inode_cache

Harry Yoo posted 8 patches 1 month ago
There is a newer version of this series
[PATCH V5 3/8] ext4: specify the free pointer offset for ext4_inode_cache
Posted by Harry Yoo 1 month ago
Convert ext4_inode_cache to use the kmem_cache_args interface and
specify a free pointer offset.

Since ext4_inode_cache uses a constructor, the free pointer would be
placed after the object to overwriting fields used by the constructor.
However, some fields such as ->i_flags are not used by the constructor
and can safely be repurposed for the free pointer.

Specify the free pointer offset at i_flags to reduce the object size.

Signed-off-by: Harry Yoo <harry.yoo@oracle.com>
---
 fs/ext4/super.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 87205660c5d0..42580643a466 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -1491,12 +1491,20 @@ static void init_once(void *foo)
 
 static int __init init_inodecache(void)
 {
-	ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
-				sizeof(struct ext4_inode_info), 0,
-				SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
-				offsetof(struct ext4_inode_info, i_data),
-				sizeof_field(struct ext4_inode_info, i_data),
-				init_once);
+	struct kmem_cache_args args = {
+		.align = 0,
+		.useroffset = offsetof(struct ext4_inode_info, i_data),
+		.usersize = sizeof_field(struct ext4_inode_info, i_data),
+		.use_freeptr_offset = true,
+		.freeptr_offset = offsetof(struct ext4_inode_info, i_flags),
+		.ctor = init_once,
+	};
+
+	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
+				sizeof(struct ext4_inode_info),
+				&args,
+				SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT);
+
 	if (ext4_inode_cachep == NULL)
 		return -ENOMEM;
 	return 0;
-- 
2.43.0
Re: [PATCH V5 3/8] ext4: specify the free pointer offset for ext4_inode_cache
Posted by Vlastimil Babka 1 month ago
On 1/5/26 09:02, Harry Yoo wrote:
> Convert ext4_inode_cache to use the kmem_cache_args interface and
> specify a free pointer offset.
> 
> Since ext4_inode_cache uses a constructor, the free pointer would be
> placed after the object to overwriting fields used by the constructor.

                             ^ prevent?

> However, some fields such as ->i_flags are not used by the constructor
> and can safely be repurposed for the free pointer.
> 
> Specify the free pointer offset at i_flags to reduce the object size.
> 
> Signed-off-by: Harry Yoo <harry.yoo@oracle.com>
> ---
>  fs/ext4/super.c | 20 ++++++++++++++------
>  1 file changed, 14 insertions(+), 6 deletions(-)
> 
> diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> index 87205660c5d0..42580643a466 100644
> --- a/fs/ext4/super.c
> +++ b/fs/ext4/super.c
> @@ -1491,12 +1491,20 @@ static void init_once(void *foo)
>  
>  static int __init init_inodecache(void)
>  {
> -	ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
> -				sizeof(struct ext4_inode_info), 0,
> -				SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
> -				offsetof(struct ext4_inode_info, i_data),
> -				sizeof_field(struct ext4_inode_info, i_data),
> -				init_once);
> +	struct kmem_cache_args args = {
> +		.align = 0,

Nit: it's implicit so not necessary.

> +		.useroffset = offsetof(struct ext4_inode_info, i_data),
> +		.usersize = sizeof_field(struct ext4_inode_info, i_data),
> +		.use_freeptr_offset = true,
> +		.freeptr_offset = offsetof(struct ext4_inode_info, i_flags),
> +		.ctor = init_once,
> +	};
> +
> +	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
> +				sizeof(struct ext4_inode_info),
> +				&args,
> +				SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT);
> +
>  	if (ext4_inode_cachep == NULL)
>  		return -ENOMEM;
>  	return 0;
Re: [PATCH V5 3/8] ext4: specify the free pointer offset for ext4_inode_cache
Posted by Harry Yoo 1 month ago
On Wed, Jan 07, 2026 at 02:54:15PM +0100, Vlastimil Babka wrote:
> On 1/5/26 09:02, Harry Yoo wrote:
> > Convert ext4_inode_cache to use the kmem_cache_args interface and
> > specify a free pointer offset.
> > 
> > Since ext4_inode_cache uses a constructor, the free pointer would be
> > placed after the object to overwriting fields used by the constructor.
> 
>                              ^ prevent?

Oops, right.
Will fix.

> > However, some fields such as ->i_flags are not used by the constructor
> > and can safely be repurposed for the free pointer.
> > 
> > Specify the free pointer offset at i_flags to reduce the object size.
> > 
> > Signed-off-by: Harry Yoo <harry.yoo@oracle.com>
> > ---
> >  fs/ext4/super.c | 20 ++++++++++++++------
> >  1 file changed, 14 insertions(+), 6 deletions(-)
> > 
> > diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> > index 87205660c5d0..42580643a466 100644
> > --- a/fs/ext4/super.c
> > +++ b/fs/ext4/super.c
> > @@ -1491,12 +1491,20 @@ static void init_once(void *foo)
> >  
> >  static int __init init_inodecache(void)
> >  {
> > -	ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
> > -				sizeof(struct ext4_inode_info), 0,
> > -				SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
> > -				offsetof(struct ext4_inode_info, i_data),
> > -				sizeof_field(struct ext4_inode_info, i_data),
> > -				init_once);
> > +	struct kmem_cache_args args = {
> > +		.align = 0,
> 
> Nit: it's implicit so not necessary.

Right, will do.

> > +		.useroffset = offsetof(struct ext4_inode_info, i_data),
> > +		.usersize = sizeof_field(struct ext4_inode_info, i_data),
> > +		.use_freeptr_offset = true,
> > +		.freeptr_offset = offsetof(struct ext4_inode_info, i_flags),
> > +		.ctor = init_once,
> > +	};
> > +
> > +	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
> > +				sizeof(struct ext4_inode_info),
> > +				&args,
> > +				SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT);
> > +
> >  	if (ext4_inode_cachep == NULL)
> >  		return -ENOMEM;
> >  	return 0;

-- 
Cheers,
Harry / Hyeonggon