Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp6709112rdb; Fri, 15 Dec 2023 06:26:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IFsj3T33ICvTGVu5/iA+Egw3ZMG11NmGtQssWeBcWZTJ8U+bFTXRv9/peNj9XoYvzXP8G/R X-Received: by 2002:a05:6a20:f388:b0:18b:251d:d6fb with SMTP id qr8-20020a056a20f38800b0018b251dd6fbmr5898343pzb.1.1702650371779; Fri, 15 Dec 2023 06:26:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702650371; cv=none; d=google.com; s=arc-20160816; b=u1tCePemgJ8QW8iC7C4e6U1Tsgzp/+v7MhprQQLB1FX/CSmP54/dLqbUw8wx2odhXe 7BjVGF9PnW1ZCRUQWON5wpEpuCwbGlu7vFtKlREt8QqWQSb8wsbw2WHTplU4ImXHUhOI MjB8WuTsKR3LOFtdphnOD3TI/Tfc2N972eLDmf71rY9TYEbJFamIdZQ16tPUvxIrh7Bf P96XnNSAkfXkJ+CFgwnqgHi9ZFRSG+rYR/rmLIF2fEyRJDBRbLyXjs7wDLB0bnRtvPv6 a9+3lqhLVtcwIB4eqBNgSOEbSfhcr6vyPCKbPvb0Yd+aePgnUdtrCOFUdAQbQ3zFMg25 asdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature:dkim-signature:dkim-signature :dkim-signature; bh=O+1Vzn6gSgVRj2PeTCSSeHhZYkHtT8mht0lDEDG5nPo=; fh=/w0dc2O7V6smQOgU7WkICsW4zuan5V76Fq7j5x7mZ4o=; b=gtSY/PFNhgU2eQ1P9278P5WuKbSx5v4Z6joqFUfSE45t4hQQdTSd3kVFquBWGG8pFJ T64YnKDcdyHs9tyAK7ECiNe3aG6mkeTJ0Rp0pR3saP4vB3Rvamp9sAmdsgxwtc0DlIHC 0onfELJp6Q6Zzj/qFe4e9W+k02yDzRsBZSR+6aBhjXDu/qbF+tg+TPBSlXAq9bHZ4iTq tWrt3Gytbk/yf2kcJ1ChEQcmt6ODkgHIvh5U/j49mYXIYEBYl1lVPZXhDU5E6EEiL2TC jbdhjSZ+2Dla1t390fuHLisMLcMLqIDYnjWXcSTNNIPZmKUQuxxY51TEq9g780+bdLyh Mdzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=fV3YPsKf; dkim=neutral (no key) header.i=@suse.cz; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=M5nZCg9o; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=PzffO8tk; spf=pass (google.com: domain of linux-kernel+bounces-1104-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1104-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id kq10-20020a056a004b0a00b006cddfbc6095si13264734pfb.26.2023.12.15.06.26.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 06:26:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-1104-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=fV3YPsKf; dkim=neutral (no key) header.i=@suse.cz; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=M5nZCg9o; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=PzffO8tk; spf=pass (google.com: domain of linux-kernel+bounces-1104-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1104-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 4DCE7B22B37 for ; Fri, 15 Dec 2023 14:26:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5702331752; Fri, 15 Dec 2023 14:25:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="fV3YPsKf"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="cQxuTYfK"; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="M5nZCg9o"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="PzffO8tk" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D662931725; Fri, 15 Dec 2023 14:25:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.cz 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 B25E821B39; Fri, 15 Dec 2023 14:25:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1702650348; 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=O+1Vzn6gSgVRj2PeTCSSeHhZYkHtT8mht0lDEDG5nPo=; b=fV3YPsKfDjKk0wDiNz5lHnGWIiJvWJn3v5VRSZuiXcWcbcQsSTebimcXCwNzY6DwLoI4el NmkJEaGodEOhM2aK41i5PzERVhj62TqTcF7daDMICfL9lPmZEQs9F54TnsLcrObmCnIUiy DpS629c45/MhykftG8M/OZV6hrY1dMY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1702650348; 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=O+1Vzn6gSgVRj2PeTCSSeHhZYkHtT8mht0lDEDG5nPo=; b=cQxuTYfKDmFDq0YwbAMo96JMXs7XZYpTd0yp/UY6DnsDk2q75hWZgZw44jyVmMkdzDl1y1 LuAETYpHzNqfG4BA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1702650347; 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=O+1Vzn6gSgVRj2PeTCSSeHhZYkHtT8mht0lDEDG5nPo=; b=M5nZCg9oo12FS/a8eOsTeKRws1NkrZnAooEh/8YzOCZ4gEyXU3Nsd766n3YXrZzSTZYlFt sx/2zsjMbQcVFtRpzS8eJiYvE/zi1Q5AEQ0A9d/LuUuZfuF8LGWv/Pj46YNYEbo2T1UXXf IiIasiVRYe4DE4goUvHCEc4sXbXl90E= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1702650347; 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=O+1Vzn6gSgVRj2PeTCSSeHhZYkHtT8mht0lDEDG5nPo=; b=PzffO8tkAWptGRWe97c2p1cCLFw+S+acfO2t/aHorPPxZFPrWHt3ywkJvbSt11/5yEVg7k S55j3eGI9/7we0CQ== 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 A18E113A08; Fri, 15 Dec 2023 14:25:47 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id HC0rJ+thfGWeawAAn2gu4w (envelope-from ); Fri, 15 Dec 2023 14:25:47 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 286B7A07E0; Fri, 15 Dec 2023 15:25:47 +0100 (CET) Date: Fri, 15 Dec 2023 15:25:47 +0100 From: Jan Kara To: Christoph Hellwig Cc: linux-mm@kvack.org, "Matthew Wilcox (Oracle)" , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Jan Kara , David Howells Subject: Re: [PATCH 04/11] writeback: Simplify the loops in write_cache_pages() Message-ID: <20231215142547.46g2lqs2d2u3ljwl@quack3> References: <20231214132544.376574-1-hch@lst.de> <20231214132544.376574-5-hch@lst.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231214132544.376574-5-hch@lst.de> X-Spam-Level: Authentication-Results: smtp-out1.suse.de; none X-Spam-Level: X-Spam-Score: -3.80 X-Spamd-Result: default: False [-3.80 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; RCPT_COUNT_SEVEN(0.00)[7]; DBL_BLOCKED_OPENRESOLVER(0.00)[infradead.org:email,lst.de:email,suse.com:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; MID_RHS_NOT_FQDN(0.50)[]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Flag: NO On Thu 14-12-23 14:25:37, Christoph Hellwig wrote: > From: "Matthew Wilcox (Oracle)" > > Collapse the two nested loops into one. This is needed as a step > towards turning this into an iterator. > > Signed-off-by: Christoph Hellwig It would be good to mention in the changelog that we drop the condition index <= end and just rely 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 I think that's mostly a theoretical concern so I'd be fine with just mentioning this subtlety in the changelog and possibly in a comment in the code. Honza > --- > mm/page-writeback.c | 98 ++++++++++++++++++++++----------------------- > 1 file changed, 49 insertions(+), 49 deletions(-) > > diff --git a/mm/page-writeback.c b/mm/page-writeback.c > index 5a3df8665ff4f9..2087d16115710e 100644 > --- a/mm/page-writeback.c > +++ b/mm/page-writeback.c > @@ -2460,6 +2460,7 @@ int write_cache_pages(struct address_space *mapping, > void *data) > { > int error; > + int i = 0; > > if (wbc->range_cyclic) { > wbc->index = mapping->writeback_index; /* prev offset */ > @@ -2477,67 +2478,66 @@ int write_cache_pages(struct address_space *mapping, > folio_batch_init(&wbc->fbatch); > wbc->err = 0; > > - while (wbc->index <= wbc->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++]; > > - wbc->done_index = folio->index; > + wbc->done_index = folio->index; > > - folio_lock(folio); > - if (!should_writeback_folio(mapping, wbc, folio)) { > - folio_unlock(folio); > - continue; > - } > + folio_lock(folio); > + if (!should_writeback_folio(mapping, wbc, folio)) { > + folio_unlock(folio); > + continue; > + } > > - trace_wbc_writepage(wbc, inode_to_bdi(mapping->host)); > - > - error = writepage(folio, wbc, data); > - nr = folio_nr_pages(folio); > - 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) { > - wbc->err = error; > - wbc->done_index = folio->index + nr; > - return writeback_finish(mapping, > - wbc, true); > - } > - if (!wbc->err) > - wbc->err = error; > - } > + trace_wbc_writepage(wbc, inode_to_bdi(mapping->host)); > > + error = writepage(folio, wbc, data); > + nr = folio_nr_pages(folio); > + if (unlikely(error)) { > /* > - * 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. > + * 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. > */ > - wbc->nr_to_write -= nr; > - if (wbc->nr_to_write <= 0 && > - wbc->sync_mode == WB_SYNC_NONE) > + if (error == AOP_WRITEPAGE_ACTIVATE) { > + folio_unlock(folio); > + error = 0; > + } else if (wbc->sync_mode != WB_SYNC_ALL) { > + wbc->err = error; > + wbc->done_index = folio->index + nr; > return writeback_finish(mapping, wbc, true); > + } > + if (!wbc->err) > + wbc->err = error; > } > + > + /* > + * 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. > + */ > + wbc->nr_to_write -= nr; > + if (wbc->nr_to_write <= 0 && > + wbc->sync_mode == WB_SYNC_NONE) > + return writeback_finish(mapping, wbc, true); > } > > return writeback_finish(mapping, wbc, false); > -- > 2.39.2 > -- Jan Kara SUSE Labs, CR