Skip to content

Commit eb608fb

Browse files
heicarstMartin Schwidefsky
authored andcommitted
s390/exceptions: switch to relative exception table entries
This is the s390 port of 7062765 "x86, extable: Switch to relative exception table entries". Reduces the size of our exception tables by 50% on 64 bit builds. Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
1 parent 24996ed commit eb608fb

10 files changed

Lines changed: 132 additions & 33 deletions

File tree

‎arch/s390/include/asm/processor.h‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#ifndef __ASM_S390_PROCESSOR_H
1212
#define __ASM_S390_PROCESSOR_H
1313

14+
#ifndef __ASSEMBLY__
15+
1416
#include <linux/linkage.h>
1517
#include <linux/irqflags.h>
1618
#include <asm/cpu.h>
@@ -348,23 +350,6 @@ extern void (*s390_base_ext_handler_fn)(void);
348350

349351
#define ARCH_LOW_ADDRESS_LIMIT 0x7fffffffUL
350352

351-
/*
352-
* Helper macro for exception table entries
353-
*/
354-
#ifndef CONFIG_64BIT
355-
#define EX_TABLE(_fault,_target) \
356-
".section __ex_table,\"a\"\n" \
357-
" .align 4\n" \
358-
" .long " #_fault "," #_target "\n" \
359-
".previous\n"
360-
#else
361-
#define EX_TABLE(_fault,_target) \
362-
".section __ex_table,\"a\"\n" \
363-
" .align 8\n" \
364-
" .quad " #_fault "," #_target "\n" \
365-
".previous\n"
366-
#endif
367-
368353
extern int memcpy_real(void *, void *, size_t);
369354
extern void memcpy_absolute(void *, void *, size_t);
370355

@@ -375,4 +360,25 @@ extern void memcpy_absolute(void *, void *, size_t);
375360
memcpy_absolute(&(dest), &__tmp, sizeof(__tmp)); \
376361
}
377362

378-
#endif /* __ASM_S390_PROCESSOR_H */
363+
/*
364+
* Helper macro for exception table entries
365+
*/
366+
#define EX_TABLE(_fault, _target) \
367+
".section __ex_table,\"a\"\n" \
368+
".align 4\n" \
369+
".long (" #_fault ") - .\n" \
370+
".long (" #_target ") - .\n" \
371+
".previous\n"
372+
373+
#else /* __ASSEMBLY__ */
374+
375+
#define EX_TABLE(_fault, _target) \
376+
.section __ex_table,"a" ; \
377+
.align 4 ; \
378+
.long (_fault) - . ; \
379+
.long (_target) - . ; \
380+
.previous
381+
382+
#endif /* __ASSEMBLY__ */
383+
384+
#endif /* __ASM_S390_PROCESSOR_H */

‎arch/s390/include/asm/uaccess.h‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,22 @@ static inline int __range_ok(unsigned long addr, unsigned long size)
7676

7777
struct exception_table_entry
7878
{
79-
unsigned long insn, fixup;
79+
int insn, fixup;
8080
};
8181

82+
static inline unsigned long extable_insn(const struct exception_table_entry *x)
83+
{
84+
return (unsigned long)&x->insn + x->insn;
85+
}
86+
87+
static inline unsigned long extable_fixup(const struct exception_table_entry *x)
88+
{
89+
return (unsigned long)&x->fixup + x->fixup;
90+
}
91+
92+
#define ARCH_HAS_SORT_EXTABLE
93+
#define ARCH_HAS_SEARCH_EXTABLE
94+
8295
struct uaccess_ops {
8396
size_t (*copy_from_user)(size_t, const void __user *, void *);
8497
size_t (*copy_from_user_small)(size_t, const void __user *, void *);

‎arch/s390/kernel/early.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,14 @@ static __init void setup_topology(void)
255255

256256
static void early_pgm_check_handler(void)
257257
{
258-
unsigned long addr;
259258
const struct exception_table_entry *fixup;
259+
unsigned long addr;
260260

261261
addr = S390_lowcore.program_old_psw.addr;
262262
fixup = search_exception_tables(addr & PSW_ADDR_INSN);
263263
if (!fixup)
264264
disabled_wait(0);
265-
S390_lowcore.program_old_psw.addr = fixup->fixup | PSW_ADDR_AMODE;
265+
S390_lowcore.program_old_psw.addr = extable_fixup(fixup)|PSW_ADDR_AMODE;
266266
}
267267

268268
static noinline __init void setup_lowcore_early(void)

‎arch/s390/kernel/entry64.S‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <linux/init.h>
1212
#include <linux/linkage.h>
13+
#include <asm/processor.h>
1314
#include <asm/cache.h>
1415
#include <asm/errno.h>
1516
#include <asm/ptrace.h>
@@ -1008,9 +1009,7 @@ sie_fault:
10081009
.Lhost_id:
10091010
.quad 0
10101011

1011-
.section __ex_table,"a"
1012-
.quad sie_loop,sie_fault
1013-
.previous
1012+
EX_TABLE(sie_loop,sie_fault)
10141013
#endif
10151014

10161015
.section .rodata, "a"

‎arch/s390/kernel/kprobes.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ static int __kprobes kprobe_trap_handler(struct pt_regs *regs, int trapnr)
547547
*/
548548
entry = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
549549
if (entry) {
550-
regs->psw.addr = entry->fixup | PSW_ADDR_AMODE;
550+
regs->psw.addr = extable_fixup(entry) | PSW_ADDR_AMODE;
551551
return 1;
552552
}
553553

‎arch/s390/kernel/traps.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ static void __kprobes do_trap(struct pt_regs *regs,
322322
const struct exception_table_entry *fixup;
323323
fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
324324
if (fixup)
325-
regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
325+
regs->psw.addr = extable_fixup(fixup) | PSW_ADDR_AMODE;
326326
else {
327327
enum bug_trap_type btt;
328328

‎arch/s390/mm/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44

55
obj-y := init.o fault.o extmem.o mmap.o vmem.o pgtable.o maccess.o \
6-
page-states.o gup.o
6+
page-states.o gup.o extable.o
77
obj-$(CONFIG_CMM) += cmm.o
88
obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o
99
obj-$(CONFIG_DEBUG_SET_MODULE_RONX) += pageattr.o

‎arch/s390/mm/extable.c‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <linux/module.h>
2+
#include <linux/sort.h>
3+
#include <asm/uaccess.h>
4+
5+
/*
6+
* Search one exception table for an entry corresponding to the
7+
* given instruction address, and return the address of the entry,
8+
* or NULL if none is found.
9+
* We use a binary search, and thus we assume that the table is
10+
* already sorted.
11+
*/
12+
const struct exception_table_entry *
13+
search_extable(const struct exception_table_entry *first,
14+
const struct exception_table_entry *last,
15+
unsigned long value)
16+
{
17+
const struct exception_table_entry *mid;
18+
unsigned long addr;
19+
20+
while (first <= last) {
21+
mid = ((last - first) >> 1) + first;
22+
addr = extable_insn(mid);
23+
if (addr < value)
24+
first = mid + 1;
25+
else if (addr > value)
26+
last = mid - 1;
27+
else
28+
return mid;
29+
}
30+
return NULL;
31+
}
32+
33+
/*
34+
* The exception table needs to be sorted so that the binary
35+
* search that we use to find entries in it works properly.
36+
* This is used both for the kernel exception table and for
37+
* the exception tables of modules that get loaded.
38+
*
39+
*/
40+
static int cmp_ex(const void *a, const void *b)
41+
{
42+
const struct exception_table_entry *x = a, *y = b;
43+
44+
/* This compare is only valid after normalization. */
45+
return x->insn - y->insn;
46+
}
47+
48+
void sort_extable(struct exception_table_entry *start,
49+
struct exception_table_entry *finish)
50+
{
51+
struct exception_table_entry *p;
52+
int i;
53+
54+
/* Normalize entries to being relative to the start of the section */
55+
for (p = start, i = 0; p < finish; p++, i += 8)
56+
p->insn += i;
57+
sort(start, finish - start, sizeof(*start), cmp_ex, NULL);
58+
/* Denormalize all entries */
59+
for (p = start, i = 0; p < finish; p++, i += 8)
60+
p->insn -= i;
61+
}
62+
63+
#ifdef CONFIG_MODULES
64+
/*
65+
* If the exception table is sorted, any referring to the module init
66+
* will be at the beginning or the end.
67+
*/
68+
void trim_init_extable(struct module *m)
69+
{
70+
/* Trim the beginning */
71+
while (m->num_exentries &&
72+
within_module_init(extable_insn(&m->extable[0]), m)) {
73+
m->extable++;
74+
m->num_exentries--;
75+
}
76+
/* Trim the end */
77+
while (m->num_exentries &&
78+
within_module_init(extable_insn(&m->extable[m->num_exentries-1]), m))
79+
m->num_exentries--;
80+
}
81+
#endif /* CONFIG_MODULES */

‎arch/s390/mm/fault.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static noinline void do_no_context(struct pt_regs *regs)
163163
/* Are we prepared to handle this kernel fault? */
164164
fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
165165
if (fixup) {
166-
regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
166+
regs->psw.addr = extable_fixup(fixup) | PSW_ADDR_AMODE;
167167
return;
168168
}
169169

‎scripts/sortextable.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ typedef void (*table_sort_t)(char *, int);
161161
#define SORTEXTABLE_64
162162
#include "sortextable.h"
163163

164-
static int compare_x86_table(const void *a, const void *b)
164+
static int compare_relative_table(const void *a, const void *b)
165165
{
166166
int32_t av = (int32_t)r(a);
167167
int32_t bv = (int32_t)r(b);
@@ -173,7 +173,7 @@ static int compare_x86_table(const void *a, const void *b)
173173
return 0;
174174
}
175175

176-
static void sort_x86_table(char *extab_image, int image_size)
176+
static void sort_relative_table(char *extab_image, int image_size)
177177
{
178178
int i;
179179

@@ -188,7 +188,7 @@ static void sort_x86_table(char *extab_image, int image_size)
188188
i += 4;
189189
}
190190

191-
qsort(extab_image, image_size / 8, 8, compare_x86_table);
191+
qsort(extab_image, image_size / 8, 8, compare_relative_table);
192192

193193
/* Now denormalize. */
194194
i = 0;
@@ -245,9 +245,9 @@ do_file(char const *const fname)
245245
break;
246246
case EM_386:
247247
case EM_X86_64:
248-
custom_sort = sort_x86_table;
249-
break;
250248
case EM_S390:
249+
custom_sort = sort_relative_table;
250+
break;
251251
case EM_MIPS:
252252
break;
253253
} /* end switch */

0 commit comments

Comments
 (0)