kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* User space memory
@ 2013-03-10 18:00 Christoph Seitz
  2013-03-12 13:08 ` Prabhu nath
  0 siblings, 1 reply; 6+ messages in thread
From: Christoph Seitz @ 2013-03-10 18:00 UTC (permalink / raw)
  To: kernelnewbies

Hi all,

I have some problems allocation Memory the right way and use it in my
kernel module.

I use a char device for reading and writing from/to a pcie dma card.
Especially the read function makes me some headache. The user allocates
some memory with posix_memalign and call the read function on the
device, so that the devices knows where to write to. My driver now uses
get_user_pages() to pin the user pages. The memory has never been
written or read by the user, so it's not yet in the RAM, right? And
get_user_pages returns a valid number of pages, but for every page the
same struct. (respectively the same pointer). Is there any way to ensure
that the user pages are in the ram and get_user_pages returns a valid
page array?

Regards
Chris

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

* User space memory
  2013-03-10 18:00 User space memory Christoph Seitz
@ 2013-03-12 13:08 ` Prabhu nath
  2013-03-12 14:03   ` Christoph Seitz
  2013-03-12 14:29   ` Valdis.Kletnieks at vt.edu
  0 siblings, 2 replies; 6+ messages in thread
From: Prabhu nath @ 2013-03-12 13:08 UTC (permalink / raw)
  To: kernelnewbies

On Sun, Mar 10, 2013 at 11:30 PM, Christoph Seitz <c.seitz@tu-bs.de> wrote:

> Hi all,
>
> I have some problems allocation Memory the right way and use it in my
> kernel module.
>
> I use a char device for reading and writing from/to a pcie dma card.
> Especially the read function makes me some headache. The user allocates
> some memory with posix_memalign and call the read function on the
> device, so that the devices knows where to write to. My driver now uses
> get_user_pages() to pin the user pages. The memory has never been
> written or read by the user, so it's not yet in the RAM, right? And
> get_user_pages returns a valid number of pages, but for every page the
> same struct. (respectively the same pointer). Is there any way to ensure
> that the user pages are in the ram and get_user_pages returns a valid
> page array?
>

If you know the RAM physical address range you can figure out by doing the
following
    *page_to_pfn(page_ptr) << 12*;
    where page_ptr is a struct page * returned by get_user_pages().
   * page_to_pfn()* will return the pfn of the corresponding page frame and
left shifting by 12 bits will give you page frame base address.



> Regards
> Chris
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>

-- 
Regards,
Prabhunath G
Linux Trainer
Bangalore
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130312/6238f3f1/attachment.html 

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

* User space memory
  2013-03-12 13:08 ` Prabhu nath
@ 2013-03-12 14:03   ` Christoph Seitz
  2013-03-12 17:18     ` Valdis.Kletnieks at vt.edu
  2013-03-12 14:29   ` Valdis.Kletnieks at vt.edu
  1 sibling, 1 reply; 6+ messages in thread
From: Christoph Seitz @ 2013-03-12 14:03 UTC (permalink / raw)
  To: kernelnewbies

Am 12.03.2013 14:08, schrieb Prabhu nath:
> 
> 
> On Sun, Mar 10, 2013 at 11:30 PM, Christoph Seitz <c.seitz@tu-bs.de
> <mailto:c.seitz@tu-bs.de>> wrote:
> 
>     Hi all,
> 
>     I have some problems allocation Memory the right way and use it in my
>     kernel module.
> 
>     I use a char device for reading and writing from/to a pcie dma card.
>     Especially the read function makes me some headache. The user allocates
>     some memory with posix_memalign and call the read function on the
>     device, so that the devices knows where to write to. My driver now uses
>     get_user_pages() to pin the user pages. The memory has never been
>     written or read by the user, so it's not yet in the RAM, right? And
>     get_user_pages returns a valid number of pages, but for every page the
>     same struct. (respectively the same pointer). Is there any way to ensure
>     that the user pages are in the ram and get_user_pages returns a valid
>     page array? 
> 
> 
> If you know the RAM physical address range you can figure out by doing
> the following
>     *page_to_pfn(page_ptr) << 12*;
>     where page_ptr is a struct page * returned by get_user_pages().
>    *page_to_pfn()* will return the pfn of the corresponding page frame
> and left shifting by 12 bits will give you page frame base address.

Maybe my description was a little bit misleading. I have the problem,
that if I don't write to that allocated memory in the user application,
I won't get any valid pages from get_user_pages. The reason seems to be,
that thees pages never got faulted and so the user memory never gets a
page frame. Is there any chance for a kernel module to force a page
fault or to assign the user memory a page frame?
I found out, if I use the force flag with get_user_pages, the pages get
faulted, but there has to be a nicer way than using the force flag.

Regards,
Chris

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

* User space memory
  2013-03-12 13:08 ` Prabhu nath
  2013-03-12 14:03   ` Christoph Seitz
@ 2013-03-12 14:29   ` Valdis.Kletnieks at vt.edu
  2013-03-13  5:18     ` Prabhu nath
  1 sibling, 1 reply; 6+ messages in thread
From: Valdis.Kletnieks at vt.edu @ 2013-03-12 14:29 UTC (permalink / raw)
  To: kernelnewbies

On Tue, 12 Mar 2013 18:38:05 +0530, Prabhu nath said:

> On Sun, Mar 10, 2013 at 11:30 PM, Christoph Seitz <c.seitz@tu-bs.de> wrote:

> > I use a char device for reading and writing from/to a pcie dma card.
> > Especially the read function makes me some headache. The user allocates
> > some memory with posix_memalign and call the read function on the
> > device, so that the devices knows where to write to. My driver now uses
> > get_user_pages() to pin the user pages. The memory has never been
> > written or read by the user, so it's not yet in the RAM, right? And
> > get_user_pages returns a valid number of pages, but for every page the
> > same struct. (respectively the same pointer). Is there any way to ensure
> > that the user pages are in the ram and get_user_pages returns a valid
> > page array?
> >
>
> If you know the RAM physical address range you can figure out by doing the
> following
>     *page_to_pfn(page_ptr) << 12*;
>     where page_ptr is a struct page * returned by get_user_pages().
>    * page_to_pfn()* will return the pfn of the corresponding page frame and
> left shifting by 12 bits will give you page frame base address.

Unfortunately, that doesn't actually tell you what Christoph was
worried about - is the page *currently* in RAM?  For that, you need
to check some bits in the pfn once you find it.

Also, note the following:

It's not always 12, because not everything uses a 4K page - consider hugepage
support, or Power and Itanium where the pages are bigger and often several
different sizes are supported.  There's an API for the current page size. Use
it. :)

Also, there's an API for pinning pages so they *stay* in RAM so you can target
them for I/O.  Use that. ;)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 865 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130312/de5bbe31/attachment-0001.bin 

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

* User space memory
  2013-03-12 14:03   ` Christoph Seitz
@ 2013-03-12 17:18     ` Valdis.Kletnieks at vt.edu
  0 siblings, 0 replies; 6+ messages in thread
From: Valdis.Kletnieks at vt.edu @ 2013-03-12 17:18 UTC (permalink / raw)
  To: kernelnewbies

On Tue, 12 Mar 2013 15:03:53 +0100, Christoph Seitz said:

> I found out, if I use the force flag with get_user_pages, the pages get
> faulted, but there has to be a nicer way than using the force flag.

Why does there have to be a nicer way?  Maybe you already got the nice way.

(Hint - why does the force flag even exist? :)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 865 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130312/5e8a95d6/attachment.bin 

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

* User space memory
  2013-03-12 14:29   ` Valdis.Kletnieks at vt.edu
@ 2013-03-13  5:18     ` Prabhu nath
  0 siblings, 0 replies; 6+ messages in thread
From: Prabhu nath @ 2013-03-13  5:18 UTC (permalink / raw)
  To: kernelnewbies

On Tue, Mar 12, 2013 at 7:59 PM, <Valdis.Kletnieks@vt.edu> wrote:

> On Tue, 12 Mar 2013 18:38:05 +0530, Prabhu nath said:
>
> > On Sun, Mar 10, 2013 at 11:30 PM, Christoph Seitz <c.seitz@tu-bs.de>
> wrote:
>
> > > I use a char device for reading and writing from/to a pcie dma card.
> > > Especially the read function makes me some headache. The user allocates
> > > some memory with posix_memalign and call the read function on the
> > > device, so that the devices knows where to write to. My driver now uses
> > > get_user_pages() to pin the user pages. The memory has never been
> > > written or read by the user, so it's not yet in the RAM, right? And
> > > get_user_pages returns a valid number of pages, but for every page the
> > > same struct. (respectively the same pointer). Is there any way to
> ensure
> > > that the user pages are in the ram and get_user_pages returns a valid
> > > page array?
> > >
> >
> > If you know the RAM physical address range you can figure out by doing
> the
> > following
> >     *page_to_pfn(page_ptr) << 12*;
> >     where page_ptr is a struct page * returned by get_user_pages().
> >    * page_to_pfn()* will return the pfn of the corresponding page frame
> and
> > left shifting by 12 bits will give you page frame base address.
>
> Unfortunately, that doesn't actually tell you what Christoph was
> worried about - is the page *currently* in RAM?  For that, you need
> to check some bits in the pfn once you find it.
>
> Also, note the following:
>
> It's not always 12, because not everything uses a 4K page - consider
> hugepage
> support, or Power and Itanium where the pages are bigger and often several
> different sizes are supported.  There's an API for the current page size.
> Use
> it. :)
>

 Oops.. it should be  page_to_pfn(page_ptr) << PAGE_SHIFT

>
> Also, there's an API for pinning pages so they *stay* in RAM so you can
> target
> them for I/O.  Use that. ;)
>



-- 
Regards,
Prabhunath G
Linux Trainer
Bangalore
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130313/60b9977c/attachment.html 

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

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

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-03-10 18:00 User space memory Christoph Seitz
2013-03-12 13:08 ` Prabhu nath
2013-03-12 14:03   ` Christoph Seitz
2013-03-12 17:18     ` Valdis.Kletnieks at vt.edu
2013-03-12 14:29   ` Valdis.Kletnieks at vt.edu
2013-03-13  5:18     ` Prabhu nath

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).