From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 09214740038 for ; Mon, 18 Mar 2024 02:20:31 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=fOSZCdhefMurtDfcxZSAxvxtKDmURT35hOTzEq65ado=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20240206; t=1710728430; v=1; b=M6gT+2x5T6aH+Fh5kttVCcXK0ytkQ3e/Ch9xJAhX5QVwjAMxeHnZfsTvXGXlJcf79yLjxYLr mhShmW9Ip4netPmXskUr/xkmQr9/eZkasVnqxvxhZX2TvpOh3fmI1B8BCJYcXbMheJppJDIh+Gz Zv/stdWQDhlJjkpiN3GGgDLJyMnO14h/OD+vDGG8am2NGtn7GvyWtNBNlzopxxfbu66vW6DMqw4 Bz4zBIdUUA6Ux39XSHEoZejQejraPZP1HY7o/xm32Xj44sC/OFvCs1UvYkL+igKpcZUoCWqxixE ZqS+ZNNi+jE+ZY/HFBK+NtTqwVuTUKH2R4X6wWWFOljjQ== X-Received: by 127.0.0.2 with SMTP id umfwYY7687511x2cvWx7oqmR; Sun, 17 Mar 2024 19:20:30 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by mx.groups.io with SMTP id smtpd.web11.34038.1710728416304381805 for ; Sun, 17 Mar 2024 19:20:30 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,11016"; a="5363137" X-IronPort-AV: E=Sophos;i="6.07,133,1708416000"; d="scan'208";a="5363137" X-Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Mar 2024 19:20:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,133,1708416000"; d="scan'208";a="13356231" X-Received: from shwdesfp01.ccr.corp.intel.com ([10.239.158.151]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Mar 2024 19:20:27 -0700 From: "Zhiguang Liu" To: devel@edk2.groups.io Cc: Zhiguang Liu , Liming Gao , Jiaxin Wu , Ray Ni , Laszlo Ersek , Ard Biesheuvel , Sami Mujawar Subject: [edk2-devel] [PATCH 6/6] StandaloneMmPkg: Support to unregister MMI handler in MMI handlers Date: Mon, 18 Mar 2024 10:19:56 +0800 Message-Id: <20240318021956.2787-7-zhiguang.liu@intel.com> In-Reply-To: <20240318021956.2787-1-zhiguang.liu@intel.com> References: <20240318021956.2787-1-zhiguang.liu@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Sun, 17 Mar 2024 19:20:30 -0700 Reply-To: devel@edk2.groups.io,zhiguang.liu@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: 7vEBYFtEaScrhsfQ7ZANoQwJx7686176AA= Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=M6gT+2x5; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io Unregistering MMI handler will free the MMI_HANDLER. However, the MmiManage() may be using the link node from MMI_HANDLER for loop if the unregistering happens in MMI handlers. To avoid that, the idea is to inform MmiHandlerUnRegister() whether it's running or not running on the stack of MmiManage(), and to postpone MMI_HANDLER deletion until after the loop finishes. Cc: Liming Gao Cc: Jiaxin Wu Cc: Ray Ni Cc: Laszlo Ersek Cc: Ray Ni Cc: Laszlo Ersek Cc: Ard Biesheuvel Cc: Sami Mujawar Signed-off-by: Zhiguang Liu --- StandaloneMmPkg/Core/Mmi.c | 102 +++++++++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 4 deletions(-) diff --git a/StandaloneMmPkg/Core/Mmi.c b/StandaloneMmPkg/Core/Mmi.c index 0de6fd17fc..d9cdd1f1a7 100644 --- a/StandaloneMmPkg/Core/Mmi.c +++ b/StandaloneMmPkg/Core/Mmi.c @@ -34,8 +34,14 @@ typedef struct { LIST_ENTRY Link; // Link on MMI_ENTRY.MmiHandlers EFI_MM_HANDLER_ENTRY_POINT Handler; // The mm handler's entry point MMI_ENTRY *MmiEntry; + BOOLEAN NeedDeleted; // To delete this MMI_HANDLER later } MMI_HANDLER; +// +// mMmiManageCallingDepth is used to track the depth of recursive calls of MmiManage. +// +UINTN mMmiManageCallingDepth = 0; + LIST_ENTRY mRootMmiHandlerList = INITIALIZE_LIST_HEAD_VARIABLE (mRootMmiHandlerList); LIST_ENTRY mMmiEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mMmiEntryList); @@ -126,11 +132,14 @@ MmiManage ( { LIST_ENTRY *Link; LIST_ENTRY *Head; + LIST_ENTRY *EntryLink; MMI_ENTRY *MmiEntry; MMI_HANDLER *MmiHandler; BOOLEAN SuccessReturn; + BOOLEAN CanReturn; EFI_STATUS Status; + mMmiManageCallingDepth++; Status = EFI_NOT_FOUND; SuccessReturn = FALSE; if (HandlerType == NULL) { @@ -171,7 +180,12 @@ MmiManage ( // no additional handlers will be processed and EFI_INTERRUPT_PENDING will be returned. // if (HandlerType != NULL) { - return EFI_INTERRUPT_PENDING; + // + // Won't go to next Handler, and will return with EFI_INTERRUPT_PENDING later + // + SuccessReturn = FALSE; + Status = EFI_INTERRUPT_PENDING; + CanReturn = TRUE; } break; @@ -183,7 +197,10 @@ MmiManage ( // additional handlers will be processed. // if (HandlerType != NULL) { - return EFI_SUCCESS; + // + // Won't go to next Handler, and return with EFI_SUCCESS + // + CanReturn = TRUE; } SuccessReturn = TRUE; @@ -211,12 +228,78 @@ MmiManage ( ASSERT_EFI_ERROR (Status); break; } + + if (CanReturn) { + break; + } } if (SuccessReturn) { Status = EFI_SUCCESS; } + ASSERT (mMmiManageCallingDepth > 0); + mMmiManageCallingDepth--; + + // + // The MmiHandlerUnRegister won't take effect inside MmiManage. + // Before returned from MmiManage, delete the MmiHandler which is + // marked as NeedDeleted. + // Note that MmiManage can be called recursively. + // + if (mMmiManageCallingDepth == 0) { + // + // Go through all MmiHandler in root Mmi handlers + // + for ( Link = GetFirstNode (&mRootMmiHandlerList) + ; !IsNull (&mRootMmiHandlerList, Link); + ) + { + MmiHandler = CR (Link, MMI_HANDLER, Link, MMI_HANDLER_SIGNATURE); + Link = GetNextNode (&mRootMmiHandlerList, Link); + if (MmiHandler->NeedDeleted) { + // + // Remove MmiHandler if the NeedDeleted is set. + // + RemoveEntryList (&MmiHandler->Link); + FreePool (MmiHandler); + } + } + + // + // Go through all MmiHandler in non-root MMI handlers + // + for ( EntryLink = GetFirstNode (&mMmiEntryList) + ; !IsNull (&mMmiEntryList, EntryLink); + ) + { + MmiEntry = CR (EntryLink, MMI_ENTRY, AllEntries, MMI_ENTRY_SIGNATURE); + EntryLink = GetNextNode (&mMmiEntryList, EntryLink); + for ( Link = GetFirstNode (&MmiEntry->MmiHandlers) + ; !IsNull (&MmiEntry->MmiHandlers, Link); + ) + { + MmiHandler = CR (Link, MMI_HANDLER, Link, MMI_HANDLER_SIGNATURE); + Link = GetNextNode (&MmiEntry->MmiHandlers, Link); + if (MmiHandler->NeedDeleted) { + // + // Remove MmiHandler if the NeedDeleted is set. + // + RemoveEntryList (&MmiHandler->Link); + FreePool (MmiHandler); + } + } + + if (IsListEmpty (&MmiEntry->MmiHandlers)) { + // + // No handler registered for this MmiEntry now, remove the MMI_ENTRY + // + RemoveEntryList (&MmiEntry->AllEntries); + FreePool (MmiEntry); + } + } + } + return Status; } @@ -252,8 +335,9 @@ MmiHandlerRegister ( return EFI_OUT_OF_RESOURCES; } - MmiHandler->Signature = MMI_HANDLER_SIGNATURE; - MmiHandler->Handler = Handler; + MmiHandler->Signature = MMI_HANDLER_SIGNATURE; + MmiHandler->Handler = Handler; + MmiHandler->NeedDeleted = FALSE; if (HandlerType == NULL) { // @@ -311,6 +395,16 @@ MmiHandlerUnRegister ( MmiEntry = MmiHandler->MmiEntry; + if (mMmiManageCallingDepth > 0) { + // + // This function is called from MmiManage() + // Do not delete or remove MmiHandler or MmiEntry now. + // Set the NeedDeleted field in MmiHandler so that MmiManage will delete it later + // + MmiHandler->NeedDeleted = TRUE; + return EFI_SUCCESS; + } + RemoveEntryList (&MmiHandler->Link); FreePool (MmiHandler); -- 2.31.1.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#116832): https://edk2.groups.io/g/devel/message/116832 Mute This Topic: https://groups.io/mt/104996186/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-