Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp1060668pxb; Thu, 15 Apr 2021 13:00:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyQVnf/xw/siHKEuDVLZVQX50R+vj8m2nbMSw9uX4idwmzCjXyXYC5scG1e7ZBs30JQvHU2 X-Received: by 2002:a63:7c42:: with SMTP id l2mr4888310pgn.98.1618516846888; Thu, 15 Apr 2021 13:00:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618516846; cv=none; d=google.com; s=arc-20160816; b=v9HL7MPRcId1xV1khzsRwzKOy6dbH3Tuw5ocZeUCz+xqVaMCzc4s2NK4wiVsk4djts /VkE9By1bk8s2KdbRTCy9D2uKFFfPO0ljYNDWVUcKCabctxScEhulEcUYqlvat94skmc Lc6X6YSVSxqJS6bpwgPT680ng9xKmBFVon/uyxH9Yip3F0X/R7sOS3NEhaugvW7fTtGG /i57Yu83kNrG0wkFIzC6GSFiXHBiBpFMFiesu1egEXgqMgP7vjx+Q41dt3/IGcRaHj7t a1D0Ew8q4drvMGIFexPP7AmwHq309eHESum5wIOaNAAeCG65ph8aoPisiacrruounBki WRxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=awd1ERb/pc00DKswh6Utn9qSQ60M4TGoVLVADWukN/g=; b=kfzmOVQ2xI6WI25M28d28w+WXs6epPN8bBnikUUFJiAVMAKxGKDQc3d8Ab+Jwfs2KV M+pxzb2/vsl8KlmdoIfXpBroXUP8QGU7uqghChmJcVfsY846Rfv5qTArN3CMjPzLPCJM HiPmueoyORRKeofhLbbRFJmswOFNiJVAPROICqz149fUPYQ19hz91/pmZ7pBVm5aCZI+ fwR3KsJhiu5jCjiNQLV9RPSAFrREwi6SWgHgxh+x4h7U9lk+gV4fwtldsuklmtRr/bCp EX4ijw5ZNH3+z/XcR/f8CjaZTjpE3yW1RWERsbkSIOfdstyr3gVeFsxXh+eKeq1YP8kz yWHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=FJ7GPf6V; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f10si4633377pgv.418.2021.04.15.13.00.30; Thu, 15 Apr 2021 13:00:46 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=FJ7GPf6V; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235210AbhDOT7S (ORCPT + 99 others); Thu, 15 Apr 2021 15:59:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235142AbhDOT7R (ORCPT ); Thu, 15 Apr 2021 15:59:17 -0400 Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0629DC061574; Thu, 15 Apr 2021 12:58:54 -0700 (PDT) Received: by mail-qv1-xf29.google.com with SMTP id x27so12359494qvd.2; Thu, 15 Apr 2021 12:58:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=awd1ERb/pc00DKswh6Utn9qSQ60M4TGoVLVADWukN/g=; b=FJ7GPf6VVf6evCpoJQqwk0xsizsVxZoo6SXBcFPMGQdAFIwuGVyHRKw61m03BKODMw sxAhrnoQR8Li4AfWZzi+aWZyof3nE8xIx7egtpHW6bUrpoKf2uujv//fW9SOlpc7KR7c kLg/J7BKlvfZ+3Buqdbxm3tf1YhKjBPDsSg3ZfxCxRJUsq3Pwa6nTdJaPRL2EjgzSVZi bYRE/OP4gEEViYg2Tx6OWuAZ89muXOPuCNhNIkNlg/3ftZg888/tmDWug7VDjbV5TkKY rJhtaLXDzWou+CzvZzpjFLdh86VkYcn7Ov5lenVk8zOvZQi5vICiUbAq3YIsIiKhXVLa n2Ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=awd1ERb/pc00DKswh6Utn9qSQ60M4TGoVLVADWukN/g=; b=lh0Ly/l5l8OuXmPFIg4jwbKueZR7CWFKKmRgxBEOOGMnsm8Z1CQ4owCld1109K47eh FFI9gpmA+oARtlz7sM9zBYFFKt4Dr/wdi+jPzzhl2dbwB4zju285cwJ1Cxz7kp+qaQ9/ TqKAO8GAsckqHAOsjfOoBHwSmAZse6hPkezbH7uLBrHaKUoQ5PuDARh72p9XiZ+meMCf NbRNaWcphATj4DtifG/UqTJvkfnt9cMFEg5+UdrSwEazD6SVTBigPch9rud/EpvsGnrc uUU1G51ixYchcypuTbI80L+kf/EGYptX0y8Y2KoBxKRaigiqgh0JSo+tM3sdnJLxyIbS ICQQ== X-Gm-Message-State: AOAM530NYMIyenzoA9eea52QGoe39bmaITHYEzDcW7GMRYp0qsMoFvLl YeouwHLa7SPoe2dmm7871dw= X-Received: by 2002:a05:6214:165:: with SMTP id y5mr4876742qvs.59.1618516733305; Thu, 15 Apr 2021 12:58:53 -0700 (PDT) Received: from localhost.localdomain (c-73-60-226-25.hsd1.nh.comcast.net. [73.60.226.25]) by smtp.gmail.com with ESMTPSA id q15sm2364377qtx.47.2021.04.15.12.58.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 12:58:52 -0700 (PDT) Date: Thu, 15 Apr 2021 15:58:50 -0400 From: Eric Whitney 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: <20210415195850.GA16226@localhost.localdomain> 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> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org * Jan Kara : > 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 > With additional data, the generic/068 failure I saw on the data=journal test configuration when testing the v2 version of this patch doesn't appear to be a regression. 100 runs of 068 on a v2 patched -rc7 kernel failed five times, and it failed three times on an unpatched -rc7 kernel. So, that failure is most likely a latent problem unrelated to this patch. Thanks, Eric