Commit Graph

98 Commits

Author SHA1 Message Date
hujun5 d8cb7759b6 arch: remove up_cpu_pause up_cpu_resume up_cpu_paused up_cpu_pausereq
reason:
  To remove the "sync pause" and decouple the critical section from the dependency on enabling interrupts,
  after that we need to further implement "schedlock + spinlock".
changelist
  1 Modify the implementation of critical sections to no longer involve enabling interrupts or handling synchronous pause events.
  2 GIC_SMP_CPUCALL attach to pause handler to remove arch interface up_cpu_paused_restore up_cpu_paused_save
  3 Completely remove up_cpu_pause, up_cpu_resume, up_cpu_paused, and up_cpu_pausereq
  4 change up_cpu_pause_async to up_send_cpu_sgi

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-10-08 08:13:22 +08:00
ligd 90dcd5edd3 critmonitor: add caller support
Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-09-19 03:16:14 +08:00
yinshengkai e28b311b78 Make each function of SCHED_CRITMONITOR independent
There will be a large performance loss after SCHED_CRITMONITOR is enabled.
By isolating thread running time-related functions, CPU load can be run with less overhead.

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
Signed-off-by: buxiasen <buxiasen@xiaomi.com>
2024-09-19 03:16:14 +08:00
hujun5 a2c704a84a Revert "irq: add [enter|leave]_critical_section_nonirq"
This reverts commit 1c5a0bf6cc.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-09-17 20:12:59 +08:00
Alin Jerpelea eb9030c891 sched: migrate to SPDX identifier
Most tools used for compliance and SBOM generation use SPDX identifiers
This change brings us a step closer to an easy SBOM generation.

Signed-off-by: Alin Jerpelea <alin.jerpelea@sony.com>
2024-09-12 01:10:14 +08:00
hujun5 cd8db329de irq: simplify code by using OSINIT_TASK_READY
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-09-11 11:05:33 +08:00
hujun5 8439296a50 irq: inline restore_critical_section
reason:
In the SMP, when a context switch occurs, restore_critical_section is executed.
In order to reduce the time taken for context switching,
we inline the restore_critical_section function.
Given that restore_critical_section is small in size
and is called from only one location, inlining it does not increase the size of the image.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-09-10 23:14:09 +08:00
hujun5 1c5a0bf6cc irq: add [enter|leave]_critical_section_nonirq
Configuring NuttX and compile:
$ ./tools/configure.sh -l qemu-armv8a:nsh_smp
$ make
Running with qemu
$ 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

reason:
In some non-irq scenarios, we can simplify
the implementation of critical sections to improve performance.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-09-08 17:50:51 +08:00
hujun5 a4fece3450 spin_lock: inline spin_lock
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
2024-07-15 02:29:30 +08:00
hujun5 1bf4198556 irq: remove g_cpu_nestcount in restore_critical_section
because 'g_cpu_nestcount[me] > 0' will never happen, in this place

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-07-02 03:02:32 +08:00
hujun5 7db6632ab3 sched: irq: Change this_task to current_task(me) in restore_critical_section()
this_task() is a function call that involves disabling interrupts and this_cpu().
Since restore_critical_section is always called in an interrupt-disabled context,
there's no need to disable interrupts again. Therefore, to save time and achieve
the same effect, I directly call tcb = current_task(me) instead of tcb = this_task().

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-06-21 11:11:07 +09:00
hujun5 f7843e2198 sched:remove g_cpu_schedlock g_cpu_irqsetlock g_cpu_locksetlock
we can use g_cpu_lockset to determine whether we are currently in the scheduling lock,
and all accesses and modifications to g_cpu_lockset, g_cpu_irqlock, g_cpu_irqset
are in the critical section, so we can directly operate on 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

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-06-21 11:11:07 +09: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 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 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 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
ligd 2241969e5a SMP: fix crash when switch to new task which is still running
cpu0 thread0:                        cpu1:
sched_yield()
nxsched_set_priority()
nxsched_running_setpriority()
nxsched_reprioritize_rtr()
nxsched_add_readytorun()
up_cpu_pause()
                                     IRQ enter
                                     arm64_pause_handler()
                                     enter_critical_section() begin
                                     up_cpu_paused() pick thread0
                                     arm64_restorestate() set thread0 tcb->xcp.regs to CURRENT_REGS
up_switch_context()
  thread0 -> thread1
arm64_syscall()
    case SYS_switch_context
     change thread0 tcb->xcp.regs
    restore_critical_section()
                                     enter_critical_section() done
                                     leave_critical_section()
                                     IRQ leave with restore CURRENT_REGS
                                     ERROR !!!

Reason:
As descript above, cpu0 swith task: thread0 -> thread1, and the
syscall() execute slowly, this time cpu1 pick thread0 to run at
up_cpu_paused(). Then cpu0 syscall execute, cpu1 IRQ leave error.

Resolve:
Move arm64_restorestate() after enter_critical_section() done

This is a continued fix with:
https://github.com/apache/nuttx/pull/6833

Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-03-01 21:05:00 +09:00
Xiang Xiao dfa0283d83 spinlock: Rename spin_islocked to spin_is_locked
align with Linux naming style

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-10-06 20:03:19 -04:00
TaiJu Wu ffba0d15a5 Feature: implement ticket spinlock
test config: ./tools/configure.sh -l qemu-armv8a:nsh_smp

Pass ostest

No matter big-endian or little-endian, ticket spinlock only check the
next and the owner is equal or not.

If they are equal, it means there is a task hold the lock or lock is
free.

Signed-off-by: TaiJu Wu <tjwu1217@gmail.com>

Co-authored-by: Xiang Xiao <xiaoxiang781216@gmail.com>
2023-10-07 01:38:37 +08:00
zhangyuan21 632d87ee71 arch: remove up_release_pending function
Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2022-12-16 21:29:57 +08:00
yinshengkai 552cf4b549 merge sched_note_spinxx into sched_note_spincommon
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2022-12-16 17:03:53 +08:00
Xiang Xiao bdeaea3742 Remove the unnessary empty line after label
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-09-30 17:54:56 +02:00
ligd e2df52390a SMP: fix crash when switch to new task which is still running
Situation:

Assume we have 2 cpus, and busy run task0.

CPU0                                CPU1
task0 -> task1                      task2 -> task0
1. remove task0 form runninglist
2. take task1 as new tcb
3. add task0 to blocklist
4. clear spinlock
                                    4.1 remove task2 form runninglist
                                    4.2 take task0 as new tcb
                                    4.3 add task2 to blocklist
                                    4.4 use svc ISR swith to task0
                                    4.5 crash
5. use svc ISR swith to task1

Fix:
Move clear spinlock to the end of svc ISR

Signed-off-by: ligd <liguiding1@xiaomi.com>
2022-09-17 17:37:47 +09:00
zhuyanlin 0460367a70 irq_csection: fix assert warning
constant 65535 with expression of type 'int16_t' (aka 'short')
is always true

Signed-off-by: zhuyanlin <zhuyanlin1@xiaomi.com>
2022-03-29 13:44:01 +08:00
Abdelatif Guettouche 3b2aea204c sched/irq/irq_csection.c: Fix typos and correct some comments.
Signed-off-by: Abdelatif Guettouche <abdelatif.guettouche@espressif.com>
2021-09-16 10:53:51 -05:00
Abdelatif Guettouche 20d34cce98 sched/irq_section.c: irq_waitlock is a private function; remove its
prototype from irq.h and define it as static.

Signed-off-by: Abdelatif Guettouche <abdelatif.guettouche@espressif.com>
2021-09-15 06:20:59 +09:00
Xiang Xiao 2e54df0f35 Don't include assert.h from public header file
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2021-06-03 08:36:03 -07:00
Masayuki Ishikawa 1b00e5d518 spinlock: Remove SP_SECTION
Summary:
- SP_SECTION was introduced to allocate spinlock in non-cachable
  region mainly for Cortex-A to stabilize the NuttX SMP kernel
- However, all spinlocks are now allocated in cachable area and
  works without any problems
- So SP_SECTION should be removed to simplify the kernel code

Impact:
- None

Testing:
- Build test only

Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-04-20 22:41:44 -05:00
Masayuki Ishikawa a24905059e sched: irq: Change irq_waitlock() from private to public
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2020-12-10 08:33:42 +01:00
Masayuki Ishikawa 12862c7b33 armv7-a: Fix comments on Cortex-A SGI
Summary:
- I noticed that Cortex-A SGI can be masked
- We thought the SGI is not maskable
- Although I can not remember how I tested it before
- It actually works as expected now
- Also, fixed the number of remaining bugs in TODO

Impact:
- No impact

Testing:
- Tested with sabre-6quad:smp (QEMU and dev board)
- Add the following code in up_idle() before calling asm("WFI");
+  if (0 != up_cpu_index())
+    {
+      up_irq_save();
+    }
- Run the hello app, you can see "Hello, World!!"
- But nsh will freeze soon because arm_pause_handler is not called.

Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2020-11-19 18:01:30 -08:00
Masayuki Ishikawa 6ec94082a1 sched: irq: Fix enter_critical_section() in an irq handler for SMP
Summary:
- I found a deadlock during Wi-Fi audio streaming test plus stress test
- The testing environment was spresense:wifi_smp (NCPUS=4)
- The deadlock happened because two CPUs called up_cpu_pause() almost simultaneously
- This situation should not happen, because up_cpu_pause() is called in a critical section
- Actually, the latter call was from nxsem_post() in an IRQ handler
- And when enter_critical_section() was called, irq_waitlock() detected a deadlock
- Then it called up_cpu_paused() to break the deadlock
- However, this resulted in setting g_cpu_irqset on the CPU
- Even though another CPU had held a g_cpu_irqlock
- This situation violates the critical section and should be avoided
- To avoid the situation, if a CPU sets g_cpu_irqset after calling up_cpu_paused()
- The CPU must release g_cpu_irqlock first
- Then retry irq_waitlock() to acquire g_cpu_irqlock

Impact:
- Affect SMP

Testing:
- Tested with spresense:wifi_smp (NCPUS=2 and 4)
- Tested with spresense:smp
- Tested with sim:smp
- Tested with sabre-6quad:smp (QEMU)
- Tested with maix-bit:smp (QEMU)
- Tested with esp32-core:smp (QEMU)
- Tested with lc823450-xgevk:rndis

Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2020-11-03 08:19:41 +01:00
Masayuki Ishikawa 08c4376606 arch, include, sched : Refactor ARCH_GLOBAL_IRQDISABLE related code
Summary:
- ARCH_GLOBAL_IRQDISABLE was initially introduced for LC823450 SMP
- At that time, i.MX6 (quad Cortex-A9) did not use this config
- However, this option is now used for all CPUs which support SMP
- So it's good timing for refactoring the code

Impact:
- Should have no impact because the logic is the same for SMP

Testing:
- Tested with board: spresense:smp, spresense:wifi_smp
- Tested with qemu: esp32-core:smp, maix-bit:smp, sabre-6quad:smp
- Build only: lc823450-xgevk:rndis, sam4cmp-db:nsh

Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2020-09-03 10:20:20 +08:00
Gregory Nutt 3ac629bdfb Run all .c and .h files modifed by the PR though nxstyle. 2020-05-09 16:58:42 -03:00
Gregory Nutt f92dba212d sched/sched/sched.h: Make naming of all internal names consistent:
1. Add internal scheduler functions should begin with nxsched_, not sched_
2. Follow the consistent naming patter of https://cwiki.apache.org/confluence/display/NUTTX/Naming+of+OS+Internal+Functions
2020-05-09 16:58:42 -03:00
Xiang Xiao c397692532 sched/semaphore/spinlock.c: spin_trylock handle memory barrier and instrumentation correctly. 2019-11-03 19:40:58 -06:00
Gregory Nutt b5111d2c38 tools/nxstyle.c: Add logic to detect if there is a blank line following the final right brace. sched/: Applied the modified nxstyle to all C file as a test. 2019-10-24 11:02:42 -06:00
Gregory Nutt a2e62f557d Squashed commit of the following:
sched/init/nx_bringup.c:  Fix a naming collision.
    sched/init:  Rename os_start() to nx_start()
    sched/init:  Rename os_smp* to nx_smp*
    sched/init:  Rename os_bringup to nx_bringup
    sched/init:  rename all internal static functions to begin with nx_ vs os_
2019-02-04 16:20:35 -06:00
Gregory Nutt 4ca7b72a98 sched/Kconfig: Simplify some configurations. This adds configuration settings that control individual features, rather than long complex OR expressions that determines if an individual feature is required. 2018-11-25 11:50:15 -06:00
Gregory Nutt 807d5bb4ae Critical Section Monitor: Add low level timer support for simulation. Fix serial bugs and logic errors in initial implementation. Still does not work; takes assertions. 2018-11-24 15:07:12 -06:00
Gregory Nutt fc6084f311 Squashed commit of the following:
fs/procfs/fs_procfsproc:  Extended the process ID ProcFS output to show per-thread maximum time for pre-emption disabled and maximum time within a critical section.

    sched/sched/sched_critmonitor.c:  Adds data collection logic in support of monitoring critical sections and pre-emption state.
2018-11-24 10:32:45 -06:00
Gregory Nutt 50ab5d638a sched/task: It is not appropriate for logic in task_exit() to call the new version of this_task(). sched/irq: Remove redundant fetch of CPU index; configs/sabre-6qguad: update README. 2018-02-06 19:06:33 -06:00
Gregory Nutt 0ba7853016 sched/irq: Fix a infinite recursion problem that a recent change introduced into the i.MX6 SMP implementation. 2018-02-06 09:18:03 -06:00
Gregory Nutt 56bb76caca sched/irq: Fix an error in a assertion introduced in commit 37c9b3d54a. Noted by Masayuki Ishikawa. 2018-02-05 21:13:42 -06:00
Gregory Nutt b884fb9fed sched/sched: Extend the last global lock change to work with the lc823450-xgevk which does not support the atomic fetch add but does support disabling interprocessor interrupts. Disabling interprocessor interrupts will also guarantee that the TCB addres calculation is atomic. 2018-02-05 13:32:09 -06:00
Gregory Nutt 37c9b3d54a sched/sched: Implements a global scheduler lock capability as part of SMP support. This allows the scheduler to be locked with no knowledge or access to the TCB of the currently running task. This is necessary because accessing the TCB of the currenlty running task is, itself, a non-atomic operation. This global scheduler lock cpability was add just to support that atomic access to the TCB. 2018-02-05 13:12:36 -06:00
Gregory Nutt 7cf88d7dbd Make sure that labeling is used consistently in all function headers. 2018-02-01 10:00:02 -06:00
Gregory Nutt 12e3e47d3c Update comments, mostly spelling errors. 2018-01-27 09:37:46 -06:00
Arjun Hary b274a97840 Miscellaneous fixes from astyle tool. 2017-08-14 17:19:27 -06:00