From nobody Tue May 26 05:40:00 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 4DC5D3B38B9 for ; Thu, 14 May 2026 09:11:14 +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=1778749877; cv=none; b=dctItb3nIgbLpajO8IuAAacmhoXp1PpgYF0Ojv2jVHIZJv5llqiqOEtKjeYtg2+SnBprYkiNq3ScdguYtb0uwwTKpA5/UYATrbus1h7Lrw3tHlAs0sPACgVKPtESPDlrZFB7+o/TCKIt7S4QyurFNFYrpIxQSes9zmSwZq/IJ2c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778749877; c=relaxed/simple; bh=h8t0RsCGQ45lMuwGneXg6mx7nNaUep6CcUMGfKkiihE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EtbAgC4dJh8r7UmUNIUdS7q3eL+wCyZ5A0UHUUrtv/8leRxEddMsQrdoW7stDUdaQY7eUHnSasfWcnV3k287rKVq0+TZbiKvQZ0Qv4ncGNDZail6oCGozjfk57ObCRTocvanrnG3Y3Y0ykv1Y589L5puGh97UdiPLQOkQtExUa4= 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=hBmPazYM; 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="hBmPazYM" Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 64E96CsM012068; Thu, 14 May 2026 17:06:12 +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 4gGPVZ6qkTz2Msdr2; Thu, 14 May 2026 17:02:46 +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; Thu, 14 May 2026 17:06:10 +0800 From: Yi Sun To: , CC: , , , , Subject: [PATCH v3 1/2] lib: bitmap: add find_last_bit_from() and _find_last_bit_from() Date: Thu, 14 May 2026 17:06:06 +0800 Message-ID: <20260514090607.231387-2-yi.sun@unisoc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260514090607.231387-1-yi.sun@unisoc.com> References: <20260514090607.231387-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 64E96CsM012068 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=unisoc.com; s=default; t=1778749586; bh=pcrFpMNC0cE0UMbbrav/3tDEkp9valkHQyUUf9egYWE=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=hBmPazYMPA3dJL6jQlXgnhS9VWDpOmaMOcQ2irDtVodxsIr9ZeR9zgeCbf09B4jYT azdg6njtiAQTSHlgXX1ZhmSUCE6HSswe6xCzT5quBCftqY4ykwxqLbyWGKLDz5LbHy 2mj+C9YhSv+A0SdX6F/V+cKEOFOu48mBq4NOkezuqCdLYlennecsBAyMMtogmh+Uid wkYjHz9/51uJbLIoDqYoHuvpHHGH26ypi7qmaRn2CmYitq32NaTchzXXqCeMm4fYjU ZPZunQ20pJWSjQ0CECglsMNGOoNwQun0NMEK2dH0s9Kw2zC0m4gc+XeDZAX1utWFrP s53BvjdlADaUg== 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 | 33 +++++++++++++++++++++++++++++++++ lib/find_bit.c | 22 ++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/include/linux/find.h b/include/linux/find.h index 6c2be8ca615d..17f1db7b41fb 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_from(const unsigned long *addr, unsign= ed 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,37 @@ unsigned long find_last_bit(const unsigned long *addr,= unsigned long size) } #endif =20 +/** + * find_last_bit_from - 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_from() 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_from(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_from(addr, size, offset); +} + /** * 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..196b946dafff 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -237,6 +237,28 @@ unsigned long _find_last_bit(const unsigned long *addr= , unsigned long size) EXPORT_SYMBOL(_find_last_bit); #endif =20 +unsigned long _find_last_bit_from(const unsigned long *addr, unsigned long= size, + unsigned long offset) +{ + unsigned long val, idx, start_idx; + + if (unlikely(offset >=3D size)) + return size; + + start_idx =3D offset / BITS_PER_LONG; + idx =3D (size - 1) / BITS_PER_LONG; + val =3D addr[idx] & BITMAP_LAST_WORD_MASK(size); + + while (!val && idx > start_idx) + val =3D addr[--idx]; + + if (idx =3D=3D start_idx) + val &=3D BITMAP_FIRST_WORD_MASK(offset); + + return val ? idx * BITS_PER_LONG + __fls(val) : size; +} +EXPORT_SYMBOL(_find_last_bit_from); + 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 05:40:00 2026 Received: from SHSQR01.spreadtrum.com (mx1.unisoc.com [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 698863DFC81 for ; Thu, 14 May 2026 09:08:28 +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=1778749710; cv=none; b=JmXoPKlEoAZW3k4Q5/ezuZ/VwClKioFsphLmZM+NX6j/9K7H2OF2MTY66PKAc5CLRyUnBdOTtRt3JBa3f2mLlsAz6yb6A9Ks3+WWP/8EsBji3cG04ZuX1bfH4UQLwb0KTAkZjYSZiuvp95CFGLQigEr97629hza93Kmr/OP2qYc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778749710; c=relaxed/simple; bh=uAnCUNpwi/P3Iv3BzUDoBjorhq+Vxsjug8h01Dt4w+8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=UWcxLEuGIoVBxsqEObmVf8SiqLRYYkgFneOU1qOuQuy25qHVUorPLq5/tQlSUwb2+zHPhnIk2UtyxXiCMmotE7CTezkbjv+LhGTty4f/BUf48Zh4t8DruyhrFu57yfHs311h5zEAkz5//ml1gJ/qTUFqeidy1ERhydCf7KriLAU= 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=BASSahcr; 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="BASSahcr" Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 64E96Efc012294; Thu, 14 May 2026 17:06:14 +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 4gGPVb65rnz2Msdqx; Thu, 14 May 2026 17:02:47 +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; Thu, 14 May 2026 17:06:11 +0800 From: Yi Sun To: , CC: , , , , Subject: [PATCH v3 2/2] lib: bitmap: reduce the number of goto again in bitmap_find_next_zero_area_off() Date: Thu, 14 May 2026 17:06:07 +0800 Message-ID: <20260514090607.231387-3-yi.sun@unisoc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260514090607.231387-1-yi.sun@unisoc.com> References: <20260514090607.231387-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 64E96Efc012294 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=unisoc.com; s=default; t=1778749590; bh=ANLwQX9tCHeq2zTiX4PeFSwE509VuBzyXZ/74Js+d0Y=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=BASSahcrzgieiMQbARWq58iHrD8/8WcsNz4tIQ8Zo0tH6mh/NFK9HR/HyOWh3odxn gNG6KvxiR36vVeWfIj78d5fiNy2khbWrNYYjpI5MY5pW+COg/0aBZKsj57dJMgbef1 O0RR1kEkSb4Z90bQLB1y2ErSzpdzlr9ucny0RAnaS6EKRWZ4Qbyrot1N8jJz5Rxslq 3B2PZzbJIqDDB1VlTJ7oRGPneBP7nAf59jIbPpyCEIzjJH+NVngJDTbmkgljRAwnYK fOrOTiaoyd0+13KdMQGAhd8TnIZCiUD33FJFEdulMMDdw+QpBHHSe3u+1joJdCEzNR lzgheLH/vov0A== 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..9b589643f72a 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_from(map, end, index); if (i < end) { start =3D i + 1; goto again; --=20 2.34.1