From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by mx.groups.io with SMTP id smtpd.web10.2283.1612407591616301107 for ; Wed, 03 Feb 2021 18:59:51 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.20, mailfrom: ray.ni@intel.com) IronPort-SDR: /M2/E6OExTBuzrQkswZjX74fjqBBcYj2kuQgAu6PwUIRzpJiScCRyBcNzWS4eAxolbBjGA+fhW La9KpMykFU6Q== X-IronPort-AV: E=McAfee;i="6000,8403,9884"; a="168269643" X-IronPort-AV: E=Sophos;i="5.79,400,1602572400"; d="scan'208";a="168269643" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 18:59:42 -0800 IronPort-SDR: 0ydndPKYMJrOiK7iVdgRnIrNox7ViU/xBzJvvxC2WsChaepcXSoadbXXyYhzfw4XbVRkOfd6x3 urzzSGubAB+g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.79,400,1602572400"; d="scan'208";a="480711045" Received: from ray-dev.ccr.corp.intel.com ([10.239.158.87]) by fmsmga001.fm.intel.com with ESMTP; 03 Feb 2021 18:59:40 -0800 From: "Ni, Ray" To: devel@edk2.groups.io Cc: Laszlo Ersek , Eric Dong , Rahul1 Kumar Subject: [PATCH 2/2] UefiCpuPkg/MpInitLib: Use XADD to avoid lock acquire/release Date: Thu, 4 Feb 2021 10:59:21 +0800 Message-Id: <20210204025921.1428-3-ray.ni@intel.com> X-Mailer: git-send-email 2.27.0.windows.1 In-Reply-To: <20210204025921.1428-1-ray.ni@intel.com> References: <20210204025921.1428-1-ray.ni@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable When AP firstly wakes up, MpFuncs.nasm contains below logic to assign an unique ApIndex to each AP according to who comes first: ---ASM--- TestLock: xchg [edi], eax cmp eax, NotVacantFlag jz TestLock mov ecx, esi add ecx, ApIndexLocation inc dword [ecx] mov ebx, [ecx] Releaselock: mov eax, VacantFlag xchg [edi], eax ---ASM END--- "lock inc" cannot be used to increase ApIndex because not only the global ApIndex should be increased, but also the result should be stored to a local general purpose register EBX. This patch learns from the NASM implementation of InternalSyncIncrement() to use "XADD" instruction which can increase the global ApIndex and store the original ApIndex to EBX in one instruction. With this patch, OVMF when running in a 255 threads QEMU spends about one second to wakeup all APs. Original implementation needs more than 10 seconds. Signed-off-by: Ray Ni Cc: Laszlo Ersek Cc: Eric Dong Cc: Rahul1 Kumar --- UefiCpuPkg/Library/MpInitLib/Ia32/MpEqu.inc | 4 ---- .../Library/MpInitLib/Ia32/MpFuncs.nasm | 21 +++++-------------- UefiCpuPkg/Library/MpInitLib/MpLib.c | 3 +-- UefiCpuPkg/Library/MpInitLib/MpLib.h | 3 +-- UefiCpuPkg/Library/MpInitLib/X64/MpEqu.inc | 4 ---- UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm | 18 ++++------------ 6 files changed, 11 insertions(+), 42 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/Ia32/MpEqu.inc b/UefiCpuPkg/Libra= ry/MpInitLib/Ia32/MpEqu.inc index 244c1e72b7..5f1f0351d2 100644 --- a/UefiCpuPkg/Library/MpInitLib/Ia32/MpEqu.inc +++ b/UefiCpuPkg/Library/MpInitLib/Ia32/MpEqu.inc @@ -12,16 +12,12 @@ ;=0D ;-------------------------------------------------------------------------= ------=0D =0D -VacantFlag equ 00h=0D -NotVacantFlag equ 0ffh=0D -=0D CPU_SWITCH_STATE_IDLE equ 0=0D CPU_SWITCH_STATE_STORED equ 1=0D CPU_SWITCH_STATE_LOADED equ 2=0D =0D MP_CPU_EXCHANGE_INFO_OFFSET equ (SwitchToRealProcEnd - RendezvousFunnelPro= cStart)=0D struc MP_CPU_EXCHANGE_INFO=0D - .Lock: resd 1=0D .StackStart: resd 1=0D .StackSize: resd 1=0D .CFunction: resd 1=0D diff --git a/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm b/UefiCpuPkg/Li= brary/MpInitLib/Ia32/MpFuncs.nasm index 908c2eb447..b7267393db 100644 --- a/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm +++ b/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm @@ -122,23 +122,12 @@ SkipEnableExecuteDisable: =0D ; AP init=0D mov edi, esi=0D - add edi, MP_CPU_EXCHANGE_INFO_OFFSET=0D - mov eax, NotVacantFlag=0D -=0D -TestLock:=0D - xchg [edi], eax=0D - cmp eax, NotVacantFlag=0D - jz TestLock=0D -=0D - mov ecx, esi=0D - add ecx, MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_INFO.ApI= ndex=0D - inc dword [ecx]=0D - mov ebx, [ecx]=0D -=0D -Releaselock:=0D - mov eax, VacantFlag=0D - xchg [edi], eax=0D + add edi, MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_INFO.ApI= ndex=0D + mov ebx, 1=0D + lock xadd [edi], ebx ; EBX =3D ApIndex++=0D + inc ebx ; EBX is CpuNumber=0D =0D + ; program stack=0D mov edi, esi=0D add edi, MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_INFO.Sta= ckSize=0D mov eax, [edi]=0D diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpIn= itLib/MpLib.c index 8b1f7f84ba..32a3951742 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -1,7 +1,7 @@ /** @file=0D CPU MP Initialize Library common functions.=0D =0D - Copyright (c) 2016 - 2020, Intel Corporation. All rights reserved.
=0D + Copyright (c) 2016 - 2021, Intel Corporation. All rights reserved.
=0D Copyright (c) 2020, AMD Inc. All rights reserved.
=0D =0D SPDX-License-Identifier: BSD-2-Clause-Patent=0D @@ -1012,7 +1012,6 @@ FillExchangeInfoData ( IA32_CR4 Cr4;=0D =0D ExchangeInfo =3D CpuMpData->MpCpuExchangeInfo;=0D - ExchangeInfo->Lock =3D 0;=0D ExchangeInfo->StackStart =3D CpuMpData->Buffer;=0D ExchangeInfo->StackSize =3D CpuMpData->CpuApStackSize;=0D ExchangeInfo->BufferStart =3D CpuMpData->WakeupBuffer;=0D diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h b/UefiCpuPkg/Library/MpIn= itLib/MpLib.h index 02652eaae1..0bd60388b1 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.h +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h @@ -1,7 +1,7 @@ /** @file=0D Common header file for MP Initialize Library.=0D =0D - Copyright (c) 2016 - 2020, Intel Corporation. All rights reserved.
=0D + Copyright (c) 2016 - 2021, Intel Corporation. All rights reserved.
=0D Copyright (c) 2020, AMD Inc. All rights reserved.
=0D =0D SPDX-License-Identifier: BSD-2-Clause-Patent=0D @@ -190,7 +190,6 @@ typedef struct _CPU_MP_DATA CPU_MP_DATA; // into this structure are used in assembly code in this module=0D //=0D typedef struct {=0D - UINTN Lock;=0D UINTN StackStart;=0D UINTN StackSize;=0D UINTN CFunction;=0D diff --git a/UefiCpuPkg/Library/MpInitLib/X64/MpEqu.inc b/UefiCpuPkg/Librar= y/MpInitLib/X64/MpEqu.inc index 3974330991..32e9a262bc 100644 --- a/UefiCpuPkg/Library/MpInitLib/X64/MpEqu.inc +++ b/UefiCpuPkg/Library/MpInitLib/X64/MpEqu.inc @@ -12,16 +12,12 @@ ;=0D ;-------------------------------------------------------------------------= ------=0D =0D -VacantFlag equ 00h=0D -NotVacantFlag equ 0ffh=0D -=0D CPU_SWITCH_STATE_IDLE equ 0=0D CPU_SWITCH_STATE_STORED equ 1=0D CPU_SWITCH_STATE_LOADED equ 2=0D =0D MP_CPU_EXCHANGE_INFO_OFFSET equ (SwitchToRealProcEnd - RendezvousFunnelPro= cStart)=0D struc MP_CPU_EXCHANGE_INFO=0D - .Lock: resq 1=0D .StackStart: resq 1=0D .StackSize: resq 1=0D .CFunction: resq 1=0D diff --git a/UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm b/UefiCpuPkg/Lib= rary/MpInitLib/X64/MpFuncs.nasm index 423beb2cca..383b4974f8 100644 --- a/UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm +++ b/UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm @@ -158,21 +158,11 @@ LongModeStart: =0D ; AP init=0D mov edi, esi=0D - add edi, MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_INFO.Loc= k=0D - mov rax, NotVacantFlag=0D + add edi, MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_INFO.ApI= ndex=0D + mov ebx, 1=0D + lock xadd [edi], ebx ; EBX =3D ApIndex++=0D + inc ebx ; EBX is CpuNumber=0D =0D -TestLock:=0D - xchg qword [edi], rax=0D - cmp rax, NotVacantFlag=0D - jz TestLock=0D -=0D - lea ecx, [esi + MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_I= NFO.ApIndex]=0D - inc dword [ecx]=0D - mov ebx, [ecx]=0D -=0D -Releaselock:=0D - mov rax, VacantFlag=0D - xchg qword [edi], rax=0D ; program stack=0D mov edi, esi=0D add edi, MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_INFO.Sta= ckSize=0D --=20 2.27.0.windows.1