Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp891781pxb; Thu, 15 Apr 2021 08:55:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzn1llw1YpC/AQ/F9TIDHhDSomVHArUmbfP/L0ZaJnHp7GfttTmVctKXnV3nW8Z50y0zj/Z X-Received: by 2002:aa7:82cc:0:b029:213:db69:18d9 with SMTP id f12-20020aa782cc0000b0290213db6918d9mr3819695pfn.36.1618502107894; Thu, 15 Apr 2021 08:55:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618502107; cv=none; d=google.com; s=arc-20160816; b=Y7I6sUZ0XjisLqqN6/ubTk+vpMvwEHMQbFgCMaG4GJoI3472FdixKOlG/B/mXV1+mo JCIy8ErnvXJfvliZupH7aTvUkTsT16eCRUCd48t+cueQT2i34LM3jvIUPInGM+z7oCzJ CWChz71JJW1kQrNuEnZYB4YAn19lwGRRsPMZvuDjIuUpw8NRMzM3TVDmW3CVf0mSQTFU /VImSEr0EHBViZD4BcJDIJW11sak0g29Wg+/Nwuq27mMSCT9NdH6RmdwX0v/hPyKkS2N dDNmGmNup65VwkE1qwqrm3baqcL161ZwBGV/s/FrjOF5ggGbdskk59UN6IkeQgXAsOXQ S3Rw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=uJVzdXqe5NJxLSyUq25QwkWMuu2bNsf25dlpxMEzpYA=; b=Ki4jE0se05erjjcvB+3TPcKGg6zYg58rPes24euRdICEkSFelqglHTSBXNGL5jQZhs ORBcXaiMfZoc8yaR31kDhB385u9bn9Hxiwl4RlMXH8WfARGax1OeJ/POinxt9zcbToQA EkGgY/TBnRLZnsijq49KgPC4BumglTkhOgij9135z6MJt5pho3bcWdrdCf71rbZp/UWh nkE7CMIkT6n+1MCQRjElM9nVRNdQ41KJGKJFaB5z1y0LDX21WD+qSukcDrnAh89O+te+ FwzLATVQ4Se7zLhUI9A03lXuz+qaZFT975jt1sHNjivFgtgOWa3jj+bFyhws5H7dCsYd I1Og== 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 ob16si3665741pjb.149.2021.04.15.08.54.52; Thu, 15 Apr 2021 08:55:07 -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 S233330AbhDOPyp (ORCPT + 99 others); Thu, 15 Apr 2021 11:54:45 -0400 Received: from mx2.suse.de ([195.135.220.15]:49622 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231726AbhDOPyp (ORCPT ); Thu, 15 Apr 2021 11:54:45 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id E6F47B2DD; Thu, 15 Apr 2021 15:54:20 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id A527B1F2B65; Thu, 15 Apr 2021 17:54:20 +0200 (CEST) From: Jan Kara To: Ted Tso Cc: , Eric Whitney , Jan Kara , stable@vger.kernel.org Subject: [PATCH v3] ext4: Fix occasional generic/418 failure Date: Thu, 15 Apr 2021 17:54:17 +0200 Message-Id: <20210415155417.4734-1-jack@suse.cz> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org 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; } -- 2.26.2