From patchwork Wed May 31 07:20:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13261554 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 B896BC77B7C for ; Wed, 31 May 2023 07:20:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5C5A1280004; Wed, 31 May 2023 03:20:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 57367280003; Wed, 31 May 2023 03:20:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3772F280004; Wed, 31 May 2023 03:20:40 -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 27C2B280003 for ; Wed, 31 May 2023 03:20:40 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id ED93A12022F for ; Wed, 31 May 2023 07:20:39 +0000 (UTC) X-FDA: 80849702598.26.FC0B6B0 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf28.hostedemail.com (Postfix) with ESMTP id 22C85C0004 for ; Wed, 31 May 2023 07:20:37 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=J1M7xmS2; dmarc=none; spf=none (imf28.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=1685517638; 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=JHyotPJLUVxL+dA041++97mXWjWSVKWHUEqhc6RwL0QWFO0JVP8CIuVQFgjiLUkX56trE8 UgotN47l8iQ19qHMrwe6187FHlfW3U/zTQw0lHBIo8vIE20Se9B5YFVzBUVPiY4XL++DB0 6tSTY4M2tNt1k17LJtjGExTvh5LPvXg= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=J1M7xmS2; dmarc=none; spf=none (imf28.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=1685517638; a=rsa-sha256; cv=none; b=MDU212HGSqbnIb099S/ndlh4Qy7hT8p7rTdfRzi+BunkXElpab7PPgFHqaxiqfv+czysWp QWYx+wuX4UB61oWbB8dSGe6VeAgGxiGr2rCRtt8Qix+x9o1rAbIZ5Ap3YHjUiDhed690s5 sEnRHINU2pL9+VMYfEYVCOhcYxf0RFI= 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=J1M7xmS2smYT0WGZBNMeQ7h67W 05CC3gy3+uiDSRnwU9CvRdOwOXzI4hvby1CGkGqCB328TKi5Q03euobXsrGg8apFwmDbL5lAco0CQ 2zk8Lhn5l16g1f1ztPQGJF1t8Log1MveoJ4bxlU5qDwgCAZv98DsQJfceEvpKKKx59WH33C6Xhr+j jLFOFUvmiqSPPiqw9tAMb/ss52uKMM/5zEKK0XnS2i5LeK+kABfmmwS5v4UnVgIKhrvjMYcXhu9MA D/UTl71t6BFAkGEL9i8SWj1IzbYMcwqk/G9jaoCuVA0Q95Xq1tMEAHhbTU0cnj5wJo/sunF9oga36 5AZj3qDw==; 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 1q4G89-00GPjL-1N; Wed, 31 May 2023 07:20:25 +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 6/8] filemap: add a kiocb_invalidate_post_direct_write helper Date: Wed, 31 May 2023 09:20:04 +0200 Message-Id: <20230531072006.476386-7-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230531072006.476386-1-hch@lst.de> References: <20230531072006.476386-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-Rspamd-Queue-Id: 22C85C0004 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature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vWULdusBF9atWnTyDR+AqI5dm6t5Wz7I2vG0JXJaFGTfzSwBzmvXVDtWVqwaJs6x5XijyzBPAReWxJJzYaRgxNqqByaanPtYsgqoI0FW+fg5i+kjNZXAqdlr97ZpKMl+D/pPZ+09TPX+QDxCJRq3viCVx2kxoC6rPvk4vml70XefsA+3Ts0TFdSUY49gBnWLuR0dUvnRRr3zu4n9ofE+MgxeL6dK2jzqv92prRyGksoHJP0HI3/swyO+BD/JIVyVYFzchvo3VfIX6r7iz+vDWOzgSVZo0SRol0obpGRXdtjgUvudxUvX/noGd2f4Rw0RJDRMU+FqqsW11bgwiqZnooCmHfeP7u4cMGwjx006BecWNK+x/awUpYax4GXl1s7iq/pb8rMS4P85i0z0iY5KPNJcnHKj8ygJEfjk9Ijt2Doo79uoBXJIFTF6EaYiqBpOeBgdk/t6XDKID7Gr4/EPbX2H9t9mKcd3lelsOywyXLMtzagT59KTglPcpzFqdtyGvTi2Wv9sPnrD438PBFzTXOc+Q= 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);