Skip to content

Commit 7d8ab5b

Browse files
stefanhaRHkevmw
authored andcommitted
virtio-scsi: protect events_dropped field
The block layer can invoke the resize callback from any AioContext that is processing requests. The virtqueue is already protected but the events_dropped field also needs to be protected against races. Cover it using the event virtqueue lock because it is closely associated with accesses to the virtqueue. Signed-off-by: Stefan Hajnoczi <[email protected]> Reviewed-by: Kevin Wolf <[email protected]> Message-ID: <[email protected]> Tested-by: Peter Krempa <[email protected]> Signed-off-by: Kevin Wolf <[email protected]>
1 parent b348ca2 commit 7d8ab5b

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

hw/scsi/virtio-scsi.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,10 @@ static void virtio_scsi_reset(VirtIODevice *vdev)
948948

949949
vs->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE;
950950
vs->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE;
951-
s->events_dropped = false;
951+
952+
WITH_QEMU_LOCK_GUARD(&s->event_lock) {
953+
s->events_dropped = false;
954+
}
952955
}
953956

954957
typedef struct {
@@ -978,14 +981,16 @@ static void virtio_scsi_push_event(VirtIOSCSI *s,
978981
}
979982

980983
req = virtio_scsi_pop_req(s, vs->event_vq, &s->event_lock);
981-
if (!req) {
982-
s->events_dropped = true;
983-
return;
984-
}
984+
WITH_QEMU_LOCK_GUARD(&s->event_lock) {
985+
if (!req) {
986+
s->events_dropped = true;
987+
return;
988+
}
985989

986-
if (s->events_dropped) {
987-
event |= VIRTIO_SCSI_T_EVENTS_MISSED;
988-
s->events_dropped = false;
990+
if (s->events_dropped) {
991+
event |= VIRTIO_SCSI_T_EVENTS_MISSED;
992+
s->events_dropped = false;
993+
}
989994
}
990995

991996
if (virtio_scsi_parse_req(req, 0, sizeof(VirtIOSCSIEvent))) {
@@ -1014,7 +1019,13 @@ static void virtio_scsi_push_event(VirtIOSCSI *s,
10141019

10151020
static void virtio_scsi_handle_event_vq(VirtIOSCSI *s, VirtQueue *vq)
10161021
{
1017-
if (s->events_dropped) {
1022+
bool events_dropped;
1023+
1024+
WITH_QEMU_LOCK_GUARD(&s->event_lock) {
1025+
events_dropped = s->events_dropped;
1026+
}
1027+
1028+
if (events_dropped) {
10181029
VirtIOSCSIEventInfo info = {
10191030
.event = VIRTIO_SCSI_T_NO_EVENT,
10201031
};

include/hw/virtio/virtio-scsi.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ struct VirtIOSCSI {
8282

8383
SCSIBus bus;
8484
int resetting; /* written from main loop thread, read from any thread */
85+
86+
QemuMutex event_lock; /* protects event_vq and events_dropped */
8587
bool events_dropped;
8688

8789
QemuMutex ctrl_lock; /* protects ctrl_vq */
88-
QemuMutex event_lock; /* protects event_vq */
8990

9091
/*
9192
* TMFs deferred to main loop BH. These fields are protected by

0 commit comments

Comments
 (0)