From nobody Tue May 26 04:50:19 2026 Received: from SHSQR01.spreadtrum.com (unknown [222.66.158.135]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ECDDB224AF2 for ; Tue, 12 May 2026 04:12:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=222.66.158.135 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778559154; cv=none; b=GpXxTZporLEq+ycuVjGapUKbfkA6abyP7UESbn2R8Ecn4LLy43nqMwAjtfwtyE896mTtK19IeVtDTDSJnudYwBMoFCn1fMdM0aJGXjrK3EAjuCN2bcabkL3bpAb0PO6uEEDGqo5p/94vWZ0eNFZmcrmz4PsdnxBkT17tye950fA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778559154; c=relaxed/simple; bh=O3RlzmprFOjor1g5oFAlUgHmjY2djMBouS9fhivkbYI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=YfpXoFqi+xMRDE9KD6YOr47jRiC06TnCJ5sBJknnYu38FDZVQR4mkSfp2TVJRS4XpsBXZSSdNgAGCkHIu/rXjgNslVe1MNh7HNISTi7UwEQBhe1d/8Wu5CS/efEwQ2oEG45ZA1gANJ5Yu/s5ZPJUKua3ycHR0jSHTtzLuAgmCSw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=unisoc.com; spf=pass smtp.mailfrom=unisoc.com; dkim=pass (2048-bit key) header.d=unisoc.com header.i=@unisoc.com header.b=zvif2Mt2; arc=none smtp.client-ip=222.66.158.135 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=unisoc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=unisoc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=unisoc.com header.i=@unisoc.com header.b="zvif2Mt2" Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 64C476mN006366; Tue, 12 May 2026 12:07:06 +0800 (+08) (envelope-from Yi.Sun@unisoc.com) Received: from SHDLP.spreadtrum.com (BJMBX02.spreadtrum.com [10.0.64.8]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4gF2yS5n1lz2M3YFT; Tue, 12 May 2026 12:03:44 +0800 (CST) Received: from localhost.localdomain (10.5.32.15) by BJMBX02.spreadtrum.com (10.0.64.8) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Tue, 12 May 2026 12:07:03 +0800 From: Yi Sun To: , , CC: , , Subject: [PATCH 1/2] lib: bitmap: add find_last_bit_range() and _find_last_bit_range() Date: Tue, 12 May 2026 12:06:58 +0800 Message-ID: <20260512040659.2992142-2-yi.sun@unisoc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260512040659.2992142-1-yi.sun@unisoc.com> References: <20260512040659.2992142-1-yi.sun@unisoc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX02.spreadtrum.com (10.0.64.8) X-MAIL: SHSQR01.spreadtrum.com 64C476mN006366 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=unisoc.com; s=default; t=1778558835; bh=T5far75EKY/0cnmchBxDDX+QbsVWO+aeLV3JvHuab74=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=zvif2Mt2bVlMDmJdI7d4nbTF9hsrCeN88gKI2oZ4Se/u5kXOJfV61Fw1bDmA5fOQZ tKMxRi9+FRD3NSl95WCsappzS/oJCgmlmj/jV9jGiXVj/8ynHVog1A2s+m1tcyNt96 /JIePCSS849LV1V5w+DkQXkAcd3KASJ4g/C4a63p1XzD9dhIlklXjxP7usIVdF0mld oco31x2A4MR8aqNpmYAm5R16sYP8gqmoKlwDfMsM85P0jtNsxflk4jHLsEjU1dtog7 G1AUNujuPtCa3VwNKlsXGIbZzCC/YVpI9rAAgHXsd/xLGNNTbw3y0cgnqFBR45CR5E Nadour85PRunQ== Content-Type: text/plain; charset="utf-8" In some scenarios, it's not desirable to keep searching through the beginning of the bitmap, but rather to search within a specific part. The newly added function can accomplish this quickly. Signed-off-by: Yi Sun Acked-by: Micha=C5=82 Nazarewicz --- include/linux/find.h | 35 +++++++++++++++++++++++++++++++++++ lib/find_bit.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/include/linux/find.h b/include/linux/find.h index 6c2be8ca615d..7126b0fffe0f 100644 --- a/include/linux/find.h +++ b/include/linux/find.h @@ -33,6 +33,8 @@ unsigned long _find_first_and_and_bit(const unsigned long= *addr1, const unsigned const unsigned long *addr3, unsigned long size); extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsig= ned long size); extern unsigned long _find_last_bit(const unsigned long *addr, unsigned lo= ng size); +extern unsigned long _find_last_bit_range(const unsigned long *addr, unsig= ned long size, + unsigned long offset); =20 #ifdef __BIG_ENDIAN unsigned long _find_first_zero_bit_le(const unsigned long *addr, unsigned = long size); @@ -413,6 +415,39 @@ unsigned long find_last_bit(const unsigned long *addr,= unsigned long size) } #endif =20 +#ifndef find_last_bit_range +/** + * find_last_bit_range - find the last set bit in a memory region + * @addr: The address to base the search on + * @size: The bitmap size in bits + * @offset: The bit number to start searching at + * + * Compared to the find_last_bit(), + * find_last_bit_range() has an additional parameter @offset, + * so it can search within a specific range of the bitmap, + * just like the find_next_bit(). + * + * Returns the bit number of the last set bit, or size. + */ +static __always_inline +unsigned long find_last_bit_range(const unsigned long *addr, unsigned long= size, + unsigned long offset) +{ + if (small_const_nbits(size)) { + unsigned long val; + + if (unlikely(offset >=3D size)) + return size; + + val =3D *addr & GENMASK(size - 1, offset); + + return val ? __fls(val) : size; + } + + return _find_last_bit_range(addr, size, offset); +} +#endif + /** * find_next_and_bit_wrap - find the next set bit in both memory regions * @addr1: The first address to base the search on diff --git a/lib/find_bit.c b/lib/find_bit.c index 5ac52dfce730..bedc85053cea 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -237,6 +237,36 @@ unsigned long _find_last_bit(const unsigned long *addr= , unsigned long size) EXPORT_SYMBOL(_find_last_bit); #endif =20 +#ifndef find_last_bit_range +unsigned long _find_last_bit_range(const unsigned long *addr, unsigned lon= g size, + unsigned long offset) +{ + unsigned long val, idx, start_idx; + + if (unlikely(offset >=3D size)) + return size; + + val =3D BITMAP_LAST_WORD_MASK(size); + idx =3D (size - 1) / BITS_PER_LONG; + start_idx =3D offset / BITS_PER_LONG; + + do { + val &=3D addr[idx]; + + if (idx =3D=3D start_idx) + val &=3D BITMAP_FIRST_WORD_MASK(offset); + + if (val) + return idx * BITS_PER_LONG + __fls(val); + + val =3D ~0UL; + } while (idx-- > start_idx); + + return size; +} +EXPORT_SYMBOL(_find_last_bit_range); +#endif + unsigned long find_next_clump8(unsigned long *clump, const unsigned long *= addr, unsigned long size, unsigned long offset) { --=20 2.34.1 From nobody Tue May 26 04:50:19 2026 Received: from SHSQR01.spreadtrum.com (unknown [222.66.158.135]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 672B1383338 for ; Tue, 12 May 2026 04:09:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=222.66.158.135 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778558985; cv=none; b=gLeFRQXsEjeBGD59H7yywMBBNHMbaLSTpiCrLot2A/Z7ey/d66M5GVKeGBonnzkguS8nOrrikRb4jQRoJScUymEXjbLUV8ju5VEXVt7sIQuBMXScGpu7XPqAiE53/AYZ2Vs5GqNkNFvBfbWqL/LQhOXQZj7qNvbGL8VLe+BHrXo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778558985; c=relaxed/simple; bh=Q3EncE/Ge5T4DIH4kRzLYf5r/zIe2RGNwkFFCQINGcA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JrxZBD4lpB07Gk6u1MgYiWyGIlOlDfOWBh9V5brLDAafjZeikIjnG13q8Av7ND3U+LRAvciQqM7nvFtCrx0fC3yL2gChQKBJj+5IIrTfo6/LIqKMLdGCxh7msbM8D7jMu1UaazNH1/WjznLwTjMtNW9fATVq9bm8Zzqm022pS44= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=unisoc.com; spf=pass smtp.mailfrom=unisoc.com; dkim=pass (2048-bit key) header.d=unisoc.com header.i=@unisoc.com header.b=idXOEz7Y; arc=none smtp.client-ip=222.66.158.135 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=unisoc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=unisoc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=unisoc.com header.i=@unisoc.com header.b="idXOEz7Y" Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 64C477N5006468; Tue, 12 May 2026 12:07:07 +0800 (+08) (envelope-from Yi.Sun@unisoc.com) Received: from SHDLP.spreadtrum.com (BJMBX02.spreadtrum.com [10.0.64.8]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4gF2yT6dLCz2M3YFM; Tue, 12 May 2026 12:03:45 +0800 (CST) Received: from localhost.localdomain (10.5.32.15) by BJMBX02.spreadtrum.com (10.0.64.8) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Tue, 12 May 2026 12:07:04 +0800 From: Yi Sun To: , , CC: , , Subject: [PATCH 2/2] lib: bitmap: reduce the number of goto again in bitmap_find_next_zero_area_off() Date: Tue, 12 May 2026 12:06:59 +0800 Message-ID: <20260512040659.2992142-3-yi.sun@unisoc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260512040659.2992142-1-yi.sun@unisoc.com> References: <20260512040659.2992142-1-yi.sun@unisoc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX02.spreadtrum.com (10.0.64.8) X-MAIL: SHSQR01.spreadtrum.com 64C477N5006468 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=unisoc.com; s=default; t=1778558843; bh=MMUyRkgmRCU5cdh+jjddYqLABEbeluyuXTP/tXDa4q8=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=idXOEz7YMIWLxT6MmCrh38Gg3+2Aru1eFcstrAw2OWT1s73FY4hoNAgOUgM1CT4XM PrN4A4m0Cy19deej7wl0jU1jEpi9dPAQOsE6CPzUraOkB/GL/R/NOUp6DCKzoOFF7O uqDN8jQbAffyg+lrNBuxqnu/rbPQhz4MtbrF/l8i5d+d72DqoynPhFE4LQl5I+ZZKw zFD+iMQNZaU3Y51Yf+uX6WslJKlxnCwao3sa+c8Cds7UVGNBg/e7LPW9aRPqszFTeD TbpShMyabeYuI/oFS24pSoiz74LRdjLqurRMcKvgJ9c2aa1a2XscxLY6N84BlguHcD bRhhNCin+nPJQ== Content-Type: text/plain; charset="utf-8" Finding a contiguous free region in a highly fragmented bitmap is not easy and may require many repeated attempts. Therefore, find_next_bit(map, end, index) is not the optimal choice. This is because there may be multiple scattered free regions within the range [index, end) and none of them will meet the length requirement of @nr. Instead, it's sufficient to directly find the last bit within the range [index, end), thus reducing unnecessary "goto again" calls. Signed-off-by: Yi Sun Acked-by: Micha=C5=82 Nazarewicz --- lib/bitmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/bitmap.c b/lib/bitmap.c index b9bfa157e095..53961a7683a4 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -442,7 +442,7 @@ unsigned long bitmap_find_next_zero_area_off(unsigned l= ong *map, end =3D index + nr; if (end > size) return end; - i =3D find_next_bit(map, end, index); + i =3D find_last_bit_range(map, end, index); if (i < end) { start =3D i + 1; goto again; --=20 2.34.1