From patchwork Wed Jun 24 15:05:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 11623451 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 51701138C for ; Wed, 24 Jun 2020 15:07:14 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 11617206C0 for ; Wed, 24 Jun 2020 15:07:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SLXKFqjZ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 11617206C0 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 191756B0033; Wed, 24 Jun 2020 11:07:13 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 11B566B0037; Wed, 24 Jun 2020 11:07:13 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EFDD06B0055; Wed, 24 Jun 2020 11:07:12 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0212.hostedemail.com [216.40.44.212]) by kanga.kvack.org (Postfix) with ESMTP id D2BCA6B0033 for ; Wed, 24 Jun 2020 11:07:12 -0400 (EDT) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 81AC322876 for ; Wed, 24 Jun 2020 15:07:12 +0000 (UTC) X-FDA: 76964433504.21.cover02_030162926e45 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin21.hostedemail.com (Postfix) with ESMTP id 2B988180445F7 for ; Wed, 24 Jun 2020 15:06:23 +0000 (UTC) X-Spam-Summary: 1,0,0,2a7e86ebaf9e3247,d41d8cd98f00b204,laoar.shao@gmail.com,,RULES_HIT:2:41:355:379:541:800:960:966:973:981:988:989:1260:1345:1437:1535:1605:1730:1747:1777:1792:2196:2198:2199:2200:2393:2553:2559:2562:2892:2895:2897:2910:2924:2926:3138:3139:3140:3141:3142:3664:3865:3866:3867:3868:3870:3871:3872:3874:4049:4120:4250:4321:4385:4605:5007:6117:6119:6261:6653:7514:7903:8660:9413:10004:10226:11026:11232:11473:11658:11914:12043:12048:12296:12297:12438:12517:12519:12555:12663:12895:12986:13148:13230:13255:14096:14394:14687:14877:21080:21433:21444:21451:21627:21666:21740:21809:21939:21990:30003:30054:30069:30070:30075:30090,0,RBL:209.85.222.195:@gmail.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100;04ygm8pz3pu3zsb5x9phzgkne54e9opphww1toa93hp31anrkpz4fmgaezyh85e.f87zh6gsuqpzut8cu13iaee7kiejgs5nu4k9kr5upfbzjwybxh4u4tp5w97rkuu.e-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Cust om_rules X-HE-Tag: cover02_030162926e45 X-Filterd-Recvd-Size: 9463 Received: from mail-qk1-f195.google.com (mail-qk1-f195.google.com [209.85.222.195]) by imf04.hostedemail.com (Postfix) with ESMTP for ; Wed, 24 Jun 2020 15:06:22 +0000 (UTC) Received: by mail-qk1-f195.google.com with SMTP id l6so2103974qkc.6 for ; Wed, 24 Jun 2020 08:06:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=BLinj9Kg0W1/34KrqwmkEhLauk6NHTmIpjaR2XnC5qs=; b=SLXKFqjZG3FQG2tZdxU/Ss/zollqCbWEQXMya7rMx4rr4DSab5IXUwelYXnsqu6TEv +40cvniBKzWETgRrHpSjGDbG9cza1NcmfGwQoeJJUXU9x+lxVkVm265zy3TrUBYa/3X0 xL2XdG2g+DQaXAedWpWLByIod4YNC+DTxb/iKfekG0+mae3gq/3Cy5E3KCwpW4/ss7zu O3VYUrRldjczm6MxHGVJ5pXGc2kQTtRThrAnE97sf7USiqGN/QSIFk3egGTGoGzIFuFw dRW/DgaN8jU6rql7jTYwWkmLNBA0ND0J/FreCzoNK2NAYuS+cUVLcRoYQ1ELDAy89fEU tSuw== 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; bh=BLinj9Kg0W1/34KrqwmkEhLauk6NHTmIpjaR2XnC5qs=; b=HlS+YeiwhquMZN6Uyaj59ChVIHYTxNmql/dKeZUStVx1r1GwHeFX6PVMgo7fupBIMB gHSnuznny9u+pbvyXxOzqaVG7gnZZrdukz8RorQsstJpPD+/x8nik9oBQj4aUpoU/s+w EXR5TOIGoplMAtOqUPaaZo2W915dxaXklR4VzjL0SMLCyL5VkMj9Tm1ZzOxzX1sYzhA0 Xkd0SPYNLmF7i2BT9xEu/eeBwNkunf5gw5kOonjIgmjDy4kChmOEExIGnuiZgCGA4kGt MimJlzwG6uoxrhuOLm51jmSh+yukGycIMXQEDd0Kd7phs4nfoVnnL9iTQp70T7J2yXya 335Q== X-Gm-Message-State: AOAM531NTQ0+XT6XW/Ia5rgmBFhYPRLpzCxlJsODzUyWAlAkcHJoJniC 0M81jQtjGfDJbfUv4EdJeVM= X-Google-Smtp-Source: ABdhPJxVU1E9AO2pt3jX2R6NoPKTzpapTWz8JPYLuKeelKxaG9eZDZ8E0U+1nG/Y3rqY9wwKZmRPJQ== X-Received: by 2002:a37:59c4:: with SMTP id n187mr4057972qkb.303.1593011182036; Wed, 24 Jun 2020 08:06:22 -0700 (PDT) Received: from dev.localdomain ([183.134.211.54]) by smtp.gmail.com with ESMTPSA id x26sm3435188qtp.54.2020.06.24.08.06.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Jun 2020 08:06:21 -0700 (PDT) From: Yafang Shao To: david@fromorbit.com, mhocko@kernel.org, darrick.wong@oracle.com, hch@infradead.org, akpm@linux-foundation.org, bfoster@redhat.com, vbabka@suse.cz, holger@applied-asynchrony.com Cc: linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Yafang Shao Subject: [PATCH v2] xfs: reintroduce PF_FSTRANS for transaction reservation recursion protection Date: Wed, 24 Jun 2020 11:05:42 -0400 Message-Id: <1593011142-10209-1-git-send-email-laoar.shao@gmail.com> X-Mailer: git-send-email 1.8.3.1 X-Rspamd-Queue-Id: 2B988180445F7 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: PF_FSTRANS which is used to avoid transaction reservation recursion, is dropped since commit 9070733b4efa ("xfs: abstract PF_FSTRANS to PF_MEMALLOC_NOFS") and commit 7dea19f9ee63 ("mm: introduce memalloc_nofs_{save,restore} API") and replaced by PF_MEMALLOC_NOFS which means to avoid filesystem reclaim recursion. That change is subtle. Let's take the exmple of the check of WARN_ON_ONCE(current->flags & PF_MEMALLOC_NOFS)) to explain why this abstraction from PF_FSTRANS to PF_MEMALLOC_NOFS is not proper. Bellow comment is quoted from Dave, > It wasn't for memory allocation recursion protection in XFS - it was for > transaction reservation recursion protection by something trying to flush > data pages while holding a transaction reservation. Doing > this could deadlock the journal because the existing reservation > could prevent the nested reservation for being able to reserve space > in the journal and that is a self-deadlock vector. > IOWs, this check is not protecting against memory reclaim recursion > bugs at all (that's the previous check [1]). This check is > protecting against the filesystem calling writepages directly from a > context where it can self-deadlock. > So what we are seeing here is that the PF_FSTRANS -> > PF_MEMALLOC_NOFS abstraction lost all the actual useful information > about what type of error this check was protecting against. [1]. Bellow check is to avoid memory reclaim recursion. if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == PF_MEMALLOC)) goto redirty; Suggested-by: Dave Chinner Signed-off-by: Yafang Shao Cc: Michal Hocko --- fs/iomap/buffered-io.c | 4 ++-- fs/xfs/libxfs/xfs_btree.c | 2 +- fs/xfs/xfs_aops.c | 4 ++-- fs/xfs/xfs_trans.c | 12 ++++++------ include/linux/sched.h | 1 + 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c index bcfc288..0f1945c 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -1500,9 +1500,9 @@ static void iomap_writepage_end_bio(struct bio *bio) /* * Given that we do not allow direct reclaim to call us, we should - * never be called in a recursive filesystem reclaim context. + * never be called while in a filesystem transaction. */ - if (WARN_ON_ONCE(current->flags & PF_MEMALLOC_NOFS)) + if (WARN_ON_ONCE(current->flags & PF_FSTRANS)) goto redirty; /* diff --git a/fs/xfs/libxfs/xfs_btree.c b/fs/xfs/libxfs/xfs_btree.c index 2d25bab..4a7c8b7 100644 --- a/fs/xfs/libxfs/xfs_btree.c +++ b/fs/xfs/libxfs/xfs_btree.c @@ -2814,7 +2814,7 @@ struct xfs_btree_split_args { struct xfs_btree_split_args *args = container_of(work, struct xfs_btree_split_args, work); unsigned long pflags; - unsigned long new_pflags = PF_MEMALLOC_NOFS; + unsigned long new_pflags = PF_MEMALLOC_NOFS | PF_FSTRANS; /* * we are in a transaction context here, but may also be doing work diff --git a/fs/xfs/xfs_aops.c b/fs/xfs/xfs_aops.c index b356118..79da028 100644 --- a/fs/xfs/xfs_aops.c +++ b/fs/xfs/xfs_aops.c @@ -62,7 +62,7 @@ static inline bool xfs_ioend_is_append(struct iomap_ioend *ioend) * 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); + current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS | PF_FSTRANS); return 0; } @@ -125,7 +125,7 @@ static inline bool xfs_ioend_is_append(struct iomap_ioend *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); + current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS | PF_FSTRANS); __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_trans.c b/fs/xfs/xfs_trans.c index 3c94e5f..011f52f 100644 --- a/fs/xfs/xfs_trans.c +++ b/fs/xfs/xfs_trans.c @@ -153,7 +153,7 @@ 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); + current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS | PF_FSTRANS); /* * Attempt to reserve the needed disk blocks by decrementing @@ -163,7 +163,7 @@ 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); + current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS | PF_FSTRANS); return -ENOSPC; } tp->t_blk_res += blocks; @@ -240,7 +240,7 @@ tp->t_blk_res = 0; } - current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); + current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS | PF_FSTRANS); return error; } @@ -861,7 +861,7 @@ xfs_log_commit_cil(mp, tp, &commit_lsn, regrant); - current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); + current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS | PF_FSTRANS); xfs_trans_free(tp); /* @@ -893,7 +893,7 @@ xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket); tp->t_ticket = NULL; } - current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); + current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS | PF_FSTRANS); xfs_trans_free_items(tp, !!error); xfs_trans_free(tp); @@ -954,7 +954,7 @@ } /* mark this thread as no longer being in a transaction */ - current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); + current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS | PF_FSTRANS); xfs_trans_free_items(tp, dirty); xfs_trans_free(tp); diff --git a/include/linux/sched.h b/include/linux/sched.h index b62e6aa..02045e8 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1511,6 +1511,7 @@ static inline int is_global_init(struct task_struct *tsk) #define PF_KTHREAD 0x00200000 /* I am a kernel thread */ #define PF_RANDOMIZE 0x00400000 /* Randomize virtual address space */ #define PF_SWAPWRITE 0x00800000 /* Allowed to write to swap */ +#define PF_FSTRANS 0x01000000 /* Inside a filesystem transaction */ #define PF_UMH 0x02000000 /* I'm an Usermodehelper process */ #define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_mask */ #define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */