From 9ed5bb4cfd7fafb33f6149678d61b1cf049561c6 Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Mon, 23 Nov 2026 14:32:09 -0300 Subject: [PATCH 1/3] kernel: the boot's device-setup exits as one change, 0021-0025 (#627) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The five kernel patches of #590, #591 and #592 that each remove VM exits from device setup, carried together to be measured as one arm: alone each read within noise, as a bound the five read −7.4 [−12.1, −2.8] at 60 a side (lab 45114405201). Rebased onto 7.3.5 as pinned (they apply unchanged), remade with git format-patch, checkpatch --strict clean, and asserted on the vmlinux each goes into. The budget entry names the run that decides it. Co-Authored-By: Claude Opus 5.5 --- docs/boot-time.md | 1 + kernel/Dockerfile | 27 +- kernel/README.md | 5 + ...the-boot-time-RTE-accesses-that-chan.patch | 231 ++++++++++++++++++ ...e-s-state-at-add-only-where-error-re.patch | 95 +++++++ ...the-vendor-capabilities-once-in-vp_m.patch | 176 +++++++++++++ ...-queue_select-only-when-the-queue-ch.patch | 189 ++++++++++++++ ...p-the-message-write-when-the-affinit.patch | 67 +++++ 8 files changed, 790 insertions(+), 1 deletion(-) create mode 100644 kernel/patches/0021-x86-ioapic-Skip-the-boot-time-RTE-accesses-that-chan.patch create mode 100644 kernel/patches/0022-PCI-Save-a-device-s-state-at-add-only-where-error-re.patch create mode 100644 kernel/patches/0023-virtio_pci-Walk-the-vendor-capabilities-once-in-vp_m.patch create mode 100644 kernel/patches/0024-virtio_pci-Write-queue_select-only-when-the-queue-ch.patch create mode 100644 kernel/patches/0025-x86-apic-msi-Skip-the-message-write-when-the-affinit.patch diff --git a/docs/boot-time.md b/docs/boot-time.md index f8794867..a4d5bd7e 100644 --- a/docs/boot-time.md +++ b/docs/boot-time.md @@ -487,6 +487,7 @@ The rows are ranked by ceiling. A row stays here until it is decided, and then m | What functional items cost, last measured on another machine: BPF JIT and BTF (+21.4 ms to "Freeing", 09-07, a vmlinux through PVH, before the ftrace patches), lockdown at confidentiality (nothing told from noise, 10-01, a host at load 6-10) | cold | a few ms each, unknown in spin's shape | `ssh-first` with a `--kernel` built without each, 20 boots | open. They stay for function, so a number says what they cost and decides nothing | | The no-systemd floor, each release | a bound | how much of the cold number is systemd's to give back: 445 → 240 in spin's shape | `ssh-first` on the release with `--edit floor --append init=/usr/local/sbin/spin-floor` and `login=lane`; `releases measure` does it beside every release, and report.yml records it (docs/releases.md, "Bounds") | last measured 11-22 on v20261122.02: the lane's first login 407 → 241, −166.3 [−174.7, −160.3] (report.yml run 45018301368, 20 boots). On v20261122.01: 404 → 250, −153.9 [−162.5, −151.2], sshd accepting 242 → 197 (lab 44978079694, 20 boots). 11-20: 445 → 240 (lab 44750322384) | | A workspace's cgroup at a higher `cpu.weight` while it boots or restores, on a busy host (spin's runner owns the cgroup) | restore, as a busy host sees it | with 16 workspaces keeping the 16 CPUs busy, the restored first login is 306 ms against 115 idle, and QEMU's threads wait 282 ms for a CPU (lab 44925573288). The same weight ratio as QEMU at nice −10, set as the crowd at nice 10: restored −79.3 [−93.5, −71.0], cold −104.7 [−258.3, +150.9], within noise (lab 44927312303) | spin's `restore-together` or a crowd on its runner, with the weight raised through the workspace's cgroup and set back after the login, against without it | a proposal for spin. Measured in one cgroup, where each thread competes alone. In spin each workspace is a cgroup, and the weight is shared by its threads. The idle host has nothing to win (Measured and rejected, "Memory and the host") | +| The boot's device-setup exits as one change: kernel 0021-0025, the five exit patches of #590, #591 and #592 (IOAPIC table read once, the state save at add gated, virtio's capabilities walked once, `queue_select` written on a change, an unchanged MSI message not rewritten) (#627) | cold | the kernel's clock to "Freeing", 79.9: the five remove 838 exits, 366 of them MMIO, and the host's handling −5.2 [−11.2, +1.2] ms (3 a side, lab 45114428660); as a bound, first login −7.4 [−12.1, −2.8] at 60 a side (lab 45114405201). Alone each was within noise ("Measured and rejected", "Kernel and its command line"). Prior art in #590, #591 and #592 | `ssh-first`, the five as one arm against v20261123.06, 120 a side; `restore-first` beside it, evicted, 30 a side, for a restore made no worse. The set is the bound's five, chosen before the run | open, #627 | | The restore with more than 2 GiB of RAM | restore | unmeasured (#242's checklist) | `restore-first` with `plugged` above 2048 | open | | A workspace's 2048 MiB at boot (`memory=2048 plugged=0`) instead of 128 plugged to 2048 through virtio-mem during the boot (#561) | cold | the plug and the onlining of 2048 MiB on the one vCPU while the boot runs; separate runs read −15 nested and −8 metal (v20261122.06, labs 45074465204, 45074579100, 45074680619, 45078339515), so the ceiling is ~15 ms. Prior art: the kernel onlines hotplugged memory strictly in sequence, one memory block after another, and deferred struct page init, which spreads the boot's own memory over threads, does not apply to it (David Hildenbrand on linux-mm, [lore](https://lore.kernel.org/linux-mm/87v9disj2s.fsf@oracle.com/T/)). Squeezy (arXiv 2411.12893, 2024) measured virtio-mem's operations in seconds at GiB sizes and found init_on_free slows a plug. Neither measured a boot. The kernel has init_on_alloc and init_on_free off | `ssh-first`, the two shapes as two trees of one run (`--opt` on a tree), 40 a side on bench and on bench-metal, `restore-first` beside them; 3 ledgers of each shape for the onlining's CPU | a proposal for spin, spin-stack/spin#1660. Measured as two trees of one run (v20261123.04, spin's login): cold first login −5.5 [−10.9, −0.7] nested at 80 a side (lab 45111507862; −2.5 [−7.3, +2.0] at 40, lab 45110554589), −7.0 [−11.4, −3.4] on metal at 40 (lab 45110570912); restored login −2.5 [−3.7, −1.0], the state −17.6 MiB, QEMU's RSS −26 MiB (labs 45110562994, 45111507862). The ledger (lab 45110580480, 3 boots each) shows `kworker/0:1` on the vCPU ~8 ms before PID 1 when plugged and not at all when the memory is at boot, which costs the kernel's own setup ~4.5 ms more: PID 1 −3.4 ms. The probes keep spin's shape until spin changes it | diff --git a/kernel/Dockerfile b/kernel/Dockerfile index d4dd27b3..0d9d02e7 100644 --- a/kernel/Dockerfile +++ b/kernel/Dockerfile @@ -464,7 +464,8 @@ RUN <:" || { echo "ERROR: no function $f in vmlinux; the check that names it reads nothing"; failed=1; } done @@ -564,6 +565,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 883148d7..9531fb8b 100644 --- a/kernel/README.md +++ b/kernel/README.md @@ -168,6 +168,11 @@ every kernel bump. | 0018 | the compiled decompressor reads its relocations from `.data.reloc` | | 0019 | the compiled `arch/x86/boot/startup/kaslr.c` sets `KASLR_FLAG` | | 0020 | the compiled `arch/x86/boot/startup/kaslr.c` reads RDRAND from CPUID leaf 1's ECX | +| 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..c84e38cc --- /dev/null +++ b/kernel/patches/0021-x86-ioapic-Skip-the-boot-time-RTE-accesses-that-chan.patch @@ -0,0 +1,231 @@ +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, not +yet measured). 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. + +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..1c9441e5 --- /dev/null +++ b/kernel/patches/0022-PCI-Save-a-device-s-state-at-add-only-where-error-re.patch @@ -0,0 +1,95 @@ +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. + +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..2cf31906 --- /dev/null +++ b/kernel/patches/0023-virtio_pci-Walk-the-vendor-capabilities-once-in-vp_m.patch @@ -0,0 +1,176 @@ +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). 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. + +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..fa0a482a --- /dev/null +++ b/kernel/patches/0024-virtio_pci-Write-queue_select-only-when-the-queue-ch.patch @@ -0,0 +1,189 @@ +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, not yet 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. + +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..3e00f2c4 --- /dev/null +++ b/kernel/patches/0025-x86-apic-msi-Skip-the-message-write-when-the-affinit.patch @@ -0,0 +1,67 @@ +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. Nested on the bench +fleet an exit costs ~6.7 us, about 0.5 ms, not yet measured. + +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. + +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 + From 4ee59c59e00a251751d0165f166c1d368677df94 Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Mon, 23 Nov 2026 17:09:09 -0300 Subject: [PATCH 2/3] kernel: 0021-0025's changelogs say what each removed and was measured (#627) Each changelog now gives the exits measured gone and what the five together did to the first login, measured against v20261123.06. Co-Authored-By: Claude Opus 5.5 --- ...Skip-the-boot-time-RTE-accesses-that-chan.patch | 14 +++++++++++--- ...device-s-state-at-add-only-where-error-re.patch | 8 ++++++++ ...Walk-the-vendor-capabilities-once-in-vp_m.patch | 13 +++++++++++-- ...Write-queue_select-only-when-the-queue-ch.patch | 10 +++++++++- ...i-Skip-the-message-write-when-the-affinit.patch | 12 ++++++++++-- 5 files changed, 49 insertions(+), 8 deletions(-) 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 index c84e38cc..7f9d5028 100644 --- 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 @@ -41,15 +41,23 @@ Remove the three passes that change nothing: 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, not -yet measured). At ~6.7 us a nested exit that is ~2.6 ms of a nested -cold boot (inferred). +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 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 index 1c9441e5..d811fc54 100644 --- 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 @@ -42,6 +42,14 @@ 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 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 index 2cf31906..2c11e93d 100644 --- 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 @@ -24,13 +24,22 @@ 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). At -about 6.7 us a nested exit, that is ~0.8 ms on the boot's path. +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 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 index fa0a482a..aec656b7 100644 --- 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 @@ -28,7 +28,7 @@ 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, not yet measured. +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 @@ -37,6 +37,14 @@ 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 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 index 3e00f2c4..9cbfc663 100644 --- 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 @@ -25,13 +25,21 @@ 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. Nested on the bench -fleet an exit costs ~6.7 us, about 0.5 ms, not yet measured. +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 From 0c8c4a558309005757814cf88cb5f9edf29aa6fe Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Mon, 23 Nov 2026 17:47:30 -0300 Subject: [PATCH 3/3] =?UTF-8?q?kernel:=200021-0025=20kept,=20the=20first?= =?UTF-8?q?=20login=20=E2=88=929.4=20ms=20at=20120=20a=20side=20(#627)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As one change against v20261123.07 (bench, spin's shape and login): the first login −9.4 [−14.2, −5.7] and sshd accepting −9.5 [−14.4, −5.5] at 120 a side (lab 45260781177); the restored login −0.4 [−2.0, +1.7] evicted and +0.2 [−6.6, +7.3] cached, 60 a side (lab 45261629539). The row moves to "What the machine carries", the log has its entry, and each changelog carries the .07 numbers. Co-Authored-By: Claude Opus 5.5 --- docs/boot-time.md | 4 ++-- docs/log.md | 2 ++ ...x86-ioapic-Skip-the-boot-time-RTE-accesses-that-chan.patch | 2 +- ...PCI-Save-a-device-s-state-at-add-only-where-error-re.patch | 2 +- ...virtio_pci-Walk-the-vendor-capabilities-once-in-vp_m.patch | 2 +- ...virtio_pci-Write-queue_select-only-when-the-queue-ch.patch | 2 +- ...x86-apic-msi-Skip-the-message-write-when-the-affinit.patch | 2 +- 7 files changed, 9 insertions(+), 7 deletions(-) diff --git a/docs/boot-time.md b/docs/boot-time.md index a4d5bd7e..8ec6037d 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 | against v20261123.07: first login −9.4 [−14.2, −5.7], sshd accepting −9.5 [−14.4, −5.5], 120 a side (lab 45260781177); restored −0.4 [−2.0, +1.7] evicted and +0.2 [−6.6, +7.3] cached, the state +0.0 MiB [−0.1, +0.2], 60 a side (lab 45261629539); 838 exits fewer, 366 of them MMIO (3 a side, lab 45114428660) | measured | labs 45260781177, 45261629539 | | `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 | since #645 the compression of the shipped bzImage only, which this machine boots for a release before it (#660). Before, 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 | @@ -349,7 +350,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.4 [−14.2, −5.7] 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 @@ -487,7 +488,6 @@ The rows are ranked by ceiling. A row stays here until it is decided, and then m | What functional items cost, last measured on another machine: BPF JIT and BTF (+21.4 ms to "Freeing", 09-07, a vmlinux through PVH, before the ftrace patches), lockdown at confidentiality (nothing told from noise, 10-01, a host at load 6-10) | cold | a few ms each, unknown in spin's shape | `ssh-first` with a `--kernel` built without each, 20 boots | open. They stay for function, so a number says what they cost and decides nothing | | The no-systemd floor, each release | a bound | how much of the cold number is systemd's to give back: 445 → 240 in spin's shape | `ssh-first` on the release with `--edit floor --append init=/usr/local/sbin/spin-floor` and `login=lane`; `releases measure` does it beside every release, and report.yml records it (docs/releases.md, "Bounds") | last measured 11-22 on v20261122.02: the lane's first login 407 → 241, −166.3 [−174.7, −160.3] (report.yml run 45018301368, 20 boots). On v20261122.01: 404 → 250, −153.9 [−162.5, −151.2], sshd accepting 242 → 197 (lab 44978079694, 20 boots). 11-20: 445 → 240 (lab 44750322384) | | A workspace's cgroup at a higher `cpu.weight` while it boots or restores, on a busy host (spin's runner owns the cgroup) | restore, as a busy host sees it | with 16 workspaces keeping the 16 CPUs busy, the restored first login is 306 ms against 115 idle, and QEMU's threads wait 282 ms for a CPU (lab 44925573288). The same weight ratio as QEMU at nice −10, set as the crowd at nice 10: restored −79.3 [−93.5, −71.0], cold −104.7 [−258.3, +150.9], within noise (lab 44927312303) | spin's `restore-together` or a crowd on its runner, with the weight raised through the workspace's cgroup and set back after the login, against without it | a proposal for spin. Measured in one cgroup, where each thread competes alone. In spin each workspace is a cgroup, and the weight is shared by its threads. The idle host has nothing to win (Measured and rejected, "Memory and the host") | -| The boot's device-setup exits as one change: kernel 0021-0025, the five exit patches of #590, #591 and #592 (IOAPIC table read once, the state save at add gated, virtio's capabilities walked once, `queue_select` written on a change, an unchanged MSI message not rewritten) (#627) | cold | the kernel's clock to "Freeing", 79.9: the five remove 838 exits, 366 of them MMIO, and the host's handling −5.2 [−11.2, +1.2] ms (3 a side, lab 45114428660); as a bound, first login −7.4 [−12.1, −2.8] at 60 a side (lab 45114405201). Alone each was within noise ("Measured and rejected", "Kernel and its command line"). Prior art in #590, #591 and #592 | `ssh-first`, the five as one arm against v20261123.06, 120 a side; `restore-first` beside it, evicted, 30 a side, for a restore made no worse. The set is the bound's five, chosen before the run | open, #627 | | The restore with more than 2 GiB of RAM | restore | unmeasured (#242's checklist) | `restore-first` with `plugged` above 2048 | open | | A workspace's 2048 MiB at boot (`memory=2048 plugged=0`) instead of 128 plugged to 2048 through virtio-mem during the boot (#561) | cold | the plug and the onlining of 2048 MiB on the one vCPU while the boot runs; separate runs read −15 nested and −8 metal (v20261122.06, labs 45074465204, 45074579100, 45074680619, 45078339515), so the ceiling is ~15 ms. Prior art: the kernel onlines hotplugged memory strictly in sequence, one memory block after another, and deferred struct page init, which spreads the boot's own memory over threads, does not apply to it (David Hildenbrand on linux-mm, [lore](https://lore.kernel.org/linux-mm/87v9disj2s.fsf@oracle.com/T/)). Squeezy (arXiv 2411.12893, 2024) measured virtio-mem's operations in seconds at GiB sizes and found init_on_free slows a plug. Neither measured a boot. The kernel has init_on_alloc and init_on_free off | `ssh-first`, the two shapes as two trees of one run (`--opt` on a tree), 40 a side on bench and on bench-metal, `restore-first` beside them; 3 ledgers of each shape for the onlining's CPU | a proposal for spin, spin-stack/spin#1660. Measured as two trees of one run (v20261123.04, spin's login): cold first login −5.5 [−10.9, −0.7] nested at 80 a side (lab 45111507862; −2.5 [−7.3, +2.0] at 40, lab 45110554589), −7.0 [−11.4, −3.4] on metal at 40 (lab 45110570912); restored login −2.5 [−3.7, −1.0], the state −17.6 MiB, QEMU's RSS −26 MiB (labs 45110562994, 45111507862). The ledger (lab 45110580480, 3 boots each) shows `kworker/0:1` on the vCPU ~8 ms before PID 1 when plugged and not at all when the memory is at boot, which costs the kernel's own setup ~4.5 ms more: PID 1 −3.4 ms. The probes keep spin's shape until spin changes it | diff --git a/docs/log.md b/docs/log.md index 391ae3a3..335f6f29 100644 --- a/docs/log.md +++ b/docs/log.md @@ -673,6 +673,8 @@ Checked against the probe it replaced on the bench fleet, `lab dispatch ssh-firs **11-23 · release · v20261123.07 holds #645's cold gain; its record's 20 a side did not see it (labs 45252584128, 45219785493, release run 45241645340, #694).** *Why:* the record read the cold first login at −3.5 [−12.9, +2.9], within noise, against #645's −16.8 [−20.9, −13.0], and spin was about to be told what the bump gains. *Ruled out* (read from the artefacts): the lab arm and the release boot the same machine. QEMU (`f271a946…`), qboot, `pvh.bin` and `linuxboot_dma.bin` are byte-identical, `spin-machine args` prints one command line but for paths, and both vmlinux files are stripped, 40,165,128 bytes, with the same two LOAD segments and the same config; the release's adds 0020. QEMU's RSS moves the same in both (anonymous −34.0 MiB, file +19.9/+20.1). *Measured* (bench, nested, spin's shape and login, both releases as published, each with its own spin-machine, 60 a side, interleaved): first login 378 → 359, −18.6 [−23.6, −14.0], sshd accepting −15.3 [−21.2, −12.6], the console prompt −18.7 [−24.0, −12.8] (lab 45252584128). The record's run disagreed with itself: its `restore-first` cold boot, the same boot at 40 a side, read −14.2 [−19.9, −8.6]; its 20 `.07` boots sat at a p50 of 370 where the lab's sit at 352-359, and 20 a side drawn from lab 45219785493's samples reach −3.5 in 5 of 100,000 draws. *Status:* the gain stands at ~−17 to −19 ms cold. How many boots the record takes is #694. +**11-23 · kernel · the boot's device-setup exits as one change: kernel 0021-0025 (#627, parts run 45256426877, labs 45260781177, 45261629539, 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). #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`, as 0021-0025 after #645's 0014-0020; they apply to 7.3.6 with no fuzz. 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 stayed in the set because the set was fixed before the run. *Measured* (bench, nested, spin's shape and login, against v20261123.07, interleaved): first login −9.4 [−14.2, −5.7], sshd accepting −9.5 [−14.4, −5.5], console prompt −10.4 [−14.2, −7.8], second login +0.3 [−0.1, +0.6], QEMU RSS −1.7 MiB [−1.8, −0.1], 120 a side (lab 45260781177). `restore-first`, 60 a side (lab 45261629539): restored login −0.4 [−2.0, +1.7] evicted and +0.2 [−6.6, +7.3] cached, the state +0.0 MiB [−0.1, +0.2], all within noise; its cold row −11.8 [−18.5, −3.2] evicted. The same set against v20261123.06 had read −9.2 [−12.2, −6.0] (lab 45231943664), before 7.3.6 and #645. *Status:* in place; reaches the goal number with the next release. + ## 2. Rework cycles Each cycle: the sequence, what each step measured, and the lesson. 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 index 7f9d5028..8b204b00 100644 --- 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 @@ -55,7 +55,7 @@ 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 +9.4 ms faster, 95% interval [5.7, 14.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. 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 index d811fc54..c8bd102b 100644 --- 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 @@ -47,7 +47,7 @@ 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 +9.4 ms faster, 95% interval [5.7, 14.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. 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 index 2c11e93d..1ef958c9 100644 --- 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 @@ -37,7 +37,7 @@ 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 +9.4 ms faster, 95% interval [5.7, 14.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. 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 index aec656b7..1fb6f0bc 100644 --- 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 @@ -42,7 +42,7 @@ 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 +9.4 ms faster, 95% interval [5.7, 14.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. 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 index 9cbfc663..511109be 100644 --- 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 @@ -37,7 +37,7 @@ 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 +9.4 ms faster, 95% interval [5.7, 14.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.