2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2014-09-22 18:53:50 +02:00
|
|
|
* mm/mm_heap/mm_sem.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* 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
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* 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.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 21:36:18 +01:00
|
|
|
#include <nuttx/config.h>
|
2012-07-15 01:31:12 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2020-05-04 12:55:11 +02:00
|
|
|
#include <debug.h>
|
2012-07-15 01:31:12 +02:00
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
#include <nuttx/semaphore.h>
|
2014-09-24 15:29:09 +02:00
|
|
|
#include <nuttx/mm/mm.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-03-02 09:03:00 +01:00
|
|
|
#include "mm_heap/mm.h"
|
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2009-12-15 02:39:03 +01:00
|
|
|
* Pre-processor Definitions
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2018-12-30 15:41:06 +01:00
|
|
|
/* This is a special value that indicates that there is no holder of the
|
|
|
|
* semaphore. The valid range of PIDs is 0-32767 and any value outside of
|
|
|
|
* that range could be used (except -ESRCH which is a special return value
|
|
|
|
* from getpid())
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define NO_HOLDER ((pid_t)-1)
|
|
|
|
|
2018-12-20 20:39:06 +01:00
|
|
|
/* Define MONITOR_MM_SEMAPHORE to enable semaphore state monitoring */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
#ifdef MONITOR_MM_SEMAPHORE
|
2016-06-16 20:33:32 +02:00
|
|
|
# define msemerr _err
|
|
|
|
# define msemwarn _warn
|
|
|
|
# define mseminfo _info
|
2007-02-18 00:21:28 +01:00
|
|
|
#else
|
2020-05-04 12:55:11 +02:00
|
|
|
# define msemerr _none
|
|
|
|
# define msemwarn _none
|
|
|
|
# define mseminfo _none
|
2007-02-18 00:21:28 +01:00
|
|
|
#endif
|
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2007-03-14 19:58:21 +01:00
|
|
|
* Name: mm_seminitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the MM mutex
|
|
|
|
*
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
void mm_seminitialize(FAR struct mm_heap_s *heap)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Initialize the MM semaphore to one (to support one-at-a-time access to
|
2016-02-13 01:03:08 +01:00
|
|
|
* private data sets).
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
_SEM_INIT(&heap->mm_semaphore, 0, 1);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
heap->mm_holder = NO_HOLDER;
|
|
|
|
heap->mm_counts_held = 0;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2007-03-14 19:58:21 +01:00
|
|
|
* Name: mm_trysemaphore
|
|
|
|
*
|
|
|
|
* Description:
|
2012-07-15 01:31:12 +02:00
|
|
|
* Try to take the MM mutex. This is called only from the OS in certain
|
|
|
|
* conditions when it is necessary to have exclusive access to the memory
|
|
|
|
* manager but it is impossible to wait on a semaphore (e.g., the idle
|
|
|
|
* process when it performs its background memory cleanup).
|
2007-03-14 19:58:21 +01:00
|
|
|
*
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
int mm_trysemaphore(FAR struct mm_heap_s *heap)
|
2007-03-14 19:58:21 +01:00
|
|
|
{
|
2018-12-30 15:41:06 +01:00
|
|
|
pid_t my_pid = getpid();
|
2017-10-05 15:59:06 +02:00
|
|
|
int ret;
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2018-12-30 15:41:06 +01:00
|
|
|
/* getpid() returns the task ID of the task at the head of the ready-to-
|
|
|
|
* run task list. mm_trysemaphore() may be called during context
|
|
|
|
* switches. There are certain situations during context switching when
|
|
|
|
* the OS data structures are in flux and where the current task (i.e.,
|
|
|
|
* the task at the head of the ready-to-run task list) is not actually
|
|
|
|
* running. Granting the semaphore access in that case is known to result
|
|
|
|
* in heap corruption as in the following failure scenario.
|
2018-12-20 17:19:52 +01:00
|
|
|
*
|
2018-12-27 16:32:47 +01:00
|
|
|
* ---------------------------- -------------------------------
|
|
|
|
* TASK A TASK B
|
|
|
|
* ---------------------------- -------------------------------
|
|
|
|
* Begins memory allocation.
|
|
|
|
* - Holder is set to TASK B
|
|
|
|
* <---- Task B preempted, Task A runs
|
|
|
|
* Task A exits
|
|
|
|
* - Current task set to Task B
|
|
|
|
* Free tcb and stack memory
|
|
|
|
* - Since holder is Task B,
|
|
|
|
* memory manager is re-
|
|
|
|
* entered, and
|
|
|
|
* - Heap is corrupted.
|
|
|
|
* ---------------------------- -------------------------------
|
2018-12-30 15:41:06 +01:00
|
|
|
*
|
|
|
|
* This is handled by getpid(): If the case where Task B is not actually
|
|
|
|
* running, then getpid() will return the special value -ESRCH. That will
|
|
|
|
* avoid taking the fatal 'if' logic and will fall through to use the
|
|
|
|
* 'else', albeit with a nonsensical PID value.
|
2018-12-20 17:19:52 +01:00
|
|
|
*/
|
|
|
|
|
2020-04-08 10:03:01 +02:00
|
|
|
if (my_pid < 0)
|
|
|
|
{
|
2020-04-13 04:46:27 +02:00
|
|
|
ret = my_pid;
|
|
|
|
goto errout;
|
2020-04-08 10:03:01 +02:00
|
|
|
}
|
|
|
|
|
2018-12-27 16:32:47 +01:00
|
|
|
/* Does the current task already hold the semaphore? Is the current
|
|
|
|
* task actually running?
|
|
|
|
*/
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
if (heap->mm_holder == my_pid)
|
2007-03-14 19:58:21 +01:00
|
|
|
{
|
2018-12-27 16:32:47 +01:00
|
|
|
/* Yes, just increment the number of references held by the current
|
|
|
|
* task.
|
|
|
|
*/
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
heap->mm_counts_held++;
|
2018-03-20 13:34:38 +01:00
|
|
|
ret = OK;
|
2007-03-14 19:58:21 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-20 17:19:52 +01:00
|
|
|
/* Try to take the semaphore */
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
ret = _SEM_TRYWAIT(&heap->mm_semaphore);
|
2017-10-05 15:59:06 +02:00
|
|
|
if (ret < 0)
|
2020-02-13 14:58:07 +01:00
|
|
|
{
|
2020-05-02 16:05:41 +02:00
|
|
|
ret = _SEM_ERRVAL(ret);
|
2020-02-13 14:58:07 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2018-12-27 16:32:47 +01:00
|
|
|
/* We have it. Claim the heap for the current task and return */
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
heap->mm_holder = my_pid;
|
|
|
|
heap->mm_counts_held = 1;
|
2018-03-20 13:34:38 +01:00
|
|
|
ret = OK;
|
2007-03-14 19:58:21 +01:00
|
|
|
}
|
2018-03-20 13:34:38 +01:00
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
2007-03-14 19:58:21 +01:00
|
|
|
}
|
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2007-03-14 19:58:21 +01:00
|
|
|
* Name: mm_takesemaphore
|
|
|
|
*
|
|
|
|
* Description:
|
2012-07-15 01:31:12 +02:00
|
|
|
* Take the MM mutex. This is the normal action before all memory
|
|
|
|
* management actions.
|
2007-03-14 19:58:21 +01:00
|
|
|
*
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
void mm_takesemaphore(FAR struct mm_heap_s *heap)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
pid_t my_pid = getpid();
|
|
|
|
|
2018-12-27 16:32:47 +01:00
|
|
|
/* Does the current task already hold the semaphore? */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
if (heap->mm_holder == my_pid)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2018-12-27 16:32:47 +01:00
|
|
|
/* Yes, just increment the number of references held by the current
|
|
|
|
* task.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
heap->mm_counts_held++;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
2016-06-12 17:46:23 +02:00
|
|
|
mseminfo("PID=%d taking\n", my_pid);
|
2017-10-04 23:22:27 +02:00
|
|
|
do
|
2012-07-15 01:31:12 +02:00
|
|
|
{
|
2021-07-03 16:16:25 +02:00
|
|
|
ret = _SEM_WAIT(&heap->mm_semaphore);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
|
|
|
/* The only case that an error should occur here is if the wait
|
|
|
|
* was awakened by a signal.
|
2012-07-15 01:31:12 +02:00
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-10-07 15:16:33 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-05-02 16:05:41 +02:00
|
|
|
ret = _SEM_ERRVAL(ret);
|
2018-02-20 19:24:53 +01:00
|
|
|
DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
|
2017-10-07 15:16:33 +02:00
|
|
|
}
|
2012-07-15 01:31:12 +02:00
|
|
|
}
|
2017-10-04 23:22:27 +02:00
|
|
|
while (ret == -EINTR);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
/* We have it (or some awful, unexpected error occurred). Claim
|
2018-12-27 16:32:47 +01:00
|
|
|
* the semaphore for the current task and return.
|
2017-10-04 23:22:27 +02:00
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
heap->mm_holder = my_pid;
|
|
|
|
heap->mm_counts_held = 1;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
mseminfo("Holder=%d count=%d\n", heap->mm_holder,
|
|
|
|
heap->mm_counts_held);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2007-03-14 19:58:21 +01:00
|
|
|
* Name: mm_givesemaphore
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release the MM mutex when it is not longer needed.
|
|
|
|
*
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
void mm_givesemaphore(FAR struct mm_heap_s *heap)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2018-12-27 16:32:47 +01:00
|
|
|
/* The current task should be holding at least one reference to the
|
|
|
|
* semaphore.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
DEBUGASSERT(heap->mm_holder == getpid());
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2018-12-27 16:32:47 +01:00
|
|
|
/* Does the current task hold multiple references to the semaphore */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
if (heap->mm_counts_held > 1)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
/* Yes, just release one count and return */
|
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
heap->mm_counts_held--;
|
|
|
|
mseminfo("Holder=%d count=%d\n", heap->mm_holder,
|
|
|
|
heap->mm_counts_held);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-27 16:32:47 +01:00
|
|
|
/* Nope, this is the last reference held by the current task. */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2019-06-03 17:02:55 +02:00
|
|
|
mseminfo("PID=%d giving\n", getpid());
|
2014-05-28 22:09:58 +02:00
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
heap->mm_holder = NO_HOLDER;
|
|
|
|
heap->mm_counts_held = 0;
|
|
|
|
DEBUGVERIFY(_SEM_POST(&heap->mm_semaphore));
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|