Re: [Patch v2] mm/sparsemem: get address to page struct instead of address to pfn

From: David Hildenbrand
Date: Mon Feb 10 2020 - 04:02:14 EST


On 10.02.20 01:50, Wei Yang wrote:
> memmap should be the address to page struct instead of address to pfn.
>

"mm/sparsemem: fix wrong address in ms->section_mem_map with sub-sections

We want to store the address of the memmap, not the address of the first
pfn.

E.g., we can have both (boot) system memory and devmem residing in a
single section. Once we hot-add the devmem part, the address stored in
ms->section_mem_map would be wrong, and kdump would not be able to
dump the right memory.
"

? See below

> As mentioned by David, if system memory and devmem sit within a
> section, the mismatch address would lead kdump to dump unexpected
> memory.
>
> Since sub-section only works for SPARSEMEM_VMEMMAP, pfn_to_page() is
> valid to get the page struct address at this point.
>
> Fixes: ba72b4c8cf60 ("mm/sparsemem: support sub-section hotplug")
> Signed-off-by: Wei Yang <richardw.yang@xxxxxxxxxxxxxxx>
> CC: Dan Williams <dan.j.williams@xxxxxxxxx>
> CC: David Hildenbrand <david@xxxxxxxxxx>
> CC: Baoquan He <bhe@xxxxxxxxxx>
>
> ---
> v2:
> * adjust comment to mention the mismatch data would affect kdump
>
> ---
> mm/sparse.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/sparse.c b/mm/sparse.c
> index 586d85662978..4862ec2cfbc0 100644
> --- a/mm/sparse.c
> +++ b/mm/sparse.c
> @@ -887,7 +887,7 @@ int __meminit sparse_add_section(int nid, unsigned long start_pfn,
>
> /* Align memmap to section boundary in the subsection case */
> if (section_nr_to_pfn(section_nr) != start_pfn)
> - memmap = pfn_to_kaddr(section_nr_to_pfn(section_nr));
> + memmap = pfn_to_page(section_nr_to_pfn(section_nr));

I think this whole code should be reworked.

Callee returns a pointer. Caller: Nah, I know it better.

Just nasty.


Can we do something like this instead:


diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index 200aef686722..c5091feef29e 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -266,5 +266,5 @@ struct page * __meminit
__populate_section_memmap(unsigned long pfn,
if (vmemmap_populate(start, end, nid, altmap))
return NULL;

- return pfn_to_page(pfn);
+ return pfn_to_page(SECTION_ALIGN_DOWN(pfn));
}
diff --git a/mm/sparse.c b/mm/sparse.c
index c184b69460b7..21902d7931e4 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -788,6 +788,10 @@ static void section_deactivate(unsigned long pfn,
unsigned long nr_pages,
depopulate_section_memmap(pfn, nr_pages, altmap);
}

+/*
+ * Returns the memmap of the first pfn of the section (not of
+ * sub-sections).
+ */
static struct page * __meminit section_activate(int nid, unsigned long pfn,
unsigned long nr_pages, struct vmem_altmap *altmap)
{
@@ -882,9 +886,6 @@ int __meminit sparse_add_section(int nid, unsigned
long start_pfn,
set_section_nid(section_nr, nid);
section_mark_present(ms);

- /* Align memmap to section boundary in the subsection case */
- if (section_nr_to_pfn(section_nr) != start_pfn)
- memmap = pfn_to_kaddr(section_nr_to_pfn(section_nr));
sparse_init_one_section(ms, section_nr, memmap, ms->usage, 0);

return 0;


Untested, of course :)

--
Thanks,

David / dhildenb