From patchwork Mon Dec 18 15:35:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13497163 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 B2C5DC35274 for ; Mon, 18 Dec 2023 15:36:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B3D9B8D0010; Mon, 18 Dec 2023 10:36:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A72FC8D0009; Mon, 18 Dec 2023 10:36:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C95B8D0010; Mon, 18 Dec 2023 10:36:23 -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 72E638D0009 for ; Mon, 18 Dec 2023 10:36:23 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 42C02C023F for ; Mon, 18 Dec 2023 15:36:23 +0000 (UTC) X-FDA: 81580340646.30.58ED8CD Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf18.hostedemail.com (Postfix) with ESMTP id 7AAC41C0029 for ; Mon, 18 Dec 2023 15:36:21 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=DfogWbTo; dmarc=none; spf=none (imf18.hostedemail.com: domain of BATV+09f18f96a25a69770120+7421+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+09f18f96a25a69770120+7421+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=1702913781; 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=9E46P8RBBTh6OS7XsshX1gEfzWzF0rjfwCHf/2TCoS8=; b=ZjDsCmQ5LsDYiFqq+jXCYjfRGSq9ff0JMpe5Ssr12RfNM8nB+BZW2/PvK5y5cJNlOQ76AG idWypeb/yAK4QOgs+PGWT/4KI8lEglRnJGVwSvJP5zdxPmXWRubs/6fbKeRNDEE8caQdSh qQqwUzhrhF60AKFHWQZ7yDJkEQyJoH8= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=DfogWbTo; dmarc=none; spf=none (imf18.hostedemail.com: domain of BATV+09f18f96a25a69770120+7421+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+09f18f96a25a69770120+7421+infradead.org+hch@bombadil.srs.infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702913781; a=rsa-sha256; cv=none; b=PKfnflueBLopSirMnz0hxQNjJIXs2BpfMPWB+8IdXjOG6JJy2yaw7ne7nWJkJYeTXBGOLN swZRhYATeAQHTxJitTmG0bcp49OIlmk8iqgzyclcC6bAwmq2WA5jZHJWjx2xk6hpFTEnTX 1GIaFZmCrsOcWQ7iG+VlEySki6ps/hw= 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=9E46P8RBBTh6OS7XsshX1gEfzWzF0rjfwCHf/2TCoS8=; b=DfogWbTovC+MaOjnIwtN1k+ufe 43Ts4mac1JK2rDHqW2s9mobtENbKUfR9BFoKwLFg1BcjgMQ/GLdPgjocsslPLAvmryBkB+L6aBzFd 4f2bI16jaiAJ26Qxe8VT2ft+yHYcDpYUO4wZZ0CHOua1wdkZm/mH7Yml1PKCixNZ4/egD9MPx9yG5 v8SBQAHvbwgiAd3NzfUQUr96kLLHmE17pRAuOmQu3Y0G9xI54rO+mUK3+sHk+j57wTC5j5rMCEMXx +wxkIw4M6tuiC9vd/QjEMHqBqbxQtz1uRFx2lutS1EQnjOZt5+jkyHdqmKI58AU1hOHa774zf+XRm vMF4vlvQ==; 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 1rFFfH-00BEQj-3D; Mon, 18 Dec 2023 15:36:20 +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 Subject: [PATCH 09/17] writeback: Simplify the loops in write_cache_pages() Date: Mon, 18 Dec 2023 16:35:45 +0100 Message-Id: <20231218153553.807799-10-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231218153553.807799-1-hch@lst.de> References: <20231218153553.807799-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: 7AAC41C0029 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 6feqr19i7i77r16oc489xdt89s38ofyc X-HE-Tag: 1702913781-502342 X-HE-Meta: U2FsdGVkX1/UC+KjGVQMghV7IGKLGhi3FT1TDlLghGKy5PBHHF0mfGLPxRJnHKVlnGhU/4LjLC7qVPXKZbrTCRkvW2yxRPHUe42lYg3jVgnx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cv76sU0KVwqIJFWDoks5ff1qRzTbjqs+PZ7bmS1CpD2QAAJKd5OSDOFONeLC3z8Oj5ZMkWU6fOOUeOanLyECtuBkvluTT7tAWZut0nnkW8dvhnB/7vpbidKHytkvxVcqjlnnhHiGKFxSlJS1jNg7Nmh/xb+yUdiHAWWj6u3d6XpyufLI2jM6TGhkBZWIUJx/ta5fLSltiTHf9y2bhcEhVrVkJfOaBFbq0PlYnFM8NyFZHs0KhgBWuDZgWXkEmCh6dZNmWLNkO4GiLpYcrk6cAMFV+fLVorTJU7HGD26WiZDUbQyP/dAlZbebld1VJzlC1s/tbqNeQDy2xb/bBMc= 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)" Collapse the two nested loops into one. This is needed as a step towards turning this into an iterator. Note that this drops the "index <= end" check in the previous outer loop and just relies on filemap_get_folios_tag() to return 0 entries when index > end. This actually has a subtle implication when end == -1 because then the returned index will be -1 as well and thus if there is page present on index -1, we could be looping indefinitely. But as the comment in filemap_get_folios_tag documents this as already broken anyway we should not worry about it here either. The fix for that would probably a change to the filemap_get_folios_tag() calling convention. Signed-off-by: Matthew Wilcox (Oracle) [hch: updated the commit log based on feedback from Jan Kara] Signed-off-by: Christoph Hellwig Reviewed-by: Jan Kara --- mm/page-writeback.c | 94 ++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/mm/page-writeback.c b/mm/page-writeback.c index 2a004c0b9bdfbf..c7983ea3040be4 100644 --- a/mm/page-writeback.c +++ b/mm/page-writeback.c @@ -2473,6 +2473,7 @@ int write_cache_pages(struct address_space *mapping, { int error; pgoff_t end; /* Inclusive */ + int i = 0; if (wbc->range_cyclic) { wbc->index = mapping->writeback_index; /* prev offset */ @@ -2487,63 +2488,60 @@ int write_cache_pages(struct address_space *mapping, folio_batch_init(&wbc->fbatch); wbc->err = 0; - while (wbc->index <= end) { - int i; - - writeback_get_batch(mapping, wbc); + for (;;) { + struct folio *folio; + unsigned long nr; + if (i == wbc->fbatch.nr) { + writeback_get_batch(mapping, wbc); + i = 0; + } if (wbc->fbatch.nr == 0) break; - for (i = 0; i < wbc->fbatch.nr; i++) { - struct folio *folio = wbc->fbatch.folios[i]; - unsigned long nr; + folio = wbc->fbatch.folios[i++]; - folio_lock(folio); - if (!should_writeback_folio(mapping, wbc, folio)) { - folio_unlock(folio); - continue; - } + folio_lock(folio); + if (!should_writeback_folio(mapping, wbc, folio)) { + folio_unlock(folio); + continue; + } - trace_wbc_writepage(wbc, inode_to_bdi(mapping->host)); + trace_wbc_writepage(wbc, inode_to_bdi(mapping->host)); - error = writepage(folio, wbc, data); - nr = folio_nr_pages(folio); - wbc->nr_to_write -= nr; + error = writepage(folio, wbc, data); + nr = folio_nr_pages(folio); + wbc->nr_to_write -= nr; - /* - * Handle the legacy AOP_WRITEPAGE_ACTIVATE magic return - * value. Eventually all instances should just unlock - * the folio themselves and return 0; - */ - if (error == AOP_WRITEPAGE_ACTIVATE) { - folio_unlock(folio); - error = 0; - } - - if (error && !wbc->err) - wbc->err = error; + /* + * Handle the legacy AOP_WRITEPAGE_ACTIVATE magic return value. + * Eventually all instances should just unlock the folio + * themselves and return 0; + */ + if (error == AOP_WRITEPAGE_ACTIVATE) { + folio_unlock(folio); + error = 0; + } - /* - * For integrity sync we have to keep going until we - * have written all the folios we tagged for writeback - * prior to entering this loop, even if we run past - * wbc->nr_to_write or encounter errors. This is - * because the file system may still have state to clear - * for each folio. We'll eventually return the first - * error encountered. - * - * For background writeback just push done_index past - * this folio so that we can just restart where we left - * off and media errors won't choke writeout for the - * entire file. - */ - if (wbc->sync_mode == WB_SYNC_NONE && - (wbc->err || wbc->nr_to_write <= 0)) { - writeback_finish(mapping, wbc, - folio->index + nr); - return error; - } + if (error && !wbc->err) + wbc->err = error; + + /* + * For integrity sync we have to keep going until we have + * written all the folios we tagged for writeback prior to + * entering this loop, even if we run past wbc->nr_to_write or + * encounter errors. This is because the file system may still + * have state to clear for each folio. We'll eventually return + * the first error encountered. + * + * For background writeback just push done_index past this folio + * so that we can just restart where we left off and media + * errors won't choke writeout for the entire file. + */ + if (wbc->sync_mode == WB_SYNC_NONE && + (wbc->err || wbc->nr_to_write <= 0)) { + writeback_finish(mapping, wbc, folio->index + nr); + return error; } }