Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp7538045ybc; Thu, 28 Nov 2019 19:39:21 -0800 (PST) X-Google-Smtp-Source: APXvYqyBE0KxOmB2V1YBijmNIoyuMZX4/ggSKddgjVxL+ZE4A3hGVRoDIaXXecMZMqgONKbfy8xo X-Received: by 2002:a05:6402:2d0:: with SMTP id b16mr6430167edx.227.1574998761677; Thu, 28 Nov 2019 19:39:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574998761; cv=none; d=google.com; s=arc-20160816; b=MwxA7tQZ02JjRittEtj8/ETkY+kvorWaSHJzYyNK3jNRjTZ6EEJH6ZaVZYImwg70Id xuudXR6P5bdcWthl+PWymzjambO1O4RGGFstPmvmFLI9Al2L4/Skwj0sxEkhpqrYs6M8 wcIvXRBlzNuhaDJUFfN625EqPj12tTDcp9PTP0Qhnlwf909QtQFvyFH8hwGxKNRk+kKk oa4Inoews/47elhNrUPceJ+dDQagQGqa84wu+euUmWlagny/UVfI+9hDvVpEh3GvxPw1 f07YPQPKbKjEgH8BhYEtc674w4budtzeAQkF0qqiMtiHEv9AJbAODofnXnnh8zvZLZ2K vNxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=QJ0BehjFJkWJyP8RtCONd9xszCsERb/O9WmeExTQd5k=; b=J9w2igjsacRFjz0bDrRENDbfS8DD6gLxiJi3ZGzqyHr8tLNTKMNui3Xc0GPQfwyX4A /jaVJc5WqwEM1JShJ8Mzxty3doI1yWz0PB2kJAIX1ypXdS4qeUMRhprIbh+HBA3ZLWRC mmgoh0+BbsEGZL07iXU3e+T4mCoUmKWE+4PWbrqpm1O2UP4k67KmgprrRo1wJGfj3iXD sDO2XaTByTrD+TmeODoTYoj+xVU7Vp4w5oViGnkgcqBKHlJeJHOnDD6krQI3SVzi514k 5QaIUsz7Hi7SPQbsB9gcmD7lLRakKSQ8+hC1+/czCi5Xv8Vad3ZGI7RaduXI8mexymQz TISQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@javigon-com.20150623.gappssmtp.com header.s=20150623 header.b=QfkEgP0L; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d18si1668886eds.125.2019.11.28.19.38.57; Thu, 28 Nov 2019 19:39:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@javigon-com.20150623.gappssmtp.com header.s=20150623 header.b=QfkEgP0L; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726934AbfK2DfV (ORCPT + 99 others); Thu, 28 Nov 2019 22:35:21 -0500 Received: from mail-wm1-f67.google.com ([209.85.128.67]:40694 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726788AbfK2DfV (ORCPT ); Thu, 28 Nov 2019 22:35:21 -0500 Received: by mail-wm1-f67.google.com with SMTP id y5so13425906wmi.5 for ; Thu, 28 Nov 2019 19:35:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=javigon-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=QJ0BehjFJkWJyP8RtCONd9xszCsERb/O9WmeExTQd5k=; b=QfkEgP0LSNnXnXJcK7sI02htl820DB5bfsY47OwRNyzmGbQX/PzK/FxsZx3bI28akq LeDtDTO+1gBXwVGMvLEnATIxv7qa7CjbeB0ls0dcWEM78aKuIiAPgr2fcnEL7efCNIgQ KsbuiUjKC4h/6cCfw7Y+LWD+RHGWQS35wN2Dlb/F+3qx7SbAInmdwnRtQgCF33LeVFby RmHDDK6gVQpiZb+mqxjyG4hYbCTcncu3C3OlFxGou2TT8n/6sBG6aWB0dsQ5+iMuy8Jo Sa924LLfQY3/gWJ5RWOzAjTBjsgu5fS7Ti1KvcBpxmvI6r8ackcKQW+IabakzoOM3mZx ZSIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=QJ0BehjFJkWJyP8RtCONd9xszCsERb/O9WmeExTQd5k=; b=SxlsDpXvoAIEQjZmvZ94X9CnhY04XIAXCKlephyfIghBp1JHZmwRxGbjYqea36yj65 DZTI4Psa1p2hBVjDN7QLhX2kYF4hkCwy2jc+ZIESudcSIeYLqJC9z/+5DHrwvdrYRg0m /qBrG1qltMt9puMkm8H1UaRod7wBfj3P5pRiAMRGKOkKshuLF5wggjBAFfOvH4KpHhUq QdTLE1TpCpc47Won2xTPTf58yZ2p8LCao/FiXU4UzcgtKC7wdB+JHbBZX0W8MNUK40OJ jQcJAD/kwXnD5FW3FFAiQzm9g1iQFCbDO1wRDCzUnVxC37GdAHDyT6F3Fi0bjBMo0aG6 BB7g== X-Gm-Message-State: APjAAAUm8TF/nsTarv6tO4erHFD91ZJGnoofIhE7jkscpqFvkWVmjl0b 4i05366b6FRtpzX0hCVgBXiIPQ== X-Received: by 2002:a1c:3941:: with SMTP id g62mr11971950wma.165.1574998517292; Thu, 28 Nov 2019 19:35:17 -0800 (PST) Received: from localhost (ip-5-186-122-168.cgn.fibianet.dk. [5.186.122.168]) by smtp.gmail.com with ESMTPSA id x9sm25131773wru.32.2019.11.28.19.35.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Nov 2019 19:35:16 -0800 (PST) Date: Fri, 29 Nov 2019 04:35:15 +0100 From: Javier Gonzalez To: Jaegeuk Kim Cc: Damien Le Moal , linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu , linux-fsdevel@vger.kernel.org, Shinichiro Kawasaki Subject: Re: [PATCH] f2fs: Fix direct IO handling Message-ID: <20191129033515.ehkdf65toblntkrq@MacBook-Pro.gnusmas> References: <20191126075719.1046485-1-damien.lemoal@wdc.com> <20191126234428.GB20652@jaegeuk-macbookpro.roam.corp.google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20191126234428.GB20652@jaegeuk-macbookpro.roam.corp.google.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 26.11.2019 15:44, Jaegeuk Kim wrote: >On 11/26, Damien Le Moal wrote: >> f2fs_preallocate_blocks() identifies direct IOs using the IOCB_DIRECT >> flag for a kiocb structure. However, the file system direct IO handler >> function f2fs_direct_IO() may have decided that a direct IO has to be >> exececuted as a buffered IO using the function f2fs_force_buffered_io(). >> This is the case for instance for volumes including zoned block device >> and for unaligned write IOs with LFS mode enabled. >> >> These 2 different methods of identifying direct IOs can result in >> inconsistencies generating stale data access for direct reads after a >> direct IO write that is treated as a buffered write. Fix this >> inconsistency by combining the IOCB_DIRECT flag test with the result >> of f2fs_force_buffered_io(). >> >> Reported-by: Javier Gonzalez >> Signed-off-by: Damien Le Moal >> --- >> fs/f2fs/data.c | 4 +++- >> 1 file changed, 3 insertions(+), 1 deletion(-) >> >> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c >> index 5755e897a5f0..8ac2d3b70022 100644 >> --- a/fs/f2fs/data.c >> +++ b/fs/f2fs/data.c >> @@ -1073,6 +1073,8 @@ int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from) >> int flag; >> int err = 0; >> bool direct_io = iocb->ki_flags & IOCB_DIRECT; >> + bool do_direct_io = direct_io && >> + !f2fs_force_buffered_io(inode, iocb, from); >> >> /* convert inline data for Direct I/O*/ >> if (direct_io) { >> @@ -1081,7 +1083,7 @@ int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from) >> return err; >> } >> >> - if (direct_io && allow_outplace_dio(inode, iocb, from)) >> + if (do_direct_io && allow_outplace_dio(inode, iocb, from)) > >It seems f2fs_force_buffered_io() includes allow_outplace_dio(). > >How about this? >--- > fs/f2fs/data.c | 13 ------------- > fs/f2fs/file.c | 35 +++++++++++++++++++++++++---------- > 2 files changed, 25 insertions(+), 23 deletions(-) > >diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c >index a034cd0ce021..fc40a72f7827 100644 >--- a/fs/f2fs/data.c >+++ b/fs/f2fs/data.c >@@ -1180,19 +1180,6 @@ int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from) > int err = 0; > bool direct_io = iocb->ki_flags & IOCB_DIRECT; > >- /* convert inline data for Direct I/O*/ >- if (direct_io) { >- err = f2fs_convert_inline_inode(inode); >- if (err) >- return err; >- } >- >- if (direct_io && allow_outplace_dio(inode, iocb, from)) >- return 0; >- >- if (is_inode_flag_set(inode, FI_NO_PREALLOC)) >- return 0; >- > map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos); > map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from)); > if (map.m_len > map.m_lblk) >diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c >index c0560d62dbee..6b32ac6c3382 100644 >--- a/fs/f2fs/file.c >+++ b/fs/f2fs/file.c >@@ -3386,18 +3386,33 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) > ret = -EAGAIN; > goto out; > } >- } else { >- preallocated = true; >- target_size = iocb->ki_pos + iov_iter_count(from); >+ goto write; >+ } > >- err = f2fs_preallocate_blocks(iocb, from); >- if (err) { >- clear_inode_flag(inode, FI_NO_PREALLOC); >- inode_unlock(inode); >- ret = err; >- goto out; >- } >+ if (is_inode_flag_set(inode, FI_NO_PREALLOC)) >+ goto write; >+ >+ if (iocb->ki_flags & IOCB_DIRECT) { >+ /* convert inline data for Direct I/O*/ >+ err = f2fs_convert_inline_inode(inode); >+ if (err) >+ goto out_err; >+ >+ if (!f2fs_force_buffered_io(inode, iocb, from)) >+ goto write; >+ } >+ preallocated = true; >+ target_size = iocb->ki_pos + iov_iter_count(from); >+ >+ err = f2fs_preallocate_blocks(iocb, from); >+ if (err) { >+out_err: >+ clear_inode_flag(inode, FI_NO_PREALLOC); >+ inode_unlock(inode); >+ ret = err; >+ goto out; > } >+write: > ret = __generic_file_write_iter(iocb, from); > clear_inode_flag(inode, FI_NO_PREALLOC); > >-- >2.19.0.605.g01d371f741-goog > This also addresses the original problem. Tested-by: Javier González