* [PATCH] vfio: selftests: Add NVMe driver
@ 2026-11-24 2:08 Josh Hilke
2026-11-26 0:12 ` David Matlack
0 siblings, 1 reply; 2+ messages in thread
From: Josh Hilke @ 2026-11-24 2:08 UTC (permalink / raw)
To: David Matlack, Alex Williamson
Cc: kvm, linux-kselftest, linux-doc, linux-kernel, Josh Hilke
Add a VFIO selftest driver for NVMe controllers. It binds to any PCI
function with the NVMe PCI class code 01:08:02, so vfio_pci_driver_test
can run on the NVMe SSDs that are common in servers, and on the nvme
device that QEMU emulates.
NVMe has no memory-to-memory copy command, so the driver implements a
memcpy through the logical blocks at the start of the first active
namespace. memcpy_start() writes @src to the logical blocks from LBA 0,
waits for the write, and then queues @count reads of the same blocks into
@dst. The driver must wait for the write because the controller can run
the commands of a queue in any order. All @count reads are in flight when
memcpy_start() returns. The driver spreads the reads over up to 16 such
queues, because one queue holds only as many commands as the controller
allows, 2047 on the QEMU device. The memcpy commands go on I/O queues
that have interrupts disabled. send_msi() sends a flush on one more I/O
queue that has interrupts enabled.
The driver overwrites the first max_memcpy_size bytes of the namespace.
Do not use a device that holds data you want to keep.
On some controllers, a DMA error from the unmapped-IOVA tests leaves the
submission queue in an error state that only a Function Level Reset
clears. After a failed memcpy, the driver disables the controller, resets
the function with VFIO_DEVICE_RESET, and initializes the controller
again.
The driver needs a controller that supports the NVM command set, 4 KiB
memory pages, at least two I/O queue pairs, and MSI-X, along with
a namespace that has no metadata and a logical block size of at most 4 KiB.
Controllers that need one of the quirks in drivers/nvme/host/pci.c are
not supported.
The driver uses the definitions in include/linux/nvme.h through a
symbolic link, like the other drivers do with their kernel headers.
Users can verify the driver in QEMU with these commands, after they
build the kernel and the VFIO selftests:
truncate -s 64M /tmp/nvme.img
vng \
--run arch/x86/boot/bzImage \
--user root \
--disable-microvm \
--memory 4G \
--cpus 4 \
--qemu-opts="-M q35,accel=kvm,kernel-irqchip=split" \
--qemu-opts="-device intel-iommu,intremap=on,caching-mode=on" \
--qemu-opts="-drive file=/tmp/nvme.img,if=none,id=nvm,format=raw" \
--qemu-opts="-device nvme,serial=deadbeef,drive=nvm,addr=10.0" \
--append "console=ttyS0 intel_iommu=on" \
--exec "modprobe vfio-pci && \
./tools/testing/selftests/vfio/scripts/setup.sh 0000:00:10.0 && \
./tools/testing/selftests/vfio/scripts/run.sh \
./tools/testing/selftests/vfio/vfio_pci_driver_test"
Signed-off-by: Josh Hilke <jrhilke@google.com>
---
Testing:
- Built VFIO and KVM selftests on x86_64 (dynamic and static) and VFIO selftests on arm64 (static).
- QEMU nvme (x86_64): vfio_pci_driver_test 35/35 passed (default and nsid=2); msix_qsize=1 and max_ioqpairs=1 failed at their expected assertions.
- Samsung PM175x VF (x86_64): vfio_pci_device_test 5/5, vfio_pci_driver_test 35/35, and KVM irq_test passed.
- Samsung PM174X VF (arm64): vfio_pci_device_test 5/5 and vfio_pci_driver_test 35/35 passed.
Documentation/driver-api/vfio-selftests.rst | 4 +
.../selftests/vfio/lib/drivers/nvme/nvme.c | 745 ++++++++++++++++++
.../selftests/vfio/lib/drivers/nvme/nvme.h | 1 +
tools/testing/selftests/vfio/lib/libvfio.mk | 1 +
.../selftests/vfio/lib/vfio_pci_driver.c | 2 +
5 files changed, 753 insertions(+)
create mode 100644 tools/testing/selftests/vfio/lib/drivers/nvme/nvme.c
create mode 120000 tools/testing/selftests/vfio/lib/drivers/nvme/nvme.h
diff --git a/Documentation/driver-api/vfio-selftests.rst b/Documentation/driver-api/vfio-selftests.rst
index 9d39a22519d6..b26040c48d5d 100644
--- a/Documentation/driver-api/vfio-selftests.rst
+++ b/Documentation/driver-api/vfio-selftests.rst
@@ -39,10 +39,14 @@ includes driver support for the following devices:
- Intel IOAT (I/O Acceleration Technology) DMA engines
- Intel DSA (Data Streaming Accelerator)
- NVIDIA Falcon engines (found in many NVIDIA GPUs, e.g., GTX 1080 or RTX 2080)
+- NVMe controllers
Note that using Intel DSA devices requires setting the module parameter
``vfio_pci.disable_denylist=Y`` to allow ``vfio-pci`` to bind to DSA devices.
+Note that the NVMe driver overwrites the start of the first active namespace.
+Do not use an NVMe device that holds data you want to keep.
+
Setting up Devices
------------------
diff --git a/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.c b/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.c
new file mode 100644
index 000000000000..6f82e0ddf309
--- /dev/null
+++ b/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.c
@@ -0,0 +1,745 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <stddef.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <asm/barrier.h>
+#include <linux/align.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <linux/pci_ids.h>
+#include <linux/pci_regs.h>
+#include <linux/sizes.h>
+#include <linux/stddef.h>
+#include <linux/types.h>
+
+#include <libvfio.h>
+
+/* nvme.h needs uuid_t from libuuid and struct_group(). */
+#include <uuid/uuid.h>
+
+#ifndef struct_group
+#define struct_group(NAME, MEMBERS...) \
+ __struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS)
+#endif
+
+#include "nvme.h"
+
+#define NVME_ADMIN_QID 0
+#define NVME_MSI_QID 1
+#define NVME_MAX_POLL_QUEUES 16
+#define NVME_IO_MSIX_VEC 1
+#define NVME_ADMIN_QUEUE_DEPTH 64
+
+/* A queue of depth N holds N - 1 commands. msiq has one in flight. */
+#define NVME_MSI_QUEUE_DEPTH 2
+
+/*
+ * Ring entries for all polled queues together. This bounds max_memcpy_count so
+ * tests that churn through many memcpys, such as memcpy_storm, finish within
+ * their timeouts on slow controllers.
+ */
+#define NVME_POLL_TOTAL_ENTRIES 8192
+
+/* Rings for the polled queues, plus page alignment of each ring. */
+#define NVME_POLL_RINGS_SIZE (NVME_POLL_TOTAL_ENTRIES * \
+ (sizeof(struct nvme_command) + \
+ sizeof(struct nvme_completion)) + \
+ NVME_MAX_POLL_QUEUES * 2 * SZ_4K)
+
+#define NVME_PRP_LIST_ENTRIES (SZ_4K / sizeof(__le64))
+
+/* The driver builds one PRP list page, which covers 2 MiB at any offset. */
+#define NVME_MAX_XFER_SIZE SZ_2M
+#define NVME_MIN_LBA_SHIFT 9
+#define NVME_MAX_LBA_SHIFT 12
+#define NVME_TIMEOUT_MS 5000ULL
+#define NVME_CAP_TIMEOUT_UNIT_MS 500ULL
+#define NVME_POLL_INTERVAL_US 1000
+
+struct nvme_queue {
+ /* Byte offsets of the submission and completion rings in the region. */
+ u64 sq_offset;
+ u64 cq_offset;
+
+ /* Number of entries in each ring. */
+ u32 depth;
+
+ /* Commands submitted whose completions are not yet consumed. */
+ u32 inflight;
+
+ /* Queue identifier, which also selects the doorbell registers. */
+ u16 qid;
+
+ u16 sq_tail;
+ u16 cq_head;
+
+ /* Expected phase tag of the next completion entry. */
+ u16 cq_phase;
+};
+
+struct nvme_state {
+ struct nvme_queue adminq;
+ /*
+ * I/O queue whose completions raise device->driver.msi. It carries
+ * only the flush command of nvme_send_msi().
+ */
+ struct nvme_queue msiq;
+ /*
+ * I/O queues whose completions raise no interrupt. They carry all
+ * memcpy commands. Their rings start at poll_rings.
+ */
+ struct nvme_queue pollq[NVME_MAX_POLL_QUEUES];
+ u32 nr_pollqs;
+
+ /* Doorbell register stride in bytes, derived from CAP.DSTRD. */
+ u32 db_stride;
+
+ /* The namespace that the memcpy commands use. */
+ u32 nsid;
+
+ /* Power-of-two shift for converting bytes to logical blocks. */
+ u32 lba_shift;
+
+ /* Result of the write in nvme_memcpy_start(). */
+ int memcpy_err;
+
+ /* DMA buffers addressed by the byte offsets held in adminq and msiq. */
+ struct nvme_command admin_sq[NVME_ADMIN_QUEUE_DEPTH] __aligned(SZ_4K);
+ struct nvme_completion admin_cq[NVME_ADMIN_QUEUE_DEPTH] __aligned(SZ_4K);
+ struct nvme_command msi_sq[NVME_MSI_QUEUE_DEPTH] __aligned(SZ_4K);
+ struct nvme_completion msi_cq[NVME_MSI_QUEUE_DEPTH] __aligned(SZ_4K);
+
+ /* Identify data, written by the device. */
+ struct nvme_id_ctrl id_ctrl __aligned(SZ_4K);
+ struct nvme_id_ns id_ns __aligned(SZ_4K);
+ __le32 ns_list[NVME_IDENTIFY_DATA_SIZE / sizeof(__le32)] __aligned(SZ_4K);
+ /*
+ * PRP lists for @src and @dst of the current memcpy. Every command of a
+ * memcpy uses the same two buffers, so the queued commands share them.
+ */
+ __le64 prp_src[NVME_PRP_LIST_ENTRIES] __aligned(SZ_4K);
+ __le64 prp_dst[NVME_PRP_LIST_ENTRIES] __aligned(SZ_4K);
+ u8 poll_rings[] __aligned(SZ_4K);
+};
+
+static inline struct nvme_state *to_nvme_state(struct vfio_pci_device *device)
+{
+ return device->driver.region.vaddr;
+}
+
+static inline struct nvme_command *nvme_sq(struct vfio_pci_device *device,
+ struct nvme_queue *q)
+{
+ return device->driver.region.vaddr + q->sq_offset;
+}
+
+static inline struct nvme_completion *nvme_cq(struct vfio_pci_device *device,
+ struct nvme_queue *q)
+{
+ return device->driver.region.vaddr + q->cq_offset;
+}
+
+/* Offset of the SQ tail doorbell of @qid, or the CQ head doorbell if @cq. */
+static u64 nvme_db_offset(struct nvme_state *state, u16 qid, bool cq)
+{
+ return NVME_REG_DBS + (2 * qid + cq) * state->db_stride;
+}
+
+static void nvme_ring_sq_db(struct vfio_pci_device *device,
+ struct nvme_queue *q)
+{
+ struct nvme_state *state = to_nvme_state(device);
+
+ /* Make the commands visible to the device before the doorbell. */
+ dma_wmb();
+ writel(q->sq_tail,
+ device->bars[0].vaddr + nvme_db_offset(state, q->qid, false));
+}
+
+static void nvme_ring_cq_db(struct vfio_pci_device *device,
+ struct nvme_queue *q)
+{
+ struct nvme_state *state = to_nvme_state(device);
+
+ /* Finish reading the completion before releasing the slot. */
+ dma_rmb();
+ writel(q->cq_head,
+ device->bars[0].vaddr + nvme_db_offset(state, q->qid, true));
+}
+
+static void nvme_queue_init(struct vfio_pci_device *device,
+ struct nvme_queue *q, u16 qid, u32 depth,
+ u64 sq_offset, u64 cq_offset)
+{
+ q->sq_offset = sq_offset;
+ q->cq_offset = cq_offset;
+ q->depth = depth;
+ q->inflight = 0;
+ q->qid = qid;
+ q->sq_tail = 0;
+ q->cq_head = 0;
+ q->cq_phase = 1;
+
+ memset(nvme_sq(device, q), 0, depth * sizeof(struct nvme_command));
+ memset(nvme_cq(device, q), 0, depth * sizeof(struct nvme_completion));
+}
+
+static void nvme_sq_push(struct vfio_pci_device *device, struct nvme_queue *q,
+ const struct nvme_command *cmd)
+{
+ struct nvme_command *sq = nvme_sq(device, q);
+
+ memcpy(&sq[q->sq_tail], cmd, sizeof(*cmd));
+ sq[q->sq_tail].common.command_id = q->sq_tail;
+ q->sq_tail = (q->sq_tail + 1) % q->depth;
+ q->inflight++;
+}
+
+static void nvme_sq_submit(struct vfio_pci_device *device, struct nvme_queue *q,
+ const struct nvme_command *cmd)
+{
+ nvme_sq_push(device, q, cmd);
+ nvme_ring_sq_db(device, q);
+}
+
+/* Consume the next completion on @q if the device has posted it. */
+static bool nvme_cq_consume(struct vfio_pci_device *device, struct nvme_queue *q,
+ struct nvme_completion *cqe)
+{
+ struct nvme_completion *entry = &nvme_cq(device, q)[q->cq_head];
+
+ if ((le16_to_cpu(READ_ONCE(entry->status)) & 1) != q->cq_phase)
+ return false;
+
+ /* Do not read the completion before the phase tag is observed. */
+ dma_rmb();
+ memcpy(cqe, entry, sizeof(*cqe));
+
+ /* The controller inverts the phase tag on each pass through the ring. */
+ q->cq_head = (q->cq_head + 1) % q->depth;
+ if (q->cq_head == 0)
+ q->cq_phase ^= 1;
+ q->inflight--;
+
+ return true;
+}
+
+static int nvme_cq_wait(struct vfio_pci_device *device, struct nvme_queue *q,
+ struct nvme_completion *cqe)
+{
+ int timeout_ms = NVME_TIMEOUT_MS;
+
+ while (!nvme_cq_consume(device, q, cqe)) {
+ if (timeout_ms-- == 0)
+ return -ETIMEDOUT;
+ usleep(NVME_POLL_INTERVAL_US);
+ }
+
+ nvme_ring_cq_db(device, q);
+
+ return 0;
+}
+
+/* Return the Status Code Type and Status Code of @cqe. */
+static u16 nvme_status(const struct nvme_completion *cqe)
+{
+ return (le16_to_cpu(cqe->status) >> 1) & NVME_SCT_SC_MASK;
+}
+
+/*
+ * The driver overwrites the first max_memcpy_size bytes of the namespace. Do
+ * not use a device that holds data you want to keep.
+ */
+static int nvme_probe(struct vfio_pci_device *device)
+{
+ u32 class_code;
+
+ class_code = vfio_pci_config_readl(device, PCI_CLASS_REVISION) >> 8;
+
+ return class_code == PCI_CLASS_STORAGE_EXPRESS ? 0 : -EINVAL;
+}
+
+static u32 nvme_submit_admin_cmd(struct vfio_pci_device *device,
+ struct nvme_command *cmd)
+{
+ struct nvme_state *state = to_nvme_state(device);
+ struct nvme_completion cqe = {};
+ int ret;
+
+ nvme_sq_submit(device, &state->adminq, cmd);
+ ret = nvme_cq_wait(device, &state->adminq, &cqe);
+ VFIO_ASSERT_EQ(ret, 0, "Admin command 0x%x timed out",
+ cmd->common.opcode);
+ VFIO_ASSERT_EQ(nvme_status(&cqe), 0,
+ "Admin command 0x%x failed with status 0x%x",
+ cmd->common.opcode, nvme_status(&cqe));
+
+ return le32_to_cpu(cqe.result.u32);
+}
+
+/* Return the number of I/O queue pairs that the controller allocated. */
+static u32 nvme_set_num_queues(struct vfio_pci_device *device, u16 nr_queues)
+{
+ struct nvme_command cmd = {};
+ u32 nr_sqs, nr_cqs;
+ u32 result;
+
+ cmd.features.opcode = nvme_admin_set_features;
+ cmd.features.fid = cpu_to_le32(NVME_FEAT_NUM_QUEUES);
+ cmd.features.dword11 = cpu_to_le32((u32)(nr_queues - 1) << 16 |
+ (nr_queues - 1));
+
+ /* Dword 0 holds zero-based counts: SQs in bits 15:0, CQs in bits 31:16. */
+ result = nvme_submit_admin_cmd(device, &cmd);
+ nr_sqs = (result & 0xffff) + 1;
+ nr_cqs = (result >> 16) + 1;
+
+ /* A queue pair needs one SQ and one CQ. */
+ return min(nr_sqs, nr_cqs);
+}
+
+static void nvme_identify(struct vfio_pci_device *device, u32 nsid, u8 cns,
+ void *buf)
+{
+ struct nvme_command cmd = {};
+
+ cmd.identify.opcode = nvme_admin_identify;
+ cmd.identify.nsid = cpu_to_le32(nsid);
+ cmd.identify.dptr.prp1 = cpu_to_le64(to_iova(device, buf));
+ cmd.identify.cns = cns;
+
+ nvme_submit_admin_cmd(device, &cmd);
+}
+
+static void nvme_enable_irqs(struct vfio_pci_device *device)
+{
+ /*
+ * Admin completions always use vector 0. Require at least two MSI-X
+ * vectors so I/O completions use vector 1 and admin commands never
+ * signal the eventfd that the tests check.
+ */
+ VFIO_ASSERT_GE(device->msix_info.count, 2,
+ "Device must support at least 2 MSI-X vectors");
+
+ device->driver.msi = NVME_IO_MSIX_VEC;
+ vfio_pci_msix_enable(device, 0, NVME_IO_MSIX_VEC + 1);
+}
+
+/*
+ * Poll CSTS.RDY until it equals @rdy. The controller sets RDY after CC.EN
+ * changes from 0 to 1, and clears it after CC.EN changes from 1 to 0. Wait for
+ * the CAP.TO worst case in 500 ms units, and at least 5 seconds.
+ */
+static int nvme_wait_rdy(struct vfio_pci_device *device, bool rdy)
+{
+ void *bar0 = device->bars[0].vaddr;
+ int timeout_ms;
+ u32 csts;
+ u64 cap;
+
+ cap = readq(bar0 + NVME_REG_CAP);
+ timeout_ms = max(NVME_TIMEOUT_MS,
+ NVME_CAP_TIMEOUT(cap) * NVME_CAP_TIMEOUT_UNIT_MS);
+
+ for (;;) {
+ csts = readl(bar0 + NVME_REG_CSTS);
+ /* A function that does not respond returns all ones. */
+ if (csts == ~0U)
+ return -ENODEV;
+ if (rdy && (csts & NVME_CSTS_CFS))
+ return -EIO;
+ if (!!(csts & NVME_CSTS_RDY) == rdy)
+ return 0;
+ if (timeout_ms-- == 0)
+ return -ETIMEDOUT;
+ usleep(NVME_POLL_INTERVAL_US);
+ }
+}
+
+static int nvme_disable_ctrl(struct vfio_pci_device *device)
+{
+ void *bar0 = device->bars[0].vaddr;
+ u32 cc;
+
+ cc = readl(bar0 + NVME_REG_CC);
+ if (cc & NVME_CC_ENABLE)
+ writel(cc & ~NVME_CC_ENABLE, bar0 + NVME_REG_CC);
+
+ return nvme_wait_rdy(device, false);
+}
+
+static void nvme_enable_ctrl(struct vfio_pci_device *device)
+{
+ struct nvme_state *state = to_nvme_state(device);
+ void *bar0 = device->bars[0].vaddr;
+ u32 aqa, cc;
+ u64 cap;
+ int ret;
+
+ VFIO_ASSERT_NOT_NULL(bar0);
+
+ vfio_pci_cmd_set(device, PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
+ PCI_COMMAND_INTX_DISABLE);
+
+ ret = nvme_disable_ctrl(device);
+ VFIO_ASSERT_EQ(ret, 0, "Controller did not disable (CSTS 0x%x)",
+ readl(bar0 + NVME_REG_CSTS));
+
+ cap = readq(bar0 + NVME_REG_CAP);
+ VFIO_ASSERT_TRUE(NVME_CAP_CSS(cap) & NVME_CAP_CSS_NVM,
+ "Controller does not support the NVM command set");
+ VFIO_ASSERT_EQ(NVME_CAP_MPSMIN(cap), 0,
+ "Controller does not support 4 KiB memory pages");
+ state->db_stride = 4 << NVME_CAP_STRIDE(cap);
+
+ nvme_queue_init(device, &state->adminq, NVME_ADMIN_QID,
+ NVME_ADMIN_QUEUE_DEPTH,
+ offsetof(struct nvme_state, admin_sq),
+ offsetof(struct nvme_state, admin_cq));
+ nvme_queue_init(device, &state->msiq, NVME_MSI_QID, NVME_MSI_QUEUE_DEPTH,
+ offsetof(struct nvme_state, msi_sq),
+ offsetof(struct nvme_state, msi_cq));
+
+ /*
+ * No command creates the admin queue. Its depth and ring addresses go
+ * into controller registers instead.
+ */
+ aqa = (NVME_ADMIN_QUEUE_DEPTH - 1) << 16 | (NVME_ADMIN_QUEUE_DEPTH - 1);
+ writel(aqa, bar0 + NVME_REG_AQA);
+ writeq(to_iova(device, state->admin_sq), bar0 + NVME_REG_ASQ);
+ writeq(to_iova(device, state->admin_cq), bar0 + NVME_REG_ACQ);
+
+ /*
+ * Configure the NVM command set, 4 KiB memory pages, round-robin
+ * arbitration, and 64-byte SQ and 16-byte CQ entries. The PRP math in
+ * nvme_set_prp() depends on 4 KiB pages. Then enable the controller.
+ */
+ cc = NVME_CC_CSS_NVM | NVME_CC_AMS_RR | NVME_CC_IOSQES | NVME_CC_IOCQES;
+ writel(cc, bar0 + NVME_REG_CC);
+ writel(cc | NVME_CC_ENABLE, bar0 + NVME_REG_CC);
+
+ ret = nvme_wait_rdy(device, true);
+ VFIO_ASSERT_EQ(ret, 0, "Controller did not become ready (CSTS 0x%x)",
+ readl(bar0 + NVME_REG_CSTS));
+}
+
+static void nvme_create_io_queue(struct vfio_pci_device *device,
+ struct nvme_queue *q, bool ien)
+{
+ u16 flags = NVME_QUEUE_PHYS_CONTIG;
+ struct nvme_command cmd = {};
+
+ if (ien)
+ flags |= NVME_CQ_IRQ_ENABLED;
+
+ cmd.create_cq.opcode = nvme_admin_create_cq;
+ cmd.create_cq.prp1 = cpu_to_le64(to_iova(device, nvme_cq(device, q)));
+ cmd.create_cq.cqid = cpu_to_le16(q->qid);
+ cmd.create_cq.qsize = cpu_to_le16(q->depth - 1);
+ cmd.create_cq.cq_flags = cpu_to_le16(flags);
+ cmd.create_cq.irq_vector = cpu_to_le16(ien ? device->driver.msi : 0);
+ nvme_submit_admin_cmd(device, &cmd);
+
+ memset(&cmd, 0, sizeof(cmd));
+ cmd.create_sq.opcode = nvme_admin_create_sq;
+ cmd.create_sq.prp1 = cpu_to_le64(to_iova(device, nvme_sq(device, q)));
+ cmd.create_sq.sqid = cpu_to_le16(q->qid);
+ cmd.create_sq.qsize = cpu_to_le16(q->depth - 1);
+ cmd.create_sq.sq_flags = cpu_to_le16(NVME_QUEUE_PHYS_CONTIG);
+ cmd.create_sq.cqid = cpu_to_le16(q->qid);
+ nvme_submit_admin_cmd(device, &cmd);
+}
+
+/*
+ * Split the region from poll_rings to its end evenly between the polled
+ * queues. Each queue gets the largest depth that fits, up to the CAP.MQES
+ * limit.
+ */
+static void nvme_init_pollqs(struct vfio_pci_device *device)
+{
+ u64 offset = offsetof(struct nvme_state, poll_rings);
+ struct nvme_state *state = to_nvme_state(device);
+ u64 bytes, sq_bytes, cq_bytes, cap;
+ u32 depth, i;
+
+ cap = readq(device->bars[0].vaddr + NVME_REG_CAP);
+ bytes = (device->driver.region.size - offset) / state->nr_pollqs;
+ VFIO_ASSERT_GT(bytes, 2 * SZ_4K);
+ depth = min_t(u64, NVME_CAP_MQES(cap) + 1, (bytes - 2 * SZ_4K) /
+ (sizeof(struct nvme_command) +
+ sizeof(struct nvme_completion)));
+ VFIO_ASSERT_GT(depth, 1);
+
+ sq_bytes = ALIGN((u64)depth * sizeof(struct nvme_command), SZ_4K);
+ cq_bytes = ALIGN((u64)depth * sizeof(struct nvme_completion), SZ_4K);
+
+ for (i = 0; i < state->nr_pollqs; i++) {
+ nvme_queue_init(device, &state->pollq[i], NVME_MSI_QID + 1 + i,
+ depth, offset, offset + sq_bytes);
+ offset += sq_bytes + cq_bytes;
+ }
+}
+
+static void nvme_create_io_queues(struct vfio_pci_device *device)
+{
+ struct nvme_state *state = to_nvme_state(device);
+ u32 granted, i;
+ u32 last_qid;
+ u64 db_end;
+
+ granted = nvme_set_num_queues(device, 1 + NVME_MAX_POLL_QUEUES);
+ VFIO_ASSERT_GE(granted, 2,
+ "Controller granted %u I/O queue pairs, need at least 2 (1 MSI + 1 polled)",
+ granted);
+ state->nr_pollqs = min_t(u32, granted - 1, NVME_MAX_POLL_QUEUES);
+
+ /* The CQ head doorbell of the last queue is the highest one. */
+ last_qid = NVME_MSI_QID + state->nr_pollqs;
+ db_end = nvme_db_offset(state, last_qid, true) + sizeof(u32);
+ VFIO_ASSERT_LE(db_end, device->bars[0].info.size);
+
+ nvme_init_pollqs(device);
+
+ nvme_create_io_queue(device, &state->msiq, true);
+ for (i = 0; i < state->nr_pollqs; i++)
+ nvme_create_io_queue(device, &state->pollq[i], false);
+}
+
+/* Use the first active namespace for the memcpy commands. */
+static void nvme_find_nsid(struct vfio_pci_device *device)
+{
+ struct nvme_state *state = to_nvme_state(device);
+
+ /* The list holds the active NSIDs above 0, in increasing order. */
+ nvme_identify(device, 0, NVME_ID_CNS_NS_ACTIVE_LIST, state->ns_list);
+ state->nsid = le32_to_cpu(state->ns_list[0]);
+ VFIO_ASSERT_NE(state->nsid, 0, "Controller has no active namespace");
+}
+
+/*
+ * Record the logical block size of the namespace. The LBA Format that FLBAS
+ * selects reports it as a power-of-two shift.
+ */
+static void nvme_set_ns_geometry(struct nvme_state *state)
+{
+ struct nvme_lbaf *lbaf;
+
+ lbaf = &state->id_ns.lbaf[nvme_lbaf_index(state->id_ns.flbas)];
+
+ VFIO_ASSERT_NE(le64_to_cpu(state->id_ns.nsze), 0,
+ "Namespace %u has size 0", state->nsid);
+ VFIO_ASSERT_EQ(le16_to_cpu(lbaf->ms), 0,
+ "Namespace %u has metadata, format it without metadata",
+ state->nsid);
+ VFIO_ASSERT_GE(lbaf->ds, NVME_MIN_LBA_SHIFT,
+ "Namespace %u has an invalid LBA data size", state->nsid);
+ VFIO_ASSERT_LE(lbaf->ds, NVME_MAX_LBA_SHIFT,
+ "Namespace %u has logical blocks larger than 4 KiB",
+ state->nsid);
+
+ state->lba_shift = lbaf->ds;
+}
+
+static void nvme_set_limits(struct vfio_pci_device *device)
+{
+ struct nvme_state *state = to_nvme_state(device);
+ u64 max_size, nsze;
+ u8 mdts;
+
+ /*
+ * The controller accepts at most 2^MDTS pages of CAP.MPSMIN size per
+ * command, and MDTS 0 means no limit. CAP.MPSMIN is 0, which makes a
+ * page 4 KiB. Both limits are powers of two of at least 4 KiB, so the
+ * result is a whole number of logical blocks.
+ */
+ mdts = state->id_ctrl.mdts;
+ max_size = NVME_MAX_XFER_SIZE;
+ if (mdts && mdts < 32)
+ max_size = min_t(u64, max_size, (u64)SZ_4K << mdts);
+
+ /* All memcpy operations use the logical blocks from LBA 0. */
+ nsze = le64_to_cpu(state->id_ns.nsze);
+ if (nsze < max_size >> state->lba_shift)
+ max_size = nsze << state->lba_shift;
+
+ device->driver.max_memcpy_size = max_size;
+ /* A ring of depth entries holds at most depth - 1 commands. */
+ device->driver.max_memcpy_count = state->nr_pollqs *
+ (state->pollq[0].depth - 1);
+}
+
+static void nvme_init(struct vfio_pci_device *device)
+{
+ struct nvme_state *state = to_nvme_state(device);
+
+ VFIO_ASSERT_GE(device->driver.region.size, sizeof(*state));
+ memset(state, 0, sizeof(*state));
+
+ nvme_enable_irqs(device);
+ nvme_enable_ctrl(device);
+
+ nvme_identify(device, 0, NVME_ID_CNS_CTRL, &state->id_ctrl);
+ nvme_find_nsid(device);
+ nvme_identify(device, state->nsid, NVME_ID_CNS_NS, &state->id_ns);
+ nvme_set_ns_geometry(state);
+ nvme_create_io_queues(device);
+ nvme_set_limits(device);
+}
+
+static void nvme_remove(struct vfio_pci_device *device)
+{
+ nvme_disable_ctrl(device);
+ vfio_pci_cmd_clear(device, PCI_COMMAND_MASTER);
+ vfio_pci_msix_disable(device);
+}
+
+/*
+ * Return the controller to a working state after a failed command. On some
+ * controllers, a PCIe target abort, which the unmapped-IOVA tests trigger on
+ * purpose, leaves the submission queue in an error state that only a Function
+ * Level Reset clears. Disable the controller first so it stops DMA before the
+ * reset arrives.
+ */
+static void nvme_recover(struct vfio_pci_device *device)
+{
+ nvme_disable_ctrl(device);
+ vfio_pci_device_reset(device);
+ vfio_pci_msix_reenable(device, 0, device->driver.msi + 1);
+ nvme_enable_ctrl(device);
+ nvme_create_io_queues(device);
+}
+
+/*
+ * prp1 points at @buf. If the transfer reaches one more page, prp2 points at
+ * it. If it reaches more, prp2 points at @prp_list, which holds the address
+ * of each page after the first. CC.MPS is 0, which makes a page 4 KiB.
+ */
+static void nvme_set_prp(struct vfio_pci_device *device,
+ struct nvme_command *cmd, iova_t buf, u64 size,
+ __le64 *prp_list)
+{
+ u64 first = SZ_4K - (buf & (SZ_4K - 1));
+ u64 nr_entries, i;
+
+ VFIO_ASSERT_TRUE(IS_ALIGNED(buf, 4), "PRP1 0x%lx is not dword aligned",
+ buf);
+ cmd->rw.dptr.prp1 = cpu_to_le64(buf);
+ if (size <= first)
+ return;
+
+ nr_entries = DIV_ROUND_UP(size - first, SZ_4K);
+ VFIO_ASSERT_LE(nr_entries, NVME_PRP_LIST_ENTRIES);
+ for (i = 0; i < nr_entries; i++)
+ prp_list[i] = cpu_to_le64(buf + first + i * SZ_4K);
+
+ if (nr_entries == 1)
+ cmd->rw.dptr.prp2 = prp_list[0];
+ else
+ cmd->rw.dptr.prp2 = cpu_to_le64(to_iova(device, prp_list));
+}
+
+/*
+ * Build a read or write command covering @size bytes from LBA 0, transferring
+ * to or from @buf. @size must be a whole number of logical blocks.
+ */
+static void nvme_prep_rw_cmd(struct vfio_pci_device *device,
+ struct nvme_command *cmd, u8 opcode,
+ iova_t buf, u64 size, __le64 *prp_list)
+{
+ struct nvme_state *state = to_nvme_state(device);
+
+ VFIO_ASSERT_GT(size, 0);
+ VFIO_ASSERT_TRUE(IS_ALIGNED(size, 1ULL << state->lba_shift));
+
+ memset(cmd, 0, sizeof(*cmd));
+ cmd->rw.opcode = opcode;
+ cmd->rw.nsid = cpu_to_le32(state->nsid);
+ nvme_set_prp(device, cmd, buf, size, prp_list);
+ cmd->rw.length = cpu_to_le16((size >> state->lba_shift) - 1);
+}
+
+static int nvme_io_wait(struct vfio_pci_device *device, struct nvme_queue *q)
+{
+ struct nvme_completion cqe;
+ int ret;
+
+ ret = nvme_cq_wait(device, q, &cqe);
+ if (ret)
+ return ret;
+
+ return nvme_status(&cqe) ? -EIO : 0;
+}
+
+/*
+ * Copy @src to @dst through the logical blocks from LBA 0 of the namespace.
+ * A write stores @src first. Then the driver queues @count reads of those
+ * blocks into @dst, spread over the polled queues.
+ *
+ * Unlike the vfio_pci_driver_ops contract, this function does not return at
+ * once. It waits for the write to complete. The controller can run the
+ * commands of a queue in any order, and the reads must see the data of the
+ * write. All @count reads are in flight when this function returns.
+ */
+static void nvme_memcpy_start(struct vfio_pci_device *device,
+ iova_t src, iova_t dst, u64 size, u64 count)
+{
+ struct nvme_state *state = to_nvme_state(device);
+ struct nvme_command cmd;
+ u64 i;
+
+ nvme_prep_rw_cmd(device, &cmd, nvme_cmd_write, src, size,
+ state->prp_src);
+ nvme_sq_submit(device, &state->pollq[0], &cmd);
+ state->memcpy_err = nvme_io_wait(device, &state->pollq[0]);
+ if (state->memcpy_err)
+ return;
+
+ nvme_prep_rw_cmd(device, &cmd, nvme_cmd_read, dst, size,
+ state->prp_dst);
+ for (i = 0; i < count; i++)
+ nvme_sq_push(device, &state->pollq[i % state->nr_pollqs], &cmd);
+ for (i = 0; i < min_t(u64, count, state->nr_pollqs); i++)
+ nvme_ring_sq_db(device, &state->pollq[i]);
+}
+
+static int nvme_memcpy_wait(struct vfio_pci_device *device)
+{
+ struct nvme_state *state = to_nvme_state(device);
+ int ret = state->memcpy_err;
+ u32 i;
+
+ for (i = 0; !ret && i < state->nr_pollqs; i++) {
+ while (!ret && state->pollq[i].inflight)
+ ret = nvme_io_wait(device, &state->pollq[i]);
+ }
+
+ if (ret)
+ nvme_recover(device);
+
+ return ret;
+}
+
+static void nvme_send_msi(struct vfio_pci_device *device)
+{
+ struct nvme_state *state = to_nvme_state(device);
+ struct nvme_command cmd = {};
+
+ cmd.common.opcode = nvme_cmd_flush;
+ cmd.common.nsid = cpu_to_le32(state->nsid);
+
+ nvme_sq_submit(device, &state->msiq, &cmd);
+ VFIO_ASSERT_EQ(nvme_io_wait(device, &state->msiq), 0);
+}
+
+const struct vfio_pci_driver_ops nvme_ops = {
+ .name = "nvme",
+ .region_size = sizeof(struct nvme_state) + NVME_POLL_RINGS_SIZE,
+ .probe = nvme_probe,
+ .init = nvme_init,
+ .remove = nvme_remove,
+ .memcpy_start = nvme_memcpy_start,
+ .memcpy_wait = nvme_memcpy_wait,
+ .send_msi = nvme_send_msi,
+};
diff --git a/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.h b/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.h
new file mode 120000
index 000000000000..32258e9c9311
--- /dev/null
+++ b/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.h
@@ -0,0 +1 @@
+../../../../../../../include/linux/nvme.h
\ No newline at end of file
diff --git a/tools/testing/selftests/vfio/lib/libvfio.mk b/tools/testing/selftests/vfio/lib/libvfio.mk
index eff95828719e..9d33d29043c2 100644
--- a/tools/testing/selftests/vfio/lib/libvfio.mk
+++ b/tools/testing/selftests/vfio/lib/libvfio.mk
@@ -18,6 +18,7 @@ endif
LIBVFIO_C += drivers/mlx5/mlx5.c
LIBVFIO_C += drivers/nv_falcon/nv_falcon.c
LIBVFIO_C += drivers/igb/igb.c
+LIBVFIO_C += drivers/nvme/nvme.c
LIBVFIO_OUTPUT := $(OUTPUT)/libvfio
diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_driver.c b/tools/testing/selftests/vfio/lib/vfio_pci_driver.c
index b31abfe2c7ff..f54b4ac505dd 100644
--- a/tools/testing/selftests/vfio/lib/vfio_pci_driver.c
+++ b/tools/testing/selftests/vfio/lib/vfio_pci_driver.c
@@ -9,6 +9,7 @@ extern struct vfio_pci_driver_ops ioat_ops;
extern struct vfio_pci_driver_ops mlx5st_ops;
extern struct vfio_pci_driver_ops nv_falcon_ops;
extern struct vfio_pci_driver_ops igb_ops;
+extern struct vfio_pci_driver_ops nvme_ops;
static struct vfio_pci_driver_ops *driver_ops[] = {
#ifdef __x86_64__
@@ -18,6 +19,7 @@ static struct vfio_pci_driver_ops *driver_ops[] = {
&mlx5st_ops,
&nv_falcon_ops,
&igb_ops,
+ &nvme_ops,
};
void vfio_pci_driver_probe(struct vfio_pci_device *device)
--
2.56.1.831.g720f3f5937-goog
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] vfio: selftests: Add NVMe driver
2026-11-24 2:08 [PATCH] vfio: selftests: Add NVMe driver Josh Hilke
@ 2026-11-26 0:12 ` David Matlack
0 siblings, 0 replies; 2+ messages in thread
From: David Matlack @ 2026-11-26 0:12 UTC (permalink / raw)
To: Josh Hilke, David Matlack, Alex Williamson
Cc: kvm, linux-kselftest, linux-doc, linux-kernel
On Mon Nov 23, 2026 at 6:08 PM PST, Josh Hilke wrote:
> The driver overwrites the first max_memcpy_size bytes of the namespace.
> Do not use a device that holds data you want to keep.
I'm worried about this. Today vfio_pci_driver_test skips NVMe devices
("No driver found"), so anyone who already passes an NVMe device to the
VFIO selftests (e.g. to run vfio_pci_device_test) would start losing the
start of namespace 1 after this patch, without changing anything on
their end. On a real disk that's the partition table or filesystem
superblock. A note in the commit message and docs isn't enough
protection for that.
I'd like the driver to refuse to write anywhere that it can't prove is
unused. How about something like this:
- Reserve the first 4 KiB of the namespace for a header (a magic string
plus the number of bytes after the header the driver has claimed),
and do the memcpys in the blocks after it.
- On the first memcpy, read the header. If it's present and covers
max_memcpy_size, proceed.
- Otherwise, check that every byte the driver could write (the header
plus max_memcpy_size, or just the part past what an existing header
already covers) is zero. If it isn't, fail with a message explaining
how to zero the range if the user really wants to use this device.
If it is, write the header and proceed.
That way things just work on a blank namespace (e.g. the QEMU device in
your instructions), reruns on the same namespace just work since the
memcpys never touch the header, and a disk with data on it is refused.
A crash in the middle of a test doesn't matter either. And since the
check happens on the first memcpy, tests that only use send_msi() (e.g.
KVM's irq_test) still work on any NVMe device.
I prototyped this on top of your patch (about 130 lines, mostly one
function called from the first nvme_memcpy_start()) and
vfio_pci_driver_test passes 35/35 on QEMU on a fresh namespace and on a
rerun, and correctly refuses when there's data in the range. The diff is
at the end of this email.
> +/* nvme.h needs uuid_t from libuuid and struct_group(). */
> +#include <uuid/uuid.h>
Samiullah's "vfio: selftests: Remove libuuid dependency" removes libuuid
from the VFIO selftests and adds uuid_t to libvfio, so please drop this
include and rebase on top of that if it lands first.
> +#include "nvme.h"
This fails to build with clang:
nvme.h:524:52: error: '_Static_assert' with no message is a C23 extension [-Werror,-Wc23-extensions]
524 | static_assert(sizeof(struct nvme_id_ns_nvm) == 4096);
include/linux/nvme.h uses the kernel's static_assert(), where the
message is optional, but <assert.h> has already defined static_assert()
as _Static_assert(), which requires one before C23. gcc doesn't
complain, so please build with LLVM=1 too. Something like this before
including nvme.h fixes it:
/*
* nvme.h uses the kernel's static_assert(), which makes the message
* optional. <assert.h> defines static_assert() as _Static_assert(),
* which needs one before C23.
*/
#undef static_assert
#define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
#ifndef __static_assert
#define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
#endif
Also, please Cc linux-nvme and the NVMe maintainers on the next version,
so they're aware that the selftests now depend on include/linux/nvme.h,
and to get their feedback on the driver, especially the scratch header
design above.
[...]
> +static void nvme_enable_irqs(struct vfio_pci_device *device)
> +{
> + /*
> + * Admin completions always use vector 0. Require at least two MSI-X
> + * vectors so I/O completions use vector 1 and admin commands never
> + * signal the eventfd that the tests check.
> + */
> + VFIO_ASSERT_GE(device->msix_info.count, 2,
> + "Device must support at least 2 MSI-X vectors");
Please check this in nvme_probe() instead, and fail the probe rather
than asserting. A device that can't work with the driver shouldn't
probe successfully. msix_info and the BARs are already set up when
probe() runs (ioat_probe() already reads BAR0), so the CAP checks in
nvme_enable_ctrl() (NVM command set, 4 KiB pages) can move to probe()
too. Only the checks that need admin commands (number of queues,
namespace format) have to stay in init().
[...]
> +static void nvme_create_io_queue(struct vfio_pci_device *device,
> + struct nvme_queue *q, bool ien)
nit: ien is hard to read unless you know it's the IEN bit from the spec.
Please spell it out, e.g. irq_enabled.
[...]
> +static void nvme_set_prp(struct vfio_pci_device *device,
> + struct nvme_command *cmd, iova_t buf, u64 size,
> + __le64 *prp_list)
> +{
> + u64 first = SZ_4K - (buf & (SZ_4K - 1));
nit: Please add something like NVME_CTRL_PAGE_SIZE (like the kernel
driver has in drivers/nvme/host/nvme.h) and use it instead of SZ_4K
everywhere the code means the controller's memory page size (CC.MPS),
e.g. here, in the PRP list and queue alignment, and in
NVME_PRP_LIST_ENTRIES. That makes it clear that it's unrelated to the
CPU's page size, which can be 16K or 64K on arm64.
[...]
> + nvme_prep_rw_cmd(device, &cmd, nvme_cmd_read, dst, size,
> + state->prp_dst);
> + for (i = 0; i < count; i++)
> + nvme_sq_push(device, &state->pollq[i % state->nr_pollqs], &cmd);
memcpy_start() is supposed to kick off @count memcpy operations, i.e.
@count DMA reads of @src and @count DMA writes to @dst. This only reads
@src once. Please do @count writes from @src as well. e.g. keep the
first write and the wait for it, and then queue the other @count - 1
writes along with the @count reads. Once the first write completes, the
blocks already hold @src, and the other writes store the same data, so
the reads see @src no matter what order the controller runs them in.
Note that max_memcpy_count will need to account for the extra commands
(2 * @count - 1 in flight instead of @count).
One possible concern with this is SSD wear. memcpy_storm would write
around max_memcpy_count * max_memcpy_size (several GiB) per run on a real
SSD, for each IOMMU mode. That's probably fine on datacenter drives, but
I'm curious what you think.
---
diff --git a/Documentation/driver-api/vfio-selftests.rst b/Documentation/driver-api/vfio-selftests.rst
index b26040c48d5d..567262ac3d55 100644
--- a/Documentation/driver-api/vfio-selftests.rst
+++ b/Documentation/driver-api/vfio-selftests.rst
@@ -44,8 +44,10 @@ includes driver support for the following devices:
Note that using Intel DSA devices requires setting the module parameter
``vfio_pci.disable_denylist=Y`` to allow ``vfio-pci`` to bind to DSA devices.
-Note that the NVMe driver overwrites the start of the first active namespace.
-Do not use an NVMe device that holds data you want to keep.
+Note that the NVMe driver writes to the start of the first active namespace.
+To avoid destroying data, it refuses to write unless that space is all zero,
+or was zeroed and claimed by an earlier run (marked by a header in the first
+4 KiB).
Setting up Devices
------------------
diff --git a/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.c b/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.c
index 01f38b71a46b..04ae89f31cb2 100644
--- a/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.c
+++ b/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.c
@@ -69,6 +69,21 @@
#define NVME_CAP_TIMEOUT_UNIT_MS 500ULL
#define NVME_POLL_INTERVAL_US 1000
+/*
+ * The first NVME_SCRATCH_HDR_SIZE bytes of the namespace hold a header that
+ * marks the namespace as scratch space for VFIO selftests. memcpy commands
+ * use the blocks after it, so the header survives a test that crashes.
+ */
+#define NVME_SCRATCH_HDR_SIZE SZ_4K
+#define NVME_SCRATCH_MAGIC "VFIO selftests NVMe scratch v1"
+#define NVME_SCRATCH_CHUNK SZ_64K
+
+struct nvme_scratch_header {
+ char magic[32];
+ /* Bytes after the header that the driver has verified were zero. */
+ __le64 size;
+};
+
struct nvme_queue {
/* Byte offsets of the submission and completion rings in the region. */
u64 sq_offset;
@@ -113,6 +128,12 @@ struct nvme_state {
/* Power-of-two shift for converting bytes to logical blocks. */
u32 lba_shift;
+ /* First logical block that memcpy commands use, after the header. */
+ u64 data_slba;
+
+ /* Set once nvme_claim_scratch() has verified the scratch space. */
+ bool scratch_claimed;
+
/* Result of the write in nvme_memcpy_start(). */
int memcpy_err;
@@ -132,6 +153,8 @@ struct nvme_state {
*/
__le64 prp_src[NVME_PRP_LIST_ENTRIES] __aligned(SZ_4K);
__le64 prp_dst[NVME_PRP_LIST_ENTRIES] __aligned(SZ_4K);
+ /* Bounce buffer for reading and writing the scratch header and space. */
+ u8 scratch_buf[NVME_SCRATCH_CHUNK] __aligned(SZ_4K);
u8 poll_rings[] __aligned(SZ_4K);
};
@@ -259,10 +282,6 @@ static u16 nvme_status(const struct nvme_completion *cqe)
return (le16_to_cpu(cqe->status) >> 1) & NVME_SCT_SC_MASK;
}
-/*
- * The driver overwrites the first max_memcpy_size bytes of the namespace. Do
- * not use a device that holds data you want to keep.
- */
static int nvme_probe(struct vfio_pci_device *device)
{
u32 class_code;
@@ -570,10 +589,12 @@ static void nvme_set_limits(struct vfio_pci_device *device)
if (mdts && mdts < 32)
max_size = min_t(u64, max_size, (u64)SZ_4K << mdts);
- /* All memcpy operations use the logical blocks from LBA 0. */
- nsze = le64_to_cpu(state->id_ns.nsze);
- if (nsze < max_size >> state->lba_shift)
- max_size = nsze << state->lba_shift;
+ /* All memcpy operations use the logical blocks after the header. */
+ nsze = le64_to_cpu(state->id_ns.nsze) << state->lba_shift;
+ VFIO_ASSERT_GT(nsze, NVME_SCRATCH_HDR_SIZE,
+ "Namespace %u is too small", state->nsid);
+ max_size = min_t(u64, max_size, nsze - NVME_SCRATCH_HDR_SIZE);
+ state->data_slba = NVME_SCRATCH_HDR_SIZE >> state->lba_shift;
device->driver.max_memcpy_size = max_size;
/* A ring of depth entries holds at most depth - 1 commands. */
@@ -652,11 +673,11 @@ static void nvme_set_prp(struct vfio_pci_device *device,
}
/*
- * Build a read or write command covering @size bytes from LBA 0, transferring
+ * Build a read or write command covering @size bytes from @slba, transferring
* to or from @buf. @size must be a whole number of logical blocks.
*/
static void nvme_prep_rw_cmd(struct vfio_pci_device *device,
- struct nvme_command *cmd, u8 opcode,
+ struct nvme_command *cmd, u8 opcode, u64 slba,
iova_t buf, u64 size, __le64 *prp_list)
{
struct nvme_state *state = to_nvme_state(device);
@@ -667,6 +688,7 @@ static void nvme_prep_rw_cmd(struct vfio_pci_device *device,
memset(cmd, 0, sizeof(*cmd));
cmd->rw.opcode = opcode;
cmd->rw.nsid = cpu_to_le32(state->nsid);
+ cmd->rw.slba = cpu_to_le64(slba);
nvme_set_prp(device, cmd, buf, size, prp_list);
cmd->rw.length = cpu_to_le16((size >> state->lba_shift) - 1);
}
@@ -683,8 +705,92 @@ static int nvme_io_wait(struct vfio_pci_device *device, struct nvme_queue *q)
return nvme_status(&cqe) ? -EIO : 0;
}
+/* Synchronously read or write @size bytes at byte @offset of the namespace. */
+static int nvme_scratch_rw(struct vfio_pci_device *device, u8 opcode,
+ u64 offset, u64 size)
+{
+ struct nvme_state *state = to_nvme_state(device);
+ struct nvme_command cmd;
+
+ nvme_prep_rw_cmd(device, &cmd, opcode, offset >> state->lba_shift,
+ to_iova(device, state->scratch_buf), size, state->prp_src);
+ nvme_sq_submit(device, &state->pollq[0], &cmd);
+
+ return nvme_io_wait(device, &state->pollq[0]);
+}
+
+static bool nvme_scratch_buf_is_zero(struct nvme_state *state, u64 size)
+{
+ u64 *p = (u64 *)state->scratch_buf;
+ u64 i;
+
+ for (i = 0; i < size / sizeof(*p); i++) {
+ if (p[i])
+ return false;
+ }
+
+ return true;
+}
+
/*
- * Copy @src to @dst through the logical blocks from LBA 0 of the namespace.
+ * Refuse to overwrite data that the driver did not put there. The namespace
+ * is scratch space if it starts with a header that covers max_memcpy_size.
+ * Otherwise, claim it by checking that every byte the driver could write is
+ * zero, and then writing the header. If the header covers less than
+ * max_memcpy_size, only the bytes past what it covers need to be zero.
+ */
+static void nvme_claim_scratch(struct vfio_pci_device *device)
+{
+ struct nvme_scratch_header *hdr = (void *)to_nvme_state(device)->scratch_buf;
+ struct nvme_state *state = to_nvme_state(device);
+ u64 max_size = device->driver.max_memcpy_size;
+ u64 start = 0, end, offset, size;
+ struct nvme_command cmd;
+ int ret;
+
+ ret = nvme_scratch_rw(device, nvme_cmd_read, 0, NVME_SCRATCH_HDR_SIZE);
+ VFIO_ASSERT_EQ(ret, 0, "Failed to read the scratch header");
+
+ if (!memcmp(hdr->magic, NVME_SCRATCH_MAGIC, sizeof(NVME_SCRATCH_MAGIC))) {
+ if (le64_to_cpu(hdr->size) >= max_size)
+ return;
+
+ start = NVME_SCRATCH_HDR_SIZE + le64_to_cpu(hdr->size);
+ }
+
+ end = NVME_SCRATCH_HDR_SIZE + max_size;
+ for (offset = start; offset < end; offset += size) {
+ size = min_t(u64, end - offset, NVME_SCRATCH_CHUNK);
+
+ ret = nvme_scratch_rw(device, nvme_cmd_read, offset, size);
+ VFIO_ASSERT_EQ(ret, 0, "Failed to read namespace %u", state->nsid);
+
+ VFIO_ASSERT_TRUE(nvme_scratch_buf_is_zero(state, size),
+ "Namespace %u: bytes [0x%lx, 0x%lx) are not all zero.\n"
+ "The NVMe driver only writes to zeroed space, to avoid\n"
+ "destroying data. Zero that range to allow it, e.g.\n"
+ " blkdiscard -z -l %lu /dev/nvmeXn%u\n",
+ state->nsid, start, end, end, state->nsid);
+ }
+
+ memset(state->scratch_buf, 0, NVME_SCRATCH_HDR_SIZE);
+ memcpy(hdr->magic, NVME_SCRATCH_MAGIC, sizeof(NVME_SCRATCH_MAGIC));
+ hdr->size = cpu_to_le64(max_size);
+
+ ret = nvme_scratch_rw(device, nvme_cmd_write, 0, NVME_SCRATCH_HDR_SIZE);
+ VFIO_ASSERT_EQ(ret, 0, "Failed to write the scratch header");
+
+ /* Make sure the header survives a reset or FLR of the controller. */
+ memset(&cmd, 0, sizeof(cmd));
+ cmd.common.opcode = nvme_cmd_flush;
+ cmd.common.nsid = cpu_to_le32(state->nsid);
+ nvme_sq_submit(device, &state->pollq[0], &cmd);
+ VFIO_ASSERT_EQ(nvme_io_wait(device, &state->pollq[0]), 0,
+ "Failed to flush the scratch header");
+}
+
+/*
+ * Copy @src to @dst through the logical blocks after the scratch header.
* A write stores @src first. Then the driver queues @count reads of those
* blocks into @dst, spread over the polled queues.
*
@@ -700,14 +806,19 @@ static void nvme_memcpy_start(struct vfio_pci_device *device,
struct nvme_command cmd;
u64 i;
- nvme_prep_rw_cmd(device, &cmd, nvme_cmd_write, src, size,
+ if (!state->scratch_claimed) {
+ nvme_claim_scratch(device);
+ state->scratch_claimed = true;
+ }
+
+ nvme_prep_rw_cmd(device, &cmd, nvme_cmd_write, state->data_slba, src, size,
state->prp_src);
nvme_sq_submit(device, &state->pollq[0], &cmd);
state->memcpy_err = nvme_io_wait(device, &state->pollq[0]);
if (state->memcpy_err)
return;
- nvme_prep_rw_cmd(device, &cmd, nvme_cmd_read, dst, size,
+ nvme_prep_rw_cmd(device, &cmd, nvme_cmd_read, state->data_slba, dst, size,
state->prp_dst);
for (i = 0; i < count; i++)
nvme_sq_push(device, &state->pollq[i % state->nr_pollqs], &cmd);
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-26 0:13 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 2:08 [PATCH] vfio: selftests: Add NVMe driver Josh Hilke
2026-11-26 0:12 ` David Matlack
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®