首页 > 解决方案 > 用户模式应用程序调用 open() 导致 oops

问题描述

我正在学习 IOCTL 通信,并且正在尝试创建 IOCTL 的工作示例。当我调用 open() 打开我的驱动程序时,函数失败,并且 errno 变量的值为 22。驱动程序失败。我已经尝试在驱动程序中处理错误以创建设备,但似乎设备创建没有问题。这可能是我的 open() 例程的问题。这是哎呀消​​息:

[  503.738485] Modules linked in: simple_module(OE) nls_utf8 isofs vboxvideo(OE) crct10dif_pclmul crc32_pclmul ghash_clmulni_intel aesni_intel aes_x86_64 snd_intel8x0 crypto_simd cryptd snd_ac97_codec glue_helper ac97_bus snd_pcm snd_seq_midi snd_seq_midi_event snd_rawmidi joydev snd_seq snd_seq_device snd_timer vmwgfx snd ttm input_leds soundcore drm_kms_helper serio_raw drm fb_sys_fops syscopyarea sysfillrect sysimgblt mac_hid vboxguest(OE) sch_fq_codel parport_pc ppdev lp parport ip_tables x_tables autofs4 hid_generic usbhid hid psmouse ahci libahci i2c_piix4 e1000 pata_acpi video
[  503.738511] CPU: 0 PID: 2555 Comm: driver_tester Tainted: G           OE     5.0.0-31-generic #33~18.04.1-Ubuntu
[  503.738512] Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006
[  503.738515] RIP: 0010:do_dentry_open+0x1c8/0x3a0
[  503.738516] Code: 87 68 01 00 00 48 85 c0 74 33 48 8d 50 28 48 3b 50 28 74 23 ba 20 00 00 00 e8 b4 d6 06 00 85 c0 74 1b 41 89 c7 e9 16 ff ff ff <0f> 0b 41 bf ea ff ff ff e9 12 ff ff ff 48 3b 50 30 75 d7 8b 43 44
[  503.738518] RSP: 0018:ffff98c982937c60 EFLAGS: 00010202
[  503.738519] RAX: 0000000000000001 RBX: ffff8b83dea98300 RCX: 0000000000000000
[  503.738520] RDX: 0000000000000002 RSI: ffff8b843f016448 RDI: 0000000000000000
[  503.738521] RBP: ffff98c982937c88 R08: 0000000000000209 R09: 0000000000000004
[  503.738522] R10: ffff8b83d57fc540 R11: 0000000000000001 R12: ffff8b83c2e18e00
[  503.738523] R13: ffff8b83dea98310 R14: ffffffffb44bb1d0 R15: 0000000000000001
[  503.738524] FS:  00007f6cceff44c0(0000) GS:ffff8b843f000000(0000) knlGS:0000000000000000
[  503.738525] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  503.738526] CR2: 00005605b1db9008 CR3: 000000005eadc000 CR4: 00000000000406f0
[  503.738530] Call Trace:
[  503.738534]  vfs_open+0x2f/0x40
[  503.738536]  path_openat+0x2e8/0x1700
[  503.738539]  ? wake_up_process+0x15/0x20
[  503.738541]  ? insert_work+0x6c/0x80
[  503.738543]  do_filp_open+0x9b/0x110
[  503.738545]  ? __check_object_size+0xdb/0x1b0
[  503.738548]  ? strncpy_from_user+0x54/0x190
[  503.738550]  ? __alloc_fd+0x46/0x170
[  503.738552]  do_sys_open+0x1bb/0x2d0
[  503.738553]  ? do_sys_open+0x1bb/0x2d0
[  503.738555]  __x64_sys_openat+0x20/0x30
[  503.738558]  do_syscall_64+0x5a/0x120
[  503.738561]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
[  503.738563] RIP: 0033:0x7f6cceb04c8e
[  503.738564] Code: 25 00 00 41 00 3d 00 00 41 00 74 48 48 8d 05 81 0c 2e 00 8b 00 85 c0 75 69 89 f2 b8 01 01 00 00 48 89 fe bf 9c ff ff ff 0f 05 <48> 3d 00 f0 ff ff 0f 87 a6 00 00 00 48 8b 4c 24 28 64 48 33 0c 25
[  503.738565] RSP: 002b:00007ffe16c6d430 EFLAGS: 00000246 ORIG_RAX: 0000000000000101
[  503.738566] RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007f6cceb04c8e
[  503.738567] RDX: 0000000000000002 RSI: 00005605afff1880 RDI: 00000000ffffff9c
[  503.738568] RBP: 00007ffe16c6d4b0 R08: 0000000000000000 R09: 0000000000000000
[  503.738569] R10: 0000000000000000 R11: 0000000000000246 R12: 00005605afff1660
[  503.738570] R13: 00007ffe16c6d590 R14: 0000000000000000 R15: 0000000000000000
[  503.738571] ---[ end trace 7a17033f1fab638b ]---

这是我的 module_init()

struct file_operations fops = { 
read: hello_read,
write: hello_write,
open: hello_open,
release: hello_release,
unlocked_ioctl: ext_ioctl,

};

static __init my_init(void)
{
printk("module load");
strncpy(message, "hello world", 1023);
num_bytes = strlen(message);

alloc_chrdev_region(&dev, 0, 1, "IOCTLdriver");


deviceclass = class_create(THIS_MODULE, "Ioctldevices");

cdev_init(&mycdev, &fops);
cdev_add(&mycdev, dev, 1);

device_create(deviceclass, NULL, dev, NULL, "IOCTLdevice1");


printk("the hello device is major: %d\n", dev);
return 0;

这是我的 open() 例程

int hello_open(struct inode* inodepointer, struct file* filepointer)
{
    printk("reached point 1\n");
    if (is_open == 1)
    {
    printk("Error - hello device already open\n");
    return -EBUSY;
    }
    is_open = 1;
    try_module_get(THIS_MODULE);
}```

标签: clinux-kerneldriverlinux-device-driverembedded-linux

解决方案


推荐阅读