LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Embedded & Single-board computer (https://www.linuxquestions.org/questions/linux-embedded-and-single-board-computer-78/)
-   -   kmalloc onto an aligned border (https://www.linuxquestions.org/questions/linux-embedded-and-single-board-computer-78/kmalloc-onto-an-aligned-border-835200/)

timboar 09-29-2010 10:06 AM

kmalloc onto an aligned border
 
I have a way around this (see my previous thread), but I spent some time researching this to no avail, and I'm wondering if I missed something.

Is it possible to allocate memory in the kernel, such that it starts on a 2^n boundary (for some value of n)? I'm asking because I have a sub-processor which can do address translation using or/mask registers, so its internal memory has to start on a physical 2^n boundary (where 2^n > imagesize). Of course, the memory has to be physically contiguous as well. The allocation would happen near boot time so memory fragmentation would not be an issue. imagesize is large, so allocating 2^(n+1), and finding a boundary within isn't ideal.


Thanks

nini09 10-05-2010 02:36 PM

The kmalloc function can't do memory align. You have to do it yourself after allocating.
kmem_cache_create function can allocate memory on cache line align.

timboar 10-06-2010 11:20 AM

How would you 'do it yourself after allocating'? Is there a way to free part of a malloc'ed buffer?

also, can kmem_cache_create align to a size larger than a page size (say to a 32k boundary?)

Thanks

nini09 10-06-2010 02:47 PM

Assume you want to allocate 32k buffer and boundary is 32k, you can allocate 64k buffer and start to use at 32k boundary address. So you have to keep two pointers, buffer starting address and starting using address. Use buffer starting address to free the buffer later.

timboar 10-07-2010 09:13 AM

Yes, but then I've allocated 64K, 32K of which is being wasted. I would prefer to have that extra 32K available for other applications. If it was possible to allocate the 64K, and then free only the unused parts, that would be great...

timboar 10-30-2010 09:36 AM

I thought I would post my solution in case anyone else runs into the same problem. I used the kmem_cache_create / kmem_cache_alloc, with an align field set to the correct value. I read through the SLAB code, and it appears as though this will yield the correct result. I can't guarantee that this works with non-SLAB memory types though. Also, this is on the 2.6.30 kernel. I'm assuming this will be forward compatible with other kernel versions.


All times are GMT -5. The time now is 02:10 AM.