Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kernel/sched: fix logging after stack overflow #6622

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions os/kernel/sched/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ CSRCS += sched_lock.c sched_unlock.c sched_lockcount.c sched_self.c
CSRCS += sched_getaffinity.c sched_setaffinity.c
CSRCS += sched_getcpu.c

ifeq ($(CONFIG_SW_STACK_OVERFLOW_DETECTION),y)
CSRCS += sched_checkstackoverflow.c
endif

ifeq ($(CONFIG_PRIORITY_INHERITANCE),y)
CSRCS += sched_reprioritize.c
endif
Expand Down
4 changes: 4 additions & 0 deletions os/kernel/sched/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ void sched_addblocked(FAR struct tcb_s *btcb, tstate_t task_state);
void sched_removeblocked(FAR struct tcb_s *btcb);
int sched_setpriority(FAR struct tcb_s *tcb, int sched_priority);

#ifdef CONFIG_SW_STACK_OVERFLOW_DETECTION
void sched_checkstackoverflow(FAR struct tcb_s *rtcb);
#endif

#ifdef CONFIG_PRIORITY_INHERITANCE
int sched_reprioritize(FAR struct tcb_s *tcb, int sched_priority);
#else
Expand Down
18 changes: 2 additions & 16 deletions os/kernel/sched/sched_addreadytorun.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,7 @@ bool sched_addreadytorun(FAR struct tcb_s *btcb)
bool ret;

#ifdef CONFIG_SW_STACK_OVERFLOW_DETECTION
if (*(uint32_t *)(rtcb->stack_base_ptr) != STACK_COLOR) {
dbg_noarg("############### STACK OVERFLOW at pid %d ", rtcb->pid);
#if CONFIG_TASK_NAME_SIZE > 0
dbg_noarg("(%s) ", rtcb->name);
#endif
dbg_noarg("###################\n");
PANIC();
}
sched_checkstackoverflow(rtcb);
#endif
/* Check if pre-emption is disabled for the current running task and if
* the new ready-to-run task would cause the current running task to be
Expand Down Expand Up @@ -182,14 +175,7 @@ bool sched_addreadytorun(FAR struct tcb_s *btcb)
int cpu;

#ifdef CONFIG_SW_STACK_OVERFLOW_DETECTION
if (*(uint32_t *)(rtcb->stack_base_ptr) != STACK_COLOR) {
dbg_noarg("############### STACK OVERFLOW at pid %d ", rtcb->pid);
#if CONFIG_TASK_NAME_SIZE > 0
dbg_noarg("(%s) ", rtcb->name);
#endif
dbg_noarg("###################\n");
PANIC();
}
sched_checkstackoverflow(rtcb);
#endif
/* Check if the blocked TCB is locked to this CPU */

Expand Down
83 changes: 83 additions & 0 deletions os/kernel/sched/sched_checkstackoverflow.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/****************************************************************************
*
* Copyright 2025 Samsung Electronics All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <tinyara/config.h>
#include <tinyara/arch.h>
#include <tinyara/sched.h>
#include <tinyara/irq.h>

#include <assert.h>
#include <debug.h>
#include <stdint.h>

#include "sched/sched.h"

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

/****************************************************************************
* Private Type Declarations
****************************************************************************/

/****************************************************************************
* Global Variables
****************************************************************************/

/****************************************************************************
* Private Variables
****************************************************************************/

/****************************************************************************
* Private Function Prototypes
****************************************************************************/

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: sched_checkstackoverflow
*
* Description:
* This function checks stack overflow condition for a thread.
*
* Inputs:
* rtcb - Points to the TCB that is ready-to-run
*
****************************************************************************/

void sched_checkstackoverflow(FAR struct tcb_s *rtcb)
{
irqstate_t flags = enter_critical_section();

if (*(uint32_t *)(rtcb->stack_base_ptr) != STACK_COLOR) {
lldbg_noarg("\n############### STACK OVERFLOW at pid %d ", rtcb->pid);
sunghan-chang marked this conversation as resolved.
Show resolved Hide resolved
#if CONFIG_TASK_NAME_SIZE > 0
lldbg_noarg("(%s) ", rtcb->name);
#endif
lldbg_noarg("###################\n");
PANIC();
}

leave_critical_section(flags);
}
20 changes: 4 additions & 16 deletions os/kernel/sched/sched_removereadytorun.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,9 @@ bool sched_removereadytorun(FAR struct tcb_s *rtcb)
bool ret = false;

#ifdef CONFIG_SW_STACK_OVERFLOW_DETECTION
if (*(uint32_t *)(rtcb->stack_base_ptr) != STACK_COLOR) {
dbg_noarg("############### STACK OVERFLOW at pid %d ", rtcb->pid);
#if CONFIG_TASK_NAME_SIZE > 0
dbg_noarg("(%s) ", rtcb->name);
#endif
dbg_noarg("###################\n");
PANIC();
}
sched_checkstackoverflow(rtcb);
#endif

/* Check if the TCB to be removed is at the head of the ready to run list.
* In this case, we are removing the currently active task.
*/
Expand Down Expand Up @@ -157,15 +151,9 @@ bool sched_removereadytorun(FAR struct tcb_s *rtcb)
int cpu;

#ifdef CONFIG_SW_STACK_OVERFLOW_DETECTION
if (*(uint32_t *)(rtcb->stack_base_ptr) != STACK_COLOR) {
dbg_noarg("############### STACK OVERFLOW at pid %d ", rtcb->pid);
#if CONFIG_TASK_NAME_SIZE > 0
dbg_noarg("(%s) ", rtcb->name);
#endif
dbg_noarg("###################\n");
PANIC();
}
sched_checkstackoverflow(rtcb);
#endif

/* Which CPU (if any) is the task running on? Which task list holds
* the TCB
*/
Expand Down