Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1602465pxb; Tue, 8 Feb 2022 23:45:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJzdfxxW1We70W65tfDUhjnFqpWx1RNrQBD0yWarOCHzmqnN/oGoZegBv0XSOtPOHzWjjf0Q X-Received: by 2002:a17:906:ce3b:: with SMTP id sd27mr827341ejb.720.1644392755827; Tue, 08 Feb 2022 23:45:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644392755; cv=none; d=google.com; s=arc-20160816; b=FBCdASyFkw2CSVI8nm3W/pEMJVH7VX2+tEkxlI281vVBt0g/tirYSBE0rgiNrpF1W5 uBgofJvLL2re6Ah+Tnwyak14TkOtNLfHtl5bl9LyitZa6EX3CsOCy4yqf8bJ8O2ZiGFT xlkf7JJAWSEHRJRyW3ojFDF30hJ+ld0MQpQdXeGwSQz1ahezhP1zDHhcVdfTP0SxDnWB XQeClstOzAZvg6+acRxmNvmPUBi/denUCAJEC2ymuGwTyxzRATk3k9VCdNPjo0N9BzhW 5x2eDbgOBjBBPv0rFjBxHuuD9QSU86rX0BkEBYSZX9K6MfhyqiQP7Orhof9HiRs179hP 7k9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:sender:dkim-signature; bh=Oedbq8uCfgl/Lmier/kIS95BKeP6ZEkA8SDWcSTE1FI=; b=gE5CXhI2XmmsjEuqijRwdVJCdiHxdIFD30ogJHxVRkxH7DBIsqLU3mPGqShT+phhFb jCF1QY8Hk1eUxvjUfSRv79+uHtEFvaYC34mSi7QZb0BxkZI6Gw57uvc8ihYSHXKmm5EG +/1bNajmV19107dxJk89JkfNDUMz8K3jbuvPWrIPvJ5ipmr9WaaeOuAx3O+fS+xjo8ri dugCoQRrzWBXXtLQRbhuv0/YfNsM28vF6NpVaSHefsOhzjBcJF1akDDa4k9jrQ7qJ0ne sIKKdlp1cU1W80h7gqNPzSdwx6Yd3Q253j1p/ZY8bVFMTEQP+fN4jWUR4yd3XKamAzil eszw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=grDnjNcZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ds7si12536095ejc.603.2022.02.08.23.45.30; Tue, 08 Feb 2022 23:45:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=grDnjNcZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1384917AbiBHS0o (ORCPT + 99 others); Tue, 8 Feb 2022 13:26:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355630AbiBHS0n (ORCPT ); Tue, 8 Feb 2022 13:26:43 -0500 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8F82C061578 for ; Tue, 8 Feb 2022 10:26:42 -0800 (PST) Received: by mail-pf1-x42d.google.com with SMTP id c10so1472111pfi.9 for ; Tue, 08 Feb 2022 10:26:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=Oedbq8uCfgl/Lmier/kIS95BKeP6ZEkA8SDWcSTE1FI=; b=grDnjNcZkEEhpCEfjigfvL1FDIQrRjnM1TMnJGjPMzCpftYc4E1x+pbMOfDIhFJs0W bRclRAm+tx4bYGZx6b9T4LTCqadYtwRgzynyiIwx47ykyqynQiyCOuUp7Pd2+kq0C30O oaaGvabktvOgzD9Ab5uG+GoZURiTqdIesGgEbZ9XHjWvQPv+cUwEIauZB3M1FxQFovGB TpTjgwvdk1C5j2g6SjO+Pw5sEYdkaMYzyG8iGOncU870sQLfAVp78jezRhYl2bCkq4e6 +OUgN4QSbHJwRNDcxDrA4HGYRjfUe8jUZ4J1WbfJdeyKs+03biUSPg5R9xTgY+WS6F9p Q3KQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to; bh=Oedbq8uCfgl/Lmier/kIS95BKeP6ZEkA8SDWcSTE1FI=; b=wZsCSkZaF03x/2aKSUseqcUi9R1MKk0OvrYoazfLB0rPgAfIPVukTEYahPD/5kwp5z zZgMFmNmRJqJXbPcV6dtRJut6D06AHG8b4y+lQYRkpcCq14jMcPnPcvnr5Cn4G0HkT6M Jzor76Gk7Eq/DdsP5Pmb/1nl4hlP4MdwDBI5otkZl5i2Rma+Fs1fY8NwKC6wvIzIXdir u709EPqAxTHcOdEVCzU3WSjiTzV46EIZe79d9kvstb0Y0IukiGr9HAsl9dl0KGUyX1mL PGiEo1DQgllftUGB6Y8mKB0o1CFn6rUwdgjNkbHAUQK12OXMFZbJKzAocPd2y8LyFcsd 0Rfw== X-Gm-Message-State: AOAM532cH69wQ0LqnSN+8ubQX0Y/MZnsUPBaDPOUZkuLzzbauff95JrR Fp26jgLS/uVOc7hFvHKo34k= X-Received: by 2002:a63:690a:: with SMTP id e10mr4516261pgc.599.1644344802118; Tue, 08 Feb 2022 10:26:42 -0800 (PST) Received: from localhost (2603-800c-1a02-1bae-e24f-43ff-fee6-449f.res6.spectrum.com. [2603:800c:1a02:1bae:e24f:43ff:fee6:449f]) by smtp.gmail.com with ESMTPSA id k14sm16950038pff.25.2022.02.08.10.26.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Feb 2022 10:26:41 -0800 (PST) Sender: Tejun Heo Date: Tue, 8 Feb 2022 08:26:40 -1000 From: Tejun Heo To: Imran Khan 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. Message-ID: References: <20220206010925.1033990-1-imran.f.khan@oracle.com> <20220206010925.1033990-3-imran.f.khan@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220206010925.1033990-3-imran.f.khan@oracle.com> X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > #include > > +#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