qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Kyle Evans" <kevans@freebsd.org>,
	minyihh@uci.edu, robhenry@microsoft.com,
	"Laurent Vivier" <laurent@vivier.eu>,
	mahmoudabdalghany@outlook.com, aaron@os.amperecomputing.com,
	cota@braap.org, "Alexandre Iooss" <erdnaxe@crans.org>,
	kuhn.chenqun@huawei.com, ma.mandourr@gmail.com,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Warner Losh" <imp@bsdimp.com>
Subject: Re: [RFC PATCH] tcg/plugins: implement a qemu_plugin_user_exit helper
Date: Mon, 19 Jul 2021 14:56:08 +0100	[thread overview]
Message-ID: <87im16pfsq.fsf@linaro.org> (raw)
In-Reply-To: <20210719123732.24457-1-alex.bennee@linaro.org>


Alex Bennée <alex.bennee@linaro.org> writes:

> 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.
>
<snip>
>  
> +/*
> + * Handle exit from linux-user. Unlike the normal atexit() mechanism
> + * we need to handle the clean-up manually as it's possible threads
> + * are still running. We need to remove all callbacks from code
> + * generation, flush the current translations and then we can safely
> + * trigger the exit callbacks.
> + */
> +
> +void qemu_plugin_user_exit(void)
> +{
> +    enum qemu_plugin_event ev;
> +
> +    QEMU_LOCK_GUARD(&plugin.lock);
> +
> +    start_exclusive();
> +
> +    /* un-register all callbacks except the final AT_EXIT one */
> +    for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) {
> +        if (ev != QEMU_PLUGIN_EV_ATEXIT) {
> +            struct qemu_plugin_ctx *ctx;
> +            QTAILQ_FOREACH(ctx, &plugin.ctxs, entry) {
> +                plugin_unregister_cb__locked(ctx, ev);
> +            }
> +        }
> +    }
> +
> +    tb_flush(current_cpu);

We also need to disable memory helpers during the exclusive period as
that is another route into a callback:

--8<---------------cut here---------------start------------->8---
modified   plugins/core.c
@@ -498,6 +499,7 @@ void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id,
 void qemu_plugin_user_exit(void)
 {
     enum qemu_plugin_event ev;
+    CPUState *cpu;
 
     QEMU_LOCK_GUARD(&plugin.lock);
 
@@ -514,6 +516,11 @@ void qemu_plugin_user_exit(void)
     }
 
     tb_flush(current_cpu);
+
+    CPU_FOREACH(cpu) {
+        qemu_plugin_disable_mem_helpers(cpu);
+    }
+
     end_exclusive();
 
     /* now it's safe to handle the exit case */
--8<---------------cut here---------------end--------------->8---



> +    end_exclusive();
> +
> +    /* now it's safe to handle the exit case */
> +    qemu_plugin_atexit_cb();
> +}
> +
>  /*
>   * Call this function after longjmp'ing to the main loop. It's possible that the
>   * last instruction of a TB might have used helpers, and therefore the


-- 
Alex Bennée


  reply	other threads:[~2021-07-19 13:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-19 12:37 [RFC PATCH] tcg/plugins: implement a qemu_plugin_user_exit helper Alex Bennée
2021-07-19 13:56 ` Alex Bennée [this message]
2021-07-19 18:03   ` Warner Losh
2021-07-19 19:21     ` Alex Bennée
2021-07-19 19:26       ` Warner Losh
2021-07-20 12:28 ` Mahmoud Mandour

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87im16pfsq.fsf@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=aaron@os.amperecomputing.com \
    --cc=cota@braap.org \
    --cc=erdnaxe@crans.org \
    --cc=imp@bsdimp.com \
    --cc=kevans@freebsd.org \
    --cc=kuhn.chenqun@huawei.com \
    --cc=laurent@vivier.eu \
    --cc=ma.mandourr@gmail.com \
    --cc=mahmoudabdalghany@outlook.com \
    --cc=minyihh@uci.edu \
    --cc=qemu-devel@nongnu.org \
    --cc=robhenry@microsoft.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).