Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2519924pxj; Mon, 10 May 2021 05:05:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzEtebanATVaFy6bdFneoN+/II/a+ZVmo282UOxs8FnLHmSUyXIS5v/1bbCvYHrjSxeiaNE X-Received: by 2002:a05:6402:750:: with SMTP id p16mr28983445edy.156.1620648335926; Mon, 10 May 2021 05:05:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620648335; cv=none; d=google.com; s=arc-20160816; b=lCctBZAaCw9Gc03496UlJpQpr3fOU0qzm60whdNNNOKpCfKf9VyggNDBzJK35dVH0j ncaCvOjUonFjUX171g0QJw4yp69x1YNQMM2gfmHL1zEbyH9WjDcWUxA84kv46jYqCkTb 0n+0xIDmNm7yNaQbVrtVg8uO84g1EMoRRDsUC3w51+swy2DlGSqa498dX57NWwQo4l7g sYJgPQcYgmUzoYEsAt133nOQeioucTEsgtoaCXL6q+7mhjysHqDyKu8/SYRLgA6CvhF9 iXH+GrAUc1Tj1gSM0S5AcvL+yC/4/dO+rwP1FdbWM3qqqGkwZ6lIF2slG5oBUPLxEI4c ddiw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=eESDqSawxpNk70psbknDl71kfq4YmeRd1vvOQWEkS6w=; b=N47M1P+iFGcLfcBM4DVsZWKgNLd8nRXG/b1cqpyBkEhqcp396ysP3AujkGpjHcwcMa Qnsc2GUTEZBVH3ZCSF58MY1MDYCWLdqpVhwzvlCuof8FHGRV+YpZBwGl9ThE1D9cDrSm +7oDMh7ilUY12MtQvOzAj65U9cN9dLr4CoYTMAZzxbVRaNi4wXsvP5PFmHwhYnz3G23r l9P9ht3u6X0UIHWD7PIDW2W6pWgHhegwvHdifhDbxutO7PmpZsb3TovfT8MkPyXk6S2T e+QYFGVqetLgHuj1jW9occXyKhxk5wf796hsKzyEcDVUN5occvul0d8/ObNNEXu9289U cutQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0CCfCN0n; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w9si12586825edq.453.2021.05.10.05.05.12; Mon, 10 May 2021 05:05:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0CCfCN0n; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243826AbhEJL50 (ORCPT + 99 others); Mon, 10 May 2021 07:57:26 -0400 Received: from mail.kernel.org ([198.145.29.99]:36378 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235744AbhEJLF7 (ORCPT ); Mon, 10 May 2021 07:05:59 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 731FC61483; Mon, 10 May 2021 10:56:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620644161; bh=DXx2Ls0t2fcGvRGK/ur0/utpc2YNDO5Cp0VN0+2jXc8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0CCfCN0nEHw0J9IbUTc1UQAv6p1u73uITrBmeDiihWvom1NAN1BbubhxaA4L8+GMf CcnSrqHSZF4x/0Xrgb0K63Ln9KA/hSJtTMDQRQZbNrOngzy9hvB/mvCO9VxUpCmXDO v0i4vjmNwTW+WtEb+KhlGuNFnC7rjC0KyvMg7pU4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jan Kara , Dave Chinner , Theodore Tso , Eric Whitney Subject: [PATCH 5.11 312/342] ext4: Fix occasional generic/418 failure Date: Mon, 10 May 2021 12:21:42 +0200 Message-Id: <20210510102020.417240571@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210510102010.096403571@linuxfoundation.org> References: <20210510102010.096403571@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jan Kara commit 5899593f51e63dde2f07c67358bd65a641585abb upstream. 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 Acked-by: Dave Chinner Link: https://lore.kernel.org/r/20210415155417.4734-1-jack@suse.cz Signed-off-by: Theodore Ts'o Signed-off-by: Greg Kroah-Hartman --- fs/ext4/file.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) --- a/fs/ext4/file.c +++ b/fs/ext4/file.c @@ -372,15 +372,32 @@ truncate: 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; }