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

Sched:Automatically find deadlocks when assert #9583

Merged
merged 2 commits into from Jun 22, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions arch/Kconfig
Expand Up @@ -902,6 +902,12 @@ config ARCH_USBDUMP
---help---
Enable to do USB trace after assertions

config ARCH_DEADLOCKDUMP
bool "Dump dead lock thread"
default "n"
---help---
This option will dump the dead lock thread when assert happen..

config ENDIAN_BIG
bool "Big Endian Architecture"
default n
Expand Down
17 changes: 17 additions & 0 deletions include/nuttx/sched.h
Expand Up @@ -1555,6 +1555,23 @@ pid_t nxsched_getpid(void);

pid_t nxsched_getppid(void);

/****************************************************************************
* Name: nxsched_collect_deadlock
*
* Description:
* Check if there is a deadlock and get the thread pid of the deadlock.
*
* Input parameters:
* pid - The array to store the thread pid of the deadlock.
* count - The size of the pid array.
*
* Returned Value:
* The number of thread deadlocks.
*
****************************************************************************/

size_t nxsched_collect_deadlock(FAR pid_t *pid, size_t count);

#undef EXTERN
#if defined(__cplusplus)
}
Expand Down
4 changes: 4 additions & 0 deletions libs/libc/misc/lib_mutex.c
Expand Up @@ -89,7 +89,11 @@ int nxmutex_init(FAR mutex_t *mutex)
}

mutex->holder = NXMUTEX_NO_HOLDER;
#ifdef CONFIG_PRIORITY_INHERITANCE
_SEM_SETPROTOCOL(&mutex->sem, SEM_TYPE_MUTEX | SEM_PRIO_INHERIT);
#else
_SEM_SETPROTOCOL(&mutex->sem, SEM_TYPE_MUTEX);
#endif
return ret;
}

Expand Down
9 changes: 5 additions & 4 deletions libs/libc/semaphore/sem_setprotocol.c
Expand Up @@ -77,20 +77,21 @@ int nxsem_set_protocol(FAR sem_t *sem, int protocol)
{
DEBUGASSERT(sem != NULL);

switch (protocol)
switch (protocol & SEM_PRIO_MASK)
{
case SEM_PRIO_NONE:
return OK;
break;

case SEM_PRIO_INHERIT:
case SEM_PRIO_PROTECT:
return -ENOTSUP;

default:
break;
return -EINVAL;
}

return -EINVAL;
sem->flags = protocol;
return OK;
}

/****************************************************************************
Expand Down
4 changes: 4 additions & 0 deletions sched/misc/Make.defs
Expand Up @@ -20,6 +20,10 @@

CSRCS += assert.c panic_notifier.c reboot_notifier.c

ifeq ($(CONFIG_ARCH_DEADLOCKDUMP),y)
CSRCS += deadlock.c
endif

# Include init build support

DEPPATH += --dep-path misc
Expand Down
33 changes: 33 additions & 0 deletions sched/misc/assert.c
Expand Up @@ -50,6 +50,8 @@
* Pre-processor Definitions
****************************************************************************/

#define DEADLOCK_MAX 8

#ifndef CONFIG_BOARD_RESET_ON_ASSERT
# define CONFIG_BOARD_RESET_ON_ASSERT 0
#endif
Expand Down Expand Up @@ -511,6 +513,31 @@ static void dump_core(pid_t pid)
}
#endif

/****************************************************************************
* Name: dump_deadlock
****************************************************************************/

#ifdef CONFIG_ARCH_DEADLOCKDUMP
static void dump_deadlock(void)
{
pid_t deadlock[DEADLOCK_MAX];
size_t i = nxsched_collect_deadlock(deadlock, DEADLOCK_MAX);

if (i > 0)
{
_alert("Deadlock detected\n");
while (i-- > 0)
{
#ifdef CONFIG_SCHED_BACKTRACE
sched_dumpstack(deadlock[i]);
#else
_alert("deadlock pid: %d\n", deadlock[i])
#endif
}
}
}
#endif

/****************************************************************************
* Public Functions
****************************************************************************/
Expand Down Expand Up @@ -594,6 +621,12 @@ void _assert(FAR const char *filename, int linenum,
{
show_tasks();

#ifdef CONFIG_ARCH_DEADLOCKDUMP
/* Deadlock Dump */

dump_deadlock();
#endif

#ifdef CONFIG_ARCH_USBDUMP
/* Dump USB trace data */

Expand Down
115 changes: 115 additions & 0 deletions sched/misc/deadlock.c
@@ -0,0 +1,115 @@
/****************************************************************************
* sched/misc/deadlock.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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 <nuttx/mutex.h>
#include <nuttx/sched.h>

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

struct deadlock_info_s
{
FAR pid_t *pid;
size_t count;
size_t found;
};

/****************************************************************************
* Private Functions
****************************************************************************/

/****************************************************************************
* Name:find_circular_lock
****************************************************************************/

static void collect_deadlock(FAR struct tcb_s *tcb, FAR void *arg)
{
FAR struct deadlock_info_s *info = arg;
size_t index;

for (index = 0; index < info->found; index++)
{
if (info->pid[index] == tcb->pid)
{
return;
}
}

for (index = info->found; index < info->count; index++)
{
FAR sem_t *sem = tcb->waitobj;
pid_t next;
size_t i;

if (tcb->task_state != TSTATE_WAIT_SEM || sem == NULL ||
!(sem->flags & SEM_TYPE_MUTEX))
{
return;
}

next = ((FAR mutex_t *)sem)->holder;
for (i = info->found; i < index; i++)
{
if (info->pid[i] == next)
{
info->found = index;
return;
}
}

info->pid[index] = tcb->pid;
tcb = nxsched_get_tcb(next);
}
}

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

/****************************************************************************
* Name: nxsched_collect_deadlock
*
* Description:
* Check if there is a deadlock and get the thread pid of the deadlock.
*
* Input parameters:
* pid - The array to store the thread pid of the deadlock.
* count - The size of the pid array.
*
* Returned Value:
* The number of thread deadlocks.
*
****************************************************************************/

size_t nxsched_collect_deadlock(FAR pid_t *pid, size_t count)
{
struct deadlock_info_s info;

info.pid = pid;
info.count = count;
info.found = 0;
nxsched_foreach(collect_deadlock, &info);
return info.found;
}