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

kernel: sched: Use k_ticks_t in z_tick_sleep #29343

Merged
merged 2 commits into from
Dec 5, 2020

Conversation

ceolin
Copy link
Member

@ceolin ceolin commented Oct 20, 2020

z_tick_sleep was using int32_t what could cause a possible overflow
when converting from k_ticks_t.

Fixes #29066

@ceolin ceolin added the bug The issue is a bug, or the PR is fixing a bug label Oct 20, 2020
@ceolin
Copy link
Member Author

ceolin commented Oct 20, 2020

@andyross any idea how to exercise this issue ? I reproduced the problem hardcoding curr_tick to overflow, but this is not exposed at all.

@ceolin ceolin force-pushed the k_sleep branch 2 times, most recently from c739e3e to 94804f7 Compare October 21, 2020 04:39
kernel/sched.c Outdated
@@ -1258,7 +1262,7 @@ static int32_t z_tick_sleep(int32_t ticks)
timeout = Z_TIMEOUT_TICKS(ticks);
#else
ticks += _TICK_ALIGN;
timeout = (k_ticks_t) ticks;
timeout = ticks;
Copy link
Collaborator

@JordanYates JordanYates Oct 21, 2020

Choose a reason for hiding this comment

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

Should this also be Z_TIMEOUT_TICKS(ticks) rather than casting the int to a struct

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, I missed that we had it to legacy api, just changing it. Thanks for pointing it out.

Copy link
Member

Choose a reason for hiding this comment

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

The initial change was correct. There was no casting to structure, because modified line was executed when CONFIG_LEGACY_TIMEOUT_API=y.

Fix in #30842.

@ceolin ceolin requested a review from pabigot as a code owner October 26, 2020 23:59
@github-actions github-actions bot added the area: Tests Issues related to a particular existing or missing test label Oct 26, 2020
z_tick_sleep was using int32_t what could cause a possible overflow
when converting from k_ticks_t.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
@ceolin
Copy link
Member Author

ceolin commented Nov 3, 2020

@andyross could you take a look on it ?

Copy link
Member

@martinjaeger martinjaeger left a comment

Choose a reason for hiding this comment

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

We recently encountered this overflow issue in our application when using k_sleep(K_TIMEOUT_ABS_MS(...)); and I was very happy to find this PR. Thanks for the fix.

I tested it over the weekend an can confirm that it works. Just got one suggestion for more clear variable naming, so that we don't confuse timeouts with ticks.

Change a variable name to avoid confusion between time and ticks.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
@ceolin ceolin requested a review from martinjaeger November 16, 2020 18:43
@martinjaeger martinjaeger added this to the v2.5.0 milestone Dec 1, 2020
Copy link
Contributor

@andyross andyross left a comment

Choose a reason for hiding this comment

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

Looks correct

/* LOG subsys does not handle 64-bit values
* https://github.com/zephyrproject-rtos/zephyr/issues/26246
*/
LOG_DBG("thread %p for %u ticks", _current, ticks);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why bother with %u vs. %d? Both are lossy in this circumstance.

@nashif nashif merged commit 9a16097 into zephyrproject-rtos:master Dec 5, 2020
@kurddt
Copy link
Contributor

kurddt commented Apr 23, 2021

@ceolin I ran into this issue. Do you think it's reasonably "safe" to apply 7a815d5 on top of zephyr 2.4 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Kernel area: Tests Issues related to a particular existing or missing test bug The issue is a bug, or the PR is fixing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

kernel: k_sleep doesn't handle relative or absolute timeouts >INT_MAX
8 participants