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 605C0C4828D for ; Mon, 5 Feb 2024 11:34:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C9B976B0071; Mon, 5 Feb 2024 06:34:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C4B486B0072; Mon, 5 Feb 2024 06:34:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC5786B0074; Mon, 5 Feb 2024 06:34:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9A2D96B0071 for ; Mon, 5 Feb 2024 06:34:17 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 65C1D140952 for ; Mon, 5 Feb 2024 11:34:17 +0000 (UTC) X-FDA: 81757541754.08.7532CD4 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf12.hostedemail.com (Postfix) with ESMTP id 0F5D940002 for ; Mon, 5 Feb 2024 11:34:14 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=nWlGlpjg; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b="rDIaZo/o"; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=ZYJ3k7Ln; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=4hfbs10r; dmarc=none; spf=pass (imf12.hostedemail.com: domain of jack@suse.cz designates 195.135.223.131 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=1707132855; 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=m/SxzjtBf487sMzoXkTo79HeTFHw6kYb0uZ9SjI1QhA=; b=TvQPw8BZVQ2nDd8Ry7ELpD6yVFn8gdshdFp00t8nFnsMWcneNBJ0lMXvSxc9CDk3NOFF1r 3kVcoHpddfRjjvbBC5H/XH+35tqsnUKj8XMdrP+1e1sK9Ur1nJKEl+Bq3dolNMt1J6iL60 L0MMGIHRtBeSQyFtJkBniu5aLmqpJ3M= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=nWlGlpjg; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b="rDIaZo/o"; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=ZYJ3k7Ln; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=4hfbs10r; dmarc=none; spf=pass (imf12.hostedemail.com: domain of jack@suse.cz designates 195.135.223.131 as permitted sender) smtp.mailfrom=jack@suse.cz ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707132855; a=rsa-sha256; cv=none; b=blSqnPHYf5Y8f1eZ6hxAXUT9iQ1z0WF3ulNwYGfDGhVUGBwm11zyrpG9PLksLA4w4plyfu ERWT07iLU2O43NFHbHKTloBvElqgrQ8jborebsmuEdjbBSydyRT+v6BuUwfK4+EMS7cmDR 26MVoSK0L8JC+m8kGeDLelahWs9d+90= Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (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-out2.suse.de (Postfix) with ESMTPS id D2A731F8BD; Mon, 5 Feb 2024 11:34:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1707132853; 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=m/SxzjtBf487sMzoXkTo79HeTFHw6kYb0uZ9SjI1QhA=; b=nWlGlpjgTnSsdsU/4IcQAUBsJUNt8O2LS7OBMRW/5yB+zMgVMogO2G+6I+vWntP+poRgnm rJEEvOz0fexQj7YIOTCnK1gEBres+5IfiNKztez0kJBxSCZubRQY7oqWlUHl9OyDx08GxV Nz03ign2qpbSzbzEwnVZ2F11yM2fnds= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1707132853; 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=m/SxzjtBf487sMzoXkTo79HeTFHw6kYb0uZ9SjI1QhA=; b=rDIaZo/oPFtJBnyilzrNT6NyPncrgL7uudHAAz46rJPOfXzKlJfWp0h67EmRzw9xkWlVmC HVxRsmi3P39nLwBA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1707132852; 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=m/SxzjtBf487sMzoXkTo79HeTFHw6kYb0uZ9SjI1QhA=; b=ZYJ3k7LnR8sbw5nxKHiqRmRqE91mDOoDVovLO4nQPFElWqeuESWMWhrS0AtUOcZADAOk5p WhqBGGaMCP30TWpTUlaQDHggmWK25bcY7BmIUu+lbMnxnOvo6MaFQs+bv1zSmYhChgz/f3 8eXa9KFGPXdAs/s2cJDz6xXwRqfBf5E= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1707132852; 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=m/SxzjtBf487sMzoXkTo79HeTFHw6kYb0uZ9SjI1QhA=; b=4hfbs10r0RNUuNudNYnaEV4nGp5Cc7TlxBs/M1B2a7tinE1tu2CGehJdf1sMy0vBpiYjnY z5Wxwt1PVqxCDIAQ== Received: from imap1.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id C68F8136F5; Mon, 5 Feb 2024 11:34:12 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 6ZR0MLTHwGVeQgAAD6G6ig (envelope-from ); Mon, 05 Feb 2024 11:34:12 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 7903EA0809; Mon, 5 Feb 2024 12:34:12 +0100 (CET) Date: Mon, 5 Feb 2024 12:34:12 +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 05/13] writeback: rework the loop termination condition in write_cache_pages Message-ID: <20240205113412.ceemxahic7bpodip@quack3> References: <20240203071147.862076-1-hch@lst.de> <20240203071147.862076-6-hch@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240203071147.862076-6-hch@lst.de> X-Rspam-User: X-Stat-Signature: 55fky4mfpg9otco18fc69uguiz3ps4uh X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 0F5D940002 X-HE-Tag: 1707132854-585204 X-HE-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IAw1CBH5p6mom9ncpKzsp1qTJYqrpBwgL2iy8dp0toj3QvGLDr/rIUVWTnF3bAMI4u2LM4bogNvyFGWDl5mOHAntP/9NlMQ6ULtbAxHLzfTku2mNzuAYBupQG31rHEtHuHGbMbvzjoHnPRkO5gs9bSrP8pECG5PaltwbzhPHhwWT1PETFfIUM2temqQlyVu+x1J+7bycWIqiueNCpyB5qxBBzOfv3tZkZK8JcDZ8sVTQC+Z325b3ysiEA18SuQ540ir9RgJ/wa2mrR 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 Sat 03-02-24 08:11:39, Christoph Hellwig wrote: > Rework the way we deal with the cleanup after the writepage call. > > First handle the magic AOP_WRITEPAGE_ACTIVATE separately from real error > returns to get it out of the way of the actual error handling path. > > The split the handling on intgrity vs non-integrity branches first, > and return early using a goto for the non-ingegrity early loop condition > to remove the need for the done and done_index local variables, and for > assigning the error to ret when we can just return error directly. > > Signed-off-by: Christoph Hellwig Looks good. Feel free to add: Reviewed-by: Jan Kara Honza > --- > mm/page-writeback.c | 84 ++++++++++++++++++--------------------------- > 1 file changed, 33 insertions(+), 51 deletions(-) > > diff --git a/mm/page-writeback.c b/mm/page-writeback.c > index c7c494526bc650..88b2c4c111c01b 100644 > --- a/mm/page-writeback.c > +++ b/mm/page-writeback.c > @@ -2396,13 +2396,12 @@ int write_cache_pages(struct address_space *mapping, > void *data) > { > int ret = 0; > - int done = 0; > int error; > struct folio_batch fbatch; > + struct folio *folio; > int nr_folios; > pgoff_t index; > pgoff_t end; /* Inclusive */ > - pgoff_t done_index; > xa_mark_t tag; > > folio_batch_init(&fbatch); > @@ -2419,8 +2418,7 @@ int write_cache_pages(struct address_space *mapping, > } else { > tag = PAGECACHE_TAG_DIRTY; > } > - done_index = index; > - while (!done && (index <= end)) { > + while (index <= end) { > int i; > > nr_folios = filemap_get_folios_tag(mapping, &index, end, > @@ -2430,11 +2428,7 @@ int write_cache_pages(struct address_space *mapping, > break; > > for (i = 0; i < nr_folios; i++) { > - struct folio *folio = fbatch.folios[i]; > - unsigned long nr; > - > - done_index = folio->index; > - > + folio = fbatch.folios[i]; > folio_lock(folio); > > /* > @@ -2469,45 +2463,32 @@ int write_cache_pages(struct address_space *mapping, > > trace_wbc_writepage(wbc, inode_to_bdi(mapping->host)); > error = writepage(folio, wbc, data); > - nr = folio_nr_pages(folio); > - wbc->nr_to_write -= nr; > - if (unlikely(error)) { > - /* > - * Handle errors according to the type of > - * writeback. There's no need to continue for > - * background writeback. Just push done_index > - * past this page so media errors won't choke > - * writeout for the entire file. For integrity > - * writeback, we must process the entire dirty > - * set regardless of errors because the fs may > - * still have state to clear for each page. In > - * that case we continue processing and return > - * the first error. > - */ > - if (error == AOP_WRITEPAGE_ACTIVATE) { > - folio_unlock(folio); > - error = 0; > - } else if (wbc->sync_mode != WB_SYNC_ALL) { > - ret = error; > - done_index = folio->index + nr; > - done = 1; > - break; > - } > - if (!ret) > - ret = error; > + wbc->nr_to_write -= folio_nr_pages(folio); > + > + if (error == AOP_WRITEPAGE_ACTIVATE) { > + folio_unlock(folio); > + error = 0; > } > > /* > - * We stop writing back only if we are not doing > - * integrity sync. In case of integrity sync we have to > - * keep going until we have written all the pages > - * we tagged for writeback prior to entering this loop. > + * 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. > */ > - done_index = folio->index + nr; > - if (wbc->nr_to_write <= 0 && > - wbc->sync_mode == WB_SYNC_NONE) { > - done = 1; > - break; > + if (wbc->sync_mode == WB_SYNC_ALL) { > + if (error && !ret) > + ret = error; > + } else { > + if (error || wbc->nr_to_write <= 0) > + goto done; > } > } > folio_batch_release(&fbatch); > @@ -2524,14 +2505,15 @@ int write_cache_pages(struct address_space *mapping, > * of the file if we are called again, which can only happen due to > * -ENOMEM from the file system. > */ > - if (wbc->range_cyclic) { > - if (done) > - mapping->writeback_index = done_index; > - else > - mapping->writeback_index = 0; > - } > - > + if (wbc->range_cyclic) > + mapping->writeback_index = 0; > return ret; > + > +done: > + folio_batch_release(&fbatch); > + if (wbc->range_cyclic) > + mapping->writeback_index = folio->index + folio_nr_pages(folio); > + return error; > } > EXPORT_SYMBOL(write_cache_pages); > > -- > 2.39.2 > -- Jan Kara SUSE Labs, CR