The elfcorehdr segment is located by scanning the segments for
the ELF magic. Factor that scan out into a new helper function,
crash_find_elfcorehdr(), to clean up crash_handle_hotplug_event()
and prepare for its reuse in crash hotplug code.

Cc: Andrew Morton <[email protected]>
Cc: Baoquan He <[email protected]>
Cc: Mike Rapoport <[email protected]>
Cc: Pasha Tatashin <[email protected]>
Cc: Pratyush Yadav <[email protected]>
Cc: Dave Young <[email protected]>
Cc: Breno Leitao <[email protected]>
Signed-off-by: Jinjie Ruan <[email protected]>
---
 kernel/crash_core.c | 39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/kernel/crash_core.c b/kernel/crash_core.c
index 406b68d2adfd..4a990b17b66c 100644
--- a/kernel/crash_core.c
+++ b/kernel/crash_core.c
@@ -647,6 +647,28 @@ int crash_check_hotplug_support(void)
        return rc;
 }
 
+static void crash_find_elfcorehdr(struct kimage *image)
+{
+       unsigned char *ptr;
+       unsigned long mem;
+       unsigned int n;
+
+       if (image->elfcorehdr_index >= 0)
+               return;
+
+       for (n = 0; n < image->nr_segments; n++) {
+               mem = image->segment[n].mem;
+               ptr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT));
+               if (!ptr)
+                       continue;
+
+               /* The segment containing elfcorehdr */
+               if (memcmp(ptr, ELFMAG, SELFMAG) == 0)
+                       image->elfcorehdr_index = (int)n;
+               kunmap_local(ptr);
+       }
+}
+
 /*
  * To accurately reflect hot un/plug changes of CPU and Memory resources
  * (including onling and offlining of those resources), the relevant
@@ -702,22 +724,7 @@ static void crash_handle_hotplug_event(unsigned int 
hp_action, unsigned int cpu,
         * is allocated. Find the segment containing the elfcorehdr,
         * if not already found.
         */
-       if (image->elfcorehdr_index < 0) {
-               unsigned long mem;
-               unsigned char *ptr;
-               unsigned int n;
-
-               for (n = 0; n < image->nr_segments; n++) {
-                       mem = image->segment[n].mem;
-                       ptr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT));
-                       if (ptr) {
-                               /* The segment containing elfcorehdr */
-                               if (memcmp(ptr, ELFMAG, SELFMAG) == 0)
-                                       image->elfcorehdr_index = (int)n;
-                               kunmap_local(ptr);
-                       }
-               }
-       }
+       crash_find_elfcorehdr(image);
 
        if (image->elfcorehdr_index < 0) {
                pr_err("unable to locate elfcorehdr segment");
-- 
2.34.1


Reply via email to