From mboxrd@z Thu Jan 1 00:00:00 1970 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 AA053C3DA7F for ; Wed, 31 Jul 2024 11:51:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 28BD26B0089; Wed, 31 Jul 2024 07:51:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1ED056B008A; Wed, 31 Jul 2024 07:51:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 08D516B008C; Wed, 31 Jul 2024 07:51:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id DC4746B0089 for ; Wed, 31 Jul 2024 07:51:40 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 92CAD1202C8 for ; Wed, 31 Jul 2024 11:51:40 +0000 (UTC) X-FDA: 82399883160.05.F8730B5 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf17.hostedemail.com (Postfix) with ESMTP id 2888740024 for ; Wed, 31 Jul 2024 11:51:36 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=uNehLLZ2; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=BqdbbUZ6; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=uNehLLZ2; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=BqdbbUZ6; spf=pass (imf17.hostedemail.com: domain of jack@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=jack@suse.cz; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722426669; a=rsa-sha256; cv=none; b=hQ8BPPiXwSmVDkIAjEFOSo01Am+7a4H2rYTK43aVujxgoW93iafa9RR47L6Tbjc5AD8kPs PuwaI9aNRBos2kiNMEeYRTFmQIjZMR+ozK0WFD1R+z36aEbnXYEYXo+xkXzRHiHLBW5wLM h1yTDR1XOPPnT/3RJV+s8HBOh0cuWno= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=uNehLLZ2; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=BqdbbUZ6; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=uNehLLZ2; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=BqdbbUZ6; spf=pass (imf17.hostedemail.com: domain of jack@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=jack@suse.cz; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722426669; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=sq9YIN5DlT9IR3A/G0n5OxqApgOU08B3b72mMHt4Hk8=; b=5z3YiF5Zp8TzL/n4onoYbseyPYmRdb0pQvsOvgcQ1Hzdnq+HL/uXDFWtzhMMnxMSTBi8Er NuIhHyxoZbKy7gh+Cz1Xxnzdgk9ZX5duN+BmK3285743HXbsQocQq0DP2rU4/UfvsXVWWu KQw1pB86Hh3B46tjEevC+Jv6o+jBp1w= Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 4C04521BD9; Wed, 31 Jul 2024 11:51:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1722426695; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=sq9YIN5DlT9IR3A/G0n5OxqApgOU08B3b72mMHt4Hk8=; b=uNehLLZ2yvwWe+a4kFtXOzpuywYbALziTswUG1/ys1LzgD4pG5owhT25DckHgqGaStKSVV Djm4KI7PGVfQyo5pU+53XdMnB4fC09dJCoBP0fNm4XA2J/s83PQooofnlJ/aZxBaTpgTUv lvy+d3F3G7A6+OrgR8LZVlCOK/yDlH8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1722426695; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=sq9YIN5DlT9IR3A/G0n5OxqApgOU08B3b72mMHt4Hk8=; b=BqdbbUZ6LQ+g5rg0aZzVZmLxXl9/7x4IjGe9tvj7exfn2AmuKiSGORuXK0INTI2QV+OUNn +d6Er+F/Zt4w8SBA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1722426695; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=sq9YIN5DlT9IR3A/G0n5OxqApgOU08B3b72mMHt4Hk8=; b=uNehLLZ2yvwWe+a4kFtXOzpuywYbALziTswUG1/ys1LzgD4pG5owhT25DckHgqGaStKSVV Djm4KI7PGVfQyo5pU+53XdMnB4fC09dJCoBP0fNm4XA2J/s83PQooofnlJ/aZxBaTpgTUv lvy+d3F3G7A6+OrgR8LZVlCOK/yDlH8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1722426695; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=sq9YIN5DlT9IR3A/G0n5OxqApgOU08B3b72mMHt4Hk8=; b=BqdbbUZ6LQ+g5rg0aZzVZmLxXl9/7x4IjGe9tvj7exfn2AmuKiSGORuXK0INTI2QV+OUNn +d6Er+F/Zt4w8SBA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 2C3411368F; Wed, 31 Jul 2024 11:51:35 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 7kXFCkclqmYxRAAAD6G6ig (envelope-from ); Wed, 31 Jul 2024 11:51:35 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 7BDCFA099C; Wed, 31 Jul 2024 13:51:34 +0200 (CEST) Date: Wed, 31 Jul 2024 13:51:34 +0200 From: Jan Kara To: yangerkun Cc: hch@infradead.org, chuck.lever@oracle.com, brauner@kernel.org, viro@zeniv.linux.org.uk, jack@suse.cz, hughd@google.com, zlang@kernel.org, fdmanana@suse.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, yangerkun@huaweicloud.com Subject: Re: [PATCH] libfs: fix infinite directory reads for offset dir Message-ID: <20240731115134.tkiklyu72lwnhbxg@quack3> References: <20240731043835.1828697-1-yangerkun@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240731043835.1828697-1-yangerkun@huawei.com> X-Spamd-Bar: / X-Rspamd-Action: no action X-Stat-Signature: yez8y3q8tj6nj5fypacc4oq8b6xmpyqy X-Rspamd-Queue-Id: 2888740024 X-Rspam-User: X-Rspam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p2wGH6UUJFaHnVQxorMjD90F4KDpxbwnHCPYrKAdMtnLrmDWr9u6D6c+by4FzceW7tJBe9SfvPNXG88aSC4kNzU7ZFJOltKANq9HHv5MXBGEXP7/yTmbYl/QHRG6849HbqoCv1xB2U1fFVIBwdcSo5Nw3WdXFhM9zcG3jDii5RgP8Gki7boS0n6XZVwCiLf78wkK4pVLDkzVyUIBfKFZ4wP5fZpFG2jUxBOTkpWOVJ7ZQFyuiri8dCVx+uDVuQl9c6BdkMVrA6xf3PktTSzzcarE7A/8sQ== 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: On Wed 31-07-24 12:38:35, yangerkun wrote: > After we switch tmpfs dir operations from simple_dir_operations to > simple_offset_dir_operations, every rename happened will fill new dentry > to dest dir's maple tree(&SHMEM_I(inode)->dir_offsets->mt) with a free > key starting with octx->newx_offset, and then set newx_offset equals to > free key + 1. This will lead to infinite readdir combine with rename > happened at the same time, which fail generic/736 in xfstests(detail show > as below). > > 1. create 5000 files(1 2 3...) under one dir > 2. call readdir(man 3 readdir) once, and get one entry > 3. rename(entry, "TEMPFILE"), then rename("TEMPFILE", entry) > 4. loop 2~3, until readdir return nothing or we loop too many > times(tmpfs break test with the second condition) > > We choose the same logic what commit 9b378f6ad48cf ("btrfs: fix infinite > directory reads") to fix it, record the last_index when we open dir, and > do not emit the entry which index >= last_index. The file->private_data > now used in offset dir can use directly to do this, and we also update > the last_index when we llseek the dir file. The patch looks good! Just I'm not sure about the llseek part. As far as I understand it was added due to this sentence in the standard: "If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified." So if the offset used in offset_dir_llseek() is 0, then we should update last_index. But otherwise I'd leave it alone because IMHO it would do more harm than good. Honza > > Fixes: a2e459555c5f ("shmem: stable directory offsets") > Signed-off-by: yangerkun > --- > fs/libfs.c | 34 +++++++++++++++++++++++----------- > 1 file changed, 23 insertions(+), 11 deletions(-) > > diff --git a/fs/libfs.c b/fs/libfs.c > index 8aa34870449f..38b306738c00 100644 > --- a/fs/libfs.c > +++ b/fs/libfs.c > @@ -450,6 +450,14 @@ void simple_offset_destroy(struct offset_ctx *octx) > mtree_destroy(&octx->mt); > } > > +static int offset_dir_open(struct inode *inode, struct file *file) > +{ > + struct offset_ctx *ctx = inode->i_op->get_offset_ctx(inode); > + > + file->private_data = (void *)ctx->next_offset; > + return 0; > +} > + > /** > * offset_dir_llseek - Advance the read position of a directory descriptor > * @file: an open directory whose position is to be updated > @@ -463,6 +471,9 @@ void simple_offset_destroy(struct offset_ctx *octx) > */ > static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) > { > + struct inode *inode = file->f_inode; > + struct offset_ctx *ctx = inode->i_op->get_offset_ctx(inode); > + > switch (whence) { > case SEEK_CUR: > offset += file->f_pos; > @@ -476,7 +487,7 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) > } > > /* In this case, ->private_data is protected by f_pos_lock */ > - file->private_data = NULL; > + file->private_data = (void *)ctx->next_offset; > return vfs_setpos(file, offset, LONG_MAX); > } > > @@ -507,7 +518,7 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) > inode->i_ino, fs_umode_to_dtype(inode->i_mode)); > } > > -static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) > +static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx, long last_index) > { > struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); > struct dentry *dentry; > @@ -515,17 +526,21 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) > while (true) { > dentry = offset_find_next(octx, ctx->pos); > if (!dentry) > - return ERR_PTR(-ENOENT); > + return; > + > + if (dentry2offset(dentry) >= last_index) { > + dput(dentry); > + return; > + } > > if (!offset_dir_emit(ctx, dentry)) { > dput(dentry); > - break; > + return; > } > > ctx->pos = dentry2offset(dentry) + 1; > dput(dentry); > } > - return NULL; > } > > /** > @@ -552,22 +567,19 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) > static int offset_readdir(struct file *file, struct dir_context *ctx) > { > struct dentry *dir = file->f_path.dentry; > + long last_index = (long)file->private_data; > > lockdep_assert_held(&d_inode(dir)->i_rwsem); > > if (!dir_emit_dots(file, ctx)) > return 0; > > - /* In this case, ->private_data is protected by f_pos_lock */ > - if (ctx->pos == DIR_OFFSET_MIN) > - file->private_data = NULL; > - else if (file->private_data == ERR_PTR(-ENOENT)) > - return 0; > - file->private_data = offset_iterate_dir(d_inode(dir), ctx); > + offset_iterate_dir(d_inode(dir), ctx, last_index); > return 0; > } > > const struct file_operations simple_offset_dir_operations = { > + .open = offset_dir_open, > .llseek = offset_dir_llseek, > .iterate_shared = offset_readdir, > .read = generic_read_dir, > -- > 2.39.2 > -- Jan Kara SUSE Labs, CR