From patchwork Thu Feb 15 06:36: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: 13557440 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 7B258C4829E for ; Thu, 15 Feb 2024 06:37:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9AB726B00B1; Thu, 15 Feb 2024 01:37:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 937956B00B2; Thu, 15 Feb 2024 01:37:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 761F76B00B3; Thu, 15 Feb 2024 01:37:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 604496B00B1 for ; Thu, 15 Feb 2024 01:37:16 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 378CA120482 for ; Thu, 15 Feb 2024 06:37:16 +0000 (UTC) X-FDA: 81793081272.30.15231BF Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf05.hostedemail.com (Postfix) with ESMTP id B6C08100010 for ; Thu, 15 Feb 2024 06:37:14 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=GGTJ9HLd; spf=none (imf05.hostedemail.com: domain of BATV+a252b21bd4a653ddb6e6+7480+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+a252b21bd4a653ddb6e6+7480+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=1707979034; 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=rJ/bHu0SmYtGFgvaRb+fv4HTWCusH5CJuQRO0eXVfZ0=; b=S3Vk2syQjukWT70FY7nC9tXrj7VVB+/3htDaxSmEqwqxCTq/T6WSL+l9BB2mffbdCa0/wd eZkwnILVcp7Vqrg010Aq48yYekD7/zWxMUhVaIrAKMFbkhjVD4Y7PkbXWgKlUFIFX9lSIZ 40FguVu34XrYCzgjrVtfOx8bCshGlA0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707979034; a=rsa-sha256; cv=none; b=CSduNQjjocb6hHtYk71/DzhluiES9T3nMm0vn2iMEfsjseljXD1foNqgAfRlqmTVX37OCa GwJ2uNC5ESVb51LWc6pDmmu8T+KoP8iJAOWxz/FldYVdHq1tNkAIzXwBtvqqlxt9f+UPaK SMEtKFUcjYBkbgLab8tBJ9tEqSVG4MU= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=GGTJ9HLd; spf=none (imf05.hostedemail.com: domain of BATV+a252b21bd4a653ddb6e6+7480+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+a252b21bd4a653ddb6e6+7480+infradead.org+hch@bombadil.srs.infradead.org; dmarc=none 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=rJ/bHu0SmYtGFgvaRb+fv4HTWCusH5CJuQRO0eXVfZ0=; b=GGTJ9HLdBqJtjEDdAZPFyCXJKz 5OpCGv6U6mxH7l0ON1NV9hf/LGAvWPmgLT5xEtpqFEdbTb3JgxbZtpqS6QErp9Yn7+xFzbLI5X8Gy Kf/P1l06aiZEP55CJiJ/1dn82k66gidI9kL49JKCNwGP0FmjQLuAYAaSB4pTo5WloYMO5+NumsjRB 0U6k9DH+J1Dg67Xev9cQ1i4nBYi7tNwUyF3AcqlRBNqUINiRl+AdCmdy0Gv9to8alx4PFHbzgtWqo 3gs1KYgVW425l2kLMfdn98G37mY25LpqEV+C07/CBp/lAPCNv7QImxzejpnNHH6nTNXu+iu9/sGGI xs1Epexg==; 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.97.1 #2 (Red Hat Linux)) id 1raVMv-0000000F6ss-1jAV; Thu, 15 Feb 2024 06:37:13 +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: Thu, 15 Feb 2024 07:36:41 +0100 Message-Id: <20240215063649.2164017-7-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240215063649.2164017-1-hch@lst.de> References: <20240215063649.2164017-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: B6C08100010 X-Rspam-User: X-Stat-Signature: qytwxfksbsdpoojhonjwi9rda9heapxd X-Rspamd-Server: rspam03 X-HE-Tag: 1707979034-954787 X-HE-Meta: U2FsdGVkX1/HB7w9p+oUkXXiPYpRMW3UUZGV+Qqn5z6rRkVsBeQeShpAYbZ27HYg63zZwYrbCpVOiiNeY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H0Z1FaXWU2c2t+BYCLyqFeENxjzijw== 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 f02014007b57cc..3a1e23bed4052c 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);