Skip to contentSkip to conversation

fs/namespace: use percpu_rw_semaphore for writer holding

RFC PATCH In review Sebastian Andrzej Siewior 3 participants 3 messages Jul 2, 2020 – Jul 20, 2020 VFS

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 fs/namespace: use percpu_rw_semaphore for writer holding

From: John Ogness <[email protected]>

The MNT_WRITE_HOLD flag is used to manually implement a rwsem. Remove that flag and instead use a percpu_rw_semaphore, which effectively provides the same functionality. This allows lockdep to be used for the writer holding, allows CONFIG_PREEMPT to preempt held writers, and simplifies the writer holding code.

Note that writer holding is now performed on a per super block basis rather than a per mount basis.

---
v1…v2:
- Switch two percpu rwsem

TEST COMMAND
lat_mmap -P $pval -W 32 -N 50 64m file

On a 32 core Xeon.

OUTPUT FORMAT
pval: avg std

Preemptible Kernel
BASE RWS4

 1:   289.40  2.97   286.00  2.35
 2:   307.60  3.58   292.40  3.65
 4:   323.20  3.56   313.20  2.28
 8:   375.00  6.08   361.20  1.30
16:   412.40  1.34   413.00  2.83
32:   611.80  4.32   613.40  4.28
64:  1239.00  7.84  1251.00  9.30

Preemptible Kernel, no PTI
BASE RWS4

 1:   286.00  1.22   289.20  3.35
 2:   312.00  9.06   311.00  3.87
 4:   321.20  5.17   394.40  2.30
 8:   368.60  2.07   447.80  3.35
16:   411.40  2.41   476.40  1.14
32:   623.40  6.47   651.00  3.39
64:  1264.20  9.55  1292.60  2.51

Voluntary Kernel Preemption
BASE RWS4

 1:   285.80  1.48   283.40  5.94
 2:   303.40  3.78   303.60  3.21
 4:   315.20  7.29   309.80  3.49
 8:   361.40  2.30   361.00  7.00
16:   415.40  3.36   419.80  6.14
32:   606.60  5.13   612.80  5.40
64:  1211.40  7.27  1205.20  8.04

Voluntary Kernel Preemption, no PTI
BASE RWS4

 1:   281.60  2.51   293.80  3.03
 2:   295.00  3.81   321.00  5.96
 4:   312.20  2.77   372.40  8.73
 8:   359.20  0.45   424.40  1.82
16:   413.20  3.96   449.40  2.30
32:   623.20  5.36   635.80  1.64
64:  1212.00  6.63  1229.00  5.15

No Forced Preemption
BASE RWS4

 1:   273.80  0.45   275.40  0.55
 2:   291.60  7.20   285.60  1.82
 4:   310.60  4.22   302.20  1.79
 8:   362.40  3.05   350.20  1.30
16:   416.40  3.78   405.00  2.92
32:   626.20  2.28   611.80  1.30
64:  1215.80  3.83  1185.60 11.67

No Forced Preemption, no PTI
BASE RWS4

 1:   274.80  2.68   274.00  3.39
 2:   291.00  5.66   289.60  3.05
 4:   304.00  2.74   303.20  1.48
 8:   352.40  1.82   350.60  2.30
16:   408.40  0.89   406.20  2.49
32:   605.20  3.27   607.20  3.96
64:  1189.80  4.09  1194.40  1.82
 fs/mount.h            |  2 +
 fs/namespace.c        | 99 ++++++++++++++++---------------------------
 fs/super.c            |  2 +
 include/linux/fs.h    |  1 +
 include/linux/mount.h |  6 +--
 5 files changed, 44 insertions(+), 66 deletions(-)
fs/mount.h +2 -0
index c7abb7b394d8e..67ef1f87b89fe 100644
@@ -4,6 +4,7 @@
 #include <linux/poll.h>
 #include <linux/ns_common.h>
 #include <linux/fs_pin.h>
+#include <linux/percpu-rwsem.h>
 
 struct mnt_namespace {
 	atomic_t		count;
@@ -46,6 +47,7 @@ struct mount {
 		struct rcu_head mnt_rcu;
 		struct llist_node mnt_llist;
 	};
+	struct percpu_rw_semaphore mnt_writers_rws;
 #ifdef CONFIG_SMP
 	struct mnt_pcp __percpu *mnt_pcp;
 #else
fs/namespace.c +37 -62

197 lines not on this page · raw message

fs/super.c +2 -0
index 904459b351199..5c70bc135ba27 100644
@@ -210,6 +210,8 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags,
 	INIT_LIST_HEAD(&s->s_mounts);
 	s->s_user_ns = get_user_ns(user_ns);
 	init_rwsem(&s->s_umount);
+	percpu_init_rwsem(&s->mnt_writers_rws);
+
 	lockdep_set_class(&s->s_umount, &type->s_umount_key);
 	/*
 	 * sget() can have s_umount recursion.
include/linux/fs.h +1 -0
index 3f881a892ea74..2406e823a37df 100644
@@ -1464,6 +1464,7 @@ struct super_block {
 #endif
 	struct hlist_bl_head	s_roots;	/* alternate root dentries for NFS */
 	struct list_head	s_mounts;	/* list of mounts; _not_ for fs use */
+	struct percpu_rw_semaphore mnt_writers_rws;
 	struct block_device	*s_bdev;
 	struct backing_dev_info *s_bdi;
 	struct mtd_info		*s_mtd;
include/linux/mount.h +2 -4
index de657bd211fa6..dce9bda5eeaa1 100644
@@ -32,7 +32,6 @@ struct fs_context;
 #define MNT_READONLY	0x40	/* does the user want this to be r/o? */
 
 #define MNT_SHRINKABLE	0x100
-#define MNT_WRITE_HOLD	0x200
 
 #define MNT_SHARED	0x1000	/* if the vfsmount is a shared mount */
 #define MNT_UNBINDABLE	0x2000	/* if the vfsmount is a unbindable mount */
@@ -49,9 +48,8 @@ struct fs_context;
 				 | MNT_READONLY)
 #define MNT_ATIME_MASK (MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME )
 
-#define MNT_INTERNAL_FLAGS (MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL | \
-			    MNT_DOOMED | MNT_SYNC_UMOUNT | MNT_MARKED | \
-			    MNT_CURSOR)
+#define MNT_INTERNAL_FLAGS (MNT_SHARED | MNT_INTERNAL | MNT_CURSOR | \
+			    MNT_DOOMED | MNT_SYNC_UMOUNT | MNT_MARKED)
 
 #define MNT_INTERNAL	0x4000
 
Signature · 1 line
2.27.0
maintainer
#1 lore raw
Quoted 1 line · Sebastian Andrzej Siewior

The MNT_WRITE_HOLD flag is used to manually implement a rwsem.

Could you show me where does it currently sleep? Your version does, unless I'm misreading it...

#2 lore raw
Quoted 4 lines · Al Viro
Quoted 1 line

The MNT_WRITE_HOLD flag is used to manually implement a rwsem.

Could you show me where does it currently sleep? Your version does, unless I'm misreading it...

You are reading it correctly. This patch introduces new possible sleeping for__mnt_want_write() when writers for a superblock are being held.

The RFCv1 [0] is a variant that does not introduce sleeping, but instead reverts back to per-cpu spinlocks.

Sebastian and I are requesting comments on these two possible solutions. Or perhaps you have an idea how to solve the potential live lock situation.

John Ogness

[0] https://lkml.kernel.org/r/[email protected]