I am newbie with Linux kernel/devices and I have tried the following experiment:
sudo mknod /dev/mydev c 100 101
// misc_hello.c
static struct miscdevice helloworld_miscdevice = {
.minor = MISC_DYNAMIC_MINOR,
.name = "mydev",
.fops = &my_dev_fops,
};
static int __init hello_init(void)
{
int ret_val;
ret_val = misc_register(&helloworld_miscdevice);
// ret_val is 0 so the device is registered succesfully
After executing insmod misc_hello.ko
, I don't see any error in dmesg
. When I inspect the devices with ls /dev/ | grep mydev
I see that the dev/mydev/
node is the one created manually before with mknode
command. Shouldn't the module registration fail if sysfs
finds that there is another device node with the same name? How is name conflict between nodes detected by Linux kernel?
Policy dictates that userspace has ultimate responsibility for the contents of /dev
, even when populated dynamically.
If the devtmpfs file system is supported (by the CONFIG_DEVTMPFS
option since kernel version 2.6.32), the kernel will attempt to populate it, but it does not care if it fails to create a device node, just as it does not care if the devtmpfs file system is mounted or not. When mounted, the contents of the devtmpfs file system can be modified by userspace (e.g. by udevd) at any time. As it says in the original commit:
Devtmpfs can be changed and altered by userspace at any time, and in any way needed - just like today's udev-mounted tmpfs. Unmodified udev versions will run just fine on top of it, and will recognize an already existing kernel-created device node and use it. The default node permissions are root:root 0600. Proper permissions and user/group ownership, meaningful symlinks, all other policy still needs to be applied by userspace.
If a node is created by devtmps, devtmpfs will remove the device node when the device goes away. If the device node was created by userspace, or the devtmpfs created node was replaced by userspace, it will no longer be removed by devtmpfs.
If /dev
is mounted as a tmpfs rather than a devtmpfs, the kernel does not populate it at all, but it can still be populated by userspace (e.g. by udevd).
The automatic creation of the device node by the kernel is done by the call to devtmpfs_create_node(dev);
from the device_add()
function, and the automatic deletion is done by the call to devtmpfs_delete_node(dev);
from the device_del()
function. Although the devtmpfs_create_node()
and devtmpfs_delete_node()
functions to return a negative error number on error, the return values of calls to those functions are ignored by the device_add()
and device_del()
functions.