Skip to content

ASoC: SOF: add mutex to protect the dsp_power_state access - #2600

Merged
bardliao merged 1 commit into
thesofproject:topic/sof-devfrom
keyonjie:dsp_power_state
Dec 23, 2020
Merged

bardliao merged 1 commit into
thesofproject:topic/sof-devfrom
keyonjie:dsp_power_state

Conversation

@keyonjie

@keyonjie keyonjie commented Dec 4, 2020

Copy link
Copy Markdown

There could be more than one thread read/write the dsp_power_state
simultaneously, add a mutex power_state_access to make sure the access
to it is mutually exclusive.

Signed-off-by: Keyon Jie yang.jie@linux.intel.com

Comment thread sound/soc/sof/ops.h Resolved

@kv2019i kv2019i left a comment

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.

@lyakh point on mutex init needs addressing.

Otherwise looks good. I would prefer a bit more explanation in what kind of scenarios there could be many threads accessing. I guess on Intel platforms, this is primarily hda->d0i3_work work queue. Do we have other cases as well?

@plbossart

Copy link
Copy Markdown
Member

@lyakh point on mutex init needs addressing.

Otherwise looks good. I would prefer a bit more explanation in what kind of scenarios there could be many threads accessing. I guess on Intel platforms, this is primarily hda->d0i3_work work queue. Do we have other cases as well?

Same on my side, I don't get the issue. IIRC the transition to D0i3 will be handled by a system suspend with state==S0, and the exit is with any IPC initiated by the driver. @ranj063 probably need you to chime in on this one.

There could be more than one thread read/write the dsp_power_state
simultaneously (e.g. hda_dsp_d0i3_work and sof_ipc_tx_message), add a
mutex power_state_access to make sure the access to it is mutually
exclusive.

Signed-off-by: Keyon Jie <yang.jie@linux.intel.com>
@keyonjie

keyonjie commented Dec 7, 2020

Copy link
Copy Markdown
Author

@kv2019i @plbossart basically if snd_sof_dsp_set_power_state() is called simultaneously, the issue could happen. e.g.
hda_dsp_d0i3_work() is calling snd_sof_dsp_set_power_state() after 5 Seconds timeout from the WoV is trigger started,
and sof_ipc_tx_message() is calling snd_sof_dsp_set_power_state() before trying to send e.g. trigger stop IPC to the FW (we have logic to make sure the IPC is sent under DSP at D0).

I think this is what scenario we hit exactly here: #2571

So, @kv2019i this PR should be enough to fix the issue #2571 theoretically, @keqiaozhang can you help to run stress with this PR also (don't apply #2581)?

@keyonjie
keyonjie requested a review from lyakh December 7, 2020 01:52
@plbossart

Copy link
Copy Markdown
Member

@kv2019i @plbossart basically if snd_sof_dsp_set_power_state() is called simultaneously, the issue could happen. e.g.
hda_dsp_d0i3_work() is calling snd_sof_dsp_set_power_state() after 5 Seconds timeout from the WoV is trigger started,
and sof_ipc_tx_message() is calling snd_sof_dsp_set_power_state() before trying to send e.g. trigger stop IPC to the FW (we have logic to make sure the IPC is sent under DSP at D0).

I am pretty sure we discussed this case with @ranj063 and I am not convinced we have such a blatant race condition in the code today. Ranjani, please chime in, my memory is fading...

@keyonjie

keyonjie commented Dec 8, 2020 •

Copy link
Copy Markdown
Author

@kv2019i @plbossart basically if snd_sof_dsp_set_power_state() is called simultaneously, the issue could happen. e.g.
hda_dsp_d0i3_work() is calling snd_sof_dsp_set_power_state() after 5 Seconds timeout from the WoV is trigger started,
and sof_ipc_tx_message() is calling snd_sof_dsp_set_power_state() before trying to send e.g. trigger stop IPC to the FW (we have logic to make sure the IPC is sent under DSP at D0).

I am pretty sure we discussed this case with @ranj063 and I am not convinced we have such a blatant race condition in the code today. Ranjani, please chime in, my memory is fading...

@plbossart @ranj063 I don't know what you have discussed about this, but yes, the race condition here is obvious as I mentioned above, no matter which process (workqueue scheduling process or ioctl trigger stop) has higher priority, it can preempt the other one and the _set_power_state() sequence is indeterminate.

@kv2019i kv2019i left a comment

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.

Thanks for the explanation @keyonjie , I think this looks valid.

@kv2019i

kv2019i commented Dec 9, 2020

Copy link
Copy Markdown
Collaborator

@plbossart @ranj063 Can you take a look at this? I believe this is valid as hda_dsp_d0i3_work is running when DSP is active, so it can for sure run concurrently with ioctls (anything that calls sof_ipc_tx_message()). We cancel the workqueue only when going to S3, but that's not the scenario here.

@ranj063

ranj063 commented Dec 11, 2020

Copy link
Copy Markdown
Collaborator

@plbossart @ranj063 Can you take a look at this? I believe this is valid as hda_dsp_d0i3_work is running when DSP is active, so it can for sure run concurrently with ioctls (anything that calls sof_ipc_tx_message()). We cancel the workqueue only when going to S3, but that's not the scenario here.

@keyonjie sorry for the delay. Basically I think the problem we're trying to fix is simultaenous requests from different threads to enter D0i0 and D0i3, right? I dont think we need a mutex for setting the power state at the top-level. I'd think that cancelling any pending D0i3 work before setting D0i0 in hda_dsp_set_power_state (), specifically hda_dsp_set_D0_state() should be enough. What do you think?

@kv2019i

kv2019i commented Dec 11, 2020

Copy link
Copy Markdown
Collaborator

@ranj063 wrote:

@keyonjie sorry for the delay. Basically I think the problem we're trying to fix is simultaenous requests from different threads
to enter D0i0 and D0i3, right? I dont think we need a mutex for setting the power state at the top-level. I'd think that cancelling

One of the threads is ipc_tx_message() which can be called at quite high frequency. Taking a mutex pretty lightweight operation compared to a mandatory sync point. It of course depends on high likely it is to have the D0i3 work queue running when sending a lot of IPCs, but this seems like a valid case.

@ranj063

ranj063 commented Dec 12, 2020

Copy link
Copy Markdown
Collaborator

@ranj063 wrote:

@keyonjie sorry for the delay. Basically I think the problem we're trying to fix is simultaenous requests from different threads
to enter D0i0 and D0i3, right? I dont think we need a mutex for setting the power state at the top-level. I'd think that cancelling

One of the threads is ipc_tx_message() which can be called at quite high frequency. Taking a mutex pretty lightweight operation compared to a mandatory sync point. It of course depends on high likely it is to have the D0i3 work queue running when sending a lot of IPCs, but this seems like a valid case.

@kv2019i it's more a question of whether we need it at the system level or not. The core is agnostic to the dsp substates. So a race in setting the substates should be handled at the platform level IMHO

@keyonjie

Copy link
Copy Markdown
Author

@plbossart @ranj063 Can you take a look at this? I believe this is valid as hda_dsp_d0i3_work is running when DSP is active, so it can for sure run concurrently with ioctls (anything that calls sof_ipc_tx_message()). We cancel the workqueue only when going to S3, but that's not the scenario here.

@keyonjie sorry for the delay. Basically I think the problem we're trying to fix is simultaenous requests from different threads to enter D0i0 and D0i3, right? I dont think we need a mutex for setting the power state at the top-level. I'd think that cancelling any pending D0i3 work before setting D0i0 in hda_dsp_set_power_state (), specifically hda_dsp_set_D0_state() should be enough. What do you think?

@ranj063 no, we can't cancel pending D0i3 work (name it A below) in hda_dsp_set_power_state/hda_dsp_set_D0_state() (name it B below).

Basically, B is a platform specific callback sof_ops(sdev)->set_power_state here, it is some kind of helpers. A here is actually a caller to B, we should not cancel it's caller from B internal.

Here, the A is a caller to B, and the ipc_tx_message() is another caller to B, there could be more callers to B added in future, adding mutex to make sure B is used/called exclusively looks naturally and low cost to me.

@ranj063 I don't think you should concern about the top-level mutex: the dsp_power_state is a snd_sof_dev top level member, so the mutex protection should naturally be at that level also. And for those platforms which doesn't support dsp_power_state (the sof_ops(sdev)->set_power_state is NULL), only a pare of mutex_lock/_unlock() calling should be light enough also.

@kv2019i

kv2019i commented Dec 15, 2020

Copy link
Copy Markdown
Collaborator

Hmm, @ranj063 it's indeed a bit problematic to force locking in core, if the reason to lock is platform specific. OTOH, we do have "dsp_power_state" in common code, so based on kernel practises, the mutex should be there as well. We could push locking to hw-dependent code, but as common code does touch the data as well (e.g. in sof_ipc_tx_message()), this wouldn't work.

So it does look that the ability to lock is something core should be providing to different hw backends, and both common core code and specific implementations should take the lock whenever changing DSP state. Zooming back a bit, it still makes sense. As core is using the data in code that handles user-initiated actions (ioctls essentially), data in question is related to PM state and PM flows are typically initiated asynchronously, this capability might be needed for other hw as well. In other words, I don't think this is very specific to Intel hardware.

@lyakh

lyakh commented Dec 21, 2020

Copy link
Copy Markdown
Collaborator

In principle LGTM now - thanks for fixing @keyonjie ! But I'm thinking about the following: the added mutex protects .dsp_power_state. That field is accessed in multiple contexts, e.g.

  • in probing
  • in suspend / resume
  • when sending IPC
  • in D0i3 work

Some are trivial (probing) and need no protection, but others are less trivial and since we're adding a generic protection for this field, we need to at least explain why we don't need to protect it everywhere? I.e. add comments in D0i3 work (it's deactivated when suspended) and in resume (cannot yet race with anything when resuming)?

@keyonjie

Copy link
Copy Markdown
Author

In principle LGTM now - thanks for fixing @keyonjie ! But I'm thinking about the following: the added mutex protects .dsp_power_state. That field is accessed in multiple contexts, e.g.

  • in probing
  • in suspend / resume
  • when sending IPC
  • in D0i3 work

Some are trivial (probing) and need no protection, but others are less trivial and since we're adding a generic protection for this field, we need to at least explain why we don't need to protect it everywhere? I.e. add comments in D0i3 work (it's deactivated when suspended) and in resume (cannot yet race with anything when resuming)?

Thanks for went through details during the reviewing @lyakh .

For probing, yes, we are doing initialization only, we add wrap to use a function like snd_sof_dsp_power_state_init(), but not necessary IMO.
For in D0i3 work, actually the work will eventually call snd_sof_dsp_set_power_state() so it is actually one of the consumer of the helper and protection take effect there.
For sending IPC, it will call snd_sof_dsp_set_power_state() also and it is another consumer of the helper and the protection.
For runtime suspend/resume, we are calling snd_sof_dsp_set_power_state() also.
For suspend, we are calling snd_sof_dsp_set_power_state() directly.
For resume, we have a check before calling snd_sof_dsp_set_power_state():

	if (sdev->dsp_power_state.state == SOF_DSP_PM_D0) {

As we are in suspended state here nobody else (except the resume callback itself) has chance to touch the dsp_power_state, so we are still good here. But yes, maybe I can add some comment here.

@kv2019i kv2019i added the P1 Blocker bugs or important features label Dec 22, 2020
@kv2019i

kv2019i commented Dec 22, 2020

Copy link
Copy Markdown
Collaborator

@lyakh Ack on @keyonjie 's analysis. The check on hda_dsp_resume() seems safe asthe opportunistic D0i3 work queue is not running when device is in suspend.

@kv2019i

kv2019i commented Dec 22, 2020

Copy link
Copy Markdown
Collaborator

Note to all reviewers: tagging as P1 to expedite the review.

@bardliao

Copy link
Copy Markdown
Collaborator

SOFCI TEST

@bardliao
bardliao merged commit 36975c4 into thesofproject:topic/sof-dev Dec 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1 Blocker bugs or important features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants