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=-0.6 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED 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 281E2C433E0 for ; Thu, 18 Jun 2020 11:05:30 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D325C2078D for ; Thu, 18 Jun 2020 11:05:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="X8DQwMbc" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D325C2078D 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 84F4A8D000B; Thu, 18 Jun 2020 07:05:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8004E8D0001; Thu, 18 Jun 2020 07:05:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 715D78D000B; Thu, 18 Jun 2020 07:05:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0227.hostedemail.com [216.40.44.227]) by kanga.kvack.org (Postfix) with ESMTP id 5680E8D0001 for ; Thu, 18 Jun 2020 07:05:29 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 0B5C5181AC9BF for ; Thu, 18 Jun 2020 11:05:29 +0000 (UTC) X-FDA: 76942051578.20.look54_5411fcb26e0f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin20.hostedemail.com (Postfix) with ESMTP id 71429180C08FE for ; Thu, 18 Jun 2020 11:05:26 +0000 (UTC) X-HE-Tag: look54_5411fcb26e0f X-Filterd-Recvd-Size: 5706 Received: from mail-io1-f67.google.com (mail-io1-f67.google.com [209.85.166.67]) by imf07.hostedemail.com (Postfix) with ESMTP for ; Thu, 18 Jun 2020 11:05:25 +0000 (UTC) Received: by mail-io1-f67.google.com with SMTP id s18so6562795ioe.2 for ; Thu, 18 Jun 2020 04:05:25 -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; bh=lvd3pIj+tWBoeTv5FpltPgwLvfskcVybHdrc5kKxO6Y=; b=X8DQwMbcf5Krh9k+oLulHjiUuvnnxP2Z4E13xrA/lO3Tlh/9roxRFYolP0cY9EnFjw WFowAyOz9DvtNu76Pygkksd/D3PGcqVfQAZ5Ujkb0q0tLCEwvXTY2CWUEVQpTw3Dilf8 qprA16JC2kUDs29OzvCVVHYbWK1cKgx79/vAyLv4q+Q2G/Wxcm7+8FVc5ff/Tn7oBDj/ r1SmyITtQEvbxUlIJMuph7seUNw5TcpX6vFb8q/ZN/u7iGggXgD9uvHoTqJnO2ZIQRUu MhGUu4biC2V+acZsUm/b8LpsypuH9DMpISZCCE+Is3UmD0tEuMNdrd3XZEjOfqCSBdx3 yN6A== 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; bh=lvd3pIj+tWBoeTv5FpltPgwLvfskcVybHdrc5kKxO6Y=; b=fMI5Dpfre87ufDVuIKrdbkxvOoZ9mXm679jrtD2cNXNJUQG23I8vQ36rmJW4NmCcJe 6fvXK5faQfA8+RgWbT5Ew1E7ZyJbM9JQCVVOJ255Q5bq6qqbpq2HYsX/6s4viU13D6w5 1xOBsfyiitlPqxICs8GsagwbA0bQUn8+8F6M8gpCkHtgmtHou8yMR68nOJXkKsnR2WXT stMBqdoaQoqOs/KZ1kmks9BVc5O+KU+15gHBWez+Z9mZ4Fsjm0EG/X3RDfSWgE93TG2V GAizL9yTsWjZiaAKagIh1JI5wXZrnVsn1TJKkWJfLIJBasqKYwQryFL8qjxodsy3VI18 VtgA== X-Gm-Message-State: AOAM5329aFS41ozS+Snc8S8dG98OSvAKU6H6JuicqBUULL92eJwgFvN4 IPJhcGmnoAlg+70J0DBJqFlguEagTI8JSJ/rg6Q= X-Google-Smtp-Source: ABdhPJxF7QN7rHiGQq2iqDeLUd0PNzN1JOcG99q/rVp9Wrjhswig4yTInE8Kk+BVnb+KWzAteZUcOup3r2agEL3iCKE= X-Received: by 2002:a05:6638:10b:: with SMTP id x11mr3729129jao.109.1592478325264; Thu, 18 Jun 2020 04:05:25 -0700 (PDT) MIME-Version: 1.0 References: <1592222181-9832-1-git-send-email-laoar.shao@gmail.com> <20200616081628.GC9499@dhcp22.suse.cz> <20200616092727.GD9499@dhcp22.suse.cz> <20200616104806.GE9499@dhcp22.suse.cz> <20200618003427.GZ2040@dread.disaster.area> In-Reply-To: <20200618003427.GZ2040@dread.disaster.area> From: Yafang Shao Date: Thu, 18 Jun 2020 19:04:49 +0800 Message-ID: Subject: Re: [PATCH v3] xfs: avoid deadlock when trigger memory reclaim in ->writepages To: Dave Chinner Cc: Michal Hocko , Christoph Hellwig , "Darrick J. Wong" , Andrew Morton , linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, Linux MM Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 71429180C08FE 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 Thu, Jun 18, 2020 at 8:34 AM Dave Chinner wrote: > > On Tue, Jun 16, 2020 at 12:48:06PM +0200, Michal Hocko wrote: > > On Tue 16-06-20 17:39:33, Yafang Shao wrote: > > > The history is complicated, but it doesn't matter. > > > Let's turn back to the upstream kernel now. As I explained in the commit log, > > > xfs_vm_writepages > > > -> iomap_writepages. > > > -> write_cache_pages > > > -> lock_page <<<< This page is locked. > > > -> writepages ( which is iomap_do_writepage) > > > -> xfs_map_blocks > > > -> xfs_convert_blocks > > > -> xfs_bmapi_convert_delalloc > > > -> xfs_trans_alloc > > > -> kmem_zone_zalloc //It should alloc page > > > with GFP_NOFS > > > > > > If GFP_NOFS isn't set in xfs_trans_alloc(), the kmem_zone_zalloc() may > > > trigger the memory reclaim then it may wait on the page locked in > > > write_cache_pages() ... > > > > This cannot happen because the memory reclaim backs off on locked pages. > > ->writepages can hold a bio with multiple PageWriteback pages > already attached to it. Direct GFP_KERNEL page reclaim can wait on > them - if that happens the the bio will never be issued and so > reclaim will deadlock waiting for the writeback state to clear... > Thanks for the explanation. > > > That means the ->writepages should be set with GFP_NOFS to avoid this > > > recursive filesystem reclaim. > > Indeed. We already have parts of the IO submission path under > PF_MEMALLOC_NOFS so we can do transaction allocation, etc. See > xfs_prepare_ioend(), which is called from iomap via: > > iomap_submit_ioend() > ->prepare_ioend() > xfs_prepare_ioend() > > we can get there from: > > iomap_writepage() > iomap_do_writepage() > iomap_writepage_map() > iomap_submit_ioend() > iomap_submit_ioend() > > and: > > iomap_writepages() > write_cache_pages() > iomap_do_writepage() > iomap_writepage_map() > iomap_submit_ioend() > iomap_submit_ioend() > > Which says that we really should be putting both iomap_writepage() > and iomap_writepages() under PF_MEMALLOC_NOFS context so that > filesystem callouts don't have to repeatedly enter and exit > PF_MEMALLOC_NOFS context to avoid memory reclaim recursion... > Looks reasonable. I will update this patch to put both iomap_writepage() and iomap_writepages() under PF_MEMALLOC_NOFS context and try to remove the usage of PF_MEMALLOC_NOFS from the filesystem callouts. -- Thanks Yafang