From patchwork Thu Jan 25 08:57:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13530261 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 635C0C47422 for ; Thu, 25 Jan 2024 08:58:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E97056B0072; Thu, 25 Jan 2024 03:58:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E47D46B007B; Thu, 25 Jan 2024 03:58:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CE6EE6B0083; Thu, 25 Jan 2024 03:58:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B9CDB6B0072 for ; Thu, 25 Jan 2024 03:58:30 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 95566C0C57 for ; Thu, 25 Jan 2024 08:58:30 +0000 (UTC) X-FDA: 81717232380.21.84D7D4B Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf17.hostedemail.com (Postfix) with ESMTP id E26BE40013 for ; Thu, 25 Jan 2024 08:58:28 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=M2BAjIGA; spf=none (imf17.hostedemail.com: domain of BATV+b97a0fc658bf3e588113+7459+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+b97a0fc658bf3e588113+7459+infradead.org+hch@bombadil.srs.infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706173109; a=rsa-sha256; cv=none; b=LJxrkPJvaT/5H4SG8uUuuzZ3zbwZMJ4EdDdtPqIljoBXh06yeRSrXLrlf9+jCTTaz87PHP 4+MJlZtQQb5F1j+eZtpPgwiUHg79yXvjDwmis8MTwwGxROud+epvss33QpEwFYe88KzQIM 22N6StIcD5pmurj+U3cqZjRtvNVZctw= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=M2BAjIGA; spf=none (imf17.hostedemail.com: domain of BATV+b97a0fc658bf3e588113+7459+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+b97a0fc658bf3e588113+7459+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=1706173109; 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=Vkaa3agK4bmExpMMpwz6raRR84eX1JPD0T6pdU6vMG0=; b=7WWizbhLmxElZqfpqZvtLh19P52cSQ/gRWRYHN4a6PnjngIAWhtB2wpmEWpYd4mmvy2k0i DD22ELxvHBLJS7FfA7MyjOxYsJms8c0FgTZ3lEZhkQ1h3jb2c+jyGu3j6jb6pUPHdzZA+v S+37WVkfg6rzibK/JzfAcsf7FxTETiI= 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=Vkaa3agK4bmExpMMpwz6raRR84eX1JPD0T6pdU6vMG0=; b=M2BAjIGAHnJ1E+ndMgAiCOjjYp 1a+nS3OdgBz8+6pf3SQxZDnckkrJM9+/8/o7yfitpdM+slw3S/cxglf8duyGyWNHivAuCeIQ0hBka 5zjfblB5iys/OqtNw3HsMQFxI1q7UhTSjlehkJhmeryqswG1YOAGC0KJnH5LeDkL1RKTzqXvV+xI4 lGFpynQi3ruaXpEk9G6tEusnvGA4IkCRSO2MGmqi/vhP2a0hMR3Vs9ztPcuS13v3qC2y9e5faOubW RnDjBphg9mhBmn8msek0d6YR3AwsDL+dmrJtKFhBgM70Rw9kRaBaVThf1Ai1b/8smeMnJDxmKIfLz Nv/zZssQ==; 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 1rSvZ3-007QGN-10; Thu, 25 Jan 2024 08:58:25 +0000 From: Christoph Hellwig To: linux-mm@kvack.org Cc: Matthew Wilcox , Jan Kara , David Howells , Brian Foster , Christian Brauner , "Darrick J. Wong" , linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Jan Kara , Dave Chinner Subject: [PATCH 06/19] writeback: Factor out writeback_finish() Date: Thu, 25 Jan 2024 09:57:45 +0100 Message-Id: <20240125085758.2393327-7-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240125085758.2393327-1-hch@lst.de> References: <20240125085758.2393327-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-Server: rspam08 X-Rspamd-Queue-Id: E26BE40013 X-Stat-Signature: oi3k4b14sqkiqno49xq5rk1h1aej5nis X-Rspam-User: X-HE-Tag: 1706173108-224072 X-HE-Meta: U2FsdGVkX1+5tjOdOrzNeteSUY1fbeRUQDZ206EpWCXfPFJev7BlXDOWulC+xqSca2Czpp4Dyc3j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QB/vjcbwTOUWND97iddV0s3Xj0+Fbn6RM4CSwcAqHTOwLSJadh7yKxsA2a/uSFNqdxSNOTNoYoL0Sr/jrJnd/eDOXzNlaFxl5/TObtXG29GULJs2Z/37Mt/3RCQ6Df5vjURFw/+uy2ZzCuspbnZi29iusmrGvnn8t9S+pHNNs/TkaV3vpNJHKKr60k4wnTrKKYpN23Y7KV1RIW0RpbQ23EdRaqSZ1ny2yKubUqiUbDmYeVSgtqdcKY4upFX103x9Bcwr9xXp3H8kMcTU5mW6Zf5mDexU0aRtpPzITbww+2R6dxIs1iTG6CGhNJ0lWG463bfotPOr6gccLviv2MqF+yxH72dJkv2slsHyAvbEi4FyxiRG0xpjtUbg/cfRSg9em3CyQLbafn+R8E6Y4ve2bSAQW0VuqDrD8TatCP 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)" Instead of having a 'done' variable that controls the nested loops, have a writeback_finish() that can be returned directly. This involves keeping more things in writeback_control, but it's just moving stuff allocated on the stack to being allocated slightly earlier on the stack. Signed-off-by: Matthew Wilcox (Oracle) [hch: heavily rebased, reordered and commented struct writeback_control] Signed-off-by: Christoph Hellwig Reviewed-by: Jan Kara Acked-by: Dave Chinner Reviewed-by: Brian Foster --- include/linux/writeback.h | 6 +++ mm/page-writeback.c | 79 ++++++++++++++++++++------------------- 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/include/linux/writeback.h b/include/linux/writeback.h index 4b8cf9e4810bad..7d60a68fa4ea47 100644 --- a/include/linux/writeback.h +++ b/include/linux/writeback.h @@ -11,6 +11,7 @@ #include #include #include +#include struct bio; @@ -40,6 +41,7 @@ enum writeback_sync_modes { * in a manner such that unspecified fields are set to zero. */ struct writeback_control { + /* public fields that can be set and/or consumed by the caller: */ long nr_to_write; /* Write this many pages, and decrement this for each page written */ long pages_skipped; /* Pages which were not written */ @@ -77,6 +79,10 @@ struct writeback_control { */ struct swap_iocb **swap_plug; + /* internal fields used by the ->writepages implementation: */ + struct folio_batch fbatch; + int err; + #ifdef CONFIG_CGROUP_WRITEBACK struct bdi_writeback *wb; /* wb this writeback is issued under */ struct inode *inode; /* inode being written out */ diff --git a/mm/page-writeback.c b/mm/page-writeback.c index 437745a511c634..fcd90a176d806c 100644 --- a/mm/page-writeback.c +++ b/mm/page-writeback.c @@ -2360,6 +2360,29 @@ void tag_pages_for_writeback(struct address_space *mapping, } EXPORT_SYMBOL(tag_pages_for_writeback); +static void writeback_finish(struct address_space *mapping, + struct writeback_control *wbc, pgoff_t done_index) +{ + folio_batch_release(&wbc->fbatch); + + /* + * For range cyclic writeback we need to remember where we stopped so + * that we can continue there next time we are called. If we hit the + * last page and there is more work to be done, wrap back to the start + * of the file. + * + * For non-cyclic writeback we always start looking up at the beginning + * of the file if we are called again, which can only happen due to + * -ENOMEM from the file system. + */ + if (wbc->range_cyclic) { + if (wbc->err || wbc->nr_to_write <= 0) + mapping->writeback_index = done_index; + else + mapping->writeback_index = 0; + } +} + /** * 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 @@ -2395,17 +2418,12 @@ int write_cache_pages(struct address_space *mapping, struct writeback_control *wbc, writepage_t writepage, void *data) { - int ret = 0; - int done = 0; int error; - struct folio_batch fbatch; int nr_folios; pgoff_t index; pgoff_t end; /* Inclusive */ - pgoff_t done_index; xa_mark_t tag; - folio_batch_init(&fbatch); if (wbc->range_cyclic) { index = mapping->writeback_index; /* prev offset */ end = -1; @@ -2419,22 +2437,23 @@ int write_cache_pages(struct address_space *mapping, } else { tag = PAGECACHE_TAG_DIRTY; } - done_index = index; - while (!done && (index <= end)) { + + folio_batch_init(&wbc->fbatch); + wbc->err = 0; + + while (index <= end) { int i; nr_folios = filemap_get_folios_tag(mapping, &index, end, - tag, &fbatch); + tag, &wbc->fbatch); if (nr_folios == 0) break; for (i = 0; i < nr_folios; i++) { - struct folio *folio = fbatch.folios[i]; + struct folio *folio = wbc->fbatch.folios[i]; unsigned long nr; - done_index = folio->index; - folio_lock(folio); /* @@ -2481,6 +2500,9 @@ int write_cache_pages(struct address_space *mapping, folio_unlock(folio); error = 0; } + + if (error && !wbc->err) + wbc->err = error; /* * For integrity sync we have to keep going until we @@ -2496,38 +2518,19 @@ int write_cache_pages(struct address_space *mapping, * off and media errors won't choke writeout for the * entire file. */ - if (error && !ret) - ret = error; - if (wbc->sync_mode == WB_SYNC_NONE) { - if (ret || wbc->nr_to_write <= 0) { - done_index = folio->index + nr; - done = 1; - break; - } + if (wbc->sync_mode == WB_SYNC_NONE && + (wbc->err || wbc->nr_to_write <= 0)) { + writeback_finish(mapping, wbc, + folio->index + nr); + return error; } } - folio_batch_release(&fbatch); + folio_batch_release(&wbc->fbatch); cond_resched(); } - /* - * For range cyclic writeback we need to remember where we stopped so - * that we can continue there next time we are called. If we hit the - * last page and there is more work to be done, wrap back to the start - * of the file. - * - * For non-cyclic writeback we always start looking up at the beginning - * of the file if we are called again, which can only happen due to - * -ENOMEM from the file system. - */ - if (wbc->range_cyclic) { - if (done) - mapping->writeback_index = done_index; - else - mapping->writeback_index = 0; - } - - return ret; + writeback_finish(mapping, wbc, 0); + return 0; } EXPORT_SYMBOL(write_cache_pages);