* Questions about mmap
[not found] <3fedcc3b0810050621y3f44417ag605ee75dd0389100@mail.gmail.com>
@ 2008-10-05 13:24 ` Thiago Lacerda
2008-10-05 14:01 ` Stefan Richter
0 siblings, 1 reply; 9+ messages in thread
From: Thiago Lacerda @ 2008-10-05 13:24 UTC (permalink / raw)
To: linux-kernel
Hi everyone,
I'd like to share a hashtable among kernel and userspace.
The hashtable resolves collision by chaining.
I have the following structure:
typedef struct flow_t flow_t;
struct flow_t {
...
...
flow_t *next;
flow_t *prev;
};
typedef struct hashtable {
flow_t *hashBuckets[X]; // X is a natural number
unsigned int size;
} Hastable;
So, my question is: Can I mmap the hashtable struct and access
directly from userspace? I'm afraid that this could turn on mess
because of the array of pointers.
If it's possible, does anyone know a piece of code that can match my
problem? I've been googling and I just find codes dealing with structs
itself, not pointers.
Best regards
--
Thiago de Barros Lacerda
Computer Science Undergraduate Student - CIn/UFPE - 2004.2
Researcher/Software Developer - GPRT - Networking and
Telecommunications Research Group
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Questions about mmap
2008-10-05 13:24 ` Questions about mmap Thiago Lacerda
@ 2008-10-05 14:01 ` Stefan Richter
[not found] ` <3fedcc3b0810070638u60681b6do8a7e94c4919f4a40@mail.gmail.com>
0 siblings, 1 reply; 9+ messages in thread
From: Stefan Richter @ 2008-10-05 14:01 UTC (permalink / raw)
To: Thiago Lacerda; +Cc: linux-kernel
Thiago Lacerda wrote:
> typedef struct hashtable {
> flow_t *hashBuckets[X]; // X is a natural number
> unsigned int size;
> } Hastable;
>
>
> So, my question is: Can I mmap the hashtable struct and access
> directly from userspace? I'm afraid that this could turn on mess
> because of the array of pointers.
> If it's possible, does anyone know a piece of code that can match my
> problem? I've been googling and I just find codes dealing with structs
> itself, not pointers.
If you require portability, you can only use integer types of fixed size
in kernel<->userspace ABIs. Pointers can then be exchanged as __u64
under the assumption that pointers not wider than 64 bits. See for
example the FW_CDEV_IOC_QUEUE_ISO ioctl in include/linux/firewire-cdev.h.
--
Stefan Richter
-=====-==--- =-=- --=-=
http://arcgraph.de/sr/
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Questions about mmap
[not found] ` <3fedcc3b0810070638u60681b6do8a7e94c4919f4a40@mail.gmail.com>
@ 2008-10-07 13:40 ` Thiago Lacerda
2008-10-07 16:02 ` Nick Piggin
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Thiago Lacerda @ 2008-10-07 13:40 UTC (permalink / raw)
To: linux-kernel; +Cc: Stefan Richter
Thank you Stefan.... by I'd something more concrete.
I'm trying to do like this:
//Code of the char device
unsigned int **test;
static int device_mmap(struct file *filp, struct vm_area_struct *vma) {
printk(KERN_INFO"Calling mmap\n");
vma->vm_flags |= VM_LOCKED;
if(remap_pfn_range(vma, vma->vm_start,
virt_to_phys((void*)((unsigned long)test)) >> PAGE_SHIFT, vma->vm_end
- vma->vm_start, PAGE_SHARED))
return -EAGAIN;
printk(KERN_INFO"mmap returned\n");
return 0;
}
static int __init testeInit(void) {
/* after creating char device and registering it*/
...
....
test = (unsigned int**) kmalloc(sizeof(unsigned int*)*1024, GFP_KERNEL);
int i;
for(i = 0; i < 1024; i++)
test[i] = NULL;
unsigned int* temp1 = (unsigned int*) kmalloc(sizeof(unsigned int), GFP_KERNEL);
(*temp1) = 9;
test[0] = temp1;
return 0;
}
and at user program:
int main() {
int fd;
unsigned int **mptr;
size_t size = 1024*sizeof(unsigned int*);
fd = open("/dev/myDev", O_RDWR);
if( fd == -1) {
printf("open error...\n");
exit(0);
}
mptr = mmap(0, sizeof(unsigned int*)*1024, PROT_READ | PROT_WRITE,
MAP_FILE | MAP_SHARED, fd, 0);
if(mptr == MAP_FAILED) {
printf("mmap() failed\n");
exit(1);
}
printf("teste[0]: %u\n", *mptr[0]);
munmap(mptr, size);
close(fd);
return 0;
}
It's not working, could you tell me what am I doing wrong? It is for
my undergraduate thesis ans it is really driving me mad.
I hope that you can help me.
best regards.
On Sun, Oct 5, 2008 at 11:01 AM, Stefan Richter
<stefanr@s5r6.in-berlin.de> wrote:
>
> Thiago Lacerda wrote:
> > typedef struct hashtable {
> > flow_t *hashBuckets[X]; // X is a natural number
> > unsigned int size;
> > } Hastable;
> >
> >
> > So, my question is: Can I mmap the hashtable struct and access
> > directly from userspace? I'm afraid that this could turn on mess
> > because of the array of pointers.
> > If it's possible, does anyone know a piece of code that can match my
> > problem? I've been googling and I just find codes dealing with structs
> > itself, not pointers.
>
> If you require portability, you can only use integer types of fixed size
> in kernel<->userspace ABIs. Pointers can then be exchanged as __u64
> under the assumption that pointers not wider than 64 bits. See for
> example the FW_CDEV_IOC_QUEUE_ISO ioctl in include/linux/firewire-cdev.h.
> --
> Stefan Richter
> -=====-==--- =-=- --=-=
> http://arcgraph.de/sr/
--
Thiago de Barros Lacerda
Computer Science Undergraduate Student - CIn/UFPE - 2004.2
Researcher/Software Developer - GPRT - Networking and
Telecommunications Research Group
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Questions about mmap
2008-10-07 13:40 ` Thiago Lacerda
@ 2008-10-07 16:02 ` Nick Piggin
2008-10-07 16:06 ` Stefan Richter
2008-10-07 20:43 ` linux-os (Dick Johnson)
2 siblings, 0 replies; 9+ messages in thread
From: Nick Piggin @ 2008-10-07 16:02 UTC (permalink / raw)
To: Thiago Lacerda; +Cc: linux-kernel, Stefan Richter
On Wednesday 08 October 2008 00:40, Thiago Lacerda wrote:
> Thank you Stefan.... by I'd something more concrete.
>
> I'm trying to do like this:
>
> //Code of the char device
> unsigned int **test;
> static int device_mmap(struct file *filp, struct vm_area_struct *vma) {
> printk(KERN_INFO"Calling mmap\n");
> vma->vm_flags |= VM_LOCKED;
> if(remap_pfn_range(vma, vma->vm_start,
> virt_to_phys((void*)((unsigned long)test)) >> PAGE_SHIFT, vma->vm_end
> - vma->vm_start, PAGE_SHARED))
I guess it is more usual to use vma->vm_page_prot for the last argument.
Unless you really specifically want to override it.
> return -EAGAIN;
> printk(KERN_INFO"mmap returned\n");
> return 0;
> }
>
> static int __init testeInit(void) {
> /* after creating char device and registering it*/
> ...
> ....
> test = (unsigned int**) kmalloc(sizeof(unsigned int*)*1024, GFP_KERNEL);
> int i;
> for(i = 0; i < 1024; i++)
> test[i] = NULL;
>
> unsigned int* temp1 = (unsigned int*) kmalloc(sizeof(unsigned int),
> GFP_KERNEL); (*temp1) = 9;
> test[0] = temp1;
> return 0;
> }
The idea seems strange because the userspace program will never be able
to dereference these pointers.
> and at user program:
>
> int main() {
> int fd;
>
> unsigned int **mptr;
> size_t size = 1024*sizeof(unsigned int*);
> fd = open("/dev/myDev", O_RDWR);
> if( fd == -1) {
> printf("open error...\n");
> exit(0);
> }
>
> mptr = mmap(0, sizeof(unsigned int*)*1024, PROT_READ | PROT_WRITE,
> MAP_FILE | MAP_SHARED, fd, 0);
> if(mptr == MAP_FAILED) {
> printf("mmap() failed\n");
> exit(1);
> }
> printf("teste[0]: %u\n", *mptr[0]);
> munmap(mptr, size);
> close(fd);
> return 0;
> }
>
> It's not working, could you tell me what am I doing wrong? It is for
> my undergraduate thesis ans it is really driving me mad.
>
> I hope that you can help me.
What is not working? What is failing or going wrong for you?
Can't really help without knowing that.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Questions about mmap
2008-10-07 13:40 ` Thiago Lacerda
2008-10-07 16:02 ` Nick Piggin
@ 2008-10-07 16:06 ` Stefan Richter
2008-10-07 17:09 ` Thiago Lacerda
2008-10-07 20:43 ` linux-os (Dick Johnson)
2 siblings, 1 reply; 9+ messages in thread
From: Stefan Richter @ 2008-10-07 16:06 UTC (permalink / raw)
To: Thiago Lacerda; +Cc: linux-kernel
Thiago Lacerda wrote:
> I'm trying to do like this:
>
> //Code of the char device
> unsigned int **test;
> static int device_mmap(struct file *filp, struct vm_area_struct *vma) {
[...]
> could you tell me what am I doing wrong?
I can't; I never implemented a mmap file operation myself.
--
Stefan Richter
-=====-==--- =-=- --===
http://arcgraph.de/sr/
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Questions about mmap
2008-10-07 16:06 ` Stefan Richter
@ 2008-10-07 17:09 ` Thiago Lacerda
2008-10-08 4:45 ` Nick Piggin
0 siblings, 1 reply; 9+ messages in thread
From: Thiago Lacerda @ 2008-10-07 17:09 UTC (permalink / raw)
To: Stefan Richter; +Cc: linux-kernel
Thank you people.
When I try to dereference those pointers in user space I get a segfault :(
On Tue, Oct 7, 2008 at 1:06 PM, Stefan Richter
<stefanr@s5r6.in-berlin.de> wrote:
> Thiago Lacerda wrote:
>> I'm trying to do like this:
>>
>> //Code of the char device
>> unsigned int **test;
>> static int device_mmap(struct file *filp, struct vm_area_struct *vma) {
> [...]
>> could you tell me what am I doing wrong?
>
> I can't; I never implemented a mmap file operation myself.
> --
> Stefan Richter
> -=====-==--- =-=- --===
> http://arcgraph.de/sr/
>
--
Thiago de Barros Lacerda
Computer Science Undergraduate Student - CIn/UFPE - 2004.2
Researcher/Software Developer - GPRT - Networking and
Telecommunications Research Group
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Questions about mmap
2008-10-07 13:40 ` Thiago Lacerda
2008-10-07 16:02 ` Nick Piggin
2008-10-07 16:06 ` Stefan Richter
@ 2008-10-07 20:43 ` linux-os (Dick Johnson)
2008-10-07 20:56 ` Chris Friesen
2 siblings, 1 reply; 9+ messages in thread
From: linux-os (Dick Johnson) @ 2008-10-07 20:43 UTC (permalink / raw)
To: Thiago Lacerda; +Cc: Linux kernel, Stefan Richter
On Tue, 7 Oct 2008, Thiago Lacerda wrote:
> Thank you Stefan.... by I'd something more concrete.
>
> I'm trying to do like this:
>
> //Code of the char device
> unsigned int **test;
> static int device_mmap(struct file *filp, struct vm_area_struct *vma) {
> printk(KERN_INFO"Calling mmap\n");
> vma->vm_flags |= VM_LOCKED;
> if(remap_pfn_range(vma, vma->vm_start,
> virt_to_phys((void*)((unsigned long)test)) >> PAGE_SHIFT, vma->vm_end
> - vma->vm_start, PAGE_SHARED))
> return -EAGAIN;
> printk(KERN_INFO"mmap returned\n");
> return 0;
> }
>
> static int __init testeInit(void) {
> /* after creating char device and registering it*/
> ...
> ....
> test = (unsigned int**) kmalloc(sizeof(unsigned int*)*1024, GFP_KERNEL);
> int i;
> for(i = 0; i < 1024; i++)
> test[i] = NULL;
>
> unsigned int* temp1 = (unsigned int*) kmalloc(sizeof(unsigned int), GFP_KERNEL);
> (*temp1) = 9;
> test[0] = temp1;
> return 0;
> }
>
> and at user program:
>
> int main() {
> int fd;
>
> unsigned int **mptr;
> size_t size = 1024*sizeof(unsigned int*);
> fd = open("/dev/myDev", O_RDWR);
> if( fd == -1) {
> printf("open error...\n");
> exit(0);
> }
>
> mptr = mmap(0, sizeof(unsigned int*)*1024, PROT_READ | PROT_WRITE,
> MAP_FILE | MAP_SHARED, fd, 0);
> if(mptr == MAP_FAILED) {
> printf("mmap() failed\n");
> exit(1);
> }
> printf("teste[0]: %u\n", *mptr[0]);
> munmap(mptr, size);
> close(fd);
> return 0;
> }
>
> It's not working, could you tell me what am I doing wrong? It is for
> my undergraduate thesis ans it is really driving me mad.
>
> I hope that you can help me.
>
> best regards.
>
> --
> Thiago de Barros Lacerda
It looks like you are trying to memory-map a pointer that
you expect to de-reference in user space. Perhaps you can
tell us what it is that you are trying to do. You need
to use copy/to/from_user to copy things from or to the kernel.
Attempts to dereference kernel data will fail because the
segments for kernel data or code are not the same as user
data or code.
Cheers,
Dick Johnson
Penguin : Linux version 2.6.25.17 on an i686 machine (4786.81 BogoMips).
My book : http://www.AbominableFirebug.com/
_
****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to DeliveryErrors@analogic.com - and destroy all copies of this information, including any attachments, without reading or disclosing them.
Thank you.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Questions about mmap
2008-10-07 20:43 ` linux-os (Dick Johnson)
@ 2008-10-07 20:56 ` Chris Friesen
0 siblings, 0 replies; 9+ messages in thread
From: Chris Friesen @ 2008-10-07 20:56 UTC (permalink / raw)
To: linux-os (Dick Johnson); +Cc: Thiago Lacerda, Linux kernel, Stefan Richter
linux-os (Dick Johnson) wrote:
> On Tue, 7 Oct 2008, Thiago Lacerda wrote:
>
>
>>Thank you Stefan.... by I'd something more concrete.
>>
>>I'm trying to do like this:
>>
>>//Code of the char device
>>unsigned int **test;
>>static int device_mmap(struct file *filp, struct vm_area_struct *vma) {
>> printk(KERN_INFO"Calling mmap\n");
>> vma->vm_flags |= VM_LOCKED;
>> if(remap_pfn_range(vma, vma->vm_start,
>>virt_to_phys((void*)((unsigned long)test)) >> PAGE_SHIFT, vma->vm_end
>>- vma->vm_start, PAGE_SHARED))
>> return -EAGAIN;
>> printk(KERN_INFO"mmap returned\n");
>> return 0;
>>}
>>
>>static int __init testeInit(void) {
>>/* after creating char device and registering it*/
>>...
>>....
>>test = (unsigned int**) kmalloc(sizeof(unsigned int*)*1024, GFP_KERNEL);
>>int i;
>>for(i = 0; i < 1024; i++)
>> test[i] = NULL;
>>
>>unsigned int* temp1 = (unsigned int*) kmalloc(sizeof(unsigned int), GFP_KERNEL);
>>(*temp1) = 9;
>>test[0] = temp1;
>>return 0;
>>}
>>
>>and at user program:
>>
>>int main() {
>> int fd;
>>
>> unsigned int **mptr;
>> size_t size = 1024*sizeof(unsigned int*);
>> fd = open("/dev/myDev", O_RDWR);
>> if( fd == -1) {
>> printf("open error...\n");
>> exit(0);
>> }
>>
>> mptr = mmap(0, sizeof(unsigned int*)*1024, PROT_READ | PROT_WRITE,
>>MAP_FILE | MAP_SHARED, fd, 0);
>> if(mptr == MAP_FAILED) {
>> printf("mmap() failed\n");
>> exit(1);
>> }
>> printf("teste[0]: %u\n", *mptr[0]);
>> munmap(mptr, size);
>> close(fd);
>> return 0;
>>}
>>
>>It's not working, could you tell me what am I doing wrong? It is for
>>my undergraduate thesis ans it is really driving me mad.
>>
>>I hope that you can help me.
>>
>>best regards.
>>
>>--
>>Thiago de Barros Lacerda
>
>
> It looks like you are trying to memory-map a pointer that
> you expect to de-reference in user space. Perhaps you can
> tell us what it is that you are trying to do. You need
> to use copy/to/from_user to copy things from or to the kernel.
>
> Attempts to dereference kernel data will fail because the
> segments for kernel data or code are not the same as user
> data or code.
I do not think this is entirely true. I haven't tried it recently, but
I have reserved a page of kernel memory in a driver and mapped it into
userspace such that it can be accessed by both.
This can be useful for cases where you want to make continuously-updated
information available to userspace without the overhead of a syscall.
There can be some gotchas though...last time I tried this on ARM I had
to explicitly flush the cache on the memory area after writing it in the
kernel to ensure that userspace saw the updated data since it was
accessing it via a different virtual address.
Chris
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Questions about mmap
2008-10-07 17:09 ` Thiago Lacerda
@ 2008-10-08 4:45 ` Nick Piggin
0 siblings, 0 replies; 9+ messages in thread
From: Nick Piggin @ 2008-10-08 4:45 UTC (permalink / raw)
To: Thiago Lacerda; +Cc: Stefan Richter, linux-kernel
On Wednesday 08 October 2008 04:09, Thiago Lacerda wrote:
> Thank you people.
>
> When I try to dereference those pointers in user space I get a segfault :(
Dereference the pointer returned from mmap? Or dereference the pointers
referenced by that pointer?
The first should be possible, and will give you an array of kernel pointers.
If you try to dereference those kernel pointers, yes you should get a segfault
because kernel memory is always mapped in the page tables as privileged.
If you're on x86, you could try adding _PAGE_USER to __PAGE_KERNEL_EXEC. That
would be a fairly wild ride :)
Hmm, you might be able vmap the kernel memory with
__pgprot(__PAGE_KERNEL | _PAGE_USER), and use that mapping from both user and
kernel space. Not guaranteed to be portable or even correct.
Best would be to rethink what exactly you are trying to do, and achieve it
some other way.
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2008-10-08 4:45 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <3fedcc3b0810050621y3f44417ag605ee75dd0389100@mail.gmail.com>
2008-10-05 13:24 ` Questions about mmap Thiago Lacerda
2008-10-05 14:01 ` Stefan Richter
[not found] ` <3fedcc3b0810070638u60681b6do8a7e94c4919f4a40@mail.gmail.com>
2008-10-07 13:40 ` Thiago Lacerda
2008-10-07 16:02 ` Nick Piggin
2008-10-07 16:06 ` Stefan Richter
2008-10-07 17:09 ` Thiago Lacerda
2008-10-08 4:45 ` Nick Piggin
2008-10-07 20:43 ` linux-os (Dick Johnson)
2008-10-07 20:56 ` Chris Friesen
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.