copy_to_user 在 linux 内核版本 4.12.8 中未定义

copy_to_user undefined in linux kernel version 4.12.8

在我的项目中,我使用字符驱动程序在用户 space 和内核 space 之间进行通信。我使用函数 copy_to_user(void user *to, const void *from, unsigned long n) 将数据从内核 space 复制到用户 space 缓冲区。我们可以在 #include < asm/uaccess.h > 头文件下找到这个函数。 我使用 Linux 内核版本 4.4.0-59-generic、Ubuntu OS 版本 16.04 LTS 编译了该项目,它工作正常,没有任何错误和警告。我得到了想要的输出。

我使用 Linux 内核版本 4.12.8、Ubuntu OS 版本 16.04.2 LTS 编译了同一个项目,它在编译期间向我发出警告 WARNING: "copy_to_user" [/home/ldrv1/Desktop/Vijay/code/build/uts.ko] undefined!.当我对我的模块执行 insmod 时,出现如下错误 insmod: ERROR: could not insert module uts.ko: Unknown symbol in module。我认为 4.12.8 内核版本仍然支持 #include <asm/uaccess.h> 头文件,否则我会遇到致命错误:编译时没有这样的文件或目录错误。我尝试使用 apt-get install linux-headers-$(uname -r) 命令更新 linux 内核头文件,我得到了以下响应:

Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package linux-headers-4.12.8
E: Couldn't find any package by glob 'linux-headers-4.12.8'
E: Couldn't find any package by regex 'linux-headers-4.12.8'

此 OS 版本 16.04.2 LTS 具有 linux-headers-4.10.0-35。 我如何摆脱这个警告?建议和支持表示赞赏。如果需要更多信息,请随时询问。

  1. 对于 4.12.8,您应该使用 #include <linux/uaccess.h>。 这是 definition.

    4.4 中,一些驱动程序使用 #include <asm/uaccess.h> 而其他驱动程序 使用 #include <linux/uaccess.h>.

    我认为

    #include <linux/uaccess.h>更好。

  2. 你应该做 apt-get update 然后 apt-get install linux-headers-generic.

函数copy_to_usercopy_from_user定义在 asm/uaccess.h。我认为您在定义此功能时遇到了一些问题。我用一些关于内核 space 和用户 space 之间的数据传输的例子编写了字符设备驱动程序。查看我的 github: my code 以供参考。如果您觉得对您有帮助,请加星标:)。它在示例 3 中有一个小错误。我正在计算它们,但是示例 1 和示例 2 运行良好

Bronislav Elizaveti 给出的答案是正确的。如果我们使用 #include <linux/uaccess.h> 而不是 #include <asm/uaccess.h>,那么我们将不会收到警告。

如果您仍然只想使用 #include <asm/uaccess.h>,则需要使用 _copy_to_user 而不是 copy_to_user(参数相同)。一个简单的 _ 就可以完成这项工作。