Repository navigation
drivers/virtio: Let a virtio-mmio device be restricted to a memory pool. - #22942
Open
DavidMCerdeira wants to merge 1 commit into
Open
DavidMCerdeira wants to merge 1 commit into
DavidMCerdeira wants to merge 1 commit into
Conversation
The MMIO transport assumes the device can access all memory: it allocates the virtqueues and the driver buffers from the kernel heap and hands the buffers of the callers to the device as they are. That does not hold when the device is implemented by another virtual machine of a static partitioning hypervisor, such as Bao, which only shares one memory region with it. All the RAM of NuttX then has to live in that region, exposed to the backend. Add virtio_register_mmio_device_pool(), which registers a device that can only access a pool created with virtio_mmio_pool_create() from such a region. The device gets the memory operations of the pool: alloc and free take the virtqueues and the buffers the drivers allocate from it, and map and unmap bounce the other buffers through it, copying in the direction the caller states. Devices registered with virtio_register_mmio_device() are not affected. The pool is an array of fixed size blocks whose state is kept in the kernel heap, out of the reach of the device, and it can be used from interrupt handlers. Describe the pool in the VirtIO framework documentation. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: David Cerdeira <davidmcerdeira@gmail.com>
DavidMCerdeira
requested review from
jerpelea,
masayuki2009 and
yf13
as code owners
December 5, 2026 16:04
|
| * blocks is kept out of the pool, where the device cannot reach it. | ||
| */ | ||
|
|
||
| struct virtio_mmio_pool_s |
Contributor
There was a problem hiding this comment.
but why not reuse mm_heap like remoetproc/pci transport?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
feat/linux-nuttx-virtio-bouncebranch of bao-demos.drivers/virtio/virtio-mmio.c,include/nuttx/virtio/virtio-mmio.h,drivers/virtio/Kconfigand the VirtIO framework documentation, in one commit. Everything is under the new optionCONFIG_DRIVERS_VIRTIO_MMIO_POOL, which is off by default.virtio_mmio_pool_create(base, size)turns the shared region into a pool, andvirtio_register_mmio_device_pool(regs, irq, pool)registers a device that can only access that pool. Several devices can use the same pool. Devices registered withvirtio_register_mmio_device()are not affected.struct virtio_memory_ops.allocandfreetake the virtqueues and the buffers the drivers allocate from the pool.mapandunmapbounce the other buffers through it, copying in the direction the caller states (enum virtio_dma_map).free_largestreports the largest buffer the pool can provide. These are the hooks the drivers already call for rptun and for address environments, so no driver changes.CONFIG_DRIVERS_VIRTIO_MMIO_POOL_BLOCKSIZEbytes (64 by default) with first fit allocation. A search skips the allocations it meets, so at worst it is one pass over the pool. The state of the blocks is kept in the kernel heap, out of the reach of the device, and holds the size of each allocation. It is protected by a spinlock of the pool, so it can be used from interrupt handlers. When a device reports that it wrote more than what was mapped,unmapcopies back no more than the mapped length.Impact
CONFIG_DRIVERS_VIRTIO_MMIO_POOLandCONFIG_DRIVERS_VIRTIO_MMIO_POOL_BLOCKSIZE.virtio_register_mmio_device_pool().Documentation/os/drivers/special/virtio/framework.rstgets a section on the pool: when it is needed, the two functions, an example and the limits.struct virtqueue), stays private.virtio_get_shm_region()is not implemented for a device with a pool and returns-ENXIO.mm_heap, which rptun uses to bounce buffers, andtlsf) andmempoolkeep their state inside the memory they manage, where the device could change it. The granule allocator (mm/mm_gran) keeps it out of that memory, as this PR does. I tried it and kept the allocator in this PR for two reasons.gran_free()needs the size of the allocation, which thefreeandunmapoperations do not get, so a private table with the sizes is still needed. And the buffers are mapped from interrupt handlers, which needsCONFIG_GRAN_INTR, an option that changes the locking of every user of the granule allocator in the system. I can switch to it if it is preferred.CONFIG_DRIVERS_VIRTIO_MMIO_QUEUE_LEN=64for a 64 KiB pool.Assisted-bytrailer); I have reviewed and tested the change.Testing
I confirm that changes are verified on local setup and works as intended:
rv-virt:netnsh64on QEMU (-M virt), with virtio-mmio serial, rng, net and blk devices, both as modern (-global virtio-mmio.force-legacy=false) and legacy devices, andqemu-armv8a:netnshon QEMU with the same devices. Also NuttX as a guest of the Bao hypervisor on QEMU, RISC-V and ARM64 (see below).rv-virt:netnsh64unchanged: the session below (without thePOOLCHECKlines) gives the same results as before the commit, modern and legacy.qemu-armv8a:netnshpasses the same session, modern and legacy, and also builds and passes it withCONFIG_DRIVERS_VIRTIO_MMIO_POOL=y(no device uses a pool there).rv-virttake a 2 MiB region from the heap, create one pool from it and register every virtio-mmio device withvirtio_register_mmio_device_pool(), and it makes the transport count, on each notification, the descriptors that were made available to the device inside and outside the pool (thePOOLCHECKlines). QEMU can access all the memory of the guest, so this checks that the drivers work through the pool and that their buffers are in it; it does not check isolation. Result, modern and legacy: every descriptor of virtio-rng (device id 4), virtio-serial (3) and virtio-blk (2) is inside the pool, and the session passes. The descriptors of virtio-net (1) are outside, which is the limit stated above. The test-only change is this commit, the tip of the test/virtio-mmio-pool-rv-virt branch of the Bao fork, which is this PR's commit plus that change.unmap, and the drivers other than virtio-serial, virtio-blk and virtio-rng with a pool.Testing logs before change:
This is a new feature that is off by default. Before the change
rv-virt:netnsh64gives the session below without thePOOLCHECKlines, and a virtio-mmio device that can only access one memory region cannot be used unless all the RAM of NuttX is in that region.Testing logs after change:
rv-virt:netnsh64withCONFIG_DRIVERS_VIRTIO_MMIO_POOL=yand the test-only change, modern devices (the legacy run gives the same counts):The file behind the QEMU console device contains
hello-from-nuttx.NuttX console as a guest of Bao on RISC-V (
hello-from-linuxwas written to the pseudo-terminal of the device model):The second sector of the disk image, read on the backend after NuttX wrote it, contains
Hello from NuttX.PR verification Self-Check