From: XueBing Chen <chenxb_99091@126.com>
To: akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org, XueBing Chen <chenxb_99091@126.com>
Subject: [PATCH] lib/bsearch: add mutex protection for thread-safe binary search
Date: Thu, 16 Oct 2025 17:06:40 +0800 [thread overview]
Message-ID: <20251016090640.6331-1-chenxb_99091@126.com> (raw)
Replace the __inline_bsearch() wrapper with a full implementation
that includes mutex protection to ensure thread safety when
multiple threads call bsearch() concurrently.
The original implementation lacked synchronization, which could
lead to race conditions in multi-threaded environments when
accessing shared arrays or using non-atomic comparison functions.
Signed-off-by: XueBing Chen <chenxb_99091@126.com>
---
lib/bsearch.c | 29 ++++++++++++++++++++++++++---
1 file changed, 26 insertions(+), 3 deletions(-)
diff --git a/lib/bsearch.c b/lib/bsearch.c
index bf86aa66f..9a5a2e949 100644
--- a/lib/bsearch.c
+++ b/lib/bsearch.c
@@ -1,9 +1,12 @@
-// SPDX-License-Identifier: GPL-2.0-only
/*
* A generic implementation of binary search for the Linux kernel
*
* Copyright (C) 2008-2009 Ksplice, Inc.
* Author: Tim Abbott <tabbott@ksplice.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; version 2.
*/
#include <linux/export.h>
@@ -28,9 +31,29 @@
* the key and elements in the array are of the same type, you can use
* the same comparison function for both sort() and bsearch().
*/
-void *bsearch(const void *key, const void *base, size_t num, size_t size, cmp_func_t cmp)
+DEFINE_MUTEX(cmp_mutex);
+void *bsearch(const void *key, const void *base, size_t num, size_t size,
+ int (*cmp)(const void *key, const void *elt))
{
- return __inline_bsearch(key, base, num, size, cmp);
+ const char *pivot;
+ int result;
+
+ while (num > 0) {
+ pivot = base + (num >> 1) * size;
+ mutex_lock(&cmp_mutex);
+ result = cmp(key, pivot);
+ mutex_unlock(&cmp_mutex);
+ if (result == 0)
+ return (void *)pivot;
+
+ if (result > 0) {
+ base = pivot + size;
+ num--;
+ }
+ num >>= 1;
+ }
+
+ return NULL;
}
EXPORT_SYMBOL(bsearch);
NOKPROBE_SYMBOL(bsearch);
--
2.17.1
next reply other threads:[~2025-10-16 9:17 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-16 9:06 XueBing Chen [this message]
2025-10-16 9:42 ` Kuan-Wei Chiu
2025-10-17 7:28 ` kernel test robot
2025-10-21 4:47 ` kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251016090640.6331-1-chenxb_99091@126.com \
--to=chenxb_99091@126.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®