Commit a94c33dd authored by Thomas Meyer's avatar Thomas Meyer Committed by Linus Torvalds

lib/extable.c: use bsearch() library function in search_extable()

[thomas@m3y3r.de: v3: fix arch specific implementations]
  Link: http://lkml.kernel.org/r/1497890858.12931.7.camel@m3y3r.deSigned-off-by: default avatarThomas Meyer <thomas@m3y3r.de>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 12e8fd6f
...@@ -317,7 +317,8 @@ const struct exception_table_entry *search_module_dbetables(unsigned long addr) ...@@ -317,7 +317,8 @@ const struct exception_table_entry *search_module_dbetables(unsigned long addr)
spin_lock_irqsave(&dbe_lock, flags); spin_lock_irqsave(&dbe_lock, flags);
list_for_each_entry(dbe, &dbe_list, dbe_list) { list_for_each_entry(dbe, &dbe_list, dbe_list) {
e = search_extable(dbe->dbe_start, dbe->dbe_end - 1, addr); e = search_extable(dbe->dbe_start,
dbe->dbe_end - dbe->dbe_start, addr);
if (e) if (e)
break; break;
} }
......
...@@ -429,7 +429,8 @@ static const struct exception_table_entry *search_dbe_tables(unsigned long addr) ...@@ -429,7 +429,8 @@ static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
{ {
const struct exception_table_entry *e; const struct exception_table_entry *e;
e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr); e = search_extable(__start___dbe_table,
__stop___dbe_table - __start___dbe_table, addr);
if (!e) if (!e)
e = search_module_dbetables(addr); e = search_module_dbetables(addr);
return e; return e;
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
* License. See the file "COPYING" in the main directory of this archive * License. See the file "COPYING" in the main directory of this archive
* for more details. * for more details.
*/ */
#include <linux/bsearch.h>
#include <linux/rwsem.h> #include <linux/rwsem.h>
#include <linux/extable.h> #include <linux/extable.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
...@@ -40,10 +41,23 @@ static const struct exception_table_entry *check_exception_ranges(unsigned long ...@@ -40,10 +41,23 @@ static const struct exception_table_entry *check_exception_ranges(unsigned long
return NULL; return NULL;
} }
static int cmp_ex_search(const void *key, const void *elt)
{
const struct exception_table_entry *_elt = elt;
unsigned long _key = *(unsigned long *)key;
/* avoid overflow */
if (_key > _elt->insn)
return 1;
if (_key < _elt->insn)
return -1;
return 0;
}
/* Simple binary search */ /* Simple binary search */
const struct exception_table_entry * const struct exception_table_entry *
search_extable(const struct exception_table_entry *first, search_extable(const struct exception_table_entry *base,
const struct exception_table_entry *last, const size_t num,
unsigned long value) unsigned long value)
{ {
const struct exception_table_entry *mid; const struct exception_table_entry *mid;
...@@ -52,20 +66,8 @@ search_extable(const struct exception_table_entry *first, ...@@ -52,20 +66,8 @@ search_extable(const struct exception_table_entry *first,
if (mid) if (mid)
return mid; return mid;
while (first <= last) { return bsearch(&value, base, num,
long diff; sizeof(struct exception_table_entry), cmp_ex_search);
mid = (last - first) / 2 + first;
diff = mid->insn - value;
if (diff == 0)
return mid;
else if (diff < 0)
first = mid+1;
else
last = mid-1;
}
return NULL;
} }
int fixup_exception(struct pt_regs *regs) int fixup_exception(struct pt_regs *regs)
......
...@@ -13,11 +13,11 @@ void sort_extable(struct exception_table_entry *start, ...@@ -13,11 +13,11 @@ void sort_extable(struct exception_table_entry *start,
/* Caller knows they are in a range if ret->fixup == 0 */ /* Caller knows they are in a range if ret->fixup == 0 */
const struct exception_table_entry * const struct exception_table_entry *
search_extable(const struct exception_table_entry *start, search_extable(const struct exception_table_entry *base,
const struct exception_table_entry *last, const size_t num,
unsigned long value) unsigned long value)
{ {
const struct exception_table_entry *walk; int i;
/* Single insn entries are encoded as: /* Single insn entries are encoded as:
* word 1: insn address * word 1: insn address
...@@ -37,30 +37,30 @@ search_extable(const struct exception_table_entry *start, ...@@ -37,30 +37,30 @@ search_extable(const struct exception_table_entry *start,
*/ */
/* 1. Try to find an exact match. */ /* 1. Try to find an exact match. */
for (walk = start; walk <= last; walk++) { for (i = 0; i < num; i++) {
if (walk->fixup == 0) { if (base[i].fixup == 0) {
/* A range entry, skip both parts. */ /* A range entry, skip both parts. */
walk++; i++;
continue; continue;
} }
/* A deleted entry; see trim_init_extable */ /* A deleted entry; see trim_init_extable */
if (walk->fixup == -1) if (base[i].fixup == -1)
continue; continue;
if (walk->insn == value) if (base[i].insn == value)
return walk; return &base[i];
} }
/* 2. Try to find a range match. */ /* 2. Try to find a range match. */
for (walk = start; walk <= (last - 1); walk++) { for (i = 0; i < (num - 1); i++) {
if (walk->fixup) if (base[i].fixup)
continue; continue;
if (walk[0].insn <= value && walk[1].insn > value) if (base[i].insn <= value && base[i + 1].insn > value)
return walk; return &base[i];
walk++; i++;
} }
return NULL; return NULL;
......
...@@ -2,13 +2,14 @@ ...@@ -2,13 +2,14 @@
#define _LINUX_EXTABLE_H #define _LINUX_EXTABLE_H
#include <linux/stddef.h> /* for NULL */ #include <linux/stddef.h> /* for NULL */
#include <linux/types.h>
struct module; struct module;
struct exception_table_entry; struct exception_table_entry;
const struct exception_table_entry * const struct exception_table_entry *
search_extable(const struct exception_table_entry *first, search_extable(const struct exception_table_entry *base,
const struct exception_table_entry *last, const size_t num,
unsigned long value); unsigned long value);
void sort_extable(struct exception_table_entry *start, void sort_extable(struct exception_table_entry *start,
struct exception_table_entry *finish); struct exception_table_entry *finish);
......
...@@ -55,7 +55,8 @@ const struct exception_table_entry *search_exception_tables(unsigned long addr) ...@@ -55,7 +55,8 @@ const struct exception_table_entry *search_exception_tables(unsigned long addr)
{ {
const struct exception_table_entry *e; const struct exception_table_entry *e;
e = search_extable(__start___ex_table, __stop___ex_table-1, addr); e = search_extable(__start___ex_table,
__stop___ex_table - __start___ex_table, addr);
if (!e) if (!e)
e = search_module_extables(addr); e = search_module_extables(addr);
return e; return e;
......
...@@ -4196,7 +4196,7 @@ const struct exception_table_entry *search_module_extables(unsigned long addr) ...@@ -4196,7 +4196,7 @@ const struct exception_table_entry *search_module_extables(unsigned long addr)
goto out; goto out;
e = search_extable(mod->extable, e = search_extable(mod->extable,
mod->extable + mod->num_exentries - 1, mod->num_exentries,
addr); addr);
out: out:
preempt_enable(); preempt_enable();
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
* 2 of the License, or (at your option) any later version. * 2 of the License, or (at your option) any later version.
*/ */
#include <linux/bsearch.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/sort.h> #include <linux/sort.h>
...@@ -51,7 +52,7 @@ static void swap_ex(void *a, void *b, int size) ...@@ -51,7 +52,7 @@ static void swap_ex(void *a, void *b, int size)
* This is used both for the kernel exception table and for * This is used both for the kernel exception table and for
* the exception tables of modules that get loaded. * the exception tables of modules that get loaded.
*/ */
static int cmp_ex(const void *a, const void *b) static int cmp_ex_sort(const void *a, const void *b)
{ {
const struct exception_table_entry *x = a, *y = b; const struct exception_table_entry *x = a, *y = b;
...@@ -67,7 +68,7 @@ void sort_extable(struct exception_table_entry *start, ...@@ -67,7 +68,7 @@ void sort_extable(struct exception_table_entry *start,
struct exception_table_entry *finish) struct exception_table_entry *finish)
{ {
sort(start, finish - start, sizeof(struct exception_table_entry), sort(start, finish - start, sizeof(struct exception_table_entry),
cmp_ex, swap_ex); cmp_ex_sort, swap_ex);
} }
#ifdef CONFIG_MODULES #ifdef CONFIG_MODULES
...@@ -93,6 +94,20 @@ void trim_init_extable(struct module *m) ...@@ -93,6 +94,20 @@ void trim_init_extable(struct module *m)
#endif /* !ARCH_HAS_SORT_EXTABLE */ #endif /* !ARCH_HAS_SORT_EXTABLE */
#ifndef ARCH_HAS_SEARCH_EXTABLE #ifndef ARCH_HAS_SEARCH_EXTABLE
static int cmp_ex_search(const void *key, const void *elt)
{
const struct exception_table_entry *_elt = elt;
unsigned long _key = *(unsigned long *)key;
/* avoid overflow */
if (_key > ex_to_insn(_elt))
return 1;
if (_key < ex_to_insn(_elt))
return -1;
return 0;
}
/* /*
* Search one exception table for an entry corresponding to the * Search one exception table for an entry corresponding to the
* given instruction address, and return the address of the entry, * given instruction address, and return the address of the entry,
...@@ -101,25 +116,11 @@ void trim_init_extable(struct module *m) ...@@ -101,25 +116,11 @@ void trim_init_extable(struct module *m)
* already sorted. * already sorted.
*/ */
const struct exception_table_entry * const struct exception_table_entry *
search_extable(const struct exception_table_entry *first, search_extable(const struct exception_table_entry *base,
const struct exception_table_entry *last, const size_t num,
unsigned long value) unsigned long value)
{ {
while (first <= last) { return bsearch(&value, base, num,
const struct exception_table_entry *mid; sizeof(struct exception_table_entry), cmp_ex_search);
mid = ((last - first) >> 1) + first;
/*
* careful, the distance between value and insn
* can be larger than MAX_LONG:
*/
if (ex_to_insn(mid) < value)
first = mid + 1;
else if (ex_to_insn(mid) > value)
last = mid - 1;
else
return mid;
}
return NULL;
} }
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment