diff --git a/docs/boot-time.md b/docs/boot-time.md index c3079987..20837c37 100644 --- a/docs/boot-time.md +++ b/docs/boot-time.md @@ -212,6 +212,7 @@ them for boot time. |---|---|---|---|---| | Kernel patches 0001-0005, 0007, 0008, 0010 | kernel/patches | cold +67.5 [+62.5, +72.4], with 0006, 0009 and 0011 (since deleted, worth nothing) | group A | lab 44818953987 | | Kernel 0012 (vmlinux BTF parsed on first use) and 0013 (the verifier asks for it only where a kernel type enters a program) | kernel/patches | cold −6.4 [−9.4, −1.0] at 60 a side (lab 45118194310, the kernel carried); before the review's fixes −10.0 [−12.7, −3.3] at 60 and −6.3 [−9.1, −4.6] at 120 (labs 45114405201, 45115330128); restored −1.0 [−6.0, +4.2] evicted, the state 1.3 MiB smaller (lab 45115341056) | measured | lab 45118194310 | +| Kernel 0021-0025, the boot's device-setup exits as one change (#627): the IOAPIC table read once (#590), the state save at add only where error recovery can restore it (#591 a), virtio-pci's vendor capabilities walked once (#591 b), `queue_select` written only on a change and an unchanged MSI message not rewritten (#592). 0022's gate has to grow with whatever calls error handlers next (its changelog). Alone each was within noise ("Measured and rejected") | kernel/patches | first login −9.2 [−12.2, −6.0], sshd accepting −9.0 [−12.4, −6.7], 120 a side (lab 45231943664); restored −0.6 [−2.3, +1.6] evicted and −0.7 [−1.5, +0.8] cached, the state +0.3 MiB [−0.1, +0.6], 30 a side (lab 45232800577); 838 exits fewer, 366 of them MMIO (3 a side, lab 45114428660) | measured | labs 45231943664, 45232800577 | | `BLK_DEV_LOOP_MIN_COUNT=0`, `SERIAL_8250_RUNTIME_UARTS=1` | kernel/config-7.3-x86_64 | cold +5.3 [−3.2, +15.0]: inside the noise, but the upper bound does not rule out its ~2-4 ms ceiling | measured, 40 a side | lab 44839538785 | | `KERNEL_LZ4` | kernel/config-7.3-x86_64 | the alternatives lost: an uncompressed payload, on QEMU 0007, −4.0 [−7.9, +0.3] at 40 a side, within noise, and QEMU's RSS +32 MiB (lab 45183618987); before 0007, +9 ms in copies (laptop) | measured, 40 a side | lab 45183618987, #326 | | `VT=n`, `X86_MSR=n`, `X86_CPUID=n` | kernel/config-7.3-x86_64 | function: dead code, less surface | - | kernel/Dockerfile | @@ -348,7 +349,7 @@ and a row measured in another shape than spin's has such a reason. | x86's `msi_set_affinity` skips the message write when the vector and destination did not change (#592 lever 2, a kernel patch on branch `exp/592-l2-msix-once`) | 56 MMIO exits fewer (`__pci_write_msi_msg` 168 → 112, 3 a side, lab 45113437048), the first login −0.1 [−3.1, +4.5] at 60 a side (lab 45114405201) | spin | labs 45113437048, 45114405201 | 11-23 | | `pci_save_state()` at a device's add kept only where an error handler can restore it (#591 a, a kernel patch on branch `exp/591a-no-save-state`) | 128 MMIO exits fewer, the host's handling −3.3 [−6.2, −2.1] ms (3 a side, lab 45113437048), but the first login −4.3 [−6.8, +0.5] at 60 a side (lab 45114405201) and −1.3 [−3.5, +1.3] at 120 (lab 45115330128). Its gate has to grow by hand: upstream a2f1e22390ac took out `pci_restore_state()`'s refusal of a state never saved | spin | labs 45113437048, 45114405201, 45115330128 | 11-23 | | virtio-pci's vendor capabilities walked once per device (#591 b, a kernel patch on branch `exp/591b-virtio-caps-once`) | 114 MMIO exits fewer (3 a side, lab 45113437048). The first login −5.8 [−8.2, −0.6] at 60 a side (lab 45114405201), three times what 114 exits can cost, and +0.9 [−1.8, +2.7] at 120 (lab 45115330128) | spin | labs 45113437048, 45114405201, 45115330128 | 11-23 | -| The IOAPIC's redirection table read once at boot, and written back only where it was masked (#590, a kernel patch on branch `exp/590-ioapic-once`) | IOAPIC accesses 589 → 205 and the host's handling −3.1 [−6.2, −1.1] ms (3 a side, lab 45113437048), but the first login −4.5 [−7.8, +0.4] at 60 a side (lab 45114405201) and −2.0 [−4.3, +0.6] at 120 (lab 45115330128). With the four other exit patches of #591 and #592, as a bound, −7.4 [−12.1, −2.8] (#627) | spin | labs 45113437048, 45114405201, 45115330128 | 11-23 | +| The IOAPIC's redirection table read once at boot, and written back only where it was masked (#590, a kernel patch on branch `exp/590-ioapic-once`) | IOAPIC accesses 589 → 205 and the host's handling −3.1 [−6.2, −1.1] ms (3 a side, lab 45113437048), but the first login −4.5 [−7.8, +0.4] at 60 a side (lab 45114405201) and −2.0 [−4.3, +0.6] at 120 (lab 45115330128). With the four other exit patches of #591 and #592, as a bound, −7.4 [−12.1, −2.8], and as one change −9.2 [−12.2, −6.0] at 120 a side: carried as 0021-0025 (#627, "What the machine carries") | spin | labs 45113437048, 45114405201, 45115330128 | 11-23 | ### Firmware, QEMU and the machine diff --git a/docs/log.md b/docs/log.md index d9f5dbe5..f793ec73 100644 --- a/docs/log.md +++ b/docs/log.md @@ -666,6 +666,7 @@ Checked against the probe it replaced on the bench fleet, `lab dispatch ssh-firs **11-23 · qemu · a bzImage mapped, not read into QEMU's heap (#646, labs 45182382388, 45182382864, 45183618987, this change).** `x86_load_linux` read the whole 16.7 MB bzImage into a buffer that only fw_cfg reads, 6.2-6.9 ms of QEMU's init traced (labs 45177818419, 45177817516, #611), and qboot copies the same bytes again by fw_cfg DMA. `qemu/patches/0007` maps the file read-only (`g_mapped_file_new_from_fd`), as upstream has mapped the initrd since 4.2; a kernel with a dtb appended keeps its buffer. The setup header QEMU patches is already a copy of its own, so nothing writes to the mapping. The QEMU build asserts that a held TCG machine's maps name a stub bzImage given to it; the published QEMU fails that check. *Prior art* is in docs/boot-time.md's row: Garzarella's 2019 series mapped the ELF kernel and the initrd but not the bzImage. *Measured* (bench, nested, spin's shape and login, v20261123.05 against the same release with the patched QEMU, parts run 45180746171, 40 a side, interleaved): first login 380 → 367, −13.4 [−16.9, −7.3], sshd accepting −13.3 [−18.7, −7.5], the console prompt −10.6 [−14.2, −6.3]; QEMU's anonymous RSS −15.9 MiB and its file RSS +16.1, total unmoved (lab 45182382388). Restored, 40 a side: login −9.3 [−11.2, −6.1] evicted and −7.7 [−8.9, −5.1] cached, `running` −8.8 and −7.9, the state unmoved (lab 45182382864): a restore starts QEMU, which loads the kernel into fw_cfg there too. The gain is twice the traced read; what else the anonymous buffer cost nested (its page faults, presumably) was not measured. *A lead, not a decision:* an uncompressed payload (#326's kernel patch, renumbered, branch `exp/646-uncompressed`, parts run 45180892535) against LZ4, both on the patched QEMU: first login −4.0 [−7.9, +0.3], within noise at 40, QEMU's RSS +32 MiB (lab 45183618987). The mapping removes the "+6-7 in QEMU's start" half of #326's rejection; whether the rest is a gain needs more boots, which is #645's. *Status:* in place; the record does not move, as the kernel reaches the guest through fw_cfg. **11-23 · machine, qemu · QEMU's start and the held start's QMP, split and rejected (#647, #649, labs 45177818419, 45230790902, 45231358105, #669, this change).** #611's split left two cold phases without an owner. *#649, the held start's 14 commands, 6.1 ms on bench:* QEMU waited 4.5 ms p50 for the client, 0.9 of it busy with the probe's plug monitor (the memory spin sizes right after the start), so spin-machine's own share is ~3.6 ms, ~0.28 ms a round trip (24 boots, lab 45177818419). The candidate sent the bind ahead of its answers (`qmp.Send`): the run state with the places, every serial at once, each image's two nodes together, the reopen with the placeholder's delete, 13 round trips to 9; add-fd still holds the connection until its answer and `cont` waits for the bind. *Measured* (bench, nested, spin's shape and login, v20261123.06's parts, spin-machine main at f273377 against the candidate at c966407, 60 a side): first login −1.7 [−5.6, +2.6], sshd accepting −2.4 [−5.5, +2.8] (lab 45230790902); restored login −0.3 [−3.8, +3.7] evicted, +0.9 [−10.6, +7.5] cached (lab 45231358105). Rejected: the four round trips it removes are ~1.1 ms (inferred), and even all of spin-machine's 3.6 ms is under what 60 boots a side resolve. The code is on `archive/exp-649-held-start`. *#647, QEMU's exec to its first trace event, 9.7 ms on bench:* the release's QEMU is static-pie on musl, so there is no dynamic loader and `LD_DEBUG` has nothing to report; musl relocates its 59,700 relative relocations itself. The split, on the laptop as a lead (exec to the first `flatview_new`, 40 starts a row, 4.7-5.4 ms p50): a trivial static-pie binary 0.23, QEMU's `-version` 2.0, `-sandbox` −0.7 to −0.9, the rest option parsing, trace and main-loop start and the machine object. perf on an unstripped build of the same Dockerfile: relocation 7% of `-version`, page faults ~15%, nothing else over 2%. No candidate's ceiling is over ~2 ms on bench (inferred at ~2× the laptop): a seccomp filter compiled at build time (Firecracker's seccompiler), a non-PIE binary (which gives up ASLR), DT_RELR. None was built. The allocator, musl's mallocng, is ~2.3 of the 17 ms of CPU QEMU's main thread spends from exec to held, a lead across QEMU's whole start, opened as #669. *Status:* rejected, both. +**11-23 · kernel · the boot's device-setup exits as one change: kernel 0021-0025 (#627, parts run 45228777973, labs 45231943664, 45232800577, this change).** *Why:* five patches each removed the VM exits their changelogs counted (#590, #591, #592), and none moved the first login alone; as a bound the five read −7.4 [−12.1, −2.8] at 60 a side (lab 45114405201, 11-23 above). #627 asked whether "the boot's MMIO exits in device setup" is one change and, if so, for one arm at 120 a side, the set chosen before the run. *Changed:* the bound's five, from `archive/exp-batch2-bound`, applied to 7.3.5 unchanged and remade with `git format-patch` as 0021-0025 (0014-0020 are #645's). The build asserts each on its vmlinux (kernel/README.md); checkpatch `--strict` and sparse pass. 0022 keeps the risk #627 named: upstream a2f1e22390ac took out `pci_restore_state()`'s refusal of a state never saved, so its gate (AER, EEH, s390, Xen pcifront, `reset_root_port`) has to grow by hand with whatever calls error handlers next; it was kept in the set because the set was fixed before the run. *Measured* (bench, nested, spin's shape and login, against v20261123.06, interleaved): first login −9.2 [−12.2, −6.0], sshd accepting −9.0 [−12.4, −6.7], console prompt −11.9 [−15.2, −8.1], second login −0.0 [−0.3, +0.3], QEMU RSS +0.1 MiB within noise, 120 a side (lab 45231943664). `restore-first`, 30 a side (lab 45232800577): restored login −0.6 [−2.3, +1.6] evicted and −0.7 [−1.5, +0.8] cached, the state +0.3 MiB [−0.1, +0.6], all within noise; its cold row −17.1 [−23.6, −10.8]. *Status:* in place; reaches the goal number with the next release. ## 2. Rework cycles diff --git a/kernel/Dockerfile b/kernel/Dockerfile index 4132e7fd..27d84cfb 100644 --- a/kernel/Dockerfile +++ b/kernel/Dockerfile @@ -461,7 +461,8 @@ RUN <:" || { echo "ERROR: no function $f in vmlinux; the check that names it reads nothing"; failed=1; } done @@ -513,6 +514,30 @@ RUN <' || lost 0021 "enable_IO_APIC still reads every pin again through clear_IO_APIC" + + # 0022: a device's state is saved at add only where something can restore it unsaved; the + # check of the root bus's host bridge is the gate's, and the call is there only with it. + dis pci_bus_add_device | grep -q '' || lost 0022 "pci_bus_add_device saves every device's state" + + # 0023: virtio-pci's vendor capabilities are walked once, not once for each of four types, + # so vp_modern_probe starts one walk. + [ "$(dis vp_modern_probe | grep -c '')" = 1 ] || + lost 0023 "vp_modern_probe walks the vendor capabilities once for each type" + + # 0024: queue_select is written only when the queue changes. The helper is static and + # inlined into every accessor, so the source that was compiled is read. + grep -qF 'mdev->selected_queue == index' drivers/virtio/virtio_pci_modern_dev.c || + lost 0024 "virtio-pci writes queue_select before every queue field" + + # 0025: msi_set_affinity() returns before the message write when the vector and the + # destination did not change. A comparison in its machine code looks like any other, so the + # source that was compiled is read. + grep -qF 'cfg->dest_apicid == old_cfg.dest_apicid' arch/x86/kernel/apic/msi.c || + lost 0025 "msi_set_affinity writes an unchanged message" + [ "$failed" = 0 ] || exit 1 echo "kernel patches: each is in this vmlinux" EOT diff --git a/kernel/README.md b/kernel/README.md index def81582..9c094fbf 100644 --- a/kernel/README.md +++ b/kernel/README.md @@ -139,6 +139,11 @@ every kernel bump. | 0010 | `early_quirks` calls `kvm_para_available` before its scan | | 0012 | `btf_vmlinux_defer` is in the vmlinux, and the compiled `kernel/bpf/btf.c` defers registrations through it | | 0013 | the compiled `kernel/bpf/verifier.c` asks for vmlinux BTF through `need_btf_vmlinux(env)` | +| 0021 | `enable_IO_APIC` does not call `clear_IO_APIC`: each pin is cleared from the read its ExtINT search made | +| 0022 | `pci_bus_add_device` calls `pci_find_host_bridge`, the gate on its state save | +| 0023 | `vp_modern_probe` calls `pci_find_capability` once | +| 0024 | the compiled `drivers/virtio/virtio_pci_modern_dev.c` writes `queue_select` only when `selected_queue` differs | +| 0025 | the compiled `arch/x86/kernel/apic/msi.c` compares the vector and the destination before the message write | None of them is a guest check. What the patches save is CPU and VM exits, which a guest cannot count, and the format files 0003 defers are behind lockdown. What 0012 and 0013 save is a parse of diff --git a/kernel/patches/0021-x86-ioapic-Skip-the-boot-time-RTE-accesses-that-chan.patch b/kernel/patches/0021-x86-ioapic-Skip-the-boot-time-RTE-accesses-that-chan.patch new file mode 100644 index 00000000..7f9d5028 --- /dev/null +++ b/kernel/patches/0021-x86-ioapic-Skip-the-boot-time-RTE-accesses-that-chan.patch @@ -0,0 +1,239 @@ +From: Manuel de Brito Fontes +Date: Mon, 23 Nov 2026 14:00:00 -0300 +Subject: [PATCH] x86/ioapic: Skip the boot-time RTE accesses that change + nothing + +Before the first initcall a 64-bit boot goes through the IO-APIC +redirection table several times. enable_IR_x2apic() saves every entry, +masks those that are not masked and, when interrupt remapping was not +enabled, writes every saved entry back. enable_IO_APIC() then reads +every entry to look for the i8259's ExtINT pin, and clear_IO_APIC() +reads each again, masks it with every other bit cleared, and reads it a +third time to check its remote IRR. Each 32-bit register access is two +MMIO accesses, the index written to IOREGSEL and the data read or +written at IOWIN, so an entry costs four MMIO accesses to read or to +write. In a guest each MMIO access is a VM exit. + +On a QEMU q35 guest without an IOMMU (24 pins, all reset masked) that +is 96 accesses for the save, 96 for the restore, 96 for the ExtINT +search and 288 for the clear: 576 exits, counted by guest RIP. + +Remove the three passes that change nothing: + + - mask_ioapic_entries() returns whether it masked anything, and + enable_IR_x2apic() writes the saved entries back only if it did. + Nothing else writes an entry between the save and the restore, so + when nothing was masked every entry already holds what was saved. + When an entry was masked, the restore is unchanged. + + - enable_IO_APIC() reads each entry once and uses it for both the + ExtINT search and the clear. Clearing a pin changes neither the mask + nor the delivery mode of another, which is all the search looks at, + and the first ExtINT pin is still the one recorded. + + - clear_IO_APIC_pin() returns after its first read when the entry + already reads as what it leaves behind: masked, every other bit zero, + no remote IRR and no delivery pending. Writing that value again and + reading it back can change nothing. Any other entry, an SMI pin, or + one whose reserved bits read nonzero, goes through the clear as + before. + +The entries are still read: nothing assumes their reset state, which a +kexec'd or crash kernel, or other firmware, does not leave. On the guest +above the boot makes 96 accesses for the save and 96 for the single +read in enable_IO_APIC(), 384 fewer exits (counted from the source; +IO-APIC accesses measured 589 -> 205 a boot). At ~6.7 us a nested exit +that is ~2.6 ms of a nested cold boot (inferred). + +The suspend and resume paths are unchanged: resume still restores every +entry, since the IO-APIC lost them. Reboot, kexec and crash, which call +clear_IO_APIC(), skip the write and the check on an entry already +clear. + +Measured on the bench fleet (nested KVM, Xeon 6975P-C, one vCPU, the +workspace's shape), alone this patch moved the first SSH login by less +than the noise. With the other four patches that remove exits from +device setup (the IO-APIC table, the state save at add, virtio's +capabilities, queue_select, the MSI message), the cold first login was +9.2 ms faster, 95% interval [6.0, 12.2], over 120 interleaved boots a +side (2026-11-23), and a restored guest's login did not move. + +Local patch for this machine. Not upstream. + +Assisted-by: LLM +--- + arch/x86/include/asm/io_apic.h | 4 ++-- + arch/x86/kernel/apic/apic.c | 10 ++++++-- + arch/x86/kernel/apic/io_apic.c | 43 +++++++++++++++++++++++++--------- + 3 files changed, 42 insertions(+), 15 deletions(-) + +diff --git a/arch/x86/include/asm/io_apic.h b/arch/x86/include/asm/io_apic.h +index 0d806513c..56586e0d9 100644 +--- a/arch/x86/include/asm/io_apic.h ++++ b/arch/x86/include/asm/io_apic.h +@@ -136,7 +136,7 @@ extern void ioapic_insert_resources(void); + extern int arch_early_ioapic_init(void); + + extern int save_ioapic_entries(void); +-extern void mask_ioapic_entries(void); ++bool mask_ioapic_entries(void); + extern int restore_ioapic_entries(void); + + extern void setup_ioapic_ids_from_mpc(void); +@@ -197,7 +197,7 @@ static inline int save_ioapic_entries(void) + return -ENOMEM; + } + +-static inline void mask_ioapic_entries(void) { } ++static inline bool mask_ioapic_entries(void) { return false; } + static inline int restore_ioapic_entries(void) + { + return -ENOMEM; +diff --git a/arch/x86/kernel/apic/apic.c b/arch/x86/kernel/apic/apic.c +index 90025451a..f867881e6 100644 +--- a/arch/x86/kernel/apic/apic.c ++++ b/arch/x86/kernel/apic/apic.c +@@ -1907,6 +1907,7 @@ void __init enable_IR_x2apic(void) + { + unsigned long flags; + int ret, ir_stat; ++ bool masked; + + if (ioapic_is_disabled) { + pr_info("Not enabling interrupt remapping due to skipped IO-APIC setup\n"); +@@ -1925,7 +1926,7 @@ void __init enable_IR_x2apic(void) + + local_irq_save(flags); + legacy_pic->mask_all(); +- mask_ioapic_entries(); ++ masked = mask_ioapic_entries(); + + /* If irq_remapping_prepare() succeeded, try to enable it */ + if (ir_stat >= 0) +@@ -1933,7 +1934,12 @@ void __init enable_IR_x2apic(void) + /* ir_stat contains the remap mode or an error code */ + try_to_enable_x2apic(ir_stat); + +- if (ir_stat < 0) ++ /* ++ * Only the mask has written an entry since the save. When it had ++ * nothing to mask, every entry still holds what was saved, and ++ * writing it back would only cost two accesses a register. ++ */ ++ if (ir_stat < 0 && masked) + restore_ioapic_entries(); + legacy_pic->restore_mask(); + local_irq_restore(flags); +diff --git a/arch/x86/kernel/apic/io_apic.c b/arch/x86/kernel/apic/io_apic.c +index 7d7175d01..4e7c3a1cc 100644 +--- a/arch/x86/kernel/apic/io_apic.c ++++ b/arch/x86/kernel/apic/io_apic.c +@@ -465,15 +465,24 @@ static void eoi_ioapic_pin(int vector, struct mp_chip_data *data) + __eoi_ioapic_pin(entry->apic, entry->pin, vector); + } + +-static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin) ++static void clear_IO_APIC_entry(unsigned int apic, unsigned int pin, ++ struct IO_APIC_route_entry entry) + { +- struct IO_APIC_route_entry entry; ++ struct IO_APIC_route_entry cleared = { .masked = true }; + + /* Check delivery_mode to be sure we're not clearing an SMI pin */ +- entry = ioapic_read_entry(apic, pin); + if (entry.delivery_mode == APIC_DELIVERY_MODE_SMI) + return; + ++ /* ++ * An entry that already reads as what the clear leaves behind, ++ * masked with no remote IRR and nothing pending, has nothing to ++ * clear and nothing to check afterwards. A virtual IO-APIC resets ++ * every entry to this, and there each access skipped is a VM exit. ++ */ ++ if (entry.w1 == cleared.w1 && entry.w2 == cleared.w2) ++ return; ++ + /* + * Make sure the entry is masked and re-read the contents to check + * if it is a level triggered pin and if the remote-IRR is set. +@@ -509,6 +518,11 @@ static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin) + mpc_ioapic_id(apic), pin); + } + ++static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin) ++{ ++ clear_IO_APIC_entry(apic, pin, ioapic_read_entry(apic, pin)); ++} ++ + void clear_IO_APIC (void) + { + int apic, pin; +@@ -572,10 +586,11 @@ int save_ioapic_entries(void) + } + + /* +- * Mask all IO APIC entries. ++ * Mask all IO APIC entries. Returns whether any entry had to be masked. + */ +-void mask_ioapic_entries(void) ++bool mask_ioapic_entries(void) + { ++ bool masked = false; + int apic, pin; + + for_each_ioapic(apic) { +@@ -589,9 +604,11 @@ void mask_ioapic_entries(void) + if (!entry.masked) { + entry.masked = true; + ioapic_write_entry(apic, pin, entry); ++ masked = true; + } + } + } ++ return masked; + } + + /* +@@ -1279,19 +1296,26 @@ void __init enable_IO_APIC(void) + if (!nr_legacy_irqs() || !nr_ioapics) + return; + ++ /* ++ * Read each pin once, both to find the pin in ExtINT mode and to ++ * clear it: do not trust the IO-APIC being empty at bootup. Clearing ++ * a pin changes neither the mask nor the delivery mode of another, ++ * which is all the search looks at. ++ */ + for_each_ioapic_pin(apic, pin) { +- /* See if any of the pins is in ExtINT mode */ + struct IO_APIC_route_entry entry = ioapic_read_entry(apic, pin); + + /* + * If the interrupt line is enabled and in ExtInt mode I + * have found the pin where the i8259 is connected. + */ +- if (!entry.masked && entry.delivery_mode == APIC_DELIVERY_MODE_EXTINT) { ++ if (ioapic_i8259.pin == -1 && !entry.masked && ++ entry.delivery_mode == APIC_DELIVERY_MODE_EXTINT) { + ioapic_i8259.apic = apic; + ioapic_i8259.pin = pin; +- break; + } ++ ++ clear_IO_APIC_entry(apic, pin, entry); + } + + /* +@@ -1313,9 +1337,6 @@ void __init enable_IO_APIC(void) + if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) && + (i8259_pin >= 0) && (ioapic_i8259.pin >= 0)) + pr_warn("ExtINT in hardware and MP table differ\n"); +- +- /* Do not trust the IO-APIC being empty at bootup */ +- clear_IO_APIC(); + } + + void native_restore_boot_irq_mode(void) +-- +2.53.0 + diff --git a/kernel/patches/0022-PCI-Save-a-device-s-state-at-add-only-where-error-re.patch b/kernel/patches/0022-PCI-Save-a-device-s-state-at-add-only-where-error-re.patch new file mode 100644 index 00000000..d811fc54 --- /dev/null +++ b/kernel/patches/0022-PCI-Save-a-device-s-state-at-add-only-where-error-re.patch @@ -0,0 +1,103 @@ +From: Manuel de Brito Fontes +Date: Mon, 23 Nov 2026 01:26:15 -0300 +Subject: [PATCH] PCI: Save a device's state at add only where error recovery + can use it + +Commit a2f1e22390ac ("PCI/ERR: Ensure error recoverability at all +times") saves every device's configuration space in +pci_bus_add_device(), so that a driver's error handlers can restore it +after an error has already lost it, and so that pci_restore_state() +no longer needs to refuse a state that was never saved. The save reads +the first 64 bytes of configuration space a dword at a time, and the +capability state pci_save_state() keeps. + +A kernel built without anything that calls a driver's error handlers +never restores that state. Error recovery through pcie_do_recovery() +needs CONFIG_PCIEAER, which DPC and EDR depend on, and the other +callers of the handlers are EEH, s390's zPCI and Xen's PCI frontend. +Every other path that loses the state saves it first: runtime and +system suspend in pci-driver.c, hibernation's freeze, the reset +methods through pci_dev_save_and_disable() and the bus and slot resets +through pci_bus_save_and_disable_locked(). The one other restore of a +state nobody saved is pcibios_reset_secondary_bus(), which restores a +Root Port on a host bridge that provides reset_root_port(). + +Skip the save at add when none of those is built and the device is +not on such a root bus. + +Under a hypervisor every configuration read is a trap. On this +machine's QEMU q35, eight conventional PCI functions on the root bus +(the host bridge, the LPC bridge and six virtio-pci devices), the save +reads 16 dwords from each: 128 reads per boot, measured with the +function tracer. At about 6.7 us a nested exit, that is ~0.9 ms on the +boot's path. + +What it costs: with the save skipped, pci_restore_state() on a device +whose state was never saved writes zeros back over its header. In a +kernel built with none of the error recovery above, that takes a +driver that restores in a path of its own without a pci_save_state() +of its own. Of the drivers this machine's config builds, only 8250_pci +restores, and only in its error handler. A driver that does, +built into a later config, would leave its device unusable after its +own reset, so the list above has to grow with whatever calls error +handlers next. + +Measured on the bench fleet (nested KVM, Xeon 6975P-C, one vCPU, the +workspace's shape), alone this patch moved the first SSH login by less +than the noise. With the other four patches that remove exits from +device setup (the IO-APIC table, the state save at add, virtio's +capabilities, queue_select, the MSI message), the cold first login was +9.2 ms faster, 95% interval [6.0, 12.2], over 120 interleaved boots a +side (2026-11-23), and a restored guest's login did not move. + +Local patch for this machine. Not upstream. + +Assisted-by: LLM +--- + drivers/pci/bus.c | 23 ++++++++++++++++++++++- + 1 file changed, 22 insertions(+), 1 deletion(-) + +diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c +index 655ed5343..a8c3794ad 100644 +--- a/drivers/pci/bus.c ++++ b/drivers/pci/bus.c +@@ -336,6 +336,26 @@ void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { } + + void __weak pcibios_bus_add_device(struct pci_dev *pdev) { } + ++/* ++ * Whether anything can restore @dev's state without saving it first. ++ * Error recovery can: it calls a driver's error handlers once the error ++ * has lost the state, and those restore what was saved beforehand. ++ * Every other path that loses it, a power transition or a reset, ++ * saves it first. pcie_do_recovery() needs AER, which DPC and EDR ++ * depend on; EEH, s390's zPCI and Xen's PCI frontend call the handlers ++ * themselves. pcibios_reset_secondary_bus() restores a Root Port its ++ * host has reset. ++ */ ++static bool pci_dev_needs_saved_state(struct pci_dev *dev) ++{ ++ if (IS_ENABLED(CONFIG_PCIEAER) || IS_ENABLED(CONFIG_EEH) || ++ IS_ENABLED(CONFIG_S390) || IS_ENABLED(CONFIG_XEN_PCIDEV_FRONTEND)) ++ return true; ++ ++ return pci_is_root_bus(dev->bus) && ++ pci_find_host_bridge(dev->bus)->reset_root_port; ++} ++ + /** + * pci_bus_add_device - start driver for a single device + * @dev: device to add +@@ -358,7 +378,8 @@ void pci_bus_add_device(struct pci_dev *dev) + pci_bridge_d3_update(dev); + + /* Save config space for error recoverability */ +- pci_save_state(dev); ++ if (pci_dev_needs_saved_state(dev)) ++ pci_save_state(dev); + + /* + * Enable runtime PM, which potentially allows the device to +-- +2.53.0 + diff --git a/kernel/patches/0023-virtio_pci-Walk-the-vendor-capabilities-once-in-vp_m.patch b/kernel/patches/0023-virtio_pci-Walk-the-vendor-capabilities-once-in-vp_m.patch new file mode 100644 index 00000000..2c11e93d --- /dev/null +++ b/kernel/patches/0023-virtio_pci-Walk-the-vendor-capabilities-once-in-vp_m.patch @@ -0,0 +1,185 @@ +From: Manuel de Brito Fontes +Date: Mon, 23 Nov 2026 01:25:02 -0300 +Subject: [PATCH] virtio_pci: Walk the vendor capabilities once in + vp_modern_probe() + +vp_modern_probe() looks for four structures - common, ISR, notify and +device configuration - and walks the device's vendor-specific +capabilities from the start for each one, reading every capability's +cfg_type and BAR until the one it wants. QEMU lists a modern device's +capabilities in the reverse of the order it adds them: PCI +configuration access, notify, device, ISR, common. The search for the +common structure, which comes first, reads all five capabilities, and +the four searches read 28 bytes of configuration space between them. + +Walk the list once, keep the first usable capability of each of the +four types, and stop once all four are found. A capability's BAR is +read only when its type is still wanted. The result is the same: for +each type, the first capability of that type whose BAR is in use, and +the BARs of those capabilities in modern_bars. On QEMU the walk reads 9 +bytes instead of 28, the PCI configuration access capability's type +and the type and BAR of the other four. + +Under a hypervisor every configuration read is a trap. On this +machine's QEMU q35 with six virtio-pci devices it is 114 reads fewer +per boot, out of the 258 that vp_modern_probe() and +vp_modern_map_capability() make (counted from the source and QEMU's +capability order; the 258 were measured with the function tracer, and +114 fewer MMIO exits with the patch). At about 6.7 us a nested exit, +that is ~0.8 ms on the boot's path. + +It costs nothing at run time. The capabilities are read as before, +fewer times; vp_modern_map_capability() still reads each structure's +BAR again before it maps it. + +Measured on the bench fleet (nested KVM, Xeon 6975P-C, one vCPU, the +workspace's shape), alone this patch moved the first SSH login by less +than the noise. With the other four patches that remove exits from +device setup (the IO-APIC table, the state save at add, virtio's +capabilities, queue_select, the MSI message), the cold first login was +9.2 ms faster, 95% interval [6.0, 12.2], over 120 interleaved boots a +side (2026-11-23), and a restored guest's login did not move. + +Local patch for this machine. Not upstream. + +Assisted-by: LLM +--- + drivers/virtio/virtio_pci_modern_dev.c | 74 ++++++++++++++------------ + 1 file changed, 39 insertions(+), 35 deletions(-) + +diff --git a/drivers/virtio/virtio_pci_modern_dev.c b/drivers/virtio/virtio_pci_modern_dev.c +index 413a8c353..03430963b 100644 +--- a/drivers/virtio/virtio_pci_modern_dev.c ++++ b/drivers/virtio/virtio_pci_modern_dev.c +@@ -103,27 +103,36 @@ vp_modern_map_capability(struct virtio_pci_modern_device *mdev, int off, + } + + /** +- * virtio_pci_find_capability - walk capabilities to find device info. ++ * virtio_pci_find_capabilities - walk capabilities once to find device info. + * @dev: the pci device +- * @cfg_type: the VIRTIO_PCI_CAP_* value we seek ++ * @pos: offsets found, indexed by VIRTIO_PCI_CAP_* value, 0 where none is + * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO. +- * @bars: the bitmask of BARs ++ * @bars: the bitmask of BARs the capabilities found use + * +- * Returns offset of the capability, or 0. ++ * Takes, for each structure type from VIRTIO_PCI_CAP_COMMON_CFG to ++ * VIRTIO_PCI_CAP_DEVICE_CFG, the first capability of that type whose BAR ++ * is in use. A BAR is read only for a capability whose type is still ++ * wanted. + */ +-static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type, +- u32 ioresource_types, int *bars) ++static void virtio_pci_find_capabilities(struct pci_dev *dev, ++ int pos[VIRTIO_PCI_CAP_DEVICE_CFG + 1], ++ u32 ioresource_types, int *bars) + { +- int pos; ++ int cap, missing = VIRTIO_PCI_CAP_DEVICE_CFG; + +- for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); +- pos > 0; +- pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) { ++ for (cap = pci_find_capability(dev, PCI_CAP_ID_VNDR); ++ cap > 0 && missing; ++ cap = pci_find_next_capability(dev, cap, PCI_CAP_ID_VNDR)) { + u8 type, bar; +- pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap, ++ ++ pci_read_config_byte(dev, cap + offsetof(struct virtio_pci_cap, + cfg_type), + &type); +- pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap, ++ if (type < VIRTIO_PCI_CAP_COMMON_CFG || ++ type > VIRTIO_PCI_CAP_DEVICE_CFG || pos[type]) ++ continue; ++ ++ pci_read_config_byte(dev, cap + offsetof(struct virtio_pci_cap, + bar), + &bar); + +@@ -131,15 +140,13 @@ static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type, + if (bar >= PCI_STD_NUM_BARS) + continue; + +- if (type == cfg_type) { +- if (pci_resource_len(dev, bar) && +- pci_resource_flags(dev, bar) & ioresource_types) { +- *bars |= (1 << bar); +- return pos; +- } ++ if (pci_resource_len(dev, bar) && ++ pci_resource_flags(dev, bar) & ioresource_types) { ++ *bars |= (1 << bar); ++ pos[type] = cap; ++ missing--; + } + } +- return 0; + } + + /* This is part of the ABI. Don't screw with it. */ +@@ -223,7 +230,9 @@ static inline void check_offsets(void) + int vp_modern_probe(struct virtio_pci_modern_device *mdev) + { + struct pci_dev *pci_dev = mdev->pci_dev; ++ int pos[VIRTIO_PCI_CAP_DEVICE_CFG + 1] = {}; + int err, common, isr, notify, device; ++ int bars = 0; + u32 notify_length; + u32 notify_offset; + int devid; +@@ -252,23 +261,25 @@ int vp_modern_probe(struct virtio_pci_modern_device *mdev) + } + mdev->id.vendor = pci_dev->subsystem_vendor; + ++ virtio_pci_find_capabilities(pci_dev, pos, ++ IORESOURCE_IO | IORESOURCE_MEM, &bars); ++ common = pos[VIRTIO_PCI_CAP_COMMON_CFG]; ++ isr = pos[VIRTIO_PCI_CAP_ISR_CFG]; ++ notify = pos[VIRTIO_PCI_CAP_NOTIFY_CFG]; ++ /* Device capability is only mandatory for devices that have ++ * device-specific configuration. ++ */ ++ device = pos[VIRTIO_PCI_CAP_DEVICE_CFG]; ++ + /* check for a common config: if not, use legacy mode (bar 0). */ +- common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG, +- IORESOURCE_IO | IORESOURCE_MEM, +- &mdev->modern_bars); + if (!common) { + dev_info(&pci_dev->dev, + "virtio_pci: leaving for legacy driver\n"); + return -ENODEV; + } ++ mdev->modern_bars |= bars; + + /* If common is there, these should be too... */ +- isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG, +- IORESOURCE_IO | IORESOURCE_MEM, +- &mdev->modern_bars); +- notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG, +- IORESOURCE_IO | IORESOURCE_MEM, +- &mdev->modern_bars); + if (!isr || !notify) { + dev_err(&pci_dev->dev, + "virtio_pci: missing capabilities %i/%i/%i\n", +@@ -284,13 +295,6 @@ int vp_modern_probe(struct virtio_pci_modern_device *mdev) + if (err) + dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n"); + +- /* Device capability is only mandatory for devices that have +- * device-specific configuration. +- */ +- device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG, +- IORESOURCE_IO | IORESOURCE_MEM, +- &mdev->modern_bars); +- + err = pci_request_selected_regions(pci_dev, mdev->modern_bars, + "virtio-pci-modern"); + if (err) +-- +2.53.0 + diff --git a/kernel/patches/0024-virtio_pci-Write-queue_select-only-when-the-queue-ch.patch b/kernel/patches/0024-virtio_pci-Write-queue_select-only-when-the-queue-ch.patch new file mode 100644 index 00000000..aec656b7 --- /dev/null +++ b/kernel/patches/0024-virtio_pci-Write-queue_select-only-when-the-queue-ch.patch @@ -0,0 +1,197 @@ +From: Manuel de Brito Fontes +Date: Mon, 23 Nov 2026 12:00:00 -0300 +Subject: [PATCH] virtio_pci: Write queue_select only when the queue changes + +Every vp_modern_*() accessor of a queue field writes queue_select +first, and setting up a virtqueue with an MSI-X vector calls seven of +them for the same queue: its size and enable state read, its size, +addresses and vector written, its notify offset read, and, once every +queue is set up, its enable written. Under a hypervisor each access to +the common configuration is a trap to the device model, so most of +those writes are exits that change nothing. + +Remember the queue last selected in struct virtio_pci_modern_device and +write queue_select only when it changes. A reset leaves the device to +choose, so writing 0 to device_status forgets it; every path that +resets the device, a PCI function reset and a resume included, writes +0 through vp_modern_set_status() before it touches a queue. + +The MSI-X vector is still read back after it is written. That read is +how a device reports that it could not map the vector (it returns +VIRTIO_MSI_NO_VECTOR), the specification requires the driver to make +it, and no other register carries the refusal, so the driver's fallback +to shared vectors or INTx depends on it. + +Counted from the source for this machine's six devices and 13 +virtqueues, each with a vector (vsock 3, balloon 4, SCSI 3, and rng, +blk and virtio-mem 1 each): 91 writes to queue_select by the end of +probe, 23 with this patch, one per queue at setup and one per queue +of a multiqueue device when the queues are enabled. Nested on the +bench fleet an exit costs ~6.7 us, which puts the 68 removed at about +0.45 ms (91 -> 23 writes measured). + +The cost is that the driver now trusts the device to keep queue_select +between two of its own accesses, however far apart, as the +specification says it does, where before it rewrote it a few +instructions earlier. A device model that loses it without a reset, +such as a VMM restoring a snapshot without that register, would have a +later queue reset address the wrong queue. + +Measured on the bench fleet (nested KVM, Xeon 6975P-C, one vCPU, the +workspace's shape), alone this patch moved the first SSH login by less +than the noise. With the other four patches that remove exits from +device setup (the IO-APIC table, the state save at add, virtio's +capabilities, queue_select, the MSI message), the cold first login was +9.2 ms faster, 95% interval [6.0, 12.2], over 120 interleaved boots a +side (2026-11-23), and a restored guest's login did not move. + +Local patch for this machine. Not upstream. + +Assisted-by: LLM +--- + drivers/virtio/virtio_pci_modern_dev.c | 42 ++++++++++++++++++++------ + include/linux/virtio_pci_modern.h | 2 ++ + 2 files changed, 35 insertions(+), 9 deletions(-) + +diff --git a/drivers/virtio/virtio_pci_modern_dev.c b/drivers/virtio/virtio_pci_modern_dev.c +index 03430963b..88ec20eb3 100644 +--- a/drivers/virtio/virtio_pci_modern_dev.c ++++ b/drivers/virtio/virtio_pci_modern_dev.c +@@ -260,6 +260,7 @@ int vp_modern_probe(struct virtio_pci_modern_device *mdev) + } + } + mdev->id.vendor = pci_dev->subsystem_vendor; ++ mdev->selected_queue = -1; + + virtio_pci_find_capabilities(pci_dev, pos, + IORESOURCE_IO | IORESOURCE_MEM, &bars); +@@ -505,9 +506,32 @@ void vp_modern_set_status(struct virtio_pci_modern_device *mdev, + * before writing to the MMIO region. + */ + vp_iowrite8(status, &cfg->device_status); ++ ++ /* A reset selects a queue of the device's choosing. */ ++ if (!status) ++ mdev->selected_queue = -1; + } + EXPORT_SYMBOL_GPL(vp_modern_set_status); + ++/* ++ * vp_modern_select_queue - select the queue the queue fields refer to ++ * @mdev: the modern virtio-pci device ++ * @index: queue index ++ * ++ * Setting up a virtqueue reads or writes its fields seven times, each ++ * selecting the queue first. Under a hypervisor every write to ++ * queue_select is a trap to the device; write it only when it changes. ++ */ ++static void vp_modern_select_queue(struct virtio_pci_modern_device *mdev, ++ u16 index) ++{ ++ if (mdev->selected_queue == index) ++ return; ++ ++ vp_iowrite16(index, &mdev->common->queue_select); ++ mdev->selected_queue = index; ++} ++ + /* + * vp_modern_get_queue_reset - get the queue reset status + * @mdev: the modern virtio-pci device +@@ -519,7 +543,7 @@ int vp_modern_get_queue_reset(struct virtio_pci_modern_device *mdev, u16 index) + + cfg = (struct virtio_pci_modern_common_cfg __iomem *)mdev->common; + +- vp_iowrite16(index, &cfg->cfg.queue_select); ++ vp_modern_select_queue(mdev, index); + return vp_ioread16(&cfg->queue_reset); + } + EXPORT_SYMBOL_GPL(vp_modern_get_queue_reset); +@@ -535,7 +559,7 @@ void vp_modern_set_queue_reset(struct virtio_pci_modern_device *mdev, u16 index) + + cfg = (struct virtio_pci_modern_common_cfg __iomem *)mdev->common; + +- vp_iowrite16(index, &cfg->cfg.queue_select); ++ vp_modern_select_queue(mdev, index); + vp_iowrite16(1, &cfg->queue_reset); + + while (vp_ioread16(&cfg->queue_reset)) +@@ -559,7 +583,7 @@ u16 vp_modern_queue_vector(struct virtio_pci_modern_device *mdev, + { + struct virtio_pci_common_cfg __iomem *cfg = mdev->common; + +- vp_iowrite16(index, &cfg->queue_select); ++ vp_modern_select_queue(mdev, index); + vp_iowrite16(vector, &cfg->queue_msix_vector); + /* Flush the write out to device */ + return vp_ioread16(&cfg->queue_msix_vector); +@@ -600,7 +624,7 @@ void vp_modern_queue_address(struct virtio_pci_modern_device *mdev, + { + struct virtio_pci_common_cfg __iomem *cfg = mdev->common; + +- vp_iowrite16(index, &cfg->queue_select); ++ vp_modern_select_queue(mdev, index); + + vp_iowrite64_twopart(desc_addr, &cfg->queue_desc_lo, + &cfg->queue_desc_hi); +@@ -620,7 +644,7 @@ EXPORT_SYMBOL_GPL(vp_modern_queue_address); + void vp_modern_set_queue_enable(struct virtio_pci_modern_device *mdev, + u16 index, bool enable) + { +- vp_iowrite16(index, &mdev->common->queue_select); ++ vp_modern_select_queue(mdev, index); + vp_iowrite16(enable, &mdev->common->queue_enable); + } + EXPORT_SYMBOL_GPL(vp_modern_set_queue_enable); +@@ -635,7 +659,7 @@ EXPORT_SYMBOL_GPL(vp_modern_set_queue_enable); + bool vp_modern_get_queue_enable(struct virtio_pci_modern_device *mdev, + u16 index) + { +- vp_iowrite16(index, &mdev->common->queue_select); ++ vp_modern_select_queue(mdev, index); + + return vp_ioread16(&mdev->common->queue_enable); + } +@@ -650,7 +674,7 @@ EXPORT_SYMBOL_GPL(vp_modern_get_queue_enable); + void vp_modern_set_queue_size(struct virtio_pci_modern_device *mdev, + u16 index, u16 size) + { +- vp_iowrite16(index, &mdev->common->queue_select); ++ vp_modern_select_queue(mdev, index); + vp_iowrite16(size, &mdev->common->queue_size); + + } +@@ -666,7 +690,7 @@ EXPORT_SYMBOL_GPL(vp_modern_set_queue_size); + u16 vp_modern_get_queue_size(struct virtio_pci_modern_device *mdev, + u16 index) + { +- vp_iowrite16(index, &mdev->common->queue_select); ++ vp_modern_select_queue(mdev, index); + + return vp_ioread16(&mdev->common->queue_size); + +@@ -695,7 +719,7 @@ EXPORT_SYMBOL_GPL(vp_modern_get_num_queues); + static u16 vp_modern_get_queue_notify_off(struct virtio_pci_modern_device *mdev, + u16 index) + { +- vp_iowrite16(index, &mdev->common->queue_select); ++ vp_modern_select_queue(mdev, index); + + return vp_ioread16(&mdev->common->queue_notify_off); + } +diff --git a/include/linux/virtio_pci_modern.h b/include/linux/virtio_pci_modern.h +index 9a3f2fc53..c416818fb 100644 +--- a/include/linux/virtio_pci_modern.h ++++ b/include/linux/virtio_pci_modern.h +@@ -44,6 +44,8 @@ struct virtio_pci_modern_device { + int notify_map_cap; + + u32 notify_offset_multiplier; ++ /* The queue last written to queue_select, or -1 if not known */ ++ int selected_queue; + int modern_bars; + struct virtio_device_id id; + +-- +2.53.0 + diff --git a/kernel/patches/0025-x86-apic-msi-Skip-the-message-write-when-the-affinit.patch b/kernel/patches/0025-x86-apic-msi-Skip-the-message-write-when-the-affinit.patch new file mode 100644 index 00000000..9cbfc663 --- /dev/null +++ b/kernel/patches/0025-x86-apic-msi-Skip-the-message-write-when-the-affinit.patch @@ -0,0 +1,75 @@ +From: Manuel de Brito Fontes +Date: Mon, 23 Nov 2026 12:30:00 -0300 +Subject: [PATCH] x86/apic/msi: Skip the message write when the affinity change + moves nothing + +request_irq() of a PCI MSI-X interrupt writes its message twice in a +row. Activation assigns the vector and msi_domain_activate() writes the +message; irq_startup() then sets the affinity before the entry is +unmasked (IRQCHIP_AFFINITY_PRE_STARTUP), the vector domain keeps the +vector because its CPU is in the mask (assign_vector_locked() and +assign_managed_vector() return early), and msi_set_affinity() writes +the same message again. For an MSI-X entry that is three writes to the +table and a read to flush them, each an exit to the VMM in a guest. + +Return from msi_set_affinity() without writing when neither the vector +nor the destination APIC ID changed. The message is composed from those +two alone (__irq_msi_compose_msg()), and the device already holds it: +apic_set_affinity() refuses an interrupt that is not activated, and the +vector and destination of an activated interrupt change only through +activation and through this function, both of which write the message. +A device whose table was lost to a reset or a sleep state gets it back +from pci_restore_msi_state(), which writes the message the descriptor +recorded, the same one. An affinity change that moves the interrupt +writes as before, through the same paths. + +Counted from the source for this machine's six virtio devices, 19 +vectors, each requested at probe: 19 writes of four MMIO accesses to +the MSI-X table BAR, 76 exits by the first login, of which 56 were measured +gone. Nested on the bench fleet an exit costs ~6.7 us, about 0.4 ms. + +The one behaviour that changes is for a device that loses its MSI-X +table without the PCI core restoring it: an affinity change to the CPU +it already targets no longer happens to repair the entry. + +Measured on the bench fleet (nested KVM, Xeon 6975P-C, one vCPU, the +workspace's shape), alone this patch moved the first SSH login by less +than the noise. With the other four patches that remove exits from +device setup (the IO-APIC table, the state save at add, virtio's +capabilities, queue_select, the MSI message), the cold first login was +9.2 ms faster, 95% interval [6.0, 12.2], over 120 interleaved boots a +side (2026-11-23), and a restored guest's login did not move. + +Local patch for this machine. Not upstream. + +Assisted-by: LLM +--- + arch/x86/kernel/apic/msi.c | 12 ++++++++++++ + 1 file changed, 12 insertions(+) + +diff --git a/arch/x86/kernel/apic/msi.c b/arch/x86/kernel/apic/msi.c +index 66bc5d3e7..7fb7c25f8 100644 +--- a/arch/x86/kernel/apic/msi.c ++++ b/arch/x86/kernel/apic/msi.c +@@ -48,6 +48,18 @@ msi_set_affinity(struct irq_data *irqd, const struct cpumask *mask, bool force) + if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE) + return ret; + ++ /* ++ * The parent keeps the vector when its CPU is in the new mask, as ++ * it is when the interrupt starts up. The device then holds this ++ * message already: the vector and destination of an activated ++ * interrupt change only through activation and through here, and ++ * both write it. Writing it again only costs MMIO accesses to the ++ * device, each a VM exit in a guest. ++ */ ++ if (cfg->vector == old_cfg.vector && ++ cfg->dest_apicid == old_cfg.dest_apicid) ++ return ret; ++ + /* + * For non-maskable and non-remapped MSI interrupts the migration + * to a different destination CPU and a different vector has to be +-- +2.53.0 +