Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp9343407rwp; Thu, 20 Jul 2023 03:35:28 -0700 (PDT) X-Google-Smtp-Source: APBJJlGe1pjxy1Wu0hWvp55Th4tvjceaOfK5gGoLj92i1EdwCaWjM9x59AC4K4Phg5iPM/6jy1JE X-Received: by 2002:aa7:db49:0:b0:51e:34a1:8bf7 with SMTP id n9-20020aa7db49000000b0051e34a18bf7mr4245373edt.39.1689849328618; Thu, 20 Jul 2023 03:35:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689849328; cv=none; d=google.com; s=arc-20160816; b=I3mBCcbkzh9AF3jsInfpa8h/nY/MWVz2GzQNmYy1UFjGPKSpEXOdvj6mZyvC4rM+ct x3+6crj0Kq6/G60/ImqDCt2SYFJky9X/txS2xRZuxq25bx+nt2tgnMi0MoznXqXJfeoh uwglJ3TqtJbikxCSnp220YPqTviDFGRS6WqaxgwIPmBHV9pSWB+kuBil3z/xPVMji16O 8Dpno+Bh1QfYTpWyhC5unWRfymL7/EzER9sy6uaAQ6AmBbPQ+0K1nzNXa6XLUrFOgqbl /QEfOJUaMZ/d35wcCkoFHtp8U5fn8O89O1PbxaXLuOYZEeZL8n2HerFzJCcRODyZzWCI zHgA== 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; bh=HexLk5YdZ9UMqQVRKs36MeqJ3bE8mocAttG9oHtUF6A=; fh=hjJkBnU2szufEu6iYNeHw5+ILtjbq+a6zNGIo+CbvPw=; b=hvadqSH1NX1ZRaIFBgVZ+EmcpYqNEn/fOcD1gsawenvHa1L2/VIVxCKIapTFdnVSBk h7hCD3mL+HYh5gZgMAK8gYNcWHjmUYx+x+Bt2vgMb0bR1FvTBrNNHHUY13aDMvBu77ky mhDlgL6HBRivdi5ayJ8ihejP77u5AkVVOo6hfTlqbl2Up0yYWZ8eXL7ck/MZr2idCoxh O/fqsGOkWIw/dv841mH9JNqa9Z+HCVP2ErZeonygrUHrJ7A2XnoUS7EBdrVtBVfs6//W llUbsGMnvxYjES5TYysTg4XxG+p9yqW/4V9ASS0TvygIJEmFsk0DL82hVBy8gu8QTtmt kN5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=T3g0daIq; 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=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l1-20020aa7c3c1000000b0051ddbdeb0c7si616823edr.350.2023.07.20.03.35.04; Thu, 20 Jul 2023 03:35:28 -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=@linux.dev header.s=key1 header.b=T3g0daIq; 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=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230445AbjGTJsy (ORCPT + 99 others); Thu, 20 Jul 2023 05:48:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231404AbjGTJsL (ORCPT ); Thu, 20 Jul 2023 05:48:11 -0400 Received: from out-33.mta0.migadu.com (out-33.mta0.migadu.com [IPv6:2001:41d0:1004:224b::21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 974AB2708 for ; Thu, 20 Jul 2023 02:47:21 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1689846440; h=from:from:reply-to:subject:subject: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=HexLk5YdZ9UMqQVRKs36MeqJ3bE8mocAttG9oHtUF6A=; b=T3g0daIqsxUtbedfWKPmCc61Qc12FtWKResFJpeO2mr/aRXdYhFQp9Pq27ndDZNyrYc5Kt 7uDJsxRP4E45zPxPxh7vmCqiZoRPQfuQmeTGRkOu8Atn66cRWwHReZ+iWGrbu+ify0SdMJ eCiiqpd8AYC3WMEkSJGYMsndmZ5Jq5E= From: chengming.zhou@linux.dev To: axboe@kernel.dk, osandov@fb.com, ming.lei@redhat.com, kbusch@kernel.org, krisman@suse.de Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, zhouchengming@bytedance.com Subject: [PATCH 2/6] sbitmap: fix round-robin non-wrap find with hint > 0 Date: Thu, 20 Jul 2023 17:45:51 +0800 Message-ID: <20230720094555.1397621-3-chengming.zhou@linux.dev> In-Reply-To: <20230720094555.1397621-1-chengming.zhou@linux.dev> References: <20230720094555.1397621-1-chengming.zhou@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 From: Chengming Zhou If we have alloc_hint > 0 and don't wrap, we need to recheck sb->map[index] with hint == 0 to exhaust the map. Signed-off-by: Chengming Zhou --- lib/sbitmap.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/sbitmap.c b/lib/sbitmap.c index 5ed6c2adf58e..ccb96d1f92ba 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c @@ -192,10 +192,18 @@ static int sbitmap_find_bit(struct sbitmap *sb, unsigned int alloc_hint, bool wrap) { + unsigned int map_nr = sb->map_nr; unsigned int i; int nr = -1; - for (i = 0; i < sb->map_nr; i++) { + /* + * If we have alloc_hint > 0 and don't wrap, we need to + * recheck sb->map[index] with hint == 0 to exhaust the map. + */ + if (alloc_hint && !wrap) + map_nr += 1; + + for (i = 0; i < map_nr; i++) { nr = sbitmap_find_bit_in_word(&sb->map[index], min_t(unsigned int, __map_depth(sb, index), -- 2.41.0