Repository navigation
!libc/pthread, sched: Fix PSE52 conformance issues. - #21834
Conversation
PSE52 requires the thread priority inheritance, thread priority protection and thread-safe functions options. <unistd.h> did not define _POSIX_THREAD_PRIO_INHERIT, _POSIX_THREAD_PRIO_PROTECT and _POSIX_THREAD_SAFE_FUNCTIONS, so applications could not detect them at build time, and sysconf(_SC_THREAD_SAFE_FUNCTIONS) returned -1. Define the three macros. The priority protocols are only advertised when CONFIG_PRIORITY_INHERITANCE or CONFIG_PRIORITY_PROTECT is enabled, and sysconf() returns the macros, so that the build time and the run time answers agree. Assisted-by: Kiro:auto Signed-off-by: ganjing <ganjing@xiaomi.com>
When a thread calls pthread_exit(), POSIX requires it to set its cancelability state to PTHREAD_CANCEL_DISABLE and its cancelability type to PTHREAD_CANCEL_DEFERRED. pthread_exit() only disabled cancellation, so a thread that had selected asynchronous cancellation kept it while its cleanup handlers and TLS destructors ran. Also set the cancelability type to deferred. Assisted-by: Kiro:auto Signed-off-by: ganjing <ganjing@xiaomi.com>
pthread_kill() passed an invalid signal number to tkill() and took the error from errno, so the call also changed the errno of the caller. Check the signal number first and return EINVAL directly. Assisted-by: Kiro:auto Signed-off-by: ganjing <ganjing@xiaomi.com>
pthread_join() returned EDEADLK only when a thread joined itself. Threads that join each other in a circle, for example A joins B while B joins A, blocked forever. POSIX allows pthread_join() to fail with EDEADLK when it detects a deadlock. Follow the chain of the threads that the target is joining, and return EDEADLK if it leads back to the caller. Each step walks the members of the task group, so the check is only built with CONFIG_DEBUG_FEATURES. Assisted-by: Kiro:auto Signed-off-by: ganjing <ganjing@xiaomi.com>
pthread_key_delete() and pthread_setspecific() only checked that the key was in range. They returned success for a key that was never created or was already deleted, and pthread_setspecific() then stored a value in a free slot. Return EINVAL for such a key, as POSIX allows. A key that is out of range also returns EINVAL instead of failing an assertion. Assisted-by: Kiro:auto Signed-off-by: ganjing <ganjing@xiaomi.com>
The SCHED_* values were one more than the policy bits that the TCB stores, so every policy had to be translated between the two. Renumber SCHED_FIFO, SCHED_RR, SCHED_SPORADIC and SCHED_OTHER to the values 0 to 3, the same as the policy bits of the TCB, and define TCB_FLAG_SCHED_OTHER for the value 3: * nxsched_get_scheduler() returns the policy bits directly instead of adding one. * nxsched_set_scheduler() handles SCHED_OTHER, SCHED_FIFO and SCHED_RR in one path, and maps SCHED_OTHER to SCHED_FIFO also when CONFIG_RR_INTERVAL is 0. * sched_get_priority_min() returns EINVAL for an unknown policy instead of failing an assertion, as sched_get_priority_max() already does. BREAKING CHANGE: The values of SCHED_FIFO, SCHED_RR, SCHED_SPORADIC, SCHED_OTHER and SCHED_NORMAL changed. Code that uses the symbolic names only needs to be rebuilt. Code that stores or exchanges the numeric values must be updated. Assisted-by: Kiro:auto Signed-off-by: ganjing <ganjing@xiaomi.com>
The previous commit renumbered the scheduling policies from SCHED_FIFO (0) to SCHED_OTHER (3). Update the range of the assertion in posix_spawnattr_setschedpolicy(), which still checked SCHED_OTHER to SCHED_SPORADIC. Assisted-by: Kiro:auto Signed-off-by: ganjing <ganjing@xiaomi.com>
POSIX requires that a thread that unlocks a PTHREAD_PRIO_PROTECT mutex is not moved to the tail of the scheduling queue of its priority. nxsem_protect_post() restored the priority with nxsched_set_priority(), which puts the thread behind the other ready threads of that priority. Restore the priority through the saved priority plus one, then set sched_priority to the saved priority, so that the thread stays at the head of the queue. If the saved priority is already the maximum, set it directly. Assisted-by: Kiro:auto Signed-off-by: ganjing <ganjing@xiaomi.com>
nxnotify_cancellation() only woke a thread that waited at a cancellation point with deferred cancellation. For a thread with asynchronous cancellation it returned false: pthread_cancel() then ran the cleanup handlers of the target in the context of the caller and terminated the target with nxtask_terminate(), also while the target was blocked. When a thread with asynchronous cancellation is blocked, mark the cancellation pending and wake it with ECANCELED as for deferred cancellation, so that it exits through its own cancellation path. A thread with asynchronous cancellation that is not blocked is still terminated by the caller. Assisted-by: Kiro:auto Signed-off-by: ganjing <ganjing@xiaomi.com>
The field holds one of the PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT and PTHREAD_PRIO_PROTECT values. Name it prio after them, so that the name tells what the field selects. Assisted-by: Kiro:auto Signed-off-by: ganjing <ganjing@xiaomi.com>
POSIX allows the pthread functions to fail with EINVAL when an attribute object, a condition variable or a mutex is not initialized or was destroyed, and PSE52 tests it. Most of the functions only checked for NULL or failed an assertion. Check that the object is valid: * pthread_attr_t: the priority is in range. * pthread_condattr_t, pthread_mutexattr_t: pshared is a valid value. * pthread_cond_t: clockid is CLOCK_REALTIME or CLOCK_MONOTONIC. pthread_cond_destroy() clears clockid, and returns EBUSY while threads wait on the condition. * pthread_mutex_t: the semaphore is a mutex. nxsem_destroy() clears the semaphore flags. A zero-filled object must not pass the checks, so 0 is no longer a valid PTHREAD_PROCESS_* or CLOCK_* value. CLOCK_FD, CLOCK_MASK and CLOCK_SHIFT move from <nuttx/clock.h> to <time.h>, next to the clock IDs. BREAKING CHANGE: PTHREAD_PROCESS_PRIVATE and PTHREAD_PROCESS_SHARED are now 1 and 2, and CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID, CLOCK_BOOTTIME and CLOCK_FD are now 1 to 6. Code that uses the symbolic names only needs to be rebuilt. Code that takes numeric values from the user, such as the -c option of cyclictest in nuttx-apps, must map them to the names. pthread_cond_destroy() now returns EBUSY while threads wait on the condition: wake them before the condition is destroyed. Assisted-by: Kiro:auto Signed-off-by: ganjing <ganjing@xiaomi.com>
pthread_cond_clockwait() counts the waiter in wait_count before it blocks, and only pthread_cond_signal() and pthread_cond_broadcast() drop the count of the waiters they wake. A waiter whose wait timed out kept its count, so pthread_cond_destroy() returned EBUSY afterwards, and a later pthread_cond_broadcast() posted the semaphore for waiters that were gone. Drop the count when the wait times out. Assisted-by: Kiro:auto Signed-off-by: ganjing <ganjing@xiaomi.com>
pthread_cond_wait() and pthread_cond_clockwait() release the mutex and only then block on cond->sem. If the waiter is preempted in between, another thread can take the mutex, call pthread_cond_signal() (the post finds no blocked waiter and just raises the count) and then wait on the same condition itself, consuming that post. The original waiter blocks afterwards and both threads wait forever. PSE52 pthread_cancel, pthread_sigmask and sigwait stress hang this way with RR scheduling: the coredumps show flag == 1, wait_count == 2 and semcount == -2 with both threads on the cond waitlist and the mutex free. Hold sched_lock() across the release and the block so no other thread on this CPU can run in the window. Blocking with the scheduler locked is already done by nxsem_wait_slow() for priority inheritance. This does not close the window against threads on another CPU under SMP. Assisted-by: Kiro:auto Signed-off-by: pangzhen1 <pangzhen1@xiaomi.com>
|
🔗 Cross-repo PR dependenciesThe read-only Build run reported the following dependent PR(s) and fetched head SHA(s):
CI run: https://github.com/apache/nuttx/actions/runs/43403109691 |
|
CI fails because b-g431b-esc1:nsh, nucleo-g431kb:nsh, nucleo-g431rb:nsh and nucleo-l432kc:wgen overflow the flash by 160 to 1028 bytes.They build ostest with LTO, and the new EINVAL checks keep the ostest error paths that LTO used to drop. Earlier overflows here were fixed by disabling unused NSH commands(cb6d987, b118542).Is there a guideline for which options to disable, or would you prefer another approach? @xiaoxiang781216 |
let's enable LTO first, but why this pr increase the code size so larger? |
Most of the growth is in ostest. On nucleo-g431rb:nsh the "Return EINVAL for invalid pthread objects" commit adds 1191 of the 1235 bytes. nucleo-l432kc:wgen fits with CONFIG_LTO_FULL=y. The three stm32g4 configs already use LTO and still overflow by 160-1028 bytes. |
🔗 Cross-repo PR dependenciesThe read-only Build run reported the following dependent PR(s) and fetched head SHA(s):
CI run: https://github.com/apache/nuttx/actions/runs/43855070933 |
Note: Please adhere to Contributing Guidelines.
Summary
This series fixes behavior that the PSE52 profile (IEEE 1003.13) requires
and that its test suite checks: the PSE52 thread options in
<unistd.h>and
sysconf(), thread cancellation, thread-specific data keys, circularpthread_join(), scheduling policies and thePTHREAD_PRIO_PROTECTunlock, EINVAL for invalid pthread objects, and the wait count of
condition variables. Each commit message describes its change.
The commits are submitted together because they are one conformance
change: the validity checks need the new constant values, and the
condition variable fixes build on each other.
Depends-On: apache/nuttx-apps#4049
Impact
BREAKING CHANGE: two commits change the values of public constants:
SCHED_FIFO,SCHED_RR,SCHED_SPORADIC,SCHED_OTHERandSCHED_NORMAL(commit "sched: Make the SCHED_* values match the TCBpolicy bits.").
PTHREAD_PROCESS_PRIVATE,PTHREAD_PROCESS_SHAREDand theCLOCK_*IDs (commit "libc/pthread: Return EINVAL for invalid pthread objects.").
Code that uses the symbolic names only needs to be rebuilt. Code that
takes numeric values from the user must map them to the names; the
nuttx-apps PR above does this for the
-coption of cyclictest.pthread_cond_destroy()now returns EBUSY while threads wait on thecondition; the nuttx-apps PR updates the ostest cancel test for it.
Size,
b-l475e-iot01a:nsh: text 121231 -> 121295 bytes (+64).Testing
ostest passes before and after the change on a B-L475E-IOT01A board
(
b-l475e-iot01a:nshwithCONFIG_TESTING_OSTEST=y), and with the changein these builds:
sim:ostest(Make and CMake builds), 5 loops.qemu-armv7r:pnshwithCONFIG_TESTING_OSTEST=y, QEMU 10.2.1.qemu-armv7a:knshwithCONFIG_TESTING_OSTEST=y.B-L475E-IOT01A, with the change:
No new regression test is added: the changes fix failures that the PSE52
test suite found, and that suite is not part of nuttx-apps.
Not tested: RISC-V, AArch64 and other architectures, SMP, and
CONFIG_SCHED_SPORADIC.