Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions sound/soc/sof/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,15 @@ int snd_sof_device_shutdown(struct device *dev)
{
struct snd_sof_dev *sdev = dev_get_drvdata(dev);

/*
* Let's check if the sdev is valid or not.
* If probe is not completed and sdev is NULL,
* it means audio is not enabled and we should skip
* the shutdown.
*/
if (!sdev)
return 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this is wrong.

int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data)
{
	struct snd_sof_dev *sdev;

	sdev = devm_kzalloc(dev, sizeof(*sdev), GFP_KERNEL);
	if (!sdev)
		return -ENOMEM;

	/* initialize sof device */
	sdev->dev = dev;

	/* initialize default DSP power state */
	sdev->dsp_power_state.state = SOF_DSP_PM_D0;

	sdev->pdata = plat_data;
	sdev->first_boot = true;
	sdev->fw_state = SOF_FW_BOOT_NOT_STARTED;
#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
	sdev->extractor_stream_tag = SOF_PROBE_INVALID_NODE_ID;
#endif
	dev_set_drvdata(dev, sdev);

so if the probe fails in the workqueue, sdev would still be set. That's not a valid indicator of success.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is partially valid. We should also check is sdev->probe_complete additionally.

@ranj063 ranj063 Mar 10, 2021 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking a bit more about this, we should ask the question "would shutdown() ever get called if probe() is not successful?". If the answer is no, then sdev will never be NULL here, right? But sof_probe_continue() could still have failed and in that case the probe_complete is the right indicator for success.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this is wrong.

so if the probe fails in the workqueue, sdev would still be set. That's not a valid indicator of success.

@plbossart snd_sof_device_probe() is called by sof_pci_probe(). There are chances that sof_pci_probe() fails before snd_sof_device_probe() is called.

@libinyang libinyang Mar 11, 2021 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is partially valid. We should also check is sdev->probe_complete additionally.

@ranj063 My first version patch does check the sdev->probe_complete. After a second thought, the shutdown() will set sdev->system_suspend_target = SOF_SUSPEND_S3 and call snd_sof_suspend(sdev->dev) directly in the PR #2768. In snd_sof_suspend(), it will check sdev->fw_state != SOF_FW_BOOT_COMPLETE (something like checking the probe_complete). If the probe() fails, it will directly return in snd_sof_suspend().

thinking a bit more about this, we should ask the question "would shutdown() ever get called if probe() is not successful?". If the answer is no, then sdev will never be NULL here, right? But sof_probe_continue() could still have failed and in that case the probe_complete is the right indicator for success.

Based on the issue reported by Pelton, Kyle D, the shutdown() callback will still be called even when the probe fails when loading the FW.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and oh btw I tested this on our TGL chromebook and confirmed that shutdown() is only called when probe() is successful.

@libinyang libinyang Mar 12, 2021 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plbossart and @ranj063 some updates based on my investigations:

  1. some background knowledge
    When reboot, the kernel will check dev->driver and call dev->driver->shutdown() if there is no dev->bus->shutdown() defined. And if probe returns error, dev->driver will be set to NULL.

  2. Will shutdown be called if probe fails in SOF driver?
    In some cases, yes. In snd_sof_device_probe(), we may use schedule_work(&sdev->probe_work). In this case, snd_sof_device_probe() always return 0 even the probe_work fails. So the dev->drv won't be cleared.

  3. Is the sdev check necessary?
    No. If probe is successfully, sdev is always there. If probe failed in the probe_work, it means sdev has already been allocated successfully. In all other conditions, sof_pci_probe() will return failure and dev->drv is cleared and the shutdown won't be called.

  4. Some further investigations and thoughts.
    I checked another driver in kernel, wcnss_ctrl.c. It did the same operation as our SOF. It will use schedule_work for the probe and it return 0 directly. In the scheduled work, the probe may fail and it doesn't do any further clearance.
    But as dd 's flow, if the driver fails in probe while it returns 0, like our case, dd will think the probe is OK and the driver will be bound to the device. We should take care of this situation.
    I saw there is a function driver_detach() in dd.c. If we call this function in scheduled work when probe failed, it may clean up the things. But I didn't see any special driver call this function. It is only called by bus.c

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plbossart and @ranj063 some updates based on my investigations:

  1. some background knowledge
    When reboot, the kernel will check dev->driver and call dev->driver->shutdown() if there is no dev->bus->shutdown() defined. And if probe returns error, dev->driver will be set to NULL.

this is a PCI bus, there is a .shutdown for that bus.

static void pci_device_shutdown(struct device *dev)
{
	struct pci_dev *pci_dev = to_pci_dev(dev);
	struct pci_driver *drv = pci_dev->driver;

	pm_runtime_resume(dev);

	if (drv && drv->shutdown)
		drv->shutdown(pci_dev);
  1. Will shutdown be called if probe fails in SOF driver?
    In some cases, yes. In snd_sof_device_probe(), we may use schedule_work(&sdev->probe_work). In this case, snd_sof_device_probe() always return 0 even the probe_work fails. So the dev->drv won't be cleared.

if the failure happens in the workqueue, then technically the probe didn't fail.

  1. Is the sdev check necessary?
    No. If probe is successfully, sdev is always there. If probe failed in the probe_work, it means sdev has already been allocated successfully. In all other conditions, sof_pci_probe() will return failure and dev->drv is cleared and the shutdown won't be called.

so we agree this test is useless then?

  1. Some further investigations and thoughts.
    I checked another driver in kernel, wcnss_ctrl.c. It did the same operation as our SOF. It will use schedule_work for the probe and it return 0 directly. In the scheduled work, the probe may fail and it doesn't do any further clearance.
    But as dd 's flow, if the driver fails in probe while it returns 0, like our case, dd will think the probe is OK and the driver will be bound to the device. We should take care of this situation.
    I saw there is a function driver_detach() in dd.c. If we call this function in scheduled work when probe failed, it may clean up the things. But I didn't see any special driver call this function. It is only called by bus.c

We need to do the same thing as for the remove case, we need to test if the workqueue succeeded (probe_completed or state > boot_not_started). The workqueue will cleanup all the resources if it fails.

@libinyang libinyang Mar 13, 2021 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Is the sdev check necessary?
    No. If probe is successfully, sdev is always there. If probe failed in the probe_work, it means sdev has already been allocated successfully. In all other conditions, sof_pci_probe() will return failure and dev->drv is cleared and the shutdown won't be called.

so we agree this test is useless then?

Yes, I will close this PR later.

We need to do the same thing as for the remove case, we need to test if the workqueue succeeded (probe_completed or state > boot_not_started). The workqueue will cleanup all the resources if it fails.

@plbossart Do you mean even the resources allocated by devm_xxx? It seems we should call devm_kfree() to free the memory as the driver is not bound to the pci->dev now while the memories allocated by devm_xxx are bound to pci->dev. But it is harmless if don't free it and let kernel to free it when pci->dev is released.

update:
It suddenly occurs to me: We should make the device into D3 before shutdown. If we release the resources, audio may not be able to enter D3 clearly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@libinyang see what I meant in PR #2791

return snd_sof_shutdown(sdev);
}
EXPORT_SYMBOL(snd_sof_device_shutdown);
Expand Down