Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp417249pxb; Wed, 14 Apr 2021 19:49:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwPAbAcoynZzrHn86xeaKcsYLpswIE/w9JVJZb2wchvRZaf5R9dMq9pqQjYAKw+E0lYZR+t X-Received: by 2002:a17:90a:94c4:: with SMTP id j4mr1301604pjw.14.1618454992524; Wed, 14 Apr 2021 19:49:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618454992; cv=none; d=google.com; s=arc-20160816; b=DgsZ2LD/L8GT2jo2080nYpvJe65W8RT5brNavb5Zscp3MTpjtgQtjTtRtbYpzD+lsQ JGgXAuGQCry6lpPdv2nXyxkFfsCBpd0EFkbatgd91mfxwv86Ok8LF3ZYBSWFLxfoyz5H Q+TUC644dE8w531A25sRGyKsMAblGUdL/OyfRnTo3JxkOSdOV8EP+PkwEqawY4AoqRZc kswyE2mpkDhFizovrHbnr6MGH7gv1NYiFQAL++P1GxnFnjjeD2rMOU41ttpWUWeEolM9 Kj7t4Q6n61PlWgd1R8aEbPago6rGO0Ss+++C+kxdfvWbCr3VtnkSOGk7d+tuyfdZXLJG 1cMg== 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=0zUeiH06mBKSrliw6cEQYUGYKxzM84XwKgiVle5jUn8=; b=W3UDdmwjIRjJp6JaJtmughsJ4JsnYfyyvRNpGSz1AEPfYVMu+MVy+7thfaab6XBVtn eNY70FGBB0utlRFn/4Ho8NOpA9nEZBZOrMcnLXEmSmPEm5w6x/Dvd/Wmcteu/yrTTL9Q SCZLm52VdB6hVjD7aWmHkIGe37qSnFhc2xRy2IOmOEjUu/D4arNFW8hpGPFwd5dpPaSs GBoUY+nk9i8rFnRMmrRVcMk9FlefgGjtKQMBMf+0QNK2iOZkNN7uaR40o8U+t/GgOW4k rxp69CDGOJ/NTnA1AFmZjB53KFJYrZtHy3IwSKNoLRmlXEUyWu3q2UDChzhmanCmIdmj dZZQ== 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 w6si1645132plp.140.2021.04.14.19.49.31; Wed, 14 Apr 2021 19:49:52 -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 S229618AbhDOCrw (ORCPT + 99 others); Wed, 14 Apr 2021 22:47:52 -0400 Received: from mail104.syd.optusnet.com.au ([211.29.132.246]:38463 "EHLO mail104.syd.optusnet.com.au" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229475AbhDOCrv (ORCPT ); Wed, 14 Apr 2021 22:47:51 -0400 Received: from dread.disaster.area (pa49-181-239-12.pa.nsw.optusnet.com.au [49.181.239.12]) by mail104.syd.optusnet.com.au (Postfix) with ESMTPS id 9234282FB37; Thu, 15 Apr 2021 12:47:27 +1000 (AEST) Received: from dave by dread.disaster.area with local (Exim 4.92.3) (envelope-from ) id 1lWs2O-008X2l-84; Thu, 15 Apr 2021 12:47:24 +1000 Date: Thu, 15 Apr 2021 12:47:24 +1000 From: Dave Chinner To: Jan Kara Cc: Ted Tso , linux-ext4@vger.kernel.org, Eric Whitney , stable@vger.kernel.org Subject: Re: [PATCH v2] ext4: Fix occasional generic/418 failure Message-ID: <20210415024724.GV1990290@dread.disaster.area> References: <20210414131453.4945-1-jack@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210414131453.4945-1-jack@suse.cz> X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.3 cv=Tu+Yewfh 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=7-415B0cAAAA:8 a=gV7zzQzcnHcuDfqADMcA: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 Wed, Apr 14, 2021 at 03:14:53PM +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-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 | 20 ++++++++++++++++---- > 1 file changed, 16 insertions(+), 4 deletions(-) > > Eric, can you please try whether this patch fixes the failures you are > occasionally seeing? > > Changes since v1: > * Rewritten the fix to avoid the need for separate transaction handle for > orphan list update > > diff --git a/fs/ext4/file.c b/fs/ext4/file.c > index 194f5d00fa32..be1e80af61be 100644 > --- a/fs/ext4/file.c > +++ b/fs/ext4/file.c > @@ -371,15 +371,27 @@ 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. > + */ > + pos += size; > + if (pos > i_size_read(inode)) > + i_size_write(inode, pos); Might be worth explaining why this doesn't require locking to prevent racing completions from updating the inode size and potentially losing an EOF update. I know why but it might not be so obvious to others (DIO extending writes are serialised at submission in ext4) but it's probably worth having a comment similar to the one in xfs_dio_write_end_io() that explains why XFS needs locking. Cheers, Dave. -- Dave Chinner david@fromorbit.com