1377 lines · 33.8 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-only/* * kexec.c - kexec system call core code. * Copyright (C) 2002-2004 Eric Biederman  <ebiederm@xmission.com> */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/btf.h>#include <linux/capability.h>#include <linux/mm.h>#include <linux/file.h>#include <linux/slab.h>#include <linux/fs.h>#include <linux/kexec.h>#include <linux/mutex.h>#include <linux/list.h>#include <linux/liveupdate.h>#include <linux/highmem.h>#include <linux/syscalls.h>#include <linux/reboot.h>#include <linux/ioport.h>#include <linux/hardirq.h>#include <linux/elf.h>#include <linux/elfcore.h>#include <linux/utsname.h>#include <linux/numa.h>#include <linux/suspend.h>#include <linux/device.h>#include <linux/freezer.h>#include <linux/panic_notifier.h>#include <linux/pm.h>#include <linux/cpu.h>#include <linux/uaccess.h>#include <linux/io.h>#include <linux/console.h>#include <linux/vmalloc.h>#include <linux/swap.h>#include <linux/syscore_ops.h>#include <linux/compiler.h>#include <linux/hugetlb.h>#include <linux/objtool.h>#include <linux/kmsg_dump.h>#include <linux/dma-map-ops.h>#include <linux/sysfs.h> #include <asm/page.h>#include <asm/sections.h> #include "kexec_internal.h" atomic_t __kexec_lock = ATOMIC_INIT(0); /* Flag to indicate we are going to kexec a new kernel */bool kexec_in_progress = false; bool kexec_file_dbg_print; /* * When kexec transitions to the new kernel there is a one-to-one * mapping between physical and virtual addresses.  On processors * where you can disable the MMU this is trivial, and easy.  For * others it is still a simple predictable page table to setup. * * In that environment kexec copies the new kernel to its final * resting place.  This means I can only support memory whose * physical address can fit in an unsigned long.  In particular * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled. * If the assembly stub has more restrictive requirements * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be * defined more restrictively in <asm/kexec.h>. * * The code for the transition from the current kernel to the * new kernel is placed in the control_code_buffer, whose size * is given by KEXEC_CONTROL_PAGE_SIZE.  In the best case only a single * page of memory is necessary, but some architectures require more. * Because this memory must be identity mapped in the transition from * virtual to physical addresses it must live in the range * 0 - TASK_SIZE, as only the user space mappings are arbitrarily * modifiable. * * The assembly stub in the control code buffer is passed a linked list * of descriptor pages detailing the source pages of the new kernel, * and the destination addresses of those source pages.  As this data * structure is not used in the context of the current OS, it must * be self-contained. * * The code has been made to work with highmem pages and will use a * destination page in its final resting place (if it happens * to allocate it).  The end product of this is that most of the * physical address space, and most of RAM can be used. * * Future directions include: *  - allocating a page table with the control code buffer identity *    mapped, to simplify machine_kexec and make kexec_on_panic more *    reliable. */ /* * KIMAGE_NO_DEST is an impossible destination address..., for * allocating pages whose destination address we do not care about. */#define KIMAGE_NO_DEST (-1UL)#define PAGE_COUNT(x) (((x) + PAGE_SIZE - 1) >> PAGE_SHIFT) static struct page *kimage_alloc_page(struct kimage *image,				       gfp_t gfp_mask,				       unsigned long dest); int sanity_check_segment_list(struct kimage *image){	int i;	unsigned long nr_segments = image->nr_segments;	unsigned long total_pages = 0;	unsigned long nr_pages = totalram_pages(); 	/*	 * Verify we have good destination addresses.  The caller is	 * responsible for making certain we don't attempt to load	 * the new image into invalid or reserved areas of RAM.  This	 * just verifies it is an address we can use.	 *	 * Since the kernel does everything in page size chunks ensure	 * the destination addresses are page aligned.  Too many	 * special cases crop of when we don't do this.  The most	 * insidious is getting overlapping destination addresses	 * simply because addresses are changed to page size	 * granularity.	 */	for (i = 0; i < nr_segments; i++) {		unsigned long mstart, mend; 		mstart = image->segment[i].mem;		mend   = mstart + image->segment[i].memsz;		if (mstart > mend)			return -EADDRNOTAVAIL;		if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))			return -EADDRNOTAVAIL;		if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)			return -EADDRNOTAVAIL;	} 	/* Verify our destination addresses do not overlap.	 * If we alloed overlapping destination addresses	 * through very weird things can happen with no	 * easy explanation as one segment stops on another.	 */	for (i = 0; i < nr_segments; i++) {		unsigned long mstart, mend;		unsigned long j; 		mstart = image->segment[i].mem;		mend   = mstart + image->segment[i].memsz;		for (j = 0; j < i; j++) {			unsigned long pstart, pend; 			pstart = image->segment[j].mem;			pend   = pstart + image->segment[j].memsz;			/* Do the segments overlap ? */			if ((mend > pstart) && (mstart < pend))				return -EINVAL;		}	} 	/* Ensure our buffer sizes are strictly less than	 * our memory sizes.  This should always be the case,	 * and it is easier to check up front than to be surprised	 * later on.	 */	for (i = 0; i < nr_segments; i++) {		if (image->segment[i].bufsz > image->segment[i].memsz)			return -EINVAL;	} 	/*	 * Verify that no more than half of memory will be consumed. If the	 * request from userspace is too large, a large amount of time will be	 * wasted allocating pages, which can cause a soft lockup.	 */	for (i = 0; i < nr_segments; i++) {		if (PAGE_COUNT(image->segment[i].memsz) > nr_pages / 2)			return -EINVAL; 		total_pages += PAGE_COUNT(image->segment[i].memsz);	} 	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	 * attempt to load the new image into invalid or reserved	 * areas of RAM.  But crash kernels are preloaded into a	 * reserved area of ram.  We must ensure the addresses	 * are in the reserved area otherwise preloading the	 * kernel could corrupt things.	 */ 	if (image->type == KEXEC_TYPE_CRASH) {		for (i = 0; i < nr_segments; i++) {			unsigned long mstart, mend; 			mstart = image->segment[i].mem;			mend = mstart + image->segment[i].memsz - 1;			/* Ensure we are within the crash kernel limits */			if ((mstart < phys_to_boot_phys(crashk_res.start)) ||			    (mend > phys_to_boot_phys(crashk_res.end)))				return -EADDRNOTAVAIL;		}	}#endif 	/*	 * The destination addresses are searched from system RAM rather than	 * being allocated from the buddy allocator, so they are not guaranteed	 * to be accepted by the current kernel.  Accept the destination	 * addresses before kexec swaps their content with the segments' source	 * pages to avoid accessing memory before it is accepted.	 */	for (i = 0; i < nr_segments; i++)		accept_memory(image->segment[i].mem, image->segment[i].memsz); 	return 0;} struct kimage *do_kimage_alloc_init(void){	struct kimage *image; 	/* Allocate a controlling structure */	image = kzalloc_obj(*image);	if (!image)		return NULL; 	image->entry = &image->head;	image->last_entry = &image->head;	image->control_page = ~0; /* By default this does not apply */	image->type = KEXEC_TYPE_DEFAULT; 	/* Initialize the list of control pages */	INIT_LIST_HEAD(&image->control_pages); 	/* Initialize the list of destination pages */	INIT_LIST_HEAD(&image->dest_pages); 	/* 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;} int kimage_is_destination_range(struct kimage *image,					unsigned long start,					unsigned long end){	unsigned long i; 	for (i = 0; i < image->nr_segments; i++) {		unsigned long mstart, mend; 		mstart = image->segment[i].mem;		mend = mstart + image->segment[i].memsz - 1;		if ((end >= mstart) && (start <= mend))			return 1;	} 	return 0;} static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order){	struct page *pages; 	if (fatal_signal_pending(current))		return NULL;	pages = alloc_pages(gfp_mask & ~__GFP_ZERO, order);	if (pages) {		unsigned int count, i; 		pages->mapping = NULL;		set_page_private(pages, order);		count = 1 << order;		for (i = 0; i < count; i++)			SetPageReserved(pages + i); 		arch_kexec_post_alloc_pages(page_address(pages), count,					    gfp_mask); 		if (gfp_mask & __GFP_ZERO)			for (i = 0; i < count; i++)				clear_highpage(pages + i);	} 	return pages;} static void kimage_free_pages(struct page *page){	unsigned int order, count, i; 	order = page_private(page);	count = 1 << order; 	arch_kexec_pre_free_pages(page_address(page), count); 	for (i = 0; i < count; i++)		ClearPageReserved(page + i);	__free_pages(page, order);} void kimage_free_page_list(struct list_head *list){	struct page *page, *next; 	list_for_each_entry_safe(page, next, list, lru) {		list_del(&page->lru);		kimage_free_pages(page);	}} static struct page *kimage_alloc_normal_control_pages(struct kimage *image,							unsigned int order){	/* Control pages are special, they are the intermediaries	 * that are needed while we copy the rest of the pages	 * to their final resting place.  As such they must	 * not conflict with either the destination addresses	 * or memory the kernel is already using.	 *	 * The only case where we really need more than one of	 * these are for architectures where we cannot disable	 * the MMU and must instead generate an identity mapped	 * page table for all of the memory.	 *	 * At worst this runs in O(N) of the image size.	 */	struct list_head extra_pages;	struct page *pages;	unsigned int count; 	count = 1 << order;	INIT_LIST_HEAD(&extra_pages); 	/* Loop while I can allocate a page and the page allocated	 * is a destination page.	 */	do {		unsigned long pfn, epfn, addr, eaddr; 		pages = kimage_alloc_pages(KEXEC_CONTROL_MEMORY_GFP, order);		if (!pages)			break;		pfn   = page_to_boot_pfn(pages);		epfn  = pfn + count;		addr  = pfn << 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);			pages = NULL;		}	} while (!pages); 	if (pages) {		/* Remember the allocated page... */		list_add(&pages->lru, &image->control_pages); 		/* Because the page is already in it's destination		 * location we will never allocate another page at		 * that address.  Therefore kimage_alloc_pages		 * will not return it (again) and we don't need		 * to give it an entry in image->segment[].		 */	}	/* Deal with the destination pages I have inadvertently allocated.	 *	 * Ideally I would convert multi-page allocations into single	 * page allocations, and add everything to image->dest_pages.	 *	 * For now it is simpler to just free the pages.	 */	kimage_free_page_list(&extra_pages); 	return pages;} #ifdef CONFIG_CRASH_DUMPstatic struct page *kimage_alloc_crash_control_pages(struct kimage *image,						      unsigned int order){	/* Control pages are special, they are the intermediaries	 * that are needed while we copy the rest of the pages	 * to their final resting place.  As such they must	 * not conflict with either the destination addresses	 * or memory the kernel is already using.	 *	 * Control pages are also the only pags we must allocate	 * when loading a crash kernel.  All of the other pages	 * are specified by the segments and we just memcpy	 * into them directly.	 *	 * The only case where we really need more than one of	 * these are for architectures where we cannot disable	 * the MMU and must instead generate an identity mapped	 * page table for all of the memory.	 *	 * Given the low demand this implements a very simple	 * allocator that finds the first hole of the appropriate	 * size in the reserved memory region, and allocates all	 * of the memory up to and including the hole.	 */	unsigned long hole_start, hole_end, size;	struct page *pages; 	pages = NULL;	size = (1 << order) << PAGE_SHIFT;	hole_start = ALIGN(image->control_page, size);	hole_end   = hole_start + size - 1;	while (hole_end <= crashk_res.end) {		unsigned long i; 		cond_resched(); 		if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)			break;		/* See if I overlap any of the segments */		for (i = 0; i < image->nr_segments; i++) {			unsigned long mstart, mend; 			mstart = image->segment[i].mem;			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 = ALIGN(mend, size);				hole_end   = hole_start + size - 1;				break;			}		}		/* 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 + 1;			break;		}	} 	/* Ensure that these pages are decrypted if SME is enabled. */	if (pages)		arch_kexec_post_alloc_pages(page_address(pages), 1 << order, 0); 	return pages;}#endif  struct page *kimage_alloc_control_pages(struct kimage *image,					 unsigned int order){	struct page *pages = NULL; 	switch (image->type) {	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;} static int kimage_add_entry(struct kimage *image, kimage_entry_t entry){	if (*image->entry != 0)		image->entry++; 	if (image->entry == image->last_entry) {		kimage_entry_t *ind_page;		struct page *page; 		page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);		if (!page)			return -ENOMEM; 		ind_page = page_address(page);		*image->entry = virt_to_boot_phys(ind_page) | IND_INDIRECTION;		image->entry = ind_page;		image->last_entry = ind_page +				      ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);

Lines 1–500 of 1377.