samples/vfio-mdev/mdpy.c
Source file repositories/reference/linux-study-clean/samples/vfio-mdev/mdpy.c
File Facts
- System
- Linux kernel
- Corpus path
samples/vfio-mdev/mdpy.c- Extension
.c- Size
- 17685 bytes
- Lines
- 744
- Domain
- Support Tooling And Documentation
- Bucket
- samples
- Inferred role
- Support Tooling And Documentation: operation-table or driver-model contract
- Status
- pattern 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.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- 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.
- 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/init.hlinux/module.hlinux/kernel.hlinux/slab.hlinux/vmalloc.hlinux/cdev.hlinux/vfio.hlinux/iommu.hlinux/sysfs.hlinux/mdev.hlinux/pci.hdrm/drm_fourcc.hmdpy-defs.h
Detected Declarations
struct mdev_statefunction mdpy_create_config_spacefunction handle_pci_cfg_writefunction mdev_accessfunction mdpy_resetfunction mdpy_init_devfunction mdpy_probefunction mdpy_release_devfunction mdpy_removefunction mdpy_readfunction mdpy_writefunction mdpy_mmapfunction mdpy_ioctl_get_region_infofunction mdpy_get_irq_infofunction mdpy_get_device_infofunction mdpy_query_gfx_planefunction mdpy_ioctlfunction resolution_showfunction mdpy_show_descriptionfunction mdpy_device_releasefunction mdpy_dev_exitmodule init mdpy_dev_init
Annotated Snippet
static const struct file_operations vd_fops = {
.owner = THIS_MODULE,
};
static void mdpy_device_release(struct device *dev)
{
/* nothing */
}
static int __init mdpy_dev_init(void)
{
int ret = 0;
ret = alloc_chrdev_region(&mdpy_devt, 0, MINORMASK + 1, MDPY_NAME);
if (ret < 0) {
pr_err("Error: failed to register mdpy_dev, err: %d\n", ret);
return ret;
}
cdev_init(&mdpy_cdev, &vd_fops);
cdev_add(&mdpy_cdev, mdpy_devt, MINORMASK + 1);
pr_info("%s: major %d\n", __func__, MAJOR(mdpy_devt));
ret = mdev_register_driver(&mdpy_driver);
if (ret)
goto err_cdev;
ret = class_register(&mdpy_class);
if (ret)
goto err_driver;
mdpy_dev.class = &mdpy_class;
mdpy_dev.release = mdpy_device_release;
dev_set_name(&mdpy_dev, "%s", MDPY_NAME);
ret = device_register(&mdpy_dev);
if (ret)
goto err_put;
ret = mdev_register_parent(&mdpy_parent, &mdpy_dev, &mdpy_driver,
mdpy_mdev_types,
ARRAY_SIZE(mdpy_mdev_types));
if (ret)
goto err_device;
return 0;
err_device:
device_del(&mdpy_dev);
err_put:
put_device(&mdpy_dev);
class_unregister(&mdpy_class);
err_driver:
mdev_unregister_driver(&mdpy_driver);
err_cdev:
cdev_del(&mdpy_cdev);
unregister_chrdev_region(mdpy_devt, MINORMASK + 1);
return ret;
}
static void __exit mdpy_dev_exit(void)
{
mdpy_dev.bus = NULL;
mdev_unregister_parent(&mdpy_parent);
device_unregister(&mdpy_dev);
mdev_unregister_driver(&mdpy_driver);
cdev_del(&mdpy_cdev);
unregister_chrdev_region(mdpy_devt, MINORMASK + 1);
class_unregister(&mdpy_class);
}
module_param_named(count, mdpy_driver.max_instances, int, 0444);
MODULE_PARM_DESC(count, "number of " MDPY_NAME " devices");
module_init(mdpy_dev_init)
module_exit(mdpy_dev_exit)
Annotation
- Immediate include surface: `linux/init.h`, `linux/module.h`, `linux/kernel.h`, `linux/slab.h`, `linux/vmalloc.h`, `linux/cdev.h`, `linux/vfio.h`, `linux/iommu.h`.
- Detected declarations: `struct mdev_state`, `function mdpy_create_config_space`, `function handle_pci_cfg_write`, `function mdev_access`, `function mdpy_reset`, `function mdpy_init_dev`, `function mdpy_probe`, `function mdpy_release_dev`, `function mdpy_remove`, `function mdpy_read`.
- Atlas domain: Support Tooling And Documentation / samples.
- Implementation status: pattern 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.
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.