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

p4wq has a race with work item re-use #32052

Closed
andyross opened this issue Feb 5, 2021 · 1 comment · Fixed by #32191
Closed

p4wq has a race with work item re-use #32052

andyross opened this issue Feb 5, 2021 · 1 comment · Fixed by #32191
Assignees
Labels
bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug

Comments

@andyross
Copy link
Contributor

andyross commented Feb 5, 2021

The p4wq code, to handle the case where a work item was re-submitted, needs to inspect the thread field of the work item after the user callback has returned. But this will race with the case where the user code needs to (in some other context, on some other CPU) re-use that work item (for example by freeing it into the heap, or filling it out again for resubmission).

In the case where the item was not resubmitted, the p4wq code must never touch the memory of a work item after its callback has returned.

(In practice, we just need one bit for "was resubmitted", which we can track per-thread somewhere I guess. Needs a little thought, but shouldn't be difficult.)

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

andyross commented Feb 5, 2021

Race found by @lyakh while investigating #29623

@andyross andyross self-assigned this Feb 5, 2021
@nashif nashif added the priority: low Low impact/importance bug label Feb 9, 2021
andyross pushed a commit to andyross/zephyr that referenced this issue Feb 11, 2021
Work items can be legally resubmitted from within their own handler.
Currently the p4wq detects this case by checking their thread field to
see if it's been set to NULL.  But that's a race, because if the item
was NOT resubmitted then it no longer belongs to the queue and may
have been freed or reused or otherwise clobbered legally by user code.

Instead, steal a single bit in the thread struct for this purpose.
This patch adds a K_CALLBACK_STATE bit in user_options and documents
it in such a way (as being intended for "callback manager" utilities)
that it can't be used recursively or otherwise collide.

Fixes zephyrproject-rtos#32052

Signed-off-by: Andy Ross <[email protected]>
nashif pushed a commit that referenced this issue Feb 14, 2021
Work items can be legally resubmitted from within their own handler.
Currently the p4wq detects this case by checking their thread field to
see if it's been set to NULL.  But that's a race, because if the item
was NOT resubmitted then it no longer belongs to the queue and may
have been freed or reused or otherwise clobbered legally by user code.

Instead, steal a single bit in the thread struct for this purpose.
This patch adds a K_CALLBACK_STATE bit in user_options and documents
it in such a way (as being intended for "callback manager" utilities)
that it can't be used recursively or otherwise collide.

Fixes #32052

Signed-off-by: Andy Ross <[email protected]>
github-actions bot pushed a commit that referenced this issue Feb 14, 2021
Work items can be legally resubmitted from within their own handler.
Currently the p4wq detects this case by checking their thread field to
see if it's been set to NULL.  But that's a race, because if the item
was NOT resubmitted then it no longer belongs to the queue and may
have been freed or reused or otherwise clobbered legally by user code.

Instead, steal a single bit in the thread struct for this purpose.
This patch adds a K_CALLBACK_STATE bit in user_options and documents
it in such a way (as being intended for "callback manager" utilities)
that it can't be used recursively or otherwise collide.

Fixes #32052

Signed-off-by: Andy Ross <[email protected]>
nashif pushed a commit that referenced this issue Feb 17, 2021
Work items can be legally resubmitted from within their own handler.
Currently the p4wq detects this case by checking their thread field to
see if it's been set to NULL.  But that's a race, because if the item
was NOT resubmitted then it no longer belongs to the queue and may
have been freed or reused or otherwise clobbered legally by user code.

Instead, steal a single bit in the thread struct for this purpose.
This patch adds a K_CALLBACK_STATE bit in user_options and documents
it in such a way (as being intended for "callback manager" utilities)
that it can't be used recursively or otherwise collide.

Fixes #32052

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 priority: low Low impact/importance bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants