• Baoquan He's avatar
    mm/sparse: optimize memmap allocation during sparse_init() · c98aff64
    Baoquan He authored
    In sparse_init(), two temporary pointer arrays, usemap_map and map_map
    are allocated with the size of NR_MEM_SECTIONS.  They are used to store
    each memory section's usemap and mem map if marked as present.  With the
    help of these two arrays, continuous memory chunk is allocated for
    usemap and memmap for memory sections on one node.  This avoids too many
    memory fragmentations.  Like below diagram, '1' indicates the present
    memory section, '0' means absent one.  The number 'n' could be much
    smaller than NR_MEM_SECTIONS on most of systems.
    
      |1|1|1|1|0|0|0|0|1|1|0|0|...|1|0||1|0|...|1||0|1|...|0|
      -------------------------------------------------------
       0 1 2 3         4 5         i   i+1     n-1   n
    
    If we fail to populate the page tables to map one section's memmap, its
    ->section_mem_map will be cleared finally to indicate that it's not
    present.  After use, these two arrays will be released at the end of
    sparse_init().
    
    In 4-level paging mode, each array costs 4M which can be ignorable.
    While in 5-level paging, they costs 256M each, 512M altogether.  Kdump
    kernel Usually only reserves very few memory, e.g 256M.  So, even thouth
    they are temporarily allocated, still not acceptable.
    
    In fact, there's no need to allocate them with the size of
    NR_MEM_SECTIONS.  Since the ->section_mem_map clearing has been deferred
    to the last, the number of present memory sections are kept the same
    during sparse_init() until we finally clear out the memory section's
    ->section_mem_map if its usemap or memmap is not correctly handled.
    Thus in the middle whenever for_each_present_section_nr() loop is taken,
    the i-th present memory section is always the same one.
    
    Here only allocate usemap_map and map_map with the size of
    'nr_present_sections'.  For the i-th present memory section, install its
    usemap and memmap to usemap_map[i] and mam_map[i] during allocation.
    Then in the last for_each_present_section_nr() loop which clears the
    failed memory section's ->section_mem_map, fetch usemap and memmap from
    usemap_map[] and map_map[] array and set them into mem_section[]
    accordingly.
    
    [akpm@linux-foundation.org: coding-style fixes]
    Link: http://lkml.kernel.org/r/20180628062857.29658-5-bhe@redhat.comSigned-off-by: default avatarBaoquan He <bhe@redhat.com>
    Reviewed-by: default avatarPavel Tatashin <pasha.tatashin@oracle.com>
    Cc: Pasha Tatashin <Pavel.Tatashin@microsoft.com>
    Cc: Dave Hansen <dave.hansen@intel.com>
    Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
    Cc: Oscar Salvador <osalvador@techadventures.net>
    Cc: Pankaj Gupta <pagupta@redhat.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    c98aff64
sparse.c 24 KB