From patchwork Mon Apr 24 05:49:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13221659 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 F4064C77B73 for ; Mon, 24 Apr 2023 05:50:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B6CC6B007E; Mon, 24 Apr 2023 01:50:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8670D6B0080; Mon, 24 Apr 2023 01:50:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 707B2900002; Mon, 24 Apr 2023 01:50:01 -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 640836B007E for ; Mon, 24 Apr 2023 01:50:01 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 36FB31601A2 for ; Mon, 24 Apr 2023 05:50:01 +0000 (UTC) X-FDA: 80715208602.30.561BA26 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf28.hostedemail.com (Postfix) with ESMTP id 930D9C0008 for ; Mon, 24 Apr 2023 05:49:59 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=ksnjCNaa; spf=none (imf28.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=1682315399; 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=LInMwgmm7juakhqMJmUm/pISuBKmyhtj1h64RTxGVFE=; b=RQpuY4hq92wXgWKVec9Nq4EO7ri40OBEUyx4zlghNPzc3JnYyr2a1dU5HA8FJXalkaDFsq Zc2UdJ1MsSmLjEaId2svo9qFv++UmFUPeWN412ZxfrHJq5+L3LUbLyhyvcNfx5W3TV7qjF vgfo3KefES2jXM2OXJ/bbi8hQfJOZT8= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=ksnjCNaa; spf=none (imf28.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=1682315399; a=rsa-sha256; cv=none; b=npr+Pb2dXRHR/5bK30vFXsIa/MLv664YubrgFk0WItHBS7vmTN5X3FGZu5Me6/AqSZ1lGD RZD10SX8SS5bvPhfTF/jrYXTUFOLbd8mLAUbVyUaVNCZ5+glR48Imlp0LLQrn/sthLfAit rLPWC2YvLRunnjopi+HNUKzlExiwiII= 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=LInMwgmm7juakhqMJmUm/pISuBKmyhtj1h64RTxGVFE=; b=ksnjCNaaIYILtAb94qr1FTT2Wz kZRuCMhWL9racZ9eP9X8K5p9+krm6xIvaHOewtdjyif7/XMRozz4R7mpRrFqpSzaYOYZZp+hz9j64 ccya7w488Ktn5XvstbBsAUFLV2jC6akCFOTNSUsoO0g72ip2tieVyNrcMOs/bqwhYxlcGZnjpO8P8 p+tTKt17x8IxnhUcRLwo01cYYILh46k+ybO9S5xEZSDgd33UCHppPSdPVnIHFLtlAPgyTTg0J5cIP VsGdLLnL5NPrn4LLLxCrWgG5kPHsLi0NxWZb5LKRKHJgnMP5ZYVjS6xFc2Hlea4qeMAV15N77hydj ZnQ3LaDg==; 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 1pqp5C-00FOwD-39; Mon, 24 Apr 2023 05:49:51 +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 07/17] filemap: add a kiocb_invalidate_pages helper Date: Mon, 24 Apr 2023 07:49:16 +0200 Message-Id: <20230424054926.26927-8-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-Rspamd-Queue-Id: 930D9C0008 X-Stat-Signature: xrurp7sgx17xoiyhht4p6fza9znig55q X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1682315399-431978 X-HE-Meta: U2FsdGVkX1/vGJH2jWOJcUcwgDYWHQOvZySEk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6svfdWFhoHj+cLI0BjxkVA1fZEew/FAAhXYzoBs2lnAlmJSh2Tzm2zGuhU2erKOjNxOdszmcDjg08/xTUolB15X4Fs0nQQrAk4z93l4LtiS9ztIHnoxZkLgJDmHhX9H4PO19dykL3P6wad1vO5+AsxGNneTdvDnfoNEVzQD3ryOZIyvoFvMdzO7WGw7toNOM5HEhrfGV/G/WVq/NCANEFyrAlwhUi184KDaaj4ZTV2TicVVo3BkYLXgyZKr47b51cwIl/bW/5sQoUNzKmhyOBNkBCAcZnJ2D7sfdGu1M3Nd1EEy6sQLSm0nJPih6QLjdUtgZNN4bO8mV8x/jvSgpCDwThId1I0PiehAe1wqQPz4dy3c/i0mHWy8dbo= 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: Factor out a helper that calls filemap_write_and_wait_range and invalidate_inode_pages2_rangefor a the range covered by a write kiocb or returns -EAGAIN if the kiocb is marked as nowait and there would be pages to write or invalidate. Signed-off-by: Christoph Hellwig --- include/linux/pagemap.h | 1 + mm/filemap.c | 48 ++++++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 51f7aea51169c4..f6c5ef9d3d7646 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -30,6 +30,7 @@ static inline void invalidate_remote_inode(struct inode *inode) 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); 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 28ea9804191d6b..afb3b0169a9173 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2780,6 +2780,33 @@ int kiocb_write_and_wait(struct kiocb *iocb, size_t count) return filemap_write_and_wait_range(mapping, pos, end); } +int kiocb_invalidate_pages(struct kiocb *iocb, size_t count) +{ + struct address_space *mapping = iocb->ki_filp->f_mapping; + loff_t pos = iocb->ki_pos; + loff_t end = pos + count - 1; + int ret; + + if (iocb->ki_flags & IOCB_NOWAIT) { + /* we could block if there are any pages in the range */ + if (filemap_range_has_page(mapping, pos, end)) + return -EAGAIN; + } else { + ret = filemap_write_and_wait_range(mapping, pos, end); + if (ret) + return ret; + } + + /* + * After a write we want buffered reads to be sure to go to disk to get + * the new data. We invalidate clean cached page from the region we're + * about to write. We do this *before* the write so that we can return + * without clobbering -EIOCBQUEUED from ->direct_IO(). + */ + return invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT, + end >> PAGE_SHIFT); +} + /** * generic_file_read_iter - generic filesystem read routine * @iocb: kernel I/O control block @@ -3823,30 +3850,11 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from) write_len = iov_iter_count(from); end = (pos + write_len - 1) >> PAGE_SHIFT; - if (iocb->ki_flags & IOCB_NOWAIT) { - /* If there are pages to writeback, return */ - if (filemap_range_has_page(file->f_mapping, pos, - pos + write_len - 1)) - return -EAGAIN; - } else { - written = filemap_write_and_wait_range(mapping, pos, - pos + write_len - 1); - if (written) - goto out; - } - - /* - * After a write we want buffered reads to be sure to go to disk to get - * the new data. We invalidate clean cached page from the region we're - * about to write. We do this *before* the write so that we can return - * without clobbering -EIOCBQUEUED from ->direct_IO(). - */ - written = invalidate_inode_pages2_range(mapping, - pos >> PAGE_SHIFT, end); /* * If a page can not be invalidated, return 0 to fall back * to buffered write. */ + written = kiocb_invalidate_pages(iocb, write_len); if (written) { if (written == -EBUSY) return 0;