linuxlinux-kerneldmesg

Where does "Freeing unused kernel memory" come from?


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?


Solution

  • 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)