Character-device lifetime after deletion 2026-11-05
Character-device lifetime after deletion is the interval during which a removed character device still needs valid backing state because existing users can invoke its operations. Linux
cdev_del removes the device from new-open lookup; it does not invalidate already-open file descriptors. The kernel explicitly permits their file-operation callbacks after deletion returns.[ref]Character-device number registration in the Linux kernel documentation. Char devices, cdev_del lifetime note
Consider an original event sequence. Process A opens an interface and retains a pointer to driver state. Removal calls
cdev_del; process B can no longer obtain a new open through that registration. Process A then calls read. Freeing the state immediately after deletion would make that callback access dead storage: a use-after-free. The lookup lifetime and the backing allocation lifetime are different.A useful ownership model gives the allocation one registration reference and one reference for each successful open. In a simplified reference counting design with opens, the reference count is . Removal drops the registration reference; each final close drops its open reference. With two opens, the sequence is , and storage is reclaimed only at zero. This is a model of ownership, not a complete driver implementation: taking references must itself be synchronized with removal, and asynchronous work can require additional references.
The character device may be disconnected from its hardware before its storage is reclaimed. Existing operations can observe a synchronized disconnected state and return an appropriate error while the allocation remains valid. Reference protection alone does not make removed hardware usable or serialize simultaneous operations.
For combined device objects,
cdev_device_add and cdev_device_del have their own documented pairing and parent-reference rules. The add helper can expose callbacks even when it reports failure; the delete helper distinguishes stopped sysfs callbacks from still-callable operations on existing opens.[ref]Character-device number registration in the Linux kernel documentation. cdev_device_add description and failure warning; cdev_device_del note
Therefore unwinding a failed character-device registration must follow the actual publication and ownership state. Releasing the device number reservation is separate from proving that every user of private storage has finished. Dynamic character-device number allocation 2026-11-05
Dynamic character-device number allocation asks the Linux kernel to reserve a range of character-device numbers while choosing its major device number at runtime. The API is
alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name). It returns zero on success or a negative error code; the first assigned combined device number is written through dev. The return status is not the allocated major number.[ref]Character-device number registration in the Linux kernel documentation. Char devices, alloc_chrdev_region, parameters and return convention
For an original request, suppose
alloc_chrdev_region(&first, 5, 3, "sample") succeeds and the returned first has major . Its reserved identifiers are , and , not . If one illustrative successful run returned major 243, the caller would use 243; another allocation can return a different major. This is a hypothetical range example, not a kernel operation executed here.Keep
first and the successful reservation count for cleanup. If a later initialization step fails, unregister_chrdev_region(first, 3) releases this reservation; it must not be treated as ownership of every minor under the returned major. Unwinding a failed character-device registration releases only resources actually acquired.Number reservation is separate from associating file operations with a live character-device object and from creating a device node. The related
cdev_add operation makes a registered object live; its name or bookkeeping label does not itself establish a pathname under /dev.[ref]Character-device number registration in the Linux kernel documentation. Char devices, cdev_add, __register_chrdev name description and unregister_chrdev_region
Removal must also respect existing users.
cdev_del prevents new opens, but already open file descriptors can still call device operations, so deleting the registration alone does not prove that backing state can be freed immediately.[ref]Character-device number registration in the Linux kernel documentation. Char devices, cdev_del lifetime note
A dynamic number is a reserved interface identifier, not a persistent hardware identity or an inode number.