Commit Graph

2509 Commits

Author SHA1 Message Date
chao an
cd169e9b2c sched/pthread: simplify pthread_create() branch logic
remove redundant branch logic

Signed-off-by: chao an <anchao@lixiang.com>
2024-06-07 01:32:42 +08:00
chao an
e456c88c09 Revert "sched: replace some global variables to macro"
sched implementation not depends on macro abstraction, so revert below commit:

This reverts commit 4e62d0005a
This reverts commit 0f0c370520
This reverts commit ad0efd04ee

Signed-off-by: chao an <anchao@lixiang.com>
2024-06-06 22:00:25 +08:00
chao an
58ad896281 sched/signal: move signal structures pool to bss
Decoupling with memory allocator

Signed-off-by: chao an <anchao@lixiang.com>
2024-06-04 14:26:55 +08:00
chao an
81c140a953 sched/mqueue: move message queue buffer to bss
Signed-off-by: chao an <anchao@lixiang.com>
2024-06-04 14:26:55 +08:00
Ville Juven
c1ceec3404 sched/Kconfig: waitpid() depends on SCHED_HAVE_PARENT if BUILD_KERNEL
waitpid() cannot be used in kernel mode unless SCHED_HAVE_PARENT is
selected -> add dependency if BUILD_KERNEL is selected.

Why? Because without SCHED_HAVE_PARENT waitpid() works in a non-standard
way, meaning it does not use SIGCHLD to wake the parent, as it should.

Also, returning the child status via stat_loc corrupts memory as stat_loc
points to the parent's address environment:

pid_t nxsched_waitpid(pid_t pid, int *stat_loc, int options)
{
  ...
  group->tg_statloc = stat_loc;
  ...
}

And later when the status is returned, the child writes to tg_statloc,
which points to the parent's address environment -> random memory
corruption:

static inline void nxtask_exitwakeup(FAR struct tcb_s *tcb, int status)
{
  ...
  if (group->tg_statloc != NULL)
    {
      *group->tg_statloc = status << 8;
    }
  ...
}
2024-06-03 18:00:40 +02:00
Roy Feng
2e98f29277 tls: fix build error under CONFIG_PTHREAD_ATFORK enabled 2024-05-28 13:54:30 +02:00
Yanfeng Liu
0fc2e74b04 sched/mqueue: reclaim message upon failure
This reclaims the allocated message if it can't be sent to avoid
memory leaks.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2024-05-26 10:03:34 -03:00
Yanfeng Liu
d019828d3c sched/pthread: rename pthread_initialize.c as pthread_sem.c
This is to better match the contents of the source file.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2024-05-22 19:06:38 +08:00
hujun5
e067f897c0 sched: remove redundant variables "switched"
test:
We can use qemu for testing.

compiling
make distclean -j20; ./tools/configure.sh -l qemu-armv8a:nsh_smp ;make -j20
running
qemu-system-aarch64 -cpu cortex-a53 -smp 4 -nographic -machine virt,virtualization=on,gic-version=3 -net none -chardev stdio,id=con,mux=on -serial chardev:con -mon chardev=con,mode=readline -kernel ./nuttx

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-05-21 09:00:29 +08:00
chao an
66509e490e sched/mqueue: add support of tick based send() and receive()
Add tick based send()/receive() to reduce the cost of time conversion

Signed-off-by: chao an <anchao@lixiang.com>
2024-05-14 00:32:13 +08:00
simbit18
09bfaa7292 fix nxstyle
fix Relative file path does not match actual file.
2024-05-11 01:19:06 +08:00
chao an
1890b58e67 sched/misc: dump stack as platform bit width
before:
stack_dump: 0x8002ff80: 8003057b 00000000 00000000 00000000 00000002 00000000 80030068 00000000
stack_dump: 0x8002ffa0: 00000002 00000000 00000000 00000000 80030280 00000000 8000898e 00000000
stack_dump: 0x8002ffc0: 00000000 00000000 00000000 00000000 0000000b 00000000 0000000a 00000000
stack_dump: 0x8002ffe0: 8002a470 00000000 00000000 00000000 00000000 00000000 00000000 00000000

after:
stack_dump: 0x8002ff80: 000000008003057b 0000000000000000 0000000000000002 0000000080030068 0000000000000002 0000000000000000 0000000080030280 000000008000898e
stack_dump: 0x8002ffc0: 0000000000000000 0000000000000000 000000000000000b 000000000000000a 000000008002a470 0000000000000000 0000000000000000 0000000000000000

Signed-off-by: chao an <anchao@lixiang.com>
2024-05-10 19:42:41 +08:00
Yanfeng Liu
7ec304e6a8 pthread/join: catch null pexit_value case
This adds handling for null pointer in errout route as it crashes my
ostest sometimes.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2024-05-10 19:42:13 +08:00
Yanfeng Liu
a35b2a5cc7 sched/posixspawn: disable tedious logging
This log appears for NSH internal commands in KERNEL build and
duplicates a lot with task_spawn: log. So drop it to make the
DEBUG_SCHED_INFO logging more readable.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2024-05-10 15:40:34 +08:00
hujun5
9a36b8b823 csection: We can save execution time by removing judgments.
test:
We can use qemu for testing.

compiling
make distclean -j20; ./tools/configure.sh -l qemu-armv8a:nsh_smp ;make -j20
running
qemu-system-aarch64 -cpu cortex-a53 -smp 4 -nographic -machine virt,virtualization=on,gic-version=3 -net none -chardev stdio,id=con,mux=on -serial chardev:con -mon chardev=con,mode=readline -kernel ./nuttx

or
compiling
make distclean -j20; ./tools/configure.sh -l sabre-6quad:smp ;make -j20
running
qemu-system-arm  -semihosting -M sabrelite -m 1024 -smp 4 -kernel nuttx/nuttx -nographic

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-05-09 13:45:02 +08:00
hujun5
2ee8aa6f2b sched: we use spin_lock_irqsave replace enter_critical_section to protect g_irqvector
enter_critical_section may be called before os initialized

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-05-09 13:45:02 +08:00
hujun5
90f9ffc2e8 sched/sched: CONFIG_SCHED_RESUMESCHEDULER macro define error
we removed "select SCHED_RESUMESCHEDULER",
As SCHED_RESUMESCHEDULER is not a necessary feature in SMP,
turning it on by default may affect performance.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-05-09 01:39:47 +08:00
Yanfeng Liu
64e008f65b sched/task: minor polish on task_init()
This removes duplicated mask operation and revises comments.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2024-05-08 15:43:25 +08:00
hujun5
03895f1073 sched: sched: Simplify the scheduling logic in nxsched_add_readytorun() for SMP
test:
We can use qemu for testing.
compiling
make distclean -j20; ./tools/configure.sh -l qemu-armv8a:nsh_smp ;make -j20 running
qemu-system-aarch64 -cpu cortex-a53 -smp 4 -nographic -machine virt,virtualization=on,gic-version=3 -net none -chardev stdio,id=con,mux=on -serial chardev:con -mon chardev=con,mode=readline -kernel ./nuttx

We have also tested this patch on other ARM hardware platforms.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-05-07 01:15:55 +08:00
hujun5
a4aad284eb sched: wqueue: Remove sched_[un]lock from kwork_inherit.c
purpose:
1 sched_lock is very time-consuming, and reducing its invocations can improve performance.
2 sched_lock is prone to misuse, and narrowing its scope of use is to prevent people from referencing incorrect code and using it

test:
We can use qemu for testing.
compiling
make distclean -j20; ./tools/configure.sh -l qemu-armv8a:nsh_smp ;make -j20
running
qemu-system-aarch64 -cpu cortex-a53 -smp 4 -nographic -machine virt,virtualization=on,gic-version=3 -net none -chardev stdio,id=con,mux=on -serial chardev:con -mon chardev=con,mode=readline -kernel ./nuttx

We have also tested this patch on other ARM hardware platforms.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-05-01 11:08:07 +08:00
hujun5
8a6aa5f5bf sched/sched: There is no need to use sched_[un]lock
purpose:
1 sched_lock is very time-consuming, and reducing its invocations can improve performance.
2 sched_lock is prone to misuse, and narrowing its scope of use is to prevent people from referencing incorrect code and using it

test:
We can use qemu for testing.
compiling
make distclean -j20; ./tools/configure.sh -l qemu-armv8a:nsh_smp ;make -j20
running
qemu-system-aarch64 -cpu cortex-a53 -smp 4 -nographic -machine virt,virtualization=on,gic-version=3 -net none -chardev stdio,id=con,mux=on -serial chardev:con -mon chardev=con,mode=readline -kernel ./nuttx

We have also tested this patch on other ARM hardware platforms.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-04-30 11:34:24 -03:00
hujun5
d0820acbbb sched: misc: Remove sched_lock in _assert()
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-04-21 11:40:01 +02:00
chao an
3a5abf724e sched/Kconfig: make disable idle loop configurable
disable idle loop in experimental

Signed-off-by: chao an <anchao@lixiang.com>
2024-04-19 20:12:17 +08:00
Xiang Xiao
55e3cf7e2d sched: Move dump.c to sched_dumponexit.c
The dump.c is a part of sched module, so move it to sched directory.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2024-04-19 08:39:10 +08:00
hujun5
82acf6e6a7 irq: All irq_cpu_locked is called in the critical_section, and the parameter is the current cpu id.
so it must return false, We can safely delete.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-04-12 17:40:23 +08:00
hujun5
1b068b0d4b sched/irq: remove scheduling check in csection
1. The critical section does not prevent task scheduling
2. If the critical section is in sched_lock, there is no need to check,
   scheduling is not going to happen
3. If sched_lock is in the critical section, sched_unlock will also
   trigger scheduling without waiting for the exit of the critical section
4. After exiting the critical section, if there is an interrupt,
   the scheduling will be automatically triggered

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-04-12 16:15:42 +08:00
chao an
23e9489345 sched/mqueue: correct list parameter
1. correct list parameter
2. move global data to bss

Signed-off-by: chao an <anchao@lixiang.com>
2024-04-11 22:19:34 +08:00
hujun5
6c24ff95d4 timer: use spin_lock_irqsave protect g_alloctimers
all other g_alloctimers being protect by spin_lock_irqsave

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-04-11 10:45:34 +08:00
ligd
3844efb5b8 stack: update up_get_intstackbase API to support cpu id
For crash dump all the CPU intstack

Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-04-09 16:59:00 -03:00
chao an
09e5dca965 sched/pthread: detached thread should destroy the join info
In order to ensure the detached thread obtain the correct return
value from pthread_join()/pthread_cancel(), the detached thread
will create joininfo to save the detached status after thread
destroyed.  If there are too many of detached threads in the
process group, the joininfo will consume too much memory.
This is not friendly to embedded MCU devices.
This commit keep the semantics as #11898 was introduced,
will no longer save joininfo for detached threads to avoid wasting memory.

Signed-off-by: chao an <anchao@lixiang.com>
2024-04-09 13:46:51 +08:00
Mingjie Shen
c40421c05b SMP: Fix returning uninitialized variable in nxsched_add_readytorun()
Prior to this commit, doswitch is returned uninitialized if
(task_state == TSTATE_TASK_ASSIGNED || task_state == TSTATE_TASK_RUNNING)
&& no context switch occurs
&& (cpu == me).

Signed-off-by: Mingjie Shen <shen497@purdue.edu>
2024-04-03 22:30:45 +09:00
chao an
ad4fb55d1f sched/environ: Improve performance of set/unset environment
reserve dimensional array of environment to avoid frequent scaling

Signed-off-by: chao an <anchao@lixiang.com>
2024-04-03 00:10:56 +08:00
chao an
ee5d5d7312 sched/mqueue: merge System-V message queue initialize into posix
Signed-off-by: chao an <anchao@lixiang.com>
2024-03-26 17:32:11 +01:00
chao an
a8de85de0b sched/signal: add sanity check for siginfo
add sanity check for siginfo to ensure whether there is really a
consumer waiting for the signal, since the task state will not be
changed appropriately if in cancel/killed case

Test Case:
https://github.com/linux-test-project/ltp/blob/master/testcases/open_posix_testsuite/conformance/interfaces/sigpause/1-1.c#L63

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-26 20:55:04 +08:00
chao an
25339b0a17 sched/irq: spinlock should not depends on SMP
spinlock should work independently without CONFIG_SMP

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-26 15:21:52 +08:00
chao an
b9a74f30c6 sched/init: refine init state interface
N/A, refine init state interface

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-23 01:04:15 +08:00
chao an
f7434c2bc4 sched/init: move task list table from data to bss
N/A, help to extend the multi-core implementation

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-23 01:04:15 +08:00
chao an
7cc89bbd0e sched/init: move idle task initialize to inline function
N/A, help to extend the multi-core implementation

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-23 01:04:15 +08:00
chao an
63a0a544be sched/signal: merge allocate of signal pool
1. merge allocate of signal pool
2. remove unnecessary static variables

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-22 21:40:56 +08:00
chao an
feb6ede434 sched/cpu: replace up_cpu_index() to this_cpu()
In SMP mode, up_cpu_index()/this_cpu() are the same, both return the index of the physical core.
In AMP mode, up_cpu_index() will return the index of the physical core, and this_cpu() will always return 0

| #ifdef CONFIG_SMP
| #  define this_cpu()             up_cpu_index()
| #elif defined(CONFIG_AMP)
| #  define this_cpu()             (0)
| #else
| #  define this_cpu()             (0)
| #endif

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-21 18:52:35 +08:00
chao an
4e62d0005a sched: replace some global variables to macro
replace to macro will help to extend the scheduling implementation

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-21 18:51:25 +08:00
chao an
306c1c0b7d sched/tasklist: replace task status list with macro definition
replace to macro will help to extend the scheduling implementation

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-21 11:23:46 +09:00
chao an
ad0efd04ee sched/wqueue: replace some global variables to macro
replace to macro will help to extend the scheduling implementation

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-21 11:22:41 +09:00
Jukka Laitinen
7c6b6171f5 sched/group/group_killchildren.c: Force-cancel children if parent is force-cancelled
There is no point in waiting for children to exit if the parent is force-cancelled

Signed-off-by: Jukka Laitinen <jukkax@ssrc.tii.ae>
2024-03-20 18:03:15 -03:00
TaiJu Wu
23d65cb97f shced:sem: replace mutex with spinlock
The overhead of spinlok is less than mutext (mutex need to call
enter_critical section.)
After this patch, `down_write_trylock` and `down_read_trylock` can be
use in interrupt context.

The instruction is protected with mutex only one instruction so using
spinlock is better.

Signed-off-by: TaiJu Wu <tjwu1217@gmail.com>
2024-03-19 18:06:12 +08:00
chao an
d4d49e9645 stdio/file: unify group file list to common single queue
unify group file list to common single queue

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-18 19:32:49 +08:00
chao an
2b4002d9ad sched/signal: move unblock signal info to stack
struct tcb_s will reduce by 24 bytes

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-15 01:10:55 +08:00
chao an
df30a1f8d3 sched/pthread/join: refactor pthread join to support join task
1. add support to join main task

| static pthread_t self;
|
| static void *join_task(void *arg)
| {
|   int ret;
|   ret = pthread_join(self, NULL);          <---  /* Fix Task could not be joined */
|   return NULL;
| }
|
| int main(int argc, char *argv[])
| {
|   pthread_t thread;
|
|   self = pthread_self();
|
|   pthread_create(&thread, NULL, join_task, NULL);
|   sleep(1);
|
|   pthread_exit(NULL);
|   return 0;
| }

2. Detach active thread will not alloc for additional join, just update the task flag.
3. Remove the return value waiting lock logic (data_sem),
   the return value will be stored in the waiting tcb.
4. Revise the return value of pthread_join(), consistent with linux
   e.g:
       Joining a detached and canceled thread should return EINVAL, not ESRCH

   https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_join.html

   [EINVAL]
      The value specified by thread does not refer to a joinable thread.

NOTE:

This PR will not increase stack usage, but struct tcb_s will increase 32 bytes.

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-13 18:06:56 +09:00
chao an
be482cd830 sched/pthread: replace pthread_sem_give() to nxsem_post() to unify the post method
replace pthread_sem_give() to nxsem_post() to unify the post method

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-11 22:57:26 +08:00
chao an
68c21df444 sched/pthread/join: remove unused joininfo
remove unused joininfo, minor issue found from code reading

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-11 22:05:33 +08:00