Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1072039rwb; Fri, 23 Sep 2022 07:54:31 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4fw+uSZv+ISWDWOYvCcxczFRLW8pBHGiuK9YWINxOBvtu3nFDWh6l3okWI47FNlIK8eSdy X-Received: by 2002:a05:6402:2073:b0:44e:3555:5fcd with SMTP id bd19-20020a056402207300b0044e35555fcdmr8536116edb.82.1663944871288; Fri, 23 Sep 2022 07:54:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663944871; cv=none; d=google.com; s=arc-20160816; b=Md9iPGyeWqA9iABW7+xNGIohXiGAkQJ6d/yYW7Q/EGjFxhGSDk+EQzd4sYmh8Kr0lD xzfGUqZNK4ZcX3gYzCN9D3MFUzuoCCehEXo3GUr/befCiN6eTGmjmBGVIws7ACidc4J0 EshMkxjj6rX5odG8mqKvNOhbLg5roitBNm0vV0DxRvPKbuUD4xWbKlVyL05RHVb9r3v5 wao3ytKoBGcq/woKDV4lmmeX+/miqQttPKGZEryhodptW0+3eyYEjABtrLONa0JmyBKK VIohs/rfstliP5b9s5DwIL563D9wnA54KBQqTRN+QpXpLUlNNabB1c+NviXt3q3alkWY 6pOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature :dkim-signature; bh=3eRIeqJ35bjBaxEIVHyjl5HzJmU8eUg6UuiOKVVDhfI=; b=pC5V/EBo8zHWr4AIXYYaqcDCleROPTrRr8a5xuZ4leM2GTFZgmDOHQkDUHBtlWNHWt piB85LwwuR44V8gskjNtOwOeC+J7TthD7zIcxMn8ckwh5rnXPXxSFLrJ5wabFNHQQdpE lrG4OpWMGpvw2UsTQhSD2cAXL+UxvB6Vcd/9ewkA4W90QeeYQFdscqDRJZo5qk2oDTgr HpD6P4ns7M2wUXUJZg1g8/cBr8BqFpG2ZRNbSocyUMkqqWBBiT6DuwXaMa1JwnQanhcw fxkMi77vSI/JZ1RsVsuBR8mEP8z+jmavx98eRqWmK/AiMCyyDn8d5Xlaxvk78fsKXxvD +uew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=pjBYBvVq; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z24-20020a1709063a1800b0073da5c8de1asi6802983eje.178.2022.09.23.07.54.05; Fri, 23 Sep 2022 07:54:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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.cz header.s=susede2_rsa header.b=pjBYBvVq; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229963AbiIWOnK (ORCPT + 99 others); Fri, 23 Sep 2022 10:43:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231856AbiIWOnH (ORCPT ); Fri, 23 Sep 2022 10:43:07 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53530143280; Fri, 23 Sep 2022 07:43:05 -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 B8233219F6; Fri, 23 Sep 2022 14:43:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1663944183; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=3eRIeqJ35bjBaxEIVHyjl5HzJmU8eUg6UuiOKVVDhfI=; b=pjBYBvVquyKzp9m/dCfmi5ulC/6lWsIX/9ZYV8ku+G07BvI2orRR6wcvN3rJlD54BThb0V TPnLhu0D9cr0KZZ7TjMunkwpvmTkrAd3cKB9kuPUg3qug7CbB5bm2HM7Q6qnBUH7CbzSbg BFibh786yP7q2BPSvJOSXGz4Yr8GuwY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1663944183; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=3eRIeqJ35bjBaxEIVHyjl5HzJmU8eUg6UuiOKVVDhfI=; b=BqksOAX4xO6kfjd1zqXmCr3Qb02d8iX6p3pHkpUHPMJQR84+hdETfPOPNtr+82WUJV5deM rHwtczfMmOIi3nBQ== 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 A903E13A00; Fri, 23 Sep 2022 14:43:03 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id TI87KffFLWPOKwAAMHmgww (envelope-from ); Fri, 23 Sep 2022 14:43:03 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 3415CA0685; Fri, 23 Sep 2022 16:43:03 +0200 (CEST) Date: Fri, 23 Sep 2022 16:43:03 +0200 From: Jan Kara To: Hugh Dickins Cc: Keith Busch , Jens Axboe , Yu Kuai , Jan Kara , Liu Song , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH next] sbitmap: fix lockup while swapping Message-ID: <20220923144303.fywkmgnkg6eken4x@quack3> References: <20220921164012.s7lvklp2qk6occcg@quack3> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220921164012.s7lvklp2qk6occcg@quack3> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS 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-kernel@vger.kernel.org On Wed 21-09-22 18:40:12, Jan Kara wrote: > On Mon 19-09-22 16:01:39, Hugh Dickins wrote: > > On Mon, 19 Sep 2022, Keith Busch wrote: > > > On Sun, Sep 18, 2022 at 02:10:51PM -0700, Hugh Dickins wrote: > > > > I have almost no grasp of all the possible sbitmap races, and their > > > > consequences: but using the same !waitqueue_active() check as used > > > > elsewhere, fixes the lockup and shows no adverse consequence for me. > > > > > > > > > > Fixes: 4acb83417cad ("sbitmap: fix batched wait_cnt accounting") > > > > Signed-off-by: Hugh Dickins > > > > --- > > > > > > > > lib/sbitmap.c | 2 +- > > > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > > > > > --- a/lib/sbitmap.c > > > > +++ b/lib/sbitmap.c > > > > @@ -620,7 +620,7 @@ static bool __sbq_wake_up(struct sbitmap > > > > * function again to wakeup a new batch on a different 'ws'. > > > > */ > > > > if (cur == 0) > > > > - return true; > > > > + return !waitqueue_active(&ws->wait); > > > > > > If it's 0, that is supposed to mean another thread is about to make it not zero > > > as well as increment the wakestate index. That should be happening after patch > > > 48c033314f37 was included, at least. > > > > I believe that the thread about to make wait_cnt not zero (and increment the > > wakestate index) is precisely this interrupted thread: the backtrace shows > > that it had just done its wakeups, so has not yet reached making wait_cnt > > not zero; and I suppose that either its wakeups did not empty the waitqueue > > completely, or another waiter got added as soon as it dropped the spinlock. I was trying to wrap my head around this but I am failing to see how we could have wait_cnt == 0 for long enough to cause any kind of stall let alone a lockup in sbitmap_queue_wake_up() as you describe. I can understand we have: CPU1 CPU2 sbitmap_queue_wake_up() ws = sbq_wake_ptr(sbq); cur = atomic_read(&ws->wait_cnt); do { ... wait_cnt = cur - sub; /* this will be 0 */ } while (!atomic_try_cmpxchg(&ws->wait_cnt, &cur, wait_cnt)); ... /* Gets the same waitqueue */ ws = sbq_wake_ptr(sbq); cur = atomic_read(&ws->wait_cnt); do { if (cur == 0) return true; /* loop */ wake_up_nr(&ws->wait, wake_batch); smp_mb__before_atomic(); sbq_index_atomic_inc(&sbq->wake_index); atomic_set(&ws->wait_cnt, wake_batch); /* This stops looping on CPU2 */ So until CPU1 reaches the atomic_set(), CPU2 can be looping. But how come this takes so long that is causes a hang as you describe? Hum... So either CPU1 takes really long to get to atomic_set(): - can CPU1 get preempted? Likely not at least in the context you show in your message - can CPU1 spend so long in wake_up_nr()? Maybe the waitqueue lock is contended but still... or CPU2 somehow sees cur==0 for longer than it should. The whole sequence executed in a loop on CPU2 does not contain anything that would force CPU2 to refresh its cache and get new ws->wait_cnt value so we are at the mercy of CPU cache coherency mechanisms to stage the write on CPU1 and propagate it to other CPUs. But still I would not expect that to take significantly long. Any other ideas? Honza -- Jan Kara SUSE Labs, CR