On Fri, Apr 16, 2021 at 11:08:41AM +0300, Vladimir Sementsov-Ogievskiy wrote:
> From: Roman Kagan <[email protected]>
>
> Simplify lifetime management of BDRVNBDState->connect_thread by
> delaying the possible cleanup of it until the BDRVNBDState itself goes
> away.
>
> This also reverts
> 0267101af6 "block/nbd: fix possible use after free of s->connect_thread"
> as now s->connect_thread can't be cleared until the very end.
>
> Signed-off-by: Roman Kagan <[email protected]>
> [vsementsov: rebase, revert 0267101af6 changes]
> Signed-off-by: Vladimir Sementsov-Ogievskiy <[email protected]>
> ---
> block/nbd.c | 56 ++++++++++++++++++++---------------------------------
> 1 file changed, 21 insertions(+), 35 deletions(-)
>
> static void nbd_clear_bdrvstate(BlockDriverState *bs)
> {
> BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
> + NBDConnectThread *thr = s->connect_thread;
> + bool thr_running;
> +
> + qemu_mutex_lock(&thr->mutex);
> + thr_running = thr->state == CONNECT_THREAD_RUNNING;
> + if (thr_running) {
> + thr->state = CONNECT_THREAD_RUNNING_DETACHED;
> + }
> + qemu_mutex_unlock(&thr->mutex);
> +
> + /* the runaway thread will clean it up itself */
s/clean it up/clean up/
Reviewed-by: Eric Blake <[email protected]>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org