Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit ac52776

Browse files
Alexander Gordeevgregkh
Alexander Gordeev
authored andcommitted
fs/proc/kcore.c: allow translation of physical memory addresses
commit 3d5854d upstream. When /proc/kcore is read an attempt to read the first two pages results in HW-specific page swap on s390 and another (so called prefix) pages are accessed instead. That leads to a wrong read. Allow architecture-specific translation of memory addresses using kc_xlate_dev_mem_ptr() and kc_unxlate_dev_mem_ptr() callbacks similarily to /dev/mem xlate_dev_mem_ptr() and unxlate_dev_mem_ptr() callbacks. That way an architecture can deal with specific physical memory ranges. Re-use the existing /dev/mem callback implementation on s390, which handles the described prefix pages swapping correctly. For other architectures the default callback is basically NOP. It is expected the condition (vaddr == __va(__pa(vaddr))) always holds true for KCORE_RAM memory type. Link: https://lkml.kernel.org/r/20240930122119.1651546-1-agordeev@linux.ibm.com Signed-off-by: Alexander Gordeev <agordeev@linux.ibm.com> Suggested-by: Heiko Carstens <hca@linux.ibm.com> Cc: Vasily Gorbik <gor@linux.ibm.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent cda5423 commit ac52776

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

arch/s390/include/asm/io.h

+2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
#include <asm/pci_io.h>
1717

1818
#define xlate_dev_mem_ptr xlate_dev_mem_ptr
19+
#define kc_xlate_dev_mem_ptr xlate_dev_mem_ptr
1920
void *xlate_dev_mem_ptr(phys_addr_t phys);
2021
#define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
22+
#define kc_unxlate_dev_mem_ptr unxlate_dev_mem_ptr
2123
void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr);
2224

2325
#define IO_SPACE_LIMIT 0

fs/proc/kcore.c

+34-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ static struct proc_dir_entry *proc_root_kcore;
5050
#define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
5151
#endif
5252

53+
#ifndef kc_xlate_dev_mem_ptr
54+
#define kc_xlate_dev_mem_ptr kc_xlate_dev_mem_ptr
55+
static inline void *kc_xlate_dev_mem_ptr(phys_addr_t phys)
56+
{
57+
return __va(phys);
58+
}
59+
#endif
60+
#ifndef kc_unxlate_dev_mem_ptr
61+
#define kc_unxlate_dev_mem_ptr kc_unxlate_dev_mem_ptr
62+
static inline void kc_unxlate_dev_mem_ptr(phys_addr_t phys, void *virt)
63+
{
64+
}
65+
#endif
66+
5367
static LIST_HEAD(kclist_head);
5468
static DECLARE_RWSEM(kclist_lock);
5569
static int kcore_need_update = 1;
@@ -471,6 +485,8 @@ static ssize_t read_kcore_iter(struct kiocb *iocb, struct iov_iter *iter)
471485
while (buflen) {
472486
struct page *page;
473487
unsigned long pfn;
488+
phys_addr_t phys;
489+
void *__start;
474490

475491
/*
476492
* If this is the first iteration or the address is not within
@@ -537,7 +553,8 @@ static ssize_t read_kcore_iter(struct kiocb *iocb, struct iov_iter *iter)
537553
}
538554
break;
539555
case KCORE_RAM:
540-
pfn = __pa(start) >> PAGE_SHIFT;
556+
phys = __pa(start);
557+
pfn = phys >> PAGE_SHIFT;
541558
page = pfn_to_online_page(pfn);
542559

543560
/*
@@ -557,13 +574,28 @@ static ssize_t read_kcore_iter(struct kiocb *iocb, struct iov_iter *iter)
557574
fallthrough;
558575
case KCORE_VMEMMAP:
559576
case KCORE_TEXT:
577+
if (m->type == KCORE_RAM) {
578+
__start = kc_xlate_dev_mem_ptr(phys);
579+
if (!__start) {
580+
ret = -ENOMEM;
581+
if (iov_iter_zero(tsz, iter) != tsz)
582+
ret = -EFAULT;
583+
goto out;
584+
}
585+
} else {
586+
__start = (void *)start;
587+
}
588+
560589
/*
561590
* Sadly we must use a bounce buffer here to be able to
562591
* make use of copy_from_kernel_nofault(), as these
563592
* memory regions might not always be mapped on all
564593
* architectures.
565594
*/
566-
if (copy_from_kernel_nofault(buf, (void *)start, tsz)) {
595+
ret = copy_from_kernel_nofault(buf, __start, tsz);
596+
if (m->type == KCORE_RAM)
597+
kc_unxlate_dev_mem_ptr(phys, __start);
598+
if (ret) {
567599
if (iov_iter_zero(tsz, iter) != tsz) {
568600
ret = -EFAULT;
569601
goto out;

0 commit comments

Comments
 (0)