Received: by 2002:a89:288:0:b0:1f7:eeee:6653 with SMTP id j8csp561773lqh; Tue, 7 May 2024 07:31:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXNJ0gdZZrp7TvLyetrh7SsUF5FJC6z/xVtBmmFLWbyycyIk6VXXEJ2eRzIsBZGyFX07B0Q+qYArnMihtGFXaV1Zies1yCKVrQ/bqp+Xw== X-Google-Smtp-Source: AGHT+IFJWRKK9kY8o2zdi2OhMmBtDzMtxWoqd7bXlEEPtLJGb2PHNHBkpi90e96ansGJGwAulfJH X-Received: by 2002:a17:903:2342:b0:1eb:7081:3e23 with SMTP id c2-20020a170903234200b001eb70813e23mr17065334plh.66.1715092284191; Tue, 07 May 2024 07:31:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715092284; cv=pass; d=google.com; s=arc-20160816; b=aLznFBAtDw1vplXUvY421zlQTxveWQwATDnvy+QsffLcj44h0uXOGFmSLKDIR4m2BZ v0V9kWBinKc+rGNrdk2barj0gbjB7zHKdBsrRq/oo2PUjqAP9TExP/qcjZhrXossaDnz 7UoJDggasIGKZagk33oleCw1tMtYFqZAZKxoOulu4RqOKXc9F8aD1MDAUGQtL9oDLFmK TiEO9XlS6Ynq1CupCxDnNfHC3hmYLAPeJwTOlzYTBoKyvu5EVN17FzMKhNyslBNoAE3D kbUzVsaTmTHAUNwcg3VvwbjS2Fz4K05vv+/lxxzknVIcSJvPtJxu1u4GJW4I6p5WP5lS wGOQ== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=dcUIKj4EN+/20/SDxhzv7jmW6Wb7Q+V+3wLpM4sksEI=; fh=X1/oBJvTGJUQ0YtRp03u+3tPfRw/dXXALWynfOSlfQQ=; b=L4yoFggxjNuzOIhl48TvxacPFzkQVJAwzjU3vjSGEgWWkwBSPAJfZzqaCbZRcsB/gN HCR0mEu4SudrrHOoObCLLrS/ilHsESw1r8/ZbGuxqWv8sj4DgANXL6ff9ovi3qup6Cla NbvP80A02kpILwJE+rODd1VcmOU32yqdje6+6oLZpjnT61SNvudelqP3t7hdbAmZyWbJ V0vJR3BFo6s+9/qCJ7gfZzDWnXZi4+tCcUv0mMy5gdmspAVOeb5AbNU4gFQKnqJ6BujM QvkOUTCEhsVnmIria/iA85fVMCEFSUsUlBlV1PwBIRGFNmlyccY7ZYYucpxLdTAFArcC GWCg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YnJzb3bI; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-171497-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171497-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id a12-20020a170902eccc00b001ec5ac16345si10989587plh.368.2024.05.07.07.31.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 07:31:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-171497-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YnJzb3bI; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-171497-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171497-linux.lists.archive=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id DDBC9B238EE for ; Tue, 7 May 2024 14:06:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DD03215F3E4; Tue, 7 May 2024 14:06:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="YnJzb3bI" 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 12F32156C7A for ; Tue, 7 May 2024 14:06:04 +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=1715090765; cv=none; b=Dc//fgAW/RAWN+rdiHiyAjgFvvxtZix+cMCX7PM4/CbkkD2DYwmlBlVkaZTO9Kbxb5+5cR5JP5eWByxk9PlfbgVxAk7FZHYt9eOh7lxJpTkLL/DOJaYZSmmtJcwh8uoJJoaWgW+nBUz4K35R26jCD/tc3IyUNyz37r1pJ7xEvfk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715090765; c=relaxed/simple; bh=t2Zobjc7MI6+ixD8KnilMidW0woMS15GwGLeKi1FmbM=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=PLYniHDp9blEk5Y8KvPRaRb1+MXhr/kKrFABCYPlL/PN5CI4nfaq4zDyg3EPvSWoJGZ2NuqfAJsoEUHZXpaRNR7nIcLU93H7S1xYRipmNnZ4+rTPoAE9W8IUAsiLXzZ2+DOxXjOz+ZIJpD4buF6QuBuqB3t2mIO75RVq3pLt2KY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YnJzb3bI; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54827C2BBFC; Tue, 7 May 2024 14:06:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715090764; bh=t2Zobjc7MI6+ixD8KnilMidW0woMS15GwGLeKi1FmbM=; h=From:To:Cc:Subject:Date:From; b=YnJzb3bIQFe3kLH190BdMGPr0E/HvQ4xdxZGybOaO0N2371wx4TdQn1Qlpnc4pqMc yuDw2t8/UehvZupwNXyC2fmmU5nmGiECGwUcGjy/LnJ4Q4YmAFMb+jQiLT2PjXkgqc 31l/kzAmeZlDUF5hxsnytnFkn0TUkXrS6ebSKySBtDGqYWJiwFVjkUgO+UPdA/0vXn DHojnbLRKJJhT8C5DfTqXFbXrjdZnTU5pvNlK87FWDc0+sr0n4RghyFC9hJ36dt2n1 oduTM/T+Hj82UmNM3cqeUePZ4ohusMGBu7bfrDYxXaZOXcOrhpiq+13shK1VhoW7WP rE71W4TunDKBg== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu Subject: [PATCH] f2fs: fix to avoid racing in between buffered read and OPU dio write Date: Tue, 7 May 2024 22:05:56 +0800 Message-Id: <20240507140556.1293003-1-chao@kernel.org> X-Mailer: git-send-email 2.40.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit If lfs mode is on, buffered read may race w/ OPU dio write as below, it may cause buffered read hits unwritten data unexpectly. Thread A Thread B - f2fs_file_write_iter - f2fs_dio_write_iter - __iomap_dio_rw - f2fs_iomap_begin - f2fs_map_blocks - __allocate_data_block - allocated blkaddr #x - iomap_dio_submit_bio - f2fs_file_read_iter - filemap_read - f2fs_read_data_folio - f2fs_mpage_readpages - f2fs_map_blocks : get blkaddr #x - f2fs_submit_read_bio IRQ - f2fs_read_end_io : read IO on blkaddr #x complete IRQ - iomap_dio_bio_end_io : direct write IO on blkaddr #x complete This patch introduces a new per-inode i_opu_rwsem lock to avoid such race condition. Fixes: f847c699cff3 ("f2fs: allow out-place-update for direct IO in LFS mode") Signed-off-by: Chao Yu --- fs/f2fs/f2fs.h | 1 + fs/f2fs/file.c | 20 ++++++++++++++++++-- fs/f2fs/super.c | 1 + 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index 145b985bf252..b69ec1109572 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -847,6 +847,7 @@ struct f2fs_inode_info { /* avoid racing between foreground op and gc */ struct f2fs_rwsem i_gc_rwsem[2]; struct f2fs_rwsem i_xattr_sem; /* avoid racing between reading and changing EAs */ + struct f2fs_rwsem i_opu_rwsem; /* avoid racing between buf read and opu dio write */ int i_extra_isize; /* size of extra space located in i_addr */ kprojid_t i_projid; /* id for project quota */ diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index ef4cfb4436ef..c761db952b37 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -4545,7 +4545,13 @@ static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) if (f2fs_should_use_dio(inode, iocb, to)) { ret = f2fs_dio_read_iter(iocb, to); } else { + bool do_opu = f2fs_lfs_mode(F2FS_I_SB(inode)); + + if (do_opu) + f2fs_down_read(&F2FS_I(inode)->i_opu_rwsem); ret = filemap_read(iocb, to, 0); + if (do_opu) + f2fs_up_read(&F2FS_I(inode)->i_opu_rwsem); if (ret > 0) f2fs_update_iostat(F2FS_I_SB(inode), inode, APP_BUFFERED_READ_IO, ret); @@ -4770,14 +4776,22 @@ static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from, ret = -EAGAIN; goto out; } + if (do_opu && !f2fs_down_write_trylock(&fi->i_opu_rwsem)) { + f2fs_up_read(&fi->i_gc_rwsem[READ]); + f2fs_up_read(&fi->i_gc_rwsem[WRITE]); + ret = -EAGAIN; + goto out; + } } else { ret = f2fs_convert_inline_inode(inode); if (ret) goto out; f2fs_down_read(&fi->i_gc_rwsem[WRITE]); - if (do_opu) + if (do_opu) { f2fs_down_read(&fi->i_gc_rwsem[READ]); + f2fs_down_write(&fi->i_opu_rwsem); + } } /* @@ -4801,8 +4815,10 @@ static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from, ret = iomap_dio_complete(dio); } - if (do_opu) + if (do_opu) { + f2fs_up_write(&fi->i_opu_rwsem); f2fs_up_read(&fi->i_gc_rwsem[READ]); + } f2fs_up_read(&fi->i_gc_rwsem[WRITE]); if (ret < 0) diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c index daf2c4dbe150..b4ed3b094366 100644 --- a/fs/f2fs/super.c +++ b/fs/f2fs/super.c @@ -1428,6 +1428,7 @@ static struct inode *f2fs_alloc_inode(struct super_block *sb) init_f2fs_rwsem(&fi->i_gc_rwsem[READ]); init_f2fs_rwsem(&fi->i_gc_rwsem[WRITE]); init_f2fs_rwsem(&fi->i_xattr_sem); + init_f2fs_rwsem(&fi->i_opu_rwsem); /* Will be used by directory only */ fi->i_dir_level = F2FS_SB(sb)->dir_level; -- 2.40.1