From patchwork Sat Feb 3 07:11:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13543900 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 875CFC4828F for ; Sat, 3 Feb 2024 07:12:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 194286B0078; Sat, 3 Feb 2024 02:12:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0CE216B007D; Sat, 3 Feb 2024 02:12:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ED7456B007E; Sat, 3 Feb 2024 02:12:26 -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 DF85E6B0078 for ; Sat, 3 Feb 2024 02:12:26 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 8A2BE1C126A for ; Sat, 3 Feb 2024 07:12:26 +0000 (UTC) X-FDA: 81749624292.02.DF9108F Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf27.hostedemail.com (Postfix) with ESMTP id D24CE40009 for ; Sat, 3 Feb 2024 07:12:24 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=DB0SbFi6; spf=none (imf27.hostedemail.com: domain of BATV+a0ed29cff9639d748e7e+7468+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+a0ed29cff9639d748e7e+7468+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=1706944344; 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=5NQ5foZwClGdjirNb4QF1J+mdWuoRYsiFuyY0APWPYw=; b=GzmwiiK9hsAZfis70/AKgV3tvymR9JSYQ2JTYK4Mulva8VD2cYfM+bgu/+rNBZuQSFIgh6 PXlu3K9P6zed1lwAmOfAFVlIC8CrYDbQeVf3XYZ+GQLzwSxKPYbVlxn49I5H2KxQkirlQZ biAXAL0lVSD6RtHttx0cfyaTrU2UntY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706944344; a=rsa-sha256; cv=none; b=MxFyGXp+q2mq0Tx+dH2kJiBN2qUcvs22H+2qCX5igR6AZkghgOUR04Vod/yXsirGEi4u43 +QYZY6wGl3OejoDcQLJOeLdAT5zSILDZxdSgAEUE8+qX5eDROPM1/b3LTNAfGZYcENqlqL vA7jQxcTn5aJhZXuyvH6zfdo4WOdhFc= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=DB0SbFi6; spf=none (imf27.hostedemail.com: domain of BATV+a0ed29cff9639d748e7e+7468+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+a0ed29cff9639d748e7e+7468+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=5NQ5foZwClGdjirNb4QF1J+mdWuoRYsiFuyY0APWPYw=; b=DB0SbFi6hUnk1R7j5iigmiLr5N wGakeqVDiyg/3UEtSiSGuXyTjawI0qc+3zilFXplt0yfm3V6765hxCRtqBaCEEbXRu6PmcijL0EwD ohFv1B6Ru53npud78X2mRjKDz+W8KfKClXbYz//sS9Rlej5EBI3FYmzdI0NGMiwddOLtapEwUWLxn xOakmAfp6pz1HZvZdZCjEVNJYQ8OON7wbu3bQautPLcydnJS0vvYHgPkMCCowSEe3fNMXKbop8vzO KCGYuP5WzsvA6ReigLFIw1MRl26nXVPe05zKKDjdM/xBnxIJJYynLn8OYg9q/pGoHyGTfCW9NxHyR jxtv/EuA==; Received: from [89.144.222.32] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1rWACN-0000000Fk4w-1KJm; Sat, 03 Feb 2024 07:12:23 +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 Subject: [PATCH 05/13] writeback: rework the loop termination condition in write_cache_pages Date: Sat, 3 Feb 2024 08:11:39 +0100 Message-Id: <20240203071147.862076-6-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240203071147.862076-1-hch@lst.de> References: <20240203071147.862076-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: D24CE40009 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: n193iun53w86y76hd4ky4qqux8jkocfp X-HE-Tag: 1706944344-444210 X-HE-Meta: U2FsdGVkX1+TTNxBlvDJg4v7+jFxzn8UDIKBM2x+8Is7EGte4kOfDBmA9EfcC646QsPg5roZy3wvNVGi4tlzlNV0NNT9AaPENrTVwOXupRmADNSUH70BPFSzJI5mSyXj1obmPx6eEWhet3LwQnZgei6zxVk0CTR+7Hueum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Gk6Fz2hTJjGmGvbkKPo9cqNUJq9Jk1L7vz83adUyzlJury2+DYQRD+0ZTwF/juITasmYH0ggCGQ1HAVhb9f7sSlLvyrVsr4JxkfhuzoJHGQdoszSi/b4KtP7/scshwI3vrRRNnHXuIovIHh2IRuujvhU/H2xx+PByk2dbSlIlkw3vg== 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: Jan Kara Reviewed-by: Brian Foster --- 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 c7c494526bc650..88b2c4c111c01b 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: + folio_batch_release(&fbatch); + if (wbc->range_cyclic) + mapping->writeback_index = folio->index + folio_nr_pages(folio); + return error; } EXPORT_SYMBOL(write_cache_pages);