From patchwork Thu Dec 17 01:11:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 11978881 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 A2E63C4361B for ; Thu, 17 Dec 2020 01:12:18 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0689722D57 for ; Thu, 17 Dec 2020 01:12:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0689722D57 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 62E0A6B005D; Wed, 16 Dec 2020 20:12:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E7906B0068; Wed, 16 Dec 2020 20:12:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4558E6B006C; Wed, 16 Dec 2020 20:12:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0161.hostedemail.com [216.40.44.161]) by kanga.kvack.org (Postfix) with ESMTP id 2B4C66B005D for ; Wed, 16 Dec 2020 20:12:17 -0500 (EST) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id EF1AF1EF3 for ; Thu, 17 Dec 2020 01:12:16 +0000 (UTC) X-FDA: 77600998272.30.force75_3f0dce027430 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin30.hostedemail.com (Postfix) with ESMTP id D28EB180B3C85 for ; Thu, 17 Dec 2020 01:12:16 +0000 (UTC) X-HE-Tag: force75_3f0dce027430 X-Filterd-Recvd-Size: 7496 Received: from mail-vs1-f41.google.com (mail-vs1-f41.google.com [209.85.217.41]) by imf23.hostedemail.com (Postfix) with ESMTP for ; Thu, 17 Dec 2020 01:12:16 +0000 (UTC) Received: by mail-vs1-f41.google.com with SMTP id x26so14074872vsq.1 for ; Wed, 16 Dec 2020 17:12:16 -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=N+fYoxrulW4TVGOTkp9bnpkn5FRg1xC0yuEIcSYpseY=; b=m9zRm85NyW0m3ujFgHrZGUfRmc7AyiAR0SnetEtaNxUsi69i9M2HwkeFATKPiC9Le4 Y4suXGdErdxCZZX2kA4yEPPbZZG6DXtuiFQGxfBGbpnophckJr7mMSagRbpCWmU2Zvdj jbiQgo5nPETmhIAeC1L/zlxqzBNPTOJmBIXIEZYllDBZyorR2PudDWHA2003aLQ2Ymy3 9OkwfhsrZA+6itgMuUOy1q2AOjbwxr9r/TaeXz2EONqu9zHU/Oqt8U3aDubtUmGPpL3g K1t1SQSM1MyZG5s7pSqV+pkbsX1jxODoVonLEoD/lmwmkOQ+QqZ7EBQHvshHILqEspLk vZ8A== 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=N+fYoxrulW4TVGOTkp9bnpkn5FRg1xC0yuEIcSYpseY=; b=kf+BBwnGyukio0YJNkG4TwZE9HifhP63i0z0d8fbb9SrUw1pRQA8QLTQWFCsrAADRF 4UUXFxMGhpGM5dF5pqNnnAFpL5aNAtf3RD0ohEIwsMe66y7TezhIEwJKYGfSg9RxZ8jV qa4YqlF+lwcR4fIeCMmARXURZ/tzEBZTo9OWvRTVUiBuF8VMFQhYOq+GggP7u8hyTuHJ BQOjM0Fa5HTcbpOtg/fcT87kX2SGKFgX7W5iMzSYAQuR0jG+Bg51NAYH1/xpoZZGqKL1 xgvnNFU9dkM5V6jvobobmGfzJp79nftnZjZNPhq7ielS42Ebtp4PSLvhCFUXPFh+BY/I C+Qw== X-Gm-Message-State: AOAM5327uwoaFxOKFbPM70loFxgjbndwNwSdVX0jbe5NtZ7BNglpER2x L7wwiNIks1QVavuf+AuZIwc= X-Google-Smtp-Source: ABdhPJzzrvT/RDbvWYAc6ZkkEP+w4PKSJBV8+m3oWiiJMgOgcFAaTQsMJjVO1t2AAhh4CQT/GwXLfA== X-Received: by 2002:a67:ea02:: with SMTP id g2mr36306426vso.3.1608167535710; Wed, 16 Dec 2020 17:12:15 -0800 (PST) Received: from localhost.localdomain ([50.236.19.102]) by smtp.gmail.com with ESMTPSA id i63sm2900760uad.4.2020.12.16.17.12.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Dec 2020 17:12:14 -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, Michal Hocko , Christoph Hellwig , Yafang Shao Subject: [PATCH v13 1/4] mm: Add become_kswapd and restore_kswapd Date: Thu, 17 Dec 2020 09:11:54 +0800 Message-Id: <20201217011157.92549-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20201217011157.92549-1-laoar.shao@gmail.com> References: <20201217011157.92549-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 | 23 +++++++++++++++++++++++ mm/vmscan.c | 16 +--------------- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/fs/xfs/libxfs/xfs_btree.c b/fs/xfs/libxfs/xfs_btree.c index 51dbff9b0908..0f35b7a38e76 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..2faf03e79a1e 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -278,6 +278,29 @@ 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 &= ~(flags ^ KSWAPD_PF_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 Thu Dec 17 01:11:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 11978883 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 134B6C4361B for ; Thu, 17 Dec 2020 01:12:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8D6B92076D for ; Thu, 17 Dec 2020 01:12:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8D6B92076D 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 EDE9A6B0068; Wed, 16 Dec 2020 20:12:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E8F176B006C; Wed, 16 Dec 2020 20:12:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D7DA56B006E; Wed, 16 Dec 2020 20:12:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0237.hostedemail.com [216.40.44.237]) by kanga.kvack.org (Postfix) with ESMTP id BDFCE6B0068 for ; Wed, 16 Dec 2020 20:12:22 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 8EE118249980 for ; Thu, 17 Dec 2020 01:12:22 +0000 (UTC) X-FDA: 77600998524.21.event27_3802c7727430 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin21.hostedemail.com (Postfix) with ESMTP id 6AE87180442C3 for ; Thu, 17 Dec 2020 01:12:22 +0000 (UTC) X-HE-Tag: event27_3802c7727430 X-Filterd-Recvd-Size: 7551 Received: from mail-ua1-f46.google.com (mail-ua1-f46.google.com [209.85.222.46]) by imf18.hostedemail.com (Postfix) with ESMTP for ; Thu, 17 Dec 2020 01:12:21 +0000 (UTC) Received: by mail-ua1-f46.google.com with SMTP id t15so8638065ual.6 for ; Wed, 16 Dec 2020 17:12:21 -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=iBZKS7fZyjdTYTaO3rtutG8028bfWaEOmNu0KLWmO18=; b=t+HLems35Pr8RNGxr64rdX0e2tFJztfFAhl0sEMfhs1jj0NPVY1ACFGm9izrKCkLLQ 2E0wSkU6HhRhCyGZVCNe0xq3J/3N8dqZghWvfYBbqdle0/wLOSFs6XJ4jT2kOjIUByPe fUBbDMOP7LwGspMgWgwS3klZ4+Vs2ucMW4p4ZwjlIzgtY5T0Zqhj0Zu+eo9kWtrI+u7a 3/dG1sS85bUvZ+wQRUus+plQrxr3goCz1bFdjJFb3GJ2EUSbh/DSFItzFGPP8PKPQfUJ kn1ISEhUbYWKxJx2KCgyqqFh26y4EE9j6w+K0WllWtDhKuusyw8iA7HLopZO1chfVf4t QAnw== 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=iBZKS7fZyjdTYTaO3rtutG8028bfWaEOmNu0KLWmO18=; b=qzqJe6nWs16NY6EsspN4JBWejc5GsFZFstzIFaxnVNHFW0mu8POkCgVezbkFtJ9OAK P6/OMJ13yztY0uipbZQLSyhKR8YsvjHT+ua/z8ZTrtage3K1i45xullbZZuILMB0/ajR PRbiVfADUtWf1u7Dm1alCJaKae6odUnMLXjifFVI4TN3revYN9lLTVE0ECtBLb/QK0sn 4DnZeaPMzd9lnjjRVwdXIOA8BP22GN8tIbfFAr0PEdmdVBRk5LoDYeWVI2GfPozSOByG f+KKVx5apEeUWc0+BKEi4r7pwnEYfK4ojEVn215HbglcE0JYUWWEBuYFssm55q/wVu/d 8xWA== X-Gm-Message-State: AOAM530gtN1bKNmfQW5AMn3ek8OPtZR0LH4qxWgiPQcPV7PQtwriyH7T Gza7iLE+PRRmmCpo0SxrslU= X-Google-Smtp-Source: ABdhPJz/YGsgeQJh4SGPLtJui6RuCMR6Y42EbrIIRR4DBSYZK9cZPw6yyRDLwSegXCZqjAGBjAp2IQ== X-Received: by 2002:ab0:2f4:: with SMTP id 107mr129918uah.110.1608167541431; Wed, 16 Dec 2020 17:12:21 -0800 (PST) Received: from localhost.localdomain ([50.236.19.102]) by smtp.gmail.com with ESMTPSA id i63sm2900760uad.4.2020.12.16.17.12.16 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Dec 2020 17:12:20 -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 v13 2/4] xfs: use memalloc_nofs_{save,restore} in xfs transaction Date: Thu, 17 Dec 2020 09:11:55 +0800 Message-Id: <20201217011157.92549-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20201217011157.92549-1-laoar.shao@gmail.com> References: <20201217011157.92549-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 c94e71f741b6..11d390f0d3f2 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 Thu Dec 17 01:11:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 11978885 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 A5290C4361B for ; Thu, 17 Dec 2020 01:12:33 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2DA8422D57 for ; Thu, 17 Dec 2020 01:12:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2DA8422D57 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 8C3DB6B006C; Wed, 16 Dec 2020 20:12:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8738A6B006E; Wed, 16 Dec 2020 20:12:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7629A8D0001; Wed, 16 Dec 2020 20:12:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0221.hostedemail.com [216.40.44.221]) by kanga.kvack.org (Postfix) with ESMTP id 5B9BB6B006C for ; Wed, 16 Dec 2020 20:12:28 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 2AFF0180AD80F for ; Thu, 17 Dec 2020 01:12:28 +0000 (UTC) X-FDA: 77600998776.16.boys40_611238927430 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin16.hostedemail.com (Postfix) with ESMTP id 07C00100E690B for ; Thu, 17 Dec 2020 01:12:28 +0000 (UTC) X-HE-Tag: boys40_611238927430 X-Filterd-Recvd-Size: 7522 Received: from mail-ua1-f51.google.com (mail-ua1-f51.google.com [209.85.222.51]) by imf48.hostedemail.com (Postfix) with ESMTP for ; Thu, 17 Dec 2020 01:12:27 +0000 (UTC) Received: by mail-ua1-f51.google.com with SMTP id 73so1096014uac.8 for ; Wed, 16 Dec 2020 17:12:27 -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=cd4JF0ZR2KfQvs7FahxcUpavGYokTwPG9ns84faHko4=; b=RK/hYKSW5JgHtrKLNvLNfWz0OmgDAm6pj82G4IIu4cjEoJmIANVtGWiHaW4cZij0Vn H0/APDsdXUyiwkheJ0U00RGGiXbsNtKxcx6K3mhyeH1Y3j74E6jpCP/7OUUrPkRZQT1d NmGn1WsVpKz50SFslWF9wUkiGfkPHopeAe/75ouEB7Jp5JIejC875Kfz5qIlcrGfNaxv P2+uW/7atJ5G52HMEA1tgVzOu3p9cFlzlRWmuTu3KPtDVJxKo1O9GujkyLBU/NTZnHSM Dj6E0Q5eGRB/OHz9sp6+1OtrF/5Qy0xrf6hBENTopc0cMRcUrOs2Iw89hidknMqb1l6B oRzA== 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=cd4JF0ZR2KfQvs7FahxcUpavGYokTwPG9ns84faHko4=; b=LmIbqZs14cgYEbedLehWfTaZAWvXC69WgT6ACWA2q5apvuiwi2VwuAKTiSQQPRy/7V QdPcZqyBZ61UvWLzfp98ezIy+kJIUr6LnwS1zjgwyGxfnj84ER7n/94aUxx/IeBU3vOl riem2vqdsD+lRIQ4F282BxiBibeu2clPGCsH6Jhsv90ZI2NnJ0da655wBKYlwaEcK455 8h/EN7wszZIKUE//rRtfJ8jIpPepe+siJo9Bt2hXjMng/ZRAqJRFH1qi5RYadQ8Tr93M xhPmtB+ptSXq99QqL137a0Sa8dxF0hK/0f8lWl+y7q81qQZ8087IUEY/IOMh610RBURC DvOw== X-Gm-Message-State: AOAM533xygakSs/Xh+dPLTmXJmZoU8FIJvjfCvcvtoiFbCXH83Kcj1nX mOYLU0/kxwxPzfWtzPhMnew= X-Google-Smtp-Source: ABdhPJxvwR0b2U/ZCHfDQPXvCX31ndWFVYmWR6GBXd8PtNbPGK6MzLrtIs1Rt0qPK/w+9XAPP4mtTQ== X-Received: by 2002:a9f:204e:: with SMTP id 72mr7554308uam.19.1608167546985; Wed, 16 Dec 2020 17:12:26 -0800 (PST) Received: from localhost.localdomain ([50.236.19.102]) by smtp.gmail.com with ESMTPSA id i63sm2900760uad.4.2020.12.16.17.12.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Dec 2020 17:12:26 -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 v13 3/4] xfs: refactor the usage around xfs_trans_context_{set,clear} Date: Thu, 17 Dec 2020 09:11:56 +0800 Message-Id: <20201217011157.92549-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20201217011157.92549-1-laoar.shao@gmail.com> References: <20201217011157.92549-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 to fix the issue in xfs/141 in the earlier version. [1] 1. https://lore.kernel.org/linux-xfs/20201104001649.GN7123@magnolia Cc: Darrick J. Wong Cc: Matthew Wilcox (Oracle) Cc: Christoph Hellwig Cc: Dave Chinner Signed-off-by: Yafang Shao --- fs/xfs/xfs_trans.c | 25 +++++++++++-------------- fs/xfs/xfs_trans.h | 13 +++++++++++++ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c index 11d390f0d3f2..aa213c3e2408 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..12380eaaf7ce 100644 --- a/fs/xfs/xfs_trans.h +++ b/fs/xfs/xfs_trans.h @@ -280,4 +280,17 @@ xfs_trans_context_clear(struct xfs_trans *tp) 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; + /* + * For the rolling transaction, we have to set NOFS in the old + * transaction's t_pflags so that when we clear the context on + * the old transaction we don't actually change the thread's NOFS + * state. + */ + tp->t_pflags = current->flags | PF_MEMALLOC_NOFS; +} + #endif /* __XFS_TRANS_H__ */ From patchwork Thu Dec 17 01:11:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 11978887 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 E65A5C1B0D8 for ; Thu, 17 Dec 2020 01:12:34 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6E8F12076D for ; Thu, 17 Dec 2020 01:12:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6E8F12076D 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 E1BB66B006E; Wed, 16 Dec 2020 20:12:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DCAFC8D0001; Wed, 16 Dec 2020 20:12:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C1EC56B0071; Wed, 16 Dec 2020 20:12:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0180.hostedemail.com [216.40.44.180]) by kanga.kvack.org (Postfix) with ESMTP id A4B276B006E for ; Wed, 16 Dec 2020 20:12:33 -0500 (EST) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 67F731EF3 for ; Thu, 17 Dec 2020 01:12:33 +0000 (UTC) X-FDA: 77600998986.25.car67_4617d3527430 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin25.hostedemail.com (Postfix) with ESMTP id 49FCA1804E3A0 for ; Thu, 17 Dec 2020 01:12:33 +0000 (UTC) X-HE-Tag: car67_4617d3527430 X-Filterd-Recvd-Size: 7393 Received: from mail-vs1-f43.google.com (mail-vs1-f43.google.com [209.85.217.43]) by imf05.hostedemail.com (Postfix) with ESMTP for ; Thu, 17 Dec 2020 01:12:32 +0000 (UTC) Received: by mail-vs1-f43.google.com with SMTP id p7so14048163vsf.8 for ; Wed, 16 Dec 2020 17:12: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=9alW6vAYyuzuIAm2W1sBzzo1XFQKdglwiDiVNpF1K2U=; b=Wi5ul+TpVrtbHopu2Iwb3DlGk1ZxVzDtLAsRdQMTu0GERUdvG2WVNpqpfK16fgUS9h yhq0822cOkKhzQ7ZRWz8Npvm048BUEnToPYJy+xnZwyVe3RDHJLz0OF/hw55IGWeldgE 5eN4vwFCW486dOUy2PHx60b2avDZZplCXBLtp/MZ7UGJPsDYcXQeO1lq3yyxiP14Ndbr c3KSA6WFYM51Cm3HJNKmNc96A2rjtwuTYDUHTxjQG6h9I+beliWreu54pMsFr24ff8cW Bl8HuQt0co6Qc5ubOg4BSBFIANtBrd9eSaTpbhD0uV6moRHkdHgfcLJJB4b+Q/X3+GZ2 HG7Q== 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=9alW6vAYyuzuIAm2W1sBzzo1XFQKdglwiDiVNpF1K2U=; b=B7B0RQp+50ucAwB04b+kc1B333bccJlEcdCfvt6FN3BXKlBtBr2UQharWtcQCeNlri yniID3Pu46MBDZX0i3Pw3yv+E8xyRacTyYu/11sYN/YwjmWOTY97u3Fbq243S5NTXj5N WIR9DOkqXr/OYonmeQznMxGGKkBQed83YVvTYozFq+SCFDK4+wKA6yPRCJ8e++wMoMnC xAnW0uGmXuTxPgOteWq8thl5QuylOiX6ccBxgYFu6KzE8I3rHm+GC7lmQ5x1SFWSetSr jP9szcrpbTHG2e4IgdsIO1UsoGVYx2qgwA5JBAHh0CAhlAq6QkW4I8iAQYwvv3xQZuYw yL2Q== X-Gm-Message-State: AOAM530JjdETpSdGBq+RGhHPpReQbV6h+jA9YyhNnaNlkpo+Z4QdW8Z3 8//fhegJVFTGqNzC9Wk2hjY= X-Google-Smtp-Source: ABdhPJymD38OIInUxz/A6UMIS832ePfAYJx4qTFdpzI+xn54beO/QqmFh11xxFmKvkJ1ebq2NwfIEA== X-Received: by 2002:a67:1142:: with SMTP id 63mr15960710vsr.24.1608167552295; Wed, 16 Dec 2020 17:12:32 -0800 (PST) Received: from localhost.localdomain ([50.236.19.102]) by smtp.gmail.com with ESMTPSA id i63sm2900760uad.4.2020.12.16.17.12.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Dec 2020 17:12: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, Yafang Shao , Christoph Hellwig Subject: [PATCH v13 4/4] xfs: use current->journal_info to avoid transaction reservation recursion Date: Thu, 17 Dec 2020 09:11:57 +0800 Message-Id: <20201217011157.92549-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20201217011157.92549-1-laoar.shao@gmail.com> References: <20201217011157.92549-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. 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.h | 26 +++++++++++++++++++------- 3 files changed, 36 insertions(+), 14 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.h b/fs/xfs/xfs_trans.h index 12380eaaf7ce..0c8140147b9b 100644 --- a/fs/xfs/xfs_trans.h +++ b/fs/xfs/xfs_trans.h @@ -268,29 +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 */ + 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 xfs_trans_context_swap() handed the NOFS context to a + * new transaction we do not clear the context here. + */ + 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; - /* - * For the rolling transaction, we have to set NOFS in the old - * transaction's t_pflags so that when we clear the context on - * the old transaction we don't actually change the thread's NOFS - * state. - */ - tp->t_pflags = current->flags | PF_MEMALLOC_NOFS; } #endif /* __XFS_TRANS_H__ */