kexec_file: fix incorrect temp_start value in locate_mem_hole_top_down()
authorYuntao Wang <ytcoode@gmail.com>
Sun, 17 Dec 2023 03:35:27 +0000 (11:35 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 29 Dec 2023 20:22:25 +0000 (12:22 -0800)
temp_end represents the address of the last available byte.  Therefore,
the starting address of the memory segment with temp_end as its last
available byte and a size of `kbuf->memsz`, that is, the value of
temp_start, should be `temp_end - kbuf->memsz + 1` instead of `temp_end -
kbuf->memsz`.

Additionally, use the ALIGN_DOWN macro instead of open-coding it directly
in locate_mem_hole_top_down() to improve code readability.

Link: https://lkml.kernel.org/r/20231217033528.303333-3-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_file.c

index aca5f3668f4cff178494c6e776f80a0c6d8015ae..bef2f6f2571b42203cab4f7ba497ce34c41c3794 100644 (file)
@@ -434,11 +434,11 @@ static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
        unsigned long temp_start, temp_end;
 
        temp_end = min(end, kbuf->buf_max);
-       temp_start = temp_end - kbuf->memsz;
+       temp_start = temp_end - kbuf->memsz + 1;
 
        do {
                /* align down start */
-               temp_start = temp_start & (~(kbuf->buf_align - 1));
+               temp_start = ALIGN_DOWN(temp_start, kbuf->buf_align);
 
                if (temp_start < start || temp_start < kbuf->buf_min)
                        return 0;