Commit graph

2926 commits

Author SHA1 Message Date
Jukka Laitinen
9413b6f331 sched/signal/sig_dispatch.c: Simplify the nxsig_dispatch
The nxsig_dispatch should just deliver the signal to either a
thread by pid (tid) or to the process (group) by pid.

Simplify the code so that the intent is more obvious.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-09-27 21:42:03 +08:00
Jukka Laitinen
a4c42e54f7 sched/signal/sig_dispatch.c: Correct signal dispatch to specific thread
The signal dispatch is called from interrupt handlers as well, so
this_task() is wrong. The thread to which the signal is supposed to
be delivered is known (stcb), use that.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-09-27 21:42:03 +08:00
wangchengdong
3430ad2e1c sched/init/nx_bringup.c: Add support for CONFIG_INIT_NONE in a flat build
In a flat build, a separate init thread should not be mandatory,
users can create a user task in the board early initialization or late initialization hook.
2025-08-31 18:38:36 +08:00
wangjianyu3
b9203c1747 sched/signal: Old signal action need save sa_user
For example, set sigaction after create signalfd,
the sa_sigaction was restored but sa_user not,
causing signalfd_action() get the wild private data.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-08-28 21:52:44 +08:00
p-szafonimateusz
8108aad1a4 pthread: add pthread_attr_{get|set}guardsize support
This commit adds simple implementation of guardsize for pthreads.
At this moment this option simply increases the size of allocated pthread stack.

At default pthread guard size is set to 0.

Signed-off-by: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
2025-08-22 20:50:13 +08:00
Jukka Laitinen
0dbbce3a8f sched: Remove unnecessary affinity restriction & restoration over smp calls
When the task has TCB_FLAG_CPU_LOCKED it is locked to the CPU regardless
of the affinity. There is no need to switch the affinity back and forth.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-08-20 10:16:43 +08:00
Jukka Laitinen
4cc384757b sched: Change SMP list "g_assignedtasks" to a vector
Since g_assignedtasks only holds the running task for each CPU, it can
be just a vector. Idle tasks are already preserved in statically allocated
structures "g_idletcb", and can be used from there.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-08-20 10:16:43 +08:00
Jukka Laitinen
142f32121a sched: Remove pending list for CONFIG_SMP
This fixes several places, where the sched erroneously checks if the scheduling
is locked on current cpu/task, when it should check if the scheduling is locked
on the target cpu/task.

The original code randomly caused a task to be added to the pending list, and never
taken out from there, leading to system halt.

For SMP, there is no need for the pending list. Each CPU has got it's own
running list (assigned tasks list), and pending tasks can just be kept in
the unassigned (readytorun) list.

In addition, the smp scheduling is changed in a way that every CPU just picks
up the tasks from the ready-to-run list themselves. Which task to pick is
not tried to be dictated by another CPU.

This also allows using up_send_smp_sched for asynchronously
- re-prioritizing a running task
- triggering round robin scheduling switch

Iow, no separate smp call mechanism is needed for those and the code can be simplified.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-08-20 10:16:43 +08:00
Jukka Laitinen
3e29996d97 sched: After nxsched_add_readytorun, change first parameter to up_switch_context to this_task()
Change every occurence of up_switch_context to use this_task() as the first parameter.

"nxsched_add_readytorun" returns "true" if context switch is required. "nxsched_add_readytorun"
typically could only switch the assigned/running task to the one which is passed in as parameter.
But this is not strictly guaranteed in SMP; if other CPUs tweak with affinities or priorities,
it may be possible that the running task after the call is changed, but is some other
task from the readytorun list (and it should be, if there is higher priority one available or the
affinity of the added task prevents it to be scheduled in, but the previous head of the readytorun
list should run.

this_task() is always the correct one to switch to, since it always points to the tcb which was
just switched in by the nxsched_add_readytorun.

This is also a precursor to re-writing the SMP queue logic to remove pending lists for SMP.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-08-20 10:16:43 +08:00
Jukka Laitinen
f55e6f6383 sched: Remove race condition in sched_unlock
When exiting schedlock, that task should first take the critical section and
only after that decrease the lockcount to 0. Otherwise an interrupt might
cause a re-schedule before the task enters the critical section, which makes
the following code meaningless.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-08-20 10:16:43 +08:00
Niccolò Maggioni
571447bb06 coredump: Fix missing loglevel to logmask conversion.
The current setlogmask call used in coredump_dump_syslog specifies a raw log level
instead of a bitmask, and this causes wrong evaluations later on when that value
is checked against a mask. Therefore the LOG_UPTO macro is added for conversion.

Signed-off-by: Niccolò Maggioni <nicco.maggioni+nuttx@gmail.com>
2025-07-08 21:14:07 +08:00
guohao15
57d83907dd bugfix: share kernel thread group should not dup files from caller group
after this commits
  commit e7fa4cae6cbf567266985c8072db1f51ad480943
  Author: Yanfeng Liu <yfliu2008@qq.com>
  Date: Fri May 17 06:11:52 2024 +0800

  sched/tcb: use shared group for kthreads

all kernel thread share group idle
and should not dup filelist to this group

Signed-off-by: guohao15 <guohao15@xiaomi.com>
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-06-27 21:45:33 +08:00
wangmingrong1
bdd3869d26 debug/0 address: Add 0 address access panic configuration
Implemented using up_debugpoint_add

Signed-off-by: wangmingrong1 <wangmingrong1@xiaomi.com>
2025-06-26 19:25:31 +08:00
ouyangxiangzhen
0717928b0e spinlock: Fix compilation error with atomic.h.
This commit fixed compilation error with atomic.h included.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-06-25 22:13:00 +08:00
Lingao Meng
6c1020204a sched: assert: Print Stack pointer value when not within the stack
When Stack pointer value not within the stack, the default methon ignored,
while will be discard this information for debug.

Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
2025-06-20 09:51:39 +08:00
ouyangxiangzhen
a0aa654c70 sched/wqueue: Fix windows compilation errors.
This commit fixed windows compilation errors `struct
lp_wqueue_s/hp_wqueue_s has an illegal zero-sized array`.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-06-18 13:57:20 +02:00
Ville Juven
b8e30b54ec fs/vfs: Separate file descriptors from file descriptions
This patch is a rework of the NuttX file descriptor implementation. The
goal is two-fold:

1. Improve POSIX compliance. The old implementation tied file description
to inode only, not the file struct. POSIX however dictates otherwise.
2. Fix a bug with descriptor duplication (dup2() and dup3()). There is
an existing race condition with this POSIX API that currently results
in a kernel side crash.

The crash occurs when a partially open / closed file descriptor is
duplicated. The reason for the crash is that even if the descriptor is
closed, the file might still be in use by the kernel (due to e.g. ongoing
write to file). The open file data is changed by file_dup3() and this
causes a crash in the device / drivers themselves as they lose access to
the inode and private data.

The fix is done by separating struct file into file and file descriptor
structs. The file struct can live on even if the descriptor is closed,
fixing the crash. This also fixes the POSIX issue, as two descriptors
can now point to the same file.

Signed-off-by: Ville Juven <ville.juven@unikie.com>
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-06-12 18:12:42 +08:00
dongjiuzhu1
a12d21e830 fs/file: unify prefix about file_xxx api, like file_open, file_ioctl
old:
fs_getfilep, fs_putfilep, fs_reffilep
new:
file_get, file_put, file_ref

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-06-12 18:12:42 +08:00
ouyangxiangzhen
df183b6682 sched/wqueue: Remove the work_queue_period.
For simplicity, better performance and lower memory-overhead, this commit replaced the periodical workqueue APIs with the more expressive work_queue_next. The work_queue_next restarts work based on the last expiration time.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-06-10 11:02:45 -03:00
ouyangxiangzhen
efe2af643f sched/wdog: Replace periodcial timer with the wd_start_next.
This commit replaced periodical timer with the wd_start_next to improve the timing accuracy.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-06-10 11:02:45 -03:00
ouyangxiangzhen
9d1b958524 sched/wdog: Remove the wd_start_period.
For better simplicity, this commit replaced the periodical wdog APIs with the more expressive wd_start_next. The wd_start_next restarts watchdog timer based on the last expiration time.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-06-10 11:02:45 -03:00
raiden00pl
a8e03eefcd sched: unify Private Types banners
unify Private Types banners according to NuttX coding standard

Signed-off-by: raiden00pl <raiden00@railab.me>
2025-05-28 10:17:15 +08:00
ouyangxiangzhen
947856efe0 sched/wdog: Add max delay tick limitation.
This commit changed the type of the delay ticks to the unsigned, which can reduce the useless branch conditions
Besides, this commit added max delay tick limitation to fix incorrect timing behavior if we delay SCLOCK_MAX in the SMP build.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-27 12:10:39 +02:00
ouyangxiangzhen
fce40edb6b sched/wqueue: Add max delay tick limitation.
This commit added max delay tick limitation for the workqueue.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-27 12:10:39 +02:00
wangzhi16
35a62b7d5e Revert "Reduce the size of tcb by four bytes."
This reverts commit 893c5e92c2.
2025-05-27 16:30:35 +08:00
Lars Kruse
3ce85ca54e style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
Jukka Laitinen
b2315e98c4 sched/semaphore: Always free mutex holder at nxsem_post
There is no need to check the holder structure "counts". There are cases
where the counts may be greater than 1 when several tasks block
on the mutex, but there is always just one holder, which must be freed.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-23 01:39:53 +08:00
Jukka Laitinen
f3231a1934 sched/semaphore: Remove redundant clearing of mutex blocking bit in sem_waitirq
This is not a bug, but unnecessary code. If the mutex is no longer blocking,
the released thread will set the holder and clear the blocking bit in the end
of nxsem_wait.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-23 01:39:53 +08:00
Jukka Laitinen
b5a284fd6d sched/semaphore: Fix a bug in posting mutex
There was a logical error in setting blocking bit when releasing another thread

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-23 01:39:53 +08:00
Jukka Laitinen
6ed21ec1af sched: Remove checking of WDOG_ISACTIVE when cancelling wdog
Checking only adds a race condition. The checking if the wdog is active
or not must be done inside wd_cancel, where the proper spinlock is held

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-19 20:34:31 +08:00
Jukka Laitinen
466525891d Revert "sched/wdog/wd_cancel: Return 0 (OK) when cancelling an inactive wdog"
This reverts commit c28c14ad26.
2025-05-16 10:30:41 +08:00
Serg Podtynnyi
c5757ed71f sched/timedwait: Ensure critical section for nxsig_timeout
nxsig_timeout calls nxsched_add_readytorun and up_switch_context, so
must be in critical section.
nxsig_timeout is used as wdentry in nxsig_clockwait

See wd_expiration CALL_FUNC is not in critical section.

Signed-off-by: Serg Podtynnyi <serg@podtynnyi.com>
2025-05-15 19:36:17 +08:00
Jukka Laitinen
c28c14ad26 sched/wdog/wd_cancel: Return 0 (OK) when cancelling an inactive wdog
In case the wdog has already completed when calling cancel, the cancel is
supposed to just return OK.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-14 21:13:48 +08:00
ouyangxiangzhen
64a7049dec clock: Add clock_delay2abstick.
This commit added a macro function clock_delay2abstick to calculate the
absolute tick after the delay.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-12 19:56:29 +08:00
ouyangxiangzhen
2e8583f759 clock: Fix timing error.
This commit resolves a timing error caused by the round-up behavior in clock_time2ticks. In rare cases, this could lead to a two-tick increment within a single tick interval. To fix this, we introduced clock_time2ticks_floor, which guarantees the correct semantics for obtaining current system ticks.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-12 19:56:29 +08:00
ouyangxiangzhen
c67ac63da3 timer: pt_expected + 1 before set the timer.
This commit is to prevent the insufficient sleep time.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-12 19:56:29 +08:00
ouyangxiangzhen
3622e4830a sched: Correct word spelling mistakes.
This commit fixed serveral word spelling mistakes.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-12 19:56:29 +08:00
ouyangxiangzhen
a255c99eb8 sched/wdog: Add CONFIG_TIMER_ADJUST_USEC.
This commit added CONFIG_TIMER_ADJUST_USEC to support the time compensation for wdog timer. Normally, timer event cannot triggered on exact time due to the existence of interrupt latency. Assuming that the interrupt latency is distributed within [Best-Case Execution Time, Worst-Case Excution Time], we can set the timer adjustment value to the BCET to reduce the latency.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-12 19:56:29 +08:00
ouyangxiangzhen
8e65375365 sched: Remove unnecessary tick++ for absolute wdog timer.
To improve the absolute timer accuracy, this commit move the tick++ to relative wdog timer.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-12 19:56:29 +08:00
ouyangxiangzhen
ba06ea6fd7 sched/wdog: Simplify nxsched_timer_process.
This commit simplified `nxsched_timer_process` implementation and
improve its readability.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-12 19:56:29 +08:00
buxiasen
cd7a8f73eb comments: update text typo pre-empted to preempted
Or will be catch by codespell, when do checkpatch.sh

Also fix the relative comment file changed.

include/nuttx/scsi.h
drivers/syslog/ramlog.c
excluded as we have to modify field name in struct

Signed-off-by: buxiasen <buxiasen@xiaomi.com>
2025-05-12 15:01:37 +08:00
ouyangxiangzhen
36a4d5feaf sched/wqueue: Improve performance of the work_queue.
This commit improve the performance of the work_queue by reducing
unnecessary wdog timer setting.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-11 10:47:13 +08:00
ouyangxiangzhen
f442a41102 sched/wqueue: Fix work_cancel_sync.
This commit fixed work_cancel_sync at a very rare boundary case. When a worker thread re-enqueues the work data structure during the execution of work, the user thread cannot directly dequeue the work in work_cancel_sync. Instead, it should wait until all workers' references to the work data structure have been eliminated after dequeuing.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-11 10:47:13 +08:00
Jukka Laitinen
e4d5c78008 Fix signal delivered to a wrong thread
When using pthread_kill, the signal should be delivered to the
specified thread. Current implementation, however, may add the
signal to the groups pending list, if the signal is masked at the
time of dispatch. From the group's pending list it can be delivered
to any thread of the group, which is wrong.

Fix this by adding a new field "FAR struct tcb_s *tcb" to
"struct sigpendq", marking if the signal needs to be delivered
to a specific thread. Use NULL for the value if delivery to any
thread in the group is ok.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-08 21:32:35 +08:00
Jukka Laitinen
efc0f400bb sched/signal/sig_dispatch.c: Correct spelling in comments, waitting ==> waiting
Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-08 21:32:08 +08:00
Jukka Laitinen
a0efb1fefe sched/signal: Move dynamic allocation of pending signals to a safe place
The kmm_alloc can break the critical section, if it sleeps on the
heap mutex. If we run out of pending signal structures, allocate more
right after entering the critical section but before checking if the signal
needs to be added to the pending queue.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-08 21:32:08 +08:00
Jukka Laitinen
77df55db45 sched/signal: Move nxsig_find_action outside of critical section
The TCB used in find_action is locked by spinlock, so it doesn't belong
inside critical section. Just find the possible action already in the
beginning of nxsig_tcbdispatch.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-08 21:32:08 +08:00
Jukka Laitinen
156469f158 Clean up the nxmutex library
- Remove the redundant holder, as nxsem now manages hoder TID
- Remove DEBUGASSERTIONS which are managed in nxsem
- Remove the "reset" handling logic, as it is now managed in nxsem
- Inline the simplest functions

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-08 16:00:05 +08:00
Jukka Laitinen
b6f2729730 Integrate nxmutex support fully into nxsem
This puts the mutex support fully inside nxsem, allowing
locking the mutex and setting the holder with single atomic
operation.

This enables fast mutex locking from userspace, avoiding taking
critical_sections, which may be heavy in SMP and cleanup
of nxmutex library in the future.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-08 16:00:05 +08:00
Jukka Laitinen
5c188b6625 sched/semaphore: Remove tcb from the SEM_WAITLIST when the task is deleted
The task which is deleted should be removed from the semaphores waitlist,
if the task happens to be blocked.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-08 16:00:05 +08:00