Skip to contentSkip to conversation

simpe_xattr: fixes for per-sb cache

PATCH New Miklos Szeredi 1 participant 3 messages May 26

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover simpe_xattr: fixes for per-sb cache

Fix two issues in commit 12e9e3cd03b5 ("simpe_xattr: use per-sb cache").

Please feel free to fold this (with updated memory use numbers).

---
Miklos Szeredi (2):
simpe_xattr: fix rcu'd union in struct simple_xattr
simpe_xattr: fix racing lazy allocation

 fs/xattr.c            | 19 ++++++++++++++-----
 include/linux/xattr.h |  6 ++----
 2 files changed, 16 insertions(+), 9 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/2 simpe_xattr: fix rcu'd union in struct simple_xattr

'node' can't be in union with 'rcu' because simple_xattr_list() uses 'node' to walk the list of xattrs with just rcu protection.

call_rcu() on the freed xattr would corrupt the forward chaining that is used in this situation.

Removing from the union results in a 16 byte increase in the size of simple_xattr. New measurements:

v7.0 (no rhashtable):
File creation: 993.40 bytes/file
Xattr addition: 79.99 bytes/file

v7.1-rc2 (per-inode rhashtable):
File creation: 939.73 bytes/file
Xattr addition: 1296.08 bytes/file

v7.1-rc2 + this patch (per-sb rhashtable)
File creation: 946.84 bytes/file
Xattr addition: 111.86 bytes/file

The overhead of a single xattr is reduced to nearly v7.0 levels. The per xattr overhead is slightly larger due to the addition of three pointers to struct simple_xattr.

Fixes 12e9e3cd03b5 ("simpe_xattr: use per-sb cache")
---
 include/linux/xattr.h | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
include/linux/xattr.h +2 -4
index a75b99eb1f04..7aaaf4f8aff5 100644
@@ -112,11 +112,9 @@ struct simple_xattr_cache {
 
 struct simple_xattr {
 	struct rhash_head hash_node;
-	union {
-		struct list_head node;
-		struct rcu_head rcu;
-	};
 	struct list_head *parent;
+	struct list_head node;
+	struct rcu_head rcu;
 	char *name;
 	size_t size;
 	char value[] __counted_by(size);
Signature · 1 line
2.54.0
#2 lore raw

PATCH 2/2 simpe_xattr: fix racing lazy allocation

In reply to Miklos Szeredi

Before being made per-sb, lazy allocation was protected by inode lock. Now inode lock no longer provides sufficient exclusion, so use cmpxchg() to ensure atomicity.

Though I haven't found a description of this pattern, after some research it seems that cmpxchg_release() and READ_ONCE() should provide the necessary memory barriers.

Fixes 12e9e3cd03b5 ("simpe_xattr: use per-sb cache")
---
 fs/xattr.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/xattr.c +14 -5
index 31cae48e11a4..57e117ab85da 100644
@@ -1332,8 +1332,7 @@ int simple_xattr_get(struct simple_xattr_cache *cache, struct list_head *xattrs,
 {
 	struct simple_xattr *xattr;
 	struct sx_key key = { .parent = xattrs, .name = name };
-	/* cache->ht is stored set once from NULL to non-NULL */
-	struct rhashtable *ht = smp_load_acquire(&cache->ht);
+	struct rhashtable *ht = READ_ONCE(cache->ht);
 	int ret = -ENODATA;
 
 	if (!ht)
@@ -1356,7 +1355,7 @@ int simple_xattr_get(struct simple_xattr_cache *cache, struct list_head *xattrs,
 static struct rhashtable *simple_xattrs_lazy_alloc(struct simple_xattr_cache *cache,
 						   const void *value, int flags)
 {
-	struct rhashtable *ht = cache->ht;
+	struct rhashtable *oldht, *ht = READ_ONCE(cache->ht);
 	int err;
 
 	if (unlikely(!ht)) {
@@ -1373,8 +1372,18 @@ static struct rhashtable *simple_xattrs_lazy_alloc(struct simple_xattr_cache *ca
 			return ERR_PTR(err);
 		}
 
-		/* Paris with smp_load_acquire() in simple_xattr_get() */
-		smp_store_release(&cache->ht, ht);
+		/*
+		 * Provides release semantics on success, so that use of a
+		 * non-NULL READ_ONCE(cache->ht) will be ordered relative to the
+		 * above initialization, due to implicit address dependency.
+		 */
+		oldht = cmpxchg_release(&cache->ht, NULL, ht);
+		if (oldht) {
+			/* Race lost */
+			rhashtable_destroy(ht);
+			kfree(ht);
+			ht = oldht;
+		}
 	}
 	return ht;
 }
Signature · 1 line
2.54.0