modpost: work around unaligned data access
cover modpost: work around unaligned data access
The latest binutils stopped aligning section data in relocatable ELF. It saves small number of bytes that were previously inserted between sections. However, modpost crashes due to unaligned access:
https://sourceware.org/bugzilla/show_bug.cgi?id=32435
https://sourceware.org/bugzilla/show_bug.cgi?id=32493
Similar to kernel space, unaligned data access in userspace can be problematic on some architectures.
Simple example on ARM:
$ CC=arm-linux-gnueabihf-gcc
$ echo 'int foo(int *p) { return *p + *(p + 1); }' | ${CC} -O2 -x c - -c -o foo.o
$ echo 'int foo(int *p); int main(void) { char str[16] = "helloworld"; return foo((int *)(str + 1)); }' | ${CC} -x c - -c -o main.o
$ ${CC} -static -o unalign-test main.o foo.o
$ qemu-armhf unalign-test
qemu: uncaught target signal 7 (Bus error) - core dumped
Bus error (core dumped)In this case, the 'ldrd' instruction causes a bus error due to an unaligned access. If the -O2 flag is omitted, this issue does not occur.
To avoid potential unaligned access, use the get_unaligned() approach as seen in the kernel space.
Masahiro Yamada (3):
modpost: fix the missed iteration for the max bit in do_input()
modpost: refactor do_vmbus_entry()
modpost: work around unaligned data access error
scripts/mod/file2alias.c | 36 +++++++++++++++++------------------- scripts/mod/modpost.c | 24 ++++++++++++------------ scripts/mod/modpost.h | 14 ++++++++++++++ 3 files changed, 43 insertions(+), 31 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/3 modpost: fix the missed iteration for the max bit in do_input()
This loop should iterate over the range from 'min' to 'max' inclusively. The last interation is missed.
---
scripts/mod/file2alias.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/mod/file2alias.c +1 -1
@@ -656,7 +656,7 @@ static void do_input(char *alias,for (i = min /BITS_PER_LONG ; i < max /BITS_PER_LONG +1 ; i++) arr[i] =TO_NATIVE (arr[i]); -for (i = min; i < max; i++) +for (i = min; i <= max; i++)if (arr[i /BITS_PER_LONG ] & (1ULL << (i%BITS_PER_LONG )))sprintf (alias +strlen (alias),"%X,*" , i); }
Signature · 1 line
2.43.0
PATCH 2/3 modpost: refactor do_vmbus_entry()
In reply to Masahiro YamadaOptimize the size of guid_name[], as it only requires 1 additional byte for '\0' instead of 2.
Simplify the loop by incrementing the iterator by 1 instead of 2.
Remove the unnecessary TO_NATIVE() call, as the guid is represented as a byte stream.
---
scripts/mod/file2alias.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
scripts/mod/file2alias.c +3 -5
@@ -812,15 +812,13 @@ static void do_virtio_entry(struct module *mod, void *symval)* Each byte of the guid will be represented by two hex characters * in the name. */ -static void do_vmbus_entry (struct module *mod,void *symval) { -int i;DEF_FIELD_ADDR (symval, hv_vmbus_device_id, guid); -char guid_name[(sizeof (*guid) +1 ) *2 ]; +char guid_name[sizeof (*guid) *2 +1 ]; -for (i =0 ; i < (sizeof (*guid) *2 ); i +=2 ) -sprintf (&guid_name[i],"%02x" ,TO_NATIVE ((guid->b)[i/2 ])); +for (int i =0 ; i <sizeof (*guid); i++) +sprintf (&guid_name[i *2 ],"%02x" , guid->b[i]);module_alias_printf (mod, false,"vmbus:%s" , guid_name); }
Signature · 1 line
2.43.0
PATCH 3/3 modpost: work around unaligned data access error
In reply to Masahiro YamadaWith the latest binutils, modpost fails with a bus error on some architectures such as ARM and sparc64.
Since binutils commit 1f1b5e506bf0 ("bfd/ELF: restrict file alignment for object files"), the byte offset to each section (sh_offset) in relocatable ELF is no longer guaranteed to be aligned.
modpost parses MODULE_DEVICE_TABLE() data structures, which are usually located in the .rodata section. If it is not properly aligned, unaligned access errors may occur.
To address the issue, this commit imports the get_unaligned() helper from include/linux/unaligned.h.
The get_unaligned_native() helper caters to the endianness in addition to handling the unaligned access.
I slightly refactored do_pcmcia_entry() and do_input() to avoid writing back to an unaligned address. (We would need the put_unaligned() helper to do that.)
The addend_*_rel() functions need similar adjustments because the .text sections are not aligned either.
It seems that the .symtab, .rel.* and .rela.* sections are still aligned. Keep normal pointer access for these sections to avoid unnecessary performance costs.
---
scripts/mod/file2alias.c | 26 +++++++++++++------------- scripts/mod/modpost.c | 24 ++++++++++++------------ scripts/mod/modpost.h | 14 ++++++++++++++ 3 files changed, 39 insertions(+), 25 deletions(-)
scripts/mod/file2alias.c +13 -13
@@ -132,7 +132,8 @@ struct devtable {* based at address m. */ #define DEF_FIELD (m, devid, f) \ - typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f)) + typeof(((struct devid *)0)->f) f = \ + get_unaligned_native((typeof(f) *)((m) + OFF_##devid##_##f))/* Define a variable f that holds the address of field f of struct devid * based at address m. Due to the way typeof works, for a field of type
@@ -600,7 +601,7 @@ static void do_pnp_card_entry(struct module *mod, void *symval)static void do_pcmcia_entry (struct module *mod,void *symval) {char alias[256 ] = {}; -unsigned int i; +DEF_FIELD (symval, pcmcia_device_id, match_flags);DEF_FIELD (symval, pcmcia_device_id, manf_id);DEF_FIELD (symval, pcmcia_device_id, card_id);
@@ -609,10 +610,6 @@ static void do_pcmcia_entry(struct module *mod, void *symval)DEF_FIELD (symval, pcmcia_device_id, device_no);DEF_FIELD_ADDR (symval, pcmcia_device_id, prod_id_hash); -for (i=0 ; i<4 ; i++) { - (*prod_id_hash)[i] =TO_NATIVE ((*prod_id_hash)[i]); - } -ADD (alias,"m" , match_flags &PCMCIA_DEV_ID_MATCH_MANF_ID , manf_id); ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
@@ -623,10 +620,14 @@ static void do_pcmcia_entry(struct module *mod, void *symval) function);ADD (alias,"pfn" , match_flags &PCMCIA_DEV_ID_MATCH_DEVICE_NO , device_no); -ADD (alias,"pa" , match_flags &PCMCIA_DEV_ID_MATCH_PROD_ID1 , (*prod_id_hash)[0 ]); -ADD (alias,"pb" , match_flags &PCMCIA_DEV_ID_MATCH_PROD_ID2 , (*prod_id_hash)[1 ]); -ADD (alias,"pc" , match_flags &PCMCIA_DEV_ID_MATCH_PROD_ID3 , (*prod_id_hash)[2 ]); -ADD (alias,"pd" , match_flags &PCMCIA_DEV_ID_MATCH_PROD_ID4 , (*prod_id_hash)[3 ]); +ADD (alias,"pa" , match_flags &PCMCIA_DEV_ID_MATCH_PROD_ID1 , +get_unaligned_native (*prod_id_hash +0 )); +ADD (alias,"pb" , match_flags &PCMCIA_DEV_ID_MATCH_PROD_ID2 , +get_unaligned_native (*prod_id_hash +1 )); +ADD (alias,"pc" , match_flags &PCMCIA_DEV_ID_MATCH_PROD_ID3 , +get_unaligned_native (*prod_id_hash +2 )); +ADD (alias,"pd" , match_flags &PCMCIA_DEV_ID_MATCH_PROD_ID4 , +get_unaligned_native (*prod_id_hash +3 ));module_alias_printf (mod, true,"pcmcia:%s" , alias); }
@@ -654,10 +655,9 @@ static void do_input(char *alias, {unsigned int i; -for (i = min /BITS_PER_LONG ; i < max /BITS_PER_LONG +1 ; i++) - arr[i] =TO_NATIVE (arr[i]);for (i = min; i <= max; i++) -if (arr[i /BITS_PER_LONG ] & (1ULL << (i%BITS_PER_LONG ))) +if (get_unaligned_native (arr + i /BITS_PER_LONG ) & + (1ULL << (i %BITS_PER_LONG )))sprintf (alias +strlen (alias),"%X,*" , i); }
scripts/mod/modpost.c +12 -12
@@ -1138,9 +1138,9 @@ static Elf_Addr addend_386_rel(uint32_t *location, unsigned int r_type) {switch (r_type) {case R_386_32 : -return TO_NATIVE (*location); +return get_unaligned_native (location);case R_386_PC32 : -return TO_NATIVE (*location) +4 ; +return get_unaligned_native (location) +4 ; }return (Elf_Addr)(-1 );
@@ -1161,24 +1161,24 @@ static Elf_Addr addend_arm_rel(void *loc, Elf_Sym *sym, unsigned int r_type)switch (r_type) {case R_ARM_ABS32 :case R_ARM_REL32 : - inst =TO_NATIVE (*(uint32_t *)loc); + inst =get_unaligned_native ((uint32_t *)loc);return inst + sym->st_value;case R_ARM_MOVW_ABS_NC :case R_ARM_MOVT_ABS : - inst =TO_NATIVE (*(uint32_t *)loc); + inst =get_unaligned_native ((uint32_t *)loc); offset =sign_extend32 (((inst &0xf0000 ) >>4 ) | (inst &0xfff ),15 );return offset + sym->st_value;case R_ARM_PC24 :case R_ARM_CALL :case R_ARM_JUMP24 : - inst =TO_NATIVE (*(uint32_t *)loc); + inst =get_unaligned_native ((uint32_t *)loc); offset =sign_extend32 ((inst &0x00ffffff ) <<2 ,25 );return offset + sym->st_value +8 ;case R_ARM_THM_MOVW_ABS_NC :case R_ARM_THM_MOVT_ABS : - upper =TO_NATIVE (*(uint16_t *)loc); - lower =TO_NATIVE (*((uint16_t *)loc +1 )); + upper =get_unaligned_native ((uint16_t *)loc); + lower =get_unaligned_native ((uint16_t *)loc +1 ); offset = sign_extend32(((upper & 0x000f) << 12) | ((upper & 0x0400) << 1) | ((lower & 0x7000) >> 4) |
@@ -1195,8 +1195,8 @@ static Elf_Addr addend_arm_rel(void *loc, Elf_Sym *sym, unsigned int r_type)* imm11 = lower[10:0] * imm32 = SignExtend(S:J2:J1:imm6:imm11:'0') */ - upper =TO_NATIVE (*(uint16_t *)loc); - lower =TO_NATIVE (*((uint16_t *)loc +1 )); + upper =get_unaligned_native ((uint16_t *)loc); + lower =get_unaligned_native ((uint16_t *)loc +1 ); sign = (upper >>10 ) &1 ; j1 = (lower >>13 ) &1 ;
@@ -1219,8 +1219,8 @@ static Elf_Addr addend_arm_rel(void *loc, Elf_Sym *sym, unsigned int r_type)* I2 = NOT(J2 XOR S) * imm32 = SignExtend(S:I1:I2:imm10:imm11:'0') */ - upper =TO_NATIVE (*(uint16_t *)loc); - lower =TO_NATIVE (*((uint16_t *)loc +1 )); + upper =get_unaligned_native ((uint16_t *)loc); + lower =get_unaligned_native ((uint16_t *)loc +1 ); sign = (upper >>10 ) &1 ; j1 = (lower >>13 ) &1 ;
@@ -1241,7 +1241,7 @@ static Elf_Addr addend_mips_rel(uint32_t *location, unsigned int r_type) {uint32_t inst; - inst =TO_NATIVE (*location); + inst =get_unaligned_native (location);switch (r_type) {case R_MIPS_LO16 :return inst &0xffff ;
scripts/mod/modpost.h +14 -0
@@ -65,6 +65,20 @@#define TO_NATIVE (x) \ (target_is_big_endian == host_is_big_endian ? x : bswap(x)) +#define __get_unaligned_t (type, ptr) ({ \ + const struct { type x; } __attribute__((__packed__)) *__pptr = \ + (typeof(__pptr))(ptr); \ + __pptr->x; \ +}) + +#define get_unaligned (ptr) __get_unaligned_t(typeof(*(ptr)), (ptr)) + +#define get_unaligned_native (ptr) \ +({ \ + typeof(*(ptr)) _val = get_unaligned(ptr); \ + TO_NATIVE(_val); \ +}) +#define ARRAY_SIZE (arr) (sizeof(arr) / sizeof((arr)[0]))#define strstarts (str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
Signature · 1 line
2.43.0
Hi Masahiro,
Quoted 19 lines
The latest binutils stopped aligning section data in relocatable ELF. It saves small number of bytes that were previously inserted between sections. However, modpost crashes due to unaligned access:
https://sourceware.org/bugzilla/show_bug.cgi?id=32435
https://sourceware.org/bugzilla/show_bug.cgi?id=32493Similar to kernel space, unaligned data access in userspace can be problematic on some architectures.
Simple example on ARM:
$ CC=arm-linux-gnueabihf-gcc $ echo 'int foo(int *p) { return *p + *(p + 1); }' | ${CC} -O2 -x c - -c -o foo.o $ echo 'int foo(int *p); int main(void) { char str[16] = "helloworld"; return foo((int *)(str + 1)); }' | ${CC} -x c - -c -o main.o $ ${CC} -static -o unalign-test main.o foo.o $ qemu-armhf unalign-test qemu: uncaught target signal 7 (Bus error) - core dumped Bus error (core dumped)
Quoted 5 lines
In this case, the 'ldrd' instruction causes a bus error due to an unaligned access. If the -O2 flag is omitted, this issue does not occur.
To avoid potential unaligned access, use the
get_unaligned()approach as seen in the kernel space.
I have just applied this series against Linus' tree and I can confirm that it fixes the unalignment access anymore on sparc64. There is no more "Bus error" and the build succeeds.
Thanks,
Adrian
--
.''`. John Paul Adrian Glaubitz : :' : Debian Developer `. `' Physicist `- GPG: 62FF 8A75 84E0 2956 9546 0006 7426 3B37 F5B5 F913
Quoted 25 lines · John Paul Adrian Glaubitz
Hi Masahiro,
Quoted 22 lines
The latest binutils stopped aligning section data in relocatable ELF. It saves small number of bytes that were previously inserted between sections. However, modpost crashes due to unaligned access:
https://sourceware.org/bugzilla/show_bug.cgi?id=32435
https://sourceware.org/bugzilla/show_bug.cgi?id=32493Similar to kernel space, unaligned data access in userspace can be problematic on some architectures.
Simple example on ARM:
$ CC=arm-linux-gnueabihf-gcc $ echo 'int foo(int *p) { return *p + *(p + 1); }' | ${CC} -O2 -x c - -c -o foo.o $ echo 'int foo(int *p); int main(void) { char str[16] = "helloworld"; return foo((int *)(str + 1)); }' | ${CC} -x c - -c -o main.o $ ${CC} -static -o unalign-test main.o foo.o $ qemu-armhf unalign-test qemu: uncaught target signal 7 (Bus error) - core dumped Bus error (core dumped)In this case, the 'ldrd' instruction causes a bus error due to an unaligned access. If the -O2 flag is omitted, this issue does not occur.
Quoted 7 lines
Quoted 2 lines
To avoid potential unaligned access, use the
get_unaligned()approach as seen in the kernel space.I have just applied this series against Linus' tree and I can confirm that it fixes the unalignment access anymore on sparc64. There is no more "Bus error" and the build succeeds.
Thanks for the compile test.
Loadable modules (*.ko files) are relocatable ELF. So, there is no alignment in *.ko files any more if the latest binutils is used.
Just in case, I did run-tests for arm and arm64. Even if there is no alignment in *.ko files, I confirmed that the kernel can load modules.
If you have time, please run-test and double-check loadable modules are working.
Signature · 2 lines
Best Regards Masahiro Yamada
John Paul Adrian Glaubitz
Tested-byHi Masahiro,
Quoted 12 lines · Masahiro Yamada
Quoted 4 lines
I have just applied this series against Linus' tree and I can confirm that it fixes the unalignment access anymore on sparc64. There is no more "Bus error" and the build succeeds.
Thanks for the compile test.
Loadable modules (*.ko files) are relocatable ELF. So, there is no alignment in *.ko files any more if the latest binutils is used.
Quoted 6 lines
Just in case, I did run-tests for arm and arm64. Even if there is no alignment in *.ko files, I confirmed that the kernel can load modules.
If you have time, please run-test and double-check loadable modules are working.
I have just verified that. I applied your series against v6.13-rc4 and can confirm that module loading works without any issues on sparc64. Tested on SPARC T5 LDOM.
So, please extend my Tested-By to both compile and runtime testing on sparc64.
Adrian
Signature · 4 lines
.''`. John Paul Adrian Glaubitz : :' : Debian Developer `. `' Physicist `- GPG: 62FF 8A75 84E0 2956 9546 0006 7426 3B37 F5B5 F913