From patchwork Wed May 31 07:50:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13261643 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 AF1E9C7EE3A for ; Wed, 31 May 2023 07:50:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4CD9F6B007E; Wed, 31 May 2023 03:50:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 47C546B0080; Wed, 31 May 2023 03:50:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 344C8900002; Wed, 31 May 2023 03:50:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 26D516B007E for ; Wed, 31 May 2023 03:50:58 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id F1EA640274 for ; Wed, 31 May 2023 07:50:57 +0000 (UTC) X-FDA: 80849778954.01.C158AD2 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf29.hostedemail.com (Postfix) with ESMTP id 4CC7112000E for ; Wed, 31 May 2023 07:50:56 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=VcmrNqxv; dmarc=none; spf=none (imf29.hostedemail.com: domain of BATV+b81a8c9a6d22e8bb2302+7220+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+b81a8c9a6d22e8bb2302+7220+infradead.org+hch@bombadil.srs.infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685519456; 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=nJKP2lo8qunKNZgc/SdmDxY20EOraczIBDBFCWeBRL0=; b=5TSOYWRDz2jC8XfYbFDYYy2tUqSzxOeb0P1XVM32wjB4dWtJWNT/kwW0ogHljlHnhpmBA1 j9kL/YBSPtbR0PmjY1CSxiP1IAff9Pzl1BFkUL/UjKw+0Qe4iVtlNi8krcAtya514Zt/wM I5YLnKsSv30YnUHqHwThP4/8DInz92o= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=VcmrNqxv; dmarc=none; spf=none (imf29.hostedemail.com: domain of BATV+b81a8c9a6d22e8bb2302+7220+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+b81a8c9a6d22e8bb2302+7220+infradead.org+hch@bombadil.srs.infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685519456; a=rsa-sha256; cv=none; b=bzve0QSEFdKof8ZjoTjc8nKegT8aBtcrDFWuCkJoXu7QRd8bexbVU32KCnGjc4oIrTbFy8 feM5AZfNYPjQiyKEUq/4gZUANuO2XdqnOJcDM4AddFb5ZMyD9rQzLq3Icins+OW6NDy4Lt NI5GOd0NJNianiHrzShpbO0Q7BEtOIo= 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=nJKP2lo8qunKNZgc/SdmDxY20EOraczIBDBFCWeBRL0=; b=VcmrNqxv6S1vmZIVE8SqEniQ4K Ht4eRjWh//ZZRedyE2Z6jK13px/jDCNCkcSP7a6uSXXa/UA8qcUY6tmc2HdJotSsc/YgRZZqPIAqy 1zZHXQunc/rbJwCSAN2NabXi0UCMx+vnSvyncvJR51m42KTjvtsrPadacaIND/KLcewXau9x3INds 6SG2baH6RhFWFFLB+EuHNHEqUCaFLhpF0L+GuFU5Fpyh4cWfa+zXkNkvDEV25oAFzTIydQGskBztG BLqbxh8Dndj/ZmVPDzAQUEokLF4cjS/5E+W8Pk/u8s9wXTwjrWBkSS8p6uMbCdhMqUKMNgnES7SrK RE6cKBbA==; Received: from [2001:4bb8:182:6d06:f5c3:53d7:b5aa:b6a7] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1q4GbV-00GVql-0P; Wed, 31 May 2023 07:50:45 +0000 From: Christoph Hellwig To: Matthew Wilcox Cc: Jens Axboe , Xiubo Li , Ilya Dryomov , Alexander Viro , Christian Brauner , "Theodore Ts'o" , Jaegeuk Kim , Chao Yu , Miklos Szeredi , Andreas Gruenbacher , "Darrick J. Wong" , Trond Myklebust , Anna Schumaker , Damien Le Moal , Andrew Morton , linux-block@vger.kernel.org, ceph-devel@vger.kernel.org, linux-fsdevel@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, Hannes Reinecke Subject: [PATCH 06/12] filemap: add a kiocb_invalidate_post_direct_write helper Date: Wed, 31 May 2023 09:50:20 +0200 Message-Id: <20230531075026.480237-7-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230531075026.480237-1-hch@lst.de> References: <20230531075026.480237-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: rspam12 X-Rspamd-Queue-Id: 4CC7112000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Uv/0T6dOdoA/S+Rb00LQSQ/oQ2w+iGVIYW1BuH1UC4bJ+KtNt2F53lUk0wORi68xn/+MVlFTMX5dkoAGyaxuq+21Us/gjfF0m8IZjt/iTpwa+Mc0lvy5o7C8ncq2GDbvUGLx09f0pMlHduPvYpE44CEAVdVms3ordaDCuaG39q5DaccuaD5sxskoQPcp0TKujW11KJ7shzTt6IASIH6FxORRuH69rErEdfe4XXZAXPWqwn/pAPbVdSasr5EdrTvp0QgRnxQxzykwUo1R2AAx5DZL+/+nFJaNCXkjvsdpMHgI+Elxb0FFizpUeMxUtiSKSpkLlyAU1efHNXJXdSqjXBhvdS7O2fWms8EQY+Sdnx+bymGfjofv5nV2/EBAMBCtbVgYgXlw6VjRdpVIbbNvtqK7/+VOkFIo5AYVNcV9yayvkQEsocccIJCScxpEaLUPNOfm9WsB8uhfNcZSrtH581ymNWVaULPN13EX/dfY8zyjIg9iZAfZb0Y+OfOzxkrQbooxGgq7knlgrQRAZOrpBW3zjH0soVacPaNOcKD1kxPldyS35GivHUD7W3VOii2j3gIbMHGxIwL/l4Z+qekWxKLbb4Bqnz5kA== 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 Reviewed-by: Damien Le Moal Reviewed-by: Hannes Reinecke Acked-by: Darrick J. Wong --- 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 0b380bb8a81e11..4f9069aee0fe19 100644 --- a/fs/direct-io.c +++ b/fs/direct-io.c @@ -285,14 +285,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_direct_write(dio->iocb, ret); inode_dio_end(dio->inode); diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c index 6207a59d2162e1..0795c54a745bca 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; @@ -108,15 +107,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_direct_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 133f0640fb2411..91021b4e1f6f48 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2837,11 +2837,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 6e4c9ee40baa99..6ecc4aaf5e3d51 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_direct_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 a1cb01a4b8046a..ddb6f8aa86d6ca 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3816,7 +3816,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]; @@ -3833,19 +3833,23 @@ void dio_warn_stale_pagecache(struct file *filp) } } +void kiocb_invalidate_post_direct_write(struct kiocb *iocb, size_t count) +{ + struct address_space *mapping = iocb->ki_filp->f_mapping; + + if (mapping->nrpages && + invalidate_inode_pages2_range(mapping, + iocb->ki_pos >> PAGE_SHIFT, + (iocb->ki_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 @@ -3855,7 +3859,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); @@ -3877,11 +3881,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_direct_write(iocb, written); pos += written; write_len -= written; if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) { @@ -3892,7 +3896,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);