From patchwork Fri Jan 24 19:19:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13949883 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79321C0218C for ; Fri, 24 Jan 2025 19:19:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 10B6F280096; Fri, 24 Jan 2025 14:19:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BA0F280092; Fri, 24 Jan 2025 14:19:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC55A280096; Fri, 24 Jan 2025 14:19:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C621F280092 for ; Fri, 24 Jan 2025 14:19:54 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7D00FA0FC5 for ; Fri, 24 Jan 2025 19:19:54 +0000 (UTC) X-FDA: 83043310308.14.377740F Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf20.hostedemail.com (Postfix) with ESMTP id C4EA61C0015 for ; Fri, 24 Jan 2025 19:19:52 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QKo7eiYs; spf=pass (imf20.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737746392; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=OG9Wcof/bc4xR3FPccrdiiZ/fDzrZduD5mlzSE5vVsI=; b=ZiQkwSS+dLrvU993HBZts5Uw6BGYsPOQL6AwvQOCE0YDCdBfs/qNgWdG4N5HgYn0LUoWH3 P3X70qYzGo+/+1x0s9aUjgWiyh6P67c6kJVlSgV1A7ju+s9sM8WgMjwfR6qVg2ljn0NNh4 5OeZb6GzHKYm4eQFkpqtyg1MZPMaEAo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737746392; a=rsa-sha256; cv=none; b=iVVehvfNamt/6QFI0O8nvjYItidtTdNnbD+SaGBy2lTkcS9G6Ug+9cC962cJ52VDio56FP EeLWuoWkrGzWkF9F67tPFmKfhbsNgZj94vOAUwUt/YYOnduVzauOUNXq+dnLZl2ugbXfDu dE3ty6/Az5Ya8of27uJWOTeclXmc8ZA= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QKo7eiYs; spf=pass (imf20.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 45AFD5C6196; Fri, 24 Jan 2025 19:19:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2853FC4CEE3; Fri, 24 Jan 2025 19:19:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737746391; bh=AAimS1DXsD+1vqFtQxl+lQZkXctbDP9Ux3Ct0lAIRKY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QKo7eiYsDqb+VJ1oghRzIW9zWNc0+ukRB/vdi13rpjkOm/x9/En0p5WvGX9nbJBoE HKKpJWQKWEq9pjE+a5Gj5Vkn3ZJDWMNASXHPi/KNRrCFjDrc2JApS7dOBmshLFifcJ kQHpa+wwadPT1PZ35KhcpESCLAeXiAYkgJOpq7xYRwZjpO+AjnvksUFltUOinBZ/ys oDlVcHtv4t6rEoisItohXmncZXTf5yIiyu2U8gGGuNLFtRu8nCIFZZfjxilbyW7BD2 Sp6Py8Q3zyWbOYEDHjsfHF6GR4d6tifGaCioJUdrvRKF7xpSpxttbURQsEQhr1uQCZ QgUKGWdEXQJlQ== From: cel@kernel.org To: Hugh Dickins , Andrew Morten , Christian Brauner , Al Viro , Greg Kroah-Hartman , Sasha Levin Cc: , , , yukuai3@huawei.com, yangerkun@huawei.com, Chuck Lever , "Liam R. Howlett" , Jan Kara Subject: [RFC PATCH v6.6 01/10] libfs: Re-arrange locking in offset_iterate_dir() Date: Fri, 24 Jan 2025 14:19:36 -0500 Message-ID: <20250124191946.22308-2-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250124191946.22308-1-cel@kernel.org> References: <20250124191946.22308-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: C4EA61C0015 X-Stat-Signature: 5xztj3uayfgoozh8oegb6esbgrby8jz7 X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1737746392-201146 X-HE-Meta: U2FsdGVkX19YGJVg0n6Wx5iowPWF8oAKrGOQBQSZiWAXAYEEXAC+GYd7KmkNzNKHloiLweAwOJ83bKvoLDkqYMfjzancgC3WmwzEI/++suZ30DBuR+gFdo3KNO6LN7b08ALWyk/lzVybrsd9nieq8XOm00PIH5gFxFg1lRLBd5dFY7mL8GJT816nRGONwmCs0Ho6MSMkLEQPur6u5teKjSWcI/dnRn3vc5LmaFTdIxgXiFPFpqQ2xHt/9XJOH0e3cI37esWkvA1PmNw+lsOapG/Sso/SefotlfjTb7SSZrFrK5IrPnT65mdSdrpUL6MF/TcFDdscb3K+S0vGBjeZej9A1pkXZJNJMGJfqGeW31fk3ASO0+jJpQoQQj4w97wbeeU82Y6X1mREsI7/tlEj0Jgs1ZigfhbVuJwkbp58IeLlS3/Cse98lEE4gCL/pp/XGuaQLZSVCpUKadG+Ds+qisGpxsg6UHE5E+Diftvm+ho6VJE40Cfo2Wb3WLG2O03JzRc0icI5aVbTtKWLQ7TujuI9ewXUx7ylAL1XfLkounIUgU63rgTH+kxYXt3V7+V1OBv0yZpieFnd+eA+af5TUwFFQV5DuARg7TspocStDcA/xc+pvv6P0ZWqGvwF+gasxYGjz9QhSdnfh3/Xsf/8dWYoUxHByuRwI7ph7SGGjHBqRnAIuXE7gXBpkaLiLKuG5j/axHH8P5Zu95zzrR64GVxHLSbr/geFRT7zQnpkRre5m5qeXIJrZLGZA997UfFSH5QJ6LkivP78c0yme3JvPa01pUZ1dWXZLoPB3nz2COihGvCuzusIJ17gZaqCgw2YPNETQYomYrZbPJ5BrAytgTZW6nq/HTp1tBysdVnv3pIQ325NUHoeOOBk+g5ES52Hau5QWlGhL2lBO5FkNZceGlNWBXzkfIXPSIX4QSJilZVF7RlxraO4llHxL+d0uqeEWv4U7ChIk5uIQGygFPJ 61STOntw pkjfHj+N35i8jXIi02WT757Fk0srJN1LkTnS+e12uaJECEbG0aM0S1GCdAhdKAkhnB5xY2dI/sRZgZ9LPbnu5D45F3FMutPapKiQEJVwnAxayhpLAoerKvvF2F8VpL/39O+t17tds/0PamE3F4tUZwOgKgtuR+D6g/+CbwMJKKzyqceGQqij6ufR7XLXy5iKrR/hhtF1qI9awEsST4z5Dydo5vUOce1fOuQQKx+txtaQRNIKCjacegS6+mI1NXbEnox2WJhxrycARBL9mpS/G60HaC0234OfqCIrQ24zDflGY6JhYi5UGo9K/6BrhCfEO74oi7e5Xi/0sWkigv13QeEVPYrVRNDEWqJackq6ZDrHCy5w= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever [ Upstream commit 3f6d810665dfde0d33785420618ceb03fba0619d ] Liam and Matthew say that once the RCU read lock is released, xa_state is not safe to re-use for the next xas_find() call. But the RCU read lock must be released on each loop iteration so that dput(), which might_sleep(), can be called safely. Thus we are forced to walk the offset tree with fresh state for each directory entry. xa_find() can do this for us, though it might be a little less efficient than maintaining xa_state locally. We believe that in the current code base, inode->i_rwsem provides protection for the xa_state maintained in offset_iterate_dir(). However, there is no guarantee that will continue to be the case in the future. Since offset_iterate_dir() doesn't build xa_state locally any more, there's no longer a strong need for offset_find_next(). Clean up by rolling these two helpers together. Suggested-by: Liam R. Howlett Message-ID: <170785993027.11135.8830043889278631735.stgit@91.116.238.104.host.secureserver.net> Signed-off-by: Chuck Lever Link: https://lore.kernel.org/r/170820142021.6328.15047865406275957018.stgit@91.116.238.104.host.secureserver.net Reviewed-by: Jan Kara Signed-off-by: Christian Brauner Signed-off-by: Chuck Lever --- fs/libfs.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index dc0f7519045f..430f7c95336c 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -401,12 +401,13 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) return vfs_setpos(file, offset, U32_MAX); } -static struct dentry *offset_find_next(struct xa_state *xas) +static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset) { struct dentry *child, *found = NULL; + XA_STATE(xas, &octx->xa, offset); rcu_read_lock(); - child = xas_next_entry(xas, U32_MAX); + child = xas_next_entry(&xas, U32_MAX); if (!child) goto out; spin_lock(&child->d_lock); @@ -429,12 +430,11 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) { - struct offset_ctx *so_ctx = inode->i_op->get_offset_ctx(inode); - XA_STATE(xas, &so_ctx->xa, ctx->pos); + struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); struct dentry *dentry; while (true) { - dentry = offset_find_next(&xas); + dentry = offset_find_next(octx, ctx->pos); if (!dentry) return ERR_PTR(-ENOENT); @@ -443,8 +443,8 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) break; } + ctx->pos = dentry2offset(dentry) + 1; dput(dentry); - ctx->pos = xas.xa_index + 1; } return NULL; }