Why do you have to use copy_to_user()/copy_from_user() to access user space from the kernel?

后端 未结 2 1789
春和景丽
春和景丽 2020-12-08 03:12

I\'m curious, because I got a kernel panic after trying to access memory directly (then I found these functions).

2条回答
  •  情歌与酒
    2020-12-08 03:30

    Those functions check whether the memory is accessible. If the kernel attempts to directly access a non-accessible address, it will panic. But in addition, the kernel and user address spaces may be different ... a valid address in the user address space may not be accessible in the kernel, and if it is it may point to kernel stuff rather than user stuff.

    For more details, see https://developer.ibm.com/articles/l-kernel-memory-access

    On a historical note: once upon a time there were operating systems in which the kernel was designed to be part of the user address space, and in those systems the kernel could always access user space directly. There may still be such systems, but modern linux isn't one. The user process's memory being part of the kernel address space is always an option for the implementation, of course, and that can make copy_to_user and copy_from_user a lot faster.

提交回复
热议问题