tools/virtio/vringh_test.c
Source file repositories/reference/linux-study-clean/tools/virtio/vringh_test.c
File Facts
- System
- Linux kernel
- Corpus path
tools/virtio/vringh_test.c- Extension
.c- Size
- 20671 bytes
- Lines
- 764
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
sched.herr.hlinux/kernel.hlinux/err.hlinux/virtio.hlinux/vringh.hlinux/virtio_ring.hlinux/virtio_config.hlinux/uaccess.hsys/types.hsys/stat.hsys/mman.hsys/wait.hfcntl.h
Detected Declarations
struct guest_virtio_devicefunction never_notify_hostfunction never_callback_guestfunction getrange_iovfunction getrange_slowfunction parallel_notify_hostfunction no_notify_hostfunction find_cpusfunction vringh_get_headfunction parallel_testfunction main
Annotated Snippet
struct guest_virtio_device {
struct virtio_device vdev;
int to_host_fd;
unsigned long notifies;
};
static bool parallel_notify_host(struct virtqueue *vq)
{
int rc;
struct guest_virtio_device *gvdev;
gvdev = container_of(vq->vdev, struct guest_virtio_device, vdev);
rc = write(gvdev->to_host_fd, "", 1);
if (rc < 0)
return false;
gvdev->notifies++;
return true;
}
static bool no_notify_host(struct virtqueue *vq)
{
return true;
}
#define NUM_XFERS (10000000)
/* We aim for two "distant" cpus. */
static void find_cpus(unsigned int *first, unsigned int *last)
{
unsigned int i;
*first = -1U;
*last = 0;
for (i = 0; i < 4096; i++) {
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(i, &set);
if (sched_setaffinity(getpid(), sizeof(set), &set) == 0) {
if (i < *first)
*first = i;
if (i > *last)
*last = i;
}
}
}
/* Opencoded version for fast mode */
static inline int vringh_get_head(struct vringh *vrh, u16 *head)
{
u16 avail_idx, i;
int err;
err = get_user(avail_idx, &vrh->vring.avail->idx);
if (err)
return err;
if (vrh->last_avail_idx == avail_idx)
return 0;
/* Only get avail ring entries after they have been exposed by guest. */
virtio_rmb(vrh->weak_barriers);
i = vrh->last_avail_idx & (vrh->vring.num - 1);
err = get_user(*head, &vrh->vring.avail->ring[i]);
if (err)
return err;
vrh->last_avail_idx++;
return 1;
}
static int parallel_test(u64 features,
bool (*getrange)(struct vringh *vrh,
u64 addr, struct vringh_range *r),
bool fast_vringh)
{
void *host_map, *guest_map;
int pipe_ret, fd, mapsize, to_guest[2], to_host[2];
unsigned long xfers = 0, notifies = 0, receives = 0;
unsigned int first_cpu, last_cpu;
cpu_set_t cpu_set;
char buf[128];
/* Create real file to mmap. */
fd = open("/tmp/vringh_test-file", O_RDWR|O_CREAT|O_TRUNC, 0600);
if (fd < 0)
err(1, "Opening /tmp/vringh_test-file");
/* Extra room at the end for some data, and indirects */
Annotation
- Immediate include surface: `sched.h`, `err.h`, `linux/kernel.h`, `linux/err.h`, `linux/virtio.h`, `linux/vringh.h`, `linux/virtio_ring.h`, `linux/virtio_config.h`.
- Detected declarations: `struct guest_virtio_device`, `function never_notify_host`, `function never_callback_guest`, `function getrange_iov`, `function getrange_slow`, `function parallel_notify_host`, `function no_notify_host`, `function find_cpus`, `function vringh_get_head`, `function parallel_test`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.