Skip to content

Commit 8d6b029

Browse files
committed
Merge tag 's390-6.10-3' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
Pull s390 fixes from Alexander Gordeev: - Do not create PT_LOAD program header for the kenel image when the virtual memory informaton in OS_INFO data is not available. That fixes stand-alone dump failures against kernels that do not provide the virtual memory informaton - Add KVM s390 shared zeropage selftest * tag 's390-6.10-3' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux: KVM: s390x: selftests: Add shared zeropage test s390/crash: Do not use VM info if os_info does not have it
2 parents 8d43786 + 01c51a3 commit 8d6b029

File tree

3 files changed

+142
-24
lines changed

3 files changed

+142
-24
lines changed

arch/s390/kernel/crash_dump.c

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ static void *nt_final(void *ptr)
451451
/*
452452
* Initialize ELF header (new kernel)
453453
*/
454-
static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
454+
static void *ehdr_init(Elf64_Ehdr *ehdr, int phdr_count)
455455
{
456456
memset(ehdr, 0, sizeof(*ehdr));
457457
memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
@@ -465,11 +465,8 @@ static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
465465
ehdr->e_phoff = sizeof(Elf64_Ehdr);
466466
ehdr->e_ehsize = sizeof(Elf64_Ehdr);
467467
ehdr->e_phentsize = sizeof(Elf64_Phdr);
468-
/*
469-
* Number of memory chunk PT_LOAD program headers plus one kernel
470-
* image PT_LOAD program header plus one PT_NOTE program header.
471-
*/
472-
ehdr->e_phnum = mem_chunk_cnt + 1 + 1;
468+
/* Number of PT_LOAD program headers plus PT_NOTE program header */
469+
ehdr->e_phnum = phdr_count + 1;
473470
return ehdr + 1;
474471
}
475472

@@ -503,12 +500,14 @@ static int get_mem_chunk_cnt(void)
503500
/*
504501
* Initialize ELF loads (new kernel)
505502
*/
506-
static void loads_init(Elf64_Phdr *phdr)
503+
static void loads_init(Elf64_Phdr *phdr, bool os_info_has_vm)
507504
{
508-
unsigned long old_identity_base = os_info_old_value(OS_INFO_IDENTITY_BASE);
505+
unsigned long old_identity_base = 0;
509506
phys_addr_t start, end;
510507
u64 idx;
511508

509+
if (os_info_has_vm)
510+
old_identity_base = os_info_old_value(OS_INFO_IDENTITY_BASE);
512511
for_each_physmem_range(idx, &oldmem_type, &start, &end) {
513512
phdr->p_type = PT_LOAD;
514513
phdr->p_vaddr = old_identity_base + start;
@@ -522,6 +521,11 @@ static void loads_init(Elf64_Phdr *phdr)
522521
}
523522
}
524523

524+
static bool os_info_has_vm(void)
525+
{
526+
return os_info_old_value(OS_INFO_KASLR_OFFSET);
527+
}
528+
525529
/*
526530
* Prepare PT_LOAD type program header for kernel image region
527531
*/
@@ -566,7 +570,7 @@ static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
566570
return ptr;
567571
}
568572

569-
static size_t get_elfcorehdr_size(int mem_chunk_cnt)
573+
static size_t get_elfcorehdr_size(int phdr_count)
570574
{
571575
size_t size;
572576

@@ -581,10 +585,8 @@ static size_t get_elfcorehdr_size(int mem_chunk_cnt)
581585
size += nt_vmcoreinfo_size();
582586
/* nt_final */
583587
size += sizeof(Elf64_Nhdr);
584-
/* PT_LOAD type program header for kernel text region */
585-
size += sizeof(Elf64_Phdr);
586588
/* PT_LOADS */
587-
size += mem_chunk_cnt * sizeof(Elf64_Phdr);
589+
size += phdr_count * sizeof(Elf64_Phdr);
588590

589591
return size;
590592
}
@@ -595,8 +597,8 @@ static size_t get_elfcorehdr_size(int mem_chunk_cnt)
595597
int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
596598
{
597599
Elf64_Phdr *phdr_notes, *phdr_loads, *phdr_text;
600+
int mem_chunk_cnt, phdr_text_cnt;
598601
size_t alloc_size;
599-
int mem_chunk_cnt;
600602
void *ptr, *hdr;
601603
u64 hdr_off;
602604

@@ -615,34 +617,38 @@ int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
615617
}
616618

617619
mem_chunk_cnt = get_mem_chunk_cnt();
620+
phdr_text_cnt = os_info_has_vm() ? 1 : 0;
618621

619-
alloc_size = get_elfcorehdr_size(mem_chunk_cnt);
622+
alloc_size = get_elfcorehdr_size(mem_chunk_cnt + phdr_text_cnt);
620623

621624
hdr = kzalloc(alloc_size, GFP_KERNEL);
622625

623-
/* Without elfcorehdr /proc/vmcore cannot be created. Thus creating
626+
/*
627+
* Without elfcorehdr /proc/vmcore cannot be created. Thus creating
624628
* a dump with this crash kernel will fail. Panic now to allow other
625629
* dump mechanisms to take over.
626630
*/
627631
if (!hdr)
628632
panic("s390 kdump allocating elfcorehdr failed");
629633

630634
/* Init elf header */
631-
ptr = ehdr_init(hdr, mem_chunk_cnt);
635+
phdr_notes = ehdr_init(hdr, mem_chunk_cnt + phdr_text_cnt);
632636
/* Init program headers */
633-
phdr_notes = ptr;
634-
ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
635-
phdr_text = ptr;
636-
ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
637-
phdr_loads = ptr;
638-
ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
637+
if (phdr_text_cnt) {
638+
phdr_text = phdr_notes + 1;
639+
phdr_loads = phdr_text + 1;
640+
} else {
641+
phdr_loads = phdr_notes + 1;
642+
}
643+
ptr = PTR_ADD(phdr_loads, sizeof(Elf64_Phdr) * mem_chunk_cnt);
639644
/* Init notes */
640645
hdr_off = PTR_DIFF(ptr, hdr);
641646
ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
642647
/* Init kernel text program header */
643-
text_init(phdr_text);
648+
if (phdr_text_cnt)
649+
text_init(phdr_text);
644650
/* Init loads */
645-
loads_init(phdr_loads);
651+
loads_init(phdr_loads, phdr_text_cnt);
646652
/* Finalize program headers */
647653
hdr_off = PTR_DIFF(ptr, hdr);
648654
*addr = (unsigned long long) hdr;

tools/testing/selftests/kvm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ TEST_GEN_PROGS_s390x += s390x/sync_regs_test
183183
TEST_GEN_PROGS_s390x += s390x/tprot
184184
TEST_GEN_PROGS_s390x += s390x/cmma_test
185185
TEST_GEN_PROGS_s390x += s390x/debug_test
186+
TEST_GEN_PROGS_s390x += s390x/shared_zeropage_test
186187
TEST_GEN_PROGS_s390x += demand_paging_test
187188
TEST_GEN_PROGS_s390x += dirty_log_test
188189
TEST_GEN_PROGS_s390x += guest_print_test
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Test shared zeropage handling (with/without storage keys)
4+
*
5+
* Copyright (C) 2024, Red Hat, Inc.
6+
*/
7+
#include <sys/mman.h>
8+
9+
#include <linux/fs.h>
10+
11+
#include "test_util.h"
12+
#include "kvm_util.h"
13+
#include "kselftest.h"
14+
#include "ucall_common.h"
15+
16+
static void set_storage_key(void *addr, uint8_t skey)
17+
{
18+
asm volatile("sske %0,%1" : : "d" (skey), "a" (addr));
19+
}
20+
21+
static void guest_code(void)
22+
{
23+
/* Issue some storage key instruction. */
24+
set_storage_key((void *)0, 0x98);
25+
GUEST_DONE();
26+
}
27+
28+
/*
29+
* Returns 1 if the shared zeropage is mapped, 0 if something else is mapped.
30+
* Returns < 0 on error or if nothing is mapped.
31+
*/
32+
static int maps_shared_zeropage(int pagemap_fd, void *addr)
33+
{
34+
struct page_region region;
35+
struct pm_scan_arg arg = {
36+
.start = (uintptr_t)addr,
37+
.end = (uintptr_t)addr + 4096,
38+
.vec = (uintptr_t)&region,
39+
.vec_len = 1,
40+
.size = sizeof(struct pm_scan_arg),
41+
.category_mask = PAGE_IS_PFNZERO,
42+
.category_anyof_mask = PAGE_IS_PRESENT,
43+
.return_mask = PAGE_IS_PFNZERO,
44+
};
45+
return ioctl(pagemap_fd, PAGEMAP_SCAN, &arg);
46+
}
47+
48+
int main(int argc, char *argv[])
49+
{
50+
char *mem, *page0, *page1, *page2, tmp;
51+
const size_t pagesize = getpagesize();
52+
struct kvm_vcpu *vcpu;
53+
struct kvm_vm *vm;
54+
struct ucall uc;
55+
int pagemap_fd;
56+
57+
ksft_print_header();
58+
ksft_set_plan(3);
59+
60+
/*
61+
* We'll use memory that is not mapped into the VM for simplicity.
62+
* Shared zeropages are enabled/disabled per-process.
63+
*/
64+
mem = mmap(0, 3 * pagesize, PROT_READ, MAP_PRIVATE | MAP_ANON, -1, 0);
65+
TEST_ASSERT(mem != MAP_FAILED, "mmap() failed");
66+
67+
/* Disable THP. Ignore errors on older kernels. */
68+
madvise(mem, 3 * pagesize, MADV_NOHUGEPAGE);
69+
70+
page0 = mem;
71+
page1 = page0 + pagesize;
72+
page2 = page1 + pagesize;
73+
74+
/* Can we even detect shared zeropages? */
75+
pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
76+
TEST_REQUIRE(pagemap_fd >= 0);
77+
78+
tmp = *page0;
79+
asm volatile("" : "+r" (tmp));
80+
TEST_REQUIRE(maps_shared_zeropage(pagemap_fd, page0) == 1);
81+
82+
vm = vm_create_with_one_vcpu(&vcpu, guest_code);
83+
84+
/* Verify that we get the shared zeropage after VM creation. */
85+
tmp = *page1;
86+
asm volatile("" : "+r" (tmp));
87+
ksft_test_result(maps_shared_zeropage(pagemap_fd, page1) == 1,
88+
"Shared zeropages should be enabled\n");
89+
90+
/*
91+
* Let our VM execute a storage key instruction that should
92+
* unshare all shared zeropages.
93+
*/
94+
vcpu_run(vcpu);
95+
get_ucall(vcpu, &uc);
96+
TEST_ASSERT_EQ(uc.cmd, UCALL_DONE);
97+
98+
/* Verify that we don't have a shared zeropage anymore. */
99+
ksft_test_result(!maps_shared_zeropage(pagemap_fd, page1),
100+
"Shared zeropage should be gone\n");
101+
102+
/* Verify that we don't get any new shared zeropages. */
103+
tmp = *page2;
104+
asm volatile("" : "+r" (tmp));
105+
ksft_test_result(!maps_shared_zeropage(pagemap_fd, page2),
106+
"Shared zeropages should be disabled\n");
107+
108+
kvm_vm_free(vm);
109+
110+
ksft_finished();
111+
}

0 commit comments

Comments
 (0)