From mboxrd@z Thu Jan 1 00:00:00 1970 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 5BD8F224AF2 for ; Mon, 22 Jun 2026 03:01:15 +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=1782097278; cv=none; b=L/7zE6Dn4E+x076RC80shE1yemOpaEwWrWVzB4eg0mnguKW3ZLOhuf8EmMALVFG9p49fkE5bFrS+KEDzHKV3FdALRFqhov5bMmaNQ/y/FLiCLnx0ib+F3l8NsRuLPlv3yftBRs1CRQ+L/O5OaBQ8Y8TJ2bCYDxKtkH/9HAvYPcw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782097278; c=relaxed/simple; bh=25JQpagMtFjmMhL6puykQv1OyYQDp/gF2B4SLONJTQc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Lvdxn2ucuCIpZpTjrHakgdSIrXWzs37Rq19t6z8NeecluimC86SOAxcn25Fh6PQYvy0iYvmChHaBFMEHiRoQbquO42U0dT+0MA0pIybYNUCEz4HznAxnYL7xLP+DfaO6aQ3fw99s6qLIJFuTnSHU7CQP3k6HG7RM++ITNiRkfr4= 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=OLW8XuoG; 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="OLW8XuoG" Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTPS id 65M30gIV067591 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Mon, 22 Jun 2026 11:00:42 +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 4gkCWK13VYz2NsPCW; Mon, 22 Jun 2026 10:55:57 +0800 (CST) Received: from tj10379pcu.spreadtrum.com (10.5.32.15) by BJMBX02.spreadtrum.com (10.0.64.8) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Mon, 22 Jun 2026 11:00:39 +0800 From: Yi Sun To: CC: , <279644543@qq.com>, , , , , , , , , , , , Subject: [PATCH v7 2/2] lib: bitmap: optimize bitmap_find_next_zero_area_off() Date: Mon, 22 Jun 2026 11:00:36 +0800 Message-ID: <20260622030036.1744080-3-yi.sun@unisoc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260622030036.1744080-1-yi.sun@unisoc.com> References: <20260622030036.1744080-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: 8bit Content-Type: text/plain X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX02.spreadtrum.com (10.0.64.8) X-MAIL:SHSQR01.spreadtrum.com 65M30gIV067591 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=unisoc.com; s=default; t=1782097267; bh=Cr5tzJsckZnfP4fznZm/POofxhCsLMWwxxI/37UtKV4=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=OLW8XuoGm9H4WLlpyIyWgbs3R9dQVECfkVn30k87fRXFHOO9P7ogeel0jlbc4lw4Q d2sn+m5VWj//SRbWrFQ6O9QT+ait7abJqtoNhsVx/SDMhPCp2qwcdOlah/ToXpCxxH Nv2V7FBy41x6blGAhyJ+fih+11GWXhWtiRQYjp2FhW3t/WQNMtjKCJi/qCeGj8HyHa YsKkZA8xvQcHvWwetlVf4IGf4lz10SJ7qJ+pw1A5Hkma4cKGZ/Jno31XxTkxljYen0 HkwpS8dNghruE0dWm5c9X7gpdBs2O4JVINlEloMY2CUX2D51oiAOvCQSrKOtSkvzAx bbiOnunjldg1w== 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 repeated calls. An example of a bitmap: Bits 0-3: cleared(4 bits) Bits 4-5: set (2 bits) Bits 6-8: cleared(4 bits) Bits 9-10: set (2 bits) Bits 11-20: cleared(10 bits) The goal is to find a 10-bit free region. The old code logic is as follows: find_next_zero_bit(start = 0, find bit 0) -> find_next_bit(find bit 4) -> next loop -> find_next_zero_bit(start = 5, find bit 6) -> find_next_bit(find bit 9) -> next loop -> find_next_zero_bit(start = 10, find bit 11) -> success The new code logic is as follows: find_next_zero_bit(start = 0, find bit 0) -> find_last_bit(find bit 9) -> next loop -> find_next_zero_bit(start = 10, find bit 11) -> success Performance test results on my hardware(use lib/find_bit_benchmark.c): before after change p-value dense 1211 688 -43.2% 8.3e-11 sparse 13.3 13.4 0.8% 0.27 Co-developed-by: Yury Norov Signed-off-by: Yury Norov Signed-off-by: Yi Sun --- lib/bitmap.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/bitmap.c b/lib/bitmap.c index b9bfa157e095..a00a71f2b7bb 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -432,22 +432,23 @@ unsigned long bitmap_find_next_zero_area_off(unsigned long *map, unsigned long align_mask, unsigned long align_offset) { - unsigned long index, end, i; -again: - index = find_next_zero_bit(map, size, start); - - /* Align allocation */ - index = __ALIGN_MASK(index + align_offset, align_mask) - align_offset; - - end = index + nr; - if (end > size) - return end; - i = find_next_bit(map, end, index); - if (i < end) { - start = i + 1; - goto again; + unsigned long end, i, off; + + for_each_clear_bit_from(start, map, size) { + start = __ALIGN_MASK(start + align_offset, align_mask) - align_offset; + end = start + nr; + if (end > size) + break; + + off = round_down(start, BITS_PER_LONG); + i = find_last_bit(map + start / BITS_PER_LONG, end - off) + off; + if (i >= end || i < start) + return start; + + start = i; } - return index; + + return size; } EXPORT_SYMBOL(bitmap_find_next_zero_area_off); -- 2.34.1