From patchwork Thu Dec 14 13:25:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13493055 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 D7C7CC4332F for ; Thu, 14 Dec 2023 13:26:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 71BCE8D00B4; Thu, 14 Dec 2023 08:26:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6CBB18D00B1; Thu, 14 Dec 2023 08:26:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 593498D00B4; Thu, 14 Dec 2023 08:26:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4A4C98D00B1 for ; Thu, 14 Dec 2023 08:26:00 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 22F77A22AB for ; Thu, 14 Dec 2023 13:26:00 +0000 (UTC) X-FDA: 81565496880.27.F18EAE5 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf11.hostedemail.com (Postfix) with ESMTP id 6F4AC4000E for ; Thu, 14 Dec 2023 13:25:57 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=ztKYDWSI; spf=none (imf11.hostedemail.com: domain of BATV+55581d165dda9d0af87b+7417+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+55581d165dda9d0af87b+7417+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=1702560357; 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=dGiI8OkHMxqBpcMGTmwSGDbSvODrydoaDrjFK0YrpEE=; b=dzJJye7WIlN/DPrAO3LkvzPeeAblv2GMgRxlmNSr5ygsQQvjin3w2PwJAVfq5Y5TRe10+D Rj7BJZhrnThUyUVPFdoH0Gp8aF34p85U7Umd3IvTPEegwXEHaS8MfpVJOREEhdl2C2U/mj wmXVS6BO8QutSg8O3CGtelmUw6qJ1bs= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=ztKYDWSI; spf=none (imf11.hostedemail.com: domain of BATV+55581d165dda9d0af87b+7417+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+55581d165dda9d0af87b+7417+infradead.org+hch@bombadil.srs.infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702560357; a=rsa-sha256; cv=none; b=5MWxJkAqadzD9KtfbV7gmo6TDdPGqtk4uFkNGB81Ibcoc/h9Zk6HGQEiRsc3FP0hlU6B+q qR8Qn8pDafQaB4YQvRE+Nb1sxDvYgh/N/ZQrTC+/TxQG0Iz9wVLaCSa2vWp/yVbsbXCmOw Nbf0RMxJgymkJxSKEWu823QJCY7QEi0= 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=dGiI8OkHMxqBpcMGTmwSGDbSvODrydoaDrjFK0YrpEE=; b=ztKYDWSIzMbt21zCgDHn0IQVdR WEnQpPcfNE9IlRkh+DfxnVLV9Y7bB90QZSd/Lgm77je6hoN3kXz5/FjSVlwy075Pt5Qfc+OGTIGDA /RRDRJbdv3i7JmHPk6/sfyN27+59AhFDskZx5dg8aL2TD08X8OUYfLgW7aYfX/Zk6CX/bX53bbbSb iWXq5RbHXwlywgPE8elTNw4eOQlPgFXKwCnb2QiLjjeLh9DMPsJmIO6KbmVgsmrTBrVyM5Ucz9Kyu jeStapBP9fjfHrc+av6YzxBJtg9mqdk3JliqK0MEFezGFRPl+cEs7bc1E/jKgORcUtfPBSE/Ay7R0 KOPvYEGw==; Received: from [88.128.88.27] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1rDliu-000N0o-0h; Thu, 14 Dec 2023 13:25:56 +0000 From: Christoph Hellwig To: linux-mm@kvack.org Cc: "Matthew Wilcox (Oracle)" , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Jan Kara , David Howells Subject: [PATCH 01/11] writeback: Factor out writeback_finish() Date: Thu, 14 Dec 2023 14:25:34 +0100 Message-Id: <20231214132544.376574-2-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231214132544.376574-1-hch@lst.de> References: <20231214132544.376574-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: 6F4AC4000E X-Rspam-User: X-Stat-Signature: t8kks1h4zht6ifegzc3dnthxsrnxjymm X-Rspamd-Server: rspam01 X-HE-Tag: 1702560357-550505 X-HE-Meta: U2FsdGVkX1+CX5ACfjid/RmeU0NK81endrR36fxkUl4mbixT9yizVlKhiIA//w5oWSTspEUm2QdkjH4Q4J9c0UI/fTf2Ah1Eg6HfhK0GWdKLjJXpnQePnUfWqjYJjHr3yO/KtjtzRnu9cF2HySB7GIoTz0k4sRl2aUmL9KnBgX7NI3VTY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YQiCRqUmml1zlnN9Uiptzxbz9FXOJexuTOqGBEE/Key1vtYP6JYIkYvUP4xJUtYMjpLCC/vrUfSQzFOC2ZQXKZx3xvrT5sFAoRg6tdZXJxiJB0ER9l5gdE3N8Lyq6ZtxLMGm/D0L27CvXvcDkZEHWem1Man9w6p682v+TCwTVxyS8njRtCFlEofZCjODH3CJpnNu+JD7J2ievofmbvFdEVA0s4+c8Zxj3hvMb3I2NtmTquhMEii5hKRsdz5wyGLxop+0hmW3QBRaa7PTc= 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)" Instead of having a 'done' variable that controls the nested loops, have a writeback_finish() that can be returned directly. This involves keeping more things in writeback_control, but it's just moving stuff allocated on the stack to being allocated slightly earlier on the stack. Signed-off-by: Matthew Wilcox (Oracle) [hch: reorder and comment struct writeback_control] Signed-off-by: Christoph Hellwig --- include/linux/writeback.h | 8 +++++ mm/page-writeback.c | 72 +++++++++++++++++++++------------------ 2 files changed, 46 insertions(+), 34 deletions(-) diff --git a/include/linux/writeback.h b/include/linux/writeback.h index 083387c00f0c8b..05e8add4b5ae3c 100644 --- a/include/linux/writeback.h +++ b/include/linux/writeback.h @@ -11,6 +11,7 @@ #include #include #include +#include struct bio; @@ -40,6 +41,7 @@ enum writeback_sync_modes { * in a manner such that unspecified fields are set to zero. */ struct writeback_control { + /* public fields that can be set and/or consumed by the caller: */ long nr_to_write; /* Write this many pages, and decrement this for each page written */ long pages_skipped; /* Pages which were not written */ @@ -77,6 +79,12 @@ struct writeback_control { */ struct swap_iocb **swap_plug; + /* internal fields used by the ->writepages implementation: */ + struct folio_batch fbatch; + pgoff_t done_index; + int err; + unsigned range_whole:1; /* entire file */ + #ifdef CONFIG_CGROUP_WRITEBACK struct bdi_writeback *wb; /* wb this writeback is issued under */ struct inode *inode; /* inode being written out */ diff --git a/mm/page-writeback.c b/mm/page-writeback.c index ee2fd6a6af4072..45309f3b8193f8 100644 --- a/mm/page-writeback.c +++ b/mm/page-writeback.c @@ -2360,6 +2360,24 @@ void tag_pages_for_writeback(struct address_space *mapping, } EXPORT_SYMBOL(tag_pages_for_writeback); +static int writeback_finish(struct address_space *mapping, + struct writeback_control *wbc, bool done) +{ + folio_batch_release(&wbc->fbatch); + + /* + * If we hit the last page and there is more work to be done: + * wrap the index back to the start of the file for the next + * time we are called. + */ + if (wbc->range_cyclic && !done) + wbc->done_index = 0; + if (wbc->range_cyclic || (wbc->range_whole && wbc->nr_to_write > 0)) + mapping->writeback_index = wbc->done_index; + + return wbc->err; +} + /** * write_cache_pages - walk the list of dirty pages of the given address space and write all of them. * @mapping: address space structure to write @@ -2395,18 +2413,12 @@ int write_cache_pages(struct address_space *mapping, struct writeback_control *wbc, writepage_t writepage, void *data) { - int ret = 0; - int done = 0; int error; - struct folio_batch fbatch; int nr_folios; pgoff_t index; pgoff_t end; /* Inclusive */ - pgoff_t done_index; - int range_whole = 0; xa_mark_t tag; - folio_batch_init(&fbatch); if (wbc->range_cyclic) { index = mapping->writeback_index; /* prev offset */ end = -1; @@ -2414,7 +2426,7 @@ int write_cache_pages(struct address_space *mapping, index = wbc->range_start >> PAGE_SHIFT; end = wbc->range_end >> PAGE_SHIFT; if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) - range_whole = 1; + wbc->range_whole = 1; } if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) { tag_pages_for_writeback(mapping, index, end); @@ -2422,21 +2434,25 @@ int write_cache_pages(struct address_space *mapping, } else { tag = PAGECACHE_TAG_DIRTY; } - done_index = index; - while (!done && (index <= end)) { + + wbc->done_index = index; + folio_batch_init(&wbc->fbatch); + wbc->err = 0; + + while (index <= end) { int i; nr_folios = filemap_get_folios_tag(mapping, &index, end, - tag, &fbatch); + tag, &wbc->fbatch); if (nr_folios == 0) break; for (i = 0; i < nr_folios; i++) { - struct folio *folio = fbatch.folios[i]; + struct folio *folio = wbc->fbatch.folios[i]; unsigned long nr; - done_index = folio->index; + wbc->done_index = folio->index; folio_lock(folio); @@ -2490,13 +2506,13 @@ int write_cache_pages(struct address_space *mapping, folio_unlock(folio); error = 0; } else if (wbc->sync_mode != WB_SYNC_ALL) { - ret = error; - done_index = folio->index + nr; - done = 1; - break; + wbc->err = error; + wbc->done_index = folio->index + nr; + return writeback_finish(mapping, + wbc, true); } - if (!ret) - ret = error; + if (!wbc->err) + wbc->err = error; } /* @@ -2507,26 +2523,14 @@ int write_cache_pages(struct address_space *mapping, */ wbc->nr_to_write -= nr; if (wbc->nr_to_write <= 0 && - wbc->sync_mode == WB_SYNC_NONE) { - done = 1; - break; - } + wbc->sync_mode == WB_SYNC_NONE) + return writeback_finish(mapping, wbc, true); } - folio_batch_release(&fbatch); + folio_batch_release(&wbc->fbatch); cond_resched(); } - /* - * If we hit the last page and there is more work to be done: wrap - * back the index back to the start of the file for the next - * time we are called. - */ - if (wbc->range_cyclic && !done) - done_index = 0; - if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) - mapping->writeback_index = done_index; - - return ret; + return writeback_finish(mapping, wbc, false); } EXPORT_SYMBOL(write_cache_pages);