lib/kunit/device.c
Source file repositories/reference/linux-study-clean/lib/kunit/device.c
File Facts
- System
- Linux kernel
- Corpus path
lib/kunit/device.c- Extension
.c- Size
- 5230 bytes
- Lines
- 202
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: operation-table or driver-model contract
- Status
- pattern 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.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- 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/device.hlinux/dma-mapping.hkunit/test.hkunit/device.hkunit/resource.hdevice-impl.h
Detected Declarations
struct kunit_devicefunction kunit_bus_initfunction kunit_bus_shutdownfunction kunit_device_releasefunction kunit_device_unregisterexport kunit_driver_createexport kunit_device_register_with_driverexport kunit_device_registerexport kunit_device_unregister
Annotated Snippet
KUNIT_DEFINE_ACTION_WRAPPER(driver_unregister_wrapper, driver_unregister, struct device_driver *);
/* The root device for the KUnit bus, parent of all kunit_devices. */
static struct device *kunit_bus_device;
/* A device owned by a KUnit test. */
struct kunit_device {
struct device dev;
/* The KUnit test which owns this device. */
struct kunit *owner;
/* If the driver is managed by KUnit and unique to this device. */
const struct device_driver *driver;
};
#define to_kunit_device(d) container_of_const(d, struct kunit_device, dev)
static const struct bus_type kunit_bus_type = {
.name = "kunit",
};
/* Register the 'kunit_bus' used for fake devices. */
int kunit_bus_init(void)
{
int error;
kunit_bus_device = root_device_register("kunit");
if (IS_ERR(kunit_bus_device))
return PTR_ERR(kunit_bus_device);
error = bus_register(&kunit_bus_type);
if (error)
root_device_unregister(kunit_bus_device);
return error;
}
/* Unregister the 'kunit_bus' in case the KUnit module is unloaded. */
void kunit_bus_shutdown(void)
{
/* Make sure the bus exists before we unregister it. */
if (IS_ERR_OR_NULL(kunit_bus_device))
return;
bus_unregister(&kunit_bus_type);
root_device_unregister(kunit_bus_device);
kunit_bus_device = NULL;
}
/* Release a 'fake' KUnit device. */
static void kunit_device_release(struct device *d)
{
kfree(to_kunit_device(d));
}
/*
* Create and register a KUnit-managed struct device_driver on the kunit_bus.
* Returns an error pointer on failure.
*/
struct device_driver *kunit_driver_create(struct kunit *test, const char *name)
{
struct device_driver *driver;
int err = -ENOMEM;
driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL);
if (!driver)
return ERR_PTR(err);
driver->name = kunit_kstrdup_const(test, name, GFP_KERNEL);
driver->bus = &kunit_bus_type;
driver->owner = THIS_MODULE;
err = driver_register(driver);
if (err) {
kunit_kfree(test, driver);
return ERR_PTR(err);
}
kunit_add_action(test, driver_unregister_wrapper, driver);
return driver;
}
EXPORT_SYMBOL_GPL(kunit_driver_create);
/* Helper which creates a kunit_device, attaches it to the kunit_bus*/
static struct kunit_device *kunit_device_register_internal(struct kunit *test,
const char *name)
{
struct kunit_device *kunit_dev;
int err = -ENOMEM;
Annotation
- Immediate include surface: `linux/device.h`, `linux/dma-mapping.h`, `kunit/test.h`, `kunit/device.h`, `kunit/resource.h`, `device-impl.h`.
- Detected declarations: `struct kunit_device`, `function kunit_bus_init`, `function kunit_bus_shutdown`, `function kunit_device_release`, `function kunit_device_unregister`, `export kunit_driver_create`, `export kunit_device_register_with_driver`, `export kunit_device_register`, `export kunit_device_unregister`.
- Atlas domain: Kernel Services / lib.
- Implementation status: pattern implementation candidate.
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.