diff mbox series

[v5,03/16] fs: introduce alloc_inode_sb() to allocate filesystems specific inode

Message ID 20211220085649.8196-4-songmuchun@bytedance.com (mailing list archive)
State New, archived
Headers show
Series Optimize list lru memory consumption | expand

Commit Message

Muchun Song Dec. 20, 2021, 8:56 a.m. UTC
The allocated inode cache is supposed to be added to its memcg list_lru
which should be allocated as well in advance. That can be done by
kmem_cache_alloc_lru() which allocates object and list_lru. The file
systems is main user of it. So introduce alloc_inode_sb() to allocate
file system specific inodes and set up the inode reclaim context
properly. The file system is supposed to use alloc_inode_sb() to
allocate inodes. In the later patches, we will convert all users to the
new API.

Signed-off-by: Muchun Song <songmuchun@bytedance.com>
---
 Documentation/filesystems/porting.rst |  5 +++++
 fs/inode.c                            |  2 +-
 include/linux/fs.h                    | 11 +++++++++++
 3 files changed, 17 insertions(+), 1 deletion(-)

Comments

Roman Gushchin Jan. 11, 2022, 6:55 p.m. UTC | #1
On Mon, Dec 20, 2021 at 04:56:36PM +0800, Muchun Song wrote:
> The allocated inode cache is supposed to be added to its memcg list_lru
> which should be allocated as well in advance. That can be done by
> kmem_cache_alloc_lru() which allocates object and list_lru. The file
> systems is main user of it. So introduce alloc_inode_sb() to allocate
> file system specific inodes and set up the inode reclaim context
> properly. The file system is supposed to use alloc_inode_sb() to
> allocate inodes. In the later patches, we will convert all users to the
> new API.
> 
> Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> ---
>  Documentation/filesystems/porting.rst |  5 +++++
>  fs/inode.c                            |  2 +-
>  include/linux/fs.h                    | 11 +++++++++++
>  3 files changed, 17 insertions(+), 1 deletion(-)
> 
> diff --git a/Documentation/filesystems/porting.rst b/Documentation/filesystems/porting.rst
> index bf19fd6b86e7..c9c157d7b7bb 100644
> --- a/Documentation/filesystems/porting.rst
> +++ b/Documentation/filesystems/porting.rst
> @@ -45,6 +45,11 @@ typically between calling iget_locked() and unlocking the inode.
>  
>  At some point that will become mandatory.
>  
> +**mandatory**
> +
> +The foo_inode_info should always be allocated through alloc_inode_sb() rather
> +than kmem_cache_alloc() or kmalloc() related.

I'd add a couple of words on why it has to be allocated this way.
> +
>  ---

Reviewed-by: Roman Gushchin <guro@fb.com>

Thanks!
Muchun Song Jan. 12, 2022, 2:54 a.m. UTC | #2
On Wed, Jan 12, 2022 at 2:55 AM Roman Gushchin <guro@fb.com> wrote:
>
> On Mon, Dec 20, 2021 at 04:56:36PM +0800, Muchun Song wrote:
> > The allocated inode cache is supposed to be added to its memcg list_lru
> > which should be allocated as well in advance. That can be done by
> > kmem_cache_alloc_lru() which allocates object and list_lru. The file
> > systems is main user of it. So introduce alloc_inode_sb() to allocate
> > file system specific inodes and set up the inode reclaim context
> > properly. The file system is supposed to use alloc_inode_sb() to
> > allocate inodes. In the later patches, we will convert all users to the
> > new API.
> >
> > Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> > ---
> >  Documentation/filesystems/porting.rst |  5 +++++
> >  fs/inode.c                            |  2 +-
> >  include/linux/fs.h                    | 11 +++++++++++
> >  3 files changed, 17 insertions(+), 1 deletion(-)
> >
> > diff --git a/Documentation/filesystems/porting.rst b/Documentation/filesystems/porting.rst
> > index bf19fd6b86e7..c9c157d7b7bb 100644
> > --- a/Documentation/filesystems/porting.rst
> > +++ b/Documentation/filesystems/porting.rst
> > @@ -45,6 +45,11 @@ typically between calling iget_locked() and unlocking the inode.
> >
> >  At some point that will become mandatory.
> >
> > +**mandatory**
> > +
> > +The foo_inode_info should always be allocated through alloc_inode_sb() rather
> > +than kmem_cache_alloc() or kmalloc() related.
>
> I'd add a couple of words on why it has to be allocated this way.

Will do.

> > +
> >  ---
>
> Reviewed-by: Roman Gushchin <guro@fb.com>

Thanks.
diff mbox series

Patch

diff --git a/Documentation/filesystems/porting.rst b/Documentation/filesystems/porting.rst
index bf19fd6b86e7..c9c157d7b7bb 100644
--- a/Documentation/filesystems/porting.rst
+++ b/Documentation/filesystems/porting.rst
@@ -45,6 +45,11 @@  typically between calling iget_locked() and unlocking the inode.
 
 At some point that will become mandatory.
 
+**mandatory**
+
+The foo_inode_info should always be allocated through alloc_inode_sb() rather
+than kmem_cache_alloc() or kmalloc() related.
+
 ---
 
 **mandatory**
diff --git a/fs/inode.c b/fs/inode.c
index 6b80a51129d5..dcb1e6cad201 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -234,7 +234,7 @@  static struct inode *alloc_inode(struct super_block *sb)
 	if (ops->alloc_inode)
 		inode = ops->alloc_inode(sb);
 	else
-		inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
+		inode = alloc_inode_sb(sb, inode_cachep, GFP_KERNEL);
 
 	if (!inode)
 		return NULL;
diff --git a/include/linux/fs.h b/include/linux/fs.h
index bbf812ce89a8..4592f00ec5e7 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -41,6 +41,7 @@ 
 #include <linux/stddef.h>
 #include <linux/mount.h>
 #include <linux/cred.h>
+#include <linux/slab.h>
 
 #include <asm/byteorder.h>
 #include <uapi/linux/fs.h>
@@ -3178,6 +3179,16 @@  extern void free_inode_nonrcu(struct inode *inode);
 extern int should_remove_suid(struct dentry *);
 extern int file_remove_privs(struct file *);
 
+/*
+ * This must be used for allocating filesystems specific inodes to set
+ * up the inode reclaim context correctly.
+ */
+static inline void *
+alloc_inode_sb(struct super_block *sb, struct kmem_cache *cache, gfp_t gfp)
+{
+	return kmem_cache_alloc_lru(cache, &sb->s_inode_lru, gfp);
+}
+
 extern void __insert_inode_hash(struct inode *, unsigned long hashval);
 static inline void insert_inode_hash(struct inode *inode)
 {