From patchwork Mon Apr 24 05:49:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 86849 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2524496vqo; Sun, 23 Apr 2023 22:51:26 -0700 (PDT) X-Google-Smtp-Source: AKy350YmGtOJS+U6H48FE9OfwGt51RYTjTqvvRpk5K3sbeuvZRRIresf5GmnXUcHkFeyTAh0TPH7 X-Received: by 2002:a17:902:f68b:b0:1a6:48e6:ea7c with SMTP id l11-20020a170902f68b00b001a648e6ea7cmr15282005plg.6.1682315486309; Sun, 23 Apr 2023 22:51:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682315486; cv=none; d=google.com; s=arc-20160816; b=vcjSsqolp1A/L8VcAEH67KqzdAksrCKZtJ0NvLPA91mn1EnP4F3qGjP58bMuDeOlh0 D+tyst7TfsjHQypYqip7dO4MdtCxn7D+kyADfZ91B3c2vWuc+F8/CqnDsZ70o05pIYPb jYOz2FX9tbXsvoQvDq2pGOl5SL3CzQGOksKbQaaK9YMozePa8TFXhEhxYxBdddi0cd2d U/KMESTenGQzuZNEodwzXnLHcy0CUKrd4Lv+0CHbS4KdvXFTeo2lqUgyIBP62M8F25gl rfp4G1J4DqUWyxRAac7ruRzK9Ho07JLHtETUgUOzH6p7BYJHjhyNYoqIFTwp+oIpEqVk Yg3w== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=i8BBYqYTYYknSfjW0NZ6ikX9GZ70UEDP1VbXwkZOOdg=; b=yzZD80NQe+gMyNPkFeXQXuGc9wjyDuuT73/l/AA2m4beyxEDnWq6Tx0X0G0sCL6kEB RC6EJlnrtp1btzacFNoq5mAIhpT5kQWTwJqOGRJcsptr0moaVbyJISRFBdPDroPCcAiH ZMHq7uIwkqjQlNdCHPWCVIdljAo3Hn0epOGXnYlE/jGVsurf1fdxe9p9R8QfcTGH7IIT ZfR26Y/aLFfXgbzxxg+EZXgYcCFcw2c1ak7y4uMAbdSNqR+HZ1oi2Cr0+XOAwU72JQo6 znDgCGLGW3XzVUvhl5tayBrsNiTGKGmMGHdMrcY7sdd/ENkJfjMxVitAGkm5xygYU7Xs NcNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=uebmB2Tr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u9-20020a170903124900b001a979141a25si1721499plh.243.2023.04.23.22.51.14; Sun, 23 Apr 2023 22:51:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=uebmB2Tr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231247AbjDXFuS (ORCPT + 99 others); Mon, 24 Apr 2023 01:50:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231190AbjDXFuE (ORCPT ); Mon, 24 Apr 2023 01:50:04 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9381940D9; Sun, 23 Apr 2023 22:49:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=i8BBYqYTYYknSfjW0NZ6ikX9GZ70UEDP1VbXwkZOOdg=; b=uebmB2TrdTF1zXHpxlQ4zmX9p7 OE6/jCrxtNpUSsm3oRanJdH9cjwZyFUIVGkLL3HYIYEywM7CWJS6u1SGHCJ+9nmF+IYsXgZypSalJ qySPa4U+nhr2t5BJFjqYAaRNRBDkR3XXH0rzwvnUK69Rxok4xokOYbpVylefv6AGEw3Xqz4XFhj93 mAJQ63xk8LERHrGP4/hp4F1dnTZ+EkYcEImpKJG/Ve4SPd0ElXFB7R7aRuKRQ8jMol/kzP9KAq1at GxurAMdHe8eQJ7AZtyAbdJg5xYLyMe1YbYIbp1uWOlFDuYVBmi1q6cEUcjG09cqN3xYIkIvgoG0z6 iOJ344gA==; Received: from [2001:4bb8:189:a74f:e8a5:5f73:6d2:23b8] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1pqp5F-00FOxj-2z; Mon, 24 Apr 2023 05:49:54 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Miklos Szeredi , "Darrick J. Wong" , Andrew Morton , David Howells , Matthew Wilcox , linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, ceph-devel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, cluster-devel@redhat.com, linux-xfs@vger.kernel.org, linux-nfs@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 08/17] filemap: add a kiocb_invalidate_post_write helper Date: Mon, 24 Apr 2023 07:49:17 +0200 Message-Id: <20230424054926.26927-9-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230424054926.26927-1-hch@lst.de> References: <20230424054926.26927-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764035643347062188?= X-GMAIL-MSGID: =?utf-8?q?1764035643347062188?= Add a helper to invalidate page cache after a dio write. Signed-off-by: Christoph Hellwig --- fs/direct-io.c | 10 ++-------- fs/iomap/direct-io.c | 12 ++---------- include/linux/fs.h | 5 ----- include/linux/pagemap.h | 1 + mm/filemap.c | 37 ++++++++++++++++++++----------------- 5 files changed, 25 insertions(+), 40 deletions(-) diff --git a/fs/direct-io.c b/fs/direct-io.c index ab0d7ea89813a6..f2d2e544c259dc 100644 --- a/fs/direct-io.c +++ b/fs/direct-io.c @@ -286,14 +286,8 @@ static ssize_t dio_complete(struct dio *dio, ssize_t ret, unsigned int flags) * zeros from unwritten extents. */ if (flags & DIO_COMPLETE_INVALIDATE && - ret > 0 && dio_op == REQ_OP_WRITE && - dio->inode->i_mapping->nrpages) { - err = invalidate_inode_pages2_range(dio->inode->i_mapping, - offset >> PAGE_SHIFT, - (offset + ret - 1) >> PAGE_SHIFT); - if (err) - dio_warn_stale_pagecache(dio->iocb->ki_filp); - } + ret > 0 && dio_op == REQ_OP_WRITE) + kiocb_invalidate_post_write(dio->iocb, ret); inode_dio_end(dio->inode); diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c index f771001574d008..2bf8d684675615 100644 --- a/fs/iomap/direct-io.c +++ b/fs/iomap/direct-io.c @@ -81,7 +81,6 @@ ssize_t iomap_dio_complete(struct iomap_dio *dio) { const struct iomap_dio_ops *dops = dio->dops; struct kiocb *iocb = dio->iocb; - struct inode *inode = file_inode(iocb->ki_filp); loff_t offset = iocb->ki_pos; ssize_t ret = dio->error; @@ -109,15 +108,8 @@ ssize_t iomap_dio_complete(struct iomap_dio *dio) * ->end_io() when necessary, otherwise a racing buffer read would cache * zeros from unwritten extents. */ - if (!dio->error && dio->size && - (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) { - int err; - err = invalidate_inode_pages2_range(inode->i_mapping, - offset >> PAGE_SHIFT, - (offset + dio->size - 1) >> PAGE_SHIFT); - if (err) - dio_warn_stale_pagecache(iocb->ki_filp); - } + if (!dio->error && dio->size && (dio->flags & IOMAP_DIO_WRITE)) + kiocb_invalidate_post_write(iocb, dio->size); inode_dio_end(file_inode(iocb->ki_filp)); /* diff --git a/include/linux/fs.h b/include/linux/fs.h index c85916e9f7db50..b8ca376e606255 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2818,11 +2818,6 @@ static inline void inode_dio_end(struct inode *inode) wake_up_bit(&inode->i_state, __I_DIO_WAKEUP); } -/* - * Warn about a page cache invalidation failure diring a direct I/O write. - */ -void dio_warn_stale_pagecache(struct file *filp); - extern void inode_set_flags(struct inode *inode, unsigned int flags, unsigned int mask); diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index f6c5ef9d3d7646..38d767499daa1a 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -31,6 +31,7 @@ int invalidate_inode_pages2(struct address_space *mapping); int invalidate_inode_pages2_range(struct address_space *mapping, pgoff_t start, pgoff_t end); int kiocb_invalidate_pages(struct kiocb *iocb, size_t count); +void kiocb_invalidate_post_write(struct kiocb *iocb, size_t count); int write_inode_now(struct inode *, int sync); int filemap_fdatawrite(struct address_space *); diff --git a/mm/filemap.c b/mm/filemap.c index afb3b0169a9173..8c5196cf93a454 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3819,7 +3819,7 @@ EXPORT_SYMBOL(read_cache_page_gfp); /* * Warn about a page cache invalidation failure during a direct I/O write. */ -void dio_warn_stale_pagecache(struct file *filp) +static void dio_warn_stale_pagecache(struct file *filp) { static DEFINE_RATELIMIT_STATE(_rs, 86400 * HZ, DEFAULT_RATELIMIT_BURST); char pathname[128]; @@ -3836,19 +3836,23 @@ void dio_warn_stale_pagecache(struct file *filp) } } +void kiocb_invalidate_post_write(struct kiocb *iocb, size_t count) +{ + struct address_space *mapping = iocb->ki_filp->f_mapping; + loff_t pos = iocb->ki_pos; + + if (mapping->nrpages && + invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT, + (pos + count - 1) >> PAGE_SHIFT)) + dio_warn_stale_pagecache(iocb->ki_filp); +} + ssize_t generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from) { - struct file *file = iocb->ki_filp; - struct address_space *mapping = file->f_mapping; - struct inode *inode = mapping->host; - loff_t pos = iocb->ki_pos; - ssize_t written; - size_t write_len; - pgoff_t end; - - write_len = iov_iter_count(from); - end = (pos + write_len - 1) >> PAGE_SHIFT; + struct address_space *mapping = iocb->ki_filp->f_mapping; + size_t write_len = iov_iter_count(from); + ssize_t written; /* * If a page can not be invalidated, return 0 to fall back @@ -3858,7 +3862,7 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from) if (written) { if (written == -EBUSY) return 0; - goto out; + return written; } written = mapping->a_ops->direct_IO(iocb, from); @@ -3880,11 +3884,11 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from) * * Skip invalidation for async writes or if mapping has no pages. */ - if (written > 0 && mapping->nrpages && - invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT, end)) - dio_warn_stale_pagecache(file); - if (written > 0) { + struct inode *inode = mapping->host; + loff_t pos = iocb->ki_pos; + + kiocb_invalidate_post_write(iocb, write_len); pos += written; write_len -= written; if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) { @@ -3895,7 +3899,6 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from) } if (written != -EIOCBQUEUED) iov_iter_revert(from, write_len - iov_iter_count(from)); -out: return written; } EXPORT_SYMBOL(generic_file_direct_write);