从我之前的问题:如何在 ubuntu 中构建外部模块?我以为我无法加载模块,因为loading out-of-tree
or tainting kernel
,但是我的scull_init_module
(下面的来源)我已经添加了printk
,所以我知道我是从内核日志加载的。但我没有(日志中没有输出——日志与链接中的相同)。
uname -a
== 4.19.0-9-amd64 #1 SMP Debian 4.19.118-2 (2020-04-29) x86_64 GNU/Linux
(不使用 ubuntu - 来自链接)。
scull_init_module
:
#ifndef scull_major
#define scull_major 0
#endif
#ifndef scull_nr_devs
#define scull_nr_devs 4
#endif
struct scull_dev *scull_devices; /*array of active devices */
static void scull_setup_cdev(struct scull_dev *, int); /*helper function (internal) */
int scull_minor = 0;
int scull_init_module(void) {
int i;
dev_t dev = 0;
/* register devices */
int result = 1;
result = alloc_chrdev_region(&dev, scull_minor, scull_nr_devs, "scull"); /**comment later**/
printk(KERN_ALERT"Passed %s %d \n in scull_init_module\n", __FUNCTION__, __LINE__);
if(result < 0){
printk(KERN_WARNING "scull: can't get major %d\n", scull_major);
return result;
}
/* allocate space for devices */
scull_devices = kmalloc(sizeof(struct scull_dev) * scull_nr_devs, GFP_KERNEL);
if(!scull_devices){
result = -1;
goto fail;
}
/*initilization of devices */
for(i=0; i<scull_nr_devs;i++){
scull_devices[i].quantum = scull_quantum;
scull_devices[i].qset = scull_qset;
scull_setup_cdev(&scull_devices[i], i);
}
return 0;
fail:
scull_cleanup_module();
return result;
}
module_init(scull_init_module);
它基本上来自Linux 设备驱动程序,第三版,第 3 章。但我是手写的(没有复制/粘贴)。alloc_chrdev_region
被正确调用,所以是scull_setup_cdev
,那么哪里可能有问题?
编辑(dmesg 的输出):dmesg
看起来它确实加载了模块:
[ 2765.707018] scull: loading out-of-tree module taints kernel.
[ 2765.707106] scull: module verification failed: signature and/or required key missing - tainting kernel
[ 2765.707929] Passed scull_init_module 41
in scull_init_module
但是仍然/dev/scull0
给出的节点No such device or address
#mknod /dev/scull0 c 241 0
#cat /dev/scull0
cat: /dev/scull0: No such device or address
因此,如果设备已正确注册(请参阅 dmesg)并且我在 /dev 层次结构中创建节点,那么意味着no device or address
什么?为什么我会得到它?
EDIT2(函数 scull_setup_cdev):
static void scull_setup_cdev(struct scull_dev *dev, int index){
int err, devno=MKDEV(scull_major, scull_minor + index);
cdev_init(&dev->cdev, &scull_fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &scull_fops; /**comment later**/
err = cdev_add(&dev->cdev, devno, 1);
if (err)
printk(KERN_NOTICE "Error %d adding scull%d", err, index);
}