Find the answer to your Linux question:
Results 1 to 6 of 6
Hi all, I'm new in the stuff of kernel programming. I want to write a kernel module in which I have to allocate a continious memory of 24MB. And I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    2

    How to allocate 24MB memory in kernel space


    Hi all,
    I'm new in the stuff of kernel programming. I want to write a kernel module in which I have to allocate a continious memory of 24MB. And I need the physical start address of these area to give it to another program.

    I already tried diffrent things but nothing solved my problem.

    1. to allocate with kmalloc worked fine but then I heard about the 128kB boarder.
    2. __get_free_pages an __get_dma_pages failed in kernel exeptions.
    3. I also tried to give the kernel the parameter mem=1000M at boottime to reserve the last 24MB for my module.
    But when I tried to remap the memory in kernelspace with ioremap( 0x3E800000 /* 1000MB */, 0x1800000 /* 24MB */) to get the physical start address of these area I get a kernel exeption again.

    Can anyone help me?

    Thanks to everyone who spend time on my problem.

    Michael

  2. #2
    Just Joined!
    Join Date
    May 2008
    Posts
    55

    Thumbs up

    have you tried with vmalloc ? of course it will give you virtually contigous memory....is that what you need ?
    another thing is : you never get physical start address, but get kernel virtual start address.
    of couse physical address is PAGE_OFFSET away.

  3. #3
    Just Joined!
    Join Date
    Mar 2009
    Posts
    2
    hi matt,
    thanks for your answer but vmalloc doesn't work. I need physical addresses because I have to give the startaddress to an PCI-Device which is using the memory for buffering data.
    when I use kmalloc I get an address which can be translated to physical address by using virt_to_phys() but the memory I can malloc with kmalloc isn't big enough.

  4. #4
    Just Joined!
    Join Date
    Mar 2009
    Location
    Norway
    Posts
    67
    What's the 128kB border? According to slab.h, the KMALLOC_MAX_SIZE is 4MB
    (2^(MAX_ORDER + PAGE_SHIFT -1), which is 2^(11 + 12 - 1) = 1MB * 2^2

    Meaning, the maximum size chunks kmalloc can allocate should be 4MB. So, how did you manage to do this with kmalloc?

    Could you post some of your code along with dmesg/error output?

  5. #5
    Just Joined!
    Join Date
    May 2008
    Posts
    55
    Is it possible to load your module just at the time of kernel startup may be init scripts or something !!?
    there may be __get_free_pages might work.
    but this is not a convenient solution.

    I have some questions:

    1 )why is really a requirement of having 24 MB contiguous physical space ??

    2) is that the pointer you are going to pass to user space or kernel space ?

    3) I suppose virtually contiguous area should be enough in most cases irrespective of user/kernel space.

    4) as you said :
    you need physical addresses because you have to give the start address to an PCI-Device which is using the memory for buffering data.
    is it DMA capable ?
    otherwise I suppose, vmalloc may be used.

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,406
    I suppose you already digested all the docs on kernel driver and module programming at The Linux Documentation Project Also, there is an entire chapter in Linux Device Drivers, Third Edition by Corbet, Rubini, and Kroah-Hartman (O'Reilly) that covers memory allocation and management in kernel drivers. This book is available for free download as a set of PDF's. I have found it to be of great help in kernel module and driver development. In any case, if you need more than about 128K of memory (24M in your case), you need to use __get_free_pages(unsigned int flags, unsigned int order). The order argument is the number of pages as log2N. IE, if you want 1 page, use 0. If you want 8 pages, use 3. Assuming the system uses 2048 byte page size, you want 12K pages. Since this is a logrithmic scale, you want an order of 14 == 16K pages. The flag will determine whether or not the memory is in DMA-capable space, or high memory. Since generally DMA memory is limited to 16M, you probably want high mem, so the flags argument would be __GFP_HIGHMEM or'd with other appropriate flags, such as __GFP_NOFAIL.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •