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 19ED83A6F07 for ; Thu, 14 May 2026 03:57:43 +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=1778731067; cv=none; b=m0tzFKyRO9TtQNGIDCtM+pXmc6NgPPju1N0i+CyW6p981TQI23NtqovDOI3bsl50vfGDITYcdzrXEzOIw2+XwQ3MX5MvVsLb/moXgU3dilQnVwhb4Q5IEd88yIY0R+izU7mrsNPHSKn6RY5irwYrSEhmc4EZnizzzZdvPVdOwdo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778731067; c=relaxed/simple; bh=3uZL6H1fMPIuwGltJ9G+0yFQChuSw9F5nxnNdm2q9nc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=SK1bVlXaJkcRZRFt7Q6uQu0Xjsom88njumqg8mTevRR0dB+jJLsya/vH+akG1jR5fXCCcgQEJ4NiASk8j+vQRpOwaA61OEBBipohmj+a1eHLoraWglWDRzZTgErpcsfH3X0j87T6E7OVO+/yLsbRa+BvVueiMirgeCYZ98c6JB8= 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=w0HMhWCq; 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="w0HMhWCq" Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 64E3unHM044179; Thu, 14 May 2026 11:56:49 +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 4gGGdb5Gctz2K66XZ; Thu, 14 May 2026 11:53:23 +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 11:56:46 +0800 From: Yi Sun To: , CC: , , , , Subject: [PATCH v2 1/1] lib: bitmap: reduce the number of goto again in bitmap_find_next_zero_area_off() Date: Thu, 14 May 2026 11:56:44 +0800 Message-ID: <20260514035644.4118050-2-yi.sun@unisoc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260514035644.4118050-1-yi.sun@unisoc.com> References: <20260514035644.4118050-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: SHCAS01.spreadtrum.com (10.0.1.201) To BJMBX02.spreadtrum.com (10.0.64.8) X-MAIL:SHSQR01.spreadtrum.com 64E3unHM044179 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=unisoc.com; s=default; t=1778731017; bh=p8plaB1ZSuCfc0X5/0jPhZc9lMUBSMtXfhHKdU1qm5Q=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=w0HMhWCqCBD2ZgHF8ua+aJxCFFYJKv23ANNB5ZW+60R5tgBVHcuMqxwup99CWUEel bj2TuWN6E5V22qmzxDZyahYb20uOLPOa32qdtWo4LuGZrLYaYIbcz+0JH3rdjshfpx yg63zQC5GwZmIIhcJ2oILdngmQ57TM1jslITvAl/runqXvb0b5tDZlMNvOHBGjM1Qx T7gexU2emmrrWVI2S6iWXOlSa+xbCueYsRZ+LdUh3Al7gY0BZyD/kAD0oblCxgFZ0x mBPP/wzm6Nkbre5g6iT4Or4AzS5Y5aQbyV94H+O6Oyv0krq5NjulQzrXzOVAUzh9TN 3v2TUK+DRWOCg== 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