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=1796418663; b=eODyvGNo4YzrSrhwhf7shjdgkUdMFdTuPbzDw4l7DPd12Ec4lYNcvrhZYxQRsd8upLvC hu1LH+2Bl2/Emu2bB+DTLPASuUUEfKsEKNrWQaSwKSe5r7xnC25V2lH5ZkMX1kpjwFv+p gFl8sD80sLWJpzAwgWbz8gt40EPZn2bUwv8hmXMrptrg7ngT0VvcimHxPlGCdbDRKQ1Fe tS9nyRt0QTHogGwR1YmpaAuif6Q/RGdFeg45TGmzBftYncBRnzcR7tvMMey9zJMsGcR6K XpKqKGld7+X05+o3V4XTiVPewMoKGQlpaLREb8Z5alnblYMr+b/4mYt63XY1Z3EZOqg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796418663; 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=QQfqK3BJvHBCwnMuk+CJAmkDLnfYMyiCO/On/AnD3HgQnlgxZiGZW2JHZ010qyWfI88k QLypQzTC4yIsXVOP348SwJfmqaj/YXhv8D0Rx0Uwk8Zt3/2LofL+T7ubMoq/YQYoKPWdf mST9kPGVevcdtvTgeVvSnm/pf6ifheSTyje4GSphSOHjyKdz5T0ecOsjHzaSUsaclzu4G j+Kn2bxiX5TCeei+I1HCFnPwLR0zriashqM7ld4cSSZY4Kt0UinyVUiMMoQNTMY5eva0c H7fXoQ7kJ2oOPzRcTY9esBlYyhTqs2u8VxDSBM23MlAHA1n+Wu2MFATLMrvI9E7U/6w== 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=Yti4eoxm; 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 EB865606A32D for ; Fri, 4 Dec 2026 21:11:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EB865606A32D 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=Yti4eoxm X-Envelope-To: cgroups@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=1796418660; v=1; x=1797023460; b=Yti4eoxmoPC4QvNmOUjuMyaxK48Z/zqAMzfI4v0snUiVdJyCbAi2ZLMee8FUrpy6Nss4wU4S E8YF/DOrWb2bcPDGLjYTUkz/TX/CAyTFiT/NjlGtTl1B9FytPkiPqnxkRWFfUrujbijFY09wEIv tHXBRrCM68Gx39fUDugQK1HA= X-Envelope-To: cgroups@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: cgroups@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