Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

64 bit race on timer counter in cavs_timer #31599

Closed
andyross opened this issue Jan 25, 2021 · 2 comments · Fixed by #31958
Closed

64 bit race on timer counter in cavs_timer #31599

andyross opened this issue Jan 25, 2021 · 2 comments · Fixed by #31958
Assignees
Labels
bug The issue is a bug, or the PR is fixing a bug platform: Intel ADSP Intel Audio platforms priority: low Low impact/importance bug

Comments

@andyross
Copy link
Contributor

The CAVS hardware has a 64 bit timer counter (it's the same device as the HD Audio wall clock timer). But the code to read it looks like this:

static uint64_t count(void)
{
	return shim_regs->walclk;
}

When compiled on Xtensa, this produces two sequential 32 bit reads of the value. And that's wrong, because the hardware continues to run. It's possible for the two reads to straddle a rollover of the low word and increment of the high word.

This needs to be fixed to be safe. The simplest scheme is to read the high word twice, compare the two values, and repeat the process if they don't match.

@andyross andyross added the bug The issue is a bug, or the PR is fixing a bug label Jan 25, 2021
@andyross
Copy link
Contributor Author

Discovered via conversation with @lgirdwood and @lyakh who had tripped over the same bug in upstream (non-Zephyr) SOF code. We have exactly the same messup.

@andyross andyross self-assigned this Jan 25, 2021
@nashif nashif added platform: Intel ADSP Intel Audio platforms priority: low Low impact/importance bug labels Jan 25, 2021
@lgirdwood
Copy link
Collaborator

@andyross fell free to copy and paste from SOF once it merged and passes QA and validation.

andyross pushed a commit to andyross/zephyr that referenced this issue Feb 3, 2021
The count register is 64 bits, but we're a 32 bit CPU that can only
read four bytes at a time, so a bit of care is needed to prevent
racing against a wraparound of the low word.  Wrap the low read
between two reads of the high word and make sure it didn't change.

Fixes zephyrproject-rtos#31599

Signed-off-by: Andy Ross <[email protected]>
nashif pushed a commit that referenced this issue Feb 4, 2021
The count register is 64 bits, but we're a 32 bit CPU that can only
read four bytes at a time, so a bit of care is needed to prevent
racing against a wraparound of the low word.  Wrap the low read
between two reads of the high word and make sure it didn't change.

Fixes #31599

Signed-off-by: Andy Ross <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue is a bug, or the PR is fixing a bug platform: Intel ADSP Intel Audio platforms priority: low Low impact/importance bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants