Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'akpm' (patches from Andrew)

Merge yet more updates from Andrew Morton:

- various hotfixes

- kexec_file updates and feature work

* emailed patches from Andrew Morton <akpm@linux-foundation.org>: (27 commits)
kernel/kexec_file.c: move purgatories sha256 to common code
kernel/kexec_file.c: allow archs to set purgatory load address
kernel/kexec_file.c: remove mis-use of sh_offset field during purgatory load
kernel/kexec_file.c: remove unneeded variables in kexec_purgatory_setup_sechdrs
kernel/kexec_file.c: remove unneeded for-loop in kexec_purgatory_setup_sechdrs
kernel/kexec_file.c: split up __kexec_load_puragory
kernel/kexec_file.c: use read-only sections in arch_kexec_apply_relocations*
kernel/kexec_file.c: search symbols in read-only kexec_purgatory
kernel/kexec_file.c: make purgatory_info->ehdr const
kernel/kexec_file.c: remove checks in kexec_purgatory_load
include/linux/kexec.h: silence compile warnings
kexec_file, x86: move re-factored code to generic side
x86: kexec_file: clean up prepare_elf64_headers()
x86: kexec_file: lift CRASH_MAX_RANGES limit on crash_mem buffer
x86: kexec_file: remove X86_64 dependency from prepare_elf64_headers()
x86: kexec_file: purge system-ram walking from prepare_elf64_headers()
kexec_file,x86,powerpc: factor out kexec_file_ops functions
kexec_file: make use of purgatory optional
proc: revalidate misc dentries
mm, slab: reschedule cache_reap() on the same CPU
...

+684 -657
+2
arch/mips/mm/gup.c
··· 178 178 /* 179 179 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall 180 180 * back to the regular GUP. 181 + * Note a difference with get_user_pages_fast: this always returns the 182 + * number of pages pinned, 0 if no pages were pinned. 181 183 */ 182 184 int __get_user_pages_fast(unsigned long start, int nr_pages, int write, 183 185 struct page **pages)
+3
arch/powerpc/Kconfig
··· 552 552 for kernel and initramfs as opposed to a list of segments as is the 553 553 case for the older kexec call. 554 554 555 + config ARCH_HAS_KEXEC_PURGATORY 556 + def_bool KEXEC_FILE 557 + 555 558 config RELOCATABLE 556 559 bool "Build a relocatable kernel" 557 560 depends on PPC64 || (FLATMEM && (44x || FSL_BOOKE))
+1 -1
arch/powerpc/include/asm/kexec.h
··· 95 95 } 96 96 97 97 #ifdef CONFIG_KEXEC_FILE 98 - extern struct kexec_file_ops kexec_elf64_ops; 98 + extern const struct kexec_file_ops kexec_elf64_ops; 99 99 100 100 #ifdef CONFIG_IMA_KEXEC 101 101 #define ARCH_HAS_KIMAGE_ARCH
+6 -5
arch/powerpc/kernel/kexec_elf_64.c
··· 572 572 { 573 573 int ret; 574 574 unsigned int fdt_size; 575 - unsigned long kernel_load_addr, purgatory_load_addr; 575 + unsigned long kernel_load_addr; 576 576 unsigned long initrd_load_addr = 0, fdt_load_addr; 577 577 void *fdt; 578 578 const void *slave_code; ··· 580 580 struct elf_info elf_info; 581 581 struct kexec_buf kbuf = { .image = image, .buf_min = 0, 582 582 .buf_max = ppc64_rma_size }; 583 + struct kexec_buf pbuf = { .image = image, .buf_min = 0, 584 + .buf_max = ppc64_rma_size, .top_down = true }; 583 585 584 586 ret = build_elf_exec_info(kernel_buf, kernel_len, &ehdr, &elf_info); 585 587 if (ret) ··· 593 591 594 592 pr_debug("Loaded the kernel at 0x%lx\n", kernel_load_addr); 595 593 596 - ret = kexec_load_purgatory(image, 0, ppc64_rma_size, true, 597 - &purgatory_load_addr); 594 + ret = kexec_load_purgatory(image, &pbuf); 598 595 if (ret) { 599 596 pr_err("Loading purgatory failed.\n"); 600 597 goto out; 601 598 } 602 599 603 - pr_debug("Loaded purgatory at 0x%lx\n", purgatory_load_addr); 600 + pr_debug("Loaded purgatory at 0x%lx\n", pbuf.mem); 604 601 605 602 if (initrd != NULL) { 606 603 kbuf.buffer = initrd; ··· 658 657 return ret ? ERR_PTR(ret) : fdt; 659 658 } 660 659 661 - struct kexec_file_ops kexec_elf64_ops = { 660 + const struct kexec_file_ops kexec_elf64_ops = { 662 661 .probe = elf64_probe, 663 662 .load = elf64_load, 664 663 };
+3 -36
arch/powerpc/kernel/machine_kexec_file_64.c
··· 31 31 32 32 #define SLAVE_CODE_SIZE 256 33 33 34 - static struct kexec_file_ops *kexec_file_loaders[] = { 34 + const struct kexec_file_ops * const kexec_file_loaders[] = { 35 35 &kexec_elf64_ops, 36 + NULL 36 37 }; 37 38 38 39 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf, 39 40 unsigned long buf_len) 40 41 { 41 - int i, ret = -ENOEXEC; 42 - struct kexec_file_ops *fops; 43 - 44 42 /* We don't support crash kernels yet. */ 45 43 if (image->type == KEXEC_TYPE_CRASH) 46 44 return -EOPNOTSUPP; 47 45 48 - for (i = 0; i < ARRAY_SIZE(kexec_file_loaders); i++) { 49 - fops = kexec_file_loaders[i]; 50 - if (!fops || !fops->probe) 51 - continue; 52 - 53 - ret = fops->probe(buf, buf_len); 54 - if (!ret) { 55 - image->fops = fops; 56 - return ret; 57 - } 58 - } 59 - 60 - return ret; 61 - } 62 - 63 - void *arch_kexec_kernel_image_load(struct kimage *image) 64 - { 65 - if (!image->fops || !image->fops->load) 66 - return ERR_PTR(-ENOEXEC); 67 - 68 - return image->fops->load(image, image->kernel_buf, 69 - image->kernel_buf_len, image->initrd_buf, 70 - image->initrd_buf_len, image->cmdline_buf, 71 - image->cmdline_buf_len); 72 - } 73 - 74 - int arch_kimage_file_post_load_cleanup(struct kimage *image) 75 - { 76 - if (!image->fops || !image->fops->cleanup) 77 - return 0; 78 - 79 - return image->fops->cleanup(image->image_loader_data); 46 + return kexec_image_probe_default(image, buf, buf_len); 80 47 } 81 48 82 49 /**
+2
arch/s390/mm/gup.c
··· 220 220 /* 221 221 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall 222 222 * back to the regular GUP. 223 + * Note a difference with get_user_pages_fast: this always returns the 224 + * number of pages pinned, 0 if no pages were pinned. 223 225 */ 224 226 int __get_user_pages_fast(unsigned long start, int nr_pages, int write, 225 227 struct page **pages)
+2
arch/sh/mm/gup.c
··· 160 160 /* 161 161 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall 162 162 * back to the regular GUP. 163 + * Note a difference with get_user_pages_fast: this always returns the 164 + * number of pages pinned, 0 if no pages were pinned. 163 165 */ 164 166 int __get_user_pages_fast(unsigned long start, int nr_pages, int write, 165 167 struct page **pages)
+4
arch/sparc/mm/gup.c
··· 193 193 return 1; 194 194 } 195 195 196 + /* 197 + * Note a difference with get_user_pages_fast: this always returns the 198 + * number of pages pinned, 0 if no pages were pinned. 199 + */ 196 200 int __get_user_pages_fast(unsigned long start, int nr_pages, int write, 197 201 struct page **pages) 198 202 {
+3
arch/x86/Kconfig
··· 2008 2008 for kernel and initramfs as opposed to list of segments as 2009 2009 accepted by previous system call. 2010 2010 2011 + config ARCH_HAS_KEXEC_PURGATORY 2012 + def_bool KEXEC_FILE 2013 + 2011 2014 config KEXEC_VERIFY_SIG 2012 2015 bool "Verify kernel signature during kexec_file_load() syscall" 2013 2016 depends on KEXEC_FILE
+1 -1
arch/x86/include/asm/kexec-bzimage64.h
··· 2 2 #ifndef _ASM_KEXEC_BZIMAGE64_H 3 3 #define _ASM_KEXEC_BZIMAGE64_H 4 4 5 - extern struct kexec_file_ops kexec_bzImage64_ops; 5 + extern const struct kexec_file_ops kexec_bzImage64_ops; 6 6 7 7 #endif /* _ASM_KEXE_BZIMAGE64_H */
+62 -272
arch/x86/kernel/crash.c
··· 38 38 #include <asm/virtext.h> 39 39 #include <asm/intel_pt.h> 40 40 41 - /* Alignment required for elf header segment */ 42 - #define ELF_CORE_HEADER_ALIGN 4096 43 - 44 - /* This primarily represents number of split ranges due to exclusion */ 45 - #define CRASH_MAX_RANGES 16 46 - 47 - struct crash_mem_range { 48 - u64 start, end; 49 - }; 50 - 51 - struct crash_mem { 52 - unsigned int nr_ranges; 53 - struct crash_mem_range ranges[CRASH_MAX_RANGES]; 54 - }; 55 - 56 - /* Misc data about ram ranges needed to prepare elf headers */ 57 - struct crash_elf_data { 58 - struct kimage *image; 59 - /* 60 - * Total number of ram ranges we have after various adjustments for 61 - * crash reserved region, etc. 62 - */ 63 - unsigned int max_nr_ranges; 64 - 65 - /* Pointer to elf header */ 66 - void *ehdr; 67 - /* Pointer to next phdr */ 68 - void *bufp; 69 - struct crash_mem mem; 70 - }; 71 - 72 41 /* Used while preparing memory map entries for second kernel */ 73 42 struct crash_memmap_data { 74 43 struct boot_params *params; ··· 187 218 return 0; 188 219 } 189 220 190 - 191 221 /* Gather all the required information to prepare elf headers for ram regions */ 192 - static void fill_up_crash_elf_data(struct crash_elf_data *ced, 193 - struct kimage *image) 222 + static struct crash_mem *fill_up_crash_elf_data(void) 194 223 { 195 224 unsigned int nr_ranges = 0; 196 - 197 - ced->image = image; 225 + struct crash_mem *cmem; 198 226 199 227 walk_system_ram_res(0, -1, &nr_ranges, 200 228 get_nr_ram_ranges_callback); 229 + if (!nr_ranges) 230 + return NULL; 201 231 202 - ced->max_nr_ranges = nr_ranges; 232 + /* 233 + * Exclusion of crash region and/or crashk_low_res may cause 234 + * another range split. So add extra two slots here. 235 + */ 236 + nr_ranges += 2; 237 + cmem = vzalloc(sizeof(struct crash_mem) + 238 + sizeof(struct crash_mem_range) * nr_ranges); 239 + if (!cmem) 240 + return NULL; 203 241 204 - /* Exclusion of crash region could split memory ranges */ 205 - ced->max_nr_ranges++; 242 + cmem->max_nr_ranges = nr_ranges; 243 + cmem->nr_ranges = 0; 206 244 207 - /* If crashk_low_res is not 0, another range split possible */ 208 - if (crashk_low_res.end) 209 - ced->max_nr_ranges++; 210 - } 211 - 212 - static int exclude_mem_range(struct crash_mem *mem, 213 - unsigned long long mstart, unsigned long long mend) 214 - { 215 - int i, j; 216 - unsigned long long start, end; 217 - struct crash_mem_range temp_range = {0, 0}; 218 - 219 - for (i = 0; i < mem->nr_ranges; i++) { 220 - start = mem->ranges[i].start; 221 - end = mem->ranges[i].end; 222 - 223 - if (mstart > end || mend < start) 224 - continue; 225 - 226 - /* Truncate any area outside of range */ 227 - if (mstart < start) 228 - mstart = start; 229 - if (mend > end) 230 - mend = end; 231 - 232 - /* Found completely overlapping range */ 233 - if (mstart == start && mend == end) { 234 - mem->ranges[i].start = 0; 235 - mem->ranges[i].end = 0; 236 - if (i < mem->nr_ranges - 1) { 237 - /* Shift rest of the ranges to left */ 238 - for (j = i; j < mem->nr_ranges - 1; j++) { 239 - mem->ranges[j].start = 240 - mem->ranges[j+1].start; 241 - mem->ranges[j].end = 242 - mem->ranges[j+1].end; 243 - } 244 - } 245 - mem->nr_ranges--; 246 - return 0; 247 - } 248 - 249 - if (mstart > start && mend < end) { 250 - /* Split original range */ 251 - mem->ranges[i].end = mstart - 1; 252 - temp_range.start = mend + 1; 253 - temp_range.end = end; 254 - } else if (mstart != start) 255 - mem->ranges[i].end = mstart - 1; 256 - else 257 - mem->ranges[i].start = mend + 1; 258 - break; 259 - } 260 - 261 - /* If a split happend, add the split to array */ 262 - if (!temp_range.end) 263 - return 0; 264 - 265 - /* Split happened */ 266 - if (i == CRASH_MAX_RANGES - 1) { 267 - pr_err("Too many crash ranges after split\n"); 268 - return -ENOMEM; 269 - } 270 - 271 - /* Location where new range should go */ 272 - j = i + 1; 273 - if (j < mem->nr_ranges) { 274 - /* Move over all ranges one slot towards the end */ 275 - for (i = mem->nr_ranges - 1; i >= j; i--) 276 - mem->ranges[i + 1] = mem->ranges[i]; 277 - } 278 - 279 - mem->ranges[j].start = temp_range.start; 280 - mem->ranges[j].end = temp_range.end; 281 - mem->nr_ranges++; 282 - return 0; 245 + return cmem; 283 246 } 284 247 285 248 /* 286 249 * Look for any unwanted ranges between mstart, mend and remove them. This 287 - * might lead to split and split ranges are put in ced->mem.ranges[] array 250 + * might lead to split and split ranges are put in cmem->ranges[] array 288 251 */ 289 - static int elf_header_exclude_ranges(struct crash_elf_data *ced, 290 - unsigned long long mstart, unsigned long long mend) 252 + static int elf_header_exclude_ranges(struct crash_mem *cmem) 291 253 { 292 - struct crash_mem *cmem = &ced->mem; 293 254 int ret = 0; 294 255 295 - memset(cmem->ranges, 0, sizeof(cmem->ranges)); 296 - 297 - cmem->ranges[0].start = mstart; 298 - cmem->ranges[0].end = mend; 299 - cmem->nr_ranges = 1; 300 - 301 256 /* Exclude crashkernel region */ 302 - ret = exclude_mem_range(cmem, crashk_res.start, crashk_res.end); 257 + ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end); 303 258 if (ret) 304 259 return ret; 305 260 306 261 if (crashk_low_res.end) { 307 - ret = exclude_mem_range(cmem, crashk_low_res.start, crashk_low_res.end); 262 + ret = crash_exclude_mem_range(cmem, crashk_low_res.start, 263 + crashk_low_res.end); 308 264 if (ret) 309 265 return ret; 310 266 } ··· 239 345 240 346 static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg) 241 347 { 242 - struct crash_elf_data *ced = arg; 243 - Elf64_Ehdr *ehdr; 244 - Elf64_Phdr *phdr; 245 - unsigned long mstart, mend; 246 - struct kimage *image = ced->image; 247 - struct crash_mem *cmem; 248 - int ret, i; 348 + struct crash_mem *cmem = arg; 249 349 250 - ehdr = ced->ehdr; 350 + cmem->ranges[cmem->nr_ranges].start = res->start; 351 + cmem->ranges[cmem->nr_ranges].end = res->end; 352 + cmem->nr_ranges++; 251 353 252 - /* Exclude unwanted mem ranges */ 253 - ret = elf_header_exclude_ranges(ced, res->start, res->end); 254 - if (ret) 255 - return ret; 256 - 257 - /* Go through all the ranges in ced->mem.ranges[] and prepare phdr */ 258 - cmem = &ced->mem; 259 - 260 - for (i = 0; i < cmem->nr_ranges; i++) { 261 - mstart = cmem->ranges[i].start; 262 - mend = cmem->ranges[i].end; 263 - 264 - phdr = ced->bufp; 265 - ced->bufp += sizeof(Elf64_Phdr); 266 - 267 - phdr->p_type = PT_LOAD; 268 - phdr->p_flags = PF_R|PF_W|PF_X; 269 - phdr->p_offset = mstart; 270 - 271 - /* 272 - * If a range matches backup region, adjust offset to backup 273 - * segment. 274 - */ 275 - if (mstart == image->arch.backup_src_start && 276 - (mend - mstart + 1) == image->arch.backup_src_sz) 277 - phdr->p_offset = image->arch.backup_load_addr; 278 - 279 - phdr->p_paddr = mstart; 280 - phdr->p_vaddr = (unsigned long long) __va(mstart); 281 - phdr->p_filesz = phdr->p_memsz = mend - mstart + 1; 282 - phdr->p_align = 0; 283 - ehdr->e_phnum++; 284 - pr_debug("Crash PT_LOAD elf header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n", 285 - phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz, 286 - ehdr->e_phnum, phdr->p_offset); 287 - } 288 - 289 - return ret; 290 - } 291 - 292 - static int prepare_elf64_headers(struct crash_elf_data *ced, 293 - void **addr, unsigned long *sz) 294 - { 295 - Elf64_Ehdr *ehdr; 296 - Elf64_Phdr *phdr; 297 - unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz; 298 - unsigned char *buf, *bufp; 299 - unsigned int cpu; 300 - unsigned long long notes_addr; 301 - int ret; 302 - 303 - /* extra phdr for vmcoreinfo elf note */ 304 - nr_phdr = nr_cpus + 1; 305 - nr_phdr += ced->max_nr_ranges; 306 - 307 - /* 308 - * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping 309 - * area on x86_64 (ffffffff80000000 - ffffffffa0000000). 310 - * I think this is required by tools like gdb. So same physical 311 - * memory will be mapped in two elf headers. One will contain kernel 312 - * text virtual addresses and other will have __va(physical) addresses. 313 - */ 314 - 315 - nr_phdr++; 316 - elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr); 317 - elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN); 318 - 319 - buf = vzalloc(elf_sz); 320 - if (!buf) 321 - return -ENOMEM; 322 - 323 - bufp = buf; 324 - ehdr = (Elf64_Ehdr *)bufp; 325 - bufp += sizeof(Elf64_Ehdr); 326 - memcpy(ehdr->e_ident, ELFMAG, SELFMAG); 327 - ehdr->e_ident[EI_CLASS] = ELFCLASS64; 328 - ehdr->e_ident[EI_DATA] = ELFDATA2LSB; 329 - ehdr->e_ident[EI_VERSION] = EV_CURRENT; 330 - ehdr->e_ident[EI_OSABI] = ELF_OSABI; 331 - memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD); 332 - ehdr->e_type = ET_CORE; 333 - ehdr->e_machine = ELF_ARCH; 334 - ehdr->e_version = EV_CURRENT; 335 - ehdr->e_phoff = sizeof(Elf64_Ehdr); 336 - ehdr->e_ehsize = sizeof(Elf64_Ehdr); 337 - ehdr->e_phentsize = sizeof(Elf64_Phdr); 338 - 339 - /* Prepare one phdr of type PT_NOTE for each present cpu */ 340 - for_each_present_cpu(cpu) { 341 - phdr = (Elf64_Phdr *)bufp; 342 - bufp += sizeof(Elf64_Phdr); 343 - phdr->p_type = PT_NOTE; 344 - notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu)); 345 - phdr->p_offset = phdr->p_paddr = notes_addr; 346 - phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t); 347 - (ehdr->e_phnum)++; 348 - } 349 - 350 - /* Prepare one PT_NOTE header for vmcoreinfo */ 351 - phdr = (Elf64_Phdr *)bufp; 352 - bufp += sizeof(Elf64_Phdr); 353 - phdr->p_type = PT_NOTE; 354 - phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note(); 355 - phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE; 356 - (ehdr->e_phnum)++; 357 - 358 - #ifdef CONFIG_X86_64 359 - /* Prepare PT_LOAD type program header for kernel text region */ 360 - phdr = (Elf64_Phdr *)bufp; 361 - bufp += sizeof(Elf64_Phdr); 362 - phdr->p_type = PT_LOAD; 363 - phdr->p_flags = PF_R|PF_W|PF_X; 364 - phdr->p_vaddr = (Elf64_Addr)_text; 365 - phdr->p_filesz = phdr->p_memsz = _end - _text; 366 - phdr->p_offset = phdr->p_paddr = __pa_symbol(_text); 367 - (ehdr->e_phnum)++; 368 - #endif 369 - 370 - /* Prepare PT_LOAD headers for system ram chunks. */ 371 - ced->ehdr = ehdr; 372 - ced->bufp = bufp; 373 - ret = walk_system_ram_res(0, -1, ced, 374 - prepare_elf64_ram_headers_callback); 375 - if (ret < 0) 376 - return ret; 377 - 378 - *addr = buf; 379 - *sz = elf_sz; 380 354 return 0; 381 355 } 382 356 ··· 252 490 static int prepare_elf_headers(struct kimage *image, void **addr, 253 491 unsigned long *sz) 254 492 { 255 - struct crash_elf_data *ced; 256 - int ret; 493 + struct crash_mem *cmem; 494 + Elf64_Ehdr *ehdr; 495 + Elf64_Phdr *phdr; 496 + int ret, i; 257 497 258 - ced = kzalloc(sizeof(*ced), GFP_KERNEL); 259 - if (!ced) 498 + cmem = fill_up_crash_elf_data(); 499 + if (!cmem) 260 500 return -ENOMEM; 261 501 262 - fill_up_crash_elf_data(ced, image); 502 + ret = walk_system_ram_res(0, -1, cmem, 503 + prepare_elf64_ram_headers_callback); 504 + if (ret) 505 + goto out; 506 + 507 + /* Exclude unwanted mem ranges */ 508 + ret = elf_header_exclude_ranges(cmem); 509 + if (ret) 510 + goto out; 263 511 264 512 /* By default prepare 64bit headers */ 265 - ret = prepare_elf64_headers(ced, addr, sz); 266 - kfree(ced); 513 + ret = crash_prepare_elf64_headers(cmem, 514 + IS_ENABLED(CONFIG_X86_64), addr, sz); 515 + if (ret) 516 + goto out; 517 + 518 + /* 519 + * If a range matches backup region, adjust offset to backup 520 + * segment. 521 + */ 522 + ehdr = (Elf64_Ehdr *)*addr; 523 + phdr = (Elf64_Phdr *)(ehdr + 1); 524 + for (i = 0; i < ehdr->e_phnum; phdr++, i++) 525 + if (phdr->p_type == PT_LOAD && 526 + phdr->p_paddr == image->arch.backup_src_start && 527 + phdr->p_memsz == image->arch.backup_src_sz) { 528 + phdr->p_offset = image->arch.backup_load_addr; 529 + break; 530 + } 531 + out: 532 + vfree(cmem); 267 533 return ret; 268 534 } 269 535 ··· 337 547 /* Exclude Backup region */ 338 548 start = image->arch.backup_load_addr; 339 549 end = start + image->arch.backup_src_sz - 1; 340 - ret = exclude_mem_range(cmem, start, end); 550 + ret = crash_exclude_mem_range(cmem, start, end); 341 551 if (ret) 342 552 return ret; 343 553 344 554 /* Exclude elf header region */ 345 555 start = image->arch.elf_load_addr; 346 556 end = start + image->arch.elf_headers_sz - 1; 347 - return exclude_mem_range(cmem, start, end); 557 + return crash_exclude_mem_range(cmem, start, end); 348 558 } 349 559 350 560 /* Prepare memory map for crash dump kernel */
+5 -5
arch/x86/kernel/kexec-bzimage64.c
··· 334 334 unsigned long setup_header_size, params_cmdline_sz; 335 335 struct boot_params *params; 336 336 unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr; 337 - unsigned long purgatory_load_addr; 338 337 struct bzimage64_data *ldata; 339 338 struct kexec_entry64_regs regs64; 340 339 void *stack; ··· 341 342 unsigned int efi_map_offset, efi_map_sz, efi_setup_data_offset; 342 343 struct kexec_buf kbuf = { .image = image, .buf_max = ULONG_MAX, 343 344 .top_down = true }; 345 + struct kexec_buf pbuf = { .image = image, .buf_min = MIN_PURGATORY_ADDR, 346 + .buf_max = ULONG_MAX, .top_down = true }; 344 347 345 348 header = (struct setup_header *)(kernel + setup_hdr_offset); 346 349 setup_sects = header->setup_sects; ··· 380 379 * Load purgatory. For 64bit entry point, purgatory code can be 381 380 * anywhere. 382 381 */ 383 - ret = kexec_load_purgatory(image, MIN_PURGATORY_ADDR, ULONG_MAX, 1, 384 - &purgatory_load_addr); 382 + ret = kexec_load_purgatory(image, &pbuf); 385 383 if (ret) { 386 384 pr_err("Loading purgatory failed\n"); 387 385 return ERR_PTR(ret); 388 386 } 389 387 390 - pr_debug("Loaded purgatory at 0x%lx\n", purgatory_load_addr); 388 + pr_debug("Loaded purgatory at 0x%lx\n", pbuf.mem); 391 389 392 390 393 391 /* ··· 538 538 } 539 539 #endif 540 540 541 - struct kexec_file_ops kexec_bzImage64_ops = { 541 + const struct kexec_file_ops kexec_bzImage64_ops = { 542 542 .probe = bzImage64_probe, 543 543 .load = bzImage64_load, 544 544 .cleanup = bzImage64_cleanup,
+28 -83
arch/x86/kernel/machine_kexec_64.c
··· 30 30 #include <asm/set_memory.h> 31 31 32 32 #ifdef CONFIG_KEXEC_FILE 33 - static struct kexec_file_ops *kexec_file_loaders[] = { 33 + const struct kexec_file_ops * const kexec_file_loaders[] = { 34 34 &kexec_bzImage64_ops, 35 + NULL 35 36 }; 36 37 #endif 37 38 ··· 365 364 /* arch-dependent functionality related to kexec file-based syscall */ 366 365 367 366 #ifdef CONFIG_KEXEC_FILE 368 - int arch_kexec_kernel_image_probe(struct kimage *image, void *buf, 369 - unsigned long buf_len) 370 - { 371 - int i, ret = -ENOEXEC; 372 - struct kexec_file_ops *fops; 373 - 374 - for (i = 0; i < ARRAY_SIZE(kexec_file_loaders); i++) { 375 - fops = kexec_file_loaders[i]; 376 - if (!fops || !fops->probe) 377 - continue; 378 - 379 - ret = fops->probe(buf, buf_len); 380 - if (!ret) { 381 - image->fops = fops; 382 - return ret; 383 - } 384 - } 385 - 386 - return ret; 387 - } 388 - 389 367 void *arch_kexec_kernel_image_load(struct kimage *image) 390 368 { 391 369 vfree(image->arch.elf_headers); ··· 379 399 image->cmdline_buf_len); 380 400 } 381 401 382 - int arch_kimage_file_post_load_cleanup(struct kimage *image) 383 - { 384 - if (!image->fops || !image->fops->cleanup) 385 - return 0; 386 - 387 - return image->fops->cleanup(image->image_loader_data); 388 - } 389 - 390 - #ifdef CONFIG_KEXEC_VERIFY_SIG 391 - int arch_kexec_kernel_verify_sig(struct kimage *image, void *kernel, 392 - unsigned long kernel_len) 393 - { 394 - if (!image->fops || !image->fops->verify_sig) { 395 - pr_debug("kernel loader does not support signature verification."); 396 - return -EKEYREJECTED; 397 - } 398 - 399 - return image->fops->verify_sig(kernel, kernel_len); 400 - } 401 - #endif 402 - 403 402 /* 404 403 * Apply purgatory relocations. 405 404 * 406 - * ehdr: Pointer to elf headers 407 - * sechdrs: Pointer to section headers. 408 - * relsec: section index of SHT_RELA section. 405 + * @pi: Purgatory to be relocated. 406 + * @section: Section relocations applying to. 407 + * @relsec: Section containing RELAs. 408 + * @symtabsec: Corresponding symtab. 409 409 * 410 410 * TODO: Some of the code belongs to generic code. Move that in kexec.c. 411 411 */ 412 - int arch_kexec_apply_relocations_add(const Elf64_Ehdr *ehdr, 413 - Elf64_Shdr *sechdrs, unsigned int relsec) 412 + int arch_kexec_apply_relocations_add(struct purgatory_info *pi, 413 + Elf_Shdr *section, const Elf_Shdr *relsec, 414 + const Elf_Shdr *symtabsec) 414 415 { 415 416 unsigned int i; 416 417 Elf64_Rela *rel; 417 418 Elf64_Sym *sym; 418 419 void *location; 419 - Elf64_Shdr *section, *symtabsec; 420 420 unsigned long address, sec_base, value; 421 421 const char *strtab, *name, *shstrtab; 422 + const Elf_Shdr *sechdrs; 422 423 423 - /* 424 - * ->sh_offset has been modified to keep the pointer to section 425 - * contents in memory 426 - */ 427 - rel = (void *)sechdrs[relsec].sh_offset; 424 + /* String & section header string table */ 425 + sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff; 426 + strtab = (char *)pi->ehdr + sechdrs[symtabsec->sh_link].sh_offset; 427 + shstrtab = (char *)pi->ehdr + sechdrs[pi->ehdr->e_shstrndx].sh_offset; 428 428 429 - /* Section to which relocations apply */ 430 - section = &sechdrs[sechdrs[relsec].sh_info]; 429 + rel = (void *)pi->ehdr + relsec->sh_offset; 431 430 432 - pr_debug("Applying relocate section %u to %u\n", relsec, 433 - sechdrs[relsec].sh_info); 431 + pr_debug("Applying relocate section %s to %u\n", 432 + shstrtab + relsec->sh_name, relsec->sh_info); 434 433 435 - /* Associated symbol table */ 436 - symtabsec = &sechdrs[sechdrs[relsec].sh_link]; 437 - 438 - /* String table */ 439 - if (symtabsec->sh_link >= ehdr->e_shnum) { 440 - /* Invalid strtab section number */ 441 - pr_err("Invalid string table section index %d\n", 442 - symtabsec->sh_link); 443 - return -ENOEXEC; 444 - } 445 - 446 - strtab = (char *)sechdrs[symtabsec->sh_link].sh_offset; 447 - 448 - /* section header string table */ 449 - shstrtab = (char *)sechdrs[ehdr->e_shstrndx].sh_offset; 450 - 451 - for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { 434 + for (i = 0; i < relsec->sh_size / sizeof(*rel); i++) { 452 435 453 436 /* 454 437 * rel[i].r_offset contains byte offset from beginning 455 438 * of section to the storage unit affected. 456 439 * 457 - * This is location to update (->sh_offset). This is temporary 458 - * buffer where section is currently loaded. This will finally 459 - * be loaded to a different address later, pointed to by 440 + * This is location to update. This is temporary buffer 441 + * where section is currently loaded. This will finally be 442 + * loaded to a different address later, pointed to by 460 443 * ->sh_addr. kexec takes care of moving it 461 444 * (kexec_load_segment()). 462 445 */ 463 - location = (void *)(section->sh_offset + rel[i].r_offset); 446 + location = pi->purgatory_buf; 447 + location += section->sh_offset; 448 + location += rel[i].r_offset; 464 449 465 450 /* Final address of the location */ 466 451 address = section->sh_addr + rel[i].r_offset; ··· 436 491 * to apply. ELF64_R_SYM() and ELF64_R_TYPE() macros get 437 492 * these respectively. 438 493 */ 439 - sym = (Elf64_Sym *)symtabsec->sh_offset + 440 - ELF64_R_SYM(rel[i].r_info); 494 + sym = (void *)pi->ehdr + symtabsec->sh_offset; 495 + sym += ELF64_R_SYM(rel[i].r_info); 441 496 442 497 if (sym->st_name) 443 498 name = strtab + sym->st_name; ··· 460 515 461 516 if (sym->st_shndx == SHN_ABS) 462 517 sec_base = 0; 463 - else if (sym->st_shndx >= ehdr->e_shnum) { 518 + else if (sym->st_shndx >= pi->ehdr->e_shnum) { 464 519 pr_err("Invalid section %d for symbol %s\n", 465 520 sym->st_shndx, name); 466 521 return -ENOEXEC; 467 522 } else 468 - sec_base = sechdrs[sym->st_shndx].sh_addr; 523 + sec_base = pi->sechdrs[sym->st_shndx].sh_addr; 469 524 470 525 value = sym->st_value; 471 526 value += sec_base;
+3
arch/x86/purgatory/Makefile
··· 6 6 targets += $(purgatory-y) 7 7 PURGATORY_OBJS = $(addprefix $(obj)/,$(purgatory-y)) 8 8 9 + $(obj)/sha256.o: $(srctree)/lib/sha256.c 10 + $(call if_changed_rule,cc_o_c) 11 + 9 12 LDFLAGS_purgatory.ro := -e purgatory_start -r --no-undefined -nostdlib -z nodefaultlib 10 13 targets += purgatory.ro 11 14
+1 -1
arch/x86/purgatory/purgatory.c
··· 11 11 */ 12 12 13 13 #include <linux/bug.h> 14 + #include <linux/sha256.h> 14 15 #include <asm/purgatory.h> 15 16 16 - #include "sha256.h" 17 17 #include "../boot/string.h" 18 18 19 19 unsigned long purgatory_backup_dest __section(.kexec-purgatory);
+2 -2
arch/x86/purgatory/sha256.c lib/sha256.c
··· 16 16 */ 17 17 18 18 #include <linux/bitops.h> 19 + #include <linux/sha256.h> 20 + #include <linux/string.h> 19 21 #include <asm/byteorder.h> 20 - #include "sha256.h" 21 - #include "../boot/string.h" 22 22 23 23 static inline u32 Ch(u32 x, u32 y, u32 z) 24 24 {
+10 -1
arch/x86/purgatory/sha256.h include/linux/sha256.h
··· 13 13 #include <linux/types.h> 14 14 #include <crypto/sha.h> 15 15 16 + /* 17 + * Stand-alone implementation of the SHA256 algorithm. It is designed to 18 + * have as little dependencies as possible so it can be used in the 19 + * kexec_file purgatory. In other cases you should use the implementation in 20 + * crypto/. 21 + * 22 + * For details see lib/sha256.c 23 + */ 24 + 16 25 extern int sha256_init(struct sha256_state *sctx); 17 26 extern int sha256_update(struct sha256_state *sctx, const u8 *input, 18 - unsigned int length); 27 + unsigned int length); 19 28 extern int sha256_final(struct sha256_state *sctx, u8 *hash); 20 29 21 30 #endif /* SHA256_H */
+12
arch/x86/purgatory/string.c
··· 10 10 * Version 2. See the file COPYING for more details. 11 11 */ 12 12 13 + #include <linux/types.h> 14 + 13 15 #include "../boot/string.c" 16 + 17 + void *memcpy(void *dst, const void *src, size_t len) 18 + { 19 + return __builtin_memcpy(dst, src, len); 20 + } 21 + 22 + void *memset(void *dst, int c, size_t len) 23 + { 24 + return __builtin_memset(dst, c, len); 25 + }
+22 -1
fs/proc/generic.c
··· 15 15 #include <linux/stat.h> 16 16 #include <linux/mm.h> 17 17 #include <linux/module.h> 18 + #include <linux/namei.h> 18 19 #include <linux/slab.h> 19 20 #include <linux/printk.h> 20 21 #include <linux/mount.h> ··· 218 217 ida_simple_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST); 219 218 } 220 219 220 + static int proc_misc_d_revalidate(struct dentry *dentry, unsigned int flags) 221 + { 222 + if (flags & LOOKUP_RCU) 223 + return -ECHILD; 224 + 225 + if (atomic_read(&PDE(d_inode(dentry))->in_use) < 0) 226 + return 0; /* revalidate */ 227 + return 1; 228 + } 229 + 230 + static int proc_misc_d_delete(const struct dentry *dentry) 231 + { 232 + return atomic_read(&PDE(d_inode(dentry))->in_use) < 0; 233 + } 234 + 235 + static const struct dentry_operations proc_misc_dentry_ops = { 236 + .d_revalidate = proc_misc_d_revalidate, 237 + .d_delete = proc_misc_d_delete, 238 + }; 239 + 221 240 /* 222 241 * Don't create negative dentries here, return -ENOENT by hand 223 242 * instead. ··· 255 234 inode = proc_get_inode(dir->i_sb, de); 256 235 if (!inode) 257 236 return ERR_PTR(-ENOMEM); 258 - d_set_d_op(dentry, &simple_dentry_operations); 237 + d_set_d_op(dentry, &proc_misc_dentry_ops); 259 238 d_add(dentry, inode); 260 239 return NULL; 261 240 }
+53 -28
include/linux/kexec.h
··· 99 99 100 100 #ifdef CONFIG_KEXEC_FILE 101 101 struct purgatory_info { 102 - /* Pointer to elf header of read only purgatory */ 103 - Elf_Ehdr *ehdr; 104 - 105 - /* Pointer to purgatory sechdrs which are modifiable */ 102 + /* 103 + * Pointer to elf header at the beginning of kexec_purgatory. 104 + * Note: kexec_purgatory is read only 105 + */ 106 + const Elf_Ehdr *ehdr; 107 + /* 108 + * Temporary, modifiable buffer for sechdrs used for relocation. 109 + * This memory can be freed post image load. 110 + */ 106 111 Elf_Shdr *sechdrs; 107 112 /* 108 - * Temporary buffer location where purgatory is loaded and relocated 109 - * This memory can be freed post image load 113 + * Temporary, modifiable buffer for stripped purgatory used for 114 + * relocation. This memory can be freed post image load. 110 115 */ 111 116 void *purgatory_buf; 112 - 113 - /* Address where purgatory is finally loaded and is executed from */ 114 - unsigned long purgatory_load_addr; 115 117 }; 118 + 119 + struct kimage; 116 120 117 121 typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size); 118 122 typedef void *(kexec_load_t)(struct kimage *image, char *kernel_buf, ··· 138 134 kexec_verify_sig_t *verify_sig; 139 135 #endif 140 136 }; 137 + 138 + extern const struct kexec_file_ops * const kexec_file_loaders[]; 139 + 140 + int kexec_image_probe_default(struct kimage *image, void *buf, 141 + unsigned long buf_len); 141 142 142 143 /** 143 144 * struct kexec_buf - parameters for finding a place for a buffer in memory ··· 168 159 bool top_down; 169 160 }; 170 161 162 + int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf); 163 + int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name, 164 + void *buf, unsigned int size, 165 + bool get_value); 166 + void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name); 167 + 168 + int __weak arch_kexec_apply_relocations_add(struct purgatory_info *pi, 169 + Elf_Shdr *section, 170 + const Elf_Shdr *relsec, 171 + const Elf_Shdr *symtab); 172 + int __weak arch_kexec_apply_relocations(struct purgatory_info *pi, 173 + Elf_Shdr *section, 174 + const Elf_Shdr *relsec, 175 + const Elf_Shdr *symtab); 176 + 171 177 int __weak arch_kexec_walk_mem(struct kexec_buf *kbuf, 172 178 int (*func)(struct resource *, void *)); 173 179 extern int kexec_add_buffer(struct kexec_buf *kbuf); 174 180 int kexec_locate_mem_hole(struct kexec_buf *kbuf); 181 + 182 + /* Alignment required for elf header segment */ 183 + #define ELF_CORE_HEADER_ALIGN 4096 184 + 185 + struct crash_mem_range { 186 + u64 start, end; 187 + }; 188 + 189 + struct crash_mem { 190 + unsigned int max_nr_ranges; 191 + unsigned int nr_ranges; 192 + struct crash_mem_range ranges[0]; 193 + }; 194 + 195 + extern int crash_exclude_mem_range(struct crash_mem *mem, 196 + unsigned long long mstart, 197 + unsigned long long mend); 198 + extern int crash_prepare_elf64_headers(struct crash_mem *mem, int kernel_map, 199 + void **addr, unsigned long *sz); 175 200 #endif /* CONFIG_KEXEC_FILE */ 176 201 177 202 struct kimage { ··· 252 209 unsigned long cmdline_buf_len; 253 210 254 211 /* File operations provided by image loader */ 255 - struct kexec_file_ops *fops; 212 + const struct kexec_file_ops *fops; 256 213 257 214 /* Image loader handling the kernel can store a pointer here */ 258 215 void *image_loader_data; ··· 269 226 extern int kernel_kexec(void); 270 227 extern struct page *kimage_alloc_control_pages(struct kimage *image, 271 228 unsigned int order); 272 - extern int kexec_load_purgatory(struct kimage *image, unsigned long min, 273 - unsigned long max, int top_down, 274 - unsigned long *load_addr); 275 - extern int kexec_purgatory_get_set_symbol(struct kimage *image, 276 - const char *name, void *buf, 277 - unsigned int size, bool get_value); 278 - extern void *kexec_purgatory_get_symbol_addr(struct kimage *image, 279 - const char *name); 280 229 extern void __crash_kexec(struct pt_regs *); 281 230 extern void crash_kexec(struct pt_regs *); 282 231 int kexec_should_crash(struct task_struct *); ··· 308 273 size_t crash_get_memory_size(void); 309 274 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end); 310 275 311 - int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf, 312 - unsigned long buf_len); 313 - void * __weak arch_kexec_kernel_image_load(struct kimage *image); 314 - int __weak arch_kimage_file_post_load_cleanup(struct kimage *image); 315 - int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf, 316 - unsigned long buf_len); 317 - int __weak arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, 318 - Elf_Shdr *sechdrs, unsigned int relsec); 319 - int __weak arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, 320 - unsigned int relsec); 321 276 void arch_kexec_protect_crashkres(void); 322 277 void arch_kexec_unprotect_crashkres(void); 323 278
+20 -3
ipc/shm.c
··· 225 225 if (IS_ERR(shp)) 226 226 return PTR_ERR(shp); 227 227 228 + if (shp->shm_file != sfd->file) { 229 + /* ID was reused */ 230 + shm_unlock(shp); 231 + return -EINVAL; 232 + } 233 + 228 234 shp->shm_atim = ktime_get_real_seconds(); 229 235 ipc_update_pid(&shp->shm_lprid, task_tgid(current)); 230 236 shp->shm_nattch++; ··· 461 455 int ret; 462 456 463 457 /* 464 - * In case of remap_file_pages() emulation, the file can represent 465 - * removed IPC ID: propogate shm_lock() error to caller. 458 + * In case of remap_file_pages() emulation, the file can represent an 459 + * IPC ID that was removed, and possibly even reused by another shm 460 + * segment already. Propagate this case as an error to caller. 466 461 */ 467 462 ret = __shm_open(vma); 468 463 if (ret) ··· 487 480 struct shm_file_data *sfd = shm_file_data(file); 488 481 489 482 put_ipc_ns(sfd->ns); 483 + fput(sfd->file); 490 484 shm_file_data(file) = NULL; 491 485 kfree(sfd); 492 486 return 0; ··· 1453 1445 file->f_mapping = shp->shm_file->f_mapping; 1454 1446 sfd->id = shp->shm_perm.id; 1455 1447 sfd->ns = get_ipc_ns(ns); 1456 - sfd->file = shp->shm_file; 1448 + /* 1449 + * We need to take a reference to the real shm file to prevent the 1450 + * pointer from becoming stale in cases where the lifetime of the outer 1451 + * file extends beyond that of the shm segment. It's not usually 1452 + * possible, but it can happen during remap_file_pages() emulation as 1453 + * that unmaps the memory, then does ->mmap() via file reference only. 1454 + * We'll deny the ->mmap() if the shm segment was since removed, but to 1455 + * detect shm ID reuse we need to compare the file pointers. 1456 + */ 1457 + sfd->file = get_file(shp->shm_file); 1457 1458 sfd->vm_ops = NULL; 1458 1459 1459 1460 err = security_mmap_file(file, prot, flags);
+1
kernel/crash_core.c
··· 454 454 VMCOREINFO_NUMBER(PG_lru); 455 455 VMCOREINFO_NUMBER(PG_private); 456 456 VMCOREINFO_NUMBER(PG_swapcache); 457 + VMCOREINFO_NUMBER(PG_swapbacked); 457 458 VMCOREINFO_NUMBER(PG_slab); 458 459 #ifdef CONFIG_MEMORY_FAILURE 459 460 VMCOREINFO_NUMBER(PG_hwpoison);
+415 -210
kernel/kexec_file.c
··· 22 22 #include <linux/ima.h> 23 23 #include <crypto/hash.h> 24 24 #include <crypto/sha.h> 25 + #include <linux/elf.h> 26 + #include <linux/elfcore.h> 27 + #include <linux/kernel.h> 28 + #include <linux/kexec.h> 29 + #include <linux/slab.h> 25 30 #include <linux/syscalls.h> 26 31 #include <linux/vmalloc.h> 27 32 #include "kexec_internal.h" 28 33 29 34 static int kexec_calculate_store_digests(struct kimage *image); 30 35 36 + /* 37 + * Currently this is the only default function that is exported as some 38 + * architectures need it to do additional handlings. 39 + * In the future, other default functions may be exported too if required. 40 + */ 41 + int kexec_image_probe_default(struct kimage *image, void *buf, 42 + unsigned long buf_len) 43 + { 44 + const struct kexec_file_ops * const *fops; 45 + int ret = -ENOEXEC; 46 + 47 + for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) { 48 + ret = (*fops)->probe(buf, buf_len); 49 + if (!ret) { 50 + image->fops = *fops; 51 + return ret; 52 + } 53 + } 54 + 55 + return ret; 56 + } 57 + 31 58 /* Architectures can provide this probe function */ 32 59 int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf, 33 60 unsigned long buf_len) 34 61 { 35 - return -ENOEXEC; 62 + return kexec_image_probe_default(image, buf, buf_len); 63 + } 64 + 65 + static void *kexec_image_load_default(struct kimage *image) 66 + { 67 + if (!image->fops || !image->fops->load) 68 + return ERR_PTR(-ENOEXEC); 69 + 70 + return image->fops->load(image, image->kernel_buf, 71 + image->kernel_buf_len, image->initrd_buf, 72 + image->initrd_buf_len, image->cmdline_buf, 73 + image->cmdline_buf_len); 36 74 } 37 75 38 76 void * __weak arch_kexec_kernel_image_load(struct kimage *image) 39 77 { 40 - return ERR_PTR(-ENOEXEC); 78 + return kexec_image_load_default(image); 79 + } 80 + 81 + static int kexec_image_post_load_cleanup_default(struct kimage *image) 82 + { 83 + if (!image->fops || !image->fops->cleanup) 84 + return 0; 85 + 86 + return image->fops->cleanup(image->image_loader_data); 41 87 } 42 88 43 89 int __weak arch_kimage_file_post_load_cleanup(struct kimage *image) 44 90 { 45 - return -EINVAL; 91 + return kexec_image_post_load_cleanup_default(image); 46 92 } 47 93 48 94 #ifdef CONFIG_KEXEC_VERIFY_SIG 95 + static int kexec_image_verify_sig_default(struct kimage *image, void *buf, 96 + unsigned long buf_len) 97 + { 98 + if (!image->fops || !image->fops->verify_sig) { 99 + pr_debug("kernel loader does not support signature verification.\n"); 100 + return -EKEYREJECTED; 101 + } 102 + 103 + return image->fops->verify_sig(buf, buf_len); 104 + } 105 + 49 106 int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf, 50 107 unsigned long buf_len) 51 108 { 52 - return -EKEYREJECTED; 109 + return kexec_image_verify_sig_default(image, buf, buf_len); 53 110 } 54 111 #endif 55 112 56 - /* Apply relocations of type RELA */ 113 + /* 114 + * arch_kexec_apply_relocations_add - apply relocations of type RELA 115 + * @pi: Purgatory to be relocated. 116 + * @section: Section relocations applying to. 117 + * @relsec: Section containing RELAs. 118 + * @symtab: Corresponding symtab. 119 + * 120 + * Return: 0 on success, negative errno on error. 121 + */ 57 122 int __weak 58 - arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, 59 - unsigned int relsec) 123 + arch_kexec_apply_relocations_add(struct purgatory_info *pi, Elf_Shdr *section, 124 + const Elf_Shdr *relsec, const Elf_Shdr *symtab) 60 125 { 61 126 pr_err("RELA relocation unsupported.\n"); 62 127 return -ENOEXEC; 63 128 } 64 129 65 - /* Apply relocations of type REL */ 130 + /* 131 + * arch_kexec_apply_relocations - apply relocations of type REL 132 + * @pi: Purgatory to be relocated. 133 + * @section: Section relocations applying to. 134 + * @relsec: Section containing RELs. 135 + * @symtab: Corresponding symtab. 136 + * 137 + * Return: 0 on success, negative errno on error. 138 + */ 66 139 int __weak 67 - arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, 68 - unsigned int relsec) 140 + arch_kexec_apply_relocations(struct purgatory_info *pi, Elf_Shdr *section, 141 + const Elf_Shdr *relsec, const Elf_Shdr *symtab) 69 142 { 70 143 pr_err("REL relocation unsupported.\n"); 71 144 return -ENOEXEC; ··· 605 532 struct kexec_sha_region *sha_regions; 606 533 struct purgatory_info *pi = &image->purgatory_info; 607 534 535 + if (!IS_ENABLED(CONFIG_ARCH_HAS_KEXEC_PURGATORY)) 536 + return 0; 537 + 608 538 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT); 609 539 zero_buf_sz = PAGE_SIZE; 610 540 ··· 709 633 return ret; 710 634 } 711 635 712 - /* Actually load purgatory. Lot of code taken from kexec-tools */ 713 - static int __kexec_load_purgatory(struct kimage *image, unsigned long min, 714 - unsigned long max, int top_down) 636 + #ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY 637 + /* 638 + * kexec_purgatory_setup_kbuf - prepare buffer to load purgatory. 639 + * @pi: Purgatory to be loaded. 640 + * @kbuf: Buffer to setup. 641 + * 642 + * Allocates the memory needed for the buffer. Caller is responsible to free 643 + * the memory after use. 644 + * 645 + * Return: 0 on success, negative errno on error. 646 + */ 647 + static int kexec_purgatory_setup_kbuf(struct purgatory_info *pi, 648 + struct kexec_buf *kbuf) 715 649 { 716 - struct purgatory_info *pi = &image->purgatory_info; 717 - unsigned long align, bss_align, bss_sz, bss_pad; 718 - unsigned long entry, load_addr, curr_load_addr, bss_addr, offset; 719 - unsigned char *buf_addr, *src; 720 - int i, ret = 0, entry_sidx = -1; 721 - const Elf_Shdr *sechdrs_c; 722 - Elf_Shdr *sechdrs = NULL; 723 - struct kexec_buf kbuf = { .image = image, .bufsz = 0, .buf_align = 1, 724 - .buf_min = min, .buf_max = max, 725 - .top_down = top_down }; 650 + const Elf_Shdr *sechdrs; 651 + unsigned long bss_align; 652 + unsigned long bss_sz; 653 + unsigned long align; 654 + int i, ret; 726 655 727 - /* 728 - * sechdrs_c points to section headers in purgatory and are read 729 - * only. No modifications allowed. 730 - */ 731 - sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff; 732 - 733 - /* 734 - * We can not modify sechdrs_c[] and its fields. It is read only. 735 - * Copy it over to a local copy where one can store some temporary 736 - * data and free it at the end. We need to modify ->sh_addr and 737 - * ->sh_offset fields to keep track of permanent and temporary 738 - * locations of sections. 739 - */ 740 - sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr)); 741 - if (!sechdrs) 742 - return -ENOMEM; 743 - 744 - memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr)); 745 - 746 - /* 747 - * We seem to have multiple copies of sections. First copy is which 748 - * is embedded in kernel in read only section. Some of these sections 749 - * will be copied to a temporary buffer and relocated. And these 750 - * sections will finally be copied to their final destination at 751 - * segment load time. 752 - * 753 - * Use ->sh_offset to reflect section address in memory. It will 754 - * point to original read only copy if section is not allocatable. 755 - * Otherwise it will point to temporary copy which will be relocated. 756 - * 757 - * Use ->sh_addr to contain final address of the section where it 758 - * will go during execution time. 759 - */ 760 - for (i = 0; i < pi->ehdr->e_shnum; i++) { 761 - if (sechdrs[i].sh_type == SHT_NOBITS) 762 - continue; 763 - 764 - sechdrs[i].sh_offset = (unsigned long)pi->ehdr + 765 - sechdrs[i].sh_offset; 766 - } 767 - 768 - /* 769 - * Identify entry point section and make entry relative to section 770 - * start. 771 - */ 772 - entry = pi->ehdr->e_entry; 773 - for (i = 0; i < pi->ehdr->e_shnum; i++) { 774 - if (!(sechdrs[i].sh_flags & SHF_ALLOC)) 775 - continue; 776 - 777 - if (!(sechdrs[i].sh_flags & SHF_EXECINSTR)) 778 - continue; 779 - 780 - /* Make entry section relative */ 781 - if (sechdrs[i].sh_addr <= pi->ehdr->e_entry && 782 - ((sechdrs[i].sh_addr + sechdrs[i].sh_size) > 783 - pi->ehdr->e_entry)) { 784 - entry_sidx = i; 785 - entry -= sechdrs[i].sh_addr; 786 - break; 787 - } 788 - } 789 - 790 - /* Determine how much memory is needed to load relocatable object. */ 791 - bss_align = 1; 792 - bss_sz = 0; 656 + sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff; 657 + kbuf->buf_align = bss_align = 1; 658 + kbuf->bufsz = bss_sz = 0; 793 659 794 660 for (i = 0; i < pi->ehdr->e_shnum; i++) { 795 661 if (!(sechdrs[i].sh_flags & SHF_ALLOC)) ··· 739 721 740 722 align = sechdrs[i].sh_addralign; 741 723 if (sechdrs[i].sh_type != SHT_NOBITS) { 742 - if (kbuf.buf_align < align) 743 - kbuf.buf_align = align; 744 - kbuf.bufsz = ALIGN(kbuf.bufsz, align); 745 - kbuf.bufsz += sechdrs[i].sh_size; 724 + if (kbuf->buf_align < align) 725 + kbuf->buf_align = align; 726 + kbuf->bufsz = ALIGN(kbuf->bufsz, align); 727 + kbuf->bufsz += sechdrs[i].sh_size; 746 728 } else { 747 - /* bss section */ 748 729 if (bss_align < align) 749 730 bss_align = align; 750 731 bss_sz = ALIGN(bss_sz, align); 751 732 bss_sz += sechdrs[i].sh_size; 752 733 } 753 734 } 735 + kbuf->bufsz = ALIGN(kbuf->bufsz, bss_align); 736 + kbuf->memsz = kbuf->bufsz + bss_sz; 737 + if (kbuf->buf_align < bss_align) 738 + kbuf->buf_align = bss_align; 754 739 755 - /* Determine the bss padding required to align bss properly */ 756 - bss_pad = 0; 757 - if (kbuf.bufsz & (bss_align - 1)) 758 - bss_pad = bss_align - (kbuf.bufsz & (bss_align - 1)); 740 + kbuf->buffer = vzalloc(kbuf->bufsz); 741 + if (!kbuf->buffer) 742 + return -ENOMEM; 743 + pi->purgatory_buf = kbuf->buffer; 759 744 760 - kbuf.memsz = kbuf.bufsz + bss_pad + bss_sz; 761 - 762 - /* Allocate buffer for purgatory */ 763 - kbuf.buffer = vzalloc(kbuf.bufsz); 764 - if (!kbuf.buffer) { 765 - ret = -ENOMEM; 766 - goto out; 767 - } 768 - 769 - if (kbuf.buf_align < bss_align) 770 - kbuf.buf_align = bss_align; 771 - 772 - /* Add buffer to segment list */ 773 - ret = kexec_add_buffer(&kbuf); 745 + ret = kexec_add_buffer(kbuf); 774 746 if (ret) 775 747 goto out; 776 - pi->purgatory_load_addr = kbuf.mem; 777 748 778 - /* Load SHF_ALLOC sections */ 779 - buf_addr = kbuf.buffer; 780 - load_addr = curr_load_addr = pi->purgatory_load_addr; 781 - bss_addr = load_addr + kbuf.bufsz + bss_pad; 749 + return 0; 750 + out: 751 + vfree(pi->purgatory_buf); 752 + pi->purgatory_buf = NULL; 753 + return ret; 754 + } 755 + 756 + /* 757 + * kexec_purgatory_setup_sechdrs - prepares the pi->sechdrs buffer. 758 + * @pi: Purgatory to be loaded. 759 + * @kbuf: Buffer prepared to store purgatory. 760 + * 761 + * Allocates the memory needed for the buffer. Caller is responsible to free 762 + * the memory after use. 763 + * 764 + * Return: 0 on success, negative errno on error. 765 + */ 766 + static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi, 767 + struct kexec_buf *kbuf) 768 + { 769 + unsigned long bss_addr; 770 + unsigned long offset; 771 + Elf_Shdr *sechdrs; 772 + int i; 773 + 774 + /* 775 + * The section headers in kexec_purgatory are read-only. In order to 776 + * have them modifiable make a temporary copy. 777 + */ 778 + sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr)); 779 + if (!sechdrs) 780 + return -ENOMEM; 781 + memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff, 782 + pi->ehdr->e_shnum * sizeof(Elf_Shdr)); 783 + pi->sechdrs = sechdrs; 784 + 785 + offset = 0; 786 + bss_addr = kbuf->mem + kbuf->bufsz; 787 + kbuf->image->start = pi->ehdr->e_entry; 782 788 783 789 for (i = 0; i < pi->ehdr->e_shnum; i++) { 790 + unsigned long align; 791 + void *src, *dst; 792 + 784 793 if (!(sechdrs[i].sh_flags & SHF_ALLOC)) 785 794 continue; 786 795 787 796 align = sechdrs[i].sh_addralign; 788 - if (sechdrs[i].sh_type != SHT_NOBITS) { 789 - curr_load_addr = ALIGN(curr_load_addr, align); 790 - offset = curr_load_addr - load_addr; 791 - /* We already modifed ->sh_offset to keep src addr */ 792 - src = (char *) sechdrs[i].sh_offset; 793 - memcpy(buf_addr + offset, src, sechdrs[i].sh_size); 794 - 795 - /* Store load address and source address of section */ 796 - sechdrs[i].sh_addr = curr_load_addr; 797 - 798 - /* 799 - * This section got copied to temporary buffer. Update 800 - * ->sh_offset accordingly. 801 - */ 802 - sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset); 803 - 804 - /* Advance to the next address */ 805 - curr_load_addr += sechdrs[i].sh_size; 806 - } else { 797 + if (sechdrs[i].sh_type == SHT_NOBITS) { 807 798 bss_addr = ALIGN(bss_addr, align); 808 799 sechdrs[i].sh_addr = bss_addr; 809 800 bss_addr += sechdrs[i].sh_size; 801 + continue; 810 802 } 803 + 804 + offset = ALIGN(offset, align); 805 + if (sechdrs[i].sh_flags & SHF_EXECINSTR && 806 + pi->ehdr->e_entry >= sechdrs[i].sh_addr && 807 + pi->ehdr->e_entry < (sechdrs[i].sh_addr 808 + + sechdrs[i].sh_size)) { 809 + kbuf->image->start -= sechdrs[i].sh_addr; 810 + kbuf->image->start += kbuf->mem + offset; 811 + } 812 + 813 + src = (void *)pi->ehdr + sechdrs[i].sh_offset; 814 + dst = pi->purgatory_buf + offset; 815 + memcpy(dst, src, sechdrs[i].sh_size); 816 + 817 + sechdrs[i].sh_addr = kbuf->mem + offset; 818 + sechdrs[i].sh_offset = offset; 819 + offset += sechdrs[i].sh_size; 811 820 } 812 821 813 - /* Update entry point based on load address of text section */ 814 - if (entry_sidx >= 0) 815 - entry += sechdrs[entry_sidx].sh_addr; 816 - 817 - /* Make kernel jump to purgatory after shutdown */ 818 - image->start = entry; 819 - 820 - /* Used later to get/set symbol values */ 821 - pi->sechdrs = sechdrs; 822 - 823 - /* 824 - * Used later to identify which section is purgatory and skip it 825 - * from checksumming. 826 - */ 827 - pi->purgatory_buf = kbuf.buffer; 828 - return ret; 829 - out: 830 - vfree(sechdrs); 831 - vfree(kbuf.buffer); 832 - return ret; 822 + return 0; 833 823 } 834 824 835 825 static int kexec_apply_relocations(struct kimage *image) 836 826 { 837 827 int i, ret; 838 828 struct purgatory_info *pi = &image->purgatory_info; 839 - Elf_Shdr *sechdrs = pi->sechdrs; 829 + const Elf_Shdr *sechdrs; 840 830 841 - /* Apply relocations */ 831 + sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff; 832 + 842 833 for (i = 0; i < pi->ehdr->e_shnum; i++) { 843 - Elf_Shdr *section, *symtab; 834 + const Elf_Shdr *relsec; 835 + const Elf_Shdr *symtab; 836 + Elf_Shdr *section; 844 837 845 - if (sechdrs[i].sh_type != SHT_RELA && 846 - sechdrs[i].sh_type != SHT_REL) 838 + relsec = sechdrs + i; 839 + 840 + if (relsec->sh_type != SHT_RELA && 841 + relsec->sh_type != SHT_REL) 847 842 continue; 848 843 849 844 /* ··· 865 834 * symbol table. And ->sh_info contains section header 866 835 * index of section to which relocations apply. 867 836 */ 868 - if (sechdrs[i].sh_info >= pi->ehdr->e_shnum || 869 - sechdrs[i].sh_link >= pi->ehdr->e_shnum) 837 + if (relsec->sh_info >= pi->ehdr->e_shnum || 838 + relsec->sh_link >= pi->ehdr->e_shnum) 870 839 return -ENOEXEC; 871 840 872 - section = &sechdrs[sechdrs[i].sh_info]; 873 - symtab = &sechdrs[sechdrs[i].sh_link]; 841 + section = pi->sechdrs + relsec->sh_info; 842 + symtab = sechdrs + relsec->sh_link; 874 843 875 844 if (!(section->sh_flags & SHF_ALLOC)) 876 845 continue; ··· 887 856 * Respective architecture needs to provide support for applying 888 857 * relocations of type SHT_RELA/SHT_REL. 889 858 */ 890 - if (sechdrs[i].sh_type == SHT_RELA) 891 - ret = arch_kexec_apply_relocations_add(pi->ehdr, 892 - sechdrs, i); 893 - else if (sechdrs[i].sh_type == SHT_REL) 894 - ret = arch_kexec_apply_relocations(pi->ehdr, 895 - sechdrs, i); 859 + if (relsec->sh_type == SHT_RELA) 860 + ret = arch_kexec_apply_relocations_add(pi, section, 861 + relsec, symtab); 862 + else if (relsec->sh_type == SHT_REL) 863 + ret = arch_kexec_apply_relocations(pi, section, 864 + relsec, symtab); 896 865 if (ret) 897 866 return ret; 898 867 } ··· 900 869 return 0; 901 870 } 902 871 903 - /* Load relocatable purgatory object and relocate it appropriately */ 904 - int kexec_load_purgatory(struct kimage *image, unsigned long min, 905 - unsigned long max, int top_down, 906 - unsigned long *load_addr) 872 + /* 873 + * kexec_load_purgatory - Load and relocate the purgatory object. 874 + * @image: Image to add the purgatory to. 875 + * @kbuf: Memory parameters to use. 876 + * 877 + * Allocates the memory needed for image->purgatory_info.sechdrs and 878 + * image->purgatory_info.purgatory_buf/kbuf->buffer. Caller is responsible 879 + * to free the memory after use. 880 + * 881 + * Return: 0 on success, negative errno on error. 882 + */ 883 + int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf) 907 884 { 908 885 struct purgatory_info *pi = &image->purgatory_info; 909 886 int ret; ··· 919 880 if (kexec_purgatory_size <= 0) 920 881 return -EINVAL; 921 882 922 - if (kexec_purgatory_size < sizeof(Elf_Ehdr)) 923 - return -ENOEXEC; 883 + pi->ehdr = (const Elf_Ehdr *)kexec_purgatory; 924 884 925 - pi->ehdr = (Elf_Ehdr *)kexec_purgatory; 926 - 927 - if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0 928 - || pi->ehdr->e_type != ET_REL 929 - || !elf_check_arch(pi->ehdr) 930 - || pi->ehdr->e_shentsize != sizeof(Elf_Shdr)) 931 - return -ENOEXEC; 932 - 933 - if (pi->ehdr->e_shoff >= kexec_purgatory_size 934 - || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) > 935 - kexec_purgatory_size - pi->ehdr->e_shoff)) 936 - return -ENOEXEC; 937 - 938 - ret = __kexec_load_purgatory(image, min, max, top_down); 885 + ret = kexec_purgatory_setup_kbuf(pi, kbuf); 939 886 if (ret) 940 887 return ret; 888 + 889 + ret = kexec_purgatory_setup_sechdrs(pi, kbuf); 890 + if (ret) 891 + goto out_free_kbuf; 941 892 942 893 ret = kexec_apply_relocations(image); 943 894 if (ret) 944 895 goto out; 945 896 946 - *load_addr = pi->purgatory_load_addr; 947 897 return 0; 948 898 out: 949 899 vfree(pi->sechdrs); 950 900 pi->sechdrs = NULL; 951 - 901 + out_free_kbuf: 952 902 vfree(pi->purgatory_buf); 953 903 pi->purgatory_buf = NULL; 954 904 return ret; 955 905 } 956 906 957 - static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi, 958 - const char *name) 907 + /* 908 + * kexec_purgatory_find_symbol - find a symbol in the purgatory 909 + * @pi: Purgatory to search in. 910 + * @name: Name of the symbol. 911 + * 912 + * Return: pointer to symbol in read-only symtab on success, NULL on error. 913 + */ 914 + static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi, 915 + const char *name) 959 916 { 960 - Elf_Sym *syms; 961 - Elf_Shdr *sechdrs; 962 - Elf_Ehdr *ehdr; 963 - int i, k; 917 + const Elf_Shdr *sechdrs; 918 + const Elf_Ehdr *ehdr; 919 + const Elf_Sym *syms; 964 920 const char *strtab; 921 + int i, k; 965 922 966 - if (!pi->sechdrs || !pi->ehdr) 923 + if (!pi->ehdr) 967 924 return NULL; 968 925 969 - sechdrs = pi->sechdrs; 970 926 ehdr = pi->ehdr; 927 + sechdrs = (void *)ehdr + ehdr->e_shoff; 971 928 972 929 for (i = 0; i < ehdr->e_shnum; i++) { 973 930 if (sechdrs[i].sh_type != SHT_SYMTAB) ··· 972 937 if (sechdrs[i].sh_link >= ehdr->e_shnum) 973 938 /* Invalid strtab section number */ 974 939 continue; 975 - strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset; 976 - syms = (Elf_Sym *)sechdrs[i].sh_offset; 940 + strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset; 941 + syms = (void *)ehdr + sechdrs[i].sh_offset; 977 942 978 943 /* Go through symbols for a match */ 979 944 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) { ··· 1001 966 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name) 1002 967 { 1003 968 struct purgatory_info *pi = &image->purgatory_info; 1004 - Elf_Sym *sym; 969 + const Elf_Sym *sym; 1005 970 Elf_Shdr *sechdr; 1006 971 1007 972 sym = kexec_purgatory_find_symbol(pi, name); ··· 1024 989 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name, 1025 990 void *buf, unsigned int size, bool get_value) 1026 991 { 1027 - Elf_Sym *sym; 1028 - Elf_Shdr *sechdrs; 1029 992 struct purgatory_info *pi = &image->purgatory_info; 993 + const Elf_Sym *sym; 994 + Elf_Shdr *sec; 1030 995 char *sym_buf; 1031 996 1032 997 sym = kexec_purgatory_find_symbol(pi, name); ··· 1039 1004 return -EINVAL; 1040 1005 } 1041 1006 1042 - sechdrs = pi->sechdrs; 1007 + sec = pi->sechdrs + sym->st_shndx; 1043 1008 1044 - if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) { 1009 + if (sec->sh_type == SHT_NOBITS) { 1045 1010 pr_err("symbol %s is in a bss section. Cannot %s\n", name, 1046 1011 get_value ? "get" : "set"); 1047 1012 return -EINVAL; 1048 1013 } 1049 1014 1050 - sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset + 1051 - sym->st_value; 1015 + sym_buf = (char *)pi->purgatory_buf + sec->sh_offset + sym->st_value; 1052 1016 1053 1017 if (get_value) 1054 1018 memcpy((void *)buf, sym_buf, size); 1055 1019 else 1056 1020 memcpy((void *)sym_buf, buf, size); 1057 1021 1022 + return 0; 1023 + } 1024 + #endif /* CONFIG_ARCH_HAS_KEXEC_PURGATORY */ 1025 + 1026 + int crash_exclude_mem_range(struct crash_mem *mem, 1027 + unsigned long long mstart, unsigned long long mend) 1028 + { 1029 + int i, j; 1030 + unsigned long long start, end; 1031 + struct crash_mem_range temp_range = {0, 0}; 1032 + 1033 + for (i = 0; i < mem->nr_ranges; i++) { 1034 + start = mem->ranges[i].start; 1035 + end = mem->ranges[i].end; 1036 + 1037 + if (mstart > end || mend < start) 1038 + continue; 1039 + 1040 + /* Truncate any area outside of range */ 1041 + if (mstart < start) 1042 + mstart = start; 1043 + if (mend > end) 1044 + mend = end; 1045 + 1046 + /* Found completely overlapping range */ 1047 + if (mstart == start && mend == end) { 1048 + mem->ranges[i].start = 0; 1049 + mem->ranges[i].end = 0; 1050 + if (i < mem->nr_ranges - 1) { 1051 + /* Shift rest of the ranges to left */ 1052 + for (j = i; j < mem->nr_ranges - 1; j++) { 1053 + mem->ranges[j].start = 1054 + mem->ranges[j+1].start; 1055 + mem->ranges[j].end = 1056 + mem->ranges[j+1].end; 1057 + } 1058 + } 1059 + mem->nr_ranges--; 1060 + return 0; 1061 + } 1062 + 1063 + if (mstart > start && mend < end) { 1064 + /* Split original range */ 1065 + mem->ranges[i].end = mstart - 1; 1066 + temp_range.start = mend + 1; 1067 + temp_range.end = end; 1068 + } else if (mstart != start) 1069 + mem->ranges[i].end = mstart - 1; 1070 + else 1071 + mem->ranges[i].start = mend + 1; 1072 + break; 1073 + } 1074 + 1075 + /* If a split happened, add the split to array */ 1076 + if (!temp_range.end) 1077 + return 0; 1078 + 1079 + /* Split happened */ 1080 + if (i == mem->max_nr_ranges - 1) 1081 + return -ENOMEM; 1082 + 1083 + /* Location where new range should go */ 1084 + j = i + 1; 1085 + if (j < mem->nr_ranges) { 1086 + /* Move over all ranges one slot towards the end */ 1087 + for (i = mem->nr_ranges - 1; i >= j; i--) 1088 + mem->ranges[i + 1] = mem->ranges[i]; 1089 + } 1090 + 1091 + mem->ranges[j].start = temp_range.start; 1092 + mem->ranges[j].end = temp_range.end; 1093 + mem->nr_ranges++; 1094 + return 0; 1095 + } 1096 + 1097 + int crash_prepare_elf64_headers(struct crash_mem *mem, int kernel_map, 1098 + void **addr, unsigned long *sz) 1099 + { 1100 + Elf64_Ehdr *ehdr; 1101 + Elf64_Phdr *phdr; 1102 + unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz; 1103 + unsigned char *buf; 1104 + unsigned int cpu, i; 1105 + unsigned long long notes_addr; 1106 + unsigned long mstart, mend; 1107 + 1108 + /* extra phdr for vmcoreinfo elf note */ 1109 + nr_phdr = nr_cpus + 1; 1110 + nr_phdr += mem->nr_ranges; 1111 + 1112 + /* 1113 + * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping 1114 + * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64). 1115 + * I think this is required by tools like gdb. So same physical 1116 + * memory will be mapped in two elf headers. One will contain kernel 1117 + * text virtual addresses and other will have __va(physical) addresses. 1118 + */ 1119 + 1120 + nr_phdr++; 1121 + elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr); 1122 + elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN); 1123 + 1124 + buf = vzalloc(elf_sz); 1125 + if (!buf) 1126 + return -ENOMEM; 1127 + 1128 + ehdr = (Elf64_Ehdr *)buf; 1129 + phdr = (Elf64_Phdr *)(ehdr + 1); 1130 + memcpy(ehdr->e_ident, ELFMAG, SELFMAG); 1131 + ehdr->e_ident[EI_CLASS] = ELFCLASS64; 1132 + ehdr->e_ident[EI_DATA] = ELFDATA2LSB; 1133 + ehdr->e_ident[EI_VERSION] = EV_CURRENT; 1134 + ehdr->e_ident[EI_OSABI] = ELF_OSABI; 1135 + memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD); 1136 + ehdr->e_type = ET_CORE; 1137 + ehdr->e_machine = ELF_ARCH; 1138 + ehdr->e_version = EV_CURRENT; 1139 + ehdr->e_phoff = sizeof(Elf64_Ehdr); 1140 + ehdr->e_ehsize = sizeof(Elf64_Ehdr); 1141 + ehdr->e_phentsize = sizeof(Elf64_Phdr); 1142 + 1143 + /* Prepare one phdr of type PT_NOTE for each present cpu */ 1144 + for_each_present_cpu(cpu) { 1145 + phdr->p_type = PT_NOTE; 1146 + notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu)); 1147 + phdr->p_offset = phdr->p_paddr = notes_addr; 1148 + phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t); 1149 + (ehdr->e_phnum)++; 1150 + phdr++; 1151 + } 1152 + 1153 + /* Prepare one PT_NOTE header for vmcoreinfo */ 1154 + phdr->p_type = PT_NOTE; 1155 + phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note(); 1156 + phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE; 1157 + (ehdr->e_phnum)++; 1158 + phdr++; 1159 + 1160 + /* Prepare PT_LOAD type program header for kernel text region */ 1161 + if (kernel_map) { 1162 + phdr->p_type = PT_LOAD; 1163 + phdr->p_flags = PF_R|PF_W|PF_X; 1164 + phdr->p_vaddr = (Elf64_Addr)_text; 1165 + phdr->p_filesz = phdr->p_memsz = _end - _text; 1166 + phdr->p_offset = phdr->p_paddr = __pa_symbol(_text); 1167 + ehdr->e_phnum++; 1168 + phdr++; 1169 + } 1170 + 1171 + /* Go through all the ranges in mem->ranges[] and prepare phdr */ 1172 + for (i = 0; i < mem->nr_ranges; i++) { 1173 + mstart = mem->ranges[i].start; 1174 + mend = mem->ranges[i].end; 1175 + 1176 + phdr->p_type = PT_LOAD; 1177 + phdr->p_flags = PF_R|PF_W|PF_X; 1178 + phdr->p_offset = mstart; 1179 + 1180 + phdr->p_paddr = mstart; 1181 + phdr->p_vaddr = (unsigned long long) __va(mstart); 1182 + phdr->p_filesz = phdr->p_memsz = mend - mstart + 1; 1183 + phdr->p_align = 0; 1184 + ehdr->e_phnum++; 1185 + phdr++; 1186 + pr_debug("Crash PT_LOAD elf header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n", 1187 + phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz, 1188 + ehdr->e_phnum, phdr->p_offset); 1189 + } 1190 + 1191 + *addr = buf; 1192 + *sz = elf_sz; 1058 1193 return 0; 1059 1194 }
+2 -1
kernel/resource.c
··· 651 651 alloc.start = constraint->alignf(constraint->alignf_data, &avail, 652 652 size, constraint->align); 653 653 alloc.end = alloc.start + size - 1; 654 - if (resource_contains(&avail, &alloc)) { 654 + if (alloc.start <= alloc.end && 655 + resource_contains(&avail, &alloc)) { 655 656 new->start = alloc.start; 656 657 new->end = alloc.end; 657 658 return 0;
+5 -1
mm/filemap.c
··· 2719 2719 sb_end_pagefault(inode->i_sb); 2720 2720 return ret; 2721 2721 } 2722 - EXPORT_SYMBOL(filemap_page_mkwrite); 2723 2722 2724 2723 const struct vm_operations_struct generic_file_vm_ops = { 2725 2724 .fault = filemap_fault, ··· 2749 2750 return generic_file_mmap(file, vma); 2750 2751 } 2751 2752 #else 2753 + int filemap_page_mkwrite(struct vm_fault *vmf) 2754 + { 2755 + return -ENOSYS; 2756 + } 2752 2757 int generic_file_mmap(struct file * file, struct vm_area_struct * vma) 2753 2758 { 2754 2759 return -ENOSYS; ··· 2763 2760 } 2764 2761 #endif /* CONFIG_MMU */ 2765 2762 2763 + EXPORT_SYMBOL(filemap_page_mkwrite); 2766 2764 EXPORT_SYMBOL(generic_file_mmap); 2767 2765 EXPORT_SYMBOL(generic_file_readonly_mmap); 2768 2766
+7 -2
mm/gup.c
··· 1740 1740 1741 1741 /* 1742 1742 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to 1743 - * the regular GUP. It will only return non-negative values. 1743 + * the regular GUP. 1744 + * Note a difference with get_user_pages_fast: this always returns the 1745 + * number of pages pinned, 0 if no pages were pinned. 1744 1746 */ 1745 1747 int __get_user_pages_fast(unsigned long start, int nr_pages, int write, 1746 1748 struct page **pages) ··· 1808 1806 len = (unsigned long) nr_pages << PAGE_SHIFT; 1809 1807 end = start + len; 1810 1808 1809 + if (nr_pages <= 0) 1810 + return 0; 1811 + 1811 1812 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ, 1812 1813 (void __user *)start, len))) 1813 - return 0; 1814 + return -EFAULT; 1814 1815 1815 1816 if (gup_fast_permitted(start, nr_pages, write)) { 1816 1817 local_irq_disable();
+3 -1
mm/gup_benchmark.c
··· 23 23 struct page **pages; 24 24 25 25 nr_pages = gup->size / PAGE_SIZE; 26 - pages = kvmalloc(sizeof(void *) * nr_pages, GFP_KERNEL); 26 + pages = kvzalloc(sizeof(void *) * nr_pages, GFP_KERNEL); 27 27 if (!pages) 28 28 return -ENOMEM; 29 29 ··· 41 41 } 42 42 43 43 nr = get_user_pages_fast(addr, nr, gup->flags & 1, pages + i); 44 + if (nr <= 0) 45 + break; 44 46 i += nr; 45 47 } 46 48 end_time = ktime_get();
+2 -1
mm/slab.c
··· 4086 4086 next_reap_node(); 4087 4087 out: 4088 4088 /* Set up the next iteration */ 4089 - schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_AC)); 4089 + schedule_delayed_work_on(smp_processor_id(), work, 4090 + round_jiffies_relative(REAPTIMEOUT_AC)); 4090 4091 } 4091 4092 4092 4093 void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
+4 -2
mm/util.c
··· 297 297 /* 298 298 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall 299 299 * back to the regular GUP. 300 - * If the architecture not support this function, simply return with no 301 - * page pinned 300 + * Note a difference with get_user_pages_fast: this always returns the 301 + * number of pages pinned, 0 if no pages were pinned. 302 + * If the architecture does not support this function, simply return with no 303 + * pages pinned. 302 304 */ 303 305 int __weak __get_user_pages_fast(unsigned long start, 304 306 int nr_pages, int write, struct page **pages)