From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796387529; b=sMDcykyU+pE09o1+lhfsVUkOt7E5qrqdoWSlONNjzlIw6YZ/4EIQGN7Qy6pBkhCjcSGE Yc0wr6hCw36De7dEsWPOIGkUEKjytZTeZlVoItjGA5telJP6VtooAYaWwCQ+AeAs2fMH6 UYBT5zHDx9YnJFQmvI/BKm3AKfd2Zw1tH3CuC2C+7P2wfhjShZsOEAZcpyYJRcLFPgfCp BgyNjX0wMiMIqd96gN27i9FJTEySJ04Y9xJzFmPg8RACPskO4tIbknWjHT4dgqH3xcpPI o4VP87TP2k2wuPWwlZ02P3D5YD+wSXigeh/B/Hff/b7WU4Urm5ruDWZHPsC+/RyMPgQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387529; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=23mBxmMmqrQbohfKhkpnAys3ufYeafSq29VWv3Gc7dc=; b=MAVSByzNxcTcbGid3eLz9cwAu5DvPQohk/gKV5kzOMSnXzUL81fdBUaYfMiSm34YLUPS hTkz3TEUwWOZEKd8DxFHdpZf+/Bk9MS5poSh7YDrU67KmQi1ctYcqHeyK+juIjHx6yb3q n2HWbWiIsfTCCfeSkfW7VG+0g711X0sDvTdS2sB5+y8KQmWabghl7h3e9F72CIT1mj/YA 1Ed8Jv1/YtVhRhjhp+DqL4nlklmL9Z2+dOz6oAa7cVi1dS4+W53NgS7q3Va8qxlX+EyJK fyxTVGw6g03Zr/IlD6rPvgTJ+xKZwKzLLVjlKX9LjOZMg/Rm+VkTa4lk6XqaTZ2ipyg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=A8peTxQX; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB900606A32C; Fri, 4 Dec 2026 12:32:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=A8peTxQX Received: by smtp.kernel.org (Postfix) with ESMTPSA id 249F21F00897; Fri, 4 Dec 2026 12:32:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387529; bh=23mBxmMmqrQbohfKhkpnAys3ufYeafSq29VWv3Gc7dc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=A8peTxQXxAPj/tu3FzDL25IkHI1ytJyqRkUAaJF8AkfPm+PyJsX7Bdl29V6UjoXdG irLqN/4yoPVl48JrdBKXTMZGM1ukzBp69Yj860NWhnZWGeJIDkmPF/TZW0fQl0Vr0z SH7svq0bFzqqA8Ng+BfUuGZrpFJzuxrCu4OIXez8= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Tejun Heo , Shakeel Butt , "Christian Brauner (Amutable)" , Sasha Levin Subject: [PATCH 6.1 0284/1059] kernfs: dont repeat or skip an entry when readdir resumes Date: Fri, 4 Dec 2026 18:56:55 +0900 Message-ID: <20261204100651.629943075@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Shakeel Butt [ Upstream commit 5ebcb578adeec7407dd501108ab2f423ca8e3e55 ] readdir keeps its place as a name hash in ctx->pos and pins the entry in file->private_data. If that entry is gone when the listing comes back, kernfs_dir_pos() searches the rbtree for the hash and keeps whatever node the descent stopped on. That is the entry before or after the missing one, depending on the shape of the tree. Landing before it repeats an entry the previous getdents(2) call already reported. Landing after it, kernfs_dir_next_pos() calls rb_next() and steps over an unreported entry. With children A(10), B(20), C(30): report A, ctx->pos = 10 A removed kernfs_dir_next_pos(10, A) A is gone, the search for 10 stops at B rb_next(B) -> C, so B is never reported Only the repeat happens today, between two getdents(2) calls. The skip needs the pinned entry to go away inside one call, which the next patch allows when it drops kernfs_rwsem around dir_emit(). Before the commit 4e4d6d860b93 the descent kept a node only on the way left, which is a search for the first entry at or after the hash. That commit moved the assignment to the top of the loop, where it runs on right turns too. Restore that search, and step forward only when the pinned entry is still there rather than when the hash matches, since two entries in one directory can share a hash. While here, kernfs_dir_next_pos() called the hash @ino. It is never an inode number, so name it @hash. Fixes: 4e4d6d860b93 ("sysfs: Add s_hash to sysfs_dirent and order directory entries by hash") Acked-by: Tejun Heo Signed-off-by: Shakeel Butt Link: https://patch.msgid.link/20260911182815.1992483-2-shakeel.butt@linux.dev Signed-off-by: Christian Brauner (Amutable) [ sashal: Adapted to the older tree's plain void *ns and direct pos->parent and pos->name accesses, since it has no ns_common, RCU parent or RCU name. ] Signed-off-by: Sasha Levin --- fs/kernfs/dir.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index c01d900f09de1..0798f582149f1 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -1774,27 +1774,42 @@ static int kernfs_dir_fop_release(struct inode *inode, struct file *filp) return 0; } +/* + * Find where a listing left off. @resumed says whether @pos is still that + * entry; if not, the first entry at or after @hash is returned instead. + */ static struct kernfs_node *kernfs_dir_pos(const void *ns, - struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos) + struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos, + bool *resumed) { + if (resumed) + *resumed = false; if (pos) { int valid = kernfs_active(pos) && pos->parent == parent && hash == pos->hash; kernfs_put(pos); if (!valid) pos = NULL; + else if (resumed) + *resumed = true; } if (!pos && (hash > 1) && (hash < INT_MAX)) { struct rb_node *node = parent->dir.children.rb_node; + + /* + * Keep a node only on the way left, so the search ends on the + * first entry at or after @hash. The empty name sorts before + * every entry sharing the hash, so it lands on the first. + */ while (node) { - pos = rb_to_kn(node); + struct kernfs_node *kn = rb_to_kn(node); - if (hash < pos->hash) + if (kernfs_name_compare(hash, "", ns, kn) < 0) { + pos = kn; node = node->rb_left; - else if (hash > pos->hash) + } else { node = node->rb_right; - else - break; + } } } /* Skip over entries which are dying/dead or in the wrong namespace */ @@ -1809,10 +1824,13 @@ static struct kernfs_node *kernfs_dir_pos(const void *ns, } static struct kernfs_node *kernfs_dir_next_pos(const void *ns, - struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos) + struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos) { - pos = kernfs_dir_pos(ns, parent, ino, pos); - if (pos) { + bool resumed; + + pos = kernfs_dir_pos(ns, parent, hash, pos, &resumed); + /* Step over @pos only if it survived; two entries can share a hash. */ + if (pos && resumed) { do { struct rb_node *node = rb_next(&pos->rb); if (!node) @@ -1841,7 +1859,7 @@ static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx) if (kernfs_ns_enabled(parent)) ns = kernfs_info(dentry->d_sb)->ns; - for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos); + for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos, NULL); pos; pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) { const char *name = pos->name; -- 2.53.0