From patchwork Tue Dec 8 02:15:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 11957443 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 395A9C433FE for ; Tue, 8 Dec 2020 02:18:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 065D323A33 for ; Tue, 8 Dec 2020 02:18:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727810AbgLHCRS (ORCPT ); Mon, 7 Dec 2020 21:17:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725877AbgLHCRS (ORCPT ); Mon, 7 Dec 2020 21:17:18 -0500 Received: from mail-vk1-xa41.google.com (mail-vk1-xa41.google.com [IPv6:2607:f8b0:4864:20::a41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E20FC06179C; Mon, 7 Dec 2020 18:16:38 -0800 (PST) Received: by mail-vk1-xa41.google.com with SMTP id w190so3611207vkg.13; Mon, 07 Dec 2020 18:16:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ud8rBXD383H8tEM/7RAKMWYYnbYnYvq5454vllAa0OE=; b=Qqo7OMfIODT0cQfm8dnnj7xuaz2fFGAPpLPtCbF+RJSR0PcsyCBCzFQcsYJ52hbBvn WP0NoKT9tH9GH3/0d0wKCcZU88ei84NxIU+3VX5TmXrfR92NWToMsLWs47+7pyolak72 mY5GLhKPbJMCf6a5MmUawtDm6FEsp3Zgm8CM1ItZbCbPIgLcS5C45RA4mLz6o4rimZZ5 9jG2vjB+ZXWG4DIEH8G4lM1QQHCq/0RISNpiYjD85MI31U8/fcNQ6xw/WA8OwicScduq So9NMhbU3b3/igZl5m/4Tr5DJNDrqZYAbCIIwC5TQvyxmKWbPQbVZLH2e80P/s4QAQxV dvtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ud8rBXD383H8tEM/7RAKMWYYnbYnYvq5454vllAa0OE=; b=Yyn1+GDxLKlkPyAhroOzaB1P0f96KRaNPaLvylJlNW6O9y06kxvIYrTpzPCOblMBNH zMbZ1OpNXfziiv7g2XQ9OC7B4kkceOO3ZFjEEbYLyTa5jafr/eYrvcy9tqMSRDeMLNo/ 5UXo01uFSGQu8ojoYrcdrYEF3xwWuQtaEgTBu66xNT6xDL0o6vAUP50YdjJu0Bbl01va MnxKrhWgp2XQGoncjh/LkNagZKooyGYK7BbGsiPrtSVcy6Iw+Vgnphej56YatCeoRePZ VaBCPz9YR5lHHVk3R8pi/Je0pPrWPKe/bHCQUY/eLqwzZh5rD/f6fxXGZXKjNVl4ZR1u 7Yhw== X-Gm-Message-State: AOAM532LG/gy38BUHe5elWWzRRdgg2n0PnqKAHrKrdw7OiD/gH3UiR0d Ujm0Yu0PmLynE44/K5QtHWBBvPtRfqHtIw== X-Google-Smtp-Source: ABdhPJwtzaP1P4kV/OMfdLX2VU8BtGy0weGaN3rGGYXb2OGURumF3K/0Y8TvDMi1DQu/8hTd0EjkSw== X-Received: by 2002:a1f:a541:: with SMTP id o62mr14930646vke.9.1607393797625; Mon, 07 Dec 2020 18:16:37 -0800 (PST) Received: from localhost.localdomain ([50.236.19.102]) by smtp.gmail.com with ESMTPSA id o192sm1936000vko.19.2020.12.07.18.16.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Dec 2020 18:16:37 -0800 (PST) From: Yafang Shao To: darrick.wong@oracle.com, willy@infradead.org, david@fromorbit.com, hch@infradead.org, mhocko@kernel.org, akpm@linux-foundation.org, dhowells@redhat.com, jlayton@redhat.com Cc: linux-fsdevel@vger.kernel.org, linux-cachefs@redhat.com, linux-xfs@vger.kernel.org, linux-mm@kvack.org, Yafang Shao , Christoph Hellwig Subject: [PATCH v10 4/4] xfs: use current->journal_info to avoid transaction reservation recursion Date: Tue, 8 Dec 2020 10:15:43 +0800 Message-Id: <20201208021543.76501-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20201208021543.76501-1-laoar.shao@gmail.com> References: <20201208021543.76501-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org PF_FSTRANS which is used to avoid transaction reservation recursion, is dropped since commit 9070733b4efa ("xfs: abstract PF_FSTRANS to PF_MEMALLOC_NOFS") and commit 7dea19f9ee63 ("mm: introduce memalloc_nofs_{save,restore} API") and replaced by PF_MEMALLOC_NOFS which means to avoid filesystem reclaim recursion. As these two flags have different meanings, we'd better reintroduce PF_FSTRANS back. To avoid wasting the space of PF_* flags in task_struct, we can reuse the current->journal_info to do that, per Willy. As the check of transaction reservation recursion is used by XFS only, we can move the check into xfs_vm_writepage(s), per Dave. To better abstract that behavoir, two new helpers are introduced, as follows, - xfs_trans_context_active To check whehter current is in fs transcation or not - xfs_trans_context_swap Transfer the transaction context when rolling a permanent transaction These two new helpers are instroduced in xfs_trans.h. Cc: Darrick J. Wong Cc: Matthew Wilcox (Oracle) Cc: Christoph Hellwig Cc: Dave Chinner Cc: Michal Hocko Cc: David Howells Cc: Jeff Layton Signed-off-by: Yafang Shao --- fs/iomap/buffered-io.c | 7 ------- fs/xfs/xfs_aops.c | 17 +++++++++++++++++ fs/xfs/xfs_trans.c | 3 +++ fs/xfs/xfs_trans.h | 25 +++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c index 10cc7979ce38..3c53fa6ce64d 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -1458,13 +1458,6 @@ iomap_do_writepage(struct page *page, struct writeback_control *wbc, void *data) PF_MEMALLOC)) goto redirty; - /* - * Given that we do not allow direct reclaim to call us, we should - * never be called in a recursive filesystem reclaim context. - */ - if (WARN_ON_ONCE(current->flags & PF_MEMALLOC_NOFS)) - goto redirty; - /* * Is this page beyond the end of the file? * diff --git a/fs/xfs/xfs_aops.c b/fs/xfs/xfs_aops.c index 2371187b7615..28db93d0da97 100644 --- a/fs/xfs/xfs_aops.c +++ b/fs/xfs/xfs_aops.c @@ -568,6 +568,16 @@ xfs_vm_writepage( { struct xfs_writepage_ctx wpc = { }; + /* + * Given that we do not allow direct reclaim to call us, we should + * never be called while in a filesystem transaction. + */ + if (xfs_trans_context_active()) { + redirty_page_for_writepage(wbc, page); + unlock_page(page); + return 0; + } + return iomap_writepage(page, wbc, &wpc.ctx, &xfs_writeback_ops); } @@ -579,6 +589,13 @@ xfs_vm_writepages( struct xfs_writepage_ctx wpc = { }; xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED); + /* + * Given that we do not allow direct reclaim to call us, we should + * never be called while in a filesystem transaction. + */ + if (xfs_trans_context_active()) + return 0; + return iomap_writepages(mapping, wbc, &wpc.ctx, &xfs_writeback_ops); } diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c index fe20398a214e..08d4916ffb13 100644 --- a/fs/xfs/xfs_trans.c +++ b/fs/xfs/xfs_trans.c @@ -124,6 +124,9 @@ xfs_trans_dup( tp->t_rtx_res = tp->t_rtx_res_used; ntp->t_pflags = tp->t_pflags; + /* Associate the new transaction with this thread. */ + xfs_trans_context_swap(tp, ntp); + /* move deferred ops over to the new tp */ xfs_defer_move(ntp, tp); diff --git a/fs/xfs/xfs_trans.h b/fs/xfs/xfs_trans.h index 44b11c64a15e..82c6735e40fc 100644 --- a/fs/xfs/xfs_trans.h +++ b/fs/xfs/xfs_trans.h @@ -268,16 +268,41 @@ xfs_trans_item_relog( return lip->li_ops->iop_relog(lip, tp); } +static inline bool +xfs_trans_context_active(void) +{ + /* Use journal_info to indicate current is in a transaction */ + if (WARN_ON_ONCE(current->journal_info != NULL)) + return true; + + return false; +} + static inline void xfs_trans_context_set(struct xfs_trans *tp) { + ASSERT(!current->journal_info); + current->journal_info = tp; tp->t_pflags = memalloc_nofs_save(); } static inline void xfs_trans_context_clear(struct xfs_trans *tp) { + ASSERT(current->journal_info == tp); + current->journal_info = NULL; memalloc_nofs_restore(tp->t_pflags); } +/* + * Transfer the transaction context when rolling a permanent + * transaction. + */ +static inline void +xfs_trans_context_swap(struct xfs_trans *tp, struct xfs_trans *ntp) +{ + ASSERT(current->journal_info == tp); + current->journal_info = ntp; +} + #endif /* __XFS_TRANS_H__ */