Commit Graph

3024 Commits

Author SHA1 Message Date
guohao15 0625b7a760 sched/clock: [bugfix] should return -EINVAL when clock_id is invalid
bug fix about clock_gettime

Signed-off-by: guohao15 <guohao15@xiaomi.com>
2025-12-30 10:22:09 -03:00
dongjiuzhu1 752a405f86 sched/clock: support using CLOCKFD to call clock_adjtime
This patch implements clock_adjtime() with CLOCKFD support, enabling
precise time and frequency adjustments for PTP clocks through the
standard POSIX clock API.

Key changes include:

1. New clock_adjtime() system call:
   - Added sched/clock/clock_adjtime.c implementation
   - Provides POSIX-compliant time adjustment interface
   - Supports both standard clocks and CLOCKFD-based dynamic clocks
   - Added CONFIG_CLOCK_ADJTIME Kconfig option

2. CLOCKFD support in clock_adjtime():
   - Detects CLOCKFD-encoded clockids via CLOCKFD_TO_FD() check
   - Extracts file descriptor and validates through fs_getfilep()
   - Issues PTP_CLOCK_ADJTIME ioctl to underlying PTP clock device
   - Returns clock status and adjustment results via struct timex

3. Enhanced struct timex support:
   - Extended include/sys/timex.h with additional ADJ_* flags
   - Added ADJ_OFFSET, ADJ_FREQUENCY, ADJ_MAXERROR, ADJ_ESTERROR
   - Added ADJ_STATUS, ADJ_TIMECONST, ADJ_TAI, ADJ_SETOFFSET
   - Added ADJ_MICRO, ADJ_NANO for time unit selection
   - Added STA_* status flags for clock state reporting

4. Clock operations supported:
   - ADJ_SETOFFSET: Apply time offset adjustment
   - ADJ_FREQUENCY: Adjust clock frequency (PPM)
   - ADJ_MAXERROR: Set maximum error estimate
   - ADJ_ESTERROR: Set estimated error
   - ADJ_STATUS: Modify clock status bits
   - ADJ_NANO: Use nanosecond resolution
   - ADJ_SETOFFSET: Set absolute time offset

5. API declarations:
   - Added clock_adjtime() prototype in include/nuttx/clock.h
   - Enabled by CONFIG_CLOCK_ADJTIME configuration
   - Compatible with Linux clock_adjtime() interface

Usage example:
  int fd = open("/dev/ptp0", O_RDWR);
  struct timex tx = {0};
  tx.modes = ADJ_FREQUENCY;
  tx.freq = 10000000;  /* Adjust frequency by 10 PPM */
  clock_adjtime(CLOCKFD(fd), &tx);
  close(fd);

This completes the PTP clock framework's POSIX clock API integration,
providing comprehensive time control capabilities for precision timing
applications including PTP synchronization daemons (ptp4l, timemaster).

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-12-30 10:22:09 -03:00
dongjiuzhu1 6802d3510b sched/clock: support using CLOCKFD to call clock_getres
This patch extends CLOCKFD support to clock_getres(), allowing
applications to query the resolution of PTP clocks through file
descriptors using the standard POSIX clock API.

Key changes include:

1. Move clock_getres() from libc to kernel:
   - Relocated from libs/libc/sched/clock_getres.c to sched/clock/
   - Enables direct kernel-level file descriptor handling
   - Maintains POSIX compliance while adding CLOCKFD support

2. CLOCKFD support in clock_getres():
   - Detects CLOCKFD-encoded clockids using CLOCKFD_TO_FD() check
   - Extracts file descriptor from clockid parameter
   - Validates file descriptor through fs_getfilep()
   - Issues PTP_CLOCK_GETRES ioctl to query clock resolution

3. PTP clock resolution query:
   - Retrieves resolution from PTP clock device via ioctl
   - Returns nanosecond-precision timing resolution
   - Enables applications to determine clock accuracy capabilities
   - Falls back to standard clock resolution for non-CLOCKFD clockids

4. Error handling:
   - Returns EINVAL for invalid CLOCKFD file descriptors
   - Properly manages file reference counting with fs_putfilep()
   - Maintains backward compatibility with existing clock types

5. Build system updates:
   - Updated libs/libc/sched/CMakeLists.txt and Make.defs
   - Updated sched/clock/CMakeLists.txt to include clock_getres.c
   - Ensures proper compilation in kernel context

Usage example:
  int fd = open("/dev/ptp0", O_RDONLY);
  struct timespec res;
  clock_getres(CLOCKFD(fd), &res);  /* Query PTP clock resolution */
  printf("Resolution: %ld.%09ld sec\n", res.tv_sec, res.tv_nsec);
  close(fd);

This completes the basic POSIX clock API support for dynamic PTP clocks,
providing applications with comprehensive clock information access.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-12-30 10:22:09 -03:00
dongjiuzhu1 1c643b5cc3 sched/clock: support using CLOCKFD to call clock_gettime/settime
This patch implements POSIX-compliant CLOCKFD support, enabling user
applications to access dynamic PTP clocks through file descriptors
combined with clock_gettime() and clock_settime() system calls.

Key changes include:

1. CLOCKFD macro support:
   - Added CLOCKFD() macro in include/nuttx/clock.h
   - Allows encoding file descriptor into clockid_t: CLOCKFD(fd)
   - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts)

2. clock_gettime() enhancements:
   - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids
   - Extracts file descriptor from clockid using CLOCKFD_TO_FD()
   - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME
   - Falls back to standard clock handling for non-CLOCKFD clockids

3. clock_settime() enhancements:
   - Modified sched/clock/clock_settime.c to support CLOCKFD clockids
   - Extracts file descriptor and validates accessibility
   - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device
   - Maintains backward compatibility with standard POSIX clocks

4. File descriptor validation:
   - Checks file descriptor validity using fs_getfilep()
   - Ensures proper reference counting with fs_putfilep()
   - Returns appropriate error codes (EINVAL, EBADF) on failures

5. Integration with PTP clock framework:
   - Seamlessly integrates with PTP clock driver's ioctl interface
   - Enables standard POSIX clock API usage for dynamic PTP clocks
   - No changes required to existing applications using standard clocks

Usage example:
  int fd = open("/dev/ptp0", O_RDONLY);
  struct timespec ts;
  clock_gettime(CLOCKFD(fd), &ts);  /* Get PTP clock time */
  clock_settime(CLOCKFD(fd), &ts);  /* Set PTP clock time */
  close(fd);

This implementation follows the Linux kernel's PTP clock model, providing
a familiar interface for developers working with PTP hardware.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-12-30 10:22:09 -03:00
ouyangxiangzhen dfdbf4dcf9 sched: Simplify the timer_start/cancel in sched_timerexpiration.
This commit simplified the timer_start/cancel in sched_timerexpiration.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-26 11:51:38 +08:00
ouyangxiangzhen 299ece7cbe sched: Simplify the interval adjustment in nxsched_timer_start.
This commit simplified the interval adjustment in nxsched_timer_start.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-26 11:51:38 +08:00
chao an 6300bc5522 sched/wdog: fix race condition in wd_gettime()
Move WDOG_ISACTIVE check inside critical section protection to avoid race condition

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-12-26 11:51:02 +08:00
ouyangxiangzhen 2dc2b30a6a sched/wdog: Simplify the code and correct the critical_section.
This commmit simplified the code and correct the critical_section.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
jiangtao16 c5814ac910 sched/wdog: compatible with MISRA-C
This commit fixed the MISRA C-2004 violation rule 11.1, 10.4 and more.

Signed-off-by: jiangtao16 <jiangtao16@xiaomi.com>
Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen 6b63a81f54 sched/wdog: Simplify wd_timer to reduce WCET.
This commit simplified wd_timer() to reduce WCET when `noswitches` is false.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen 43e0c50aa7 sched/wdog: Revert the spin-lock to big-kernel-lock (enter_critical_section).
If the wdog use the fine-grained spin-lock, and allow the callback execution without the lock held, there will be incorrect interleaving.
E.g. The first `nxsem_timeout` callback function caused the second semaphore wait to fail.

    Core 0 [nxsem_clockwait]       |  Core 1
    enter_critical_section()       |  ...
    wd_start(nxsem_timeout)        |  ...
    nxsem_wait(sem)                |  wd_expiration() --> nxsem_timeout
    wd_cancel(&rtcb->waitdog)      |  try enter_critical_section()
    leave_critical_section()       |  Failed retry...
    ....nxsem_clockwait            |  Failed retry...
    enter_critical_section()       |   Failed retry...
    wd_start(nxsem_timeout)        |  Failed retry...
    nxsem_wait(sem)                |  Core 1 enter the critical section
                                   |  nxsem_wait_irq(wtcb, ETIMEDOUT) -> incorrectly wake-up the rtcb.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen 164b075d3f sched/wdog: Remove workaround for wdog latency.
This commit removed the workaround for wdog latency.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen 476e02c7e0 sched/wdog: Faster wdog deleting.
This commit reduced 1 write operation for wdog deleting and decoupled
the WDOG_ISACTIVE with the list implementation.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen 694dfc7a0c sched/wdog: Remove wd_recover.
This commit Removed wd_recover.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen 7d01d8aab5 sched/wdog: Move the g_wdtimernested to sched_timerexpiration.
This commit moved the g_wdtimernested to sched_timerexpiration, since
wdog and hrtimer can share it.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
ouyangxiangzhen e5db83d7db sched/sched: Remove nxsched_alarm_expiration
This commit removed nxsched_alarm_expiration to simplify the timer
expiration.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
ouyangxiangzhen 3aed2485f8 sched/sched: Remove nxsched_alarm_tick_expiration
This commit removed nxsched_alarm_tick_expiration to simplify the timer
expiration.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
ouyangxiangzhen b048984dd4 sched/sched: Simplify nxsched_process_scheduler.
This commit simplified nxsched_process_scheduler.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
ouyangxiangzhen 5569755868 sched/sched: Change the return value semantics.
This commit changed the return value semantics.
In past designs, timers that were expected to fire immediately were delayed by at least one tick, resulting in a decrease in real-time performance.
This commit re-encoded the return value to allow the timers fire
immediately.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
ouyangxiangzhen c785ed7175 sched/sched: Change the uint32_t ticks to clock_t.
This commit changed the uint32_t ticks to clock_t.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
ouyangxiangzhen 9848e5ecec sched: Simplify the timer expiration.
This commit simplified the timer expiration.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
anjiahao 994c9d8929 sched:use tcb inside of pthread_tcb_s, remove all cast
Simplify the code and remove different tcb

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2025-12-22 17:55:45 +08:00
anjiahao 0a2b01950c sched:use tcb_s inside of task_tcb_s , remove all cast
Simplify the code and remove different tcb

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2025-12-22 17:55:45 +08:00
wangzhi16 31e562b10f Revert "sched/group: move task group into task_tcb_s to improve performance"
This reverts commit 29e50ffa73.

reason:
Placing the main thread and the gourd in the same memory block, and allocating and freeing memory simultaneously, presents the following two problems:

When the main thread creates a child thread and performs a detach operation, there is a possibility that the main thread may have exited, but the main thread's TCB (Transaction Control Block) may not have been released.

This could potentially cause the main thread's TCB to be double-freed. The core contradiction in this problem lies in binding the main thread's TCB (Trust Container Registry) and the group together. When releasing the main thread's TCB, an additional check is needed to ensure the main thread was the last to leave the group. If this check and the free operation are atomically guaranteed, the logic is sound, and double freeing won't occur. However, this atomicity cannot be completely guaranteed. If other free operations cause a block, problems still arise.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
2025-12-21 08:53:46 -03:00
anjiahao fe7a877c4e sched/env_putenv:putenv is kernel function need use kmm_free
strdup if use __KERNEL__ defined by nx_strdup
so need kmm_free it

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2025-12-18 21:59:31 +08:00
wangchengdong edf89ddedd sched/hrtimer: add safe synchronous hrtimer cancel API
Add a safe synchronous hrtimer cancel API, hrtimer_cancel_sync().
If the timer callback is currently executing, this function waits
until the callback has completed and the timer state transitions
to HRTIMER_STATE_INACTIVE.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-12-18 18:36:28 +08:00
wangchengdong ed1cb6f9b8 sched/hrtimer: Add high-resolution timer support for NuttX
NuttX provides the wdog module to implement timers for the scheduler.
While it is lightweight and efficient, wdog only supports tick-level timers,
typically in milliseconds. Although the tick duration can be configured,
setting it to microsecond or nanosecond resolution is not practical.
Doing so may cause an interrupt storm, where the CPU is constantly
occupied handling tick interrupts.

To address this limitation, a new hrtimer module is introduced.
It coexists with the wdog module, providing both tick-level timers
for wdog and high-resolution timers (nanosecond-level) directly to users.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-12-18 18:36:28 +08:00
Xiang Xiao 0ba93e5b68 sched/init: Add OSINIT_IS_PANIC to replace 'g_nx_initstate == OSINIT_PANIC'
to simplify the code logic which need handle the panic in special way

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2025-12-18 18:21:55 +08:00
anjiahao 6f1e99c9bd sched: assert if call timedwait from interrupt
Nuttx does not allow calling interfaces like TIME_WAIT in interrupts, so we need to directly assert.

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2025-12-18 18:19:42 +08:00
Junbo Zheng 53edfb5815 sched/misc: correct coredump log format
since it has been replaced with
https://github.com/apache/nuttx/blob/master/sched/misc/coredump.c#L763
and `nput` map to `off_t` https://github.com/apache/nuttx/blob/master/include/nuttx/streams.h#L98

align with other prints, reference:
- https://github.com/apache/nuttx/blob/master/drivers/misc/rwbuffer.c#L1121
- https://github.com/apache/nuttx/blob/master/fs/mmap/fs_rammap.c#L84
- https://github.com/apache/nuttx/blob/master/libs/libc/elf/elf_read.c#L85

Signed-off-by: Junbo Zheng <zhengjunbo1@xiaomi.com>
2025-12-12 14:54:05 +01:00
wangchengdong 273578f658 sched/clock: Remove unnecessary preprocessor definition
Remove an unnecessary preprocessor definition in clock_systime_ticks.c.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-12-12 12:31:16 +08:00
ligd 452557a249 binfmt: Fix error handling in exec_module
Remove an extra return statement in exec_module failure path,
ensuring proper cleanup when errors occurs.

Signed-off-by: ligd <liguiding1@xiaomi.com>
2025-12-11 12:57:07 -05:00
ligd eb65ea5612 env: Always call env_release in group_release
Remove unnecessary #ifdef CONFIG_DISABLE_ENVIRON.
This makes sure environment resources are always cleaned up,
regardless of configuration.

Signed-off-by: ligd <liguiding1@xiaomi.com>
2025-12-11 12:57:07 -05:00
wangchengdong 23733e7642 sched/clock: Fix missing clock_inittime() call
The current implementation only calls
  clock_inittime() when RTC is enabled,
  which causes it to be skipped on other
  architectures. This patch ensures
  clock_inittime() is invoked during clock
  initialization for all cases.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-12-11 20:07:00 +08:00
donghaokun 839468f52d docs/sched/sched: Add documentation for different sleep interfaces
Build Documentation / build-html (push) Has been cancelled
As pull request apache#17200 & apache#17368 introduced support for scheduling sleep, a documentation is needed for different sleep interfaces.
This patch adds the description for sleep interfaces currently provided in NuttX, including Scheduled sleep(nxsched_sleep()...), Signal-scheduled sleep(nxsig_sleep()...), and Busy sleep(up_udelay()).

Signed-off-by: Haokun Dong <donghaokun@lixiang.com>
2025-12-04 18:59:58 +01:00
hujun5 0c6fe04ef7 Revert "sched_idletask: remove the check for whether tcb is NULL"
This reverts commit 8f91054b1d.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-11-27 08:26:53 -03:00
hujun5 8f91054b1d sched_idletask: remove the check for whether tcb is NULL
We should not call these functions before nx_start;
therefore, this_task will not be empty.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-11-25 16:01:29 -03:00
Matteo Golin 3edf6de552 drivers/timers/arch_alarm: Revert removal of ndelay_accurate
Docker-Linux / push (push) Has been cancelled
This reverts the removal of ndelay_accurate from #14450, since as
mentioned in #17011, this fails to consider the `sim` architecture
where CONFIG_BOARD_LOOPSPERMSEC was set to 0 because of reliance on the
accurate implementations of the up_delay functions. All the commit did
was remove a more accurate implementation in favour of a less accurate
one.

Signed-off-by: Matteo Golin <matteo.golin@gmail.com>
2025-11-25 22:33:48 +08:00
wangchengdong ba05c7f133 sched/signal: Fix nxsig_ismember() return value behavior
Build Documentation / build-html (push) Has been cancelled
nxsig_ismember() has a return type of int, but the current
implementation returns a boolean value, which is incorrect.

All callers should determine membership by checking whether
the return value is 1 or 0, which is also consistent with the POSIX sigismember() API.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-25 10:02:52 +08:00
wangchengdong 817e4ee354 sched/signal: Initialize signal action pool during init phase
Initialize the signal action pool during the signal initialization
phase to improve performance and reduce footprint.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-11-25 10:02:24 +08:00
wangchengdong bc561c677a sched/sleep: Add nxched_nanosleep() API
Introduce the nxched_nanosleep() API to provide a lightweight
nanosecond-level sleep based on nxsched_ticksleep().
This API offers the same functionality as nxsig_nanosleep() but without
signal-related overhead, making it suitable for implementing libc
sleep() or usleep() when signals are disabled.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-25 10:02:13 +08:00
wangchengdong df17524e55 sched/signal: Use clock_compare() in nxsig_clockwait()
clock_compare() should be used when comparing clock tick values to
ensure correct handling of tick wrap-around and time ordering.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-25 09:50:13 +08:00
wangchengdong 7421846c94 sched/signal: Remove redundant wd_cancel() in nxsig_clockwait()
Both the watchdog timeout and signal dispatch paths already cancel
the watchdog timer, so the explicit wd_cancel() call is redundant.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-25 09:50:13 +08:00
wangchengdong a232815648 sched/signal: Fix remaining time calculation in nxsig_clockwait()
Always compute the expected wake-up time by default, so the remaining
time can be calculated correctly when the flag is not TIMER_ABSTIME.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-25 09:50:13 +08:00
chengdong wang df3fe0e16f Revert "sched/signal: Unblock task waiting for event when a signal received"
Build Documentation / build-html (push) Has been cancelled
Events are not defined in the POSIX standard, so signals are not required
to wake tasks that are in the TSTATE_WAIT_EVENT state.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-24 11:00:23 -03:00
wangchengdong e758e92830 sched/signal: Unblock task waiting for event when a signal received
Build Documentation / build-html (push) Has been cancelled
If the task is blocked waiting for a event, then that task must
 be unblocked when a signal is received.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-11-22 17:24:53 -03:00
ouyangxiangzhen fc28b93224 timers/oneshot: Remove all callback and args.
This commit remove all callback and args in the APIs.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-11-18 13:02:29 +01:00
ouyangxiangzhen 5c113f79b7 timers/oneshot: Remove all private callback.
This commit removed all private callback and handle it on the upperhalf
of oneshot.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-11-18 13:02:29 +01:00
wangchengdong 1b58a449c5 sched/clock: Make g_system_ticks a static variable
Make g_system_ticks a static variable, as it is only accessed by
    clock_get_sched_ticks().

    This change improves code modularity and enhances safety, since
    clock_get_sched_ticks() performs proper synchronization when
    reading g_system_ticks.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-13 18:40:16 -03:00
wangchengdong 2440b4ef39 sched/sched: Fix nxsched_suspend() logic
nxsched_deliver_task() or nxsched_merge_pending() should only be
called when a context switch is required. This behavior is
independent of whether the current task is locked.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-11-13 21:01:18 +08:00