From patchwork Mon Feb 12 07:13:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13552747 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 499B4C48297 for ; Mon, 12 Feb 2024 07:14:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D085A6B0080; Mon, 12 Feb 2024 02:14:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CB85B6B0081; Mon, 12 Feb 2024 02:14:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B59438D0001; Mon, 12 Feb 2024 02:14:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A37A66B0080 for ; Mon, 12 Feb 2024 02:14:18 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 83A07A06A0 for ; Mon, 12 Feb 2024 07:14:18 +0000 (UTC) X-FDA: 81782288196.11.E5CF01B Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf15.hostedemail.com (Postfix) with ESMTP id E419CA0011 for ; Mon, 12 Feb 2024 07:14:16 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=nfOtutd8; dmarc=none; spf=none (imf15.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=1707722057; 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=To4jTyxCYxUY5wd7w+9Le458r9Ys3/VfyFqqGfNjFPc=; b=vBVKW9QimrGJaGNBudOit5d0YCZCTBqRj/MA/pC8uc//b2ynHxfv3GYqHyLADB9Um8KyQO jdzQTLXXE0K+gQYDTHlYAx74CqRkVtwN3y6okcyaoyRuhZfGwzd3MeWmr+ASXlostrZKQ5 iWQt4dWZieqPBxnSOQTT9m68ntH04yA= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=nfOtutd8; dmarc=none; spf=none (imf15.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=1707722057; a=rsa-sha256; cv=none; b=Pub+kPHpwXf23YdAuwbB9jjyaAix64fg53H2h2G96B+Uo1joSh3PF7YKhcabKHb9RtEGLb TdLcByir6v36PAOdwtLVGPXjYSrYp4kC/W3Ygz9oYVjyaUJsdV0cn98bgR0CIObE0dB+Id QU7aFWSqr9in4VgL+ExKHX/2198GSjY= 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=To4jTyxCYxUY5wd7w+9Le458r9Ys3/VfyFqqGfNjFPc=; b=nfOtutd8j/0yHOPqqs1KM8fcvG q75YNKjRHxoc4Blgsq58A8PsYUG7YdvL+tmtdrQdsxoxjEgr/IjOWJBkBvMs+l5cmHKgcjd8uv+Kp 6Ol6MRgsBc4zfotAveb+QxwCtqStBJfTcZsbd+o4cxI/Spis1YF5p1g67mL5OBPIwz1jE8rVpopVk +kfLdFl+bliXGifeP7QdRNGixZVQ7R48kHZkHkKkbNGGFGtUCLQRyqq9BHODEkvg0et9bymQbGSWO agC+LF8N1vhEkgfFNyGl7XtKg3Cl9fUsChZREhpyU0ujaUfgWS5nFXu0L8mJuYizPNtdFfLAfkXJN 8s709i4g==; 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 1rZQW6-00000004Su2-363S; Mon, 12 Feb 2024 07:14:15 +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 Subject: [PATCH 06/14] writeback: rework the loop termination condition in write_cache_pages Date: Mon, 12 Feb 2024 08:13:40 +0100 Message-Id: <20240212071348.1369918-7-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-Rspamd-Queue-Id: E419CA0011 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: e5y9m64amiu5r8o6u7ndtphc6hffc7z1 X-HE-Tag: 1707722056-605495 X-HE-Meta: U2FsdGVkX19E10bU38fvLjbrTrZgs4nT04tt0xN12tV4TIiZraBl7h0+4u29VzLLfKdUoRIO5YJ3dhm4ZLE08t93FVf6gd64+X9U8H/smFtj9c2jpK7ECEmM0ZtB2u80G1tjWskJe2v37gOJEK2v0ecHzdn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9qNoe10XyicZgnGpCcJbV0OTrU/lUqdKuMEW6y0n6oFwcGV1WrMjgZ0D1MEDzxaKvvE1ex7EqlvWRK/bDLbp3dFQuvViVjYGSo1Puhw9nC9gFelanUXUBZfG8mgWOjuwHuHVN+go63BZl9pbXFgyD0GdnWl3gdSHQlm9B7F4wCoVHj4BARMtNwvxrtz55HqmQuvdzXikQZFj9M0eXkIayFf4N23+CZ1HlvCVwKPNHuW5uWx8xM5YJ7tDg= 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: Rework the way we deal with the cleanup after the writepage call. First handle the magic AOP_WRITEPAGE_ACTIVATE separately from real error returns to get it out of the way of the actual error handling path. The split the handling on intgrity vs non-integrity branches first, and return early using a goto for the non-ingegrity early loop condition to remove the need for the done and done_index local variables, and for assigning the error to ret when we can just return error directly. Signed-off-by: Christoph Hellwig Reviewed-by: Brian Foster Reviewed-by: Jan Kara --- mm/page-writeback.c | 84 ++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 51 deletions(-) diff --git a/mm/page-writeback.c b/mm/page-writeback.c index 4d862f196d1f05..b49ee15a863e99 100644 --- a/mm/page-writeback.c +++ b/mm/page-writeback.c @@ -2396,13 +2396,12 @@ int write_cache_pages(struct address_space *mapping, void *data) { int ret = 0; - int done = 0; int error; struct folio_batch fbatch; + struct folio *folio; int nr_folios; pgoff_t index; pgoff_t end; /* Inclusive */ - pgoff_t done_index; xa_mark_t tag; folio_batch_init(&fbatch); @@ -2419,8 +2418,7 @@ int write_cache_pages(struct address_space *mapping, } else { tag = PAGECACHE_TAG_DIRTY; } - done_index = index; - while (!done && (index <= end)) { + while (index <= end) { int i; nr_folios = filemap_get_folios_tag(mapping, &index, end, @@ -2430,11 +2428,7 @@ int write_cache_pages(struct address_space *mapping, break; for (i = 0; i < nr_folios; i++) { - struct folio *folio = fbatch.folios[i]; - unsigned long nr; - - done_index = folio->index; - + folio = fbatch.folios[i]; folio_lock(folio); /* @@ -2469,45 +2463,32 @@ int write_cache_pages(struct address_space *mapping, trace_wbc_writepage(wbc, inode_to_bdi(mapping->host)); error = writepage(folio, wbc, data); - nr = folio_nr_pages(folio); - wbc->nr_to_write -= nr; - if (unlikely(error)) { - /* - * Handle errors according to the type of - * writeback. There's no need to continue for - * background writeback. Just push done_index - * past this page so media errors won't choke - * writeout for the entire file. For integrity - * writeback, we must process the entire dirty - * set regardless of errors because the fs may - * still have state to clear for each page. In - * that case we continue processing and return - * the first error. - */ - if (error == AOP_WRITEPAGE_ACTIVATE) { - folio_unlock(folio); - error = 0; - } else if (wbc->sync_mode != WB_SYNC_ALL) { - ret = error; - done_index = folio->index + nr; - done = 1; - break; - } - if (!ret) - ret = error; + wbc->nr_to_write -= folio_nr_pages(folio); + + if (error == AOP_WRITEPAGE_ACTIVATE) { + folio_unlock(folio); + error = 0; } /* - * We stop writing back only if we are not doing - * integrity sync. In case of integrity sync we have to - * keep going until we have written all the pages - * we tagged for writeback prior to entering this loop. + * For integrity writeback we have to keep going until + * we have written all the folios we tagged for + * writeback above, even if we run past wbc->nr_to_write + * or encounter errors. + * We stash away the first error we encounter in + * wbc->saved_err so that it can be retrieved when we're + * done. This is because the file system may still have + * state to clear for each folio. + * + * For background writeback we exit as soon as we run + * past wbc->nr_to_write or encounter the first error. */ - done_index = folio->index + nr; - if (wbc->nr_to_write <= 0 && - wbc->sync_mode == WB_SYNC_NONE) { - done = 1; - break; + if (wbc->sync_mode == WB_SYNC_ALL) { + if (error && !ret) + ret = error; + } else { + if (error || wbc->nr_to_write <= 0) + goto done; } } folio_batch_release(&fbatch); @@ -2524,14 +2505,15 @@ int write_cache_pages(struct address_space *mapping, * 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; - } - + if (wbc->range_cyclic) + mapping->writeback_index = 0; return ret; + +done: + if (wbc->range_cyclic) + mapping->writeback_index = folio->index + folio_nr_pages(folio); + folio_batch_release(&fbatch); + return error; } EXPORT_SYMBOL(write_cache_pages);