Find the answer to your Linux question:
Results 1 to 6 of 6
I want to write a loop that sucks up all the 2mb slugs of memory I can get in the kernel. However, I want to leave "some" free space (say ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2010
    Posts
    3

    How to determine Amount of free kernel memory and problem with __get_free_pages()


    I want to write a loop that sucks up all the 2mb slugs of memory I can get in the kernel. However, I want to leave "some" free space (say 20% of the machine's total phys mem). So, how can I do that? I have a loop now something like as an experiment to get all the mem I can and then release enough for the system to continue running but the kernel hangs:

    for( i = 0; i < MAX_SLUGS; ++i )
    {
    if(( pMemSlugs[ i ] = __get_free_pages( GFP_KERNEL | __GFP_HIGHMEM | __GFP_NORETRY, 9)) == 0 )
    {
    break;
    }
    InfoLOG("Got page 0x%lx, PA=0x%lx\n", pMemSlugs[ i ], virt_to_phys((volatile void * )pMemSlugs[ i ]) );
    }

    If I make MAX_SLUGS too big, the kernel either panics or hangs (I haven't looked at which since the machine has no console). If I keep MAX_SLUGS small enough, the loop works (FYI, I release all this memory right after this loop since this is just an experiment). I have tried various flavors of the flags (GFP_KERNEL, etc.) to no avail.

    So, why does this hang the system? I have seen similar samples of code allocating all slugs of order N as a test and presumably, these worked.

    Second question, is there another way to do this? Could I instead loop allocating my 2mb slugs and then make another call and monitor the free memory and then stop allocating at some threshold.

    All help is appreciated.

    Scott

  2. #2
    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,755
    Remember, Linux is a multi-tasking system with a reintrant kernel. When you suck up all the memory available to the kernel, other operations that need memory will fail, resulting in the expected panic and/or hang. Unfortunately I don't know enough right at the moment to tell you if there is a function to tell you how many pages are available, although there is in linux/slab.h the function slab_is_available(void) which returns an int indicating if there is kernel memory available.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    May 2010
    Posts
    3
    Quote Originally Posted by Rubberman View Post
    Remember, Linux is a multi-tasking system with a reintrant kernel. When you suck up all the memory available to the kernel, other operations that need memory will fail, resulting in the expected panic and/or hang. Unfortunately I don't know enough right at the moment to tell you if there is a function to tell you how many pages are available, although there is in linux/slab.h the function slab_is_available(void) which returns an int indicating if there is kernel memory available.
    Said jokingly - oh, I thought this was DOS.

    The function is nr_free_pages().

    slab_is_available() is not really useful since it just tells if the slab allocator is available but not how much free memory there is.

  4. $spacer_open
    $spacer_close
  5. #4
    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,755
    Quote Originally Posted by staggart View Post
    Said jokingly - oh, I thought this was DOS.

    The function is nr_free_pages().

    slab_is_available() is not really useful since it just tells if the slab allocator is available but not how much free memory there is.
    Thanks I'll keep that function in mind for future reference. So, why don't you use that to determine how much memory to leave free for other kernel use?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #5
    Just Joined!
    Join Date
    May 2010
    Posts
    3
    Quote Originally Posted by Rubberman View Post
    Thanks I'll keep that function in mind for future reference. So, why don't you use that to determine how much memory to leave free for other kernel use?
    I do now. I didn't know about it when I made the original post! What I really want is to know how many "slugs" of order "N" are still available at any point in time (like what /proc/buddyinfo shows). I am poking around in the kernel now seeing how to get that in my driver.

  7. #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,755
    Ah. Delving into the mysteries of The Kernel! Black Magic of the highest order, for sure! Watch out, or pretty soon someone will recruit you as a kernel maintainer...
    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
  •