From nobody Tue Dec 24 16:38:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1511753532860356.0759045313562; Sun, 26 Nov 2017 19:32:12 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id A18E521A1099D; Sun, 26 Nov 2017 19:27:49 -0800 (PST) Received: from mail-pg0-x22a.google.com (mail-pg0-x22a.google.com [IPv6:2607:f8b0:400e:c05::22a]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 16CA621A1099A for ; Sun, 26 Nov 2017 19:27:47 -0800 (PST) Received: by mail-pg0-x22a.google.com with SMTP id l19so18092959pgo.2 for ; Sun, 26 Nov 2017 19:32:09 -0800 (PST) Received: from szxbz956.huaweiobz.com ([45.56.152.184]) by smtp.gmail.com with ESMTPSA id b8sm23294231pff.26.2017.11.26.19.32.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 26 Nov 2017 19:32:08 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2607:f8b0:400e:c05::22a; helo=mail-pg0-x22a.google.com; envelope-from=heyi.guo@linaro.org; receiver=edk2-devel@lists.01.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=hTpuq/qGtzLRmU15q2wRM4s6k6GjYgvToqROWxhq3pQ=; b=EgSzkY7ZDm1Y9zjp83dlNpwrjTuIFqEQAFbxAEUBL2vnqI7nbKTOy4538gmaQfSORk 4MeHrc3meBCv5O8115ptxmqkZlyobYppxEhlRmvhsNeK4T2eRtc1mNy91NxtN/6WFrft BqztO5h9nAOV1tueKYQET8SSOtQmdbohHlUzo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=hTpuq/qGtzLRmU15q2wRM4s6k6GjYgvToqROWxhq3pQ=; b=chXXAwWsWcwZGnlcrYyPNG50c7bDuJsOU4D0JW5OOHboIX/cDOTxFOPSKYxZqldUp2 ljl3f+AntcKKv4PM15aix/XeZqBvFovXgPoV5asNx8eSykuWzo5YjXzYMES9X88PiLnj 2eyi1y1oR5msQfDBqwvVSWxpiWEEVSBK4+OewVta47y3poTNZkg1jD3dwpepS8NORETq w7zx2PyxyFtljPkQgQawMh6cFJyBIR4uffjCCvNH12GLWmy37P3WAEpQy0GtCAqsEicU CxjRyHBBp8RfKgxZ/iNrIXTS0uXwzrkYc/j94lMl1s/LKFQcW+Pof8m0fvc2dkdb3bpP fy4g== X-Gm-Message-State: AJaThX4cwrdrJr29LT0DoCjnWQWyy9SFNr9y+eotm3/n+lIKVr+Tka/8 yJT1b3P4u+M5c1zgvErJXnETTw== X-Google-Smtp-Source: AGs4zMZwhrMv4BOzP4yD4UwzsFTRkKaXMrqnBEClQvg9RIuQQhPp9bWdyGWZ3fIlfJipcOpoC18qMQ== X-Received: by 10.99.127.84 with SMTP id p20mr35717706pgn.204.1511753528743; Sun, 26 Nov 2017 19:32:08 -0800 (PST) From: Heyi Guo To: linaro-uefi@lists.linaro.org, edk2-devel@lists.01.org Date: Mon, 27 Nov 2017 11:31:44 +0800 Message-Id: <1511753504-38548-1-git-send-email-heyi.guo@linaro.org> X-Mailer: git-send-email 2.7.2.windows.1 Subject: [edk2] [PATCH] MdeModulePkg/PerformanceLib: add lock protection X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Heyi Guo , Liming Gao , Eric Dong , Star Zeng MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" DXE performance gauge record access functions might be reentered since we are supporting something like USB hot-plug, which is a timer event where gBS->ConnectController might be called and then PERF will be called in CoreConnectSingleController. When StartGaugeEx is being reentered, not only the gauge record might be overwritten, more serious situation will be caused if gauge data buffer reallocation procedure is interrupted, between line 180 and 187 in DxeCorePerformanceLib.c specifically. There, mMaxGaugeRecords will be doubled twice (denoted as 4X), but mGaugeData only points to a buffer of size 2X, which will probably cause the following 2X memory to be overflowed when gauge records are increased. So we add EFI lock with with TPL_NOTIFY in StartGaugeEx/EndGaugeEx/GetGaugeEx to avoid memory overflow and gauge data corruption. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Heyi Guo Cc: Star Zeng Cc: Eric Dong Cc: Liming Gao Reviewed-by: Star Zeng with the redundant with at " w= ith with " in the commit log. --- .../DxeCorePerformanceLib/DxeCorePerformanceLib.c | 67 ++++++++++++++++++= +++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceL= ib.c b/MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceLib.c index 51f488a..7c0e207 100644 --- a/MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceLib.c +++ b/MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceLib.c @@ -63,6 +63,11 @@ PERFORMANCE_EX_PROTOCOL mPerformanceExInterface =3D { =20 PERFORMANCE_PROPERTY mPerformanceProperty; =20 +// +// Gauge record lock to avoid data corruption or even memory overflow +// +STATIC EFI_LOCK mPerfRecordLock =3D EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTI= FY); + /** Searches in the gauge array with keyword Handle, Token, Module and Ident= ifier. =20 @@ -162,6 +167,12 @@ StartGaugeEx ( UINTN OldGaugeDataSize; GAUGE_DATA_HEADER *OldGaugeData; UINT32 Index; + EFI_STATUS Status; + + Status =3D EfiAcquireLockOrFail (&mPerfRecordLock); + if (EFI_ERROR (Status)) { + return Status; + } =20 Index =3D mGaugeData->NumberOfEntries; if (Index >=3D mMaxGaugeRecords) { @@ -175,6 +186,7 @@ StartGaugeEx ( =20 NewGaugeData =3D AllocateZeroPool (GaugeDataSize); if (NewGaugeData =3D=3D NULL) { + EfiReleaseLock (&mPerfRecordLock); return EFI_OUT_OF_RESOURCES; } =20 @@ -209,6 +221,8 @@ StartGaugeEx ( =20 mGaugeData->NumberOfEntries++; =20 + EfiReleaseLock (&mPerfRecordLock); + return EFI_SUCCESS; } =20 @@ -250,6 +264,12 @@ EndGaugeEx ( { GAUGE_DATA_ENTRY_EX *GaugeEntryExArray; UINT32 Index; + EFI_STATUS Status; + + Status =3D EfiAcquireLockOrFail (&mPerfRecordLock); + if (EFI_ERROR (Status)) { + return Status; + } =20 if (TimeStamp =3D=3D 0) { TimeStamp =3D GetPerformanceCounter (); @@ -257,11 +277,13 @@ EndGaugeEx ( =20 Index =3D InternalSearchForGaugeEntry (Handle, Token, Module, Identifier= ); if (Index >=3D mGaugeData->NumberOfEntries) { + EfiReleaseLock (&mPerfRecordLock); return EFI_NOT_FOUND; } GaugeEntryExArray =3D (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1); GaugeEntryExArray[Index].EndTimeStamp =3D TimeStamp; =20 + EfiReleaseLock (&mPerfRecordLock); return EFI_SUCCESS; } =20 @@ -274,6 +296,8 @@ EndGaugeEx ( If it stands for a valid entry, then EFI_SUCCESS is returned and GaugeDataEntryEx stores the pointer to that entry. =20 + This internal function is added to avoid releasing lock before each retu= rn statement. + @param LogEntryKey The key for the previous performance mea= surement log entry. If 0, then the first performance measure= ment log entry is retrieved. @param GaugeDataEntryEx The indirect pointer to the extended gau= ge data entry specified by LogEntryKey @@ -287,7 +311,7 @@ EndGaugeEx ( **/ EFI_STATUS EFIAPI -GetGaugeEx ( +InternalGetGaugeEx ( IN UINTN LogEntryKey, OUT GAUGE_DATA_ENTRY_EX **GaugeDataEntryEx ) @@ -314,6 +338,47 @@ GetGaugeEx ( } =20 /** + Retrieves a previously logged performance measurement. + It can also retrieve the log created by StartGauge and EndGauge of PERFO= RMANCE_PROTOCOL, + and then assign the Identifier with 0. + + Retrieves the performance log entry from the performance log specified b= y LogEntryKey. + If it stands for a valid entry, then EFI_SUCCESS is returned and + GaugeDataEntryEx stores the pointer to that entry. + + @param LogEntryKey The key for the previous performance mea= surement log entry. + If 0, then the first performance measure= ment log entry is retrieved. + @param GaugeDataEntryEx The indirect pointer to the extended gau= ge data entry specified by LogEntryKey + if the retrieval is successful. + + @retval EFI_SUCCESS The GuageDataEntryEx is successfully fou= nd based on LogEntryKey. + @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equal= s to the total entry number). + @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (gr= eater than the total entry number). + @retval EFI_INVALIDE_PARAMETER GaugeDataEntryEx is NULL. + +**/ +EFI_STATUS +EFIAPI +GetGaugeEx ( + IN UINTN LogEntryKey, + OUT GAUGE_DATA_ENTRY_EX **GaugeDataEntryEx + ) +{ + EFI_STATUS Status; + + Status =3D EfiAcquireLockOrFail (&mPerfRecordLock); + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D InternalGetGaugeEx (LogEntryKey, GaugeDataEntryEx); + + EfiReleaseLock (&mPerfRecordLock); + + return Status; +} + +/** Adds a record at the end of the performance measurement log that records the start time of a performance measurement. =20 --=20 2.7.4 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel