Skip to content

Commit f7e68c9

Browse files
committed
tcg/plugins: implement a qemu_plugin_user_exit helper
In user-mode emulation there is a small race between preexit_cleanup and exit_group() which means we may end up calling instrumented instructions before the kernel reaps child threads. To solve this we implement a new helper which ensures the callbacks are flushed along with any translations before we let the host do it's a thing. While we are at it make the documentation of qemu_plugin_register_atexit_cb clearer as to what the user can expect. Signed-off-by: Alex Bennée <[email protected]> Reviewed-by: Mahmoud Mandour <[email protected]> Acked-by: Warner Losh <[email protected]> Message-Id: <[email protected]>
1 parent 094d278 commit f7e68c9

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

bsd-user/syscall.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
335335
_mcleanup();
336336
#endif
337337
gdb_exit(arg1);
338-
qemu_plugin_atexit_cb();
338+
qemu_plugin_user_exit();
339339
/* XXX: should free thread stack and CPU env */
340340
_exit(arg1);
341341
ret = 0; /* avoid warning */
@@ -437,7 +437,7 @@ abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
437437
_mcleanup();
438438
#endif
439439
gdb_exit(arg1);
440-
qemu_plugin_atexit_cb();
440+
qemu_plugin_user_exit();
441441
/* XXX: should free thread stack and CPU env */
442442
_exit(arg1);
443443
ret = 0; /* avoid warning */
@@ -516,7 +516,7 @@ abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
516516
_mcleanup();
517517
#endif
518518
gdb_exit(arg1);
519-
qemu_plugin_atexit_cb();
519+
qemu_plugin_user_exit();
520520
/* XXX: should free thread stack and CPU env */
521521
_exit(arg1);
522522
ret = 0; /* avoid warning */

include/qemu/plugin.h

+12
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ void qemu_plugin_add_dyn_cb_arr(GArray *arr);
190190

191191
void qemu_plugin_disable_mem_helpers(CPUState *cpu);
192192

193+
/**
194+
* qemu_plugin_user_exit(): clean-up callbacks before calling exit callbacks
195+
*
196+
* This is a user-mode only helper that ensure we have fully cleared
197+
* callbacks from all threads before calling the exit callbacks. This
198+
* is so the plugins themselves don't have to jump through hoops to
199+
* guard against race conditions.
200+
*/
201+
void qemu_plugin_user_exit(void);
202+
193203
#else /* !CONFIG_PLUGIN */
194204

195205
static inline void qemu_plugin_add_opts(void)
@@ -250,6 +260,8 @@ void qemu_plugin_add_dyn_cb_arr(GArray *arr)
250260
static inline void qemu_plugin_disable_mem_helpers(CPUState *cpu)
251261
{ }
252262

263+
static inline void qemu_plugin_user_exit(void)
264+
{ }
253265
#endif /* !CONFIG_PLUGIN */
254266

255267
#endif /* QEMU_PLUGIN_H */

include/qemu/qemu-plugin.h

+13
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,19 @@ void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,
549549
void qemu_plugin_register_flush_cb(qemu_plugin_id_t id,
550550
qemu_plugin_simple_cb_t cb);
551551

552+
/**
553+
* qemu_plugin_register_atexit_cb() - register exit callback
554+
* @id: plugin ID
555+
* @cb: callback
556+
* @userdata: user data for callback
557+
*
558+
* The @cb function is called once execution has finished. Plugins
559+
* should be able to free all their resources at this point much like
560+
* after a reset/uninstall callback is called.
561+
*
562+
* In user-mode it is possible a few un-instrumented instructions from
563+
* child threads may run before the host kernel reaps the threads.
564+
*/
552565
void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id,
553566
qemu_plugin_udata_cb_t cb, void *userdata);
554567

linux-user/exit.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ void preexit_cleanup(CPUArchState *env, int code)
3535
__gcov_dump();
3636
#endif
3737
gdb_exit(code);
38-
qemu_plugin_atexit_cb();
38+
qemu_plugin_user_exit();
3939
}

plugins/core.c

+39
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,45 @@ void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id,
487487
plugin_register_cb_udata(id, QEMU_PLUGIN_EV_ATEXIT, cb, udata);
488488
}
489489

490+
/*
491+
* Handle exit from linux-user. Unlike the normal atexit() mechanism
492+
* we need to handle the clean-up manually as it's possible threads
493+
* are still running. We need to remove all callbacks from code
494+
* generation, flush the current translations and then we can safely
495+
* trigger the exit callbacks.
496+
*/
497+
498+
void qemu_plugin_user_exit(void)
499+
{
500+
enum qemu_plugin_event ev;
501+
CPUState *cpu;
502+
503+
QEMU_LOCK_GUARD(&plugin.lock);
504+
505+
start_exclusive();
506+
507+
/* un-register all callbacks except the final AT_EXIT one */
508+
for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) {
509+
if (ev != QEMU_PLUGIN_EV_ATEXIT) {
510+
struct qemu_plugin_ctx *ctx;
511+
QTAILQ_FOREACH(ctx, &plugin.ctxs, entry) {
512+
plugin_unregister_cb__locked(ctx, ev);
513+
}
514+
}
515+
}
516+
517+
tb_flush(current_cpu);
518+
519+
CPU_FOREACH(cpu) {
520+
qemu_plugin_disable_mem_helpers(cpu);
521+
}
522+
523+
end_exclusive();
524+
525+
/* now it's safe to handle the exit case */
526+
qemu_plugin_atexit_cb();
527+
}
528+
490529
/*
491530
* Call this function after longjmp'ing to the main loop. It's possible that the
492531
* last instruction of a TB might have used helpers, and therefore the

0 commit comments

Comments
 (0)