From patchwork Tue Nov 15 22:45:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriel Krisman Bertazi X-Patchwork-Id: 20625 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2978267wru; Tue, 15 Nov 2022 14:49:53 -0800 (PST) X-Google-Smtp-Source: AA0mqf6RmGyO0GCq9wklAe8Nx9F+y3CHofN0lXNLN8vAns/ryCosMlW0zG69rHGNm1eGDiKsrcTz X-Received: by 2002:a17:907:6748:b0:7ae:bfeb:f484 with SMTP id qm8-20020a170907674800b007aebfebf484mr16262444ejc.174.1668552593459; Tue, 15 Nov 2022 14:49:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668552593; cv=none; d=google.com; s=arc-20160816; b=wtGbmOv+E9ydQLILrLyen5HJeWD1BTjtcdsl0nJqGBx73DoB4lkzFR8tWoT3ambSeC MwNnLShvIr9N5lvLexJzumj1d4hNCaR1JiO78yux3mD6j9L/d/F147ZxC4RL2ue3wWiY TJZjRdrIClbm0kvisDKlYDkbN1OnGMCTUS38yeQIZuZyqkXepTCmwvA3V9SvWSS9xo1A pYk+2TUAQ4yEBTYl4GxDfFg2gs5693NdglZfE2wY3DWE+EIhIeBmMTEWph+bfnUjhQDn p1AQpAa2pswxnv8uu9qH5TvUmk3oSaTC+DLhX+Wgwr8Krl59XtZTGhW3mKwZpqcoH8CH Mbug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=d3jRfb3u2UMhELX3RFNE2yKyqYVqcOMcurpZIDpUfqQ=; b=WHtfR4RHm3SwvCqfr2mDyksVbY+LhL/b9fpiaWvg3dTUAhX9TMIFVhuJYmVr+Fd4JY kJ6c1bo4c3XuTkk+1cc96j3DnRSXihwEFT2VgzsSAW3chL5cI5fC4A/RgKEVB4yAnnR1 AF/vU7Azv9RBAL0UA+r9/8kAYlobRQqhmrrgaxDwsUoWfc17aNOzl+4O1qtwxkCqJPHM V6imc9ZN2oZKFzbY14w0PEiQVakjeQQMFYSOBs1ehIwcDgwQKhknrjz0sr+YDK+6vO+1 T5C57654wFiVr636k/IR/nIbZ01LmE5GtKDwsedZbDNps8sK/zSah9pAO+e8yiJeZKAg 3ofg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=ICr7+Pbd; dkim=neutral (no key) header.i=@suse.de; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hs24-20020a1709073e9800b007ab1b8b719csi15007811ejc.0.2022.11.15.14.49.29; Tue, 15 Nov 2022 14:49:53 -0800 (PST) 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.de header.s=susede2_rsa header.b=ICr7+Pbd; dkim=neutral (no key) header.i=@suse.de; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232296AbiKOWq1 (ORCPT + 99 others); Tue, 15 Nov 2022 17:46:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231754AbiKOWqT (ORCPT ); Tue, 15 Nov 2022 17:46:19 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 147B61FCFE; Tue, 15 Nov 2022 14:46:15 -0800 (PST) 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-out2.suse.de (Postfix) with ESMTPS id 7A5CF20A90; Tue, 15 Nov 2022 22:46:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1668552374; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d3jRfb3u2UMhELX3RFNE2yKyqYVqcOMcurpZIDpUfqQ=; b=ICr7+PbdeP+cvI4LbyR8wehVSKXI9D+8UYj/5EI5AZlluzxdnsPEZEd1qGvXKzSRNwdWEr 3+iX//2sCDWNtPdX61Ol2e1nAgZu7aUFQtYsYgMWy5NTcfMyT56IdlHqUfIoPx/JnyVmvA SbBZmECalYSPPyWnXXAK1mCm2KXGpBs= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1668552374; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d3jRfb3u2UMhELX3RFNE2yKyqYVqcOMcurpZIDpUfqQ=; b=a5XL0CFX5ESUe67zQsjAkZjjyX5RwRLma+q2/quZ8SEzsSCciMKWsyEY+8PctohwjOaoRd XgjXOpMSoqafK9DQ== 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 D122213273; Tue, 15 Nov 2022 22:46:13 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id nvQtI7UWdGPzSwAAMHmgww (envelope-from ); Tue, 15 Nov 2022 22:46:13 +0000 From: Gabriel Krisman Bertazi To: axboe@kernel.dk Cc: mingo@redhat.com, peterz@infradead.org, jack@suse.cz, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, liusong@linux.alibaba.com, chaitanyak@nvidia.com, Gabriel Krisman Bertazi Subject: [PATCH 3/3] sbitmap: Try each queue to wake up at least one waiter Date: Tue, 15 Nov 2022 17:45:53 -0500 Message-Id: <20221115224553.23594-4-krisman@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20221115224553.23594-1-krisman@suse.de> References: <20221115224553.23594-1-krisman@suse.de> MIME-Version: 1.0 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749604204406055340?= X-GMAIL-MSGID: =?utf-8?q?1749604204406055340?= Jan reported the new algorithm as merged might be problematic if the queue being awaken becomes empty between the waitqueue_active inside sbq_wake_ptr check and the wake up. If that happens, wake_up_nr will not wake up any waiter and we loose too many wake ups. In order to guarantee progress, we need to wake up at least one waiter here, if there are any. This now requires trying to wake up from every queue. Instead of walking through all the queues with sbq_wake_ptr, this call moves the wake up inside that function. In a previous version of the patch, I found that updating wake_index several times when walking through queues had a measurable overhead. This ensures we only update it once, at the end. Fixes: 4f8126bb2308 ("sbitmap: Use single per-bitmap counting to wake up queued tags") Reported-by: Jan Kara Signed-off-by: Gabriel Krisman Bertazi Reviewed-by: Jan Kara --- lib/sbitmap.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/lib/sbitmap.c b/lib/sbitmap.c index bea7984f7987..586deb333237 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c @@ -560,12 +560,12 @@ void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq, } EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth); -static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq) +static void __sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr) { int i, wake_index; if (!atomic_read(&sbq->ws_active)) - return NULL; + return; wake_index = atomic_read(&sbq->wake_index); for (i = 0; i < SBQ_WAIT_QUEUES; i++) { @@ -579,20 +579,22 @@ static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq) */ wake_index = sbq_index_inc(wake_index); - if (waitqueue_active(&ws->wait)) { - if (wake_index != atomic_read(&sbq->wake_index)) - atomic_set(&sbq->wake_index, wake_index); - return ws; - } + /* + * It is sufficient to wake up at least one waiter to + * guarantee forward progress. + */ + if (waitqueue_active(&ws->wait) && + wake_up_nr(&ws->wait, nr)) + break; } - return NULL; + if (wake_index != atomic_read(&sbq->wake_index)) + atomic_set(&sbq->wake_index, wake_index); } void sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr) { unsigned int wake_batch = READ_ONCE(sbq->wake_batch); - struct sbq_wait_state *ws = NULL; unsigned int wakeups; if (!atomic_read(&sbq->ws_active)) @@ -604,16 +606,10 @@ void sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr) do { if (atomic_read(&sbq->completion_cnt) - wakeups < wake_batch) return; - - if (!ws) { - ws = sbq_wake_ptr(sbq); - if (!ws) - return; - } } while (!atomic_try_cmpxchg(&sbq->wakeup_cnt, &wakeups, wakeups + wake_batch)); - wake_up_nr(&ws->wait, wake_batch); + __sbitmap_queue_wake_up(sbq, wake_batch); } EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);