A machine with 4 GiB fitted reports MemTotal: 4000424 kB, and the missing 189 MiB is neither lost nor leaked. It is memory memblock had marked reserved before the buddy allocator existed, so those pages were never handed over and never counted. This part traces the handover in mm/memblock.c and mm/mm_init.c, shows that MemTotal is one counter rather than a sum of the /proc/meminfo lines, and reconciles the three per-zone page counts to the exact page.
This is the first part of a new series on how the Linux memory manager accounts for RAM. This track has walked many kernel subsystems from source; memory management has not had its entry. The series starts where the accounting starts, before the buddy allocator is running at all, and establishes the arithmetic the rest of it reuses. If your question is how much RAM to fit, DRAM Shortage: How Much RAM Does Your Embedded Linux Product Need? answers that. This part answers where the RAM you fitted went.
A 4 GiB machine that reports 3,906 MiB
The machine below is an arm64 virtual machine with 4 GiB of RAM presented to it, running an Ubuntu 6.8.0-149-generic kernel with a 4 KiB page size. It is not an embedded board; every figure here comes from it, not from a product.
raghu@techveda.org:~$ free -m
total used free shared buff/cache available
Mem: 3906 122 1743 4 2040 3625
Swap: 0 0 0
raghu@techveda.org:~$ grep -E '^MemTotal|^CmaTotal' /proc/meminfo
MemTotal: 4000424 kB
CmaTotal: 32768 kBFour GiB is 4,194,304 kB. MemTotal is 4,000,424 kB. The difference is 193,880 kB, or 189.33 MiB. Two explanations are commonly offered and both are wrong. The first is the kernel image, which on arm64 is a few tens of megabytes at most. The second is the CMA pool, which the capture rules out: CmaTotal is 32 MiB and, as shown below, CMA is inside MemTotal at runtime.
What follows applies to kernels from 6.12 to 7.3 on any architecture that boots through memblock, and the source was read at the v6.12, v6.18 and v7.3 tags. It does not cover the 32-bit lowmem and highmem split, which Proposed Memory Split Defaults for 32-bit Arm covers, nor zone internals.
What the kernel knows about RAM before the buddy allocator exists
Very early in boot there is no page allocator, no struct page array and no zones. There is memblock, which is two arrays of physical address ranges: memblock.memory, the ranges that exist, and memblock.reserved, the ranges inside those that something has already claimed.
The first array is populated from whatever the platform uses to describe RAM. On a Device Tree platform the /memory node’s reg property is parsed and each range added through early_init_dt_add_memory_arch() in drivers/of/fdt.c; on x86 the same job is done from the E820 or EFI map. The result is a list of physical ranges with holes between them, and nothing yet about ownership.
The second array is where the 189 MiB comes from. Every claim made before the buddy allocator exists is a memblock_reserve() call, and three categories matter on a typical board:
- The kernel image. On arm64 this is one line in
arch/arm64/mm/init.c:memblock_reserve(__pa_symbol(_text), _end - _text);, covering text, rodata, data, BSS and the init sections. - Device Tree
reserved-memorychild nodes, scanned byearly_init_fdt_scan_reserved_mem(), which callsmemblock_reserve()for each. A firmware carve-out, a trusted execution region, a remoteproc buffer or a framebuffer reservation is recorded here, along with the Device Tree blob itself. - Early allocations through the
memblockallocator, which is how thestruct pagearray, the zone structures and the early page tables are obtained.
memblock.reserved is a record of decisions already taken. Nothing later in boot revisits them to ask whether the buddy allocator could have the memory instead.
memblock_free_all() hands the free ranges to the buddy allocator
The handover is a single function, short enough at v7.3 to read in full:
void __init memblock_free_all(void)
{
unsigned long pages;
free_unused_memmap();
reset_all_zones_managed_pages();
memblock_clear_kho_scratch_only();
pages = free_low_memory_core_early();
totalram_pages_add(pages);
}reset_all_zones_managed_pages() walks every zone of every online node and does atomic_long_set(&z->managed_pages, 0), so the count starts from zero. Then free_low_memory_core_early() does the work:
static unsigned long __init free_low_memory_core_early(void)
{
unsigned long count = 0;
phys_addr_t start, end;
u64 i;
memblock_clear_hotplug(0, -1);
memmap_init_reserved_pages();
/*
* We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
* because in some case like Node0 doesn't have RAM installed
* low ram will be on Node1
*/
for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
NULL)
count += __free_memory_core(start, end);
return count;
}Free has a precise meaning in that iterator: a range in memblock.memory and not in memblock.reserved. Reserved ranges are not visited at all. Each range that is visited goes to __free_memory_core(), which rounds the start up and the end down to whole pages, calls __free_pages_memory() to release them into the buddy allocator in the largest orders that fit, and returns the page count. The returns are summed and added to one global counter by totalram_pages_add(pages).
That is the whole accounting event. A reserved page is not marked as belonging to the kernel and is not filed under a category. It is simply never passed to __free_memory_core(), so it is never counted, and no later code adds it back.
The call site moved during the version range covered here. At v6.12, mm_core_init() in mm/mm_init.c calls only mem_init(), and each architecture’s mem_init() calls memblock_free_all() itself. At v6.18 and v7.3, mm_core_init() calls memblock_free_all() directly and then mem_init(), and arm64’s mem_init() at v7.3 no longer contains it. The sequence is unchanged; the owner of the call site is not.
Three page counts per zone: spanned, present and managed
The counter just incremented is global, but the same quantity is tracked per zone, and the definitions are written down in include/linux/mmzone.h:
/*
* spanned_pages is the total pages spanned by the zone, including
* holes, which is calculated as:
* spanned_pages = zone_end_pfn - zone_start_pfn;
*
* present_pages is physical pages existing within the zone, which
* is calculated as:
* present_pages = spanned_pages - absent_pages(pages in holes);
*
* managed_pages is present pages managed by the buddy system, which
* is calculated as (reserved_pages includes pages allocated by the
* bootmem allocator):
* managed_pages = present_pages - reserved_pages;
*
* cma pages is present pages that are assigned for CMA use
* (MIGRATE_CMA).
*
* [hotplug notes and locking rules omitted]
*/
atomic_long_t managed_pages;
unsigned long spanned_pages;
unsigned long present_pages;Address range, physical RAM, and the subset the buddy allocator was given. Only the third is an atomic_long_t, because only the third changes at runtime. This comment is identical at all three tags.
Reading the three counts off a running kernel
All three are exported per zone in /proc/zoneinfo, so the arithmetic can be checked rather than assumed:
raghu@techveda.org:~$ awk '/^Node/{z=$0} /spanned|present|managed/{print z" "$1" "$2}' /proc/zoneinfo
Node 0, zone DMA spanned 589824
Node 0, zone DMA present 589824
Node 0, zone DMA managed 561763
Node 0, zone DMA32 spanned 0
Node 0, zone DMA32 present 0
Node 0, zone DMA32 managed 0
Node 0, zone Normal spanned 458752
Node 0, zone Normal present 458752
Node 0, zone Normal managed 438343
Node 0, zone Movable spanned 0
Node 0, zone Movable present 0
Node 0, zone Movable managed 0
Node 0, zone Device spanned 0
Node 0, zone Device present 0
Node 0, zone Device managed 0
raghu@techveda.org:~$ awk '/present/{p+=$2} /managed/{m+=$2} END{print p, m, p-m, (p-m)*4}' /proc/zoneinfo
1048576 1000106 48470 193880Present pages sum to 1,048,576, which at 4 KiB is exactly 4,194,304 kB, so nothing physical is unaccounted for. Managed pages sum to 1,000,106, which is exactly 4,000,424 kB, which is MemTotal to the kilobyte. The gap is 48,470 pages, or 189.33 MiB.
The per-zone view adds something MemTotal cannot. The DMA zone here is missing 28,061 pages and the Normal zone 20,409. A reservation lands in a specific zone, so a carve-out placed low can leave a DMA-capable zone much smaller than its size suggests.
Where MemTotal is computed, and what it is not
Much of the confusion comes from assuming MemTotal is derived from the lines beneath it. In fs/proc/meminfo.c, meminfo_proc_show() calls si_meminfo(&i) and prints:
show_val_kb(m, "MemTotal: ", i.totalram);
show_val_kb(m, "MemFree: ", i.freeram);
show_val_kb(m, "MemAvailable: ", available);And si_meminfo() in mm/show_mem.c fills that field from one place:
void si_meminfo(struct sysinfo *val)
{
val->totalram = totalram_pages();
val->sharedram = global_node_page_state(NR_SHMEM);
val->freeram = global_zone_page_state(NR_FREE_PAGES);
val->bufferram = nr_blockdev_pages();
val->totalhigh = totalhigh_pages();
val->freehigh = nr_free_highpages();
val->mem_unit = PAGE_SIZE;
}totalram_pages() is an inline in include/linux/mm.h that reads one variable, atomic_long_t _totalram_pages, and totalram_pages_add() is the only way it moves. This chain is identical at v6.12, v6.18 and v7.3.
So MemTotal is a count of pages the buddy allocator owns, with two writers: the one-off addition in memblock_free_all(), and adjust_managed_page_count() afterwards. It is not the DRAM size, it is not a sum of the lines below it, and those lines are not required to add up to it.
The boot message that itemises the missing 189 MiB
The kernel prints the split once, from mem_init_print_info() in mm/mm_init.c:
pr_info("Memory: %luK/%luK available (%luK kernel code, %luK rwdata, %luK rodata, %luK init, %luK bss, %luK reserved, %luK cma-reserved"
#ifdef CONFIG_HIGHMEM
", %luK highmem"
#endif
")\n",
K(nr_free_pages()), K(physpages),
codesize / SZ_1K, datasize / SZ_1K, rosize / SZ_1K,
(init_data_size + init_code_size) / SZ_1K, bss_size / SZ_1K,
K(physpages - totalram_pages() - totalcma_pages),
K(totalcma_pages)
#ifdef CONFIG_HIGHMEM
, K(totalhigh_pages())
#endif
);Two things follow. physpages comes from get_num_physpages(), an inline in include/linux/mm.h that sums node_present_pages() over the online nodes, so the second figure in Memory: X/Y available is the present total, not the DRAM size and not MemTotal. And the reserved figure is not counted from a list: it is computed as physpages - totalram_pages() - totalcma_pages, the remainder. The code sizes beside it come from linker symbols such as _etext - _stext and are informational; nothing is subtracted using them.
The machine used for the captures does not permit an unprivileged read of the kernel log, so this line was not captured; the format and arithmetic are quoted from source at v7.3, where apart from one unrelated added call the function is unchanged from v6.12. On your own board it appears early in dmesg, and it is the only place the kernel itemises the reservation.
Why CMA is inside MemTotal at runtime but outside it in the boot message
This is where experienced engineers most often reason wrongly, and it follows from initcall ordering rather than from any special case in the arithmetic.
A CMA region is reserved through memblock_reserve() early, like any other carve-out, so memblock_free_all() skips it and it is absent from MemTotal at that moment. Later, cma_init_reserved_areas() in mm/cma.c releases each CMA pageblock into the buddy allocator as migratable memory:
void __init init_cma_reserved_pageblock(struct page *page)
{
unsigned i = pageblock_nr_pages;
struct page *p = page;
do {
__ClearPageReserved(p);
set_page_count(p, 0);
} while (++p, --i);
init_pageblock_migratetype(page, MIGRATE_CMA, false);
set_page_refcounted(page);
/* pages were reserved and not allocated */
clear_page_tag_ref(page);
__free_pages(page, pageblock_order);
adjust_managed_page_count(page, pageblock_nr_pages);
page_zone(page)->cma_pages += pageblock_nr_pages;
}The last two lines are the accounting. adjust_managed_page_count() raises both the zone’s managed_pages and the global _totalram_pages, and cma_pages records how much is CMA. After this runs, CMA memory is in MemTotal.
The ordering decides the rest. cma_init_reserved_areas() is a core_initcall() at all three tags, and core initcalls run inside do_basic_setup(). In kernel_init_freeable() in init/main.c, page_alloc_init_late() runs before do_basic_setup(), and it is page_alloc_init_late() that calls mem_init_print_info(). The boot message is printed before any CMA pageblock reaches the buddy allocator, which is why the format string carries a separate cma-reserved field and why totalcma_pages is subtracted alongside totalram_pages().
On this machine the two views differ by the size of the pool. At boot-message time the managed total excludes the 8,192 CMA pages, so the gap from present is 56,662 pages, or 221.34 MiB, reported as 189.33 MiB reserved plus 32,768 kB cma-reserved. At runtime those pages are managed and the gap is 48,470 pages, or 189.33 MiB. The figure the boot line calls reserved and the runtime gap are therefore the same number, not coincidentally equal.
What changed in adjust_managed_page_count between 6.12 and 6.13
Since adjust_managed_page_count() is the only runtime writer of these counters, it is worth reading at more than one tag. At v6.12 it is two statements:
void adjust_managed_page_count(struct page *page, long count)
{
atomic_long_add(count, &page_zone(page)->managed_pages);
totalram_pages_add(count);
}From v6.13 it is three, and the third was confirmed present at every tag from v6.13 to v6.18 and at v7.3:
void adjust_managed_page_count(struct page *page, long count)
{
atomic_long_add(count, &page_zone(page)->managed_pages);
totalram_pages_add(count);
setup_per_zone_lowmem_reserve();
}The lowmem_reserve array is derived from each zone’s managed page count, and it is what stops allocations that could have come from a higher zone from consuming a lower one. Before v6.13, changing managed_pages did not recompute it, so the array could reflect a zone size that no longer applied. This is not a boot-time concern, since zone setup computes the array anyway. It matters where managed_pages moves after boot: CMA initialisation, memory hotplug, and a virtio balloon inflating or deflating. If you are on a 6.12 longterm BSP and your product moves managed page counts at runtime, that kernel does not refresh the array when it happens. No kernel was built or booted for this part, so the claim here is limited to what the source shows about when the array is recomputed, not to a measured effect.
Common wrong turns when the numbers do not add up
Each is correct reasoning from an incomplete model, so the incomplete part is named with it.
Adding up the /proc/meminfo lines and expecting MemTotal. Attractive because the file looks like a breakdown with a total on top. MemTotal is _totalram_pages; the lines below are independent counters, several overlapping on purpose. The incomplete assumption is that a table printed together is a table that reconciles.
Treating MemTotal as the amount of RAM on the board. Attractive because on a desktop the difference looks like rounding. With a firmware carve-out and a CMA pool it is not. For the physical figure, sum present in /proc/zoneinfo; for what the buddy allocator can hand out, use MemTotal. A bill-of-materials decision needs the first.
Assuming CMA sits outside MemTotal. Attractive because CMA is reserved and reserved memory is excluded, so the inference is internally consistent. It is wrong because init_cma_reserved_pageblock() hands the pool to the buddy allocator later, so subtracting CmaTotal from MemTotal double-counts it. The incomplete part is the timeline: a reservation lasts until something gives it back, not forever.
Comparing MemTotal across two kernel versions and calling the difference a regression. Attractive because the comparison is cheap and the number moved. But MemTotal is the result of every early reservation that build made, so a different struct page size, a changed CMA default, an added reserved-memory node or a different page size moves it with nothing being wasted. Compare the per-zone gap and the boot line’s itemisation first.
Verification checklist
raghu@techveda.org:~$ awk '/present/{p+=$2} /managed/{m+=$2} END{print p, m, p-m}' /proc/zoneinfo— the second figure times the page size in kB must equalMemTotalexactly. If not, you have more than one node; repeat per node.- The first figure times the page size must equal the RAM the board has. If it is short, memory is missing before
memblocksaw it, which is a firmware or/memoryproblem rather than an accounting one. raghu@techveda.org:~$ dmesg | grep 'Memory:'— thereservedandcma-reservedfigures itemise the third figure above. Record it per build.raghu@techveda.org:~$ ls /proc/device-tree/reserved-memory/— on a Device Tree platform every child node here contributed to the gap. Confirm each is still required.
Key takeaways
MemTotalis one counter,_totalram_pages, holding the pages the buddy allocator owns. It is not the DRAM size and not a sum of the lines beneath it.- A page is excluded because
memblock_free_all()never visited it, not because anything marked it used.for_each_free_mem_range()skips reserved ranges and nothing adds them back. spanned,presentandmanagedin/proc/zoneinfoare address range, physical RAM and buddy allocator RAM, and they reconcile to the page againstMemTotal.- CMA is outside the boot message’s total and inside
MemTotalat runtime, becausecma_init_reserved_areas()is a core initcall running aftermem_init_print_info(). adjust_managed_page_count()gained asetup_per_zone_lowmem_reserve()call atv6.13; on 6.12 the reserve array is not refreshed when managed counts move after boot.
Reading these paths in the source rather than inferring them from output is the habit our Linux kernel infrastructure course is built around.
Related articles on this site
- DRAM Shortage: How Much RAM Does Your Embedded Linux Product Need? — sizing the part before you fit it, the decision this accounting feeds back into.
- From start_kernel() to Userspace: A Line-by-Line Walkthrough — the surrounding boot sequence, including where
mm_core_init()sits relative to the initcalls. - Proposed Memory Split Defaults for 32-bit Arm — what changes when the lowmem and highmem boundary moves, which this part leaves out.
- Kernel Memory Attribution Is a Product Decision, Not a Debugging Trick — attributing kernel allocations once the pages are inside the allocator.
Frequently asked questions
Why does MemTotal not equal the RAM fitted on the board?
Because MemTotal counts only the pages the buddy allocator was given. Every range memblock had already reserved, including the kernel image, the Device Tree reservations and early allocations, is skipped by memblock_free_all() and never counted.
Is the CMA pool included in MemTotal?
At runtime, yes. CMA pageblocks are released into the allocator by init_cma_reserved_pageblock(), which calls adjust_managed_page_count(). The boot-time Memory: line is printed before that happens, so it reports CMA separately as cma-reserved.
How do I find the physical RAM size from a running kernel?
Sum the present lines in /proc/zoneinfo and multiply by the page size. On the machine here that gives 1,048,576 pages, exactly 4 GiB, while MemTotal reports 4,000,424 kB.
What changed in the memory accounting code between 6.12 and 7.3?
Two things. memblock_free_all() moved from each architecture’s mem_init() into generic mm_core_init(), and adjust_managed_page_count() gained a setup_per_zone_lowmem_reserve() call at v6.13. How MemTotal itself is computed is unchanged.
Further reading
- Boot time memory management — the kernel’s documentation for
memblock, including the reserved array and the handover. - The /proc filesystem — the reference for the
/proc/meminfoand/proc/zoneinfofields. - reserved-memory Device Tree bindings — how a carve-out is declared so that
early_init_fdt_scan_reserved_mem()records it. - mm/memblock.c at v7.3 — the source for
memblock_free_all()andfree_low_memory_core_early(). - mm/mm_init.c at v7.3 — the source for
mem_init_print_info(),init_cma_reserved_pageblock()andmm_core_init().




