From: Tejun Heo <tj@kernel.org>
To: Imran Khan <imran.f.khan@oracle.com>
Cc: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5 2/2] kernfs: Replace per-fs global rwsem with per-fs hashed rwsem.
Date: Tue, 8 Feb 2022 08:26:40 -1000 [thread overview]
Message-ID: <YgK14ElTBW1BOXxW@slm.duckdns.org> (raw)
In-Reply-To: <20220206010925.1033990-3-imran.f.khan@oracle.com>
Hello,
On Sun, Feb 06, 2022 at 12:09:25PM +1100, Imran Khan wrote:
> Having a single rwsem to synchronize all operations across a kernfs
> based file system (cgroup, sysfs etc.) does not scale well. Replace
> it with a hashed rwsem to reduce contention around single per-fs
> rwsem.
> Also introduce a perfs rwsem to protect per-fs list of kernfs_super_info.
Can you split the two conversions into separate patches? Also, I'm not sure
about the per-fs hashtable as before.
> static bool kernfs_active(struct kernfs_node *kn)
> {
> - lockdep_assert_held(&kernfs_root(kn)->kernfs_rwsem);
> + int idx = hash_ptr(kn, NR_KERNFS_LOCK_BITS);
> +
> + lockdep_assert_held(&kernfs_root(kn)->kernfs_rwsem[idx]);
Please encapsulate this into a function. If possible, it'd be ideal if the
conversion can be done in steps - e.g. first introduce lock encapsulation
interface while leaving locking itself alone and then switch the actual
locking.
> -static void kernfs_drain(struct kernfs_node *kn)
> - __releases(&kernfs_root(kn)->kernfs_rwsem)
> - __acquires(&kernfs_root(kn)->kernfs_rwsem)
> +static void kernfs_drain(struct kernfs_node *kn, struct kernfs_node *anc)
> + __releases(&kernfs_root(anc)->kernfs_rwsem[a_idx])
> + __acquires(&kernfs_root(anc)->kernfs_rwsem[a_idx])
> {
> struct kernfs_root *root = kernfs_root(kn);
> + int a_idx = hash_ptr(anc, NR_KERNFS_LOCK_BITS);
>
> - lockdep_assert_held_write(&root->kernfs_rwsem);
> - WARN_ON_ONCE(kernfs_active(kn));
> + lockdep_assert_held_write(&root->kernfs_rwsem[a_idx]);
> + WARN_ON_ONCE(atomic_read(&kn->active) >= 0);
Ditto, I'd much prefer to see the lock lookup and accompanying operations to
be encapsulated somehow.
> @@ -1588,37 +1597,65 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
> const char *new_name, const void *new_ns)
> {
> struct kernfs_node *old_parent;
> - struct kernfs_root *root;
> const char *old_name = NULL;
> - int error;
> + int error, idx, np_idx, p_idx;
>
> /* can't move or rename root */
> if (!kn->parent)
> return -EINVAL;
>
> - root = kernfs_root(kn);
> - down_write(&root->kernfs_rwsem);
> + /*
> + * Take lock of node's old (current) parent.
> + * If new parent has a different lock, then take that
> + * lock as well.
> + */
> + idx = hash_ptr(kn, NR_KERNFS_LOCK_BITS);
> + p_idx = hash_ptr(kn->parent, NR_KERNFS_LOCK_BITS);
> + np_idx = hash_ptr(new_parent, NR_KERNFS_LOCK_BITS);
> +
> + /*
> + * Take only kn's lock. The subsequent kernfs_put
> + * may free up old_parent so if old_parent has a
> + * different lock, we will explicitly release that.
> + */
> + down_write_kernfs_rwsem(kn, LOCK_SELF, 0);
> +
> + if (idx != np_idx) /* new parent hashes to different lock */
> + down_write_kernfs_rwsem(new_parent, LOCK_SELF, 1);
> +
> + /* old_parent hashes to a different lock */
> + if (idx != p_idx && p_idx != np_idx)
> + down_write_kernfs_rwsem(kn->parent, LOCK_SELF, 2);
Can't this lead to ABBA deadlock? When double locking, the locking order
should always be consistent. If we were doing per-kernfs_node lock, child ->
parent ordering works but we're hashing locks, so that doesn't work anymore
- one child-parent combo can lock A then B while the other child-parent
combo hash the other way around and lock B then A. The only order we can
define is in terms of the locks themselves - e.g. if the address (or index)
of lock A < lock B, then we lock A first whether that maps to the child or
parent.
Also, please encapsulate double locking in a set of functions. We really
don't wanna see all the details in the users.
> --- a/fs/kernfs/kernfs-internal.h
> +++ b/fs/kernfs/kernfs-internal.h
> @@ -19,6 +19,9 @@
> #include <linux/kernfs.h>
> #include <linux/fs_context.h>
>
> +#define LOCK_SELF 0
> +#define LOCK_SELF_AND_PARENT 1
I get that this is private header but can you please add some identifying
prefix and make them enums? That makes it way easier for debuggers, bpf and
tracing.
> +/*
> + * If both node and it's parent need locking,
> + * lock child first so that kernfs_rename_ns
> + * does not change the parent, leaving us
> + * with old parent here.
> + */
Please reflow it close to 80 chars and ditto with above. We can't follow
child -> parent order with hashed locks.
Thanks.
--
tejun
next prev parent reply other threads:[~2022-02-08 18:26 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-06 1:09 [PATCH v5 0/2] kernfs: use hashed mutex and spinlock in place of global ones Imran Khan
2022-02-06 1:09 ` [PATCH v5 1/2] " Imran Khan
2022-02-08 11:27 ` Greg KH
2022-02-14 12:13 ` Imran Khan
2022-02-08 17:22 ` Tejun Heo
2022-02-14 12:19 ` Imran Khan
2022-02-14 17:39 ` Tejun Heo
2022-02-06 1:09 ` [PATCH v5 2/2] kernfs: Replace per-fs global rwsem with per-fs hashed rwsem Imran Khan
2022-02-08 18:26 ` Tejun Heo [this message]
2022-02-14 12:27 ` Imran Khan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YgK14ElTBW1BOXxW@slm.duckdns.org \
--to=tj@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=imran.f.khan@oracle.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®