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: 200666 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp823390dyb; Tue, 13 Feb 2024 13:40:58 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXnz1G7vm62t7d/4CkYl8SseVckY67/1I+XmaYyAAJc0rtJJj0OLAHfzPV4Iyqxy1YgXEkjOb1yKcblb6GnK50a1lo7tw== X-Google-Smtp-Source: AGHT+IHlJtG6lBPnnKBpXjVCoSNwnPGhAVegn0unloePjb9L4dZCbrzNRI99+6r/KdgQmHPPW7PB X-Received: by 2002:a0c:f594:0:b0:686:ab05:59fb with SMTP id k20-20020a0cf594000000b00686ab0559fbmr768714qvm.8.1707860458757; Tue, 13 Feb 2024 13:40:58 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707860458; cv=pass; d=google.com; s=arc-20160816; b=RUEIAExlqT//m+1Hz+PXHF+/x8E8o10I1xRzp9jWZ06+8Mh2lK88aG/2kNDxvI1+XQ JhL1epsUlhFSnNqF50a9wtJ+eI3ybwJ25N/nE+X4BUjwtyiynffku6Pl9U5BIekuqB4O UexNxFQVFBF6c5Qyhl8B1+RI8c4/ZjqIDThg3zL6Go7edHERUDDi4yWqMLwuYkWly/5E l/Lk+sHXHzgHtaPrMBWItErukYHJk/g1fHX0beF2mP/vgnBbOW6GEGkwwRhWbOsH8KoG y41Qs8dQPbl577EPnIGeRaiZXKwFjtZ9p+oLDobctBxmEwHH1+59ywlS77ccBDH/U1CB y7qw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:references:in-reply-to :message-id:date:cc:to:from:subject:dkim-signature; bh=R/sSg6xxDbxmhGUF8KYJydF2AEnkGCxNzIuo/Lb2e1I=; fh=XC7QALz6am8mCRHWKOZO3uxZQg3wt+lVBimFUtIi/zs=; b=CjvIGu905Sx9bJplvskt/VSSBu7DvZuFTBDr/xJcJRQPiWLJFyZyxQcWTDz7Ch++uR kJ4q+NoMba/Bn/UeA+J8o08cl35KM0S6LyI2xb953hFmurCMP7BtEKNFXoMzS/Ug8/rl oYa2ZxOpsFDYmA1sgOGg/iUpY8pcs02A0MJrETiUmVmcaI7Va2bw0zi7SzV9ajOM/1oG fgovsh0NVaqYtxe3RJ4e3MGYhCdj0MhQyglubgJMvniJ27e3Y9JH852p3YiUX3VMsqTm yPpkdwFNanaiSJQMreYZMrLiWzvDzcofYw0KPF7TyUiSRxCPMj10zjGN9gluoaGrUUMa QShA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BtQTNyV5; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-64348-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64348-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCWdOA9wn3MUEwclOcT4dBtgvni0A2i4a48vcOOJ0A0bQvmS6yqpw0ulRkr8kHu2TxMmaMV4+Ei7fnNVc4ssJv7CLGk2aA== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 5-20020a0562140dc500b0068cd85d5d9csi4036388qvt.93.2024.02.13.13.40.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 13:40:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64348-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BtQTNyV5; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-64348-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64348-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 3C3B01C2737A for ; Tue, 13 Feb 2024 21:40:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 716DD62A1A; Tue, 13 Feb 2024 21:38:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BtQTNyV5" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E16E4629E7; Tue, 13 Feb 2024 21:38:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707860291; cv=none; b=AgYFfxcwSXUYGoWaQBTXtoTOvjAb6AQpLz64GCS32gcjOn+65kuJ67bgqOBFwctJ/7G9DO9JNXJEa1YMI12qDfrt2TlHexjrqTF6pUP8W1eUyQslYBhtgmtNlMgUrLt7j7y1iuA3WMvCsY/fD6auYpfgnpAqhDcdejqe7q2swfA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707860291; c=relaxed/simple; bh=SovcTJwr5T0bwcx24VxTqCxw8DF70LVQIJpozpuEJ/c=; h=Subject:From:To:Cc:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rW/KUxZb4jC1VjwUzXhJWfah05t/rAqctgkP1j3p+A2hrGDv/jk8j2VtQnGJSXSo9TJyxFqOjmAwL9kuldIKPEA0Uqci+DOu/mMdbGmr4AxQ1SDlksbu4mDXvyMk4twEylCVGKJY3ijm2p90lr4dwKkhGSUa4KCOXMnyTCuCoE4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BtQTNyV5; arc=none smtp.client-ip=10.30.226.201 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 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790821488500366785 X-GMAIL-MSGID: 1790821488500366785 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); } /**