Skip to content
Advertisement

Initialization of number of free pages in each order in a memory zone

I am going through the memory initialization code in linux kernel, and I can see the number of free pages for each order and migration type for the each memory zone in a memory node is set to 0.

static void __meminit zone_init_free_lists(struct zone *zone)
{
int order, t;
for_each_migratetype_order(order, t) {
INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
zone->free_area[order].nr_free = 0;
}
}

I am not able to figure out how the list is set to the proper state, as in which order has how many default pages to to start with, or we start with all entries in the highest order.

In the book, I read the following:

The number of free pages (nr_free) is still currently defined as 0, and this obviously does not reflect the true situation. The correct value is not set until the bootmem allocator is disabled and normal buddy allocation comes into effect.

Can someone point to the actual location where this part is set to the default starting value?

Thanks a lot

Advertisement

Answer

There is a call mem_init(), which marks the free areas in the mem_map and tells us how much memory is free. This is done after various parts of the system have claimed their memory after the kernel image.

which further calls free_unused_memmap() and free_all_bootmem() api’s to free the memory (in pages) and will adds the free memory chunks(in pages) into different orders free_lists.

You can print the zones(Normal, Highmem etc) related information using show_free_areas().

The result of the show_free_areas() before/after freeing all bootup and other mem_map related memory.

Before:

Normal free:0kB … present:778240kB managed:772160kB … etc

HighMem free:0kB … present:270336kB managed:270336kB … etc

Normal: 0*4kB 0*8kB 0*16kB 0*32kB 0*64kB 0*128kB 0*256kB 0*512kB 0*1024kB 0*2048kB 0*4096kB = 0kB

HighMem: 0*4kB 0*8kB 0*16kB 0*32kB 0*64kB 0*128kB 0*256kB 0*512kB 0*1024kB 0*2048kB 0*4096kB = 0kB

After:

Normal free:542868kB … present:778240kB managed:542868kB … etc

HighMem free:270336kB … present:270336kB managed:270336kB … etc

Normal: 5*4kB (M) 2*8kB (M) 3*16kB (M) 4*32kB (M) 3*64kB (M) 4*128kB (M) 3*256kB (M) 7*512kB (M) 7*1024kB (M) 9*2048kB (M) 125*4096kB (M) = 542868kB

HighMem: 0*4kB 0*8kB 0*16kB 0*32kB 0*64kB 0*128kB 0*256kB 0*512kB 0*1024kB 0*2048kB 66*4096kB (M) = 270336kB

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement