Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2303157ybz; Thu, 30 Apr 2020 14:44:43 -0700 (PDT) X-Google-Smtp-Source: APiQypIS2d8FhjQCwdrsRXn/VHQwsahzm7WO69TJHi0Qv5zZ94GVZilLuNXDacEpTL2kgzCanayl X-Received: by 2002:aa7:d4c1:: with SMTP id t1mr1027206edr.175.1588283082962; Thu, 30 Apr 2020 14:44:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588283082; cv=none; d=google.com; s=arc-20160816; b=k6YHn0QILchptPA4hrPmA5zTc4qHEfDitQrwzkELDHV5Ib1DYFbAmeA+YbPSHEvvjK D8gTlIFpTjarjC59i+eYCIlibX8PBKnpShi0EFx4870/oydWVGPSl2opfp9rV/0diiLq mnO93Il7bANc52/xLTp/F5OOEUOXTW03N4AhZ3xptki19m8SLaic5yYoOx7m2UF7aC4l OMbF9Jb0ry6LjCKOq3xhB6Vc91ZhvS44FRcBGn57Us/kpQs3s+e/Uas0P0mic+IihIWy 4ExguEVpp4zv2G03i105NG6LZIHKw0mT2w+Qk3DuGGzWNmtCGuL4uUqxZU+CYXii4Twp /nLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=i2ZqTGgRzrCsk858BimFAxgKXh4OreLeCR1nn/jo56s=; b=JSo9kiUxhuEg/kC/1jGEIp9gH1Au1eShfqDMszMxshkX25O1Nvnj8vnjf0aPPRPybU hvGPV5OWwzQUsZFq+9teeJu11QDHBi17qTCq9+7SCGGawHkLR8c/howTCAAl9EtBaIrv sS3o6+OqRISA6X/9ctlCPGkazwYfpsfADpGgb9FBq0MuHG53oWDjRWDkZOUEYfxzY016 S0mBJBg/YlHnuBa5lxr2W907ZJbmoHuclxkJsOmJOC9PhdDHc215TB1xbOsayZLNWq6n n22P9RP6+ij3e52RxJUnorbCSwYscNQs/KRz6hVZLVBUH/jak/QHTGdSkg0ZmSz7cvy9 TDEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=bC8IorIv; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s3si517188edq.467.2020.04.30.14.44.18; Thu, 30 Apr 2020 14:44:42 -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=bC8IorIv; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727058AbgD3VkU (ORCPT + 99 others); Thu, 30 Apr 2020 17:40:20 -0400 Received: from mail.kernel.org ([198.145.29.99]:48196 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726447AbgD3VkU (ORCPT ); Thu, 30 Apr 2020 17:40:20 -0400 Received: from localhost.localdomain (c-73-231-172-41.hsd1.ca.comcast.net [73.231.172.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 872792064C; Thu, 30 Apr 2020 21:40:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588282819; bh=mJs5hluqduo9dtI8ms49qYStG1gIgUgTJaDMGc0TwsY=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=bC8IorIvBOl0MDL52/kEKkObD8oM2TuQj0h4qMWCgw91RtBB3hNmYby7KsliyZY/M zZl0imt5lRxodssgDt7UYZkzSoJBk7pK5roliimn5FjgWANZtHDXpfsNqtHfYlO33c JCGyBuuntmyuEFjLJdo7KY8mjVnUhjBCyuK81QjY= Date: Thu, 30 Apr 2020 14:40:18 -0700 From: Andrew Morton To: fdmanana@kernel.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, dennis@kernel.org, tj@kernel.org, cl@linux.com, linux-btrfs@vger.kernel.org, Filipe Manana Subject: Re: [PATCH] percpu: make pcpu_alloc() aware of current gfp context Message-Id: <20200430144018.c855f031b321d68e5c89b30c@linux-foundation.org> In-Reply-To: <20200430164356.15543-1-fdmanana@kernel.org> References: <20200430164356.15543-1-fdmanana@kernel.org> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. But why did btrfs use memalloc_nofs_save()/restore() rather than s/GFP_KERNEL/GFP_NOFS/?