2

I often see Freeing unused kernel memory: xxxK (......) from dmesg, but I can never find this log from kernel source code with the help of grep/rg.

Where does it come from?

Chen Li
  • 4,824
  • 3
  • 28
  • 55

2 Answers2

3

That line of text does not exist as a single, complete string, hence your failure to grep it.
This all gets rolling when free_initmem() in init/main.c calls free_initmem_default().

The line in question originates from free_initmem_default() in include/linux/mm.h:

/*
 * Default method to free all the __init memory into the buddy system.
 * The freed pages will be poisoned with pattern "poison" if it's within
 * range [0, UCHAR_MAX].
 * Return pages freed into the buddy system.
 */
static inline unsigned long free_initmem_default(int poison)
{
    extern char __init_begin[], __init_end[];

    return free_reserved_area(&__init_begin, &__init_end,
                  poison, "unused kernel");
}

The rest of that text is from free_reserved_area() in mm/page_alloc.c:

unsigned long free_reserved_area(void *start, void *end, int poison, const char *s)
{
    void *pos;
    unsigned long pages = 0;

    ...

    if (pages && s)
        pr_info("Freeing %s memory: %ldK\n",
            s, pages << (PAGE_SHIFT - 10));

    return pages;
}

(Code excerpts from v5.2)

sawdust
  • 16,103
  • 3
  • 40
  • 50
1

From my answer here:

Some functions in the kernel source code are marked with __init because they run only once during initialization. This instructs the compiler to mark a function in a special way. The linker collects all such functions and puts them at the end of the final binary file.

Example method signature:

static int __init clk_disable_unused(void) 
{
   // some code
}

When the kernel starts, this code runs only once during initialization. After it runs, the kernel can free this memory to reuse it and you will see the kernel message:

Freeing unused kernel memory: 108k freed

sashoalm
  • 75,001
  • 122
  • 434
  • 781