【问题标题】:How to call ioctl from kernel space in Linux kernel 5.10?如何在 Linux 内核 5.10 中从内核空间调用 ioctl?
【发布时间】:2022-01-21 11:21:50
【问题描述】:

我的驱动程序的 IOCTL 处理程序将 IOCTL 请求重定向到另一个驱动程序。这曾经很好用,但是当我需要添加对 5.10 内核的支持时,结果发现旧方法不再适用了。现在怎么办?

#define TARGET_ID "TGT"
// ...
char id[sizeof(TARGET_ID)];
// ...
mm_segment_t old_fs = get_fs();
set_fs(KERNEL_DS);
res = __blkdev_driver_ioctl(dev->bdev_raw, 0, SCSI_IOCTL_TARGET_ID, (unsigned long)id);
set_fs(old_fs);

if(0 == strcmp(id, TARGET_ID))
{
    PINFO("*** target driver detected! ***\n");
    dev->is_target_driver = true;
}
else
{
    ...

【问题讨论】:

    标签: linux module kernel chaining ioctl


    【解决方案1】:

    万岁!万岁!万岁! ;) 这就是它的工作方式:

        #define TARGET_ID "TGT"
        // ...
        char id[sizeof(TARGET_ID)];
        // ...
    #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 4)
        mm_segment_t old_fs = force_uaccess_begin();
        res = __blkdev_driver_ioctl(dev->bdev_raw, 0, SCSI_IOCTL_TARGET_ID, (unsigned long)id);
        force_uaccess_end(old_fs);
    #else
        mm_segment_t old_fs = get_fs();
        set_fs(KERNEL_DS);
        res = __blkdev_driver_ioctl(dev->bdev_raw, 0, SCSI_IOCTL_TARGET_ID, (unsigned long)id);
        set_fs(old_fs);
    #endif
        
        if(0 == strcmp(id, TARGET_ID))
        {
            PINFO("*** target driver detected! ***\n");
            dev->is_target_driver = true;
        }
        else
        {
            ...
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2011-07-17
    • 2023-03-31
    • 2012-06-21
    • 2011-02-11
    • 2013-09-25
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    相关资源
    最近更新 更多