Skip to contentSkip to conversation

Question about scsi device hotplug (e.g scsi-hd)

DISCUSSION Maxim Levitsky 4 participants 9 messages Mar 31, 2020 – Apr 3, 2020

lore
Share & tools
lore
#0 lore raw

Hi!

I recently investigated an interesting issue related to repeated scsi-hd hotplug/hotunplug. The bugzilla for it is here:
https://bugzilla.redhat.com/show_bug.cgi?id=1812399

In nutshell the issue that I think that I found and I would like to ask your opinion on it, since I don't have enough experience to be 100% sure that I didn't miss something is this:

When a new device is hotplugged via monitor, the qdev_device_add first puts the device on the bus where the user requested it to be, and then calls the device's .realize.

However for scsi bus, each time a new request is sent from the guest, the scsi adapter drivers (e.g virtio-scsi) call scsi_device_find to find the LUN's driver to dispatch the request to, and scsi_device_find will return the added device as soon as it is placed on the bus.

Thus between the point when the new device is placed on the bus and until the end of the .realize, the device can be accessed by the guest when it is not yet realized or partially realized as happens in the bugreport.

What do you think about it?

Best regards,
Maxim Levitsky

#1 lore raw
Quoted 16 lines · Maxim Levitsky

Hi!

I recently investigated an interesting issue related to repeated scsi-hd hotplug/hotunplug. The bugzilla for it is here:
https://bugzilla.redhat.com/show_bug.cgi?id=1812399

In nutshell the issue that I think that I found and I would like to ask your opinion on it, since I don't have enough experience to be 100% sure that I didn't miss something is this:

When a new device is hotplugged via monitor, the qdev_device_add first puts the device on the bus where the user requested it to be, and then calls the device's .realize.

However for scsi bus, each time a new request is sent from the guest, the scsi adapter drivers (e.g virtio-scsi) call scsi_device_find to find the LUN's driver to dispatch the request to, and scsi_device_find will return the added device as soon as it is placed on the bus.

Quoted 5 lines

Thus between the point when the new device is placed on the bus and until the end of the .realize, the device can be accessed by the guest when it is not yet realized or partially realized as happens in the bugreport.

What do you think about it?

Maybe aio_disable_external() is needed to postpone device emulation until after realize has finished?

Virtqueue kick ioeventfds are marked "external" and won't be processed while external events are disabled. See also virtio_queue_aio_set_host_notifier_handler() -> aio_set_event_notifier().

Stefan

#2 lore raw
Quoted 9 lines · Stefan Hajnoczi
Quoted 1 line

What do you think about it?

Maybe aio_disable_external() is needed to postpone device emulation until after realize has finished?

Virtqueue kick ioeventfds are marked "external" and won't be processed while external events are disabled. See also virtio_queue_aio_set_host_notifier_handler() -> aio_set_event_notifier().

Yes, I think Stefan is right.

Paolo

#3 lore raw
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Stefan Hajnoczi
Quoted 1 line

What do you think about it?

Maybe aio_disable_external() is needed to postpone device emulation until after realize has finished?

Quoted 6 lines
Quoted 4 lines

Virtqueue kick ioeventfds are marked "external" and won't be processed while external events are disabled. See also virtio_queue_aio_set_host_notifier_handler() -> aio_set_event_notifier().

Yes, I think Stefan is right.

Is this issue limited to SCSI devices?

#4 lore raw
Quoted 8 lines · Markus Armbruster
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Stefan Hajnoczi
Quoted 1 line

What do you think about it?

Maybe aio_disable_external() is needed to postpone device emulation until after realize has finished?

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Virtqueue kick ioeventfds are marked "external" and won't be processed while external events are disabled. See also virtio_queue_aio_set_host_notifier_handler() -> aio_set_event_notifier().

Yes, I think Stefan is right.

Is this issue limited to SCSI devices?

Yes, because they (and more specifically virtio-scsi) are the only ones that can be processed

1) outside the BQL

2) by a device other than themselves (in this case, by the adapter).

Paolo

#5 lore raw
In reply to Paolo Bonzini
Quoted 5 lines · Paolo Bonzini
Quoted 4 lines · Stefan Hajnoczi
Quoted 1 line

What do you think about it?

Maybe aio_disable_external() is needed to postpone device emulation until after realize has finished?

Isn't that virtio specific? In theory this issue can touch any driver that behaves similar to scsi.

Also due to racing, the request might already be in virtqueue and the virtio-scsi iothread might have already fetched it when we place the device on the bus.

In fact I don't see any locking in bus_add_child / scsi_device_find so in fact if the timing is right and iothread calls the scsi_device_find while main thread adds the device on the bus that will cause the whole thing go south.

IMHO the right solution for this is first to realize the device and then place it on the bus, and even that I am not sure that will fix the race completely.

IMHO the correct solution here is to stop querying the bus children on the guest controlled IO path (scsi_device_find) but rather use the hotplug handlers to
store the active luns of the scsi device in parallel (and with proper locking).

Best regards,
Maxim Levitsky

Quoted 10 lines
Quoted 5 lines

Virtqueue kick ioeventfds are marked "external" and won't be processed while external events are disabled. See also virtio_queue_aio_set_host_notifier_handler() -> aio_set_event_notifier().

Yes, I think Stefan is right.

Paolo

#6 lore raw
Quoted 9 lines · Maxim Levitsky
Quoted 5 lines · Paolo Bonzini
Quoted 4 lines · Stefan Hajnoczi
Quoted 1 line

What do you think about it?

Maybe aio_disable_external() is needed to postpone device emulation until after realize has finished?

Isn't that virtio specific? In theory this issue can touch any driver that behaves similar to scsi.

No, aio_disable_external() is a general solution for suspending file descriptor handlers. In practice virtio is the main user of external fds (ioeventfd). Devices that do not use ioeventfd will not perform device emulation from the AioContext event loop and are therefore safe.

Quoted 6 lines

Also due to racing, the request might already be in virtqueue and the virtio-scsi iothread might have already fetched it when we place the device on the bus.

In fact I don't see any locking in bus_add_child / scsi_device_find so in fact if the timing is right and iothread calls the scsi_device_find while main thread adds the device on the bus that will cause the whole thing go south.

Thread-safety with IOThreads is a separate issue and also worth fixing. It requires a different and complementary solution from the single-threaded ioeventfd bug we've been discussing so far.

It looks like virtio_scsi_acquire/release() were intended to provide thread-safety for virtio-scsi:

  static bool virtio_scsi_data_plane_handle_cmd(VirtIODevice *vdev,
                                                VirtQueue *vq)
  {
      bool progress;
      VirtIOSCSI *s = VIRTIO_SCSI(vdev);
      virtio_scsi_acquire(s);
      assert(s->ctx && s->dataplane_started);
      progress = virtio_scsi_handle_cmd_vq(s, vq);
      virtio_scsi_release(s);
      return progress;

}

If the monitor code calls virtio_scsi_acquire() then further request processing will have to wait until the monitor code is finished.

Quoted 6 lines

IMHO the right solution for this is first to realize the device and then place it on the bus, and even that I am not sure that will fix the race completely.

IMHO the correct solution here is to stop querying the bus children on the guest controlled IO path (scsi_device_find) but rather use the hotplug handlers to
store the active luns of the scsi device in parallel (and with proper locking).

It would be nice to have a general solution in qdev core that makes thread-safe hotplug easy for all devices.

Stefan

#7 lore raw

Stefan Hajnoczi <[email protected]> writes:

[...]

Quoted 2 lines

It would be nice to have a general solution in qdev core that makes thread-safe hotplug easy for all devices.

Excuse my ignorance / confusion...

The "realize" concept exists to enable

create device
configure / wire up step by step without impact on the guest realize, device becomes "real" atomically

How and why does this break down with threads?

#8 lore raw
Quoted 7 lines · Markus Armbruster

Stefan Hajnoczi <[email protected]> writes:

[...]

Quoted 2 lines

It would be nice to have a general solution in qdev core that makes thread-safe hotplug easy for all devices.

Excuse my ignorance / confusion...

Quoted 7 lines

The "realize" concept exists to enable

create device
configure / wire up step by step without impact on the guest realize, device becomes "real" atomically

How and why does this break down with threads?

After more discussions with Maxim, it's a bug in the SCSI layer. It looks for devices without checking that they have been realized.

Paolo