[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 7/7] virtio-scsi: use scsi_device_get
From: |
Maxim Levitsky |
Subject: |
[PATCH v2 7/7] virtio-scsi: use scsi_device_get |
Date: |
Mon, 11 May 2020 19:09:51 +0300 |
This will help us to avoid the scsi device disappearing
after we took a reference to it.
It doesn't by itself forbid case when we try to access
an unrealized device
Suggested-by: Stefan Hajnoczi <address@hidden>
Signed-off-by: Maxim Levitsky <address@hidden>
---
hw/scsi/virtio-scsi.c | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c
index 1cc1fc557c..65cd4186fe 100644
--- a/hw/scsi/virtio-scsi.c
+++ b/hw/scsi/virtio-scsi.c
@@ -33,7 +33,7 @@ static inline int virtio_scsi_get_lun(uint8_t *lun)
return ((lun[2] << 8) | lun[3]) & 0x3FFF;
}
-static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *lun)
+static inline SCSIDevice *virtio_scsi_device_get(VirtIOSCSI *s, uint8_t *lun)
{
SCSIDevice *device = NULL;
@@ -44,7 +44,7 @@ static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI
*s, uint8_t *lun)
return NULL;
}
- device = scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun));
+ device = scsi_device_get(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun));
/*
* This function might run on the IO thread and we might race against
@@ -61,6 +61,8 @@ static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI
*s, uint8_t *lun)
return device;
}
+
+
void virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq, VirtIOSCSIReq *req)
{
VirtIODevice *vdev = VIRTIO_DEVICE(s);
@@ -273,7 +275,7 @@ static inline void virtio_scsi_ctx_check(VirtIOSCSI *s,
SCSIDevice *d)
* case of async cancellation. */
static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req)
{
- SCSIDevice *d = virtio_scsi_device_find(s, req->req.tmf.lun);
+ SCSIDevice *d = virtio_scsi_device_get(s, req->req.tmf.lun);
SCSIRequest *r, *next;
BusChild *kid;
int target;
@@ -387,10 +389,10 @@ static int virtio_scsi_do_tmf(VirtIOSCSI *s,
VirtIOSCSIReq *req)
rcu_read_lock();
QTAILQ_FOREACH_RCU(kid, &s->bus.qbus.children, sibling) {
- d = SCSI_DEVICE(kid->child);
- if (d->channel == 0 && d->id == target) {
- qdev_reset_all(&d->qdev);
- }
+ SCSIDevice *d1 = SCSI_DEVICE(kid->child);
+ if (d1->channel == 0 && d1->id == target) {
+ qdev_reset_all(&d1->qdev);
+ }
}
rcu_read_unlock();
@@ -403,14 +405,17 @@ static int virtio_scsi_do_tmf(VirtIOSCSI *s,
VirtIOSCSIReq *req)
break;
}
+ object_unref(OBJECT(d));
return ret;
incorrect_lun:
req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN;
+ object_unref(OBJECT(d));
return ret;
fail:
req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
+ object_unref(OBJECT(d));
return ret;
}
@@ -581,7 +586,7 @@ static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI
*s, VirtIOSCSIReq *req)
}
}
- d = virtio_scsi_device_find(s, req->req.cmd.lun);
+ d = virtio_scsi_device_get(s, req->req.cmd.lun);
if (!d) {
req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
virtio_scsi_complete_cmd_req(req);
@@ -597,10 +602,12 @@ static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI
*s, VirtIOSCSIReq *req)
req->sreq->cmd.xfer > req->qsgl.size)) {
req->resp.cmd.response = VIRTIO_SCSI_S_OVERRUN;
virtio_scsi_complete_cmd_req(req);
+ object_unref(OBJECT(d));
return -ENOBUFS;
}
scsi_req_ref(req->sreq);
blk_io_plug(d->conf.blk);
+ object_unref(OBJECT(d));
return 0;
}
--
2.17.2
- [PATCH v2 2/7] Implement drain_call_rcu and use it in hmp_device_del, (continued)
- [PATCH v2 2/7] Implement drain_call_rcu and use it in hmp_device_del, Maxim Levitsky, 2020/05/11
- [PATCH v2 3/7] device-core: use RCU for list of childs of a bus, Maxim Levitsky, 2020/05/11
- [PATCH v2 4/7] device-core: use atomic_set on .realized property, Maxim Levitsky, 2020/05/11
- [PATCH v2 5/7] virtio-scsi: don't touch scsi devices that are not yet realized or about to be un-realized, Maxim Levitsky, 2020/05/11
- [PATCH v2 6/7] scsi: Add scsi_device_get, Maxim Levitsky, 2020/05/11
- [PATCH v2 7/7] virtio-scsi: use scsi_device_get,
Maxim Levitsky <=
- Re: [PATCH v2 0/7] RFC/WIP: Fix scsi devices plug/unplug races w.r.t virtio-scsi iothread, no-reply, 2020/05/11
- Re: [PATCH v2 0/7] RFC/WIP: Fix scsi devices plug/unplug races w.r.t virtio-scsi iothread, no-reply, 2020/05/11