From patchwork Sat Feb 17 20:23:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13561580 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 F235AC5475B for ; Sat, 17 Feb 2024 20:23:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C12E8D0005; Sat, 17 Feb 2024 15:23:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 672598D0001; Sat, 17 Feb 2024 15:23:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4EAFF8D0005; Sat, 17 Feb 2024 15:23:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 3773B8D0001 for ; Sat, 17 Feb 2024 15:23:45 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1740DA0349 for ; Sat, 17 Feb 2024 20:23:45 +0000 (UTC) X-FDA: 81802421610.03.AC351AA Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf18.hostedemail.com (Postfix) with ESMTP id 5E0AE1C0009 for ; Sat, 17 Feb 2024 20:23:43 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=P4X30OxK; spf=pass (imf18.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708201423; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LMwQkCs3Uwwg0hCaF42Dp3GN9kHRnCL1eJ1vax4uPe8=; b=kz5H1WJappccMC7hrqykju3K1JrLNxbOGayiMNbsaB3XLgf/KBBvlEsZzkhfyFsIfWDEX5 zzoxtXeTMlSWIzsDyRzKkEA2xGMzpiWBfbOVIoIMWP/zZf7y0JoOpMHyGA0SvSSMIyCXB+ zCHfV1OT2W44o2O2zGJdqfDS18UMeH0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708201423; a=rsa-sha256; cv=none; b=lcfw6L+enosabQoe7HXgSdjJyAgHt96Dql/RIHVZAyCXIVfKd0D5au+/33S5DhJa/lzURL e6auoX1xpepc2osJD7BRjc9FB05l9d0Wp43Ns9KTzCqb5JDgVE1bY6UzM1Jhe/wE6eH/qZ DWnQDriJvIZsldBxD/zU37ayIPQFh1s= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=P4X30OxK; spf=pass (imf18.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=none) 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 8042A6066F; Sat, 17 Feb 2024 20:23:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 32618C433F1; Sat, 17 Feb 2024 20:23:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708201422; bh=R5X4D36CuE1wpCsrFE/3Fuex0DnO2GtnjfkqVv5cjQg=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=P4X30OxKc0EN8nPXm8GE/AdSc3yeT5iVNwWMRGyRMVcJFIie5F4caTrhUc28NAuXO 9voVKSq/opp81jg2cRH3/szMzpTTQ/4XgrT8WdD2WpFkvBWhxoEj1F/vcTYv1kOP4+ FZdDrkL9Ov1gk0qvcx9T66jKl6xWpHB0h4rQB5Ze7WhCUX3SKZ6cEUOGk9Z5vnO5+j XY0aze0RMR8AG9xJitjPfN95aBR+joD0NoDr2zyACqqmGYNchunmvsmDUeW9cgCf9a DCi8QemiRpOkaILmDK6aiHQNcv88w0pNMBL71uYhHVosom/O2z6gP7OwvFkgLUc3ph MCA7wONlVDoYw== Subject: [PATCH v2 1/6] libfs: Re-arrange locking in offset_iterate_dir() From: Chuck Lever To: viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, hughd@google.com, akpm@linux-foundation.org, Liam.Howlett@oracle.com, oliver.sang@intel.com, feng.tang@intel.com Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, maple-tree@lists.infradead.org, linux-mm@kvack.org, lkp@intel.com Date: Sat, 17 Feb 2024 15:23:40 -0500 Message-ID: <170820142021.6328.15047865406275957018.stgit@91.116.238.104.host.secureserver.net> In-Reply-To: <170820083431.6328.16233178852085891453.stgit@91.116.238.104.host.secureserver.net> References: <170820083431.6328.16233178852085891453.stgit@91.116.238.104.host.secureserver.net> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspamd-Queue-Id: 5E0AE1C0009 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: wuacf1o78d7iea4eg5ywaoz3m4q8byaz X-HE-Tag: 1708201423-772189 X-HE-Meta: U2FsdGVkX1/ezRKmVi8CYt8bL1UrJC4+pLcoCP2x6N+kbPUORzdTVgssy/YNmwpUeMa0XB6L3ugH3QTOdKzdBlML4f8KkolX6SNo7hESABsWpFuV52gcYfgMzuXRDF0X1tJWsQnxLnsmCQRS4hTupwysAjLCmO06RG7L2GrQyvzF5dWamfI0D+yek+g9JZdKfaY6PRj+CulwTPynuQ44ut8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Xc= 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 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 Reviewed-by: Jan Kara --- fs/libfs.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index eec6031b0155..752e24c669d9 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -402,12 +402,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); @@ -430,12 +431,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); @@ -444,8 +444,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; }