I have a requirement in kernel space wherein i have to program device register with 64K aligned address. Currently i am using kmalloc() to allocate memory but not sure how to align this 64K. Does linux provide a API which does this? Do i need to write my own code to do the same?
            Asked
            
        
        
            Active
            
        
            Viewed 3,073 times
        
    1
            
            
        - 
                    See http://stackoverflow.com/questions/5310641/how-to-perform-data-structure-alignment-with-kmalloc – Dan D. May 17 '13 at 08:51
 
1 Answers
2
            
            
        Try following approach:
First calling kmem_cache_create(...) function to create a cache pool for your structure which should be 64K aligned like following:
    buff_64k_cachep = kmem_cache_create("buff_64k_cache",
                                     sizeof(struct test_struct),
                                     1<<16, /* 64k alignment */
                                     flags,
                                     );
Then calling kmem_cache_alloc(...) and kmem_cache_free(...) to allocate and free the buffers you needed.
        tian_yufeng
        
- 1,780
 - 10
 - 8
 
- 
                    Thanks for the reply. I am only supposed to use kmalloc(). Doing some reading on net i see few answers: For eg if i want a memory of 2K, allocate 4K of memory, add 64K offset to the kmalloc() address and then &~(0x10000) to make it 64K aligned. I quite didn't understand this as we are wasting memory here. I also got confused when they say "bit" and "byte" alignment are different. Please help me out here. – pkumarn May 17 '13 at 13:09
 - 
                    After Googling and some think throw, i finally arrived at this logic. pusedocode : unal_addr = (Memory address + alignment_no); aligned_addr = unal_addr - (unal_addr%alignment_no) should give the required alignment. Please let me know if this is not right. – pkumarn May 21 '13 at 10:51
 - 
                    i dont' think so... your aligned_address should be larger than unalligned address – Erben Mo Dec 16 '14 at 02:11