Skip to content

Commit 7deb18d

Browse files
mhiramathitachiIngo Molnar
authored andcommitted
x86: Introduce text_poke_smp_batch() for batch-code modifying
Introduce text_poke_smp_batch(). This function modifies several text areas with one stop_machine() on SMP. Because calling stop_machine() is heavy task, it is better to aggregate text_poke requests. ( Note: I've talked with Rusty about this interface, and he would not like to expand stop_machine() interface, since it is not for generic use. ) Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com> Acked-by: Steven Rostedt <rostedt@goodmis.org> Cc: Rusty Russell <rusty@rustcorp.com.au> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com> Cc: Jason Baron <jbaron@redhat.com> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Jan Beulich <jbeulich@novell.com> Cc: 2nddept-manager@sdl.hitachi.co.jp LKML-Reference: <20101203095422.2961.51217.stgit@ltc236.sdl.hitachi.co.jp> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 0490cd1 commit 7deb18d

2 files changed

Lines changed: 47 additions & 9 deletions

File tree

‎arch/x86/include/asm/alternative.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,15 @@ extern void *text_poke_early(void *addr, const void *opcode, size_t len);
180180
* On the local CPU you need to be protected again NMI or MCE handlers seeing an
181181
* inconsistent instruction while you patch.
182182
*/
183+
struct text_poke_param {
184+
void *addr;
185+
const void *opcode;
186+
size_t len;
187+
};
188+
183189
extern void *text_poke(void *addr, const void *opcode, size_t len);
184190
extern void *text_poke_smp(void *addr, const void *opcode, size_t len);
191+
extern void text_poke_smp_batch(struct text_poke_param *params, int n);
185192

186193
#if defined(CONFIG_DYNAMIC_FTRACE) || defined(HAVE_JUMP_LABEL)
187194
#define IDEAL_NOP_SIZE_5 5

‎arch/x86/kernel/alternative.c‎

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -591,17 +591,21 @@ static atomic_t stop_machine_first;
591591
static int wrote_text;
592592

593593
struct text_poke_params {
594-
void *addr;
595-
const void *opcode;
596-
size_t len;
594+
struct text_poke_param *params;
595+
int nparams;
597596
};
598597

599598
static int __kprobes stop_machine_text_poke(void *data)
600599
{
601600
struct text_poke_params *tpp = data;
601+
struct text_poke_param *p;
602+
int i;
602603

603604
if (atomic_dec_and_test(&stop_machine_first)) {
604-
text_poke(tpp->addr, tpp->opcode, tpp->len);
605+
for (i = 0; i < tpp->nparams; i++) {
606+
p = &tpp->params[i];
607+
text_poke(p->addr, p->opcode, p->len);
608+
}
605609
smp_wmb(); /* Make sure other cpus see that this has run */
606610
wrote_text = 1;
607611
} else {
@@ -610,8 +614,12 @@ static int __kprobes stop_machine_text_poke(void *data)
610614
smp_mb(); /* Load wrote_text before following execution */
611615
}
612616

613-
flush_icache_range((unsigned long)tpp->addr,
614-
(unsigned long)tpp->addr + tpp->len);
617+
for (i = 0; i < tpp->nparams; i++) {
618+
p = &tpp->params[i];
619+
flush_icache_range((unsigned long)p->addr,
620+
(unsigned long)p->addr + p->len);
621+
}
622+
615623
return 0;
616624
}
617625

@@ -631,17 +639,40 @@ static int __kprobes stop_machine_text_poke(void *data)
631639
void *__kprobes text_poke_smp(void *addr, const void *opcode, size_t len)
632640
{
633641
struct text_poke_params tpp;
642+
struct text_poke_param p;
634643

635-
tpp.addr = addr;
636-
tpp.opcode = opcode;
637-
tpp.len = len;
644+
p.addr = addr;
645+
p.opcode = opcode;
646+
p.len = len;
647+
tpp.params = &p;
648+
tpp.nparams = 1;
638649
atomic_set(&stop_machine_first, 1);
639650
wrote_text = 0;
640651
/* Use __stop_machine() because the caller already got online_cpus. */
641652
__stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
642653
return addr;
643654
}
644655

656+
/**
657+
* text_poke_smp_batch - Update instructions on a live kernel on SMP
658+
* @params: an array of text_poke parameters
659+
* @n: the number of elements in params.
660+
*
661+
* Modify multi-byte instruction by using stop_machine() on SMP. Since the
662+
* stop_machine() is heavy task, it is better to aggregate text_poke requests
663+
* and do it once if possible.
664+
*
665+
* Note: Must be called under get_online_cpus() and text_mutex.
666+
*/
667+
void __kprobes text_poke_smp_batch(struct text_poke_param *params, int n)
668+
{
669+
struct text_poke_params tpp = {.params = params, .nparams = n};
670+
671+
atomic_set(&stop_machine_first, 1);
672+
wrote_text = 0;
673+
stop_machine(stop_machine_text_poke, (void *)&tpp, NULL);
674+
}
675+
645676
#if defined(CONFIG_DYNAMIC_FTRACE) || defined(HAVE_JUMP_LABEL)
646677

647678
#ifdef CONFIG_X86_64

0 commit comments

Comments
 (0)