From patchwork Wed Dec 9 13:11:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 11961341 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=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,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=ham 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 881D0C4361B for ; Wed, 9 Dec 2020 13:12:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 308E423B45 for ; Wed, 9 Dec 2020 13:12:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 308E423B45 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id BC3036B00E2; Wed, 9 Dec 2020 08:12:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B71448D001E; Wed, 9 Dec 2020 08:12:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A38F26B00E5; Wed, 9 Dec 2020 08:12:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0232.hostedemail.com [216.40.44.232]) by kanga.kvack.org (Postfix) with ESMTP id 877266B00E2 for ; Wed, 9 Dec 2020 08:12:38 -0500 (EST) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 564791EE6 for ; Wed, 9 Dec 2020 13:12:38 +0000 (UTC) X-FDA: 77573783196.15.fall33_050265a273ef Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin15.hostedemail.com (Postfix) with ESMTP id 109F11814B0C7 for ; Wed, 9 Dec 2020 13:12:38 +0000 (UTC) X-HE-Tag: fall33_050265a273ef X-Filterd-Recvd-Size: 7997 Received: from mail-qk1-f194.google.com (mail-qk1-f194.google.com [209.85.222.194]) by imf43.hostedemail.com (Postfix) with ESMTP for ; Wed, 9 Dec 2020 13:12:37 +0000 (UTC) Received: by mail-qk1-f194.google.com with SMTP id y18so1065186qki.11 for ; Wed, 09 Dec 2020 05:12:37 -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=Vo2hMZxAvlst2gwI48xlPnEAZTs7fn2tgVy4jkvkfak=; b=rA0uPedR6iMFDORqVibYIouE2sHlfBsLJl/kDe5KAyEAiHd/5lBoupnyE10Vk+5LPp fO01gbxW8xbv1FBkrvWBN1BtDVW1kS2tD8sTUeV0lwDNYEbHVGF0NYDMtZv+0T0Km9ZA /u7b/RQh7hCku29/FDMiRYzfjCp1IpWsu8tUf2zFYw21T59oj0Ke78GOTuAOQdpcrugq tt5gPAz5wwdiQWbFSGiBgYmXw5GX1SwqR1dY7OX1bOZjE2ZBOc929vIRTMPQfyWI7p/j /j0ajn4wLxea+xgZyyNTsRqR6tsXIKqS4R0QHYhpacoGraXbMeEqS43YUyPSw6Tk08aF mE5w== 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=Vo2hMZxAvlst2gwI48xlPnEAZTs7fn2tgVy4jkvkfak=; b=IxwhFeYShIzvnQMOTMmkpuOsk1t4CaLlBbLZvKIBEI/ATDUdYII9cDAgT8ovzjd2WJ 2QAu1z6tUJOiDlQeJtYBumX2QNIA7ce91IuvhYucCIkoWVAUqJOpyi/710trWtcpFvZn LzlRIhV0gP8QrZFnJcvAJOo1zNUQM76qQ1ShMULTCVybys2VU0Zjy0xUTwjXpGfoFRjI DCgWB91jJweM5DwD7nj9f0XIZUhDMvUi64JeNfBKdd+nGOGDWpKy7XNJYJsrf2k9b5Z7 L+GaXyk15W6gsR4L+bu9vM2KVQ++bLgj9HiChCDhZa/da+kSSiMWSohsXVHJRw7ynoMi 7mlQ== X-Gm-Message-State: AOAM530KkkimaJ3nuYAcvDSeh4u2S0b/WBI+YAjreYQ/p+cOvsGORVLf /GjNv0MUL5+Pe7przPo8QeqsiDN9CJIx4A== X-Google-Smtp-Source: ABdhPJzE8FiJCH3XNPdUh271c8izjZ1PbF4C+rKvyzEFeqiYiRR5QcVOZ62jupL5NknQ1sFGJAj5tw== X-Received: by 2002:a37:8384:: with SMTP id f126mr2848840qkd.500.1607519557106; Wed, 09 Dec 2020 05:12:37 -0800 (PST) Received: from localhost.localdomain ([50.236.19.102]) by smtp.gmail.com with ESMTPSA id u72sm938114qka.15.2020.12.09.05.12.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Dec 2020 05:12:36 -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 v12 4/4] xfs: use current->journal_info to avoid transaction reservation recursion Date: Wed, 9 Dec 2020 21:11:46 +0800 Message-Id: <20201209131146.67289-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20201209131146.67289-1-laoar.shao@gmail.com> References: <20201209131146.67289-1-laoar.shao@gmail.com> MIME-Version: 1.0 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: 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 | 4 +++- fs/xfs/xfs_trans.h | 23 +++++++++++++++++++++++ 4 files changed, 43 insertions(+), 8 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..0da0242d42c3 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 (WARN_ON_ONCE(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 (WARN_ON_ONCE(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 4f4645329bb2..7793391fe536 100644 --- a/fs/xfs/xfs_trans.c +++ b/fs/xfs/xfs_trans.c @@ -130,7 +130,9 @@ xfs_trans_dup( ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used; 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..e994b01eeb57 100644 --- a/fs/xfs/xfs_trans.h +++ b/fs/xfs/xfs_trans.h @@ -268,16 +268,39 @@ 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 */ + return current->journal_info != NULL; +} + 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); + ntp->t_pflags = tp->t_pflags; + current->journal_info = ntp; +} + #endif /* __XFS_TRANS_H__ */