Message ID | 20231203193307.542794-2-yury.norov@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp2382143vqy; Sun, 3 Dec 2023 11:33:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IFkzFLY+vMJP9/xlLm7HbAa36+RRIhL3IC3vqnzuapFhn4tM+LE/WRh/0sKX4/iRFpvM08t X-Received: by 2002:a05:6358:640b:b0:170:160e:b193 with SMTP id f11-20020a056358640b00b00170160eb193mr3394694rwh.22.1701632007572; Sun, 03 Dec 2023 11:33:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701632007; cv=none; d=google.com; s=arc-20160816; b=cEmMkVoCca9w+L9ahye4SngM4j6MY6U0KJ8SeQT/lS4nuvSDoRD5eVmhYP8a7z2yML NFgs85gXPITRmok+kCxY7G5CyZ91wDl2HMD+EHJnqGNONkA5YhR3UK9dhPrM2vCENM0O IjB1TFgzep3Q5kDoKVx69kRnbJIl9LFwcPwZpohqH18wW1yNTqRLqpp//lmaJlL1aW5j EuBeZp9iiS5O7GTFh9WAnpqG2b6dtfgsYGFPU/lfaFD06GinASqzbVwaJ0zUx8YUqLNa wakZLhLQHoZsoucYzldhG7jOzNt6mTvgEASlq9OocSxWnDgbBK6q5ZvGzjqIhqWjDqf9 D1MA== 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=lAyYvrL0t2q2EDlcDQbeeWn4OCREUUSIjrvkd6SFaaI=; fh=dp19uW080dhyduaU3ZulKMOfLdMz0dW2Wi61Kfp9p4I=; b=wOtH8lT7hQ6mX4JbQP9B/ioh6afI6e9uBbdIajLjorJkhzIL4V3eDeX+H0kJjzWxfo TjHtjXhAFwbG19a1xFFalktEOYqJQ5yiTNYak3H4DJ6eHjf9QGbiajJ1fD1AQ5ytY0tI 76bbSx0WOaYBxHC1jPyfCE/4/HWyNtiPBT3/dwwc0U+Cy31uBb6DT5gCVJmBML4jMCM2 Vrc9hS7OiOn4M1pXDE2V/TpPUHjl7MIhdILhkqc+K1wrBD84/lxo/Tn+7lNKaoxotsf4 Yd1ekT+/d4KohDTcejZkC7SFUGuQKvcc61PHfxaG4ocGwbBgXE+fFPh//huenPLwcNb1 asew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ViFkvIUY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id s15-20020a17090aad8f00b00285f2c4dc2csi2715666pjq.144.2023.12.03.11.33.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Dec 2023 11:33:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ViFkvIUY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id A85B58065809; Sun, 3 Dec 2023 11:33:24 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233890AbjLCTdI (ORCPT <rfc822;pwkd43@gmail.com> + 99 others); Sun, 3 Dec 2023 14:33:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233862AbjLCTdH (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 3 Dec 2023 14:33:07 -0500 Received: from mail-yw1-x1134.google.com (mail-yw1-x1134.google.com [IPv6:2607:f8b0:4864:20::1134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D75E1D7; Sun, 3 Dec 2023 11:33:13 -0800 (PST) Received: by mail-yw1-x1134.google.com with SMTP id 00721157ae682-5d8a772157fso827577b3.3; Sun, 03 Dec 2023 11:33:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701631992; x=1702236792; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lAyYvrL0t2q2EDlcDQbeeWn4OCREUUSIjrvkd6SFaaI=; b=ViFkvIUYg4v6/lNqDfNqYsLRcXHWUnBnF+vsVxHZ3HO+xYkMlTwHPwBk/TMsyuvK8o ND7cW9QOxRjxm1CcydxuC7IJZsJLyvUmxVF11Qpic71op2WLmHcI+M8emGqsSJvks2Hs TP/8dagURbCYETp5iZv65uIui3uAmWDpzF3wf4ClTD481/390d/ziG+Lp9fJR28krv/i 5yAop0eKmyuNRmunozf1w8GRsNvylHvTqypmB41VxIj6MSYq6yAWq4TlCEEVZqD6h0v9 ry2+208kxYnOC4FkFILHQ2KYF0uOnfZfGmgUVFoA9ZcOx5pE+gCKujxGAKOy6fN7YY56 gbrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701631992; x=1702236792; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lAyYvrL0t2q2EDlcDQbeeWn4OCREUUSIjrvkd6SFaaI=; b=m4Pxl3NUEzQIOOySBkFw0xgh8D9HcB79wwpEsubH6B7mStLh412Grm7g9sZa2NUDMy GDP/RaUng+LDYRjBSDbo4lAlfZWaj0+IWSMpA7h+erQBSIkGx3ARB6U9iSyfQ7p9dcmS BYlw0sIFHvAa4aH3GzbUpALnbl8gG2ZOqtQ4fdXwfzcjuGhNUgMGdD/IwAscEYUxOBzr w0AjvfqTovHTZ8PVN+JWvxgjZvUewAUBuXhjfCVljmkviy71L8USPDBYZXdepcDaFI5u YK9l1KwMZQN2HH9ZdzTYNunP99zaTtQvMBcv004pz7OJaMb0uaDdusw0JH86epfSx/fM 5Htg== X-Gm-Message-State: AOJu0YySR3cnvTDvzPI5UCbESjy0flZt12t6x7ENKxysqNEzcFSNP0a2 adEX8SG8F26krxJ1BQpn6ypE4tJeYmt72w== X-Received: by 2002:a81:ee0b:0:b0:5d7:1941:a9a with SMTP id l11-20020a81ee0b000000b005d719410a9amr2124651ywm.53.1701631992159; Sun, 03 Dec 2023 11:33:12 -0800 (PST) Received: from localhost ([2601:344:8301:57f0:cb98:c3e:57c:8191]) by smtp.gmail.com with ESMTPSA id w19-20020a0dd413000000b005d6da42e443sm1718421ywd.59.2023.12.03.11.33.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Dec 2023 11:33:11 -0800 (PST) From: Yury Norov <yury.norov@gmail.com> To: linux-kernel@vger.kernel.org, Jens Axboe <axboe@kernel.dk>, linux-block@vger.kernel.org Cc: Yury Norov <yury.norov@gmail.com>, Jan Kara <jack@suse.cz>, Mirsad Todorovac <mirsad.todorovac@alu.unizg.hr>, Matthew Wilcox <willy@infradead.org>, Rasmus Villemoes <linux@rasmusvillemoes.dk>, Andy Shevchenko <andriy.shevchenko@linux.intel.com>, Maxim Kuvyrkov <maxim.kuvyrkov@linaro.org>, Alexey Klimov <klimov.linux@gmail.com>, Bart Van Assche <bvanassche@acm.org>, Sergey Shtylyov <s.shtylyov@omp.ru> Subject: [PATCH v2 03/35] lib/sbitmap; make __sbitmap_get_word() using find_and_set_bit() Date: Sun, 3 Dec 2023 11:32:35 -0800 Message-Id: <20231203193307.542794-2-yury.norov@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231203193307.542794-1-yury.norov@gmail.com> References: <20231203192422.539300-1-yury.norov@gmail.com> <20231203193307.542794-1-yury.norov@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,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 agentk.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Sun, 03 Dec 2023 11:33:24 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784290483780816687 X-GMAIL-MSGID: 1784290483780816687 |
Series |
bitops: add atomic find_bit() operations
|
|
Commit Message
Yury Norov
Dec. 3, 2023, 7:32 p.m. UTC
__sbitmap_get_word() opencodes either find_and_set_bit_wrap(), or
find_and_set_next_bit() depending on hint and wrap parameters.
Switch it to use the atomic find_bit() API. While here, simplify
sbitmap_find_bit_in_word(), which calls it.
Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
lib/sbitmap.c | 46 ++++++++--------------------------------------
1 file changed, 8 insertions(+), 38 deletions(-)
Comments
On Sun 03-12-23 11:32:35, Yury Norov wrote: > __sbitmap_get_word() opencodes either find_and_set_bit_wrap(), or > find_and_set_next_bit() depending on hint and wrap parameters. > > Switch it to use the atomic find_bit() API. While here, simplify > sbitmap_find_bit_in_word(), which calls it. > > Signed-off-by: Yury Norov <yury.norov@gmail.com> Looks good to me. Feel free to add: Reviewed-by: Jan Kara <jack@suse.cz> Honza > --- > lib/sbitmap.c | 46 ++++++++-------------------------------------- > 1 file changed, 8 insertions(+), 38 deletions(-) > > diff --git a/lib/sbitmap.c b/lib/sbitmap.c > index d0a5081dfd12..b21aebd07fd6 100644 > --- a/lib/sbitmap.c > +++ b/lib/sbitmap.c > @@ -133,38 +133,11 @@ void sbitmap_resize(struct sbitmap *sb, unsigned int depth) > } > EXPORT_SYMBOL_GPL(sbitmap_resize); > > -static int __sbitmap_get_word(unsigned long *word, unsigned long depth, > +static inline int __sbitmap_get_word(unsigned long *word, unsigned long depth, > unsigned int hint, bool wrap) > { > - int nr; > - > - /* don't wrap if starting from 0 */ > - wrap = wrap && hint; > - > - while (1) { > - nr = find_next_zero_bit(word, depth, hint); > - if (unlikely(nr >= depth)) { > - /* > - * We started with an offset, and we didn't reset the > - * offset to 0 in a failure case, so start from 0 to > - * exhaust the map. > - */ > - if (hint && wrap) { > - hint = 0; > - continue; > - } > - return -1; > - } > - > - if (!test_and_set_bit_lock(nr, word)) > - break; > - > - hint = nr + 1; > - if (hint >= depth - 1) > - hint = 0; > - } > - > - return nr; > + return wrap ? find_and_set_bit_wrap_lock(word, depth, hint) : > + find_and_set_next_bit_lock(word, depth, hint); > } > > static int sbitmap_find_bit_in_word(struct sbitmap_word *map, > @@ -175,15 +148,12 @@ static int sbitmap_find_bit_in_word(struct sbitmap_word *map, > int nr; > > do { > - nr = __sbitmap_get_word(&map->word, depth, > - alloc_hint, wrap); > - if (nr != -1) > - break; > - if (!sbitmap_deferred_clear(map)) > - break; > - } while (1); > + nr = __sbitmap_get_word(&map->word, depth, alloc_hint, wrap); > + if (nr < depth) > + return nr; > + } while (sbitmap_deferred_clear(map)); > > - return nr; > + return -1; > } > > static int sbitmap_find_bit(struct sbitmap *sb, > -- > 2.40.1 >
On 12/3/23 12:32 PM, Yury Norov wrote: > diff --git a/lib/sbitmap.c b/lib/sbitmap.c > index d0a5081dfd12..b21aebd07fd6 100644 > --- a/lib/sbitmap.c > +++ b/lib/sbitmap.c > @@ -133,38 +133,11 @@ void sbitmap_resize(struct sbitmap *sb, unsigned int depth) > } > EXPORT_SYMBOL_GPL(sbitmap_resize); > > -static int __sbitmap_get_word(unsigned long *word, unsigned long depth, > +static inline int __sbitmap_get_word(unsigned long *word, unsigned long depth, > unsigned int hint, bool wrap) > { > - int nr; > - > - /* don't wrap if starting from 0 */ > - wrap = wrap && hint; > - > - while (1) { > - nr = find_next_zero_bit(word, depth, hint); > - if (unlikely(nr >= depth)) { > - /* > - * We started with an offset, and we didn't reset the > - * offset to 0 in a failure case, so start from 0 to > - * exhaust the map. > - */ > - if (hint && wrap) { > - hint = 0; > - continue; > - } > - return -1; > - } > - > - if (!test_and_set_bit_lock(nr, word)) > - break; > - > - hint = nr + 1; > - if (hint >= depth - 1) > - hint = 0; > - } > - > - return nr; > + return wrap ? find_and_set_bit_wrap_lock(word, depth, hint) : > + find_and_set_next_bit_lock(word, depth, hint); > } Please make this: if (wrap) return find_and_set_bit_wrap_lock(word, depth, hint) : return find_and_set_next_bit_lock(word, depth, hint); so this is a lot more readable.
diff --git a/lib/sbitmap.c b/lib/sbitmap.c index d0a5081dfd12..b21aebd07fd6 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c @@ -133,38 +133,11 @@ void sbitmap_resize(struct sbitmap *sb, unsigned int depth) } EXPORT_SYMBOL_GPL(sbitmap_resize); -static int __sbitmap_get_word(unsigned long *word, unsigned long depth, +static inline int __sbitmap_get_word(unsigned long *word, unsigned long depth, unsigned int hint, bool wrap) { - int nr; - - /* don't wrap if starting from 0 */ - wrap = wrap && hint; - - while (1) { - nr = find_next_zero_bit(word, depth, hint); - if (unlikely(nr >= depth)) { - /* - * We started with an offset, and we didn't reset the - * offset to 0 in a failure case, so start from 0 to - * exhaust the map. - */ - if (hint && wrap) { - hint = 0; - continue; - } - return -1; - } - - if (!test_and_set_bit_lock(nr, word)) - break; - - hint = nr + 1; - if (hint >= depth - 1) - hint = 0; - } - - return nr; + return wrap ? find_and_set_bit_wrap_lock(word, depth, hint) : + find_and_set_next_bit_lock(word, depth, hint); } static int sbitmap_find_bit_in_word(struct sbitmap_word *map, @@ -175,15 +148,12 @@ static int sbitmap_find_bit_in_word(struct sbitmap_word *map, int nr; do { - nr = __sbitmap_get_word(&map->word, depth, - alloc_hint, wrap); - if (nr != -1) - break; - if (!sbitmap_deferred_clear(map)) - break; - } while (1); + nr = __sbitmap_get_word(&map->word, depth, alloc_hint, wrap); + if (nr < depth) + return nr; + } while (sbitmap_deferred_clear(map)); - return nr; + return -1; } static int sbitmap_find_bit(struct sbitmap *sb,