From patchwork Tue Feb 13 21:38:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13555687 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 E74D8C48260 for ; Tue, 13 Feb 2024 21:38:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 747DF8D0016; Tue, 13 Feb 2024 16:38:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 69F026B00A3; Tue, 13 Feb 2024 16:38:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 51A4A8D0016; Tue, 13 Feb 2024 16:38:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 3DF1C6B00A2 for ; Tue, 13 Feb 2024 16:38:16 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 161241C1593 for ; Tue, 13 Feb 2024 21:38:16 +0000 (UTC) X-FDA: 81788094192.18.F9B7D1F Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf21.hostedemail.com (Postfix) with ESMTP id C11431C0011 for ; Tue, 13 Feb 2024 21:38:13 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BtQTNyV5; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf21.hostedemail.com: domain of cel@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707860294; a=rsa-sha256; cv=none; b=vRHa4+B9A71z1dudYz/6kCKglfWTSYYlvB9Je3dT3/nb8tYIxzn7sJedQJlwT50LEPw63l +Xw2f/lmVK/mVRO9lIwKmztvkane16qMqM2lQToqS8PRlqfgoFgimkhVvFIwNQHZDOHbGC TVrGdwFYrhUkVWP0PRBsQnnvItX2FwU= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BtQTNyV5; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf21.hostedemail.com: domain of cel@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707860294; 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=R/sSg6xxDbxmhGUF8KYJydF2AEnkGCxNzIuo/Lb2e1I=; b=LZNvrptJy/5jv3bGX1mF6eiMy7LRqDQkuQcF114C5aUfnh0x7zovRbsaCQ5P46p1LNki/C EFyqkAubNq00xsDT892N3nZVhkHnxj80O6fmGROOtDKXZVH7BnizMBOb9m7KC0PWRt6432 WfygDx967yBhsOWi5aQdreI1dytxoMM= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 26825CE1E4F; Tue, 13 Feb 2024 21:38:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6C3E2C433C7; Tue, 13 Feb 2024 21:38:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707860290; bh=SovcTJwr5T0bwcx24VxTqCxw8DF70LVQIJpozpuEJ/c=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=BtQTNyV5Zn/oTgtUO3HAG41FZzx+r21bj4Czooq47TPXHx+lj/idVHknoPLK3RJVH XgyxesfzyB2xskU8InhLkEzRzd2KWcyozSsJCvHLwQa4wW/pOt4dWfHHMJDxF/hD3A LxnOHDiCc8VnukMJfNU0tmiGpoGIVz5iOCXClF4YTEHFcAVLNGtutlmBjVMtVdx6Cb YlYV2q3zpxRKJXV6PPZuUtDHrlJfnxTdXlgzmcoClySh5Gm1bEay5VT63RB9M3AflK 19AvfgMz+13A6rmaQthtztDQ268tQjbpw0iXdKSAJPuHCcfVETwWx0apSTAmxJX+fZ BdiSRx/XBm5NA== Subject: [PATCH RFC 7/7] 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: Tue, 13 Feb 2024 16:38:08 -0500 Message-ID: <170786028847.11135.14775608389430603086.stgit@91.116.238.104.host.secureserver.net> In-Reply-To: <170785993027.11135.8830043889278631735.stgit@91.116.238.104.host.secureserver.net> References: <170785993027.11135.8830043889278631735.stgit@91.116.238.104.host.secureserver.net> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: C11431C0011 X-Stat-Signature: m3qqs5htqf1i83c5qz1ory9ya5nymx86 X-HE-Tag: 1707860293-846379 X-HE-Meta: U2FsdGVkX199RZvRZUBwTNgTT41yWVywZoEK8ju8OkFy6vV7H9E0XeXTNqnfLPMrXuwJl4IemYYocS3LXSoSpxIPbSJaD85yn0HNN859z1755eFB37ZILj/yIdHk9GcTuS/OXOGocAZLF9Ar3SvOgbbrWjIydFIWpgLHdoLrzY+eYPYf4gECPZB59qdhY2TnJqdkZocNmlPrDm3yCU9hO9bS9civhi2RlgnRSRA1Afys0+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@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever Liam says that, unlike with xarray, once the RCU read lock is released ma_state is not safe to re-use for the next mas_find() call. But the RCU read lock has to be released on each loop iteration so that dput() can be called safely. Thus we are forced to walk the offset tree with fresh state for each directory entry. mt_find() can do this for us, though it might be a little less efficient than maintaining ma_state locally. Since offset_iterate_dir() doesn't build ma_state locally any more, there's no longer a strong need for offset_find_next(). Clean up by rolling these two helpers together. Signed-off-by: Chuck Lever --- fs/libfs.c | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index f073e9aeb2bf..6e01fde1cf95 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -436,23 +436,6 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) return vfs_setpos(file, offset, MAX_LFS_FILESIZE); } -static struct dentry *offset_find_next(struct ma_state *mas) -{ - struct dentry *child, *found = NULL; - - rcu_read_lock(); - child = mas_find(mas, ULONG_MAX); - if (!child) - goto out; - spin_lock(&child->d_lock); - if (simple_positive(child)) - found = dget_dlock(child); - spin_unlock(&child->d_lock); -out: - rcu_read_unlock(); - return found; -} - static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) { unsigned long offset = dentry2offset(dentry); @@ -465,13 +448,22 @@ 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 *octx = inode->i_op->get_offset_ctx(inode); - MA_STATE(mas, &octx->mt, ctx->pos, ctx->pos); - struct dentry *dentry; + struct dentry *dentry, *found; + unsigned long offset; + offset = ctx->pos; while (true) { - dentry = offset_find_next(&mas); + found = mt_find(&octx->mt, &offset, ULONG_MAX); + if (!found) + goto out_noent; + + dentry = NULL; + spin_lock(&found->d_lock); + if (simple_positive(found)) + dentry = dget_dlock(found); + spin_unlock(&found->d_lock); if (!dentry) - return ERR_PTR(-ENOENT); + goto out_noent; if (!offset_dir_emit(ctx, dentry)) { dput(dentry); @@ -479,9 +471,12 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) } dput(dentry); - ctx->pos = mas.index + 1; + ctx->pos = offset; } return NULL; + +out_noent: + return ERR_PTR(-ENOENT); } /**