Re: memcopy from Kernel to user and vice versa

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



Hello Tushar,

a bit off-topic, isn't it ?

Anyway, the book "Linux Kernel Development" by Robert Love (which I hereby 
recommend) uses 3 chapters to explain things around Linux memory management.

Here a two-sentences-version:

Memory is organized in pages of 4kBytes (on i386) each. For each page there is 
a struct that holds information about that page, e.g. if it's free, who uses 
it, and so on. The hardware MMU of your computer is programmed by the kernel 
to map between hardware addresses and virtual addresses. If you write a user 
space program, you never care about that. You allocate memory and it looks 
contiguous, though it might be located in non-contiguous hardware addresses.

The MMU also detects Segmentation Faults if you try to access addresses that 
you don't own. For that reason you need special functions, namely 
copy_from_user() and copy_to_user() to copy between memory owned by the 
kernel and memory owned by a user process. These functions are mostly written 
in assembler (see include/asm/uaccess.h).

Though these functions are optimized for speed, they are of course not as fast 
as a simple "mov" instruction a compiler generates for user-user copies. So, 
if it is possible to copy one big block instead of many small ones, you 
reduce overhead (that's always true, not only in the kernel). Anyway, if you 
write a kernel module, you have no choice. You simply have to use special 
functions to copy between user and kernel memory. Use them wisely.

Hans


> Hi All,
>
> Can somebody explain how memory copy works between User and Kernel space?
> Is there any extra penalty for using more number of copies between Kernel
> and User space in Linux? I would really appreciate if some body can explain
> how virtual memory mapping works in Linux.
>
> Thanks,
> Tushar




[Index of Archives]     [Linux DVB]     [Video Disk Recorder]     [Asterisk]     [Photo]     [DCCP]     [Netdev]     [Xorg]     [Util Linux NG]     [Xfree86]     [Free Photo Albums]     [Fedora Users]     [Fedora Women]     [ALSA Users]     [ALSA Devel]     [Linux USB]

Powered by Linux