public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Laszlo Ersek" <lersek@redhat.com>
To: devel@edk2.groups.io, kraxel@redhat.com, ray.ni@intel.com
Cc: Oliver Steffen <osteffen@redhat.com>,
	"Kumar, Rahul R" <rahul.r.kumar@intel.com>
Subject: Re: [edk2-devel] [PATCH v2 5/5] UefiCpuPkg/MpInitLib: Add support for multiple HOBs to SaveCpuMpData()
Date: Thu, 22 Feb 2024 02:34:26 +0100	[thread overview]
Message-ID: <ce14e362-c365-5562-458f-3f946b7dd2d1@redhat.com> (raw)
In-Reply-To: <ria72vj44ybeuaigxqtqqjbbba67wwsh3lqfd2ycdvdv2zrcs2@co7np5a7yojp>

On 2/21/24 11:24, Gerd Hoffmann wrote:
> On Wed, Feb 21, 2024 at 03:48:25AM +0000, Ni, Ray wrote:
>>>
>>> +  MaxCpusPerHob = (MAX_UINT16 - sizeof (EFI_HOB_GUID_TYPE) - sizeof
>>> (MP_HAND_OFF)) / sizeof (PROCESSOR_HAND_OFF);
>>
>> Above formula assumes the maximum HOB length could be 0xFFFF.
> 
> Which is IMHO correct.
> 
>> But actually the maximum HOB length could be only 0xFFF8 because
>> PeiCore::PeiCreateHob() contains following logic:
>>
>>   if (0x10000 - Length <= 0x7) {
>>     return EFI_INVALID_PARAMETER;
>>   }
> 
> That Length is the *data* size, the HOB header is not included.
> 
> The "- sizeof (EFI_HOB_GUID_TYPE)" in the formula above accounts the
> space needed for HOB header and GUID.


From the patch:

  MaxCpusPerHob = (MAX_UINT16 - sizeof (EFI_HOB_GUID_TYPE) - sizeof (MP_HAND_OFF)) / sizeof (PROCESSOR_HAND_OFF);
  ...
  CpusInHob     = MIN (CpuMpData->CpuCount - HobBase, MaxCpusPerHob);
  MpHandOffSize = sizeof (MP_HAND_OFF) + sizeof (PROCESSOR_HAND_OFF) * CpusInHob;
  MpHandOff     = (MP_HAND_OFF *)BuildGuidHob (&mMpHandOffGuid, MpHandOffSize);

Assuming that the division is exact, and that the MIN selects MaxCpusPerHob for CpusInHob, we get:

  MpHandOffSize = sizeof (MP_HAND_OFF) + sizeof (PROCESSOR_HAND_OFF) * (MAX_UINT16 - sizeof (EFI_HOB_GUID_TYPE) - sizeof (MP_HAND_OFF)) / sizeof (PROCESSOR_HAND_OFF);

the multiplication and the division cancel out (again, assuming exact division):

  MpHandOffSize = sizeof (MP_HAND_OFF) + (MAX_UINT16 - sizeof (EFI_HOB_GUID_TYPE) - sizeof (MP_HAND_OFF));

the sizeof (MP_HAND_OFF) addition and subtraction cancel out:

  MpHandOffSize = MAX_UINT16 - sizeof (EFI_HOB_GUID_TYPE);

then we get:

  MpHandOff     = (MP_HAND_OFF *)BuildGuidHob (&mMpHandOffGuid, MAX_UINT16 - sizeof (EFI_HOB_GUID_TYPE));

Inside BuildGuidHob() [MdePkg/Library/PeiHobLib/HobLib.c], we get

  DataLength = MAX_UINT16 - sizeof (EFI_HOB_GUID_TYPE);

and further

  ASSERT (DataLength <= (0xFFF8 - sizeof (EFI_HOB_GUID_TYPE)));

Substituting for DataLength,

  ASSERT (MAX_UINT16 - sizeof (EFI_HOB_GUID_TYPE) <= (0xFFF8 - sizeof (EFI_HOB_GUID_TYPE)));

which is

  ASSERT (MAX_UINT16  <= 0xFFF8);

which fails.

So, as Ray says, and as I wrote under v1, you need to use 0xFFF8 here, not MAX_UINT16.

... Under v1, I wrote

  CpusPerHob = (0xFFE0 - sizeof (MP_HAND_OFF)) / sizeof (PROCESSOR_HAND_OFF);

If you want to make "sizeof (EFI_HOB_GUID_TYPE)" -- 24 bytes -- explicit in that subtraction, that is fine, but then we just get back to 0xFFF8:

  CpusPerHob = (0xFFF8 - sizeof (EFI_HOB_GUID_TYPE) - sizeof (MP_HAND_OFF)) / sizeof (PROCESSOR_HAND_OFF);

Laszlo



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



  reply	other threads:[~2024-02-22  1:34 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-20 17:49 [edk2-devel] [PATCH v2 0/5] UefiCpuPkg/MpInitLib: Add support for multiple MP_HAND_OFF HOBs Gerd Hoffmann
2024-02-20 17:49 ` [edk2-devel] [PATCH v2 1/5] UefiCpuPkg/MpInitLib: Add support for multiple HOBs to GetMpHandOffHob Gerd Hoffmann
2024-02-21  3:35   ` Ni, Ray
2024-02-22  0:30     ` Laszlo Ersek
2024-02-20 17:49 ` [edk2-devel] [PATCH v2 2/5] UefiCpuPkg/MpInitLib: Add support for multiple HOBs to GetBspNumber() Gerd Hoffmann
2024-02-21  3:36   ` Ni, Ray
2024-02-21  3:36   ` Ni, Ray
2024-02-22  0:37   ` Laszlo Ersek
2024-02-22  0:38   ` Laszlo Ersek
2024-02-22 10:24     ` Ni, Ray
2024-02-20 17:49 ` [edk2-devel] [PATCH v2 3/5] UefiCpuPkg/MpInitLib: Add support for multiple HOBs to SwitchApContext() Gerd Hoffmann
2024-02-21  5:47   ` Ni, Ray
2024-02-22  0:44   ` Laszlo Ersek
2024-02-20 17:49 ` [edk2-devel] [PATCH v2 4/5] UefiCpuPkg/MpInitLib: Add support for multiple HOBs to MpInitLibInitialize Gerd Hoffmann
2024-02-21  3:37   ` Ni, Ray
2024-02-22  1:11   ` Laszlo Ersek
2024-02-22 12:28     ` Gerd Hoffmann
2024-02-23  0:23       ` Ni, Ray
2024-02-25 12:54         ` Laszlo Ersek
2024-02-20 17:49 ` [edk2-devel] [PATCH v2 5/5] UefiCpuPkg/MpInitLib: Add support for multiple HOBs to SaveCpuMpData() Gerd Hoffmann
2024-02-21  3:48   ` Ni, Ray
2024-02-21 10:24     ` Gerd Hoffmann
2024-02-22  1:34       ` Laszlo Ersek [this message]
2024-02-22  1:49       ` Laszlo Ersek
2024-02-22  2:17         ` Laszlo Ersek
2024-02-22  2:03   ` Laszlo Ersek

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=ce14e362-c365-5562-458f-3f946b7dd2d1@redhat.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