From patchwork Wed Mar 1 16:37:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eryu Guan X-Patchwork-Id: 9598823 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 99764600CB for ; Wed, 1 Mar 2017 16:47:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8BD0C28445 for ; Wed, 1 Mar 2017 16:47:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7EFE828490; Wed, 1 Mar 2017 16:47:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 136C928445 for ; Wed, 1 Mar 2017 16:47:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750704AbdCAQr2 (ORCPT ); Wed, 1 Mar 2017 11:47:28 -0500 Received: from mail-pg0-f66.google.com ([74.125.83.66]:35473 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751291AbdCAQrX (ORCPT ); Wed, 1 Mar 2017 11:47:23 -0500 Received: by mail-pg0-f66.google.com with SMTP id 1so6397100pgz.2; Wed, 01 Mar 2017 08:47:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=G1B3RN6GuxZ6LNQNZrjhuCmFihB0g9e61WF+3EejBzM=; b=G2UEI2/20R/4lqItny/9GpHK1RDwJUzTPkbcPZXtbk3prvv+uGJnM31HukNNr7CPNW rit6l+4XDEC+zJdhYzFiR6lN4Gb7LLHdFFwNZf3zqRfdHIwb3qm2eIwJW51umHVU1I+d ZfMWnKpSemWsuKJXS58v0wWIo45mRi+6fIfX8divcEDBVUBz4al5peoVG5v9+gyaHhMJ kk4mt0lD5r2U3nc7ZwnUn3T7xCQs681ajB3Lsiip7kJBMeUt/dkizUzamfOadK+rYxzj fsP0uQ6AEo/ZTK3U9+SlaBUQfqahjMbswyJOXEwOUsG1KvCPmHgZGI+I1U1eQ4eoeVeF 2hAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=G1B3RN6GuxZ6LNQNZrjhuCmFihB0g9e61WF+3EejBzM=; b=Q77btNwU5sTBUxNCYKzMcRS2LV0I3/zQLKwWlAfn3h7UkUou61l1+vJkx/XeriWEjs lV6+ErVllsSv9iRqNnAM9UxzgmeTrSBUTOGkTD5+8CeTlpsXqEY6PF36iWtgnGJpOIut dpX0gMmtwiVW7mTh4IdFucnEKpFetMjsXmB9kEbY+2IQweN1DxSh0QbhRvQsAQuPrB47 HmGZ5V4j9Ivuje1YKKKjMTgwhCAALpPgv3mjidd8M7h1t7ocnp8xePVtOSepLe8NtiUb smX41r0A9lMOuhJzlFAxvuW8emqdJ73kW5HUffBv207WWUpLCBw4ZgeVjr9GWStS7/m+ zsyg== X-Gm-Message-State: AMke39lYIWabRZS75RRXcmMqYehjY6/yystCNczghByUXKbMa7qoMfntIvK1TYwKoC5zZA== X-Received: by 10.84.138.129 with SMTP id 1mr11524180plp.37.1488386292931; Wed, 01 Mar 2017 08:38:12 -0800 (PST) Received: from localhost ([128.199.137.77]) by smtp.gmail.com with ESMTPSA id i15sm11634599pfj.0.2017.03.01.08.38.12 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 01 Mar 2017 08:38:12 -0800 (PST) From: Eryu Guan To: linux-fsdevel@vger.kernel.org Cc: linux-xfs@vger.kernel.org, hch@lst.de, Eryu Guan Subject: [PATCH v2] iomap: invalidate page caches should be after iomap_dio_complete() in direct write Date: Thu, 2 Mar 2017 00:37:08 +0800 Message-Id: <20170301163708.7002-1-guaneryu@gmail.com> X-Mailer: git-send-email 2.9.3 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eryu Guan After XFS switching to iomap based DIO (commit acdda3aae146 ("xfs: use iomap_dio_rw")), I started to notice dio29/dio30 tests failures from LTP run on ppc64 hosts, and they can be reproduced on x86_64 hosts with 512B/1k block size XFS too. dio29 diotest3 -b 65536 -n 100 -i 1000 -o 1024000 dio30 diotest6 -b 65536 -n 100 -i 1000 -o 1024000 The failure message is like: bufcmp: offset 0: Expected: 0x62, got 0x0 diotest03 1 TPASS : Read with Direct IO, Write without diotest03 2 TFAIL : diotest3.c:142: comparsion failed; child=98 offset=1425408 diotest03 3 TFAIL : diotest3.c:194: Write Direct-child 98 failed Direct write wrote 0x62 but buffer read got zero. This is because, when doing direct write to a hole or preallocated file, we invalidate the page caches before converting the extent from unwritten state to normal state, which is done by iomap_dio_complete(), thus leave a window for other buffer reader to cache the unwritten state extent. Consider this case, with sub-page blocksize XFS, two processes are direct writing to different blocksize-aligned regions (say 512B) of the same preallocated file, and reading the region back via buffered I/O to compare contents. process A, region [0,512] process B, region [512,1024] xfs_file_write_iter xfs_file_aio_dio_write iomap_dio_rw iomap_apply invalidate_inode_pages2_range xfs_file_write_iter xfs_file_aio_dio_write iomap_dio_rw iomap_apply invalidate_inode_pages2_range iomap_dio_complete xfs_file_read_iter xfs_file_buffered_aio_read generic_file_read_iter do_generic_file_read iomap_dio_complete xfs_file_read_iter Process A first invalidates page caches, at this point the underlying extent is still in unwritten state (iomap_dio_complete not called yet), and process B finishs direct write and populates page caches via readahead, which caches zeros in page for region A, then process A reads zeros from page cache, instead of the actual data. Fix it by invalidating page caches after converting unwritten extent to make sure we read content from disk after extent state changed, as what we did before switching to iomap based dio. Also introduce a new 'start' variable to save the original write offset (iomap_dio_complete() updates iocb->ki_pos), and a 'err' variable for invalidating caches result, cause we can't reuse 'ret' anymore. Signed-off-by: Eryu Guan Reviewed-by: Christoph Hellwig --- v2: - kill global 'res' var and use local var in if block with better name fs/iomap.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/fs/iomap.c b/fs/iomap.c index 0f85f24..864ad17 100644 --- a/fs/iomap.c +++ b/fs/iomap.c @@ -844,7 +844,8 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, struct address_space *mapping = iocb->ki_filp->f_mapping; struct inode *inode = file_inode(iocb->ki_filp); size_t count = iov_iter_count(iter); - loff_t pos = iocb->ki_pos, end = iocb->ki_pos + count - 1, ret = 0; + loff_t pos = iocb->ki_pos, start = pos; + loff_t end = iocb->ki_pos + count - 1, ret = 0; unsigned int flags = IOMAP_DIRECT; struct blk_plug plug; struct iomap_dio *dio; @@ -885,12 +886,12 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, } if (mapping->nrpages) { - ret = filemap_write_and_wait_range(mapping, iocb->ki_pos, end); + ret = filemap_write_and_wait_range(mapping, start, end); if (ret) goto out_free_dio; ret = invalidate_inode_pages2_range(mapping, - iocb->ki_pos >> PAGE_SHIFT, end >> PAGE_SHIFT); + start >> PAGE_SHIFT, end >> PAGE_SHIFT); WARN_ON_ONCE(ret); ret = 0; } @@ -939,6 +940,8 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, __set_current_state(TASK_RUNNING); } + ret = iomap_dio_complete(dio); + /* * Try again to invalidate clean pages which might have been cached by * non-direct readahead, or faulted in by get_user_pages() if the source @@ -947,12 +950,12 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, * this invalidation fails, tough, the write still worked... */ if (iov_iter_rw(iter) == WRITE && mapping->nrpages) { - ret = invalidate_inode_pages2_range(mapping, - iocb->ki_pos >> PAGE_SHIFT, end >> PAGE_SHIFT); - WARN_ON_ONCE(ret); + int err = invalidate_inode_pages2_range(mapping, + start >> PAGE_SHIFT, end >> PAGE_SHIFT); + WARN_ON_ONCE(err); } - return iomap_dio_complete(dio); + return ret; out_free_dio: kfree(dio);