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: 13221661 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3577DC7EE2A for ; Mon, 24 Apr 2023 05:50:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B6D56B0081; Mon, 24 Apr 2023 01:50:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 867996B0082; Mon, 24 Apr 2023 01:50:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 707EC6B0083; Mon, 24 Apr 2023 01:50:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 6327F6B0081 for ; Mon, 24 Apr 2023 01:50:05 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 325D8ABEDF for ; Mon, 24 Apr 2023 05:50:05 +0000 (UTC) X-FDA: 80715208770.12.318F4E1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf15.hostedemail.com (Postfix) with ESMTP id 76A2BA0013 for ; Mon, 24 Apr 2023 05:50:03 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=uebmB2Tr; spf=none (imf15.hostedemail.com: domain of BATV+e16e2fc4419b117693a1+7183+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+e16e2fc4419b117693a1+7183+infradead.org+hch@bombadil.srs.infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682315403; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=i8BBYqYTYYknSfjW0NZ6ikX9GZ70UEDP1VbXwkZOOdg=; b=CbAhgscNKyielGpQ22yZN512RSuS1SO10QZQnLH8E4ezpJq8J65OSHEDoWvskWF8m0PnE+ LMtweJ6j2Lpam0+ZzuMwoWoVFqM56nmsgzO/LGbTD++OKO7rzLBaMdNJp/LVJQF7UnXzSw bJoI48xlR54yGLdkADZOeOPcaNIlPBI= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=uebmB2Tr; spf=none (imf15.hostedemail.com: domain of BATV+e16e2fc4419b117693a1+7183+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+e16e2fc4419b117693a1+7183+infradead.org+hch@bombadil.srs.infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682315403; a=rsa-sha256; cv=none; b=0aI0DCBcG8FTeWrRmrQ6Dt+r3I2fNhi0dCRUln+WoLkhKmUvU86h3LUA0KAe7Ryzj2kI0T tAcFMuGvt3f/agvq/tbz2GGcLLWk5MLTJcrnbNUt8nt2NU7QRbFj+Vu0tejVEOct/FMbFe 4x9vJZvUnPbe/j44QzXfrx98QvgIrT4= 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-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: wgqh19ejcwsqb3g1hypx6y5dqrbx17zc X-Rspamd-Queue-Id: 76A2BA0013 X-HE-Tag: 1682315403-924792 X-HE-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kzxSZ8uOVy6Og1S1okXAKnYoTKFduKIPzX3LYDNaxTsgXg1cDLC3QYJzNSLQQHGay6q+AJ92qqQxbByCEtkkyEt3bRqX8eE6nKQO6H0fdCo11a1M36Qb/t+RUbk1H5TA3Io/P0254Nvt4+flp4c+dml2Q5ACyV628vA4bn7/u+Q668CwCvkr5qZF3zrM23k4FN4eN/vGC4rK1FBvBJ4bHQs9R69mfW2K4TDi/PempvN5gqZYXIwpPxQpxIfNNBBKNV8uY/nZ1W1HZR0KrY7eXpCS89+INk20OZavCq/nOJCAyWWjmPCKUwiThXuyeFO2VvGrfrGu0RBdtH+7vmU7AsYodkIVIFXpVricF9lYg31R1g24Dmx0ukZQPc82UJzKic/iuPgJ7XOJSy0UNk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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);