Skip to content
Snippets Groups Projects
  • Kevin Wolf's avatar
    6bc0bcc8
    block: Fix deadlocks in bdrv_graph_wrunlock() · 6bc0bcc8
    Kevin Wolf authored
    
    bdrv_graph_wrunlock() calls aio_poll(), which may run callbacks that
    have a nested event loop. Nested event loops can depend on other
    iothreads making progress, so in order to allow them to make progress it
    must not hold the AioContext lock of another thread while calling
    aio_poll().
    
    This introduces a @bs parameter to bdrv_graph_wrunlock() whose
    AioContext is temporarily dropped (which matches bdrv_graph_wrlock()),
    and a bdrv_graph_wrunlock_ctx() that can be used if the BlockDriverState
    doesn't necessarily exist any more when unlocking.
    
    This also requires a change to bdrv_schedule_unref(), which was relying
    on the incorrectly taken lock. It needs to take the lock itself now.
    While this is a separate bug, it can't be fixed a separate patch because
    otherwise the intermediate state would either deadlock or try to release
    a lock that we don't even hold.
    
    Signed-off-by: default avatarKevin Wolf <kwolf@redhat.com>
    Message-ID: <20231115172012.112727-3-kwolf@redhat.com>
    Reviewed-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
    [kwolf: Fixed up bdrv_schedule_unref()]
    Signed-off-by: default avatarKevin Wolf <kwolf@redhat.com>
    6bc0bcc8
    History
    block: Fix deadlocks in bdrv_graph_wrunlock()
    Kevin Wolf authored
    
    bdrv_graph_wrunlock() calls aio_poll(), which may run callbacks that
    have a nested event loop. Nested event loops can depend on other
    iothreads making progress, so in order to allow them to make progress it
    must not hold the AioContext lock of another thread while calling
    aio_poll().
    
    This introduces a @bs parameter to bdrv_graph_wrunlock() whose
    AioContext is temporarily dropped (which matches bdrv_graph_wrlock()),
    and a bdrv_graph_wrunlock_ctx() that can be used if the BlockDriverState
    doesn't necessarily exist any more when unlocking.
    
    This also requires a change to bdrv_schedule_unref(), which was relying
    on the incorrectly taken lock. It needs to take the lock itself now.
    While this is a separate bug, it can't be fixed a separate patch because
    otherwise the intermediate state would either deadlock or try to release
    a lock that we don't even hold.
    
    Signed-off-by: default avatarKevin Wolf <kwolf@redhat.com>
    Message-ID: <20231115172012.112727-3-kwolf@redhat.com>
    Reviewed-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
    [kwolf: Fixed up bdrv_schedule_unref()]
    Signed-off-by: default avatarKevin Wolf <kwolf@redhat.com>
block.c 254.67 KiB
/*
 * QEMU System Emulator block driver
 *
 * Copyright (c) 2003 Fabrice Bellard
 * Copyright (c) 2020 Virtuozzo International GmbH.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "qemu/osdep.h"
#include "block/trace.h"
#include "block/block_int.h"
#include "block/blockjob.h"
#include "block/dirty-bitmap.h"
#include "block/fuse.h"
#include "block/nbd.h"
#include "block/qdict.h"
#include "qemu/error-report.h"
#include "block/module_block.h"
#include "qemu/main-loop.h"
#include "qemu/module.h"
#include "qapi/error.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qjson.h"
#include "qapi/qmp/qnull.h"
#include "qapi/qmp/qstring.h"
#include "qapi/qobject-output-visitor.h"
#include "qapi/qapi-visit-block-core.h"
#include "sysemu/block-backend.h"
#include "qemu/notify.h"
#include "qemu/option.h"
#include "qemu/coroutine.h"
#include "block/qapi.h"
#include "qemu/timer.h"
#include "qemu/cutils.h"
#include "qemu/id.h"
#include "qemu/range.h"
#include "qemu/rcu.h"
#include "block/coroutines.h"

#ifdef CONFIG_BSD
#include <sys/ioctl.h>
#include <sys/queue.h>
#if defined(HAVE_SYS_DISK_H)
#include <sys/disk.h>
#endif
#endif

#ifdef _WIN32
#include <windows.h>
#endif

#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */