Commit graph

3242 commits

Author SHA1 Message Date
anjiahao
af0eac7a9c sched/semaphore: Add debug assert to detect mutex recursion.
Add DEBUGASSERT in nxsem_wait_slow() to catch illegal mutex
recursion attempts. This helps identify bugs where a task tries
to lock a mutex it already holds, which is not allowed.

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2026-02-03 11:56:24 -05:00
hujun5
fad28050f8 sched_setscheduler.c: coverity HIS_metric_violation: RETURN
Refactor nxsched_set_scheduler() to eliminate goto statements and consolidate multiple return
paths into a single exit point. Extract sporadic scheduling logic into a separate function and
restructure parameter validation for better code quality and MISRA HIS compliance.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-02-03 19:22:38 +08:00
hujun5
0f052df6db sem_rw.c: coverity: HIS_metric_violation(HIS_GOTO)
This change replaces goto-based control flow with structured if-else blocks
in the up_read() function to comply with MISRA HIS coding standards while
maintaining identical functional behavior.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-02-03 14:49:04 +08:00
Yongrong Wang
53e9c5b986 sem_rw: optimize up_read/up_write API
This change optimizes the reader-writer semaphore release process by consolidating
redundant context switch operations and improving code efficiency, reducing
unnecessary wake-ups during concurrent access scenarios.

Signed-off-by: Yongrong Wang <wangyongrong@xiaomi.com>
2026-02-03 14:49:04 +08:00
hujun5
2ec7d90eba sched_setparam.c: coverity HIS_metric_violation: RETURN
This change refactors nxsched_set_param() by extracting complex conditional logic
into dedicated helper functions and consolidating multiple return statements into
single exit points to reduce cyclomatic complexity and comply with MISRA HIS coding
standards for safety-critical embedded systems.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-02-03 10:20:24 +08:00
hujun5
29e514b276 clock: Correct the usage of atomic64_t atomic functions
Replace CONFIG_SYSTEM_TIME64 conditional atomic64 operations with a unified
seqlock-based approach for managing the system tick counter. This simplifies
code by eliminating architecture-specific branches and provides more robust
thread-safe access patterns.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-02-03 10:18:11 +08:00
simbit18
47623e5d09 sched: Fix Kconfig style
- Remove spaces from Kconfig

- Add TABs

- Replace help => ---help---

Signed-off-by: simbit18 <simbit18@gmail.com>
2026-02-03 09:36:16 +08:00
hujun5
63aefd9975 arch/armv7-r: userspace PMU access
This change enables performance monitoring unit (PMU) access from userspace
on ARMv7-R architecture by adding CONFIG_ARCH_HAVE_PERF_EVENTS_USER_ACCESS
support and building PMU code for userspace when needed.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-02-02 16:20:39 +01:00
hujun5
c844ecf393 sched_profil.c: coverity HIS_metric_violation: RETURN
This change consolidates multiple return statements in the profil() function
into a single exit point by inverting the parameter validation condition and
restructuring error handling to reduce cyclomatic complexity and comply with MISRA HIS.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-02-02 22:18:16 +08:00
fangxinyong
a724a5f4f4 sched/cancelpt: Fix MISRA C 2012 Rule 10.4 violations
Fix violations of MISRA C:2012 Rule 10.4 (operand of unsigned and signed)
in cancellation point handling code.

Changed all CANCEL_FLAG_* macro definitions and their usage to use unsigned
literals (1u instead of 1) to ensure consistent unsigned arithmetic when
performing bitwise operations. This eliminates mixed signed/unsigned operand
violations in:

- CANCEL_FLAG_NONCANCELABLE
- CANCEL_FLAG_CANCEL_ASYNC
- CANCEL_FLAG_CANCEL_PENDING

The changes affect cancellation point entry/exit logic, cancellation state
management, and cancellation type handling across both kernel and libc
implementations.

Signed-off-by: fangxinyong <fangxinyong@xiaomi.com>
2026-02-02 21:09:40 +08:00
fangxinyong
2b9506bc5d sched/spawn: Fix MISRA C 2012 Rule 10.4 violations
Fix violations of MISRA C:2012 Rule 10.4 (essential type operand of
unsigned and signed) in posix_spawn attribute handling code.

Changed all POSIX_SPAWN_* flag macro definitions to use unsigned literals
(1u instead of 1) to ensure consistent unsigned arithmetic when performing
bitwise operations. This eliminates mixed signed/unsigned operand violations
in the following flags:

- POSIX_SPAWN_RESETIDS
- POSIX_SPAWN_SETPGROUP
- POSIX_SPAWN_SETSCHEDPARAM
- POSIX_SPAWN_SETSCHEDULER
- POSIX_SPAWN_SETSIGDEF
- POSIX_SPAWN_SETSIGMASK
- POSIX_SPAWN_SETSID

Updated all flag checking comparisons in spawn_execattrs() to compare
against 0u instead of 0 for consistency.

Signed-off-by: fangxinyong <fangxinyong@xiaomi.com>
2026-02-02 20:39:36 +08:00
hujun5
9ae2fce3c1 sched_getparam.c: coverity HIS_metric_violation: RETURN
This change consolidates multiple return statements in nxsched_get_param()
into a single exit point and restructures the error handling path to reduce
cyclomatic complexity and comply with MISRA HIS coding standards.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-02-02 14:54:58 +08:00
ouyangxiangzhen
5b978a7f9b sched/hrtimer: workaround for SCHED_RR in tickless mode.
This commit provided workaround for incorrect SCHED_RR behavior in
tickless mode.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
b685d97199 sched/hrtimer: Simplify the hrtimer.
This commit simplified the hrtimer.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
dac48484ce sched/hrtimer: Update the comments.
This commit updated the comments.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
3eedf5f22b sched/wdog: Simplify timer expiration for hrtimer.
This commit simplified the timer expiration for hrtimer.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
5bab9fcc7f sched/hrtimer: Add guard timer.
This commit added the guard timer for hrtimer.
The guard timer uses a small memory footprint, offering two main advantages:
- Reduced branches checking for an empty hrtimer queue, simplifying code implementation and improving the performance.
- Additional health monitoring allows the system to enter a safe state in case of time acquisition errors, and supports custom error handling callback functions.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
78dce81c5a sched/hrtimer: Fix uncancelled hardware timer.
If there are no timers in the hrtimer queue, we should cancel the timers to avoid unnecessary timer interruptions. Since we currently do not have a timer cancellation interface, we can achieve cancellation by setting the timer to its maximum value.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
018e6d50c5 sched/hrtimer: Fix finding the rbtree left-most node.
Determining whether a red-black tree node is the left-mode node using `RB_LEFT(hrtimer, node) == NULL` is functionally incorrect. This is because the left node of any leaf node can also be NULL. For example, in the following rbtree:
 5
/ \
3 7
\
4
the left node of the right-most node 7 would also be NULL.
To avoid extra performance overhead to find the left-most node when the
rb-tree changed, this commit used `g_cached_first` to cache the
left-most node.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
d1c45f7c4e sched/hrtimer: Fix delay in hrtimer_process.
This commit fixed the overflow check and renamed the period to delay, since the callback return value is the next delay not the period.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
33c6b4115c sched/hrtimer: Simplify the hrtimer_process.
This commit simplified the hrtimer_process.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
493e2075a1 sched/hrtimer: Fix functional corrctness issue in re-enqueuing the timer
When a hrtimer is removed and then re-enqueued, if the removed hrtimer is the head node and the re-enqueued node is not the head node, the hardware timer still needs to be reset. This patch fixes this issue and simplifies the enqueuing.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
ada6d14672 sched/hrtimer: Simplify the rbtree.
This commit simplified the rbtree in hrtimer and provided better
branchless compare function.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
54dfa46c09 sched/hrtimer: Rename the hrtimer_is_armed.
This commit renamed the hrtimer_is_armed to hrtimer_is_pending, which is
more accurate in the semamtic, and simplify it.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
7b67055150 sched/hrtimer: inline hrtimer_start.
This commit inlined the `hrtimer_start` to allow the compiler to optimize at least 1 branch in
hrtimer_start.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
hujun5
7801c46a0d sem_rw.c: down_read_trylock coverity HIS_metric_violation: RETURN
This change consolidates multiple return statements in down_read_trylock() into
a single exit point and replaces goto with if-else structure to reduce cyclomatic
complexity and comply with MISRA HIS coding standards.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-02-02 13:21:24 +08:00
hujun5
437cdfac8d sem_rw.c: coverity: HIS_metric_violation(HIS_GOTO)
This change replaces goto-based control flow with structured if-else blocks
in the down_read() function to comply with MISRA HIS coding standards while
maintaining identical functional behavior.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-02-02 13:21:24 +08:00
hujun5
3200185665 sem_rw.c: coverity HIS_metric_violation: RETURN
This change consolidates multiple return statements in init_rwsem() into a
single exit point by inverting error conditions and restructuring nested
if-else blocks for MISRA HIS compliance.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-02-02 13:21:24 +08:00
hujun5
03d9d78adf sched_critmonitor.c: coverity HIS_metric_violation: RETURN
Reduce multiple return statements and simplify control flow by inverting the
condition check and moving all critical monitoring operations into a single
conditional block. This improves code maintainability and addresses the
Coverity HIS_metric_violation (RETURN) defect.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-02-02 11:06:05 +08:00
hujun5
0d25df7663 sched: fix code style violations in clock conversion macros (MISRA C-2012)
Add explicit type casts to uint32_t for time conversion macro divisors to
comply with MISRA C-2012 Rules 10.1, 10.3, 11.1, 11.3, and 11.4. This fixes
Coverity warnings and improves type safety in clock_time2ticks_floor and related
time conversion operations.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-02-01 01:14:01 +08:00
hujun5
7a5d333454 sched_waitid.c: HIS_metric_violation: CCM
Extract the core waitid logic into a separate waittcb() helper function to reduce the
cyclomatic complexity of the main waitid() function. This improves code maintainability,
reduces nested conditions, and enhances code clarity while preserving all functionality.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-31 13:11:34 -03:00
hujun5
5fca12c277 sched_waitid.c: HIS_metric_violation(HIS_GOTO)
Replace all goto statements with structured control flow using while loops and
conditional blocks. Consolidate early returns into a single error-handling path
for better code structure and MISRA HIS standards compliance.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-31 13:11:34 -03:00
hujun5
1900d4c8fb sched_sysinfo.c: coverity HIS_metric_violation: RETURN
This change consolidates multiple return statements in the sysinfo() function
into a single exit point and inverts the error condition to improve code structure
and comply with MISRA HIS coding standards for safety-critical systems.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-31 02:53:58 +08:00
hujun5
ceb23153c3 sem_rw.c: coverity HIS_metric_violation: RETURN
This change consolidates multiple return statements in down_write_trylock()
into a single exit point to reduce cyclomatic complexity and comply with MISRA
HIS coding standards for safety-critical embedded systems.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-31 02:45:02 +08:00
hujun5
3ec45774fb sched_get_stateinfo.c: misra_c_2012_rule_8_9_violation
Move the static g_statenames array from file scope to block scope within the
nxsched_get_stateinfo() function. This resolves MISRA C 2012 Rule 8.9 violation
which requires static variables to be used at block scope when only one function accesses them.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-31 02:39:19 +08:00
hujun5
2860333552 sched_setpriority.c: coverity HIS_metric_violation: RETURN
This change consolidates multiple return statements in nxsched_set_priority()
into a single exit point to reduce cyclomatic complexity and comply with MISRA
HIS coding standards for safety-critical embedded systems.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-31 02:38:55 +08:00
hujun5
926f5de541 sched_cpuload.c: misra_c_2012_rule_8_9_violation
Move the static g_cpuload_wdog watchdog timer from file scope to block scope
within the cpuload_init() function. This resolves MISRA C 2012 Rule 8.9 violation
which requires static variables used by a single function to be scoped at block level.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-30 12:52:22 -03:00
dongjiuzhu1
3c486b98dd fs/timerfd: implement TFD_TIMER_CANCEL_ON_SET to detect clock changes
Implement Linux-compatible TFD_TIMER_CANCEL_ON_SET flag for timerfd to
allow applications to detect discontinuous changes to CLOCK_REALTIME.

Background:
According to Linux timerfd_create(2) man page, when a timerfd is created
with CLOCK_REALTIME and TFD_TIMER_CANCEL_ON_SET flag is specified, the
read() operation should fail with ECANCELED if the real-time clock
undergoes a discontinuous change. This allows applications to detect
and respond to system time changes.

Implementation:
1. Add clock notifier infrastructure (clock_notifier.h/c) to notify
   interested parties when system time changes
2. Implement TFD_TIMER_CANCEL_ON_SET flag support in timerfd
3. Register timerfd with clock notifier when flag is set
4. Cancel timer and return ECANCELED when clock change is detected
5. Notify clock changes in:
   - clock_settime()
   - clock_initialize()
   - clock_timekeeping_set_wall_time()
   - rpmsg_rtc time synchronization

Changes include:
- New files: include/nuttx/clock_notifier.h, sched/clock/clock_notifier.c
- Modified: fs/vfs/fs_timerfd.c to handle TFD_TIMER_CANCEL_ON_SET
- Modified: clock subsystem to call notifier chain on time changes
- Modified: rpmsg_rtc to notify time changes during sync

Use case example:
Applications using timerfd with absolute time can now detect when
system time is adjusted (e.g., NTP sync, manual time change) and
take appropriate action such as recalculating timeouts or updating
scheduled events.

Reference: https://man7.org/linux/man-pages/man2/timerfd_create.2.html

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2026-01-30 17:20:24 +08:00
hujun5
effdb88710 sched_getscheduler.c: coverity HIS_metric_violation: RETURN
Refactor nxsched_get_scheduler() to consolidate multiple return statements into a single
exit point by inverting the null check condition. This improves code structure and resolves
Coverity HIS_metric_violation defect for better MISRA HIS standards compliance.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-28 13:07:20 -03:00
hujun5
a4d09a4e25 sched_sysinfo.c: misra_c_2012_rule_9_1_violation: uninit_use
Initialize the cpuload_s structure with zero values at declaration to eliminate
uninitialized variable usage. This resolves MISRA C 2012 Rule 9.1 violation and
ensures consistent, predictable system information retrieval behavior.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-28 13:05:04 -03:00
hujun5
e171f547fa sem_post.c: coverity HIS_metric_violation: RETURN
Refactor nxsem_post_slow() to consolidate multiple return statements into a single exit point
by introducing a result variable and wrapping semaphore operations in an error-check condition block.
This improves code structure and resolves Coverity HIS_metric_violation defect for MISRA HIS standards compliance.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-28 13:04:11 -03:00
hujun5
dc6ddd8d4d sched: Fix scheduler CPU selection and ready-to-run list management
Fix critical issues in SMP scheduler: correct CPU selection logic by
removing redundant lock check and prioritizing eligible CPUs, simplify
ready-to-run list addition by removing unnecessary condition check and
early validation, and relocate switch_running() call for proper cleanup
sequencing in remove_self() operation.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-28 13:02:58 -03:00
hujun5
3a214b5808 sem_waitirq.c: coverity HIS_metric_violation: RETURN
Refactor nxsem_wait_irq() to consolidate multiple return statements into a single
exit point by inverting the mutex condition check. This improves code maintainability
and resolves Coverity HIS_metric_violation defect for better compliance with MISRA HIS standards.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-28 23:43:16 +08:00
hujun5
c7bb7ece8e sched_profil.c: misra_c_2012_rule_8_9_violation
Move static g_prof variable declaration into block scope for the profil()
function when CONFIG_SMP is disabled, following MISRA C 2012 Rule 8.9 which
requires objects used by only one function to be declared within that function's scope.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-28 22:01:45 +08:00
hujun5
dde0d7405d sched_getfiles.c: coverity HIS_metric_violation: RETURN
This change consolidates multiple return statements in nxsched_get_fdlist_from_tcb()
into a single exit point to reduce cyclomatic complexity and comply with MISRA HIS
coding standards for safety-critical embedded systems.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-28 22:01:01 +08:00
pangzhen1
7b154402fb sched/irq: Consolidate IRQ bounds checking into IRQ_TO_NDX macro
Previously, IRQ bounds checking (irq >= 0 && irq < NR_IRQS) was duplicated
across multiple IRQ subsystem functions before calling IRQ_TO_NDX(). This
led to code duplication and inconsistency.

This patch consolidates all IRQ bounds checking into the IRQ_TO_NDX() macro
itself, which now returns -EINVAL for out-of-bounds IRQ numbers. This approach:

1. Eliminates duplicated bounds checking code
2. Ensures consistent error handling across all IRQ functions
3. Simplifies caller code - just check if IRQ_TO_NDX() returns negative
4. Makes the macro behavior more predictable and self-contained

Changes:
- Modified IRQ_TO_NDX() to check (irq < 0 || irq >= NR_IRQS) and return -EINVAL
- Removed redundant IRQ range checks in irq_attach(), irq_attach_thread(),
  irq_attach_wqueue(), and irqchain_detach()
- Simplified error handling to check ndx < 0 after IRQ_TO_NDX() call

This consolidation reduces code size and improves maintainability while
preserving all existing error checking functionality.

Signed-off-by: pangzhen1 <pangzhen1@xiaomi.com>
2026-01-28 13:35:30 +08:00
pangzhen1
3650636e39 sched/irq: Avoid out-of-bounds array access
This patch fixes an array out-of-bounds access issue in the irq_get_wqueue() function.

Signed-off-by: pangzhen1 <pangzhen1@xiaomi.com>
2026-01-28 03:34:33 +08:00
hujun5
49cb8e08e6 sem_trywait.c: coverity HIS_metric_violation(HIS_GOTO)
Refactor sem_trywait.c to replace goto statements with structured control
flow to comply with MISRA HIS coding standards. This improves code clarity
and maintainability while preserving all functional behavior and performance
characteristics.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-27 14:29:33 -03:00
p-gaoxiang43
922426346e sched/semaphore: remove dead code if no CONFIG_PRIORITY_PROTEC
Consolidate CONFIG_PRIORITY_PROTECT checks by moving macro definitions to
their actual usage locations and removing duplicate/dead code in semaphore
implementation. This reduces code duplication and improves maintainability
across sem_wait, sem_trywait, and sem_post functions.

Signed-off-by: p-gaoxiang43 <p-gaoxiang43@xiaomi.com>
2026-01-27 14:29:33 -03:00
wangzhi16
e23f2546c3 sched/sem : Optimize code
Simplify sem_trywait() code structure by removing redundant logic and
consolidating return paths. This optimization maintains functional correctness
while improving code clarity and reducing cyclomatic complexity.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
2026-01-27 14:29:33 -03:00