From patchwork Mon Feb 12 07:13:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13552750 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 51254C4829E for ; Mon, 12 Feb 2024 07:14:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E19026B0083; Mon, 12 Feb 2024 02:14:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DA1D06B0085; Mon, 12 Feb 2024 02:14:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C91F46B0087; Mon, 12 Feb 2024 02:14:26 -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 AB9B46B0083 for ; Mon, 12 Feb 2024 02:14:26 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7DF6C1606A1 for ; Mon, 12 Feb 2024 07:14:26 +0000 (UTC) X-FDA: 81782288532.24.560117C Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf12.hostedemail.com (Postfix) with ESMTP id E01A340006 for ; Mon, 12 Feb 2024 07:14:24 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=bWtEI6r6; dmarc=none; spf=none (imf12.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=1707722064; 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=Sdj3TuIooUnXttuDakRWtpzrehF7T0XwpBYd8+XY4jQ=; b=RFMti5YM8HNCJuz2ewfyuMVDfAnWwfLMu2b1u5jb6/oc5i5hclzHnw336FXtLk7GaDxxVy mFLEQQkWs+BhWE4Hu98WdLK18APU4jLmx5JMnqt6454B1/7Zm7qM38xFq61i2E3JyjO9n9 yQelSt2XhYfIFPEG7qhhNSn/Q7Myi6c= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=bWtEI6r6; dmarc=none; spf=none (imf12.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=1707722064; a=rsa-sha256; cv=none; b=Glo1yzvv6Xm5WsZFbVp/IHqk6zv182YpaLpBxBDmz1KSGO/e/0/KYWzpsqjyuJZFygaf1I z5UQlZv1j5kz1AKMUn6HM3lpB0KD614rKp76nj7hRMoNmpuiyKaxLuec7r496DwvhipAVt vZlkZ7MVOnnGDNolLrceb17Wr/XnBLM= 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=Sdj3TuIooUnXttuDakRWtpzrehF7T0XwpBYd8+XY4jQ=; b=bWtEI6r60ayNLZELpTZOYkI7ni V0YLes3NCWJScVbi3epGe0ytl7yZvhrutFXCXRDlgRfNSYcjDKED1urb/sMAXD2TtAWOT2t0SOKMD K56jZaVtX4m5/XF3bHchfDQ5+TyHEhxlaRD95cNyd7gkg2rkgPmbESs78s7lxoWGgGNgMBlygZS9n em5EpFhw6x5kD6xPyLFXSWxfrZMCZmBg5ksCZBQNvyOj58FJY9sBxyzKTaSEnV9qBViF8T2xl+0sh PfB0XRahp4DAw/qv2YFfVgEzx6/X0zRF1hjoGkeCTLsEl3dFTdqW8yCJwphVJzDOisjnV+TU79VBR 2iOpdZtg==; 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 1rZQWF-00000004Sze-2F2g; Mon, 12 Feb 2024 07:14:23 +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 , Dave Chinner Subject: [PATCH 09/14] writeback: Simplify the loops in write_cache_pages() Date: Mon, 12 Feb 2024 08:13:43 +0100 Message-Id: <20240212071348.1369918-10-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: E01A340006 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: i6rbtuhia8o3ig5z14id5groo4hszq89 X-HE-Tag: 1707722064-307141 X-HE-Meta: U2FsdGVkX18u1CcABuu3XP5Qkkqd3ZKmWZkF0Si5BFf+P6fd9YXR+fooEKOuABf5loBCtCSCt5hzzZDQb4X1fSjQKu5R+rbVbyZIfOawrVfdaGl5aFemkRFOYIQ5ZOtzXEEdi0G/DZ+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rGAG7QtviYCZiymzAp3gPFOdrep3jP2UTJJfQsvVhACUYubGxvYm+H7iNR8/HornmcVOGPMuoOOPZa851NFzRfman1ywwdrTrfahOVjVhvpdlTPEmYg4H/jb9lXOUzWPCK2DOlUUaBjOjj2Q/THRSQ4oH2E5JMTpfUHXXg6zpvEas0pD1Xi8hQGY551YL4B+cuga+WG5J2lhwtN81regJawC5SaPD4McMqaAdMuA0FEEiFz8P63fLkYfrGxnH52PrUs1K0QNOgQLutegG9bz+5nv1GygW5p/mH++j8m812Mg0V8 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: Brian Foster Reviewed-by: Jan Kara Acked-by: Dave Chinner --- mm/page-writeback.c | 75 ++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/mm/page-writeback.c b/mm/page-writeback.c index 045ca252c0423d..a94a77b1805969 100644 --- a/mm/page-writeback.c +++ b/mm/page-writeback.c @@ -2454,6 +2454,7 @@ int write_cache_pages(struct address_space *mapping, int error; struct folio *folio; pgoff_t end; /* Inclusive */ + int i = 0; if (wbc->range_cyclic) { wbc->index = mapping->writeback_index; /* prev offset */ @@ -2467,53 +2468,49 @@ int write_cache_pages(struct address_space *mapping, folio_batch_init(&wbc->fbatch); - while (wbc->index <= end) { - int i; - - writeback_get_batch(mapping, wbc); - + for (;;) { + 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++) { - folio = wbc->fbatch.folios[i]; + folio = wbc->fbatch.folios[i++]; - folio_lock(folio); - if (!folio_prepare_writeback(mapping, wbc, folio)) { - folio_unlock(folio); - continue; - } + folio_lock(folio); + if (!folio_prepare_writeback(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); - wbc->nr_to_write -= folio_nr_pages(folio); + error = writepage(folio, wbc, data); + wbc->nr_to_write -= folio_nr_pages(folio); - if (error == AOP_WRITEPAGE_ACTIVATE) { - folio_unlock(folio); - error = 0; - } + if (error == AOP_WRITEPAGE_ACTIVATE) { + folio_unlock(folio); + error = 0; + } - /* - * 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. - */ - if (wbc->sync_mode == WB_SYNC_ALL) { - if (error && !ret) - ret = error; - } else { - if (error || wbc->nr_to_write <= 0) - goto done; - } + /* + * 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. + */ + if (wbc->sync_mode == WB_SYNC_ALL) { + if (error && !ret) + ret = error; + } else { + if (error || wbc->nr_to_write <= 0) + goto done; } }