Skip to content

Commit 831b624

Browse files
byang-intelkees
authored andcommitted
pstore: Fix incorrect persistent ram buffer mapping
persistent_ram_vmap() returns the page start vaddr. persistent_ram_iomap() supports non-page-aligned mapping. persistent_ram_buffer_map() always adds offset-in-page to the vaddr returned from these two functions, which causes incorrect mapping of non-page-aligned persistent ram buffer. By default ftrace_size is 4096 and max_ftrace_cnt is nr_cpu_ids. Without this patch, the zone_sz in ramoops_init_przs() is 4096/nr_cpu_ids which might not be page aligned. If the offset-in-page > 2048, the vaddr will be in next page. If the next page is not mapped, it will cause kernel panic: [ 0.074231] BUG: unable to handle kernel paging request at ffffa19e0081b000 ... [ 0.075000] RIP: 0010:persistent_ram_new+0x1f8/0x39f ... [ 0.075000] Call Trace: [ 0.075000] ramoops_init_przs.part.10.constprop.15+0x105/0x260 [ 0.075000] ramoops_probe+0x232/0x3a0 [ 0.075000] platform_drv_probe+0x3e/0xa0 [ 0.075000] driver_probe_device+0x2cd/0x400 [ 0.075000] __driver_attach+0xe4/0x110 [ 0.075000] ? driver_probe_device+0x400/0x400 [ 0.075000] bus_for_each_dev+0x70/0xa0 [ 0.075000] driver_attach+0x1e/0x20 [ 0.075000] bus_add_driver+0x159/0x230 [ 0.075000] ? do_early_param+0x95/0x95 [ 0.075000] driver_register+0x70/0xc0 [ 0.075000] ? init_pstore_fs+0x4d/0x4d [ 0.075000] __platform_driver_register+0x36/0x40 [ 0.075000] ramoops_init+0x12f/0x131 [ 0.075000] do_one_initcall+0x4d/0x12c [ 0.075000] ? do_early_param+0x95/0x95 [ 0.075000] kernel_init_freeable+0x19b/0x222 [ 0.075000] ? rest_init+0xbb/0xbb [ 0.075000] kernel_init+0xe/0xfc [ 0.075000] ret_from_fork+0x3a/0x50 Signed-off-by: Bin Yang <[email protected]> [kees: add comments describing the mapping differences, updated commit log] Fixes: 24c3d2f ("staging: android: persistent_ram: Make it possible to use memory outside of bootmem") Cc: [email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 5736184 commit 831b624

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

fs/pstore/ram_core.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,12 @@ static void *persistent_ram_vmap(phys_addr_t start, size_t size,
429429
vaddr = vmap(pages, page_count, VM_MAP, prot);
430430
kfree(pages);
431431

432-
return vaddr;
432+
/*
433+
* Since vmap() uses page granularity, we must add the offset
434+
* into the page here, to get the byte granularity address
435+
* into the mapping to represent the actual "start" location.
436+
*/
437+
return vaddr + offset_in_page(start);
433438
}
434439

435440
static void *persistent_ram_iomap(phys_addr_t start, size_t size,
@@ -448,6 +453,11 @@ static void *persistent_ram_iomap(phys_addr_t start, size_t size,
448453
else
449454
va = ioremap_wc(start, size);
450455

456+
/*
457+
* Since request_mem_region() and ioremap() are byte-granularity
458+
* there is no need handle anything special like we do when the
459+
* vmap() case in persistent_ram_vmap() above.
460+
*/
451461
return va;
452462
}
453463

@@ -468,7 +478,7 @@ static int persistent_ram_buffer_map(phys_addr_t start, phys_addr_t size,
468478
return -ENOMEM;
469479
}
470480

471-
prz->buffer = prz->vaddr + offset_in_page(start);
481+
prz->buffer = prz->vaddr;
472482
prz->buffer_size = size - sizeof(struct persistent_ram_buffer);
473483

474484
return 0;
@@ -515,7 +525,8 @@ void persistent_ram_free(struct persistent_ram_zone *prz)
515525

516526
if (prz->vaddr) {
517527
if (pfn_valid(prz->paddr >> PAGE_SHIFT)) {
518-
vunmap(prz->vaddr);
528+
/* We must vunmap() at page-granularity. */
529+
vunmap(prz->vaddr - offset_in_page(prz->paddr));
519530
} else {
520531
iounmap(prz->vaddr);
521532
release_mem_region(prz->paddr, prz->size);

0 commit comments

Comments
 (0)