virt/kvm/binary_stats.c
Source file repositories/reference/linux-study-clean/virt/kvm/binary_stats.c
File Facts
- System
- Linux kernel
- Corpus path
virt/kvm/binary_stats.c- Extension
.c- Size
- 4604 bytes
- Lines
- 145
- Domain
- Kernel Services
- Bucket
- virt
- Inferred role
- Kernel Services: implementation source
- Status
- source implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kvm_host.hlinux/kvm.hlinux/errno.hlinux/uaccess.h
Detected Declarations
function kvm_stats_read
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0-only
/*
* KVM binary statistics interface implementation
*
* Copyright 2021 Google LLC
*/
#include <linux/kvm_host.h>
#include <linux/kvm.h>
#include <linux/errno.h>
#include <linux/uaccess.h>
/**
* kvm_stats_read() - Common function to read from the binary statistics
* file descriptor.
*
* @id: identification string of the stats
* @header: stats header for a vm or a vcpu
* @desc: start address of an array of stats descriptors for a vm or a vcpu
* @stats: start address of stats data block for a vm or a vcpu
* @size_stats: the size of stats data block pointed by @stats
* @user_buffer: start address of userspace buffer
* @size: requested read size from userspace
* @offset: the start position from which the content will be read for the
* corresponding vm or vcp file descriptor
*
* The file content of a vm/vcpu file descriptor is now defined as below:
* +-------------+
* | Header |
* +-------------+
* | id string |
* +-------------+
* | Descriptors |
* +-------------+
* | Stats Data |
* +-------------+
* Although this function allows userspace to read any amount of data (as long
* as in the limit) from any position, the typical usage would follow below
* steps:
* 1. Read header from offset 0. Get the offset of descriptors and stats data
* and some other necessary information. This is a one-time work for the
* lifecycle of the corresponding vm/vcpu stats fd.
* 2. Read id string from its offset. This is a one-time work for the lifecycle
* of the corresponding vm/vcpu stats fd.
* 3. Read descriptors from its offset and discover all the stats by parsing
* descriptors. This is a one-time work for the lifecycle of the
* corresponding vm/vcpu stats fd.
* 4. Periodically read stats data from its offset using pread.
*
* Return: the number of bytes that has been successfully read
*/
ssize_t kvm_stats_read(char *id, const struct kvm_stats_header *header,
const struct kvm_stats_desc *desc,
void *stats, size_t size_stats,
char __user *user_buffer, size_t size, loff_t *offset)
{
ssize_t len;
ssize_t copylen;
ssize_t remain = size;
size_t size_desc;
size_t size_header;
void *src;
loff_t pos = *offset;
char __user *dest = user_buffer;
size_header = sizeof(*header);
size_desc = header->num_desc * sizeof(*desc);
len = KVM_STATS_NAME_SIZE + size_header + size_desc + size_stats - pos;
len = min(len, remain);
if (len <= 0)
return 0;
remain = len;
/*
* Copy kvm stats header.
* The header is the first block of content userspace usually read out.
* The pos is 0 and the copylen and remain would be the size of header.
* The copy of the header would be skipped if offset is larger than the
* size of header. That usually happens when userspace reads stats
* descriptors and stats data.
*/
copylen = size_header - pos;
copylen = min(copylen, remain);
if (copylen > 0) {
src = (void *)header + pos;
if (copy_to_user(dest, src, copylen))
return -EFAULT;
remain -= copylen;
pos += copylen;
Annotation
- Immediate include surface: `linux/kvm_host.h`, `linux/kvm.h`, `linux/errno.h`, `linux/uaccess.h`.
- Detected declarations: `function kvm_stats_read`.
- Atlas domain: Kernel Services / virt.
- Implementation status: source implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
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.