From patchwork Fri Dec 22 15:08:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13503416 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 ED46CC46CD2 for ; Fri, 22 Dec 2023 15:08:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 487228D0001; Fri, 22 Dec 2023 10:08:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E75E6B0089; Fri, 22 Dec 2023 10:08:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 23C178D0001; Fri, 22 Dec 2023 10:08:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 08D1C6B0088 for ; Fri, 22 Dec 2023 10:08:51 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C54CA40E7D for ; Fri, 22 Dec 2023 15:08:50 +0000 (UTC) X-FDA: 81594786420.21.06074C4 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf12.hostedemail.com (Postfix) with ESMTP id 19EF84002B for ; Fri, 22 Dec 2023 15:08:48 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=wfmB6gnW; dmarc=none; spf=none (imf12.hostedemail.com: domain of BATV+af923ccb694ecd533eae+7425+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+af923ccb694ecd533eae+7425+infradead.org+hch@bombadil.srs.infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703257729; a=rsa-sha256; cv=none; b=0Fe5C2NAZEj74RCBws3btcRxhjITyMSES+BRtn6ARHdQr2pFvOU+9RH9Tnj4FwxQ4h4GrZ nafB6EJ67By+GLdNs98lPuA/APVltj09o6ikuHmF71D5lEuEDKHXEFScntBwjQgU5KJPcY yNKp0pt5AYf3DLDZ8nJyFnbFpIoUr18= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=wfmB6gnW; dmarc=none; spf=none (imf12.hostedemail.com: domain of BATV+af923ccb694ecd533eae+7425+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+af923ccb694ecd533eae+7425+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=1703257729; 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=BwSwsSrvBcosA4alobfWiefPc9fIbR5mK3Ls0770Y3s=; b=aqM+McFgzQEB6nFZADLaUJi44SNaveutVueRrYXb9yHVhxBNwqJbg4Sib1ZNMp5Wlf9q77 gIMZWVLapEHb+mnn/b9Y66MVx9AxKMIIyC5uGgUWKw1sLeX+2thX4RLIYFmfzFw9Bv8lXG z2zgCzHrrLJfQ+JtjpbUXsuyw3wWpps= 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=BwSwsSrvBcosA4alobfWiefPc9fIbR5mK3Ls0770Y3s=; b=wfmB6gnWpnCr0AsGy1pjmk2FRv 9RZSI3vxFS6RBs4R1tZQlsDjGMbS5AgfaIQyY8ZfRUa4Z0Obe+NYQYoO8e7R/4Gt2MiyF4eHZhRpa 04GcAhvmm7iS/h1QLGYOxhyxxFto7ZTNAYSq8hoonSrTIW3QjmzBVy9hvaw9kQzam+c+YWh/OsC7/ vN8H5b3iW7d1DEOYaDwwWMp75w/Qoi7X7UiegVtM3tVbW7TPNFsyHfpA2nIdBmnDd7s88Npe2D92p 0alD3lK0uoAVqQynOao5x+eGhNarLIbFcKcxno0TpTXOOEO9PCYfs8kBEYY8lzUkrY//dXLo6d4qB F8spWMTw==; Received: from 2a02-8389-2341-5b80-39d3-4735-9a3c-88d8.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:39d3:4735:9a3c:88d8] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1rGh8p-006BRN-0o; Fri, 22 Dec 2023 15:08:47 +0000 From: Christoph Hellwig To: linux-mm@kvack.org Cc: "Matthew Wilcox (Oracle)" , Jan Kara , David Howells , Brian Foster , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Jan Kara , Dave Chinner Subject: [PATCH 08/17] writeback: Factor folio_prepare_writeback() out of write_cache_pages() Date: Fri, 22 Dec 2023 16:08:18 +0100 Message-Id: <20231222150827.1329938-9-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231222150827.1329938-1-hch@lst.de> References: <20231222150827.1329938-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: 19EF84002B X-Stat-Signature: 5t9f4orzjkihbdeno85s6o7gsdos9jh1 X-HE-Tag: 1703257728-731237 X-HE-Meta: U2FsdGVkX18US6FnMLxZO8UgOybKBkvhxe51qcOGt+fwCMa338pK1gdMnUBBL9dx33lqn1ViSDdB/uOj/gOz50c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ftQaZruwEiwphTOi2r7aFE2A0oGcSaWO7BZl6h0XQFQtFNwRoYQRCgqnOhWxVkk5Mabc47GVHL+iLHOTRFIJI7+PyuiycSuslpSae6O25qMpZCT5EAJWZo06E79bpRmzP/j68FSWxL23aY7Izjsajm7X1YwjKdaupKfkbEhTi/pGJN48dVbhTAD/I/pKXFLIszLPs6SQFuYZ7BgkDG17pHo9DNSr2+qulXzxb0Ffq5upc4vYIp+Abr2oKh62H80PrmemeFXTmlq8Aslc6RTWz/iGZCIQHr8aSeKlG+0f2osoaymwiOnKN9ccJWowon6pPU9I6cJaNRn4HNsWRj7sgVR2IUJYtdbn5rB6MT1orbWdKfTeHyNcDwrjIjo 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: 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 798e5264dc0353..fe508548482217 100644 --- a/mm/page-writeback.c +++ b/mm/page-writeback.c @@ -2397,6 +2397,38 @@ static pgoff_t wbc_end(struct writeback_control *wbc) return wbc->range_end >> PAGE_SHIFT; } +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; +} + static void writeback_get_batch(struct address_space *mapping, struct writeback_control *wbc) { @@ -2470,38 +2502,13 @@ int write_cache_pages(struct address_space *mapping, unsigned long nr; 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); nr = folio_nr_pages(folio); wbc->nr_to_write -= nr;