Re: Use of copy_to_user() and copy_from_user() functions

2013-02-11 Thread Mandeep Sandhu
On Mon, Feb 11, 2013 at 4:51 PM, पारस bepa...@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@kernelnewbies.org
 http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Use of copy_to_user() and copy_from_user() functions

2013-02-11 Thread Chetan Nanda
On Mon, Feb 11, 2013 at 5:25 PM, anish singh anish198519851...@gmail.comwrote:



 On Mon, Feb 11, 2013 at 4:51 PM, पारस bepa...@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?
 what will happen if the user space process gets scheduled out while
 you were using it?

 copy_to/from_user should always be called from process context. So even if
a process is scheduled out there is not impact, as when process schedule
back in copying will resume to/from correct page.


  Can any one explain why kernel can't directly access the user-space
 address.
 
  Thanks
 
 
  ___
  Kernelnewbies mailing list
  Kernelnewbies@kernelnewbies.org
  http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
 


 ___
 Kernelnewbies mailing list
 Kernelnewbies@kernelnewbies.org
 http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Use of copy_to_user() and copy_from_user() functions

2013-02-11 Thread anish singh
On Tue, Feb 12, 2013 at 9:24 AM, Chetan Nanda chetanna...@gmail.com wrote:



 On Mon, Feb 11, 2013 at 5:25 PM, anish singh anish198519851...@gmail.com 
 wrote:



 On Mon, Feb 11, 2013 at 4:51 PM, पारस bepa...@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?
 what will happen if the user space process gets scheduled out while
 you were using it?

 copy_to/from_user should always be called from process context. So even if a 
 process is scheduled out there is not impact, as when process schedule back 
 in copying will resume to/from correct page.
because page fault will be triggered and the user space will be swapped in.
 That answers the original question.

  Can any one explain why kernel can't directly access the user-space 
  address.
 
  Thanks
 
 
  ___
  Kernelnewbies mailing list
  Kernelnewbies@kernelnewbies.org
  http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
 


 ___
 Kernelnewbies mailing list
 Kernelnewbies@kernelnewbies.org
 http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies



___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies