Skip to contentSkip to conversation

mm, sparse: do not waste pre allocated memmap space

PATCH In review Michal Hocko 3 participants 4 messages Nov 19, 2019 – Nov 20, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm, sparse: do not waste pre allocated memmap space

From: Michal Hocko <[email protected]>

Vincent has noticed [1] that there is something unusual with the memmap allocations going on on his platform
: I noticed this because on my ARM64 platform, with 1 GiB of memory the : first [and only] section is allocated from the zeroing path while with : 2 GiB of memory the first 1 GiB section is allocated from the : non-zeroing path.

The underlying problem is that although sparse_buffer_init allocates enough memory for all sections on the node sparse_buffer_alloc is not able to consume them due to mismatch in the expected allocation alignement. While sparse_buffer_init preallocation uses the PAGE_SIZE alignment the real memmap has to be aligned to section_map_size() this results in a wasted initial chunk of the preallocated memmap and unnecessary fallback allocation for a section.

While we are at it also change __populate_section_memmap to align to the requested size because at least VMEMMAP has constrains to have memmap properly aligned.

[1] http://lkml.kernel.org/r/[email protected]

Fixes 35fd1eb1e821 ("mm/sparse: abstract sparse buffer allocations")
---
 mm/sparse.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
mm/sparse.c +7 -3
index f6891c1992b1..079f3e3c4cab 100644
@@ -458,8 +458,7 @@ struct page __init *__populate_section_memmap(unsigned long pfn,
 	if (map)
 		return map;
 
-	map = memblock_alloc_try_nid(size,
-					  PAGE_SIZE, addr,
+	map = memblock_alloc_try_nid(size, size, addr,
 					  MEMBLOCK_ALLOC_ACCESSIBLE, nid);
 	if (!map)
 		panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%pa\n",
@@ -482,8 +481,13 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
 {
 	phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
 	WARN_ON(sparsemap_buf);	/* forgot to call sparse_buffer_fini()? */
+	/*
+	 * Pre-allocated buffer is mainly used by __populate_section_memmap
+	 * and we want it to be properly aligned to the section size - this is
+	 * especially the case for VMEMMAP which maps memmap to PMDs
+	 */
 	sparsemap_buf =
-		memblock_alloc_try_nid_raw(size, PAGE_SIZE,
+		memblock_alloc_try_nid_raw(size, section_map_size(),
 						addr,
 						MEMBLOCK_ALLOC_ACCESSIBLE, nid);
 	sparsemap_buf_end = sparsemap_buf + size;
Signature · 1 line
2.20.1
Acked-by on v1 1/1
#1 lore raw
Quoted 47 lines · Michal Hocko

From: Michal Hocko <[email protected]>

Vincent has noticed [1] that there is something unusual with the memmap allocations going on on his platform
: I noticed this because on my ARM64 platform, with 1 GiB of memory the : first [and only] section is allocated from the zeroing path while with : 2 GiB of memory the first 1 GiB section is allocated from the : non-zeroing path.

The underlying problem is that although sparse_buffer_init allocates enough memory for all sections on the node sparse_buffer_alloc is not able to consume them due to mismatch in the expected allocation alignement. While sparse_buffer_init preallocation uses the PAGE_SIZE alignment the real memmap has to be aligned to section_map_size() this results in a wasted initial chunk of the preallocated memmap and unnecessary fallback allocation for a section.

While we are at it also change __populate_section_memmap to align to the requested size because at least VMEMMAP has constrains to have memmap properly aligned.

[1] http://lkml.kernel.org/r/[email protected]

Fixes 35fd1eb1e821 ("mm/sparse: abstract sparse buffer allocations")
---
  mm/sparse.c | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
mm/sparse.c +1 -2
index f6891c1992b1..079f3e3c4cab 100644
@@ -458,8 +458,7 @@ struct page __init *__populate_section_memmap(unsigned long pfn,
  	if (map)
  		return map;
 
-	map = memblock_alloc_try_nid(size,
-					  PAGE_SIZE, addr,
+	map = memblock_alloc_try_nid(size, size, addr,
  					  MEMBLOCK_ALLOC_ACCESSIBLE, nid);
  	if (!map)
  		panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%pa\n",
@@ -482,8 +481,13 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
  {
  	phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
  	WARN_ON(sparsemap_buf);	/* forgot to call sparse_buffer_fini()? */

mm/sparse.c static void __init sparse_buffer_init(unsigned long size, int nid) · line 492

Quoted 10 lines
mm/sparse.c +6 -1
+	/*
+	 * Pre-allocated buffer is mainly used by __populate_section_memmap
+	 * and we want it to be properly aligned to the section size - this is
+	 * especially the case for VMEMMAP which maps memmap to PMDs
+	 */
  	sparsemap_buf =
-		memblock_alloc_try_nid_raw(size, PAGE_SIZE,
+		memblock_alloc_try_nid_raw(size, section_map_size(),
  						addr,
  						MEMBLOCK_ALLOC_ACCESSIBLE, nid);

Wow, that alignment/layout gives me nightmares ^

None of your business, though :)

mm/sparse.c static void __init sparse_buffer_init(unsigned long size, int nid) · line 493

Quoted 2 lines
mm/sparse.c +0 -0
  	sparsemap_buf_end = sparsemap_buf + size;
Signature · 3 lines
Thanks,

David / dhildenb
maintainer
#2 lore raw
Quoted 17 lines · On Tue, 19 Nov 2019 11:03:58 +0100 David Hildenbrand <[email protected]> wrote:
Quoted 4 lines
(quoted diff) +0 -0
@@ -482,8 +481,13 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
  {
  	phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
  	WARN_ON(sparsemap_buf);	/* forgot to call sparse_buffer_fini()? */

Review static void __init sparse_buffer_init(unsigned long size, int nid) · line 492

Quoted 10 lines
(quoted diff) +6 -1
+	/*
+	 * Pre-allocated buffer is mainly used by __populate_section_memmap
+	 * and we want it to be properly aligned to the section size - this is
+	 * especially the case for VMEMMAP which maps memmap to PMDs
+	 */
  	sparsemap_buf =
-		memblock_alloc_try_nid_raw(size, PAGE_SIZE,
+		memblock_alloc_try_nid_raw(size, section_map_size(),
  						addr,
  						MEMBLOCK_ALLOC_ACCESSIBLE, nid);

Wow, that alignment/layout gives me nightmares ^

Quoted 1 line

None of your business, though :)

We're allowed to change it ;)

mm/sparse.c renamed from mm/sparse.c~mm-sparse-do-not-waste-pre-allocated-memmap-space-fix +2 -4
@@ -486,10 +486,8 @@ static void __init sparse_buffer_init(un
 	 * and we want it to be properly aligned to the section size - this is
 	 * especially the case for VMEMMAP which maps memmap to PMDs
 	 */
-	sparsemap_buf =
-		memblock_alloc_try_nid_raw(size, section_map_size(),
-						addr,
-						MEMBLOCK_ALLOC_ACCESSIBLE, nid);
+	sparsemap_buf = memblock_alloc_try_nid_raw(size, section_map_size(),
+					addr, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
 	sparsemap_buf_end = sparsemap_buf + size;
 }
 

_

#3 lore raw
Quoted 27 lines · Andrew Morton
Quoted 17 lines · On Tue, 19 Nov 2019 11:03:58 +0100 David Hildenbrand <[email protected]> wrote:
Quoted 4 lines
(quoted diff) +0 -0
@@ -482,8 +481,13 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
  {
  	phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
  	WARN_ON(sparsemap_buf);	/* forgot to call sparse_buffer_fini()? */

Review static void __init sparse_buffer_init(unsigned long size, int nid) · line 492

Quoted 10 lines
(quoted diff) +6 -1
+	/*
+	 * Pre-allocated buffer is mainly used by __populate_section_memmap
+	 * and we want it to be properly aligned to the section size - this is
+	 * especially the case for VMEMMAP which maps memmap to PMDs
+	 */
  	sparsemap_buf =
-		memblock_alloc_try_nid_raw(size, PAGE_SIZE,
+		memblock_alloc_try_nid_raw(size, section_map_size(),
  						addr,
  						MEMBLOCK_ALLOC_ACCESSIBLE, nid);

Wow, that alignment/layout gives me nightmares ^

Quoted 1 line

None of your business, though :)

We're allowed to change it ;)

mm/sparse.c renamed from mm/sparse.c~mm-sparse-do-not-waste-pre-allocated-memmap-space-fix +0 -0
@@ -486,10 +486,8 @@ static void __init sparse_buffer_init(un
 	 * and we want it to be properly aligned to the section size - this is

mm/sparse.c static void __init sparse_buffer_init(un · line 492

Quoted 10 lines
mm/sparse.c renamed from mm/sparse.c~mm-sparse-do-not-waste-pre-allocated-memmap-space-fix +2 -4
 	 * especially the case for VMEMMAP which maps memmap to PMDs
 	 */
-	sparsemap_buf =
-		memblock_alloc_try_nid_raw(size, section_map_size(),
-						addr,
-						MEMBLOCK_ALLOC_ACCESSIBLE, nid);
+	sparsemap_buf = memblock_alloc_try_nid_raw(size, section_map_size(),
+					addr, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
 	sparsemap_buf_end = sparsemap_buf + size;
 }

I didn't bother mostly because the creative code layout made the intention of the patch more obvious. But if it saves from nightmares then why not.

Signature · 2 lines
Michal Hocko
SUSE Labs