Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp1138181pxb; Thu, 15 Apr 2021 15:17:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz3uTT1TPQD4CuD3tJEMAcbe5eHS3WOjgy70gpl2YmOzAj0fcr/vctnAyKPNW7MfmsjeN+A X-Received: by 2002:a63:ab05:: with SMTP id p5mr5338988pgf.149.1618525035058; Thu, 15 Apr 2021 15:17:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618525035; cv=none; d=google.com; s=arc-20160816; b=RuVv+AfmTiXZN20cqXGy0+kN/IVAdHJCOMytupq1dVLQFQU9I/yIEQKM1n8jt7p3Dp OqH0mseY5Tq27OYJZMbSaAcQJiUrC8oy7gKlJ/uKcNnsZccH+H7ovmpI26SSve00Q10y c3LrgcINoVYuZAbmoo7l+cc7o2PN6cSkO7FRo6jK5hWKlQ3IiXn+tZDBXa0XGsbM4KXU yG2pSw7aTiCU8xTLO8rLtDKMYTzoTKPLYGAx4XrHNYQwxHG+DwNID4oMjZAOJAWnRaXx PgKexP/ol/rnMcFhFNFaxq4QgUoA2bGARZzMViA+ean5/xyP9gre41m+hh/faGTtEKfI M2Cg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=BEkm3q2RbY7fGXdSuS7vsyxeP1a0lV/3urPNWPZtMWY=; b=dbQWhZFgAJAJaJOZamdk0b31sxQhO+R//2LJBVE5gw09W+4yqQwEhxSA1kONW2yMxw x5CQ+Q8QKd436V1K1uBk7E6MRUCQLiGp1VpFE4hsTHrVenbRzTxgl9K5VIuVUcOsaWg0 wzs9ic3SGSlLWRP9OzgakaSgbZF7O6eC5uI1nJoD59senotdbjOs9C2EjXDBx0c8lefg ibfxJB6QIWBOzaPd9Ov713iZFiHVvja6oLhNU7LT1Pm7i7k4HMLtrgl3VyPOLK+L02QY ANuRVuyRDCfWJsMMjIue5le4uiBmJSdHOdObmFQZcJzazVBEahprR1gGFCLXrmtz9LSS AGhg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a1si5081222pgw.574.2021.04.15.15.16.55; Thu, 15 Apr 2021 15:17:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234716AbhDOWBx (ORCPT + 99 others); Thu, 15 Apr 2021 18:01:53 -0400 Received: from mail108.syd.optusnet.com.au ([211.29.132.59]:37477 "EHLO mail108.syd.optusnet.com.au" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234777AbhDOWBx (ORCPT ); Thu, 15 Apr 2021 18:01:53 -0400 Received: from dread.disaster.area (pa49-181-239-12.pa.nsw.optusnet.com.au [49.181.239.12]) by mail108.syd.optusnet.com.au (Postfix) with ESMTPS id 376301AEBCB; Fri, 16 Apr 2021 08:01:24 +1000 (AEST) Received: from dave by dread.disaster.area with local (Exim 4.92.3) (envelope-from ) id 1lXA37-009m6A-H1; Fri, 16 Apr 2021 08:01:21 +1000 Date: Fri, 16 Apr 2021 08:01:21 +1000 From: Dave Chinner To: Jan Kara Cc: Ted Tso , linux-ext4@vger.kernel.org, Eric Whitney , stable@vger.kernel.org Subject: Re: [PATCH v3] ext4: Fix occasional generic/418 failure Message-ID: <20210415220121.GX1990290@dread.disaster.area> References: <20210415155417.4734-1-jack@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210415155417.4734-1-jack@suse.cz> X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.3 cv=F8MpiZpN c=1 sm=1 tr=0 cx=a_idp_f a=gO82wUwQTSpaJfP49aMSow==:117 a=gO82wUwQTSpaJfP49aMSow==:17 a=kj9zAlcOel0A:10 a=3YhXtTcJ-WEA:10 a=pGLkceISAAAA:8 a=VwQbUJbxAAAA:8 a=20KFwNOVAAAA:8 a=7-415B0cAAAA:8 a=BVGimuPS_KFzXVpvEk4A:9 a=CjuIK1q_8ugA:10 a=HUqATDVKn4QA:10 a=AjGcO6oz07-iQ99wixmX:22 a=biEYGPWJfzWAr4FL6Ov7:22 Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Thu, Apr 15, 2021 at 05:54:17PM +0200, Jan Kara wrote: > Eric has noticed that after pagecache read rework, generic/418 is > occasionally failing for ext4 when blocksize < pagesize. In fact, the > pagecache rework just made hard to hit race in ext4 more likely. The > problem is that since ext4 conversion of direct IO writes to iomap > framework (commit 378f32bab371), we update inode size after direct IO > write only after invalidating page cache. Thus if buffered read sneaks > at unfortunate moment like: > > CPU1 - write at offset 1k CPU2 - read from offset 0 > iomap_dio_rw(..., IOMAP_DIO_FORCE_WAIT); > ext4_readpage(); > ext4_handle_inode_extension() > > the read will zero out tail of the page as it still sees smaller inode > size and thus page cache becomes inconsistent with on-disk contents with > all the consequences. > > Fix the problem by moving inode size update into end_io handler which > gets called before the page cache is invalidated. > > Reported-and-tested-by: Eric Whitney > Fixes: 378f32bab371 ("ext4: introduce direct I/O write using iomap infrastructure") > CC: stable@vger.kernel.org > Signed-off-by: Jan Kara > --- > fs/ext4/file.c | 25 +++++++++++++++++++++---- > 1 file changed, 21 insertions(+), 4 deletions(-) > > diff --git a/fs/ext4/file.c b/fs/ext4/file.c > index 194f5d00fa32..7924634ab0bf 100644 > --- a/fs/ext4/file.c > +++ b/fs/ext4/file.c > @@ -371,15 +371,32 @@ static ssize_t ext4_handle_inode_extension(struct inode *inode, loff_t offset, > static int ext4_dio_write_end_io(struct kiocb *iocb, ssize_t size, > int error, unsigned int flags) > { > - loff_t offset = iocb->ki_pos; > + loff_t pos = iocb->ki_pos; > struct inode *inode = file_inode(iocb->ki_filp); > > if (error) > return error; > > - if (size && flags & IOMAP_DIO_UNWRITTEN) > - return ext4_convert_unwritten_extents(NULL, inode, > - offset, size); > + if (size && flags & IOMAP_DIO_UNWRITTEN) { > + error = ext4_convert_unwritten_extents(NULL, inode, pos, size); > + if (error < 0) > + return error; > + } > + /* > + * If we are extending the file, we have to update i_size here before > + * page cache gets invalidated in iomap_dio_rw(). Otherwise racing > + * buffered reads could zero out too much from page cache pages. Update > + * of on-disk size will happen later in ext4_dio_write_iter() where > + * we have enough information to also perform orphan list handling etc. > + * Note that we perform all extending writes synchronously under > + * i_rwsem held exclusively so i_size update is safe here in that case. > + * If the write was not extending, we cannot see pos > i_size here > + * because operations reducing i_size like truncate wait for all > + * outstanding DIO before updating i_size. > + */ > + pos += size; > + if (pos > i_size_read(inode)) > + i_size_write(inode, pos); > > return 0; > } Thanks for updating the comment, Jan! Acked-by: Dave Chinner Cheers, Dave. -- Dave Chinner david@fromorbit.com