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 X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4CE89C4338F for ; Fri, 6 Aug 2021 23:49:15 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DD66C6113B for ; Fri, 6 Aug 2021 23:49:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org DD66C6113B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=fromorbit.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id DC1D08D0002; Fri, 6 Aug 2021 19:49:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D712D8D0001; Fri, 6 Aug 2021 19:49:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C86B38D0002; Fri, 6 Aug 2021 19:49:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0034.hostedemail.com [216.40.44.34]) by kanga.kvack.org (Postfix) with ESMTP id AE3048D0001 for ; Fri, 6 Aug 2021 19:49:13 -0400 (EDT) Received: from smtpin37.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 35A10180CC767 for ; Fri, 6 Aug 2021 23:49:13 +0000 (UTC) X-FDA: 78446299386.37.B174E15 Received: from mail107.syd.optusnet.com.au (mail107.syd.optusnet.com.au [211.29.132.53]) by imf14.hostedemail.com (Postfix) with ESMTP id 44B626018101 for ; Fri, 6 Aug 2021 23:49:12 +0000 (UTC) Received: from dread.disaster.area (pa49-195-182-146.pa.nsw.optusnet.com.au [49.195.182.146]) by mail107.syd.optusnet.com.au (Postfix) with ESMTPS id 629C51142E81; Sat, 7 Aug 2021 09:49:09 +1000 (AEST) Received: from dave by dread.disaster.area with local (Exim 4.92.3) (envelope-from ) id 1mC9aO-00FLzu-AW; Sat, 07 Aug 2021 09:49:08 +1000 Date: Sat, 7 Aug 2021 09:49:08 +1000 From: Dave Chinner To: Pavel Begunkov Cc: Andrew Morton , linux-mm@kvack.org, Alexander Viro , linux-fsdevel@vger.kernel.org, Jens Axboe , linux-kernel@vger.kernel.org Subject: Re: [RFC] mm: optimise generic_file_read_iter Message-ID: <20210806234908.GC2566745@dread.disaster.area> References: <07bd408d6cad95166b776911823b40044160b434.1628248975.git.asml.silence@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <07bd408d6cad95166b776911823b40044160b434.1628248975.git.asml.silence@gmail.com> X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.3 cv=F8MpiZpN c=1 sm=1 tr=0 a=QpfB3wCSrn/dqEBSktpwZQ==:117 a=QpfB3wCSrn/dqEBSktpwZQ==:17 a=kj9zAlcOel0A:10 a=MhDmnRu9jo8A:10 a=pGLkceISAAAA:8 a=7-415B0cAAAA:8 a=0ShQZFDq3J1pPZILnEgA:9 a=CjuIK1q_8ugA:10 a=biEYGPWJfzWAr4FL6Ov7:22 Authentication-Results: imf14.hostedemail.com; dkim=none; spf=none (imf14.hostedemail.com: domain of david@fromorbit.com has no SPF policy when checking 211.29.132.53) smtp.mailfrom=david@fromorbit.com; dmarc=none X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 44B626018101 X-Stat-Signature: 396hmnm3keagtfqwzob1yig3pmsrhzks X-HE-Tag: 1628293752-578716 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: On Fri, Aug 06, 2021 at 12:42:43PM +0100, Pavel Begunkov wrote: > Unless direct I/O path of generic_file_read_iter() ended up with an > error or a short read, it doesn't use inode. So, load inode and size > later, only when they're needed. This cuts two memory reads and also > imrpoves code generation, e.g. loads from stack. > > Signed-off-by: Pavel Begunkov > --- > > NOTE: as a side effect, it reads inode->i_size after ->direct_IO(), and > I'm not sure whether that's valid, so would be great to get feedback > from someone who knows better. I can see that it changes behaviour in a very subtle way. It depends on what each individual filesystem does with direct IO as to whether this may introduce potential data coherency/corruption issues, so I can't say that it's a safe change. It doesn't affect XFS, because XFS doesn't do direct IO through generic_file_read_iter(). Fundamentally, the issue is that ->direct_IO() can race with inode size extensions due to write IO completions while the read IO is in flight. > mm/filemap.c | 10 +++++----- > 1 file changed, 5 insertions(+), 5 deletions(-) > > diff --git a/mm/filemap.c b/mm/filemap.c > index d1458ecf2f51..0030c454ec35 100644 > --- a/mm/filemap.c > +++ b/mm/filemap.c > @@ -2658,10 +2658,8 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) > if (iocb->ki_flags & IOCB_DIRECT) { > struct file *file = iocb->ki_filp; > struct address_space *mapping = file->f_mapping; > - struct inode *inode = mapping->host; > - loff_t size; > + struct inode *inode; > > - size = i_size_read(inode); > if (iocb->ki_flags & IOCB_NOWAIT) { > if (filemap_range_needs_writeback(mapping, iocb->ki_pos, > iocb->ki_pos + count - 1)) > @@ -2693,8 +2691,10 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) > * the rest of the read. Buffered reads will not work for > * DAX files, so don't bother trying. > */ > - if (retval < 0 || !count || iocb->ki_pos >= size || > - IS_DAX(inode)) Hence this check in the current code is determining if the IO file offset *after* the IO completed is at or beyond the EOF *before the IO was started*. i.e. it always detects a short read, because the EOF can only ascend while a DIO is in progress - truncation cannot run concurrently with DIO reads. Hence if we get less bytes read than we ask for, and we are beyond the EOF we sampled at the start of the IO, we know for certain we got a short read and we drop out without going through the buffered read path. > + if (retval < 0 || !count) > + return retval; > + inode = mapping->host; > + if (iocb->ki_pos >= i_size_read(inode) || IS_DAX(inode)) > return retval; This changes the check to read the inode size after the read IO completed. This means the IO could have raced with size extensions from other concurrent DIO writes (or even racing buffered IO writeback), so despite getting less bytes than we asked for, we won't detect it as a short DIO read. Hence we now fall through to the buffered read path. So at minimum, this is a _very subtle_ change of behaviour in the direct IO code, resulting in short reads at EOF now sometimes falling through to the buffered IO path where they never did before. It may not be an issue but per-filesystem audits will be needed to determine that.... Cheers, Dave. -- Dave Chinner david@fromorbit.com