Skip to contentSkip to conversation

ia64: fix ptrace(PTRACE_GETREGS) (unbreaks strace, gdb)

PATCH Superseded Sergei Trofimovich 2 participants 6 messages Feb 2, 2018 – Aug 4, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ia64: fix ptrace(PTRACE_GETREGS) (unbreaks strace, gdb)

The strace breakage looks like that:
./strace: get_regs: get_regs_error: Input/output error

It happens because ia64 needs to load unwind tables to read certain registers. Unwind tables fail to load due to GCC quirk on the following code:

    extern char __end_unwind[];
    const struct unw_table_entry *end = (struct unw_table_entry *)table_end;
    table->end = segment_base + end[-1].end_offset;

GCC does not generate correct code for this single memory reference after constant propagation (see https://gcc.gnu.org/PR84184).
Two triggers are required for bad code generation:
- '__end_unwind' has alignment lower (char), than
'struct unw_table_entry' (8).
- symbol offset is negative.

This commit workarounds it by fixing alignment of '__end_unwind'. While at it use hidden symbols to generate shorter gp-relative relocations.

---
 arch/ia64/include/asm/sections.h |  1 -
 arch/ia64/kernel/unwind.c        | 15 ++++++++++++++-
 2 files changed, 14 insertions(+), 2 deletions(-)
arch/ia64/include/asm/sections.h +0 -1
index f3481408594e..0fc4f1757a44 100644
@@ -24,7 +24,6 @@ extern char __start_gate_mckinley_e9_patchlist[], __end_gate_mckinley_e9_patchli
 extern char __start_gate_vtop_patchlist[], __end_gate_vtop_patchlist[];
 extern char __start_gate_fsyscall_patchlist[], __end_gate_fsyscall_patchlist[];
 extern char __start_gate_brl_fsys_bubble_down_patchlist[], __end_gate_brl_fsys_bubble_down_patchlist[];
-extern char __start_unwind[], __end_unwind[];
 extern char __start_ivt_text[], __end_ivt_text[];
 
 #undef dereference_function_descriptor
arch/ia64/kernel/unwind.c +14 -1
index e04efa088902..025ba6700790 100644
@@ -2243,7 +2243,20 @@ __initcall(create_gate_table);
 void __init
 unw_init (void)
 {
-	extern char __gp[];
+	#define __ia64_hidden __attribute__((visibility("hidden")))
+	/*
+	 * We use hidden symbols to generate more efficient code using
+	 * gp-relative addressing.
+	 */
+	extern char __gp[] __ia64_hidden;
+	/*
+	 * Unwind tables need to have proper alignment as init_unwind_table()
+	 * uses negative offsets against '__end_unwind'.
+	 * See https://gcc.gnu.org/PR84184
+	 */
+	extern const struct unw_table_entry __start_unwind[] __ia64_hidden;
+	extern const struct unw_table_entry __end_unwind[] __ia64_hidden;
+	#undef __ia64_hidden
 	extern void unw_hash_index_t_is_too_narrow (void);
 	long i, off;
 
Signature · 1 line
2.16.1
#1 lore raw
Quoted 7 lines · Sergei Trofimovich

The strace breakage looks like that:
./strace: get_regs: get_regs_error: Input/output error

It happens because ia64 needs to load unwind tables to read certain registers. Unwind tables fail to load due to GCC quirk on the following code:

Quoted 6 lines
    extern char __end_unwind[];
    const struct unw_table_entry *end = (struct unw_table_entry *)table_end;
    table->end = segment_base + end[-1].end_offset;

GCC does not generate correct code for this single memory reference after constant propagation (see https://gcc.gnu.org/PR84184).

I'm not seeing this ... probably because I build with a pre-historic 4.3.4 version of gcc.

Do you know which version(s) are affected? I'm not looking for an exhaustive list, just the one on which you found this would be good.

-Tony

#2 lore raw
Quoted 15 lines · Tony
Quoted 7 lines · Sergei Trofimovich

The strace breakage looks like that:
./strace: get_regs: get_regs_error: Input/output error

It happens because ia64 needs to load unwind tables to read certain registers. Unwind tables fail to load due to GCC quirk on the following code:

Quoted 6 lines
    extern char __end_unwind[];
    const struct unw_table_entry *end = (struct unw_table_entry *)table_end;
    table->end = segment_base + end[-1].end_offset;

GCC does not generate correct code for this single memory reference after constant propagation (see https://gcc.gnu.org/PR84184).

Quoted 8 lines

I'm not seeing this ... probably because I build with a pre-historic 4.3.4 version of gcc.

Do you know which version(s) are affected? I'm not looking for an exhaustive list, just the one on which you found this would be good.

-Tony

Original bug https://bugs.gentoo.org/518130 claims regression appeared around gcc-4.5. Locally am seeing the problem with gcc-6.4.0, gcc-7.2.0 and gcc-8 (HEAD).

Signature · 1 line
  Sergei
#3 lore raw
Quoted 25 lines · On Fri, 2 Feb 2018 23:02:20 +0000 Sergei Trofimovich <[email protected]> wrote:
Quoted 22 lines · Tony
Quoted 7 lines · Sergei Trofimovich

The strace breakage looks like that:
./strace: get_regs: get_regs_error: Input/output error

It happens because ia64 needs to load unwind tables to read certain registers. Unwind tables fail to load due to GCC quirk on the following code:

Quoted 6 lines
    extern char __end_unwind[];
    const struct unw_table_entry *end = (struct unw_table_entry *)table_end;
    table->end = segment_base + end[-1].end_offset;

GCC does not generate correct code for this single memory reference after constant propagation (see https://gcc.gnu.org/PR84184).

I'm not seeing this ... probably because I build with a pre-historic 4.3.4 version of gcc.

Do you know which version(s) are affected? I'm not looking for an exhaustive list, just the one on which you found this would be good.

Quoted 5 lines
Quoted 1 line

-Tony

Original bug https://bugs.gentoo.org/518130 claims regression appeared around gcc-4.5. Locally am seeing the problem with gcc-6.4.0, gcc-7.2.0 and gcc-8 (HEAD).

Another report on the positive patch effect:

rx2600 boots successfully with this patch (did not without, my guess is due to early access fault at bad address): https://bugs.gentoo.org/579278#c13

Signature · 1 line
  Sergei
#4 lore raw

PATCH v2 1/1 ia64: fix ptrace(PTRACE_GETREGS) (unbreaks strace, gdb)

The strace breakage looks like that:
./strace: get_regs: get_regs_error: Input/output error

It happens because ia64 needs to load unwind tables to read certain registers in 'PTRACE_GETREGS'. Unwind tables fail to load at kernel startup due to GCC quirk on the following code (logged as PR 84184):

    extern char __end_unwind[];
    const struct unw_table_entry *end = (struct unw_table_entry *)table_end;
    table->end = segment_base + end[-1].end_offset;

GCC does not generate correct code for this single memory reference after constant propagation.
Two triggers are required for bad code generation:
- '__end_unwind' has alignment lower (char), than 'struct unw_table_entry' (8).
- symbol offset is negative.

This commit workarounds it by disabling inline on init_unwind_table(). This way we avoid const-propagation of '__end_unwind' and pass address via register.

Tested in ski (emulator) and on rx2600, rx3600 (real hardware). In case of rx2600 it unbreaks booting.

This patch is a lighter version of patch
https://lkml.org/lkml/2018/2/2/914

---
 arch/ia64/kernel/unwind.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
arch/ia64/kernel/unwind.c +8 -1
index e04efa088902..a18190bc99a9 100644
@@ -2078,7 +2078,14 @@ unw_init_from_blocked_task (struct unw_frame_info *info, struct task_struct *t)
 }
 EXPORT_SYMBOL(unw_init_from_blocked_task);
 
-static void
+/*
+ * We use 'noinline' to evade GCC bug https://gcc.gnu.org/PR84184
+ * where gcc code generator emits incorrect code when '__end_unwind'
+ * is const-propagated to 'end[-1].end_offset' and gcc generates
+ * incorrect code. The prigger there is negative offset relative
+ * to externally-defined symbol.
+ */
+noinline static void
 init_unwind_table (struct unw_table *table, const char *name, unsigned long segment_base,
 		   unsigned long gp, const void *table_start, const void *table_end)
 {
Signature · 1 line
2.16.2