Use of copy_to_user() and copy_from_user() functions

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Mon Feb 11 06:59:23 EST 2013


On Mon, Feb 11, 2013 at 4:51 PM, पारस <beparas at gmail.com> wrote:
> Hi All,
>
> To read/write data to user-space from kernel-space we use copy_from_user()
> and copy_to_user() functions.
>
> What is the use of these function?
> Why kernel can't directly access user address and read/write on to it?
> Can any one explain why kernel can't directly access the user-space address.

There are a lot resources out there which answer this question
adequately in detail (just google for them). I'll give a short answer
here:

'User-space' is essentially a particular process' address space, i.e
the pages (of physical memory) that have been mapped to this process'
virtual memory.
Since pages of virtual memory _might_ be swapped out by the kernel,
one cannot directly transfer data from the kernel memory to the user
mapped memory. For this reason, copy_to/from_user() family of
functions is there, which verify first that the user space address
(pointed to by the user-space pointer) is valid and accessible and
_then_ transfer data to/from it.

If the page corresponding to the address pointed to by the user-space
pointer has been swapped out, the kernel will first swap in that page
and then do the transfer.

CMIIW.

HTH,
-mandeep


>
> Thanks
>
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>



More information about the Kernelnewbies mailing list