kexec: modify the meaning of the end parameter in kimage_is_destination_range()
authorYuntao Wang <ytcoode@gmail.com>
Sun, 17 Dec 2023 03:35:26 +0000 (11:35 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 29 Dec 2023 20:22:25 +0000 (12:22 -0800)
The end parameter received by kimage_is_destination_range() should be the
last valid byte address of the target memory segment plus 1.  However, in
the locate_mem_hole_bottom_up() and locate_mem_hole_top_down() functions,
the corresponding value passed to kimage_is_destination_range() is the
last valid byte address of the target memory segment, which is 1 less.

There are two ways to fix this bug.  We can either correct the logic of
the locate_mem_hole_bottom_up() and locate_mem_hole_top_down() functions,
or we can fix kimage_is_destination_range() by making the end parameter
represent the last valid byte address of the target memory segment.  Here,
we choose the second approach.

Due to the modification to kimage_is_destination_range(), we also need to
adjust its callers, such as kimage_alloc_normal_control_pages() and
kimage_alloc_page().

Link: https://lkml.kernel.org/r/20231217033528.303333-2-ytcoode@gmail.com
Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
Acked-by: Baoquan He <bhe@redhat.com>
Cc: Borislav Petkov (AMD) <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
kernel/kexec_core.c

index 6e0f022987ff3b818edc87edc55b0a6d08a25c5a..2f039a7d9af966eb641586894aa033773e46f1d5 100644 (file)
@@ -278,8 +278,8 @@ int kimage_is_destination_range(struct kimage *image,
                unsigned long mstart, mend;
 
                mstart = image->segment[i].mem;
-               mend = mstart + image->segment[i].memsz;
-               if ((end > mstart) && (start < mend))
+               mend = mstart + image->segment[i].memsz - 1;
+               if ((end >= mstart) && (start <= mend))
                        return 1;
        }
 
@@ -372,7 +372,7 @@ static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
                pfn   = page_to_boot_pfn(pages);
                epfn  = pfn + count;
                addr  = pfn << PAGE_SHIFT;
-               eaddr = epfn << PAGE_SHIFT;
+               eaddr = (epfn << PAGE_SHIFT) - 1;
                if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
                              kimage_is_destination_range(image, addr, eaddr)) {
                        list_add(&pages->lru, &extra_pages);
@@ -718,7 +718,7 @@ static struct page *kimage_alloc_page(struct kimage *image,
 
                /* If the page is not a destination page use it */
                if (!kimage_is_destination_range(image, addr,
-                                                 addr + PAGE_SIZE))
+                                                 addr + PAGE_SIZE - 1))
                        break;
 
                /*