LCOV - code coverage report
Current view: top level - /home/ubuntu/zephyrproject/zephyr/kernel - sched.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 50.2 % 229 115
Test Date: 2026-11-06 14:43:19 Functions: 56.7 % 30 17
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 33.7 % 104 35

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2018 Intel Corporation
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: Apache-2.0
       5                 :             :  */
       6                 :             : #include <zephyr/kernel.h>
       7                 :             : #include <kspinlock.h>
       8                 :             : #include <ksched.h>
       9                 :             : #include <zephyr/spinlock.h>
      10                 :             : #include <wait_q.h>
      11                 :             : #include <kthread.h>
      12                 :             : #include <priority_q.h>
      13                 :             : #include <kswap.h>
      14                 :             : #include <ipi.h>
      15                 :             : #include <ksmp.h>
      16                 :             : #include <kernel_arch_func.h>
      17                 :             : #include <zephyr/internal/syscall_handler.h>
      18                 :             : #include <zephyr/drivers/timer/system_timer.h>
      19                 :             : #include <stdbool.h>
      20                 :             : #include <kernel_internal.h>
      21                 :             : #include <zephyr/logging/log.h>
      22                 :             : #include <zephyr/sys/atomic.h>
      23                 :             : #include <zephyr/sys/math_extras.h>
      24                 :             : #include <zephyr/timing/timing.h>
      25                 :             : #include <zephyr/sys/util.h>
      26                 :             : #include <metairq.h>
      27                 :             : #include <run_q.h>
      28                 :             : #include <timeslicing.h>
      29                 :             : 
      30                 :             : LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
      31                 :             : 
      32                 :             : /* pending_current is owned by timeslicing.c; sleep.c also accesses it */
      33                 :             : #if defined(CONFIG_SWAP_NONATOMIC) && defined(CONFIG_TIMESLICING)
      34                 :             : extern struct k_thread *pending_current;
      35                 :             : #endif
      36                 :             : 
      37                 :             : struct k_spinlock _sched_spinlock;  /* The scheduler's spinlock */
      38                 :             : 
      39                 :             : /* Storage to "complete" the context switch from an invalid/incomplete thread
      40                 :             :  * context (ex: exiting an ISR that aborted _current)
      41                 :             :  */
      42                 :             : __incoherent struct k_thread _thread_dummy;
      43                 :             : 
      44                 :             : static ALWAYS_INLINE void update_cache(int preempt_ok);
      45                 :             : static void halt_thread(struct k_thread *thread, uint8_t new_state, k_spinlock_key_t *key);
      46                 :             : static void add_to_waitq_locked(struct k_thread *thread, _wait_q_t *wait_q);
      47                 :             : 
      48                 :             : #ifndef CONFIG_SMP
      49                 :             : /* Uniprocessor selection of the next thread, used to refresh the ready
      50                 :             :  * queue cache. The SMP scheduler selects its next thread at the context
      51                 :             :  * switch instead, in kernel/smp/sched_smp.c.
      52                 :             :  */
      53                 :         130 : static ALWAYS_INLINE struct k_thread *next_up(void)
      54                 :             : {
      55                 :         130 :         struct k_thread *thread = runq_best();
      56                 :             : 
      57                 :         130 :         thread = metairq_preempt_recover(thread);
      58                 :             : 
      59                 :             :         /* In uniprocessor mode, we can leave the current thread in
      60                 :             :          * the queue (actually we have to, otherwise the assembly
      61                 :             :          * context switch code for all architectures would be
      62                 :             :          * responsible for putting it back in z_swap and ISR return!),
      63                 :             :          * which makes this choice simple.
      64                 :             :          */
      65         [ -  + ]:         130 :         return (thread != NULL) ? thread : _current_cpu->idle_thread;
      66                 :             : }
      67                 :             : #endif /* !CONFIG_SMP */
      68                 :             : 
      69                 :           0 : void move_current_to_end_of_prio_q(void)
      70                 :             : {
      71                 :           0 :         runq_yield();
      72                 :             : 
      73                 :           0 :         update_cache(1);
      74                 :           0 : }
      75                 :             : 
      76                 :         130 : static ALWAYS_INLINE void update_cache(int preempt_ok)
      77                 :             : {
      78                 :             : #ifdef CONFIG_SMP
      79                 :             :         /* There is no ready queue cache on SMP: the next thread is selected
      80                 :             :          * at the context switch, so only record whether a cooperative swap
      81                 :             :          * is OK until then.
      82                 :             :          */
      83                 :             :         z_smp_swap_ok_set(preempt_ok);
      84                 :             : #else
      85                 :         130 :         struct k_thread *thread = next_up();
      86                 :             : 
      87         [ +  + ]:         130 :         if (should_preempt(thread, preempt_ok)) {
      88                 :             : #ifdef CONFIG_TIMESLICING
      89         [ +  + ]:         126 :                 if (thread != _current) {
      90                 :         121 :                         z_time_slice_reset(thread);
      91                 :             :                 }
      92                 :             : #endif /* CONFIG_TIMESLICING */
      93                 :         126 :                 update_metairq_preempt(thread);
      94                 :         126 :                 _kernel.ready_q.cache = thread;
      95                 :             :         } else {
      96                 :           4 :                 _kernel.ready_q.cache = _current;
      97                 :             :         }
      98                 :             : #endif /* CONFIG_SMP */
      99                 :         130 : }
     100                 :             : 
     101                 :          65 : static inline void ready_thread(struct k_thread *thread)
     102                 :             : {
     103                 :             : #ifdef CONFIG_KERNEL_COHERENCE
     104                 :             :         __ASSERT_NO_MSG(sys_cache_is_mem_coherent(thread));
     105                 :             : #endif /* CONFIG_KERNEL_COHERENCE */
     106                 :             : 
     107                 :             :         /* If thread is queued already, do not try and added it to the
     108                 :             :          * run queue again
     109                 :             :          */
     110   [ +  -  +  - ]:          65 :         if (!z_is_thread_queued(thread) && z_is_thread_ready(thread)) {
     111                 :          65 :                 SYS_PORT_TRACING_OBJ_FUNC(k_thread, sched_ready, thread);
     112                 :             : 
     113                 :          65 :                 queue_thread(thread);
     114                 :          65 :                 update_cache(0);
     115                 :             : 
     116                 :          65 :                 flag_ipi(ipi_mask_create(thread));
     117                 :             :         }
     118                 :          65 : }
     119                 :             : 
     120                 :           1 : void z_ready_thread(struct k_thread *thread)
     121                 :             : {
     122         [ +  + ]:           2 :         Z_SCHED_SPINLOCK {
     123         [ +  - ]:           1 :                 if (z_smp_thread_active_elsewhere(thread) == NULL) {
     124                 :           1 :                         ready_thread(thread);
     125                 :             :                 }
     126                 :             :         }
     127                 :           1 : }
     128                 :             : 
     129                 :             : void z_sched_ready_locked(struct k_thread *thread) ALIAS_OF(ready_thread);
     130                 :             : 
     131                 :          10 : static void unready_thread(struct k_thread *thread)
     132                 :             : {
     133         [ +  - ]:          10 :         if (z_is_thread_queued(thread)) {
     134                 :             :                 /* Clear idle CPU coverage before removing the thread from the run queue. */
     135                 :          10 :                 ipi_idle_thread_unreserve(thread);
     136                 :          10 :                 dequeue_thread(thread);
     137                 :             :         }
     138                 :          10 :         update_cache(thread == _current);
     139                 :          10 : }
     140                 :             : 
     141                 :             : /* This routine exists for benchmarking purposes. It is not used in
     142                 :             :  * general production code.
     143                 :             :  */
     144                 :           0 : void z_unready_thread(struct k_thread *thread)
     145                 :             : {
     146         [ #  # ]:           0 :         Z_SCHED_SPINLOCK {
     147                 :           0 :                 unready_thread(thread);
     148                 :             :         }
     149                 :           0 : }
     150                 :             : 
     151                 :             : 
     152                 :             : void z_sched_unready_locked(struct k_thread *thread) ALIAS_OF(unready_thread);
     153                 :             : 
     154                 :             : /* This routine only used for testing purposes */
     155                 :           0 : void z_yield_testing_only(void)
     156                 :             : {
     157         [ #  # ]:           0 :         Z_SCHED_SPINLOCK {
     158                 :           0 :                 move_current_to_end_of_prio_q();
     159                 :             :         }
     160                 :           0 : }
     161                 :             : 
     162                 :             : /* Shared handler for k_thread_{suspend,abort}().  Called with the
     163                 :             :  * scheduler lock held and the key passed (which it may
     164                 :             :  * release/reacquire!) which will be released before a possible return
     165                 :             :  * (aborting _current will not return, obviously), which may be after
     166                 :             :  * a context switch.
     167                 :             :  */
     168                 :          54 : void z_thread_halt(struct k_thread *thread, k_spinlock_key_t key,
     169                 :             :                                         bool terminate)
     170                 :             : {
     171                 :          54 :         z_metairq_preempted_clear(thread);
     172                 :             : 
     173                 :             :         /* If the target is a thread running on another CPU, flag and
     174                 :             :          * poke (note that we might spin to wait, so a true
     175                 :             :          * synchronous IPI is needed here, not deferred!), it will
     176                 :             :          * halt itself in the IPI.  Otherwise it's unscheduled, so we
     177                 :             :          * can clean it up directly.
     178                 :             :          */
     179                 :             : 
     180                 :          54 :         struct _cpu *cpu = z_smp_thread_active_elsewhere(thread);
     181                 :             : 
     182         [ +  - ]:          54 :         if (cpu != NULL) {
     183                 :             :                 z_smp_halt_remote(thread, cpu, key, terminate);
     184                 :             :         } else {
     185         [ -  + ]:          54 :                 halt_thread(thread, terminate ? _THREAD_DEAD : _THREAD_SUSPENDED, &key);
     186   [ +  -  +  - ]:          54 :                 if ((thread == _current) && !arch_is_in_isr()) {
     187         [ -  + ]:          54 :                         if (z_is_thread_essential(thread)) {
     188                 :           0 :                                 z_sched_spinlock_unlock(key);
     189                 :           0 :                                 k_panic();
     190                 :           0 :                                 key = z_sched_spinlock_lock();
     191                 :             :                         }
     192                 :          54 :                         z_swap_locked(key);
     193         [ #  # ]:           0 :                         __ASSERT(!terminate, "aborted _current back from dead");
     194                 :             :                 } else {
     195                 :           0 :                         z_sched_spinlock_unlock(key);
     196                 :             :                 }
     197                 :             :         }
     198                 :             :         /* NOTE: the scheduler lock has been released.  Don't put
     199                 :             :          * logic here, it's likely to be racy/deadlocky even if you
     200                 :             :          * re-take the lock!
     201                 :             :          */
     202                 :           0 : }
     203                 :             : 
     204                 :         109 : static inline bool resched(uint32_t key)
     205                 :             : {
     206                 :         109 :         z_smp_swap_ok_set(false);
     207                 :             : 
     208   [ +  -  -  + ]:         109 :         return arch_irq_unlocked(key) && !arch_is_in_isr();
     209                 :             : }
     210                 :             : 
     211                 :             : /*
     212                 :             :  * Check if the next ready thread is the same as the current thread
     213                 :             :  * and save the trip if true.
     214                 :             :  */
     215                 :         109 : static inline bool need_swap(void)
     216                 :             : {
     217                 :             :         /* the SMP case will be handled in C based z_swap() */
     218                 :             : #ifdef CONFIG_SMP
     219                 :             :         return true;
     220                 :             : #else
     221                 :         109 :         struct k_thread *new_thread;
     222                 :             : 
     223                 :             :         /* Check if the next ready thread is the same as the current thread */
     224                 :         109 :         new_thread = _kernel.ready_q.cache;
     225                 :         109 :         return new_thread != _current;
     226                 :             : #endif /* CONFIG_SMP */
     227                 :             : }
     228                 :             : 
     229                 :          59 : static void reschedule(struct k_spinlock *lock, k_spinlock_key_t key)
     230                 :             : {
     231   [ +  -  +  + ]:          59 :         if (resched(key.key) && need_swap()) {
     232                 :          50 :                 z_swap(lock, key);
     233                 :             :         } else {
     234                 :           9 :                 signal_pending_ipi();
     235                 :           9 :                 k_spin_unlock(lock, key);
     236                 :             :         }
     237                 :          59 : }
     238                 :             : 
     239                 :             : /**
     240                 :             :  * Like reschedule(), but the scheduler's spinlock is known to be the lock.
     241                 :             :  */
     242                 :          55 : static void reschedule_locked(k_spinlock_key_t key)
     243                 :             : {
     244                 :          55 :         return reschedule(&_sched_spinlock, key);
     245                 :             : }
     246                 :             : 
     247                 :           1 : void z_sched_lock_reschedule(k_spinlock_key_t key)
     248                 :             : {
     249                 :           1 :         update_cache(0);
     250                 :           1 :         reschedule_locked(key);
     251                 :           1 : }
     252                 :             : 
     253                 :           0 : void z_sched_yield(void)
     254                 :             : {
     255                 :           0 :         k_spinlock_key_t key = z_sched_spinlock_lock();
     256                 :             : 
     257                 :           0 :         runq_yield();
     258                 :           0 :         update_cache(1);
     259                 :           0 :         z_swap_locked(key);
     260                 :           0 : }
     261                 :             : 
     262                 :             : /* The scheduler's spinlock must be held */
     263                 :          10 : static void add_to_waitq_locked(struct k_thread *thread, _wait_q_t *wait_q)
     264                 :             : {
     265                 :             :         /* A thread must not already be on a wait queue when added to a new one. */
     266         [ -  + ]:          10 :         __ASSERT_NO_MSG(thread->base.pended_on == NULL);
     267                 :             : 
     268                 :          10 :         unready_thread(thread);
     269                 :          10 :         z_mark_thread_as_pending(thread);
     270                 :             : 
     271                 :          10 :         SYS_PORT_TRACING_FUNC(k_thread, sched_pend, thread);
     272                 :             : 
     273         [ +  - ]:          10 :         if (wait_q != NULL) {
     274                 :          10 :                 thread->base.pended_on = wait_q;
     275                 :          10 :                 _priq_wait_add(&wait_q->waitq, thread);
     276                 :             :         }
     277                 :          10 : }
     278                 :             : 
     279                 :             : void z_sched_add_to_waitq_locked(struct k_thread *thread, _wait_q_t *wait_q)
     280                 :             :         ALIAS_OF(add_to_waitq_locked);
     281                 :             : 
     282                 :           4 : static void pend_locked(struct k_thread *thread, _wait_q_t *wait_q,
     283                 :             :                         k_timeout_t timeout)
     284                 :             : {
     285                 :             : #ifdef CONFIG_KERNEL_COHERENCE
     286                 :             :         __ASSERT_NO_MSG(wait_q == NULL || sys_cache_is_mem_coherent(wait_q));
     287                 :             : #endif /* CONFIG_KERNEL_COHERENCE */
     288                 :           4 :         add_to_waitq_locked(thread, wait_q);
     289                 :           4 :         z_add_thread_timeout(thread, timeout);
     290                 :           4 : }
     291                 :             : 
     292                 :           0 : void z_pend_thread(struct k_thread *thread, _wait_q_t *wait_q,
     293                 :             :                    k_timeout_t timeout)
     294                 :             : {
     295   [ #  #  #  # ]:           0 :         __ASSERT_NO_MSG(thread == _current || is_thread_dummy(thread));
     296         [ #  # ]:           0 :         Z_SCHED_SPINLOCK {
     297                 :           0 :                 pend_locked(thread, wait_q, timeout);
     298                 :             :         }
     299                 :           0 : }
     300                 :             : 
     301                 :           0 : void z_unpend_thread_no_timeout(struct k_thread *thread)
     302                 :             : {
     303         [ #  # ]:           0 :         Z_SCHED_SPINLOCK {
     304         [ #  # ]:           0 :                 if (thread->base.pended_on != NULL) {
     305                 :           0 :                         unpend_thread_no_timeout(thread);
     306                 :             :                 }
     307                 :             :         }
     308                 :           0 : }
     309                 :             : 
     310                 :           0 : void z_sched_wake_thread_locked(struct k_thread *thread)
     311                 :             : {
     312                 :             :         /* No K_SPINLOCK: caller must hold the scheduler's spinlock when calling */
     313                 :           0 :         bool killed = (thread->base.thread_state &
     314                 :             :                         (_THREAD_DEAD | _THREAD_ABORTING));
     315                 :             : 
     316         [ #  # ]:           0 :         if (!killed) {
     317                 :             :                 /* The thread is not being killed */
     318         [ #  # ]:           0 :                 if (thread->base.pended_on != NULL) {
     319                 :           0 :                         unpend_thread_no_timeout(thread);
     320                 :             :                 }
     321                 :           0 :                 z_mark_thread_as_not_sleeping(thread);
     322                 :           0 :                 ready_thread(thread);
     323                 :             :         }
     324                 :           0 : }
     325                 :             : 
     326                 :             : #ifdef CONFIG_SYS_CLOCK_EXISTS
     327                 :             : /* Timeout handler for *_thread_timeout() APIs */
     328                 :           0 : void z_thread_timeout(struct _timeout *timeout)
     329                 :             : {
     330                 :           0 :         struct k_thread *thread = CONTAINER_OF(timeout,
     331                 :             :                                                struct k_thread, base.timeout);
     332                 :             : 
     333         [ #  # ]:           0 :         Z_SCHED_SPINLOCK {
     334                 :             :                 /* A concurrent waker (e.g. a sem give on another CPU) may
     335                 :             :                  * have unpended and readied the thread, after which the
     336                 :             :                  * thread could run and re-pend elsewhere -- possibly with no
     337                 :             :                  * timeout. Such a waker aborts this timeout, flagging it
     338                 :             :                  * superseded; bail so we don't wake the thread from its new
     339                 :             :                  * wait.
     340                 :             :                  */
     341         [ #  # ]:           0 :                 if (!z_timeout_inflight_superseded(timeout)) {
     342                 :           0 :                         z_sched_wake_thread_locked(thread);
     343                 :             :                 }
     344                 :             :         }
     345                 :           0 : }
     346                 :             : #endif /* CONFIG_SYS_CLOCK_EXISTS */
     347                 :             : 
     348                 :           4 : int z_pend_curr(struct k_spinlock *lock, k_spinlock_key_t key,
     349                 :             :                _wait_q_t *wait_q, k_timeout_t timeout)
     350                 :             : {
     351                 :             :         /* A blocking pend from ISR context is a programming error with no
     352                 :             :          * safe recovery: it would sleep whatever thread was interrupted and,
     353                 :             :          * on CONFIG_SWAP_NONATOMIC, corrupt the scheduler. Refuse it fatally
     354                 :             :          * in every build rather than degrading into the corruption traced in
     355                 :             :          * #111518. Placed first so the refusal performs no side effect.
     356                 :             :          */
     357         [ -  + ]:           4 :         if (arch_is_in_isr()) {
     358                 :           0 :                 __ASSERT(false, "blocking pend from ISR context");
     359                 :           4 :                 k_panic();
     360                 :             :         }
     361                 :             : 
     362                 :             : #if defined(CONFIG_TIMESLICING) && defined(CONFIG_SWAP_NONATOMIC)
     363                 :             :         pending_current = _current;
     364                 :             : #endif /* CONFIG_TIMESLICING && CONFIG_SWAP_NONATOMIC */
     365                 :           4 :         __ASSERT_NO_MSG((sizeof(struct k_spinlock) == 0) || !z_is_sched_spinlock(lock));
     366                 :             : 
     367                 :             :         /* We do a "lock swap" prior to calling z_swap(), such that
     368                 :             :          * the caller's lock gets released as desired.  But we ensure
     369                 :             :          * that we hold the scheduler lock and leave local interrupts
     370                 :             :          * masked until we reach the context switch.  z_swap() itself
     371                 :             :          * has similar code; the duplication is because it's a legacy
     372                 :             :          * API that doesn't expect to be called with scheduler lock
     373                 :             :          * held.
     374                 :             :          */
     375                 :           4 :         (void) z_sched_spinlock_lock();
     376                 :           4 :         pend_locked(_current, wait_q, timeout);
     377                 :           4 :         k_spin_release(lock);
     378                 :           4 :         return z_swap_locked(key);
     379                 :             : }
     380                 :             : 
     381                 :           0 : struct k_thread *z_unpend1_no_timeout(_wait_q_t *wait_q)
     382                 :             : {
     383                 :           0 :         struct k_thread *thread = NULL;
     384                 :             : 
     385         [ #  # ]:           0 :         Z_SCHED_SPINLOCK {
     386                 :           0 :                 thread = _priq_wait_best(&wait_q->waitq);
     387                 :             : 
     388         [ #  # ]:           0 :                 if (thread != NULL) {
     389                 :           0 :                         unpend_thread_no_timeout(thread);
     390                 :             :                 }
     391                 :             :         }
     392                 :             : 
     393                 :           0 :         return thread;
     394                 :             : }
     395                 :             : 
     396                 :           0 : void z_unpend_thread(struct k_thread *thread)
     397                 :             : {
     398                 :           0 :         k_spinlock_key_t key = z_sched_spinlock_lock();
     399                 :             : 
     400         [ #  # ]:           0 :         if (thread->base.pended_on != NULL) {
     401                 :           0 :                 unpend_thread_no_timeout(thread);
     402                 :             :         }
     403         [ #  # ]:           0 :         while (z_try_abort_thread_timeout(thread) == -EAGAIN) {
     404                 :           0 :                 z_sched_spinlock_unlock(key);
     405                 :           0 :                 key = z_sched_spinlock_lock();
     406                 :             :         }
     407                 :           0 :         z_sched_spinlock_unlock(key);
     408                 :           0 : }
     409                 :             : 
     410                 :             : /* Priority set utility that does no rescheduling, it just changes the
     411                 :             :  * run queue state, returning true if a reschedule is needed later.
     412                 :             :  */
     413                 :           0 : bool z_thread_prio_set(struct k_thread *thread, int prio)
     414                 :             : {
     415                 :           0 :         bool need_sched = false;
     416                 :           0 :         int old_prio = thread->base.prio;
     417                 :             : 
     418         [ #  # ]:           0 :         Z_SCHED_SPINLOCK {
     419                 :           0 :                 need_sched = z_is_thread_ready(thread);
     420                 :             : 
     421         [ #  # ]:           0 :                 if (need_sched) {
     422                 :           0 :                         if (!IS_ENABLED(CONFIG_SMP) || z_is_thread_queued(thread)) {
     423                 :           0 :                                 dequeue_thread(thread);
     424                 :           0 :                                 thread->base.prio = prio;
     425                 :           0 :                                 queue_thread(thread);
     426                 :             : 
     427                 :           0 :                                 if (old_prio > prio) {
     428                 :           0 :                                         flag_ipi(ipi_mask_create(thread));
     429                 :             :                                 }
     430                 :             :                         } else {
     431                 :             :                                 /*
     432                 :             :                                  * This is a running thread on SMP. Update its
     433                 :             :                                  * priority, but do not requeue it. An IPI is
     434                 :             :                                  * needed if the priority is both being lowered
     435                 :             :                                  * and it is running on another CPU.
     436                 :             :                                  */
     437                 :             : 
     438                 :             :                                 thread->base.prio = prio;
     439                 :             : 
     440                 :             :                                 struct _cpu *cpu;
     441                 :             : 
     442                 :             :                                 cpu = z_smp_thread_active_elsewhere(thread);
     443                 :             :                                 if ((cpu != NULL) && (old_prio < prio)) {
     444                 :           0 :                                         flag_ipi(IPI_CPU_MASK(cpu->id));
     445                 :             :                                 }
     446                 :             :                         }
     447                 :             : 
     448                 :           0 :                         update_cache(1);
     449         [ #  # ]:           0 :                 } else if (z_is_thread_pending(thread)) {
     450                 :             :                         /* Thread is pending, remove it from the waitq
     451                 :             :                          * and reinsert it with the new priority to avoid
     452                 :             :                          * violating waitq ordering and rb assumptions.
     453                 :             :                          */
     454                 :           0 :                         _wait_q_t *wait_q = pended_on_thread(thread);
     455                 :             : 
     456                 :           0 :                         _priq_wait_remove(&wait_q->waitq, thread);
     457                 :           0 :                         thread->base.prio = prio;
     458                 :           0 :                         _priq_wait_add(&wait_q->waitq, thread);
     459                 :             :                 } else {
     460                 :           0 :                         thread->base.prio = prio;
     461                 :             :                 }
     462                 :             :         }
     463                 :             : 
     464                 :           0 :         SYS_PORT_TRACING_OBJ_FUNC(k_thread, sched_priority_set, thread, prio);
     465                 :             : 
     466                 :           0 :         return need_sched;
     467                 :             : }
     468                 :             : 
     469                 :             : void z_reschedule(struct k_spinlock *lock, k_spinlock_key_t key) ALIAS_OF(reschedule);
     470                 :             : 
     471                 :             : void z_reschedule_locked(k_spinlock_key_t key) ALIAS_OF(reschedule_locked);
     472                 :             : 
     473                 :          50 : void z_reschedule_irqlock(uint32_t key)
     474                 :             : {
     475   [ +  -  -  + ]:          50 :         if (resched(key) && need_swap()) {
     476                 :           0 :                 z_swap_irqlock(key);
     477                 :             :         } else {
     478                 :          50 :                 signal_pending_ipi();
     479                 :          50 :                 irq_unlock(key);
     480                 :             :         }
     481                 :          50 : }
     482                 :             : 
     483                 :             : #if defined(CONFIG_USE_SWITCH) && !defined(CONFIG_SMP)
     484                 :             : void *z_get_next_switch_handle(void *interrupted)
     485                 :             : {
     486                 :             :         z_check_stack_sentinel();
     487                 :             : 
     488                 :             :         struct k_thread *next = _kernel.ready_q.cache;
     489                 :             : 
     490                 :             :         z_sched_usage_switch(next);
     491                 :             :         _current->switch_handle = interrupted;
     492                 :             :         z_sched_set_current(next);
     493                 :             :         return next->switch_handle;
     494                 :             : }
     495                 :             : #endif /* CONFIG_USE_SWITCH && !CONFIG_SMP */
     496                 :             : 
     497                 :           0 : int z_unpend_all(_wait_q_t *wait_q)
     498                 :             : {
     499                 :           0 :         int need_sched = 0;
     500                 :           0 :         struct k_thread *thread;
     501                 :             : 
     502         [ #  # ]:           0 :         Z_SCHED_SPINLOCK {
     503                 :           0 :                 for (thread = z_waitq_head_locked(wait_q);
     504         [ #  # ]:           0 :                      thread != NULL;
     505                 :           0 :                      thread = z_waitq_head_locked(wait_q)) {
     506                 :           0 :                         unpend_thread_no_timeout(thread);
     507                 :             :                         /* Abort the timeout and ready the thread unconditionally. If
     508                 :             :                          * the timeout handler is in flight on another CPU, the abort
     509                 :             :                          * flags it superseded and z_thread_timeout() bails when it
     510                 :             :                          * runs -- so it will NOT ready the thread, we must.
     511                 :             :                          */
     512                 :           0 :                         (void)z_try_abort_thread_timeout(thread);
     513                 :           0 :                         ready_thread(thread);
     514                 :           0 :                         need_sched = 1;
     515                 :             :                 }
     516                 :             :         }
     517                 :             : 
     518                 :           0 :         return need_sched;
     519                 :             : }
     520                 :             : 
     521                 :          54 : static inline void unpend_all(_wait_q_t *wait_q)
     522                 :             : {
     523                 :          54 :         struct k_thread *thread;
     524                 :             : 
     525                 :          54 :         for (thread = z_waitq_head_locked(wait_q);
     526         [ +  + ]:          60 :              thread != NULL;
     527                 :           6 :              thread = z_waitq_head_locked(wait_q)) {
     528                 :           6 :                 unpend_thread_no_timeout(thread);
     529                 :           6 :                 arch_thread_return_value_set(thread, 0);
     530                 :             :                 /* See z_unpend_all(): the in-flight handler bails on the
     531                 :             :                  * superseded mark, so we ready the thread unconditionally.
     532                 :             :                  */
     533                 :           6 :                 (void)z_try_abort_thread_timeout(thread);
     534                 :           6 :                 ready_thread(thread);
     535                 :             :         }
     536                 :          54 : }
     537                 :             : 
     538                 :             : void z_sched_unpend_all_locked(_wait_q_t *wait_q) ALIAS_OF(unpend_all);
     539                 :             : 
     540                 :             : #ifdef CONFIG_THREAD_ABORT_HOOK
     541                 :             : extern void thread_abort_hook(struct k_thread *thread);
     542                 :             : #endif /* CONFIG_THREAD_ABORT_HOOK */
     543                 :             : 
     544                 :             : /**
     545                 :             :  * @brief Dequeues the specified thread
     546                 :             :  *
     547                 :             :  * Dequeues the specified thread and move it into the specified new state.
     548                 :             :  *
     549                 :             :  * @param thread Identify the thread to halt
     550                 :             :  * @param new_state New thread state (_THREAD_DEAD or _THREAD_SUSPENDED)
     551                 :             :  * @param key Pointer to the scheduler spinlock key held by the caller
     552                 :             :  */
     553                 :          54 : static void halt_thread(struct k_thread *thread, uint8_t new_state, k_spinlock_key_t *key)
     554                 :             : {
     555                 :          54 :         bool dummify = false;
     556                 :             : 
     557                 :             :         /* We hold the lock, and the thread is known not to be running
     558                 :             :          * anywhere.
     559                 :             :          */
     560         [ +  - ]:          54 :         if ((thread->base.thread_state & new_state) == 0U) {
     561                 :          54 :                 thread->base.thread_state |= new_state;
     562         [ +  - ]:          54 :                 if (z_is_thread_queued(thread)) {
     563                 :             :                         /* Clear idle CPU coverage before removing the thread from the run queue. */
     564                 :          54 :                         ipi_idle_thread_unreserve(thread);
     565                 :          54 :                         dequeue_thread(thread);
     566                 :             :                 }
     567                 :             : 
     568         [ +  - ]:          54 :                 if (new_state == _THREAD_DEAD) {
     569         [ -  + ]:          54 :                         if (thread->base.pended_on != NULL) {
     570                 :           0 :                                 unpend_thread_no_timeout(thread);
     571                 :             :                         }
     572                 :             :                         /* Wait for any in-flight handler to complete before
     573                 :             :                          * we proceed: the caller may free this thread's
     574                 :             :                          * storage (dynamic threads), and an in-flight handler
     575                 :             :                          * would UAF when it eventually runs z_thread_timeout()
     576                 :             :                          * and dereferences thread->base. _THREAD_DEAD is
     577                 :             :                          * already set, so once the handler runs it bails via
     578                 :             :                          * z_sched_wake_thread_locked()'s killed check.
     579                 :             :                          *
     580                 :             :                          * NULL key is the next_up() self-halt path on a
     581                 :             :                          * running _current that has no linked timeout; there
     582                 :             :                          * is nothing to do here. That path is always reached
     583                 :             :                          * via z_thread_halt()'s IF branch, which spins on
     584                 :             :                          * z_try_abort_thread_timeout(thread) outside any lock
     585                 :             :                          * after the halt-queue wait completes, closing any
     586                 :             :                          * remaining in-flight window before the caller of
     587                 :             :                          * z_thread_halt() returns.
     588                 :             :                          */
     589   [ +  -  -  + ]:         108 :                         if ((key != NULL) &&
     590                 :          54 :                             (z_try_abort_thread_timeout(thread) == -EAGAIN)) {
     591                 :             :                                 /* The wait drops the lock with the thread
     592                 :             :                                  * already dead (only possible on SMP). Flag
     593                 :             :                                  * the halt as still in progress, so
     594                 :             :                                  * k_thread_join() waits on join_queue instead
     595                 :             :                                  * of returning early or spinning on the lock
     596                 :             :                                  * (clear_halting() below drops the flag), and
     597                 :             :                                  * keep interrupts masked: a dead _current that
     598                 :             :                                  * an interrupt switches away never comes back
     599                 :             :                                  * to finish the halt.
     600                 :             :                                  */
     601                 :           0 :                                 thread->base.thread_state |= _THREAD_ABORTING;
     602                 :           0 :                                 do {
     603                 :           0 :                                         z_sched_spinlock_release();
     604                 :           0 :                                         (void)z_sched_spinlock_lock();
     605         [ #  # ]:           0 :                                 } while (z_try_abort_thread_timeout(thread) == -EAGAIN);
     606                 :             :                         }
     607                 :          54 :                         unpend_all(&thread->join_queue);
     608                 :             : 
     609                 :             :                         /* Edge case: aborting _current from within an
     610                 :             :                          * ISR that preempted it requires clearing the
     611                 :             :                          * _current pointer so the upcoming context
     612                 :             :                          * switch doesn't clobber the now-freed
     613                 :             :                          * memory
     614                 :             :                          */
     615                 :          54 :                         if (thread == _current && arch_is_in_isr()) {
     616                 :             :                                 dummify = true;
     617                 :             :                         }
     618                 :             :                 }
     619                 :          54 :                 z_smp_halt_queue_wake(thread);
     620                 :          54 :                 update_cache(1);
     621                 :             : 
     622         [ +  - ]:          54 :                 if (new_state == _THREAD_SUSPENDED) {
     623                 :             :                         z_smp_clear_halting(thread);
     624                 :             :                         return;
     625                 :             :                 }
     626                 :             : 
     627                 :          54 :                 arch_coprocessors_disable(thread);
     628                 :             : 
     629                 :          54 :                 SYS_PORT_TRACING_FUNC(k_thread, sched_abort, thread);
     630                 :             : 
     631                 :          54 :                 z_thread_monitor_exit(thread);
     632                 :             : #ifdef CONFIG_THREAD_ABORT_HOOK
     633                 :             :                 thread_abort_hook(thread);
     634                 :             : #endif /* CONFIG_THREAD_ABORT_HOOK */
     635                 :             : 
     636                 :             : #ifdef CONFIG_OBJ_CORE_THREAD
     637                 :             : #ifdef CONFIG_OBJ_CORE_STATS_THREAD
     638                 :             :                 k_obj_core_stats_deregister(K_OBJ_CORE(thread));
     639                 :             : #endif /* CONFIG_OBJ_CORE_STATS_THREAD */
     640                 :             :                 k_obj_core_unlink(K_OBJ_CORE(thread));
     641                 :             : #endif /* CONFIG_OBJ_CORE_THREAD */
     642                 :             : 
     643                 :             : #ifdef CONFIG_USERSPACE
     644                 :             :                 z_mem_domain_exit_thread(thread);
     645                 :             :                 k_thread_perms_all_clear(thread);
     646                 :             :                 k_object_uninit(thread->stack_obj);
     647                 :             :                 k_object_uninit(thread);
     648                 :             : #endif /* CONFIG_USERSPACE */
     649                 :             : 
     650                 :             : #ifdef CONFIG_THREAD_ABORT_NEED_CLEANUP
     651                 :             :                 k_thread_abort_cleanup(thread);
     652                 :             : #endif /* CONFIG_THREAD_ABORT_NEED_CLEANUP */
     653                 :             : 
     654                 :             :                 /* Do this "set _current to dummy" step last so that
     655                 :             :                  * subsystems above can rely on _current being
     656                 :             :                  * unchanged.  Disabled for posix as that arch
     657                 :             :                  * continues to use the _current pointer in its swap
     658                 :             :                  * code.  Note that we must leave a non-null switch
     659                 :             :                  * handle for any threads spinning in join() (this can
     660                 :             :                  * never be used, as our thread is flagged dead, but
     661                 :             :                  * it must not be NULL otherwise join can deadlock).
     662                 :             :                  * Use 1 as a clearly invalid but non-NULL value.
     663                 :             :                  */
     664                 :          54 :                 if (dummify && !IS_ENABLED(CONFIG_ARCH_POSIX)) {
     665                 :             : #ifdef CONFIG_USE_SWITCH
     666                 :             :                         _current->switch_handle = (void *)1;
     667                 :             : #endif
     668                 :             : #ifdef CONFIG_SPIN_VALIDATE
     669                 :             :                         /* On arches where exceptions run as ISRs (e.g. Xtensa),
     670                 :             :                          * the dying thread's lock tracking is never cleared via the
     671                 :             :                          * normal abort path.  Reset it here before _thread_dummy
     672                 :             :                          * takes over.  Sentinel-gated so genuine bugs still assert.
     673                 :             :                          */
     674                 :             :                         if (thread->base.swap_data ==
     675                 :             :                             (void *)&z_spinlock_abort_sentinel) {
     676                 :             :                                 z_spin_validate_reset(true);
     677                 :             :                         }
     678                 :             : #endif
     679                 :             :                         z_dummy_thread_init(&_thread_dummy);
     680                 :             : 
     681                 :             :                 }
     682                 :             : 
     683                 :             :                 /* Finally update the halting thread state, on which
     684                 :             :                  * other CPUs might be spinning (see
     685                 :             :                  * thread_halt_spin()).
     686                 :             :                  */
     687                 :          54 :                 z_smp_clear_halting(thread);
     688                 :             :         }
     689                 :             : }
     690                 :             : 
     691                 :             : void z_sched_halt_thread_locked(struct k_thread *thread, uint8_t new_state, k_spinlock_key_t *key)
     692                 :             :         ALIAS_OF(halt_thread);
     693                 :             : 
     694                 :           0 : void z_thread_suspend_current(struct k_thread *thread)
     695                 :             : {
     696                 :           0 :         k_spinlock_key_t key = z_sched_spinlock_lock();
     697                 :             : 
     698                 :           0 :         z_mark_thread_as_suspended(thread);
     699                 :           0 :         z_metairq_preempted_clear(thread);
     700                 :           0 :         dequeue_thread(thread);
     701                 :           0 :         update_cache(1);
     702                 :           0 :         z_swap_locked(key);
     703                 :           0 : }
        

Generated by: LCOV version 2.0-1