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

Submitted by Anonymous
on May 4, 2010 - 8:41pm

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, Page2NOrder )) == 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?

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