Skip to content

Commit b31288f

Browse files
konne88Martin Schwidefsky
authored andcommitted
s390/kvm: support collaborative memory management
This patch enables Collaborative Memory Management (CMM) for kvm on s390. CMM allows the guest to inform the host about page usage (see arch/s390/mm/cmm.c). The host uses this information to avoid swapping in unused pages in the page fault handler. Further, a CPU provided list of unused invalid pages is processed to reclaim swap space of not yet accessed unused pages. [ Martin Schwidefsky: patch reordering and cleanup ] Signed-off-by: Konstantin Weitz <konstantin.weitz@gmail.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
1 parent 4596172 commit b31288f

6 files changed

Lines changed: 175 additions & 1 deletion

File tree

arch/s390/include/asm/kvm_host.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ struct kvm_s390_sie_block {
106106
__u64 gbea; /* 0x0180 */
107107
__u8 reserved188[24]; /* 0x0188 */
108108
__u32 fac; /* 0x01a0 */
109-
__u8 reserved1a4[68]; /* 0x01a4 */
109+
__u8 reserved1a4[20]; /* 0x01a4 */
110+
__u64 cbrlo; /* 0x01b8 */
111+
__u8 reserved1c0[40]; /* 0x01c0 */
110112
__u64 itdba; /* 0x01e8 */
111113
__u8 reserved1f0[16]; /* 0x01f0 */
112114
} __attribute__((packed));
@@ -155,6 +157,7 @@ struct kvm_vcpu_stat {
155157
u32 instruction_stsi;
156158
u32 instruction_stfl;
157159
u32 instruction_tprot;
160+
u32 instruction_essa;
158161
u32 instruction_sigp_sense;
159162
u32 instruction_sigp_sense_running;
160163
u32 instruction_sigp_external_call;

arch/s390/include/asm/pgtable.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ extern unsigned long MODULES_END;
229229
#define _PAGE_READ 0x010 /* SW pte read bit */
230230
#define _PAGE_WRITE 0x020 /* SW pte write bit */
231231
#define _PAGE_SPECIAL 0x040 /* SW associated with special page */
232+
#define _PAGE_UNUSED 0x080 /* SW bit for pgste usage state */
232233
#define __HAVE_ARCH_PTE_SPECIAL
233234

234235
/* Set of bits not changed in pte_modify */
@@ -394,6 +395,12 @@ extern unsigned long MODULES_END;
394395

395396
#endif /* CONFIG_64BIT */
396397

398+
/* Guest Page State used for virtualization */
399+
#define _PGSTE_GPS_ZERO 0x0000000080000000UL
400+
#define _PGSTE_GPS_USAGE_MASK 0x0000000003000000UL
401+
#define _PGSTE_GPS_USAGE_STABLE 0x0000000000000000UL
402+
#define _PGSTE_GPS_USAGE_UNUSED 0x0000000001000000UL
403+
397404
/*
398405
* A user page table pointer has the space-switch-event bit, the
399406
* private-space-control bit and the storage-alteration-event-control
@@ -617,6 +624,14 @@ static inline int pte_none(pte_t pte)
617624
return pte_val(pte) == _PAGE_INVALID;
618625
}
619626

627+
static inline int pte_swap(pte_t pte)
628+
{
629+
/* Bit pattern: (pte & 0x603) == 0x402 */
630+
return (pte_val(pte) & (_PAGE_INVALID | _PAGE_PROTECT |
631+
_PAGE_TYPE | _PAGE_PRESENT))
632+
== (_PAGE_INVALID | _PAGE_TYPE);
633+
}
634+
620635
static inline int pte_file(pte_t pte)
621636
{
622637
/* Bit pattern: (pte & 0x601) == 0x600 */
@@ -821,6 +836,7 @@ unsigned long gmap_translate(unsigned long address, struct gmap *);
821836
unsigned long __gmap_fault(unsigned long address, struct gmap *);
822837
unsigned long gmap_fault(unsigned long address, struct gmap *);
823838
void gmap_discard(unsigned long from, unsigned long to, struct gmap *);
839+
void __gmap_zap(unsigned long address, struct gmap *);
824840

825841
void gmap_register_ipte_notifier(struct gmap_notifier *);
826842
void gmap_unregister_ipte_notifier(struct gmap_notifier *);
@@ -852,6 +868,7 @@ static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
852868

853869
if (mm_has_pgste(mm)) {
854870
pgste = pgste_get_lock(ptep);
871+
pgste_val(pgste) &= ~_PGSTE_GPS_ZERO;
855872
pgste_set_key(ptep, pgste, entry);
856873
pgste_set_pte(ptep, entry);
857874
pgste_set_unlock(ptep, pgste);
@@ -881,6 +898,12 @@ static inline int pte_young(pte_t pte)
881898
return (pte_val(pte) & _PAGE_YOUNG) != 0;
882899
}
883900

901+
#define __HAVE_ARCH_PTE_UNUSED
902+
static inline int pte_unused(pte_t pte)
903+
{
904+
return pte_val(pte) & _PAGE_UNUSED;
905+
}
906+
884907
/*
885908
* pgd/pmd/pte modification functions
886909
*/
@@ -1196,6 +1219,9 @@ static inline pte_t ptep_clear_flush(struct vm_area_struct *vma,
11961219
pte_val(*ptep) = _PAGE_INVALID;
11971220

11981221
if (mm_has_pgste(vma->vm_mm)) {
1222+
if ((pgste_val(pgste) & _PGSTE_GPS_USAGE_MASK) ==
1223+
_PGSTE_GPS_USAGE_UNUSED)
1224+
pte_val(pte) |= _PAGE_UNUSED;
11991225
pgste = pgste_update_all(&pte, pgste);
12001226
pgste_set_unlock(ptep, pgste);
12011227
}

arch/s390/kvm/kvm-s390.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
6868
{ "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
6969
{ "instruction_stsch", VCPU_STAT(instruction_stsch) },
7070
{ "instruction_chsc", VCPU_STAT(instruction_chsc) },
71+
{ "instruction_essa", VCPU_STAT(instruction_essa) },
7172
{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
7273
{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
7374
{ "instruction_tprot", VCPU_STAT(instruction_tprot) },
@@ -283,7 +284,11 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
283284
if (kvm_is_ucontrol(vcpu->kvm))
284285
gmap_free(vcpu->arch.gmap);
285286

287+
if (vcpu->arch.sie_block->cbrlo)
288+
__free_page(__pfn_to_page(
289+
vcpu->arch.sie_block->cbrlo >> PAGE_SHIFT));
286290
free_page((unsigned long)(vcpu->arch.sie_block));
291+
287292
kvm_vcpu_uninit(vcpu);
288293
kmem_cache_free(kvm_vcpu_cache, vcpu);
289294
}
@@ -390,6 +395,8 @@ int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
390395

391396
int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
392397
{
398+
struct page *cbrl;
399+
393400
atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
394401
CPUSTAT_SM |
395402
CPUSTAT_STOPPED |
@@ -401,6 +408,14 @@ int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
401408
vcpu->arch.sie_block->ecb2 = 8;
402409
vcpu->arch.sie_block->eca = 0xC1002001U;
403410
vcpu->arch.sie_block->fac = (int) (long) vfacilities;
411+
if (kvm_enabled_cmma()) {
412+
cbrl = alloc_page(GFP_KERNEL | __GFP_ZERO);
413+
if (cbrl) {
414+
vcpu->arch.sie_block->ecb2 |= 0x80;
415+
vcpu->arch.sie_block->ecb2 &= ~0x08;
416+
vcpu->arch.sie_block->cbrlo = page_to_phys(cbrl);
417+
}
418+
}
404419
hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
405420
tasklet_init(&vcpu->arch.tasklet, kvm_s390_tasklet,
406421
(unsigned long) vcpu);
@@ -761,6 +776,16 @@ static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
761776
return rc;
762777
}
763778

779+
bool kvm_enabled_cmma(void)
780+
{
781+
if (!MACHINE_IS_LPAR)
782+
return false;
783+
/* only enable for z10 and later */
784+
if (!MACHINE_HAS_EDAT1)
785+
return false;
786+
return true;
787+
}
788+
764789
static int __vcpu_run(struct kvm_vcpu *vcpu)
765790
{
766791
int rc, exit_reason;

arch/s390/kvm/kvm-s390.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ void s390_vcpu_block(struct kvm_vcpu *vcpu);
156156
void s390_vcpu_unblock(struct kvm_vcpu *vcpu);
157157
void exit_sie(struct kvm_vcpu *vcpu);
158158
void exit_sie_sync(struct kvm_vcpu *vcpu);
159+
/* are we going to support cmma? */
160+
bool kvm_enabled_cmma(void);
159161
/* implemented in diag.c */
160162
int kvm_s390_handle_diag(struct kvm_vcpu *vcpu);
161163

arch/s390/kvm/priv.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,8 +636,49 @@ static int handle_pfmf(struct kvm_vcpu *vcpu)
636636
return 0;
637637
}
638638

639+
static int handle_essa(struct kvm_vcpu *vcpu)
640+
{
641+
/* entries expected to be 1FF */
642+
int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
643+
unsigned long *cbrlo, cbrle;
644+
struct gmap *gmap;
645+
int i;
646+
647+
VCPU_EVENT(vcpu, 5, "cmma release %d pages", entries);
648+
gmap = vcpu->arch.gmap;
649+
vcpu->stat.instruction_essa++;
650+
if (!kvm_enabled_cmma() || !vcpu->arch.sie_block->cbrlo)
651+
return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
652+
653+
if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
654+
return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
655+
656+
if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
657+
return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
658+
659+
/* Rewind PSW to repeat the ESSA instruction */
660+
vcpu->arch.sie_block->gpsw.addr =
661+
__rewind_psw(vcpu->arch.sie_block->gpsw, 4);
662+
vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
663+
cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
664+
down_read(&gmap->mm->mmap_sem);
665+
for (i = 0; i < entries; ++i) {
666+
cbrle = cbrlo[i];
667+
if (unlikely(cbrle & ~PAGE_MASK || cbrle < 2 * PAGE_SIZE))
668+
/* invalid entry */
669+
break;
670+
/* try to free backing */
671+
__gmap_zap(cbrle, gmap);
672+
}
673+
up_read(&gmap->mm->mmap_sem);
674+
if (i < entries)
675+
return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
676+
return 0;
677+
}
678+
639679
static const intercept_handler_t b9_handlers[256] = {
640680
[0x8d] = handle_epsw,
681+
[0xab] = handle_essa,
641682
[0xaf] = handle_pfmf,
642683
};
643684

arch/s390/mm/pgtable.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/quicklist.h>
1818
#include <linux/rcupdate.h>
1919
#include <linux/slab.h>
20+
#include <linux/swapops.h>
2021

2122
#include <asm/pgtable.h>
2223
#include <asm/pgalloc.h>
@@ -594,6 +595,82 @@ unsigned long gmap_fault(unsigned long address, struct gmap *gmap)
594595
}
595596
EXPORT_SYMBOL_GPL(gmap_fault);
596597

598+
static void gmap_zap_swap_entry(swp_entry_t entry, struct mm_struct *mm)
599+
{
600+
if (!non_swap_entry(entry))
601+
dec_mm_counter(mm, MM_SWAPENTS);
602+
else if (is_migration_entry(entry)) {
603+
struct page *page = migration_entry_to_page(entry);
604+
605+
if (PageAnon(page))
606+
dec_mm_counter(mm, MM_ANONPAGES);
607+
else
608+
dec_mm_counter(mm, MM_FILEPAGES);
609+
}
610+
free_swap_and_cache(entry);
611+
}
612+
613+
/**
614+
* The mm->mmap_sem lock must be held
615+
*/
616+
static void gmap_zap_unused(struct mm_struct *mm, unsigned long address)
617+
{
618+
unsigned long ptev, pgstev;
619+
spinlock_t *ptl;
620+
pgste_t pgste;
621+
pte_t *ptep, pte;
622+
623+
ptep = get_locked_pte(mm, address, &ptl);
624+
if (unlikely(!ptep))
625+
return;
626+
pte = *ptep;
627+
if (!pte_swap(pte))
628+
goto out_pte;
629+
/* Zap unused and logically-zero pages */
630+
pgste = pgste_get_lock(ptep);
631+
pgstev = pgste_val(pgste);
632+
ptev = pte_val(pte);
633+
if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
634+
((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID))) {
635+
gmap_zap_swap_entry(pte_to_swp_entry(pte), mm);
636+
pte_clear(mm, address, ptep);
637+
}
638+
pgste_set_unlock(ptep, pgste);
639+
out_pte:
640+
pte_unmap_unlock(*ptep, ptl);
641+
}
642+
643+
/*
644+
* this function is assumed to be called with mmap_sem held
645+
*/
646+
void __gmap_zap(unsigned long address, struct gmap *gmap)
647+
{
648+
unsigned long *table, *segment_ptr;
649+
unsigned long segment, pgstev, ptev;
650+
struct gmap_pgtable *mp;
651+
struct page *page;
652+
653+
segment_ptr = gmap_table_walk(address, gmap);
654+
if (IS_ERR(segment_ptr))
655+
return;
656+
segment = *segment_ptr;
657+
if (segment & _SEGMENT_ENTRY_INVALID)
658+
return;
659+
page = pfn_to_page(segment >> PAGE_SHIFT);
660+
mp = (struct gmap_pgtable *) page->index;
661+
address = mp->vmaddr | (address & ~PMD_MASK);
662+
/* Page table is present */
663+
table = (unsigned long *)(segment & _SEGMENT_ENTRY_ORIGIN);
664+
table = table + ((address >> 12) & 0xff);
665+
pgstev = table[PTRS_PER_PTE];
666+
ptev = table[0];
667+
/* quick check, checked again with locks held */
668+
if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
669+
((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID)))
670+
gmap_zap_unused(gmap->mm, address);
671+
}
672+
EXPORT_SYMBOL_GPL(__gmap_zap);
673+
597674
void gmap_discard(unsigned long from, unsigned long to, struct gmap *gmap)
598675
{
599676

0 commit comments

Comments
 (0)