Linux device-number allocation

ID: linux-device-number-allocation

Linux device-number allocation assigns or reserves the numerical identifiers through which device-special interfaces are registered. The relevant identity includes device type together with major device number and minor device number. It is distinct from naming a device node, assigning its inode number, or obtaining a process-local file descriptor.
The kernel's allocated-device list records established assignments. For example, character major 1 includes minor 3 for /dev/null and minor 5 for /dev/zero. The same integer major 1 has a separate block-device allocation for RAM disks. These entries concern different interface namespaces, not interchangeable interpretations of a single untyped integer.[ref]
For character devices, the kernel API separates requesting a particular range with register_chrdev_region from dynamic character-device number allocation with alloc_chrdev_region. The latter receives a first minor and count, chooses the major dynamically, and returns the first assigned combined device number. The caller must check whether reservation succeeded.[ref]
An original allocation example requests four minor values starting at 20. If the returned major is , the reserved range in this example is
It does not contain . Code should use the returned rather than guess a major from an earlier machine or boot. A numerical allocation can serve software interfaces as well as physical hardware, so four minors do not necessarily imply four physical peripherals.
Reserving numbers is only one step. The character-device object must also be associated with its operations and added to the kernel; cdev_add makes it live. Separately, userspace needs an appropriate device node or other naming arrangement. The registration name used for bookkeeping is not itself a pathname under /dev.[ref]
This separation explains a common diagnostic trap. Creating a node containing a chosen major/minor pair does not reserve that range or supply a working device driver. Conversely, allocating a range does not make every invented pathname reach it. Inspect the actual registered interface, file type and stored device number together when identifying a device.
The allocation is also not a persistent hardware serial number. Dynamic assignment can differ between configurations or registration histories. Releasing a number range with unregister_chrdev_region returns the reservation; a complete driver teardown must also handle the device object's lifetime and existing users. The kernel documentation notes that deleting a character-device object prevents new opens while operations on already open descriptors can remain possible.[ref]

New to topics? Read the docs here!