From patchwork Wed May 24 06:38:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13253307 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 47C26C7EE31 for ; Wed, 24 May 2023 06:38:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B71A4280004; Wed, 24 May 2023 02:38:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B1E4F280001; Wed, 24 May 2023 02:38:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9BFB8280004; Wed, 24 May 2023 02:38:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 8BA2C280001 for ; Wed, 24 May 2023 02:38:44 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3A05B1408C2 for ; Wed, 24 May 2023 06:38:44 +0000 (UTC) X-FDA: 80824195368.25.0C3D337 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf01.hostedemail.com (Postfix) with ESMTP id 8F4DD40014 for ; Wed, 24 May 2023 06:38:42 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=aIT9RxPA; spf=none (imf01.hostedemail.com: domain of BATV+8349dc162fb675e8d54f+7213+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+8349dc162fb675e8d54f+7213+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=1684910322; 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=HdqYutACGg449lbpC5g+tM8dj77PJUcg5E3vRPFmYq0=; b=4Gf1r3ls7o3vXqB23UsZ5ARBh8mDhs5oF4dGcBpzdYbfipYUSfvw6J6QAvd1XhvByhCFsL KKC0REmGliinUHcNBnsmQuoxkSvQnydIqO8Umoia23UQy5Vn4oAIwiT0m3DgpSPtJ9V6nk Yz+l9rtclRGE5NUksVc7dvmI1l0ja8A= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684910322; a=rsa-sha256; cv=none; b=SVuJR2BmN+OxJ9iGkJRITkmsDlEULTGjOrF7LVivCrLfihC1zhoLHQUrtN+Q8MtE2TwiLD Rm/cxL39VjAJCqP7uUXbuWVV8cV9D0WJFvpd+yS1PNVkn3DZS9ZyGjnHf95/FouTaTA6/R GpZNpawB1+NrT9iPxty9RXBZOGm/TQA= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=aIT9RxPA; spf=none (imf01.hostedemail.com: domain of BATV+8349dc162fb675e8d54f+7213+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+8349dc162fb675e8d54f+7213+infradead.org+hch@bombadil.srs.infradead.org; dmarc=none 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=HdqYutACGg449lbpC5g+tM8dj77PJUcg5E3vRPFmYq0=; b=aIT9RxPAfhHOwqFZulkLmucxGV rLmMDARC7Dok2azH//gTb4c6Pv6q2CYBKqB8tbL2AuYl8Tfm9vOJ5XyRfvYGxjYgaZYnnX2Hx6xah vrC1KPdTBTHrMFDf6/pdyS2fU62TnVe2a/zKaA4Er+WhodXEB55zZVt7ffWjrFYZ8qwq2O1XSL6jZ Jf55EKX6Pdh43s8/PobRAATn/qfOc8Kte4qHFp4RoHJZ42gh+MbsmJr0BE8l4SxPAs7j+iwO11vfy 2Y+30GGr5oR4tTZ1yrEZRl9Jb/f+do7t9ATt+B+FC6zzk83WgR+wA/beHBKCN+AZh236kLMfXWU/D OHHhHfaQ==; Received: from [2001:4bb8:188:23b2:cbb8:fcea:a637:5089] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1q1i8l-00CVg9-0q; Wed, 24 May 2023 06:38:31 +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 Subject: [PATCH 06/11] filemap: add a kiocb_invalidate_post_direct_write helper Date: Wed, 24 May 2023 08:38:05 +0200 Message-Id: <20230524063810.1595778-7-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230524063810.1595778-1-hch@lst.de> References: <20230524063810.1595778-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: 8F4DD40014 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: xuisbhicprausdzuz6xdqmtsybji54wi X-HE-Tag: 1684910322-645483 X-HE-Meta: U2FsdGVkX182VQ9lYf6pHiSIgp3YStf+fIuO2qZTwciZLke3diD5HA/0yx98QJVc1q3aFTzPMLJH977cuJD5v+l8YeNDnhqazleUkOSfaKqKoarVlIt/u03gdx87fN2bjWwiw17JbICUaD6SJc/J41Zod0TmijVwbGISuM/cr8W4RBb6EA9G5fUxxrEXRsEuHNgtmpgljYz6nzWjmr3ToYOl0A1/KsxndxDG76qfyk9p5gc2HWhFcoKr2yPrMw17NAxV3svWtjxdPiMOn+PJ5cycQyyIQjz3DUi25mBrucV1ASlZBfbtDa5MKRMOgR+EbmeF+oaPnsy/BzTIzo0dIolA5EhzmKV87h+UfFcJetO2Nn+R7c3k+e9VWzWJcoA50InXtFC150GhJxF0DGA+mWnMGMipdo6Wc+SLletibAhm0gpiMAgeTUAvHWOcCx6q6tI8RiKaRKJkto1buRK0NfVOyZkfJwjIw83+YOBaWIb2n62Xh3OY4oBbtCOR4CZxGQQ7xteO/YwTQmQyIiDPMFSmxiSYAbXmMeNs536sFG22f28yO1fRv3+cGQaGWYtbuIlM5ro4Ab5TvVEVq0An6kNHrI3CMPjOk8Q9ZR6DbFcHfQKk0J2skIm8Ei3qcowC/zAjU2pP8XwURi+On6ArG9+7VgnssDGUYnIy35VImXt9NVUITBcDZN90dKGZvbwZ5MbY0ADIaDRNFqls5A56nGtx+l0qyZO1KA8Ia3kgqSbVb4dxTKGNXTTVP83/maKb43NkfyqwW+0D/EyIbRpdCihZxCS48jnoaxs8Hww631yGWWx72TkTaTUUfuV1lny36BUBqDkD94kyHAWxqD5I+pTX2dRzGXUGKq30CEVf+hc2NH7zJa1BioUGa4SiRGZvPCxDQSkSRghqDqGRQcghL7McFJbGi20eOm8omExYsPdJ5ShnCBhQ1nxbs0LEqRf69dA6K8s7iWF0tISMQLK JsO93E3C dC3lWot6HExDoNxLUMwH8aXWTMR6b8N4ccehFjOogcGEKdgifVReFTjLYe/a6TydxlGfnikRwz9ZENeyUUaB1loVSICvqIuwYLdHReasgB2n5TCb4TuGn2aMsxKHoD2swPYLHWek5R/c6z9KZhFupue12hzo9hXBagEmhcqPBVfMLdoHJ/7EfoYFgp8OPutFaXacv7vTz0WLCzIdA+8TCytfwxf7tAEScUk0fcg064c5iVXFe8o34OWa+x2VtSbM3FgM+RAnHx2P5TSfAEsJCh3YHcsel8aYjK2rBRUM3Pez7nwY9lfT/mHyJxGWVO7sijmnDlYG+hXNtnQM3NpjfMvWUuZPKyARtJz+DwShdfkjUCZEvgFrek/xIEuiaUmRCa7hXe3o0MaRRaNvoZ0MRNL/e/9u7Davy5qJv+/uMoAZj07h2hNFibWi1JMGtpGfP15sSTKzmdrZlF0jqR23VS4nqlEuqJbnNY06wLKfONW0uQGCQ6997NEjlM6y9VsbKOn3RUn+rQpv9YfDrm7PLHmcbqNBoDewLGV9ZT+x+CGoE2Bw= 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 Acked-by: Darrick J. Wong Reviewed-by: Hannes Reinecke --- 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 21a98168085641..e4efc1792a877a 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);