Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sharing memory between kernel and user space

638 views
Skip to first unread message

Brian D. McGrew

unread,
May 30, 2006, 6:33:27 PM5/30/06
to linux-...@vger.kernel.org
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 { br...@visionpro.com || br...@doubledimension.com }
--
> 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!

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majo...@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

H. Peter Anvin

unread,
May 30, 2006, 6:47:14 PM5/30/06
to linux-...@vger.kernel.org
Followup to: <14CFC56C96D8554AA0B8...@chicken.machinevisionproducts.com>
By author: "Brian D. McGrew" <br...@visionpro.com>
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

Brian D. McGrew

unread,
May 30, 2006, 6:53:36 PM5/30/06
to H. Peter Anvin, linux-...@vger.kernel.org
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 { br...@visionpro.com || br...@doubledimension.com }
--
> 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: linux-ker...@vger.kernel.org
[mailto:linux-ker...@vger.kernel.org] On Behalf Of H. Peter Anvin
Sent: Tuesday, May 30, 2006 3:46 PM
To: linux-...@vger.kernel.org
Subject: Re: Sharing memory between kernel and user space

Followup to:
<14CFC56C96D8554AA0B8...@chicken.machinevisionproducts.

Christopher Friesen

unread,
May 30, 2006, 7:21:55 PM5/30/06
to Brian D. McGrew, H. Peter Anvin, linux-...@vger.kernel.org
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

Brian D. McGrew

unread,
May 30, 2006, 8:09:41 PM5/30/06
to H. Peter Anvin, linux-...@vger.kernel.org
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 { br...@visionpro.com || br...@doubledimension.com }
--
> 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: linux-ker...@vger.kernel.org
[mailto:linux-ker...@vger.kernel.org] On Behalf Of H. Peter Anvin
Sent: Tuesday, May 30, 2006 3:46 PM
To: linux-...@vger.kernel.org
Subject: Re: Sharing memory between kernel and user space

Followup to:
<14CFC56C96D8554AA0B8...@chicken.machinevisionproducts.
com>
By author: "Brian D. McGrew" <br...@visionpro.com>
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

Jonathan Corbet

unread,
May 31, 2006, 8:34:37 AM5/31/06
to Brian D. McGrew, linux-...@vger.kernel.org
> 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

0 new messages