Add VIRTIO_F_DMB (Device Memory Buffer)
PATCH v6 1/1 Add VIRTIO_F_DMB (Device Memory Buffer)
From: Alexander Graf <[email protected]>
Virtio devices assume R/W access of guest RAM to perform DMA into it to access its virt queues and data to operate on. This basic assumption leads to multiple problems:
Confidential Compute - In SEV-SNP or TDX, the host has no direct access
to all of guest RAM. We work around DMA access by having the guest limit DMA access to a common shared memory window between guest and host (swiotlb).
Vhost-user - vhost-user backends require access to all guest memory, not
only the memory they are actually supposed to access. This is fine for trusted backends, but if you want to limit visibility into guest, you need to do weird stunts like bouncing memory in the vmm [1].
To restrict DMA visibility, virtio-iommu allows the guest to open specific windows into guest memory to the device, but it comes with its own bag of problems, such as dynamic allocations and complicated device <-> iommu connections that need to be represented reliably.
Instead, introduce a simple mechanism that lets a device communicate through its own dedicated shared memory region: Device Memory Buffer. A device that offers Device Memory Buffer exposes a shared memory region, owned by the device, in which the driver places the virtqueues and the buffers they reference.
Once VIRTIO_F_DMB is negotiated, an imaginary per-device IOMMU translates bus address N to position N in the region, for every N from 16 up to the region's length, and translates no other address. That covers the virtqueues, the buffers, and memory a request refers to, such as the backing pages a GPU driver attaches to a resource. To process virtqueues the device reads and writes no guest memory outside the region. Addresses that are protocol messages about guest memory, such as balloon page frame numbers, keep the meaning their device type gives them.
Without VIRTIO_F_DMB, a driver passes physical addresses, or addresses that the platform IOMMU translates if it negotiated VIRTIO_F_ACCESS_PLATFORM. VIRTIO_F_DMB is an exception to both for the addresses it reinterprets.
The device reports the region's shmid in dmb_shm_id on PCI and DMBSHMId on MMIO, so the shmid does not collide with device-specific or future transport-specific shared memory regions. The driver locates the region through the existing Shared Memory Regions facility, VIRTIO_F_DMB needs a transport that supports SHM.
The region's shmid only has to differ from the device's other regions and avoid values its device type names; values reserved for future use stay available. virtio-gpu and virtio-scmi assign a fixed shmid to every region, so those sections now exempt the Device Memory Buffer from their rules. A driver reads the region's shmid before negotiation, so the exemption has to hold from the moment the device offers the feature. The device designates the region as soon as it offers VIRTIO_F_DMB and keeps it whether or not the driver accepts.
The device also reports the Device Memory Buffer memory type, in dmb_mem_type on PCI and DMBMemType on MMIO. The only type defined here is VIRTIO_DMB_MEM_TYPE_COHERENT, under which driver and device accesses are mutually visible without cache maintenance. A region is not obliged to be coherent (virtio-gpu already enumerates four cache behaviours for its own regions in VIRTIO_GPU_MAP_CACHE_*), so a driver reads the type before it accepts the feature and declines one it does not know.
[1] https://lore.kernel.org/qemu-devel/[email protected]/T/#t
---
v1 -> v2:
- Replace the fixed reserved shmid 2 and its registry with a shmid the device reports per transport, as Stefan Hajnoczi suggested on v1
- Spell out per-transport compatibility for dmb_shm_id and DMBSHMId
- Set FAILED when the driver cannot find the region, rather than refuse to negotiate
- List VIRTIO_F_DMB under CCW's features reserved for future use
v2 -> v3:
- Require the region to be memory the driver shares with the device
- Require coherency; couple VIRTIO_F_DMB to VIRTIO_F_ORDER_PLATFORM, replacing v2's sentence on the areas' interaction with it
- Reserve offset 0 in the region
- Except the Device Memory Buffer from the shared-region prohibition
- Allow a device to expose further shared memory regions
- Move bit 44 into the queue and feature negotiation reserved list
- Aim two cross-references at the Reserved Feature Bits chapter
v3 -> v4:
- Decouple VIRTIO_F_DMB from VIRTIO_F_ACCESS_PLATFORM and VIRTIO_F_ORDER_PLATFORM
- Describe the mapping as a per-device IOMMU instead of as offsets into the region (Michael)
- Report the region's memory type in the transport (Michael)
- Let a device require either VIRTIO_F_DMB or VIRTIO_F_ACCESS_PLATFORM (Michael)
- Require the region's base address to be aligned (Michael)
- State the property the mapping must have instead of how to map (Michael)
- State the negotiation condition once per normative section (Michael)
- Rewrite description
v4 -> v5:
- Carry the imaginary IOMMU through the normative sections instead of falling back to addresses into a region (Michael)
- Leave the region's first 16 bytes unmapped instead of forbidding address 0 structure by structure, and state the alignment as 16 bytes (Michael)
- Say bus address space rather than IOVA space (Michael asked for DMA address space; neither term appears in the specification, and VIRTIO_F_ACCESS_PLATFORM introduces bus address)
- Exclude notifications rather than IRQ logic, and say that DMB does not make a notification an ordering point (Michael)
- Give the Device Memory Buffer memory type its own subsection and a name, VIRTIO_DMB_MEM_TYPE_COHERENT, and justify the field's extensibility in the description (Michael)
- Require what the device reports about coherency rather than what it ensures about the driver's mapping (Michael)
- Keep RFC 2119 keywords out of the descriptive text and give the bounds, alignment, rejection, length, FAILED and DEVICE_NEEDS_RESET rules a descriptive counterpart (Michael)
- Drop the shmid and further-regions requirements the transport sections already carry (Michael)
- Drop the sentence leaving the driver's mapping to the platform (Michael)
- Say negotiated rather than active, and apply if rather than apply once (Michael)
- Say cache maintenance operations by either side (Michael)
- Except VIRTIO_F_DMB from the requirement to pass only physical addresses when VIRTIO_F_ACCESS_PLATFORM is not offered
- Require the region to be long enough to hold the virtqueues
v5 -> v6:
- Let a device treat an access outside the mapping as an error it cannot recover from and set DEVICE_NEEDS_RESET, or ignore it, instead of requiring the former
- State the mapping's extent once, as bus address N at position N for every N from 16 up to the region's length, instead of saying both that it starts at the first byte and that it skips the first 16 (Matias)
- Define once when an access lies within the mapping, and use that in the driver and the device requirements
- Check the areas when the driver enables a virtqueue rather than when it programs each area's address
- Say that every virtqueue or buffer address the specification calls physical is a bus address into the Device Memory Buffer once VIRTIO_F_DMB has been negotiated (Matias)
- Extend that to memory a request refers to, such as the backing pages a GPU driver attaches to a resource, and say that the device reads and writes no guest memory outside the Device Memory Buffer
- Leave the device's own memory, shared memory regions included, out of that rule
- Except virtqueue, buffer and request memory addresses from both halves of the VIRTIO_F_ACCESS_PLATFORM driver requirement, not only from the one without it
- Keep protocol addresses, such as balloon page frame numbers, free page hints and reports, and memory device plug and unplug ranges, at the meaning their device type gives them
- Designate the Device Memory Buffer when the device offers VIRTIO_F_DMB, keep it the same through negotiation, and make dmb_shm_id and DMBSHMId valid from then, as dmb_mem_type already is
- Require the Device Memory Buffer's shmid to differ from the device's other regions and from every value its device type names, leaving values reserved for future use available, and exclude it from the virtio-gpu and virtio-scmi shmid rules (Matias)
- Except the Device Memory Buffer where the shared memory chapter says a shmid's meaning is device-specific and that regions are addressed by offset
- Size the region for the 16 unmapped bytes, the areas' alignment and both the split and, if offered, the packed layout at maximum queue size, and name those two layouts instead of saying virtqueue layout
- Drop the rule that had the device judge what the driver's platform permits
- Say that the driver places the virtqueues and the buffers in the Device Memory Buffer, rather than that the region holds them (Parav)
conformance.tex | 2 + content.tex | 35 +++- device-types/gpu/description.tex | 6 +- device-types/scmi/description.tex | 6 +- shared-mem.tex | 265 +++++++++++++++++++++++++++++- transport-ccw.tex | 1 + transport-mmio.tex | 29 ++++ transport-pci.tex | 36 ++++ 8 files changed, 366 insertions(+), 14 deletions(-)
conformance.tex +2 -0
@@ -104,6 +104,7 @@ \section{Conformance Targets}\label{sec:Conformance / Conformance Targets} \item \ref{drivernormative:General Initialization And Device Operation / Device Initialization} \item \ref{drivernormative:General Initialization And Device Operation / Device Cleanup} \item \ref{drivernormative:Reserved Feature Bits} +\item \ref{drivernormative:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer} \item \ref{drivernormative:Basic Facilities of a Virtio Device / Device groups / Group administration commands / Device and driver capabilities} \item \ref{drivernormative:Basic Facilities of a Virtio Device / Device groups / Group administration commands / Device resource objects} \item \ref{drivernormative:Basic Facilities of a Virtio Device / Device groups / Group administration commands / Device parts}
@@ -191,6 +192,7 @@ \section{Conformance Targets}\label{sec:Conformance / Conformance Targets} \item \ref{devicenormative:Basic Facilities of a Virtio Device / Packed Virtqueues / The Virtqueue Descriptor Table} \item \ref{devicenormative:Basic Facilities of a Virtio Device / Packed Virtqueues / Scatter-Gather Support} \item \ref{devicenormative:Basic Facilities of a Virtio Device / Shared Memory Regions} +\item \ref{devicenormative:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer} \item \ref{devicenormative:Reserved Feature Bits} \item \ref{devicenormative:Basic Facilities of a Virtio Device / Device groups / Group administration commands / Device and driver capabilities} \item \ref{devicenormative:Basic Facilities of a Virtio Device / Device groups / Group administration commands / Device resource objects}
content.tex +28 -7
@@ -102,10 +102,10 @@ \section{Feature Bits}\label{sec:Basic Facilities of a Virtio Device / Feature B \begin{description} \item[0 to 23, 41, 42 and 50 to 127] Feature bits for the specific device type -\item[24 to 40, and 43] Feature bits reserved for extensions to the queue and +\item[24 to 40, 43 and 44] Feature bits reserved for extensions to the queue and feature negotiation mechanisms, see \ref{sec:Reserved Feature Bits} -\item[44 to 49, and 128 and above] Feature bits reserved for future extensions. +\item[45 to 49, and 128 and above] Feature bits reserved for future extensions. \end{description} \begin{note}
@@ -946,6 +946,15 @@ \chapter{Reserved Feature Bits}\label{sec:Reserved Feature Bits} suspend the device by set the SUSPEND bit to 1. See \ref{sec:Basic Facilities of a Virtio Device / Device Status Field}. + \item[VIRTIO_F_DMB(44)] This feature indicates that the device offers a + Device Memory Buffer: a shared memory region, owned by the device, in + which the driver places the virtqueues and the buffers they reference. + When this feature has been negotiated, every device access to a virtqueue, + to a buffer, or to memory a request refers to is routed through a static, + imaginary, per-device transparent IOMMU which translates a bus address to + the same position in the Device Memory Buffer region. + See \ref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer}~\nameref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer}. + \end{description} \drivernormative{\section}{Reserved Feature Bits}{Reserved Feature Bits}
@@ -953,11 +962,15 @@ \chapter{Reserved Feature Bits}\label{sec:Reserved Feature Bits} A driver MUST accept VIRTIO_F_VERSION_1 if it is offered. A driver MAY fail to operate further if VIRTIO_F_VERSION_1 is not offered. -A driver SHOULD accept VIRTIO_F_ACCESS_PLATFORM if it is offered, and it MUST -then either disable the IOMMU or configure the IOMMU to translate bus addresses -passed to the device into physical addresses in memory. If -VIRTIO_F_ACCESS_PLATFORM is not offered, then a driver MUST pass only physical -addresses to the device. +A driver SHOULD accept VIRTIO_F_ACCESS_PLATFORM if it is offered, and it +MUST then either disable the IOMMU or configure the IOMMU to translate bus +addresses passed to the device into physical addresses in memory. If +VIRTIO_F_ACCESS_PLATFORM is not offered, then a driver MUST pass only +physical addresses to the device. If VIRTIO_F_DMB has been negotiated, +neither of these applies to the address of a virtqueue part, of a buffer or +of memory a request refers to, other than a protocol address, which the +driver MUST instead pass as an address into the Device Memory Buffer (see +\ref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer}). A driver SHOULD accept VIRTIO_F_RING_PACKED if it is offered.
@@ -982,6 +995,10 @@ \chapter{Reserved Feature Bits}\label{sec:Reserved Feature Bits} A driver SHOULD accept VIRTIO_F_NOTIF_CONFIG_DATA if it is offered. +For the driver requirements that apply if VIRTIO_F_DMB has been +negotiated, see +\ref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer}~\nameref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer}. + \devicenormative{\section}{Reserved Feature Bits}{Reserved Feature Bits} A device MUST offer VIRTIO_F_VERSION_1. A device MAY fail to operate further
@@ -1011,6 +1028,10 @@ \chapter{Reserved Feature Bits}\label{sec:Reserved Feature Bits} and presents a PCI SR-IOV capability structure, otherwise it MUST NOT offer VIRTIO_F_SR_IOV. +For the device requirements that apply if VIRTIO_F_DMB has been +negotiated, see +\ref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer}~\nameref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer}. + \section{Legacy Interface: Reserved Feature Bits}\label{sec:Reserved Feature Bits / Legacy Interface: Reserved Feature Bits} Transitional devices MAY offer the following:
device-types/gpu/description.tex +4 -2
@@ -97,8 +97,10 @@ \subsubsection{Events} choose to use a fallback, such as 1024x768 at display 0. The driver SHOULD query all shared memory regions supported by the device. -If the device supports shared memory, the \field{shmid} of a region MUST -(see \ref{sec:Basic Facilities of a Virtio Device / +If the device supports shared memory, the \field{shmid} of a region other +than the Device Memory Buffer (see +\ref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer}) +MUST (see \ref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions}~\nameref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions}) be one of the following:
device-types/scmi/description.tex +4 -2
@@ -247,8 +247,10 @@ \subsubsection{Shared Memory Operation} an SCMI statistics shared memory region using a virtio shared memory region. -If the device implements a shared memory region, the device MUST assign -the corresponding shmid as per the following table: +If the device implements a shared memory region other than the Device +Memory Buffer (see +\ref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer}), +the device MUST assign the corresponding shmid as per the following table: \begin{tabular}{|l|l|} \hline
shared-mem.tex +262 -3
280 lines not on this page · raw message
transport-ccw.tex +1 -0
@@ -610,6 +610,7 @@ \subsection{Features reserved for future use}\label{sec:Virtio Transport Options \item VIRTIO_F_ADMIN_VQ \item VIRTIO_F_RING_RESET \item Shared memory regions including VIRTIO_PMEM_F_SHMEM_REGION +\item VIRTIO_F_DMB \end{itemize}
transport-mmio.tex +29 -0
@@ -248,6 +248,23 @@ \subsection{MMIO Device Register Layout}\label{sec:Virtio Transport Options / Vi apply to the queue selected by writing to \field{QueueSel}. } \hline + \mmioreg{DMBSHMId}{Device Memory Buffer shared memory ID}{0x0c4}{R}{% + Reading from this register returns the \field{shmid} of the shared + memory region that is the Device Memory Buffer (see \ref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer}). + This register is valid whenever the device offers VIRTIO_F_DMB, as + opposed to after VIRTIO_F_DMB has been negotiated, so that the driver + can identify the Device Memory Buffer before it decides whether to + accept the feature. + } + \hline + \mmioreg{DMBMemType}{Device Memory Buffer memory type}{0x0c8}{R}{% + Reading from this register returns the Device Memory Buffer memory + type (see \ref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer / Device Memory Buffer memory type}). + This register is valid whenever the device offers VIRTIO_F_DMB, as + opposed to after VIRTIO_F_DMB has been negotiated, because the driver + reads it in order to decide whether to accept the feature. + } + \hline \mmioreg{ConfigGeneration}{Configuration atomicity value}{0x0fc}{R}{ Reading from this register returns a value describing a version of the device-specific configuration space (see \field{Config}). The driver can then access the configuration space and, when finished, read \field{ConfigGeneration} again.
@@ -313,6 +330,12 @@ \subsection{MMIO Device Register Layout}\label{sec:Virtio Transport Options / Vi when queue reset has completed. (see \ref{sec:Basic Facilities of a Virtio Device / Virtqueues / Virtqueue Reset}). +If the device offers VIRTIO_F_DMB, \field{DMBSHMId} MUST equal the +\field{shmid} of a shared memory region that the device exposes through +\field{SHMSel}, \field{SHMLenLow}, \field{SHMLenHigh}, \field{SHMBaseLow} +and \field{SHMBaseHigh}, and \field{DMBMemType} MUST report a Device Memory Buffer +memory type that this specification defines. + \drivernormative{\subsubsection}{MMIO Device Register Layout}{Virtio Transport Options / Virtio Over MMIO / MMIO Device Register Layout} The driver MUST NOT access memory locations not described in the table \ref{tab:Virtio Transport Options / Virtio Over MMIO / MMIO Device Register Layout}
@@ -367,6 +390,12 @@ \subsection{MMIO Device Register Layout}\label{sec:Virtio Transport Options / Vi that were used before the queue reset. (see \ref{sec:Basic Facilities of a Virtio Device / Virtqueues / Virtqueue Reset}). +The driver MUST NOT read \field{DMBSHMId} unless the device offers +VIRTIO_F_DMB. + +The driver MUST NOT read \field{DMBMemType} unless the device offers +VIRTIO_F_DMB. + \subsection{MMIO-specific Initialization And Device Operation}\label{sec:Virtio Transport Options / Virtio Over MMIO / MMIO-specific Initialization And Device Operation} \subsubsection{Device Initialization}\label{sec:Virtio Transport Options / Virtio Over MMIO / MMIO-specific Initialization And Device Operation / Device Initialization}
transport-pci.tex +36 -0
@@ -325,6 +325,10 @@ \subsubsection{Common configuration structure layout}\label{sec:Virtio Transport /* About the administration virtqueue. */ le16 admin_queue_index; /* read-only for driver */ le16 admin_queue_num; /* read-only for driver */ + + /* About the Device Memory Buffer. */ + le16 dmb_shm_id; /* read-only for driver */ + le16 dmb_mem_type; /* read-only for driver */ }; \end{lstlisting}
@@ -428,6 +432,25 @@ \subsubsection{Common configuration structure layout}\label{sec:Virtio Transport The value 0 indicates no supported administration virtqueues. This field is valid only if VIRTIO_F_ADMIN_VQ has been negotiated. + +\item[\field{dmb_shm_id}] + The device uses this to report the \field{shmid} of the shared + memory region that is the Device Memory Buffer (see + \ref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer}). + This field is read-only for the driver. This field is valid + whenever the device offers VIRTIO_F_DMB, as opposed to after + VIRTIO_F_DMB has been negotiated, so that the driver can identify + the Device Memory Buffer before it decides whether to accept the + feature. + +\item[\field{dmb_mem_type}] + The device uses this to report the Device Memory Buffer memory + type (see + \ref{sec:Basic Facilities of a Virtio Device / Shared Memory Regions / Device Memory Buffer / Device Memory Buffer memory type}). + This field is read-only for the driver. Like + \field{dmb_shm_id}, this field is valid whenever the device + offers VIRTIO_F_DMB, because the driver reads it in order to + decide whether to accept the feature. \end{description} \devicenormative{\paragraph}{Common configuration structure layout}{Virtio Transport Options / Virtio Over PCI Bus / PCI Device Layout / Common configuration structure layout}
@@ -495,6 +518,13 @@ \subsubsection{Common configuration structure layout}\label{sec:Virtio Transport to ensure that indices of valid admin queues fit into a 16 bit range beyond all other virtqueues. +If the device offers VIRTIO_F_DMB, the device MUST present the common +configuration structure with a \field{length} that is large enough to +include the \field{dmb_mem_type} field, and \field{dmb_shm_id} MUST equal +the \field{id} of a VIRTIO_PCI_CAP_SHARED_MEMORY_CFG capability that the +device presents. Since \field{id} in \field{virtio_pci_cap} is 8 bits, +the high-order 8 bits of \field{dmb_shm_id} MUST be zero. + \drivernormative{\paragraph}{Common configuration structure layout}{Virtio Transport Options / Virtio Over PCI Bus / PCI Device Layout / Common configuration structure layout} The driver MUST NOT write to \field{device_feature}, \field{num_queues},
@@ -531,6 +561,12 @@ \subsubsection{Common configuration structure layout}\label{sec:Virtio Transport The driver MAY configure fewer administration virtqueues than supported by the device. +The driver MUST NOT read \field{dmb_shm_id} unless the device offers +VIRTIO_F_DMB. + +The driver MUST NOT read \field{dmb_mem_type} unless the device offers +VIRTIO_F_DMB. + \subsubsection{Notification structure layout}\label{sec:Virtio Transport Options / Virtio Over PCI Bus / PCI Device Layout / Notification capability} The notification location is found using the VIRTIO_PCI_CAP_NOTIFY_CFG
Signature · 1 line
2.50.1