From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.58 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796418664; b=vAuZlRmpCCzO8gfLKZ2udnqC8f0vjEkT3nLyNv5diVBrCadJMrhmTHqco979uAD7ri27 yYkK8QtFKPoExPDHHK8zZw10UMnokz/g/7BKWTJ3S3+yyT8bbQSU7DJ+SdTypo6cXa514 N/sDBFbQl8Yn80Z6GJ7FilX5DajQqTIqOmYt12gTlUpvYSEoUEhPh9ncYPG86fuyoKBjP p13iQvRA6cOtl3MRBphy0g8VvWGEUQclKRRgOAReXSAH86LyTx+KjB0W8AC/aT5lkz1y+ DfIxUSwJ8gOPKyrg0M8PSyx9ST0DR2hK2ybe/wXJ0LWwGzcyUQKGYuxD8MvZAjkcVpw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796418664; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=nSPZOCIP+xA63jcJizIrw4ZzxHvXRsdxKhzm+RBbG+A=; b=1NQGGFTDwXPYj4JGRWCVRLdyFUiAo+OaNTbhlRaY8qZh5X6JFF+uKbcR7kAvPQrgl/WB th1xkDGYGSBvsbxkxzl3ygGjKrYCxDv0pB1ykn9Mi+uO21tGHwNNLY+MyqTjX/VFfaM7y PKmmtxDVmZMys5qDrSnKErVGYrMawnCtTU37VykjyHQaIt89Y4x7w0t1jatYfVqQo/4CJ kYyqyMjzat0wCf5/T4rWwH91Cf9IZmHwUYcCeEy9C9i7EBDqDWgN+EI46FP9kNKrHPZQr ewIU/X4JRrR2nan+axndI9ASdtcqHFlMuYMBeNwHKEITFEmX8ZZFN6mjAx8EzZ7g5+Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=u1WBc+bC; arc=none smtp.remote-ip=91.218.175.58 Received: from mta0.migadu.com (out-58.mta0.migadu.com [91.218.175.58]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB7B2606A32C for ; Fri, 4 Dec 2026 21:11:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EB7B2606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=u1WBc+bC X-Envelope-To: linux-fsdevel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=DpgA0N7+POIzPl8/ZnBWnaXytV8111zbW8QsYhDJ+Lk=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796418661; v=1; x=1797023461; b=u1WBc+bCYpQJ/SWAu/3cBw3eDTJPEt9T6dQTwu7WlXRP7EBAZ7iqgO0bfUZaXpVo90KyyH42 5d6+9MxirJNtIRJ2gKz+Dfgocq1zzm4okjGuziAjvazzNSTy+H0DOSY5/fj/Yg7kgmXtcHeX/ul RT53r+LC6kxymWjOl4l0ao2o= X-Envelope-To: linux-fsdevel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 0d6fe80d27854596; Fri, 04 Dec 2026 21:10:58 +0000 X-Mizu-Trace-ID: 0d6fe80d27854596 X-Migadu-Flow: FLOW_OUT From: Shakeel Butt To: Christian Brauner Cc: Greg Kroah-Hartman , Tejun Heo , Andrew Morton , Ian Kent , Jori Koolstra , "T . J . Mercier" , Meta kernel team , cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org, driver-core@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 2/3] kernfs: add a sequence count to directories Date: Fri, 4 Dec 2026 13:10:31 -0800 Message-ID: <20261204211034.3436390-3-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261204211034.3436390-1-shakeel.butt@linux.dev> References: <20261204211034.3436390-1-shakeel.butt@linux.dev> X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To let readers walk a directory's children without kernfs_rwsem, give each directory a sequence count that is bumped around every change to its children rbtree. kernfs_rwsem, held for writing, already serializes the writers. The write section disables preemption, so a reader never spins on a preempted writer. New children are published with rb_link_node_rcu(), and the rbtree's own stores are WRITE_ONCE(), so a reader walking down the tree under RCU always ends on a valid node, and the sequence count tells it when to retry. Rename writes kn->hash with WRITE_ONCE(), and kernfs_name_compare() reads it and kn->ns with READ_ONCE(). subdirs becomes an unsigned int, so that without lockdep the new field does not make kernfs_node bigger. kernfs.h only needs the type, from seqlock_types.h. No functional change. Signed-off-by: Shakeel Butt --- fs/kernfs/dir.c | 22 ++++++++++++++-------- fs/kernfs/kernfs-internal.h | 27 +++++++++++++++++++++++++++ include/linux/kernfs.h | 9 ++++++++- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 4cb505242acb..9acefb8713fd 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -348,12 +348,14 @@ static unsigned int kernfs_name_hash(const char *name, static int kernfs_name_compare(unsigned int hash, const char *name, const struct ns_common *ns, const struct kernfs_node *kn) { + /* readers without kernfs_rwsem can race with a rename */ + unsigned int kn_hash = READ_ONCE(kn->hash); u64 ns_id = kernfs_ns_id(ns); - u64 kn_ns_id = kernfs_ns_id(kn->ns); + u64 kn_ns_id = kernfs_ns_id(READ_ONCE(kn->ns)); - if (hash < kn->hash) + if (hash < kn_hash) return -1; - if (hash > kn->hash) + if (hash > kn_hash) return 1; if (ns_id < kn_ns_id) return -1; @@ -408,13 +410,15 @@ static int kernfs_link_sibling(struct kernfs_node *kn) } /* add new node and rebalance the tree */ - rb_link_node(&kn->rb, parent, node); + kernfs_dir_write_begin(kn_parent); + rb_link_node_rcu(&kn->rb, parent, node); rb_insert_color(&kn->rb, &kn_parent->dir.children); /* successfully added, account subdir number */ if (kernfs_type(kn) == KERNFS_DIR) WRITE_ONCE(kn_parent->dir.subdirs, kn_parent->dir.subdirs + 1); kernfs_inc_rev(kn_parent); + kernfs_dir_write_end(kn_parent); return 0; } @@ -442,12 +446,14 @@ static bool kernfs_unlink_sibling(struct kernfs_node *kn) return false; kn_parent = kernfs_parent(kn); + kernfs_dir_write_begin(kn_parent); if (kernfs_type(kn) == KERNFS_DIR) WRITE_ONCE(kn_parent->dir.subdirs, kn_parent->dir.subdirs - 1); kernfs_inc_rev(kn_parent); rb_erase(&kn->rb, &kn_parent->dir.children); RB_CLEAR_NODE(&kn->rb); + kernfs_dir_write_end(kn_parent); return true; } @@ -1056,7 +1062,7 @@ struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops, } kn->priv = priv; - kn->dir.root = root; + kernfs_init_dir(kn, root); root->syscall_ops = scops; root->flags = flags; @@ -1125,7 +1131,7 @@ struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent, if (!kn) return ERR_PTR(-ENOMEM); - kn->dir.root = parent->dir.root; + kernfs_init_dir(kn, parent->dir.root); kn->ns = ns; kn->priv = priv; @@ -1158,7 +1164,7 @@ struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent, return ERR_PTR(-ENOMEM); kn->flags |= KERNFS_EMPTY_DIR; - kn->dir.root = parent->dir.root; + kernfs_init_dir(kn, parent->dir.root); kn->ns = NULL; kn->priv = NULL; @@ -1936,7 +1942,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, if (reparent) kernfs_put(old_parent); - kn->hash = kernfs_name_hash(new_name ?: old_name, kn->ns); + WRITE_ONCE(kn->hash, kernfs_name_hash(new_name ?: old_name, kn->ns)); kernfs_link_sibling(kn); if (new_name && !is_kernel_rodata((unsigned long)old_name)) diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h index 9bba1fc8acfd..00e57f3c1397 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -17,6 +17,7 @@ #include #include +#include #include struct kernfs_iattrs { @@ -170,6 +171,32 @@ static inline bool kernfs_dir_changed(struct kernfs_node *parent, return READ_ONCE(parent->dir.rev) != READ_ONCE(dentry->d_time); } +static inline void kernfs_init_dir(struct kernfs_node *kn, + struct kernfs_root *root) +{ + kn->dir.root = root; + seqcount_init(&kn->dir.seq); +} + +/* + * Bracket a change to @dir's children, so that readers walking them without + * kernfs_rwsem retry. kernfs_rwsem serializes the writers. The section + * must not be preempted, or those readers would spin until the writer runs + * again. + */ +static inline void kernfs_dir_write_begin(struct kernfs_node *dir) +{ + lockdep_assert_held_write(&dir->dir.root->kernfs_rwsem); + preempt_disable(); + write_seqcount_begin(&dir->dir.seq); +} + +static inline void kernfs_dir_write_end(struct kernfs_node *dir) +{ + write_seqcount_end(&dir->dir.seq); + preempt_enable(); +} + extern const struct super_operations kernfs_sops; extern struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache; diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index 803294a67e47..5d0c1122cb8d 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -18,6 +18,7 @@ #include #include #include +#include #include struct file; @@ -160,7 +161,13 @@ enum kernfs_root_flag { /* type-specific structures for kernfs_node union members */ struct kernfs_elem_dir { - unsigned long subdirs; + unsigned int subdirs; + /* + * Changes to @children are made with kernfs_rwsem held for writing + * and inside a write section of @seq, so that readers without the + * lock can walk @children under RCU and retry if it changed. + */ + seqcount_t seq; /* children rbtree starts here and goes through kn->rb */ struct rb_root children; -- 2.53.1-Meta