Page MenuHomeFreeBSD

amd64 pmap: add comment explaining TLB invalidation modes.
ClosedPublic

Authored by kib on Jul 26 2020, 4:44 PM.
Tags
None
Referenced Files
Unknown Object (File)
Tue, Nov 19, 4:38 AM
Unknown Object (File)
Tue, Nov 19, 4:34 AM
Unknown Object (File)
Tue, Nov 19, 4:32 AM
Unknown Object (File)
Tue, Nov 19, 4:17 AM
Unknown Object (File)
Tue, Nov 19, 4:16 AM
Unknown Object (File)
Tue, Nov 19, 2:39 AM
Unknown Object (File)
Thu, Nov 14, 12:14 AM
Unknown Object (File)
Mon, Nov 11, 8:57 AM
Subscribers

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Skipped
Unit
Tests Skipped
Build Status
Buildable 32572

Event Timeline

There are a very large number of changes, so older changes are hidden. Show Older Changes
sys/amd64/amd64/pmap.c
2461

"The kernel page table maps the user and kernel address spaces in their entirety. It is identical to the per-process page table allocated in non-PTI ...

2462–2463

I would open the paragraph with this sentence.

kib marked 7 inline comments as done.Nov 8 2020, 8:57 PM
kib added inline comments.
sys/amd64/amd64/pmap.c
2452

I said 'high operational impact' there. It is explained below, at this point intent is only to note to reader that PTI changes a lot. What specifically is changed comparing for !PTI, is described later.

2454–2455

Typo: I mean PTI vs. non-PTI.

kib marked 2 inline comments as done.

Edits.

sys/amd64/amd64/pmap.c
2458

-> "the Meltdown bug ..."

Remove the stray space between PTI and the subsequent comma.

2467

-> "... that the user space ..."

I would drop the "s" at the end of "kernel page tables".

2468

I would take it for granted that TLB coherence must be maintained. Is the real point that you're trying to make here that these TLB invalidations can't be deferred, unlike the user page table ones mentioned below? If so, I think that this paragraph would be clearer if you swapped the order of the sentences, that is, talk about the user page table first.

2469

"... when the CPU is in user mode. Consequently, some TLB invalidations ...

2472

"The existence of a user mode page table ..."

2473

"by a pm_ucr3 value that differs from ..., in which case pm_ucr3 contains

2474

"... for the user mode page table's root.

2476

"... which CPUs currently have the pmap active. A CPU's bit is set on ...

2478

"... For the kernel page table, the pm_active field

2481

"present in hardware" -> "in use by the hardware"

2484

"..., the pmap sends ...

2485

"... recorded as active in pm_active. Updates to and reads from pm_active are not synchronized, and so they may race with each other. Shootdown handlers ...

2491

"... to. This feature provides a limited namespace ..."

(The text that follows uses "PCID" to refer to a "Process ID" so I want to stop using it to refer to the feature.)

2492

"... 12 bits, supporting 4095 simultaneous IDs total.

2494

"Allocation of a PCID to a pmap ...

2495

"section 15.12, "Other TLB Consistency Algorithms", of Vahalia's book "Unix Internals".

2496

"A PCID cannot ... whole lifetime of a pmap in ...

2498

"when the CPU ...

2500

"switch that activates ...

2504

"On invalidation" -> "On TLB invalidation"

and

"is" -> "are"

2505

"... that the previously allocated ..."

2506–2508

"Consequently, if a pmap is inactive on a CPU, then a TLB shootdown for that pmap and CPU can be initiated by an ordinary memory access to reset the target CPU's generation count within the pmap. The CPU initiating the TLB shootdown does not need to send an IPI to the target CPU.

2511

For consistency, "user mode"

2512

"A user PCID ...

2513

"... bit, 11, to ...

2515

"Userspace" -> "User space"
and
"usermode" -> "user mode"

2516–2517

"... clearing bit 63 of the loaded ..."

2518

"usermode" -> "user mode"

Also, can you clarify whether this is a "total invalidation" for one pmap or all pmaps on that CPU.

2519

-> "... in the user ..."

2523

INVTLB?

2526

-> "If the INVPCID ...

2527

-> "from the kernel page table.

2529

-> "... The kernel reserves ...

2530

"usermode" -> "user mode"

2531

-> "... A context switch allocates a new ...

2532

-> "the recorded PCID is zero or the recorded generation does not match the CPU's

2533

-> "..., effectively flushing the TLB ...

2574

"usermode" -> "user mode"

2575

-> "... for the user page table.

kib marked 39 inline comments as done.

39 more edits.

That's all that I have.

sys/amd64/amd64/pmap.c
2449–2450

I would drop the last sentence of this paragraph, i.e., the sentence "For SMP, ...", and then merge this paragraph with the next one.

(The use of IPIs comes up below. Here, I think it is lower level than the rest of this new combined paragraph.)

2451

This line is missing an "*".

2452

"The kernel configuration option that has the greatest operational impact on TLB invalidation is PTI, which ...

2452–2455

PTI is the kernel configuration option that most affects

2453

"CPUs. The most impactful hardware ...

2465

"allocated" -> "used"

2469–2471

"... mode. In contrast, the user space part of the kernel page table is used for copyout(9), so TLB invalidations on this page table cannot be postponed."

2474

"... by a pm_ucr3

2487

For consistency, add another space after the period.

2505–2506

"... count, pm_gen, which is ...

2509

"... valid. Effectively, zeroing any of these counters triggers a TLB shootdown ...

2511

"of a new ...

2526

Drop the first "the": "clearing bit 63 ...

2531–2532

"modes. If the requested invalidation is for a specific address or the total invalidation of a currently active pmap, then the TLB is flushed using INVLPG for a kernel page table, and ..."

I feel like there is a missing qualifying phrase at the end of INVPCID(INVPCID_CTXGLOB)/invltlb_glob().

2549

I think that all of the entries of this form would be clearer if written as "remote user page, inactive pmap:" Otherwise, it reads like the page must be inactive.

kib marked 14 inline comments as done.Jan 6 2021, 7:10 PM
kib added inline comments.
sys/amd64/amd64/pmap.c
2452–2455

Sorry, I cannot get sense of this comment.

Another batch of edits.
Move comment out of #ifdef SMP.

sys/amd64/amd64/pmap.c
2452–2455

I think that this was the start of a comment that I intended to delete because it was superseded by another comment.

sys/amd64/amd64/pmap.c
2452

"mainly" -> "first"

2469

Can shorten to: "... from kernel to user mode."

2471

-> "... be similarly postponed.

2479

-> "... switch to the pmap, and ..."

2483

Add comma, -> "..., e.g., ..."

2503

Add comma, -> "i.e., ..."

2527

-> "... TLB entries for ..."

2528

Can we change "If ucr3_load_mask is set, then local ..." to "In which case, local ..."?

2535

We rarely use the word "process" in this comment. Can we say, "user space", instead?

2551

Here is a technical question as opposed to one about wording: Given the IPI:INVLPG why is pm_gen zeroed? Is this because of aforementioned pm_active race?

2589

Are you planning to replace this?

kib marked 13 inline comments as done.Jan 9 2021, 9:29 PM
kib added inline comments.
sys/amd64/amd64/pmap.c
2551

Yes, due to a possibility to lost the race and have the target pmap no longer active.

I believe that the recent fix where pm_gen zeroing was misplaced and this caused observable issues proves the point.

2589

Not in this change.

I plan to write about generation, emulation of A/D. Might be mention different format of EPT tables.

For SVM, something should be also noted.

kib marked 2 inline comments as done.

Next batch of edits.

sys/amd64/amd64/pmap.c
2551

Then, I would add a parenthetical comment on the next line: "(Both actions are required to handle the aforementioned pm_active races.)"

sys/amd64/amd64/pmap.c
2511

For consistency, "cpu" -> "CPU"

kib marked an inline comment as done.

Add note about INVLPG+pm_gen = 0

kib marked an inline comment as done.Jan 9 2021, 11:38 PM
This revision is now accepted and ready to land.Jan 9 2021, 11:56 PM