From nobody Mon Feb 9 10:32:49 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+71132+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+71132+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1612407597; cv=none; d=zohomail.com; s=zohoarc; b=ljUnWvwVgmTSmbwSEyK+PZcYcQh83X5n7sFHR8UjaAXGQjeId6u99nFhdsuIcbnRSL1Puw7P7Gs/wrZLAgE1V8NUORudh2XDJqHzoI3I9sXYFaAPuQp5P8MBeiN4ndA5tVPGP5nOmNilZvPLJgKIhDh8Et+R37oyIXDLDjwcZrY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612407597; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=ojv3X7HgIQuX/8DLc78cHLXtuL8sDdTyCRL/nIpSfPI=; b=CNRs/sqH49wSWmb2Wbbj2yPghQL68BLTdJ8zxUv3TsZzGgCK+Fk4sgf6E22XWZwCamqN7LutvGvCfWT1rzaFC5iMOd3yHjRmvO5uNnNhXHY7YwpmTOguzJm6ECKzgBtT0qZrnXoqifFzzK1tVji7D0CgImn93Mx7tYAIPqjO/j8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+71132+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1612407597470323.11611341915454; Wed, 3 Feb 2021 18:59:57 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id EWK2YY1788612xbiJLhUxzIt; Wed, 03 Feb 2021 18:59:57 -0800 X-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 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" X-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" X-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: [edk2-devel] [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> In-Reply-To: <20210204025921.1428-1-ray.ni@intel.com> References: <20210204025921.1428-1-ray.ni@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ray.ni@intel.com X-Gm-Message-State: 1WxzplspMQJC7xqpnbp3Uof9x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1612407597; bh=BcNLqViY/23PenIysQGtdm/XD6FoYvGysE/zLHA9xgc=; h=Cc:Date:From:Reply-To:Subject:To; b=SK/JW/Htyi0d7gI4ChmJR2U+F/746xCeqi085c7qehwa/eVdxM5wzVxFGk7Tfs8ZSlZ jkRDb3xrZ4QsrqGu1dDlBTWA5ly4Lfbn8rIAnmG44LClyhe/9oYJUIXhvPoMh6H2a8FjW rkeDZUEmK6fJtfZvdeBbldMxTwVT69GoQPc= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" 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 @@ ; ;-------------------------------------------------------------------------= ------ =20 -VacantFlag equ 00h -NotVacantFlag equ 0ffh - CPU_SWITCH_STATE_IDLE equ 0 CPU_SWITCH_STATE_STORED equ 1 CPU_SWITCH_STATE_LOADED equ 2 =20 MP_CPU_EXCHANGE_INFO_OFFSET equ (SwitchToRealProcEnd - RendezvousFunnelPro= cStart) struc MP_CPU_EXCHANGE_INFO - .Lock: resd 1 .StackStart: resd 1 .StackSize: resd 1 .CFunction: resd 1 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: =20 ; AP init mov edi, esi - add edi, MP_CPU_EXCHANGE_INFO_OFFSET - mov eax, NotVacantFlag - -TestLock: - xchg [edi], eax - cmp eax, NotVacantFlag - jz TestLock - - mov ecx, esi - add ecx, MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_INFO.ApI= ndex - inc dword [ecx] - mov ebx, [ecx] - -Releaselock: - mov eax, VacantFlag - xchg [edi], eax + add edi, MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_INFO.ApI= ndex + mov ebx, 1 + lock xadd [edi], ebx ; EBX =3D ApIndex++ + inc ebx ; EBX is CpuNumber =20 + ; program stack mov edi, esi add edi, MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_INFO.Sta= ckSize mov eax, [edi] 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 CPU MP Initialize Library common functions. =20 - Copyright (c) 2016 - 2020, Intel Corporation. All rights reserved.
+ Copyright (c) 2016 - 2021, Intel Corporation. All rights reserved.
Copyright (c) 2020, AMD Inc. All rights reserved.
=20 SPDX-License-Identifier: BSD-2-Clause-Patent @@ -1012,7 +1012,6 @@ FillExchangeInfoData ( IA32_CR4 Cr4; =20 ExchangeInfo =3D CpuMpData->MpCpuExchangeInfo; - ExchangeInfo->Lock =3D 0; ExchangeInfo->StackStart =3D CpuMpData->Buffer; ExchangeInfo->StackSize =3D CpuMpData->CpuApStackSize; ExchangeInfo->BufferStart =3D CpuMpData->WakeupBuffer; 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 Common header file for MP Initialize Library. =20 - Copyright (c) 2016 - 2020, Intel Corporation. All rights reserved.
+ Copyright (c) 2016 - 2021, Intel Corporation. All rights reserved.
Copyright (c) 2020, AMD Inc. All rights reserved.
=20 SPDX-License-Identifier: BSD-2-Clause-Patent @@ -190,7 +190,6 @@ typedef struct _CPU_MP_DATA CPU_MP_DATA; // into this structure are used in assembly code in this module // typedef struct { - UINTN Lock; UINTN StackStart; UINTN StackSize; UINTN CFunction; 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 @@ ; ;-------------------------------------------------------------------------= ------ =20 -VacantFlag equ 00h -NotVacantFlag equ 0ffh - CPU_SWITCH_STATE_IDLE equ 0 CPU_SWITCH_STATE_STORED equ 1 CPU_SWITCH_STATE_LOADED equ 2 =20 MP_CPU_EXCHANGE_INFO_OFFSET equ (SwitchToRealProcEnd - RendezvousFunnelPro= cStart) struc MP_CPU_EXCHANGE_INFO - .Lock: resq 1 .StackStart: resq 1 .StackSize: resq 1 .CFunction: resq 1 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: =20 ; AP init mov edi, esi - add edi, MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_INFO.Lock - mov rax, NotVacantFlag + add edi, MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_INFO.ApI= ndex + mov ebx, 1 + lock xadd [edi], ebx ; EBX =3D ApIndex++ + inc ebx ; EBX is CpuNumber =20 -TestLock: - xchg qword [edi], rax - cmp rax, NotVacantFlag - jz TestLock - - lea ecx, [esi + MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_I= NFO.ApIndex] - inc dword [ecx] - mov ebx, [ecx] - -Releaselock: - mov rax, VacantFlag - xchg qword [edi], rax ; program stack mov edi, esi add edi, MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_INFO.Sta= ckSize --=20 2.27.0.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 (#71132): https://edk2.groups.io/g/devel/message/71132 Mute This Topic: https://groups.io/mt/80372087/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-