Re: [PATCH v5 2/9] ALSA: virtio: add virtio sound driver

From: Anton Yakovlev
Date: Thu Feb 25 2021 - 06:53:53 EST


On 25.02.2021 11:38, Takashi Iwai wrote:
On Mon, 22 Feb 2021 16:34:37 +0100,
Anton Yakovlev wrote:
+static int virtsnd_find_vqs(struct virtio_snd *snd)
+{
+ struct virtio_device *vdev = snd->vdev;
+ vq_callback_t *callbacks[VIRTIO_SND_VQ_MAX] = {
+ [VIRTIO_SND_VQ_EVENT] = virtsnd_event_notify_cb
+ };
+ const char *names[VIRTIO_SND_VQ_MAX] = {

Shouldn't be static?

Well, yes. Although in this particular case, I do not think it is that
critical. :)


Also it's often const char * const names[] = { ... }
unless you overwrite something.

I tried to use the same type names as in the function prototype.
Otherwise the compiler or static analyzer may complain.


+/**
+ * virtsnd_reset_fn() - Kernel worker's function to reset the device.
+ * @work: Reset device work.
+ *
+ * Context: Process context.
+ */
+static void virtsnd_reset_fn(struct work_struct *work)
+{
+ struct virtio_snd *snd =
+ container_of(work, struct virtio_snd, reset_work);
+ struct virtio_device *vdev = snd->vdev;
+ struct device *dev = &vdev->dev;
+ int rc;
+
+ dev_info(dev, "sound device needs reset\n");
+
+ /*
+ * It seems that the only way to properly reset the device is to remove
+ * and re-create the ALSA sound card device.
+ */
+ rc = device_reprobe(dev);
+ if (rc)
+ dev_err(dev, "failed to reprobe sound device: %d\n", rc);

Now I'm wondering whether it's safe to do that from this place.
Basically device_reprobe() unbinds the device that releases the full
resources once including the devm_* stuff. And this work itself is in
a part of devm allocated resource, so it'll be released there. That
said, we might hit use-after-free... This needs to be verified.

It's safe. Suicide kernel workers are funny but possible things. Since
the kernel itself (AFAIU) assumes such a situation and does not access
the worker structure after the callback function call.


thanks,

Takashi


--
Anton Yakovlev
Senior Software Engineer

OpenSynergy GmbH
Rotherstr. 20, 10245 Berlin