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 3C96C78003C for ; Mon, 25 Mar 2024 02:48:22 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=Ql41gPfQM/TNp0N9JKlmZ52mmpwHnuXdvxzCp/GAs+A=; c=relaxed/simple; d=groups.io; h=From:To:CC:Subject:Thread-Topic:Thread-Index:Date:Message-ID:References:In-Reply-To:Accept-Language:msip_labels:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Language:Content-Type; s=20240206; t=1711334900; v=1; b=BmvxlN58bcIDV7auH4+pXLOm+VknsLO3CZPK1fF1sV+GxCC9NgLnk4DZAth6viL920tZqBBC HyeuqA708kiyqN0KDuqUJIqr0iDefIx3GTY5x296nDyL+4UQoVB77MOeJ+LYSVrzIWoXl4r/Z4E 672zcOJP0eFOvNUWxTgcTxhYemD6Z7oIR5FRjiacM8dhT4Zb3KQSnKHqgv0qSOs+uUqu5Oz9JkS Ns8cl5pTx0BYR+ulpk7alME4ZeL00PTBLhaN7q0nSmzzYtuQyFOsp2OgEJmg4hYeOupFTwZU6jc ArofslJIT2xU9fXT541QrzEcU2vBrbxiVYdqkD/HTEjsg== X-Received: by 127.0.0.2 with SMTP id 1CgtYY7687511xCijkwm5LB1; Sun, 24 Mar 2024 19:48:20 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.13]) by mx.groups.io with SMTP id smtpd.web10.43655.1711334900288912859 for ; Sun, 24 Mar 2024 19:48:20 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,11023"; a="17461332" X-IronPort-AV: E=Sophos;i="6.07,152,1708416000"; d="scan'208,217";a="17461332" X-Received: from orviesa009.jf.intel.com ([10.64.159.149]) by orvoesa105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Mar 2024 19:48:20 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,152,1708416000"; d="scan'208,217";a="15454033" X-Received: from fmsmsx603.amr.corp.intel.com ([10.18.126.83]) by orviesa009.jf.intel.com with ESMTP/TLS/AES256-GCM-SHA384; 24 Mar 2024 19:48:20 -0700 X-Received: from fmsmsx612.amr.corp.intel.com (10.18.126.92) by fmsmsx603.amr.corp.intel.com (10.18.126.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Sun, 24 Mar 2024 19:48:19 -0700 X-Received: from FMSEDG603.ED.cps.intel.com (10.1.192.133) by fmsmsx612.amr.corp.intel.com (10.18.126.92) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35 via Frontend Transport; Sun, 24 Mar 2024 19:48:19 -0700 X-Received: from NAM02-BN1-obe.outbound.protection.outlook.com (104.47.51.41) by edgegateway.intel.com (192.55.55.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Sun, 24 Mar 2024 19:48:18 -0700 X-Received: from MN6PR11MB8244.namprd11.prod.outlook.com (2603:10b6:208:470::14) by MN0PR11MB6253.namprd11.prod.outlook.com (2603:10b6:208:3c6::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.31; Mon, 25 Mar 2024 02:48:16 +0000 X-Received: from MN6PR11MB8244.namprd11.prod.outlook.com ([fe80::2c31:82b7:9f26:5817]) by MN6PR11MB8244.namprd11.prod.outlook.com ([fe80::2c31:82b7:9f26:5817%5]) with mapi id 15.20.7409.028; Mon, 25 Mar 2024 02:48:16 +0000 From: "Ni, Ray" To: "Liu, Zhiguang" , "devel@edk2.groups.io" CC: Liming Gao , "Wu, Jiaxin" , Laszlo Ersek , Ard Biesheuvel , Sami Mujawar Subject: Re: [edk2-devel] [PATCH 6/6] StandaloneMmPkg: Support to unregister MMI handler in MMI handlers Thread-Topic: [PATCH 6/6] StandaloneMmPkg: Support to unregister MMI handler in MMI handlers Thread-Index: AQHaeNrkaIF1f0nMt0up5tWlmHrw3bFHy4TL Date: Mon, 25 Mar 2024 02:48:16 +0000 Message-ID: References: <20240318021956.2787-1-zhiguang.liu@intel.com> <20240318021956.2787-7-zhiguang.liu@intel.com> In-Reply-To: <20240318021956.2787-7-zhiguang.liu@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: MN6PR11MB8244:EE_|MN0PR11MB6253:EE_ x-ms-office365-filtering-correlation-id: 633a179e-54cc-4569-9bdf-08dc4c7605d4 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam-message-info: 7UHk/OaVYJMOJrEas2Plaim6jqXctFI4rzO6K1rBA6esq4NkpXgmD8llwSQlHeGmwpBQn3ZkeaR9F/vU2KHL1ghPphWqA3dmNQcH01vIE+jrU5WnsVl9q+RqtDbC6yqtHRm4YLIV7GS1kjbYEsR7F18GsdhDJg/KLosyWco9cA+S4K3ipXjYKJzu4kllnwZR1dF0GaLdbCTaJR2qK0Wv1QuhJR85H5qkzPh+OFthZX1K+5+HDxt/fK8PkOMP3wNsWdKqob0h1HTxAj23zP7vPle1imdaHGy8aN7gkYtRyVHJggjgIgSyca35PGY72ElOX3FxBHbo6mB1gB8Yvaw5FvhD2KsT9J13nh4hM3eH1zqkNHzdTWn8Tjpl/Zrg15xx0dhg9nvBoS8IglhSqry8lOO9b3WTTTjohdxXtzs5mgBfbYeRttn+Ke4jtU2zDmZhZDXzQc5Zo/ol9mfPliF6WcZOAexON3pBFBHzY3YNIEKqwu5hweZaiyfeKopp278y0WX2BLKntEvWedkA0D7lZUzEbHJLGrCdeEaoku110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mtCS3XmOHZgFZw/n7vJf12P/IIVZyZ?= =?iso-8859-1?Q?oaGxnZsNS3xlQlxLlj3v6MBxGHA+vHvF/LZlCKIbiAnOn4/hLiBZVYw8K/?= =?iso-8859-1?Q?C8XFN5W3VziIY=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MN6PR11MB8244.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 633a179e-54cc-4569-9bdf-08dc4c7605d4 X-MS-Exchange-CrossTenant-originalarrivaltime: 25 Mar 2024 02:48:16.7241 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: pbQP6J5CXn0R0gERQldBdnI1T7lO7AXkYqldJXh6KhckUdim0XScyLKEX6pRuXj+bJn14UkY3A5SXSxwBVJwtg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR11MB6253 X-OriginatorOrg: intel.com 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, 24 Mar 2024 19:48:20 -0700 Reply-To: devel@edk2.groups.io,ray.ni@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: b5JiPh4hJVFM1ca80lEL4HiGx7686176AA= Content-Language: en-US Content-Type: multipart/alternative; boundary="_000_MN6PR11MB82441E87A43EDF98101AFFE58C362MN6PR11MB8244namp_" X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=BmvxlN58; 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 --_000_MN6PR11MB82441E87A43EDF98101AFFE58C362MN6PR11MB8244namp_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Zhiguang, I feel that you could simplify the logic in MmiManage () by adding a new lo= cal variable to cache the return status. Can you think about that and come up with a new version of patch? Also, the list maninpulation logics are duplicated in MmiManage () and Unre= gister(). can you try to avoid it? Thanks, Ray ________________________________ From: Liu, Zhiguang Sent: Monday, March 18, 2024 10:19 To: devel@edk2.groups.io Cc: Liu, Zhiguang ; Liming Gao ; Wu, Jiaxin ; Ni, Ray ; Laszl= o Ersek ; Ard Biesheuvel ; Sa= mi Mujawar Subject: [PATCH 6/6] StandaloneMmPkg: Support to unregister MMI handler in = MMI handlers 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.MmiHandl= ers EFI_MM_HANDLER_ENTRY_POINT Handler; // The mm handler's entry poi= nt 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 =3D 0; + LIST_ENTRY mRootMmiHandlerList =3D INITIALIZE_LIST_HEAD_VARIABLE (mRootMm= iHandlerList); LIST_ENTRY mMmiEntryList =3D INITIALIZE_LIST_HEAD_VARIABLE (mMmiEnt= ryList); @@ -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 =3D EFI_NOT_FOUND; SuccessReturn =3D FALSE; if (HandlerType =3D=3D NULL) { @@ -171,7 +180,12 @@ MmiManage ( // no additional handlers will be processed and EFI_INTERRUPT_PEND= ING will be returned. // if (HandlerType !=3D NULL) { - return EFI_INTERRUPT_PENDING; + // + // Won't go to next Handler, and will return with EFI_INTERRUPT_= PENDING later + // + SuccessReturn =3D FALSE; + Status =3D EFI_INTERRUPT_PENDING; + CanReturn =3D TRUE; } break; @@ -183,7 +197,10 @@ MmiManage ( // additional handlers will be processed. // if (HandlerType !=3D NULL) { - return EFI_SUCCESS; + // + // Won't go to next Handler, and return with EFI_SUCCESS + // + CanReturn =3D TRUE; } SuccessReturn =3D TRUE; @@ -211,12 +228,78 @@ MmiManage ( ASSERT_EFI_ERROR (Status); break; } + + if (CanReturn) { + break; + } } if (SuccessReturn) { Status =3D 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 =3D=3D 0) { + // + // Go through all MmiHandler in root Mmi handlers + // + for ( Link =3D GetFirstNode (&mRootMmiHandlerList) + ; !IsNull (&mRootMmiHandlerList, Link); + ) + { + MmiHandler =3D CR (Link, MMI_HANDLER, Link, MMI_HANDLER_SIGNATURE); + Link =3D 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 =3D GetFirstNode (&mMmiEntryList) + ; !IsNull (&mMmiEntryList, EntryLink); + ) + { + MmiEntry =3D CR (EntryLink, MMI_ENTRY, AllEntries, MMI_ENTRY_SIGNAT= URE); + EntryLink =3D GetNextNode (&mMmiEntryList, EntryLink); + for ( Link =3D GetFirstNode (&MmiEntry->MmiHandlers) + ; !IsNull (&MmiEntry->MmiHandlers, Link); + ) + { + MmiHandler =3D CR (Link, MMI_HANDLER, Link, MMI_HANDLER_SIGNATURE)= ; + Link =3D 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_ENT= RY + // + RemoveEntryList (&MmiEntry->AllEntries); + FreePool (MmiEntry); + } + } + } + return Status; } @@ -252,8 +335,9 @@ MmiHandlerRegister ( return EFI_OUT_OF_RESOURCES; } - MmiHandler->Signature =3D MMI_HANDLER_SIGNATURE; - MmiHandler->Handler =3D Handler; + MmiHandler->Signature =3D MMI_HANDLER_SIGNATURE; + MmiHandler->Handler =3D Handler; + MmiHandler->NeedDeleted =3D FALSE; if (HandlerType =3D=3D NULL) { // @@ -311,6 +395,16 @@ MmiHandlerUnRegister ( MmiEntry =3D 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 dele= te it later + // + MmiHandler->NeedDeleted =3D TRUE; + return EFI_SUCCESS; + } + RemoveEntryList (&MmiHandler->Link); FreePool (MmiHandler); -- 2.31.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#117078): https://edk2.groups.io/g/devel/message/117078 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] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- --_000_MN6PR11MB82441E87A43EDF98101AFFE58C362MN6PR11MB8244namp_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Zhiguang,
I feel that you could simplify the log= ic in MmiManage () by adding a new local variable to cache the return status.
Can you think about t= hat and come up with a new version of patch?

Also, the list maninp= ulation logics are duplicated in MmiManage () and Unregister(). can you try= to avoid it?

Thanks,
Ray


From: Liu, Z= higuang <zhiguang.liu@intel.com>
Sent: Monday, March 18, 2024 10:19
To: devel@edk2.groups.io <devel@edk2.groups.io>
Cc: Liu, Zhiguang <zhiguang.liu@intel.com>; Liming Gao &l= t;gaoliming@byosoft.com.cn>; Wu, Jiaxin <jiaxin.wu@intel.com>; Ni,= Ray <ray.ni@intel.com>; Laszlo Ersek <lersek@redhat.com>; Ard = Biesheuvel <ardb+tianocore@kernel.org>; Sami Mujawar <sami.mujawar= @arm.com>
Subject: [PATCH 6/6] StandaloneMmPkg: Support to unregister MMI= handler in MMI handlers
 
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 <gaoliming@byosoft.com.cn>
Cc: Jiaxin Wu <jiaxin.wu@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Sami Mujawar <sami.mujawar@arm.com>
Signed-off-by: Zhiguang Liu <zhiguang.liu@intel.com>
---
 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        &nbs= p;           Link; &= nbsp;      // Link on MMI_ENTRY.MmiHandlers
   EFI_MM_HANDLER_ENTRY_POINT    Handler; &nb= sp;   // The mm handler's entry point
   MMI_ENTRY         = ;            *MmiEnt= ry;
+  BOOLEAN          =              Ne= edDeleted; // To delete this MMI_HANDLER later
 } MMI_HANDLER;
 
+//
+// mMmiManageCallingDepth is used to track the depth of recursive calls of= MmiManage.
+//
+UINTN  mMmiManageCallingDepth =3D 0;
+
 LIST_ENTRY  mRootMmiHandlerList =3D INITIALIZE_LIST_HEAD_VARIABL= E (mRootMmiHandlerList);
 LIST_ENTRY  mMmiEntryList       = =3D 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        =3D EFI_NOT_F= OUND;
   SuccessReturn =3D FALSE;
   if (HandlerType =3D=3D NULL) {
@@ -171,7 +180,12 @@ MmiManage (
         // no additional handlers = will be processed and EFI_INTERRUPT_PENDING will be returned.
         //
         if (HandlerType !=3D NULL)= {
-          return EFI_INTERRUP= T_PENDING;
+          //
+          // Won't go to next= Handler, and will return with EFI_INTERRUPT_PENDING later
+          //
+          SuccessReturn =3D F= ALSE;
+          Status  &= nbsp;     =3D EFI_INTERRUPT_PENDING;
+          CanReturn &nbs= p;   =3D TRUE;
         }
 
         break;
@@ -183,7 +197,10 @@ MmiManage (
         // additional handlers wil= l be processed.
         //
         if (HandlerType !=3D NULL)= {
-          return EFI_SUCCESS;=
+          //
+          // Won't go to next= Handler, and return with EFI_SUCCESS
+          //
+          CanReturn =3D TRUE;=
         }
 
         SuccessReturn =3D TRUE; @@ -211,12 +228,78 @@ MmiManage (
         ASSERT_EFI_ERROR (Status);=
         break;
     }
+
+    if (CanReturn) {
+      break;
+    }
   }
 
   if (SuccessReturn) {
     Status =3D 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 =3D=3D 0) {
+    //
+    // Go through all MmiHandler in root Mmi handlers
+    //
+    for ( Link =3D GetFirstNode (&mRootMmiHandlerList)<= br> +          ; !IsNull (&mRo= otMmiHandlerList, Link);
+          )
+    {
+      MmiHandler =3D CR (Link, MMI_HANDLER, Link,= MMI_HANDLER_SIGNATURE);
+      Link       = =3D GetNextNode (&mRootMmiHandlerList, Link);
+      if (MmiHandler->NeedDeleted) {
+        //
+        // Remove MmiHandler if the Nee= dDeleted is set.
+        //
+        RemoveEntryList (&MmiHandle= r->Link);
+        FreePool (MmiHandler);
+      }
+    }
+
+    //
+    // Go through all MmiHandler in non-root MMI handlers +    //
+    for ( EntryLink =3D GetFirstNode (&mMmiEntryList) +          ; !IsNull (&mMm= iEntryList, EntryLink);
+          )
+    {
+      MmiEntry  =3D CR (EntryLink, MMI_ENTRY= , AllEntries, MMI_ENTRY_SIGNATURE);
+      EntryLink =3D GetNextNode (&mMmiEntryLi= st, EntryLink);
+      for ( Link =3D GetFirstNode (&MmiEntry-= >MmiHandlers)
+            ; !IsNu= ll (&MmiEntry->MmiHandlers, Link);
+            )
+      {
+        MmiHandler =3D CR (Link, MMI_HA= NDLER, Link, MMI_HANDLER_SIGNATURE);
+        Link    &nb= sp;  =3D GetNextNode (&MmiEntry->MmiHandlers, Link);
+        if (MmiHandler->NeedDeleted)= {
+          //
+          // Remove MmiHandle= r if the NeedDeleted is set.
+          //
+          RemoveEntryList (&a= mp;MmiHandler->Link);
+          FreePool (MmiHandle= r);
+        }
+      }
+
+      if (IsListEmpty (&MmiEntry->MmiHandl= ers)) {
+        //
+        // No handler registered for th= is 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 =3D MMI_HANDLER_SIGNATURE;
-  MmiHandler->Handler   =3D Handler;
+  MmiHandler->Signature   =3D MMI_HANDLER_SIGNATURE;
+  MmiHandler->Handler     =3D Handler;
+  MmiHandler->NeedDeleted =3D FALSE;
 
   if (HandlerType =3D=3D NULL) {
     //
@@ -311,6 +395,16 @@ MmiHandlerUnRegister (
 
   MmiEntry =3D MmiHandler->MmiEntry;
 
+  if (mMmiManageCallingDepth > 0) {
+    //
+    // This function is called from MmiManage()
+    // Do not delete or remove MmiHandler or MmiEntry now.<= br> +    // Set the NeedDeleted field in MmiHandler so that MmiM= anage will delete it later
+    //
+    MmiHandler->NeedDeleted =3D TRUE;
+    return EFI_SUCCESS;
+  }
+
   RemoveEntryList (&MmiHandler->Link);
   FreePool (MmiHandler);
 
--
2.31.1.windows.1

_._,_._,_

Groups.io Links:

=20 You receive all messages sent to this group. =20 =20

View/Reply Online (#117078) | =20 | Mute= This Topic | New Topic
Your Subscriptio= n | Contact Group Owner | Unsubscribe [rebecca@openfw.io]

_._,_._,_
--_000_MN6PR11MB82441E87A43EDF98101AFFE58C362MN6PR11MB8244namp_--