Skip to main content
Linux uses a virtual memory system. Therefore, the addresses that the user program accesses don’t correspond to the physical addresses that the hardware uses directly. Virtual memory introduces a layer of indirection, allowing programs to assign extra memory beyond the physically available memory. Memory management implementation covers the following areas:
  • Management of physical pages in the memory
  • Buddy system to assign memory in large chunks
  • Slab, slub, and slob allocators to assign smaller chunks of memory
  • vmalloc mechanism to assign noncontiguous blocks of memory
  • Address space of the processes

/proc file system

The /proc file system provides the following files:

/proc/meminfo

This file provides information about distribution and usage of memory.
  • To view the contents of this file, run the following command:
    Sample output:
For more information about the parameters available in this file, see /proc filesystem.

/proc/vmstat

This file shows detailed virtual memory statistics from the kernel. Most of the statistics are available only if you enable the CONFIG_VM_EVENT_COUNTERS option in the init/Kconfig file.
  • To view the contents of this file, run the following command:
    Sample output:
For more information about the parameters available in this file, see Linux manual page.

/proc/iomem

This file shows the memory map of the system for its various device drivers.
  • To view the contents of this file, run the following command:
    Sample output:

/proc/vmallocinfo

This file shows detailed information about virtual address allocation through vmalloc or ioremap.
  • To view the contents of this file, run the following command:
    Sample output:

memblock interface

The memblock interface on the debugfs file system provides details about the available and reserved memory regions in the system. This interface provides the following files:

/sys/kernel/debug/memblock/memory

This file provides information about all the available memory regions (HLOS and non-HLOS) visible to the Linux kernel. To determine the overall memory accessible to the kernel, calculate the difference between the start and end addresses of each of the region and sum these values to get the total occupied RAM. The remaining memory, which is the difference between the RAM size of the device and the occupied RAM, is the non-HLOS memory or the memory occupied by other subsystems.
  • To view the contents of this file, run the following command:
    Sample output:

/sys/kernel/debug/memblock/reserved

This file provides information about all the reserved memory regions in the system.
  • To view the contents of this file, run the following command:
    Sample output:

Debug memory leak issues

To debug kernel memory leak issues, enable the following configuration options:
  • CONFIG_DEBUG_KMEMLEAK=y
  • CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE=4000
  • CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF=y
By default, a kernel thread scans the memory every 10 minutes and prints the number of new unreferenced objects found. For example,
Disable the KMEMLEAK option at boot time by passing KMEMLEAK=off on the kernel command line. For more information about the kmemleak.txt file, see kernel documentation. The following extra kernel configuration options are available to track the allocator of each page of memory:
  • CONFIG_PAGE_OWNER
  • CONFIG_PAGE_OWNER_ENABLE_DEFAULT
  • CONFIG_PAGE_EXTENSION
Enabling these options can help identify several allocations, which may indicate a memory leak issue.

Identify memory corruption issues

Enable the following kernel configuration options to identify memory corruption issues:
  • CONFIG_PAGE_POISONING
  • CONFIG_SLUB_DEBUG_ON
  • CONFIG_DEBUG_LIST
  • CONFIG_SLUB_DEBUG
Sample log:
For more information about slub debugging, see the Documentation/vm/slub.txt file available in the kernel documentation.

Out of memory

When the system fails to assign a page, the kernel logs display a message such as the following:
These messages indicate that the system couldn’t assign the requested page. The top line of the log message provides several details about the out-of-memory issue. For example, in the following log:
  • order: Indicates the size of the page. In this example, 2 2 x PAGE_SIZE (4 K) = 16 K
    • Linux uses a buddy allocator that allocates pages in powers of 2.
      • The maximum size of the buddy allocator is the order of 10 = 2 10 x 4 kB = 4 MB.
      • For allocation > 4 MB, use an alternate allocation method such as the contiguous memory allocator (CMA).
    • For failure of higher-order allocations, examine whether the memory can be virtually contiguous, instead of being physically contiguous.
  • mode: Indicates the type of page requested
    • mode provides information about get free pages (GFP) flags. In this example, mode is 0x10d2.
    • mode is the result of OR operation on all the GFP flags in the allocation.
  • Pages available in the system A page allocation failure message prints details about the size of pages that were available in the system.

IOMMU page fault

Input-output memory management unit (IOMMU), also known as the system memory management unit (SMMU), performs memory management functions on behalf of subsystems that don’t have their own MMU. The IOMMU hardware block allows physically noncontiguous pages to support virtually contiguous memory. The memory translation logic in the IOMMU is the same as the logic in the CPU MMU. The IOMMU page fault is the most common IOMMU issue. The fault occurs when the requested page is mapped in the page table but isn’t found in the memory. The fault handler receives the context bank instance of the IOMMU and dumps the registers for this context. The following log indicates an IOMMU page fault.
The following table describes the fields captured in the log message. Table: Information in IOMMU page fault log
  • Translation fault (TF)
  • Access permission fault (APF)
  • Stalled status (SS)
The FSR is one of the most important registers in IOMMU debugging. This register has read/write-clear access. The read operation on this register reads the value in the register while the write operation clears the bits corresponding to 1s in the written data and leaves the bits corresponding to 0s unchanged. This process prevents inadvertent clearing of new faults when writing the register to clear an old fault. Some useful bits in this register are: Table: Bits in fault status register The TF, APF, and SL flags indicate normal operation, whereas the TLBMF, HTWDEEF, HTWSEEF, and MHF flags indicate that there is an issue.

IOMMU page table

The IOMMU page table dump provides a faulting address from the FAR and the register dump in the kernel log. This faulting address represents the virtual address, and you can acquire the corresponding physical address from the page table. From the page table dump, you can identify whether the requested address is mapped or not mapped. Each IOMMU domain has a page table, and the dump includes page tables for each of the domains. Currently, there are six domains. The following is the sample dump of the Domain: 2 page table.
In this example, the first column represents the virtual address, the second column represents the number of bytes in the corresponding region of contiguous physical addresses, and the third column represents the physical addresses. The permissions are also mentioned for each of these regions.

Memory map

For more information about the memory map, see the latest Release Notes.

Stack corruption

Wrong coding logic accesses memory locations in the stack, leading to changes in values at those memory locations, causing stack corruption. Stack corruption can occur in the following ways:
  • Due to bad code logic, the program consumes all the stack memory and writes memory beyond the stack boundaries, resulting in a stack overflow.
  • Accessing an array that’s out of bounds.
  • An undefined or freed pointer that points at a stack address.
  • Corrupted return address of a caller function.
To identify the stack corruption issues, enable the following kernel configuration options:
  • CONFIG_STACKPROTECTOR
  • CONFIG_STACKPROTECTOR_STRONG
The Kernel address sanitizer (KASAN) utility also helps in identifying some stack corruption issues.