From mboxrd@z Thu Jan 1 00:00:00 1970 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 6F7E738BF75 for ; Mon, 1 Jun 2026 09:43: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=1780307010; cv=none; b=KLNXoErR5M0Yhf792l1LNNpMqAFTSzABWMPhZrT1TGLRaRYwdMTJy/YlkCakoPzeHsrfofmT1mtwvlO0AsyccJMA7SZind9OVBgFzUc7Mh038/8CgfraiUoechaJLa+AWvMrQ9T/qIeTEYfNch16gcU8UyM2sssCjGgFhti2yHI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780307010; c=relaxed/simple; bh=3uZL6H1fMPIuwGltJ9G+0yFQChuSw9F5nxnNdm2q9nc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=d3oZSOSeCWxC9J/1C6EIRN8/0E8rbrvkiPFaspUtK9IuRURtYA074M0p2l+FOJfAt7J60nJa3MUSYv0+TfOWgyh+E1zealhgAlBVSJi4WB//r9zHA31MuDJrXGLN5wQ2FSaYA1p1vlHd0a3QsaSJDPf55KAg5DE5gjs6m7M0WvM= 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=iHb41+Ck; 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="iHb41+Ck" Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 6519gdEj008893; Mon, 1 Jun 2026 17:42:39 +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 4gTTRd2MZyz2MH0Ws; Mon, 1 Jun 2026 17:38:37 +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, 1 Jun 2026 17:42:38 +0800 From: Yi Sun To: , CC: , , , , , Subject: [PATCH v4 1/2] lib: bitmap: reduce the number of goto again in bitmap_find_next_zero_area_off() Date: Mon, 1 Jun 2026 17:42:33 +0800 Message-ID: <20260601094234.103863-2-yi.sun@unisoc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260601094234.103863-1-yi.sun@unisoc.com> References: <20260601094234.103863-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 6519gdEj008893 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=unisoc.com; s=default; t=1780306968; bh=p8plaB1ZSuCfc0X5/0jPhZc9lMUBSMtXfhHKdU1qm5Q=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=iHb41+CktTEXYlCtmz55BRatH4QNAwPvqhXZmK4g30AO6fO6wU4lFHN2XzYgpNlNl Yhb4T/ru4oBxMoH7wiQjNuiCs9t5ostCVppFN1treRzawS9/pxUH6ul0hX/nznHjbl RsukZzrYCWc514TqS1FqJViYI22OxzYiSUGW1Sy0IwyujWjxO5ej0tkITo8r8dP8oP RyB3lnlrZXj31ozGQcUsls7p6pT81c47MXZ7B1+VtJr+a2A0W7E4RuDPNsqDMdJ88x Tcmdcv1dfj4s14L0jnPfZvANZsPFMLr/PGY2v99wlPsJk7nXO9JRIr+dCn/7Xf0qtj tl2rrjaTjFLEQ== 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 --- lib/bitmap.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/bitmap.c b/lib/bitmap.c index b9bfa157e095..7d0fbcbe6973 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -432,7 +432,7 @@ unsigned long bitmap_find_next_zero_area_off(unsigned long *map, unsigned long align_mask, unsigned long align_offset) { - unsigned long index, end, i; + unsigned long index, end, i, index_bits_align, index_idx; again: index = find_next_zero_bit(map, size, start); @@ -442,8 +442,12 @@ unsigned long bitmap_find_next_zero_area_off(unsigned long *map, end = index + nr; if (end > size) return end; - i = find_next_bit(map, end, index); - if (i < end) { + + index_bits_align = round_down(index, BITS_PER_LONG); + index_idx = index / BITS_PER_LONG; + + i = find_last_bit(map + index_idx, end - index_bits_align) + index_bits_align; + if (i > index && i < end) { start = i + 1; goto again; } -- 2.34.1