From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 987B8606A332 for ; Wed, 2 Dec 2026 00:40:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=XWLHVH4Z; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=casper.20170209 header.b=RuUA0Zcj; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=ggOtZ+n6 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Unsubscribe-Post:List-Owner; bh=Xp42Wnc8bmAQAYSTYrgdTdby+Cv904bnctTSsGdXeUw=; b=XWLHVH4Z4ia5W6lXuc3oBM7qeL wjKtdpDj7Sy3odo5I6F0ncq1cDQ8XMjBQ56ZyTvONxQ0Yy9IeEFJR4JPJvEfehZ13nHelMkqjJopl 68AtANQ+VTWGnLD1TopKp129xqCkMqaJXmgulVtZBTHszuBc6zGqViTcTG/qDtC/NvEHhDUbNlKNK n/10CrmbyBl0zyFlcUs8G7On/8SxuYlBwRNlNkE+CBOGzeWTpSK4WQIq1tzRE6YqNC5S3ju0G9amV mxCiCix/ngURW3v7ZL0lLqDH/tmK3iyHWo2h/X2HLFM1/RxP82/KMAyhQaYVaSqKxomOb/c0bt7ZP wgqjUBrw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYYOm-00000009mrp-2HlW; Wed, 02 Dec 2026 00:40:40 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYYOQ-00000009mCv-0fng for linux-riscv@bombadil.infradead.org; Wed, 02 Dec 2026 00:40:18 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=84kbs4vSYgt6SMaGI7HuX01IVi1XXxUoywehovKEQgo=; b=RuUA0ZcjqFcdZGyDUe+DGkvFhu 4msSbh91/QxfatNzwJkOWPsJrc1GVv4BEr0mC8vwv9rJHnb9LU7VNdUdxn3s9Q5OVnSXhjvhJBqaA kibnB5b88ghvM3g3zhOWNCMK9l43NIgoESs+OXd3eznFlkqrnSobeFZAsK2/gzLBHisjJUHLtLfv1 mMsgToqqi+AB8LiaxjUbojKX8qTZXgs3ryEH7yVTtwoqJOOLRbTit8fphRBhTGljDpI1NocEOazit 6hgfjc/nJ4eTkkJXuhdh/d01FuhdzaC/uSEchjltecuJN2AzfAGXbOWIvIn8QxdqDBVBFv9kWJDfT HtgWbeNw==; Received: from mail-ot1-x348.google.com ([2607:f8b0:4864:20::348]) by casper.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYYOM-0000000HKg7-1ZWr for linux-riscv@lists.infradead.org; Wed, 02 Dec 2026 00:40:17 +0000 Received: by mail-ot1-x348.google.com with SMTP id 46e09a7af769-847e2926d94so22547536a34.0 for ; Tue, 01 Dec 2026 16:40:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796172011; x=1796776811; darn=lists.infradead.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=84kbs4vSYgt6SMaGI7HuX01IVi1XXxUoywehovKEQgo=; b=ggOtZ+n67YMIr+pEJqFSJGTWS57+VqjWVBgGXF85PRwE0kyTn8Wo+AfEyUQjkoyPw0 CbJido6tMvSaNUFsQz4u3EptSvTQEYB2JrVrMj102ACB+UUCyAPbXfyJP+ps6lODAKTZ 9yt5OFHs4cyrnUGwi/llRYz2WJfye3STU/139vEClul3RHIISxN+wBdRNjwsd+GMAr0K Rw7L1jHTGL4nqIl0B+aWUWsQUZ+nDgiaDIMvcxzAtZUZNOxNkdmMVxzrUf4Ji21gVV87 3BXqkOmLvirfJw3ClTb4A5JeDEqTR8Hr+P2fzalza9iNv6LfzuSFsmHTCnpk9hzb918B mN/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796172011; x=1796776811; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=84kbs4vSYgt6SMaGI7HuX01IVi1XXxUoywehovKEQgo=; b=tB3aGJRBr1aZlfyqjnMytiNGJZ7+MlTyLTMbA/2HX0AZKNpXCHO7w/B4wPupBHvGMD oY7eZy0ifp8rLHGq+0zFU9ZXVv4gDS8po8HNlymxDESO1EVvPuC7KSEcQehI4N5yPDBT TCZcdpriU67O4HK/haGdBbGfxLjDpptFAQvNrVatbeQz3QcZl89d0zu1RR46pkHhecjV aUN8TbFY0SYO8t9MkIOm/YMtfwD9B8bn25KkruPCv41iAtF6pGSz0ufFpoVmKhIYCROL OyCTtDXRxXXwnvYp2qW3CAMOhpXLkSJgQ32vb1cowUkrqGGyQJTxws4q3UJbPg/8Ap4A QEmw== X-Forwarded-Encrypted: i=1; AL+hErEAfyBvTiTYaALMUsgvXpfPW1yv9XbbT/KhH8DOmUFwx6zTzoLUH15jU9qI1LdAF9MVdnfy73nDm2M6vw==@lists.infradead.org X-Gm-Message-State: APuU4Z8aUc4YlS9VGCIwfCgPRXGCWg07UZp1/k0YLBYl25sk1RLbb6nC fziikYc3bYacI76C0W5QDyvUJQR/YJVvJrRaeUUehV2BpS2SaKeuYkVljy/rq5V+GD9/xMSegE1 336r0MapWAba4Xw== X-Received: from jaho19-n2.prod.google.com ([2002:a05:6638:67d3:20b0:622:9529:ee52]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:4f65:b0:4e6:e72a:1ba0 with SMTP id 5614622812f47-57a0a516e7bmr41769502b6e.12.1796172011029; Tue, 01 Dec 2026 16:40:11 -0800 (PST) Date: Wed, 2 Dec 2026 00:39:48 +0000 In-Reply-To: <20261202003955.3607156-1-dmatlack@google.com> X-Archived-By: Mime-Version: 1.0 References: <20261202003955.3607156-1-dmatlack@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202003955.3607156-12-dmatlack@google.com> Subject: [PATCH 11/18] selftests/liveupdate: pci: Add a test with an endpoint below a root port From: David Matlack To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-pci@vger.kernel.org, linux-riscv@lists.infradead.org Cc: Albert Ou , Alexandre Ghiti , Alex Williamson , Bjorn Helgaas , David Matlack , Jacob Pan , Jason Gunthorpe , Josh Hilke , Lukas Wunner , Mike Rapoport , Palmer Dabbelt , Parav Pandit , Pasha Tatashin , Paul Walmsley , Pranjal Shrivastava , Pratyush Yadav , Samiullah Khawaja , Shuah Khan , Vipin Sharma X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261202_004014_530372_BAB28319 X-CRM114-Status: GOOD ( 28.76 ) X-Spam-Score: -9.5 (---------) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" X-Spam-Status: No, score=-16.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 Add the first PCI Live Update selftest, which preserves a PCI device across a Live Update in a QEMU VM and checks that the PCI core keeps it, and the bridges above it, running. The test builds a topology with two root ports and an IGB endpoint below each. It preserves one endpoint with the test driver, and binds the other (the control) to the test driver without preserving it. The next kernel then checks that: - The preserved endpoint and the root port above it keep their BDF, bus numbers, Bus Master Enable, ARI Forwarding Enable and ACS controls, both before and after the test driver probes and finishes the endpoint. - The test driver sees the preserved endpoint as incoming-preserved at probe time, and no longer after the session is finished. - The control endpoint had Bus Master Enable cleared during kexec. The test binary, pci/luo_pci_test, takes the endpoints to preserve and to use as a control as arguments. Endpoints are named by their path from the root bus, so that tests do not depend on bus numbers. pci_vmtest_lib.sh provides helpers to build QEMU topologies and name the endpoints, for this test and the ones to follow. Add the Kconfig options that the PCI tests need to the Live Update selftests' config files. The test runs on x86_64 (q35), arm64 (virt) and riscv64 (virt). On arm64 and riscv64, where the generic host bridge driver reassigns all bus numbers at boot, the bus number checks also show that the next kernel keeps the bus numbers of the preserved root port. Signed-off-by: David Matlack --- tools/testing/selftests/liveupdate/Makefile | 1 + tools/testing/selftests/liveupdate/config | 4 + .../selftests/liveupdate/config.aarch64 | 1 + .../selftests/liveupdate/config.riscv64 | 1 + .../selftests/liveupdate/pci/luo_pci_test.c | 707 ++++++++++++++++++ .../liveupdate/pci/pci_vmtest_lib.sh | 64 ++ .../liveupdate/pci/root_port.vmtest.sh | 7 + 7 files changed, 785 insertions(+) create mode 100644 tools/testing/selftests/liveupdate/pci/luo_pci_test.c create mode 100644 tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh create mode 100644 tools/testing/selftests/liveupdate/pci/root_port.vmtest.sh diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/selftests/liveupdate/Makefile index 2547af3c5353..fd1ee09a0f0a 100644 --- a/tools/testing/selftests/liveupdate/Makefile +++ b/tools/testing/selftests/liveupdate/Makefile @@ -13,6 +13,7 @@ TEST_GEN_PROGS_EXTENDED += luo_kexec_simple TEST_GEN_PROGS_EXTENDED += luo_multi_session TEST_GEN_PROGS_EXTENDED += luo_stress_sessions TEST_GEN_PROGS_EXTENDED += luo_stress_files +TEST_GEN_PROGS_EXTENDED += pci/luo_pci_test TEST_FILES += do_kexec.sh diff --git a/tools/testing/selftests/liveupdate/config b/tools/testing/selftests/liveupdate/config index 016d009dba13..29fcfbcb7542 100644 --- a/tools/testing/selftests/liveupdate/config +++ b/tools/testing/selftests/liveupdate/config @@ -8,5 +8,9 @@ CONFIG_KEXEC_HANDOVER_DEBUG=y CONFIG_LIVEUPDATE=y CONFIG_LIVEUPDATE_TEST=y CONFIG_MEMFD_CREATE=y +CONFIG_MODULES=y +CONFIG_PCI=y +CONFIG_PCIEPORTBUS=y +CONFIG_PCI_LIVEUPDATE=y CONFIG_TMPFS=y CONFIG_SHMEM=y diff --git a/tools/testing/selftests/liveupdate/config.aarch64 b/tools/testing/selftests/liveupdate/config.aarch64 index 445716403925..3aaa919a80b6 100644 --- a/tools/testing/selftests/liveupdate/config.aarch64 +++ b/tools/testing/selftests/liveupdate/config.aarch64 @@ -1,2 +1,3 @@ +CONFIG_PCI_HOST_GENERIC=y CONFIG_SERIAL_AMBA_PL011=y CONFIG_SERIAL_AMBA_PL011_CONSOLE=y diff --git a/tools/testing/selftests/liveupdate/config.riscv64 b/tools/testing/selftests/liveupdate/config.riscv64 index edbc551199b0..ab5ade373f11 100644 --- a/tools/testing/selftests/liveupdate/config.riscv64 +++ b/tools/testing/selftests/liveupdate/config.riscv64 @@ -1,3 +1,4 @@ +CONFIG_PCI_HOST_GENERIC=y CONFIG_SERIAL_8250=y CONFIG_SERIAL_8250_CONSOLE=y CONFIG_SERIAL_OF_PLATFORM=y diff --git a/tools/testing/selftests/liveupdate/pci/luo_pci_test.c b/tools/testing/selftests/liveupdate/pci/luo_pci_test.c new file mode 100644 index 000000000000..7995c9e4252b --- /dev/null +++ b/tools/testing/selftests/liveupdate/pci/luo_pci_test.c @@ -0,0 +1,707 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Preserve a PCI device across a Live Update and check that the PCI core keeps + * it, and the bridges above it, running. + * + * The test takes the following options, which must be the same in both + * stages except for -s: + * + * -s 1 before the Live Update, 2 after it. + * -p Endpoint to preserve. + * -c Optional. Endpoint to bind to the test driver but not + * preserve. + * + * A identifies a device independently of bus numbers: a device on the + * root bus followed by the device.function of each device below it, e.g. + * 0000:00:02.0/00.0 is function 0 of device 0 below the bridge at 0000:00:02.0. + * + * Stage 1 snapshots the config space of the endpoints and of all bridges + * above them, and preserves the endpoint. Stage 2 checks the devices + * against the snapshot before and after the driver retrieves and + * finishes them. + * + * Copyright (c) 2026, Google LLC. + * David Matlack + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include "luo_pci_test_driver.h" + +#define STATE_SESSION_NAME "lupci_state" +#define STATE_MEMFD_TOKEN 1 +#define DATA_SESSION_NAME "lupci_data" +#define DATA_MEMFD_TOKEN 1 +#define DEVICE_SESSION_NAME "lupci_devices" +#define DEVICE_TOKEN 1 + +#define CONFIG_SIZE 4096 +#define MAX_DEVICES 32 +#define BDF_LEN 12 /* DDDD:BB:dd.f */ + +enum role { + ROLE_PRESERVED, + ROLE_CONTROL, +}; + +struct pci_snapshot { + char bdf[BDF_LEN + 1]; + enum role role; + bool endpoint; /* Named by a path, not an upstream bridge */ + bool bridge; + uint16_t vendor; + uint16_t device; + uint16_t command; + uint8_t secondary; + uint8_t subordinate; + uint16_t pcie; /* Capability offsets, 0 if not present */ + uint16_t acs; + uint16_t devctl2; + uint16_t acs_ctrl; +}; + +static struct pci_snapshot devices[MAX_DEVICES]; +static int nr_devices; + +/* Stage 2 snapshot, taken before the test driver is loaded. */ +static struct pci_snapshot before[MAX_DEVICES]; +static int nr_before; + +static const char *opt_preserve = ""; +static const char *opt_control = ""; + +/* strcspn(), which nolibc lacks. */ +static size_t span_until(const char *s, const char *reject) +{ + size_t len = 0; + + while (s[len] && !strchr(reject, s[len])) + len++; + + return len; +} + +static int parse_args(int argc, char *argv[]) +{ + int stage = 1; + int opt; + + while ((opt = getopt(argc, argv, "s:p:c:")) != -1) { + switch (opt) { + case 's': + stage = atoi(optarg); + break; + case 'p': + opt_preserve = optarg; + break; + case 'c': + opt_control = optarg; + break; + default: + ksft_exit_fail_msg("Unknown option\n"); + } + } + + if (stage != 1 && stage != 2) + ksft_exit_fail_msg("Invalid stage (-s)\n"); + if (!opt_preserve[0]) + ksft_exit_fail_msg("No device to preserve (-p)\n"); + + return stage; +} + +static void mount_sysfs(void) +{ + mkdir("/sys", 0755); + if (mount("sysfs", "/sys", "sysfs", 0, NULL) < 0 && errno != EBUSY) + fail_exit("mount sysfs"); +} + +static void load_module(void) +{ + int fd, ret; + + fd = open(LUO_PCI_TEST_MODULE, O_RDONLY); + if (fd < 0) + fail_exit("open %s", LUO_PCI_TEST_MODULE); + + ret = syscall(__NR_finit_module, fd, "", 0); + if (ret < 0 && errno != EEXIST) + fail_exit("finit_module %s", LUO_PCI_TEST_MODULE); + + close(fd); +} + +static int read_config(const char *bdf, uint8_t *cfg) +{ + char path[64]; + ssize_t n; + int fd, len = 0; + + memset(cfg, 0, CONFIG_SIZE); + + snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/config", bdf); + fd = open(path, O_RDONLY); + if (fd < 0) + return -errno; + + while (len < CONFIG_SIZE) { + n = read(fd, cfg + len, CONFIG_SIZE - len); + if (n <= 0) + break; + len += n; + } + + close(fd); + return len; +} + +static uint16_t cfg16(const uint8_t *cfg, int pos) +{ + return cfg[pos] | cfg[pos + 1] << 8; +} + +static uint32_t cfg32(const uint8_t *cfg, int pos) +{ + return cfg16(cfg, pos) | (uint32_t)cfg16(cfg, pos + 2) << 16; +} + +static uint16_t find_cap(const uint8_t *cfg, uint8_t id) +{ + int ttl = 48; + uint8_t pos; + + if (!(cfg16(cfg, PCI_STATUS) & PCI_STATUS_CAP_LIST)) + return 0; + + pos = cfg[PCI_CAPABILITY_LIST] & ~3; + while (pos && ttl--) { + if (cfg[pos + PCI_CAP_LIST_ID] == id) + return pos; + pos = cfg[pos + PCI_CAP_LIST_NEXT] & ~3; + } + + return 0; +} + +static uint16_t find_ext_cap(const uint8_t *cfg, int len, uint16_t id) +{ + int ttl = (CONFIG_SIZE - PCI_CFG_SPACE_SIZE) / 8; + uint16_t pos = PCI_CFG_SPACE_SIZE; + uint32_t header; + + while (pos && pos + 4 <= len && ttl--) { + header = cfg32(cfg, pos); + if (!header) + return 0; + if (PCI_EXT_CAP_ID(header) == id) + return pos; + pos = PCI_EXT_CAP_NEXT(header); + } + + return 0; +} + +static void take_snapshot(struct pci_snapshot *dev) +{ + uint8_t cfg[CONFIG_SIZE]; + int len; + + len = read_config(dev->bdf, cfg); + if (len < PCI_CFG_SPACE_SIZE) + fail_exit("Failed to read config space of %s", dev->bdf); + + dev->vendor = cfg16(cfg, PCI_VENDOR_ID); + dev->device = cfg16(cfg, PCI_DEVICE_ID); + dev->command = cfg16(cfg, PCI_COMMAND); + dev->bridge = (cfg[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_BRIDGE; + dev->secondary = dev->bridge ? cfg[PCI_SECONDARY_BUS] : 0; + dev->subordinate = dev->bridge ? cfg[PCI_SUBORDINATE_BUS] : 0; + + dev->pcie = find_cap(cfg, PCI_CAP_ID_EXP); + dev->devctl2 = dev->pcie ? cfg16(cfg, dev->pcie + PCI_EXP_DEVCTL2) : 0; + + dev->acs = find_ext_cap(cfg, len, PCI_EXT_CAP_ID_ACS); + /* Ignore a truncated ACS capability. */ + if (dev->acs + PCI_ACS_CTRL + 2 > len) + dev->acs = 0; + dev->acs_ctrl = dev->acs ? cfg16(cfg, dev->acs + PCI_ACS_CTRL) : 0; +} + +static struct pci_snapshot *find_device(const char *bdf) +{ + int i; + + for (i = 0; i < nr_devices; i++) { + if (!strcmp(devices[i].bdf, bdf)) + return &devices[i]; + } + + return NULL; +} + +static struct pci_snapshot *add_device(const char *bdf, enum role role) +{ + struct pci_snapshot *dev = find_device(bdf); + + if (dev) { + /* A bridge shared with a preserved endpoint is preserved. */ + if (role == ROLE_PRESERVED) + dev->role = ROLE_PRESERVED; + return dev; + } + + if (nr_devices == MAX_DEVICES) + fail_exit("Too many devices"); + + dev = &devices[nr_devices++]; + memset(dev, 0, sizeof(*dev)); + snprintf(dev->bdf, sizeof(dev->bdf), "%s", bdf); + dev->role = role; + return dev; +} + +/* Find the device at @devfn (e.g. "00.0") on the secondary bus of @parent. */ +static void find_child(const char *parent, const char *devfn, size_t devfn_len, + char *child) +{ + uint8_t cfg[CONFIG_SIZE]; + char sysfs[64]; + + if (devfn_len != 4 || devfn[2] != '.') + fail_exit("Invalid device.function %.*s", (int)devfn_len, devfn); + + if (read_config(parent, cfg) < PCI_CFG_SPACE_SIZE) + fail_exit("Failed to read config space of %s", parent); + + if ((cfg[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MASK) != PCI_HEADER_TYPE_BRIDGE) + fail_exit("%s is not a bridge", parent); + + /* A bridge's secondary bus must be numbered above its own bus. */ + if (cfg[PCI_SECONDARY_BUS] <= strtoul(parent + 5, NULL, 16)) + fail_exit("%s has an invalid secondary bus number (0x%02x)", parent, + cfg[PCI_SECONDARY_BUS]); + + snprintf(child, BDF_LEN + 1, "%04lx:%02x:", strtoul(parent, NULL, 16), + cfg[PCI_SECONDARY_BUS]); + memcpy(child + 8, devfn, devfn_len); + child[BDF_LEN] = 0; + + snprintf(sysfs, sizeof(sysfs), "/sys/bus/pci/devices/%s", child); + if (access(sysfs, F_OK)) + fail_exit("No device %.*s below %s", (int)devfn_len, devfn, parent); +} + +/* Add every device along @path, and mark the last one as an endpoint. */ +static void add_path(const char *path, enum role role) +{ + char bdf[BDF_LEN + 1]; + const char *p = path; + char sysfs[64]; + size_t n; + + n = span_until(p, "/"); + if (n != BDF_LEN) + fail_exit("Invalid device path %s", path); + + memcpy(bdf, p, BDF_LEN); + bdf[BDF_LEN] = 0; + + snprintf(sysfs, sizeof(sysfs), "/sys/bus/pci/devices/%s", bdf); + if (access(sysfs, F_OK)) + fail_exit("No device %s", bdf); + + add_device(bdf, role); + p += n; + + while (*p == '/') { + char child[BDF_LEN + 1]; + + p++; + n = span_until(p, "/"); + find_child(bdf, p, n, child); + memcpy(bdf, child, sizeof(bdf)); + add_device(bdf, role); + p += n; + } + + find_device(bdf)->endpoint = true; +} + +static int format_snapshot(const struct pci_snapshot *dev, char *buf, size_t size) +{ + return snprintf(buf, size, + "bdf=%s role=%s endpoint=%d bridge=%d id=0x%04x device=0x%04x " + "cmd=0x%04x sec=0x%02x sub=0x%02x pcie=0x%x acs=0x%x " + "devctl2=0x%04x acsctrl=0x%04x\n", + dev->bdf, dev->role == ROLE_PRESERVED ? "preserved" : "control", + dev->endpoint, dev->bridge, dev->vendor, dev->device, + dev->command, dev->secondary, dev->subordinate, dev->pcie, + dev->acs, dev->devctl2, dev->acs_ctrl); +} + +static unsigned long get_field(const char *line, const char *key) +{ + const char *p = strstr(line, key); + + if (!p) + fail_exit("Snapshot is missing %s", key); + + return strtoul(p + strlen(key), NULL, 0); +} + +static void parse_snapshots(const char *text) +{ + struct pci_snapshot *dev; + const char *p = text; + char line[512]; + size_t len; + + while (*p) { + len = span_until(p, "\n"); + if (len >= sizeof(line)) + fail_exit("Snapshot line is too long"); + + memcpy(line, p, len); + line[len] = 0; + p += len; + if (*p == '\n') + p++; + + if (!strstr(line, "bdf=")) + continue; + + if (nr_devices == MAX_DEVICES) + fail_exit("Too many devices in snapshot"); + + dev = &devices[nr_devices++]; + memset(dev, 0, sizeof(*dev)); + memcpy(dev->bdf, strstr(line, "bdf=") + 4, BDF_LEN); + dev->role = strstr(line, "role=preserved") ? ROLE_PRESERVED : ROLE_CONTROL; + dev->endpoint = get_field(line, "endpoint="); + dev->bridge = get_field(line, "bridge="); + dev->vendor = get_field(line, "id="); + dev->device = get_field(line, "device="); + dev->command = get_field(line, "cmd="); + dev->secondary = get_field(line, "sec="); + dev->subordinate = get_field(line, "sub="); + dev->pcie = get_field(line, "pcie="); + dev->acs = get_field(line, "acs="); + dev->devctl2 = get_field(line, "devctl2="); + dev->acs_ctrl = get_field(line, "acsctrl="); + } +} + +static int get_device_fd(int ctl_fd, const char *bdf) +{ + struct luo_pci_test_device dev = {}; + unsigned int domain, bus, slot, func; + int fd; + + domain = strtoul(bdf, NULL, 16); + bus = strtoul(bdf + 5, NULL, 16); + slot = strtoul(bdf + 8, NULL, 16); + func = strtoul(bdf + 11, NULL, 16); + + dev.domain = domain; + dev.bdf = bus << 8 | slot << 3 | func; + + fd = ioctl(ctl_fd, LUO_PCI_TEST_GET_DEVICE_FD, &dev); + if (fd < 0) + fail_exit("Failed to get a file for %s. Is it bound to the test driver?", bdf); + + return fd; +} + +static uint16_t get_status(int fd, const char *bdf) +{ + struct luo_pci_test_device dev = {}; + char actual[BDF_LEN + 1]; + + if (ioctl(fd, LUO_PCI_TEST_GET_STATUS, &dev) < 0) + fail_exit("LUO_PCI_TEST_GET_STATUS for %s", bdf); + + snprintf(actual, sizeof(actual), "%04x:%02x:%02x.%x", dev.domain, + dev.bdf >> 8, (dev.bdf >> 3) & 0x1f, dev.bdf & 7); + if (strcmp(actual, bdf)) + fail_exit("Expected device %s, got %s", bdf, actual); + + return dev.flags; +} + +static int nr_failures; + +#define check(cond, fmt, ...) do { \ + if (!(cond)) { \ + ksft_print_msg("FAIL: " fmt "\n", ##__VA_ARGS__); \ + nr_failures++; \ + } \ +} while (0) + +/* + * Check the devices against the stage 1 snapshot. If @taken is NULL, check + * their current state. Otherwise check the snapshots in @taken, which were + * taken in the same order as the stage 1 snapshot. + */ +static void check_devices(const char *when, const struct pci_snapshot *taken) +{ + struct pci_snapshot now; + int i; + + ksft_print_msg("[STAGE 2] Checking devices %s\n", when); + + for (i = 0; i < nr_devices; i++) { + const struct pci_snapshot *dev = &devices[i]; + + if (taken) { + now = taken[i]; + check(!strcmp(now.bdf, dev->bdf), "%s: Device moved to %s", + dev->bdf, now.bdf); + } else { + now = *dev; + take_snapshot(&now); + } + + check(now.vendor == dev->vendor && now.device == dev->device, + "%s: ID changed from %04x:%04x to %04x:%04x", dev->bdf, + dev->vendor, dev->device, now.vendor, now.device); + + if (dev->role == ROLE_CONTROL) { + if (dev->endpoint && taken) + check(!(now.command & PCI_COMMAND_MASTER), + "%s: Bus Master still enabled on control device", + dev->bdf); + continue; + } + + check(now.command & PCI_COMMAND_MASTER, + "%s: Bus Master disabled (Command 0x%04x)", dev->bdf, now.command); + + if (dev->bridge) + check(now.secondary == dev->secondary && + now.subordinate == dev->subordinate, + "%s: Bus numbers changed from [%02x-%02x] to [%02x-%02x]", + dev->bdf, dev->secondary, dev->subordinate, + now.secondary, now.subordinate); + + if (dev->bridge && dev->pcie) + check((now.devctl2 & PCI_EXP_DEVCTL2_ARI) == + (dev->devctl2 & PCI_EXP_DEVCTL2_ARI), + "%s: ARI Forwarding Enable changed (DEVCTL2 0x%04x -> 0x%04x)", + dev->bdf, dev->devctl2, now.devctl2); + + if (dev->acs) + check(now.acs_ctrl == dev->acs_ctrl, + "%s: ACS controls changed from 0x%04x to 0x%04x", + dev->bdf, dev->acs_ctrl, now.acs_ctrl); + } + + if (nr_failures) + ksft_exit_fail_msg("%d check(s) failed %s\n", nr_failures, when); +} + +static void run_stage_1(int luo_fd) +{ + char text[4096] = {}; + int session_fd, ctl_fd, fd, i; + size_t len = 0; + + ksft_print_msg("[STAGE 1] Starting pre-kexec setup...\n"); + + for (i = 0; i < nr_devices; i++) { + struct pci_snapshot *dev = &devices[i]; + + if (dev->role == ROLE_PRESERVED || dev->endpoint) { + if (!(dev->command & PCI_COMMAND_MASTER)) + fail_exit("%s: Bus Master is not enabled", dev->bdf); + } + + len += format_snapshot(dev, text + len, sizeof(text) - len); + if (len >= sizeof(text)) + fail_exit("Snapshot is too large"); + } + + create_state_file(luo_fd, STATE_SESSION_NAME, STATE_MEMFD_TOKEN, 2); + + session_fd = luo_create_session(luo_fd, DATA_SESSION_NAME); + if (session_fd < 0) + fail_exit("luo_create_session %s", DATA_SESSION_NAME); + if (create_and_preserve_memfd(session_fd, DATA_MEMFD_TOKEN, text) < 0) + fail_exit("Failed to preserve snapshot"); + + session_fd = luo_create_session(luo_fd, DEVICE_SESSION_NAME); + if (session_fd < 0) + fail_exit("luo_create_session %s", DEVICE_SESSION_NAME); + + ctl_fd = open(LUO_PCI_TEST_DEVICE, O_RDWR); + if (ctl_fd < 0) + fail_exit("open %s", LUO_PCI_TEST_DEVICE); + + for (i = 0; i < nr_devices; i++) { + struct pci_snapshot *dev = &devices[i]; + + if (dev->role != ROLE_PRESERVED || !dev->endpoint) + continue; + + ksft_print_msg("[STAGE 1] Preserving %s\n", dev->bdf); + fd = get_device_fd(ctl_fd, dev->bdf); + if (luo_session_preserve_fd(session_fd, fd, DEVICE_TOKEN) < 0) + fail_exit("Failed to preserve %s", dev->bdf); + close(fd); + } + + close(ctl_fd); + + close(luo_fd); + daemonize_and_wait(); +} + +static void run_stage_2(int luo_fd, int state_session_fd) +{ + int fds[MAX_DEVICES] = {}; + int session_fd, ctl_fd, mfd, stage, i; + char text[4096] = {}; + uint16_t flags; + + ksft_print_msg("[STAGE 2] Starting post-kexec verification...\n"); + + restore_and_read_stage(state_session_fd, STATE_MEMFD_TOKEN, &stage); + if (stage != 2) + fail_exit("Expected stage 2, but state file contains %d", stage); + + session_fd = luo_retrieve_session(luo_fd, DATA_SESSION_NAME); + if (session_fd < 0) + fail_exit("luo_retrieve_session %s", DATA_SESSION_NAME); + + mfd = restore_and_verify_memfd(session_fd, DATA_MEMFD_TOKEN, NULL); + if (mfd < 0) + fail_exit("Failed to restore snapshot"); + if (read(mfd, text, sizeof(text) - 1) < 0) + fail_exit("Failed to read snapshot"); + close(mfd); + + parse_snapshots(text); + if (luo_session_finish(session_fd) < 0) + fail_exit("luo_session_finish %s", DATA_SESSION_NAME); + close(session_fd); + + if (nr_devices != nr_before) + fail_exit("Expected %d devices, found %d", nr_devices, nr_before); + + check_devices("before probe", before); + + session_fd = luo_retrieve_session(luo_fd, DEVICE_SESSION_NAME); + if (session_fd < 0) + fail_exit("luo_retrieve_session %s", DEVICE_SESSION_NAME); + + ctl_fd = open(LUO_PCI_TEST_DEVICE, O_RDWR); + if (ctl_fd < 0) + fail_exit("open %s", LUO_PCI_TEST_DEVICE); + + for (i = 0; i < nr_devices; i++) { + struct pci_snapshot *dev = &devices[i]; + + if (!dev->endpoint) + continue; + + if (dev->role == ROLE_CONTROL) { + fds[i] = get_device_fd(ctl_fd, dev->bdf); + flags = get_status(fds[i], dev->bdf); + check(!(flags & LUO_PCI_TEST_INCOMING_AT_PROBE), + "%s: Control device was incoming-preserved", dev->bdf); + continue; + } + + ksft_print_msg("[STAGE 2] Retrieving %s\n", dev->bdf); + fds[i] = luo_session_retrieve_fd(session_fd, DEVICE_TOKEN); + if (fds[i] < 0) + fail_exit("Failed to retrieve %s", dev->bdf); + + flags = get_status(fds[i], dev->bdf); + check(flags & LUO_PCI_TEST_INCOMING_AT_PROBE, + "%s: Device was not incoming-preserved at probe", dev->bdf); + check(flags & LUO_PCI_TEST_INCOMING, + "%s: Device is not incoming-preserved", dev->bdf); + } + + check_devices("after probe", NULL); + + ksft_print_msg("[STAGE 2] Finishing %s...\n", DEVICE_SESSION_NAME); + if (luo_session_finish(session_fd) < 0) + fail_exit("luo_session_finish %s", DEVICE_SESSION_NAME); + + for (i = 0; i < nr_devices; i++) { + struct pci_snapshot *dev = &devices[i]; + + if (!dev->endpoint || dev->role != ROLE_PRESERVED) + continue; + + flags = get_status(fds[i], dev->bdf); + check(!(flags & LUO_PCI_TEST_INCOMING), + "%s: Device is still incoming-preserved after finish", dev->bdf); + } + + check_devices("after finish", NULL); + + if (luo_session_finish(state_session_fd) < 0) + fail_exit("luo_session_finish %s", STATE_SESSION_NAME); + + ksft_print_msg("\n--- PCI LIVE UPDATE TEST PASSED ---\n"); +} + +int main(int argc, char *argv[]) +{ + int stage = parse_args(argc, argv); + char stage_arg[] = { '0' + stage, 0 }; + char *luo_argv[] = { argv[0], "-s", stage_arg, NULL }; + int i; + + mount_sysfs(); + + add_path(opt_preserve, ROLE_PRESERVED); + if (opt_control[0]) + add_path(opt_control, ROLE_CONTROL); + + /* + * In stage 2, snapshot the devices before the test driver is loaded to + * see how the PCI core left them after enumeration. They are checked + * against the stage 1 snapshot once that is retrieved. + */ + if (stage == 2) { + for (i = 0; i < nr_devices; i++) + take_snapshot(&devices[i]); + + memcpy(before, devices, sizeof(devices)); + nr_before = nr_devices; + nr_devices = 0; + } + + /* + * LUO deserializes all preserved files when /dev/liveupdate is first + * opened, which requires the test driver's file handler. + */ + load_module(); + + /* In stage 1, snapshot the devices once the test driver is bound. */ + if (stage == 1) { + for (i = 0; i < nr_devices; i++) + take_snapshot(&devices[i]); + } + + /* luo_test() only takes -s. */ + return luo_test(3, luo_argv, STATE_SESSION_NAME, run_stage_1, run_stage_2); +} diff --git a/tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh b/tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh new file mode 100644 index 000000000000..ebed414510f4 --- /dev/null +++ b/tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh @@ -0,0 +1,64 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Common setup for the PCI Live Update selftests. +# +# Sourced by pci/*.vmtest.sh, which are themselves sourced by ../vmtest.sh +# after $arch, $target, $test_dir and $workspace_dir are set. See vmtest.sh for +# the TEST_* variables. +# +# Each test builds a PCIe topology out of the helpers below, using the QEMU +# "igb" device as the endpoint, and names the endpoints to preserve and the +# control endpoints (bound to the test driver but not preserved) by their path +# from the root bus. See luo_pci_test.c for the path format. + +PCI_CHASSIS=0 + +TEST_BINARY=pci/luo_pci_test +TEST_MODULES=(luo_pci_test_driver) +TEST_TIMEOUT="180s" + +TEST_QEMU_ARGS=() + +# The default x86 machine (pc) does not support PCIe. +if [[ "$arch" == "x86" ]]; then + TEST_QEMU_ARGS+=(-M q35) +fi + +# pci_root_port +# Add a root port at device on the root bus. +function pci_root_port() { + PCI_CHASSIS=$((PCI_CHASSIS + 1)) + TEST_QEMU_ARGS+=(-device "pcie-root-port,id=$1,bus=pcie.0,addr=$2,chassis=$PCI_CHASSIS") +} + +# pci_igb [] +# Add an IGB endpoint at device (default 0) on . +function pci_igb() { + TEST_QEMU_ARGS+=(-device "igb,id=$1,bus=$2,addr=${3:-0}") +} + +# pci_preserve +# Preserve the endpoint at . +function pci_preserve() { + TEST_ARGS+=(-p "$1") +} + +# pci_control +# Bind the endpoint at to the test driver, but don't preserve it. +# Bus Master must be cleared on it during kexec. +function pci_control() { + TEST_ARGS+=(-c "$1") +} + +# pci_topology_root_ports +# Two root ports, with an endpoint to preserve below one and a control +# endpoint below the other. +function pci_topology_root_ports() { + pci_root_port rp1 0x2 + pci_igb igb1 rp1 + pci_root_port rp2 0x3 + pci_igb igb2 rp2 + + pci_preserve 0000:00:02.0/00.0 + pci_control 0000:00:03.0/00.0 +} diff --git a/tools/testing/selftests/liveupdate/pci/root_port.vmtest.sh b/tools/testing/selftests/liveupdate/pci/root_port.vmtest.sh new file mode 100644 index 000000000000..05af30ddb296 --- /dev/null +++ b/tools/testing/selftests/liveupdate/pci/root_port.vmtest.sh @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Preserve an endpoint below a root port. + +source "$test_dir/pci/pci_vmtest_lib.sh" + +pci_topology_root_ports -- 2.56.1.911.g883d8bcb20-goog _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv