Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp2391763imw; Wed, 6 Jul 2022 05:17:29 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sK9kGGQCs8CWHF2DRWA0YeAMNx5iIrYmzvnlx5cnRuyhYLY7ORfVmgXFX8h/rahLhPo8pq X-Received: by 2002:a17:907:9808:b0:72a:f35f:7cdd with SMTP id ji8-20020a170907980800b0072af35f7cddmr4635449ejc.36.1657109849536; Wed, 06 Jul 2022 05:17:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657109849; cv=none; d=google.com; s=arc-20160816; b=wn44HCiXQOt/1wyWYOq/B1pdDDYbfBtBifQ7IGCa/YUIUCFfp27zE65cKXm3uiQdRp 3sFfXuGkPF5Jeqbv2KXviZEvcDWdBkAqo2ncQTTwE6xoLpE0/pWxvcwGOBZLwApTjVN/ 3ieoWSY+EY4cH/Vvn2qi9sUsJQ5kVkI2a9KV05Sqd++EBcqHzl6HLnrZpDF1tpbN1axD zEu1Yrb78aVlo9UQNK6wa/LDE9R8Q37ICIKSlP4qj/KFHvJak+0DfmQEZySjK9uwnq5y h5LhB3OEeOpJUgu2+VncBbxcRQpz5XgvksLhvadCeB5bKKELCh0mGGSME8MUb2PR8WbJ MyuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=bNjs4CeN6qcocdZIdOCebG/R8nWdledPMHEDqWNAPVI=; b=DUjM3qBy3e7NrrbW1IbTfrUE0bnz/TkDkANiCVO/Sxtq05Mban5DFdGWH+hSAasCFe 8AQ2UYDSxJrVPbjjKzNPzC/3fl3qwmhpapX6V455GUx+POnwNu5Yoww4aQrJzBm1UH2x S7wOYYJVlp0Tn/GY0AlnGHjHOoHsCz48gIP5tyaiwu+1Fg6WtjM5gArVOVwk2rKYP9Ey NM613pRto9jJr6WvdtJrJER+54sck4gthIt/6LskLq8S8ohJQVwoNlCTqqMy7a/hpAyR o0NkX/6k2vdptyVuubT6393bdVGSQ1lSXm94dDFRuuN8xJS/pQUYIUOkpMaUSXA+qKoA 2G+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=F1zVAERc; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ga6-20020a1709070c0600b006fea036b38esi50947173ejc.310.2022.07.06.05.16.55; Wed, 06 Jul 2022 05:17:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=F1zVAERc; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233481AbiGFMN6 (ORCPT + 99 others); Wed, 6 Jul 2022 08:13:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233580AbiGFMNn (ORCPT ); Wed, 6 Jul 2022 08:13:43 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E2A623178; Wed, 6 Jul 2022 05:13:34 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id D59372265B; Wed, 6 Jul 2022 12:13:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1657109612; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bNjs4CeN6qcocdZIdOCebG/R8nWdledPMHEDqWNAPVI=; b=F1zVAERcuMACTj0R5SC+srzcBseoxqdEzh7Uva2L86BFrWfwiaCJmdCsqL37x4fOtNjPrw nlc0bUZa+j7e/MjK9UOPPT1YCchitEs16A3TVmyTt1lPrgkyqk/Zp+pQbkQGBuDTcfjvut zX74nekaNvFi9/jcb5c1vmNdHOJrExM= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 72C85134CF; Wed, 6 Jul 2022 12:13:32 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 1NPhGGx8xWLnCQAAMHmgww (envelope-from ); Wed, 06 Jul 2022 12:13:32 +0000 Message-ID: Date: Wed, 6 Jul 2022 15:13:31 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: bug in btrfs during low memory testing. Content-Language: en-US To: Matthew Wilcox Cc: Filipe Manana , dai.ngo@oracle.com, linux-btrfs , gniebler@suse.com, "linux-nfs@vger.kernel.org" References: <148c0ac2-add4-69e8-ced7-49772841720b@suse.com> From: Nikolay Borisov In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org On 6.07.22 г. 15:09 ч., Matthew Wilcox wrote: > On Wed, Jul 06, 2022 at 09:36:42AM +0300, Nikolay Borisov wrote: >> On 5.07.22 г. 23:29 ч., Matthew Wilcox wrote: >>> On Tue, Jul 05, 2022 at 09:26:47PM +0100, Filipe Manana wrote: >>>> In this case we can actually call xa_insert() without holding that >>>> spinlock, it's safe against other concurrent calls to >>>> btrfs_get_or_create_delayed_node(), btrfs_get_delayed_node(), >>>> btrfs_kill_delayed_inode_items(), etc. >>>> >>>> However, looking at xa_insert() we have: >>>> >>>> xa_lock(xa); >>>> err = __xa_insert(xa, index, entry, gfp); >>>> xa_unlock(xa); >>>> >>>> And xa_lock() is defined as: >>>> >>>> #define xa_lock(xa) spin_lock(&(xa)->xa_lock) >>>> >>>> So we'll always be under a spinlock even if we change btrfs to not >>>> take the root->inode_lock spinlock. >>>> >>>> This seems more like a general problem outside btrfs' control. >>>> So CC'ing Willy to double check. >>> >>> No, the XArray knows about its own spinlock. It'll drop it if it needs >>> to allocate memory and the GFP flags indicate that the caller can sleep. >>> It doesn't know about your spinlock, so it can't do the same thing for >>> you ;-) >> >> >> In order to catch (and prevent) further offensive can we perhaps have >> something like that in xa_insert: >> >> >> diff --git a/include/linux/xarray.h b/include/linux/xarray.h >> index c29e11b2c073..63c00b2945a2 100644 >> --- a/include/linux/xarray.h >> +++ b/include/linux/xarray.h >> @@ -770,6 +770,9 @@ static inline int __must_check xa_insert(struct xarray >> *xa, >> { >> int err; >> >> + if (gfpflags_allow_blocking(gfp)) >> + might_sleep() >> + >> xa_lock(xa); >> err = __xa_insert(xa, index, entry, gfp); >> xa_unlock(xa); > > I think you mean: > > might_alloc(gfp); > > And yes, I think that makes a lot of sense. Quite a few similar places > to do ... I'll take care of it. Actually I had in mind more along the lines of: might_sleep_if(gfpflags_allow_blocking(gfp_mask)); but this is essentially what might_alloc does + lockdep annotations, so yeah, that would work. What I'd like to achieve with such a modification is for new users of xa array to instantly get a splat when xa_insert is executed irrespective of whether the allocation happens or not so that they have a chance to fix their code sooner rather than later.