Compare commits

...

3 commits

Author SHA1 Message Date
hujun5
89c06eeca0
Merge 3cff6dc97a into aa0aecbd80 2025-01-12 01:36:28 +08:00
wangmingrong1
aa0aecbd80 mempool: addbacktrace should be before kasan_unpoison
If thread 1 is executing kasan_unpoison but a scheduling occurs and the block is trampled upon, the displayed backtracking may still be from the previously allocated backtracking

Signed-off-by: wangmingrong1 <wangmingrong1@xiaomi.com>
2025-01-12 01:29:14 +08:00
hujun5
3cff6dc97a pthread: remove enter_critical_section in pthread_mutex
reason:
We would like to replace the critical section with a small lock.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-10 21:28:31 +08:00
5 changed files with 65 additions and 100 deletions

View file

@ -397,16 +397,17 @@ retry:
pool->nalloc++;
spin_unlock_irqrestore(&pool->lock, flags);
blk = kasan_unpoison(blk, pool->blocksize);
#ifdef CONFIG_MM_FILL_ALLOCATIONS
memset(blk, MM_ALLOC_MAGIC, pool->blocksize);
#endif
#if CONFIG_MM_BACKTRACE >= 0
mempool_add_backtrace(pool, (FAR struct mempool_backtrace_s *)
((FAR char *)blk + pool->blocksize));
#endif
blk = kasan_unpoison(blk, pool->blocksize);
#ifdef CONFIG_MM_FILL_ALLOCATIONS
memset(blk, MM_ALLOC_MAGIC, pool->blocksize);
#endif
return blk;
}

View file

@ -46,8 +46,7 @@ if(NOT CONFIG_DISABLE_PTHREAD)
pthread_setschedprio.c)
if(NOT CONFIG_PTHREAD_MUTEX_UNSAFE)
list(APPEND SRCS pthread_mutex.c pthread_mutexconsistent.c
pthread_mutexinconsistent.c)
list(APPEND SRCS pthread_mutex.c pthread_mutexconsistent.c)
endif()
if(CONFIG_SMP)

View file

@ -32,7 +32,7 @@ CSRCS += pthread_completejoin.c pthread_findjoininfo.c
CSRCS += pthread_release.c pthread_setschedprio.c
ifneq ($(CONFIG_PTHREAD_MUTEX_UNSAFE),y)
CSRCS += pthread_mutex.c pthread_mutexconsistent.c pthread_mutexinconsistent.c
CSRCS += pthread_mutex.c pthread_mutexconsistent.c
endif
ifeq ($(CONFIG_SMP),y)

View file

@ -38,6 +38,12 @@
#include "sched/sched.h"
#include "pthread/pthread.h"
/****************************************************************************
* Private Data
****************************************************************************/
static spinlock_t g_mutex_lock = SP_UNLOCKED;
/****************************************************************************
* Private Functions
****************************************************************************/
@ -65,10 +71,10 @@ static void pthread_mutex_add(FAR struct pthread_mutex_s *mutex)
/* Add the mutex to the list of mutexes held by this pthread */
flags = enter_critical_section();
flags = spin_lock_irqsave(&g_mutex_lock);
mutex->flink = rtcb->mhead;
rtcb->mhead = mutex;
leave_critical_section(flags);
spin_unlock_irqrestore(&g_mutex_lock, flags);
}
/****************************************************************************
@ -92,7 +98,7 @@ static void pthread_mutex_remove(FAR struct pthread_mutex_s *mutex)
FAR struct pthread_mutex_s *prev;
irqstate_t flags;
flags = enter_critical_section();
flags = spin_lock_irqsave(&g_mutex_lock);
/* Remove the mutex from the list of mutexes held by this task */
@ -118,7 +124,7 @@ static void pthread_mutex_remove(FAR struct pthread_mutex_s *mutex)
}
mutex->flink = NULL;
leave_critical_section(flags);
spin_unlock_irqrestore(&g_mutex_lock, flags);
}
/****************************************************************************
@ -346,3 +352,51 @@ int pthread_mutex_restorelock(FAR struct pthread_mutex_s *mutex,
return ret;
}
/****************************************************************************
* Name: pthread_mutex_inconsistent
*
* Description:
* This function is called when a pthread is terminated via either
* pthread_exit() or pthread_cancel(). It will check for any mutexes
* held by exitting thread. It will mark them as inconsistent and
* then wake up the highest priority waiter for the mutex. That
* instance of pthread_mutex_lock() will then return EOWNERDEAD.
*
* Input Parameters:
* tcb -- a reference to the TCB of the exitting pthread.
*
* Returned Value:
* None.
*
****************************************************************************/
void pthread_mutex_inconsistent(FAR struct tcb_s *tcb)
{
FAR struct pthread_mutex_s *mutex;
irqstate_t flags;
DEBUGASSERT(tcb != NULL);
flags = spin_lock_irqsave(&g_mutex_lock);
sched_lock();
/* Remove and process each mutex held by this task */
while (tcb->mhead != NULL)
{
/* Remove the mutex from the TCB list */
mutex = tcb->mhead;
tcb->mhead = mutex->flink;
mutex->flink = NULL;
/* Mark the mutex as INCONSISTENT and wake up any waiting thread */
mutex->flags |= _PTHREAD_MFLAGS_INCONSISTENT;
mutex_unlock(&mutex->mutex);
}
spin_unlock_irqrestore(&g_mutex_lock, flags);
sched_unlock();
}

View file

@ -1,89 +0,0 @@
/****************************************************************************
* sched/pthread/pthread_mutexinconsistent.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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/config.h>
#include <pthread.h>
#include <sched.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/sched.h>
#include <nuttx/semaphore.h>
#include "pthread/pthread.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: pthread_mutex_inconsistent
*
* Description:
* This function is called when a pthread is terminated via either
* pthread_exit() or pthread_cancel(). It will check for any mutexes
* held by exitting thread. It will mark them as inconsistent and
* then wake up the highest priority waiter for the mutex. That
* instance of pthread_mutex_lock() will then return EOWNERDEAD.
*
* Input Parameters:
* tcb -- a reference to the TCB of the exitting pthread.
*
* Returned Value:
* None.
*
****************************************************************************/
void pthread_mutex_inconsistent(FAR struct tcb_s *tcb)
{
FAR struct pthread_mutex_s *mutex;
irqstate_t flags;
DEBUGASSERT(tcb != NULL);
sched_lock();
/* Remove and process each mutex held by this task */
while (tcb->mhead != NULL)
{
/* Remove the mutex from the TCB list */
flags = enter_critical_section();
mutex = tcb->mhead;
tcb->mhead = mutex->flink;
mutex->flink = NULL;
leave_critical_section(flags);
/* Mark the mutex as INCONSISTENT and wake up any waiting thread */
mutex->flags |= _PTHREAD_MFLAGS_INCONSISTENT;
mutex_unlock(&mutex->mutex);
}
sched_unlock();
}