Skip to content

Commit 2b2cbf8

Browse files
cfriedtjgl-meta
authored andcommitted
posix: sched: ensure min and max priority are schedulable
Previously, there was an off-by-one error for SCHED_RR. Fixes #56729 Signed-off-by: Chris Friedt <[email protected]>
1 parent 7f3326a commit 2b2cbf8

File tree

2 files changed

+15
-45
lines changed

2 files changed

+15
-45
lines changed

lib/posix/pthread.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818
#define PTHREAD_INIT_FLAGS PTHREAD_CANCEL_ENABLE
1919
#define PTHREAD_CANCELED ((void *) -1)
2020

21-
#define LOWEST_POSIX_THREAD_PRIORITY 1
22-
2321
K_MUTEX_DEFINE(pthread_once_lock);
2422

2523
static const struct pthread_attr init_pthread_attrs = {
26-
.priority = LOWEST_POSIX_THREAD_PRIORITY,
24+
.priority = 0,
2725
.stack = NULL,
2826
.stacksize = 0,
2927
.flags = PTHREAD_INIT_FLAGS,
@@ -73,9 +71,11 @@ static uint32_t zephyr_to_posix_priority(int32_t z_prio, int *policy)
7371
if (z_prio < 0) {
7472
*policy = SCHED_FIFO;
7573
prio = -1 * (z_prio + 1);
74+
__ASSERT_NO_MSG(prio < CONFIG_NUM_COOP_PRIORITIES);
7675
} else {
7776
*policy = SCHED_RR;
78-
prio = (CONFIG_NUM_PREEMPT_PRIORITIES - z_prio);
77+
prio = (CONFIG_NUM_PREEMPT_PRIORITIES - z_prio - 1);
78+
__ASSERT_NO_MSG(prio < CONFIG_NUM_PREEMPT_PRIORITIES);
7979
}
8080

8181
return prio;
@@ -87,9 +87,11 @@ static int32_t posix_to_zephyr_priority(uint32_t priority, int policy)
8787

8888
if (policy == SCHED_FIFO) {
8989
/* Zephyr COOP priority starts from -1 */
90+
__ASSERT_NO_MSG(priority < CONFIG_NUM_COOP_PRIORITIES);
9091
prio = -1 * (priority + 1);
9192
} else {
92-
prio = (CONFIG_NUM_PREEMPT_PRIORITIES - priority);
93+
__ASSERT_NO_MSG(priority < CONFIG_NUM_PREEMPT_PRIORITIES);
94+
prio = (CONFIG_NUM_PREEMPT_PRIORITIES - priority - 1);
9395
}
9496

9597
return prio;

lib/posix/pthread_sched.c

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@
77
#include <zephyr/kernel.h>
88
#include <zephyr/posix/sched.h>
99

10-
static bool valid_posix_policy(int policy)
10+
static inline bool valid_posix_policy(int policy)
1111
{
12-
if (policy != SCHED_FIFO && policy != SCHED_RR) {
13-
return false;
14-
}
15-
16-
return true;
12+
return policy == SCHED_FIFO || policy == SCHED_RR;
1713
}
1814

1915
/**
@@ -23,25 +19,12 @@ static bool valid_posix_policy(int policy)
2319
*/
2420
int sched_get_priority_min(int policy)
2521
{
26-
if (valid_posix_policy(policy) == false) {
22+
if (!valid_posix_policy(policy)) {
2723
errno = EINVAL;
2824
return -1;
2925
}
3026

31-
if (IS_ENABLED(CONFIG_COOP_ENABLED)) {
32-
if (policy == SCHED_FIFO) {
33-
return 0;
34-
}
35-
}
36-
37-
if (IS_ENABLED(CONFIG_PREEMPT_ENABLED)) {
38-
if (policy == SCHED_RR) {
39-
return 0;
40-
}
41-
}
42-
43-
errno = EINVAL;
44-
return -1;
27+
return 0;
4528
}
4629

4730
/**
@@ -51,25 +34,10 @@ int sched_get_priority_min(int policy)
5134
*/
5235
int sched_get_priority_max(int policy)
5336
{
54-
if (valid_posix_policy(policy) == false) {
55-
errno = EINVAL;
56-
return -1;
57-
}
58-
59-
if (IS_ENABLED(CONFIG_COOP_ENABLED)) {
60-
if (policy == SCHED_FIFO) {
61-
/* Posix COOP priority starts from 0
62-
* whereas zephyr starts from -1
63-
*/
64-
return (CONFIG_NUM_COOP_PRIORITIES - 1);
65-
}
66-
67-
}
68-
69-
if (IS_ENABLED(CONFIG_PREEMPT_ENABLED)) {
70-
if (policy == SCHED_RR) {
71-
return CONFIG_NUM_PREEMPT_PRIORITIES;
72-
}
37+
if (IS_ENABLED(CONFIG_COOP_ENABLED) && policy == SCHED_FIFO) {
38+
return CONFIG_NUM_COOP_PRIORITIES - 1;
39+
} else if (IS_ENABLED(CONFIG_PREEMPT_ENABLED) && policy == SCHED_RR) {
40+
return CONFIG_NUM_PREEMPT_PRIORITIES - 1;
7341
}
7442

7543
errno = EINVAL;

0 commit comments

Comments
 (0)