drivers/virtio/virtio_mem.c
Source file repositories/reference/linux-study-clean/drivers/virtio/virtio_mem.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/virtio/virtio_mem.c- Extension
.c- Size
- 87215 bytes
- Lines
- 3159
- Domain
- Driver Families
- Bucket
- drivers/virtio
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- 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
linux/virtio.hlinux/virtio_mem.hlinux/workqueue.hlinux/slab.hlinux/module.hlinux/mm.hlinux/memory_hotplug.hlinux/memory.hlinux/hrtimer.hlinux/crash_dump.hlinux/mutex.hlinux/bitmap.hlinux/lockdep.hlinux/log2.hlinux/vmalloc.hlinux/suspend.hacpi/acpi_numa.h
Detected Declarations
struct virtio_memenum virtio_mem_sbm_mb_stateenum virtio_mem_bbm_bb_statefunction register_virtio_mem_devicefunction unregister_virtio_mem_devicefunction virtio_mem_phys_to_mb_idfunction virtio_mem_mb_id_to_physfunction virtio_mem_phys_to_bb_idfunction virtio_mem_bb_id_to_physfunction virtio_mem_phys_to_sb_idfunction virtio_mem_bbm_set_bb_statefunction virtio_mem_bbm_get_bb_statefunction virtio_mem_bbm_bb_states_prepare_next_bbfunction virtio_mem_sbm_get_mb_statefunction virtio_mem_sbm_mb_states_prepare_next_mbfunction virtio_mem_sbm_set_sb_pluggedfunction virtio_mem_sbm_set_sb_unpluggedfunction virtio_mem_sbm_test_sb_pluggedfunction virtio_mem_sbm_test_sb_unpluggedfunction virtio_mem_sbm_first_unplugged_sbfunction virtio_mem_sbm_sb_states_prepare_next_mbfunction virtio_mem_could_add_memoryfunction virtio_mem_add_memoryfunction virtio_mem_add_memoryfunction virtio_mem_add_memoryfunction virtio_mem_remove_memoryfunction virtio_mem_remove_memoryfunction virtio_mem_offline_and_remove_memoryfunction virtio_mem_offline_and_remove_memoryfunction Tryfunction virtio_mem_offline_and_remove_memoryfunction virtio_mem_retryfunction virtio_mem_translate_node_idfunction fromfunction virtio_mem_contains_rangefunction virtio_mem_sbm_notify_going_onlinefunction virtio_mem_sbm_notify_offlinefunction virtio_mem_sbm_notify_onlinefunction virtio_mem_sbm_notify_going_offlinefunction virtio_mem_sbm_notify_cancel_offlinefunction virtio_mem_bbm_notify_going_offlinefunction virtio_mem_bbm_notify_cancel_offlinefunction add_memoryfunction virtio_mem_pm_notifier_cbfunction virtio_mem_set_fake_offlinefunction virtio_mem_clear_fake_offlinefunction virtio_mem_fake_onlinefunction PageDirty
Annotated Snippet
struct virtio_mem {
struct virtio_device *vdev;
/* We might first have to unplug all memory when starting up. */
bool unplug_all_required;
/* Workqueue that processes the plug/unplug requests. */
struct work_struct wq;
atomic_t wq_active;
atomic_t config_changed;
/* Virtqueue for guest->host requests. */
struct virtqueue *vq;
/* Wait for a host response to a guest request. */
wait_queue_head_t host_resp;
/* Space for one guest request and the host response. */
struct virtio_mem_req req;
struct virtio_mem_resp resp;
/* The current size of the device. */
uint64_t plugged_size;
/* The requested size of the device. */
uint64_t requested_size;
/* The device block size (for communicating with the device). */
uint64_t device_block_size;
/* The determined node id for all memory of the device. */
int nid;
/* Physical start address of the memory region. */
uint64_t addr;
/* Maximum region size in bytes. */
uint64_t region_size;
/* Usable region size in bytes. */
uint64_t usable_region_size;
/* The parent resource for all memory added via this device. */
struct resource *parent_resource;
/*
* Copy of "System RAM (virtio_mem)" to be used for
* add_memory_driver_managed().
*/
const char *resource_name;
/* Memory group identification. */
int mgid;
/*
* We don't want to add too much memory if it's not getting onlined,
* to avoid running OOM. Besides this threshold, we allow to have at
* least two offline blocks at a time (whatever is bigger).
*/
#define VIRTIO_MEM_DEFAULT_OFFLINE_THRESHOLD (1024 * 1024 * 1024)
atomic64_t offline_size;
uint64_t offline_threshold;
/* If set, the driver is in SBM, otherwise in BBM. */
bool in_sbm;
union {
struct {
/* Id of the first memory block of this device. */
unsigned long first_mb_id;
/* Id of the last usable memory block of this device. */
unsigned long last_usable_mb_id;
/* Id of the next memory bock to prepare when needed. */
unsigned long next_mb_id;
/* The subblock size. */
uint64_t sb_size;
/* The number of subblocks per Linux memory block. */
uint32_t sbs_per_mb;
/*
* Some of the Linux memory blocks tracked as "partially
* plugged" are completely unplugged and can be offlined
* and removed -- which previously failed.
*/
bool have_unplugged_mb;
/* Summary of all memory block states. */
unsigned long mb_count[VIRTIO_MEM_SBM_MB_COUNT];
/*
* One byte state per memory block. Allocated via
* vmalloc(). Resized (alloc+copy+free) on demand.
*
* With 128 MiB memory blocks, we have states for 512
* GiB of memory in one 4 KiB page.
*/
Annotation
- Immediate include surface: `linux/virtio.h`, `linux/virtio_mem.h`, `linux/workqueue.h`, `linux/slab.h`, `linux/module.h`, `linux/mm.h`, `linux/memory_hotplug.h`, `linux/memory.h`.
- Detected declarations: `struct virtio_mem`, `enum virtio_mem_sbm_mb_state`, `enum virtio_mem_bbm_bb_state`, `function register_virtio_mem_device`, `function unregister_virtio_mem_device`, `function virtio_mem_phys_to_mb_id`, `function virtio_mem_mb_id_to_phys`, `function virtio_mem_phys_to_bb_id`, `function virtio_mem_bb_id_to_phys`, `function virtio_mem_phys_to_sb_id`.
- Atlas domain: Driver Families / drivers/virtio.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.