Skip to contentSkip to conversation

ext[24]: MBCache rewrite

PATCH In review Jan Kara 2 participants 14 messages Feb 22, 2016

Subsystems (3)
lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

v3 cover ext[24]: MBCache rewrite

Hello,

since Ted didn't merge the mbcache rewrite patches yet, here is a new version of mbcache rewrite series with some improvements from Andreas Grunbacher. Ted, can you please pick up this series for the next merge window? Thanks!

Changes since v2:
* change key type from unsigned int to u32
* remove hash list head pointer from mbcache entry
* add flag to track whether mbcache entry can get more references
* added cleanup of ext4_mballoc_ready

Changes since v1:
* renamed mbcache2 to mbcache (and all functions and structures) once old mbcache code is removed.
* renamed LRU list since it isn't LRU anymore
* removed unused mb2_cache_entry_delete() function
* updated explanation of mbcache function
* fixed swapped entries in table of benchmark results

---
Full motivation:

Inspired by recent reports [1] of problems with mbcache I had a look into what we could to improve it. I found the current code rather overengineered (counting with single entry being in several indices, having homegrown implementation of rw semaphore, ...).

After some thinking I've decided to just reimplement mbcache instead of improving the original code in small steps since the fundamental changes in locking and layout would be actually harder to review in small steps than in one big chunk and overall the new mbcache is actually pretty simple piece of code (~450 lines).

The result of rewrite is smaller code (almost half the original size), smaller cache entries (7 longs instead of 13), and better performance (see below for details).

For measuring performance of mbcache I have written a stress test (I called it xattr-bench). The test spawns P processes, each process sets xattr for F different files, and the value of xattr is randomly chosen from a pool of V values. Each process runs until it sets extended attribute 50000 times (this is arbitrarily chosen number so that run times for the particular test machine are reasonable). The test machine has 24 CPUs and 64 GB of RAM, the test filesystem was created on ramdisk. Each test has been run 5 times.

I have measured performance for original mbache, new mbcache2 code where LRU is implemented as a simple list, mbcache2 where LRU is implemented using list_lru, and mbcache2 where we keep LRU lazily and just use referenced bit. I have also measured performance when mbcache was completely disabled (to be able to quantify how much gain can some loads get from disabling mbcache). The graphs for different combinations of parameters (I have measured P=1,2,4,8,16,32,64; F=10,100,1000; V=10,100,1000,10000,100000) can be found at [2].

Based on the numbers I have chosen the implementation using LRU with referenced bit for submission. Implementation using list_lru is faster in some heavily contended cases but slower in most of the cases so I figured it is not worth it. My measurements show that completely disabling mbcache can still result in upto ~2x faster execution of the benchmark so even after improvements there is some gain users like Lustre or Ceph could have from completely disabling mbcache.

Here is a comparison table with averages of 5 runs. Measured numbers are in order "old mbcache", "mbcache2 with normal LRU", "mbcache2 with list_lru LRU", "mbcache2 with referenced bit", "disabled mbcache". Note that some numbers for "old mbcache" are not available since the machine just dies due to softlockups under the pressure.

V=10
F\P 1 2 4 8 16 32 64
10 0.158,0.157,0.209,0.155,0.135 0.208,0.196,0.263,0.229,0.154 0.500,0.277,0.364,0.305,0.176 0.798,0.400,0.380,0.384,0.237 3.258,0.584,0.593,0.664,0.500 13.807,1.047,1.029,1.100,0.986 61.339,2.803,3.615,2.994,1.799
100 0.172,0.167,0.161,0.185,0.126 0.279,0.222,0.244,0.222,0.156 0.520,0.275,0.275,0.273,0.199 0.825,0.341,0.408,0.333,0.217 2.981,0.505,0.523,0.523,0.315 12.022,1.202,1.210,1.125,1.293 44.641,2.943,2.869,3.337,13.056
1000 0.185,0.174,0.187,0.153,0.160 0.297,0.239,0.247,0.227,0.176 0.445,0.283,0.276,0.272,0.957 0.767,0.340,0.357,0.324,1.975 2.329,0.480,0.498,0.476,5.391 6.342,1.198,1.235,1.204,8.283 16.440,3.888,3.817,3.896,17.878

V=100
F\P 1 2 4 8 16 32 64
10 0.162,0.153,0.180,0.126,0.126 0.200,0.186,0.241,0.165,0.154 0.362,0.257,0.313,0.208,0.181 0.671,0.496,0.422,0.379,0.194 1.433,0.943,0.773,0.676,0.570 3.801,1.345,1.353,1.221,1.021 7.938,2.501,2.700,2.484,1.790
100 0.153,0.160,0.164,0.130,0.144 0.221,0.199,0.232,0.217,0.166 0.404,0.264,0.300,0.270,0.180 0.945,0.379,0.400,0.322,0.240 1.556,0.485,0.512,0.496,0.339 3.761,1.156,1.214,1.197,1.301 7.901,2.484,2.508,2.526,13.039
1000 0.215,0.191,0.205,0.212,0.156 0.303,0.246,0.246,0.247,0.182 0.471,0.288,0.305,0.300,0.896 0.960,0.347,0.375,0.347,1.892 1.647,0.479,0.530,0.509,4.744 3.916,1.176,1.288,1.205,8.300 8.058,3.160,3.232,3.200,17.616

V=1000
F\P 1 2 4 8 16 32 64
10 0.151,0.129,0.179,0.160,0.130 0.210,0.163,0.248,0.193,0.155 0.326,0.245,0.313,0.204,0.191 0.685,0.521,0.493,0.365,0.210 1.284,0.859,0.772,0.613,0.389 3.087,2.251,1.307,1.745,0.896 6.451,4.801,2.693,3.736,1.806
100 0.154,0.153,0.156,0.159,0.120 0.211,0.191,0.232,0.194,0.158 0.276,0.282,0.286,0.228,0.170 0.687,0.506,0.496,0.400,0.259 1.202,0.877,0.712,0.632,0.326 3.259,1.954,1.564,1.336,1.255 8.738,2.887,14.421,3.111,13.175
1000 0.145,0.179,0.184,0.175,0.156 0.202,0.222,0.218,0.220,0.174 0.449,0.319,0.836,0.276,0.965 0.899,0.333,0.793,0.353,2.002 1.577,0.524,0.529,0.523,4.676 4.221,1.240,1.280,1.281,8.371 9.782,3.579,3.605,3.585,17.425

V=10000
F\P 1 2 4 8 16 32 64
10 0.161,0.154,0.204,0.158,0.137 0.198,0.190,0.271,0.190,0.153 0.296,0.256,0.340,0.229,0.164 0.662,0.480,0.475,0.368,0.239 1.192,0.818,0.785,0.646,0.349 2.989,2.200,1.237,1.725,0.961 6.362,4.746,2.666,3.718,1.793
100 0.176,0.174,0.136,0.155,0.123 0.236,0.203,0.202,0.188,0.165 0.326,0.255,0.267,0.241,0.182 0.696,0.511,0.415,0.387,0.213 1.183,0.855,0.679,0.689,0.330 4.205,3.444,1.444,2.760,1.249 19.510,17.760,15.203,17.387,12.828
1000 0.199,0.183,0.183,0.183,0.164 0.240,0.227,0.225,0.226,0.179 1.159,1.014,1.014,1.036,0.985 2.286,2.154,1.987,2.019,1.997 6.023,6.039,6.594,5.657,5.069 N/A,10.933,9.272,10.382,8.305 N/A,36.620,27.886,36.165,17.683

V=100000
F\P 1 2 4 8 16 32 64
10 0.171,0.162,0.220,0.163,0.143 0.204,0.198,0.272,0.192,0.154 0.285,0.230,0.318,0.218,0.172 0.692,0.500,0.505,0.367,0.210 1.225,0.881,0.827,0.687,0.338 2.990,2.243,1.266,1.696,0.942 6.379,4.771,2.609,3.722,1.778
100 0.151,0.171,0.176,0.171,0.153 0.220,0.210,0.226,0.201,0.167 0.295,0.255,0.265,0.242,0.175 0.720,0.518,0.417,0.387,0.221 1.226,0.844,0.689,0.672,0.343 3.423,2.831,1.392,2.370,1.354 19.234,17.544,15.419,16.700,13.172
1000 0.192,0.189,0.188,0.184,0.164 0.249,0.225,0.223,0.218,0.178 1.162,1.043,1.031,1.024,1.003 2.257,2.093,2.180,2.004,1.960 5.853,4.997,6.143,5.315,5.350 N/A,10.399,8.578,9.190,8.309 N/A,32.198,19.465,19.194,17.210

Thoughs, opinions, comments welcome.

Honza

[1] https://bugzilla.kernel.org/show_bug.cgi?id=107301
[2] http://beta.suse.com/private/jack/mbcache2/

author reviewer
#1 lore raw

PATCH v3 1/11 mbcache2: Reimplement mbcache

Original mbcache was designed to have more features than what ext? filesystems ended up using. It supported entry being in more hashes, it had a home-grown rwlocking of each entry, and one cache could cache entries from multiple filesystems. This genericity also resulted in more complex locking, larger cache entries, and generally more code complexity.

This is reimplementation of the mbcache functionality to exactly fit the purpose ext? filesystems use it for. Cache entries are now considerably smaller (7 instead of 13 longs), the code is considerably smaller as well (414 vs 913 lines of code), and IMO also simpler. The new code is also much more lightweight.

I have measured the speed using artificial xattr-bench benchmark, which spawns P processes, each process sets xattr for F different files, and the value of xattr is randomly chosen from a pool of V values. Averages of runtimes for 5 runs for various combinations of parameters are below. The first value in each cell is old mbache, the second value is the new mbcache.

V=10
F\P 1 2 4 8 16 32 64
10 0.158,0.157 0.208,0.196 0.500,0.277 0.798,0.400 3.258,0.584 13.807,1.047 61.339,2.803
100 0.172,0.167 0.279,0.222 0.520,0.275 0.825,0.341 2.981,0.505 12.022,1.202 44.641,2.943
1000 0.185,0.174 0.297,0.239 0.445,0.283 0.767,0.340 2.329,0.480 6.342,1.198 16.440,3.888

V=100
F\P 1 2 4 8 16 32 64
10 0.162,0.153 0.200,0.186 0.362,0.257 0.671,0.496 1.433,0.943 3.801,1.345 7.938,2.501
100 0.153,0.160 0.221,0.199 0.404,0.264 0.945,0.379 1.556,0.485 3.761,1.156 7.901,2.484
1000 0.215,0.191 0.303,0.246 0.471,0.288 0.960,0.347 1.647,0.479 3.916,1.176 8.058,3.160

V=1000
F\P 1 2 4 8 16 32 64
10 0.151,0.129 0.210,0.163 0.326,0.245 0.685,0.521 1.284,0.859 3.087,2.251 6.451,4.801
100 0.154,0.153 0.211,0.191 0.276,0.282 0.687,0.506 1.202,0.877 3.259,1.954 8.738,2.887
1000 0.145,0.179 0.202,0.222 0.449,0.319 0.899,0.333 1.577,0.524 4.221,1.240 9.782,3.579

V=10000
F\P 1 2 4 8 16 32 64
10 0.161,0.154 0.198,0.190 0.296,0.256 0.662,0.480 1.192,0.818 2.989,2.200 6.362,4.746
100 0.176,0.174 0.236,0.203 0.326,0.255 0.696,0.511 1.183,0.855 4.205,3.444 19.510,17.760
1000 0.199,0.183 0.240,0.227 1.159,1.014 2.286,2.154 6.023,6.039 ---,10.933 ---,36.620

V=100000
F\P 1 2 4 8 16 32 64
10 0.171,0.162 0.204,0.198 0.285,0.230 0.692,0.500 1.225,0.881 2.990,2.243 6.379,4.771
100 0.151,0.171 0.220,0.210 0.295,0.255 0.720,0.518 1.226,0.844 3.423,2.831 19.234,17.544
1000 0.192,0.189 0.249,0.225 1.162,1.043 2.257,2.093 5.853,4.997 ---,10.399 ---,32.198

We see that the new code is faster in pretty much all the cases and starting from 4 processes there are significant gains with the new code resulting in upto 20-times shorter runtimes. Also for large numbers of cached entries all values for the old code could not be measured as the kernel started hitting softlockups and died before the test completed.

---
 fs/Makefile              |   2 +-
 fs/mbcache2.c            | 359 +++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/mbcache2.h |  50 +++++++
 3 files changed, 410 insertions(+), 1 deletion(-)
 create mode 100644 fs/mbcache2.c
 create mode 100644 include/linux/mbcache2.h
fs/Makefile +1 -1
index 79f522575cba..15b3d6c4e46a 100644
@@ -41,7 +41,7 @@ obj-$(CONFIG_COMPAT_BINFMT_ELF)	+= compat_binfmt_elf.o
 obj-$(CONFIG_BINFMT_ELF_FDPIC)	+= binfmt_elf_fdpic.o
 obj-$(CONFIG_BINFMT_FLAT)	+= binfmt_flat.o
 
-obj-$(CONFIG_FS_MBCACHE)	+= mbcache.o
+obj-$(CONFIG_FS_MBCACHE)	+= mbcache.o mbcache2.o
 obj-$(CONFIG_FS_POSIX_ACL)	+= posix_acl.o
 obj-$(CONFIG_NFS_COMMON)	+= nfs_common/
 obj-$(CONFIG_COREDUMP)		+= coredump.o
fs/mbcache2.c new file +359 -0

359 lines not on this page · raw message

include/linux/mbcache2.h new file +50 -0

50 lines not on this page · raw message

Signature · 1 line
2.6.2
author reviewer
#2 lore raw

PATCH v3 2/11 ext4: Convert to mbcache2

In reply to Jan Kara

The conversion is generally straightforward. The only tricky part is that xattr block corresponding to found mbcache entry can get freed before we get buffer lock for that block. So we have to check whether the entry is still valid after getting buffer lock.

---
 fs/ext4/ext4.h  |   2 +-
 fs/ext4/super.c |   7 ++-
 fs/ext4/xattr.c | 136 ++++++++++++++++++++++++++++----------------------------
 fs/ext4/xattr.h |   5 +--
 4 files changed, 75 insertions(+), 75 deletions(-)
fs/ext4/ext4.h +1 -1
index 0662b285dc8a..b53cbc05b172 100644
@@ -1468,7 +1468,7 @@ struct ext4_sb_info {
 	struct list_head s_es_list;	/* List of inodes with reclaimable extents */
 	long s_es_nr_inode;
 	struct ext4_es_stats s_es_stats;
-	struct mb_cache *s_mb_cache;
+	struct mb2_cache *s_mb_cache;
 	spinlock_t s_es_lock ____cacheline_aligned_in_smp;
 
 	/* Ratelimit ext4 messages. */
fs/ext4/super.c +5 -2
index 3ed01ec011d7..ecc37e103435 100644
@@ -844,7 +844,6 @@ static void ext4_put_super(struct super_block *sb)
 	ext4_release_system_zone(sb);
 	ext4_mb_release(sb);
 	ext4_ext_release(sb);
-	ext4_xattr_put_super(sb);
 
 	if (!(sb->s_flags & MS_RDONLY)) {
 		ext4_clear_feature_journal_needs_recovery(sb);
@@ -3797,7 +3796,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
 
 no_journal:
 	if (ext4_mballoc_ready) {
-		sbi->s_mb_cache = ext4_xattr_create_cache(sb->s_id);
+		sbi->s_mb_cache = ext4_xattr_create_cache();
 		if (!sbi->s_mb_cache) {
 			ext4_msg(sb, KERN_ERR, "Failed to create an mb_cache");
 			goto failed_mount_wq;
@@ -4027,6 +4026,10 @@ failed_mount4:
 	if (EXT4_SB(sb)->rsv_conversion_wq)
 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
 failed_mount_wq:
+	if (sbi->s_mb_cache) {
+		ext4_xattr_destroy_cache(sbi->s_mb_cache);
+		sbi->s_mb_cache = NULL;
+	}
 	if (sbi->s_journal) {
 		jbd2_journal_destroy(sbi->s_journal);
 		sbi->s_journal = NULL;
fs/ext4/xattr.c +67 -69

276 lines not on this page · raw message

fs/ext4/xattr.h +2 -3
index ddc0957760ba..10b0f7323ed6 100644
@@ -108,7 +108,6 @@ extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_
 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
 
 extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
-extern void ext4_xattr_put_super(struct super_block *);
 
 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
 			    struct ext4_inode *raw_inode, handle_t *handle);
@@ -124,8 +123,8 @@ extern int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
 				       struct ext4_xattr_info *i,
 				       struct ext4_xattr_ibody_find *is);
 
-extern struct mb_cache *ext4_xattr_create_cache(char *name);
-extern void ext4_xattr_destroy_cache(struct mb_cache *);
+extern struct mb2_cache *ext4_xattr_create_cache(void);
+extern void ext4_xattr_destroy_cache(struct mb2_cache *);
 
 #ifdef CONFIG_EXT4_FS_SECURITY
 extern int ext4_init_security(handle_t *handle, struct inode *inode,
Signature · 1 line
2.6.2
author reviewer
#3 lore raw

PATCH v3 3/11 ext2: Convert to mbcache2

In reply to Jan Kara

The conversion is generally straightforward. We convert filesystem from a global cache to per-fs one. Similarly to ext4 the tricky part is that xattr block corresponding to found mbcache entry can get freed before we get buffer lock for that block. So we have to check whether the entry is still valid after getting the buffer lock.

---
 fs/ext2/ext2.h  |   3 ++
 fs/ext2/super.c |  25 ++++++----
 fs/ext2/xattr.c | 143 ++++++++++++++++++++++++++------------------------------
 fs/ext2/xattr.h |  21 ++-------
 4 files changed, 92 insertions(+), 100 deletions(-)
fs/ext2/ext2.h +3 -0
index 4c69c94cafd8..f98ce7e60a0f 100644
@@ -61,6 +61,8 @@ struct ext2_block_alloc_info {
 #define rsv_start rsv_window._rsv_start
 #define rsv_end rsv_window._rsv_end
 
+struct mb2_cache;
+
 /*
  * second extended-fs super-block data in memory
  */
@@ -111,6 +113,7 @@ struct ext2_sb_info {
 	 * of the mount options.
 	 */
 	spinlock_t s_lock;
+	struct mb2_cache *s_mb_cache;
 };
 
 static inline spinlock_t *
fs/ext2/super.c +17 -8
index 2a188413a2b0..b78caf25f746 100644
@@ -131,7 +131,10 @@ static void ext2_put_super (struct super_block * sb)
 
 	dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
 
-	ext2_xattr_put_super(sb);
+	if (sbi->s_mb_cache) {
+		ext2_xattr_destroy_cache(sbi->s_mb_cache);
+		sbi->s_mb_cache = NULL;
+	}
 	if (!(sb->s_flags & MS_RDONLY)) {
 		struct ext2_super_block *es = sbi->s_es;
 
@@ -1104,6 +1107,14 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
 		ext2_msg(sb, KERN_ERR, "error: insufficient memory");
 		goto failed_mount3;
 	}
+
+#ifdef CONFIG_EXT2_FS_XATTR
+	sbi->s_mb_cache = ext2_xattr_create_cache();
+	if (!sbi->s_mb_cache) {
+		ext2_msg(sb, KERN_ERR, "Failed to create an mb_cache");
+		goto failed_mount3;
+	}
+#endif
 	/*
 	 * set up enough so that it can read an inode
 	 */
@@ -1149,6 +1160,8 @@ cantfind_ext2:
 			sb->s_id);
 	goto failed_mount;
 failed_mount3:
+	if (sbi->s_mb_cache)
+		ext2_xattr_destroy_cache(sbi->s_mb_cache);
 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
 	percpu_counter_destroy(&sbi->s_dirs_counter);
@@ -1555,20 +1568,17 @@ MODULE_ALIAS_FS("ext2");
 
 static int __init init_ext2_fs(void)
 {
-	int err = init_ext2_xattr();
-	if (err)
-		return err;
+	int err;
+
 	err = init_inodecache();
 	if (err)
-		goto out1;
+		return err;
         err = register_filesystem(&ext2_fs_type);
 	if (err)
 		goto out;
 	return 0;
 out:
 	destroy_inodecache();
-out1:
-	exit_ext2_xattr();
 	return err;
 }
 
@@ -1576,7 +1586,6 @@ static void __exit exit_ext2_fs(void)
 {
 	unregister_filesystem(&ext2_fs_type);
 	destroy_inodecache();
-	exit_ext2_xattr();
 }
 
 MODULE_AUTHOR("Remy Card and others");
fs/ext2/xattr.c +67 -76

322 lines not on this page · raw message

fs/ext2/xattr.h +4 -13
index 60edf298644e..6ea38aa9563a 100644
@@ -53,6 +53,8 @@ struct ext2_xattr_entry {
 #define EXT2_XATTR_SIZE(size) \
 	(((size) + EXT2_XATTR_ROUND) & ~EXT2_XATTR_ROUND)
 
+struct mb2_cache;
+
 # ifdef CONFIG_EXT2_FS_XATTR
 
 extern const struct xattr_handler ext2_xattr_user_handler;
@@ -65,10 +67,9 @@ extern int ext2_xattr_get(struct inode *, int, const char *, void *, size_t);
 extern int ext2_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
 
 extern void ext2_xattr_delete_inode(struct inode *);
-extern void ext2_xattr_put_super(struct super_block *);
 
-extern int init_ext2_xattr(void);
-extern void exit_ext2_xattr(void);
+extern struct mb2_cache *ext2_xattr_create_cache(void);
+extern void ext2_xattr_destroy_cache(struct mb2_cache *cache);
 
 extern const struct xattr_handler *ext2_xattr_handlers[];
 
@@ -93,19 +94,7 @@ ext2_xattr_delete_inode(struct inode *inode)
 {
 }
 
-static inline void
-ext2_xattr_put_super(struct super_block *sb)
-{
-}
-
-static inline int
-init_ext2_xattr(void)
-{
-	return 0;
-}
author reviewer
#4 lore raw

PATCH v3 4/11 mbcache: Remove

In reply to Jan Kara

Both ext2 and ext4 are now converted to mbcache2. Remove the old mbcache code.

---
 fs/Makefile             |   2 +-
 fs/mbcache.c            | 858 ------------------------------------------------
 include/linux/mbcache.h |  55 ----
 3 files changed, 1 insertion(+), 914 deletions(-)
 delete mode 100644 fs/mbcache.c
 delete mode 100644 include/linux/mbcache.h
fs/Makefile +1 -1
index 15b3d6c4e46a..59b844007fbc 100644
@@ -41,7 +41,7 @@ obj-$(CONFIG_COMPAT_BINFMT_ELF)	+= compat_binfmt_elf.o
 obj-$(CONFIG_BINFMT_ELF_FDPIC)	+= binfmt_elf_fdpic.o
 obj-$(CONFIG_BINFMT_FLAT)	+= binfmt_flat.o
 
-obj-$(CONFIG_FS_MBCACHE)	+= mbcache.o mbcache2.o
+obj-$(CONFIG_FS_MBCACHE)	+= mbcache2.o
 obj-$(CONFIG_FS_POSIX_ACL)	+= posix_acl.o
 obj-$(CONFIG_NFS_COMMON)	+= nfs_common/
 obj-$(CONFIG_COREDUMP)		+= coredump.o
fs/mbcache.c deleted +0 -858

858 lines not on this page · raw message

include/linux/mbcache.h deleted +0 -40
deleted file mode 100644
index 6a392e7a723a..000000000000
@@ -1,55 +0,0 @@
-/*
-  File: linux/mbcache.h
-
-  (C) 2001 by Andreas Gruenbacher, <[email protected]>
-*/
-struct mb_cache_entry {
-	struct list_head		e_lru_list;
-	struct mb_cache			*e_cache;
-	unsigned short			e_used;
-	unsigned short			e_queued;
-	atomic_t			e_refcnt;
-	struct block_device		*e_bdev;
-	sector_t			e_block;
-	struct hlist_bl_node		e_block_list;
-	struct {
-		struct hlist_bl_node	o_list;
-		unsigned int		o_key;
-	} e_index;
-	struct hlist_bl_head		*e_block_hash_p;
-	struct hlist_bl_head		*e_index_hash_p;
-};
-
-struct mb_cache {
-	struct list_head		c_cache_list;
-	const char			*c_name;
-	atomic_t			c_entry_count;
-	int				c_max_entries;
-	int				c_bucket_bits;
-	struct kmem_cache		*c_entry_cache;
-	struct hlist_bl_head		*c_block_hash;
-	struct hlist_bl_head		*c_index_hash;
-};
-
-/* Functions on caches */
-
-struct mb_cache *mb_cache_create(const char *, int);
-void mb_cache_shrink(struct block_device *);
-void mb_cache_destroy(struct mb_cache *);
-
-/* Functions on cache entries */
author reviewer
#5 lore raw

PATCH v3 5/11 mbcache2: Limit cache size

In reply to Jan Kara

So far number of entries in mbcache is limited only by the pressure from the shrinker. Since too many entries degrade the hash table and generally we expect that caching more entries has diminishing returns, limit number of entries the same way as in the old mbcache to 16 * hash table size.

Once we exceed the desired maximum number of entries, we schedule a backround work to reclaim entries. If the background work cannot keep up and the number of entries exceeds two times the desired maximum, we reclaim some entries directly when allocating a new entry.

---
 fs/mbcache2.c | 50 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 45 insertions(+), 5 deletions(-)
fs/mbcache2.c +45 -5
index 5c3e1a8c38f6..3e3198d6b9d6 100644
@@ -4,6 +4,7 @@
 #include <linux/list_bl.h>
 #include <linux/module.h>
 #include <linux/sched.h>
+#include <linux/workqueue.h>
 #include <linux/mbcache2.h>
 
 /*
@@ -27,16 +28,29 @@ struct mb2_cache {
 	struct hlist_bl_head	*c_hash;
 	/* log2 of hash table size */
 	int			c_bucket_bits;
+	/* Maximum entries in cache to avoid degrading hash too much */
+	int			c_max_entries;
 	/* Protects c_lru_list, c_entry_count */
 	spinlock_t		c_lru_list_lock;
 	struct list_head	c_lru_list;
 	/* Number of entries in cache */
 	unsigned long		c_entry_count;
 	struct shrinker		c_shrink;
+	/* Work for shrinking when the cache has too many entries */
+	struct work_struct	c_shrink_work;
 };
 
 static struct kmem_cache *mb2_entry_cache;
 
+static unsigned long mb2_cache_shrink(struct mb2_cache *cache,
+				      unsigned int nr_to_scan);
+
+/*
+ * Number of entries to reclaim synchronously when there are too many entries
+ * in cache
+ */
+#define SYNC_SHRINK_BATCH 64
+
 /*
  * mb2_cache_entry_create - create entry in cache
  * @cache - cache where the entry should be created
@@ -55,6 +69,13 @@ int mb2_cache_entry_create(struct mb2_cache *cache, gfp_t mask, u32 key,
 	struct hlist_bl_node *dup_node;
 	struct hlist_bl_head *head;
 
+	/* Schedule background reclaim if there are too many entries */
+	if (cache->c_entry_count >= cache->c_max_entries)
+		schedule_work(&cache->c_shrink_work);
+	/* Do some sync reclaim if background reclaim cannot keep up */
+	if (cache->c_entry_count >= 2*cache->c_max_entries)
+		mb2_cache_shrink(cache, SYNC_SHRINK_BATCH);
+
 	entry = kmem_cache_alloc(mb2_entry_cache, mask);
 	if (!entry)
 		return -ENOMEM;
@@ -223,12 +244,9 @@ static unsigned long mb2_cache_count(struct shrinker *shrink,
 }
 
 /* Shrink number of entries in cache */
-static unsigned long mb2_cache_scan(struct shrinker *shrink,
-				    struct shrink_control *sc)
+static unsigned long mb2_cache_shrink(struct mb2_cache *cache,
+				      unsigned int nr_to_scan)
 {
-	int nr_to_scan = sc->nr_to_scan;
-	struct mb2_cache *cache = container_of(shrink, struct mb2_cache,
-					      c_shrink);
 	struct mb2_cache_entry *entry;
 	struct hlist_bl_head *head;
 	unsigned int shrunk = 0;
@@ -261,6 +279,25 @@ static unsigned long mb2_cache_scan(struct shrinker *shrink,
 	return shrunk;
 }
 
+static unsigned long mb2_cache_scan(struct shrinker *shrink,
+				    struct shrink_control *sc)
+{
+	int nr_to_scan = sc->nr_to_scan;
+	struct mb2_cache *cache = container_of(shrink, struct mb2_cache,
+					      c_shrink);
+	return mb2_cache_shrink(cache, nr_to_scan);
+}
+
+/* We shrink 1/X of the cache when we have too many entries in it */
+#define SHRINK_DIVISOR 16
+
+static void mb2_cache_shrink_worker(struct work_struct *work)
+{
+	struct mb2_cache *cache = container_of(work, struct mb2_cache,
+					       c_shrink_work);
+	mb2_cache_shrink(cache, cache->c_max_entries / SHRINK_DIVISOR);
+}
+
 /*
  * mb2_cache_create - create cache
  * @bucket_bits: log2 of the hash table size
@@ -280,6 +317,7 @@ struct mb2_cache *mb2_cache_create(int bucket_bits)
 	if (!cache)
 		goto err_out;
 	cache->c_bucket_bits = bucket_bits;
+	cache->c_max_entries = bucket_count << 4;
 	INIT_LIST_HEAD(&cache->c_lru_list);
 	spin_lock_init(&cache->c_lru_list_lock);
 	cache->c_hash = kmalloc(bucket_count * sizeof(struct hlist_bl_head),
@@ -296,6 +334,8 @@ struct mb2_cache *mb2_cache_create(int bucket_bits)
 	cache->c_shrink.seeks = DEFAULT_SEEKS;
 	register_shrinker(&cache->c_shrink);
 
+	INIT_WORK(&cache->c_shrink_work, mb2_cache_shrink_worker);
+
 	return cache;
 
 err_out:
Signature · 1 line
2.6.2
author reviewer
#6 lore raw

PATCH v3 6/11 mbcache2: Use referenced bit instead of LRU

In reply to Jan Kara

Currently we maintain perfect LRU list by moving entry to the tail of the list when it gets used. However these operations on cache-global list are relatively expensive.

In this patch we switch to lazy updates of LRU list. Whenever entry gets used, we set a referenced bit in it. When reclaiming entries, we give referenced entries another round in the LRU. Since the list is not a real LRU anymore, rename it to just 'list'.

In my testing this logic gives about 30% boost to workloads with mostly unique xattr blocks (e.g. xattr-bench with 10 files and 10000 unique xattr values).

---
 fs/mbcache2.c            | 87 +++++++++++++++++++++++++++++++-----------------
 include/linux/mbcache2.h | 11 +++---
 2 files changed, 63 insertions(+), 35 deletions(-)
fs/mbcache2.c +56 -31
index 3e3198d6b9d6..49f7a6feaa83 100644
@@ -30,9 +30,9 @@ struct mb2_cache {
 	int			c_bucket_bits;
 	/* Maximum entries in cache to avoid degrading hash too much */
 	int			c_max_entries;
-	/* Protects c_lru_list, c_entry_count */
-	spinlock_t		c_lru_list_lock;
-	struct list_head	c_lru_list;
+	/* Protects c_list, c_entry_count */
+	spinlock_t		c_list_lock;
+	struct list_head	c_list;
 	/* Number of entries in cache */
 	unsigned long		c_entry_count;
 	struct shrinker		c_shrink;
@@ -45,6 +45,29 @@ static struct kmem_cache *mb2_entry_cache;
 static unsigned long mb2_cache_shrink(struct mb2_cache *cache,
 				      unsigned int nr_to_scan);
 
+static inline bool mb2_cache_entry_referenced(struct mb2_cache_entry *entry)
+{
+	return entry->_e_hash_list_head & 1;
+}
+
+static inline void mb2_cache_entry_set_referenced(struct mb2_cache_entry *entry)
+{
+	entry->_e_hash_list_head |= 1;
+}
+
+static inline void mb2_cache_entry_clear_referenced(
+					struct mb2_cache_entry *entry)
+{
+	entry->_e_hash_list_head &= ~1;
+}
+
+static inline struct hlist_bl_head *mb2_cache_entry_head(
+					struct mb2_cache_entry *entry)
+{
+	return (struct hlist_bl_head *)
+			(entry->_e_hash_list_head & ~1);
+}
+
 /*
  * Number of entries to reclaim synchronously when there are too many entries
  * in cache
@@ -80,13 +103,13 @@ int mb2_cache_entry_create(struct mb2_cache *cache, gfp_t mask, u32 key,
 	if (!entry)
 		return -ENOMEM;
 
-	INIT_LIST_HEAD(&entry->e_lru_list);
+	INIT_LIST_HEAD(&entry->e_list);
 	/* One ref for hash, one ref returned */
 	atomic_set(&entry->e_refcnt, 1);
 	entry->e_key = key;
 	entry->e_block = block;
 	head = &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
-	entry->e_hash_list_head = head;
+	entry->_e_hash_list_head = (unsigned long)head;
 	hlist_bl_lock(head);
 	hlist_bl_for_each_entry(dup, dup_node, head, e_hash_list) {
 		if (dup->e_key == key && dup->e_block == block) {
@@ -98,12 +121,12 @@ int mb2_cache_entry_create(struct mb2_cache *cache, gfp_t mask, u32 key,
 	hlist_bl_add_head(&entry->e_hash_list, head);
 	hlist_bl_unlock(head);
 
-	spin_lock(&cache->c_lru_list_lock);
-	list_add_tail(&entry->e_lru_list, &cache->c_lru_list);
+	spin_lock(&cache->c_list_lock);
+	list_add_tail(&entry->e_list, &cache->c_list);
 	/* Grab ref for LRU list */
 	atomic_inc(&entry->e_refcnt);
 	cache->c_entry_count++;
-	spin_unlock(&cache->c_lru_list_lock);
+	spin_unlock(&cache->c_list_lock);
 
 	return 0;
 }
@@ -124,7 +147,7 @@ static struct mb2_cache_entry *__entry_find(struct mb2_cache *cache,
 	struct hlist_bl_head *head;
 
 	if (entry)
-		head = entry->e_hash_list_head;
+		head = mb2_cache_entry_head(entry);
 	else
 		head = &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
 	hlist_bl_lock(head);
@@ -203,13 +226,13 @@ void mb2_cache_entry_delete_block(struct mb2_cache *cache, u32 key,
 			/* We keep hash list reference to keep entry alive */
 			hlist_bl_del_init(&entry->e_hash_list);
 			hlist_bl_unlock(head);
-			spin_lock(&cache->c_lru_list_lock);
-			if (!list_empty(&entry->e_lru_list)) {
-				list_del_init(&entry->e_lru_list);
+			spin_lock(&cache->c_list_lock);
+			if (!list_empty(&entry->e_list)) {
+				list_del_init(&entry->e_list);
 				cache->c_entry_count--;
 				atomic_dec(&entry->e_refcnt);
 			}
-			spin_unlock(&cache->c_lru_list_lock);
+			spin_unlock(&cache->c_list_lock);
 			mb2_cache_entry_put(cache, entry);
 			return;
 		}
@@ -222,15 +245,12 @@ EXPORT_SYMBOL(mb2_cache_entry_delete_block);
  * @cache - cache the entry belongs to
  * @entry - entry that got used
  *
- * Move entry in lru list to reflect the fact that it was used.
+ * Marks entry as used to give hit higher chances of surviving in cache.
  */
 void mb2_cache_entry_touch(struct mb2_cache *cache,
 			   struct mb2_cache_entry *entry)
 {
-	spin_lock(&cache->c_lru_list_lock);
-	if (!list_empty(&entry->e_lru_list))
-		list_move_tail(&cache->c_lru_list, &entry->e_lru_list);
-	spin_unlock(&cache->c_lru_list_lock);
+	mb2_cache_entry_set_referenced(entry);
 }
 EXPORT_SYMBOL(mb2_cache_entry_touch);
 
@@ -251,18 +271,23 @@ static unsigned long mb2_cache_shrink(struct mb2_cache *cache,
 	struct hlist_bl_head *head;
 	unsigned int shrunk = 0;
 
-	spin_lock(&cache->c_lru_list_lock);
-	while (nr_to_scan-- && !list_empty(&cache->c_lru_list)) {
-		entry = list_first_entry(&cache->c_lru_list,
-					 struct mb2_cache_entry, e_lru_list);
-		list_del_init(&entry->e_lru_list);
+	spin_lock(&cache->c_list_lock);
+	while (nr_to_scan-- && !list_empty(&cache->c_list)) {
+		entry = list_first_entry(&cache->c_list,
+					 struct mb2_cache_entry, e_list);
+		if (mb2_cache_entry_referenced(entry)) {
+			mb2_cache_entry_clear_referenced(entry);
+			list_move_tail(&cache->c_list, &entry->e_list);
+			continue;
+		}
+		list_del_init(&entry->e_list);
 		cache->c_entry_count--;
 		/*
 		 * We keep LRU list reference so that entry doesn't go away
 		 * from under us.
 		 */
-		spin_unlock(&cache->c_lru_list_lock);
-		head = entry->e_hash_list_head;
+		spin_unlock(&cache->c_list_lock);
+		head = mb2_cache_entry_head(entry);
 		hlist_bl_lock(head);
 		if (!hlist_bl_unhashed(&entry->e_hash_list)) {
 			hlist_bl_del_init(&entry->e_hash_list);
@@ -272,9 +297,9 @@ static unsigned long mb2_cache_shrink(struct mb2_cache *cache,
 		if (mb2_cache_entry_put(cache, entry))
 			shrunk++;
 		cond_resched();
-		spin_lock(&cache->c_lru_list_lock);
+		spin_lock(&cache->c_list_lock);
 	}
-	spin_unlock(&cache->c_lru_list_lock);
+	spin_unlock(&cache->c_list_lock);
 
 	return shrunk;
 }
@@ -318,8 +343,8 @@ struct mb2_cache *mb2_cache_create(int bucket_bits)
 		goto err_out;
 	cache->c_bucket_bits = bucket_bits;
 	cache->c_max_entries = bucket_count << 4;
-	INIT_LIST_HEAD(&cache->c_lru_list);
-	spin_lock_init(&cache->c_lru_list_lock);
+	INIT_LIST_HEAD(&cache->c_list);
+	spin_lock_init(&cache->c_list_lock);
 	cache->c_hash = kmalloc(bucket_count * sizeof(struct hlist_bl_head),
 				GFP_KERNEL);
 	if (!cache->c_hash) {
@@ -361,13 +386,13 @@ void mb2_cache_destroy(struct mb2_cache *cache)
 	 * We don't bother with any locking. Cache must not be used at this
 	 * point.
 	 */
-	list_for_each_entry_safe(entry, next, &cache->c_lru_list, e_lru_list) {
+	list_for_each_entry_safe(entry, next, &cache->c_list, e_list) {
 		if (!hlist_bl_unhashed(&entry->e_hash_list)) {
 			hlist_bl_del_init(&entry->e_hash_list);
 			atomic_dec(&entry->e_refcnt);
 		} else
 			WARN_ON(1);
-		list_del(&entry->e_lru_list);
+		list_del(&entry->e_list);
 		WARN_ON(atomic_read(&entry->e_refcnt) != 1);
 		mb2_cache_entry_put(cache, entry);
 	}
include/linux/mbcache2.h +7 -4
index b6f160ff2533..c934843a6a31 100644
@@ -10,8 +10,8 @@
 struct mb2_cache;
 
 struct mb2_cache_entry {
-	/* LRU list - protected by cache->c_lru_list_lock */
-	struct list_head	e_lru_list;
+	/* List of entries in cache - protected by cache->c_list_lock */
+	struct list_head	e_list;
 	/* Hash table list - protected by bitlock in e_hash_list_head */
 	struct hlist_bl_node	e_hash_list;
 	atomic_t		e_refcnt;
@@ -19,8 +19,11 @@ struct mb2_cache_entry {
 	u32			e_key;
 	/* Block number of hashed block - stable during lifetime of the entry */
 	sector_t		e_block;
-	/* Head of hash list (for list bit lock) - stable */
-	struct hlist_bl_head	*e_hash_list_head;
+	/*
+	 * Head of hash list (for list bit lock) - stable. Combined with
+	 * referenced bit of entry
+	 */
+	unsigned long		_e_hash_list_head;
 };
 
 struct mb2_cache *mb2_cache_create(int bucket_bits);
Signature · 1 line
2.6.2
author reviewer
#7 lore raw

PATCH v3 7/11 mbcache2: Rename to mbcache

In reply to Jan Kara

Since old mbcache code is gone, let's rename new code to mbcache since number 2 is now meaningless. This is just a mechanical replacement.

---
 fs/Makefile              |   2 +-
 fs/ext2/ext2.h           |   4 +-
 fs/ext2/xattr.c          |  48 +++---
 fs/ext2/xattr.h          |   8 +-
 fs/ext4/ext4.h           |   2 +-
 fs/ext4/xattr.c          |  54 +++---
 fs/ext4/xattr.h          |   4 +-
 fs/mbcache.c             | 424 +++++++++++++++++++++++++++++++++++++++++++++++
 fs/mbcache2.c            | 424 -----------------------------------------------
 include/linux/mbcache.h  |  53 ++++++
 include/linux/mbcache2.h |  53 ------
 11 files changed, 538 insertions(+), 538 deletions(-)
 create mode 100644 fs/mbcache.c
 delete mode 100644 fs/mbcache2.c
 create mode 100644 include/linux/mbcache.h
 delete mode 100644 include/linux/mbcache2.h
fs/Makefile +1 -1
index 59b844007fbc..79f522575cba 100644
@@ -41,7 +41,7 @@ obj-$(CONFIG_COMPAT_BINFMT_ELF)	+= compat_binfmt_elf.o
 obj-$(CONFIG_BINFMT_ELF_FDPIC)	+= binfmt_elf_fdpic.o
 obj-$(CONFIG_BINFMT_FLAT)	+= binfmt_flat.o
 
-obj-$(CONFIG_FS_MBCACHE)	+= mbcache2.o
+obj-$(CONFIG_FS_MBCACHE)	+= mbcache.o
 obj-$(CONFIG_FS_POSIX_ACL)	+= posix_acl.o
 obj-$(CONFIG_NFS_COMMON)	+= nfs_common/
 obj-$(CONFIG_COREDUMP)		+= coredump.o
fs/ext2/ext2.h +2 -2
index f98ce7e60a0f..170939f379d7 100644
@@ -61,7 +61,7 @@ struct ext2_block_alloc_info {
 #define rsv_start rsv_window._rsv_start
 #define rsv_end rsv_window._rsv_end
 
-struct mb2_cache;
+struct mb_cache;
 
 /*
  * second extended-fs super-block data in memory
@@ -113,7 +113,7 @@ struct ext2_sb_info {
 	 * of the mount options.
 	 */
 	spinlock_t s_lock;
-	struct mb2_cache *s_mb_cache;
+	struct mb_cache *s_mb_cache;
 };
 
 static inline spinlock_t *
fs/ext2/xattr.c +24 -24

143 lines not on this page · raw message

fs/ext2/xattr.h +4 -4
index 6ea38aa9563a..6f82ab1b00ca 100644
@@ -53,7 +53,7 @@ struct ext2_xattr_entry {
 #define EXT2_XATTR_SIZE(size) \
 	(((size) + EXT2_XATTR_ROUND) & ~EXT2_XATTR_ROUND)
 
-struct mb2_cache;
+struct mb_cache;
 
 # ifdef CONFIG_EXT2_FS_XATTR
 
@@ -68,8 +68,8 @@ extern int ext2_xattr_set(struct inode *, int, const char *, const void *, size_
 
 extern void ext2_xattr_delete_inode(struct inode *);
 
-extern struct mb2_cache *ext2_xattr_create_cache(void);
-extern void ext2_xattr_destroy_cache(struct mb2_cache *cache);
+extern struct mb_cache *ext2_xattr_create_cache(void);
+extern void ext2_xattr_destroy_cache(struct mb_cache *cache);
 
 extern const struct xattr_handler *ext2_xattr_handlers[];
 
@@ -94,7 +94,7 @@ ext2_xattr_delete_inode(struct inode *inode)
 {
 }
 
-static inline void ext2_xattr_destroy_cache(struct mb2_cache *cache)
+static inline void ext2_xattr_destroy_cache(struct mb_cache *cache)
 {
 }
 
fs/ext4/ext4.h +1 -1
index b53cbc05b172..0662b285dc8a 100644
@@ -1468,7 +1468,7 @@ struct ext4_sb_info {
 	struct list_head s_es_list;	/* List of inodes with reclaimable extents */
 	long s_es_nr_inode;
 	struct ext4_es_stats s_es_stats;
-	struct mb2_cache *s_mb_cache;
+	struct mb_cache *s_mb_cache;
 	spinlock_t s_es_lock ____cacheline_aligned_in_smp;
 
 	/* Ratelimit ext4 messages. */
fs/ext4/xattr.c +27 -27

156 lines not on this page · raw message

fs/ext4/xattr.h +2 -2
index 10b0f7323ed6..69dd3e6566e0 100644
@@ -123,8 +123,8 @@ extern int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
 				       struct ext4_xattr_info *i,
 				       struct ext4_xattr_ibody_find *is);
 
-extern struct mb2_cache *ext4_xattr_create_cache(void);
-extern void ext4_xattr_destroy_cache(struct mb2_cache *);
+extern struct mb_cache *ext4_xattr_create_cache(void);
+extern void ext4_xattr_destroy_cache(struct mb_cache *);
 
 #ifdef CONFIG_EXT4_FS_SECURITY
 extern int ext4_init_security(handle_t *handle, struct inode *inode,
fs/mbcache.c new file +424 -0

424 lines not on this page · raw message

fs/mbcache2.c deleted +0 -424

424 lines not on this page · raw message

include/linux/mbcache.h new file +53 -0

53 lines not on this page · raw message

include/linux/mbcache2.h deleted +0 -51

51 lines not on this page · raw message

author reviewer
#8 lore raw

PATCH v3 8/11 ext4: Kill ext4_mballoc_ready

In reply to Jan Kara

From: Andreas Gruenbacher <[email protected]>

This variable, introduced in commit 9c191f70, is unnecessary: it is set once the module has been initialized correctly, and ext4_fill_super cannot run unless the module has been initialized correctly.

---
 fs/ext4/super.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
fs/ext4/super.c +4 -10
index ecc37e103435..2f550519e0aa 100644
@@ -55,7 +55,6 @@
 
 static struct ext4_lazy_init *ext4_li_info;
 static struct mutex ext4_li_mtx;
-static int ext4_mballoc_ready;
 static struct ratelimit_state ext4_mount_msg_ratelimit;
 
 static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
@@ -3795,12 +3794,10 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
 
 no_journal:
-	if (ext4_mballoc_ready) {
-		sbi->s_mb_cache = ext4_xattr_create_cache();
-		if (!sbi->s_mb_cache) {
-			ext4_msg(sb, KERN_ERR, "Failed to create an mb_cache");
-			goto failed_mount_wq;
-		}
+	sbi->s_mb_cache = ext4_xattr_create_cache();
+	if (!sbi->s_mb_cache) {
+		ext4_msg(sb, KERN_ERR, "Failed to create an mb_cache");
+		goto failed_mount_wq;
 	}
 
 	if ((DUMMY_ENCRYPTION_ENABLED(sbi) || ext4_has_feature_encrypt(sb)) &&
@@ -5361,8 +5358,6 @@ static int __init ext4_init_fs(void)
 	err = ext4_init_mballoc();
 	if (err)
 		goto out2;
-	else
-		ext4_mballoc_ready = 1;
 	err = init_inodecache();
 	if (err)
 		goto out1;
@@ -5378,7 +5373,6 @@ out:
 	unregister_as_ext3();
 	destroy_inodecache();
 out1:
-	ext4_mballoc_ready = 0;
 	ext4_exit_mballoc();
 out2:
 	ext4_exit_sysfs();
Signature · 1 line
2.6.2
author reviewer
#9 lore raw

PATCH v3 9/11 mbcache: Get rid of _e_hash_list_head

In reply to Jan Kara

From: Andreas Gruenbacher <[email protected]>

Get rid of field _e_hash_list_head in cache entries and add bit field e_referenced instead.

---
 fs/mbcache.c            | 41 ++++++++++-------------------------------
 include/linux/mbcache.h |  8 ++------
 2 files changed, 12 insertions(+), 37 deletions(-)
fs/mbcache.c +10 -31
index 4241b633f155..903be151dcfe 100644
@@ -45,27 +45,10 @@ static struct kmem_cache *mb_entry_cache;
 static unsigned long mb_cache_shrink(struct mb_cache *cache,
 				     unsigned int nr_to_scan);
 
-static inline bool mb_cache_entry_referenced(struct mb_cache_entry *entry)
+static inline struct hlist_bl_head *mb_cache_entry_head(struct mb_cache *cache,
+							u32 key)
 {
-	return entry->_e_hash_list_head & 1;
-}
-
-static inline void mb_cache_entry_set_referenced(struct mb_cache_entry *entry)
-{
-	entry->_e_hash_list_head |= 1;
-}
-
-static inline void mb_cache_entry_clear_referenced(
-					struct mb_cache_entry *entry)
-{
-	entry->_e_hash_list_head &= ~1;
-}
-
-static inline struct hlist_bl_head *mb_cache_entry_head(
-					struct mb_cache_entry *entry)
-{
-	return (struct hlist_bl_head *)
-			(entry->_e_hash_list_head & ~1);
+	return &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
 }
 
 /*
@@ -108,8 +91,7 @@ int mb_cache_entry_create(struct mb_cache *cache, gfp_t mask, u32 key,
 	atomic_set(&entry->e_refcnt, 1);
 	entry->e_key = key;
 	entry->e_block = block;
-	head = &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
-	entry->_e_hash_list_head = (unsigned long)head;
+	head = mb_cache_entry_head(cache, key);
 	hlist_bl_lock(head);
 	hlist_bl_for_each_entry(dup, dup_node, head, e_hash_list) {
 		if (dup->e_key == key && dup->e_block == block) {
@@ -146,10 +128,7 @@ static struct mb_cache_entry *__entry_find(struct mb_cache *cache,
 	struct hlist_bl_node *node;
 	struct hlist_bl_head *head;
 
-	if (entry)
-		head = mb_cache_entry_head(entry);
-	else
-		head = &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
+	head = mb_cache_entry_head(cache, key);
 	hlist_bl_lock(head);
 	if (entry && !hlist_bl_unhashed(&entry->e_hash_list))
 		node = entry->e_hash_list.next;
@@ -219,7 +198,7 @@ void mb_cache_entry_delete_block(struct mb_cache *cache, u32 key,
 	struct hlist_bl_head *head;
 	struct mb_cache_entry *entry;
 
-	head = &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
+	head = mb_cache_entry_head(cache, key);
 	hlist_bl_lock(head);
 	hlist_bl_for_each_entry(entry, node, head, e_hash_list) {
 		if (entry->e_key == key && entry->e_block == block) {
@@ -250,7 +229,7 @@ EXPORT_SYMBOL(mb_cache_entry_delete_block);
 void mb_cache_entry_touch(struct mb_cache *cache,
 			  struct mb_cache_entry *entry)
 {
-	mb_cache_entry_set_referenced(entry);
+	entry->e_referenced = 1;
 }
 EXPORT_SYMBOL(mb_cache_entry_touch);
 
@@ -275,8 +254,8 @@ static unsigned long mb_cache_shrink(struct mb_cache *cache,
 	while (nr_to_scan-- && !list_empty(&cache->c_list)) {
 		entry = list_first_entry(&cache->c_list,
 					 struct mb_cache_entry, e_list);
-		if (mb_cache_entry_referenced(entry)) {
-			mb_cache_entry_clear_referenced(entry);
+		if (entry->e_referenced) {
+			entry->e_referenced = 0;
 			list_move_tail(&cache->c_list, &entry->e_list);
 			continue;
 		}
@@ -287,7 +266,7 @@ static unsigned long mb_cache_shrink(struct mb_cache *cache,
 		 * from under us.
 		 */
 		spin_unlock(&cache->c_list_lock);
-		head = mb_cache_entry_head(entry);
+		head = mb_cache_entry_head(cache, entry->e_key);
 		hlist_bl_lock(head);
 		if (!hlist_bl_unhashed(&entry->e_hash_list)) {
 			hlist_bl_del_init(&entry->e_hash_list);
include/linux/mbcache.h +2 -6
index a74a1f3082fb..607e6968542e 100644
@@ -12,18 +12,14 @@ struct mb_cache;
 struct mb_cache_entry {
 	/* List of entries in cache - protected by cache->c_list_lock */
 	struct list_head	e_list;
-	/* Hash table list - protected by bitlock in e_hash_list_head */
+	/* Hash table list - protected by hash chain bitlock */
 	struct hlist_bl_node	e_hash_list;
 	atomic_t		e_refcnt;
 	/* Key in hash - stable during lifetime of the entry */
 	u32			e_key;
+	u32			e_referenced:1;
 	/* Block number of hashed block - stable during lifetime of the entry */
 	sector_t		e_block;
-	/*
-	 * Head of hash list (for list bit lock) - stable. Combined with
-	 * referenced bit of entry
-	 */
-	unsigned long		_e_hash_list_head;
 };
 
 struct mb_cache *mb_cache_create(int bucket_bits);
Signature · 1 line
2.6.2
author reviewer
#10 lore raw

PATCH v3 10/11 ext4: Shortcut setting of xattr to the same value

In reply to Jan Kara

When someone tried to set xattr to the same value (i.e., not changing anything) we did all the work of removing original xattr, possibly breaking references to shared xattr block, inserting new xattr, and merging xattr blocks again. Since this is not so rare operation and it is relatively cheap for us to detect this case, check for this and shortcut xattr setting in that case.

---
 fs/ext4/xattr.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
fs/ext4/xattr.c +18 -0
index c6af8a7a436a..b661ae8332e3 100644
@@ -1096,6 +1096,17 @@ static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
 	return 0;
 }
 
+static int ext4_xattr_value_same(struct ext4_xattr_search *s,
+				 struct ext4_xattr_info *i)
+{
+	void *value;
+
+	if (le32_to_cpu(s->here->e_value_size) != i->value_len)
+		return 0;
+	value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
+	return !memcmp(value, i->value, i->value_len);
+}
+
 /*
  * ext4_xattr_set_handle()
  *
@@ -1172,6 +1183,13 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
 		else if (!bs.s.not_found)
 			error = ext4_xattr_block_set(handle, inode, &i, &bs);
 	} else {
+		error = 0;
+		/* Xattr value did not change? Save us some work and bail out */
+		if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
+			goto cleanup;
+		if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
+			goto cleanup;
+
 		error = ext4_xattr_ibody_set(handle, inode, &i, &is);
 		if (!error && !bs.s.not_found) {
 			i.value = NULL;
Signature · 1 line
2.6.2
author reviewer
#11 lore raw

PATCH v3 11/11 mbcache: Add reusable flag to cache entries

In reply to Jan Kara

From: Andreas Gruenbacher <[email protected]>

To reduce amount of damage caused by single bad block, we limit number of inodes sharing an xattr block to 1024. Thus there can be more xattr blocks with the same contents when there are lots of files with the same extended attributes. These xattr blocks naturally result in hash collisions and can form long hash chains and we unnecessarily check each such block only to find out we cannot use it because it is already shared by too many inodes.

Add a reusable flag to cache entries which is cleared when a cache entry has reached its maximum refcount. Cache entries which are not marked reusable are skipped by mb_cache_entry_find_{first,next}. This significantly speeds up mbcache when there are many same xattr blocks. For example for xattr-bench with 5 values and each process handling 20000 files, the run for 64 processes is 25x faster with this patch. Even for 8 processes the speedup is almost 3x. We have also verified that for situations where there is only one xattr block of each kind, the patch doesn't have a measurable cost.

[JK: Remove handling of setting the same value since it is not needed anymore, check for races in e_reusable setting, improve changelog, add measurements]

---
 fs/ext2/xattr.c         |  2 +-
 fs/ext4/xattr.c         | 66 +++++++++++++++++++++++++++++++------------------
 fs/mbcache.c            | 38 +++++++++++++++++++++++++---
 include/linux/mbcache.h |  5 +++-
 4 files changed, 81 insertions(+), 30 deletions(-)
fs/ext2/xattr.c +1 -1
index 71d58c2d7a19..1a5e3bff0b63 100644
@@ -823,7 +823,7 @@ ext2_xattr_cache_insert(struct mb_cache *cache, struct buffer_head *bh)
 	__u32 hash = le32_to_cpu(HDR(bh)->h_hash);
 	int error;
 
-	error = mb_cache_entry_create(cache, GFP_NOFS, hash, bh->b_blocknr);
+	error = mb_cache_entry_create(cache, GFP_NOFS, hash, bh->b_blocknr, 1);
 	if (error) {
 		if (error == -EBUSY) {
 			ea_bdebug(bh, "already in cache (%d cache entries)",
fs/ext4/xattr.c +42 -24

124 lines not on this page · raw message

fs/mbcache.c +34 -4
index 903be151dcfe..eccda3a02de6 100644
@@ -63,13 +63,14 @@ static inline struct hlist_bl_head *mb_cache_entry_head(struct mb_cache *cache,
  * @mask - gfp mask with which the entry should be allocated
  * @key - key of the entry
  * @block - block that contains data
+ * @reusable - is the block reusable by other inodes?
  *
  * Creates entry in @cache with key @key and records that data is stored in
  * block @block. The function returns -EBUSY if entry with the same key
  * and for the same block already exists in cache. Otherwise 0 is returned.
  */
 int mb_cache_entry_create(struct mb_cache *cache, gfp_t mask, u32 key,
-			  sector_t block)
+			  sector_t block, bool reusable)
 {
 	struct mb_cache_entry *entry, *dup;
 	struct hlist_bl_node *dup_node;
@@ -91,6 +92,7 @@ int mb_cache_entry_create(struct mb_cache *cache, gfp_t mask, u32 key,
 	atomic_set(&entry->e_refcnt, 1);
 	entry->e_key = key;
 	entry->e_block = block;
+	entry->e_reusable = reusable;
 	head = mb_cache_entry_head(cache, key);
 	hlist_bl_lock(head);
 	hlist_bl_for_each_entry(dup, dup_node, head, e_hash_list) {
@@ -137,7 +139,7 @@ static struct mb_cache_entry *__entry_find(struct mb_cache *cache,
 	while (node) {
 		entry = hlist_bl_entry(node, struct mb_cache_entry,
 				       e_hash_list);
-		if (entry->e_key == key) {
+		if (entry->e_key == key && entry->e_reusable) {
 			atomic_inc(&entry->e_refcnt);
 			goto out;
 		}
@@ -184,10 +186,38 @@ struct mb_cache_entry *mb_cache_entry_find_next(struct mb_cache *cache,
 }
 EXPORT_SYMBOL(mb_cache_entry_find_next);
 
+/*
+ * mb_cache_entry_get - get a cache entry by block number (and key)
+ * @cache - cache we work with
+ * @key - key of block number @block
+ * @block - block number
+ */
+struct mb_cache_entry *mb_cache_entry_get(struct mb_cache *cache, u32 key,
+					  sector_t block)
+{
+	struct hlist_bl_node *node;
+	struct hlist_bl_head *head;
+	struct mb_cache_entry *entry;
+
+	head = mb_cache_entry_head(cache, key);
+	hlist_bl_lock(head);
+	hlist_bl_for_each_entry(entry, node, head, e_hash_list) {
+		if (entry->e_key == key && entry->e_block == block) {
+			atomic_inc(&entry->e_refcnt);
+			goto out;
+		}
+	}
+	entry = NULL;
+out:
+	hlist_bl_unlock(head);
+	return entry;
+}
+EXPORT_SYMBOL(mb_cache_entry_get);
+
 /* mb_cache_entry_delete_block - remove information about block from cache
  * @cache - cache we work with
- * @key - key of the entry to remove
- * @block - block containing data for @key
+ * @key - key of block @block
+ * @block - block number
  *
  * Remove entry from cache @cache with key @key with data stored in @block.
  */
include/linux/mbcache.h +4 -1
index 607e6968542e..86c9a8b480c5 100644
@@ -18,6 +18,7 @@ struct mb_cache_entry {
 	/* Key in hash - stable during lifetime of the entry */
 	u32			e_key;
 	u32			e_referenced:1;
+	u32			e_reusable:1;
 	/* Block number of hashed block - stable during lifetime of the entry */
 	sector_t		e_block;
 };
@@ -26,7 +27,7 @@ struct mb_cache *mb_cache_create(int bucket_bits);
 void mb_cache_destroy(struct mb_cache *cache);
 
 int mb_cache_entry_create(struct mb_cache *cache, gfp_t mask, u32 key,
-			  sector_t block);
+			  sector_t block, bool reusable);
 void __mb_cache_entry_free(struct mb_cache_entry *entry);
 static inline int mb_cache_entry_put(struct mb_cache *cache,
 				     struct mb_cache_entry *entry)
@@ -39,6 +40,8 @@ static inline int mb_cache_entry_put(struct mb_cache *cache,
 
 void mb_cache_entry_delete_block(struct mb_cache *cache, u32 key,
 				  sector_t block);
+struct mb_cache_entry *mb_cache_entry_get(struct mb_cache *cache, u32 key,
+					  sector_t block);
 struct mb_cache_entry *mb_cache_entry_find_first(struct mb_cache *cache,
 						 u32 key);
 struct mb_cache_entry *mb_cache_entry_find_next(struct mb_cache *cache,
Signature · 1 line
2.6.2
maintainer
#12 lore raw
In reply to Jan Kara
Quoted 5 lines · Jan Kara

Hello,

since Ted didn't merge the mbcache rewrite patches yet, here is a new version of mbcache rewrite series with some improvements from Andreas Grunbacher. Ted, can you please pick up this series for the next merge window? Thanks!

I had actually already pulled mbcache2 into ext4.git, but I'll replace it with your new patch series.

- Ted

author reviewer
#13 lore raw
Quoted 9 lines · Ted Tso
Quoted 5 lines · Jan Kara

Hello,

since Ted didn't merge the mbcache rewrite patches yet, here is a new version of mbcache rewrite series with some improvements from Andreas Grunbacher. Ted, can you please pick up this series for the next merge window? Thanks!

I had actually already pulled mbcache2 into ext4.git, but I'll replace it with your new patch series.

Thanks!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR