objtool: Support repeated uses of the same C jump table

ANBZ: #5530

commit bd98c81346 upstream.

This fixes objtool for both a GCC issue and a Clang issue:

1) GCC issue:

   kernel/bpf/core.o: warning: objtool: ___bpf_prog_run()+0x8d5: sibling call from callable instruction with modified stack frame

   With CONFIG_RETPOLINE=n, GCC is doing the following optimization in
   ___bpf_prog_run().

   Before:

           select_insn:
                   jmp *jumptable(,%rax,8)
                   ...
           ALU64_ADD_X:
                   ...
                   jmp select_insn
           ALU_ADD_X:
                   ...
                   jmp select_insn

   After:

           select_insn:
                   jmp *jumptable(, %rax, 8)
                   ...
           ALU64_ADD_X:
                   ...
                   jmp *jumptable(, %rax, 8)
           ALU_ADD_X:
                   ...
                   jmp *jumptable(, %rax, 8)

   This confuses objtool.  It has never seen multiple indirect jump
   sites which use the same jump table.

   For GCC switch tables, the only way of detecting the size of a table
   is by continuing to scan for more tables.  The size of the previous
   table can only be determined after another switch table is found, or
   when the scan reaches the end of the function.

   That logic was reused for C jump tables, and was based on the
   assumption that each jump table only has a single jump site.  The
   above optimization breaks that assumption.

2) Clang issue:

   drivers/usb/misc/sisusbvga/sisusb.o: warning: objtool: sisusb_write_mem_bulk()+0x588: can't find switch jump table

   With clang 9, code can be generated where a function contains two
   indirect jump instructions which use the same switch table.

The fix is the same for both issues: split the jump table parsing into
two passes.

In the first pass, locate the heads of all switch tables for the
function and mark their locations.

In the second pass, parse the switch tables and add them.

Fixes: e55a73251d ("bpf: Fix ORC unwinding in non-JIT BPF code")
Reported-by: Randy Dunlap <rdunlap@infradead.org>
Reported-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Nick Desaulniers <ndesaulniers@google.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/e995befaada9d4d8b2cf788ff3f566ba900d2b4d.1563413318.git.jpoimboe@redhat.com

Co-developed-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Yuanhe Shu <xiangzao@linux.alibaba.com>
Reviewed-by: Tianchen Ding <dtcccc@linux.alibaba.com>
Link: https://gitee.com/anolis/cloud-kernel/pulls/2651
This commit is contained in:
Jann Horn 2019-07-17 20:36:54 -05:00 committed by 小龙
parent bd46060896
commit d43f061482
3 changed files with 31 additions and 26 deletions

View File

@ -834,7 +834,7 @@ out:
}
static int add_jump_table(struct objtool_file *file, struct instruction *insn,
struct rela *table, struct rela *next_table)
struct rela *table)
{
struct rela *rela = table;
struct instruction *dest_insn;
@ -847,7 +847,9 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
* instruction.
*/
list_for_each_entry_from(rela, &table->sec->rela_list, list) {
if (rela == next_table)
/* Check for the end of the table: */
if (rela != table && rela->jump_table_start)
break;
/* Make sure the table entries are consecutive: */
@ -1006,13 +1008,15 @@ static struct rela *find_jump_table(struct objtool_file *file,
return NULL;
}
static int add_func_jump_tables(struct objtool_file *file,
struct symbol *func)
/*
* First pass: Mark the head of each jump table so that in the next pass,
* we know when a given jump table ends and the next one starts.
*/
static void mark_func_jump_tables(struct objtool_file *file,
struct symbol *func)
{
struct instruction *insn, *last = NULL, *prev_jump = NULL;
struct rela *rela, *prev_rela = NULL;
int ret;
struct instruction *insn, *last = NULL;
struct rela *rela;
func_for_each_insn_all(file, func, insn) {
if (!last)
@ -1036,26 +1040,24 @@ static int add_func_jump_tables(struct objtool_file *file,
continue;
rela = find_jump_table(file, func, insn);
if (!rela)
if (rela) {
rela->jump_table_start = true;
insn->jump_table = rela;
}
}
}
static int add_func_jump_tables(struct objtool_file *file,
struct symbol *func)
{
struct instruction *insn;
int ret;
func_for_each_insn_all(file, func, insn) {
if (!insn->jump_table)
continue;
/*
* We found a jump table, but we don't know yet how big it
* is. Don't add it until we reach the end of the function or
* the beginning of another jump table in the same function.
*/
if (prev_jump) {
ret = add_jump_table(file, prev_jump, prev_rela, rela);
if (ret)
return ret;
}
prev_jump = insn;
prev_rela = rela;
}
if (prev_jump) {
ret = add_jump_table(file, prev_jump, prev_rela, NULL);
ret = add_jump_table(file, insn, insn->jump_table);
if (ret)
return ret;
}
@ -1082,6 +1084,7 @@ static int add_jump_table_alts(struct objtool_file *file)
if (func->type != STT_FUNC)
continue;
mark_func_jump_tables(file, func);
ret = add_func_jump_tables(file, func);
if (ret)
return ret;

View File

@ -49,6 +49,7 @@ struct instruction {
struct symbol *call_dest;
struct instruction *jump_dest;
struct instruction *first_jump_src;
struct rela *jump_table;
struct list_head alts;
struct symbol *func;
struct stack_op stack_op;

View File

@ -73,6 +73,7 @@ struct rela {
unsigned int type;
unsigned long offset;
int addend;
bool jump_table_start;
};
struct elf {