From mboxrd@z Thu Jan 1 00:00:00 1970 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 D672FC46CD2 for ; Tue, 30 Jan 2024 10:46:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 700F86B007E; Tue, 30 Jan 2024 05:46:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B0186B0081; Tue, 30 Jan 2024 05:46:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 52A316B0085; Tue, 30 Jan 2024 05:46:12 -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 3FAF66B007E for ; Tue, 30 Jan 2024 05:46:12 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0EA9E1607A8 for ; Tue, 30 Jan 2024 10:46:12 +0000 (UTC) X-FDA: 81735647784.08.92200C9 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf18.hostedemail.com (Postfix) with ESMTP id B25F61C001C for ; Tue, 30 Jan 2024 10:46:09 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=gtMjDf5s; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=4wMoKlQh; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=Tg8PAfjr; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=8q27gk6s; dmarc=none; spf=pass (imf18.hostedemail.com: domain of jack@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=jack@suse.cz ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706611570; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Rd4laTPRq+btQlxHww6bI0r93gTPlM9zRmVQJZwHTLA=; b=wWjewQnnVxFgmHFUeDCUbW0Ztyif2sUTndubVpgUJ9QZbFnrwoNN0cvxUP/pCmuC4/sk1k 79jMoS5l/vNvnQkKpQEgL4TbBwy+y6nHHBfp9TDvUy4yEJfrXLJ2cOGmMdKAku8vhFt5RP PYqCu8p+soeZqEAmdJwVY7WaAJ405eo= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=gtMjDf5s; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=4wMoKlQh; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=Tg8PAfjr; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=8q27gk6s; dmarc=none; spf=pass (imf18.hostedemail.com: domain of jack@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=jack@suse.cz ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706611570; a=rsa-sha256; cv=none; b=UnJDRLCKanXZ40KMg6B0a83t7d5Fwu4Zd+p+UjmFjIWK0WskecJdRQ0PgyP5Doq5fHsdTs om+Eb9EM4H09vNSYFx756XmHxIK6d/BiisuYLQNnTcdC0DCtwlHv/SEU8G6OaD4YGrghEI l1D7/6pwXz2in/b4wmXxhQX7okWnk5U= Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [10.150.64.98]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 19B95222D9; Tue, 30 Jan 2024 10:46:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1706611568; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Rd4laTPRq+btQlxHww6bI0r93gTPlM9zRmVQJZwHTLA=; b=gtMjDf5sG1IhZDPGpqVgyLrBptiO3pdwyR6jSHrYN3bjxdtYkzIxL2PdF50p5YbseJZvnB /8H4Jly+YqPnRVFvwKAu/RMtXGeT7FRjpkzT2Eb/4nbw8rD9aagEZl/l1UN4VxX+C7fxon XAiihgKbqPn2n0nVEMN864QKS4y3gSE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1706611568; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Rd4laTPRq+btQlxHww6bI0r93gTPlM9zRmVQJZwHTLA=; b=4wMoKlQhXkMR2/C0sJSU6HKIX2Sd1aklC78v8F5E2KwhCPXc9JhMdN2tmMHj0d7a9tnQ/4 UQ6Xa2pWGryJfGBg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1706611566; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Rd4laTPRq+btQlxHww6bI0r93gTPlM9zRmVQJZwHTLA=; b=Tg8PAfjrkj0kivWc0IYSydT3PR5xS+P2VVhiWGnnx1hJtX0IXfcISdM7fvXSE1WYmaaMYW g8M970sxT6nF49Oh5mS9SwocltuFO21lkg53abt1EhUmhYbMHrV/34/v/VDwFExs7m2kZ9 E1VMnW+MFWmCm3eWtvfKHQqBDXZN2nM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1706611566; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Rd4laTPRq+btQlxHww6bI0r93gTPlM9zRmVQJZwHTLA=; b=8q27gk6sjeXkl3adNWaO5+zphzC4qxeJ/QthFB/SGTHyc6MFq2gEf+K5pJdUP2/43lWE9q E7ieQCnAT+r3X2BQ== Received: from imap2.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap2.dmz-prg2.suse.org (Postfix) with ESMTPS id EE27413212; Tue, 30 Jan 2024 10:46:05 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id 1dscOm3TuGVEfQAAn2gu4w (envelope-from ); Tue, 30 Jan 2024 10:46:05 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 8D741A0807; Tue, 30 Jan 2024 11:46:05 +0100 (CET) Date: Tue, 30 Jan 2024 11:46:05 +0100 From: Jan Kara To: Christoph Hellwig Cc: linux-mm@kvack.org, 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: Re: [PATCH 19/19] writeback: simplify writeback iteration Message-ID: <20240130104605.2i6mmdncuhwwwfin@quack3> References: <20240125085758.2393327-1-hch@lst.de> <20240125085758.2393327-20-hch@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240125085758.2393327-20-hch@lst.de> X-Rspam-User: X-Stat-Signature: gzjce1648wwwcumufb88yd3ra38o4ffq X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B25F61C001C X-HE-Tag: 1706611569-820865 X-HE-Meta: U2FsdGVkX18WTBsdlygGHqX6iIf6+jc6UZMCzrf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QYfqgv0cdCU1UYKk0sIdW3dSsNn1YDHTj0e8Ga9BP5AzZdz/Tmty4M9+0NC0dGn5RSJnq1BEvqnW7Ejb2UF8N/uFw5ISFhNzTO4qMxJhNIjsk0MoQiF3myplmz6IMfmgrsfFpekt/gnYAr8AU2iyZEEQgQHWn2/pdTdsrZnDhLqvH9H9TXJN7K 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: On Thu 25-01-24 09:57:58, Christoph Hellwig wrote: > Based on the feedback from Jan I've tried to figure out how to > avoid the error magic in the for_each_writeback_folio. This patch > tries to implement this by switching to an open while loop over a > single writeback_iter() function: > > while ((folio = writeback_iter(mapping, wbc, folio, &error))) { > ... > } > > the twist here is that the error value is passed by reference, so that > the iterator can restore it when breaking out of the loop. > > Additionally it moves the AOP_WRITEPAGE_ACTIVATE out of the iterator > and into the callers, in preparation for eventually killing it off > with the phase out of write_cache_pages(). > > To me this form of the loop feels easier to follow, and it has the > added advantage that writeback_iter() can actually be nicely used in > nested loops, which should help with further iterizing the iomap > writeback code. > > Signed-off-by: Christoph Hellwig Looking at it now I'm thinking whether we would not be better off to completely dump the 'error' argument of writeback_iter() / writeback_iter_next() and just make all .writepage implementations set wbc->err directly. But that means touching all the ~20 writepage implementations we still have... Couple of comments regarding this implementation below (overall I agree it seems somewhat easier to follow the code). > +/** > + * writepage_iter - iterate folio of a mapping for writeback > + * @mapping: address space structure to write > + * @wbc: writeback context > + * @folio: previously iterated folio (%NULL to start) > + * @error: in-out pointer for writeback errors (see below) > + * > + * This function should be called in a while loop in the ->writepages > + * implementation and returns the next folio for the writeback operation > + * described by @wbc on @mapping. > + * > + * To start writeback @folio should be passed as NULL, for every following > + * iteration the folio returned by this function previously should be passed. > + * @error should contain the error from the previous writeback iteration when > + * calling writeback_iter. > + * > + * Once the writeback described in @wbc has finished, this function will return > + * %NULL and if there was an error in any iteration restore it to @error. > + * > + * Note: callers should not manually break out of the loop using break or goto. > + */ > +struct folio *writeback_iter(struct address_space *mapping, > + struct writeback_control *wbc, struct folio *folio, int *error) > { > - unsigned long nr = folio_nr_pages(folio); > + if (folio) { > + wbc->nr_to_write -= folio_nr_pages(folio); > + if (*error && !wbc->err) > + wbc->err = *error; > > - 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; > + /* > + * For integrity sync we have to keep going until we have > + * written all the folios we tagged for writeback prior to > + * entering the writeback 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)) > + goto finish; I think it would be a bit more comprehensible if we replace the goto with: folio_batch_release(&wbc->fbatch); if (wbc->range_cyclic) mapping->writeback_index = folio->index + folio_nr_pages(folio); *error = wbc->err; return NULL; > + } else { > + if (wbc->range_cyclic) > + wbc->index = mapping->writeback_index; /* prev offset */ > + else > + wbc->index = wbc->range_start >> PAGE_SHIFT; > + if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) > + tag_pages_for_writeback(mapping, wbc->index, > + wbc_end(wbc)); > + folio_batch_init(&wbc->fbatch); > + wbc->err = 0; > } > > - if (error && !wbc->err) > - wbc->err = error; > + folio = writeback_get_folio(mapping, wbc); > + if (!folio) > + goto finish; And here we just need to do: if (wbc->range_cyclic) mapping->writeback_index = 0; *error = wbc->err; return NULL; > + return folio; > + > +finish: > + folio_batch_release(&wbc->fbatch); > > /* > - * For integrity sync we have to keep going until we have written all > - * the folios we tagged for writeback prior to entering the writeback > - * 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 range cyclic writeback we need to remember where we stopped so > + * that we can continue there next time we are called. If we hit the > + * last page and there is more work to be done, wrap back to the start > + * of the file. > * > - * 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. > + * For non-cyclic writeback we always start looking up at the beginning > + * of the file if we are called again, which can only happen due to > + * -ENOMEM from the file system. > */ > - if (wbc->sync_mode == WB_SYNC_NONE && > - (wbc->err || wbc->nr_to_write <= 0)) { > - writeback_finish(mapping, wbc, folio->index + nr); > - return NULL; > + if (wbc->range_cyclic) { > + WARN_ON_ONCE(wbc->sync_mode != WB_SYNC_NONE); > + if (wbc->err || wbc->nr_to_write <= 0) > + mapping->writeback_index = > + folio->index + folio_nr_pages(folio); > + else > + mapping->writeback_index = 0; > } > - > - return writeback_get_folio(mapping, wbc); > + *error = wbc->err; > + return NULL; > } > > /** > @@ -2563,13 +2575,17 @@ static int writeback_use_writepage(struct address_space *mapping, > struct writeback_control *wbc) > { > struct blk_plug plug; > - struct folio *folio; > - int err; > + struct folio *folio = 0; ^^ NULL please > + int err = 0; > > blk_start_plug(&plug); > - for_each_writeback_folio(mapping, wbc, folio, err) { > + while ((folio = writeback_iter(mapping, wbc, folio, &err))) { > err = mapping->a_ops->writepage(&folio->page, wbc); > mapping_set_error(mapping, err); > + if (err == AOP_WRITEPAGE_ACTIVATE) { > + folio_unlock(folio); > + err = 0; > + } > } > blk_finish_plug(&plug); > > @@ -2590,6 +2606,8 @@ int do_writepages(struct address_space *mapping, struct writeback_control *wbc) > ret = mapping->a_ops->writepages(mapping, wbc); > } else if (mapping->a_ops->writepage) { > ret = writeback_use_writepage(mapping, wbc); > + if (!ret) > + ret = wbc->err; AFAICT this should not be needed as writeback_iter() made sure wbc->err is returned when set? Honza -- Jan Kara SUSE Labs, CR