From patchwork Fri Dec 22 15:08:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13503417 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 B4795C41535 for ; Fri, 22 Dec 2023 15:08:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DD7D88D0002; Fri, 22 Dec 2023 10:08:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D38D36B008C; Fri, 22 Dec 2023 10:08:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B894F8D0002; Fri, 22 Dec 2023 10:08:52 -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 A0AFE6B008A for ; Fri, 22 Dec 2023 10:08:52 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 750DEA0EE7 for ; Fri, 22 Dec 2023 15:08:52 +0000 (UTC) X-FDA: 81594786504.24.7EBA403 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf01.hostedemail.com (Postfix) with ESMTP id DCFC840028 for ; Fri, 22 Dec 2023 15:08:50 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=yIAvYy07; dmarc=none; spf=none (imf01.hostedemail.com: domain of BATV+af923ccb694ecd533eae+7425+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+af923ccb694ecd533eae+7425+infradead.org+hch@bombadil.srs.infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703257730; a=rsa-sha256; cv=none; b=cg86AE7TPNl8QLvBHrlbbeBiGVsDWf7UsEj3mVY1iYu5idWUH2ZcGmyCHKx06KBfCSu6gq MAwAvAa47ZYPxlRNq6DLiHGa2jkdqQXsJLshrnhLoD/qNRcyG/g1cltn+cPhsamcAsabOr Qz1VmkyM93Crs4tIUxYYKHRTwDdw0OI= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=yIAvYy07; dmarc=none; spf=none (imf01.hostedemail.com: domain of BATV+af923ccb694ecd533eae+7425+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+af923ccb694ecd533eae+7425+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=1703257730; 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=3uxekAKYtWjU4xunWw9AHOgXL8ikM7hINi+MFUVXD1Q=; b=QKSE5v0lD35KN1gfjBGVTzaVKc1KmbyBYcnvDgDjPSycppzIVsuMtmGoR980Q8jEyVYOqB qwPsuoOXagtGlgY+XIT4yC5dObYh5qwLaEbpxKFZZ6TDVrt4MdRUstePzZtR2BtmmJCE+o GlSsp8qLXkb1NuQ15kVzu2k0zBMe/es= 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=3uxekAKYtWjU4xunWw9AHOgXL8ikM7hINi+MFUVXD1Q=; b=yIAvYy07YYVywxCYNd4RPpUC2z hov+bjdw7XI7TCb4f222lmJzvS4zTCCXlwyPjJvhMKKBCYuLTj719RJP03bEqb7Ny/AEXGYoBrHcJ bQlB9PsdksaKg3Kudutgim4vY/WWFKu+9i5EiMoYzhnkQ2fMUEncbFuxCNqzsW6Qo2DuXMnVs1neU 4eh+YkvnDEvwvL3fOe1ac3QeobScCtYlB/jENSX9n2lKGLzBwJBD4XxjurornDUhLw7t0P15+/sNC Hl3HtLu61rDDl8jjubtzUWofoOftxS277w6sKdRzINQQXfSfWcYjG92SuUr1GMB27Gab9/JW6O5bL q6JhGxDQ==; 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 1rGh8r-006BSh-23; Fri, 22 Dec 2023 15:08:50 +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, Jan Kara , Dave Chinner Subject: [PATCH 09/17] writeback: Simplify the loops in write_cache_pages() Date: Fri, 22 Dec 2023 16:08:19 +0100 Message-Id: <20231222150827.1329938-10-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231222150827.1329938-1-hch@lst.de> References: <20231222150827.1329938-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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: DCFC840028 X-Stat-Signature: 96ifqspg6hzkkai67o9s4u5gfj5wnhe3 X-HE-Tag: 1703257730-683046 X-HE-Meta: U2FsdGVkX1/AskXJ/qLnwrqpnGYIXRHIB5j1rZZ7ybNkKkH0d0cqAf3dfJsiZdSPtIE+WQmllVwYVc3tbCrWCGngF3BvvlnOTNNUpY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5W7KuoSiewii4gr3Bi25rRHJA== 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 Acked-by: Dave Chinner --- 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 fe508548482217..d62bc3498ed975 100644 --- a/mm/page-writeback.c +++ b/mm/page-writeback.c @@ -2475,6 +2475,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 */ @@ -2489,63 +2490,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 (!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); - 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; } }