Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp857046rwb; Thu, 10 Nov 2022 08:12:16 -0800 (PST) X-Google-Smtp-Source: AMsMyM4uw0Nq16JNHCfAWNUvDnYgpZ2BX9HpWfNxIu03DQFJzfCAdw9byED7XxLjiva1CYuEC6Qk X-Received: by 2002:a05:6402:f1e:b0:461:cfd3:48c2 with SMTP id i30-20020a0564020f1e00b00461cfd348c2mr63184004eda.294.1668096735757; Thu, 10 Nov 2022 08:12:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668096735; cv=none; d=google.com; s=arc-20160816; b=mL6gehsRXpJajDK5BXJBr6JmWiSj7uzGldEtfIPej9Wo6bOv0NyRyaNwpdqEXDr4zE 1v+ruThwb3jaFD2HfYHp3HBCeNT1C3y+FTjIxxGpyj1THaa3EjnkVXK5fITzi/dt4AF+ NP0SF1EQDej7jJs6MY2teLooeS9osPmo1IKm8NEMxWidnaHiIERLEbzy7TEwH/8gntcC S+1EWXcCUnwMAaX+kdQFjdxw70M4CJ/h1lxcD2PY/1HAf0WFEr/zWmvU+iqtL58eYxDO UsQK1omlXj1Fk66X+O5jyyxibUgESkhctbUtmFJAspjPKpxNShOIoOJp964sN0vFqtIA 4VHw== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:dkim-signature; bh=hWtIaU1eQyQNo9BJ4fdEyNYDXwH3zBNP0e+QOrpG3gA=; b=nckL7o0n8SkURUo5GFOv7b23xGas4kZpIU0kOq6CWH6iQRJnb2rQ6NQPgmPwaCXb/O ytfODk97YKk3Q1vCEpxr+ptyTMC0rCAWNpZfquGw9thQ4Q8TjRqafUY8NYLnVJzR72pW T1Y+qoFJvhwlI/+CPU/U7+RoiufQ1msrlaheNLBB9Yyzu1Qiar1Qaw42IWMv3uTPcUrz 7yMW03BIq12tC7xLcaBKnBvzPDLZsz4rK9zzOlgvluXW/B7PUJbo6T6DtuCTo/2gVHam 5cWzbdhGUJCDB6u1RYNxnbMUjcTsSJJWx/pt1bLU1eVF6EfdeY8/U6P6uKf5jFCYssBx 6jRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=j5meRBsG; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=RpAFpanb; 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 gt16-20020a1709072d9000b0078e1d1d6005si20118810ejc.23.2022.11.10.08.11.33; Thu, 10 Nov 2022 08:12:15 -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.cz header.s=susede2_rsa header.b=j5meRBsG; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=RpAFpanb; 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 S231255AbiKJPfj (ORCPT + 92 others); Thu, 10 Nov 2022 10:35:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230470AbiKJPfh (ORCPT ); Thu, 10 Nov 2022 10:35:37 -0500 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8FF4A1F6; Thu, 10 Nov 2022 07:35:36 -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-out1.suse.de (Postfix) with ESMTPS id 3A33021D3A; Thu, 10 Nov 2022 15:35:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1668094535; 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=hWtIaU1eQyQNo9BJ4fdEyNYDXwH3zBNP0e+QOrpG3gA=; b=j5meRBsGYfQc4z6YLiUuRzMOOd9NW+vai8VbzCrcd3PLQbd6Cdu9XfDmqHsiVJZ7HXzk8F S6g2yfrjQZ2NqE5VNHPKBxAPOOagU5zLpGQhWGDDqSQUmO4RWRVdDbBRA1dlndAeUXJkIF q0hRTW1KUphy7+E0VNm372dQi3lOkx8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1668094535; 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=hWtIaU1eQyQNo9BJ4fdEyNYDXwH3zBNP0e+QOrpG3gA=; b=RpAFpanbGxEqNzB1PuRmRoGhOz3MFp0TRzw491Ditpc6EsOdn5lTJf/owHe1veOeka0IW5 p2diGWN48n8g0rCg== 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 0B88813B58; Thu, 10 Nov 2022 15:35:35 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id HprGAkcabWO4SwAAMHmgww (envelope-from ); Thu, 10 Nov 2022 15:35:35 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 71B97A0704; Thu, 10 Nov 2022 16:35:33 +0100 (CET) Date: Thu, 10 Nov 2022 16:35:33 +0100 From: Jan Kara To: Yu Kuai Cc: Jan Kara , Gabriel Krisman Bertazi , axboe@kernel.dk, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Hugh Dickins , Keith Busch , Liu Song , "yukuai (C)" Subject: Re: [PATCH] sbitmap: Use single per-bitmap counting to wake up queued tags Message-ID: <20221110153533.go5qs3psm75h27mx@quack3> References: <20221105231055.25953-1-krisman@suse.de> <2a445c5c-fd15-c0bf-8655-2fb5bde3fe67@huaweicloud.com> <20221110111636.ufgyp4tkbzexugk2@quack3> <210f2c3d-0bc1-0a5f-964b-d75020d3d9fb@huaweicloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <210f2c3d-0bc1-0a5f-964b-d75020d3d9fb@huaweicloud.com> 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 Hi! On Thu 10-11-22 21:18:19, Yu Kuai wrote: > 在 2022/11/10 19:16, Jan Kara 写道: > > Hi! > > > > On Thu 10-11-22 17:42:49, Yu Kuai wrote: > > > 在 2022/11/06 7:10, Gabriel Krisman Bertazi 写道: > > > > +void sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr) > > > > { > > > > - struct sbq_wait_state *ws; > > > > - unsigned int wake_batch; > > > > - int wait_cnt, cur, sub; > > > > - bool ret; > > > > + unsigned int wake_batch = READ_ONCE(sbq->wake_batch); > > > > + struct sbq_wait_state *ws = NULL; > > > > + unsigned int wakeups; > > > > - if (*nr <= 0) > > > > - return false; > > > > + if (!atomic_read(&sbq->ws_active)) > > > > + return; > > > > - ws = sbq_wake_ptr(sbq); > > > > - if (!ws) > > > > - return false; > > > > + atomic_add(nr, &sbq->completion_cnt); > > > > + wakeups = atomic_read(&sbq->wakeup_cnt); > > > > - cur = atomic_read(&ws->wait_cnt); > > > > do { > > > > - /* > > > > - * For concurrent callers of this, callers should call this > > > > - * function again to wakeup a new batch on a different 'ws'. > > > > - */ > > > > - if (cur == 0) > > > > - return true; > > > > - sub = min(*nr, cur); > > > > - wait_cnt = cur - sub; > > > > - } while (!atomic_try_cmpxchg(&ws->wait_cnt, &cur, wait_cnt)); > > > > - > > > > - /* > > > > - * If we decremented queue without waiters, retry to avoid lost > > > > - * wakeups. > > > > - */ > > > > - if (wait_cnt > 0) > > > > - return !waitqueue_active(&ws->wait); > > > > + if (atomic_read(&sbq->completion_cnt) - wakeups < wake_batch) > > > > + return; > > > > > > Should it be considered that completion_cnt overflow and becomes > > > negtive? > > > > Yes, the counters can (and will) certainly overflow but since we only care > > about (completion_cnt - wakeups), we should be fine - this number is always > > sane (and relatively small) and in the kernel we do compile with signed > > overflows being well defined. > > I'm worried about this: for example, the extreme scenaro that there > is only one tag, currently there are only one infight rq and one thread > is waiting for tag. When the infight rq complete, if 'completion_cnt' > overflow to negative, then 'atomic_read(&sbq->completion_cnt) - wakeups > < wake_batch' will be passed unexpected, then will the thread never be > woken up if there are no new io issued ? Well but my point is that 'wakeups' is staying close to completion_cnt. So if completion_cnt wraps to INT_MIN, then 'wakeups' is close to INT_MAX and so completion_cnt - wakeups is going to wrap back and still result in a small number. That is simply how wrapping arithmetics works... Honza -- Jan Kara SUSE Labs, CR