2006-05-30 22:33:03

by Brian D. McGrew

[permalink] [raw]
Subject: Sharing memory between kernel and user space

I have a question about the best way to share memory between user and
kernel space.

Let's say I have a common structure;

struct counter {
u_long interrupt_counts;
bool saw_interupt;
}

And I need to be able to modify these elements from both the kernel and
user space. What is the best way to allocate this??? I've tried
several methods including __get_free_pages, alloc_pages, vmalloc and so
on; and thus far, I'm just confused myself.

Can someone help me out here with a quick example of some sort???

Thanks,

:b!

Brian D. McGrew { [email protected] || [email protected] }
--
> This is a test. This is only a test!
Had this been an actual emergency, you would have been
told to cancel this test and seek professional assistance!


2006-05-30 22:46:53

by H. Peter Anvin

[permalink] [raw]
Subject: Re: Sharing memory between kernel and user space

Followup to: <14CFC56C96D8554AA0B8969DB825FEA0012B331A@chicken.machinevisionproducts.com>
By author: "Brian D. McGrew" <[email protected]>
In newsgroup: linux.dev.kernel
>
> I have a question about the best way to share memory between user and
> kernel space.
>

In general, allocate the memory in kernel space (via get_free_page et
al), and make accessible to userspace via mmap on a device node.

-hpa

2006-05-30 22:53:16

by Brian D. McGrew

[permalink] [raw]
Subject: RE: Sharing memory between kernel and user space

I'm using the 2.6.16.16 kernel. Is there any chance that I could
trouble you for a snippet of code to do that? I've tried every
combination I can think of.

Thank you,

:b!

Brian D. McGrew { [email protected] || [email protected] }
--
> This is a test. This is only a test!
Had this been an actual emergency, you would have been
told to cancel this test and seek professional assistance!

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of H. Peter Anvin
Sent: Tuesday, May 30, 2006 3:46 PM
To: [email protected]
Subject: Re: Sharing memory between kernel and user space

Followup to:
<14CFC56C96D8554AA0B8969DB825FEA0012B331A@chicken.machinevisionproducts.
com>
By author: "Brian D. McGrew" <[email protected]>
In newsgroup: linux.dev.kernel
>
> I have a question about the best way to share memory between user and
> kernel space.
>

In general, allocate the memory in kernel space (via get_free_page et
al), and make accessible to userspace via mmap on a device node.

-hpa

2006-05-30 23:21:34

by Chris Friesen

[permalink] [raw]
Subject: Re: Sharing memory between kernel and user space

Brian D. McGrew wrote:
> I'm using the 2.6.16.16 kernel. Is there any chance that I could
> trouble you for a snippet of code to do that? I've tried every
> combination I can think of.

You could try looking at how /dev/kmem does it.

Also, be aware that the size of "unsigned long" can vary between the
kernel and userspace if you have a 64-bit machine.

Chris

2006-05-31 00:09:23

by Brian D. McGrew

[permalink] [raw]
Subject: RE: Sharing memory between kernel and user space

As you recommended I do a __get_free_pages in the kernel and then from
user space I try and mmap the memory.

The mmap is successful but then when I go back and try and read from
that location I get a 'can't read of address 0xbf7c8000 (which is the
address the kernel gave me back).

???

:b!

Brian D. McGrew { [email protected] || [email protected] }
--
> This is a test. This is only a test!
Had this been an actual emergency, you would have been
told to cancel this test and seek professional assistance!
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of H. Peter Anvin
Sent: Tuesday, May 30, 2006 3:46 PM
To: [email protected]
Subject: Re: Sharing memory between kernel and user space

Followup to:
<14CFC56C96D8554AA0B8969DB825FEA0012B331A@chicken.machinevisionproducts.
com>
By author: "Brian D. McGrew" <[email protected]>
In newsgroup: linux.dev.kernel
>
> I have a question about the best way to share memory between user and
> kernel space.
>

In general, allocate the memory in kernel space (via get_free_page et
al), and make accessible to userspace via mmap on a device node.

-hpa

2006-05-31 12:33:04

by Jonathan Corbet

[permalink] [raw]
Subject: Re: Sharing memory between kernel and user space

> I'm using the 2.6.16.16 kernel. Is there any chance that I could
> trouble you for a snippet of code to do that? I've tried every
> combination I can think of.

The memory mapping chapter of LDD3 (http://lwn.net/Kernel/LDD3/) has a
pretty thorough description of how to share both kernel- and user-space
memory, with code examples.

jon