public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Laszlo Ersek" <lersek@redhat.com>
To: Pedro Falcato <pedro.falcato@gmail.com>,
	"Zhou, Jianfeng" <jianfeng.zhou@intel.com>
Cc: "devel@edk2.groups.io" <devel@edk2.groups.io>,
	"Tan, Dun" <dun.tan@intel.com>, "Ni, Ray" <ray.ni@intel.com>,
	"Kumar, Rahul R" <rahul.r.kumar@intel.com>,
	Gerd Hoffmann <kraxel@redhat.com>
Subject: Re: [edk2-devel] [PATCH 1/3] UefiCpuPkg: Reduce and optimize access to attribute
Date: Wed, 7 Feb 2024 21:33:48 +0100	[thread overview]
Message-ID: <aa896e22-8c8e-be93-3c8f-cc1c6a842f15@redhat.com> (raw)
In-Reply-To: <CAKbZUD3tYm9hAU1MgBXrCgjd2db++0CZOecpYhaR_QBAEAzpAQ@mail.gmail.com>

On 2/7/24 02:05, Pedro Falcato wrote:
> On Wed, Feb 7, 2024 at 12:47 AM Zhou, Jianfeng <jianfeng.zhou@intel.com> wrote:
>>
>> Hi Laszlo, Pedro,
>>
>> Clarify one thing, this change is not for racing introduced by MP reading/writing to the same page table at the same time, but for unexpected behavior introduced by compiler.
>> As my understanding,  MP reading/writing to the same page table at the same time is not recommended, perhaps, it is not allowed.
> 
> AFAIK, it's not allowed as IIRC APs cannot run arbitrary EFI boot services code.

It need not be about two processors manipulating the same page table; it
suffices (for the problem) if one processor is massaging the page table
while another processor is accesing the affected virtual address range.
The AP may be running custom code that does not touch UEFI / PI services
at all (computing something in preallocated memory etc).

> 
>>
>> For bit operation code, such as Pnle->Bits.Present = Attribute->Bits.Present, we might think it is atomic assignment, while not. The assembly code looks like:
>>     and dword [rcx], 0xfffffffe
>>     and eax, 0x1
>>     or [rcx], eax
>> In case Pnle->Bits.Present = 1,  Attribute->Bits.Present = 1,  we might think it is harmless, as the value not changed.  While actually,
>>     and dword [rcx], 0xfffffffe  // the present bit set to 0 ---- this is unexpected !!!!! we don’t want the present bit set to 0!
>>     and eax, 0x1
>>     or [rcx], eax             // the present bit set to right value 1
>>
>> Let's consider such a MP scenario:
>> 1) one processor executing instruction "and dword [rcx], 0xfffffffe"
>> 2) other processors happened to access the memory mapped by Pnle, it may lead to exception.
> 
> I understand your original problem, but:
> 
> 1) Your fix is not correct. The compiler can tear your store, you need
> to use a volatile store for this.

(Side comment: I don't disagree that "volatile" may be, and mostly is,
sufficient for preventing tearing, but I'd like to note that the C99
spec itself does not seem to require that behavior of volatile. Section
"5.1.2.3 Program execution", paragraph 5 says, "The least requirements
on a conforming implementation are: — At sequence points, volatile
objects are stable in the sense that previous accesses are
complete and subsequent accesses have not yet occurred. [...]". I think
Linux is very correct to use a dedicated macro WRITE_ONCE, and if that
*happens* to expand to a volatile access on a particular platform &
compiler, that's great, but an implementation detail.

In edk2, I would like to see something stronger than just volatile; at
least semantically. For example, we have MmioWrite64() from IoLib, we
have EFI_CPU_IO2_PROTOCOL.Mem.Write() etc. If they are implemented with
plain "volatile" in the background, that may be fine in practice. It's
just always difficult to tell from the code and the patches whether an
access is *supposed* to be "atomic" or not!)

> 2) What kind of page table manipulation is happening while APs are
> running code, and does this mean you need a TLB shootdown mechanism?
> 

Yes, ideally, patch#1's commit message should include a stack trace for
CPU A and another for CPU B. I reckon those may be difficult to collect
*precisely*, but some natural language explanation would help.

Laszlo



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



  parent reply	other threads:[~2024-02-07 20:33 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-05 14:03 [edk2-devel] [PATCH 0/3] Fix potential issue in CpuPageTableLib and SMM page table initialization duntan
2024-02-05 14:03 ` [edk2-devel] [PATCH 1/3] UefiCpuPkg: Reduce and optimize access to attribute duntan
2024-02-06  1:20   ` Ni, Ray
2024-02-06 13:32   ` Laszlo Ersek
2024-02-06 15:02     ` Ni, Ray
2024-02-06 17:34     ` Pedro Falcato
2024-02-07  0:47       ` Zhou, Jianfeng
2024-02-07  1:05         ` Pedro Falcato
2024-02-07  1:57           ` Zhou, Jianfeng
2024-02-07 17:52             ` Pedro Falcato
2024-02-07 20:42             ` Laszlo Ersek
2024-02-08  2:29               ` Zhou, Jianfeng
2024-02-07 20:33           ` Laszlo Ersek [this message]
2024-02-07 20:17         ` Laszlo Ersek
2024-02-05 14:03 ` [edk2-devel] [PATCH 2/3] UefiCpuPkg: Add more Paging mode enumeration duntan
2024-02-06  1:21   ` Ni, Ray
2024-02-05 14:03 ` [edk2-devel] [PATCH 3/3] UefiCpuPkg/PiSmmCpuDxeSmm:Map SMRAM in 4K page granularity duntan
2024-02-06  1:23   ` Ni, Ray
2024-02-06 13:33   ` Laszlo Ersek
2024-02-06  1:48 ` [edk2-devel] [PATCH 0/3] Fix potential issue in CpuPageTableLib and SMM page table initialization Ni, Ray

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=aa896e22-8c8e-be93-3c8f-cc1c6a842f15@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