Fix AcpiWalkNamespace race condition with table unload.

Added a reader/writer locking mechanism to allow multiple
concurrent namespace walks (readers), but a dynamic table
unload will have exclusive access to the namespace. This fixes
a problem where a table unload could delete the portion of
the namespace that is currently being examined by a walk.
Adds a new file, utlock.c that implements the reader/writer
lock mechanism. ACPICA BZ 749.
This commit is contained in:
Robert Moore 2009-03-06 08:54:39 -08:00
parent 9f849c6ae7
commit b1c943d6c2
13 changed files with 426 additions and 31 deletions

View File

@ -120,6 +120,7 @@ SRCS= aeexec.c aemain.c \
../../utilities/uteval.c \
../../utilities/utglobal.c \
../../utilities/utinit.c \
../../utilities/utlock.c \
../../utilities/utmath.c \
../../utilities/utmisc.c \
../../utilities/utmutex.c \

View File

@ -14,6 +14,7 @@ SRCS= aslcompilerparse.c aslcompilerlex.c aslanalyze.c aslcodegen.c \
../utilities/utdelete.c \
../utilities/utglobal.c \
../utilities/utinit.c \
../utilities/utlock.c \
../utilities/utobject.c \
../utilities/utmisc.c \
../utilities/utmath.c \

View File

@ -272,12 +272,23 @@ AcpiDsInitializeObjects (
/* Walk entire namespace from the supplied root */
Status = AcpiWalkNamespace (ACPI_TYPE_ANY, StartNode, ACPI_UINT32_MAX,
AcpiDsInitOneObject, &Info, NULL);
Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
if (ACPI_FAILURE (Status))
{
return_ACPI_STATUS (Status);
}
/*
* We don't use AcpiWalkNamespace since we do not want to acquire
* the namespace reader lock.
*/
Status = AcpiNsWalkNamespace (ACPI_TYPE_ANY, StartNode, ACPI_UINT32_MAX,
ACPI_NS_WALK_UNLOCK, AcpiDsInitOneObject, &Info, NULL);
if (ACPI_FAILURE (Status))
{
ACPI_EXCEPTION ((AE_INFO, Status, "During WalkNamespace"));
}
(void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
Status = AcpiGetTableByIndex (TableIndex, &Table);
if (ACPI_FAILURE (Status))

View File

@ -641,13 +641,15 @@ AcpiExUnloadTable (
}
}
/*
* Delete the entire namespace under this table Node
* (Offset contains the TableId)
*/
AcpiTbDeleteNamespaceByOwner (TableIndex);
(void) AcpiTbReleaseOwnerId (TableIndex);
/* Delete the portion of the namespace owned by this table */
Status = AcpiTbDeleteNamespaceByOwner (TableIndex);
if (ACPI_FAILURE (Status))
{
return_ACPI_STATUS (Status);
}
(void) AcpiTbReleaseOwnerId (TableIndex);
AcpiTbSetTableLoadedFlag (TableIndex, FALSE);
/* Table unloaded, remove a reference to the DdbHandle object */

View File

@ -592,22 +592,41 @@ AcpiWalkNamespace (
}
/*
* Lock the namespace around the walk.
* The namespace will be unlocked/locked around each call
* to the user function - since this function
* must be allowed to make Acpi calls itself.
* Need to acquire the namespace reader lock to prevent interference
* with any concurrent table unloads (which causes the deletion of
* namespace objects). We cannot allow the deletion of a namespace node
* while the user function is using it. The exception to this are the
* nodes created and deleted during control method execution -- these
* nodes are marked as temporary nodes and are ignored by the namespace
* walk. Thus, control methods can be executed while holding the
* namespace deletion lock (and the user function can execute control
* methods.)
*/
Status = AcpiUtAcquireReadLock (&AcpiGbl_NamespaceRwLock);
if (ACPI_FAILURE (Status))
{
return (Status);
}
/*
* Lock the namespace around the walk. The namespace will be
* unlocked/locked around each call to the user function - since the user
* function must be allowed to make ACPICA calls itself (for example, it
* will typically execute control methods during device enumeration.)
*/
Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
if (ACPI_FAILURE (Status))
{
return_ACPI_STATUS (Status);
goto UnlockAndExit;
}
Status = AcpiNsWalkNamespace (Type, StartObject, MaxDepth,
ACPI_NS_WALK_UNLOCK,
UserFunction, Context, ReturnValue);
ACPI_NS_WALK_UNLOCK, UserFunction, Context, ReturnValue);
(void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
UnlockAndExit:
(void) AcpiUtReleaseReadLock (&AcpiGbl_NamespaceRwLock);
return_ACPI_STATUS (Status);
}

View File

@ -552,32 +552,62 @@ AcpiTbTerminate (
*
* PARAMETERS: TableIndex - Table index
*
* RETURN: None
* RETURN: Status
*
* DESCRIPTION: Delete all namespace objects created when this table was loaded.
*
******************************************************************************/
void
ACPI_STATUS
AcpiTbDeleteNamespaceByOwner (
UINT32 TableIndex)
{
ACPI_OWNER_ID OwnerId;
ACPI_STATUS Status;
(void) AcpiUtAcquireMutex (ACPI_MTX_TABLES);
if (TableIndex < AcpiGbl_RootTableList.Count)
ACPI_FUNCTION_TRACE (TbDeleteNamespaceByOwner);
Status = AcpiUtAcquireMutex (ACPI_MTX_TABLES);
if (ACPI_FAILURE (Status))
{
OwnerId = AcpiGbl_RootTableList.Tables[TableIndex].OwnerId;
return_ACPI_STATUS (Status);
}
else
if (TableIndex >= AcpiGbl_RootTableList.Count)
{
/* The table index does not exist */
(void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
return;
return_ACPI_STATUS (AE_NOT_EXIST);
}
/* Get the owner ID for this table, used to delete namespace nodes */
OwnerId = AcpiGbl_RootTableList.Tables[TableIndex].OwnerId;
(void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
/*
* Need to acquire the namespace writer lock to prevent interference
* with any concurrent namespace walks. The interpreter must be
* released during the deletion since the acquisition of the deletion
* lock may block, and also since the execution of a namespace walk
* must be allowed to use the interpreter.
*/
AcpiUtReleaseMutex (ACPI_MTX_INTERPRETER);
Status = AcpiUtAcquireWriteLock (&AcpiGbl_NamespaceRwLock);
AcpiNsDeleteNamespaceByOwner (OwnerId);
if (ACPI_FAILURE (Status))
{
return_ACPI_STATUS (Status);
}
AcpiUtReleaseWriteLock (&AcpiGbl_NamespaceRwLock);
Status = AcpiUtAcquireMutex (ACPI_MTX_INTERPRETER);
return_ACPI_STATUS (Status);
}

View File

@ -0,0 +1,277 @@
/******************************************************************************
*
* Module Name: utlock - Reader/Writer lock interfaces
*
*****************************************************************************/
/******************************************************************************
*
* 1. Copyright Notice
*
* Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
* All rights reserved.
*
* 2. License
*
* 2.1. This is your license from Intel Corp. under its intellectual property
* rights. You may have additional license terms from the party that provided
* you this software, covering your right to use that party's intellectual
* property rights.
*
* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
* copy of the source code appearing in this file ("Covered Code") an
* irrevocable, perpetual, worldwide license under Intel's copyrights in the
* base code distributed originally by Intel ("Original Intel Code") to copy,
* make derivatives, distribute, use and display any portion of the Covered
* Code in any form, with the right to sublicense such rights; and
*
* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
* license (with the right to sublicense), under only those claims of Intel
* patents that are infringed by the Original Intel Code, to make, use, sell,
* offer to sell, and import the Covered Code and derivative works thereof
* solely to the minimum extent necessary to exercise the above copyright
* license, and in no event shall the patent license extend to any additions
* to or modifications of the Original Intel Code. No other license or right
* is granted directly or by implication, estoppel or otherwise;
*
* The above copyright and patent license is granted only if the following
* conditions are met:
*
* 3. Conditions
*
* 3.1. Redistribution of Source with Rights to Further Distribute Source.
* Redistribution of source code of any substantial portion of the Covered
* Code or modification with rights to further distribute source must include
* the above Copyright Notice, the above License, this list of Conditions,
* and the following Disclaimer and Export Compliance provision. In addition,
* Licensee must cause all Covered Code to which Licensee contributes to
* contain a file documenting the changes Licensee made to create that Covered
* Code and the date of any change. Licensee must include in that file the
* documentation of any changes made by any predecessor Licensee. Licensee
* must include a prominent statement that the modification is derived,
* directly or indirectly, from Original Intel Code.
*
* 3.2. Redistribution of Source with no Rights to Further Distribute Source.
* Redistribution of source code of any substantial portion of the Covered
* Code or modification without rights to further distribute source must
* include the following Disclaimer and Export Compliance provision in the
* documentation and/or other materials provided with distribution. In
* addition, Licensee may not authorize further sublicense of source of any
* portion of the Covered Code, and must include terms to the effect that the
* license from Licensee to its licensee is limited to the intellectual
* property embodied in the software Licensee provides to its licensee, and
* not to intellectual property embodied in modifications its licensee may
* make.
*
* 3.3. Redistribution of Executable. Redistribution in executable form of any
* substantial portion of the Covered Code or modification must reproduce the
* above Copyright Notice, and the following Disclaimer and Export Compliance
* provision in the documentation and/or other materials provided with the
* distribution.
*
* 3.4. Intel retains all right, title, and interest in and to the Original
* Intel Code.
*
* 3.5. Neither the name Intel nor any other trademark owned or controlled by
* Intel shall be used in advertising or otherwise to promote the sale, use or
* other dealings in products derived from or relating to the Covered Code
* without prior written authorization from Intel.
*
* 4. Disclaimer and Export Compliance
*
* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
* LIMITED REMEDY.
*
* 4.3. Licensee shall not export, either directly or indirectly, any of this
* software or system incorporating such software without first obtaining any
* required license or other approval from the U. S. Department of Commerce or
* any other agency or department of the United States Government. In the
* event Licensee exports any such software from the United States or
* re-exports any such software from a foreign destination, Licensee shall
* ensure that the distribution and export/re-export of the software is in
* compliance with all laws, regulations, orders, or other restrictions of the
* U.S. Export Administration Regulations. Licensee agrees that neither it nor
* any of its subsidiaries will export/re-export any technical data, process,
* software, or service, directly or indirectly, to any country for which the
* United States government or any agency thereof requires an export license,
* other governmental approval, or letter of assurance, without first obtaining
* such license, approval or letter.
*
*****************************************************************************/
#define __UTLOCK_C__
#include "acpi.h"
#include "accommon.h"
#define _COMPONENT ACPI_UTILITIES
ACPI_MODULE_NAME ("utlock")
/*******************************************************************************
*
* FUNCTION: AcpiUtCreateRwLock
* AcpiUtDeleteRwLock
*
* PARAMETERS: Lock - Pointer to a valid RW lock
*
* RETURN: Status
*
* DESCRIPTION: Reader/writer lock creation and deletion interfaces.
*
******************************************************************************/
ACPI_STATUS
AcpiUtCreateRwLock (
ACPI_RW_LOCK *Lock)
{
ACPI_STATUS Status;
Lock->NumReaders = 0;
Status = AcpiOsCreateMutex (&Lock->ReaderMutex);
if (ACPI_FAILURE (Status))
{
return (Status);
}
Status = AcpiOsCreateMutex (&Lock->WriterMutex);
return (Status);
}
void
AcpiUtDeleteRwLock (
ACPI_RW_LOCK *Lock)
{
AcpiOsDeleteMutex (Lock->ReaderMutex);
AcpiOsDeleteMutex (Lock->WriterMutex);
Lock->NumReaders = 0;
Lock->ReaderMutex = NULL;
Lock->WriterMutex = NULL;
}
/*******************************************************************************
*
* FUNCTION: AcpiUtAcquireReadLock
* AcpiUtReleaseReadLock
*
* PARAMETERS: Lock - Pointer to a valid RW lock
*
* RETURN: Status
*
* DESCRIPTION: Reader interfaces for reader/writer locks. On acquisition,
* only the first reader acquires the write mutex. On release,
* only the last reader releases the write mutex. Although this
* algorithm can in theory starve writers, this should not be a
* problem with ACPICA since the subsystem is infrequently used
* in comparison to (for example) an I/O system.
*
******************************************************************************/
ACPI_STATUS
AcpiUtAcquireReadLock (
ACPI_RW_LOCK *Lock)
{
ACPI_STATUS Status;
Status = AcpiOsAcquireMutex (Lock->ReaderMutex, ACPI_WAIT_FOREVER);
if (ACPI_FAILURE (Status))
{
return (Status);
}
/* Acquire the write lock only for the first reader */
Lock->NumReaders++;
if (Lock->NumReaders == 1)
{
Status = AcpiOsAcquireMutex (Lock->WriterMutex, ACPI_WAIT_FOREVER);
}
AcpiOsReleaseMutex (Lock->ReaderMutex);
return (Status);
}
ACPI_STATUS
AcpiUtReleaseReadLock (
ACPI_RW_LOCK *Lock)
{
ACPI_STATUS Status;
Status = AcpiOsAcquireMutex (Lock->ReaderMutex, ACPI_WAIT_FOREVER);
if (ACPI_FAILURE (Status))
{
return (Status);
}
/* Release the write lock only for the very last reader */
Lock->NumReaders--;
if (Lock->NumReaders == 0)
{
AcpiOsReleaseMutex (Lock->WriterMutex);
}
AcpiOsReleaseMutex (Lock->ReaderMutex);
return (Status);
}
/*******************************************************************************
*
* FUNCTION: AcpiUtAcquireWriteLock
* AcpiUtReleaseWriteLock
*
* PARAMETERS: Lock - Pointer to a valid RW lock
*
* RETURN: Status
*
* DESCRIPTION: Writer interfaces for reader/writer locks. Simply acquire or
* release the writer mutex associated with the lock. Acquisition
* of the lock is fully exclusive and will block all readers and
* writers until it is released.
*
******************************************************************************/
ACPI_STATUS
AcpiUtAcquireWriteLock (
ACPI_RW_LOCK *Lock)
{
ACPI_STATUS Status;
Status = AcpiOsAcquireMutex (Lock->WriterMutex, ACPI_WAIT_FOREVER);
return (Status);
}
void
AcpiUtReleaseWriteLock (
ACPI_RW_LOCK *Lock)
{
AcpiOsReleaseMutex (Lock->WriterMutex);
}

View File

@ -141,7 +141,8 @@ AcpiUtDeleteMutex (
*
* RETURN: Status
*
* DESCRIPTION: Create the system mutex objects.
* DESCRIPTION: Create the system mutex objects. This includes mutexes,
* spin locks, and reader/writer locks.
*
******************************************************************************/
@ -156,9 +157,8 @@ AcpiUtMutexInitialize (
ACPI_FUNCTION_TRACE (UtMutexInitialize);
/*
* Create each of the predefined mutex objects
*/
/* Create each of the predefined mutex objects */
for (i = 0; i < ACPI_NUM_MUTEX; i++)
{
Status = AcpiUtCreateMutex (i);
@ -177,6 +177,14 @@ AcpiUtMutexInitialize (
}
Status = AcpiOsCreateLock (&AcpiGbl_HardwareLock);
if (ACPI_FAILURE (Status))
{
return_ACPI_STATUS (Status);
}
/* Create the reader/writer lock for namespace access */
Status = AcpiUtCreateRwLock (&AcpiGbl_NamespaceRwLock);
return_ACPI_STATUS (Status);
}
@ -189,7 +197,8 @@ AcpiUtMutexInitialize (
*
* RETURN: None.
*
* DESCRIPTION: Delete all of the system mutex objects.
* DESCRIPTION: Delete all of the system mutex objects. This includes mutexes,
* spin locks, and reader/writer locks.
*
******************************************************************************/
@ -203,9 +212,8 @@ AcpiUtMutexTerminate (
ACPI_FUNCTION_TRACE (UtMutexTerminate);
/*
* Delete each predefined mutex object
*/
/* Delete each predefined mutex object */
for (i = 0; i < ACPI_NUM_MUTEX; i++)
{
(void) AcpiUtDeleteMutex (i);
@ -215,6 +223,10 @@ AcpiUtMutexTerminate (
AcpiOsDeleteLock (AcpiGbl_GpeLock);
AcpiOsDeleteLock (AcpiGbl_HardwareLock);
/* Delete the reader/writer lock */
AcpiUtDeleteRwLock (&AcpiGbl_NamespaceRwLock);
return_VOID;
}

View File

@ -247,6 +247,10 @@ ACPI_EXTERN BOOLEAN AcpiGbl_GlobalLockPresent;
ACPI_EXTERN ACPI_SPINLOCK AcpiGbl_GpeLock; /* For GPE data structs and registers */
ACPI_EXTERN ACPI_SPINLOCK AcpiGbl_HardwareLock; /* For ACPI H/W except GPE registers */
/* Reader/Writer lock is used for namespace walk and dynamic table unload */
ACPI_EXTERN ACPI_RW_LOCK AcpiGbl_NamespaceRwLock;
/*****************************************************************************
*

View File

@ -185,6 +185,16 @@ static char *AcpiGbl_MutexNames[ACPI_NUM_MUTEX] =
#endif
#endif
/* Lock structure for reader/writer interfaces */
typedef struct acpi_rw_lock
{
ACPI_MUTEX WriterMutex;
ACPI_MUTEX ReaderMutex;
UINT32 NumReaders;
} ACPI_RW_LOCK;
/*
* Predefined handles for spinlocks used within the subsystem.

View File

@ -177,7 +177,7 @@ void
AcpiTbTerminate (
void);
void
ACPI_STATUS
AcpiTbDeleteNamespaceByOwner (
UINT32 TableIndex);

View File

@ -577,6 +577,33 @@ AcpiUtExecute_Sxds (
ACPI_NAMESPACE_NODE *DeviceNode,
UINT8 *Highest);
/*
* utlock - reader/writer locks
*/
ACPI_STATUS
AcpiUtCreateRwLock (
ACPI_RW_LOCK *Lock);
void
AcpiUtDeleteRwLock (
ACPI_RW_LOCK *Lock);
ACPI_STATUS
AcpiUtAcquireReadLock (
ACPI_RW_LOCK *Lock);
ACPI_STATUS
AcpiUtReleaseReadLock (
ACPI_RW_LOCK *Lock);
ACPI_STATUS
AcpiUtAcquireWriteLock (
ACPI_RW_LOCK *Lock);
void
AcpiUtReleaseWriteLock (
ACPI_RW_LOCK *Lock);
/*
* utobject - internal object create/delete/cache routines

View File

@ -416,6 +416,7 @@ ACPI_TYPED_IDENTIFIER_TABLE AcpiIdentifiers[] = {
{"ACPI_ROUND_UP_TO_32_BIT", SRC_TYPE_SIMPLE},
{"ACPI_RSCONVERT_INFO", SRC_TYPE_STRUCT},
{"ACPI_RSDUMP_INFO", SRC_TYPE_STRUCT},
{"ACPI_RW_LOCK", SRC_TYPE_STRUCT},
{"ACPI_SCOPE_STATE", SRC_TYPE_STRUCT},
{"ACPI_SEMAPHORE", SRC_TYPE_SIMPLE},
{"ACPI_SIGNAL_FATAL_INFO", SRC_TYPE_STRUCT},