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: 11978889 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 683B2C2BBCD for ; Thu, 17 Dec 2020 01:13:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 314622389B for ; Thu, 17 Dec 2020 01:13:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389041AbgLQBM5 (ORCPT ); Wed, 16 Dec 2020 20:12:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388972AbgLQBM4 (ORCPT ); Wed, 16 Dec 2020 20:12:56 -0500 Received: from mail-vs1-xe2d.google.com (mail-vs1-xe2d.google.com [IPv6:2607:f8b0:4864:20::e2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80AC6C06179C; Wed, 16 Dec 2020 17:12:16 -0800 (PST) Received: by mail-vs1-xe2d.google.com with SMTP id p7so14047882vsf.8; 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=kKFEgWaSZYlPmD19VnAuem1wLkUmWApRO0k83BWWT+sSf+Tc59bNNb10uYmAagfXiF 6yL/mcPWlT6LiV0jWZa+O5w4t57JtmECDLVZmSnfWAK3CsCohcucDIG8H7CbDxnW4CAP Xee1R+79NgECDHPoZkmixTndD2Gx0caaMiw8biecai0MVFUxUdWHq2olpKuex5jB0Wjj Dqs7iP8Oy8vW7Rig6brd6NCLifXn9QYb5pXrAACvQUq8ogrvtZ1GPJ08tely7l/1+Fk9 ID8nKZGj/31w+Gf8+4MgwytM5/Y+mTqYshitrnzfY9l0QADsKU380LGzrYY/ml9PFGWF A8rg== X-Gm-Message-State: AOAM5333AnC5frx3aqPO6LLRUz/t6kzUo5pfDRWgXOwqBOVpSO2I6aJa h5ctF4P9V4Y09nfInjyqmbE= 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 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 | 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: 11978891 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE8FBC2BBD5 for ; Thu, 17 Dec 2020 01:13:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9DF19238E3 for ; Thu, 17 Dec 2020 01:13:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389055AbgLQBND (ORCPT ); Wed, 16 Dec 2020 20:13:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388972AbgLQBNC (ORCPT ); Wed, 16 Dec 2020 20:13:02 -0500 Received: from mail-ua1-x931.google.com (mail-ua1-x931.google.com [IPv6:2607:f8b0:4864:20::931]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29BF8C0617A6; Wed, 16 Dec 2020 17:12:22 -0800 (PST) Received: by mail-ua1-x931.google.com with SMTP id y25so5115214uaq.7; Wed, 16 Dec 2020 17:12:22 -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=HCpJGylwC/F0bWfC0PI4zFdJZAvXihOWgR7eqRtT2r7eEArGBXGvTq9+dfMbfimeYX T+LN7cT3VhMVbR4oegSoto6N25bPSDakWvu/g/DC2phnv7fkHPNk2pAqr2WE5Nv0KC1M k1awuamgEQ8lG0IdPVHZoTQ9D08MkwwZ5qdf567muTOz0Mx0Y3B+lGBk7ndc8bsrA+2K lZy9dlI+nkusM2czNLNwMxt4MQKols0gEiKhGUczzHPjVQWdI+u1MhLuniDjHFbNFc5p tzDuEGxOq55HE2YRvDraukeHoqGqff2tiDWT6BRYa6tOE4uxA+8/MdF5l6f39hGFJFOn u1aw== X-Gm-Message-State: AOAM531t6GvatGI7Gprmn2nwf/5tgcGi6a9OsBUyaX+KNdzfze4KljoF vGSKXHHYeUnSVJsppdgXOcA= 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 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 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: 11978895 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B33BAC2D0E4 for ; Thu, 17 Dec 2020 01:13:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7884222D05 for ; Thu, 17 Dec 2020 01:13:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389085AbgLQBNI (ORCPT ); Wed, 16 Dec 2020 20:13:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389016AbgLQBNI (ORCPT ); Wed, 16 Dec 2020 20:13:08 -0500 Received: from mail-ua1-x92a.google.com (mail-ua1-x92a.google.com [IPv6:2607:f8b0:4864:20::92a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC86EC0617A7; Wed, 16 Dec 2020 17:12:27 -0800 (PST) Received: by mail-ua1-x92a.google.com with SMTP id s23so8628534uaq.10; 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=WrC0OLORi1xlTy4Y9WC/Zp5+ezJv5bbC5wTHfp4a2Z3LNSflgaP/LIv/qGahLyNZYZ fX94Gpv8UIXk0a0mJNXqGMTxtZTTPpir8b7CtHADaPUvxfQp8E4a9yqeezHhkf9WCE4Q DZjbCVuPSfkVUSmrJHvDAQYdM1t73e7eA+Y0ABH1d3K+xL2m+HYmAafYobFIMFW/xbfr mu3h9xOmdS9EQkh9HiZ+kILxP6SrATgRs2Tyvqf77ZlUHkTnX5OdgwR4vY9gsJtPVI5h vNY99d7gMXzZ94t735U+CZ53d3xSKD600GzFhWuZxNMURnBNqNcb5qt5hg75IC8aUNjN qo8w== X-Gm-Message-State: AOAM532GB9O4qCIv+zh34FbesnuIQyjCpS6lEiOknsIdnVUmfVw3L3be BMa1DgMu55cTESTqD+i2nbs= 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 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 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: 11978897 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0464BC3526C for ; Thu, 17 Dec 2020 01:13:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C735E22D05 for ; Thu, 17 Dec 2020 01:13:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389016AbgLQBNc (ORCPT ); Wed, 16 Dec 2020 20:13:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725871AbgLQBNc (ORCPT ); Wed, 16 Dec 2020 20:13:32 -0500 Received: from mail-vs1-xe36.google.com (mail-vs1-xe36.google.com [IPv6:2607:f8b0:4864:20::e36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0441FC06138C; Wed, 16 Dec 2020 17:12:33 -0800 (PST) Received: by mail-vs1-xe36.google.com with SMTP id e15so12549429vsa.0; 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=uFFdvix69oFsXkVgW99anWq3fTBojBLKzgr09ZQtvW7byQjFIM+Fn8u1ZRGxBNu3EB rkezMZDqUpnZ6xLcLkkeNFjmwfehkWS2Rr0Sgh7mZD0Ki+z22f+WrzdcLzXUVr74zfJy 1+acVqK5qFaPKzUYbhqsDOtFDRxmm9Nr46/IAB0Aj9pjltoiSmLIW2pOdu3JlfAqYikI 4HnxsO7zvFPo2JV+bSeSBnODuWd3urT3DHtVDGOfkTqzFygtfhA4FGt+UBWOK++eY4i3 qLG5kKZW/qJz55Fk27bJLNYHL9PqNzbbB47j9DcJwOz8gvikImevIaqmZJ5uXcGEkAAo lH6A== X-Gm-Message-State: AOAM530Pp50QnzwiQPC65yWMJh3w5EajK6BWFwvcL1vdm8D9BbgpocrN K9RQaO3xemd9ncDG50WWr7E3LBue5e/NBivF 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 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. 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__ */