Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp905115pxb; Tue, 8 Feb 2022 05:16:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJwxy63eNnhCV4bnkDt5EAG3+SV9cUg+6xTxLRUIZ/epy7f0TD1MqLAixBssYIrb79euA2Bu X-Received: by 2002:a17:902:ef4e:: with SMTP id e14mr4491654plx.125.1644326204960; Tue, 08 Feb 2022 05:16:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644326204; cv=none; d=google.com; s=arc-20160816; b=tFsqpi655Otv/FmuXQVGCUM4po5rQrXxe2qngJJYTaUOQi2qp6baswiM0yN9KBEIY/ fsH9xDOuinDQb46nqGqk4i4ROoChVNcA3Lw5JZJ9kOquEWhIAHP0R4+RlsrVY9VCuFM3 K34DLP/wKlsZ+GOAaWvt/dLCztxM9PX3rcPFKyetKRN0YMiqve1z8XYSQWmEZvGGgwWX zGlJHd9YKaA2WZxE432nROnG/qO8ATi5nujkYAHw56Y0UI08zBWBrDTNOiAWnYgB/Y67 fVV118SoA42YdT48D41YE9ryFgnWLhHkjidIpcm6JN2B/vWOKOWFCQqQWGXN1LaSCEqX 0iUA== 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:dkim-signature; bh=6MZEcl1w3KLgigk+IYAntCTa8ZvLVIXY7fg2XsBYzLo=; b=YsUNOETgYnNSTxwZGTmciRNIujdyoD7lK8XCx5vOQSfvRQw0qjikjG/19fJF7pLDaB zhs6iYIQGqf2mVkJyFVlU8wg55MM8IgjzfRrblxet1UQBHTIiMtFnhWtMFKejf6L8Y2u +4zscssVhgnIMCz14oZjlhT2+QCte5j0cu2HWciKG+kVmWRtOgXOboG5Uz917x3sOwZT RqPJ2TuGRGEwjwu8bo189z+5f39yMbGaD4GqRL4zCMVga9+sM8nfIMdCk6D/gvPVToqz uLPwODfTe4pbKFAAVzebUjYunEBkA5M9z0k44LZCq1+A3wDFL+KSbBQUraeDgGjRtd1f Q7xA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=kjpJt2Os; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h190si2396261pge.518.2022.02.08.05.16.30; Tue, 08 Feb 2022 05:16:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=kjpJt2Os; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359409AbiBHLgz (ORCPT + 99 others); Tue, 8 Feb 2022 06:36:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358831AbiBHL1l (ORCPT ); Tue, 8 Feb 2022 06:27:41 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17471C03FEEA for ; Tue, 8 Feb 2022 03:27:29 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A293D615C2 for ; Tue, 8 Feb 2022 11:27:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5DF72C004E1; Tue, 8 Feb 2022 11:27:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1644319648; bh=CGQqY63qbTyT2OHCC+vjreP6q7CXq083TbLUBIE3Ys4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=kjpJt2OsYIINrFQNdlgDkPgBgtcrP3piCf+lio35QnYefp7hORiCgi4zQcWWj/gv2 ENk345IS7+ZFd3tOaLrMM1rsBluQQ7xWW1vnEoQVK5IdnkX+LmJvd7/8vFz6NHyAVL DHWYZaci5OnbDbzidTk3ITZfAk09yhrvL2XYfmoA= Date: Tue, 8 Feb 2022 12:27:25 +0100 From: Greg KH To: Imran Khan Cc: tj@kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v5 1/2] kernfs: use hashed mutex and spinlock in place of global ones. Message-ID: References: <20220206010925.1033990-1-imran.f.khan@oracle.com> <20220206010925.1033990-2-imran.f.khan@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220206010925.1033990-2-imran.f.khan@oracle.com> X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 On Sun, Feb 06, 2022 at 12:09:24PM +1100, Imran Khan wrote: > diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h > index f9cc912c31e1b..cc49a6cd94154 100644 > --- a/fs/kernfs/kernfs-internal.h > +++ b/fs/kernfs/kernfs-internal.h > @@ -31,6 +31,7 @@ struct kernfs_iattrs { > atomic_t user_xattr_size; > }; > > + > /* +1 to avoid triggering overflow warning when negating it */ > #define KN_DEACTIVATED_BIAS (INT_MIN + 1) > Nit, the above change isn't needed :) > @@ -147,4 +148,54 @@ void kernfs_drain_open_files(struct kernfs_node *kn); > */ > extern const struct inode_operations kernfs_symlink_iops; > > +static inline spinlock_t *kernfs_open_node_lock_ptr(struct kernfs_node *kn) > +{ > + struct kernfs_root *root; > + int idx = hash_ptr(kn, NR_KERNFS_LOCK_BITS); > + > + root = kernfs_root(kn); > + > + return &root->open_node_locks[idx].lock; > +} > + > +static inline spinlock_t *kernfs_open_node_lock(struct kernfs_node *kn) > +{ > + struct kernfs_root *root; > + spinlock_t *lock; > + int idx = hash_ptr(kn, NR_KERNFS_LOCK_BITS); > + > + root = kernfs_root(kn); > + > + lock = &root->open_node_locks[idx].lock; > + > + spin_lock_irq(lock); > + > + return lock; > +} Can't you use kernfs_open_node_lock_ptr() in kernfs_open_node_lock() to make this use less duplicated code? > + > +static inline struct mutex *kernfs_open_file_mutex_ptr(struct kernfs_node *kn) > +{ > + struct kernfs_root *root; > + int idx = hash_ptr(kn, NR_KERNFS_LOCK_BITS); > + > + root = kernfs_root(kn); > + > + return &root->open_file_mutex[idx].lock; > +} > + > +static inline struct mutex *kernfs_open_file_mutex_lock(struct kernfs_node *kn) > +{ > + struct kernfs_root *root; > + struct mutex *lock; > + int idx = hash_ptr(kn, NR_KERNFS_LOCK_BITS); > + > + root = kernfs_root(kn); > + > + lock = &root->open_file_mutex[idx].lock; > + > + mutex_lock(lock); > + > + return lock; > +} Same thing here. > + > #endif /* __KERNFS_INTERNAL_H */ > diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h > index 861c4f0f8a29f..5bf9f02ce9dce 100644 > --- a/include/linux/kernfs.h > +++ b/include/linux/kernfs.h > @@ -18,6 +18,8 @@ > #include > #include > #include > +#include > +#include > > struct file; > struct dentry; > @@ -34,6 +36,40 @@ struct kernfs_fs_context; > struct kernfs_open_node; > struct kernfs_iattrs; > > +/* > + * NR_KERNFS_LOCK_BITS determines size (NR_KERNFS_LOCKS) of hash > + * table of locks. > + * Having a small hash table would impact scalability, since > + * more and more kernfs_node objects will end up using same lock > + * and having a very large hash table would waste memory. > + * > + * At the moment size of hash table of locks is being set based on > + * the number of CPUs as follows: > + * > + * NR_CPU NR_KERNFS_LOCK_BITS NR_KERNFS_LOCKS > + * 1 1 2 > + * 2-3 2 4 > + * 4-7 4 16 > + * 8-15 6 64 > + * 16-31 8 256 > + * 32 and more 10 1024 > + */ > +#ifdef CONFIG_SMP > +#define NR_KERNFS_LOCK_BITS (2 * (ilog2(NR_CPUS < 32 ? NR_CPUS : 32))) > +#else > +#define NR_KERNFS_LOCK_BITS 1 > +#endif > + > +#define NR_KERNFS_LOCKS (1 << NR_KERNFS_LOCK_BITS) > + > +struct kernfs_open_node_lock { > + spinlock_t lock; > +} ____cacheline_aligned_in_smp; > + > +struct kernfs_open_file_mutex { > + struct mutex lock; > +} ____cacheline_aligned_in_smp; > + > enum kernfs_node_type { > KERNFS_DIR = 0x0001, > KERNFS_FILE = 0x0002, > @@ -90,6 +126,7 @@ enum kernfs_root_flag { > KERNFS_ROOT_SUPPORT_USER_XATTR = 0x0008, > }; > > + > /* type-specific structures for kernfs_node union members */ > struct kernfs_elem_dir { > unsigned long subdirs; > @@ -201,6 +238,8 @@ struct kernfs_root { > > wait_queue_head_t deactivate_waitq; > struct rw_semaphore kernfs_rwsem; > + struct kernfs_open_node_lock open_node_locks[NR_KERNFS_LOCKS]; > + struct kernfs_open_file_mutex open_file_mutex[NR_KERNFS_LOCKS]; > }; I think struct kernfs_root can be declared locally inside fs/kernfs/ to keep other subsystems/files from having to see this structure, right? That would make this a bit less of a "rebuild the world" type of change and could be done in a patch before this one. Overall, this looks sane to me, nice work. Tejun, any comments? thanks, greg k-h