public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* remap kernel static memory to user space
@ 2013-03-20 20:06 Eduardo Cruz
  2013-03-21 23:45 ` Eduardo Cruz
  2013-03-23 21:33 ` Hugh Dickins
  0 siblings, 2 replies; 6+ messages in thread
From: Eduardo Cruz @ 2013-03-20 20:06 UTC (permalink / raw)
  To: linux-kernel

Hello.

I'm trying to remap some kernel static memory to user space using
remap_pfn_range.
For that, I wrote a module that initializes a device, which later I
use mknod, etc.

When I allocate the memory with kmalloc, everything works:   (where
user_space_shared was allocated with kmalloc)

if ((ret = remap_pfn_range(vma, vma->vm_start,
virt_to_phys(user_space_shared) >> PAGE_SHIFT, length,
vma->vm_page_prot)) < 0)

In the application in user space:
kstc = mmap(0, sizeof(user_space_shared_t), PROT_READ|PROT_WRITE,
MAP_SHARED| MAP_LOCKED, fd, 0);

With user_space_shared allocated using kmalloc, it is working.
The application is able to read the data I wrote in the kernel module.

However, when I define and allocate that var in arch/ia64/mm/fault.c:

unsigned char user_space_shared[ sizeof(user_space_shared_t) +
PAGE_SIZE ] __attribute__((aligned(PAGE_SIZE)));
EXPORT_SYMBOL(user_space_shared);

And access that var from the module:

extern unsigned char user_space_shared[ sizeof(user_space_shared_t) +
PAGE_SIZE ];

There is no error message, but I can't read the contents i wrote
inside the kernel module from the application.

Your help would be appreciated.

ps:
I am taking care of page offsets/boundaries.
I'm using kernel 2.6.32 for ia64.


-- 
Eduardo Henrique Molina da Cruz
PhD student
Parallel and Distributed Processing Group
Federal University of Rio Grande do Sul (UFRGS)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: remap kernel static memory to user space
  2013-03-20 20:06 remap kernel static memory to user space Eduardo Cruz
@ 2013-03-21 23:45 ` Eduardo Cruz
  2013-03-22  8:10   ` Clemens Ladisch
  2013-03-23 21:33 ` Hugh Dickins
  1 sibling, 1 reply; 6+ messages in thread
From: Eduardo Cruz @ 2013-03-21 23:45 UTC (permalink / raw)
  To: linux-kernel

Please, anyone knows how to solve this problem?

2013/3/20 Eduardo Cruz <eduardohmdacruz@gmail.com>:
> Hello.
>
> I'm trying to remap some kernel static memory to user space using
> remap_pfn_range.
> For that, I wrote a module that initializes a device, which later I
> use mknod, etc.
>
> When I allocate the memory with kmalloc, everything works:   (where
> user_space_shared was allocated with kmalloc)
>
> if ((ret = remap_pfn_range(vma, vma->vm_start,
> virt_to_phys(user_space_shared) >> PAGE_SHIFT, length,
> vma->vm_page_prot)) < 0)
>
> In the application in user space:
> kstc = mmap(0, sizeof(user_space_shared_t), PROT_READ|PROT_WRITE,
> MAP_SHARED| MAP_LOCKED, fd, 0);
>
> With user_space_shared allocated using kmalloc, it is working.
> The application is able to read the data I wrote in the kernel module.
>
> However, when I define and allocate that var in arch/ia64/mm/fault.c:
>
> unsigned char user_space_shared[ sizeof(user_space_shared_t) +
> PAGE_SIZE ] __attribute__((aligned(PAGE_SIZE)));
> EXPORT_SYMBOL(user_space_shared);
>
> And access that var from the module:
>
> extern unsigned char user_space_shared[ sizeof(user_space_shared_t) +
> PAGE_SIZE ];
>
> There is no error message, but I can't read the contents i wrote
> inside the kernel module from the application.
>
> Your help would be appreciated.
>
> ps:
> I am taking care of page offsets/boundaries.
> I'm using kernel 2.6.32 for ia64.
>
>
> --
> Eduardo Henrique Molina da Cruz
> PhD student
> Parallel and Distributed Processing Group
> Federal University of Rio Grande do Sul (UFRGS)



-- 
Eduardo Henrique Molina da Cruz
PhD student
Parallel and Distributed Processing Group
Federal University of Rio Grande do Sul (UFRGS)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: remap kernel static memory to user space
  2013-03-21 23:45 ` Eduardo Cruz
@ 2013-03-22  8:10   ` Clemens Ladisch
  2013-03-23 20:25     ` Eduardo Cruz
  0 siblings, 1 reply; 6+ messages in thread
From: Clemens Ladisch @ 2013-03-22  8:10 UTC (permalink / raw)
  To: Eduardo Cruz; +Cc: linux-kernel

Eduardo Cruz wrote:
> Please, anyone knows how to solve this problem?
>
> 2013/3/20 Eduardo Cruz <eduardohmdacruz@gmail.com>:
>> I'm trying to remap some kernel static memory to user space using
>> remap_pfn_range.

Well, don't do that.

What is the actual problem you're trying to solve?


Regards,
Clemens

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: remap kernel static memory to user space
  2013-03-22  8:10   ` Clemens Ladisch
@ 2013-03-23 20:25     ` Eduardo Cruz
  0 siblings, 0 replies; 6+ messages in thread
From: Eduardo Cruz @ 2013-03-23 20:25 UTC (permalink / raw)
  To: Clemens Ladisch; +Cc: linux-kernel

I'm adding some asm code in arch/ia64/kernel/ivt.S
to gather some memory usage statistics.

>From whatever reason, when I allocate the memory using
kmalloc/vmalloc, my assembly code cashes the machine when I load the
allocated memory.

If I use static allocated memory, the asm code works.

I know the problem is not the assembly code.

The problem is that I want to be able to read this data from user space.
Since the code performance critical, I don't want to do a
copy_to_user, I prefer to mmap this data into user space.

The problem is, if I use kmalloc/vmalloc, I managed to map the memory
to user space.
However, the asm interrupt handler crashs.

On the other hand, if I use static data, the asm interrupt handler
works, while the mmap fails.

Do you have any clues?

2013/3/22 Clemens Ladisch <clemens@ladisch.de>:
> Eduardo Cruz wrote:
>> Please, anyone knows how to solve this problem?
>>
>> 2013/3/20 Eduardo Cruz <eduardohmdacruz@gmail.com>:
>>> I'm trying to remap some kernel static memory to user space using
>>> remap_pfn_range.
>
> Well, don't do that.
>
> What is the actual problem you're trying to solve?
>
>
> Regards,
> Clemens



-- 
Eduardo Henrique Molina da Cruz
PhD student
Parallel and Distributed Processing Group
Federal University of Rio Grande do Sul (UFRGS)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: remap kernel static memory to user space
  2013-03-20 20:06 remap kernel static memory to user space Eduardo Cruz
  2013-03-21 23:45 ` Eduardo Cruz
@ 2013-03-23 21:33 ` Hugh Dickins
  2013-03-23 23:13   ` Eduardo Cruz
  1 sibling, 1 reply; 6+ messages in thread
From: Hugh Dickins @ 2013-03-23 21:33 UTC (permalink / raw)
  To: Eduardo Cruz; +Cc: Clemens Ladisch, linux-kernel, linux-ia64

On Wed, 20 Mar 2013, Eduardo Cruz wrote:

> Hello.
> 
> I'm trying to remap some kernel static memory to user space using
> remap_pfn_range.
> For that, I wrote a module that initializes a device, which later I
> use mknod, etc.
> 
> When I allocate the memory with kmalloc, everything works:   (where
> user_space_shared was allocated with kmalloc)
> 
> if ((ret = remap_pfn_range(vma, vma->vm_start,
> virt_to_phys(user_space_shared) >> PAGE_SHIFT, length,
> vma->vm_page_prot)) < 0)

I have no ia64 experience, but my guess is that your problem is with
that virt_to_phys(user_space_shared).

virt_to_phys() expects to be given an address from the kernel's direct
map of physical memory; whereas if you declare user_space_shared[] in
kernel static memory, I believe ia64 places that in a different region.

Try changing that to virt_to_phys(ia64_imva(user_space_shared))
and I expect it will then work: see comment above ia64_imva()
in arch/ia64/include/asm/processor.h.

Hugh

> 
> In the application in user space:
> kstc = mmap(0, sizeof(user_space_shared_t), PROT_READ|PROT_WRITE,
> MAP_SHARED| MAP_LOCKED, fd, 0);
> 
> With user_space_shared allocated using kmalloc, it is working.
> The application is able to read the data I wrote in the kernel module.
> 
> However, when I define and allocate that var in arch/ia64/mm/fault.c:
> 
> unsigned char user_space_shared[ sizeof(user_space_shared_t) +
> PAGE_SIZE ] __attribute__((aligned(PAGE_SIZE)));
> EXPORT_SYMBOL(user_space_shared);
> 
> And access that var from the module:
> 
> extern unsigned char user_space_shared[ sizeof(user_space_shared_t) +
> PAGE_SIZE ];
> 
> There is no error message, but I can't read the contents i wrote
> inside the kernel module from the application.
> 
> Your help would be appreciated.
> 
> ps:
> I am taking care of page offsets/boundaries.
> I'm using kernel 2.6.32 for ia64.
> 
> 
> -- 
> Eduardo Henrique Molina da Cruz
> PhD student
> Parallel and Distributed Processing Group
> Federal University of Rio Grande do Sul (UFRGS)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: remap kernel static memory to user space
  2013-03-23 21:33 ` Hugh Dickins
@ 2013-03-23 23:13   ` Eduardo Cruz
  0 siblings, 0 replies; 6+ messages in thread
From: Eduardo Cruz @ 2013-03-23 23:13 UTC (permalink / raw)
  To: Hugh Dickins; +Cc: Clemens Ladisch, linux-kernel, linux-ia64

Man, it worked!
You saved my life kkkkkkkkkkkkkkk.
Thank you very much!

2013/3/23 Hugh Dickins <hughd@google.com>:
> On Wed, 20 Mar 2013, Eduardo Cruz wrote:
>
>> Hello.
>>
>> I'm trying to remap some kernel static memory to user space using
>> remap_pfn_range.
>> For that, I wrote a module that initializes a device, which later I
>> use mknod, etc.
>>
>> When I allocate the memory with kmalloc, everything works:   (where
>> user_space_shared was allocated with kmalloc)
>>
>> if ((ret = remap_pfn_range(vma, vma->vm_start,
>> virt_to_phys(user_space_shared) >> PAGE_SHIFT, length,
>> vma->vm_page_prot)) < 0)
>
> I have no ia64 experience, but my guess is that your problem is with
> that virt_to_phys(user_space_shared).
>
> virt_to_phys() expects to be given an address from the kernel's direct
> map of physical memory; whereas if you declare user_space_shared[] in
> kernel static memory, I believe ia64 places that in a different region.
>
> Try changing that to virt_to_phys(ia64_imva(user_space_shared))
> and I expect it will then work: see comment above ia64_imva()
> in arch/ia64/include/asm/processor.h.
>
> Hugh
>
>>
>> In the application in user space:
>> kstc = mmap(0, sizeof(user_space_shared_t), PROT_READ|PROT_WRITE,
>> MAP_SHARED| MAP_LOCKED, fd, 0);
>>
>> With user_space_shared allocated using kmalloc, it is working.
>> The application is able to read the data I wrote in the kernel module.
>>
>> However, when I define and allocate that var in arch/ia64/mm/fault.c:
>>
>> unsigned char user_space_shared[ sizeof(user_space_shared_t) +
>> PAGE_SIZE ] __attribute__((aligned(PAGE_SIZE)));
>> EXPORT_SYMBOL(user_space_shared);
>>
>> And access that var from the module:
>>
>> extern unsigned char user_space_shared[ sizeof(user_space_shared_t) +
>> PAGE_SIZE ];
>>
>> There is no error message, but I can't read the contents i wrote
>> inside the kernel module from the application.
>>
>> Your help would be appreciated.
>>
>> ps:
>> I am taking care of page offsets/boundaries.
>> I'm using kernel 2.6.32 for ia64.
>>
>>
>> --
>> Eduardo Henrique Molina da Cruz
>> PhD student
>> Parallel and Distributed Processing Group
>> Federal University of Rio Grande do Sul (UFRGS)



-- 
Eduardo Henrique Molina da Cruz
PhD student
Parallel and Distributed Processing Group
Federal University of Rio Grande do Sul (UFRGS)

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-03-23 23:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-03-20 20:06 remap kernel static memory to user space Eduardo Cruz
2013-03-21 23:45 ` Eduardo Cruz
2013-03-22  8:10   ` Clemens Ladisch
2013-03-23 20:25     ` Eduardo Cruz
2013-03-23 21:33 ` Hugh Dickins
2013-03-23 23:13   ` Eduardo Cruz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox