From 419bc2fd0455989f52bcbee9b17e294a8c6b74cc Mon Sep 17 00:00:00 2001 From: ligd Date: Fri, 18 Feb 2022 13:26:08 +0800 Subject: [PATCH] mm: handle take mm sem in IRQ This is a fix of: 0169a51220a68d8d3bed0c20b6d606f43497a9e9 This is caused by wrong memory sem operation in IDLE. Fix: Obey the original design, don't check the IDLE in mm_takesemaphore() Signed-off-by: ligd --- mm/mm_heap/mm_free.c | 6 +++--- mm/mm_heap/mm_sem.c | 36 +++++++++++++----------------------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/mm/mm_heap/mm_free.c b/mm/mm_heap/mm_free.c index c788649f4f..894719b056 100644 --- a/mm/mm_heap/mm_free.c +++ b/mm/mm_heap/mm_free.c @@ -90,9 +90,9 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem) { kasan_unpoison(mem, mm_malloc_size(mem)); - /* We are in IDLE task & can't get sem, or meet -ESRCH return, - * which means we are in situations during context switching(See - * mm_takesemaphore() & getpid()). Then add to the delay list. + /* Meet -ESRCH return, which means we are in situations + * during context switching(See mm_takesemaphore() & getpid()). + * Then add to the delay list. */ mm_add_delaylist(heap, mem); diff --git a/mm/mm_heap/mm_sem.c b/mm/mm_heap/mm_sem.c index b26a0dec97..f73a5b40cc 100644 --- a/mm/mm_heap/mm_sem.c +++ b/mm/mm_heap/mm_sem.c @@ -80,9 +80,21 @@ bool mm_takesemaphore(FAR struct mm_heap_s *heap) if (up_interrupt_context()) { - /* Can't take semaphore in the interrupt handler */ +#if !defined(CONFIG_SMP) + int val; + + /* Check the semaphore value, if held by someone, then return false. + * Or, touch the heap internal data directly. + */ + + _SEM_GETVALUE(&heap->mm_semaphore, &val); + + return val > 0; +#else + /* Can't take semaphore in SMP interrupt handler */ return false; +#endif } else #endif @@ -101,28 +113,6 @@ bool mm_takesemaphore(FAR struct mm_heap_s *heap) { return false; } -#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__) - else if (sched_idletask()) - { - return false; - } - else if (up_interrupt_context()) - { -#ifdef CONFIG_SMP - return false; -#else - int val; - - /* Check the semaphore value, if held by someone, then return false. - * Else, we can take it, return true. - */ - - _SEM_GETVALUE(&heap->mm_semaphore, &val); - - return val > 0; -#endif - } -#endif else { int ret;