diff options
Diffstat (limited to 'fs/proc/vmcore.c')
| -rw-r--r-- | fs/proc/vmcore.c | 939 | 
1 files changed, 701 insertions, 238 deletions
diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c index 2367fb3f70b..382aa890e22 100644 --- a/fs/proc/vmcore.c +++ b/fs/proc/vmcore.c @@ -8,18 +8,23 @@   */  #include <linux/mm.h> -#include <linux/proc_fs.h> +#include <linux/kcore.h>  #include <linux/user.h>  #include <linux/elf.h>  #include <linux/elfcore.h> +#include <linux/export.h>  #include <linux/slab.h>  #include <linux/highmem.h> +#include <linux/printk.h>  #include <linux/bootmem.h>  #include <linux/init.h>  #include <linux/crash_dump.h>  #include <linux/list.h> +#include <linux/vmalloc.h> +#include <linux/pagemap.h>  #include <asm/uaccess.h>  #include <asm/io.h> +#include "internal.h"  /* List representing chunks of contiguous memory areas and their offsets in   * vmcore file. @@ -29,11 +34,55 @@ static LIST_HEAD(vmcore_list);  /* Stores the pointer to the buffer containing kernel elf core headers. */  static char *elfcorebuf;  static size_t elfcorebuf_sz; +static size_t elfcorebuf_sz_orig; + +static char *elfnotes_buf; +static size_t elfnotes_sz;  /* Total size of vmcore file. */  static u64 vmcore_size; -static struct proc_dir_entry *proc_vmcore = NULL; +static struct proc_dir_entry *proc_vmcore; + +/* + * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error + * The called function has to take care of module refcounting. + */ +static int (*oldmem_pfn_is_ram)(unsigned long pfn); + +int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn)) +{ +	if (oldmem_pfn_is_ram) +		return -EBUSY; +	oldmem_pfn_is_ram = fn; +	return 0; +} +EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram); + +void unregister_oldmem_pfn_is_ram(void) +{ +	oldmem_pfn_is_ram = NULL; +	wmb(); +} +EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram); + +static int pfn_is_ram(unsigned long pfn) +{ +	int (*fn)(unsigned long pfn); +	/* pfn is ram unless fn() checks pagetype */ +	int ret = 1; + +	/* +	 * Ask hypervisor if the pfn is really ram. +	 * A ballooned page contains no data and reading from such a page +	 * will cause high load in the hypervisor. +	 */ +	fn = oldmem_pfn_is_ram; +	if (fn) +		ret = fn(pfn); + +	return ret; +}  /* Reads a page from the oldmem device from given offset. */  static ssize_t read_from_oldmem(char *buf, size_t count, @@ -55,9 +104,15 @@ static ssize_t read_from_oldmem(char *buf, size_t count,  		else  			nr_bytes = count; -		tmp = copy_oldmem_page(pfn, buf, nr_bytes, offset, userbuf); -		if (tmp < 0) -			return tmp; +		/* If pfn is not ram, return zeros for sparse dump files */ +		if (pfn_is_ram(pfn) == 0) +			memset(buf, 0, nr_bytes); +		else { +			tmp = copy_oldmem_page(pfn, buf, nr_bytes, +						offset, userbuf); +			if (tmp < 0) +				return tmp; +		}  		*ppos += nr_bytes;  		count -= nr_bytes;  		buf += nr_bytes; @@ -69,37 +124,70 @@ static ssize_t read_from_oldmem(char *buf, size_t count,  	return read;  } -/* Maps vmcore file offset to respective physical address in memroy. */ -static u64 map_offset_to_paddr(loff_t offset, struct list_head *vc_list, -					struct vmcore **m_ptr) +/* + * Architectures may override this function to allocate ELF header in 2nd kernel + */ +int __weak elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)  { -	struct vmcore *m; -	u64 paddr; +	return 0; +} -	list_for_each_entry(m, vc_list, list) { -		u64 start, end; -		start = m->offset; -		end = m->offset + m->size - 1; -		if (offset >= start && offset <= end) { -			paddr = m->paddr + offset - start; -			*m_ptr = m; -			return paddr; -		} +/* + * Architectures may override this function to free header + */ +void __weak elfcorehdr_free(unsigned long long addr) +{} + +/* + * Architectures may override this function to read from ELF header + */ +ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos) +{ +	return read_from_oldmem(buf, count, ppos, 0); +} + +/* + * Architectures may override this function to read from notes sections + */ +ssize_t __weak elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos) +{ +	return read_from_oldmem(buf, count, ppos, 0); +} + +/* + * Architectures may override this function to map oldmem + */ +int __weak remap_oldmem_pfn_range(struct vm_area_struct *vma, +				  unsigned long from, unsigned long pfn, +				  unsigned long size, pgprot_t prot) +{ +	return remap_pfn_range(vma, from, pfn, size, prot); +} + +/* + * Copy to either kernel or user space + */ +static int copy_to(void *target, void *src, size_t size, int userbuf) +{ +	if (userbuf) { +		if (copy_to_user((char __user *) target, src, size)) +			return -EFAULT; +	} else { +		memcpy(target, src, size);  	} -	*m_ptr = NULL;  	return 0;  }  /* Read from the ELF header and then the crash dump. On error, negative value is   * returned otherwise number of bytes read are returned.   */ -static ssize_t read_vmcore(struct file *file, char __user *buffer, -				size_t buflen, loff_t *fpos) +static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos, +			     int userbuf)  {  	ssize_t acc = 0, tmp;  	size_t tsz; -	u64 start, nr_bytes; -	struct vmcore *curr_m = NULL; +	u64 start; +	struct vmcore *m = NULL;  	if (buflen == 0 || *fpos >= vmcore_size)  		return 0; @@ -110,10 +198,8 @@ static ssize_t read_vmcore(struct file *file, char __user *buffer,  	/* Read ELF core header */  	if (*fpos < elfcorebuf_sz) { -		tsz = elfcorebuf_sz - *fpos; -		if (buflen < tsz) -			tsz = buflen; -		if (copy_to_user(buffer, elfcorebuf + *fpos, tsz)) +		tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen); +		if (copy_to(buffer, elfcorebuf + *fpos, tsz, userbuf))  			return -EFAULT;  		buflen -= tsz;  		*fpos += tsz; @@ -125,147 +211,395 @@ static ssize_t read_vmcore(struct file *file, char __user *buffer,  			return acc;  	} -	start = map_offset_to_paddr(*fpos, &vmcore_list, &curr_m); -	if (!curr_m) -        	return -EINVAL; -	if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen) -		tsz = buflen; - -	/* Calculate left bytes in current memory segment. */ -	nr_bytes = (curr_m->size - (start - curr_m->paddr)); -	if (tsz > nr_bytes) -		tsz = nr_bytes; - -	while (buflen) { -		tmp = read_from_oldmem(buffer, tsz, &start, 1); -		if (tmp < 0) -			return tmp; +	/* Read Elf note segment */ +	if (*fpos < elfcorebuf_sz + elfnotes_sz) { +		void *kaddr; + +		tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen); +		kaddr = elfnotes_buf + *fpos - elfcorebuf_sz; +		if (copy_to(buffer, kaddr, tsz, userbuf)) +			return -EFAULT;  		buflen -= tsz;  		*fpos += tsz;  		buffer += tsz;  		acc += tsz; -		if (start >= (curr_m->paddr + curr_m->size)) { -			if (curr_m->list.next == &vmcore_list) -				return acc;	/*EOF*/ -			curr_m = list_entry(curr_m->list.next, -						struct vmcore, list); -			start = curr_m->paddr; + +		/* leave now if filled buffer already */ +		if (buflen == 0) +			return acc; +	} + +	list_for_each_entry(m, &vmcore_list, list) { +		if (*fpos < m->offset + m->size) { +			tsz = min_t(size_t, m->offset + m->size - *fpos, buflen); +			start = m->paddr + *fpos - m->offset; +			tmp = read_from_oldmem(buffer, tsz, &start, userbuf); +			if (tmp < 0) +				return tmp; +			buflen -= tsz; +			*fpos += tsz; +			buffer += tsz; +			acc += tsz; + +			/* leave now if filled buffer already */ +			if (buflen == 0) +				return acc;  		} -		if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen) -			tsz = buflen; -		/* Calculate left bytes in current memory segment. */ -		nr_bytes = (curr_m->size - (start - curr_m->paddr)); -		if (tsz > nr_bytes) -			tsz = nr_bytes;  	} +  	return acc;  } -static const struct file_operations proc_vmcore_operations = { -	.read		= read_vmcore, -	.llseek		= default_llseek, +static ssize_t read_vmcore(struct file *file, char __user *buffer, +			   size_t buflen, loff_t *fpos) +{ +	return __read_vmcore((__force char *) buffer, buflen, fpos, 1); +} + +/* + * The vmcore fault handler uses the page cache and fills data using the + * standard __vmcore_read() function. + * + * On s390 the fault handler is used for memory regions that can't be mapped + * directly with remap_pfn_range(). + */ +static int mmap_vmcore_fault(struct vm_area_struct *vma, struct vm_fault *vmf) +{ +#ifdef CONFIG_S390 +	struct address_space *mapping = vma->vm_file->f_mapping; +	pgoff_t index = vmf->pgoff; +	struct page *page; +	loff_t offset; +	char *buf; +	int rc; + +	page = find_or_create_page(mapping, index, GFP_KERNEL); +	if (!page) +		return VM_FAULT_OOM; +	if (!PageUptodate(page)) { +		offset = (loff_t) index << PAGE_CACHE_SHIFT; +		buf = __va((page_to_pfn(page) << PAGE_SHIFT)); +		rc = __read_vmcore(buf, PAGE_SIZE, &offset, 0); +		if (rc < 0) { +			unlock_page(page); +			page_cache_release(page); +			return (rc == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; +		} +		SetPageUptodate(page); +	} +	unlock_page(page); +	vmf->page = page; +	return 0; +#else +	return VM_FAULT_SIGBUS; +#endif +} + +static const struct vm_operations_struct vmcore_mmap_ops = { +	.fault = mmap_vmcore_fault,  }; -static struct vmcore* __init get_new_element(void) +/** + * alloc_elfnotes_buf - allocate buffer for ELF note segment in + *                      vmalloc memory + * + * @notes_sz: size of buffer + * + * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap + * the buffer to user-space by means of remap_vmalloc_range(). + * + * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is + * disabled and there's no need to allow users to mmap the buffer. + */ +static inline char *alloc_elfnotes_buf(size_t notes_sz)  { -	return kzalloc(sizeof(struct vmcore), GFP_KERNEL); +#ifdef CONFIG_MMU +	return vmalloc_user(notes_sz); +#else +	return vzalloc(notes_sz); +#endif  } -static u64 __init get_vmcore_size_elf64(char *elfptr) +/* + * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is + * essential for mmap_vmcore() in order to map physically + * non-contiguous objects (ELF header, ELF note segment and memory + * regions in the 1st kernel pointed to by PT_LOAD entries) into + * virtually contiguous user-space in ELF layout. + */ +#ifdef CONFIG_MMU +static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)  { -	int i; -	u64 size; -	Elf64_Ehdr *ehdr_ptr; -	Elf64_Phdr *phdr_ptr; +	size_t size = vma->vm_end - vma->vm_start; +	u64 start, end, len, tsz; +	struct vmcore *m; -	ehdr_ptr = (Elf64_Ehdr *)elfptr; -	phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); -	size = sizeof(Elf64_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr)); -	for (i = 0; i < ehdr_ptr->e_phnum; i++) { -		size += phdr_ptr->p_memsz; -		phdr_ptr++; +	start = (u64)vma->vm_pgoff << PAGE_SHIFT; +	end = start + size; + +	if (size > vmcore_size || end > vmcore_size) +		return -EINVAL; + +	if (vma->vm_flags & (VM_WRITE | VM_EXEC)) +		return -EPERM; + +	vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC); +	vma->vm_flags |= VM_MIXEDMAP; +	vma->vm_ops = &vmcore_mmap_ops; + +	len = 0; + +	if (start < elfcorebuf_sz) { +		u64 pfn; + +		tsz = min(elfcorebuf_sz - (size_t)start, size); +		pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT; +		if (remap_pfn_range(vma, vma->vm_start, pfn, tsz, +				    vma->vm_page_prot)) +			return -EAGAIN; +		size -= tsz; +		start += tsz; +		len += tsz; + +		if (size == 0) +			return 0;  	} -	return size; + +	if (start < elfcorebuf_sz + elfnotes_sz) { +		void *kaddr; + +		tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size); +		kaddr = elfnotes_buf + start - elfcorebuf_sz; +		if (remap_vmalloc_range_partial(vma, vma->vm_start + len, +						kaddr, tsz)) +			goto fail; +		size -= tsz; +		start += tsz; +		len += tsz; + +		if (size == 0) +			return 0; +	} + +	list_for_each_entry(m, &vmcore_list, list) { +		if (start < m->offset + m->size) { +			u64 paddr = 0; + +			tsz = min_t(size_t, m->offset + m->size - start, size); +			paddr = m->paddr + start - m->offset; +			if (remap_oldmem_pfn_range(vma, vma->vm_start + len, +						   paddr >> PAGE_SHIFT, tsz, +						   vma->vm_page_prot)) +				goto fail; +			size -= tsz; +			start += tsz; +			len += tsz; + +			if (size == 0) +				return 0; +		} +	} + +	return 0; +fail: +	do_munmap(vma->vm_mm, vma->vm_start, len); +	return -EAGAIN; +} +#else +static int mmap_vmcore(struct file *file, struct vm_area_struct *vma) +{ +	return -ENOSYS;  } +#endif + +static const struct file_operations proc_vmcore_operations = { +	.read		= read_vmcore, +	.llseek		= default_llseek, +	.mmap		= mmap_vmcore, +}; -static u64 __init get_vmcore_size_elf32(char *elfptr) +static struct vmcore* __init get_new_element(void) +{ +	return kzalloc(sizeof(struct vmcore), GFP_KERNEL); +} + +static u64 __init get_vmcore_size(size_t elfsz, size_t elfnotesegsz, +				  struct list_head *vc_list)  { -	int i;  	u64 size; -	Elf32_Ehdr *ehdr_ptr; -	Elf32_Phdr *phdr_ptr; +	struct vmcore *m; -	ehdr_ptr = (Elf32_Ehdr *)elfptr; -	phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); -	size = sizeof(Elf32_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr)); -	for (i = 0; i < ehdr_ptr->e_phnum; i++) { -		size += phdr_ptr->p_memsz; -		phdr_ptr++; +	size = elfsz + elfnotesegsz; +	list_for_each_entry(m, vc_list, list) { +		size += m->size;  	}  	return size;  } -/* Merges all the PT_NOTE headers into one. */ -static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz, -						struct list_head *vc_list) +/** + * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry + * + * @ehdr_ptr: ELF header + * + * This function updates p_memsz member of each PT_NOTE entry in the + * program header table pointed to by @ehdr_ptr to real size of ELF + * note segment. + */ +static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)  { -	int i, nr_ptnote=0, rc=0; -	char *tmp; -	Elf64_Ehdr *ehdr_ptr; -	Elf64_Phdr phdr, *phdr_ptr; +	int i, rc=0; +	Elf64_Phdr *phdr_ptr;  	Elf64_Nhdr *nhdr_ptr; -	u64 phdr_sz = 0, note_off; -	ehdr_ptr = (Elf64_Ehdr *)elfptr; -	phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); +	phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);  	for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { -		int j;  		void *notes_section; -		struct vmcore *new;  		u64 offset, max_sz, sz, real_sz = 0;  		if (phdr_ptr->p_type != PT_NOTE)  			continue; -		nr_ptnote++;  		max_sz = phdr_ptr->p_memsz;  		offset = phdr_ptr->p_offset;  		notes_section = kmalloc(max_sz, GFP_KERNEL);  		if (!notes_section)  			return -ENOMEM; -		rc = read_from_oldmem(notes_section, max_sz, &offset, 0); +		rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);  		if (rc < 0) {  			kfree(notes_section);  			return rc;  		}  		nhdr_ptr = notes_section; -		for (j = 0; j < max_sz; j += sz) { -			if (nhdr_ptr->n_namesz == 0) -				break; +		while (nhdr_ptr->n_namesz != 0) {  			sz = sizeof(Elf64_Nhdr) +  				((nhdr_ptr->n_namesz + 3) & ~3) +  				((nhdr_ptr->n_descsz + 3) & ~3); +			if ((real_sz + sz) > max_sz) { +				pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n", +					nhdr_ptr->n_namesz, nhdr_ptr->n_descsz); +				break; +			}  			real_sz += sz;  			nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);  		} - -		/* Add this contiguous chunk of notes section to vmcore list.*/ -		new = get_new_element(); -		if (!new) { -			kfree(notes_section); -			return -ENOMEM; -		} -		new->paddr = phdr_ptr->p_offset; -		new->size = real_sz; -		list_add_tail(&new->list, vc_list); -		phdr_sz += real_sz;  		kfree(notes_section); +		phdr_ptr->p_memsz = real_sz; +		if (real_sz == 0) { +			pr_warn("Warning: Zero PT_NOTE entries found\n"); +		} +	} + +	return 0; +} + +/** + * get_note_number_and_size_elf64 - get the number of PT_NOTE program + * headers and sum of real size of their ELF note segment headers and + * data. + * + * @ehdr_ptr: ELF header + * @nr_ptnote: buffer for the number of PT_NOTE program headers + * @sz_ptnote: buffer for size of unique PT_NOTE program header + * + * This function is used to merge multiple PT_NOTE program headers + * into a unique single one. The resulting unique entry will have + * @sz_ptnote in its phdr->p_mem. + * + * It is assumed that program headers with PT_NOTE type pointed to by + * @ehdr_ptr has already been updated by update_note_header_size_elf64 + * and each of PT_NOTE program headers has actual ELF note segment + * size in its p_memsz member. + */ +static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr, +						 int *nr_ptnote, u64 *sz_ptnote) +{ +	int i; +	Elf64_Phdr *phdr_ptr; + +	*nr_ptnote = *sz_ptnote = 0; + +	phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1); +	for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { +		if (phdr_ptr->p_type != PT_NOTE) +			continue; +		*nr_ptnote += 1; +		*sz_ptnote += phdr_ptr->p_memsz; +	} + +	return 0; +} + +/** + * copy_notes_elf64 - copy ELF note segments in a given buffer + * + * @ehdr_ptr: ELF header + * @notes_buf: buffer into which ELF note segments are copied + * + * This function is used to copy ELF note segment in the 1st kernel + * into the buffer @notes_buf in the 2nd kernel. It is assumed that + * size of the buffer @notes_buf is equal to or larger than sum of the + * real ELF note segment headers and data. + * + * It is assumed that program headers with PT_NOTE type pointed to by + * @ehdr_ptr has already been updated by update_note_header_size_elf64 + * and each of PT_NOTE program headers has actual ELF note segment + * size in its p_memsz member. + */ +static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf) +{ +	int i, rc=0; +	Elf64_Phdr *phdr_ptr; + +	phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1); + +	for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { +		u64 offset; +		if (phdr_ptr->p_type != PT_NOTE) +			continue; +		offset = phdr_ptr->p_offset; +		rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz, +					   &offset); +		if (rc < 0) +			return rc; +		notes_buf += phdr_ptr->p_memsz;  	} +	return 0; +} + +/* Merges all the PT_NOTE headers into one. */ +static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz, +					   char **notes_buf, size_t *notes_sz) +{ +	int i, nr_ptnote=0, rc=0; +	char *tmp; +	Elf64_Ehdr *ehdr_ptr; +	Elf64_Phdr phdr; +	u64 phdr_sz = 0, note_off; + +	ehdr_ptr = (Elf64_Ehdr *)elfptr; + +	rc = update_note_header_size_elf64(ehdr_ptr); +	if (rc < 0) +		return rc; + +	rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz); +	if (rc < 0) +		return rc; + +	*notes_sz = roundup(phdr_sz, PAGE_SIZE); +	*notes_buf = alloc_elfnotes_buf(*notes_sz); +	if (!*notes_buf) +		return -ENOMEM; + +	rc = copy_notes_elf64(ehdr_ptr, *notes_buf); +	if (rc < 0) +		return rc; +  	/* Prepare merged PT_NOTE program header. */  	phdr.p_type    = PT_NOTE;  	phdr.p_flags   = 0;  	note_off = sizeof(Elf64_Ehdr) +  			(ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr); -	phdr.p_offset  = note_off; +	phdr.p_offset  = roundup(note_off, PAGE_SIZE);  	phdr.p_vaddr   = phdr.p_paddr = 0;  	phdr.p_filesz  = phdr.p_memsz = phdr_sz;  	phdr.p_align   = 0; @@ -279,6 +613,8 @@ static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,  	i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);  	*elfsz = *elfsz - i;  	memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr))); +	memset(elfptr + *elfsz, 0, i); +	*elfsz = roundup(*elfsz, PAGE_SIZE);  	/* Modify e_phnum to reflect merged headers. */  	ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1; @@ -286,67 +622,170 @@ static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,  	return 0;  } -/* Merges all the PT_NOTE headers into one. */ -static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz, -						struct list_head *vc_list) +/** + * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry + * + * @ehdr_ptr: ELF header + * + * This function updates p_memsz member of each PT_NOTE entry in the + * program header table pointed to by @ehdr_ptr to real size of ELF + * note segment. + */ +static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)  { -	int i, nr_ptnote=0, rc=0; -	char *tmp; -	Elf32_Ehdr *ehdr_ptr; -	Elf32_Phdr phdr, *phdr_ptr; +	int i, rc=0; +	Elf32_Phdr *phdr_ptr;  	Elf32_Nhdr *nhdr_ptr; -	u64 phdr_sz = 0, note_off; -	ehdr_ptr = (Elf32_Ehdr *)elfptr; -	phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); +	phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);  	for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { -		int j;  		void *notes_section; -		struct vmcore *new;  		u64 offset, max_sz, sz, real_sz = 0;  		if (phdr_ptr->p_type != PT_NOTE)  			continue; -		nr_ptnote++;  		max_sz = phdr_ptr->p_memsz;  		offset = phdr_ptr->p_offset;  		notes_section = kmalloc(max_sz, GFP_KERNEL);  		if (!notes_section)  			return -ENOMEM; -		rc = read_from_oldmem(notes_section, max_sz, &offset, 0); +		rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);  		if (rc < 0) {  			kfree(notes_section);  			return rc;  		}  		nhdr_ptr = notes_section; -		for (j = 0; j < max_sz; j += sz) { -			if (nhdr_ptr->n_namesz == 0) -				break; +		while (nhdr_ptr->n_namesz != 0) {  			sz = sizeof(Elf32_Nhdr) +  				((nhdr_ptr->n_namesz + 3) & ~3) +  				((nhdr_ptr->n_descsz + 3) & ~3); +			if ((real_sz + sz) > max_sz) { +				pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n", +					nhdr_ptr->n_namesz, nhdr_ptr->n_descsz); +				break; +			}  			real_sz += sz;  			nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);  		} - -		/* Add this contiguous chunk of notes section to vmcore list.*/ -		new = get_new_element(); -		if (!new) { -			kfree(notes_section); -			return -ENOMEM; -		} -		new->paddr = phdr_ptr->p_offset; -		new->size = real_sz; -		list_add_tail(&new->list, vc_list); -		phdr_sz += real_sz;  		kfree(notes_section); +		phdr_ptr->p_memsz = real_sz; +		if (real_sz == 0) { +			pr_warn("Warning: Zero PT_NOTE entries found\n"); +		} +	} + +	return 0; +} + +/** + * get_note_number_and_size_elf32 - get the number of PT_NOTE program + * headers and sum of real size of their ELF note segment headers and + * data. + * + * @ehdr_ptr: ELF header + * @nr_ptnote: buffer for the number of PT_NOTE program headers + * @sz_ptnote: buffer for size of unique PT_NOTE program header + * + * This function is used to merge multiple PT_NOTE program headers + * into a unique single one. The resulting unique entry will have + * @sz_ptnote in its phdr->p_mem. + * + * It is assumed that program headers with PT_NOTE type pointed to by + * @ehdr_ptr has already been updated by update_note_header_size_elf32 + * and each of PT_NOTE program headers has actual ELF note segment + * size in its p_memsz member. + */ +static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr, +						 int *nr_ptnote, u64 *sz_ptnote) +{ +	int i; +	Elf32_Phdr *phdr_ptr; + +	*nr_ptnote = *sz_ptnote = 0; + +	phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1); +	for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { +		if (phdr_ptr->p_type != PT_NOTE) +			continue; +		*nr_ptnote += 1; +		*sz_ptnote += phdr_ptr->p_memsz;  	} +	return 0; +} + +/** + * copy_notes_elf32 - copy ELF note segments in a given buffer + * + * @ehdr_ptr: ELF header + * @notes_buf: buffer into which ELF note segments are copied + * + * This function is used to copy ELF note segment in the 1st kernel + * into the buffer @notes_buf in the 2nd kernel. It is assumed that + * size of the buffer @notes_buf is equal to or larger than sum of the + * real ELF note segment headers and data. + * + * It is assumed that program headers with PT_NOTE type pointed to by + * @ehdr_ptr has already been updated by update_note_header_size_elf32 + * and each of PT_NOTE program headers has actual ELF note segment + * size in its p_memsz member. + */ +static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf) +{ +	int i, rc=0; +	Elf32_Phdr *phdr_ptr; + +	phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1); + +	for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { +		u64 offset; +		if (phdr_ptr->p_type != PT_NOTE) +			continue; +		offset = phdr_ptr->p_offset; +		rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz, +					   &offset); +		if (rc < 0) +			return rc; +		notes_buf += phdr_ptr->p_memsz; +	} + +	return 0; +} + +/* Merges all the PT_NOTE headers into one. */ +static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz, +					   char **notes_buf, size_t *notes_sz) +{ +	int i, nr_ptnote=0, rc=0; +	char *tmp; +	Elf32_Ehdr *ehdr_ptr; +	Elf32_Phdr phdr; +	u64 phdr_sz = 0, note_off; + +	ehdr_ptr = (Elf32_Ehdr *)elfptr; + +	rc = update_note_header_size_elf32(ehdr_ptr); +	if (rc < 0) +		return rc; + +	rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz); +	if (rc < 0) +		return rc; + +	*notes_sz = roundup(phdr_sz, PAGE_SIZE); +	*notes_buf = alloc_elfnotes_buf(*notes_sz); +	if (!*notes_buf) +		return -ENOMEM; + +	rc = copy_notes_elf32(ehdr_ptr, *notes_buf); +	if (rc < 0) +		return rc; +  	/* Prepare merged PT_NOTE program header. */  	phdr.p_type    = PT_NOTE;  	phdr.p_flags   = 0;  	note_off = sizeof(Elf32_Ehdr) +  			(ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr); -	phdr.p_offset  = note_off; +	phdr.p_offset  = roundup(note_off, PAGE_SIZE);  	phdr.p_vaddr   = phdr.p_paddr = 0;  	phdr.p_filesz  = phdr.p_memsz = phdr_sz;  	phdr.p_align   = 0; @@ -360,6 +799,8 @@ static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,  	i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);  	*elfsz = *elfsz - i;  	memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr))); +	memset(elfptr + *elfsz, 0, i); +	*elfsz = roundup(*elfsz, PAGE_SIZE);  	/* Modify e_phnum to reflect merged headers. */  	ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1; @@ -371,6 +812,7 @@ static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,   * the new offset fields of exported program headers. */  static int __init process_ptload_program_headers_elf64(char *elfptr,  						size_t elfsz, +						size_t elfnotes_sz,  						struct list_head *vc_list)  {  	int i; @@ -382,32 +824,38 @@ static int __init process_ptload_program_headers_elf64(char *elfptr,  	ehdr_ptr = (Elf64_Ehdr *)elfptr;  	phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */ -	/* First program header is PT_NOTE header. */ -	vmcore_off = sizeof(Elf64_Ehdr) + -			(ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr) + -			phdr_ptr->p_memsz; /* Note sections */ +	/* Skip Elf header, program headers and Elf note segment. */ +	vmcore_off = elfsz + elfnotes_sz;  	for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { +		u64 paddr, start, end, size; +  		if (phdr_ptr->p_type != PT_LOAD)  			continue; +		paddr = phdr_ptr->p_offset; +		start = rounddown(paddr, PAGE_SIZE); +		end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE); +		size = end - start; +  		/* Add this contiguous chunk of memory to vmcore list.*/  		new = get_new_element();  		if (!new)  			return -ENOMEM; -		new->paddr = phdr_ptr->p_offset; -		new->size = phdr_ptr->p_memsz; +		new->paddr = start; +		new->size = size;  		list_add_tail(&new->list, vc_list);  		/* Update the program header offset. */ -		phdr_ptr->p_offset = vmcore_off; -		vmcore_off = vmcore_off + phdr_ptr->p_memsz; +		phdr_ptr->p_offset = vmcore_off + (paddr - start); +		vmcore_off = vmcore_off + size;  	}  	return 0;  }  static int __init process_ptload_program_headers_elf32(char *elfptr,  						size_t elfsz, +						size_t elfnotes_sz,  						struct list_head *vc_list)  {  	int i; @@ -419,43 +867,44 @@ static int __init process_ptload_program_headers_elf32(char *elfptr,  	ehdr_ptr = (Elf32_Ehdr *)elfptr;  	phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */ -	/* First program header is PT_NOTE header. */ -	vmcore_off = sizeof(Elf32_Ehdr) + -			(ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr) + -			phdr_ptr->p_memsz; /* Note sections */ +	/* Skip Elf header, program headers and Elf note segment. */ +	vmcore_off = elfsz + elfnotes_sz;  	for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { +		u64 paddr, start, end, size; +  		if (phdr_ptr->p_type != PT_LOAD)  			continue; +		paddr = phdr_ptr->p_offset; +		start = rounddown(paddr, PAGE_SIZE); +		end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE); +		size = end - start; +  		/* Add this contiguous chunk of memory to vmcore list.*/  		new = get_new_element();  		if (!new)  			return -ENOMEM; -		new->paddr = phdr_ptr->p_offset; -		new->size = phdr_ptr->p_memsz; +		new->paddr = start; +		new->size = size;  		list_add_tail(&new->list, vc_list);  		/* Update the program header offset */ -		phdr_ptr->p_offset = vmcore_off; -		vmcore_off = vmcore_off + phdr_ptr->p_memsz; +		phdr_ptr->p_offset = vmcore_off + (paddr - start); +		vmcore_off = vmcore_off + size;  	}  	return 0;  }  /* Sets offset fields of vmcore elements. */ -static void __init set_vmcore_list_offsets_elf64(char *elfptr, -						struct list_head *vc_list) +static void __init set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz, +					   struct list_head *vc_list)  {  	loff_t vmcore_off; -	Elf64_Ehdr *ehdr_ptr;  	struct vmcore *m; -	ehdr_ptr = (Elf64_Ehdr *)elfptr; - -	/* Skip Elf header and program headers. */ -	vmcore_off = sizeof(Elf64_Ehdr) + -			(ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr); +	/* Skip Elf header, program headers and Elf note segment. */ +	vmcore_off = elfsz + elfnotes_sz;  	list_for_each_entry(m, vc_list, list) {  		m->offset = vmcore_off; @@ -463,24 +912,12 @@ static void __init set_vmcore_list_offsets_elf64(char *elfptr,  	}  } -/* Sets offset fields of vmcore elements. */ -static void __init set_vmcore_list_offsets_elf32(char *elfptr, -						struct list_head *vc_list) +static void free_elfcorebuf(void)  { -	loff_t vmcore_off; -	Elf32_Ehdr *ehdr_ptr; -	struct vmcore *m; - -	ehdr_ptr = (Elf32_Ehdr *)elfptr; - -	/* Skip Elf header and program headers. */ -	vmcore_off = sizeof(Elf32_Ehdr) + -			(ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr); - -	list_for_each_entry(m, vc_list, list) { -		m->offset = vmcore_off; -		vmcore_off += m->size; -	} +	free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig)); +	elfcorebuf = NULL; +	vfree(elfnotes_buf); +	elfnotes_buf = NULL;  }  static int __init parse_crash_elf64_headers(void) @@ -492,51 +929,51 @@ static int __init parse_crash_elf64_headers(void)  	addr = elfcorehdr_addr;  	/* Read Elf header */ -	rc = read_from_oldmem((char*)&ehdr, sizeof(Elf64_Ehdr), &addr, 0); +	rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf64_Ehdr), &addr);  	if (rc < 0)  		return rc;  	/* Do some basic Verification. */  	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||  		(ehdr.e_type != ET_CORE) || -		!vmcore_elf_check_arch(&ehdr) || +		!vmcore_elf64_check_arch(&ehdr) ||  		ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||  		ehdr.e_ident[EI_VERSION] != EV_CURRENT ||  		ehdr.e_version != EV_CURRENT ||  		ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||  		ehdr.e_phentsize != sizeof(Elf64_Phdr) ||  		ehdr.e_phnum == 0) { -		printk(KERN_WARNING "Warning: Core image elf header is not" -					"sane\n"); +		pr_warn("Warning: Core image elf header is not sane\n");  		return -EINVAL;  	}  	/* Read in all elf headers. */ -	elfcorebuf_sz = sizeof(Elf64_Ehdr) + ehdr.e_phnum * sizeof(Elf64_Phdr); -	elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL); +	elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) + +				ehdr.e_phnum * sizeof(Elf64_Phdr); +	elfcorebuf_sz = elfcorebuf_sz_orig; +	elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, +					      get_order(elfcorebuf_sz_orig));  	if (!elfcorebuf)  		return -ENOMEM;  	addr = elfcorehdr_addr; -	rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0); -	if (rc < 0) { -		kfree(elfcorebuf); -		return rc; -	} +	rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr); +	if (rc < 0) +		goto fail;  	/* Merge all PT_NOTE headers into one. */ -	rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz, &vmcore_list); -	if (rc) { -		kfree(elfcorebuf); -		return rc; -	} +	rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz, +				      &elfnotes_buf, &elfnotes_sz); +	if (rc) +		goto fail;  	rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz, -							&vmcore_list); -	if (rc) { -		kfree(elfcorebuf); -		return rc; -	} -	set_vmcore_list_offsets_elf64(elfcorebuf, &vmcore_list); +						  elfnotes_sz, &vmcore_list); +	if (rc) +		goto fail; +	set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);  	return 0; +fail: +	free_elfcorebuf(); +	return rc;  }  static int __init parse_crash_elf32_headers(void) @@ -548,7 +985,7 @@ static int __init parse_crash_elf32_headers(void)  	addr = elfcorehdr_addr;  	/* Read Elf header */ -	rc = read_from_oldmem((char*)&ehdr, sizeof(Elf32_Ehdr), &addr, 0); +	rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf32_Ehdr), &addr);  	if (rc < 0)  		return rc; @@ -562,37 +999,36 @@ static int __init parse_crash_elf32_headers(void)  		ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||  		ehdr.e_phentsize != sizeof(Elf32_Phdr) ||  		ehdr.e_phnum == 0) { -		printk(KERN_WARNING "Warning: Core image elf header is not" -					"sane\n"); +		pr_warn("Warning: Core image elf header is not sane\n");  		return -EINVAL;  	}  	/* Read in all elf headers. */ -	elfcorebuf_sz = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr); -	elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL); +	elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr); +	elfcorebuf_sz = elfcorebuf_sz_orig; +	elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, +					      get_order(elfcorebuf_sz_orig));  	if (!elfcorebuf)  		return -ENOMEM;  	addr = elfcorehdr_addr; -	rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0); -	if (rc < 0) { -		kfree(elfcorebuf); -		return rc; -	} +	rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr); +	if (rc < 0) +		goto fail;  	/* Merge all PT_NOTE headers into one. */ -	rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz, &vmcore_list); -	if (rc) { -		kfree(elfcorebuf); -		return rc; -	} +	rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz, +				      &elfnotes_buf, &elfnotes_sz); +	if (rc) +		goto fail;  	rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz, -								&vmcore_list); -	if (rc) { -		kfree(elfcorebuf); -		return rc; -	} -	set_vmcore_list_offsets_elf32(elfcorebuf, &vmcore_list); +						  elfnotes_sz, &vmcore_list); +	if (rc) +		goto fail; +	set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);  	return 0; +fail: +	free_elfcorebuf(); +	return rc;  }  static int __init parse_crash_elf_headers(void) @@ -602,12 +1038,11 @@ static int __init parse_crash_elf_headers(void)  	int rc=0;  	addr = elfcorehdr_addr; -	rc = read_from_oldmem(e_ident, EI_NIDENT, &addr, 0); +	rc = elfcorehdr_read(e_ident, EI_NIDENT, &addr);  	if (rc < 0)  		return rc;  	if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) { -		printk(KERN_WARNING "Warning: Core image elf header" -					" not found\n"); +		pr_warn("Warning: Core image elf header not found\n");  		return -EINVAL;  	} @@ -615,21 +1050,19 @@ static int __init parse_crash_elf_headers(void)  		rc = parse_crash_elf64_headers();  		if (rc)  			return rc; - -		/* Determine vmcore size. */ -		vmcore_size = get_vmcore_size_elf64(elfcorebuf);  	} else if (e_ident[EI_CLASS] == ELFCLASS32) {  		rc = parse_crash_elf32_headers();  		if (rc)  			return rc; - -		/* Determine vmcore size. */ -		vmcore_size = get_vmcore_size_elf32(elfcorebuf);  	} else { -		printk(KERN_WARNING "Warning: Core image elf header is not" -					" sane\n"); +		pr_warn("Warning: Core image elf header is not sane\n");  		return -EINVAL;  	} + +	/* Determine vmcore size. */ +	vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz, +				      &vmcore_list); +  	return 0;  } @@ -638,18 +1071,48 @@ static int __init vmcore_init(void)  {  	int rc = 0; -	/* If elfcorehdr= has been passed in cmdline, then capture the dump.*/ +	/* Allow architectures to allocate ELF header in 2nd kernel */ +	rc = elfcorehdr_alloc(&elfcorehdr_addr, &elfcorehdr_size); +	if (rc) +		return rc; +	/* +	 * If elfcorehdr= has been passed in cmdline or created in 2nd kernel, +	 * then capture the dump. +	 */  	if (!(is_vmcore_usable()))  		return rc;  	rc = parse_crash_elf_headers();  	if (rc) { -		printk(KERN_WARNING "Kdump: vmcore not initialized\n"); +		pr_warn("Kdump: vmcore not initialized\n");  		return rc;  	} +	elfcorehdr_free(elfcorehdr_addr); +	elfcorehdr_addr = ELFCORE_ADDR_ERR;  	proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);  	if (proc_vmcore)  		proc_vmcore->size = vmcore_size;  	return 0;  } -module_init(vmcore_init) +fs_initcall(vmcore_init); + +/* Cleanup function for vmcore module. */ +void vmcore_cleanup(void) +{ +	struct list_head *pos, *next; + +	if (proc_vmcore) { +		proc_remove(proc_vmcore); +		proc_vmcore = NULL; +	} + +	/* clear the vmcore list. */ +	list_for_each_safe(pos, next, &vmcore_list) { +		struct vmcore *m; + +		m = list_entry(pos, struct vmcore, list); +		list_del(&m->list); +		kfree(m); +	} +	free_elfcorebuf(); +}  | 
