Skip to content

Commit ad8aa0a

Browse files
thomasmeyhnaz
authored andcommitted
lib/extable.c: use bsearch() library function in search_extable()
Signed-off-by: Thomas Meyer <[email protected]> Cc: Rasmus Villemoes <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 7441e76 commit ad8aa0a

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

include/linux/extable.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
#define _LINUX_EXTABLE_H
33

44
#include <linux/stddef.h> /* for NULL */
5+
#include <linux/types.h>
56

67
struct module;
78
struct exception_table_entry;
89

910
const struct exception_table_entry *
1011
search_extable(const struct exception_table_entry *first,
11-
const struct exception_table_entry *last,
12+
const size_t num,
1213
unsigned long value);
1314
void sort_extable(struct exception_table_entry *start,
1415
struct exception_table_entry *finish);

kernel/extable.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const struct exception_table_entry *search_exception_tables(unsigned long addr)
5555
{
5656
const struct exception_table_entry *e;
5757

58-
e = search_extable(__start___ex_table, __stop___ex_table-1, addr);
58+
e = search_extable(__start___ex_table, __stop___ex_table - __start___ex_table, addr);
5959
if (!e)
6060
e = search_module_extables(addr);
6161
return e;

kernel/module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4201,7 +4201,7 @@ const struct exception_table_entry *search_module_extables(unsigned long addr)
42014201
goto out;
42024202

42034203
e = search_extable(mod->extable,
4204-
mod->extable + mod->num_exentries - 1,
4204+
mod->num_exentries,
42054205
addr);
42064206
out:
42074207
preempt_enable();

lib/extable.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* 2 of the License, or (at your option) any later version.
1010
*/
1111

12+
#include <linux/bsearch.h>
1213
#include <linux/module.h>
1314
#include <linux/init.h>
1415
#include <linux/sort.h>
@@ -51,7 +52,7 @@ static void swap_ex(void *a, void *b, int size)
5152
* This is used both for the kernel exception table and for
5253
* the exception tables of modules that get loaded.
5354
*/
54-
static int cmp_ex(const void *a, const void *b)
55+
static int cmp_ex_sort(const void *a, const void *b)
5556
{
5657
const struct exception_table_entry *x = a, *y = b;
5758

@@ -67,7 +68,7 @@ void sort_extable(struct exception_table_entry *start,
6768
struct exception_table_entry *finish)
6869
{
6970
sort(start, finish - start, sizeof(struct exception_table_entry),
70-
cmp_ex, swap_ex);
71+
cmp_ex_sort, swap_ex);
7172
}
7273

7374
#ifdef CONFIG_MODULES
@@ -93,6 +94,20 @@ void trim_init_extable(struct module *m)
9394
#endif /* !ARCH_HAS_SORT_EXTABLE */
9495

9596
#ifndef ARCH_HAS_SEARCH_EXTABLE
97+
98+
static int cmp_ex_search(const void *key, const void *elt)
99+
{
100+
const struct exception_table_entry * _elt = elt;
101+
unsigned long k = *(unsigned long*) key;
102+
103+
/* avoid overflow */
104+
if (k > ex_to_insn(_elt))
105+
return 1;
106+
if (k < ex_to_insn(_elt))
107+
return -1;
108+
return 0;
109+
}
110+
96111
/*
97112
* Search one exception table for an entry corresponding to the
98113
* given instruction address, and return the address of the entry,
@@ -102,24 +117,9 @@ void trim_init_extable(struct module *m)
102117
*/
103118
const struct exception_table_entry *
104119
search_extable(const struct exception_table_entry *first,
105-
const struct exception_table_entry *last,
120+
const size_t num,
106121
unsigned long value)
107122
{
108-
while (first <= last) {
109-
const struct exception_table_entry *mid;
110-
111-
mid = ((last - first) >> 1) + first;
112-
/*
113-
* careful, the distance between value and insn
114-
* can be larger than MAX_LONG:
115-
*/
116-
if (ex_to_insn(mid) < value)
117-
first = mid + 1;
118-
else if (ex_to_insn(mid) > value)
119-
last = mid - 1;
120-
else
121-
return mid;
122-
}
123-
return NULL;
123+
return bsearch(&value, first, num, sizeof(struct exception_table_entry), cmp_ex_search);
124124
}
125125
#endif

0 commit comments

Comments
 (0)