Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp25589ybz; Thu, 30 Apr 2020 15:45:56 -0700 (PDT) X-Google-Smtp-Source: APiQypInLhakF7yLD1ab20V659OfVctwg8C9Q4FlsMZfO7VTv5lQOyeYEL8XMoLylEHZlZKh+4RH X-Received: by 2002:a17:906:2410:: with SMTP id z16mr701251eja.1.1588286756698; Thu, 30 Apr 2020 15:45:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588286756; cv=none; d=google.com; s=arc-20160816; b=XZEaaOv+h/htfSg+Ys0I+1+JbXX4zDHOS/XdWngq6FwfHLoOAMj3GC0mssYpsc/UT6 Nhu6Uk+XbROaNh0MXxlziHk147Tc+i3Vj0ClozdnnhoUVvdXlWiyTV3sgI3bSTWXF0sv PVBh0EkPSiQSQuhluEmOhjsDqGusy2372lnqA9Hy7/Xf6vMLcBlR4UYbrX4GLOjVFb+V xltCDy0Ka3rn5Ouwq+JKIlAjyPS9yDeurwuZ/IdUMhuBhAmnFmYdoPM4H6XYDWoLaDqS x/OhyIxe1pOx/ZwYQL/D/4edMa+HKmeh2t6hJQRmDkN5CU4J+VJBtAYkQjl9n+o7OF1L vQew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=XgFgmaM6aJ2EzpsEiht/rK5LZjyCLvjfxQ3B0VuUKy0=; b=0WNvJmWdVWQkF0XXNEGSTs91vAJte9mC2WnkmbWWsorMYPKVsGA+P3+mTdg07MC7w4 BB2qd4f7ODQ06VOI/SMxGa+C3ECfPRxX9N2067BfzoBq2XAF9h7NjkE3f0KwwcU/K4LE dPYn/QI1ZRwV3QQnl1dJIaLK26koABtFKkFdUVtnm/nqITV/LGXNgA7r+BMwSOQ7Wvkr ALmUpv5q9nWOEZyTklpEmsi076ni5BdD0sc1/pTRwUngqnsLKSVy13qgNxZ00KTVqyqE 9DQkFIqdCXIOqpsKUlXd33wKcDKfVu3ZDmgt6SBNKuuGN32WVHxh5FdXZeGc2jTEDmw6 K4+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=eixKTQab; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dd8si698387ejb.486.2020.04.30.15.45.31; Thu, 30 Apr 2020 15:45:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=eixKTQab; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727778AbgD3Wnd (ORCPT + 99 others); Thu, 30 Apr 2020 18:43:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:37632 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726697AbgD3Wnd (ORCPT ); Thu, 30 Apr 2020 18:43:33 -0400 Received: from mail-vs1-f52.google.com (mail-vs1-f52.google.com [209.85.217.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B6243207DD; Thu, 30 Apr 2020 22:43:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588286612; bh=4opDyevPEQA05BIebhqthwKvc88Qikpbmp5ElQzkcvA=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=eixKTQabEP6pogD65jtw5/ajbPgaParFaH6H9RUuJW4MSrqfI1f03tMssC6lQDtSj ZknH5IO8MTGkV+M1BoMH2lU40rPTlli+9oBLoWTDtKn6dL32+M+agtd8KoCTi28Zlm amLq2YYIiqqr7FzewHA7V+5j9oHWhUKSdp0YrvjY= Received: by mail-vs1-f52.google.com with SMTP id m24so5292692vsq.10; Thu, 30 Apr 2020 15:43:32 -0700 (PDT) X-Gm-Message-State: AGi0PuarVnSNi9YBHZC11YLBDwxWjcyamY4mDQ2jeGfqbht4GTAQbgLz VV0dHij8vB8gzGN6BvTUZ4S1s7tGk3mDzt2poPA= X-Received: by 2002:a67:407:: with SMTP id 7mr1023429vse.95.1588286611719; Thu, 30 Apr 2020 15:43:31 -0700 (PDT) MIME-Version: 1.0 References: <20200430164356.15543-1-fdmanana@kernel.org> <20200430144018.c855f031b321d68e5c89b30c@linux-foundation.org> <20200430222347.GA164259@google.com> In-Reply-To: <20200430222347.GA164259@google.com> From: Filipe Manana Date: Thu, 30 Apr 2020 23:43:20 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] percpu: make pcpu_alloc() aware of current gfp context To: Dennis Zhou Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Tejun Heo , cl@linux.com, linux-btrfs , Filipe Manana Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 30, 2020 at 11:23 PM Dennis Zhou wrote: > > On Thu, Apr 30, 2020 at 02:40:18PM -0700, Andrew Morton wrote: > > On Thu, 30 Apr 2020 17:43:56 +0100 fdmanana@kernel.org wrote: > > > > > From: Filipe Manana > > > > > > Since 5.7-rc1, on btrfs we have a percpu counter initialization for which > > > we always pass a GFP_KERNEL gfp_t argument (this happens since commit > > > 2992df73268f78 ("btrfs: Implement DREW lock")). That is safe in some > > > contextes but not on others where allowing fs reclaim could lead to a > > > deadlock because we are either holding some btrfs lock needed for a > > > transaction commit or holding a btrfs transaction handle open. Because > > > of that we surround the call to the function that initializes the percpu > > > counter with a NOFS context using memalloc_nofs_save() (this is done at > > > btrfs_init_fs_root()). > > > > > > However it turns out that this is not enough to prevent a possible > > > deadlock because percpu_alloc() determines if it is in an atomic context > > > by looking exclusively at the gfp flags passed to it (GFP_KERNEL in this > > > case) and it is not aware that a NOFS context is set. Because it thinks > > > it is in a non atomic context it locks the pcpu_alloc_mutex, which can > > > result in a btrfs deadlock when pcpu_balance_workfn() is running, has > > > acquired that mutex and is waiting for reclaim, while the btrfs task that > > > called percpu_counter_init() (and therefore percpu_alloc()) is holding > > > either the btrfs commit_root semaphore or a transaction handle (done at > > > fs/btrfs/backref.c:iterate_extent_inodes()), which prevents reclaim from > > > finishing as an attempt to commit the current btrfs transaction will > > > deadlock. > > > > > > > Patch looks good and seems sensible, thanks. > > > > Acked-by: Dennis Zhou > > > But why did btrfs use memalloc_nofs_save()/restore() rather than > > s/GFP_KERNEL/GFP_NOFS/? > > I would also like to know. For 2 reasons: 1) It's the preferred way to do it since memalloc_nofs_save()/restore() was added (according to Documentation/core-api/gfp_mask-from-fs-io.rst); 2) According to Documentation/core-api/gfp_mask-from-fs-io.rst, passing GFP_NOFS to __vmalloc() doesn't work, so one has to use the memalloc_nofs_save()/restore() API for that. And pcpu_alloc() calls helpers that end up calling __vmalloc() (through pcpu_mem_zalloc()). And that's it. Thanks. > > Thanks, > Dennis