Commit graph

2872 commits

Author SHA1 Message Date
Jukka Laitinen
2b513af2c3 sched/signal/sig_dispatch: Fix race conditions between nxsig_tcbdispatch and nxsig_deliver
For example a race in managing tcb->sigprocmask may cause signal not being delivered at all.
The mechanism is as follows:

1. cpu 1: nxsig_deliver adds the signal to the sigprocmask for the duration of signal delivery
2. cpu 2: new signal (same signo) is dispatched in nxsig_tcbdispatch
3. cpu 2: nxsig_tcbdispatch detects that signal is masked
4. cpu 2: nxsig_tcbdispatch leaves critical section before calling nxsig_add_pendigsignal
5. cpu 1: nxsig_deliver finishes. The "nxsig_unmask_pendingsignal" is called in the end but does nothing
6. cpu 2: nxsig_tcbdispatch continues and adds the pending signal

In the end, the logic in the end of nxsig_deliver, which tries to handle signals added
to the pending queue during the signal action delivery (step 5) failed, and the pending signal
was not delivered.

Fix this by just keeping the critical section for during the whole duration of nxsig_tcbdispatch,
and move things which can't be executed from within critical section outside.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-02 09:21:14 -03:00
Ville Juven
c12aa5663d sched/affinity: Fix CPU_LOCKED functionality for some SMP calls
For some SMP calls it is necessary to lock the current CPU for the process
receiving the SMP call. This is done by setting the CPU affinity to the
current CPU and preventing the CPU selection algorithm from switching
CPUs.

  dtcb->flags |= TCB_FLAG_CPU_LOCKED;
  CPU_SET(dtcb->cpu, &dtcb->affinity);

However, this logic is currently broken, as CPU_SET is defined as:

  #define CPU_SET(c,s) do { *(s) |= (1u << (c)); } while (0)

In order to assign tcb->cpu (the current CPU) to the affinity mask, the
mask must be cleared first by calling CPU_ZERO.
2025-04-26 13:56:27 +08:00
chao an
52482219c8 libc/elf: rename modlib to libelf
Renaming "modlib" to "libelf" is more in line with the implementation content,
which makes it easier for individual developers to understand the capabilities of this module.

CONFIG_LIBC_MODLIB -> CONFIG_LIBC_ELF

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-04-11 09:43:22 +08:00
anjiahao
422c43949a binfmt:use crt0 inside of starthook
test:
1.use mps3-an547 build helloxx as module and run it
2.use qemu-armv7a:knsh test kernel build helloxx and run it

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2025-04-09 23:07:29 +08:00
Ville Juven
04e760b1c2 sched/gettid: Move thread ID to TLS
There is no need for a gettid() syscall, as the thread ID is stable through
the life of the process. It is safe to put a copy of TID to TLS. This way
a user processes can access TID quickly via its own stack, instead of
having to use an expensive syscall.

Signed-off-by: Ville Juven <ville.juven@unikie.com>
2025-04-08 22:53:10 +08:00
Jukka Laitinen
4b14b206db sched: Replace direct semaphore value access with nxsem_get_value
Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-04-07 16:29:57 +08:00
Jukka Laitinen
cd84da8714 sched/pthread: Set the protocol of underlying semaphore to SEM_TYPE_MUTEX in pthread_mutex_init
Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-04-07 16:29:57 +08:00
chao an
0d6de4c0a7 sched/mutex: add ticked lock version for mutex/rmutex
Added ticked lock version for mutex to reduce time calculation overhead

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-04-03 20:13:55 +08:00
Jukka Laitinen
19a8e2403f libc/semaphore: Move fast mutex wait/post paths to libc
This avoids unnecessary syscalls in memory protected builds, when mutex
lock/unlock can be done with only atomic counter access

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-04-01 20:37:23 +08:00
Jukka Laitinen
73ee052b3f sched/semaphore: Optimize fast mutex acquire with CONFIG_PRIORITY_PROTECT or CONFIG_PRIORITY_INHERITANCE
When the semaphore priority flags is set to NONE, and the semaphore
is a mutex, the fast locking path can be used, even when
priority inheritance or priority protect are enabled globally.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-04-01 20:37:23 +08:00
Jukka Laitinen
06d00b4c88 Correct the pthread mutex priority inheritance default setting
After pthread mutexes changed to nxmutex, the priority inheritance
was set on by default; even if one tried to control it with
CONFIG_PTHREAD_MUTEX_DEFAULT_PRIO_INHERIT.

Also the CONFIG_PTHREAD_MUTEX_DEFAULT_PRIO_PROTECT is not effective.

Fix this by setting the default mutex priority adjustment flags according
to CONFIG_PTHREAD_MUTEX_DEFAULT_PRIO_INHERIT and CONFIG_PTHREAD_MUTEX_DEFAULT_PRIO_PROTECT.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-03-26 22:36:48 +08:00
chao an
bf73fbe774 sched: simplify call of get current tcb
Replace get current tcb method from nxsched_get_tcb(nxsched_gettid()) to this_task(),
change two function calls with inline function to improve performance:

FAR struct tcb_s *tcb = nxsched_get_tcb(nxsched_gettid());
FAR struct tcb_s *tcb = this_task();

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-03-18 00:22:37 +02:00
Ville Juven
4959e269d6 sched/sem_waitirq: Move kmm_map() call to sem_wait()
The kernel mapping should be performed in sem_wait (thread level) as
virtual memory mappings cannot be added from interrupt, at least for now.

The reason?

kmm_map() depends on mm_map_add(), which in turn uses a mutex for mutual
exclusion. Using mutexes from interrupt level is not permitted.

Mapping tcb->waitobj into kernel virtual memory directly in sem_wait()
makes sense, since accessing tcb->waitobj via a user virtual address can
lead to unexpected results (the wrong mappings can be in place).
2025-03-13 12:28:22 +08:00
ouyangxiangzhen
fa5590d5b1 sched/wdog: Fix int-to-pointer-cast and pointer-to-int-cast warnings.
This commit fixed the wdparm to pointer and pointer to wdparm cast
warnings.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-03-08 13:52:37 -03:00
ouyangxiangzhen
bcad1d038a sched/wqueue: Rename periodic workqueue API.
This commit renamed the periodic workqueue API.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-03-08 13:52:37 -03:00
ouyangxiangzhen
ed04000626 sched/wqueue: support for periodic workqueue.
This commit added support for periodic workqueue.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-03-08 13:52:37 -03:00
ouyangxiangzhen
f495cd4ffd sched/wdog: allow wd_cancel_period in the periodical wdog callback.
This commit addresses an issue where calling `wd_cancel_period` within the periodic watchdog callback would fail to cancel the watchdog timer.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-03-08 13:52:37 -03:00
ouyangxiangzhen
51399a76d9 sched/wdog: support for periodic wdog.
This commit added support for periodic wdog.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-03-08 13:52:37 -03:00
SPRESENSE
0938671485 sched/init: Fix build error with CONFIG_BOARD_CRASHDUMP_CUSTOM
Fix build error with the following condition.
- CONFIG_BOARD_CRASHDUMP_CUSTOM=y
- CONFIG_COREDUMP=n

The condition for calling `coredump_initialize()` is incorrect.
CONFIG_BOARD_CRASHDUMP_CUSTOM is not dependent on CONFIG_COREDUMP.

Signed-off-by: SPRESENSE <41312067+SPRESENSE@users.noreply.github.com>
2025-03-08 00:18:25 +08:00
hujun5
f22b93b337 sched/spin_lock: rename raw_spin_lock to spin_lock_notrace
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-02-13 20:48:15 +08:00
hujun5
ac26f9c690 Revert "sched/wqueue: some minor improve to reduce sched_lock range"
This reverts commit 2779989add.
2025-02-13 20:48:15 +08:00
Xiang Xiao
45422e2ad9 Revert "No need to call sched_lock explicitly after call spin_lock_irqsave, since it will be called in func spin_lock_irqsave."
This reverts commit 8f3a2a6f76.
2025-02-13 14:15:43 +08:00
Michal Lenc
0ae633cc08 clock/clock_adjtime.c: fix compile errors
The missing <nuttx/spinlock.h> header caused following compile errors:

CC:  clock/clock_adjtime.c clock/clock_adjtime.c: In function 'adjtime_wdog_callback':
clock/clock_adjtime.c:67:11: error: implicit declaration of function 'spin_lock_irqsave' [-Wimplicit-function-declaration]
   67 |   flags = spin_lock_irqsave(&g_adjtime_lock);
      |           ^~~~~~~~~~~~~~~~~
clock/clock_adjtime.c:78:3: error: implicit declaration of function 'spin_unlock_irqrestore' [-Wimplicit-function-declaration]
   78 |   spin_unlock_irqrestore(&g_adjtime_lock, flags);

Signed-off-by: Michal Lenc <michallenc@seznam.cz>
2025-02-06 09:22:44 +08:00
wangzhi16
8f3a2a6f76 No need to call sched_lock explicitly after call spin_lock_irqsave, since it will be called in func spin_lock_irqsave.
Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
2025-01-24 17:27:50 +08:00
Neo Xu
1ab1a6eaf3 arch: select LIBC_ARCH_ELF when using COREDUMP
Coredump doens't need CONFIG_ELF to be enabled, but need elf.h to include correct elf32.h or elf64.h.
Select LIBC_ARCH_ELF in COREDUMP to allow LIBC_ARCH_ELF_64BIT to be
defined correctly.

Signed-off-by: Neo Xu <neo.xu1990@gmail.com>
2025-01-24 09:37:58 +08:00
wanggang26
f6b9a8f577 coredump: fix issue that nvic region overlapped by board memory region
Firstly call arm_coredump_add_region in up_initialize to add nvic region, then call
coredump_set_memory_region to add board mem region, at this moment, an overlap occurs.

Signed-off-by: wanggang26 <wanggang26@xiaomi.com>
2025-01-24 09:15:56 +08:00
xuxingliang
ba18502b93 misc/coredump: move coredump info to note
Add a custom note for NuttX information including magic and coredump
file data size, so when system reboots, we can check if the block device
contains valid coredump file, and save it to file system.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2025-01-24 09:12:59 +08:00
hujun5
914ae532e6 sched: remove csection and reduce the interrupt disabling time in sched_[un]lock
reason:
1 Accelerated the implementation of sched_lock, remove enter_critical_section in sched_lock and
only enter_critical_section when task scheduling is required.
2 we add sched_lock_wo_note/sched_unlock_wo_note and it does not perform instrumentation logic

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-23 19:58:49 +08:00
hujun5
b49f4286fb spinlock: Due to semantic inconsistency, remove/rename some functions with the _wo_note suffix.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-23 19:58:49 +08:00
liwenxiang1
7eccf4493a arch/x86_64:Adapt coredump
Signed-off-by: liwenxiang1 <liwenxiang1@xiaomi.com>
2025-01-22 16:35:32 +08:00
Ville Juven
bb85ad849e pthread_cond_wait: Use atomic_t to protect the waiter count
The load/compare and RMW to wait_count need protection. Using atomic
operations should resolve both issues.

NOTE:
The assumption that the user will call pthread_cond_signal /
pthread_cond_broadcast with the mutex given to pthread_cond_wait held is
simply not true. It MAY hold it, but it is not forced. Thus, using the
user space lock for protecting the wait counter as well is not valid!

The pthread_cond_signal() or pthread_cond_broadcast() functions may be called by a thread whether or not it currently owns the mutex that threads calling pthread_cond_wait() or pthread_cond_timedwait() have associated with the condition variable during their waits; however, if predictable scheduling behaviour is required, then that mutex is locked by the thread calling pthread_cond_signal() or pthread_cond_broadcast().

[1] https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_cond_signal.html
2025-01-20 23:55:26 +08:00
wanggang26
1fd21bbd69 coredump: fix crash dump failed when items of pr_regs not equal with regs_num
elf_emit_tcb_note: nitems(status.pr_regs) is 18, g_tcbinfo.regs_num is 17, then g_tcbinfo.reg_off.p[17] has been out of bounds

Signed-off-by: wanggang26 <wanggang26@xiaomi.com>
2025-01-19 19:43:53 +08:00
hujun5
3193457dd0 sched/timer: remove critical section in setitimer
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-19 17:08:32 +08:00
hujun5
fcfc300f7a group: use tg_mutex to replace tg_joinlock
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-19 17:08:32 +08:00
hujun5
544da3c64c sched/timer: remove critical section in timer_deleteall
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-19 17:08:32 +08:00
chao an
2779989add sched/wqueue: some minor improve to reduce sched_lock range
Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-01-17 23:33:23 +08:00
raiden00pl
d4a0abec82 sched/mqueue/CMakeLists.txt: fix compilation for CONFIG_DISABLE_MQUEUE_SYSV=n
fix compilation for CONFIG_DISABLE_MQUEUE_SYSV=n in cmake
2025-01-17 23:14:39 +08:00
hujun5
b8660fc39c sim: fix regression from https://github.com/apache/nuttx/pull/14623
reason:
work_timer_expiry may be called in thread context

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-17 18:59:29 +08:00
chao an
67582acc14 sched/wqueue: fix potential deadlock
sched_unlock() should called after spin_unlock

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-01-17 09:05:37 +08:00
chao an
8877366c0e sched/wdog: remove wd_cancel_irq() implement
we do not need this implement after change the lock from csec to spin_lock

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-01-17 09:05:11 +08:00
chao an
dedb57cb2d libc/unistd: move NAME_MAX/LINE_MAX/PATH_MAX define to unistd
These options are unistd-specific and should not be filesystem dependent,
and also not suitable for define in the sched directory.

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-01-15 23:17:51 +08:00
hujun5
61f0c97193 wqueue: wqueue remove csection
reason:
We decouple semcount from business logic
by using an independent counting variable,
which allows us to remove critical sections in many cases.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-15 17:26:07 +08:00
hujun5
9dad781d07 sched/wdog: use small lock to protect g_wdactivelist
reason:
We would like to replace the critical section with a small lock.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-14 12:29:29 +08:00
hujun5
684ddc6ada irq: enter_critical_section_wo_note/leave_critical_section_wo_note
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-14 12:29:29 +08:00
chao an
5dedc0119f sched/clock: remove unlock logic to avoid relock
get time spec before protect g_basetime

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-01-13 19:11:49 -03:00
hujun5
daebf4c8b7 Revert "sched: misc: Remove sched_lock in _assert()"
reason:
This reverts commit d0820acbbb
assert will call syslog

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-14 00:52:10 +08:00
hujun5
699b23fa19 sched_processtimer: use atomic to protect g_timer_interval and g_timer_tick
reason:
We would like to replace the critical section with a small lock.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-14 00:27:13 +08:00
hujun5
5f4a15b690 pthread: remove enter_critical_section in pthread_mutex
reason:
We would like to replace the critical section with a small lock.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-12 22:30:35 +08:00
hujun5
19bca74c7c clock_adjtime: use small lock to protect g_adjtime_ppb g_adjtime_wdog
reason:
We would like to replace the critical section with a small lock.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-12 22:07:15 +08:00
hujun5
a2d4d74af7 clock_timekeeping: remove enter_critical_section in sched/clock/clock_timekeeping.c
reason:
We would like to replace the critical section with a small lock.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-12 16:51:40 +08:00