virtio_blk_dma_restart_cb() is tricky because the BH must deal with
virtio_blk_data_plane_start()/virtio_blk_data_plane_stop() being called.
There are two issues with the code:
1. virtio_blk_realize() should use qdev_add_vm_change_state_handler()
   instead of qemu_add_vm_change_state_handler(). This ensures the
   ordering with virtio_init()'s vm change state handler that calls
   virtio_blk_data_plane_start()/virtio_blk_data_plane_stop() is
   well-defined. Then blk's AioContext is guaranteed to be up-to-date in
   virtio_blk_dma_restart_cb() and it's no longer necessary to have a
   special case for virtio_blk_data_plane_start().
2. Only blk_drain() waits for virtio_blk_dma_restart_cb()'s
   blk_inc_in_flight() to be decremented. The bdrv_drain() family of
   functions do not wait for BlockBackend's in_flight counter to reach
   zero. virtio_blk_data_plane_stop() relies on blk_set_aio_context()'s
   implicit drain, but that's a bdrv_drain() and not a blk_drain().
   Note that virtio_blk_reset() already correctly relies on blk_drain().
   If virtio_blk_data_plane_stop() switches to blk_drain() then we can
   properly wait for pending virtio_blk_dma_restart_bh() calls.
Once these issues are taken care of the code becomes simpler. This
change is in preparation for multiple IOThreads in virtio-blk where we
need to clean up the multi-threading behavior.
I ran the reproducer from commit 49b44549ace7 ("virtio-blk: On restart,
process queued requests in the proper context") to check that there is
no regression.
Cc: Sergio Lopez <slp@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>
Cc: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Message-id: 20221102182337.252202-1-stefanha@redhat.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
		
	
			
		
			
				
	
	
		
			97 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			97 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/*
 | 
						|
 * Virtio Block Device
 | 
						|
 *
 | 
						|
 * Copyright IBM, Corp. 2007
 | 
						|
 *
 | 
						|
 * Authors:
 | 
						|
 *  Anthony Liguori   <aliguori@us.ibm.com>
 | 
						|
 *
 | 
						|
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 | 
						|
 * the COPYING file in the top-level directory.
 | 
						|
 *
 | 
						|
 */
 | 
						|
 | 
						|
#ifndef QEMU_VIRTIO_BLK_H
 | 
						|
#define QEMU_VIRTIO_BLK_H
 | 
						|
 | 
						|
#include "standard-headers/linux/virtio_blk.h"
 | 
						|
#include "hw/virtio/virtio.h"
 | 
						|
#include "hw/block/block.h"
 | 
						|
#include "sysemu/iothread.h"
 | 
						|
#include "sysemu/block-backend.h"
 | 
						|
#include "sysemu/block-ram-registrar.h"
 | 
						|
#include "qom/object.h"
 | 
						|
 | 
						|
#define TYPE_VIRTIO_BLK "virtio-blk-device"
 | 
						|
OBJECT_DECLARE_SIMPLE_TYPE(VirtIOBlock, VIRTIO_BLK)
 | 
						|
 | 
						|
/* This is the last element of the write scatter-gather list */
 | 
						|
struct virtio_blk_inhdr
 | 
						|
{
 | 
						|
    unsigned char status;
 | 
						|
};
 | 
						|
 | 
						|
#define VIRTIO_BLK_AUTO_NUM_QUEUES UINT16_MAX
 | 
						|
 | 
						|
struct VirtIOBlkConf
 | 
						|
{
 | 
						|
    BlockConf conf;
 | 
						|
    IOThread *iothread;
 | 
						|
    char *serial;
 | 
						|
    uint32_t request_merging;
 | 
						|
    uint16_t num_queues;
 | 
						|
    uint16_t queue_size;
 | 
						|
    bool seg_max_adjust;
 | 
						|
    bool report_discard_granularity;
 | 
						|
    uint32_t max_discard_sectors;
 | 
						|
    uint32_t max_write_zeroes_sectors;
 | 
						|
    bool x_enable_wce_if_config_wce;
 | 
						|
};
 | 
						|
 | 
						|
struct VirtIOBlockDataPlane;
 | 
						|
 | 
						|
struct VirtIOBlockReq;
 | 
						|
struct VirtIOBlock {
 | 
						|
    VirtIODevice parent_obj;
 | 
						|
    BlockBackend *blk;
 | 
						|
    void *rq;
 | 
						|
    VirtIOBlkConf conf;
 | 
						|
    unsigned short sector_mask;
 | 
						|
    bool original_wce;
 | 
						|
    VMChangeStateEntry *change;
 | 
						|
    bool dataplane_disabled;
 | 
						|
    bool dataplane_started;
 | 
						|
    struct VirtIOBlockDataPlane *dataplane;
 | 
						|
    uint64_t host_features;
 | 
						|
    size_t config_size;
 | 
						|
    BlockRAMRegistrar blk_ram_registrar;
 | 
						|
};
 | 
						|
 | 
						|
typedef struct VirtIOBlockReq {
 | 
						|
    VirtQueueElement elem;
 | 
						|
    int64_t sector_num;
 | 
						|
    VirtIOBlock *dev;
 | 
						|
    VirtQueue *vq;
 | 
						|
    IOVDiscardUndo inhdr_undo;
 | 
						|
    IOVDiscardUndo outhdr_undo;
 | 
						|
    struct virtio_blk_inhdr *in;
 | 
						|
    struct virtio_blk_outhdr out;
 | 
						|
    QEMUIOVector qiov;
 | 
						|
    size_t in_len;
 | 
						|
    struct VirtIOBlockReq *next;
 | 
						|
    struct VirtIOBlockReq *mr_next;
 | 
						|
    BlockAcctCookie acct;
 | 
						|
} VirtIOBlockReq;
 | 
						|
 | 
						|
#define VIRTIO_BLK_MAX_MERGE_REQS 32
 | 
						|
 | 
						|
typedef struct MultiReqBuffer {
 | 
						|
    VirtIOBlockReq *reqs[VIRTIO_BLK_MAX_MERGE_REQS];
 | 
						|
    unsigned int num_reqs;
 | 
						|
    bool is_write;
 | 
						|
} MultiReqBuffer;
 | 
						|
 | 
						|
void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq);
 | 
						|
 | 
						|
#endif
 |