Linux 6.9-rc1
[linux-2.6-microblaze.git] / kernel / kexec_core.c
index acd029b..0e96f6b 100644 (file)
@@ -6,6 +6,7 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <linux/btf.h>
 #include <linux/capability.h>
 #include <linux/mm.h>
 #include <linux/file.h>
 #include <crypto/hash.h>
 #include "kexec_internal.h"
 
-DEFINE_MUTEX(kexec_mutex);
-
-/* Per cpu memory for storing cpu states in case of system crash. */
-note_buf_t __percpu *crash_notes;
+atomic_t __kexec_lock = ATOMIC_INIT(0);
 
 /* Flag to indicate we are going to kexec a new kernel */
 bool kexec_in_progress = false;
 
-
-/* Location of the reserved area for the crash kernel */
-struct resource crashk_res = {
-       .name  = "Crash kernel",
-       .start = 0,
-       .end   = 0,
-       .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
-       .desc  = IORES_DESC_CRASH_KERNEL
-};
-struct resource crashk_low_res = {
-       .name  = "Crash kernel",
-       .start = 0,
-       .end   = 0,
-       .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
-       .desc  = IORES_DESC_CRASH_KERNEL
-};
-
-int kexec_should_crash(struct task_struct *p)
-{
-       /*
-        * If crash_kexec_post_notifiers is enabled, don't run
-        * crash_kexec() here yet, which must be run after panic
-        * notifiers in panic().
-        */
-       if (crash_kexec_post_notifiers)
-               return 0;
-       /*
-        * There are 4 panic() calls in make_task_dead() path, each of which
-        * corresponds to each of these 4 conditions.
-        */
-       if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
-               return 1;
-       return 0;
-}
-
-int kexec_crash_loaded(void)
-{
-       return !!kexec_crash_image;
-}
-EXPORT_SYMBOL_GPL(kexec_crash_loaded);
+bool kexec_file_dbg_print;
 
 /*
  * When kexec transitions to the new kernel there is a one-to-one
@@ -226,6 +185,7 @@ int sanity_check_segment_list(struct kimage *image)
        if (total_pages > nr_pages / 2)
                return -EINVAL;
 
+#ifdef CONFIG_CRASH_DUMP
        /*
         * Verify we have good destination addresses.  Normally
         * the caller is responsible for making certain we don't
@@ -248,6 +208,7 @@ int sanity_check_segment_list(struct kimage *image)
                                return -EADDRNOTAVAIL;
                }
        }
+#endif
 
        return 0;
 }
@@ -276,6 +237,12 @@ struct kimage *do_kimage_alloc_init(void)
        /* Initialize the list of unusable pages */
        INIT_LIST_HEAD(&image->unusable_pages);
 
+#ifdef CONFIG_CRASH_HOTPLUG
+       image->hp_action = KEXEC_CRASH_HP_NONE;
+       image->elfcorehdr_index = -1;
+       image->elfcorehdr_updated = false;
+#endif
+
        return image;
 }
 
@@ -289,8 +256,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;
        }
 
@@ -383,7 +350,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);
@@ -414,6 +381,7 @@ static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
        return pages;
 }
 
+#ifdef CONFIG_CRASH_DUMP
 static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
                                                      unsigned int order)
 {
@@ -443,7 +411,7 @@ static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
 
        pages = NULL;
        size = (1 << order) << PAGE_SHIFT;
-       hole_start = (image->control_page + (size - 1)) & ~(size - 1);
+       hole_start = ALIGN(image->control_page, size);
        hole_end   = hole_start + size - 1;
        while (hole_end <= crashk_res.end) {
                unsigned long i;
@@ -460,7 +428,7 @@ static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
                        mend   = mstart + image->segment[i].memsz - 1;
                        if ((hole_end >= mstart) && (hole_start <= mend)) {
                                /* Advance the hole to the end of the segment */
-                               hole_start = (mend + (size - 1)) & ~(size - 1);
+                               hole_start = ALIGN(mend, size);
                                hole_end   = hole_start + size - 1;
                                break;
                        }
@@ -468,7 +436,7 @@ static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
                /* If I don't overlap any segments I have found my hole! */
                if (i == image->nr_segments) {
                        pages = pfn_to_page(hole_start >> PAGE_SHIFT);
-                       image->control_page = hole_end;
+                       image->control_page = hole_end + 1;
                        break;
                }
        }
@@ -479,6 +447,7 @@ static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
 
        return pages;
 }
+#endif
 
 
 struct page *kimage_alloc_control_pages(struct kimage *image,
@@ -490,48 +459,16 @@ struct page *kimage_alloc_control_pages(struct kimage *image,
        case KEXEC_TYPE_DEFAULT:
                pages = kimage_alloc_normal_control_pages(image, order);
                break;
+#ifdef CONFIG_CRASH_DUMP
        case KEXEC_TYPE_CRASH:
                pages = kimage_alloc_crash_control_pages(image, order);
                break;
+#endif
        }
 
        return pages;
 }
 
-int kimage_crash_copy_vmcoreinfo(struct kimage *image)
-{
-       struct page *vmcoreinfo_page;
-       void *safecopy;
-
-       if (image->type != KEXEC_TYPE_CRASH)
-               return 0;
-
-       /*
-        * For kdump, allocate one vmcoreinfo safe copy from the
-        * crash memory. as we have arch_kexec_protect_crashkres()
-        * after kexec syscall, we naturally protect it from write
-        * (even read) access under kernel direct mapping. But on
-        * the other hand, we still need to operate it when crash
-        * happens to generate vmcoreinfo note, hereby we rely on
-        * vmap for this purpose.
-        */
-       vmcoreinfo_page = kimage_alloc_control_pages(image, 0);
-       if (!vmcoreinfo_page) {
-               pr_warn("Could not allocate vmcoreinfo buffer\n");
-               return -ENOMEM;
-       }
-       safecopy = vmap(&vmcoreinfo_page, 1, VM_MAP, PAGE_KERNEL);
-       if (!safecopy) {
-               pr_warn("Could not vmap vmcoreinfo buffer\n");
-               return -ENOMEM;
-       }
-
-       image->vmcoreinfo_data_copy = safecopy;
-       crash_update_vmcoreinfo_safecopy(safecopy);
-
-       return 0;
-}
-
 static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
 {
        if (*image->entry != 0)
@@ -561,23 +498,17 @@ static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
 static int kimage_set_destination(struct kimage *image,
                                   unsigned long destination)
 {
-       int result;
-
        destination &= PAGE_MASK;
-       result = kimage_add_entry(image, destination | IND_DESTINATION);
 
-       return result;
+       return kimage_add_entry(image, destination | IND_DESTINATION);
 }
 
 
 static int kimage_add_page(struct kimage *image, unsigned long page)
 {
-       int result;
-
        page &= PAGE_MASK;
-       result = kimage_add_entry(image, page | IND_SOURCE);
 
-       return result;
+       return kimage_add_entry(image, page | IND_SOURCE);
 }
 
 
@@ -620,10 +551,12 @@ void kimage_free(struct kimage *image)
        if (!image)
                return;
 
+#ifdef CONFIG_CRASH_DUMP
        if (image->vmcoreinfo_data_copy) {
                crash_update_vmcoreinfo_safecopy(NULL);
                vunmap(image->vmcoreinfo_data_copy);
        }
+#endif
 
        kimage_free_extra_pages(image);
        for_each_kimage_entry(image, ptr, entry) {
@@ -735,7 +668,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;
 
                /*
@@ -809,7 +742,7 @@ static int kimage_load_normal_segment(struct kimage *image,
                if (result < 0)
                        goto out;
 
-               ptr = kmap(page);
+               ptr = kmap_local_page(page);
                /* Start with a clear page */
                clear_page(ptr);
                ptr += maddr & ~PAGE_MASK;
@@ -817,22 +750,24 @@ static int kimage_load_normal_segment(struct kimage *image,
                                PAGE_SIZE - (maddr & ~PAGE_MASK));
                uchunk = min(ubytes, mchunk);
 
-               /* For file based kexec, source pages are in kernel memory */
-               if (image->file_mode)
-                       memcpy(ptr, kbuf, uchunk);
-               else
-                       result = copy_from_user(ptr, buf, uchunk);
-               kunmap(page);
+               if (uchunk) {
+                       /* For file based kexec, source pages are in kernel memory */
+                       if (image->file_mode)
+                               memcpy(ptr, kbuf, uchunk);
+                       else
+                               result = copy_from_user(ptr, buf, uchunk);
+                       ubytes -= uchunk;
+                       if (image->file_mode)
+                               kbuf += uchunk;
+                       else
+                               buf += uchunk;
+               }
+               kunmap_local(ptr);
                if (result) {
                        result = -EFAULT;
                        goto out;
                }
-               ubytes -= uchunk;
                maddr  += mchunk;
-               if (image->file_mode)
-                       kbuf += mchunk;
-               else
-                       buf += mchunk;
                mbytes -= mchunk;
 
                cond_resched();
@@ -841,6 +776,7 @@ out:
        return result;
 }
 
+#ifdef CONFIG_CRASH_DUMP
 static int kimage_load_crash_segment(struct kimage *image,
                                        struct kexec_segment *segment)
 {
@@ -873,7 +809,7 @@ static int kimage_load_crash_segment(struct kimage *image,
                        goto out;
                }
                arch_kexec_post_alloc_pages(page_address(page), 1, 0);
-               ptr = kmap(page);
+               ptr = kmap_local_page(page);
                ptr += maddr & ~PAGE_MASK;
                mchunk = min_t(size_t, mbytes,
                                PAGE_SIZE - (maddr & ~PAGE_MASK));
@@ -883,24 +819,26 @@ static int kimage_load_crash_segment(struct kimage *image,
                        memset(ptr + uchunk, 0, mchunk - uchunk);
                }
 
-               /* For file based kexec, source pages are in kernel memory */
-               if (image->file_mode)
-                       memcpy(ptr, kbuf, uchunk);
-               else
-                       result = copy_from_user(ptr, buf, uchunk);
+               if (uchunk) {
+                       /* For file based kexec, source pages are in kernel memory */
+                       if (image->file_mode)
+                               memcpy(ptr, kbuf, uchunk);
+                       else
+                               result = copy_from_user(ptr, buf, uchunk);
+                       ubytes -= uchunk;
+                       if (image->file_mode)
+                               kbuf += uchunk;
+                       else
+                               buf += uchunk;
+               }
                kexec_flush_icache_page(page);
-               kunmap(page);
+               kunmap_local(ptr);
                arch_kexec_pre_free_pages(page_address(page), 1);
                if (result) {
                        result = -EFAULT;
                        goto out;
                }
-               ubytes -= uchunk;
                maddr  += mchunk;
-               if (image->file_mode)
-                       kbuf += mchunk;
-               else
-                       buf += mchunk;
                mbytes -= mchunk;
 
                cond_resched();
@@ -908,6 +846,7 @@ static int kimage_load_crash_segment(struct kimage *image,
 out:
        return result;
 }
+#endif
 
 int kimage_load_segment(struct kimage *image,
                                struct kexec_segment *segment)
@@ -918,18 +857,74 @@ int kimage_load_segment(struct kimage *image,
        case KEXEC_TYPE_DEFAULT:
                result = kimage_load_normal_segment(image, segment);
                break;
+#ifdef CONFIG_CRASH_DUMP
        case KEXEC_TYPE_CRASH:
                result = kimage_load_crash_segment(image, segment);
                break;
+#endif
        }
 
        return result;
 }
 
+struct kexec_load_limit {
+       /* Mutex protects the limit count. */
+       struct mutex mutex;
+       int limit;
+};
+
+static struct kexec_load_limit load_limit_reboot = {
+       .mutex = __MUTEX_INITIALIZER(load_limit_reboot.mutex),
+       .limit = -1,
+};
+
+static struct kexec_load_limit load_limit_panic = {
+       .mutex = __MUTEX_INITIALIZER(load_limit_panic.mutex),
+       .limit = -1,
+};
+
 struct kimage *kexec_image;
 struct kimage *kexec_crash_image;
-int kexec_load_disabled;
+static int kexec_load_disabled;
+
 #ifdef CONFIG_SYSCTL
+static int kexec_limit_handler(struct ctl_table *table, int write,
+                              void *buffer, size_t *lenp, loff_t *ppos)
+{
+       struct kexec_load_limit *limit = table->data;
+       int val;
+       struct ctl_table tmp = {
+               .data = &val,
+               .maxlen = sizeof(val),
+               .mode = table->mode,
+       };
+       int ret;
+
+       if (write) {
+               ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
+               if (ret)
+                       return ret;
+
+               if (val < 0)
+                       return -EINVAL;
+
+               mutex_lock(&limit->mutex);
+               if (limit->limit != -1 && val >= limit->limit)
+                       ret = -EINVAL;
+               else
+                       limit->limit = val;
+               mutex_unlock(&limit->mutex);
+
+               return ret;
+       }
+
+       mutex_lock(&limit->mutex);
+       val = limit->limit;
+       mutex_unlock(&limit->mutex);
+
+       return proc_dointvec(&tmp, write, buffer, lenp, ppos);
+}
+
 static struct ctl_table kexec_core_sysctls[] = {
        {
                .procname       = "kexec_load_disabled",
@@ -941,6 +936,18 @@ static struct ctl_table kexec_core_sysctls[] = {
                .extra1         = SYSCTL_ONE,
                .extra2         = SYSCTL_ONE,
        },
+       {
+               .procname       = "kexec_load_limit_panic",
+               .data           = &load_limit_panic,
+               .mode           = 0644,
+               .proc_handler   = kexec_limit_handler,
+       },
+       {
+               .procname       = "kexec_load_limit_reboot",
+               .data           = &load_limit_reboot,
+               .mode           = 0644,
+               .proc_handler   = kexec_limit_handler,
+       },
        { }
 };
 
@@ -952,178 +959,32 @@ static int __init kexec_core_sysctl_init(void)
 late_initcall(kexec_core_sysctl_init);
 #endif
 
-/*
- * No panic_cpu check version of crash_kexec().  This function is called
- * only when panic_cpu holds the current CPU number; this is the only CPU
- * which processes crash_kexec routines.
- */
-void __noclone __crash_kexec(struct pt_regs *regs)
+bool kexec_load_permitted(int kexec_image_type)
 {
-       /* Take the kexec_mutex here to prevent sys_kexec_load
-        * running on one cpu from replacing the crash kernel
-        * we are using after a panic on a different cpu.
-        *
-        * If the crash kernel was not located in a fixed area
-        * of memory the xchg(&kexec_crash_image) would be
-        * sufficient.  But since I reuse the memory...
-        */
-       if (mutex_trylock(&kexec_mutex)) {
-               if (kexec_crash_image) {
-                       struct pt_regs fixed_regs;
-
-                       crash_setup_regs(&fixed_regs, regs);
-                       crash_save_vmcoreinfo();
-                       machine_crash_shutdown(&fixed_regs);
-                       machine_kexec(kexec_crash_image);
-               }
-               mutex_unlock(&kexec_mutex);
-       }
-}
-STACK_FRAME_NON_STANDARD(__crash_kexec);
-
-void crash_kexec(struct pt_regs *regs)
-{
-       int old_cpu, this_cpu;
+       struct kexec_load_limit *limit;
 
        /*
-        * Only one CPU is allowed to execute the crash_kexec() code as with
-        * panic().  Otherwise parallel calls of panic() and crash_kexec()
-        * may stop each other.  To exclude them, we use panic_cpu here too.
+        * Only the superuser can use the kexec syscall and if it has not
+        * been disabled.
         */
-       this_cpu = raw_smp_processor_id();
-       old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
-       if (old_cpu == PANIC_CPU_INVALID) {
-               /* This is the 1st CPU which comes here, so go ahead. */
-               __crash_kexec(regs);
-
-               /*
-                * Reset panic_cpu to allow another panic()/crash_kexec()
-                * call.
-                */
-               atomic_set(&panic_cpu, PANIC_CPU_INVALID);
+       if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
+               return false;
+
+       /* Check limit counter and decrease it.*/
+       limit = (kexec_image_type == KEXEC_TYPE_CRASH) ?
+               &load_limit_panic : &load_limit_reboot;
+       mutex_lock(&limit->mutex);
+       if (!limit->limit) {
+               mutex_unlock(&limit->mutex);
+               return false;
        }
-}
-
-size_t crash_get_memory_size(void)
-{
-       size_t size = 0;
+       if (limit->limit != -1)
+               limit->limit--;
+       mutex_unlock(&limit->mutex);
 
-       mutex_lock(&kexec_mutex);
-       if (crashk_res.end != crashk_res.start)
-               size = resource_size(&crashk_res);
-       mutex_unlock(&kexec_mutex);
-       return size;
+       return true;
 }
 
-int crash_shrink_memory(unsigned long new_size)
-{
-       int ret = 0;
-       unsigned long start, end;
-       unsigned long old_size;
-       struct resource *ram_res;
-
-       mutex_lock(&kexec_mutex);
-
-       if (kexec_crash_image) {
-               ret = -ENOENT;
-               goto unlock;
-       }
-       start = crashk_res.start;
-       end = crashk_res.end;
-       old_size = (end == 0) ? 0 : end - start + 1;
-       if (new_size >= old_size) {
-               ret = (new_size == old_size) ? 0 : -EINVAL;
-               goto unlock;
-       }
-
-       ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
-       if (!ram_res) {
-               ret = -ENOMEM;
-               goto unlock;
-       }
-
-       start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
-       end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
-
-       crash_free_reserved_phys_range(end, crashk_res.end);
-
-       if ((start == end) && (crashk_res.parent != NULL))
-               release_resource(&crashk_res);
-
-       ram_res->start = end;
-       ram_res->end = crashk_res.end;
-       ram_res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
-       ram_res->name = "System RAM";
-
-       crashk_res.end = end - 1;
-
-       insert_resource(&iomem_resource, ram_res);
-
-unlock:
-       mutex_unlock(&kexec_mutex);
-       return ret;
-}
-
-void crash_save_cpu(struct pt_regs *regs, int cpu)
-{
-       struct elf_prstatus prstatus;
-       u32 *buf;
-
-       if ((cpu < 0) || (cpu >= nr_cpu_ids))
-               return;
-
-       /* Using ELF notes here is opportunistic.
-        * I need a well defined structure format
-        * for the data I pass, and I need tags
-        * on the data to indicate what information I have
-        * squirrelled away.  ELF notes happen to provide
-        * all of that, so there is no need to invent something new.
-        */
-       buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
-       if (!buf)
-               return;
-       memset(&prstatus, 0, sizeof(prstatus));
-       prstatus.common.pr_pid = current->pid;
-       elf_core_copy_regs(&prstatus.pr_reg, regs);
-       buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
-                             &prstatus, sizeof(prstatus));
-       final_note(buf);
-}
-
-static int __init crash_notes_memory_init(void)
-{
-       /* Allocate memory for saving cpu registers. */
-       size_t size, align;
-
-       /*
-        * crash_notes could be allocated across 2 vmalloc pages when percpu
-        * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc
-        * pages are also on 2 continuous physical pages. In this case the
-        * 2nd part of crash_notes in 2nd page could be lost since only the
-        * starting address and size of crash_notes are exported through sysfs.
-        * Here round up the size of crash_notes to the nearest power of two
-        * and pass it to __alloc_percpu as align value. This can make sure
-        * crash_notes is allocated inside one physical page.
-        */
-       size = sizeof(note_buf_t);
-       align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE);
-
-       /*
-        * Break compile if size is bigger than PAGE_SIZE since crash_notes
-        * definitely will be in 2 pages with that.
-        */
-       BUILD_BUG_ON(size > PAGE_SIZE);
-
-       crash_notes = __alloc_percpu(size, align);
-       if (!crash_notes) {
-               pr_warn("Memory allocation for saving cpu register states failed\n");
-               return -ENOMEM;
-       }
-       return 0;
-}
-subsys_initcall(crash_notes_memory_init);
-
-
 /*
  * Move into place and start executing a preloaded standalone
  * executable.  If nothing was preloaded return an error.
@@ -1132,7 +993,7 @@ int kernel_kexec(void)
 {
        int error = 0;
 
-       if (!mutex_trylock(&kexec_mutex))
+       if (!kexec_trylock())
                return -EBUSY;
        if (!kexec_image) {
                error = -EINVAL;
@@ -1174,6 +1035,7 @@ int kernel_kexec(void)
                kexec_in_progress = true;
                kernel_restart_prepare("kexec reboot");
                migrate_to_reboot_cpu();
+               syscore_shutdown();
 
                /*
                 * migrate_to_reboot_cpu() disables CPU hotplug assuming that
@@ -1208,6 +1070,6 @@ int kernel_kexec(void)
 #endif
 
  Unlock:
-       mutex_unlock(&kexec_mutex);
+       kexec_unlock();
        return error;
 }