From mboxrd@z Thu Jan 1 00:00:00 1970 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=-3.6 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=no 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 50EA7C433E0 for ; Mon, 15 Jun 2020 15:10:50 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1109C2063A for ; Mon, 15 Jun 2020 15:10:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="erBmHgGu" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1109C2063A 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 B14306B000E; Mon, 15 Jun 2020 11:10:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AEADA6B0010; Mon, 15 Jun 2020 11:10:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A28646B0022; Mon, 15 Jun 2020 11:10:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0034.hostedemail.com [216.40.44.34]) by kanga.kvack.org (Postfix) with ESMTP id 8D4376B000E for ; Mon, 15 Jun 2020 11:10:49 -0400 (EDT) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 4BEA6184AE5EA for ; Mon, 15 Jun 2020 15:10:49 +0000 (UTC) X-FDA: 76931783418.11.fight63_581287026df7 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin11.hostedemail.com (Postfix) with ESMTP id D51C51856A1E9 for ; Mon, 15 Jun 2020 15:08:58 +0000 (UTC) X-HE-Tag: fight63_581287026df7 X-Filterd-Recvd-Size: 5981 Received: from mail-il1-f195.google.com (mail-il1-f195.google.com [209.85.166.195]) by imf10.hostedemail.com (Postfix) with ESMTP for ; Mon, 15 Jun 2020 15:08:58 +0000 (UTC) Received: by mail-il1-f195.google.com with SMTP id x18so15623952ilp.1 for ; Mon, 15 Jun 2020 08:08:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=CdXI19ffeyY6WRTLT9+esIHJP/80L+l0kkbdQuiJhrY=; b=erBmHgGuqLDESD9mdlhGoK0umkwSwqGQksoZMaU1fN7M7L+2VlZx02Q/HGKNBymkQS VX4G9rbcVbbB/4c7M0Njf3ts+1GMUw6/5DzQ2xiWq2TYNdqBKK+mjahsn/P2wQFWPJbZ ahsLZ6U3cKbHclwh56uw02SxPBmRoNLw2mX17l1+G6FIvT1DhsLrq1ttlD9areZSwM5J kkAvOyQ+DnwxQud+xABW/x14FMM2EKJcWkB1v3f5GxKwen/SPb55DC2YRurvbf97yFCr x37VXNN7ZFx2wC68vCqPMKy4shgcuRzJmKxyjJseEJ8gQ1VYMEIvR7sq5CpruFTo8T/D U2vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=CdXI19ffeyY6WRTLT9+esIHJP/80L+l0kkbdQuiJhrY=; b=TW0KHiG364/uP7IzO2XTZ0G9V6pIYhg7FpRL9/z+0YKT/mSiYDDcgrpjmnQirYPR4o RK2CWmEOGHVxlEMp0vpEX2MvlylHotW5baQlf4g1PK0TVcC07UxRidnXzHSKPHHE+jQE cGarsSjBYoTCwCxebwncFTxi/roUKsoJbEXvBhU+T/tYDJuUEDblCNhp5sGVUYL32wtw XU99kta3KjsqaeL+rtP9Uax648DL+uT5a/dx81GP4Uvi/6VwF+lR6TGtEPbLSjHvNLTF FdNAGErzrtSfbiWy+jSzyQhGNI/Ig8no+SLovc5L9WLAtJs/Umo3W13wKY+0cd9z98x6 aFwA== X-Gm-Message-State: AOAM530ohUUCCOn8uc/tCjrEMyvto9IdumXOruow07yIoQnXlivp0Fx0 QMu29RCTFGFwjCAkQg5hknoGhE6IQ/v7p6OkefzoKgSIg9A5PA== X-Google-Smtp-Source: ABdhPJwRCc/XqfycakbU+ys1K6CJUetTIQW6G++/2ptBcZjrvKZV3xBG2Xg8WE0ZVi8o2Qm26i6QvN10kG2yF53HRI8= X-Received: by 2002:a92:cf52:: with SMTP id c18mr26766350ilr.142.1592233737645; Mon, 15 Jun 2020 08:08:57 -0700 (PDT) MIME-Version: 1.0 References: <1592222181-9832-1-git-send-email-laoar.shao@gmail.com> <20200615145331.GK25296@dhcp22.suse.cz> In-Reply-To: <20200615145331.GK25296@dhcp22.suse.cz> From: Yafang Shao Date: Mon, 15 Jun 2020 23:08:21 +0800 Message-ID: Subject: Re: [PATCH v3] xfs: avoid deadlock when trigger memory reclaim in ->writepages To: Michal Hocko Cc: =?UTF-8?Q?Holger_Hoffst=C3=A4tte?= , linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, Linux MM Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: D51C51856A1E9 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam05 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: On Mon, Jun 15, 2020 at 10:53 PM Michal Hocko wrote: > > On Mon 15-06-20 16:25:52, Holger Hoffst=C3=A4tte wrote: > > On 2020-06-15 13:56, Yafang Shao wrote: > [...] > > > diff --git a/fs/xfs/xfs_aops.c b/fs/xfs/xfs_aops.c > > > index b356118..1ccfbf2 100644 > > > --- a/fs/xfs/xfs_aops.c > > > +++ b/fs/xfs/xfs_aops.c > > > @@ -573,9 +573,21 @@ static inline bool xfs_ioend_needs_workqueue(str= uct iomap_ioend *ioend) > > > struct writeback_control *wbc) > > > { > > > struct xfs_writepage_ctx wpc =3D { }; > > > + unsigned int nofs_flag; > > > + int ret; > > > xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED); > > > - return iomap_writepages(mapping, wbc, &wpc.ctx, &xfs_writeback_op= s); > > > + > > > + /* > > > + * We can allocate memory here while doing writeback on behalf of > > > + * memory reclaim. To avoid memory allocation deadlocks set the > > > + * task-wide nofs context for the following operations. > > > + */ > > > + nofs_flag =3D memalloc_nofs_save(); > > > + ret =3D iomap_writepages(mapping, wbc, &wpc.ctx, &xfs_writeback_o= ps); > > > + memalloc_nofs_restore(nofs_flag); > > > + > > > + return ret; > > > } > > > STATIC int > > > > > > > Not sure if I did something wrong, but while the previous version of th= is patch > > worked fine, this one gave me (with v2 removed obviously): > > > > [ +0.000004] WARNING: CPU: 0 PID: 2811 at fs/iomap/buffered-io.c:1544 = iomap_do_writepage+0x6b4/0x780 > > This corresponds to > /* > * Given that we do not allow direct reclaim to call us, we shoul= d > * never be called in a recursive filesystem reclaim context. > */ > if (WARN_ON_ONCE(current->flags & PF_MEMALLOC_NOFS)) > goto redirty; > > which effectivelly says that memalloc_nofs_save/restore cannot be used > for that code path. Hi Michal, My understanding is that this warning is to tell us we don't want a recursive filesystem reclaim with PF_MEMALLOC_NOFS being specifically set, but unfortunately PF_MEMALLOC_NOFS doesn't work so it comes here again. IOW, PF_MEMALLOC_NOFS can be set after this check, like what I did in v2. [= 1] > Your stack trace doesn't point to a reclaim path > which shows that this path is shared and also underlines that this is > not really an intended use of the api. Please refer to > Documentation/core-api/gfp_mask-from-fs-io.rst for more details but > shortly the API should be used at the layer which defines a context > which shouldn't allow to recurse. E.g. a lock which would be problematic > in the reclaim recursion path. Thanks for your information. As pointed out by Dave in v1[2] that iomap_do_writepage() can be called with a locked page cache page and calls ->map_blocks from that context. [1]. https://lore.kernel.org/linux-xfs/1591254347-15912-1-git-send-email-la= oar.shao@gmail.com/ [2] https://lore.kernel.org/linux-xfs/20200603222741.GQ2040@dread.disaster.= area/ --=20 Thanks Yafang