Skip to content

Commit d5743ad

Browse files
chaseyuvijay-suman
authored andcommitted
f2fs: use global inline_xattr_slab instead of per-sb slab cache
[ Upstream commit 1f27ef42bb0b7c0740c5616ec577ec188b8a1d05 ] As Hong Yun reported in mailing list: loop7: detected capacity change from 0 to 131072 ------------[ cut here ]------------ kmem_cache of name 'f2fs_xattr_entry-7:7' already exists WARNING: CPU: 0 PID: 24426 at mm/slab_common.c:110 kmem_cache_sanity_check mm/slab_common.c:109 [inline] WARNING: CPU: 0 PID: 24426 at mm/slab_common.c:110 __kmem_cache_create_args+0xa6/0x320 mm/slab_common.c:307 CPU: 0 UID: 0 PID: 24426 Comm: syz.7.1370 Not tainted 6.17.0-rc4 #1 PREEMPT(full) Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014 RIP: 0010:kmem_cache_sanity_check mm/slab_common.c:109 [inline] RIP: 0010:__kmem_cache_create_args+0xa6/0x320 mm/slab_common.c:307 Call Trace:  __kmem_cache_create include/linux/slab.h:353 [inline]  f2fs_kmem_cache_create fs/f2fs/f2fs.h:2943 [inline]  f2fs_init_xattr_caches+0xa5/0xe0 fs/f2fs/xattr.c:843  f2fs_fill_super+0x1645/0x2620 fs/f2fs/super.c:4918  get_tree_bdev_flags+0x1fb/0x260 fs/super.c:1692  vfs_get_tree+0x43/0x140 fs/super.c:1815  do_new_mount+0x201/0x550 fs/namespace.c:3808  do_mount fs/namespace.c:4136 [inline]  __do_sys_mount fs/namespace.c:4347 [inline]  __se_sys_mount+0x298/0x2f0 fs/namespace.c:4324  do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]  do_syscall_64+0x8e/0x3a0 arch/x86/entry/syscall_64.c:94  entry_SYSCALL_64_after_hwframe+0x76/0x7e The bug can be reproduced w/ below scripts: - mount /dev/vdb /mnt1 - mount /dev/vdc /mnt2 - umount /mnt1 - mounnt /dev/vdb /mnt1 The reason is if we created two slab caches, named f2fs_xattr_entry-7:3 and f2fs_xattr_entry-7:7, and they have the same slab size. Actually, slab system will only create one slab cache core structure which has slab name of "f2fs_xattr_entry-7:3", and two slab caches share the same structure and cache address. So, if we destroy f2fs_xattr_entry-7:3 cache w/ cache address, it will decrease reference count of slab cache, rather than release slab cache entirely, since there is one more user has referenced the cache. Then, if we try to create slab cache w/ name "f2fs_xattr_entry-7:3" again, slab system will find that there is existed cache which has the same name and trigger the warning. Let's changes to use global inline_xattr_slab instead of per-sb slab cache for fixing. Fixes: a999150 ("f2fs: use kmem_cache pool during inline xattr lookups") Cc: stable@kernel.org Reported-by: Hong Yun <yhong@link.cuhk.edu.hk> Tested-by: Hong Yun <yhong@link.cuhk.edu.hk> Signed-off-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org> [ folio => page + different module init/exit ordering ] Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> (cherry picked from commit 474cc3ed37436ddfd63cac8dbffe3b1e219e9100) Signed-off-by: Vijayendra Suman <vijayendra.suman@oracle.com>
1 parent 7be0f5c commit d5743ad

File tree

4 files changed

+24
-36
lines changed

4 files changed

+24
-36
lines changed

fs/f2fs/f2fs.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,9 +1763,6 @@ struct f2fs_sb_info {
17631763

17641764
struct workqueue_struct *post_read_wq; /* post read workqueue */
17651765

1766-
struct kmem_cache *inline_xattr_slab; /* inline xattr entry */
1767-
unsigned int inline_xattr_slab_size; /* default inline xattr slab size */
1768-
17691766
/* For reclaimed segs statistics per each GC mode */
17701767
unsigned int gc_segment_mode; /* GC state for reclaimed segments */
17711768
unsigned int gc_reclaimed_segs[MAX_GC_MODE]; /* Reclaimed segs for each mode */

fs/f2fs/super.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,6 @@ static void f2fs_put_super(struct super_block *sb)
16591659

16601660
destroy_device_list(sbi);
16611661
f2fs_destroy_page_array_cache(sbi);
1662-
f2fs_destroy_xattr_caches(sbi);
16631662
mempool_destroy(sbi->write_io_dummy);
16641663
#ifdef CONFIG_QUOTA
16651664
for (i = 0; i < MAXQUOTAS; i++)
@@ -4170,13 +4169,9 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
41704169
}
41714170
}
41724171

4173-
/* init per sbi slab cache */
4174-
err = f2fs_init_xattr_caches(sbi);
4175-
if (err)
4176-
goto free_io_dummy;
41774172
err = f2fs_init_page_array_cache(sbi);
41784173
if (err)
4179-
goto free_xattr_cache;
4174+
goto free_io_dummy;
41804175

41814176
/* get an inode for meta space */
41824177
sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
@@ -4496,8 +4491,6 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
44964491
sbi->meta_inode = NULL;
44974492
free_page_array_cache:
44984493
f2fs_destroy_page_array_cache(sbi);
4499-
free_xattr_cache:
4500-
f2fs_destroy_xattr_caches(sbi);
45014494
free_io_dummy:
45024495
mempool_destroy(sbi->write_io_dummy);
45034496
free_percpu:
@@ -4664,7 +4657,12 @@ static int __init init_f2fs_fs(void)
46644657
err = f2fs_create_casefold_cache();
46654658
if (err)
46664659
goto free_compress_cache;
4660+
err = f2fs_init_xattr_cache();
4661+
if (err)
4662+
goto free_casefold_cache;
46674663
return 0;
4664+
free_casefold_cache:
4665+
f2fs_destroy_casefold_cache();
46684666
free_compress_cache:
46694667
f2fs_destroy_compress_cache();
46704668
free_compress_mempool:
@@ -4704,6 +4702,7 @@ static int __init init_f2fs_fs(void)
47044702

47054703
static void __exit exit_f2fs_fs(void)
47064704
{
4705+
f2fs_destroy_xattr_cache();
47074706
f2fs_destroy_casefold_cache();
47084707
f2fs_destroy_compress_cache();
47094708
f2fs_destroy_compress_mempool();

fs/f2fs/xattr.c

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
#include "xattr.h"
2424
#include "segment.h"
2525

26+
static struct kmem_cache *inline_xattr_slab;
2627
static void *xattr_alloc(struct f2fs_sb_info *sbi, int size, bool *is_inline)
2728
{
28-
if (likely(size == sbi->inline_xattr_slab_size)) {
29+
if (likely(size == DEFAULT_XATTR_SLAB_SIZE)) {
2930
*is_inline = true;
30-
return f2fs_kmem_cache_alloc(sbi->inline_xattr_slab,
31+
return f2fs_kmem_cache_alloc(inline_xattr_slab,
3132
GFP_F2FS_ZERO, false, sbi);
3233
}
3334
*is_inline = false;
@@ -38,7 +39,7 @@ static void xattr_free(struct f2fs_sb_info *sbi, void *xattr_addr,
3839
bool is_inline)
3940
{
4041
if (is_inline)
41-
kmem_cache_free(sbi->inline_xattr_slab, xattr_addr);
42+
kmem_cache_free(inline_xattr_slab, xattr_addr);
4243
else
4344
kfree(xattr_addr);
4445
}
@@ -818,25 +819,14 @@ int f2fs_setxattr(struct inode *inode, int index, const char *name,
818819
return err;
819820
}
820821

821-
int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi)
822+
int __init f2fs_init_xattr_cache(void)
822823
{
823-
dev_t dev = sbi->sb->s_bdev->bd_dev;
824-
char slab_name[32];
825-
826-
sprintf(slab_name, "f2fs_xattr_entry-%u:%u", MAJOR(dev), MINOR(dev));
827-
828-
sbi->inline_xattr_slab_size = F2FS_OPTION(sbi).inline_xattr_size *
829-
sizeof(__le32) + XATTR_PADDING_SIZE;
830-
831-
sbi->inline_xattr_slab = f2fs_kmem_cache_create(slab_name,
832-
sbi->inline_xattr_slab_size);
833-
if (!sbi->inline_xattr_slab)
834-
return -ENOMEM;
835-
836-
return 0;
824+
inline_xattr_slab = f2fs_kmem_cache_create("f2fs_xattr_entry",
825+
DEFAULT_XATTR_SLAB_SIZE);
826+
return inline_xattr_slab ? 0 : -ENOMEM;
837827
}
838828

839-
void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi)
829+
void f2fs_destroy_xattr_cache(void)
840830
{
841-
kmem_cache_destroy(sbi->inline_xattr_slab);
842-
}
831+
kmem_cache_destroy(inline_xattr_slab);
832+
}

fs/f2fs/xattr.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ struct f2fs_xattr_entry {
8888
F2FS_TOTAL_EXTRA_ATTR_SIZE / sizeof(__le32) - \
8989
DEF_INLINE_RESERVED_SIZE - \
9090
MIN_INLINE_DENTRY_SIZE / sizeof(__le32))
91+
#define DEFAULT_XATTR_SLAB_SIZE (DEFAULT_INLINE_XATTR_ADDRS * \
92+
sizeof(__le32) + XATTR_PADDING_SIZE)
9193

9294
/*
9395
* On-disk structure of f2fs_xattr
@@ -131,8 +133,8 @@ extern int f2fs_setxattr(struct inode *, int, const char *,
131133
extern int f2fs_getxattr(struct inode *, int, const char *, void *,
132134
size_t, struct page *);
133135
extern ssize_t f2fs_listxattr(struct dentry *, char *, size_t);
134-
extern int f2fs_init_xattr_caches(struct f2fs_sb_info *);
135-
extern void f2fs_destroy_xattr_caches(struct f2fs_sb_info *);
136+
int __init f2fs_init_xattr_cache(void);
137+
void f2fs_destroy_xattr_cache(void);
136138
#else
137139

138140
#define f2fs_xattr_handlers NULL
@@ -149,8 +151,8 @@ static inline int f2fs_getxattr(struct inode *inode, int index,
149151
{
150152
return -EOPNOTSUPP;
151153
}
152-
static inline int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi) { return 0; }
153-
static inline void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi) { }
154+
static inline int __init f2fs_init_xattr_cache(void) { return 0; }
155+
static inline void f2fs_destroy_xattr_cache(void) { }
154156
#endif
155157

156158
#ifdef CONFIG_F2FS_FS_SECURITY

0 commit comments

Comments
 (0)