Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp8523004rwl; Sat, 31 Dec 2022 07:12:36 -0800 (PST) X-Google-Smtp-Source: AMrXdXsFrq56vfHi5DZn+kVMt8Y2x7kuNQ1iGcTg4e251rpwqvGGA/rqDkyCg86MZA425HsBTCXn X-Received: by 2002:a17:902:850c:b0:192:bbe9:4cab with SMTP id bj12-20020a170902850c00b00192bbe94cabmr1961778plb.24.1672499555853; Sat, 31 Dec 2022 07:12:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672499555; cv=none; d=google.com; s=arc-20160816; b=xJf5k3EF5RuWdRueXTvQYNfm0pkvwWzF6DM2OKM5FeSjajhCeSBneUbYxuMqp0EO9T G5+Do6mYmHX76i+59OBZXBBgAAtZteh81YcCldlcY3rxWc98XKzxCj9WGQsdIBDRPXJk tZjT/du91AoFKlNgpvgo/14h7eGUD/jtxfnjXPEMU6GJUVFnTVsCp9+tM9cC/isFg6+t IQskdItznV5dIWq1Mw7rqw19P3uSSUkS0D2be1txKN+hnRXfM21r5fj+IMun3HW9YjTq 1ldIoGsCneYhP1H5zH6f6Mlu8uCycX0YXQy7nSFc8YDJAu5l/BD4yXCJCugSCkitOfFd vFnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=fE5cihq5jbVIEnuaqd8gWb8TSdCs9FfG1DNWG6Z5bj4=; b=a0OJRx7IS4X31k77yLcxbdBo1FAW+IRmxvHk9Kj4LEdZJ+r+qrJMZtN/EBHJ9w9yBn xZgSICM5UuFQlmD+S3LqWPQrBMCtunVVn0gzejQ4tzvIKtQIgyrj6nxURj+0TIYsbWnL mEi4OTMSeTFwmPgQwi1ZgAbwWLLSrH/5ko2GZNNHaIk6vmEt8HuRB3k8GJGy6R6q+mjh epho0MOAp3CIftG3rVEHePRNGiI5zWZ4x0h9Rwiq1xpif86qt4DJDHM+dPzE1HDCeUfa kMtZzVNmdhsqw96HHmZ5ClSMRfvl5+cxYUrfhkct+yX/lcK+jisBWAYdifRcDKdXVrBP RY0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Ol/sC+uj"; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o21-20020a63f155000000b004785d99321bsi24264681pgk.598.2022.12.31.07.12.23; Sat, 31 Dec 2022 07:12:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Ol/sC+uj"; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229486AbiLaPKv (ORCPT + 99 others); Sat, 31 Dec 2022 10:10:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232122AbiLaPKu (ORCPT ); Sat, 31 Dec 2022 10:10:50 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF623A46C for ; Sat, 31 Dec 2022 07:09:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672499371; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fE5cihq5jbVIEnuaqd8gWb8TSdCs9FfG1DNWG6Z5bj4=; b=Ol/sC+ujuQ7ynJT3pg7WIy2pS2ESliWfu5oLi+xs3e9/JEBoVOGwaULn8Shw8VIaORCKzd ehmxGvubxqWWAym+uAJ8FIqFVf6otLGdnluPDhpQ1RNn/P1r2X+oMImTzx7yxdbkN/Fk+k UywE6cBUqXZMyd2iO1Nt8Ipir/4pNkg= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-554-a4Fw95cfPM22mhxNlQQ8RQ-1; Sat, 31 Dec 2022 10:09:28 -0500 X-MC-Unique: a4Fw95cfPM22mhxNlQQ8RQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B0D6F29A9D47; Sat, 31 Dec 2022 15:09:27 +0000 (UTC) Received: from pasta.redhat.com (ovpn-192-3.brq.redhat.com [10.40.192.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id 03043492B00; Sat, 31 Dec 2022 15:09:25 +0000 (UTC) From: Andreas Gruenbacher To: Christoph Hellwig , "Darrick J . Wong" , Alexander Viro , Matthew Wilcox Cc: Andreas Gruenbacher , linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, cluster-devel@redhat.com Subject: [PATCH v5 2/9] iomap/gfs2: Unlock and put folio in page_done handler Date: Sat, 31 Dec 2022 16:09:12 +0100 Message-Id: <20221231150919.659533-3-agruenba@redhat.com> In-Reply-To: <20221231150919.659533-1-agruenba@redhat.com> References: <20221231150919.659533-1-agruenba@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org When an iomap defines a ->page_done() handler in its page_ops, delegate unlocking the folio and putting the folio reference to that handler. This allows to fix a race between journaled data writes and folio writeback in gfs2: before this change, gfs2_iomap_page_done() was called after unlocking the folio, so writeback could start writing back the folio's buffers before they could be marked for writing to the journal. Also, try_to_free_buffers() could free the buffers before gfs2_iomap_page_done() was done adding the buffers to the current current transaction. With this change, gfs2_iomap_page_done() adds the buffers to the current transaction while the folio is still locked, so the problems described above can no longer occur. The only current user of ->page_done() is gfs2, so other filesystems are not affected. To catch out any out-of-tree users, switch from a page to a folio in ->page_done(). Signed-off-by: Andreas Gruenbacher --- fs/gfs2/bmap.c | 15 ++++++++++++--- fs/iomap/buffered-io.c | 8 ++++---- include/linux/iomap.h | 7 ++++--- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/fs/gfs2/bmap.c b/fs/gfs2/bmap.c index e7537fd305dd..46206286ad42 100644 --- a/fs/gfs2/bmap.c +++ b/fs/gfs2/bmap.c @@ -968,14 +968,23 @@ static int gfs2_iomap_page_prepare(struct inode *inode, loff_t pos, } static void gfs2_iomap_page_done(struct inode *inode, loff_t pos, - unsigned copied, struct page *page) + unsigned copied, struct folio *folio) { struct gfs2_trans *tr = current->journal_info; struct gfs2_inode *ip = GFS2_I(inode); struct gfs2_sbd *sdp = GFS2_SB(inode); - if (page && !gfs2_is_stuffed(ip)) - gfs2_page_add_databufs(ip, page, offset_in_page(pos), copied); + if (!folio) { + gfs2_trans_end(sdp); + return; + } + + if (!gfs2_is_stuffed(ip)) + gfs2_page_add_databufs(ip, &folio->page, offset_in_page(pos), + copied); + + folio_unlock(folio); + folio_put(folio); if (tr->tr_num_buf_new) __mark_inode_dirty(inode, I_DIRTY_DATASYNC); diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c index c30d150a9303..e13d5694e299 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -580,12 +580,12 @@ static void iomap_put_folio(struct iomap_iter *iter, loff_t pos, size_t ret, { const struct iomap_page_ops *page_ops = iter->iomap.page_ops; - if (folio) + if (page_ops && page_ops->page_done) { + page_ops->page_done(iter->inode, pos, ret, folio); + } else if (folio) { folio_unlock(folio); - if (page_ops && page_ops->page_done) - page_ops->page_done(iter->inode, pos, ret, &folio->page); - if (folio) folio_put(folio); + } } static int iomap_write_begin_inline(const struct iomap_iter *iter, diff --git a/include/linux/iomap.h b/include/linux/iomap.h index 0983dfc9a203..743e2a909162 100644 --- a/include/linux/iomap.h +++ b/include/linux/iomap.h @@ -131,13 +131,14 @@ static inline bool iomap_inline_data_valid(const struct iomap *iomap) * associated with them. * * When page_prepare succeeds, page_done will always be called to do any - * cleanup work necessary. In that page_done call, @page will be NULL if the - * associated page could not be obtained. + * cleanup work necessary. In that page_done call, @folio will be NULL if the + * associated folio could not be obtained. When folio is not NULL, page_done + * is responsible for unlocking and putting the folio. */ struct iomap_page_ops { int (*page_prepare)(struct inode *inode, loff_t pos, unsigned len); void (*page_done)(struct inode *inode, loff_t pos, unsigned copied, - struct page *page); + struct folio *folio); /* * Check that the cached iomap still maps correctly to the filesystem's -- 2.38.1