From patchwork Wed Jan 13 10:22:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12016411 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 91E7EC433DB for ; Wed, 13 Jan 2021 10:23:15 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DFAFB20789 for ; Wed, 13 Jan 2021 10:23:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DFAFB20789 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 6A1F78D002F; Wed, 13 Jan 2021 05:23:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 684A88D002E; Wed, 13 Jan 2021 05:23:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 568B88D002F; Wed, 13 Jan 2021 05:23:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0163.hostedemail.com [216.40.44.163]) by kanga.kvack.org (Postfix) with ESMTP id 4082B8D002E for ; Wed, 13 Jan 2021 05:23:14 -0500 (EST) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 0F14B181AEF30 for ; Wed, 13 Jan 2021 10:23:14 +0000 (UTC) X-FDA: 77700364308.29.swim85_190a6b62751d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin29.hostedemail.com (Postfix) with ESMTP id E7D1618086CCF for ; Wed, 13 Jan 2021 10:23:13 +0000 (UTC) X-HE-Tag: swim85_190a6b62751d X-Filterd-Recvd-Size: 7537 Received: from mail-oi1-f169.google.com (mail-oi1-f169.google.com [209.85.167.169]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Wed, 13 Jan 2021 10:23:13 +0000 (UTC) Received: by mail-oi1-f169.google.com with SMTP id q205so1497820oig.13 for ; Wed, 13 Jan 2021 02:23:13 -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=/Yc1Bbct8Nbc3LO9koCFr8h15MIb1wQ1a9mmnGoYcpk=; b=F6RNMrMlYlzlU+fcPLjxov7vGoK83JqogMt3GbZw5C8ko+w0YNb0VU9TfYdJyEi+QB N0S9gSR1qUCJgdrm/9aW4x5oGrccJAAbnKWrchroHLgAM9fco5zocBW1pJpV/3nFGaSF HmG0TR4ferxT2svYXpxc5gCbyROpUmdy6FfbL4nuMfCmInSBnbqLctn8nr9EAe94rk1U FV9YxvNfI5eRr7JcOCMffZs/t9uXopakVv0pDxbbKMwcHydriwbTcGBbtaRH2I9ID8IW ZjRz3zbGVfLn1cnUu58Nu3EWCtOpEZrkYH/gaMoFBeLpx3cOLnGmmbcaoyQjQQ8B08et ew5A== 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=/Yc1Bbct8Nbc3LO9koCFr8h15MIb1wQ1a9mmnGoYcpk=; b=oNBwZCmJDROFNSvFjqEv+JZtEoz9UxEj7MSRhRpcSOOgKND5J34gPqb5K03OMOqHIF 4q4HI1AcIKDIVip3ai0Mr7xB2AyLugUQC4jpDNNUcv8ciCLus7PmToA73R9c1XGRv1lx kZpJ7GxUcKyYn61wuM4GDMnmrqG6BtctQhIPmZRd112+MiJmUzCesVZD7f3Mvst/phfo PxftMDCuM/26fIHKsMfpMlTi69/5JQEOb9Xe4TvXne1HXX+jW0RGyh13/oq5Lh3cDmdM SCSiJOG6+JAQBMlbyb1Yi14C2ByjEfeJcxP3mEpexmjvTlVg6LwfSJwdaIUm+fe7gGMr a0oQ== X-Gm-Message-State: AOAM5307HVq5AzYCuhDZszW4491oohk3FvC8Id/hDbteC6RhPRa5/ziQ hDWdo4gldIviYMbkwi3YPN8= X-Google-Smtp-Source: ABdhPJzUiAkWIHr2kwijHoq+jSnJ10xMB1fk4Dfd7rhcx9Shi9eFjtqGc0F+UWJoXBSC8NNwusBIFQ== X-Received: by 2002:a54:4391:: with SMTP id u17mr728149oiv.30.1610533392875; Wed, 13 Jan 2021 02:23:12 -0800 (PST) Received: from localhost.localdomain ([50.236.19.102]) by smtp.gmail.com with ESMTPSA id z8sm335571oon.10.2021.01.13.02.23.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Jan 2021 02:23:12 -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, oliver.sang@intel.com, Michal Hocko , Christoph Hellwig , Yafang Shao Subject: [PATCH v15 1/4] mm: Add become_kswapd and restore_kswapd Date: Wed, 13 Jan 2021 18:22:21 +0800 Message-Id: <20210113102224.13655-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210113102224.13655-1-laoar.shao@gmail.com> References: <20210113102224.13655-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: From: "Matthew Wilcox (Oracle)" Since XFS needs to pretend to be kswapd in some of its worker threads, create methods to save & restore kswapd state. Don't bother restoring kswapd state in kswapd -- the only time we reach this code is when we're exiting and the task_struct is about to be destroyed anyway. Cc: Dave Chinner Acked-by: Michal Hocko Reviewed-by: Darrick J. Wong Reviewed-by: Christoph Hellwig Signed-off-by: Matthew Wilcox (Oracle) Signed-off-by: Yafang Shao --- fs/xfs/libxfs/xfs_btree.c | 14 ++++++++------ include/linux/sched/mm.h | 22 ++++++++++++++++++++++ mm/vmscan.c | 16 +--------------- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/fs/xfs/libxfs/xfs_btree.c b/fs/xfs/libxfs/xfs_btree.c index c4d7a9241dc3..c43f2ad85af9 100644 --- a/fs/xfs/libxfs/xfs_btree.c +++ b/fs/xfs/libxfs/xfs_btree.c @@ -2813,8 +2813,9 @@ xfs_btree_split_worker( { struct xfs_btree_split_args *args = container_of(work, struct xfs_btree_split_args, work); + bool is_kswapd = args->kswapd; unsigned long pflags; - unsigned long new_pflags = PF_MEMALLOC_NOFS; + int memalloc_nofs; /* * we are in a transaction context here, but may also be doing work @@ -2822,16 +2823,17 @@ xfs_btree_split_worker( * temporarily to ensure that we don't block waiting for memory reclaim * in any way. */ - if (args->kswapd) - new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; - - current_set_flags_nested(&pflags, new_pflags); + if (is_kswapd) + pflags = become_kswapd(); + memalloc_nofs = memalloc_nofs_save(); args->result = __xfs_btree_split(args->cur, args->level, args->ptrp, args->key, args->curp, args->stat); complete(args->done); - current_restore_flags_nested(&pflags, new_pflags); + memalloc_nofs_restore(memalloc_nofs); + if (is_kswapd) + restore_kswapd(pflags); } /* diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index d5ece7a9a403..7195e542a54d 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -278,6 +278,28 @@ static inline void memalloc_nocma_restore(unsigned int flags) } #endif +/* + * Tell the memory management code that this thread is working on behalf + * of background memory reclaim (like kswapd). That means that it will + * get access to memory reserves should it need to allocate memory in + * order to make forward progress. With this great power comes great + * responsibility to not exhaust those reserves. + */ +#define KSWAPD_PF_FLAGS (PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD) + +static inline unsigned long become_kswapd(void) +{ + unsigned long flags = current->flags & KSWAPD_PF_FLAGS; + + current->flags |= KSWAPD_PF_FLAGS; + return flags; +} + +static inline void restore_kswapd(unsigned long flags) +{ + current->flags = (current->flags & ~KSWAPD_PF_FLAGS) | flags; +} + #ifdef CONFIG_MEMCG DECLARE_PER_CPU(struct mem_cgroup *, int_active_memcg); /** diff --git a/mm/vmscan.c b/mm/vmscan.c index 7b4e31eac2cf..15af99d1f3f7 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3870,19 +3870,7 @@ static int kswapd(void *p) if (!cpumask_empty(cpumask)) set_cpus_allowed_ptr(tsk, cpumask); - /* - * Tell the memory management that we're a "memory allocator", - * and that if we need more memory we should get access to it - * regardless (see "__alloc_pages()"). "kswapd" should - * never get caught in the normal page freeing logic. - * - * (Kswapd normally doesn't need memory anyway, but sometimes - * you need a small amount of memory in order to be able to - * page out something else, and this flag essentially protects - * us from recursively trying to free more memory as we're - * trying to free the first piece of memory in the first place). - */ - tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; + become_kswapd(); set_freezable(); WRITE_ONCE(pgdat->kswapd_order, 0); @@ -3932,8 +3920,6 @@ static int kswapd(void *p) goto kswapd_try_sleep; } - tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); - return 0; } From patchwork Wed Jan 13 10:22:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12016413 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 E956EC433E0 for ; Wed, 13 Jan 2021 10:23:21 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 81B9A22CE3 for ; Wed, 13 Jan 2021 10:23:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 81B9A22CE3 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 C95E08D0030; Wed, 13 Jan 2021 05:23:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C46068D002E; Wed, 13 Jan 2021 05:23:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B361E8D0030; Wed, 13 Jan 2021 05:23:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0172.hostedemail.com [216.40.44.172]) by kanga.kvack.org (Postfix) with ESMTP id 9E7C88D002E for ; Wed, 13 Jan 2021 05:23:20 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 6C6C4181AEF30 for ; Wed, 13 Jan 2021 10:23:20 +0000 (UTC) X-FDA: 77700364560.21.talk11_3e052d02751d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin21.hostedemail.com (Postfix) with ESMTP id 485A6180442CB for ; Wed, 13 Jan 2021 10:23:20 +0000 (UTC) X-HE-Tag: talk11_3e052d02751d X-Filterd-Recvd-Size: 7579 Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) by imf15.hostedemail.com (Postfix) with ESMTP for ; Wed, 13 Jan 2021 10:23:19 +0000 (UTC) Received: by mail-oi1-f174.google.com with SMTP id x13so1558224oic.5 for ; Wed, 13 Jan 2021 02:23:19 -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=3/11ISBt7WYmjp71KLDTRlzeHUcwb0Yal3998QWyZ48=; b=acqzDSZhxLYp5H1lO5JKtduiefIe/+c/BkFYrwq60cbFt8MkdBtR2SDayxZ015Kvx2 pCPgujC5296/h+gd+mU7+NQ1YFdwiQBLHQ9Q+aruY5o6qTrVPBuXW8X8QTWissFzeSaw DdodlW3bWS1yr8iUKA4DtW3WhPxbAtwBMU2iYRM8o2vmCupyP4zoCdR/cwIko+v+CGXw ZTpxog1qRvMEDuQOLJWyk7fwvDgPfyPxZt/6FG5Yf6Hrww+4SOACSLzC4xDGWX7gcdZ+ 06HjGT/eN0z3g2Ifoa8GAkGmvwNM0wCTkyc8EPpUM8PkrrGQJqF/l0wTRbh/1JDByisr qOGw== 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=3/11ISBt7WYmjp71KLDTRlzeHUcwb0Yal3998QWyZ48=; b=MvPxGccngKgxP/y+XLzZo2sQPFpoAHb1xq7B5TiCFbNQsLdyAY+TYNs8lvwFMjRl3S srrsG5LD+C2ti9twN0oq6qVEGh3B0YaCdWs+q5xvL90oFckt9SLv2UUBYOQNa/vrxJPE owZ4jTkZXX6atAM+1a0pFOZVyN00Jf3N6783PBrm07tsqWFnx4jJGv8sx1d6lvR6l7m1 1tJt95AWy0fYbat4F4c/JwkoUeeidS+fgHQF/5poav8P0hZ7lS6pZ3d80LFBf3OWW3Jt oiJj9qai1j45HjnepApFJjToPl0cErBLZ86odp9IZwb7o2gks9I2j06Qwa7DL2fl56nN +wEQ== X-Gm-Message-State: AOAM5317mw7gY2iJnXQgjf/7/lK9koG51z6wmUesF02kCOv3JdvRhYqw wsdwFbk9Dowzq/moEsXkeQA= X-Google-Smtp-Source: ABdhPJzJFZurx7cc8zH05Ut0U7s9oDfrw3PWX7hZoe6EK+hPB3SjdAVPBOIL7+RXPQM7TxqW+/FNeg== X-Received: by 2002:aca:2b0c:: with SMTP id i12mr725650oik.76.1610533399236; Wed, 13 Jan 2021 02:23:19 -0800 (PST) Received: from localhost.localdomain ([50.236.19.102]) by smtp.gmail.com with ESMTPSA id z8sm335571oon.10.2021.01.13.02.23.13 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Jan 2021 02:23:18 -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, oliver.sang@intel.com, Yafang Shao , Christoph Hellwig Subject: [PATCH v15 2/4] xfs: use memalloc_nofs_{save,restore} in xfs transaction Date: Wed, 13 Jan 2021 18:22:22 +0800 Message-Id: <20210113102224.13655-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210113102224.13655-1-laoar.shao@gmail.com> References: <20210113102224.13655-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: Introduce a new API to mark the start and end of XFS transactions. For now, just save and restore the memalloc_nofs flags. The new helpers as follows, - xfs_trans_context_set Mark the start of XFS transactions - xfs_trans_context_clear Mark the end of XFS transactions Cc: Matthew Wilcox (Oracle) Cc: Christoph Hellwig Cc: Dave Chinner Cc: Michal Hocko Reviewed-by: Darrick J. Wong Signed-off-by: Yafang Shao --- fs/xfs/xfs_aops.c | 4 ++-- fs/xfs/xfs_linux.h | 4 ---- fs/xfs/xfs_trans.c | 13 +++++++------ fs/xfs/xfs_trans.h | 12 ++++++++++++ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/fs/xfs/xfs_aops.c b/fs/xfs/xfs_aops.c index 4304c6416fbb..2371187b7615 100644 --- a/fs/xfs/xfs_aops.c +++ b/fs/xfs/xfs_aops.c @@ -62,7 +62,7 @@ xfs_setfilesize_trans_alloc( * We hand off the transaction to the completion thread now, so * clear the flag here. */ - current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); + xfs_trans_context_clear(tp); return 0; } @@ -125,7 +125,7 @@ xfs_setfilesize_ioend( * thus we need to mark ourselves as being in a transaction manually. * Similarly for freeze protection. */ - current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); + xfs_trans_context_set(tp); __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS); /* we abort the update if there was an IO error */ diff --git a/fs/xfs/xfs_linux.h b/fs/xfs/xfs_linux.h index 5b7a1e201559..6ab0f8043c73 100644 --- a/fs/xfs/xfs_linux.h +++ b/fs/xfs/xfs_linux.h @@ -102,10 +102,6 @@ typedef __u32 xfs_nlink_t; #define xfs_cowb_secs xfs_params.cowb_timer.val #define current_cpu() (raw_smp_processor_id()) -#define current_set_flags_nested(sp, f) \ - (*(sp) = current->flags, current->flags |= (f)) -#define current_restore_flags_nested(sp, f) \ - (current->flags = ((current->flags & ~(f)) | (*(sp) & (f)))) #define NBBY 8 /* number of bits per byte */ diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c index e72730f85af1..43107af569fe 100644 --- a/fs/xfs/xfs_trans.c +++ b/fs/xfs/xfs_trans.c @@ -154,7 +154,7 @@ xfs_trans_reserve( bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; /* Mark this thread as being in a transaction */ - current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); + xfs_trans_context_set(tp); /* * Attempt to reserve the needed disk blocks by decrementing @@ -164,7 +164,7 @@ xfs_trans_reserve( if (blocks > 0) { error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd); if (error != 0) { - current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); + xfs_trans_context_clear(tp); return -ENOSPC; } tp->t_blk_res += blocks; @@ -241,7 +241,7 @@ xfs_trans_reserve( tp->t_blk_res = 0; } - current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); + xfs_trans_context_clear(tp); return error; } @@ -878,7 +878,7 @@ __xfs_trans_commit( xfs_log_commit_cil(mp, tp, &commit_lsn, regrant); - current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); + xfs_trans_context_clear(tp); xfs_trans_free(tp); /* @@ -910,7 +910,8 @@ __xfs_trans_commit( xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket); tp->t_ticket = NULL; } - current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); + + xfs_trans_context_clear(tp); xfs_trans_free_items(tp, !!error); xfs_trans_free(tp); @@ -971,7 +972,7 @@ xfs_trans_cancel( } /* mark this thread as no longer being in a transaction */ - current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); + xfs_trans_context_clear(tp); xfs_trans_free_items(tp, dirty); xfs_trans_free(tp); diff --git a/fs/xfs/xfs_trans.h b/fs/xfs/xfs_trans.h index 084658946cc8..44b11c64a15e 100644 --- a/fs/xfs/xfs_trans.h +++ b/fs/xfs/xfs_trans.h @@ -268,4 +268,16 @@ xfs_trans_item_relog( return lip->li_ops->iop_relog(lip, tp); } +static inline void +xfs_trans_context_set(struct xfs_trans *tp) +{ + tp->t_pflags = memalloc_nofs_save(); +} + +static inline void +xfs_trans_context_clear(struct xfs_trans *tp) +{ + memalloc_nofs_restore(tp->t_pflags); +} + #endif /* __XFS_TRANS_H__ */ From patchwork Wed Jan 13 10:22:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12016415 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 0BDD6C433E0 for ; Wed, 13 Jan 2021 10:23:28 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8B3C323117 for ; Wed, 13 Jan 2021 10:23:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8B3C323117 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 DA6F38D0031; Wed, 13 Jan 2021 05:23:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D57D38D002E; Wed, 13 Jan 2021 05:23:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C6D088D0031; Wed, 13 Jan 2021 05:23:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id AECC48D002E for ; Wed, 13 Jan 2021 05:23:26 -0500 (EST) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 713FC33C4 for ; Wed, 13 Jan 2021 10:23:26 +0000 (UTC) X-FDA: 77700364812.23.prose13_59017822751d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin23.hostedemail.com (Postfix) with ESMTP id 4E87D37604 for ; Wed, 13 Jan 2021 10:23:26 +0000 (UTC) X-HE-Tag: prose13_59017822751d X-Filterd-Recvd-Size: 7530 Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.44]) by imf02.hostedemail.com (Postfix) with ESMTP for ; Wed, 13 Jan 2021 10:23:25 +0000 (UTC) Received: by mail-ot1-f44.google.com with SMTP id i6so1435301otr.2 for ; Wed, 13 Jan 2021 02:23:25 -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=XxclDVwqVJvNjL6ogvvc7lutzmJKTCIl3aL2mVuveRU=; b=ayzzHhYzcClTcR5UidZ9RaV7JooeTbg7NYZeAKxe+8VXLL4MzCKSEQ+HRiDI3hcGRV lpycfcOJxoosIgx3X6xdop/IL+4QW5enr6e+YLrZMvNP/zfEA+JQ8ZoTOOZ9AUAdCYKL N2YwIn+bjH9pdPanotuQS37H/zfAVEh5kjkWNLHyLlIAgOk+O6L1ngtZGclIZyM/urzg gszDoW00rAbwKLEld222Gf8/1aG465V1HN89e/8R+i0WLj4Z7uusaomm/YBxTNnEaAVA 9Da+RFc607qjDU/QocHb0I+2wQHexo0B1KZsab0gPXqreVTPPXHqSxmzRQzni59RCt02 lgQw== 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=XxclDVwqVJvNjL6ogvvc7lutzmJKTCIl3aL2mVuveRU=; b=pPwgcSK/7AY1mBDZm+70DeBiLnSgO4KBNRaeRDeCnDZcA2pPTFJq8Mm6o2U1mIDSei t0Votd6uYpJfSarLPzLJyVW3y9YBYdukTrAjsbksZaPvN7IaSfYCrT/Y0hPJnvcoizFs iCDk/1FM+a7jIl7GgOa1akLtvl2jjB46U9HP66R2dtsFF1nksP1+RwZE5yDe97px3HIx Yapvl+bvol83k3NL2uLKtrptaj2SM299M9NLamkkqxHkVqttsc4CILQ4wvrWkGw1TAIT C1fO5n7hRzXDybfEqmO7zJ8Aj0Qivr795UTbSlw9X3ozwVEKjey/a5ts4M7cg55m6j8a G9vw== X-Gm-Message-State: AOAM531kbG7kGEO6fTsahmLLCgnphXMe1Ga71gxmAf7hJPttmznWdN/E k3Gf6Oe+Tt5eKUW5drO8N4E= X-Google-Smtp-Source: ABdhPJy3SuuCBXWeZ64qnoWABbZ8wtAGotjYfWPZu33kh0kuO2TTGVs0UOsxp8cqTnyLedLhUOmebA== X-Received: by 2002:a9d:694a:: with SMTP id p10mr722519oto.149.1610533405317; Wed, 13 Jan 2021 02:23:25 -0800 (PST) Received: from localhost.localdomain ([50.236.19.102]) by smtp.gmail.com with ESMTPSA id z8sm335571oon.10.2021.01.13.02.23.19 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Jan 2021 02:23:24 -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, oliver.sang@intel.com, Yafang Shao , Christoph Hellwig Subject: [PATCH v15 3/4] xfs: refactor the usage around xfs_trans_context_{set,clear} Date: Wed, 13 Jan 2021 18:22:23 +0800 Message-Id: <20210113102224.13655-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210113102224.13655-1-laoar.shao@gmail.com> References: <20210113102224.13655-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: The xfs_trans context should be active after it is allocated, and deactive when it is freed. The rolling transaction should be specially considered, because in the case when we clear the old transaction the thread's NOFS state shouldn't be changed, as a result we have to set NOFS in the old transaction's t_pflags in xfs_trans_context_swap(). So these helpers are refactored as, - xfs_trans_context_set() Used in xfs_trans_alloc() - xfs_trans_context_clear() Used in xfs_trans_free() And a new helper is instroduced to handle the rolling transaction, - xfs_trans_context_swap() Used in rolling transaction This patch is based on Darrick's work[1] to fix the issue in xfs/141 in the earlier version and Dave's suggestion[2]. [1]. https://lore.kernel.org/linux-xfs/20201104001649.GN7123@magnolia [2]. https://lore.kernel.org/linux-xfs/20201218000705.GR632069@dread.disaster.area Cc: Dave Chinner Cc: Darrick J. Wong Cc: Matthew Wilcox (Oracle) Cc: Christoph Hellwig Signed-off-by: Yafang Shao --- fs/xfs/xfs_trans.c | 25 +++++++++++-------------- fs/xfs/xfs_trans.h | 10 +++++++++- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c index 43107af569fe..b76e850c9ae7 100644 --- a/fs/xfs/xfs_trans.c +++ b/fs/xfs/xfs_trans.c @@ -67,6 +67,10 @@ xfs_trans_free( xfs_extent_busy_sort(&tp->t_busy); xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false); + + /* Detach the transaction from this thread. */ + xfs_trans_context_clear(tp); + trace_xfs_trans_free(tp, _RET_IP_); if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT)) sb_end_intwrite(tp->t_mountp->m_super); @@ -119,7 +123,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); @@ -153,9 +159,6 @@ xfs_trans_reserve( int error = 0; bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; - /* Mark this thread as being in a transaction */ - xfs_trans_context_set(tp); - /* * Attempt to reserve the needed disk blocks by decrementing * the number needed from the number available. This will @@ -163,10 +166,9 @@ xfs_trans_reserve( */ if (blocks > 0) { error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd); - if (error != 0) { - xfs_trans_context_clear(tp); + if (error != 0) return -ENOSPC; - } + tp->t_blk_res += blocks; } @@ -241,8 +243,6 @@ xfs_trans_reserve( tp->t_blk_res = 0; } - xfs_trans_context_clear(tp); - return error; } @@ -284,6 +284,8 @@ xfs_trans_alloc( INIT_LIST_HEAD(&tp->t_dfops); tp->t_firstblock = NULLFSBLOCK; + /* Mark this thread as being in a transaction */ + xfs_trans_context_set(tp); error = xfs_trans_reserve(tp, resp, blocks, rtextents); if (error) { xfs_trans_cancel(tp); @@ -878,7 +880,6 @@ __xfs_trans_commit( xfs_log_commit_cil(mp, tp, &commit_lsn, regrant); - xfs_trans_context_clear(tp); xfs_trans_free(tp); /* @@ -911,7 +912,6 @@ __xfs_trans_commit( tp->t_ticket = NULL; } - xfs_trans_context_clear(tp); xfs_trans_free_items(tp, !!error); xfs_trans_free(tp); @@ -971,9 +971,6 @@ xfs_trans_cancel( tp->t_ticket = NULL; } - /* mark this thread as no longer being in a transaction */ - xfs_trans_context_clear(tp); - xfs_trans_free_items(tp, dirty); xfs_trans_free(tp); } diff --git a/fs/xfs/xfs_trans.h b/fs/xfs/xfs_trans.h index 44b11c64a15e..3645fd0d74b8 100644 --- a/fs/xfs/xfs_trans.h +++ b/fs/xfs/xfs_trans.h @@ -277,7 +277,15 @@ xfs_trans_context_set(struct xfs_trans *tp) static inline void xfs_trans_context_clear(struct xfs_trans *tp) { - memalloc_nofs_restore(tp->t_pflags); + if (!tp->t_pflags) + memalloc_nofs_restore(tp->t_pflags); +} + +static inline void +xfs_trans_context_swap(struct xfs_trans *tp, struct xfs_trans *ntp) +{ + ntp->t_pflags = tp->t_pflags; + tp->t_pflags = -1; } #endif /* __XFS_TRANS_H__ */ From patchwork Wed Jan 13 10:22:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12016417 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 25621C433E0 for ; Wed, 13 Jan 2021 10:23:34 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 97C972337F for ; Wed, 13 Jan 2021 10:23:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 97C972337F 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 2349F8D0033; Wed, 13 Jan 2021 05:23:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1E48F8D0032; Wed, 13 Jan 2021 05:23:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 122298D0033; Wed, 13 Jan 2021 05:23:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0098.hostedemail.com [216.40.44.98]) by kanga.kvack.org (Postfix) with ESMTP id EFF1A8D0032 for ; Wed, 13 Jan 2021 05:23:32 -0500 (EST) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id B5672824CA00 for ; Wed, 13 Jan 2021 10:23:32 +0000 (UTC) X-FDA: 77700365064.15.eyes69_4a14af22751d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin15.hostedemail.com (Postfix) with ESMTP id 8DD121814B0C7 for ; Wed, 13 Jan 2021 10:23:32 +0000 (UTC) X-HE-Tag: eyes69_4a14af22751d X-Filterd-Recvd-Size: 6991 Received: from mail-ot1-f53.google.com (mail-ot1-f53.google.com [209.85.210.53]) by imf49.hostedemail.com (Postfix) with ESMTP for ; Wed, 13 Jan 2021 10:23:32 +0000 (UTC) Received: by mail-ot1-f53.google.com with SMTP id x13so1399299oto.8 for ; Wed, 13 Jan 2021 02:23:32 -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=2Hoa9myK143cV0+5FfOAtzs2mXHnYs56TUMaxDFngxI=; b=A27KBa9Qa0CiPAirttd1MC3c+k7HXbqbNcZaGLIuO02YysUlSXQe9nftLGDAbk+k1Y m5vlobOmcK2hqS2qzoIMlFmrlm78z+S5YpNhleNyEkdCDlJlMZcES7zOwZhpVJukDoJ2 G+FqJ4ETTUSzfZYR716nzjqMzhCkMlQ2b5rYQ5iLd+G60zEIWQ2zHlTq6sZS+LfN9Ybv tekBdi0j5Nf8H44wvix5j6SwwlNGbKwSwkCWxj7UDmWeGm5H6CZCtAkXVp3L4YyVyagJ bp1r3zK3LekfVEfyXBfd7B/4W8GGe5gvL/tuUO9t0ZoNqLhWuY9jEgf6aI4mkHb8vFx6 LfzQ== 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=2Hoa9myK143cV0+5FfOAtzs2mXHnYs56TUMaxDFngxI=; b=IDS90LopqNKNWlGv6Yw6pgZ8UBFSkw/T/zc3Ognn+BYHhyyhGswHCdn5NCBn9PCPkm wseuBqC7qN6O1R/BJIAd30PRaXBlY8V/SRec6ygHI+K/j2C+W28VVUFI1ELFY0M6qBLY NgQ5ggd0haPeGFEA56CGsiRZaDVoPMmpMeJJflI+C9ra9LCQVHflxjCVdxpOIXWxgfhY 2XJOozteN01J5f38tNiiiBIC0ZJl2El4syXPAiGs9O4GxABCK9+HHVSxHY1mY+X82FL/ CMT8AmZytip8KJEnU/SJoMd8j6Up+ZbP++//h2kh1OLQwBu78NADM4u7GJ1AyHjCin7m sx9Q== X-Gm-Message-State: AOAM533bE/fMPwPyHn5LSQ+uY5mGtUHpvKzHaiGaEicU2od0uL/MxzCj sj+AE2FnwNjHePlBZbk9klU= X-Google-Smtp-Source: ABdhPJzMbFRs70OCbw4e3J96c+SlsfFE7Ic6psQgivnf/qYiMFYIWrf1tQemCSk4aiSnqSS+tSYWsg== X-Received: by 2002:a9d:12d7:: with SMTP id g81mr761901otg.103.1610533411738; Wed, 13 Jan 2021 02:23:31 -0800 (PST) Received: from localhost.localdomain ([50.236.19.102]) by smtp.gmail.com with ESMTPSA id z8sm335571oon.10.2021.01.13.02.23.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Jan 2021 02:23:31 -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, oliver.sang@intel.com, Yafang Shao , Christoph Hellwig Subject: [PATCH v15 4/4] xfs: use current->journal_info to avoid transaction reservation recursion Date: Wed, 13 Jan 2021 18:22:24 +0800 Message-Id: <20210113102224.13655-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210113102224.13655-1-laoar.shao@gmail.com> References: <20210113102224.13655-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 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. [oliver.sang@intel.com: reported a Assertion_failed in the prev version] Cc: Darrick J. Wong Cc: Matthew Wilcox (Oracle) Cc: Christoph Hellwig Cc: Dave Chinner Cc: Michal Hocko Cc: David Howells Cc: Jeff Layton Cc: kernel test robot Signed-off-by: Yafang Shao --- fs/iomap/buffered-io.c | 7 ------- fs/xfs/xfs_aops.c | 10 ++++++++++ fs/xfs/xfs_trans.h | 22 +++++++++++++++++++--- 3 files changed, 29 insertions(+), 10 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..eed4881d4461 100644 --- a/fs/xfs/xfs_aops.c +++ b/fs/xfs/xfs_aops.c @@ -568,6 +568,12 @@ xfs_vm_writepage( { struct xfs_writepage_ctx wpc = { }; + 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 +585,10 @@ xfs_vm_writepages( struct xfs_writepage_ctx wpc = { }; xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED); + + 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.h b/fs/xfs/xfs_trans.h index 3645fd0d74b8..e2f3251d6cce 100644 --- a/fs/xfs/xfs_trans.h +++ b/fs/xfs/xfs_trans.h @@ -268,24 +268,40 @@ xfs_trans_item_relog( return lip->li_ops->iop_relog(lip, tp); } +static inline bool +xfs_trans_context_active(void) +{ + 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) { - if (!tp->t_pflags) - memalloc_nofs_restore(tp->t_pflags); + /* + * If we handed over the context via xfs_trans_context_swap() then + * the context is no longer needed to clear. + */ + if (current->journal_info != tp) + return; + + current->journal_info = NULL; + memalloc_nofs_restore(tp->t_pflags); } static inline void xfs_trans_context_swap(struct xfs_trans *tp, struct xfs_trans *ntp) { + ASSERT(current->journal_info == tp); + current->journal_info = ntp; ntp->t_pflags = tp->t_pflags; - tp->t_pflags = -1; } #endif /* __XFS_TRANS_H__ */