Issue
I'm writing a linux kernel driver and for every function that sends data up to userspace or reads data from userspace, I am using copy_to_user() and copy_from_user(). My question is: do I need to use these calls if I am just copying a basic data type such as a u32 or an int?
Solution
If the function receives a pointer to user-space data, you have to use copy_from_user()
to copy the pointed-to data from user space into kernel space (and vice versa).
Note that the pointer value itself is passed by value (like all C parameters), so you don't have to do a copy_from_user()
to obtain the pointer value before you can copy_from_user()
the data it points to.
Numeric arguments work the same way as pointer arguments; in C terms, they're both scalars. You don't have to use copy_from_user()
to copy the value of the parameter; that's already been copied. You only have to use it to copy data that's pointed to by a passed pointer.
So if you have a parameter of type int
, you can use it directly. If your parameter points to an int
, then the int
object will be in user space, and you need to use copy_to_user
to copy the value of that object into kernel space.
Answered By - Keith Thompson