kexec_md_pages() means to skip the part of each target below 4GB, which
kexec_load_md() always identity maps, with
start = max(start, cur_addr + 1);
max() takes u_int, so cur_addr + 1 (1 << 32) becomes 0 and start is cut
to its low 32 bits. For a target above 4GB, end - start then grows by
the target's address rounded down to 4GB, and that span is added to the
source total, which gets one page per 2MB. Staging a single page just
above 4GB wires 4135 pages, and by the same formula a page at 1TB would
wire over a million. Using ulmax() alone is not enough: a target below
4GB would then make end - start wrap.
Count the pages per level instead, from what kexec_load_md() does:
the fixed pages for the bottom 4GB and the trampoline copy, L3 and L2
pages for targets above 4GB, and L3, L2 and L1 pages for the staging
area, which contains the counted pages too, and for the trampoline text
page.
kexec_generate_page_tables() also takes pages from the iterator without
checking for NULL, so a short count turns into a NULL dereference.
Return ENOMEM from kexec_load_md() instead.
Add a test that stages one page above 4GB and checks how many pages
were wired.
Sponsored by: Advanced Micro Devices, Inc.
Signed-off-by: Osvaldo Janeri Filho <ojanerif@amd.com>