From patchwork Mon Feb 12 07:13:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13552748 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 2788DC4829B for ; Mon, 12 Feb 2024 07:14:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B7FC86B0081; Mon, 12 Feb 2024 02:14:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B31038D0001; Mon, 12 Feb 2024 02:14:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9F9EB6B0083; Mon, 12 Feb 2024 02:14:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8F5776B0081 for ; Mon, 12 Feb 2024 02:14:21 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 5C9C3140956 for ; Mon, 12 Feb 2024 07:14:21 +0000 (UTC) X-FDA: 81782288322.25.740EB8D Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf11.hostedemail.com (Postfix) with ESMTP id 978344000F for ; Mon, 12 Feb 2024 07:14:19 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=hYd1tnNc; dmarc=none; spf=none (imf11.hostedemail.com: domain of BATV+56c964bc8172c8a9ee6e+7477+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+56c964bc8172c8a9ee6e+7477+infradead.org+hch@bombadil.srs.infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707722059; a=rsa-sha256; cv=none; b=PUJdFqm7/1T9tDT8+ElDNTs9TwQzMmBX3oZSgjKW9grIrvvm4Bq+Na5wyUood4Tn3EpvuH 3EEA/oNgNmcH5VFhYZjmA3xQEUoEzpCoZaPX+TjSliUXSVacFX7WoHeIK0RqCD6+291cgH xEhYQYEn/n8FDuJW2XhI7dt3b0hEvx4= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=hYd1tnNc; dmarc=none; spf=none (imf11.hostedemail.com: domain of BATV+56c964bc8172c8a9ee6e+7477+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+56c964bc8172c8a9ee6e+7477+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=1707722059; 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=Vlqq71/8cVy7zsg5xjVrhlKjzr59zOQg5vGY/d9fuxQ=; b=lH1NUH6ACt2BSjem0qomDcdhY+d71oelsZmc1RuxBTPR7wI/Sa0l35LkMIV+jrepdCb3lW 4dl7QlIiW5ZtETqTyMilXDNGxDuieoPZi1dGqfVzlh+W4pUHxxS30tWb4RU5V1ZpfsJLgq 23bkgzum7Eg1cdcHW8USuthbBjUZAp8= 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=Vlqq71/8cVy7zsg5xjVrhlKjzr59zOQg5vGY/d9fuxQ=; b=hYd1tnNcGbUg+/gh3uTrVMVQBo 5C0owQ98hyLKCFuNzHZWKhpaK4i93rvDQf/YtRRosKmoQIatQX8GjBjLbNYU51u5x6BrMXyEwBThp V9v4bboxACxI4B64dBWWgcHn1ZmTMHYpFUfEuEK51eADVZ4JTtdT6uLwEFs67KWUM61jy+NECgrNs mqnGux6DoCpjC8K2uCNHmjf0eh9L9n4iEg9A171Z97uzfL7+T7QAwHc7wm2lqkpPjM1zbpyYt+M1j paXId343ZnCHv/8fufhVVqFEjQfGz33QP6rkneYGZt/dolem9SLOlXG2viWp/P2MLcPHEhc5JbcyF Tqs5/HMg==; Received: from [2001:4bb8:190:6eab:75e9:7295:a6e3:c35d] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1rZQWA-00000004SwH-0xZP; Mon, 12 Feb 2024 07:14:18 +0000 From: Christoph Hellwig To: linux-mm@kvack.org Cc: Matthew Wilcox , Jan Kara , David Howells , Brian Foster , Christian Brauner , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Jan Kara , Dave Chinner Subject: [PATCH 07/14] writeback: Factor folio_prepare_writeback() out of write_cache_pages() Date: Mon, 12 Feb 2024 08:13:41 +0100 Message-Id: <20240212071348.1369918-8-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240212071348.1369918-1-hch@lst.de> References: <20240212071348.1369918-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: rspam06 X-Rspamd-Queue-Id: 978344000F X-Stat-Signature: agwdmaufr97a6ujzrcuf1kzxhrro81cg X-HE-Tag: 1707722059-862961 X-HE-Meta: U2FsdGVkX1/HaVfSywRJdq/0x2gJXGZ4j6puiX/MHERt2oDIoakXYe0OEug9tqYqgug9VtxXUc0a+/cwnPwbW3un3jarcqMpfiZX9Qu5u+W1WZyj04d6b6kOlotYzAO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emm0GSmOy8NHMZbUxUpb3n5EttQsimZnIOXMKBprbN1UAH7XW5jZlqPMrfAP4pl7YykKuLcedN9uLytuaH4SIMNYQI1SsJQxP5R2tAcpJ07rIRFdnIhgCdelUYTusANUxLK+5fmIp7pWJv2iTP9YYIRNJ5bRVmOiMSykDB3bkLsuWvJ3wZvrR7GIE033Nt6As0x1EHogszUh3aBNcBD2TzN62TLFhPtsFrU0ztjbbq5e7ABHUihHjnzhQV3Oeyj4rYCgK/HLt1QpinWEfuDU2G9TweP7IJjY0s8rt3m/U24hJV6cGqWS9vf7FAzG0ZgBHguYT6g3sgEyO3wT 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: List-Subscribe: List-Unsubscribe: From: "Matthew Wilcox (Oracle)" Reduce write_cache_pages() by about 30 lines; much of it is commentary, but it all bundles nicely into an obvious function. Signed-off-by: Matthew Wilcox (Oracle) [hch: rename should_writeback_folio to folio_prepare_writeback based on a comment from Jan Kara] Signed-off-by: Christoph Hellwig Reviewed-by: Brian Foster Reviewed-by: Jan Kara Acked-by: Dave Chinner --- mm/page-writeback.c | 61 +++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/mm/page-writeback.c b/mm/page-writeback.c index b49ee15a863e99..20ff00c8be9d90 100644 --- a/mm/page-writeback.c +++ b/mm/page-writeback.c @@ -2360,6 +2360,38 @@ void tag_pages_for_writeback(struct address_space *mapping, } EXPORT_SYMBOL(tag_pages_for_writeback); +static bool folio_prepare_writeback(struct address_space *mapping, + struct writeback_control *wbc, struct folio *folio) +{ + /* + * Folio truncated or invalidated. We can freely skip it then, + * even for data integrity operations: the folio has disappeared + * concurrently, so there could be no real expectation of this + * data integrity operation even if there is now a new, dirty + * folio at the same pagecache index. + */ + if (unlikely(folio->mapping != mapping)) + return false; + + /* + * Did somebody else write it for us? + */ + if (!folio_test_dirty(folio)) + return false; + + if (folio_test_writeback(folio)) { + if (wbc->sync_mode == WB_SYNC_NONE) + return false; + folio_wait_writeback(folio); + } + BUG_ON(folio_test_writeback(folio)); + + if (!folio_clear_dirty_for_io(folio)) + return false; + + return true; +} + /** * write_cache_pages - walk the list of dirty pages of the given address space and write all of them. * @mapping: address space structure to write @@ -2430,38 +2462,13 @@ int write_cache_pages(struct address_space *mapping, for (i = 0; i < nr_folios; i++) { folio = fbatch.folios[i]; folio_lock(folio); - - /* - * Page truncated or invalidated. We can freely skip it - * then, even for data integrity operations: the page - * has disappeared concurrently, so there could be no - * real expectation of this data integrity operation - * even if there is now a new, dirty page at the same - * pagecache address. - */ - if (unlikely(folio->mapping != mapping)) { -continue_unlock: + if (!folio_prepare_writeback(mapping, wbc, folio)) { folio_unlock(folio); continue; } - if (!folio_test_dirty(folio)) { - /* someone wrote it for us */ - goto continue_unlock; - } - - if (folio_test_writeback(folio)) { - if (wbc->sync_mode != WB_SYNC_NONE) - folio_wait_writeback(folio); - else - goto continue_unlock; - } - - BUG_ON(folio_test_writeback(folio)); - if (!folio_clear_dirty_for_io(folio)) - goto continue_unlock; - trace_wbc_writepage(wbc, inode_to_bdi(mapping->host)); + error = writepage(folio, wbc, data); wbc->nr_to_write -= folio_nr_pages(folio);