From nobody Sat Apr 18 19:15:17 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 752BBC43334 for ; Mon, 11 Jul 2022 18:10:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231569AbiGKSKv (ORCPT ); Mon, 11 Jul 2022 14:10:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229664AbiGKSKq (ORCPT ); Mon, 11 Jul 2022 14:10:46 -0400 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46BBC13D19 for ; Mon, 11 Jul 2022 11:10:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1657563045; x=1689099045; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hsOLQf/Ey5fXFw8rD7/ny7InOv0adjlq6qZFdlk12V4=; b=ly6asgvG0oDTMpT/XjKmdQpAYV0hFFzAPJfNYIUoqdmVx2vlN6HyyJN2 9sjeIuqjL+IbagGZWKvktJ4GXc9uPQHpAfp+JyYMhfhlMSHf3UnOZxsSE Wv/Oa7djSb3IGWpR7mcom3BsselAfa94yuxtkhkzXTFR5JOih3+gyX4Ri Kc0apJfSmN/cWxPYwwnqtqVowzKFZnKiJz17VTXv7R05QjrIENHN9LrbY mgQaf7sOYOL45VHi4Us44uUE/brOMvuGixlCiBO7fU1QZ7det0+1pwPBj ApXSfTdneDCpiVWO9BJkJo0bO4NLJw1nK5FG+gwm1men/vzAxT8YBu/mf g==; X-IronPort-AV: E=McAfee;i="6400,9594,10405"; a="285477397" X-IronPort-AV: E=Sophos;i="5.92,263,1650956400"; d="scan'208";a="285477397" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jul 2022 11:10:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,263,1650956400"; d="scan'208";a="921884666" Received: from irvmail001.ir.intel.com ([10.43.11.63]) by fmsmga005.fm.intel.com with ESMTP; 11 Jul 2022 11:10:43 -0700 Received: from newjersey.igk.intel.com (newjersey.igk.intel.com [10.102.20.203]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id 26BIAfWt032629; Mon, 11 Jul 2022 19:10:42 +0100 From: Alexander Lobakin To: Yury Norov Cc: Alexander Lobakin , Andy Shevchenko , Rasmus Villemoes , linux-kernel@vger.kernel.org Subject: [PATCH 1/2] lib/bitmap: fix off-by-one in bitmap_to_arr64() Date: Mon, 11 Jul 2022 20:09:29 +0200 Message-Id: <20220711180930.28271-2-alexandr.lobakin@intel.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220711180930.28271-1-alexandr.lobakin@intel.com> References: <20220711180930.28271-1-alexandr.lobakin@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" GENMASK*() family takes the first and the last bits of the mask *including* them. So, with the current code bitmap_to_arr64() doesn't clear the tail properly: nbits % exp mask must be 1 GENMASK(1, 0) 0x3 0x1 ... 63 GENMASK(63, 0) 0xffffffffffffffff 0x7fffffffffffffff This was found by making the function always available instead of 32-bit BE systems only (for reusing in some new functionality). Turn the number of bits into the last bit set by subtracting 1. @nbits is already checked to be positive beforehand. Fixes: 0a97953fd221 ("lib: add bitmap_{from,to}_arr64") Signed-off-by: Alexander Lobakin Reviewed-by: Andy Shevchenko --- lib/bitmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/bitmap.c b/lib/bitmap.c index b18e31ea6e66..e903e13c62e1 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -1564,7 +1564,7 @@ void bitmap_to_arr64(u64 *buf, const unsigned long *b= itmap, unsigned int nbits) =20 /* Clear tail bits in the last element of array beyond nbits. */ if (nbits % 64) - buf[-1] &=3D GENMASK_ULL(nbits % 64, 0); + buf[-1] &=3D GENMASK_ULL((nbits - 1) % 64, 0); } EXPORT_SYMBOL(bitmap_to_arr64); #endif --=20 2.36.1 From nobody Sat Apr 18 19:15:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D3CA3C433EF for ; Mon, 11 Jul 2022 18:10:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231740AbiGKSKy (ORCPT ); Mon, 11 Jul 2022 14:10:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229957AbiGKSKq (ORCPT ); Mon, 11 Jul 2022 14:10:46 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2785F13CFC for ; Mon, 11 Jul 2022 11:10:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1657563046; x=1689099046; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2UgWlqXP52d73V37I18PoM3YgCtTRhnO/y/nfymCtKM=; b=DBrpc1MDqC71A7aj1P3zMqezV7ZbUVSf2sEDCKUdnFqpNWUP8GHtcT4V nbiQqTcEm4cBtRs08QlPcnQbXPmToD/5kAZ91nLUOxOltc4viXgs5f/Vg GDBiM6Vq5n5VM8r62hhyKB2a9N9kjtX2BVkWr0nuaC+f8j0+UnrPJzZMo IESOvnGZFHFQr/DBltcKK5dfny1BiWrBMHZlhbyTHssTslvOP7YYJDyKF otWY1TSVP8XWXmVIftaF+bxbVvLxYFR7d50S5JpcH3L5BC4UoHqAC0v1n FITC+h4je62iouOgFOu8y1xC2Qrz/MzWYIePMf5X30+eBSgISJAcsbB+a w==; X-IronPort-AV: E=McAfee;i="6400,9594,10405"; a="310341572" X-IronPort-AV: E=Sophos;i="5.92,263,1650956400"; d="scan'208";a="310341572" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jul 2022 11:10:45 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,263,1650956400"; d="scan'208";a="684477133" Received: from irvmail001.ir.intel.com ([10.43.11.63]) by FMSMGA003.fm.intel.com with ESMTP; 11 Jul 2022 11:10:43 -0700 Received: from newjersey.igk.intel.com (newjersey.igk.intel.com [10.102.20.203]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id 26BIAfWu032629; Mon, 11 Jul 2022 19:10:42 +0100 From: Alexander Lobakin To: Yury Norov Cc: Alexander Lobakin , Andy Shevchenko , Rasmus Villemoes , linux-kernel@vger.kernel.org Subject: [PATCH 2/2] lib/test_bitmap: test the tail after bitmap_to_arr64() Date: Mon, 11 Jul 2022 20:09:30 +0200 Message-Id: <20220711180930.28271-3-alexandr.lobakin@intel.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220711180930.28271-1-alexandr.lobakin@intel.com> References: <20220711180930.28271-1-alexandr.lobakin@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Currently, test_bitmap_arr64() only tests bitmap_to_arr64()'s sanity by comparing the result of double-conversion (bm -> arr64 -> bm2) with the input bitmap. However, this may be not enough when one side hides bugs of the second one (e.g. tail clearing, which is being performed by both). Expand the tests and check the tail of the actual arr64 used as a temporary buffer for double-converting. Signed-off-by: Alexander Lobakin Reviewed-by: Andy Shevchenko --- lib/test_bitmap.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c index d5923a640457..086b1d1db1ca 100644 --- a/lib/test_bitmap.c +++ b/lib/test_bitmap.c @@ -604,6 +604,12 @@ static void __init test_bitmap_arr64(void) pr_err("bitmap_copy_arr64(nbits =3D=3D %d:" " tail is not safely cleared: %d\n", nbits, next_bit); =20 + if ((nbits % 64) && + (arr[(nbits - 1) / 64] & ~GENMASK_ULL((nbits - 1) % 64, 0))) + pr_err("bitmap_to_arr64(nbits =3D=3D %d): tail is not safely cleared: 0= x%016llx (must be 0x%016llx)\n", + nbits, arr[(nbits - 1) / 64], + GENMASK_ULL((nbits - 1) % 64, 0)); + if (nbits < EXP1_IN_BITS - 64) expect_eq_uint(arr[DIV_ROUND_UP(nbits, 64)], 0xa5a5a5a5); } --=20 2.36.1