Skip to content

drivers/virtio: Let a virtio-mmio device be restricted to a memory pool. - #22942

Open
DavidMCerdeira wants to merge 1 commit into
apache:masterfrom
bao-project:feat/virtio-mmio-restricted-pool
Open

DavidMCerdeira wants to merge 1 commit into
apache:masterfrom
bao-project:feat/virtio-mmio-restricted-pool

Conversation

@DavidMCerdeira

Copy link
Copy Markdown
Contributor

Summary

  • Why: the virtio-mmio transport assumes the device can access all the memory of NuttX. 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 holds for QEMU, but not when the device is implemented by another virtual machine of a static partitioning hypervisor, which only shares one memory region with it. This is the case of the Bao hypervisor, where the VirtIO devices are implemented by a backend virtual machine with the Bao VirtIO device model. The only way to run NuttX there today is to place all its RAM in the shared region, which exposes the whole system to the backend. The setup is the linux+nuttx-virtio-bounce demo on the feat/linux-nuttx-virtio-bounce branch of bao-demos.
  • What: drivers/virtio/virtio-mmio.c, include/nuttx/virtio/virtio-mmio.h, drivers/virtio/Kconfig and the VirtIO framework documentation, in one commit. Everything is under the new option CONFIG_DRIVERS_VIRTIO_MMIO_POOL, which is off by default.
  • How, the interface: virtio_mmio_pool_create(base, size) turns the shared region into a pool, and virtio_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 with virtio_register_mmio_device() are not affected.
  • How, the memory operations: a device registered with a pool gets its own struct virtio_memory_ops. alloc and free take the virtqueues and the buffers the drivers allocate from the pool. map and unmap bounce the other buffers through it, copying in the direction the caller states (enum virtio_dma_map). free_largest reports 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.
  • How, the allocator: the pool is an array of blocks of CONFIG_DRIVERS_VIRTIO_MMIO_POOL_BLOCKSIZE bytes (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, unmap copies back no more than the mapped length.

Impact

  • Is new feature added? Is existing feature changed? YES. New feature: virtio-mmio devices that can only access a given memory region. No existing feature is changed.
  • Impact on user (will user need to adapt to change)? NO. The option is off by default, and with it on only the devices registered with the new function use a pool.
  • Impact on build (will build process change)? NO. Two new Kconfig options, CONFIG_DRIVERS_VIRTIO_MMIO_POOL and CONFIG_DRIVERS_VIRTIO_MMIO_POOL_BLOCKSIZE.
  • Impact on hardware (will arch(s) / board(s) / driver(s) change)? NO. No board uses the new function in this PR. A board that wants it creates the pool and registers its devices with virtio_register_mmio_device_pool().
  • Impact on documentation (is update required / provided)? YES, provided. Documentation/os/drivers/special/virtio/framework.rst gets a section on the pool: when it is needed, the two functions, an example and the limits.
  • Impact on security (any sort of implications)? YES, an improvement. With a pool the device can only reach the virtqueues and the buffers that are meant for it. The rest of the memory of NuttX, including the state of the allocator and of the virtqueues (struct virtqueue), stays private.
  • Impact on compatibility (backward/forward/interoperability)? YES, interoperability improves. NuttX can use VirtIO devices whose backend only has access to one shared memory region, as in static partitioning hypervisors such as Bao, without placing all its RAM in that region.
  • Anything else to consider or add? YES, the limits of the feature, an alternative that was considered and the use of AI assistance:
    • A driver only works with a pool if it uses the memory operations for everything it gives to the device. In the tree today, virtio-serial, virtio-ping, virtio-snd (flat builds), virtio-rpmsg and the virtio vsock transport allocate their buffers with them, and virtio-blk, virtio-rng, virtio-i2c, virtio-gpio, virtio-crypto, virtio-rpmb and virtio-spi map the buffers of their callers. These do not yet: virtio-net (its netpkts go to the virtqueues as they are), virtio-input (event buffers inside the driver state), virtio-gpu (control commands and responses on the stack), the virtio 9p transport (buffers of the 9p client) and virtio-snd in protected builds (buffers from the user heap). Those changes need their own PRs and are out of the scope of this one. Of the drivers that do, virtio-serial, virtio-blk and virtio-rng were run with a pool (see Testing).
    • virtio_get_shm_region() is not implemented for a device with a pool and returns -ENXIO.
    • Alternative: an allocator that is already in the tree. The heap allocators (mm_heap, which rptun uses to bounce buffers, and tlsf) and mempool keep 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 the free and unmap operations do not get, so a private table with the sizes is still needed. And the buffers are mapped from interrupt handlers, which needs CONFIG_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.
    • The size of the pool has to fit the virtqueues: with the default queue length they can take most of a small region. The demo sets CONFIG_DRIVERS_VIRTIO_MMIO_QUEUE_LEN=64 for a 64 KiB pool.
    • The commit was developed with AI assistance (see the Assisted-by trailer); I have reviewed and tested the change.

Testing

I confirm that changes are verified on local setup and works as intended:

  • Build Host(s): Ubuntu 22.04.5, x86_64, riscv64-unknown-elf-gcc 13.2.0, aarch64-none-elf-gcc 14.2.1, QEMU 10.0.2.
  • Target(s): rv-virt:netnsh64 on QEMU (-M virt), with virtio-mmio serial, rng, net and blk devices, both as modern (-global virtio-mmio.force-legacy=false) and legacy devices, and qemu-armv8a:netnsh on QEMU with the same devices. Also NuttX as a guest of the Bao hypervisor on QEMU, RISC-V and ARM64 (see below).
  • The tree is master (ca03e6f) with this commit only.
  • Option off (the default), rv-virt:netnsh64 unchanged: the session below (without the POOLCHECK lines) gives the same results as before the commit, modern and legacy. qemu-armv8a:netnsh passes the same session, modern and legacy, and also builds and passes it with CONFIG_DRIVERS_VIRTIO_MMIO_POOL=y (no device uses a pool there).
  • Option on: no board in the tree uses the new function, so I used a test-only change that is not part of this PR. It makes rv-virt take a 2 MiB region from the heap, create one pool from it and register every virtio-mmio device with virtio_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 (the POOLCHECK lines). 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.
  • For reference, the use case this is for: the bao/virtio-bounce branch of the Bao fork has this commit on top of the two commits of drivers/virtio: Accept VIRTIO_F_VERSION_1 in virtio-mmio #22731, which the Bao device model needs, and a change that makes virtio-net map its buffers. The linux+nuttx-virtio-bounce demo runs it as a guest of the Bao hypervisor on QEMU, RISC-V and ARM64, with out-of-tree boards that create one pool per device. There the devices are implemented by another virtual machine that has no access to the memory of NuttX outside the pools, and the console, the block device and the network interface work. None of those other commits is needed by this one.
  • Not tested: real hardware, a device that reports a wrong written length to 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:netnsh64 gives the session below without the POOLCHECK lines, 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:netnsh64 with CONFIG_DRIVERS_VIRTIO_MMIO_POOL=y and the test-only change, modern devices (the legacy run gives the same counts):

nsh> ls /dev
/dev:
 console
 null
 ram0
 random
 telnet
 ttyS0
 ttyV0
 virtblk0
 virtio
 zero
nsh> hexdump /dev/random count=16
[   14.127919] POOLCHECK device id 4: 1 descriptors in the pool, 0 outside
/dev/random at 00000000:
0000: 58 a6 91 98 7a 16 3e e3 50 04 12 d4 dc d0 fb 1b X...z.>.P.......
nsh> echo hello-from-nuttx > /dev/ttyV0
[   22.668761] POOLCHECK device id 3: 1 descriptors in the pool, 0 outside
nsh> mkfatfs /dev/virtblk0
[   28.196577] POOLCHECK device id 2: 1 descriptors in the pool, 0 outside
[   28.198944] POOLCHECK device id 2: 10 descriptors in the pool, 0 outside
[   28.212680] POOLCHECK device id 2: 100 descriptors in the pool, 0 outside
nsh> mount -t vfat /dev/virtblk0 /mnt
nsh> echo virtio-blk-ok > /mnt/test.txt
nsh> cat /mnt/test.txt
virtio-blk-ok
nsh> ping -c 3 10.0.2.2
PING 10.0.2.2 56 bytes of data
[   74.431612] POOLCHECK device id 1: 0 descriptors in the pool, 10 outside
56 bytes from 10.0.2.2: icmp_seq=0 time=5.4 ms
56 bytes from 10.0.2.2: icmp_seq=1 time=0.4 ms
56 bytes from 10.0.2.2: icmp_seq=2 time=1.2 ms
3 packets transmitted, 3 received, 0% packet loss, time 3033 ms

The file behind the QEMU console device contains hello-from-nuttx.

NuttX console as a guest of Bao on RISC-V (hello-from-linux was written to the pseudo-terminal of the device model):

Bao NuttX VirtIO demo
Initializing the virtio console (waiting for the backend)...
virtio console initialized
virtio network interface up
message from the virtio block device: Hello from the disk image of the Linux backend
Type here to send text to the virtio console
message from the virtio console: hello-from-linux

The second sector of the disk image, read on the backend after NuttX wrote it, contains Hello from NuttX.

PR verification Self-Check

  • This PR introduces only one functional change.
  • I have updated all required description fields above.
  • My PR adheres to Contributing Guidelines and Documentation (git commit title and message, coding standard, etc).
  • My PR is still work in progress (not ready for review).
  • My PR is ready for review and can be safely merged into a codebase.

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>
@github-actions github-actions Bot added Area: Drivers Drivers issues Size: L The size of the change in this PR is large labels Dec 5, 2026
@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown

* blocks is kept out of the pool, where the device cannot reach it.
*/

struct virtio_mmio_pool_s

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but why not reuse mm_heap like remoetproc/pci transport?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants