Skip to content
Draft
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
6 changes: 6 additions & 0 deletions include/nuttx/mm/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ struct mallinfo_task kmm_mallinfo_task(pid_t pid);
# endif
#endif

/* Functions contained in mm_check.c ****************************************/

#ifdef CONFIG_MM_HEALTH_CHECK
void mm_check_init(void);
#endif

/* Functions contained in mm_memdump.c **************************************/

void mm_memdump(FAR struct mm_heap_s *heap, pid_t pid);
Expand Down
31 changes: 31 additions & 0 deletions mm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,34 @@ config MM_PANIC_ON_FAILURE
depends on DEBUG_MM

source "mm/iob/Kconfig"

config MM_HEALTH_CHECK
bool "Memory health checks"
default n
---help---
If enabled, a worker will be used to perform
various health checks on the system memories.

The checks include:
* Check all stacks for overflow.
* The the heap for any corruption.

config MM_CHECK_PERIOD
int "Health check period"
default 1
depends on MM_HEALTH_CHECK
---help---
The time period that the health checks will
be executed. In seconds.

config MM_STACK_USAGE_SAFE_PERCENT
int "Safe stack usage"
default 90
depends on MM_HEALTH_CHECK
---help---
The stack usage that is considered safe. In
percent.

If the stack usage goes beyond this percentage,
the memory checks will assume that an overflow
has occured.
1 change: 1 addition & 0 deletions mm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ include shm/Make.defs
include iob/Make.defs
include circbuf/Make.defs
include kasan/Make.defs
include mm_check/Make.defs

BINDIR ?= bin

Expand Down
30 changes: 30 additions & 0 deletions mm/mm_check/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
############################################################################
# mm/mm_check/Make.defs
#
# 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.
#
############################################################################

# Memory health checks.

ifeq ($(CONFIG_MM_HEALTH_CHECK),y)
CSRCS += mm_check.c

# Add the health checks to the build.

DEPPATH += --dep-path mm_check
VPATH += :mm_check
endif
94 changes: 94 additions & 0 deletions mm/mm_check/mm_check.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/****************************************************************************
* mm/mm_check/mm_check.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/mm/mm.h>
#include <nuttx/sched.h>
#include <nuttx/irq.h>
#include <nuttx/wqueue.h>
#include <nuttx/arch.h>
#include <nuttx/compiler.h>
#include <nuttx/config.h>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this a first include


#include <time.h>
#include <assert.h>
#include <sys/types.h>

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

#ifndef CONFIG_SCHED_LPWORK
#error "Low priority work queue is required for the memory health checks."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#error "Low priority work queue is required for the memory health checks."
# error "Low priority work queue is required for the memory health checks."

#endif

/****************************************************************************
* Private Types
****************************************************************************/

static struct work_s work_q;

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

static void mm_check_worker(FAR void * arg)
{
UNUSED(arg);

int i;
FAR struct tcb_s *tcb;
irqstate_t flags;
extern FAR struct tcb_s **g_pidhash;
extern volatile int g_npidhash;

for (i = 0; i < g_npidhash; i++)
{
flags = enter_critical_section();

tcb = g_pidhash[i];

if (tcb && ((up_check_tcbstack(tcb) * 100 / tcb->adj_stack_size)
> CONFIG_MM_STACK_USAGE_SAFE_PERCENT))
{
PANIC();
}

leave_critical_section(flags);
}

kmm_checkcorruption();

work_queue(LPWORK, &work_q, mm_check_worker, NULL,
(CONFIG_MM_CHECK_PERIOD * CLOCKS_PER_SEC));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(CONFIG_MM_CHECK_PERIOD * CLOCKS_PER_SEC));
CONFIG_MM_CHECK_PERIOD * CLOCKS_PER_SEC);

}

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

void mm_check_init()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void mm_check_init()
void mm_check_init(void)

{
work_queue(LPWORK, &work_q, mm_check_worker, NULL,
(CONFIG_MM_CHECK_PERIOD * CLOCKS_PER_SEC));
}
6 changes: 6 additions & 0 deletions sched/init/nx_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,12 @@ void nx_start(void)
iob_initialize();
#endif

#ifdef CONFIG_MM_HEALTH_CHECK
/* Initialize the memory health checks. */

mm_check_init();
#endif

/* Initialize the logic that determine unique process IDs. */

g_npidhash = 4;
Expand Down