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 84F4423BD05 for ; Thu, 18 Jun 2026 01:53:23 +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=1781747605; cv=none; b=IrHfEL7XoVWpiNQP/qsYycyZ/gATjJI21rSJ7epUJ3CkSG9ZYkLzI6b/4vq9AOukonHHb5a+eMjIioTG4PTyzGTsR295JKGgkgcdnzEumG62KoKYLi08gd/5KtWPXi3ONb6JXtvTY8R8yuhJVwQ7fdg8gMxV28xiMaBTkVyP0sE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781747605; c=relaxed/simple; bh=/0Hrnm+E9R5LB1R2JyPcZcmy91fCk2HdfxoWwgKjYCo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=oItnL1bUxeFYeFGLUS0xkOzNRY1FNZqMkmu28rw6pW6vtvQdPVUqjTztzSCpsiegviKVQUbNSJEj3FPo8kwKCGX+2RTTkMmupI8iZY5nTKZdFVYli682HTJ+dNb2EReTZXbHFc4sw0k8aeBzPumca1IXk8skPTmzdQVRqlr+KAo= 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=DwOXdbhM; 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="DwOXdbhM" Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTPS id 65I1r0xN065676 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Thu, 18 Jun 2026 09:53:00 +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 4ggkCB5ZdKz2Mp18G; Thu, 18 Jun 2026 09:48:22 +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; Thu, 18 Jun 2026 09:52:57 +0800 From: Yi Sun To: , CC: , <279644543@qq.com>, , , , , , , , , , , , , , , , Subject: [PATCH v5 2/2] lib: bitmap: optimize bitmap_find_next_zero_area_off() Date: Thu, 18 Jun 2026 09:52:52 +0800 Message-ID: <20260618015252.3601554-3-yi.sun@unisoc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260618015252.3601554-1-yi.sun@unisoc.com> References: <20260618015252.3601554-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 65I1r0xN065676 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=unisoc.com; s=default; t=1781747600; bh=fSwdtoxHcwjJe+ExDxZbxNQdwPPJDfXaw/AxfudV3YA=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=DwOXdbhMr3YMKED1847xMp0nJq6/sEq9yGTNfYv7+YDDWk89BBQIicZNMs9FWac8e iMTY+J6cIYeFbLieMHiMziCvWjaXksoA9t/MjeIUOmozKDtaezLe02AYuz7i0OcShT 5QAPFTep+N/xRlZ1VRJxArf/srcjQSjs6OBVOX1AsaagFX7s9BvfYi2s7rFV2tRqzD pMgGu12PfbFTpHxuy1HdYgCDqX4UUPWvGNSdHXnvZQxUEDS1BNH/ysjjjVZo/guBV/ JieYCDsnItPH731lPcro7UC0kFLLSR7LMlkh5NlwzIu22LWl+YvQFAmiyTxH7hVYM2 2E7pWd5nkaENA== 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) -> goto again -> find_next_zero_bit(start = 5, find bit 6) -> find_next_bit(find bit 9) -> goto again -> 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) -> goto again -> 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 Signed-off-by: Yi Sun --- lib/bitmap.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/lib/bitmap.c b/lib/bitmap.c index b9bfa157e095..a2c4bd22d875 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -432,22 +432,29 @@ 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) { + unsigned long find_idx, end, i, find_word_idx, find_word_off; + + for (;;) { + find_idx = find_next_zero_bit(map, size, start); + + /* Align allocation */ + find_idx = __ALIGN_MASK(find_idx + align_offset, + align_mask) - align_offset; + + end = find_idx + nr; + if (end > size) + return end; + + find_word_idx = find_idx / BITS_PER_LONG; + find_word_off = find_word_idx * BITS_PER_LONG; + + i = find_last_bit(map + find_word_idx, + end - find_word_off) + find_word_off; + if (i >= end || i < find_idx) + return find_idx; + start = i + 1; - goto again; } - return index; } EXPORT_SYMBOL(bitmap_find_next_zero_area_off); -- 2.34.1