Sharing memory between kernel space and user space

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
All,

I want a ability to share memory between kernel and user space. What is
the mechanism available in the 2.4 kernel?

What synchronization mechanism can I use for accessing the memory from
user space and kernel space?

Shobha

Re: Sharing memory between kernel space and user space

Quoted text here. Click to load it

A simple (and tipical) way may be by implementing a character device driver.
A device driver may share memory on three ways:

- read/write/seek operation, as you do on files.

- ioctls (this is not the suggested one).

- memory map

The first may implements semaphores directly in kernel space, and is
typically used if you must wait for events.

The last (mmap) is used to share memory in asynchronous mode and may require
synchronization at user level, by using IPC semaphores.

Could you explain better what you're about to do ?


Regards,
Gabriele


Re: Sharing memory between kernel space and user space
Hi Gabriele.

I have a kernel module which should populate a database of entries
coming from the network interface. The module after populating it in
kernel space sends notification to a user space process which maps the
memory and has access to the database for reading.

I guess for synchronization the mechanism you suggested for kernel space
semaphores should work.

Thanks,
Shobha.

Gabriele Brugnoni wrote:
Quoted text here. Click to load it

Re: Sharing memory between kernel space and user space

Quoted text here. Click to load it


Take a look at drivers/video/fbmem.c, it maps to user space the framebuffer
video of a device using the mmap mechanism.

Another example: drivers/char/ftape/zftape/zftape-init.c

(Those examples comes from kernel 2.4.18)

To access the shared memory, in userspace, you should open the device and
use the mmap function to get the virtual address of the memory.


Regards,
Gabriele


Re: Sharing memory between kernel space and user space
Quoted text here. Click to load it

Why is the database a kernel module ? It sounds like it should not be.



Re: Sharing memory between kernel space and user space
Hi Gabriele,

Its not really a dababase but a array which is filled by the kernel
which the user problem accesses. This is to avoid copies from the kernel
to user space.

Thanks,
Shobha.

Geronimo W. Christ Esq wrote:
Quoted text here. Click to load it

Re: Sharing memory between kernel space and user space
Quoted text here. Click to load it

What data is that exactly, is it not already accessible from user space ?

Quoted text here. Click to load it

I hope you don't mind me asking, but why do you want to avoid copies
from kernel to user space ? It's usually very fast. The kernel is tuned
very well to do such transfers very quickly indeed.

There are a lot of disadvantages to using kernel modules especially when
they are not needed. They are more difficult to debug and profile and
obviously bugs can crash the whole system. To me personally, the
presence of kernel modules or privileged code doing work which is not
directly related to either driving a device or modifying behaviour of
the kernel, indicates bad design.


Re: Sharing memory between kernel space and user space
I guess the main advantage is the number of copies involved when the
number of packets you are referring to are fairly large. I just need a
header from the packet in the user space to make some decisions. I can
allocate some himem to allocate space from there.

Thanks,
Shobha.

Geronimo W. Christ Esq wrote:
Quoted text here. Click to load it

Re: Sharing memory between kernel space and user space
Quoted text here. Click to load it

Oh, you're moving data packets from user space to kernel space. Have you
investigated using the tun/tap device ?

Linux systems move data packets between userspace and kernelspace as a
matter of course - think of all the servers out there running apache
which constantly have to do this. I really think it is a bad idea to be
doing this in the kernel, and you will almost certainly regret it in the
future. If I were writing a program which had to analyze ethernet
packets I'd look into using the existing, well defined, stable
interfaces that permit this.

Site Timeline