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 15569C4828D for ; Mon, 5 Feb 2024 15:32:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9EFF76B007B; Mon, 5 Feb 2024 10:32:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A0256B007D; Mon, 5 Feb 2024 10:32:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8683C6B007E; Mon, 5 Feb 2024 10:32:42 -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 7827A6B007B for ; Mon, 5 Feb 2024 10:32:42 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 42D79C03DA for ; Mon, 5 Feb 2024 15:32:42 +0000 (UTC) X-FDA: 81758142564.14.65C050C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf10.hostedemail.com (Postfix) with ESMTP id 58E2BC000E for ; Mon, 5 Feb 2024 15:32:40 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=h+X8khlB; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf10.hostedemail.com: domain of bfoster@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=bfoster@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707147160; 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=GuCUqP9VPCjxaxcw8wTI5AE/DcSSjnaMSJU0S80AB1k=; b=GtxfddzTwhZ47fnD/VS+MxeRGuqHs5KcGB5Ti4RWVqMfQr4NkyVmEpz8Ehly1yX/NjSE5k WpBVDSel2fQLhOJ9SVCjiVG7/Hu4+cnR3uT7y1wQlZXXkTQ8BQxWtNIw4j9XRz88A2sAlA ZZ80H77EZwN4jhm+BSmHLNodG2sncWs= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=h+X8khlB; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf10.hostedemail.com: domain of bfoster@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=bfoster@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707147160; a=rsa-sha256; cv=none; b=kqru2RSYDsTmsTp9CK5pUubvxlZsZD4BKA6MWVH/wP0u6YGXPo57p8pQ2ZuOth1o+uR38A hLw0a0uITOe65B4nwTjgiYTiWzFmeeD2mI3Y6r5LeA3N0U/L0eLRFG/28hOkep0VjoX2FA 1NpKw3AUd9yhA6cg5HuO0oyOw4s70DA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707147159; 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=GuCUqP9VPCjxaxcw8wTI5AE/DcSSjnaMSJU0S80AB1k=; b=h+X8khlBUqwVjP6BPUgD9JdQbSs1XIE+MXpXxCxtUzALSJAa+09QXVzBfGX13LzHdvkb36 iKbfAlzz7AMHnbNwWx19dKd6YsImKz5ku1pTa8Oy58eVwHNjXyC5gs2c0v5/LK13SXc4cn bolYxjQdkcSCbCW/gaO8vXjvvp7UYLs= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-584-6iy0J4kcNSKioiYaSvkCBg-1; Mon, 05 Feb 2024 10:32:35 -0500 X-MC-Unique: 6iy0J4kcNSKioiYaSvkCBg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 AE66085A58B; Mon, 5 Feb 2024 15:32:34 +0000 (UTC) Received: from bfoster (unknown [10.22.32.186]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 483A72166B31; Mon, 5 Feb 2024 15:32:34 +0000 (UTC) Date: Mon, 5 Feb 2024 10:33:52 -0500 From: Brian Foster To: Christoph Hellwig Cc: 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 12/13] writeback: add a writeback iterator Message-ID: References: <20240203071147.862076-1-hch@lst.de> <20240203071147.862076-13-hch@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240203071147.862076-13-hch@lst.de> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 58E2BC000E X-Stat-Signature: gqh45y36198dcxhtcpfzu5si1m6bw9ma X-HE-Tag: 1707147160-955478 X-HE-Meta: U2FsdGVkX18uFnk5wChW2eV9Y5OqDWtvL4MPlVtZz8nJCWg60ag4wi0jB/J2rfMG6BMgeJaeBSszSGZedCNAmQ4q3Y9yPg3pNOON8JIi6zFbp3Gr7mKaw7OcgFM+F2WPvZNB5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mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Sat, Feb 03, 2024 at 08:11:46AM +0100, Christoph Hellwig wrote: > Refactor the code left in write_cache_pages into an iterator that the > file system can call to get the next folio for a writeback operation: > > struct folio *folio = NULL; > > while ((folio = writeback_iter(mapping, wbc, folio, &error))) { > 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. > > Handling of the magic AOP_WRITEPAGE_ACTIVATE value stays outside the > iterator and needs is just kept in the write_cache_pages legacy wrapper. > in preparation for eventually killing it off. > > Heavily based on a for_each* based iterator from Matthew Wilcox. > > Signed-off-by: Christoph Hellwig > --- > include/linux/writeback.h | 4 + > mm/page-writeback.c | 192 ++++++++++++++++++++++---------------- > 2 files changed, 118 insertions(+), 78 deletions(-) > ... > diff --git a/mm/page-writeback.c b/mm/page-writeback.c > index 3abb053e70580e..5fe4cdb7dbd61a 100644 > --- a/mm/page-writeback.c > +++ b/mm/page-writeback.c ... > @@ -2434,69 +2434,68 @@ static struct folio *writeback_get_folio(struct address_space *mapping, > } > > /** ... > */ > -int write_cache_pages(struct address_space *mapping, > - struct writeback_control *wbc, writepage_t writepage, > - void *data) > +struct folio *writeback_iter(struct address_space *mapping, > + struct writeback_control *wbc, struct folio *folio, int *error) > { ... > + } else { > wbc->nr_to_write -= folio_nr_pages(folio); > > - if (error == AOP_WRITEPAGE_ACTIVATE) { > - folio_unlock(folio); > - error = 0; > - } > + WARN_ON_ONCE(*error > 0); Why the warning on writeback error here? It looks like new behavior, but maybe I missed something. Otherwise the factoring LGTM. Brian > > /* > * For integrity writeback we have to keep going until we have > @@ -2510,33 +2509,70 @@ int write_cache_pages(struct address_space *mapping, > * wbc->nr_to_write or encounter the first error. > */ > if (wbc->sync_mode == WB_SYNC_ALL) { > - if (error && !ret) > - ret = error; > + if (*error && !wbc->saved_err) > + wbc->saved_err = *error; > } else { > - if (error || wbc->nr_to_write <= 0) > + if (*error || wbc->nr_to_write <= 0) > goto done; > } > } > > - /* > - * 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 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. > - */ > - folio_batch_release(&wbc->fbatch); > - if (wbc->range_cyclic) > - mapping->writeback_index = 0; > - return ret; > + folio = writeback_get_folio(mapping, wbc); > + if (!folio) { > + /* > + * To avoid deadlocks between range_cyclic writeback and callers > + * that hold pages in PageWriteback to aggregate I/O until > + * the writeback iteration finishes, we do not loop back to the > + * start of the file. Doing so causes a page lock/page > + * writeback access order inversion - we should only ever lock > + * multiple pages in ascending page->index order, and looping > + * back to the start of the file violates that rule and causes > + * deadlocks. > + */ > + if (wbc->range_cyclic) > + mapping->writeback_index = 0; > + > + /* > + * Return the first error we encountered (if there was any) to > + * the caller. > + */ > + *error = wbc->saved_err; > + } > + return folio; > > done: > folio_batch_release(&wbc->fbatch); > if (wbc->range_cyclic) > mapping->writeback_index = folio->index + folio_nr_pages(folio); > + return NULL; > +} > + > +/** > + * 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 > + * @wbc: subtract the number of written pages from *@wbc->nr_to_write > + * @writepage: function called for each page > + * @data: data passed to writepage function > + * > + * Return: %0 on success, negative error code otherwise > + * > + * Note: please use writeback_iter() instead. > + */ > +int write_cache_pages(struct address_space *mapping, > + struct writeback_control *wbc, writepage_t writepage, > + void *data) > +{ > + struct folio *folio = NULL; > + int 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 error; > } > EXPORT_SYMBOL(write_cache_pages); > -- > 2.39.2 > >