Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp90984ybz; Thu, 30 Apr 2020 17:06:15 -0700 (PDT) X-Google-Smtp-Source: APiQypJJXoqY33gVvlxdAAglzTuoTfwcw1db1QoVpNNOebxSPkex2/KeIlq1B0EBmfRSx8D9tpmO X-Received: by 2002:aa7:c606:: with SMTP id h6mr1426472edq.265.1588291575062; Thu, 30 Apr 2020 17:06:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588291575; cv=none; d=google.com; s=arc-20160816; b=RqlyBVn81/CzZcZEykFnMlUTdzUB2wbLXFddwrovfgplLl+RXvaz4prmfyhX+eciBq p7t0EB2dsD/GP5075wkq50HDlXHdToJIEY2xYdcMVsWs8ctCnjHDHcVOeYGl/A6CvRUL AogHQqzfx3sho9mxyqATG7UHxVkEUUsyXzEsFfLUtWxTdnE0aav/cI6M0F4/Y3td1Sjm C6bgE1CqkoMR8Hqhg3Abp2WPEhRm4TA+nSKfXfIx4tgU1feaPKwvaS8RX9n8sBEELc44 8bOrYALm787M0NEiC2B732QSKDHGYJmENvBCiBir4irynp9rJqVJWKXqkK8cIQrBsk8N nd5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=89Z7r8iD/0NKOVoXCJcX8FvWsMx9t7cw7PfWtZ+Xwlg=; b=xu+5bVHlEP0pmcddNuwmZ9i0dI/MDVzjHR9pMvC5leIYrFWUzC7cxwx7thzATcA+T3 FPV1ESQ4YTbqCGBvFik8oZPPeZj41qnCm8f3Jb9oOoyg7lIhxzqzMSNuAk9omHTnbUPq CM9ZabPG8uElvBPDhHEww0a4/Wvga6lfUWwWfD86ns/WQjz76E4GhWnRnUaTc0zO4WP2 7vbYapXoFx+NYRFRNbyLQHEuPxFpbUaSuu81Qyp/myCiX4B1jyFbGoPRh6F/ww4pa4iI 7ebQ+yaEXRCtkDdi/r7M+wsrJgkqyE6OAWq3uqzkHYZ/9PwS+mDavK0ZC5VdFmqsiS25 WJyg== ARC-Authentication-Results: i=1; mx.google.com; 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 y10si757905edt.393.2020.04.30.17.05.48; Thu, 30 Apr 2020 17:06:15 -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; 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 S1727908AbgEAADU (ORCPT + 99 others); Thu, 30 Apr 2020 20:03:20 -0400 Received: from mail-ua1-f65.google.com ([209.85.222.65]:32952 "EHLO mail-ua1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726384AbgEAADT (ORCPT ); Thu, 30 Apr 2020 20:03:19 -0400 Received: by mail-ua1-f65.google.com with SMTP id g35so3254216uad.0; Thu, 30 Apr 2020 17:03:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=89Z7r8iD/0NKOVoXCJcX8FvWsMx9t7cw7PfWtZ+Xwlg=; b=elsTxTUcbmnckuI9DwHhcFGcxxESdvwHqtL3EjbXImzmWNN1EmIsxuVOw1HetUrY/l 4J7m6mgCvRrBtdu+K304ZTvUInuBviffHlzFDBgqUAnQUXUZCf+PqqFNZQ9wMblOZSgl 1YJRfHYyVmUyBRK0qvgKFdKQx51cvRZzIImYGToBuykYd+vKsR89xh8xNxqfq6+rBLjB 3iFQWCs/7+/tQRYU8sV4hCyv9Gzu3uw/fygJ9eoQ2IKxX8GPfqDmAoULD5SosUr9vDiS lBgZcj8YXZ+c2dFJPX1bF66rSuXF+fbkG12PVe1JbjvB8dG0Zuvj0/60Afmk8xTHTSdp 5PGg== X-Gm-Message-State: AGi0Puagl/xUhijBN98xnmQe4Vbw2c4VfU8OA+e0L4EGaymYtmE2+baG S9aoSsu7p8nxBP2l59IXuMo= X-Received: by 2002:ab0:7025:: with SMTP id u5mr1045186ual.130.1588291398085; Thu, 30 Apr 2020 17:03:18 -0700 (PDT) Received: from google.com (69.104.231.35.bc.googleusercontent.com. [35.231.104.69]) by smtp.gmail.com with ESMTPSA id k184sm379805vke.42.2020.04.30.17.03.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Apr 2020 17:03:13 -0700 (PDT) Date: Fri, 1 May 2020 00:03:12 +0000 From: Dennis Zhou To: Filipe Manana Cc: Dennis Zhou , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Tejun Heo , cl@linux.com, linux-btrfs , Filipe Manana Subject: Re: [PATCH] percpu: make pcpu_alloc() aware of current gfp context Message-ID: <20200501000312.GA188766@google.com> References: <20200430164356.15543-1-fdmanana@kernel.org> <20200430144018.c855f031b321d68e5c89b30c@linux-foundation.org> <20200430222347.GA164259@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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:43:20PM +0100, Filipe Manana wrote: > 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); > Thanks. I didn't realize it completely superceded GFP_NOFS. > 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. > I'm starting to remember a bit more. I guess it's not great how percpu manages GFP_ATOMIC as !GFP_KERNEL for the possible vmalloc() calls. At the time I believe the whitelist was the only way to deal with the recursive case. If I get a chance I'll look at the flags again and see if we can't do something better/ more aligned today. > > > > > Thanks, > > Dennis