Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/jump_label: Batch jump label updates

Currently, the jump label of a static key is transformed via the arch
specific function:

void arch_jump_label_transform(struct jump_entry *entry,
enum jump_label_type type)

The new approach (batch mode) uses two arch functions, the first has the
same arguments of the arch_jump_label_transform(), and is the function:

bool arch_jump_label_transform_queue(struct jump_entry *entry,
enum jump_label_type type)

Rather than transforming the code, it adds the jump_entry in a queue of
entries to be updated. This functions returns true in the case of a
successful enqueue of an entry. If it returns false, the caller must to
apply the queue and then try to queue again, for instance, because the
queue is full.

This function expects the caller to sort the entries by the address before
enqueueuing then. This is already done by the arch independent code, though.

After queuing all jump_entries, the function:

void arch_jump_label_transform_apply(void)

Applies the changes in the queue.

Signed-off-by: Daniel Bristot de Oliveira <bristot@redhat.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Chris von Recklinghausen <crecklin@redhat.com>
Cc: Clark Williams <williams@redhat.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Jason Baron <jbaron@akamai.com>
Cc: Jiri Kosina <jkosina@suse.cz>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Scott Wood <swood@redhat.com>
Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: https://lkml.kernel.org/r/57b4caa654bad7e3b066301c9a9ae233dea065b5.1560325897.git.bristot@redhat.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by

Daniel Bristot de Oliveira and committed by
Ingo Molnar
ba54f0c3 c2ba8a15

+71
+2
arch/x86/include/asm/jump_label.h
··· 2 2 #ifndef _ASM_X86_JUMP_LABEL_H 3 3 #define _ASM_X86_JUMP_LABEL_H 4 4 5 + #define HAVE_JUMP_LABEL_BATCH 6 + 5 7 #define JUMP_LABEL_NOP_SIZE 5 6 8 7 9 #ifdef CONFIG_X86_64
+69
arch/x86/kernel/jump_label.c
··· 101 101 mutex_unlock(&text_mutex); 102 102 } 103 103 104 + #define TP_VEC_MAX (PAGE_SIZE / sizeof(struct text_poke_loc)) 105 + static struct text_poke_loc tp_vec[TP_VEC_MAX]; 106 + int tp_vec_nr = 0; 107 + 108 + bool arch_jump_label_transform_queue(struct jump_entry *entry, 109 + enum jump_label_type type) 110 + { 111 + struct text_poke_loc *tp; 112 + void *entry_code; 113 + 114 + if (system_state == SYSTEM_BOOTING) { 115 + /* 116 + * Fallback to the non-batching mode. 117 + */ 118 + arch_jump_label_transform(entry, type); 119 + return true; 120 + } 121 + 122 + /* 123 + * No more space in the vector, tell upper layer to apply 124 + * the queue before continuing. 125 + */ 126 + if (tp_vec_nr == TP_VEC_MAX) 127 + return false; 128 + 129 + tp = &tp_vec[tp_vec_nr]; 130 + 131 + entry_code = (void *)jump_entry_code(entry); 132 + 133 + /* 134 + * The INT3 handler will do a bsearch in the queue, so we need entries 135 + * to be sorted. We can survive an unsorted list by rejecting the entry, 136 + * forcing the generic jump_label code to apply the queue. Warning once, 137 + * to raise the attention to the case of an unsorted entry that is 138 + * better not happen, because, in the worst case we will perform in the 139 + * same way as we do without batching - with some more overhead. 140 + */ 141 + if (tp_vec_nr > 0) { 142 + int prev = tp_vec_nr - 1; 143 + struct text_poke_loc *prev_tp = &tp_vec[prev]; 144 + 145 + if (WARN_ON_ONCE(prev_tp->addr > entry_code)) 146 + return false; 147 + } 148 + 149 + __jump_label_set_jump_code(entry, type, 150 + (union jump_code_union *) &tp->opcode, 0); 151 + 152 + tp->addr = entry_code; 153 + tp->detour = entry_code + JUMP_LABEL_NOP_SIZE; 154 + tp->len = JUMP_LABEL_NOP_SIZE; 155 + 156 + tp_vec_nr++; 157 + 158 + return true; 159 + } 160 + 161 + void arch_jump_label_transform_apply(void) 162 + { 163 + if (!tp_vec_nr) 164 + return; 165 + 166 + mutex_lock(&text_mutex); 167 + text_poke_bp_batch(tp_vec, tp_vec_nr); 168 + mutex_unlock(&text_mutex); 169 + 170 + tp_vec_nr = 0; 171 + } 172 + 104 173 static enum { 105 174 JL_STATE_START, 106 175 JL_STATE_NO_UPDATE,