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 12939C47DB3 for ; Wed, 31 Jan 2024 12:49:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 834D86B0072; Wed, 31 Jan 2024 07:49:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E5A26B0074; Wed, 31 Jan 2024 07:49:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6AD156B0075; Wed, 31 Jan 2024 07:49:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5B7C46B0072 for ; Wed, 31 Jan 2024 07:49:17 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id F1F35A0D2F for ; Wed, 31 Jan 2024 12:49:16 +0000 (UTC) X-FDA: 81739586712.09.987366E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf10.hostedemail.com (Postfix) with ESMTP id 0D85FC0019 for ; Wed, 31 Jan 2024 12:49:14 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=HgFzf9WD; spf=pass (imf10.hostedemail.com: domain of bfoster@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=bfoster@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706705355; 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=scKPr+8Cf62Gt7irmdBRvL9FpKHGVZHfUDxCP6SwrZo=; b=HJLwv/ia3H0CC0FrGwktXO3kotO+9YsIdTnGXayfKVS4/pQ5NoFE7vjRLDsvIxfh4+f3vG tqFW39zHp2cPwOH8H3CHN2XLLEZM0VSZCSO8DFagNnjmOHWlmOxa0Iv1rENf6JkV2yy3oO Cqd0RFXrAX1XsY3wx8xkITxOkqNUDn8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706705355; a=rsa-sha256; cv=none; b=e5P5A8raJWFIJ1/EPdaRhE7HsfPduOzR5SSwDGIwG/3hHxRRJpmWOVND+/nI/wZAOD3iZk tKAfsrXrX6uoRm8XgbQ8GFP6T80vKxtCWr00MeAbvTVjdZ+Pd8YOCvTxgBMjZbDHLi1TL8 hmJQ43EBkUUqLXg0HhyqK45xe9z7NBU= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=HgFzf9WD; spf=pass (imf10.hostedemail.com: domain of bfoster@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=bfoster@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706705354; h=from:from:reply-to:subject:subject: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=scKPr+8Cf62Gt7irmdBRvL9FpKHGVZHfUDxCP6SwrZo=; b=HgFzf9WDs0TYKVtgaoL6zefE5q1OKig/xGC9QLmscG2Hw4y5//VHyn8XUlVmLWoOhUSgSz OwC2NAD1X4162f7YAN24RoNWN/fkeNy9iINcePrUDbpFxkccU60HUyk1sT/ovpC8Ls05k+ M517/GK4C1/yAQkBcuA22hD+QWKB0c0= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-529-PLiBSkJGPCiyOB6yhWpA8A-1; Wed, 31 Jan 2024 07:49:08 -0500 X-MC-Unique: PLiBSkJGPCiyOB6yhWpA8A-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 548B2285F98F; Wed, 31 Jan 2024 12:49:07 +0000 (UTC) Received: from bfoster (unknown [10.22.32.186]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C174B1C060B3; Wed, 31 Jan 2024 12:49:06 +0000 (UTC) Date: Wed, 31 Jan 2024 07:50:25 -0500 From: Brian Foster To: Christoph Hellwig Cc: Jan Kara , linux-mm@kvack.org, Matthew Wilcox , Jan Kara , David Howells , 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: References: <20240125085758.2393327-1-hch@lst.de> <20240125085758.2393327-20-hch@lst.de> <20240130104605.2i6mmdncuhwwwfin@quack3> <20240130141601.GA31330@lst.de> <20240130215016.npofgza5nmoxuw6m@quack3> <20240131071437.GA17336@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240131071437.GA17336@lst.de> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Stat-Signature: yhcib54ttxycfgugc6eof6c5ga4tauhb X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 0D85FC0019 X-Rspam-User: X-HE-Tag: 1706705354-20598 X-HE-Meta: U2FsdGVkX18Ct03C1nfA0fa65V/2sj6WRzRqT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UyJZhaiPC807VCBun805sxKxIUKvht44QSDaZ88rEkSm5T 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 Wed, Jan 31, 2024 at 08:14:37AM +0100, Christoph Hellwig wrote: > On Tue, Jan 30, 2024 at 10:50:16PM +0100, Jan Kara wrote: > > Well, batch release needs to be only here because if writeback_get_folio() > > returns NULL, the batch has been already released by it. > > Indeed. > > > So what would be > > duplicated is only the error assignment. But I'm fine with the version in > > the following email and actually somewhat prefer it compared the yet > > another variant you've sent. > > So how about another variant, this is closer to your original suggestion. > But I've switched around the ordered of the folio or not branches > from my original patch, and completely reworked and (IMHO) improved the > comments. it replaces patch 19 instead of being incremental to be > readable: > Not a thorough review but at first glance I like it. I think the direct function call makes things easier to follow. Just one quick thought... ... > diff --git a/mm/page-writeback.c b/mm/page-writeback.c > index 0763c4353a676a..eefcb00cb7b227 100644 > --- a/mm/page-writeback.c > +++ b/mm/page-writeback.c ... > @@ -2458,60 +2433,107 @@ static struct folio *writeback_get_folio(struct address_space *mapping, > return folio; > } > ... > +struct folio *writeback_iter(struct address_space *mapping, > + struct writeback_control *wbc, struct folio *folio, int *error) > { > - 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)); > - > - wbc->err = 0; > - folio_batch_init(&wbc->fbatch); > - return writeback_get_folio(mapping, wbc); > -} > + if (!folio) { > + folio_batch_init(&wbc->fbatch); > + wbc->err = 0; The implied field initialization via !folio feels a little wonky to me just because it's not clear from the client code that both fields must be initialized. Even though the interface is simpler, I wonder if it's still worth having a dumb/macro type init function that at least does the batch and error field initialization. Or on second thought maybe having writeback_iter() reset *error as well on folio == NULL might be a little cleaner without changing the interface.... Brian > > -struct folio *writeback_iter_next(struct address_space *mapping, > - struct writeback_control *wbc, struct folio *folio, int error) > -{ > - unsigned long nr = folio_nr_pages(folio); > + /* > + * For range cyclic writeback we remember where we stopped so > + * that we can continue where we stopped. > + * > + * For non-cyclic writeback we always start at the beginning of > + * the passed in range. > + */ > + if (wbc->range_cyclic) > + wbc->index = mapping->writeback_index; > + else > + wbc->index = wbc->range_start >> PAGE_SHIFT; > > - wbc->nr_to_write -= nr; > + /* > + * To avoid livelocks when other processes dirty new pages, we > + * first tag pages which should be written back and only then > + * start writing them. > + * > + * For data-integrity sync we have to be careful so that we do > + * not miss some pages (e.g., because some other process has > + * cleared the TOWRITE tag we set). The rule we follow is that > + * TOWRITE tag can be cleared only by the process clearing the > + * DIRTY tag (and submitting the page for I/O). > + */ > + if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) > + tag_pages_for_writeback(mapping, wbc->index, > + wbc_end(wbc)); > + } else { > + wbc->nr_to_write -= folio_nr_pages(folio); > > - /* > - * 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 writeback 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, and just stash away > + * the first error we encounter in wbc->err so that it can > + * be retrieved on return. > + * > + * This is because the file system may still have state to clear > + * for each folio. We'll eventually return the first error > + * encountered. > + */ > + if (wbc->sync_mode == WB_SYNC_ALL) { > + if (*error && !wbc->err) > + wbc->err = *error; > + } else { > + if (*error || wbc->nr_to_write <= 0) > + goto done; > + } > } > > - if (error && !wbc->err) > - wbc->err = error; > + folio = writeback_get_folio(mapping, wbc); > + if (!folio) { > + /* > + * For range cyclic writeback not finding another folios means > + * that we are at the end of the file. In that case go back > + * to the start of the file for the next call. > + */ > + if (wbc->range_cyclic) > + mapping->writeback_index = 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)) { > - writeback_finish(mapping, wbc, folio->index + nr); > - return NULL; > + /* > + * Return the first error we encountered (if there was any) to > + * the caller now that we are done. > + */ > + *error = wbc->err; > } > + return folio; > > - return writeback_get_folio(mapping, wbc); > +done: > + if (wbc->range_cyclic) > + mapping->writeback_index = folio->index + folio_nr_pages(folio); > + folio_batch_release(&wbc->fbatch); > + return NULL; > } > > /** > @@ -2549,13 +2571,18 @@ int write_cache_pages(struct address_space *mapping, > struct writeback_control *wbc, writepage_t writepage, > void *data) > { > - struct folio *folio; > - int error; > + struct folio *folio = NULL; > + int error = 0; > > - for_each_writeback_folio(mapping, wbc, folio, error) > + while ((folio = writeback_iter(mapping, wbc, folio, &error))) { > error = writepage(folio, wbc, data); > + if (error == AOP_WRITEPAGE_ACTIVATE) { > + folio_unlock(folio); > + error = 0; > + } > + } > > - return wbc->err; > + return error; > } > EXPORT_SYMBOL(write_cache_pages); > > @@ -2563,13 +2590,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 = NULL; > + 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); > >