public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Zhi Jin" <zhi.jin@intel.com>
To: "Ni, Ray" <ray.ni@intel.com>,
	"devel@edk2.groups.io" <devel@edk2.groups.io>
Cc: Laszlo Ersek <lersek@redhat.com>,
	"Kumar, Rahul R" <rahul.r.kumar@intel.com>,
	Gerd Hoffmann <kraxel@redhat.com>,
	"Wu, Jiaxin" <jiaxin.wu@intel.com>
Subject: Re: [edk2-devel] [PATCH 1/1] UefiCpuPkg/PiSmmCpuDxeSmm: Optimize PatchSmmSaveStateMap and FlushTlbForAll
Date: Fri, 5 Jan 2024 02:37:50 +0000	[thread overview]
Message-ID: <CO1PR11MB5185EEF22C1C23782694DC63F7662@CO1PR11MB5185.namprd11.prod.outlook.com> (raw)
In-Reply-To: <MN6PR11MB8244A69C40CA41019AC176398C662@MN6PR11MB8244.namprd11.prod.outlook.com>

Thanks for the comments, Ray.
It is a mistake to remove the FlushTlb() in this patch. I will send out the patch v2.

BRs
Zhi Jin

-----Original Message-----
From: Ni, Ray <ray.ni@intel.com> 
Sent: Friday, January 05, 2024 10:21 AM
To: devel@edk2.groups.io; Jin, Zhi <zhi.jin@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>; Kumar, Rahul R <rahul.r.kumar@intel.com>; Gerd Hoffmann <kraxel@redhat.com>; Wu, Jiaxin <jiaxin.wu@intel.com>
Subject: RE: [edk2-devel] [PATCH 1/1] UefiCpuPkg/PiSmmCpuDxeSmm: Optimize PatchSmmSaveStateMap and FlushTlbForAll

Zhi,
With your patch,
1. SMM entry(code) and SmmSaveState region (data) are changed to correct paging attributes.
2. FlushTlb() is removed after the changing.
3. FlushTlb() is updated to flush in parallel.

My concern is about #2. Can you explain a bit why FlushTlb() can be removed after changing paging attributes in #1?

Thanks,
Ray
> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Jin, Zhi
> Sent: Friday, January 5, 2024 10:04 AM
> To: devel@edk2.groups.io
> Cc: Jin, Zhi <zhi.jin@intel.com>; Ni, Ray <ray.ni@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Kumar, Rahul R <rahul.r.kumar@intel.com>; Gerd
> Hoffmann <kraxel@redhat.com>; Wu, Jiaxin <jiaxin.wu@intel.com>
> Subject: [edk2-devel] [PATCH 1/1] UefiCpuPkg/PiSmmCpuDxeSmm: Optimize
> PatchSmmSaveStateMap and FlushTlbForAll
> 
> PatchSmmSaveStateMap patches the SMM entry (code) and SmmSaveState
> region (data) for each core, which can be improved to flush TLB once
> after all the memory entries have been patched.
> FlushTlbForAll flushes TLB for each core in serial, which can be
> improved to flush TLB in parrallel.
> 
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Rahul Kumar <rahul1.kumar@intel.com>
> Cc: Gerd Hoffmann <kraxel@redhat.com>
> Cc: Jiaxin Wu <jiaxin.wu@intel.com>
> Signed-off-by: Zhi Jin <zhi.jin@intel.com>
> ---
>  .../PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c   | 95
> ++++++++++++-------
>  1 file changed, 63 insertions(+), 32 deletions(-)
> 
> diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c
> b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c
> index 15f998e501..d4066436f5 100644
> --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c
> +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c
> @@ -547,17 +547,14 @@ FlushTlbForAll (
>    VOID
>    )
>  {
> -  UINTN  Index;
> -
>    FlushTlbOnCurrentProcessor (NULL);
> -
> -  for (Index = 0; Index < gSmst->NumberOfCpus; Index++) {
> -    if (Index != gSmst->CurrentlyExecutingCpu) {
> -      // Force to start up AP in blocking mode,
> -      SmmBlockingStartupThisAp (FlushTlbOnCurrentProcessor, Index, NULL);
> -      // Do not check return status, because AP might not be present in some
> corner cases.
> -    }
> -  }
> +  InternalSmmStartupAllAPs (
> +    (EFI_AP_PROCEDURE2)FlushTlbOnCurrentProcessor,
> +    0,
> +    NULL,
> +    NULL,
> +    NULL
> +    );
>  }
> 
>  /**
> @@ -799,71 +796,105 @@ PatchSmmSaveStateMap (
>    UINTN  TileCodeSize;
>    UINTN  TileDataSize;
>    UINTN  TileSize;
> +  UINTN  PageTableBase;
> 
> -  TileCodeSize = GetSmiHandlerSize ();
> -  TileCodeSize = ALIGN_VALUE (TileCodeSize, SIZE_4KB);
> -  TileDataSize = (SMRAM_SAVE_STATE_MAP_OFFSET - SMM_PSD_OFFSET) +
> sizeof (SMRAM_SAVE_STATE_MAP);
> -  TileDataSize = ALIGN_VALUE (TileDataSize, SIZE_4KB);
> -  TileSize     = TileDataSize + TileCodeSize - 1;
> -  TileSize     = 2 * GetPowerOfTwo32 ((UINT32)TileSize);
> +  TileCodeSize  = GetSmiHandlerSize ();
> +  TileCodeSize  = ALIGN_VALUE (TileCodeSize, SIZE_4KB);
> +  TileDataSize  = (SMRAM_SAVE_STATE_MAP_OFFSET - SMM_PSD_OFFSET) +
> sizeof (SMRAM_SAVE_STATE_MAP);
> +  TileDataSize  = ALIGN_VALUE (TileDataSize, SIZE_4KB);
> +  TileSize      = TileDataSize + TileCodeSize - 1;
> +  TileSize      = 2 * GetPowerOfTwo32 ((UINT32)TileSize);
> +  PageTableBase = AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64;
> 
>    DEBUG ((DEBUG_INFO, "PatchSmmSaveStateMap:\n"));
>    for (Index = 0; Index < mMaxNumberOfCpus - 1; Index++) {
>      //
>      // Code
>      //
> -    SmmSetMemoryAttributes (
> +    ConvertMemoryPageAttributes (
> +      PageTableBase,
> +      mPagingMode,
>        mCpuHotPlugData.SmBase[Index] + SMM_HANDLER_OFFSET,
>        TileCodeSize,
> -      EFI_MEMORY_RO
> +      EFI_MEMORY_RO,
> +      TRUE,
> +      NULL
>        );
> -    SmmClearMemoryAttributes (
> +    ConvertMemoryPageAttributes (
> +      PageTableBase,
> +      mPagingMode,
>        mCpuHotPlugData.SmBase[Index] + SMM_HANDLER_OFFSET,
>        TileCodeSize,
> -      EFI_MEMORY_XP
> +      EFI_MEMORY_XP,
> +      FALSE,
> +      NULL
>        );
> 
>      //
>      // Data
>      //
> -    SmmClearMemoryAttributes (
> +    ConvertMemoryPageAttributes (
> +      PageTableBase,
> +      mPagingMode,
>        mCpuHotPlugData.SmBase[Index] + SMM_HANDLER_OFFSET +
> TileCodeSize,
>        TileSize - TileCodeSize,
> -      EFI_MEMORY_RO
> +      EFI_MEMORY_RO,
> +      FALSE,
> +      NULL
>        );
> -    SmmSetMemoryAttributes (
> +    ConvertMemoryPageAttributes (
> +      PageTableBase,
> +      mPagingMode,
>        mCpuHotPlugData.SmBase[Index] + SMM_HANDLER_OFFSET +
> TileCodeSize,
>        TileSize - TileCodeSize,
> -      EFI_MEMORY_XP
> +      EFI_MEMORY_XP,
> +      TRUE,
> +      NULL
>        );
>    }
> 
>    //
>    // Code
>    //
> -  SmmSetMemoryAttributes (
> +  ConvertMemoryPageAttributes (
> +    PageTableBase,
> +    mPagingMode,
>      mCpuHotPlugData.SmBase[mMaxNumberOfCpus - 1] +
> SMM_HANDLER_OFFSET,
>      TileCodeSize,
> -    EFI_MEMORY_RO
> +    EFI_MEMORY_RO,
> +    TRUE,
> +    NULL
>      );
> -  SmmClearMemoryAttributes (
> +  ConvertMemoryPageAttributes (
> +    PageTableBase,
> +    mPagingMode,
>      mCpuHotPlugData.SmBase[mMaxNumberOfCpus - 1] +
> SMM_HANDLER_OFFSET,
>      TileCodeSize,
> -    EFI_MEMORY_XP
> +    EFI_MEMORY_XP,
> +    FALSE,
> +    NULL
>      );
> 
>    //
>    // Data
>    //
> -  SmmClearMemoryAttributes (
> +  ConvertMemoryPageAttributes (
> +    PageTableBase,
> +    mPagingMode,
>      mCpuHotPlugData.SmBase[mMaxNumberOfCpus - 1] +
> SMM_HANDLER_OFFSET + TileCodeSize,
>      SIZE_32KB - TileCodeSize,
> -    EFI_MEMORY_RO
> +    EFI_MEMORY_RO,
> +    FALSE,
> +    NULL
>      );
> -  SmmSetMemoryAttributes (
> +  ConvertMemoryPageAttributes (
> +    PageTableBase,
> +    mPagingMode,
>      mCpuHotPlugData.SmBase[mMaxNumberOfCpus - 1] +
> SMM_HANDLER_OFFSET + TileCodeSize,
>      SIZE_32KB - TileCodeSize,
> -    EFI_MEMORY_XP
> +    EFI_MEMORY_XP,
> +    TRUE,
> +    NULL
>      );
>  }
> 
> --
> 2.39.2
> 
> 
> 
> 
> 



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113207): https://edk2.groups.io/g/devel/message/113207
Mute This Topic: https://groups.io/mt/103535844/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



  reply	other threads:[~2024-01-05  2:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <17A7512BA8BD522D.25044@groups.io>
2024-01-05  2:20 ` [edk2-devel] [PATCH 1/1] UefiCpuPkg/PiSmmCpuDxeSmm: Optimize PatchSmmSaveStateMap and FlushTlbForAll Ni, Ray
2024-01-05  2:37   ` Zhi Jin [this message]
2024-01-05  2:04 Zhi Jin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CO1PR11MB5185EEF22C1C23782694DC63F7662@CO1PR11MB5185.namprd11.prod.outlook.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox