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.8 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,SIGNED_OFF_BY,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 3F0F8C47247 for ; Thu, 30 Apr 2020 16:54:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id F27CF20731 for ; Thu, 30 Apr 2020 16:54:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="O/5SFX3P" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F27CF20731 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 889028E0005; Thu, 30 Apr 2020 12:54:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8398E8E0001; Thu, 30 Apr 2020 12:54:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 750AD8E0005; Thu, 30 Apr 2020 12:54:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0155.hostedemail.com [216.40.44.155]) by kanga.kvack.org (Postfix) with ESMTP id 597D38E0001 for ; Thu, 30 Apr 2020 12:54:41 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 23792180AD811 for ; Thu, 30 Apr 2020 16:54:41 +0000 (UTC) X-FDA: 76765120362.05.shirt91_631072fb2d01e X-HE-Tag: shirt91_631072fb2d01e X-Filterd-Recvd-Size: 5108 Received: from mail-qt1-f194.google.com (mail-qt1-f194.google.com [209.85.160.194]) by imf47.hostedemail.com (Postfix) with ESMTP for ; Thu, 30 Apr 2020 16:54:40 +0000 (UTC) Received: by mail-qt1-f194.google.com with SMTP id e17so5560721qtp.7 for ; Thu, 30 Apr 2020 09:54:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=u0otRQ4HRB+YKeilmTal/RUC4w+MjwFliS0R169CA7s=; b=O/5SFX3PPa5GnoITINqhhqhgSQx66i9Vr9eYEtt/dHcuqLd/AOHvW3QLuX+7DeqG31 GslwjmCdLPuQneMgsHDTtJ/bnlSZ25zwDS0MsgunToxCICZR7/uk8UTaNwv6JpUmQ805 YgJR0Nl0vne392X5jSeHJa6URY2mqfdOcK1I69vww560N54VbCYjt71K8PrUM7f0Chxa YWL4h4KBf+zi8iWhtQoWQUc2/C1a8+HpHg9fYZAkMfYGpIVzg/iVmbJZ4gAG/DgWUISS qEWHbunlq8x7yS0U1Io7sm1VB9nPh3mjwbzhr0uIbiaCo3pmRIOJ/0opojQU34prGTh+ q7UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to; bh=u0otRQ4HRB+YKeilmTal/RUC4w+MjwFliS0R169CA7s=; b=cIUtflqyUbeRlRF+nG4HAIbrqdQlhED7iLLEJHOksVUaY9f7UsNSK3by3xOcJOV3ql 5eNSr57h0D39fH913b2esVnRpydNI9YjssjZ5KfuBKuUb2h0BX0JtCOvMplLAIcSN3cB Hk2WGpwl0HARfnTws0ag4ZBSTX9tkka/OK9XDrUhOhxFAT8pyc1OwVVtPKJ939oZLpuo sShbw31pGwEXqz0VXnC9wPXWQs6PPwe6YFQ6YSHz8MYzfe2a3S7/QdSJveK4qdJ2/9fq moEXebTBqjhG69MFm2tvJz3wQaHlhRuMsOMrsXdzHvna0btnE42NRVwI5Ka3+iIQCR4n 7nhQ== X-Gm-Message-State: AGi0PuboClVFxDlxAGRBz5fxrOpqt/NZKXCyyBlQocyqHKtkOaLnqqJW fF5Vm/7DgXu4C490HmJmTsk= X-Google-Smtp-Source: APiQypJQTeHAEnS3J65bj3d5lEQgh6haVB0NClBsAbTwosD6YRCd9HZoVfbdmACo+8rNsGkAqoy3Zw== X-Received: by 2002:ac8:fee:: with SMTP id f43mr4727473qtk.376.1588265679714; Thu, 30 Apr 2020 09:54:39 -0700 (PDT) Received: from localhost ([2620:10d:c091:480::1:f989]) by smtp.gmail.com with ESMTPSA id i6sm414723qkk.123.2020.04.30.09.54.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Apr 2020 09:54:39 -0700 (PDT) Date: Thu, 30 Apr 2020 12:54:37 -0400 From: Tejun Heo To: fdmanana@kernel.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, dennis@kernel.org, cl@linux.com, akpm@linux-foundation.org, linux-btrfs@vger.kernel.org, Filipe Manana Subject: Re: [PATCH] percpu: make pcpu_alloc() aware of current gfp context Message-ID: <20200430165437.GF5462@mtj.thefacebook.com> References: <20200430164356.15543-1-fdmanana@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200430164356.15543-1-fdmanana@kernel.org> 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, Apr 30, 2020 at 05:43:56PM +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. ... > This could be fixed by making btrfs pass GFP_NOFS instead of GFP_KERNEL to > percpu_counter_init() in contextes where it is not reclaim safe, however > that type of approach is discouraged since memalloc_[nofs|noio]_save() > were introduced. Therefore this change makes pcpu_alloc() look up into > an existing nofs/noio context before deciding whether it is in an atomic > context or not. > > Signed-off-by: Filipe Manana Acked-by: Tejun Heo Thanks. -- tejun