Skip to contentSkip to conversation

Conceptual bug on SoundWire probe/remove?

DISCUSSION Pierre-Louis Bossart 5 participants 9 messages Mar 23, 2022 – Mar 26, 2022

lore
Share & tools
lore
#0 lore raw

Hi,
I could use feedback/guidance on a possible conceptual bug in the SoundWire probe and bus handling.

When we probe a driver, the code does this:

static int sdw_drv_probe(struct device *dev)

{
	struct sdw_slave *slave = dev_to_sdw_dev(dev);
	struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
	const struct sdw_device_id *id;
	const char *name;
	int ret;

	/*
	 * fw description is mandatory to bind
	 */
	if (!dev->fwnode)
		return -ENODEV;

	if (!IS_ENABLED(CONFIG_ACPI) && !dev->of_node)
		return -ENODEV;

	id = sdw_get_device_id(slave, drv);
	if (!id)
		return -ENODEV;

	slave->ops = drv->ops;

The last line is the problematic one. If at some point, the user does an rmmod and unbinds the SoundWire codec driver, the .remove will be called and the 'drv' will no longer be valid, but we will still have a reference to drv->ops and use that pointer in the bus code, e.g.

		/* Update the Slave driver */
		if (slave_notify && slave->ops &&
		    slave->ops->interrupt_callback) {
			slave_intr.sdca_cascade = sdca_cascade;
			slave_intr.control_port = clear;
			memcpy(slave_intr.port, &port_status,
			       sizeof(slave_intr.port));

			slave->ops->interrupt_callback(slave,
&slave_intr);
		}

I noodled with a potential fix in
https://github.com/thesofproject/linux/pull/3534/commits/82d64fb0fd39b532263f060a8ec86e47e9ab305b

where I force-reset this slave->ops pointer, but it is likely to be very racy.

We probably need to avoid such references, or have a clean mechanism to unbind, e.g. with all commands and interrupts stopped while the codec driver .remove routine is handled.

Initial error reports at https://github.com/thesofproject/linux/issues/3531

Suggestions and comments welcome, thanks!
-Pierre

#1 lore raw
Quoted 4 lines · Pierre-Louis Bossart

The last line is the problematic one. If at some point, the user does an rmmod and unbinds the SoundWire codec driver, the .remove will be called and the 'drv' will no longer be valid, but we will still have a reference to drv->ops and use that pointer in the bus code, e.g.

...

Quoted 6 lines

where I force-reset this slave->ops pointer, but it is likely to be very racy.

We probably need to avoid such references, or have a clean mechanism to unbind, e.g. with all commands and interrupts stopped while the codec driver .remove routine is handled.

Your analysis seems pretty much spot on - you'll need locking or other measures to make sure there are no live callbacks from the bus while the device is being removed. It's a fairly standard problem unfortunately.

#2 lore raw
In reply to Pierre-Louis Bossart
Quoted 19 lines · Pierre-Louis Bossart

Hi,
I could use feedback/guidance on a possible conceptual bug in the SoundWire probe and bus handling.

When we probe a driver, the code does this:

static int sdw_drv_probe(struct device *dev)
{
    struct sdw_slave *slave = dev_to_sdw_dev(dev); struct sdw_driver *drv = drv_to_sdw_driver(dev->driver); const struct sdw_device_id *id;
    const char *name;
    int ret;

    /*
     * fw description is mandatory to bind
     */
    if (!dev->fwnode)
        return -ENODEV;

Quoted 8 lines

    if (!IS_ENABLED(CONFIG_ACPI) && !dev->of_node) return -ENODEV;

    id = sdw_get_device_id(slave, drv);
    if (!id)
        return -ENODEV;

    slave->ops = drv->ops;

may be add try_module_get(dev->driver->owner) here, which should prevent the module from unloading in the first place.

--srini

Quoted 27 lines

The last line is the problematic one. If at some point, the user does an rmmod and unbinds the SoundWire codec driver, the .remove will be called and the 'drv' will no longer be valid, but we will still have a reference to drv->ops and use that pointer in the bus code, e.g.

        /* Update the Slave driver */
        if (slave_notify && slave->ops && slave->ops->interrupt_callback) { slave_intr.sdca_cascade = sdca_cascade; slave_intr.control_port = clear; memcpy(slave_intr.port, &port_status, sizeof(slave_intr.port));

            slave->ops->interrupt_callback(slave,
&slave_intr);
        }

I noodled with a potential fix in
https://github.com/thesofproject/linux/pull/3534/commits/82d64fb0fd39b532263f060a8ec86e47e9ab305b

where I force-reset this slave->ops pointer, but it is likely to be very racy.

We probably need to avoid such references, or have a clean mechanism to unbind, e.g. with all commands and interrupts stopped while the codec driver .remove routine is handled.

Initial error reports at https://github.com/thesofproject/linux/issues/3531

Suggestions and comments welcome, thanks!
-Pierre

#3 lore raw
Quoted 27 lines · Srinivas Kandagatla
Quoted 23 lines · Pierre-Louis Bossart

Hi,
I could use feedback/guidance on a possible conceptual bug in the SoundWire probe and bus handling.

When we probe a driver, the code does this:

static int sdw_drv_probe(struct device *dev)
{
    struct sdw_slave *slave = dev_to_sdw_dev(dev); struct sdw_driver *drv = drv_to_sdw_driver(dev->driver); const struct sdw_device_id *id;
    const char *name;
    int ret;

    /*
     * fw description is mandatory to bind
     */
    if (!dev->fwnode)
        return -ENODEV;

    if (!IS_ENABLED(CONFIG_ACPI) && !dev->of_node) return -ENODEV;

Quoted 8 lines
Quoted 5 lines

    id = sdw_get_device_id(slave, drv);
    if (!id)
        return -ENODEV;

    slave->ops = drv->ops;

may be add try_module_get(dev->driver->owner) here, which should prevent the module from unloading in the first place.

The module refcount helps only partially, unfortunately. The unbindig via sysfs is still allowed and it hits the very same problem.

Takashi

#4 lore raw
In reply to Pierre-Louis Bossart
Quoted 20 lines · Pierre-Louis Bossart

Hi,
I could use feedback/guidance on a possible conceptual bug in the SoundWire probe and bus handling.

When we probe a driver, the code does this:

static int sdw_drv_probe(struct device *dev)

{
	struct sdw_slave *slave = dev_to_sdw_dev(dev);
	struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
	const struct sdw_device_id *id;
	const char *name;
	int ret;

	/*
	 * fw description is mandatory to bind
	 */
	if (!dev->fwnode)
		return -ENODEV;
Quoted 8 lines
	if (!IS_ENABLED(CONFIG_ACPI) && !dev->of_node)
		return -ENODEV;

	id = sdw_get_device_id(slave, drv);
	if (!id)
		return -ENODEV;

	slave->ops = drv->ops;

That is wrong and should never happen as you lost all reference counting. Please don't do that.

Quoted 17 lines

The last line is the problematic one. If at some point, the user does an rmmod and unbinds the SoundWire codec driver, the .remove will be called and the 'drv' will no longer be valid, but we will still have a reference to drv->ops and use that pointer in the bus code, e.g.

		/* Update the Slave driver */
		if (slave_notify && slave->ops &&
		    slave->ops->interrupt_callback) {
			slave_intr.sdca_cascade = sdca_cascade;
			slave_intr.control_port = clear;
			memcpy(slave_intr.port, &port_status,
			       sizeof(slave_intr.port));

			slave->ops->interrupt_callback(slave,
&slave_intr);
		}
Quoted 5 lines

I noodled with a potential fix in
https://github.com/thesofproject/linux/pull/3534/commits/82d64fb0fd39b532263f060a8ec86e47e9ab305b

where I force-reset this slave->ops pointer, but it is likely to be very racy.

Just properly reference count things and this should be ok. You can NEVER just save off a pointer to a random thing that you do not own without increasing the reference count, otherwise bad things will happen. It's always been this way.

thanks,

greg k-h

#5 lore raw
Quoted 24 lines · Greg Kroah-Hartman
Quoted 23 lines · Pierre-Louis Bossart

Hi,
I could use feedback/guidance on a possible conceptual bug in the SoundWire probe and bus handling.

When we probe a driver, the code does this:

static int sdw_drv_probe(struct device *dev)

{
	struct sdw_slave *slave = dev_to_sdw_dev(dev);
	struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
	const struct sdw_device_id *id;
	const char *name;
	int ret;

	/*
	 * fw description is mandatory to bind
	 */
	if (!dev->fwnode)
		return -ENODEV;

	if (!IS_ENABLED(CONFIG_ACPI) && !dev->of_node)
		return -ENODEV;
Quoted 8 lines
Quoted 5 lines
	id = sdw_get_device_id(slave, drv);
	if (!id)
		return -ENODEV;

	slave->ops = drv->ops;

That is wrong and should never happen as you lost all reference counting. Please don't do that.

ok, so I think we all agree on the issue. It's not new code, it's been there since December 2017 and 4.16

Quoted 20 lines
Quoted 20 lines

The last line is the problematic one. If at some point, the user does an rmmod and unbinds the SoundWire codec driver, the .remove will be called and the 'drv' will no longer be valid, but we will still have a reference to drv->ops and use that pointer in the bus code, e.g.

		/* Update the Slave driver */
		if (slave_notify && slave->ops &&
		    slave->ops->interrupt_callback) {
			slave_intr.sdca_cascade = sdca_cascade;
			slave_intr.control_port = clear;
			memcpy(slave_intr.port, &port_status,
			       sizeof(slave_intr.port));

			slave->ops->interrupt_callback(slave,
&slave_intr);
		}

I noodled with a potential fix in
https://github.com/thesofproject/linux/pull/3534/commits/82d64fb0fd39b532263f060a8ec86e47e9ab305b

Quoted 7 lines
Quoted 2 lines

where I force-reset this slave->ops pointer, but it is likely to be very racy.

Just properly reference count things and this should be ok. You can NEVER just save off a pointer to a random thing that you do not own without increasing the reference count, otherwise bad things will happen. It's always been this way.

but I am not following the reference count recommendation in that specific case. If we increase a reference count in the probe, wouldn't that prevent the unbind/remove?

#6 lore raw
Quoted 31 lines · Pierre-Louis Bossart
Quoted 28 lines · Greg Kroah-Hartman
Quoted 27 lines · Pierre-Louis Bossart

Hi,
I could use feedback/guidance on a possible conceptual bug in the SoundWire probe and bus handling.

When we probe a driver, the code does this:

static int sdw_drv_probe(struct device *dev)

{
	struct sdw_slave *slave = dev_to_sdw_dev(dev);
	struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
	const struct sdw_device_id *id;
	const char *name;
	int ret;

	/*
	 * fw description is mandatory to bind
	 */
	if (!dev->fwnode)
		return -ENODEV;

	if (!IS_ENABLED(CONFIG_ACPI) && !dev->of_node)
		return -ENODEV;

	id = sdw_get_device_id(slave, drv);
	if (!id)
		return -ENODEV;
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
	slave->ops = drv->ops;

That is wrong and should never happen as you lost all reference counting. Please don't do that.

ok, so I think we all agree on the issue. It's not new code, it's been there since December 2017 and 4.16

It's hard to notice that in code review :(

Quoted 23 lines
Quoted 23 lines
Quoted 22 lines

The last line is the problematic one. If at some point, the user does an rmmod and unbinds the SoundWire codec driver, the .remove will be called and the 'drv' will no longer be valid, but we will still have a reference to drv->ops and use that pointer in the bus code, e.g.

		/* Update the Slave driver */
		if (slave_notify && slave->ops &&
		    slave->ops->interrupt_callback) {
			slave_intr.sdca_cascade = sdca_cascade;
			slave_intr.control_port = clear;
			memcpy(slave_intr.port, &port_status,
			       sizeof(slave_intr.port));

			slave->ops->interrupt_callback(slave,
&slave_intr);
		}

I noodled with a potential fix in
https://github.com/thesofproject/linux/pull/3534/commits/82d64fb0fd39b532263f060a8ec86e47e9ab305b

where I force-reset this slave->ops pointer, but it is likely to be very racy.

Quoted 8 lines
Quoted 4 lines

Just properly reference count things and this should be ok. You can NEVER just save off a pointer to a random thing that you do not own without increasing the reference count, otherwise bad things will happen. It's always been this way.

but I am not following the reference count recommendation in that specific case. If we increase a reference count in the probe, wouldn't that prevent the unbind/remove?

Depends on what you are increasing the reference count of :)

bind/unbind has nothing to do with the reference count of objects, it only prevents the devices/whatever from being removed from the system (hopefully.)

When doing "give me the driver ops of that driver over there" you have to be VERY careful. It's a very uncommon pattern that I can't think of anyone else doing outside of a bus logic. I don't recommend it, instead grab references to the devices themselves and go through the device and what is bound/unbound to that. Also keep things in sync when you grab those other devices, that can also get messy.

How about just not doing this at all?

#7 lore raw
Quoted 80 lines · Greg Kroah-Hartman
Quoted 31 lines · Pierre-Louis Bossart
Quoted 28 lines · Greg Kroah-Hartman
Quoted 27 lines · Pierre-Louis Bossart

Hi,
I could use feedback/guidance on a possible conceptual bug in the SoundWire probe and bus handling.

When we probe a driver, the code does this:

static int sdw_drv_probe(struct device *dev)

{
	struct sdw_slave *slave = dev_to_sdw_dev(dev);
	struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
	const struct sdw_device_id *id;
	const char *name;
	int ret;

	/*
	 * fw description is mandatory to bind
	 */
	if (!dev->fwnode)
		return -ENODEV;

	if (!IS_ENABLED(CONFIG_ACPI) && !dev->of_node)
		return -ENODEV;

	id = sdw_get_device_id(slave, drv);
	if (!id)
		return -ENODEV;
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
	slave->ops = drv->ops;

That is wrong and should never happen as you lost all reference counting. Please don't do that.

ok, so I think we all agree on the issue. It's not new code, it's been there since December 2017 and 4.16

It's hard to notice that in code review :(

Quoted 23 lines
Quoted 23 lines
Quoted 22 lines

The last line is the problematic one. If at some point, the user does an rmmod and unbinds the SoundWire codec driver, the .remove will be called and the 'drv' will no longer be valid, but we will still have a reference to drv->ops and use that pointer in the bus code, e.g.

		/* Update the Slave driver */
		if (slave_notify && slave->ops &&
		    slave->ops->interrupt_callback) {
			slave_intr.sdca_cascade = sdca_cascade;
			slave_intr.control_port = clear;
			memcpy(slave_intr.port, &port_status,
			       sizeof(slave_intr.port));

			slave->ops->interrupt_callback(slave,
&slave_intr);
		}

I noodled with a potential fix in
https://github.com/thesofproject/linux/pull/3534/commits/82d64fb0fd39b532263f060a8ec86e47e9ab305b

where I force-reset this slave->ops pointer, but it is likely to be very racy.

Quoted 8 lines
Quoted 4 lines

Just properly reference count things and this should be ok. You can NEVER just save off a pointer to a random thing that you do not own without increasing the reference count, otherwise bad things will happen. It's always been this way.

but I am not following the reference count recommendation in that specific case. If we increase a reference count in the probe, wouldn't that prevent the unbind/remove?

Depends on what you are increasing the reference count of :)

bind/unbind has nothing to do with the reference count of objects, it only prevents the devices/whatever from being removed from the system (hopefully.)

Quoted 8 lines

When doing "give me the driver ops of that driver over there" you have to be VERY careful. It's a very uncommon pattern that I can't think of anyone else doing outside of a bus logic. I don't recommend it, instead grab references to the devices themselves and go through the device and what is bound/unbound to that. Also keep things in sync when you grab those other devices, that can also get messy.

How about just not doing this at all?

That's what I was exploring, I am not sure at all we need to use this pointer at all.

Maybe we should go back to the problem statement:

The SoundWire codec driver provides a set of callbacks that need to be invoked from bus layer when events are detected (bus reconfiguration, clock about to be stopped, attach/detach and interrupt).

The devices are created upfront, and if there's no driver things will work just fine. When we bind a driver, then these callbacks are detected as not null.

The part that I am not familiar with is how we can prevent a remove/unbind while invoking the callbacks.

Maybe we can indeed increase the refcount on the device, follow the device->driver->ops pointers and reduce the refcount once the callback completes.

#8 lore raw
Quoted 97 lines · Pierre-Louis Bossart
Quoted 80 lines · Greg Kroah-Hartman
Quoted 31 lines · Pierre-Louis Bossart
Quoted 28 lines · Greg Kroah-Hartman
Quoted 27 lines · Pierre-Louis Bossart

Hi,
I could use feedback/guidance on a possible conceptual bug in the SoundWire probe and bus handling.

When we probe a driver, the code does this:

static int sdw_drv_probe(struct device *dev)

{
	struct sdw_slave *slave = dev_to_sdw_dev(dev);
	struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
	const struct sdw_device_id *id;
	const char *name;
	int ret;

	/*
	 * fw description is mandatory to bind
	 */
	if (!dev->fwnode)
		return -ENODEV;

	if (!IS_ENABLED(CONFIG_ACPI) && !dev->of_node)
		return -ENODEV;

	id = sdw_get_device_id(slave, drv);
	if (!id)
		return -ENODEV;
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
	slave->ops = drv->ops;

That is wrong and should never happen as you lost all reference counting. Please don't do that.

ok, so I think we all agree on the issue. It's not new code, it's been there since December 2017 and 4.16

It's hard to notice that in code review :(

Quoted 23 lines
Quoted 23 lines
Quoted 22 lines

The last line is the problematic one. If at some point, the user does an rmmod and unbinds the SoundWire codec driver, the .remove will be called and the 'drv' will no longer be valid, but we will still have a reference to drv->ops and use that pointer in the bus code, e.g.

		/* Update the Slave driver */
		if (slave_notify && slave->ops &&
		    slave->ops->interrupt_callback) {
			slave_intr.sdca_cascade = sdca_cascade;
			slave_intr.control_port = clear;
			memcpy(slave_intr.port, &port_status,
			       sizeof(slave_intr.port));

			slave->ops->interrupt_callback(slave,
&slave_intr);
		}

I noodled with a potential fix in
https://github.com/thesofproject/linux/pull/3534/commits/82d64fb0fd39b532263f060a8ec86e47e9ab305b

where I force-reset this slave->ops pointer, but it is likely to be very racy.

Quoted 8 lines
Quoted 4 lines

Just properly reference count things and this should be ok. You can NEVER just save off a pointer to a random thing that you do not own without increasing the reference count, otherwise bad things will happen. It's always been this way.

but I am not following the reference count recommendation in that specific case. If we increase a reference count in the probe, wouldn't that prevent the unbind/remove?

Depends on what you are increasing the reference count of :)

bind/unbind has nothing to do with the reference count of objects, it only prevents the devices/whatever from being removed from the system (hopefully.)

Quoted 8 lines

When doing "give me the driver ops of that driver over there" you have to be VERY careful. It's a very uncommon pattern that I can't think of anyone else doing outside of a bus logic. I don't recommend it, instead grab references to the devices themselves and go through the device and what is bound/unbound to that. Also keep things in sync when you grab those other devices, that can also get messy.

How about just not doing this at all?

That's what I was exploring, I am not sure at all we need to use this pointer at all.

Maybe we should go back to the problem statement:

Quoted 7 lines

The SoundWire codec driver provides a set of callbacks that need to be invoked from bus layer when events are detected (bus reconfiguration, clock about to be stopped, attach/detach and interrupt).

The devices are created upfront, and if there's no driver things will work just fine. When we bind a driver, then these callbacks are detected as not null.

Not null from where? And why is this different than any other bus?

Quoted 2 lines

The part that I am not familiar with is how we can prevent a remove/unbind while invoking the callbacks.

Who is calling these callbacks? From what path? bind/unbind happens through the driver core, into your bus code. So you have a lock for this, it should be serialized and should not be happening while some other bus callback is happening from the driver core.

If not, what specific path are you worried about here?

Quoted 3 lines

Maybe we can indeed increase the refcount on the device, follow the device->driver->ops pointers and reduce the refcount once the callback completes.

No, again, refcounts hold the device in place in memory, they have NOTHING to do with the driver binding to the device. Even if you have a refcount on a device, the driver can be unbound.

thanks,

greg k-h