kernfs: proposed locking and concurrency improvement
v4 cover kernfs: proposed locking and concurrency improvement
There have been a few instances of contention on the kernfs_mutex during path walks, a case on very large IBM systems seen by myself, a report by Brice Goglin and followed up by Fox Chen, and I've since seen a couple of other reports by CoreOS users.
The common thread is a large number of kernfs path walks leading to slowness of path walks due to kernfs_mutex contention.
The problem being that changes to the VFS over some time have increased it's concurrency capabilities to an extent that kernfs's use of a mutex is no longer appropriate. There's also an issue of walks for non-existent paths causing contention if there are quite a few of them which is a less common problem.
This patch series is relatively straight forward.
All it does is add the ability to take advantage of VFS negative dentry caching to avoid needless dentry alloc/free cycles for lookups of paths that don't exit and change the kernfs_mutex to a read/write semaphore.
The patch that tried to stay in VFS rcu-walk mode during path walks has been dropped for two reasons. First, it doesn't actually give very much improvement and, second, if there's a place where mistakes could go unnoticed it would be in that path. This makes the patch series simpler to review and reduces the likelihood of problems going unnoticed and popping up later.
The patch to use a revision to identify if a directory has changed has also been dropped. If the directory has changed the dentry revision needs to be updated to avoid subsequent rb tree searches and after changing to use a read/write semaphore the update also requires a lock. But the d_lock is the only lock available at this point which might itself be contended.
Changes since v3:
- remove unneeded indirection when referencing the super block.
- check if inode attribute update is actually needed.
Changes since v2:
- actually fix the inode attribute update locking.
- drop the patch that tried to stay in rcu-walk mode.
- drop the use a revision to identify if a directory has changed patch.
Changes since v1:
- fix locking in .permission() and .getattr() by re-factoring the attribute handling code.
---
Ian Kent (5):
kernfs: move revalidate to be near lookup
kernfs: use VFS negative dentry caching
kernfs: switch kernfs to use an rwsem
kernfs: use i_lock to protect concurrent inode updates
kernfs: add kernfs_need_inode_refresh()
fs/kernfs/dir.c | 170 ++++++++++++++++++++---------------- fs/kernfs/file.c | 4 +- fs/kernfs/inode.c | 45 ++++++++-- fs/kernfs/kernfs-internal.h | 5 +- fs/kernfs/mount.c | 12 +-- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 2 +- 7 files changed, 147 insertions(+), 95 deletions(-)
--
Ian
PATCH v4 1/5 kernfs: move revalidate to be near lookup
While the dentry operation kernfs_dop_revalidate() is grouped with dentry type functions it also has a strong affinity to the inode operation ->lookup().
In order to take advantage of the VFS negative dentry caching that can be used to reduce path lookup overhead on non-existent paths it will need to call kernfs_find_ns(). So, to avoid a forward declaration, move it to be near kernfs_iop_lookup().
There's no functional change from this patch.
--- fs/kernfs/dir.c | 86 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 43 deletions(-)
fs/kernfs/dir.c +43 -43
@@ -548,49 +548,6 @@ void kernfs_put(struct kernfs_node *kn) }EXPORT_SYMBOL_GPL (kernfs_put); -static int kernfs_dop_revalidate (struct dentry *dentry,unsigned int flags) -{ -struct kernfs_node *kn; - -if (flags &LOOKUP_RCU ) -return -ECHILD ; - -/* Always perform fresh lookup for negatives */ -if (d_really_is_negative (dentry)) - goto out_bad_unlocked; - - kn =kernfs_dentry_node (dentry); -mutex_lock (&kernfs_mutex); - -/* The kernfs node has been deactivated */ -if (!kernfs_active (kn)) - goto out_bad; - -/* The kernfs node has been moved? */ -if (kernfs_dentry_node (dentry->d_parent) != kn->parent) - goto out_bad; - -/* The kernfs node has been renamed */ -if (strcmp (dentry->d_name.name, kn->name) !=0 ) - goto out_bad; - -/* The kernfs node has been moved to a different namespace */ -if (kn->parent &&kernfs_ns_enabled (kn->parent) && -kernfs_info (dentry->d_sb)->ns != kn->ns) - goto out_bad; - -mutex_unlock (&kernfs_mutex); -return 1 ; -out_bad: -mutex_unlock (&kernfs_mutex); -out_bad_unlocked: -return 0 ; -} - -const struct dentry_operations kernfs_dops = { - .d_revalidate = kernfs_dop_revalidate, -}; -/** * kernfs_node_from_dentry - determine kernfs_node associated with a dentry * @dentry: the dentry in question
@@ -1073,6 +1030,49 @@ struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,return ERR_PTR (rc); } +static int kernfs_dop_revalidate (struct dentry *dentry,unsigned int flags) +{ +struct kernfs_node *kn; + +if (flags &LOOKUP_RCU ) +return -ECHILD ; + +/* Always perform fresh lookup for negatives */ +if (d_really_is_negative (dentry)) + goto out_bad_unlocked; + + kn =kernfs_dentry_node (dentry); +mutex_lock (&kernfs_mutex); + +/* The kernfs node has been deactivated */ +if (!kernfs_active_read (kn)) + goto out_bad; + +/* The kernfs node has been moved? */ +if (kernfs_dentry_node (dentry->d_parent) != kn->parent) + goto out_bad; + +/* The kernfs node has been renamed */ +if (strcmp (dentry->d_name.name, kn->name) !=0 ) + goto out_bad; + +/* The kernfs node has been moved to a different namespace */ +if (kn->parent &&kernfs_ns_enabled (kn->parent) && +kernfs_info (dentry->d_sb)->ns != kn->ns) + goto out_bad; + +mutex_unlock (&kernfs_mutex); +return 1 ; +out_bad: +mutex_unlock (&kernfs_mutex); +out_bad_unlocked: +return 0 ; +} + +const struct dentry_operations kernfs_dops = { + .d_revalidate = kernfs_dop_revalidate, +}; +static struct dentry *kernfs_iop_lookup (struct inode *dir,struct dentry *dentry,unsigned int flags)
PATCH v4 2/5 kernfs: use VFS negative dentry caching
In reply to Ian KentIf there are many lookups for non-existent paths these negative lookups can lead to a lot of overhead during path walks.
The VFS allows dentries to be created as negative and hashed, and caches them so they can be used to reduce the fairly high overhead alloc/free cycle that occurs during these lookups.
--- fs/kernfs/dir.c | 55 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 22 deletions(-)
fs/kernfs/dir.c +33 -22
@@ -1037,12 +1037,33 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)if (flags &LOOKUP_RCU )return -ECHILD ; -/* Always perform fresh lookup for negatives */ -if (d_really_is_negative (dentry)) - goto out_bad_unlocked; +mutex_lock (&kernfs_mutex); kn =kernfs_dentry_node (dentry); -mutex_lock (&kernfs_mutex); + +/* Negative hashed dentry? */ +if (!kn) { +struct kernfs_node *parent; + +/* If the kernfs node can be found this is a stale negative +* hashed dentry so it must be discarded and the lookup redone. +*/ + parent =kernfs_dentry_node (dentry->d_parent); +if (parent) { +const void *ns =NULL ; + +if (kernfs_ns_enabled (parent)) + ns =kernfs_info (dentry->d_sb)->ns; + kn =kernfs_find_ns (parent, dentry->d_name.name, ns); +if (kn) + goto out_bad; + } + +/* The kernfs node doesn't exist, leave the dentry negative +* and return success. +*/ + goto out; + }/* The kernfs node has been deactivated */ if (!kernfs_active_read (kn))
@@ -1060,12 +1081,11 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)if (kn->parent &&kernfs_ns_enabled (kn->parent) &&kernfs_info (dentry->d_sb)->ns != kn->ns) goto out_bad; - +out:mutex_unlock (&kernfs_mutex);return 1 ; out_bad:mutex_unlock (&kernfs_mutex); -out_bad_unlocked:return 0 ; }
@@ -1080,33 +1100,24 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir,struct dentry *ret;struct kernfs_node *parent = dir->i_private;struct kernfs_node *kn; -struct inode *inode; +struct inode *inode =NULL ;const void *ns =NULL ;mutex_lock (&kernfs_mutex); -if (kernfs_ns_enabled (parent)) ns =kernfs_info (dir->i_sb)->ns; kn =kernfs_find_ns (parent, dentry->d_name.name, ns); - -/* no such entry */ -if (!kn || !kernfs_active (kn)) { - ret =NULL ; - goto out_unlock; - } -/* attach dentry and inode */ - inode =kernfs_get_inode (dir->i_sb, kn); -if (!inode) { - ret =ERR_PTR (-ENOMEM ); - goto out_unlock; +if (kn &&kernfs_active (kn)) { + inode =kernfs_get_inode (dir->i_sb, kn); +if (!inode) + inode =ERR_PTR (-ENOMEM ); } - -/* instantiate and hash dentry */ +/* instantiate and hash (possibly negative) dentry */ ret =d_splice_alias (inode, dentry); - out_unlock:mutex_unlock (&kernfs_mutex); +return ret; }
PATCH v4 3/5 kernfs: switch kernfs to use an rwsem
In reply to Ian KentThe kernfs global lock restricts the ability to perform kernfs node lookup operations in parallel during path walks.
Change the kernfs mutex to an rwsem so that, when opportunity arises, node searches can be done in parallel with path walk lookups.
--- fs/kernfs/dir.c | 117 ++++++++++++++++++++++++------------------- fs/kernfs/file.c | 4 + fs/kernfs/inode.c | 16 +++--- fs/kernfs/kernfs-internal.h | 5 +- fs/kernfs/mount.c | 12 ++-- fs/kernfs/symlink.c | 4 + include/linux/kernfs.h | 2 - 7 files changed, 86 insertions(+), 74 deletions(-)
fs/kernfs/dir.c +64 -53
@@ -17,7 +17,7 @@#include "kernfs-internal.h" -DEFINE_MUTEX (kernfs_mutex); +DECLARE_RWSEM (kernfs_rwsem);static DEFINE_SPINLOCK (kernfs_rename_lock);/* kn->parent and ->name */ static char kernfs_pr_cont_buf[PATH_MAX ];/* protected by rename_lock */ static DEFINE_SPINLOCK (kernfs_idr_lock);/* root->ino_idr */
@@ -26,10 +26,21 @@ static DEFINE_SPINLOCK(kernfs_idr_lock); /* root->ino_idr */static bool kernfs_active (struct kernfs_node *kn) { -lockdep_assert_held (&kernfs_mutex);return atomic_read (&kn->active) >=0 ; } +static bool kernfs_active_write (struct kernfs_node *kn) +{ +lockdep_assert_held_write (&kernfs_rwsem); +return kernfs_active (kn); +} + +static bool kernfs_active_read (struct kernfs_node *kn) +{ +lockdep_assert_held_read (&kernfs_rwsem); +return kernfs_active (kn); +} +static bool kernfs_lockdep (struct kernfs_node *kn) {#ifdef CONFIG_DEBUG_LOCK_ALLOC
@@ -340,7 +351,7 @@ static int kernfs_sd_compare(const struct kernfs_node *left,* @kn->parent->dir.children. * * Locking: -* mutex_lock(kernfs_mutex) +* kernfs_rwsem held exclusive * * RETURNS: * 0 on susccess -EEXIST on failure.
@@ -385,7 +396,7 @@ static int kernfs_link_sibling(struct kernfs_node *kn)* removed, %false if @kn wasn't on the rbtree. * * Locking: -* mutex_lock(kernfs_mutex) +* kernfs_rwsem held exclusive */ static bool kernfs_unlink_sibling (struct kernfs_node *kn) {
@@ -455,14 +466,14 @@ void kernfs_put_active(struct kernfs_node *kn)* return after draining is complete. */ static void kernfs_drain (struct kernfs_node *kn) - __releases(&kernfs_mutex) __acquires(&kernfs_mutex) + __releases(&kernfs_rwsem) __acquires(&kernfs_rwsem) {struct kernfs_root *root =kernfs_root (kn); -lockdep_assert_held (&kernfs_mutex); +lockdep_assert_held_write (&kernfs_rwsem);WARN_ON_ONCE (kernfs_active (kn)); -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem);if (kernfs_lockdep (kn)) {rwsem_acquire (&kn->dep_map,0 ,0 , _RET_IP_);
@@ -481,7 +492,7 @@ static void kernfs_drain(struct kernfs_node *kn)kernfs_drain_open_files (kn); -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem); }/**
@@ -720,7 +731,7 @@ int kernfs_add_one(struct kernfs_node *kn)bool has_ns;int ret; -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem); ret = -EINVAL ; has_ns =kernfs_ns_enabled (parent);
@@ -735,7 +746,7 @@ int kernfs_add_one(struct kernfs_node *kn)if (parent->flags &KERNFS_EMPTY_DIR ) goto out_unlock; -if ((parent->flags &KERNFS_ACTIVATED ) && !kernfs_active (parent)) +if ((parent->flags &KERNFS_ACTIVATED ) && !kernfs_active_write (parent)) goto out_unlock; kn->hash =kernfs_name_hash (kn->name, kn->ns);
@@ -751,7 +762,7 @@ int kernfs_add_one(struct kernfs_node *kn) ps_iattr->ia_mtime = ps_iattr->ia_ctime; } -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem);/* * Activate the new node unless CREATE_DEACTIVATED is requested.
@@ -765,7 +776,7 @@ int kernfs_add_one(struct kernfs_node *kn)return 0 ; out_unlock: -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem);return ret; }
@@ -786,7 +797,7 @@ static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,bool has_ns =kernfs_ns_enabled (parent);unsigned int hash; -lockdep_assert_held (&kernfs_mutex); +lockdep_assert_held (&kernfs_rwsem);if (has_ns != (bool )ns) { WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
@@ -818,7 +829,7 @@ static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent,size_t len;char *p, *name; -lockdep_assert_held (&kernfs_mutex); +lockdep_assert_held_read (&kernfs_rwsem);/* grab kernfs_rename_lock to piggy back on kernfs_pr_cont_buf */ spin_lock_irq (&kernfs_rename_lock);
@@ -858,10 +869,10 @@ struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent, {struct kernfs_node *kn; -mutex_lock (&kernfs_mutex); +down_read (&kernfs_rwsem); kn =kernfs_find_ns (parent, name, ns);kernfs_get (kn); -mutex_unlock (&kernfs_mutex); +up_read (&kernfs_rwsem);return kn; }
@@ -882,10 +893,10 @@ struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent, {struct kernfs_node *kn; -mutex_lock (&kernfs_mutex); +down_read (&kernfs_rwsem); kn =kernfs_walk_ns (parent, path, ns);kernfs_get (kn); -mutex_unlock (&kernfs_mutex); +up_read (&kernfs_rwsem);return kn; }
@@ -1037,7 +1048,7 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)if (flags &LOOKUP_RCU )return -ECHILD ; -mutex_lock (&kernfs_mutex); +down_read (&kernfs_rwsem); kn =kernfs_dentry_node (dentry);
@@ -1082,10 +1093,10 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) kernfs_info(dentry->d_sb)->ns != kn->ns) goto out_bad; out: -mutex_unlock (&kernfs_mutex); +up_read (&kernfs_rwsem);return 1 ; out_bad: -mutex_unlock (&kernfs_mutex); +up_read (&kernfs_rwsem);return 0 ; }
@@ -1103,7 +1114,7 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir,struct inode *inode =NULL ;const void *ns =NULL ; -mutex_lock (&kernfs_mutex); +down_read (&kernfs_rwsem);if (kernfs_ns_enabled (parent)) ns =kernfs_info (dir->i_sb)->ns;
@@ -1116,7 +1127,7 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir, }/* instantiate and hash (possibly negative) dentry */ ret =d_splice_alias (inode, dentry); -mutex_unlock (&kernfs_mutex); +up_read (&kernfs_rwsem);return ret; }
@@ -1238,7 +1249,7 @@ static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos, {struct rb_node *rbn; -lockdep_assert_held (&kernfs_mutex); +lockdep_assert_held_write (&kernfs_rwsem);/* if first iteration, visit leftmost descendant which may be root */ if (!pos)
@@ -1274,7 +1285,7 @@ void kernfs_activate(struct kernfs_node *kn) {struct kernfs_node *pos; -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem); pos =NULL ;while ((pos =kernfs_next_descendant_post (pos, kn))) {
@@ -1288,14 +1299,14 @@ void kernfs_activate(struct kernfs_node *kn) pos->flags |=KERNFS_ACTIVATED ; } -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem); }static void __kernfs_remove (struct kernfs_node *kn) {struct kernfs_node *pos; -lockdep_assert_held (&kernfs_mutex); +lockdep_assert_held_write (&kernfs_rwsem);/* * Short-circuit if non-root @kn has already finished removal.
@@ -1310,7 +1321,7 @@ static void __kernfs_remove(struct kernfs_node *kn)/* prevent any new usage under @kn by deactivating all nodes */ pos =NULL ;while ((pos =kernfs_next_descendant_post (pos, kn))) -if (kernfs_active (pos)) +if (kernfs_active_write (pos))atomic_add (KN_DEACTIVATED_BIAS , &pos->active);/* deactivate and unlink the subtree node-by-node */
@@ -1318,7 +1329,7 @@ static void __kernfs_remove(struct kernfs_node *kn) pos =kernfs_leftmost_descendant (kn);/* -* kernfs_drain() drops kernfs_mutex temporarily and @pos's +* kernfs_drain() drops kernfs_rwsem temporarily and @pos's * base ref could have been put by someone else by the time * the function returns. Make sure it doesn't go away * underneath us.
@@ -1365,9 +1376,9 @@ static void __kernfs_remove(struct kernfs_node *kn)*/ void kernfs_remove (struct kernfs_node *kn) { -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem);__kernfs_remove (kn); -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem); }/**
@@ -1454,17 +1465,17 @@ bool kernfs_remove_self(struct kernfs_node *kn) {bool ret; -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem);kernfs_break_active_protection (kn);/* * SUICIDAL is used to arbitrate among competing invocations. Only * the first one will actually perform removal. When the removal * is complete, SUICIDED is set and the active ref is restored -* while holding kernfs_mutex. The ones which lost arbitration -* waits for SUICDED && drained which can happen only after the -* enclosing kernfs operation which executed the winning instance -* of kernfs_remove_self() finished. +* while kernfs_rwsem for held exclusive. The ones which lost +* arbitration waits for SUICIDED && drained which can happen only +* after the enclosing kernfs operation which executed the winning +* instance of kernfs_remove_self() finished. */ if (!(kn->flags &KERNFS_SUICIDAL )) { kn->flags |=KERNFS_SUICIDAL ;
@@ -1482,9 +1493,9 @@ bool kernfs_remove_self(struct kernfs_node *kn) atomic_read(&kn->active) == KN_DEACTIVATED_BIAS) break; -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem);schedule (); -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem); }finish_wait (waitq, &wait);WARN_ON_ONCE (!RB_EMPTY_NODE (&kn->rb));
@@ -1492,12 +1503,12 @@ bool kernfs_remove_self(struct kernfs_node *kn) }/* -* This must be done while holding kernfs_mutex; otherwise, waiting -* for SUICIDED && deactivated could finish prematurely. +* This must be done while kernfs_rwsem held exclusive; otherwise, +* waiting for SUICIDED && deactivated could finish prematurely. */ kernfs_unbreak_active_protection (kn); -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem);return ret; }
@@ -1521,13 +1532,13 @@ int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,return -ENOENT ; } -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem); kn =kernfs_find_ns (parent, name, ns);if (kn)__kernfs_remove (kn); -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem);if (kn)return 0 ;
@@ -1553,10 +1564,10 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,if (!kn->parent)return -EINVAL ; -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem); error = -ENOENT ; -if (!kernfs_active (kn) || !kernfs_active (new_parent) || +if (!kernfs_active_write (kn) || !kernfs_active_write (new_parent) || (new_parent->flags &KERNFS_EMPTY_DIR )) goto out;
@@ -1607,7 +1618,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, error =0 ; out: -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem);return error; }
@@ -1627,7 +1638,7 @@ static struct kernfs_node *kernfs_dir_pos(const void *ns, struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos) { if (pos) { - int valid = kernfs_active(pos) && + int valid = kernfs_active_read(pos) && pos->parent == parent && hash == pos->hash;kernfs_put (pos);if (!valid)
@@ -1647,7 +1658,7 @@ static struct kernfs_node *kernfs_dir_pos(const void *ns, } }/* Skip over entries which are dying/dead or in the wrong namespace */ -while (pos && (!kernfs_active (pos) || pos->ns != ns)) { +while (pos && (!kernfs_active_read (pos) || pos->ns != ns)) {struct rb_node *node =rb_next (&pos->rb);if (!node) pos =NULL ;
@@ -1668,7 +1679,7 @@ static struct kernfs_node *kernfs_dir_next_pos(const void *ns, pos =NULL ; else pos =rb_to_kn (node); - }while (pos && (!kernfs_active (pos) || pos->ns != ns)); + }while (pos && (!kernfs_active_read (pos) || pos->ns != ns)); }return pos; }
@@ -1682,7 +1693,7 @@ static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)if (!dir_emit_dots (file, ctx))return 0 ; -mutex_lock (&kernfs_mutex); +down_read (&kernfs_rwsem);if (kernfs_ns_enabled (parent)) ns =kernfs_info (dentry->d_sb)->ns;
@@ -1699,12 +1710,12 @@ static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx) file->private_data = pos;kernfs_get (pos); -mutex_unlock (&kernfs_mutex); +up_read (&kernfs_rwsem);if (!dir_emit (ctx, name, len, ino, type))return 0 ; -mutex_lock (&kernfs_mutex); +down_read (&kernfs_rwsem); } -mutex_unlock (&kernfs_mutex); +up_read (&kernfs_rwsem); file->private_data =NULL ; ctx->pos =INT_MAX ;return 0 ;
fs/kernfs/file.c +2 -2
@@ -860,7 +860,7 @@ static void kernfs_notify_workfn(struct work_struct *work)spin_unlock_irq (&kernfs_notify_lock);/* kick fsnotify */ -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem);list_for_each_entry (info, &kernfs_root (kn)->supers, node) {struct kernfs_node *parent;
@@ -898,7 +898,7 @@ static void kernfs_notify_workfn(struct work_struct *work)iput (inode); } -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem);kernfs_put (kn); goto repeat; }
fs/kernfs/inode.c +8 -8
@@ -106,9 +106,9 @@ int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) {int ret; -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem); ret =__kernfs_setattr (kn, iattr); -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem);return ret; }
@@ -122,7 +122,7 @@ int kernfs_iop_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,if (!kn)return -EINVAL ; -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem); error =setattr_prepare (&init_user_ns, dentry, iattr);if (error) goto out;
@@ -135,7 +135,7 @@ int kernfs_iop_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,setattr_copy (&init_user_ns, inode, iattr); out: -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem);return error; }
@@ -191,9 +191,9 @@ int kernfs_iop_getattr(struct user_namespace *mnt_userns,struct inode *inode =d_inode (path->dentry);struct kernfs_node *kn = inode->i_private; -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem);kernfs_refresh_inode (kn, inode); -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem);generic_fillattr (&init_user_ns, inode, stat);return 0 ;
@@ -284,9 +284,9 @@ int kernfs_iop_permission(struct user_namespace *mnt_userns, kn = inode->i_private; -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem);kernfs_refresh_inode (kn, inode); -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem);return generic_permission (&init_user_ns, inode, mask); }
fs/kernfs/kernfs-internal.h +3 -2
@@ -13,6 +13,7 @@#include <linux/lockdep.h> #include <linux/fs.h> #include <linux/mutex.h> +#include <linux/rwsem.h> #include <linux/xattr.h> #include <linux/kernfs.h>
@@ -69,7 +70,7 @@ struct kernfs_super_info {*/ const void *ns; -/* anchored at kernfs_root->supers, protected by kernfs_mutex */ +/* anchored at kernfs_root->supers, protected by kernfs_rwsem */ struct list_head node; };#define kernfs_info (SB ) ((struct kernfs_super_info *)(SB->s_fs_info))
@@ -102,7 +103,7 @@ int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);/* * dir.c */ -extern struct mutex kernfs_mutex; +extern struct rw_semaphore kernfs_rwsem;extern const struct dentry_operations kernfs_dops;extern const struct file_operations kernfs_dir_fops;extern const struct inode_operations kernfs_dir_iops;
fs/kernfs/mount.c +6 -6
@@ -255,9 +255,9 @@ static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *k sb->s_shrink.seeks =0 ;/* get root inode, initialize and unlock it */ -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem); inode =kernfs_get_inode (sb, info->root->kn); -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem);if (!inode) {pr_debug ("kernfs: could not get root inode\n" );return -ENOMEM ;
@@ -344,9 +344,9 @@ int kernfs_get_tree(struct fs_context *fc) } sb->s_flags |=SB_ACTIVE ; -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem);list_add (&info->node, &info->root->supers); -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem); } fc->root =dget (sb->s_root);
@@ -372,9 +372,9 @@ void kernfs_kill_sb(struct super_block *sb) {struct kernfs_super_info *info =kernfs_info (sb); -mutex_lock (&kernfs_mutex); +down_write (&kernfs_rwsem);list_del (&info->node); -mutex_unlock (&kernfs_mutex); +up_write (&kernfs_rwsem);/* * Remove the superblock from fs_supers/s_instances
fs/kernfs/symlink.c +2 -2
@@ -116,9 +116,9 @@ static int kernfs_getlink(struct inode *inode, char *path)struct kernfs_node *target = kn->symlink.target_kn;int error; -mutex_lock (&kernfs_mutex); +down_read (&kernfs_rwsem); error =kernfs_get_target_path (parent, target, path); -mutex_unlock (&kernfs_mutex); +up_read (&kernfs_rwsem);return error; }
include/linux/kernfs.h +1 -1
@@ -188,7 +188,7 @@ struct kernfs_root {u32 id_highbits;struct kernfs_syscall_ops *syscall_ops; -/* list of kernfs_super_info of this root, protected by kernfs_mutex */ +/* list of kernfs_super_info of this root, protected by kernfs_rwsem */ struct list_head supers;wait_queue_head_t deactivate_waitq;
PATCH v4 4/5 kernfs: use i_lock to protect concurrent inode updates
In reply to Ian KentThe inode operations .permission() and .getattr() use the kernfs node write lock but all that's needed is to keep the rb tree stable while updating the inode attributes as well as protecting the update itself against concurrent changes.
And .permission() is called frequently during path walks and can cause quite a bit of contention between kernfs node operations and path walks when the number of concurrent walks is high.
To change kernfs_iop_getattr() and kernfs_iop_permission() to take the rw sem read lock instead of the write lock an additional lock is needed to protect against multiple processes concurrently updating the inode attributes and link count in kernfs_refresh_inode().
The inode i_lock seems like the sensible thing to use to protect these inode attribute updates so use it in kernfs_refresh_inode().
--- fs/kernfs/inode.c | 10 ++++++---- fs/kernfs/mount.c | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-)
fs/kernfs/inode.c +6 -4
@@ -172,6 +172,7 @@ static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode) {struct kernfs_iattrs *attrs = kn->iattr; +spin_lock (&inode->i_lock); inode->i_mode = kn->mode;if (attrs)/*
@@ -182,6 +183,7 @@ static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)if (kernfs_type (kn) ==KERNFS_DIR )set_nlink (inode, kn->dir.subdirs +2 ); +spin_unlock (&inode->i_lock); } int kernfs_iop_getattr(struct user_namespace *mnt_userns,
@@ -191,9 +193,9 @@ int kernfs_iop_getattr(struct user_namespace *mnt_userns,struct inode *inode =d_inode (path->dentry);struct kernfs_node *kn = inode->i_private; -down_write (&kernfs_rwsem); +down_read (&kernfs_rwsem);kernfs_refresh_inode (kn, inode); -up_write (&kernfs_rwsem); +up_read (&kernfs_rwsem);generic_fillattr (&init_user_ns, inode, stat);return 0 ;
@@ -284,9 +286,9 @@ int kernfs_iop_permission(struct user_namespace *mnt_userns, kn = inode->i_private; -down_write (&kernfs_rwsem); +down_read (&kernfs_rwsem);kernfs_refresh_inode (kn, inode); -up_write (&kernfs_rwsem); +up_read (&kernfs_rwsem);return generic_permission (&init_user_ns, inode, mask); }
fs/kernfs/mount.c +2 -2
@@ -255,9 +255,9 @@ static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *k sb->s_shrink.seeks =0 ;/* get root inode, initialize and unlock it */ -down_write (&kernfs_rwsem); +down_read (&kernfs_rwsem); inode =kernfs_get_inode (sb, info->root->kn); -up_write (&kernfs_rwsem); +up_read (&kernfs_rwsem);if (!inode) {pr_debug ("kernfs: could not get root inode\n" );return -ENOMEM ;
PATCH v4 5/5 kernfs: add kernfs_need_inode_refresh()
In reply to Ian KentNow the kernfs_rwsem read lock is held for kernfs_refresh_inode() and the i_lock taken to protect inode updates there can be some contention introduced when .permission() is called with concurrent path walks in progress.
Since .permission() is called frequently during path walks it's worth checking if the update is actually needed before taking the lock and performing the update.
--- fs/kernfs/inode.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
fs/kernfs/inode.c +27 -0
@@ -158,6 +158,30 @@ static inline void set_default_inode_attr(struct inode *inode, umode_t mode) inode->i_ctime =current_time (inode); } +static bool kernfs_need_inode_refresh (struct kernfs_node *kn, +struct inode *inode, +struct kernfs_iattrs *attrs) +{ +if (kernfs_type (kn) ==KERNFS_DIR ) { +if (inode->i_nlink != kn->dir.subdirs +2 ) +return true; + } + +if (inode->i_mode != kn->mode) +return true; + +if (attrs) { +if (!timespec64_equal (&inode->i_atime, &attrs->ia_atime) || + !timespec64_equal (&inode->i_mtime, &attrs->ia_mtime) || + !timespec64_equal (&inode->i_ctime, &attrs->ia_ctime) || + !uid_eq (inode->i_uid, attrs->ia_uid) || + !gid_eq (inode->i_gid, attrs->ia_gid)) +return true; + } + +return false; +} +static inline void set_inode_attr (struct inode *inode,struct kernfs_iattrs *attrs) {
@@ -172,6 +196,9 @@ static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode) {struct kernfs_iattrs *attrs = kn->iattr; +if (!kernfs_need_inode_refresh (kn, inode, attrs)) +return ; +spin_lock (&inode->i_lock); inode->i_mode = kn->mode;if (attrs)
Greg Kroah-Hartman
maintainerQuoted 65 lines · Ian Kent
There have been a few instances of contention on the
kernfs_mutexduring path walks, a case on very large IBM systems seen by myself, a report by Brice Goglin and followed up by Fox Chen, and I've since seen a couple of other reports by CoreOS users.The common thread is a large number of kernfs path walks leading to slowness of path walks due to
kernfs_mutexcontention.The problem being that changes to the VFS over some time have increased it's concurrency capabilities to an extent that kernfs's use of a mutex is no longer appropriate. There's also an issue of walks for non-existent paths causing contention if there are quite a few of them which is a less common problem.
This patch series is relatively straight forward.
All it does is add the ability to take advantage of VFS negative dentry caching to avoid needless dentry alloc/free cycles for lookups of paths that don't exit and change the
kernfs_mutexto a read/write semaphore.The patch that tried to stay in VFS rcu-walk mode during path walks has been dropped for two reasons. First, it doesn't actually give very much improvement and, second, if there's a place where mistakes could go unnoticed it would be in that path. This makes the patch series simpler to review and reduces the likelihood of problems going unnoticed and popping up later.
The patch to use a revision to identify if a directory has changed has also been dropped. If the directory has changed the dentry revision needs to be updated to avoid subsequent rb tree searches and after changing to use a read/write semaphore the update also requires a lock. But the
d_lockis the only lock available at this point which might itself be contended.Changes since v3:
- remove unneeded indirection when referencing the super block.
- check if inode attribute update is actually needed.Changes since v2:
- actually fix the inode attribute update locking.
- drop the patch that tried to stay in rcu-walk mode.
- drop the use a revision to identify if a directory has changed patch.Changes since v1:
- fix locking in .permission() and .getattr() by re-factoring the attribute handling code.
---Ian Kent (5):
kernfs: move revalidate to be near lookup
kernfs: use VFS negative dentry caching
kernfs: switch kernfs to use an rwsem
kernfs: usei_lockto protect concurrent inode updates
kernfs: addkernfs_need_inode_refresh()fs/kernfs/dir.c | 170 ++++++++++++++++++++---------------- fs/kernfs/file.c | 4 +- fs/kernfs/inode.c | 45 ++++++++-- fs/kernfs/kernfs-internal.h | 5 +- fs/kernfs/mount.c | 12 +-- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 2 +- 7 files changed, 147 insertions(+), 95 deletions(-)
Quoted 3 lines
--
Ian
Any benchmark numbers that you ran that are better/worse with this patch series? That woul dbe good to know, otherwise you aren't changing functionality here, so why would we take these changes? :)
thanks,
greg k-h
Quoted 67 lines · Greg Kroah-Hartman
Quoted 65 lines · Ian Kent
There have been a few instances of contention on the
kernfs_mutexduring path walks, a case on very large IBM systems seen by myself, a report by Brice Goglin and followed up by Fox Chen, and I've since seen a couple of other reports by CoreOS users.The common thread is a large number of kernfs path walks leading to slowness of path walks due to
kernfs_mutexcontention.The problem being that changes to the VFS over some time have increased it's concurrency capabilities to an extent that kernfs's use of a mutex is no longer appropriate. There's also an issue of walks for non-existent paths causing contention if there are quite a few of them which is a less common problem.
This patch series is relatively straight forward.
All it does is add the ability to take advantage of VFS negative dentry caching to avoid needless dentry alloc/free cycles for lookups of paths that don't exit and change the
kernfs_mutexto a read/write semaphore.The patch that tried to stay in VFS rcu-walk mode during path walks has been dropped for two reasons. First, it doesn't actually give very much improvement and, second, if there's a place where mistakes could go unnoticed it would be in that path. This makes the patch series simpler to review and reduces the likelihood of problems going unnoticed and popping up later.
The patch to use a revision to identify if a directory has changed has also been dropped. If the directory has changed the dentry revision needs to be updated to avoid subsequent rb tree searches and after changing to use a read/write semaphore the update also requires a lock. But the
d_lockis the only lock available at this point which might itself be contended.Changes since v3:
- remove unneeded indirection when referencing the super block.
- check if inode attribute update is actually needed.Changes since v2:
- actually fix the inode attribute update locking.
- drop the patch that tried to stay in rcu-walk mode.
- drop the use a revision to identify if a directory has changed patch.Changes since v1:
- fix locking in .permission() and .getattr() by re-factoring the attribute handling code.
---Ian Kent (5):
kernfs: move revalidate to be near lookup
kernfs: use VFS negative dentry caching
kernfs: switch kernfs to use an rwsem
kernfs: usei_lockto protect concurrent inode updates
kernfs: addkernfs_need_inode_refresh()fs/kernfs/dir.c | 170 ++++++++++++++++++++---------------- fs/kernfs/file.c | 4 +- fs/kernfs/inode.c | 45 ++++++++-- fs/kernfs/kernfs-internal.h | 5 +- fs/kernfs/mount.c | 12 +-- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 2 +- 7 files changed, 147 insertions(+), 95 deletions(-)
Quoted 7 lines
Quoted 3 lines
--
IanAny benchmark numbers that you ran that are better/worse with this patch series? That woul dbe good to know, otherwise you aren't changing functionality here, so why would we take these changes? :)
Let me run it on my benchmark and bring back the result to you.
Quoted 3 lines
thanks,
greg k-h
thanks,
fox
Hi,
I ran it on my benchmark (https://github.com/foxhlchen/sysfs_benchmark).
machine: aws c5 (Intel Xeon with 96 logical cores)
kernel: v5.12
benchmark: create 96 threads and bind them to each core then run open+read+close on a sysfs file simultaneously for 1000 times. result:
Without the patchset, an open+read+close operation takes 550-570 us, perf shows significant time(>40%) spending on mutex_lock. After applying it, it takes 410-440 us for that operation and perf shows only ~4% time on mutex_lock.
It's weird, I don't see a huge performance boost compared to v2, even though there is no mutex problem from the perf report.
I've put console outputs and perf reports on the attachment for your reference.
thanks,
fox
Quoted 15 lines · Fox Chen
Hi,
I ran it on my benchmark (https://github.com/foxhlchen/sysfs_benchmark).
machine: aws c5 (Intel Xeon with 96 logical cores)
kernel: v5.12
benchmark: create 96 threads and bind them to each core then run open+read+close on a sysfs file simultaneously for 1000 times. result:
Without the patchset, an open+read+close operation takes 550-570 us, perf shows significant time(>40%) spending onmutex_lock. After applying it, it takes 410-440 us for that operation and perf shows only ~4% time onmutex_lock.
Quoted 1 line
It's weird, I don't see a huge performance boost compared to v2, even
I meant I don't see a huge performance boost here and it's way worse than v2. IIRC, for v2 fastest one only takes 40us
Quoted 6 lines
though there is no mutex problem from the perf report.
I've put console outputs and perf reports on the attachment for your reference.thanks,
fox
fox
Quoted 18 lines · Fox Chen
Quoted 17 lines · Fox Chen
Hi,
I ran it on my benchmark (
https://github.com/foxhlchen/sysfs_benchmark).machine: aws c5 (Intel Xeon with 96 logical cores)
kernel: v5.12
benchmark: create 96 threads and bind them to each core then run open+read+close on a sysfs file simultaneously for 1000 times. result:
Without the patchset, an open+read+close operation takes 550-570 us,
perf shows significant time(>40%) spending onmutex_lock. After applying it, it takes 410-440 us for that operation and perf shows only ~4% time onmutex_lock.
Quoted 6 lines
Quoted 2 lines
It's weird, I don't see a huge performance boost compared to v2, even
I meant I don't see a huge performance boost here and it's way worse than v2.
IIRC, for v2 fastest one only takes 40us
Thanks Fox,
I'll have a look at those reports but this is puzzling.
Perhaps the added overhead of the check if an update is needed is taking more than expected and more than just taking the lock and being done with it. Then there's the v2 series ... I'll see if I can dig out your reports on those too.
Quoted 5 lines
Quoted 5 lines
though there is no mutex problem from the perf report. I've put console outputs and perf reports on the attachment for your reference.
Yep, thanks.
Ian
Hi Ian
Quoted 29 lines · Ian Kent
Quoted 18 lines · Fox Chen
Quoted 17 lines · Fox Chen
Hi,
I ran it on my benchmark (
https://github.com/foxhlchen/sysfs_benchmark).machine: aws c5 (Intel Xeon with 96 logical cores)
kernel: v5.12
benchmark: create 96 threads and bind them to each core then run open+read+close on a sysfs file simultaneously for 1000 times. result:
Without the patchset, an open+read+close operation takes 550-570 us,
perf shows significant time(>40%) spending onmutex_lock. After applying it, it takes 410-440 us for that operation and perf shows only ~4% time onmutex_lock.Quoted 6 lines
Quoted 2 lines
It's weird, I don't see a huge performance boost compared to v2, even
I meant I don't see a huge performance boost here and it's way worse than v2.
IIRC, for v2 fastest one only takes 40usThanks Fox,
Quoted 7 lines
I'll have a look at those reports but this is puzzling.
Perhaps the added overhead of the check if an update is needed is taking more than expected and more than just taking the lock and being done with it. Then there's the v2 series ... I'll see if I can dig out your reports on those too.
Apologies, I was mistaken, it's compared to V3, not V2. The previous benchmark report is here.
https://lore.kernel.org/linux-fsdevel/CAC2o3DKNc=sL2n8291Dpiyb0bRHaX=nd33ogvO_LkJqpBj-YmA@mail.gmail.com/
Quoted 9 lines
Quoted 5 lines
Quoted 5 lines
though there is no mutex problem from the perf report. I've put console outputs and perf reports on the attachment for your reference.
Yep, thanks.
Ian
thanks,
fox
Quoted 46 lines · Fox Chen
Hi Ian
Quoted 35 lines · Ian Kent
Quoted 22 lines · Fox Chen
Quoted 20 lines · Fox Chen
Hi,
I ran it on my benchmark (
https://github.com/foxhlchen/sysfs_benchmark).machine: aws c5 (Intel Xeon with 96 logical cores)
kernel: v5.12
benchmark: create 96 threads and bind them to each core then run
open+read+close on a sysfs file simultaneously for 1000 times. result:
Without the patchset, an open+read+close operation takes 550- 570
us,
perf shows significant time(>40%) spending onmutex_lock. After applying it, it takes 410-440 us for that operation and perf
shows only ~4% time onmutex_lock.Quoted 8 lines
Quoted 3 lines
It's weird, I don't see a huge performance boost compared to v2,
evenI meant I don't see a huge performance boost here and it's way worse
than v2.
IIRC, for v2 fastest one only takes 40usThanks Fox,
Quoted 7 lines
I'll have a look at those reports but this is puzzling.
Perhaps the added overhead of the check if an update is needed is taking more than expected and more than just taking the lock and being done with it. Then there's the v2 series ... I'll see if I can dig out your reports on those too.
Quoted 3 lines
Apologies, I was mistaken, it's compared to V3, not V2. The previous benchmark report is here.
https://lore.kernel.org/linux-fsdevel/CAC2o3DKNc=sL2n8291Dpiyb0bRHaX=nd33ogvO_LkJqpBj-YmA@mail.gmail.com/
Are all these tests using a single file name in the open/read/close loop?
That being the case the per-object inode lock will behave like a mutex and once contention occurs any speed benefits of a spinlock over a mutex (or rwsem) will disappear.
In this case changing from a write lock to a read lock in those functions and adding the inode mutex will do nothing but add the overhead of taking the read lock. And similarly adding the update check function also just adds overhead and, as we see, once contention starts it has a cumulative effect that's often not linear.
The whole idea of a read lock/per-object spin lock was to reduce the possibility of contention for paths other than the same path while not impacting same path accesses too much for an overall gain. Based on this I'm thinking the update check function is probably not worth keeping, it just adds unnecessary churn and has a negative impact for same file contention access patterns.
I think that using multiple paths, at least one per test process (so if you are running 16 processes use at least 16 different files, the same in each process), and selecting one at random for each loop of the open would better simulate real world access patterns.
Ian
Quoted 48 lines · Ian Kent
Quoted 46 lines · Fox Chen
Hi Ian
Quoted 42 lines · Ian Kent
Quoted 22 lines · Fox Chen
Quoted 20 lines · Fox Chen
Hi,
I ran it on my benchmark (
https://github.com/foxhlchen/sysfs_benchmark).machine: aws c5 (Intel Xeon with 96 logical cores)
kernel: v5.12
benchmark: create 96 threads and bind them to each core then run
open+read+close on a sysfs file simultaneously for 1000 times. result:
Without the patchset, an open+read+close operation takes 550- 570
us,
perf shows significant time(>40%) spending onmutex_lock. After applying it, it takes 410-440 us for that operation and perf
shows only ~4% time onmutex_lock.Quoted 8 lines
Quoted 3 lines
It's weird, I don't see a huge performance boost compared to v2,
evenI meant I don't see a huge performance boost here and it's way worse
than v2.
IIRC, for v2 fastest one only takes 40usThanks Fox,
I'll have a look at those reports but this is puzzling.
Perhaps the added overhead of the check if an update is needed is taking more than expected and more than just taking the lock and being done with it. Then there's the v2 series ... I'll see if I can dig out your reports on those too.
Quoted 6 lines
Quoted 3 lines
Apologies, I was mistaken, it's compared to V3, not V2. The previous benchmark report is here.
https://lore.kernel.org/linux-fsdevel/CAC2o3DKNc=sL2n8291Dpiyb0bRHaX=nd33ogvO_LkJqpBj-YmA@mail.gmail.com/Are all these tests using a single file name in the open/read/close loop?
Yes, because It's easy to implement yet enough to trigger the mutex_lock.
And you are right It's not a real-life pattern, but on the bright side, it proves there is no original mutex_lock problem anymore. :)
Quoted 18 lines
That being the case the per-object inode lock will behave like a mutex and once contention occurs any speed benefits of a spinlock over a mutex (or rwsem) will disappear.
In this case changing from a write lock to a read lock in those functions and adding the inode mutex will do nothing but add the overhead of taking the read lock. And similarly adding the update check function also just adds overhead and, as we see, once contention starts it has a cumulative effect that's often not linear.
The whole idea of a read lock/per-object spin lock was to reduce the possibility of contention for paths other than the same path while not impacting same path accesses too much for an overall gain. Based on this I'm thinking the update check function is probably not worth keeping, it just adds unnecessary churn and has a negative impact for same file contention access patterns.
Quoted 9 lines
I think that using multiple paths, at least one per test process (so if you are running 16 processes use at least 16 different files, the same in each process), and selecting one at random for each loop of the open would better simulate real world access patterns.
Ian
thanks,
fox
Quoted 62 lines · Fox Chen
Quoted 61 lines · Ian Kent
Quoted 58 lines · Fox Chen
Hi Ian
Quoted 42 lines · Ian Kent
Quoted 32 lines · Fox Chen
Quoted 24 lines · Fox Chen
Hi,
I ran it on my benchmark (
https://github.com/foxhlchen/sysfs_benchmark).machine: aws c5 (Intel Xeon with 96 logical cores)
kernel: v5.12
benchmark: create 96 threads and bind them to each core then
run
open+read+close on a sysfs file simultaneously for 1000 times.
result:
Without the patchset, an open+read+close operation takes 550-
570
us,
perf shows significant time(>40%) spending onmutex_lock. After applying it, it takes 410-440 us for that operation and
perf
shows only ~4% time onmutex_lock.Quoted 4 lines
It's weird, I don't see a huge performance boost compared to
v2,
evenQuoted 5 lines
I meant I don't see a huge performance boost here and it's way
worse
than v2.
IIRC, for v2 fastest one only takes 40usThanks Fox,
Quoted 7 lines
I'll have a look at those reports but this is puzzling.
Perhaps the added overhead of the check if an update is needed is taking more than expected and more than just taking the lock and being done with it. Then there's the v2 series ... I'll see if I can dig out your reports on those too.
Apologies, I was mistaken, it's compared to V3, not V2. The previous
benchmark report is here.
https://lore.kernel.org/linux-fsdevel/CAC2o3DKNc=sL2n8291Dpiyb0bRHaX=nd33ogvO_LkJqpBj-YmA@mail.gmail.com/
Quoted 8 lines
Quoted 2 lines
Are all these tests using a single file name in the open/read/close loop?
Yes, because It's easy to implement yet enough to trigger the
mutex_lock.And you are right It's not a real-life pattern, but on the bright side, it proves there is no original
mutex_lockproblem anymore. :)
I've been looking at your reports and they are quite interesting.
Quoted 12 lines
Quoted 12 lines
That being the case the per-object inode lock will behave like a mutex and once contention occurs any speed benefits of a spinlock over a mutex (or rwsem) will disappear.
In this case changing from a write lock to a read lock in those functions and adding the inode mutex will do nothing but add the overhead of taking the read lock. And similarly adding the update check function also just adds overhead and, as we see, once contention starts it has a cumulative effect that's often not linear.
Quoted 6 lines
Quoted 6 lines
The whole idea of a read lock/per-object spin lock was to reduce the possibility of contention for paths other than the same path while not impacting same path accesses too much for an overall gain. Based on this I'm thinking the update check function is probably not worth keeping, it just adds unnecessary churn and has a negative impact for same file contention access patterns.
The reports indicate (to me anyway) that the slowdown isn't due to kernfs. It looks more like kernfs is now putting pressure on the VFS, mostly on the file table lock but it looks like there's a mild amount of contention on a few other locks as well now.
That's a whole different problem and those file table handling functions don't appear to have any obvious problems so they are doing what they have to do and that can't be avoided.
That's definitely out of scope for these changes.
And, as you'd expect, once any appreciable amount of contention happens our measurements go out the window, certainly with respect to kernfs.
It also doesn't change my option that checking if an inode attribute update is needed in kernfs isn't useful since, IIUC that file table lock contention would result even if you were using different paths.
So I'll drop that patch from the series.
Ian
Quoted 14 lines
Quoted 10 lines
I think that using multiple paths, at least one per test process (so if you are running 16 processes use at least 16 different files, the same in each process), and selecting one at random for each loop of the open would better simulate real world access patterns.
Ian
thanks,
fox
Quoted 94 lines · Ian Kent
Quoted 62 lines · Fox Chen
Quoted 61 lines · Ian Kent
Quoted 58 lines · Fox Chen
Hi Ian
Quoted 42 lines · Ian Kent
Quoted 32 lines · Fox Chen
Quoted 24 lines · Fox Chen
Hi,
I ran it on my benchmark (
https://github.com/foxhlchen/sysfs_benchmark).machine: aws c5 (Intel Xeon with 96 logical cores)
kernel: v5.12
benchmark: create 96 threads and bind them to each core then
run
open+read+close on a sysfs file simultaneously for 1000 times.
result:
Without the patchset, an open+read+close operation takes 550-
570
us,
perf shows significant time(>40%) spending onmutex_lock. After applying it, it takes 410-440 us for that operation and
perf
shows only ~4% time onmutex_lock.Quoted 4 lines
It's weird, I don't see a huge performance boost compared to
v2,
evenQuoted 5 lines
I meant I don't see a huge performance boost here and it's way
worse
than v2.
IIRC, for v2 fastest one only takes 40usThanks Fox,
Quoted 7 lines
I'll have a look at those reports but this is puzzling.
Perhaps the added overhead of the check if an update is needed is taking more than expected and more than just taking the lock and being done with it. Then there's the v2 series ... I'll see if I can dig out your reports on those too.
Apologies, I was mistaken, it's compared to V3, not V2. The previous
benchmark report is here.
https://lore.kernel.org/linux-fsdevel/CAC2o3DKNc=sL2n8291Dpiyb0bRHaX=nd33ogvO_LkJqpBj-YmA@mail.gmail.com/Quoted 8 lines
Quoted 2 lines
Are all these tests using a single file name in the open/read/close loop?
Yes, because It's easy to implement yet enough to trigger the
mutex_lock.And you are right It's not a real-life pattern, but on the bright side, it proves there is no original
mutex_lockproblem anymore. :)I've been looking at your reports and they are quite interesting.
Quoted 12 lines
Quoted 12 lines
That being the case the per-object inode lock will behave like a mutex and once contention occurs any speed benefits of a spinlock over a mutex (or rwsem) will disappear.
In this case changing from a write lock to a read lock in those functions and adding the inode mutex will do nothing but add the overhead of taking the read lock. And similarly adding the update check function also just adds overhead and, as we see, once contention starts it has a cumulative effect that's often not linear.
Quoted 6 lines
Quoted 6 lines
The whole idea of a read lock/per-object spin lock was to reduce the possibility of contention for paths other than the same path while not impacting same path accesses too much for an overall gain. Based on this I'm thinking the update check function is probably not worth keeping, it just adds unnecessary churn and has a negative impact for same file contention access patterns.
Quoted 5 lines
The reports indicate (to me anyway) that the slowdown isn't due to kernfs. It looks more like kernfs is now putting pressure on the VFS, mostly on the file table lock but it looks like there's a mild amount of contention on a few other locks as well now.
That's correct, I ran my benchmark on ext4 the result was similarly slow. But It shouldn't be that as I remember I tested it before it was very fast and you can see the result of V3 was much faster. So I ran this benchmark again on AWS c5a which also has 96 cores but with AMD CPUs. The result was amazing the fastest one had a 10x boost (~40us) very similar to the V3 one (see attachment) I guess my previous benchmark of V3 was run on c5a.
I can't figure why it is so slow on Intel's CPUs, I also tried C5.metal which is running on the physical machine, the result is still slow (~200us). But anyway, at least it shows this patchset solves the mutex_lock problem and can bring even 10x boosts on some occasions.
Quoted 10 lines
That's a whole different problem and those file table handling functions don't appear to have any obvious problems so they are doing what they have to do and that can't be avoided.
That's definitely out of scope for these changes.
And, as you'd expect, once any appreciable amount of contention happens our measurements go out the window, certainly with respect to kernfs.
Quoted 24 lines
It also doesn't change my option that checking if an inode attribute update is needed in kernfs isn't useful since, IIUC that file table lock contention would result even if you were using different paths.
So I'll drop that patch from the series.
Ian
Quoted 14 lines
Quoted 10 lines
I think that using multiple paths, at least one per test process (so if you are running 16 processes use at least 16 different files, the same in each process), and selecting one at random for each loop of the open would better simulate real world access patterns.
Ian
thanks,
fox
thanks,
fox
Quoted 110 lines · Ian Kent
Quoted 66 lines · Fox Chen
Quoted 56 lines · Ian Kent
Quoted 54 lines · Fox Chen
Hi Ian
Quoted 49 lines · Ian Kent
Quoted 32 lines · Fox Chen
Quoted 24 lines · Fox Chen
Hi,
I ran it on my benchmark (
https://github.com/foxhlchen/sysfs_benchmark).machine: aws c5 (Intel Xeon with 96 logical cores)
kernel: v5.12
benchmark: create 96 threads and bind them to each core then
run
open+read+close on a sysfs file simultaneously for 1000 times.
result:
Without the patchset, an open+read+close operation takes 550-
570
us,
perf shows significant time(>40%) spending onmutex_lock. After applying it, it takes 410-440 us for that operation and
perf
shows only ~4% time onmutex_lock.Quoted 4 lines
It's weird, I don't see a huge performance boost compared to
v2,
evenQuoted 5 lines
I meant I don't see a huge performance boost here and it's way
worse
than v2.
IIRC, for v2 fastest one only takes 40usThanks Fox,
I'll have a look at those reports but this is puzzling.
Perhaps the added overhead of the check if an update is needed is taking more than expected and more than just taking the lock and being done with it. Then there's the v2 series ... I'll see if I can dig out your reports on those too.
Quoted 8 lines
Quoted 4 lines
Apologies, I was mistaken, it's compared to V3, not V2. The previous
benchmark report is here.
https://lore.kernel.org/linux-fsdevel/CAC2o3DKNc=sL2n8291Dpiyb0bRHaX=nd33ogvO_LkJqpBj-YmA@mail.gmail.com/Are all these tests using a single file name in the open/read/close
loop?Quoted 5 lines
Yes, because It's easy to implement yet enough to trigger the
mutex_lock.And you are right It's not a real-life pattern, but on the bright side, it proves there is no original
mutex_lockproblem anymore. :)I've been looking at your reports and they are quite interesting.
Quoted 12 lines
Quoted 12 lines
That being the case the per-object inode lock will behave like a mutex and once contention occurs any speed benefits of a spinlock over a mutex (or rwsem) will disappear.
In this case changing from a write lock to a read lock in those functions and adding the inode mutex will do nothing but add the overhead of taking the read lock. And similarly adding the update check function also just adds overhead and, as we see, once contention starts it has a cumulative effect that's often not linear.
Quoted 6 lines
Quoted 6 lines
The whole idea of a read lock/per-object spin lock was to reduce the possibility of contention for paths other than the same path while not impacting same path accesses too much for an overall gain. Based on this I'm thinking the update check function is probably not worth keeping, it just adds unnecessary churn and has a negative impact for same file contention access patterns.
The reports indicate (to me anyway) that the slowdown isn't due to kernfs. It looks more like kernfs is now putting pressure on the VFS, mostly on the file table lock but it looks like there's a mild amount of contention on a few other locks as well now.
That's a whole different problem and those file table handling functions don't appear to have any obvious problems so they are doing what they have to do and that can't be avoided.
That's definitely out of scope for these changes.
And, as you'd expect, once any appreciable amount of contention happens our measurements go out the window, certainly with respect to kernfs.
Quoted 6 lines
It also doesn't change my option that checking if an inode attribute update is needed in kernfs isn't useful since, IIUC that file table lock contention would result even if you were using different paths.
So I'll drop that patch from the series.
It will probably not make any difference, but for completeness and after some thought, I felt I should post what I think is happening with the benchmark.
The benchmark application runs a pthreads thread for each CPU and goes into a tight open/read/close loop to demonstrate the contention that can occur on the kernfs mutex as the number of CPUs grows.
But that tight open/read/close loop causes contention on the VFS file table because the pthreads threads share the process file table.
So the poor performance is actually evidence the last patch is doing what it's meant to do rather than evidence of a regression with the series.
The benchmark is putting pressure on the process file table on some hardware configurations but those critical sections are small and there's really nothing obvious that can be done to improve the file table locking.
It is however important to remember that the benckmark application access pattern is not a normal access pattern by a long way.
So I don't see the need for a new revision of the series with the last patch dropped.
If there's a change of heart and the series was to be merged I'll leave whether to include this last patch to the discretion of the maintainer as the bulk of the improvement comes from the earlier patches anyway.
Quoted 17 lines
Ian
Quoted 14 lines
Quoted 10 lines
I think that using multiple paths, at least one per test process (so if you are running 16 processes use at least 16 different files, the same in each process), and selecting one at random for each loop of the open would better simulate real world access patterns.
Ian
thanks,
fox
Greg Kroah-Hartman
maintainerQuoted 143 lines · Ian Kent
Quoted 110 lines · Ian Kent
Quoted 66 lines · Fox Chen
Quoted 56 lines · Ian Kent
Quoted 54 lines · Fox Chen
Hi Ian
Quoted 49 lines · Ian Kent
Quoted 32 lines · Fox Chen
Quoted 24 lines · Fox Chen
Hi,
I ran it on my benchmark (
https://github.com/foxhlchen/sysfs_benchmark).machine: aws c5 (Intel Xeon with 96 logical cores)
kernel: v5.12
benchmark: create 96 threads and bind them to each core then
run
open+read+close on a sysfs file simultaneously for 1000 times.
result:
Without the patchset, an open+read+close operation takes 550-
570
us,
perf shows significant time(>40%) spending onmutex_lock. After applying it, it takes 410-440 us for that operation and
perf
shows only ~4% time onmutex_lock.Quoted 4 lines
It's weird, I don't see a huge performance boost compared to
v2,
evenQuoted 5 lines
I meant I don't see a huge performance boost here and it's way
worse
than v2.
IIRC, for v2 fastest one only takes 40usThanks Fox,
I'll have a look at those reports but this is puzzling.
Perhaps the added overhead of the check if an update is needed is taking more than expected and more than just taking the lock and being done with it. Then there's the v2 series ... I'll see if I can dig out your reports on those too.
Quoted 8 lines
Quoted 4 lines
Apologies, I was mistaken, it's compared to V3, not V2. The previous
benchmark report is here.
https://lore.kernel.org/linux-fsdevel/CAC2o3DKNc=sL2n8291Dpiyb0bRHaX=nd33ogvO_LkJqpBj-YmA@mail.gmail.com/Are all these tests using a single file name in the open/read/close
loop?Quoted 5 lines
Yes, because It's easy to implement yet enough to trigger the
mutex_lock.And you are right It's not a real-life pattern, but on the bright side, it proves there is no original
mutex_lockproblem anymore. :)I've been looking at your reports and they are quite interesting.
Quoted 12 lines
Quoted 12 lines
That being the case the per-object inode lock will behave like a mutex and once contention occurs any speed benefits of a spinlock over a mutex (or rwsem) will disappear.
In this case changing from a write lock to a read lock in those functions and adding the inode mutex will do nothing but add the overhead of taking the read lock. And similarly adding the update check function also just adds overhead and, as we see, once contention starts it has a cumulative effect that's often not linear.
Quoted 6 lines
Quoted 6 lines
The whole idea of a read lock/per-object spin lock was to reduce the possibility of contention for paths other than the same path while not impacting same path accesses too much for an overall gain. Based on this I'm thinking the update check function is probably not worth keeping, it just adds unnecessary churn and has a negative impact for same file contention access patterns.
The reports indicate (to me anyway) that the slowdown isn't due to kernfs. It looks more like kernfs is now putting pressure on the VFS, mostly on the file table lock but it looks like there's a mild amount of contention on a few other locks as well now.
That's a whole different problem and those file table handling functions don't appear to have any obvious problems so they are doing what they have to do and that can't be avoided.
That's definitely out of scope for these changes.
And, as you'd expect, once any appreciable amount of contention happens our measurements go out the window, certainly with respect to kernfs.
Quoted 6 lines
It also doesn't change my option that checking if an inode attribute update is needed in kernfs isn't useful since, IIUC that file table lock contention would result even if you were using different paths.
So I'll drop that patch from the series.
It will probably not make any difference, but for completeness and after some thought, I felt I should post what I think is happening with the benchmark.
The benchmark application runs a pthreads thread for each CPU and goes into a tight open/read/close loop to demonstrate the contention that can occur on the kernfs mutex as the number of CPUs grows.
But that tight open/read/close loop causes contention on the VFS file table because the pthreads threads share the process file table.
So the poor performance is actually evidence the last patch is doing what it's meant to do rather than evidence of a regression with the series.
The benchmark is putting pressure on the process file table on some hardware configurations but those critical sections are small and there's really nothing obvious that can be done to improve the file table locking.
It is however important to remember that the benckmark application access pattern is not a normal access pattern by a long way.
Quoted 7 lines
So I don't see the need for a new revision of the series with the last patch dropped.
If there's a change of heart and the series was to be merged I'll leave whether to include this last patch to the discretion of the maintainer as the bulk of the improvement comes from the earlier patches anyway.
Can you please resubmit the series, it is long-gone from my review queue.
thanks,
greg k-h
Quoted 169 lines · Greg Kroah-Hartman
Quoted 168 lines · Ian Kent
Quoted 132 lines · Ian Kent
Quoted 76 lines · Fox Chen
Quoted 70 lines · Ian Kent
Quoted 62 lines · Fox Chen
Hi Ian
Quoted 49 lines · Ian Kent
Quoted 38 lines · Fox Chen
Quoted 29 lines · Fox Chen
Hi,
I ran it on my benchmark (
https://github.com/foxhlchen/sysfs_benchmark).machine: aws c5 (Intel Xeon with 96 logical cores)
kernel: v5.12
benchmark: create 96 threads and bind them to each core
then
run
open+read+close on a sysfs file simultaneously for 1000
times.
result:
Without the patchset, an open+read+close operation takes
550-
570
us,
perf shows significant time(>40%) spending onmutex_lock.
After applying it, it takes 410-440 us for that operation
and
perf
shows only ~4% time onmutex_lock.Quoted 5 lines
It's weird, I don't see a huge performance boost
compared
to
v2,
evenQuoted 6 lines
I meant I don't see a huge performance boost here and it's
way
worse
than v2.
IIRC, for v2 fastest one only takes 40usThanks Fox,
Quoted 7 lines
I'll have a look at those reports but this is puzzling.
Perhaps the added overhead of the check if an update is needed is taking more than expected and more than just taking the lock and being done with it. Then there's the v2 series ... I'll see if I can dig out your reports on those too.
Quoted 5 lines
Apologies, I was mistaken, it's compared to V3, not V2. The
previous
benchmark report is here.
https://lore.kernel.org/linux-fsdevel/CAC2o3DKNc=sL2n8291Dpiyb0bRHaX=nd33ogvO_LkJqpBj-YmA@mail.gmail.com/Quoted 3 lines
Are all these tests using a single file name in the open/read/close
loop?Quoted 7 lines
Yes, because It's easy to implement yet enough to trigger the
mutex_lock.And you are right It's not a real-life pattern, but on the bright
side, it proves there is no originalmutex_lockproblem anymore. :)I've been looking at your reports and they are quite interesting.
Quoted 17 lines
Quoted 17 lines
That being the case the per-object inode lock will behave like a
mutex and once contention occurs any speed benefits of a spinlock
over a mutex (or rwsem) will disappear.In this case changing from a write lock to a read lock in those
functions and adding the inode mutex will do nothing but add the
overhead of taking the read lock. And similarly adding the update
check function also just adds overhead and, as we see, once contention starts it has a cumulative effect that's often not linear.Quoted 11 lines
Quoted 11 lines
The whole idea of a read lock/per-object spin lock was to reduce
the possibility of contention for paths other than the same path
while not impacting same path accesses too much for an overall
gain. Based on this I'm thinking the update check function is probably not worth keeping, it just adds unnecessary churn and
has a negative impact for same file contention access patterns.The reports indicate (to me anyway) that the slowdown isn't due to kernfs. It looks more like kernfs is now putting pressure on the VFS, mostly on the file table lock but it looks like there's a mild amount of contention on a few other locks as well now.
That's a whole different problem and those file table handling functions don't appear to have any obvious problems so they are doing what they have to do and that can't be avoided.
That's definitely out of scope for these changes.
And, as you'd expect, once any appreciable amount of contention happens our measurements go out the window, certainly with respect to kernfs.
Quoted 6 lines
It also doesn't change my option that checking if an inode attribute update is needed in kernfs isn't useful since, IIUC that file table lock contention would result even if you were using different paths.
So I'll drop that patch from the series.
It will probably not make any difference, but for completeness and after some thought, I felt I should post what I think is happening with the benchmark.
The benchmark application runs a pthreads thread for each CPU and goes into a tight open/read/close loop to demonstrate the contention that can occur on the kernfs mutex as the number of CPUs grows.
But that tight open/read/close loop causes contention on the VFS file table because the pthreads threads share the process file table.
So the poor performance is actually evidence the last patch is doing what it's meant to do rather than evidence of a regression with the series.
The benchmark is putting pressure on the process file table on some hardware configurations but those critical sections are small and there's really nothing obvious that can be done to improve the file table locking.
It is however important to remember that the benckmark application access pattern is not a normal access pattern by a long way.
So I don't see the need for a new revision of the series with the last patch dropped.
Quoted 7 lines
Quoted 4 lines
If there's a change of heart and the series was to be merged I'll leave whether to include this last patch to the discretion of the maintainer as the bulk of the improvement comes from the earlier patches anyway.
Can you please resubmit the series, it is long-gone from my review queue.
Sure, will do as soon as I can.
Thanks
Ian
Quoted 85 lines · Greg Kroah-Hartman
Quoted 84 lines · Ian Kent
There have been a few instances of contention on the
kernfs_mutexduring
path walks, a case on very large IBM systems seen by myself, a report by
Brice Goglin and followed up by Fox Chen, and I've since seen a couple
of other reports by CoreOS users.The common thread is a large number of kernfs path walks leading to slowness of path walks due to
kernfs_mutexcontention.The problem being that changes to the VFS over some time have increased
it's concurrency capabilities to an extent that kernfs's use of a mutex
is no longer appropriate. There's also an issue of walks for non- existent
paths causing contention if there are quite a few of them which is a less
common problem.This patch series is relatively straight forward.
All it does is add the ability to take advantage of VFS negative dentry
caching to avoid needless dentry alloc/free cycles for lookups of paths
that don't exit and change thekernfs_mutexto a read/write semaphore.The patch that tried to stay in VFS rcu-walk mode during path walks has
been dropped for two reasons. First, it doesn't actually give very much
improvement and, second, if there's a place where mistakes could go unnoticed it would be in that path. This makes the patch series simpler
to review and reduces the likelihood of problems going unnoticed and
popping up later.The patch to use a revision to identify if a directory has changed has
also been dropped. If the directory has changed the dentry revision needs to be updated to avoid subsequent rb tree searches and after changing to use a read/write semaphore the update also requires a lock.
But thed_lockis the only lock available at this point which might itself be contended.Changes since v3:
- remove unneeded indirection when referencing the super block.
- check if inode attribute update is actually needed.Changes since v2:
- actually fix the inode attribute update locking.
- drop the patch that tried to stay in rcu-walk mode.
- drop the use a revision to identify if a directory has changed patch.Changes since v1:
- fix locking in .permission() and .getattr() by re-factoring the attribute
handling code.
---Ian Kent (5):
kernfs: move revalidate to be near lookup
kernfs: use VFS negative dentry caching
kernfs: switch kernfs to use an rwsem
kernfs: usei_lockto protect concurrent inode updates
kernfs: addkernfs_need_inode_refresh()fs/kernfs/dir.c | 170 ++++++++++++++++++++------------
----
fs/kernfs/file.c | 4 +-
fs/kernfs/inode.c | 45 ++++++++--fs/kernfs/kernfs-internal.h| 5 +-
fs/kernfs/mount.c | 12 +-- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 2 +-7 files changed, 147 insertions(+), 95 deletions(-)
Quoted 8 lines
Quoted 3 lines
--
IanAny benchmark numbers that you ran that are better/worse with this patch
series? That woul dbe good to know, otherwise you aren't changing functionality here, so why would we take these changes? :)
Hi Greg,
I'm sorry, I don't have a benchmark.
My continued work on this has been driven by the report from Brice Goglin and Fox Chen, and also because I've seen a couple of other reports of kernfs_mutex contention that is resolved by the series.
Unfortunately the two reports I've seen fairly recently are on kernels that are about as far away from the upstream kernel as you can get so probably aren't useful in making my case.
The report I've worked on most recently is on CoreOS/Kunbernetes (based on RHEL-8.3) where the machine load goes to around 870 after loading what they call an OpenShift performance profile.
I looked at some sysreq dumps and they have a seemingly endless number of processes waiting on the kernfs_mutex.
I tried to look at the Kubernetes source but it's written in go so there would need to be a lot of time spent to work out what's going on, I'm trying to find someone to help with that.
All I can say from looking at the Kubernetes source is it has quite a few sysfs paths in it so I assume it uses sysfs fairly heavily.
The other problem I saw was also on CoreOS/Kunernetes. A vmcore analysis showed kernfs_mutex contention but with a different set of processes and not as significant as the former problem.
So, even though this isn't against the current upstream, there isn't much difference in the kernfs/sysfs source between those two kernels and given the results of Brice and Fox I fear I'll be seeing more of this as time goes by.
I'm fairly confident that the user space applications aren't optimal (although you may have stronger words for it than that) I was hoping you would agree that it's sensible for the kernel to protect itself to the extent that it can provided the change is straight forward enough.
I have tried to make the patches as simple as possible without loosing much of the improvement to minimize any potential ongoing maintenance burden.
So, I'm sorry I can't offer you more incentive to consider the series, but I remain hopeful you will.
If there is anything you would like me to follow up on please ask and, if I can, I will do what's requested.
Ian
Greg Kroah-Hartman
maintainerQuoted 139 lines · Ian Kent
Quoted 85 lines · Greg Kroah-Hartman
Quoted 84 lines · Ian Kent
There have been a few instances of contention on the
kernfs_mutexduring
path walks, a case on very large IBM systems seen by myself, a report by
Brice Goglin and followed up by Fox Chen, and I've since seen a couple
of other reports by CoreOS users.The common thread is a large number of kernfs path walks leading to slowness of path walks due to
kernfs_mutexcontention.The problem being that changes to the VFS over some time have increased
it's concurrency capabilities to an extent that kernfs's use of a mutex
is no longer appropriate. There's also an issue of walks for non- existent
paths causing contention if there are quite a few of them which is a less
common problem.This patch series is relatively straight forward.
All it does is add the ability to take advantage of VFS negative dentry
caching to avoid needless dentry alloc/free cycles for lookups of paths
that don't exit and change thekernfs_mutexto a read/write semaphore.The patch that tried to stay in VFS rcu-walk mode during path walks has
been dropped for two reasons. First, it doesn't actually give very much
improvement and, second, if there's a place where mistakes could go unnoticed it would be in that path. This makes the patch series simpler
to review and reduces the likelihood of problems going unnoticed and
popping up later.The patch to use a revision to identify if a directory has changed has
also been dropped. If the directory has changed the dentry revision needs to be updated to avoid subsequent rb tree searches and after changing to use a read/write semaphore the update also requires a lock.
But thed_lockis the only lock available at this point which might itself be contended.Changes since v3:
- remove unneeded indirection when referencing the super block.
- check if inode attribute update is actually needed.Changes since v2:
- actually fix the inode attribute update locking.
- drop the patch that tried to stay in rcu-walk mode.
- drop the use a revision to identify if a directory has changed patch.Changes since v1:
- fix locking in .permission() and .getattr() by re-factoring the attribute
handling code.
---Ian Kent (5):
kernfs: move revalidate to be near lookup
kernfs: use VFS negative dentry caching
kernfs: switch kernfs to use an rwsem
kernfs: usei_lockto protect concurrent inode updates
kernfs: addkernfs_need_inode_refresh()fs/kernfs/dir.c | 170 ++++++++++++++++++++------------
----
fs/kernfs/file.c | 4 +-
fs/kernfs/inode.c | 45 ++++++++--fs/kernfs/kernfs-internal.h| 5 +-
fs/kernfs/mount.c | 12 +-- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 2 +-7 files changed, 147 insertions(+), 95 deletions(-)Quoted 8 lines
Quoted 3 lines
--
IanAny benchmark numbers that you ran that are better/worse with this patch
series? That woul dbe good to know, otherwise you aren't changing functionality here, so why would we take these changes? :)Hi Greg,
I'm sorry, I don't have a benchmark.
My continued work on this has been driven by the report from Brice Goglin and Fox Chen, and also because I've seen a couple of other reports of
kernfs_mutexcontention that is resolved by the series.Unfortunately the two reports I've seen fairly recently are on kernels that are about as far away from the upstream kernel as you can get so probably aren't useful in making my case.
The report I've worked on most recently is on CoreOS/Kunbernetes (based on RHEL-8.3) where the machine load goes to around 870 after loading what they call an OpenShift performance profile.
I looked at some sysreq dumps and they have a seemingly endless number of processes waiting on the
kernfs_mutex.I tried to look at the Kubernetes source but it's written in go so there would need to be a lot of time spent to work out what's going on, I'm trying to find someone to help with that.
All I can say from looking at the Kubernetes source is it has quite a few sysfs paths in it so I assume it uses sysfs fairly heavily.
The other problem I saw was also on CoreOS/Kunernetes. A vmcore analysis showed
kernfs_mutexcontention but with a different set of processes and not as significant as the former problem.So, even though this isn't against the current upstream, there isn't much difference in the kernfs/sysfs source between those two kernels and given the results of Brice and Fox I fear I'll be seeing more of this as time goes by.
I'm fairly confident that the user space applications aren't optimal (although you may have stronger words for it than that) I was hoping you would agree that it's sensible for the kernel to protect itself to the extent that it can provided the change is straight forward enough.
Quoted 6 lines
I have tried to make the patches as simple as possible without loosing much of the improvement to minimize any potential ongoing maintenance burden.
So, I'm sorry I can't offer you more incentive to consider the series, but I remain hopeful you will.
At the very least, if you could test the series on those "older" systems and say "booting went from X seconds to Y seconds!".
Otherwise, while changes are nice, without a real-world test that this actually made any difference at all, why would we take these changes?
thanks,
greg k-h
Quoted 165 lines · Greg Kroah-Hartman
Quoted 164 lines · Ian Kent
Quoted 108 lines · Greg Kroah-Hartman
Quoted 101 lines · Ian Kent
There have been a few instances of contention on the
kernfs_mutex
during
path walks, a case on very large IBM systems seen by myself, a report by
Brice Goglin and followed up by Fox Chen, and I've since seen a couple
of other reports by CoreOS users.The common thread is a large number of kernfs path walks leading to
slowness of path walks due tokernfs_mutexcontention.The problem being that changes to the VFS over some time have increased
it's concurrency capabilities to an extent that kernfs's use of a
mutex
is no longer appropriate. There's also an issue of walks for non-
existent
paths causing contention if there are quite a few of them which is
a less
common problem.This patch series is relatively straight forward.
All it does is add the ability to take advantage of VFS negative
dentry
caching to avoid needless dentry alloc/free cycles for lookups of
paths
that don't exit and change thekernfs_mutexto a read/write semaphore.The patch that tried to stay in VFS rcu-walk mode during path walks
has
been dropped for two reasons. First, it doesn't actually give very
much
improvement and, second, if there's a place where mistakes could go
unnoticed it would be in that path. This makes the patch series simpler
to review and reduces the likelihood of problems going unnoticed
and
popping up later.The patch to use a revision to identify if a directory has changed
has
also been dropped. If the directory has changed the dentry revision
needs to be updated to avoid subsequent rb tree searches and after
changing to use a read/write semaphore the update also requires a
lock.
But thed_lockis the only lock available at this point which might
itself be contended.Changes since v3:
- remove unneeded indirection when referencing the super block.
- check if inode attribute update is actually needed.Changes since v2:
- actually fix the inode attribute update locking.
- drop the patch that tried to stay in rcu-walk mode.
- drop the use a revision to identify if a directory has changed
patch.Changes since v1:
- fix locking in .permission() and .getattr() by re-factoring the
attribute
handling code.
---Ian Kent (5):
kernfs: move revalidate to be near lookup
kernfs: use VFS negative dentry caching
kernfs: switch kernfs to use an rwsem
kernfs: usei_lockto protect concurrent inode updates
kernfs: addkernfs_need_inode_refresh()fs/kernfs/dir.c | 170 ++++++++++++++++++++--------
----
----
fs/kernfs/file.c | 4 +- fs/kernfs/inode.c | 45 ++++++++--fs/kernfs/kernfs-internal.h| 5 +- fs/kernfs/mount.c | 12 +-- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 2 +-Quoted 5 lines
7 files changed, 147 insertions(+), 95 deletions(-)--
IanQuoted 6 lines
Any benchmark numbers that you ran that are better/worse with this
patch
series? That woul dbe good to know, otherwise you aren't changing
functionality here, so why would we take these changes? :)Hi Greg,
I'm sorry, I don't have a benchmark.
My continued work on this has been driven by the report from Brice Goglin and Fox Chen, and also because I've seen a couple of other reports of
kernfs_mutexcontention that is resolved by the series.Unfortunately the two reports I've seen fairly recently are on kernels that are about as far away from the upstream kernel as you can get so probably aren't useful in making my case.
The report I've worked on most recently is on CoreOS/Kunbernetes (based on RHEL-8.3) where the machine load goes to around 870 after loading what they call an OpenShift performance profile.
I looked at some sysreq dumps and they have a seemingly endless number of processes waiting on the
kernfs_mutex.I tried to look at the Kubernetes source but it's written in go so there would need to be a lot of time spent to work out what's going on, I'm trying to find someone to help with that.
All I can say from looking at the Kubernetes source is it has quite a few sysfs paths in it so I assume it uses sysfs fairly heavily.
The other problem I saw was also on CoreOS/Kunernetes. A vmcore analysis showed
kernfs_mutexcontention but with a different set of processes and not as significant as the former problem.So, even though this isn't against the current upstream, there isn't much difference in the kernfs/sysfs source between those two kernels and given the results of Brice and Fox I fear I'll be seeing more of this as time goes by.
I'm fairly confident that the user space applications aren't optimal (although you may have stronger words for it than that) I was hoping you would agree that it's sensible for the kernel to protect itself to the extent that it can provided the change is straight forward enough.
I have tried to make the patches as simple as possible without loosing much of the improvement to minimize any potential ongoing maintenance burden.
Quoted 6 lines
Quoted 2 lines
So, I'm sorry I can't offer you more incentive to consider the series, but I remain hopeful you will.
At the very least, if you could test the series on those "older" systems
and say "booting went from X seconds to Y seconds!".
The last test I did was done on the system showing high load and it went from around 870 to around 3. It completely resolved the reported problem.
I need to have the current patches re-tested and that can take a while and I need to look at Fox's results results, I'm thinking the additional patch in v4 is probably not needed.
Ian