Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp122070iof; Sun, 5 Jun 2022 23:02:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwXY38aB4NzcPbYYWE7KuH0JqOZiOdg43cK75WQR2HkrcZZAvoyQtx9q1Dj/hvPdXoKeizz X-Received: by 2002:a65:4149:0:b0:3fb:1476:d0dc with SMTP id x9-20020a654149000000b003fb1476d0dcmr19481445pgp.373.1654495327234; Sun, 05 Jun 2022 23:02:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654495327; cv=none; d=google.com; s=arc-20160816; b=hT5cYwbVvO9TCRAYm5/93Ko+TU3kfWbAmY9iyuuJcazrkhHSms3Tv/T5sixkJSmKXc VSKU96WwZexT+r09OCmXYvA7jldxDBaFMYyU+ZnYnWg9zAESscjFfs5oQtS2kgMh5llR Q/uXj9eK6pL2VUhBruQV2IajTpQHI+N8onThXWFNfGDw9fkF3is4TE+jdouIj0iNWwMD ZAuplcS3N1foBlVgOrBYk0lzoiOBGIwzaKGHmEkJos2F5U5kuCoug4oK+Jied0s70k/h Q+qiM9TeTdYOYBTuuIylSp/mO1iOyC6l2ojcYE1MNJBgfbh2e4trbBYMlku9tHPlAv1i qrmQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=lMEEVMlhwqTlA7tCinHR3Gvr2O+regL9vfqAnwRJIa0=; b=EdoBO5hmiFG2ntxU5Bh8V3FrgwwgO020Njql6bxp0GAY/Ezo4UqjYJpnMMVi4hIRbO UtX+O7DwvrNgvRvqcPNeMlqPAq7lqaa+UxSJOVkvBHzqTfUbTagA6PrPJs6Hbx5oqqKk GPDTBfOLWo0822snu9sj7GK3nW5gtqDmJU6QN8JPy77urcrVOXchbqhYhfWEt+aFK3rg K0tl6ErVnaxAlAsuRaVGaVI5GIkzdWFzJf/AnUxfVcikNVWqCyynStKaBhydPMyeNgCx 29uGTouF+IJbIhMXaRYKmMW6NFVKaZh43TFBLdkUBmTPlr2bjGWxXy2Db31mgfp/2Qch Atrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=poko1HFW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id in1-20020a17090b438100b001e88d83b58esi1302980pjb.111.2022.06.05.23.02.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 23:02:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=poko1HFW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F2E6C307224; Sun, 5 Jun 2022 21:50:02 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348224AbiFEO6p (ORCPT + 99 others); Sun, 5 Jun 2022 10:58:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245476AbiFEO6n (ORCPT ); Sun, 5 Jun 2022 10:58:43 -0400 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1182A1AF; Sun, 5 Jun 2022 07:58:41 -0700 (PDT) Received: by mail-pj1-x102c.google.com with SMTP id e24so10970399pjt.0; Sun, 05 Jun 2022 07:58:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=lMEEVMlhwqTlA7tCinHR3Gvr2O+regL9vfqAnwRJIa0=; b=poko1HFW6rmaAXfQD645qOGZYFwI5bum7h+1v1sVPUUIiARKXRxZ03Lvtmv0QbiNhs EYyUYquwzyVEXlnrlD+VCAlkqClWH20PH1H6aLCl96Qp2xBDmcqq/OqMFqrZxKCHM9mf Ui1B4SI+ryxLgKD8UBY/PSr1PDD4+14Jyaq5Lk393LXH9j9rTV3Htoo4E++Hcz+zTbXt +8ZU/MslQtIsM6kxVni7roz+ulUF6sz3e4zNRmGikOTmr3Iy9nWFfDr5UAeKSAbjiy/t wJ9bfFP5lrHT9lIyPoTF6XnXccmLPWHXBQsiorHjFrF4TL6aPVAw7JVKk+z2NljiJD/i Hwuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=lMEEVMlhwqTlA7tCinHR3Gvr2O+regL9vfqAnwRJIa0=; b=oNyf1EaTqLz/oAqwBNf1oNtqqyaxbZzy8D0AEMDDYIUapE28cRbBMiZrRdCaMl90sF Xc2aAvGAIjLGmEKSNhD7ZDSm+G6ffBol82jU3W620C6JX4O/rAPzzx6a05VfTjJTo40W T5tEUdQQxIq02U9zTzneL0jhcUy4Kax0RqkQ7dL3pO4AVvvFUKevHzhB1g0pniw2eQk3 sKOIhIl16Z9CKN4CtvE28si1G/AFTBds5hlspKrByrWOy63FEcHUDXqLW3CIHpj4m5NX nEE5XfOZjEgO9yC0pqctu3BK6V5BwtXyn2stzR+Nw5ZRMsBOfr7j0AyX2Cxpy6cNrMYR BtiA== X-Gm-Message-State: AOAM531Hg2Q/w71mLxXFHlP2yn4HV177VJWzCQwvGiuJMJCDpFwyDz0Q P9CHHLmvRssIFT2Yq9Ieu5w= X-Received: by 2002:a17:902:d490:b0:167:49df:6e37 with SMTP id c16-20020a170902d49000b0016749df6e37mr13066881plg.50.1654441121406; Sun, 05 Jun 2022 07:58:41 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.248]) by smtp.gmail.com with ESMTPSA id l17-20020a170902e2d100b0015e9f45c1f4sm8746521plc.186.2022.06.05.07.58.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 07:58:41 -0700 (PDT) From: wuchi To: axboe@kernel.dk, mwilck@suse.com, andriy.shevchenko@linux.intel.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] lib/sbitmap: Fix invalid loop in __sbitmap_queue_get_batch() Date: Sun, 5 Jun 2022 22:58:35 +0800 Message-Id: <20220605145835.26916-1-wuchi.zero@gmail.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 1. Getting next index before continue branch. 2. Checking free bits when setting the target bits. Otherwise, it may reuse the busying bits. Signed-off-by: wuchi --- lib/sbitmap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/sbitmap.c b/lib/sbitmap.c index ae4fd4de9ebe..29eb0484215a 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c @@ -528,7 +528,7 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags, sbitmap_deferred_clear(map); if (map->word == (1UL << (map_depth - 1)) - 1) - continue; + goto next; nr = find_first_zero_bit(&map->word, map_depth); if (nr + nr_tags <= map_depth) { @@ -539,6 +539,8 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags, get_mask = ((1UL << map_tags) - 1) << nr; do { val = READ_ONCE(map->word); + if ((val & ~get_mask) != val) + goto next; ret = atomic_long_cmpxchg(ptr, val, get_mask | val); } while (ret != val); get_mask = (get_mask & ~ret) >> nr; @@ -549,6 +551,7 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags, return get_mask; } } +next: /* Jump to next index. */ if (++index >= sb->map_nr) index = 0; -- 2.20.1