Discussion:
Difference between kmalloc and __get_free_pages
(too old to reply)
vishpat
2004-07-06 18:24:01 UTC
Permalink
Hello World

I know that the __get_free_pages is used to allocate KERNEL memory in
terms of PAGES while kmalloc is used to allocate arbitrary sized
CONTINOUS KERNEL memory.

However I was facing a problem when I was using "kmalloc" which got
solved when I switched over to "__get_free_pages". Even though I am
done with the problem I am curious about what was I doing wrong while
using "kmalloc". Perhaps some one can catch the flaw and let me know.

I have written a CHAR device driver which I use to export some
information from the kernel to the user space programs. Initially I
was allocating FIVE pages of kernel memory using "kmalloc" and then
locking these pages using the function "SetPageReserved". I then
memory mapped the memory in the user space. However SOMETIMES whenever
I tried reading data from the kernel my machine hung. However this
problem got solved when I switched over to "__get_free_pages" and then
could easily read the required data from the kernel.

Does anyone know the reason why must this be happening?

- Vishal
Kasper Dupont
2004-07-06 19:02:25 UTC
Permalink
Post by vishpat
Hello World
I know that the __get_free_pages is used to allocate KERNEL memory in
terms of PAGES while kmalloc is used to allocate arbitrary sized
CONTINOUS KERNEL memory.
Actually kmalloc use gfp to get the memory. The most
important advantage of kmalloc is, that it can fit
multiple small allocations into a single page.

Calling gfp directly will allow you to use different
options, for example to allocate high memory, which
is not possible with kmalloc.
Post by vishpat
However I was facing a problem when I was using "kmalloc" which got
solved when I switched over to "__get_free_pages". Even though I am
done with the problem I am curious about what was I doing wrong while
using "kmalloc". Perhaps some one can catch the flaw and let me know.
I have written a CHAR device driver which I use to export some
information from the kernel to the user space programs. Initially I
was allocating FIVE pages of kernel memory using "kmalloc"
kmalloc will round up the size you request to one of
the supported sizes. You can see the sizes in
/proc/slabinfo. An unmodified kernel will support
only sizes that are a power of two. That means you
have actually allocated eight pages.
Post by vishpat
and then
locking these pages using the function "SetPageReserved". I then
memory mapped the memory in the user space. However SOMETIMES whenever
I tried reading data from the kernel my machine hung. However this
problem got solved when I switched over to "__get_free_pages" and then
could easily read the required data from the kernel.
Well, it certainly sounds like a case where kmalloc
should not be used. When you allocate using kmalloc
you really don't own the pages, kmalloc owns the
pages, you are just allowed to use the memory. I'm
not even sure you are guaranteed that the allocation
will be page aligned (though I know that is the case
with the current kmalloc implementation).

I'm not sure exactly what happened, but you may have
different parts of the kernel accessing the page
struct each thinking they own it.

Using gfp directly you cannot allocate five pages,
what you specify is the power, which means 2 is 4
pages and 3 is 8 pages. If you actually specified
5 as the argument you were in fact requesting 32
pages. If you happen to write beyond the 8 allocated
pages, the extra pages could explain why it works
with gfp and not kmalloc. This is of course based on
a lot of assumptions I can't really know anything
about without seeing the code.
--
Kasper Dupont -- der bruger for meget tid paa usenet.
I'd rather be a hammer than a nail.
vishpat
2004-07-09 16:00:48 UTC
Permalink
Nikhil

I am sorry but I can't post the code until it has been officially
released. However check the website
http://public.lanl.gov/radiant/software/magnet.html
where the patch will be officially released in about a month

- Vishal
Post by Kasper Dupont
Post by vishpat
Hello World
I know that the __get_free_pages is used to allocate KERNEL memory in
terms of PAGES while kmalloc is used to allocate arbitrary sized
CONTINOUS KERNEL memory.
Actually kmalloc use gfp to get the memory. The most
important advantage of kmalloc is, that it can fit
multiple small allocations into a single page.
Calling gfp directly will allow you to use different
options, for example to allocate high memory, which
is not possible with kmalloc.
Post by vishpat
However I was facing a problem when I was using "kmalloc" which got
solved when I switched over to "__get_free_pages". Even though I am
done with the problem I am curious about what was I doing wrong while
using "kmalloc". Perhaps some one can catch the flaw and let me know.
I have written a CHAR device driver which I use to export some
information from the kernel to the user space programs. Initially I
was allocating FIVE pages of kernel memory using "kmalloc"
kmalloc will round up the size you request to one of
the supported sizes. You can see the sizes in
/proc/slabinfo. An unmodified kernel will support
only sizes that are a power of two. That means you
have actually allocated eight pages.
Post by vishpat
and then
locking these pages using the function "SetPageReserved". I then
memory mapped the memory in the user space. However SOMETIMES whenever
I tried reading data from the kernel my machine hung. However this
problem got solved when I switched over to "__get_free_pages" and then
could easily read the required data from the kernel.
Well, it certainly sounds like a case where kmalloc
should not be used. When you allocate using kmalloc
you really don't own the pages, kmalloc owns the
pages, you are just allowed to use the memory. I'm
not even sure you are guaranteed that the allocation
will be page aligned (though I know that is the case
with the current kmalloc implementation).
I'm not sure exactly what happened, but you may have
different parts of the kernel accessing the page
struct each thinking they own it.
Using gfp directly you cannot allocate five pages,
what you specify is the power, which means 2 is 4
pages and 3 is 8 pages. If you actually specified
5 as the argument you were in fact requesting 32
pages. If you happen to write beyond the 8 allocated
pages, the extra pages could explain why it works
with gfp and not kmalloc. This is of course based on
a lot of assumptions I can't really know anything
about without seeing the code.
nikhil bhargav
2004-07-09 07:21:28 UTC
Permalink
Post by vishpat
Hello World
I know that the __get_free_pages is used to allocate KERNEL memory in
terms of PAGES while kmalloc is used to allocate arbitrary sized
CONTINOUS KERNEL memory.
However I was facing a problem when I was using "kmalloc" which got
solved when I switched over to "__get_free_pages". Even though I am
done with the problem I am curious about what was I doing wrong while
using "kmalloc". Perhaps some one can catch the flaw and let me know.
I have written a CHAR device driver which I use to export some
information from the kernel to the user space programs. Initially I
was allocating FIVE pages of kernel memory using "kmalloc" and then
locking these pages using the function "SetPageReserved". I then
memory mapped the memory in the user space. However SOMETIMES whenever
I tried reading data from the kernel my machine hung. However this
problem got solved when I switched over to "__get_free_pages" and then
could easily read the required data from the kernel.
Does anyone know the reason why must this be happening?
- Vishal
Hey vishal,

Could u post the code of the driver. this would be very helpful to
those who are beginners in this field.

nikhil
Loading...