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: 12016421 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.8 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 F337EC43381 for ; Wed, 13 Jan 2021 10:23:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ADE2D22B4B for ; Wed, 13 Jan 2021 10:23:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727222AbhAMKXy (ORCPT ); Wed, 13 Jan 2021 05:23:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726645AbhAMKXx (ORCPT ); Wed, 13 Jan 2021 05:23:53 -0500 Received: from mail-oi1-x232.google.com (mail-oi1-x232.google.com [IPv6:2607:f8b0:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A92EC061786; Wed, 13 Jan 2021 02:23:13 -0800 (PST) Received: by mail-oi1-x232.google.com with SMTP id d203so1577111oia.0; 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=YODrpaCvw8ixFMSngHxqPqDBUdFKaZJ3RDb4hvdG0t8DMayG0iiVxD0pm4xX2Fag5I NL0W+uToR1b36IIFE0pOHIHVAzJu7olsfTeSOHTxUwKvBpv3ujSWWxKpOYm3K4k1psA2 1Fkh4klIfpnLAHr7e2Yv4o/Au5brzYdnhi4149rAjVf65+28sDjCfrqxpgtQqwjm1EG5 6QZUPdwXaZklViGhA2DpjjuHCd+z+tXsYeiqRHFuoWLyeNGsQp7G8iiPM5e+L4lbxWhp oAvFUvbnEd8ZLoGYHAtj0hH2LxOBj8EYlwPiqv41zh7Ouems8FEccNE+qNGdfJJd7kSr 9DrA== X-Gm-Message-State: AOAM532ttrur5IO4xiIb/p6BW12otj81cnf92Lvzd/e/ogFbV2EncQzK ai4Tkh9Q3WilAYu1e1hlp9o= 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 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org 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: 12016423 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.8 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 3C465C4332D for ; Wed, 13 Jan 2021 10:24:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EDF0D23117 for ; Wed, 13 Jan 2021 10:24:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727233AbhAMKYA (ORCPT ); Wed, 13 Jan 2021 05:24:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726017AbhAMKYA (ORCPT ); Wed, 13 Jan 2021 05:24:00 -0500 Received: from mail-oi1-x22e.google.com (mail-oi1-x22e.google.com [IPv6:2607:f8b0:4864:20::22e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B458EC061794; Wed, 13 Jan 2021 02:23:19 -0800 (PST) Received: by mail-oi1-x22e.google.com with SMTP id d203so1577381oia.0; 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=fwjLE6/U2k83gFKOkZv8cBKogUhDqJwRJcM0Um++4xQF6nWvITkz8gtpfg0qZUgDkE ZzwAz75fZIJZRTANJHb9PhDFuso5BKCNelhQN2jRJaO9zWwX23N54lEJELHREH8yrqDQ fUP8Tx2YVNyjDohTNpIvX4l8Oe0BgvfW3k2PNgIqAOC+mY6oOmdoRqkmN2oPjV8Ax4K1 aO+oWp4HEVqseKRWhPYKV+QnY3WqiGpuOaty+xix2bN8544FSyBci4xeLjuGMmFwMbCz +lKBa7UHTdbUmD72KziHHyvd/+nS18yrvz6cdEWvNe0hYwdcozI1/ypI/E+TZalGbNe1 cmZQ== X-Gm-Message-State: AOAM532gUvB8GUGFo9FYBUHlKyp/rDXwDWrqMQt7ecMIKk8N8lVF9Ca4 qjY+3tUn7Bo5G1mW6j83onM= 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 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org 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: 12016425 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.8 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 A2AF2C433DB for ; Wed, 13 Jan 2021 10:24:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5A45522B4B for ; Wed, 13 Jan 2021 10:24:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726017AbhAMKYG (ORCPT ); Wed, 13 Jan 2021 05:24:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725949AbhAMKYG (ORCPT ); Wed, 13 Jan 2021 05:24:06 -0500 Received: from mail-ot1-x32a.google.com (mail-ot1-x32a.google.com [IPv6:2607:f8b0:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFC99C061795; Wed, 13 Jan 2021 02:23:25 -0800 (PST) Received: by mail-ot1-x32a.google.com with SMTP id j20so1417027otq.5; 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=GU13158BfKW+mygrNbFs6Dto4HjdH29hVxX+ml96iHaCw5rEAzOP3kKGcH1gNY4ix1 +h7NUawlce5P0wJa62EiliMU6YbTeNtFgkVHBD4Se0Nt00ZIFgaYqPccdOozu9t40VuZ fM5078sRndHqb9o9vBN9BQ1wIxHSqvMxT8oRNvZATtn+tXmHYJI9zNWPdNPGyE6FA88b Wx0zS16Pz6bJiMIdyqvWzi0GmnZvRuMZ4mfqhsC1nemt4/P/Mbc6zLoNj4kwPnK8xeaZ +BzTAHVHroM04nkTV/tDOvIGiDNePJ4A4v9HSJc94JPd/bTs4Ysw0DUJxRle2OyL+hMU KNVA== X-Gm-Message-State: AOAM533wAaNhSyfEJG4Tp9GRC+eyXDaBZPHwpSeQjUMs6tx/H066pwii rkedtOyjCZDQgVRTBpDrD54= 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 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org 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: 12016427 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.8 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 6F4E0C433E0 for ; Wed, 13 Jan 2021 10:24:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3747D22B4B for ; Wed, 13 Jan 2021 10:24:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727248AbhAMKYN (ORCPT ); Wed, 13 Jan 2021 05:24:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725949AbhAMKYM (ORCPT ); Wed, 13 Jan 2021 05:24:12 -0500 Received: from mail-ot1-x329.google.com (mail-ot1-x329.google.com [IPv6:2607:f8b0:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E1A1C06179F; Wed, 13 Jan 2021 02:23:32 -0800 (PST) Received: by mail-ot1-x329.google.com with SMTP id o11so1425024ote.4; 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=ldS/u21y5uNTFsQGLyB3cXcpgoPZIWQ8s+qWu/vNyikTaUOIzKtxx5MWU3wSLUfNUx hc+eXilm8Zs5otpZS+IBvC1ATYI5dhBjRebPfnCPTNOkaSsSNkBz11sIZCNVkHNvhVio x3hQ0FIntlhvGCYApLjnt6j7g+fhyq10sqcDiOXdItMC1hiynwoWo3NOUjlrim+umxs1 GAQm37co/e2/4cTlaPOHX6tVd8N3r/iVmdOw3I/Q031aUKhZ5DmDWcLiq4YrsWcWF+iw OVRrKZnMl8NYtEysmjBEOu9It9Q6ZUlIHzMV/W1wqtq+xjFsCTCh8ptQBjpNv1tQ2upV LmuQ== X-Gm-Message-State: AOAM531DnY71IJlj9vYw4AsxwfSUF93uoSIjXNYlf1gqyXDcwiAQJSh5 5dApXge8dhZ1wPxzXjrpjwI= 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 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 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__ */