2017-11-24 14:00:35 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-03-08 10:49:57 +00:00
|
|
|
/*
|
|
|
|
* KVM guest address space mapping code
|
|
|
|
*
|
2020-11-09 12:14:35 +00:00
|
|
|
* Copyright IBM Corp. 2007, 2020
|
2016-03-08 10:49:57 +00:00
|
|
|
* Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
|
2018-07-13 10:28:37 +00:00
|
|
|
* David Hildenbrand <david@redhat.com>
|
|
|
|
* Janosch Frank <frankja@linux.vnet.ibm.com>
|
2016-03-08 10:49:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
2019-08-28 14:19:53 +00:00
|
|
|
#include <linux/pagewalk.h>
|
2016-03-08 10:49:57 +00:00
|
|
|
#include <linux/swap.h>
|
|
|
|
#include <linux/smp.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/swapops.h>
|
|
|
|
#include <linux/ksm.h>
|
|
|
|
#include <linux/mman.h>
|
2020-06-09 04:32:38 +00:00
|
|
|
#include <linux/pgtable.h>
|
2020-06-09 04:32:42 +00:00
|
|
|
|
2016-03-08 10:49:57 +00:00
|
|
|
#include <asm/pgalloc.h>
|
|
|
|
#include <asm/gmap.h>
|
|
|
|
#include <asm/tlb.h>
|
|
|
|
|
2016-04-18 11:24:52 +00:00
|
|
|
#define GMAP_SHADOW_FAKE_TABLE 1ULL
|
|
|
|
|
2016-03-08 10:49:57 +00:00
|
|
|
/**
|
2016-03-08 10:55:04 +00:00
|
|
|
* gmap_alloc - allocate and initialize a guest address space
|
2016-04-04 07:41:32 +00:00
|
|
|
* @limit: maximum address of the gmap address space
|
2016-03-08 10:49:57 +00:00
|
|
|
*
|
|
|
|
* Returns a guest address space structure.
|
|
|
|
*/
|
2016-03-08 10:55:04 +00:00
|
|
|
static struct gmap *gmap_alloc(unsigned long limit)
|
2016-03-08 10:49:57 +00:00
|
|
|
{
|
|
|
|
struct gmap *gmap;
|
|
|
|
struct page *page;
|
|
|
|
unsigned long *table;
|
|
|
|
unsigned long etype, atype;
|
|
|
|
|
2017-07-05 05:37:27 +00:00
|
|
|
if (limit < _REGION3_SIZE) {
|
|
|
|
limit = _REGION3_SIZE - 1;
|
2016-03-08 10:49:57 +00:00
|
|
|
atype = _ASCE_TYPE_SEGMENT;
|
|
|
|
etype = _SEGMENT_ENTRY_EMPTY;
|
2017-07-05 05:37:27 +00:00
|
|
|
} else if (limit < _REGION2_SIZE) {
|
|
|
|
limit = _REGION2_SIZE - 1;
|
2016-03-08 10:49:57 +00:00
|
|
|
atype = _ASCE_TYPE_REGION3;
|
|
|
|
etype = _REGION3_ENTRY_EMPTY;
|
2017-07-05 05:37:27 +00:00
|
|
|
} else if (limit < _REGION1_SIZE) {
|
|
|
|
limit = _REGION1_SIZE - 1;
|
2016-03-08 10:49:57 +00:00
|
|
|
atype = _ASCE_TYPE_REGION2;
|
|
|
|
etype = _REGION2_ENTRY_EMPTY;
|
|
|
|
} else {
|
|
|
|
limit = -1UL;
|
|
|
|
atype = _ASCE_TYPE_REGION1;
|
|
|
|
etype = _REGION1_ENTRY_EMPTY;
|
|
|
|
}
|
2020-11-09 12:14:35 +00:00
|
|
|
gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL_ACCOUNT);
|
2016-03-08 10:49:57 +00:00
|
|
|
if (!gmap)
|
|
|
|
goto out;
|
|
|
|
INIT_LIST_HEAD(&gmap->crst_list);
|
2016-03-08 11:12:18 +00:00
|
|
|
INIT_LIST_HEAD(&gmap->children);
|
|
|
|
INIT_LIST_HEAD(&gmap->pt_list);
|
2020-11-09 12:14:35 +00:00
|
|
|
INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL_ACCOUNT);
|
|
|
|
INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC | __GFP_ACCOUNT);
|
|
|
|
INIT_RADIX_TREE(&gmap->host_to_rmap, GFP_ATOMIC | __GFP_ACCOUNT);
|
2016-03-08 10:49:57 +00:00
|
|
|
spin_lock_init(&gmap->guest_table_lock);
|
2016-03-08 11:12:18 +00:00
|
|
|
spin_lock_init(&gmap->shadow_lock);
|
2019-08-08 07:18:26 +00:00
|
|
|
refcount_set(&gmap->ref_count, 1);
|
2020-11-09 12:14:35 +00:00
|
|
|
page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
|
2016-03-08 10:49:57 +00:00
|
|
|
if (!page)
|
|
|
|
goto out_free;
|
|
|
|
page->index = 0;
|
|
|
|
list_add(&page->lru, &gmap->crst_list);
|
2022-10-20 14:31:55 +00:00
|
|
|
table = page_to_virt(page);
|
2016-03-08 10:49:57 +00:00
|
|
|
crst_table_init(table, etype);
|
|
|
|
gmap->table = table;
|
|
|
|
gmap->asce = atype | _ASCE_TABLE_LENGTH |
|
|
|
|
_ASCE_USER_BITS | __pa(table);
|
|
|
|
gmap->asce_end = limit;
|
|
|
|
return gmap;
|
|
|
|
|
|
|
|
out_free:
|
|
|
|
kfree(gmap);
|
|
|
|
out:
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-08 10:55:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_create - create a guest address space
|
|
|
|
* @mm: pointer to the parent mm_struct
|
|
|
|
* @limit: maximum size of the gmap address space
|
|
|
|
*
|
|
|
|
* Returns a guest address space structure.
|
|
|
|
*/
|
|
|
|
struct gmap *gmap_create(struct mm_struct *mm, unsigned long limit)
|
|
|
|
{
|
|
|
|
struct gmap *gmap;
|
2016-06-13 08:36:00 +00:00
|
|
|
unsigned long gmap_asce;
|
2016-03-08 10:55:04 +00:00
|
|
|
|
|
|
|
gmap = gmap_alloc(limit);
|
|
|
|
if (!gmap)
|
|
|
|
return NULL;
|
|
|
|
gmap->mm = mm;
|
2017-08-17 16:17:49 +00:00
|
|
|
spin_lock(&mm->context.lock);
|
2016-03-08 10:55:04 +00:00
|
|
|
list_add_rcu(&gmap->list, &mm->context.gmap_list);
|
2016-06-13 08:36:00 +00:00
|
|
|
if (list_is_singular(&mm->context.gmap_list))
|
|
|
|
gmap_asce = gmap->asce;
|
|
|
|
else
|
|
|
|
gmap_asce = -1UL;
|
|
|
|
WRITE_ONCE(mm->context.gmap_asce, gmap_asce);
|
2017-08-17 16:17:49 +00:00
|
|
|
spin_unlock(&mm->context.lock);
|
2016-03-08 10:55:04 +00:00
|
|
|
return gmap;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_create);
|
2016-03-08 10:49:57 +00:00
|
|
|
|
|
|
|
static void gmap_flush_tlb(struct gmap *gmap)
|
|
|
|
{
|
|
|
|
if (MACHINE_HAS_IDTE)
|
2016-07-07 08:44:10 +00:00
|
|
|
__tlb_flush_idte(gmap->asce);
|
2016-03-08 10:49:57 +00:00
|
|
|
else
|
|
|
|
__tlb_flush_global();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gmap_radix_tree_free(struct radix_tree_root *root)
|
|
|
|
{
|
|
|
|
struct radix_tree_iter iter;
|
|
|
|
unsigned long indices[16];
|
|
|
|
unsigned long index;
|
2017-05-09 11:44:43 +00:00
|
|
|
void __rcu **slot;
|
2016-03-08 10:49:57 +00:00
|
|
|
int i, nr;
|
|
|
|
|
|
|
|
/* A radix tree is freed by deleting all of its entries */
|
|
|
|
index = 0;
|
|
|
|
do {
|
|
|
|
nr = 0;
|
|
|
|
radix_tree_for_each_slot(slot, root, &iter, index) {
|
|
|
|
indices[nr] = iter.index;
|
|
|
|
if (++nr == 16)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (i = 0; i < nr; i++) {
|
|
|
|
index = indices[i];
|
|
|
|
radix_tree_delete(root, index);
|
|
|
|
}
|
|
|
|
} while (nr > 0);
|
|
|
|
}
|
|
|
|
|
2016-03-08 11:12:18 +00:00
|
|
|
static void gmap_rmap_radix_tree_free(struct radix_tree_root *root)
|
|
|
|
{
|
|
|
|
struct gmap_rmap *rmap, *rnext, *head;
|
|
|
|
struct radix_tree_iter iter;
|
|
|
|
unsigned long indices[16];
|
|
|
|
unsigned long index;
|
2017-05-09 11:44:43 +00:00
|
|
|
void __rcu **slot;
|
2016-03-08 11:12:18 +00:00
|
|
|
int i, nr;
|
|
|
|
|
|
|
|
/* A radix tree is freed by deleting all of its entries */
|
|
|
|
index = 0;
|
|
|
|
do {
|
|
|
|
nr = 0;
|
|
|
|
radix_tree_for_each_slot(slot, root, &iter, index) {
|
|
|
|
indices[nr] = iter.index;
|
|
|
|
if (++nr == 16)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (i = 0; i < nr; i++) {
|
|
|
|
index = indices[i];
|
|
|
|
head = radix_tree_delete(root, index);
|
|
|
|
gmap_for_each_rmap_safe(rmap, rnext, head)
|
|
|
|
kfree(rmap);
|
|
|
|
}
|
|
|
|
} while (nr > 0);
|
|
|
|
}
|
|
|
|
|
2016-03-08 10:49:57 +00:00
|
|
|
/**
|
|
|
|
* gmap_free - free a guest address space
|
|
|
|
* @gmap: pointer to the guest address space structure
|
2016-03-08 11:12:18 +00:00
|
|
|
*
|
|
|
|
* No locks required. There are no references to this gmap anymore.
|
2016-03-08 10:49:57 +00:00
|
|
|
*/
|
2016-03-08 10:55:04 +00:00
|
|
|
static void gmap_free(struct gmap *gmap)
|
2016-03-08 10:49:57 +00:00
|
|
|
{
|
|
|
|
struct page *page, *next;
|
|
|
|
|
2016-04-15 10:45:45 +00:00
|
|
|
/* Flush tlb of all gmaps (if not already done for shadows) */
|
|
|
|
if (!(gmap_is_shadow(gmap) && gmap->removed))
|
|
|
|
gmap_flush_tlb(gmap);
|
2016-03-08 10:49:57 +00:00
|
|
|
/* Free all segment & region tables. */
|
|
|
|
list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
|
2017-07-05 05:37:27 +00:00
|
|
|
__free_pages(page, CRST_ALLOC_ORDER);
|
2016-03-08 10:49:57 +00:00
|
|
|
gmap_radix_tree_free(&gmap->guest_to_host);
|
|
|
|
gmap_radix_tree_free(&gmap->host_to_guest);
|
2016-03-08 11:12:18 +00:00
|
|
|
|
|
|
|
/* Free additional data for a shadow gmap */
|
|
|
|
if (gmap_is_shadow(gmap)) {
|
|
|
|
/* Free all page tables. */
|
|
|
|
list_for_each_entry_safe(page, next, &gmap->pt_list, lru)
|
|
|
|
page_table_free_pgste(page);
|
|
|
|
gmap_rmap_radix_tree_free(&gmap->host_to_rmap);
|
|
|
|
/* Release reference to the parent */
|
|
|
|
gmap_put(gmap->parent);
|
|
|
|
}
|
|
|
|
|
2016-03-08 10:49:57 +00:00
|
|
|
kfree(gmap);
|
|
|
|
}
|
2016-03-08 10:55:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_get - increase reference counter for guest address space
|
|
|
|
* @gmap: pointer to the guest address space structure
|
|
|
|
*
|
|
|
|
* Returns the gmap pointer
|
|
|
|
*/
|
|
|
|
struct gmap *gmap_get(struct gmap *gmap)
|
|
|
|
{
|
2019-08-08 07:18:26 +00:00
|
|
|
refcount_inc(&gmap->ref_count);
|
2016-03-08 10:55:04 +00:00
|
|
|
return gmap;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_get);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_put - decrease reference counter for guest address space
|
|
|
|
* @gmap: pointer to the guest address space structure
|
|
|
|
*
|
|
|
|
* If the reference counter reaches zero the guest address space is freed.
|
|
|
|
*/
|
|
|
|
void gmap_put(struct gmap *gmap)
|
|
|
|
{
|
2019-08-08 07:18:26 +00:00
|
|
|
if (refcount_dec_and_test(&gmap->ref_count))
|
2016-03-08 10:55:04 +00:00
|
|
|
gmap_free(gmap);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_put);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_remove - remove a guest address space but do not free it yet
|
|
|
|
* @gmap: pointer to the guest address space structure
|
|
|
|
*/
|
|
|
|
void gmap_remove(struct gmap *gmap)
|
|
|
|
{
|
2016-03-08 11:12:18 +00:00
|
|
|
struct gmap *sg, *next;
|
2016-06-13 08:36:00 +00:00
|
|
|
unsigned long gmap_asce;
|
2016-03-08 11:12:18 +00:00
|
|
|
|
|
|
|
/* Remove all shadow gmaps linked to this gmap */
|
|
|
|
if (!list_empty(&gmap->children)) {
|
|
|
|
spin_lock(&gmap->shadow_lock);
|
|
|
|
list_for_each_entry_safe(sg, next, &gmap->children, list) {
|
|
|
|
list_del(&sg->list);
|
|
|
|
gmap_put(sg);
|
|
|
|
}
|
|
|
|
spin_unlock(&gmap->shadow_lock);
|
|
|
|
}
|
2016-03-08 10:55:04 +00:00
|
|
|
/* Remove gmap from the pre-mm list */
|
2017-08-17 16:17:49 +00:00
|
|
|
spin_lock(&gmap->mm->context.lock);
|
2016-03-08 10:55:04 +00:00
|
|
|
list_del_rcu(&gmap->list);
|
2016-06-13 08:36:00 +00:00
|
|
|
if (list_empty(&gmap->mm->context.gmap_list))
|
|
|
|
gmap_asce = 0;
|
|
|
|
else if (list_is_singular(&gmap->mm->context.gmap_list))
|
|
|
|
gmap_asce = list_first_entry(&gmap->mm->context.gmap_list,
|
|
|
|
struct gmap, list)->asce;
|
|
|
|
else
|
|
|
|
gmap_asce = -1UL;
|
|
|
|
WRITE_ONCE(gmap->mm->context.gmap_asce, gmap_asce);
|
2017-08-17 16:17:49 +00:00
|
|
|
spin_unlock(&gmap->mm->context.lock);
|
2016-03-08 10:55:04 +00:00
|
|
|
synchronize_rcu();
|
|
|
|
/* Put reference */
|
|
|
|
gmap_put(gmap);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_remove);
|
2016-03-08 10:49:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_enable - switch primary space to the guest address space
|
|
|
|
* @gmap: pointer to the guest address space structure
|
|
|
|
*/
|
|
|
|
void gmap_enable(struct gmap *gmap)
|
|
|
|
{
|
|
|
|
S390_lowcore.gmap = (unsigned long) gmap;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_enable);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_disable - switch back to the standard primary address space
|
|
|
|
* @gmap: pointer to the guest address space structure
|
|
|
|
*/
|
|
|
|
void gmap_disable(struct gmap *gmap)
|
|
|
|
{
|
|
|
|
S390_lowcore.gmap = 0UL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_disable);
|
|
|
|
|
2015-03-11 15:47:33 +00:00
|
|
|
/**
|
|
|
|
* gmap_get_enabled - get a pointer to the currently enabled gmap
|
|
|
|
*
|
|
|
|
* Returns a pointer to the currently enabled gmap. 0 if none is enabled.
|
|
|
|
*/
|
|
|
|
struct gmap *gmap_get_enabled(void)
|
|
|
|
{
|
|
|
|
return (struct gmap *) S390_lowcore.gmap;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_get_enabled);
|
|
|
|
|
2016-03-08 10:49:57 +00:00
|
|
|
/*
|
2020-06-09 04:33:54 +00:00
|
|
|
* gmap_alloc_table is assumed to be called with mmap_lock held
|
2016-03-08 10:49:57 +00:00
|
|
|
*/
|
|
|
|
static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
|
|
|
|
unsigned long init, unsigned long gaddr)
|
|
|
|
{
|
|
|
|
struct page *page;
|
|
|
|
unsigned long *new;
|
|
|
|
|
|
|
|
/* since we dont free the gmap table until gmap_free we can unlock */
|
2020-11-09 12:14:35 +00:00
|
|
|
page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
|
2016-03-08 10:49:57 +00:00
|
|
|
if (!page)
|
|
|
|
return -ENOMEM;
|
2022-10-20 14:31:55 +00:00
|
|
|
new = page_to_virt(page);
|
2016-03-08 10:49:57 +00:00
|
|
|
crst_table_init(new, init);
|
2016-03-08 11:12:18 +00:00
|
|
|
spin_lock(&gmap->guest_table_lock);
|
2016-03-08 10:49:57 +00:00
|
|
|
if (*table & _REGION_ENTRY_INVALID) {
|
|
|
|
list_add(&page->lru, &gmap->crst_list);
|
2022-10-20 14:31:55 +00:00
|
|
|
*table = __pa(new) | _REGION_ENTRY_LENGTH |
|
2016-03-08 10:49:57 +00:00
|
|
|
(*table & _REGION_ENTRY_TYPE_MASK);
|
|
|
|
page->index = gaddr;
|
|
|
|
page = NULL;
|
|
|
|
}
|
2016-03-08 11:12:18 +00:00
|
|
|
spin_unlock(&gmap->guest_table_lock);
|
2016-03-08 10:49:57 +00:00
|
|
|
if (page)
|
2017-07-05 05:37:27 +00:00
|
|
|
__free_pages(page, CRST_ALLOC_ORDER);
|
2016-03-08 10:49:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __gmap_segment_gaddr - find virtual address from segment pointer
|
|
|
|
* @entry: pointer to a segment table entry in the guest address space
|
|
|
|
*
|
|
|
|
* Returns the virtual address in the guest address space for the segment
|
|
|
|
*/
|
|
|
|
static unsigned long __gmap_segment_gaddr(unsigned long *entry)
|
|
|
|
{
|
|
|
|
struct page *page;
|
2022-11-25 03:45:02 +00:00
|
|
|
unsigned long offset;
|
2016-03-08 10:49:57 +00:00
|
|
|
|
|
|
|
offset = (unsigned long) entry / sizeof(unsigned long);
|
|
|
|
offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
|
2022-11-25 03:45:02 +00:00
|
|
|
page = pmd_pgtable_page((pmd_t *) entry);
|
2016-03-08 10:49:57 +00:00
|
|
|
return page->index + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __gmap_unlink_by_vmaddr - unlink a single segment via a host address
|
|
|
|
* @gmap: pointer to the guest address space structure
|
|
|
|
* @vmaddr: address in the host process address space
|
|
|
|
*
|
|
|
|
* Returns 1 if a TLB flush is required
|
|
|
|
*/
|
|
|
|
static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
|
|
|
|
{
|
|
|
|
unsigned long *entry;
|
|
|
|
int flush = 0;
|
|
|
|
|
2016-03-08 11:12:18 +00:00
|
|
|
BUG_ON(gmap_is_shadow(gmap));
|
2016-03-08 10:49:57 +00:00
|
|
|
spin_lock(&gmap->guest_table_lock);
|
|
|
|
entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
|
|
|
|
if (entry) {
|
2016-04-27 09:43:07 +00:00
|
|
|
flush = (*entry != _SEGMENT_ENTRY_EMPTY);
|
|
|
|
*entry = _SEGMENT_ENTRY_EMPTY;
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
|
|
|
spin_unlock(&gmap->guest_table_lock);
|
|
|
|
return flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __gmap_unmap_by_gaddr - unmap a single segment via a guest address
|
|
|
|
* @gmap: pointer to the guest address space structure
|
|
|
|
* @gaddr: address in the guest address space
|
|
|
|
*
|
|
|
|
* Returns 1 if a TLB flush is required
|
|
|
|
*/
|
|
|
|
static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
|
|
|
|
{
|
|
|
|
unsigned long vmaddr;
|
|
|
|
|
|
|
|
vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
|
|
|
|
gaddr >> PMD_SHIFT);
|
|
|
|
return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_unmap_segment - unmap segment from the guest address space
|
|
|
|
* @gmap: pointer to the guest address space structure
|
|
|
|
* @to: address in the guest address space
|
|
|
|
* @len: length of the memory area to unmap
|
|
|
|
*
|
|
|
|
* Returns 0 if the unmap succeeded, -EINVAL if not.
|
|
|
|
*/
|
|
|
|
int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
|
|
|
|
{
|
|
|
|
unsigned long off;
|
|
|
|
int flush;
|
|
|
|
|
2016-03-08 11:12:18 +00:00
|
|
|
BUG_ON(gmap_is_shadow(gmap));
|
2016-03-08 10:49:57 +00:00
|
|
|
if ((to | len) & (PMD_SIZE - 1))
|
|
|
|
return -EINVAL;
|
|
|
|
if (len == 0 || to + len < to)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
flush = 0;
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_write_lock(gmap->mm);
|
2016-03-08 10:49:57 +00:00
|
|
|
for (off = 0; off < len; off += PMD_SIZE)
|
|
|
|
flush |= __gmap_unmap_by_gaddr(gmap, to + off);
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_write_unlock(gmap->mm);
|
2016-03-08 10:49:57 +00:00
|
|
|
if (flush)
|
|
|
|
gmap_flush_tlb(gmap);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_unmap_segment);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_map_segment - map a segment to the guest address space
|
|
|
|
* @gmap: pointer to the guest address space structure
|
|
|
|
* @from: source address in the parent address space
|
|
|
|
* @to: target address in the guest address space
|
|
|
|
* @len: length of the memory area to map
|
|
|
|
*
|
|
|
|
* Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
|
|
|
|
*/
|
|
|
|
int gmap_map_segment(struct gmap *gmap, unsigned long from,
|
|
|
|
unsigned long to, unsigned long len)
|
|
|
|
{
|
|
|
|
unsigned long off;
|
|
|
|
int flush;
|
|
|
|
|
2016-03-08 11:12:18 +00:00
|
|
|
BUG_ON(gmap_is_shadow(gmap));
|
2016-03-08 10:49:57 +00:00
|
|
|
if ((from | to | len) & (PMD_SIZE - 1))
|
|
|
|
return -EINVAL;
|
|
|
|
if (len == 0 || from + len < from || to + len < to ||
|
2017-04-20 12:43:51 +00:00
|
|
|
from + len - 1 > TASK_SIZE_MAX || to + len - 1 > gmap->asce_end)
|
2016-03-08 10:49:57 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
flush = 0;
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_write_lock(gmap->mm);
|
2016-03-08 10:49:57 +00:00
|
|
|
for (off = 0; off < len; off += PMD_SIZE) {
|
|
|
|
/* Remove old translation */
|
|
|
|
flush |= __gmap_unmap_by_gaddr(gmap, to + off);
|
|
|
|
/* Store new translation */
|
|
|
|
if (radix_tree_insert(&gmap->guest_to_host,
|
|
|
|
(to + off) >> PMD_SHIFT,
|
|
|
|
(void *) from + off))
|
|
|
|
break;
|
|
|
|
}
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_write_unlock(gmap->mm);
|
2016-03-08 10:49:57 +00:00
|
|
|
if (flush)
|
|
|
|
gmap_flush_tlb(gmap);
|
|
|
|
if (off >= len)
|
|
|
|
return 0;
|
|
|
|
gmap_unmap_segment(gmap, to, len);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_map_segment);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __gmap_translate - translate a guest address to a user space address
|
|
|
|
* @gmap: pointer to guest mapping meta data structure
|
|
|
|
* @gaddr: guest address
|
|
|
|
*
|
|
|
|
* Returns user space address which corresponds to the guest address or
|
|
|
|
* -EFAULT if no such mapping exists.
|
|
|
|
* This function does not establish potentially missing page table entries.
|
2020-06-09 04:33:54 +00:00
|
|
|
* The mmap_lock of the mm that belongs to the address space must be held
|
2016-03-08 10:49:57 +00:00
|
|
|
* when this function gets called.
|
2016-03-08 11:12:18 +00:00
|
|
|
*
|
|
|
|
* Note: Can also be called for shadow gmaps.
|
2016-03-08 10:49:57 +00:00
|
|
|
*/
|
|
|
|
unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
|
|
|
|
{
|
|
|
|
unsigned long vmaddr;
|
|
|
|
|
|
|
|
vmaddr = (unsigned long)
|
|
|
|
radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Note: guest_to_host is empty for a shadow gmap */
|
2016-03-08 10:49:57 +00:00
|
|
|
return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(__gmap_translate);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_translate - translate a guest address to a user space address
|
|
|
|
* @gmap: pointer to guest mapping meta data structure
|
|
|
|
* @gaddr: guest address
|
|
|
|
*
|
|
|
|
* Returns user space address which corresponds to the guest address or
|
|
|
|
* -EFAULT if no such mapping exists.
|
|
|
|
* This function does not establish potentially missing page table entries.
|
|
|
|
*/
|
|
|
|
unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
|
|
|
|
{
|
|
|
|
unsigned long rc;
|
|
|
|
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_read_lock(gmap->mm);
|
2016-03-08 10:49:57 +00:00
|
|
|
rc = __gmap_translate(gmap, gaddr);
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_read_unlock(gmap->mm);
|
2016-03-08 10:49:57 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_translate);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_unlink - disconnect a page table from the gmap shadow tables
|
2021-09-02 17:47:37 +00:00
|
|
|
* @mm: pointer to the parent mm_struct
|
2016-03-08 10:49:57 +00:00
|
|
|
* @table: pointer to the host page table
|
|
|
|
* @vmaddr: vm address associated with the host page table
|
|
|
|
*/
|
|
|
|
void gmap_unlink(struct mm_struct *mm, unsigned long *table,
|
|
|
|
unsigned long vmaddr)
|
|
|
|
{
|
|
|
|
struct gmap *gmap;
|
|
|
|
int flush;
|
|
|
|
|
2016-03-08 10:54:14 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
|
2016-03-08 10:49:57 +00:00
|
|
|
flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
|
|
|
|
if (flush)
|
|
|
|
gmap_flush_tlb(gmap);
|
|
|
|
}
|
2016-03-08 10:54:14 +00:00
|
|
|
rcu_read_unlock();
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 12:21:22 +00:00
|
|
|
static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *old, pmd_t new,
|
|
|
|
unsigned long gaddr);
|
|
|
|
|
2016-03-08 10:49:57 +00:00
|
|
|
/**
|
2021-09-02 17:47:37 +00:00
|
|
|
* __gmap_link - set up shadow page tables to connect a host to a guest address
|
2016-03-08 10:49:57 +00:00
|
|
|
* @gmap: pointer to guest mapping meta data structure
|
|
|
|
* @gaddr: guest address
|
|
|
|
* @vmaddr: vm address
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
|
|
|
|
* if the vm address is already mapped to a different guest segment.
|
2020-06-09 04:33:54 +00:00
|
|
|
* The mmap_lock of the mm that belongs to the address space must be held
|
2016-03-08 10:49:57 +00:00
|
|
|
* when this function gets called.
|
|
|
|
*/
|
|
|
|
int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
|
|
|
|
{
|
|
|
|
struct mm_struct *mm;
|
|
|
|
unsigned long *table;
|
|
|
|
spinlock_t *ptl;
|
|
|
|
pgd_t *pgd;
|
2017-04-24 16:19:10 +00:00
|
|
|
p4d_t *p4d;
|
2016-03-08 10:49:57 +00:00
|
|
|
pud_t *pud;
|
|
|
|
pmd_t *pmd;
|
2018-07-17 12:21:22 +00:00
|
|
|
u64 unprot;
|
2016-03-08 10:49:57 +00:00
|
|
|
int rc;
|
|
|
|
|
2016-03-08 11:12:18 +00:00
|
|
|
BUG_ON(gmap_is_shadow(gmap));
|
2016-03-08 10:49:57 +00:00
|
|
|
/* Create higher level tables in the gmap page table */
|
|
|
|
table = gmap->table;
|
|
|
|
if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
|
2017-07-05 05:37:27 +00:00
|
|
|
table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
|
2016-03-08 10:49:57 +00:00
|
|
|
if ((*table & _REGION_ENTRY_INVALID) &&
|
|
|
|
gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
|
2017-07-05 05:37:27 +00:00
|
|
|
gaddr & _REGION1_MASK))
|
2016-03-08 10:49:57 +00:00
|
|
|
return -ENOMEM;
|
2022-10-20 14:31:55 +00:00
|
|
|
table = __va(*table & _REGION_ENTRY_ORIGIN);
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
|
|
|
if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
|
2017-07-05 05:37:27 +00:00
|
|
|
table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
|
2016-03-08 10:49:57 +00:00
|
|
|
if ((*table & _REGION_ENTRY_INVALID) &&
|
|
|
|
gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
|
2017-07-05 05:37:27 +00:00
|
|
|
gaddr & _REGION2_MASK))
|
2016-03-08 10:49:57 +00:00
|
|
|
return -ENOMEM;
|
2022-10-20 14:31:55 +00:00
|
|
|
table = __va(*table & _REGION_ENTRY_ORIGIN);
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
|
|
|
if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
|
2017-07-05 05:37:27 +00:00
|
|
|
table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
|
2016-03-08 10:49:57 +00:00
|
|
|
if ((*table & _REGION_ENTRY_INVALID) &&
|
|
|
|
gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
|
2017-07-05 05:37:27 +00:00
|
|
|
gaddr & _REGION3_MASK))
|
2016-03-08 10:49:57 +00:00
|
|
|
return -ENOMEM;
|
2022-10-20 14:31:55 +00:00
|
|
|
table = __va(*table & _REGION_ENTRY_ORIGIN);
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
2017-07-05 05:37:27 +00:00
|
|
|
table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
|
2016-03-08 10:49:57 +00:00
|
|
|
/* Walk the parent mm page table */
|
|
|
|
mm = gmap->mm;
|
|
|
|
pgd = pgd_offset(mm, vmaddr);
|
|
|
|
VM_BUG_ON(pgd_none(*pgd));
|
2017-04-24 16:19:10 +00:00
|
|
|
p4d = p4d_offset(pgd, vmaddr);
|
|
|
|
VM_BUG_ON(p4d_none(*p4d));
|
|
|
|
pud = pud_offset(p4d, vmaddr);
|
2016-03-08 10:49:57 +00:00
|
|
|
VM_BUG_ON(pud_none(*pud));
|
2016-07-04 12:47:01 +00:00
|
|
|
/* large puds cannot yet be handled */
|
|
|
|
if (pud_large(*pud))
|
|
|
|
return -EFAULT;
|
2016-03-08 10:49:57 +00:00
|
|
|
pmd = pmd_offset(pud, vmaddr);
|
|
|
|
VM_BUG_ON(pmd_none(*pmd));
|
2018-07-13 10:28:37 +00:00
|
|
|
/* Are we allowed to use huge pages? */
|
|
|
|
if (pmd_large(*pmd) && !gmap->mm->context.allow_gmap_hpage_1m)
|
2016-03-08 10:49:57 +00:00
|
|
|
return -EFAULT;
|
|
|
|
/* Link gmap segment table entry location to page table. */
|
2020-11-09 12:14:35 +00:00
|
|
|
rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
|
2016-03-08 10:49:57 +00:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
ptl = pmd_lock(mm, pmd);
|
|
|
|
spin_lock(&gmap->guest_table_lock);
|
2016-04-27 09:43:07 +00:00
|
|
|
if (*table == _SEGMENT_ENTRY_EMPTY) {
|
2016-03-08 10:49:57 +00:00
|
|
|
rc = radix_tree_insert(&gmap->host_to_guest,
|
|
|
|
vmaddr >> PMD_SHIFT, table);
|
2018-07-13 10:28:20 +00:00
|
|
|
if (!rc) {
|
|
|
|
if (pmd_large(*pmd)) {
|
2018-07-17 12:21:22 +00:00
|
|
|
*table = (pmd_val(*pmd) &
|
|
|
|
_SEGMENT_ENTRY_HARDWARE_BITS_LARGE)
|
|
|
|
| _SEGMENT_ENTRY_GMAP_UC;
|
2018-07-13 10:28:20 +00:00
|
|
|
} else
|
|
|
|
*table = pmd_val(*pmd) &
|
|
|
|
_SEGMENT_ENTRY_HARDWARE_BITS;
|
|
|
|
}
|
2018-07-17 12:21:22 +00:00
|
|
|
} else if (*table & _SEGMENT_ENTRY_PROTECT &&
|
|
|
|
!(pmd_val(*pmd) & _SEGMENT_ENTRY_PROTECT)) {
|
|
|
|
unprot = (u64)*table;
|
|
|
|
unprot &= ~_SEGMENT_ENTRY_PROTECT;
|
|
|
|
unprot |= _SEGMENT_ENTRY_GMAP_UC;
|
|
|
|
gmap_pmdp_xchg(gmap, (pmd_t *)table, __pmd(unprot), gaddr);
|
2018-07-13 10:28:20 +00:00
|
|
|
}
|
2016-03-08 10:49:57 +00:00
|
|
|
spin_unlock(&gmap->guest_table_lock);
|
|
|
|
spin_unlock(ptl);
|
|
|
|
radix_tree_preload_end();
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_fault - resolve a fault on a guest address
|
|
|
|
* @gmap: pointer to guest mapping meta data structure
|
|
|
|
* @gaddr: guest address
|
|
|
|
* @fault_flags: flags to pass down to handle_mm_fault()
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
|
|
|
|
* if the vm address is already mapped to a different guest segment.
|
|
|
|
*/
|
|
|
|
int gmap_fault(struct gmap *gmap, unsigned long gaddr,
|
|
|
|
unsigned int fault_flags)
|
|
|
|
{
|
|
|
|
unsigned long vmaddr;
|
|
|
|
int rc;
|
|
|
|
bool unlocked;
|
|
|
|
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_read_lock(gmap->mm);
|
2016-03-08 10:49:57 +00:00
|
|
|
|
|
|
|
retry:
|
|
|
|
unlocked = false;
|
|
|
|
vmaddr = __gmap_translate(gmap, gaddr);
|
|
|
|
if (IS_ERR_VALUE(vmaddr)) {
|
|
|
|
rc = vmaddr;
|
|
|
|
goto out_up;
|
|
|
|
}
|
2020-08-12 01:39:01 +00:00
|
|
|
if (fixup_user_fault(gmap->mm, vmaddr, fault_flags,
|
2016-03-08 10:49:57 +00:00
|
|
|
&unlocked)) {
|
|
|
|
rc = -EFAULT;
|
|
|
|
goto out_up;
|
|
|
|
}
|
|
|
|
/*
|
2020-06-09 04:33:54 +00:00
|
|
|
* In the case that fixup_user_fault unlocked the mmap_lock during
|
2016-03-08 10:49:57 +00:00
|
|
|
* faultin redo __gmap_translate to not race with a map/unmap_segment.
|
|
|
|
*/
|
|
|
|
if (unlocked)
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
rc = __gmap_link(gmap, gaddr, vmaddr);
|
|
|
|
out_up:
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_read_unlock(gmap->mm);
|
2016-03-08 10:49:57 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_fault);
|
|
|
|
|
|
|
|
/*
|
2020-06-09 04:33:54 +00:00
|
|
|
* this function is assumed to be called with mmap_lock held
|
2016-03-08 10:49:57 +00:00
|
|
|
*/
|
|
|
|
void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
|
|
|
|
{
|
2021-09-09 16:22:40 +00:00
|
|
|
struct vm_area_struct *vma;
|
2016-03-08 10:49:57 +00:00
|
|
|
unsigned long vmaddr;
|
|
|
|
spinlock_t *ptl;
|
|
|
|
pte_t *ptep;
|
|
|
|
|
|
|
|
/* Find the vm address for the guest address */
|
|
|
|
vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
|
|
|
|
gaddr >> PMD_SHIFT);
|
|
|
|
if (vmaddr) {
|
|
|
|
vmaddr |= gaddr & ~PMD_MASK;
|
2021-09-09 16:22:40 +00:00
|
|
|
|
|
|
|
vma = vma_lookup(gmap->mm, vmaddr);
|
|
|
|
if (!vma || is_vm_hugetlb_page(vma))
|
|
|
|
return;
|
|
|
|
|
2016-03-08 10:49:57 +00:00
|
|
|
/* Get pointer to the page table entry */
|
|
|
|
ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
|
2021-09-09 16:22:41 +00:00
|
|
|
if (likely(ptep)) {
|
2016-03-08 10:49:57 +00:00
|
|
|
ptep_zap_unused(gmap->mm, vmaddr, ptep, 0);
|
2021-09-09 16:22:41 +00:00
|
|
|
pte_unmap_unlock(ptep, ptl);
|
|
|
|
}
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(__gmap_zap);
|
|
|
|
|
|
|
|
void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
|
|
|
|
{
|
|
|
|
unsigned long gaddr, vmaddr, size;
|
|
|
|
struct vm_area_struct *vma;
|
|
|
|
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_read_lock(gmap->mm);
|
2016-03-08 10:49:57 +00:00
|
|
|
for (gaddr = from; gaddr < to;
|
|
|
|
gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
|
|
|
|
/* Find the vm address for the guest address */
|
|
|
|
vmaddr = (unsigned long)
|
|
|
|
radix_tree_lookup(&gmap->guest_to_host,
|
|
|
|
gaddr >> PMD_SHIFT);
|
|
|
|
if (!vmaddr)
|
|
|
|
continue;
|
|
|
|
vmaddr |= gaddr & ~PMD_MASK;
|
|
|
|
/* Find vma in the parent mm */
|
|
|
|
vma = find_vma(gmap->mm, vmaddr);
|
2018-08-16 08:02:31 +00:00
|
|
|
if (!vma)
|
|
|
|
continue;
|
2018-07-13 10:28:29 +00:00
|
|
|
/*
|
|
|
|
* We do not discard pages that are backed by
|
|
|
|
* hugetlbfs, so we don't have to refault them.
|
|
|
|
*/
|
2018-08-16 08:02:31 +00:00
|
|
|
if (is_vm_hugetlb_page(vma))
|
2018-07-13 10:28:29 +00:00
|
|
|
continue;
|
2016-03-08 10:49:57 +00:00
|
|
|
size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
|
2023-01-04 00:27:32 +00:00
|
|
|
zap_page_range_single(vma, vmaddr, size, NULL);
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_read_unlock(gmap->mm);
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_discard);
|
|
|
|
|
|
|
|
static LIST_HEAD(gmap_notifier_list);
|
|
|
|
static DEFINE_SPINLOCK(gmap_notifier_lock);
|
|
|
|
|
|
|
|
/**
|
2016-03-08 10:54:42 +00:00
|
|
|
* gmap_register_pte_notifier - register a pte invalidation callback
|
2016-03-08 10:49:57 +00:00
|
|
|
* @nb: pointer to the gmap notifier block
|
|
|
|
*/
|
2016-03-08 10:54:42 +00:00
|
|
|
void gmap_register_pte_notifier(struct gmap_notifier *nb)
|
2016-03-08 10:49:57 +00:00
|
|
|
{
|
|
|
|
spin_lock(&gmap_notifier_lock);
|
2016-03-08 10:54:14 +00:00
|
|
|
list_add_rcu(&nb->list, &gmap_notifier_list);
|
2016-03-08 10:49:57 +00:00
|
|
|
spin_unlock(&gmap_notifier_lock);
|
|
|
|
}
|
2016-03-08 10:54:42 +00:00
|
|
|
EXPORT_SYMBOL_GPL(gmap_register_pte_notifier);
|
2016-03-08 10:49:57 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-08 10:54:42 +00:00
|
|
|
* gmap_unregister_pte_notifier - remove a pte invalidation callback
|
2016-03-08 10:49:57 +00:00
|
|
|
* @nb: pointer to the gmap notifier block
|
|
|
|
*/
|
2016-03-08 10:54:42 +00:00
|
|
|
void gmap_unregister_pte_notifier(struct gmap_notifier *nb)
|
2016-03-08 10:49:57 +00:00
|
|
|
{
|
|
|
|
spin_lock(&gmap_notifier_lock);
|
2016-03-08 10:54:14 +00:00
|
|
|
list_del_rcu(&nb->list);
|
2016-03-08 10:49:57 +00:00
|
|
|
spin_unlock(&gmap_notifier_lock);
|
2016-03-08 10:54:14 +00:00
|
|
|
synchronize_rcu();
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
2016-03-08 10:54:42 +00:00
|
|
|
EXPORT_SYMBOL_GPL(gmap_unregister_pte_notifier);
|
2016-03-08 10:49:57 +00:00
|
|
|
|
2016-03-08 10:52:54 +00:00
|
|
|
/**
|
|
|
|
* gmap_call_notifier - call all registered invalidation callbacks
|
|
|
|
* @gmap: pointer to guest mapping meta data structure
|
|
|
|
* @start: start virtual address in the guest address space
|
|
|
|
* @end: end virtual address in the guest address space
|
|
|
|
*/
|
|
|
|
static void gmap_call_notifier(struct gmap *gmap, unsigned long start,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
struct gmap_notifier *nb;
|
|
|
|
|
|
|
|
list_for_each_entry(nb, &gmap_notifier_list, list)
|
|
|
|
nb->notifier_call(gmap, start, end);
|
|
|
|
}
|
|
|
|
|
2016-03-08 10:49:57 +00:00
|
|
|
/**
|
2016-03-08 10:54:42 +00:00
|
|
|
* gmap_table_walk - walk the gmap page tables
|
|
|
|
* @gmap: pointer to guest mapping meta data structure
|
|
|
|
* @gaddr: virtual address in the guest address space
|
2016-03-08 11:12:18 +00:00
|
|
|
* @level: page table level to stop at
|
|
|
|
*
|
|
|
|
* Returns a table entry pointer for the given guest address and @level
|
|
|
|
* @level=0 : returns a pointer to a page table table entry (or NULL)
|
|
|
|
* @level=1 : returns a pointer to a segment table entry (or NULL)
|
|
|
|
* @level=2 : returns a pointer to a region-3 table entry (or NULL)
|
|
|
|
* @level=3 : returns a pointer to a region-2 table entry (or NULL)
|
|
|
|
* @level=4 : returns a pointer to a region-1 table entry (or NULL)
|
|
|
|
*
|
|
|
|
* Returns NULL if the gmap page tables could not be walked to the
|
|
|
|
* requested level.
|
2016-03-08 10:54:42 +00:00
|
|
|
*
|
2016-03-08 11:12:18 +00:00
|
|
|
* Note: Can also be called for shadow gmaps.
|
2016-03-08 10:54:42 +00:00
|
|
|
*/
|
|
|
|
static inline unsigned long *gmap_table_walk(struct gmap *gmap,
|
2016-03-08 11:12:18 +00:00
|
|
|
unsigned long gaddr, int level)
|
2016-03-08 10:54:42 +00:00
|
|
|
{
|
2020-04-03 15:30:46 +00:00
|
|
|
const int asce_type = gmap->asce & _ASCE_TYPE_MASK;
|
2020-04-03 15:30:50 +00:00
|
|
|
unsigned long *table = gmap->table;
|
2016-03-08 10:54:42 +00:00
|
|
|
|
2016-03-08 11:12:18 +00:00
|
|
|
if (gmap_is_shadow(gmap) && gmap->removed)
|
|
|
|
return NULL;
|
2020-04-03 15:30:46 +00:00
|
|
|
|
2020-04-03 15:30:50 +00:00
|
|
|
if (WARN_ON_ONCE(level > (asce_type >> 2) + 1))
|
|
|
|
return NULL;
|
|
|
|
|
2020-04-03 15:30:46 +00:00
|
|
|
if (asce_type != _ASCE_TYPE_REGION1 &&
|
|
|
|
gaddr & (-1UL << (31 + (asce_type >> 2) * 11)))
|
2016-03-08 11:12:18 +00:00
|
|
|
return NULL;
|
2020-04-03 15:30:46 +00:00
|
|
|
|
2020-04-03 15:30:50 +00:00
|
|
|
switch (asce_type) {
|
2016-03-08 10:54:42 +00:00
|
|
|
case _ASCE_TYPE_REGION1:
|
2017-07-05 05:37:27 +00:00
|
|
|
table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
|
2016-03-08 11:12:18 +00:00
|
|
|
if (level == 4)
|
|
|
|
break;
|
2016-03-08 10:54:42 +00:00
|
|
|
if (*table & _REGION_ENTRY_INVALID)
|
|
|
|
return NULL;
|
2022-10-20 14:31:55 +00:00
|
|
|
table = __va(*table & _REGION_ENTRY_ORIGIN);
|
2020-03-11 04:51:32 +00:00
|
|
|
fallthrough;
|
2016-03-08 10:54:42 +00:00
|
|
|
case _ASCE_TYPE_REGION2:
|
2017-07-05 05:37:27 +00:00
|
|
|
table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
|
2016-03-08 11:12:18 +00:00
|
|
|
if (level == 3)
|
|
|
|
break;
|
2016-03-08 10:54:42 +00:00
|
|
|
if (*table & _REGION_ENTRY_INVALID)
|
|
|
|
return NULL;
|
2022-10-20 14:31:55 +00:00
|
|
|
table = __va(*table & _REGION_ENTRY_ORIGIN);
|
2020-03-11 04:51:32 +00:00
|
|
|
fallthrough;
|
2016-03-08 10:54:42 +00:00
|
|
|
case _ASCE_TYPE_REGION3:
|
2017-07-05 05:37:27 +00:00
|
|
|
table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
|
2016-03-08 11:12:18 +00:00
|
|
|
if (level == 2)
|
|
|
|
break;
|
2016-03-08 10:54:42 +00:00
|
|
|
if (*table & _REGION_ENTRY_INVALID)
|
|
|
|
return NULL;
|
2022-10-20 14:31:55 +00:00
|
|
|
table = __va(*table & _REGION_ENTRY_ORIGIN);
|
2020-03-11 04:51:32 +00:00
|
|
|
fallthrough;
|
2016-03-08 10:54:42 +00:00
|
|
|
case _ASCE_TYPE_SEGMENT:
|
2017-07-05 05:37:27 +00:00
|
|
|
table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
|
2016-03-08 11:12:18 +00:00
|
|
|
if (level == 1)
|
|
|
|
break;
|
|
|
|
if (*table & _REGION_ENTRY_INVALID)
|
|
|
|
return NULL;
|
2022-10-20 14:31:55 +00:00
|
|
|
table = __va(*table & _SEGMENT_ENTRY_ORIGIN);
|
2017-07-05 05:37:27 +00:00
|
|
|
table += (gaddr & _PAGE_INDEX) >> _PAGE_SHIFT;
|
2016-03-08 10:54:42 +00:00
|
|
|
}
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_pte_op_walk - walk the gmap page table, get the page table lock
|
|
|
|
* and return the pte pointer
|
|
|
|
* @gmap: pointer to guest mapping meta data structure
|
|
|
|
* @gaddr: virtual address in the guest address space
|
|
|
|
* @ptl: pointer to the spinlock pointer
|
|
|
|
*
|
|
|
|
* Returns a pointer to the locked pte for a guest address, or NULL
|
|
|
|
*/
|
|
|
|
static pte_t *gmap_pte_op_walk(struct gmap *gmap, unsigned long gaddr,
|
|
|
|
spinlock_t **ptl)
|
|
|
|
{
|
|
|
|
unsigned long *table;
|
|
|
|
|
2017-11-10 15:18:05 +00:00
|
|
|
BUG_ON(gmap_is_shadow(gmap));
|
2016-03-08 10:54:42 +00:00
|
|
|
/* Walk the gmap page table, lock and get pte pointer */
|
2016-03-08 11:12:18 +00:00
|
|
|
table = gmap_table_walk(gmap, gaddr, 1); /* get segment pointer */
|
2017-11-10 15:18:05 +00:00
|
|
|
if (!table || *table & _SEGMENT_ENTRY_INVALID)
|
2016-03-08 10:54:42 +00:00
|
|
|
return NULL;
|
|
|
|
return pte_alloc_map_lock(gmap->mm, (pmd_t *) table, gaddr, ptl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_pte_op_fixup - force a page in and connect the gmap page table
|
|
|
|
* @gmap: pointer to guest mapping meta data structure
|
|
|
|
* @gaddr: virtual address in the guest address space
|
|
|
|
* @vmaddr: address in the host process address space
|
2016-06-13 08:49:04 +00:00
|
|
|
* @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
|
2016-03-08 10:54:42 +00:00
|
|
|
*
|
|
|
|
* Returns 0 if the caller can retry __gmap_translate (might fail again),
|
|
|
|
* -ENOMEM if out of memory and -EFAULT if anything goes wrong while fixing
|
|
|
|
* up or connecting the gmap page table.
|
|
|
|
*/
|
|
|
|
static int gmap_pte_op_fixup(struct gmap *gmap, unsigned long gaddr,
|
2016-06-13 08:49:04 +00:00
|
|
|
unsigned long vmaddr, int prot)
|
2016-03-08 10:54:42 +00:00
|
|
|
{
|
|
|
|
struct mm_struct *mm = gmap->mm;
|
2016-06-13 08:49:04 +00:00
|
|
|
unsigned int fault_flags;
|
2016-03-08 10:54:42 +00:00
|
|
|
bool unlocked = false;
|
|
|
|
|
2016-03-08 11:12:18 +00:00
|
|
|
BUG_ON(gmap_is_shadow(gmap));
|
2016-06-13 08:49:04 +00:00
|
|
|
fault_flags = (prot == PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
|
2020-08-12 01:39:01 +00:00
|
|
|
if (fixup_user_fault(mm, vmaddr, fault_flags, &unlocked))
|
2016-03-08 10:54:42 +00:00
|
|
|
return -EFAULT;
|
|
|
|
if (unlocked)
|
2020-06-09 04:33:54 +00:00
|
|
|
/* lost mmap_lock, caller has to retry __gmap_translate */
|
2016-03-08 10:54:42 +00:00
|
|
|
return 0;
|
|
|
|
/* Connect the page tables */
|
|
|
|
return __gmap_link(gmap, gaddr, vmaddr);
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-08 10:54:42 +00:00
|
|
|
* gmap_pte_op_end - release the page table lock
|
2023-06-08 19:29:13 +00:00
|
|
|
* @ptep: pointer to the locked pte
|
|
|
|
* @ptl: pointer to the page table spinlock
|
2016-03-08 10:54:42 +00:00
|
|
|
*/
|
2023-06-08 19:29:13 +00:00
|
|
|
static void gmap_pte_op_end(pte_t *ptep, spinlock_t *ptl)
|
2016-03-08 10:54:42 +00:00
|
|
|
{
|
2023-06-08 19:29:13 +00:00
|
|
|
pte_unmap_unlock(ptep, ptl);
|
2018-07-13 10:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_pmd_op_walk - walk the gmap tables, get the guest table lock
|
|
|
|
* and return the pmd pointer
|
|
|
|
* @gmap: pointer to guest mapping meta data structure
|
|
|
|
* @gaddr: virtual address in the guest address space
|
|
|
|
*
|
|
|
|
* Returns a pointer to the pmd for a guest address, or NULL
|
|
|
|
*/
|
|
|
|
static inline pmd_t *gmap_pmd_op_walk(struct gmap *gmap, unsigned long gaddr)
|
|
|
|
{
|
|
|
|
pmd_t *pmdp;
|
|
|
|
|
|
|
|
BUG_ON(gmap_is_shadow(gmap));
|
|
|
|
pmdp = (pmd_t *) gmap_table_walk(gmap, gaddr, 1);
|
2018-08-06 15:54:07 +00:00
|
|
|
if (!pmdp)
|
|
|
|
return NULL;
|
2018-07-13 10:28:16 +00:00
|
|
|
|
2018-08-06 15:54:07 +00:00
|
|
|
/* without huge pages, there is no need to take the table lock */
|
|
|
|
if (!gmap->mm->context.allow_gmap_hpage_1m)
|
|
|
|
return pmd_none(*pmdp) ? NULL : pmdp;
|
|
|
|
|
|
|
|
spin_lock(&gmap->guest_table_lock);
|
|
|
|
if (pmd_none(*pmdp)) {
|
2018-07-13 10:28:16 +00:00
|
|
|
spin_unlock(&gmap->guest_table_lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 4k page table entries are locked via the pte (pte_alloc_map_lock). */
|
|
|
|
if (!pmd_large(*pmdp))
|
|
|
|
spin_unlock(&gmap->guest_table_lock);
|
|
|
|
return pmdp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_pmd_op_end - release the guest_table_lock if needed
|
|
|
|
* @gmap: pointer to the guest mapping meta data structure
|
|
|
|
* @pmdp: pointer to the pmd
|
|
|
|
*/
|
|
|
|
static inline void gmap_pmd_op_end(struct gmap *gmap, pmd_t *pmdp)
|
|
|
|
{
|
|
|
|
if (pmd_large(*pmdp))
|
|
|
|
spin_unlock(&gmap->guest_table_lock);
|
|
|
|
}
|
|
|
|
|
2018-07-13 10:28:21 +00:00
|
|
|
/*
|
|
|
|
* gmap_protect_pmd - remove access rights to memory and set pmd notification bits
|
|
|
|
* @pmdp: pointer to the pmd to be protected
|
|
|
|
* @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
|
|
|
|
* @bits: notification bits to set
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* 0 if successfully protected
|
|
|
|
* -EAGAIN if a fixup is needed
|
|
|
|
* -EINVAL if unsupported notifier bits have been specified
|
|
|
|
*
|
2020-06-09 04:33:54 +00:00
|
|
|
* Expected to be called with sg->mm->mmap_lock in read and
|
2018-07-13 10:28:21 +00:00
|
|
|
* guest_table_lock held.
|
|
|
|
*/
|
|
|
|
static int gmap_protect_pmd(struct gmap *gmap, unsigned long gaddr,
|
|
|
|
pmd_t *pmdp, int prot, unsigned long bits)
|
|
|
|
{
|
|
|
|
int pmd_i = pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID;
|
|
|
|
int pmd_p = pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT;
|
2018-07-17 12:21:22 +00:00
|
|
|
pmd_t new = *pmdp;
|
2018-07-13 10:28:21 +00:00
|
|
|
|
|
|
|
/* Fixup needed */
|
|
|
|
if ((pmd_i && (prot != PROT_NONE)) || (pmd_p && (prot == PROT_WRITE)))
|
|
|
|
return -EAGAIN;
|
|
|
|
|
2018-07-17 12:21:22 +00:00
|
|
|
if (prot == PROT_NONE && !pmd_i) {
|
2022-02-21 20:25:09 +00:00
|
|
|
new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_INVALID));
|
2018-07-17 12:21:22 +00:00
|
|
|
gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prot == PROT_READ && !pmd_p) {
|
2022-02-21 20:25:09 +00:00
|
|
|
new = clear_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_INVALID));
|
|
|
|
new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_PROTECT));
|
2018-07-17 12:21:22 +00:00
|
|
|
gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
|
|
|
|
}
|
|
|
|
|
2018-07-13 10:28:21 +00:00
|
|
|
if (bits & GMAP_NOTIFY_MPROT)
|
2022-02-21 19:50:07 +00:00
|
|
|
set_pmd(pmdp, set_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_IN)));
|
2018-07-13 10:28:21 +00:00
|
|
|
|
|
|
|
/* Shadow GMAP protection needs split PMDs */
|
|
|
|
if (bits & GMAP_NOTIFY_SHADOW)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-13 10:28:16 +00:00
|
|
|
/*
|
|
|
|
* gmap_protect_pte - remove access rights to memory and set pgste bits
|
|
|
|
* @gmap: pointer to guest mapping meta data structure
|
|
|
|
* @gaddr: virtual address in the guest address space
|
|
|
|
* @pmdp: pointer to the pmd associated with the pte
|
|
|
|
* @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
|
2018-07-13 10:28:18 +00:00
|
|
|
* @bits: notification bits to set
|
2018-07-13 10:28:16 +00:00
|
|
|
*
|
|
|
|
* Returns 0 if successfully protected, -ENOMEM if out of memory and
|
|
|
|
* -EAGAIN if a fixup is needed.
|
|
|
|
*
|
2020-06-09 04:33:54 +00:00
|
|
|
* Expected to be called with sg->mm->mmap_lock in read
|
2018-07-13 10:28:16 +00:00
|
|
|
*/
|
|
|
|
static int gmap_protect_pte(struct gmap *gmap, unsigned long gaddr,
|
|
|
|
pmd_t *pmdp, int prot, unsigned long bits)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
pte_t *ptep;
|
2023-06-08 19:29:13 +00:00
|
|
|
spinlock_t *ptl;
|
2018-07-13 10:28:18 +00:00
|
|
|
unsigned long pbits = 0;
|
2018-07-13 10:28:16 +00:00
|
|
|
|
|
|
|
if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
|
|
|
|
return -EAGAIN;
|
|
|
|
|
|
|
|
ptep = pte_alloc_map_lock(gmap->mm, pmdp, gaddr, &ptl);
|
|
|
|
if (!ptep)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-07-13 10:28:18 +00:00
|
|
|
pbits |= (bits & GMAP_NOTIFY_MPROT) ? PGSTE_IN_BIT : 0;
|
|
|
|
pbits |= (bits & GMAP_NOTIFY_SHADOW) ? PGSTE_VSIE_BIT : 0;
|
2018-07-13 10:28:16 +00:00
|
|
|
/* Protect and unlock. */
|
2018-07-13 10:28:18 +00:00
|
|
|
rc = ptep_force_prot(gmap->mm, gaddr, ptep, prot, pbits);
|
2023-06-08 19:29:13 +00:00
|
|
|
gmap_pte_op_end(ptep, ptl);
|
2018-07-13 10:28:16 +00:00
|
|
|
return rc;
|
2016-03-08 10:54:42 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 11:12:18 +00:00
|
|
|
/*
|
|
|
|
* gmap_protect_range - remove access rights to memory and set pgste bits
|
2016-03-08 10:49:57 +00:00
|
|
|
* @gmap: pointer to guest mapping meta data structure
|
|
|
|
* @gaddr: virtual address in the guest address space
|
|
|
|
* @len: size of area
|
2016-03-08 11:12:18 +00:00
|
|
|
* @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
|
|
|
|
* @bits: pgste notification bits to set
|
|
|
|
*
|
|
|
|
* Returns 0 if successfully protected, -ENOMEM if out of memory and
|
|
|
|
* -EFAULT if gaddr is invalid (or mapping for shadows is missing).
|
|
|
|
*
|
2020-06-09 04:33:54 +00:00
|
|
|
* Called with sg->mm->mmap_lock in read.
|
2016-03-08 10:49:57 +00:00
|
|
|
*/
|
2016-03-08 11:12:18 +00:00
|
|
|
static int gmap_protect_range(struct gmap *gmap, unsigned long gaddr,
|
|
|
|
unsigned long len, int prot, unsigned long bits)
|
2016-03-08 10:49:57 +00:00
|
|
|
{
|
2018-07-13 10:28:21 +00:00
|
|
|
unsigned long vmaddr, dist;
|
2018-07-13 10:28:16 +00:00
|
|
|
pmd_t *pmdp;
|
2016-03-08 11:12:18 +00:00
|
|
|
int rc;
|
|
|
|
|
2017-11-10 15:18:05 +00:00
|
|
|
BUG_ON(gmap_is_shadow(gmap));
|
2016-03-08 11:12:18 +00:00
|
|
|
while (len) {
|
|
|
|
rc = -EAGAIN;
|
2018-07-13 10:28:16 +00:00
|
|
|
pmdp = gmap_pmd_op_walk(gmap, gaddr);
|
|
|
|
if (pmdp) {
|
2018-07-13 10:28:21 +00:00
|
|
|
if (!pmd_large(*pmdp)) {
|
|
|
|
rc = gmap_protect_pte(gmap, gaddr, pmdp, prot,
|
|
|
|
bits);
|
|
|
|
if (!rc) {
|
|
|
|
len -= PAGE_SIZE;
|
|
|
|
gaddr += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rc = gmap_protect_pmd(gmap, gaddr, pmdp, prot,
|
|
|
|
bits);
|
|
|
|
if (!rc) {
|
|
|
|
dist = HPAGE_SIZE - (gaddr & ~HPAGE_MASK);
|
|
|
|
len = len < dist ? 0 : len - dist;
|
|
|
|
gaddr = (gaddr & HPAGE_MASK) + HPAGE_SIZE;
|
|
|
|
}
|
2018-07-13 10:28:16 +00:00
|
|
|
}
|
|
|
|
gmap_pmd_op_end(gmap, pmdp);
|
2016-03-08 11:12:18 +00:00
|
|
|
}
|
|
|
|
if (rc) {
|
2018-07-13 10:28:21 +00:00
|
|
|
if (rc == -EINVAL)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* -EAGAIN, fixup of userspace mm and gmap */
|
2016-03-08 11:12:18 +00:00
|
|
|
vmaddr = __gmap_translate(gmap, gaddr);
|
|
|
|
if (IS_ERR_VALUE(vmaddr))
|
|
|
|
return vmaddr;
|
2016-06-13 08:49:04 +00:00
|
|
|
rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, prot);
|
2016-03-08 11:12:18 +00:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-08 10:49:57 +00:00
|
|
|
|
2016-03-08 10:54:42 +00:00
|
|
|
/**
|
|
|
|
* gmap_mprotect_notify - change access rights for a range of ptes and
|
|
|
|
* call the notifier if any pte changes again
|
2016-03-08 10:49:57 +00:00
|
|
|
* @gmap: pointer to guest mapping meta data structure
|
|
|
|
* @gaddr: virtual address in the guest address space
|
|
|
|
* @len: size of area
|
2016-03-08 10:54:42 +00:00
|
|
|
* @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
|
2016-03-08 10:49:57 +00:00
|
|
|
*
|
2016-03-08 10:54:42 +00:00
|
|
|
* Returns 0 if for each page in the given range a gmap mapping exists,
|
|
|
|
* the new access rights could be set and the notifier could be armed.
|
|
|
|
* If the gmap mapping is missing for one or more pages -EFAULT is
|
|
|
|
* returned. If no memory could be allocated -ENOMEM is returned.
|
|
|
|
* This function establishes missing page table entries.
|
2016-03-08 10:49:57 +00:00
|
|
|
*/
|
2016-03-08 10:54:42 +00:00
|
|
|
int gmap_mprotect_notify(struct gmap *gmap, unsigned long gaddr,
|
|
|
|
unsigned long len, int prot)
|
2016-03-08 10:49:57 +00:00
|
|
|
{
|
2016-03-08 11:12:18 +00:00
|
|
|
int rc;
|
2016-03-08 10:49:57 +00:00
|
|
|
|
2016-03-08 11:12:18 +00:00
|
|
|
if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK) || gmap_is_shadow(gmap))
|
2016-03-08 10:49:57 +00:00
|
|
|
return -EINVAL;
|
2016-03-08 10:54:42 +00:00
|
|
|
if (!MACHINE_HAS_ESOP && prot == PROT_READ)
|
2016-03-08 10:49:57 +00:00
|
|
|
return -EINVAL;
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_read_lock(gmap->mm);
|
2018-07-13 10:28:18 +00:00
|
|
|
rc = gmap_protect_range(gmap, gaddr, len, prot, GMAP_NOTIFY_MPROT);
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_read_unlock(gmap->mm);
|
2016-03-08 11:12:18 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_mprotect_notify);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_read_table - get an unsigned long value from a guest page table using
|
|
|
|
* absolute addressing, without marking the page referenced.
|
|
|
|
* @gmap: pointer to guest mapping meta data structure
|
|
|
|
* @gaddr: virtual address in the guest address space
|
|
|
|
* @val: pointer to the unsigned long value to return
|
|
|
|
*
|
|
|
|
* Returns 0 if the value was read, -ENOMEM if out of memory and -EFAULT
|
2017-11-10 15:18:05 +00:00
|
|
|
* if reading using the virtual address failed. -EINVAL if called on a gmap
|
|
|
|
* shadow.
|
2016-03-08 11:12:18 +00:00
|
|
|
*
|
2020-06-09 04:33:54 +00:00
|
|
|
* Called with gmap->mm->mmap_lock in read.
|
2016-03-08 11:12:18 +00:00
|
|
|
*/
|
|
|
|
int gmap_read_table(struct gmap *gmap, unsigned long gaddr, unsigned long *val)
|
|
|
|
{
|
|
|
|
unsigned long address, vmaddr;
|
|
|
|
spinlock_t *ptl;
|
|
|
|
pte_t *ptep, pte;
|
|
|
|
int rc;
|
|
|
|
|
2017-11-10 15:18:05 +00:00
|
|
|
if (gmap_is_shadow(gmap))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2016-03-08 11:12:18 +00:00
|
|
|
while (1) {
|
2016-03-08 10:54:42 +00:00
|
|
|
rc = -EAGAIN;
|
|
|
|
ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
|
|
|
|
if (ptep) {
|
2016-03-08 11:12:18 +00:00
|
|
|
pte = *ptep;
|
|
|
|
if (pte_present(pte) && (pte_val(pte) & _PAGE_READ)) {
|
|
|
|
address = pte_val(pte) & PAGE_MASK;
|
|
|
|
address += gaddr & ~PAGE_MASK;
|
2022-10-20 14:31:55 +00:00
|
|
|
*val = *(unsigned long *)__va(address);
|
2022-02-21 19:50:07 +00:00
|
|
|
set_pte(ptep, set_pte_bit(*ptep, __pgprot(_PAGE_YOUNG)));
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Do *NOT* clear the _PAGE_INVALID bit! */
|
|
|
|
rc = 0;
|
|
|
|
}
|
2023-06-08 19:29:13 +00:00
|
|
|
gmap_pte_op_end(ptep, ptl);
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
2016-03-08 11:12:18 +00:00
|
|
|
if (!rc)
|
|
|
|
break;
|
|
|
|
vmaddr = __gmap_translate(gmap, gaddr);
|
|
|
|
if (IS_ERR_VALUE(vmaddr)) {
|
|
|
|
rc = vmaddr;
|
2016-03-08 10:49:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-06-13 08:49:04 +00:00
|
|
|
rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, PROT_READ);
|
2016-03-08 10:49:57 +00:00
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
2016-03-08 11:12:18 +00:00
|
|
|
EXPORT_SYMBOL_GPL(gmap_read_table);
|
2016-03-08 10:49:57 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-08 11:12:18 +00:00
|
|
|
* gmap_insert_rmap - add a rmap to the host_to_rmap radix tree
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @vmaddr: vm address associated with the rmap
|
|
|
|
* @rmap: pointer to the rmap structure
|
2016-03-08 10:49:57 +00:00
|
|
|
*
|
2016-03-08 11:12:18 +00:00
|
|
|
* Called with the sg->guest_table_lock
|
2016-03-08 10:49:57 +00:00
|
|
|
*/
|
2016-03-08 11:12:18 +00:00
|
|
|
static inline void gmap_insert_rmap(struct gmap *sg, unsigned long vmaddr,
|
|
|
|
struct gmap_rmap *rmap)
|
2016-03-08 10:49:57 +00:00
|
|
|
{
|
2022-04-29 15:15:26 +00:00
|
|
|
struct gmap_rmap *temp;
|
2017-05-09 11:44:43 +00:00
|
|
|
void __rcu **slot;
|
2016-03-08 10:49:57 +00:00
|
|
|
|
2016-03-08 11:12:18 +00:00
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
|
|
|
slot = radix_tree_lookup_slot(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
|
|
|
|
if (slot) {
|
|
|
|
rmap->next = radix_tree_deref_slot_protected(slot,
|
|
|
|
&sg->guest_table_lock);
|
2022-04-29 15:15:26 +00:00
|
|
|
for (temp = rmap->next; temp; temp = temp->next) {
|
|
|
|
if (temp->raddr == rmap->raddr) {
|
|
|
|
kfree(rmap);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-12-13 00:43:43 +00:00
|
|
|
radix_tree_replace_slot(&sg->host_to_rmap, slot, rmap);
|
2016-03-08 11:12:18 +00:00
|
|
|
} else {
|
|
|
|
rmap->next = NULL;
|
|
|
|
radix_tree_insert(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT,
|
|
|
|
rmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-23 21:26:18 +00:00
|
|
|
* gmap_protect_rmap - restrict access rights to memory (RO) and create an rmap
|
2016-03-08 11:12:18 +00:00
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @raddr: rmap address in the shadow gmap
|
|
|
|
* @paddr: address in the parent guest address space
|
|
|
|
* @len: length of the memory area to protect
|
|
|
|
*
|
|
|
|
* Returns 0 if successfully protected and the rmap was created, -ENOMEM
|
|
|
|
* if out of memory and -EFAULT if paddr is invalid.
|
|
|
|
*/
|
|
|
|
static int gmap_protect_rmap(struct gmap *sg, unsigned long raddr,
|
2018-01-23 21:26:18 +00:00
|
|
|
unsigned long paddr, unsigned long len)
|
2016-03-08 11:12:18 +00:00
|
|
|
{
|
|
|
|
struct gmap *parent;
|
|
|
|
struct gmap_rmap *rmap;
|
|
|
|
unsigned long vmaddr;
|
|
|
|
spinlock_t *ptl;
|
|
|
|
pte_t *ptep;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
|
|
|
parent = sg->parent;
|
|
|
|
while (len) {
|
|
|
|
vmaddr = __gmap_translate(parent, paddr);
|
|
|
|
if (IS_ERR_VALUE(vmaddr))
|
|
|
|
return vmaddr;
|
2020-11-09 12:14:35 +00:00
|
|
|
rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
|
2016-03-08 11:12:18 +00:00
|
|
|
if (!rmap)
|
|
|
|
return -ENOMEM;
|
|
|
|
rmap->raddr = raddr;
|
2020-11-09 12:14:35 +00:00
|
|
|
rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
|
2016-03-08 10:54:42 +00:00
|
|
|
if (rc) {
|
2016-03-08 11:12:18 +00:00
|
|
|
kfree(rmap);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
rc = -EAGAIN;
|
|
|
|
ptep = gmap_pte_op_walk(parent, paddr, &ptl);
|
|
|
|
if (ptep) {
|
|
|
|
spin_lock(&sg->guest_table_lock);
|
2018-01-23 21:26:18 +00:00
|
|
|
rc = ptep_force_prot(parent->mm, paddr, ptep, PROT_READ,
|
2016-03-08 11:12:18 +00:00
|
|
|
PGSTE_VSIE_BIT);
|
|
|
|
if (!rc)
|
|
|
|
gmap_insert_rmap(sg, vmaddr, rmap);
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
2023-06-08 19:29:13 +00:00
|
|
|
gmap_pte_op_end(ptep, ptl);
|
2016-03-08 11:12:18 +00:00
|
|
|
}
|
|
|
|
radix_tree_preload_end();
|
|
|
|
if (rc) {
|
|
|
|
kfree(rmap);
|
2018-01-23 21:26:18 +00:00
|
|
|
rc = gmap_pte_op_fixup(parent, paddr, vmaddr, PROT_READ);
|
2016-03-08 10:54:42 +00:00
|
|
|
if (rc)
|
2016-03-08 11:12:18 +00:00
|
|
|
return rc;
|
2016-03-08 10:49:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-03-08 11:12:18 +00:00
|
|
|
paddr += PAGE_SIZE;
|
2016-03-08 10:54:42 +00:00
|
|
|
len -= PAGE_SIZE;
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
2016-03-08 11:12:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define _SHADOW_RMAP_MASK 0x7
|
|
|
|
#define _SHADOW_RMAP_REGION1 0x5
|
|
|
|
#define _SHADOW_RMAP_REGION2 0x4
|
|
|
|
#define _SHADOW_RMAP_REGION3 0x3
|
|
|
|
#define _SHADOW_RMAP_SEGMENT 0x2
|
|
|
|
#define _SHADOW_RMAP_PGTABLE 0x1
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_idte_one - invalidate a single region or segment table entry
|
|
|
|
* @asce: region or segment table *origin* + table-type bits
|
|
|
|
* @vaddr: virtual address to identify the table entry to flush
|
|
|
|
*
|
|
|
|
* The invalid bit of a single region or segment table entry is set
|
|
|
|
* and the associated TLB entries depending on the entry are flushed.
|
|
|
|
* The table-type of the @asce identifies the portion of the @vaddr
|
|
|
|
* that is used as the invalidation index.
|
|
|
|
*/
|
|
|
|
static inline void gmap_idte_one(unsigned long asce, unsigned long vaddr)
|
|
|
|
{
|
|
|
|
asm volatile(
|
2022-02-25 09:39:02 +00:00
|
|
|
" idte %0,0,%1"
|
2016-03-08 11:12:18 +00:00
|
|
|
: : "a" (asce), "a" (vaddr) : "cc", "memory");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_unshadow_page - remove a page from a shadow page table
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @raddr: rmap address in the shadow guest address space
|
|
|
|
*
|
|
|
|
* Called with the sg->guest_table_lock
|
|
|
|
*/
|
|
|
|
static void gmap_unshadow_page(struct gmap *sg, unsigned long raddr)
|
|
|
|
{
|
|
|
|
unsigned long *table;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
|
|
|
table = gmap_table_walk(sg, raddr, 0); /* get page table pointer */
|
|
|
|
if (!table || *table & _PAGE_INVALID)
|
|
|
|
return;
|
2017-07-05 05:37:27 +00:00
|
|
|
gmap_call_notifier(sg, raddr, raddr + _PAGE_SIZE - 1);
|
2016-03-08 11:12:18 +00:00
|
|
|
ptep_unshadow_pte(sg->mm, raddr, (pte_t *) table);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __gmap_unshadow_pgt - remove all entries from a shadow page table
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @raddr: rmap address in the shadow guest address space
|
|
|
|
* @pgt: pointer to the start of a shadow page table
|
|
|
|
*
|
|
|
|
* Called with the sg->guest_table_lock
|
|
|
|
*/
|
|
|
|
static void __gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr,
|
|
|
|
unsigned long *pgt)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
2017-07-05 05:37:27 +00:00
|
|
|
for (i = 0; i < _PAGE_ENTRIES; i++, raddr += _PAGE_SIZE)
|
2016-03-08 11:12:18 +00:00
|
|
|
pgt[i] = _PAGE_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_unshadow_pgt - remove a shadow page table from a segment entry
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @raddr: address in the shadow guest address space
|
|
|
|
*
|
|
|
|
* Called with the sg->guest_table_lock
|
|
|
|
*/
|
|
|
|
static void gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr)
|
|
|
|
{
|
2022-10-20 14:31:55 +00:00
|
|
|
unsigned long *ste;
|
|
|
|
phys_addr_t sto, pgt;
|
2016-03-08 11:12:18 +00:00
|
|
|
struct page *page;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
|
|
|
ste = gmap_table_walk(sg, raddr, 1); /* get segment pointer */
|
2016-03-08 11:23:38 +00:00
|
|
|
if (!ste || !(*ste & _SEGMENT_ENTRY_ORIGIN))
|
2016-03-08 11:12:18 +00:00
|
|
|
return;
|
2017-07-05 05:37:27 +00:00
|
|
|
gmap_call_notifier(sg, raddr, raddr + _SEGMENT_SIZE - 1);
|
2022-10-20 14:31:55 +00:00
|
|
|
sto = __pa(ste - ((raddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT));
|
2016-03-08 11:12:18 +00:00
|
|
|
gmap_idte_one(sto | _ASCE_TYPE_SEGMENT, raddr);
|
2022-10-20 14:31:55 +00:00
|
|
|
pgt = *ste & _SEGMENT_ENTRY_ORIGIN;
|
2016-03-08 11:12:18 +00:00
|
|
|
*ste = _SEGMENT_ENTRY_EMPTY;
|
2022-10-20 14:31:55 +00:00
|
|
|
__gmap_unshadow_pgt(sg, raddr, __va(pgt));
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Free page table */
|
2022-10-20 14:31:55 +00:00
|
|
|
page = phys_to_page(pgt);
|
2016-03-08 11:12:18 +00:00
|
|
|
list_del(&page->lru);
|
|
|
|
page_table_free_pgste(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __gmap_unshadow_sgt - remove all entries from a shadow segment table
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @raddr: rmap address in the shadow guest address space
|
|
|
|
* @sgt: pointer to the start of a shadow segment table
|
|
|
|
*
|
|
|
|
* Called with the sg->guest_table_lock
|
|
|
|
*/
|
|
|
|
static void __gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr,
|
|
|
|
unsigned long *sgt)
|
|
|
|
{
|
|
|
|
struct page *page;
|
2022-10-20 14:31:55 +00:00
|
|
|
phys_addr_t pgt;
|
2016-03-08 11:12:18 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
2017-07-05 05:37:27 +00:00
|
|
|
for (i = 0; i < _CRST_ENTRIES; i++, raddr += _SEGMENT_SIZE) {
|
2016-03-08 11:23:38 +00:00
|
|
|
if (!(sgt[i] & _SEGMENT_ENTRY_ORIGIN))
|
2016-03-08 11:12:18 +00:00
|
|
|
continue;
|
2022-10-20 14:31:55 +00:00
|
|
|
pgt = sgt[i] & _REGION_ENTRY_ORIGIN;
|
2016-03-08 11:12:18 +00:00
|
|
|
sgt[i] = _SEGMENT_ENTRY_EMPTY;
|
2022-10-20 14:31:55 +00:00
|
|
|
__gmap_unshadow_pgt(sg, raddr, __va(pgt));
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Free page table */
|
2022-10-20 14:31:55 +00:00
|
|
|
page = phys_to_page(pgt);
|
2016-03-08 11:12:18 +00:00
|
|
|
list_del(&page->lru);
|
|
|
|
page_table_free_pgste(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_unshadow_sgt - remove a shadow segment table from a region-3 entry
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @raddr: rmap address in the shadow guest address space
|
|
|
|
*
|
|
|
|
* Called with the shadow->guest_table_lock
|
|
|
|
*/
|
|
|
|
static void gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr)
|
|
|
|
{
|
2022-10-20 14:31:55 +00:00
|
|
|
unsigned long r3o, *r3e;
|
|
|
|
phys_addr_t sgt;
|
2016-03-08 11:12:18 +00:00
|
|
|
struct page *page;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
|
|
|
r3e = gmap_table_walk(sg, raddr, 2); /* get region-3 pointer */
|
2016-03-08 11:23:38 +00:00
|
|
|
if (!r3e || !(*r3e & _REGION_ENTRY_ORIGIN))
|
2016-03-08 11:12:18 +00:00
|
|
|
return;
|
2017-07-05 05:37:27 +00:00
|
|
|
gmap_call_notifier(sg, raddr, raddr + _REGION3_SIZE - 1);
|
|
|
|
r3o = (unsigned long) (r3e - ((raddr & _REGION3_INDEX) >> _REGION3_SHIFT));
|
2022-10-20 14:31:55 +00:00
|
|
|
gmap_idte_one(__pa(r3o) | _ASCE_TYPE_REGION3, raddr);
|
|
|
|
sgt = *r3e & _REGION_ENTRY_ORIGIN;
|
2016-03-08 11:12:18 +00:00
|
|
|
*r3e = _REGION3_ENTRY_EMPTY;
|
2022-10-20 14:31:55 +00:00
|
|
|
__gmap_unshadow_sgt(sg, raddr, __va(sgt));
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Free segment table */
|
2022-10-20 14:31:55 +00:00
|
|
|
page = phys_to_page(sgt);
|
2016-03-08 11:12:18 +00:00
|
|
|
list_del(&page->lru);
|
2017-07-05 05:37:27 +00:00
|
|
|
__free_pages(page, CRST_ALLOC_ORDER);
|
2016-03-08 11:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __gmap_unshadow_r3t - remove all entries from a shadow region-3 table
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @raddr: address in the shadow guest address space
|
|
|
|
* @r3t: pointer to the start of a shadow region-3 table
|
|
|
|
*
|
|
|
|
* Called with the sg->guest_table_lock
|
|
|
|
*/
|
|
|
|
static void __gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr,
|
|
|
|
unsigned long *r3t)
|
|
|
|
{
|
|
|
|
struct page *page;
|
2022-10-20 14:31:55 +00:00
|
|
|
phys_addr_t sgt;
|
2016-03-08 11:12:18 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
2017-07-05 05:37:27 +00:00
|
|
|
for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION3_SIZE) {
|
2016-03-08 11:23:38 +00:00
|
|
|
if (!(r3t[i] & _REGION_ENTRY_ORIGIN))
|
2016-03-08 11:12:18 +00:00
|
|
|
continue;
|
2022-10-20 14:31:55 +00:00
|
|
|
sgt = r3t[i] & _REGION_ENTRY_ORIGIN;
|
2016-03-08 11:12:18 +00:00
|
|
|
r3t[i] = _REGION3_ENTRY_EMPTY;
|
2022-10-20 14:31:55 +00:00
|
|
|
__gmap_unshadow_sgt(sg, raddr, __va(sgt));
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Free segment table */
|
2022-10-20 14:31:55 +00:00
|
|
|
page = phys_to_page(sgt);
|
2016-03-08 11:12:18 +00:00
|
|
|
list_del(&page->lru);
|
2017-07-05 05:37:27 +00:00
|
|
|
__free_pages(page, CRST_ALLOC_ORDER);
|
2016-03-08 11:12:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_unshadow_r3t - remove a shadow region-3 table from a region-2 entry
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @raddr: rmap address in the shadow guest address space
|
|
|
|
*
|
|
|
|
* Called with the sg->guest_table_lock
|
|
|
|
*/
|
|
|
|
static void gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr)
|
|
|
|
{
|
2022-10-20 14:31:55 +00:00
|
|
|
unsigned long r2o, *r2e;
|
|
|
|
phys_addr_t r3t;
|
2016-03-08 11:12:18 +00:00
|
|
|
struct page *page;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
|
|
|
r2e = gmap_table_walk(sg, raddr, 3); /* get region-2 pointer */
|
2016-03-08 11:23:38 +00:00
|
|
|
if (!r2e || !(*r2e & _REGION_ENTRY_ORIGIN))
|
2016-03-08 11:12:18 +00:00
|
|
|
return;
|
2017-07-05 05:37:27 +00:00
|
|
|
gmap_call_notifier(sg, raddr, raddr + _REGION2_SIZE - 1);
|
|
|
|
r2o = (unsigned long) (r2e - ((raddr & _REGION2_INDEX) >> _REGION2_SHIFT));
|
2022-10-20 14:31:55 +00:00
|
|
|
gmap_idte_one(__pa(r2o) | _ASCE_TYPE_REGION2, raddr);
|
|
|
|
r3t = *r2e & _REGION_ENTRY_ORIGIN;
|
2016-03-08 11:12:18 +00:00
|
|
|
*r2e = _REGION2_ENTRY_EMPTY;
|
2022-10-20 14:31:55 +00:00
|
|
|
__gmap_unshadow_r3t(sg, raddr, __va(r3t));
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Free region 3 table */
|
2022-10-20 14:31:55 +00:00
|
|
|
page = phys_to_page(r3t);
|
2016-03-08 11:12:18 +00:00
|
|
|
list_del(&page->lru);
|
2017-07-05 05:37:27 +00:00
|
|
|
__free_pages(page, CRST_ALLOC_ORDER);
|
2016-03-08 11:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __gmap_unshadow_r2t - remove all entries from a shadow region-2 table
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @raddr: rmap address in the shadow guest address space
|
|
|
|
* @r2t: pointer to the start of a shadow region-2 table
|
|
|
|
*
|
|
|
|
* Called with the sg->guest_table_lock
|
|
|
|
*/
|
|
|
|
static void __gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr,
|
|
|
|
unsigned long *r2t)
|
|
|
|
{
|
2022-10-20 14:31:55 +00:00
|
|
|
phys_addr_t r3t;
|
2016-03-08 11:12:18 +00:00
|
|
|
struct page *page;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
2017-07-05 05:37:27 +00:00
|
|
|
for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION2_SIZE) {
|
2016-03-08 11:23:38 +00:00
|
|
|
if (!(r2t[i] & _REGION_ENTRY_ORIGIN))
|
2016-03-08 11:12:18 +00:00
|
|
|
continue;
|
2022-10-20 14:31:55 +00:00
|
|
|
r3t = r2t[i] & _REGION_ENTRY_ORIGIN;
|
2016-03-08 11:12:18 +00:00
|
|
|
r2t[i] = _REGION2_ENTRY_EMPTY;
|
2022-10-20 14:31:55 +00:00
|
|
|
__gmap_unshadow_r3t(sg, raddr, __va(r3t));
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Free region 3 table */
|
2022-10-20 14:31:55 +00:00
|
|
|
page = phys_to_page(r3t);
|
2016-03-08 11:12:18 +00:00
|
|
|
list_del(&page->lru);
|
2017-07-05 05:37:27 +00:00
|
|
|
__free_pages(page, CRST_ALLOC_ORDER);
|
2016-03-08 11:12:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_unshadow_r2t - remove a shadow region-2 table from a region-1 entry
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @raddr: rmap address in the shadow guest address space
|
|
|
|
*
|
|
|
|
* Called with the sg->guest_table_lock
|
|
|
|
*/
|
|
|
|
static void gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr)
|
|
|
|
{
|
2022-10-20 14:31:55 +00:00
|
|
|
unsigned long r1o, *r1e;
|
2016-03-08 11:12:18 +00:00
|
|
|
struct page *page;
|
2022-10-20 14:31:55 +00:00
|
|
|
phys_addr_t r2t;
|
2016-03-08 11:12:18 +00:00
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
|
|
|
r1e = gmap_table_walk(sg, raddr, 4); /* get region-1 pointer */
|
2016-03-08 11:23:38 +00:00
|
|
|
if (!r1e || !(*r1e & _REGION_ENTRY_ORIGIN))
|
2016-03-08 11:12:18 +00:00
|
|
|
return;
|
2017-07-05 05:37:27 +00:00
|
|
|
gmap_call_notifier(sg, raddr, raddr + _REGION1_SIZE - 1);
|
|
|
|
r1o = (unsigned long) (r1e - ((raddr & _REGION1_INDEX) >> _REGION1_SHIFT));
|
2022-10-20 14:31:55 +00:00
|
|
|
gmap_idte_one(__pa(r1o) | _ASCE_TYPE_REGION1, raddr);
|
|
|
|
r2t = *r1e & _REGION_ENTRY_ORIGIN;
|
2016-03-08 11:12:18 +00:00
|
|
|
*r1e = _REGION1_ENTRY_EMPTY;
|
2022-10-20 14:31:55 +00:00
|
|
|
__gmap_unshadow_r2t(sg, raddr, __va(r2t));
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Free region 2 table */
|
2022-10-20 14:31:55 +00:00
|
|
|
page = phys_to_page(r2t);
|
2016-03-08 11:12:18 +00:00
|
|
|
list_del(&page->lru);
|
2017-07-05 05:37:27 +00:00
|
|
|
__free_pages(page, CRST_ALLOC_ORDER);
|
2016-03-08 11:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __gmap_unshadow_r1t - remove all entries from a shadow region-1 table
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @raddr: rmap address in the shadow guest address space
|
|
|
|
* @r1t: pointer to the start of a shadow region-1 table
|
|
|
|
*
|
|
|
|
* Called with the shadow->guest_table_lock
|
|
|
|
*/
|
|
|
|
static void __gmap_unshadow_r1t(struct gmap *sg, unsigned long raddr,
|
|
|
|
unsigned long *r1t)
|
|
|
|
{
|
2022-10-20 14:31:55 +00:00
|
|
|
unsigned long asce;
|
2016-03-08 11:12:18 +00:00
|
|
|
struct page *page;
|
2022-10-20 14:31:55 +00:00
|
|
|
phys_addr_t r2t;
|
2016-03-08 11:12:18 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
2022-10-20 14:31:55 +00:00
|
|
|
asce = __pa(r1t) | _ASCE_TYPE_REGION1;
|
2017-07-05 05:37:27 +00:00
|
|
|
for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION1_SIZE) {
|
2016-03-08 11:23:38 +00:00
|
|
|
if (!(r1t[i] & _REGION_ENTRY_ORIGIN))
|
2016-03-08 11:12:18 +00:00
|
|
|
continue;
|
2022-10-20 14:31:55 +00:00
|
|
|
r2t = r1t[i] & _REGION_ENTRY_ORIGIN;
|
|
|
|
__gmap_unshadow_r2t(sg, raddr, __va(r2t));
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Clear entry and flush translation r1t -> r2t */
|
|
|
|
gmap_idte_one(asce, raddr);
|
|
|
|
r1t[i] = _REGION1_ENTRY_EMPTY;
|
|
|
|
/* Free region 2 table */
|
2022-10-20 14:31:55 +00:00
|
|
|
page = phys_to_page(r2t);
|
2016-03-08 11:12:18 +00:00
|
|
|
list_del(&page->lru);
|
2017-07-05 05:37:27 +00:00
|
|
|
__free_pages(page, CRST_ALLOC_ORDER);
|
2016-03-08 11:12:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_unshadow - remove a shadow page table completely
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
*
|
|
|
|
* Called with sg->guest_table_lock
|
|
|
|
*/
|
|
|
|
static void gmap_unshadow(struct gmap *sg)
|
|
|
|
{
|
|
|
|
unsigned long *table;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
|
|
|
if (sg->removed)
|
|
|
|
return;
|
|
|
|
sg->removed = 1;
|
|
|
|
gmap_call_notifier(sg, 0, -1UL);
|
2016-04-15 10:45:45 +00:00
|
|
|
gmap_flush_tlb(sg);
|
2022-10-20 14:31:55 +00:00
|
|
|
table = __va(sg->asce & _ASCE_ORIGIN);
|
2016-03-08 11:12:18 +00:00
|
|
|
switch (sg->asce & _ASCE_TYPE_MASK) {
|
|
|
|
case _ASCE_TYPE_REGION1:
|
|
|
|
__gmap_unshadow_r1t(sg, 0, table);
|
|
|
|
break;
|
|
|
|
case _ASCE_TYPE_REGION2:
|
|
|
|
__gmap_unshadow_r2t(sg, 0, table);
|
|
|
|
break;
|
|
|
|
case _ASCE_TYPE_REGION3:
|
|
|
|
__gmap_unshadow_r3t(sg, 0, table);
|
|
|
|
break;
|
|
|
|
case _ASCE_TYPE_SEGMENT:
|
|
|
|
__gmap_unshadow_sgt(sg, 0, table);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_find_shadow - find a specific asce in the list of shadow tables
|
|
|
|
* @parent: pointer to the parent gmap
|
|
|
|
* @asce: ASCE for which the shadow table is created
|
2016-03-08 11:17:40 +00:00
|
|
|
* @edat_level: edat level to be used for the shadow translation
|
2016-03-08 11:12:18 +00:00
|
|
|
*
|
|
|
|
* Returns the pointer to a gmap if a shadow table with the given asce is
|
2016-03-08 11:30:46 +00:00
|
|
|
* already available, ERR_PTR(-EAGAIN) if another one is just being created,
|
|
|
|
* otherwise NULL
|
2016-03-08 11:12:18 +00:00
|
|
|
*/
|
2016-03-08 11:17:40 +00:00
|
|
|
static struct gmap *gmap_find_shadow(struct gmap *parent, unsigned long asce,
|
|
|
|
int edat_level)
|
2016-03-08 11:12:18 +00:00
|
|
|
{
|
|
|
|
struct gmap *sg;
|
|
|
|
|
|
|
|
list_for_each_entry(sg, &parent->children, list) {
|
2016-03-08 11:17:40 +00:00
|
|
|
if (sg->orig_asce != asce || sg->edat_level != edat_level ||
|
|
|
|
sg->removed)
|
2016-03-08 11:12:18 +00:00
|
|
|
continue;
|
2016-03-08 11:30:46 +00:00
|
|
|
if (!sg->initialized)
|
|
|
|
return ERR_PTR(-EAGAIN);
|
2019-08-08 07:18:26 +00:00
|
|
|
refcount_inc(&sg->ref_count);
|
2016-03-08 11:12:18 +00:00
|
|
|
return sg;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-27 16:57:33 +00:00
|
|
|
/**
|
|
|
|
* gmap_shadow_valid - check if a shadow guest address space matches the
|
|
|
|
* given properties and is still valid
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @asce: ASCE for which the shadow table is requested
|
|
|
|
* @edat_level: edat level to be used for the shadow translation
|
|
|
|
*
|
|
|
|
* Returns 1 if the gmap shadow is still valid and matches the given
|
|
|
|
* properties, the caller can continue using it. Returns 0 otherwise, the
|
|
|
|
* caller has to request a new shadow gmap in this case.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
|
|
|
|
{
|
|
|
|
if (sg->removed)
|
|
|
|
return 0;
|
|
|
|
return sg->orig_asce == asce && sg->edat_level == edat_level;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_shadow_valid);
|
|
|
|
|
2016-03-08 11:12:18 +00:00
|
|
|
/**
|
|
|
|
* gmap_shadow - create/find a shadow guest address space
|
|
|
|
* @parent: pointer to the parent gmap
|
|
|
|
* @asce: ASCE for which the shadow table is created
|
2016-03-08 11:17:40 +00:00
|
|
|
* @edat_level: edat level to be used for the shadow translation
|
2016-03-08 11:12:18 +00:00
|
|
|
*
|
|
|
|
* The pages of the top level page table referred by the asce parameter
|
|
|
|
* will be set to read-only and marked in the PGSTEs of the kvm process.
|
|
|
|
* The shadow table will be removed automatically on any change to the
|
|
|
|
* PTE mapping for the source table.
|
|
|
|
*
|
2016-03-08 11:30:46 +00:00
|
|
|
* Returns a guest address space structure, ERR_PTR(-ENOMEM) if out of memory,
|
|
|
|
* ERR_PTR(-EAGAIN) if the caller has to retry and ERR_PTR(-EFAULT) if the
|
|
|
|
* parent gmap table could not be protected.
|
2016-03-08 11:12:18 +00:00
|
|
|
*/
|
2016-03-08 11:17:40 +00:00
|
|
|
struct gmap *gmap_shadow(struct gmap *parent, unsigned long asce,
|
|
|
|
int edat_level)
|
2016-03-08 11:12:18 +00:00
|
|
|
{
|
|
|
|
struct gmap *sg, *new;
|
|
|
|
unsigned long limit;
|
|
|
|
int rc;
|
|
|
|
|
2018-07-13 10:28:37 +00:00
|
|
|
BUG_ON(parent->mm->context.allow_gmap_hpage_1m);
|
2016-03-08 11:12:18 +00:00
|
|
|
BUG_ON(gmap_is_shadow(parent));
|
|
|
|
spin_lock(&parent->shadow_lock);
|
2016-03-08 11:17:40 +00:00
|
|
|
sg = gmap_find_shadow(parent, asce, edat_level);
|
2016-03-08 11:12:18 +00:00
|
|
|
spin_unlock(&parent->shadow_lock);
|
|
|
|
if (sg)
|
|
|
|
return sg;
|
|
|
|
/* Create a new shadow gmap */
|
|
|
|
limit = -1UL >> (33 - (((asce & _ASCE_TYPE_MASK) >> 2) * 11));
|
2016-04-18 14:22:24 +00:00
|
|
|
if (asce & _ASCE_REAL_SPACE)
|
|
|
|
limit = -1UL;
|
2016-03-08 11:12:18 +00:00
|
|
|
new = gmap_alloc(limit);
|
|
|
|
if (!new)
|
2016-03-08 11:30:46 +00:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2016-03-08 11:12:18 +00:00
|
|
|
new->mm = parent->mm;
|
|
|
|
new->parent = gmap_get(parent);
|
|
|
|
new->orig_asce = asce;
|
2016-03-08 11:17:40 +00:00
|
|
|
new->edat_level = edat_level;
|
2016-03-08 11:30:46 +00:00
|
|
|
new->initialized = false;
|
|
|
|
spin_lock(&parent->shadow_lock);
|
|
|
|
/* Recheck if another CPU created the same shadow */
|
2016-03-08 11:17:40 +00:00
|
|
|
sg = gmap_find_shadow(parent, asce, edat_level);
|
2016-03-08 11:30:46 +00:00
|
|
|
if (sg) {
|
|
|
|
spin_unlock(&parent->shadow_lock);
|
|
|
|
gmap_free(new);
|
|
|
|
return sg;
|
|
|
|
}
|
2016-05-02 10:10:17 +00:00
|
|
|
if (asce & _ASCE_REAL_SPACE) {
|
|
|
|
/* only allow one real-space gmap shadow */
|
|
|
|
list_for_each_entry(sg, &parent->children, list) {
|
|
|
|
if (sg->orig_asce & _ASCE_REAL_SPACE) {
|
|
|
|
spin_lock(&sg->guest_table_lock);
|
|
|
|
gmap_unshadow(sg);
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
list_del(&sg->list);
|
|
|
|
gmap_put(sg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 07:18:26 +00:00
|
|
|
refcount_set(&new->ref_count, 2);
|
2016-03-08 11:30:46 +00:00
|
|
|
list_add(&new->list, &parent->children);
|
2016-04-18 14:22:24 +00:00
|
|
|
if (asce & _ASCE_REAL_SPACE) {
|
|
|
|
/* nothing to protect, return right away */
|
|
|
|
new->initialized = true;
|
|
|
|
spin_unlock(&parent->shadow_lock);
|
|
|
|
return new;
|
|
|
|
}
|
2016-03-08 11:30:46 +00:00
|
|
|
spin_unlock(&parent->shadow_lock);
|
|
|
|
/* protect after insertion, so it will get properly invalidated */
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_read_lock(parent->mm);
|
2016-03-08 11:12:18 +00:00
|
|
|
rc = gmap_protect_range(parent, asce & _ASCE_ORIGIN,
|
2017-07-05 05:37:27 +00:00
|
|
|
((asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE,
|
2018-07-13 10:28:18 +00:00
|
|
|
PROT_READ, GMAP_NOTIFY_SHADOW);
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_read_unlock(parent->mm);
|
2016-03-08 11:30:46 +00:00
|
|
|
spin_lock(&parent->shadow_lock);
|
|
|
|
new->initialized = true;
|
2016-03-08 11:12:18 +00:00
|
|
|
if (rc) {
|
2016-03-08 11:30:46 +00:00
|
|
|
list_del(&new->list);
|
2016-03-08 11:12:18 +00:00
|
|
|
gmap_free(new);
|
2016-03-08 11:30:46 +00:00
|
|
|
new = ERR_PTR(rc);
|
|
|
|
}
|
|
|
|
spin_unlock(&parent->shadow_lock);
|
|
|
|
return new;
|
2016-03-08 11:12:18 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_shadow);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_shadow_r2t - create an empty shadow region 2 table
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @saddr: faulting address in the shadow gmap
|
|
|
|
* @r2t: parent gmap address of the region 2 table to get shadowed
|
2016-04-18 14:22:24 +00:00
|
|
|
* @fake: r2t references contiguous guest memory block, not a r2t
|
2016-03-08 11:12:18 +00:00
|
|
|
*
|
|
|
|
* The r2t parameter specifies the address of the source table. The
|
|
|
|
* four pages of the source table are made read-only in the parent gmap
|
|
|
|
* address space. A write to the source table area @r2t will automatically
|
2023-06-28 14:23:20 +00:00
|
|
|
* remove the shadow r2 table and all of its descendants.
|
2016-03-08 11:12:18 +00:00
|
|
|
*
|
|
|
|
* Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
|
|
|
|
* shadow table structure is incomplete, -ENOMEM if out of memory and
|
|
|
|
* -EFAULT if an address in the parent gmap could not be resolved.
|
|
|
|
*
|
2020-06-09 04:33:54 +00:00
|
|
|
* Called with sg->mm->mmap_lock in read.
|
2016-03-08 11:12:18 +00:00
|
|
|
*/
|
2016-04-18 14:22:24 +00:00
|
|
|
int gmap_shadow_r2t(struct gmap *sg, unsigned long saddr, unsigned long r2t,
|
|
|
|
int fake)
|
2016-03-08 11:12:18 +00:00
|
|
|
{
|
|
|
|
unsigned long raddr, origin, offset, len;
|
2022-10-20 14:31:55 +00:00
|
|
|
unsigned long *table;
|
|
|
|
phys_addr_t s_r2t;
|
2016-03-08 11:12:18 +00:00
|
|
|
struct page *page;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
|
|
|
/* Allocate a shadow region second table */
|
2020-11-09 12:14:35 +00:00
|
|
|
page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
|
2016-03-08 11:12:18 +00:00
|
|
|
if (!page)
|
|
|
|
return -ENOMEM;
|
|
|
|
page->index = r2t & _REGION_ENTRY_ORIGIN;
|
2016-04-18 14:22:24 +00:00
|
|
|
if (fake)
|
|
|
|
page->index |= GMAP_SHADOW_FAKE_TABLE;
|
2022-10-20 14:31:55 +00:00
|
|
|
s_r2t = page_to_phys(page);
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Install shadow region second table */
|
|
|
|
spin_lock(&sg->guest_table_lock);
|
|
|
|
table = gmap_table_walk(sg, saddr, 4); /* get region-1 pointer */
|
|
|
|
if (!table) {
|
|
|
|
rc = -EAGAIN; /* Race with unshadow */
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
if (!(*table & _REGION_ENTRY_INVALID)) {
|
|
|
|
rc = 0; /* Already established */
|
|
|
|
goto out_free;
|
2016-03-08 11:23:38 +00:00
|
|
|
} else if (*table & _REGION_ENTRY_ORIGIN) {
|
|
|
|
rc = -EAGAIN; /* Race with shadow */
|
|
|
|
goto out_free;
|
2016-03-08 11:12:18 +00:00
|
|
|
}
|
2022-10-20 14:31:55 +00:00
|
|
|
crst_table_init(__va(s_r2t), _REGION2_ENTRY_EMPTY);
|
2016-03-08 11:23:38 +00:00
|
|
|
/* mark as invalid as long as the parent table is not protected */
|
2022-10-20 14:31:55 +00:00
|
|
|
*table = s_r2t | _REGION_ENTRY_LENGTH |
|
2016-03-08 11:23:38 +00:00
|
|
|
_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID;
|
2016-04-18 11:24:52 +00:00
|
|
|
if (sg->edat_level >= 1)
|
|
|
|
*table |= (r2t & _REGION_ENTRY_PROTECT);
|
2016-03-08 11:12:18 +00:00
|
|
|
list_add(&page->lru, &sg->crst_list);
|
2016-04-18 14:22:24 +00:00
|
|
|
if (fake) {
|
|
|
|
/* nothing to protect for fake tables */
|
|
|
|
*table &= ~_REGION_ENTRY_INVALID;
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-08 11:12:18 +00:00
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
/* Make r2t read-only in parent gmap page table */
|
2017-07-05 05:37:27 +00:00
|
|
|
raddr = (saddr & _REGION1_MASK) | _SHADOW_RMAP_REGION1;
|
2016-03-08 11:12:18 +00:00
|
|
|
origin = r2t & _REGION_ENTRY_ORIGIN;
|
2017-07-05 05:37:27 +00:00
|
|
|
offset = ((r2t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
|
|
|
|
len = ((r2t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
|
2018-01-23 21:26:18 +00:00
|
|
|
rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
|
2016-03-08 11:23:38 +00:00
|
|
|
spin_lock(&sg->guest_table_lock);
|
|
|
|
if (!rc) {
|
|
|
|
table = gmap_table_walk(sg, saddr, 4);
|
2022-10-20 14:31:55 +00:00
|
|
|
if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_r2t)
|
2016-03-08 11:23:38 +00:00
|
|
|
rc = -EAGAIN; /* Race with unshadow */
|
|
|
|
else
|
|
|
|
*table &= ~_REGION_ENTRY_INVALID;
|
|
|
|
} else {
|
2016-03-08 11:12:18 +00:00
|
|
|
gmap_unshadow_r2t(sg, raddr);
|
|
|
|
}
|
2016-03-08 11:23:38 +00:00
|
|
|
spin_unlock(&sg->guest_table_lock);
|
2016-03-08 11:12:18 +00:00
|
|
|
return rc;
|
|
|
|
out_free:
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
2017-07-05 05:37:27 +00:00
|
|
|
__free_pages(page, CRST_ALLOC_ORDER);
|
2016-03-08 10:49:57 +00:00
|
|
|
return rc;
|
|
|
|
}
|
2016-03-08 11:12:18 +00:00
|
|
|
EXPORT_SYMBOL_GPL(gmap_shadow_r2t);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_shadow_r3t - create a shadow region 3 table
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @saddr: faulting address in the shadow gmap
|
|
|
|
* @r3t: parent gmap address of the region 3 table to get shadowed
|
2016-04-18 14:22:24 +00:00
|
|
|
* @fake: r3t references contiguous guest memory block, not a r3t
|
2016-03-08 11:12:18 +00:00
|
|
|
*
|
|
|
|
* Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
|
|
|
|
* shadow table structure is incomplete, -ENOMEM if out of memory and
|
|
|
|
* -EFAULT if an address in the parent gmap could not be resolved.
|
|
|
|
*
|
2020-06-09 04:33:54 +00:00
|
|
|
* Called with sg->mm->mmap_lock in read.
|
2016-03-08 11:12:18 +00:00
|
|
|
*/
|
2016-04-18 14:22:24 +00:00
|
|
|
int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t,
|
|
|
|
int fake)
|
2016-03-08 11:12:18 +00:00
|
|
|
{
|
|
|
|
unsigned long raddr, origin, offset, len;
|
2022-10-20 14:31:55 +00:00
|
|
|
unsigned long *table;
|
|
|
|
phys_addr_t s_r3t;
|
2016-03-08 11:12:18 +00:00
|
|
|
struct page *page;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
|
|
|
/* Allocate a shadow region second table */
|
2020-11-09 12:14:35 +00:00
|
|
|
page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
|
2016-03-08 11:12:18 +00:00
|
|
|
if (!page)
|
|
|
|
return -ENOMEM;
|
|
|
|
page->index = r3t & _REGION_ENTRY_ORIGIN;
|
2016-04-18 14:22:24 +00:00
|
|
|
if (fake)
|
|
|
|
page->index |= GMAP_SHADOW_FAKE_TABLE;
|
2022-10-20 14:31:55 +00:00
|
|
|
s_r3t = page_to_phys(page);
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Install shadow region second table */
|
|
|
|
spin_lock(&sg->guest_table_lock);
|
|
|
|
table = gmap_table_walk(sg, saddr, 3); /* get region-2 pointer */
|
|
|
|
if (!table) {
|
|
|
|
rc = -EAGAIN; /* Race with unshadow */
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
if (!(*table & _REGION_ENTRY_INVALID)) {
|
|
|
|
rc = 0; /* Already established */
|
|
|
|
goto out_free;
|
2016-03-08 11:23:38 +00:00
|
|
|
} else if (*table & _REGION_ENTRY_ORIGIN) {
|
|
|
|
rc = -EAGAIN; /* Race with shadow */
|
2020-04-03 15:30:48 +00:00
|
|
|
goto out_free;
|
2016-03-08 11:12:18 +00:00
|
|
|
}
|
2022-10-20 14:31:55 +00:00
|
|
|
crst_table_init(__va(s_r3t), _REGION3_ENTRY_EMPTY);
|
2016-03-08 11:23:38 +00:00
|
|
|
/* mark as invalid as long as the parent table is not protected */
|
2022-10-20 14:31:55 +00:00
|
|
|
*table = s_r3t | _REGION_ENTRY_LENGTH |
|
2016-03-08 11:23:38 +00:00
|
|
|
_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID;
|
2016-04-18 11:24:52 +00:00
|
|
|
if (sg->edat_level >= 1)
|
|
|
|
*table |= (r3t & _REGION_ENTRY_PROTECT);
|
2016-03-08 11:12:18 +00:00
|
|
|
list_add(&page->lru, &sg->crst_list);
|
2016-04-18 14:22:24 +00:00
|
|
|
if (fake) {
|
|
|
|
/* nothing to protect for fake tables */
|
|
|
|
*table &= ~_REGION_ENTRY_INVALID;
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-08 11:12:18 +00:00
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
/* Make r3t read-only in parent gmap page table */
|
2017-07-05 05:37:27 +00:00
|
|
|
raddr = (saddr & _REGION2_MASK) | _SHADOW_RMAP_REGION2;
|
2016-03-08 11:12:18 +00:00
|
|
|
origin = r3t & _REGION_ENTRY_ORIGIN;
|
2017-07-05 05:37:27 +00:00
|
|
|
offset = ((r3t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
|
|
|
|
len = ((r3t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
|
2018-01-23 21:26:18 +00:00
|
|
|
rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
|
2016-03-08 11:23:38 +00:00
|
|
|
spin_lock(&sg->guest_table_lock);
|
|
|
|
if (!rc) {
|
|
|
|
table = gmap_table_walk(sg, saddr, 3);
|
2022-10-20 14:31:55 +00:00
|
|
|
if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_r3t)
|
2016-03-08 11:23:38 +00:00
|
|
|
rc = -EAGAIN; /* Race with unshadow */
|
|
|
|
else
|
|
|
|
*table &= ~_REGION_ENTRY_INVALID;
|
|
|
|
} else {
|
2016-03-08 11:12:18 +00:00
|
|
|
gmap_unshadow_r3t(sg, raddr);
|
|
|
|
}
|
2016-03-08 11:23:38 +00:00
|
|
|
spin_unlock(&sg->guest_table_lock);
|
2016-03-08 11:12:18 +00:00
|
|
|
return rc;
|
|
|
|
out_free:
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
2017-07-05 05:37:27 +00:00
|
|
|
__free_pages(page, CRST_ALLOC_ORDER);
|
2016-03-08 11:12:18 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_shadow_r3t);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_shadow_sgt - create a shadow segment table
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @saddr: faulting address in the shadow gmap
|
|
|
|
* @sgt: parent gmap address of the segment table to get shadowed
|
2016-04-18 11:42:05 +00:00
|
|
|
* @fake: sgt references contiguous guest memory block, not a sgt
|
2016-03-08 11:12:18 +00:00
|
|
|
*
|
|
|
|
* Returns: 0 if successfully shadowed or already shadowed, -EAGAIN if the
|
|
|
|
* shadow table structure is incomplete, -ENOMEM if out of memory and
|
|
|
|
* -EFAULT if an address in the parent gmap could not be resolved.
|
|
|
|
*
|
2020-06-09 04:33:54 +00:00
|
|
|
* Called with sg->mm->mmap_lock in read.
|
2016-03-08 11:12:18 +00:00
|
|
|
*/
|
2016-04-18 11:42:05 +00:00
|
|
|
int gmap_shadow_sgt(struct gmap *sg, unsigned long saddr, unsigned long sgt,
|
|
|
|
int fake)
|
2016-03-08 11:12:18 +00:00
|
|
|
{
|
|
|
|
unsigned long raddr, origin, offset, len;
|
2022-10-20 14:31:55 +00:00
|
|
|
unsigned long *table;
|
|
|
|
phys_addr_t s_sgt;
|
2016-03-08 11:12:18 +00:00
|
|
|
struct page *page;
|
|
|
|
int rc;
|
|
|
|
|
2016-04-18 11:42:05 +00:00
|
|
|
BUG_ON(!gmap_is_shadow(sg) || (sgt & _REGION3_ENTRY_LARGE));
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Allocate a shadow segment table */
|
2020-11-09 12:14:35 +00:00
|
|
|
page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
|
2016-03-08 11:12:18 +00:00
|
|
|
if (!page)
|
|
|
|
return -ENOMEM;
|
|
|
|
page->index = sgt & _REGION_ENTRY_ORIGIN;
|
2016-04-18 11:42:05 +00:00
|
|
|
if (fake)
|
|
|
|
page->index |= GMAP_SHADOW_FAKE_TABLE;
|
2022-10-20 14:31:55 +00:00
|
|
|
s_sgt = page_to_phys(page);
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Install shadow region second table */
|
|
|
|
spin_lock(&sg->guest_table_lock);
|
|
|
|
table = gmap_table_walk(sg, saddr, 2); /* get region-3 pointer */
|
|
|
|
if (!table) {
|
|
|
|
rc = -EAGAIN; /* Race with unshadow */
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
if (!(*table & _REGION_ENTRY_INVALID)) {
|
|
|
|
rc = 0; /* Already established */
|
|
|
|
goto out_free;
|
2016-03-08 11:23:38 +00:00
|
|
|
} else if (*table & _REGION_ENTRY_ORIGIN) {
|
|
|
|
rc = -EAGAIN; /* Race with shadow */
|
|
|
|
goto out_free;
|
2016-03-08 11:12:18 +00:00
|
|
|
}
|
2022-10-20 14:31:55 +00:00
|
|
|
crst_table_init(__va(s_sgt), _SEGMENT_ENTRY_EMPTY);
|
2016-03-08 11:23:38 +00:00
|
|
|
/* mark as invalid as long as the parent table is not protected */
|
2022-10-20 14:31:55 +00:00
|
|
|
*table = s_sgt | _REGION_ENTRY_LENGTH |
|
2016-03-08 11:23:38 +00:00
|
|
|
_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID;
|
2016-04-18 11:24:52 +00:00
|
|
|
if (sg->edat_level >= 1)
|
|
|
|
*table |= sgt & _REGION_ENTRY_PROTECT;
|
2016-03-08 11:12:18 +00:00
|
|
|
list_add(&page->lru, &sg->crst_list);
|
2016-04-18 11:42:05 +00:00
|
|
|
if (fake) {
|
|
|
|
/* nothing to protect for fake tables */
|
|
|
|
*table &= ~_REGION_ENTRY_INVALID;
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-08 11:12:18 +00:00
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
/* Make sgt read-only in parent gmap page table */
|
2017-07-05 05:37:27 +00:00
|
|
|
raddr = (saddr & _REGION3_MASK) | _SHADOW_RMAP_REGION3;
|
2016-03-08 11:12:18 +00:00
|
|
|
origin = sgt & _REGION_ENTRY_ORIGIN;
|
2017-07-05 05:37:27 +00:00
|
|
|
offset = ((sgt & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
|
|
|
|
len = ((sgt & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
|
2018-01-23 21:26:18 +00:00
|
|
|
rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
|
2016-03-08 11:23:38 +00:00
|
|
|
spin_lock(&sg->guest_table_lock);
|
|
|
|
if (!rc) {
|
|
|
|
table = gmap_table_walk(sg, saddr, 2);
|
2022-10-20 14:31:55 +00:00
|
|
|
if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_sgt)
|
2016-03-08 11:23:38 +00:00
|
|
|
rc = -EAGAIN; /* Race with unshadow */
|
|
|
|
else
|
|
|
|
*table &= ~_REGION_ENTRY_INVALID;
|
|
|
|
} else {
|
2016-03-08 11:12:18 +00:00
|
|
|
gmap_unshadow_sgt(sg, raddr);
|
|
|
|
}
|
2016-03-08 11:23:38 +00:00
|
|
|
spin_unlock(&sg->guest_table_lock);
|
2016-03-08 11:12:18 +00:00
|
|
|
return rc;
|
|
|
|
out_free:
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
2017-07-05 05:37:27 +00:00
|
|
|
__free_pages(page, CRST_ALLOC_ORDER);
|
2016-03-08 11:12:18 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_shadow_sgt);
|
|
|
|
|
|
|
|
/**
|
2021-09-02 17:47:37 +00:00
|
|
|
* gmap_shadow_pgt_lookup - find a shadow page table
|
2016-03-08 11:12:18 +00:00
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @saddr: the address in the shadow aguest address space
|
|
|
|
* @pgt: parent gmap address of the page table to get shadowed
|
|
|
|
* @dat_protection: if the pgtable is marked as protected by dat
|
2016-04-18 11:24:52 +00:00
|
|
|
* @fake: pgt references contiguous guest memory block, not a pgtable
|
2016-03-08 11:12:18 +00:00
|
|
|
*
|
|
|
|
* Returns 0 if the shadow page table was found and -EAGAIN if the page
|
|
|
|
* table was not found.
|
|
|
|
*
|
2020-06-09 04:33:54 +00:00
|
|
|
* Called with sg->mm->mmap_lock in read.
|
2016-03-08 11:12:18 +00:00
|
|
|
*/
|
|
|
|
int gmap_shadow_pgt_lookup(struct gmap *sg, unsigned long saddr,
|
2016-04-18 11:24:52 +00:00
|
|
|
unsigned long *pgt, int *dat_protection,
|
|
|
|
int *fake)
|
2016-03-08 11:12:18 +00:00
|
|
|
{
|
|
|
|
unsigned long *table;
|
|
|
|
struct page *page;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
|
|
|
spin_lock(&sg->guest_table_lock);
|
|
|
|
table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
|
|
|
|
if (table && !(*table & _SEGMENT_ENTRY_INVALID)) {
|
|
|
|
/* Shadow page tables are full pages (pte+pgste) */
|
|
|
|
page = pfn_to_page(*table >> PAGE_SHIFT);
|
2016-04-18 11:24:52 +00:00
|
|
|
*pgt = page->index & ~GMAP_SHADOW_FAKE_TABLE;
|
2016-03-08 11:12:18 +00:00
|
|
|
*dat_protection = !!(*table & _SEGMENT_ENTRY_PROTECT);
|
2016-04-18 11:24:52 +00:00
|
|
|
*fake = !!(page->index & GMAP_SHADOW_FAKE_TABLE);
|
2016-03-08 11:12:18 +00:00
|
|
|
rc = 0;
|
|
|
|
} else {
|
|
|
|
rc = -EAGAIN;
|
|
|
|
}
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_shadow_pgt_lookup);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_shadow_pgt - instantiate a shadow page table
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @saddr: faulting address in the shadow gmap
|
|
|
|
* @pgt: parent gmap address of the page table to get shadowed
|
2016-04-18 11:24:52 +00:00
|
|
|
* @fake: pgt references contiguous guest memory block, not a pgtable
|
2016-03-08 11:12:18 +00:00
|
|
|
*
|
|
|
|
* Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
|
|
|
|
* shadow table structure is incomplete, -ENOMEM if out of memory,
|
|
|
|
* -EFAULT if an address in the parent gmap could not be resolved and
|
|
|
|
*
|
2020-06-09 04:33:54 +00:00
|
|
|
* Called with gmap->mm->mmap_lock in read
|
2016-03-08 11:12:18 +00:00
|
|
|
*/
|
2016-04-18 11:24:52 +00:00
|
|
|
int gmap_shadow_pgt(struct gmap *sg, unsigned long saddr, unsigned long pgt,
|
|
|
|
int fake)
|
2016-03-08 11:12:18 +00:00
|
|
|
{
|
|
|
|
unsigned long raddr, origin;
|
2022-10-20 14:31:55 +00:00
|
|
|
unsigned long *table;
|
2016-03-08 11:12:18 +00:00
|
|
|
struct page *page;
|
2022-10-20 14:31:55 +00:00
|
|
|
phys_addr_t s_pgt;
|
2016-03-08 11:12:18 +00:00
|
|
|
int rc;
|
|
|
|
|
2016-04-18 11:24:52 +00:00
|
|
|
BUG_ON(!gmap_is_shadow(sg) || (pgt & _SEGMENT_ENTRY_LARGE));
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Allocate a shadow page table */
|
|
|
|
page = page_table_alloc_pgste(sg->mm);
|
|
|
|
if (!page)
|
|
|
|
return -ENOMEM;
|
|
|
|
page->index = pgt & _SEGMENT_ENTRY_ORIGIN;
|
2016-04-18 11:24:52 +00:00
|
|
|
if (fake)
|
|
|
|
page->index |= GMAP_SHADOW_FAKE_TABLE;
|
2022-10-20 14:31:55 +00:00
|
|
|
s_pgt = page_to_phys(page);
|
2016-03-08 11:12:18 +00:00
|
|
|
/* Install shadow page table */
|
|
|
|
spin_lock(&sg->guest_table_lock);
|
|
|
|
table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
|
|
|
|
if (!table) {
|
|
|
|
rc = -EAGAIN; /* Race with unshadow */
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
if (!(*table & _SEGMENT_ENTRY_INVALID)) {
|
|
|
|
rc = 0; /* Already established */
|
|
|
|
goto out_free;
|
2016-03-08 11:23:38 +00:00
|
|
|
} else if (*table & _SEGMENT_ENTRY_ORIGIN) {
|
|
|
|
rc = -EAGAIN; /* Race with shadow */
|
|
|
|
goto out_free;
|
2016-03-08 11:12:18 +00:00
|
|
|
}
|
2016-03-08 11:23:38 +00:00
|
|
|
/* mark as invalid as long as the parent table is not protected */
|
2016-03-08 11:12:18 +00:00
|
|
|
*table = (unsigned long) s_pgt | _SEGMENT_ENTRY |
|
2016-03-08 11:23:38 +00:00
|
|
|
(pgt & _SEGMENT_ENTRY_PROTECT) | _SEGMENT_ENTRY_INVALID;
|
2016-03-08 11:12:18 +00:00
|
|
|
list_add(&page->lru, &sg->pt_list);
|
2016-04-18 11:24:52 +00:00
|
|
|
if (fake) {
|
|
|
|
/* nothing to protect for fake tables */
|
|
|
|
*table &= ~_SEGMENT_ENTRY_INVALID;
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-08 11:12:18 +00:00
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
/* Make pgt read-only in parent gmap page table (not the pgste) */
|
2017-07-05 05:37:27 +00:00
|
|
|
raddr = (saddr & _SEGMENT_MASK) | _SHADOW_RMAP_SEGMENT;
|
2016-03-08 11:12:18 +00:00
|
|
|
origin = pgt & _SEGMENT_ENTRY_ORIGIN & PAGE_MASK;
|
2018-01-23 21:26:18 +00:00
|
|
|
rc = gmap_protect_rmap(sg, raddr, origin, PAGE_SIZE);
|
2016-03-08 11:23:38 +00:00
|
|
|
spin_lock(&sg->guest_table_lock);
|
|
|
|
if (!rc) {
|
|
|
|
table = gmap_table_walk(sg, saddr, 1);
|
2022-10-20 14:31:55 +00:00
|
|
|
if (!table || (*table & _SEGMENT_ENTRY_ORIGIN) != s_pgt)
|
2016-03-08 11:23:38 +00:00
|
|
|
rc = -EAGAIN; /* Race with unshadow */
|
|
|
|
else
|
|
|
|
*table &= ~_SEGMENT_ENTRY_INVALID;
|
|
|
|
} else {
|
2016-03-08 11:12:18 +00:00
|
|
|
gmap_unshadow_pgt(sg, raddr);
|
|
|
|
}
|
2016-03-08 11:23:38 +00:00
|
|
|
spin_unlock(&sg->guest_table_lock);
|
2016-03-08 11:12:18 +00:00
|
|
|
return rc;
|
|
|
|
out_free:
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
page_table_free_pgste(page);
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_shadow_pgt);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_shadow_page - create a shadow page mapping
|
|
|
|
* @sg: pointer to the shadow guest address space structure
|
|
|
|
* @saddr: faulting address in the shadow gmap
|
2016-03-08 11:21:41 +00:00
|
|
|
* @pte: pte in parent gmap address space to get shadowed
|
2016-03-08 11:12:18 +00:00
|
|
|
*
|
|
|
|
* Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
|
|
|
|
* shadow table structure is incomplete, -ENOMEM if out of memory and
|
|
|
|
* -EFAULT if an address in the parent gmap could not be resolved.
|
|
|
|
*
|
2020-06-09 04:33:54 +00:00
|
|
|
* Called with sg->mm->mmap_lock in read.
|
2016-03-08 11:12:18 +00:00
|
|
|
*/
|
2016-03-08 11:21:41 +00:00
|
|
|
int gmap_shadow_page(struct gmap *sg, unsigned long saddr, pte_t pte)
|
2016-03-08 11:12:18 +00:00
|
|
|
{
|
|
|
|
struct gmap *parent;
|
|
|
|
struct gmap_rmap *rmap;
|
2016-03-08 11:21:41 +00:00
|
|
|
unsigned long vmaddr, paddr;
|
2016-03-08 11:12:18 +00:00
|
|
|
spinlock_t *ptl;
|
|
|
|
pte_t *sptep, *tptep;
|
2016-06-13 08:49:04 +00:00
|
|
|
int prot;
|
2016-03-08 11:12:18 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
|
|
|
parent = sg->parent;
|
2016-06-13 08:49:04 +00:00
|
|
|
prot = (pte_val(pte) & _PAGE_PROTECT) ? PROT_READ : PROT_WRITE;
|
2016-03-08 11:12:18 +00:00
|
|
|
|
2020-11-09 12:14:35 +00:00
|
|
|
rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
|
2016-03-08 11:12:18 +00:00
|
|
|
if (!rmap)
|
|
|
|
return -ENOMEM;
|
|
|
|
rmap->raddr = (saddr & PAGE_MASK) | _SHADOW_RMAP_PGTABLE;
|
|
|
|
|
|
|
|
while (1) {
|
2016-03-08 11:21:41 +00:00
|
|
|
paddr = pte_val(pte) & PAGE_MASK;
|
2016-03-08 11:12:18 +00:00
|
|
|
vmaddr = __gmap_translate(parent, paddr);
|
|
|
|
if (IS_ERR_VALUE(vmaddr)) {
|
|
|
|
rc = vmaddr;
|
|
|
|
break;
|
|
|
|
}
|
2020-11-09 12:14:35 +00:00
|
|
|
rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
|
2016-03-08 11:12:18 +00:00
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
rc = -EAGAIN;
|
|
|
|
sptep = gmap_pte_op_walk(parent, paddr, &ptl);
|
|
|
|
if (sptep) {
|
|
|
|
spin_lock(&sg->guest_table_lock);
|
|
|
|
/* Get page table pointer */
|
|
|
|
tptep = (pte_t *) gmap_table_walk(sg, saddr, 0);
|
|
|
|
if (!tptep) {
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
2023-06-08 19:29:13 +00:00
|
|
|
gmap_pte_op_end(sptep, ptl);
|
2016-03-08 11:12:18 +00:00
|
|
|
radix_tree_preload_end();
|
|
|
|
break;
|
|
|
|
}
|
2016-03-08 11:21:41 +00:00
|
|
|
rc = ptep_shadow_pte(sg->mm, saddr, sptep, tptep, pte);
|
2016-03-08 11:12:18 +00:00
|
|
|
if (rc > 0) {
|
|
|
|
/* Success and a new mapping */
|
|
|
|
gmap_insert_rmap(sg, vmaddr, rmap);
|
|
|
|
rmap = NULL;
|
|
|
|
rc = 0;
|
|
|
|
}
|
2023-06-08 19:29:13 +00:00
|
|
|
gmap_pte_op_end(sptep, ptl);
|
2016-03-08 11:12:18 +00:00
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
}
|
|
|
|
radix_tree_preload_end();
|
|
|
|
if (!rc)
|
|
|
|
break;
|
2016-06-13 08:49:04 +00:00
|
|
|
rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
|
2016-03-08 11:12:18 +00:00
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
kfree(rmap);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_shadow_page);
|
|
|
|
|
2021-09-02 17:47:37 +00:00
|
|
|
/*
|
2016-03-08 11:12:18 +00:00
|
|
|
* gmap_shadow_notify - handle notifications for shadow gmap
|
|
|
|
*
|
|
|
|
* Called with sg->parent->shadow_lock.
|
|
|
|
*/
|
|
|
|
static void gmap_shadow_notify(struct gmap *sg, unsigned long vmaddr,
|
2017-12-13 12:53:22 +00:00
|
|
|
unsigned long gaddr)
|
2016-03-08 11:12:18 +00:00
|
|
|
{
|
|
|
|
struct gmap_rmap *rmap, *rnext, *head;
|
2017-02-08 07:59:56 +00:00
|
|
|
unsigned long start, end, bits, raddr;
|
2016-03-08 11:12:18 +00:00
|
|
|
|
|
|
|
BUG_ON(!gmap_is_shadow(sg));
|
|
|
|
|
|
|
|
spin_lock(&sg->guest_table_lock);
|
|
|
|
if (sg->removed) {
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Check for top level table */
|
|
|
|
start = sg->orig_asce & _ASCE_ORIGIN;
|
2017-07-05 05:37:27 +00:00
|
|
|
end = start + ((sg->orig_asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE;
|
2016-04-18 14:22:24 +00:00
|
|
|
if (!(sg->orig_asce & _ASCE_REAL_SPACE) && gaddr >= start &&
|
|
|
|
gaddr < end) {
|
2016-03-08 11:12:18 +00:00
|
|
|
/* The complete shadow table has to go */
|
|
|
|
gmap_unshadow(sg);
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
list_del(&sg->list);
|
|
|
|
gmap_put(sg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Remove the page table tree from on specific entry */
|
2017-07-05 05:37:27 +00:00
|
|
|
head = radix_tree_delete(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
|
2016-03-08 11:12:18 +00:00
|
|
|
gmap_for_each_rmap_safe(rmap, rnext, head) {
|
|
|
|
bits = rmap->raddr & _SHADOW_RMAP_MASK;
|
|
|
|
raddr = rmap->raddr ^ bits;
|
|
|
|
switch (bits) {
|
|
|
|
case _SHADOW_RMAP_REGION1:
|
|
|
|
gmap_unshadow_r2t(sg, raddr);
|
|
|
|
break;
|
|
|
|
case _SHADOW_RMAP_REGION2:
|
|
|
|
gmap_unshadow_r3t(sg, raddr);
|
|
|
|
break;
|
|
|
|
case _SHADOW_RMAP_REGION3:
|
|
|
|
gmap_unshadow_sgt(sg, raddr);
|
|
|
|
break;
|
|
|
|
case _SHADOW_RMAP_SEGMENT:
|
|
|
|
gmap_unshadow_pgt(sg, raddr);
|
|
|
|
break;
|
|
|
|
case _SHADOW_RMAP_PGTABLE:
|
|
|
|
gmap_unshadow_page(sg, raddr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
kfree(rmap);
|
|
|
|
}
|
|
|
|
spin_unlock(&sg->guest_table_lock);
|
|
|
|
}
|
2016-03-08 10:49:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ptep_notify - call all invalidation callbacks for a specific pte.
|
|
|
|
* @mm: pointer to the process mm_struct
|
2021-09-02 17:47:37 +00:00
|
|
|
* @vmaddr: virtual address in the process address space
|
2016-03-08 10:49:57 +00:00
|
|
|
* @pte: pointer to the page table entry
|
2016-03-08 11:12:18 +00:00
|
|
|
* @bits: bits from the pgste that caused the notify call
|
2016-03-08 10:49:57 +00:00
|
|
|
*
|
|
|
|
* This function is assumed to be called with the page table lock held
|
|
|
|
* for the pte to notify.
|
|
|
|
*/
|
2016-03-08 11:12:18 +00:00
|
|
|
void ptep_notify(struct mm_struct *mm, unsigned long vmaddr,
|
|
|
|
pte_t *pte, unsigned long bits)
|
2016-03-08 10:49:57 +00:00
|
|
|
{
|
2017-02-08 07:59:56 +00:00
|
|
|
unsigned long offset, gaddr = 0;
|
2016-03-08 10:49:57 +00:00
|
|
|
unsigned long *table;
|
2016-03-08 11:12:18 +00:00
|
|
|
struct gmap *gmap, *sg, *next;
|
2016-03-08 10:49:57 +00:00
|
|
|
|
|
|
|
offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
|
2017-07-05 05:37:27 +00:00
|
|
|
offset = offset * (PAGE_SIZE / sizeof(pte_t));
|
2016-03-08 10:54:14 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
|
|
|
|
spin_lock(&gmap->guest_table_lock);
|
2016-03-08 10:49:57 +00:00
|
|
|
table = radix_tree_lookup(&gmap->host_to_guest,
|
|
|
|
vmaddr >> PMD_SHIFT);
|
2016-03-08 10:54:14 +00:00
|
|
|
if (table)
|
|
|
|
gaddr = __gmap_segment_gaddr(table) + offset;
|
|
|
|
spin_unlock(&gmap->guest_table_lock);
|
2017-02-08 07:59:56 +00:00
|
|
|
if (!table)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!list_empty(&gmap->children) && (bits & PGSTE_VSIE_BIT)) {
|
|
|
|
spin_lock(&gmap->shadow_lock);
|
|
|
|
list_for_each_entry_safe(sg, next,
|
|
|
|
&gmap->children, list)
|
2017-12-13 12:53:22 +00:00
|
|
|
gmap_shadow_notify(sg, vmaddr, gaddr);
|
2017-02-08 07:59:56 +00:00
|
|
|
spin_unlock(&gmap->shadow_lock);
|
|
|
|
}
|
|
|
|
if (bits & PGSTE_IN_BIT)
|
2016-03-08 10:54:14 +00:00
|
|
|
gmap_call_notifier(gmap, gaddr, gaddr + PAGE_SIZE - 1);
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
2016-03-08 10:54:14 +00:00
|
|
|
rcu_read_unlock();
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ptep_notify);
|
|
|
|
|
2018-07-13 10:28:22 +00:00
|
|
|
static void pmdp_notify_gmap(struct gmap *gmap, pmd_t *pmdp,
|
|
|
|
unsigned long gaddr)
|
|
|
|
{
|
2022-02-21 19:50:07 +00:00
|
|
|
set_pmd(pmdp, clear_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_IN)));
|
2018-07-13 10:28:22 +00:00
|
|
|
gmap_call_notifier(gmap, gaddr, gaddr + HPAGE_SIZE - 1);
|
|
|
|
}
|
|
|
|
|
2018-07-17 12:21:22 +00:00
|
|
|
/**
|
|
|
|
* gmap_pmdp_xchg - exchange a gmap pmd with another
|
|
|
|
* @gmap: pointer to the guest address space structure
|
|
|
|
* @pmdp: pointer to the pmd entry
|
|
|
|
* @new: replacement entry
|
|
|
|
* @gaddr: the affected guest address
|
|
|
|
*
|
|
|
|
* This function is assumed to be called with the guest_table_lock
|
|
|
|
* held.
|
|
|
|
*/
|
|
|
|
static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *pmdp, pmd_t new,
|
|
|
|
unsigned long gaddr)
|
|
|
|
{
|
|
|
|
gaddr &= HPAGE_MASK;
|
|
|
|
pmdp_notify_gmap(gmap, pmdp, gaddr);
|
2022-02-21 20:25:09 +00:00
|
|
|
new = clear_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_GMAP_IN));
|
2018-07-17 12:21:22 +00:00
|
|
|
if (MACHINE_HAS_TLB_GUEST)
|
|
|
|
__pmdp_idte(gaddr, (pmd_t *)pmdp, IDTE_GUEST_ASCE, gmap->asce,
|
|
|
|
IDTE_GLOBAL);
|
|
|
|
else if (MACHINE_HAS_IDTE)
|
|
|
|
__pmdp_idte(gaddr, (pmd_t *)pmdp, 0, 0, IDTE_GLOBAL);
|
|
|
|
else
|
|
|
|
__pmdp_csp(pmdp);
|
2022-02-21 19:50:07 +00:00
|
|
|
set_pmd(pmdp, new);
|
2018-07-17 12:21:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 10:28:22 +00:00
|
|
|
static void gmap_pmdp_clear(struct mm_struct *mm, unsigned long vmaddr,
|
|
|
|
int purge)
|
|
|
|
{
|
|
|
|
pmd_t *pmdp;
|
|
|
|
struct gmap *gmap;
|
|
|
|
unsigned long gaddr;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
|
|
|
|
spin_lock(&gmap->guest_table_lock);
|
|
|
|
pmdp = (pmd_t *)radix_tree_delete(&gmap->host_to_guest,
|
|
|
|
vmaddr >> PMD_SHIFT);
|
|
|
|
if (pmdp) {
|
|
|
|
gaddr = __gmap_segment_gaddr((unsigned long *)pmdp);
|
|
|
|
pmdp_notify_gmap(gmap, pmdp, gaddr);
|
2018-07-17 12:21:22 +00:00
|
|
|
WARN_ON(pmd_val(*pmdp) & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
|
|
|
|
_SEGMENT_ENTRY_GMAP_UC));
|
2018-07-13 10:28:22 +00:00
|
|
|
if (purge)
|
|
|
|
__pmdp_csp(pmdp);
|
2022-02-21 19:50:07 +00:00
|
|
|
set_pmd(pmdp, __pmd(_SEGMENT_ENTRY_EMPTY));
|
2018-07-13 10:28:22 +00:00
|
|
|
}
|
|
|
|
spin_unlock(&gmap->guest_table_lock);
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_pmdp_invalidate - invalidate all affected guest pmd entries without
|
|
|
|
* flushing
|
|
|
|
* @mm: pointer to the process mm_struct
|
|
|
|
* @vmaddr: virtual address in the process address space
|
|
|
|
*/
|
|
|
|
void gmap_pmdp_invalidate(struct mm_struct *mm, unsigned long vmaddr)
|
|
|
|
{
|
|
|
|
gmap_pmdp_clear(mm, vmaddr, 0);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_pmdp_invalidate);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_pmdp_csp - csp all affected guest pmd entries
|
|
|
|
* @mm: pointer to the process mm_struct
|
|
|
|
* @vmaddr: virtual address in the process address space
|
|
|
|
*/
|
|
|
|
void gmap_pmdp_csp(struct mm_struct *mm, unsigned long vmaddr)
|
|
|
|
{
|
|
|
|
gmap_pmdp_clear(mm, vmaddr, 1);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_pmdp_csp);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_pmdp_idte_local - invalidate and clear a guest pmd entry
|
|
|
|
* @mm: pointer to the process mm_struct
|
|
|
|
* @vmaddr: virtual address in the process address space
|
|
|
|
*/
|
|
|
|
void gmap_pmdp_idte_local(struct mm_struct *mm, unsigned long vmaddr)
|
|
|
|
{
|
|
|
|
unsigned long *entry, gaddr;
|
|
|
|
struct gmap *gmap;
|
|
|
|
pmd_t *pmdp;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
|
|
|
|
spin_lock(&gmap->guest_table_lock);
|
|
|
|
entry = radix_tree_delete(&gmap->host_to_guest,
|
|
|
|
vmaddr >> PMD_SHIFT);
|
|
|
|
if (entry) {
|
|
|
|
pmdp = (pmd_t *)entry;
|
|
|
|
gaddr = __gmap_segment_gaddr(entry);
|
|
|
|
pmdp_notify_gmap(gmap, pmdp, gaddr);
|
2018-07-17 12:21:22 +00:00
|
|
|
WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
|
|
|
|
_SEGMENT_ENTRY_GMAP_UC));
|
2018-07-13 10:28:22 +00:00
|
|
|
if (MACHINE_HAS_TLB_GUEST)
|
|
|
|
__pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
|
|
|
|
gmap->asce, IDTE_LOCAL);
|
|
|
|
else if (MACHINE_HAS_IDTE)
|
|
|
|
__pmdp_idte(gaddr, pmdp, 0, 0, IDTE_LOCAL);
|
|
|
|
*entry = _SEGMENT_ENTRY_EMPTY;
|
|
|
|
}
|
|
|
|
spin_unlock(&gmap->guest_table_lock);
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_pmdp_idte_local);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_pmdp_idte_global - invalidate and clear a guest pmd entry
|
|
|
|
* @mm: pointer to the process mm_struct
|
|
|
|
* @vmaddr: virtual address in the process address space
|
|
|
|
*/
|
|
|
|
void gmap_pmdp_idte_global(struct mm_struct *mm, unsigned long vmaddr)
|
|
|
|
{
|
|
|
|
unsigned long *entry, gaddr;
|
|
|
|
struct gmap *gmap;
|
|
|
|
pmd_t *pmdp;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
|
|
|
|
spin_lock(&gmap->guest_table_lock);
|
|
|
|
entry = radix_tree_delete(&gmap->host_to_guest,
|
|
|
|
vmaddr >> PMD_SHIFT);
|
|
|
|
if (entry) {
|
|
|
|
pmdp = (pmd_t *)entry;
|
|
|
|
gaddr = __gmap_segment_gaddr(entry);
|
|
|
|
pmdp_notify_gmap(gmap, pmdp, gaddr);
|
2018-07-17 12:21:22 +00:00
|
|
|
WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
|
|
|
|
_SEGMENT_ENTRY_GMAP_UC));
|
2018-07-13 10:28:22 +00:00
|
|
|
if (MACHINE_HAS_TLB_GUEST)
|
|
|
|
__pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
|
|
|
|
gmap->asce, IDTE_GLOBAL);
|
|
|
|
else if (MACHINE_HAS_IDTE)
|
|
|
|
__pmdp_idte(gaddr, pmdp, 0, 0, IDTE_GLOBAL);
|
|
|
|
else
|
|
|
|
__pmdp_csp(pmdp);
|
|
|
|
*entry = _SEGMENT_ENTRY_EMPTY;
|
|
|
|
}
|
|
|
|
spin_unlock(&gmap->guest_table_lock);
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_pmdp_idte_global);
|
|
|
|
|
2018-07-17 12:21:22 +00:00
|
|
|
/**
|
|
|
|
* gmap_test_and_clear_dirty_pmd - test and reset segment dirty status
|
|
|
|
* @gmap: pointer to guest address space
|
|
|
|
* @pmdp: pointer to the pmd to be tested
|
|
|
|
* @gaddr: virtual address in the guest address space
|
|
|
|
*
|
|
|
|
* This function is assumed to be called with the guest_table_lock
|
|
|
|
* held.
|
|
|
|
*/
|
2019-07-17 17:41:09 +00:00
|
|
|
static bool gmap_test_and_clear_dirty_pmd(struct gmap *gmap, pmd_t *pmdp,
|
|
|
|
unsigned long gaddr)
|
2018-07-17 12:21:22 +00:00
|
|
|
{
|
|
|
|
if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Already protected memory, which did not change is clean */
|
|
|
|
if (pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT &&
|
|
|
|
!(pmd_val(*pmdp) & _SEGMENT_ENTRY_GMAP_UC))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Clear UC indication and reset protection */
|
2022-02-21 19:50:07 +00:00
|
|
|
set_pmd(pmdp, clear_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_UC)));
|
2018-07-17 12:21:22 +00:00
|
|
|
gmap_protect_pmd(gmap, gaddr, pmdp, PROT_READ, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gmap_sync_dirty_log_pmd - set bitmap based on dirty status of segment
|
|
|
|
* @gmap: pointer to guest address space
|
|
|
|
* @bitmap: dirty bitmap for this pmd
|
|
|
|
* @gaddr: virtual address in the guest address space
|
|
|
|
* @vmaddr: virtual address in the host address space
|
|
|
|
*
|
|
|
|
* This function is assumed to be called with the guest_table_lock
|
|
|
|
* held.
|
|
|
|
*/
|
|
|
|
void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long bitmap[4],
|
|
|
|
unsigned long gaddr, unsigned long vmaddr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
pmd_t *pmdp;
|
|
|
|
pte_t *ptep;
|
|
|
|
spinlock_t *ptl;
|
|
|
|
|
|
|
|
pmdp = gmap_pmd_op_walk(gmap, gaddr);
|
|
|
|
if (!pmdp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pmd_large(*pmdp)) {
|
|
|
|
if (gmap_test_and_clear_dirty_pmd(gmap, pmdp, gaddr))
|
|
|
|
bitmap_fill(bitmap, _PAGE_ENTRIES);
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < _PAGE_ENTRIES; i++, vmaddr += PAGE_SIZE) {
|
|
|
|
ptep = pte_alloc_map_lock(gmap->mm, pmdp, vmaddr, &ptl);
|
|
|
|
if (!ptep)
|
|
|
|
continue;
|
|
|
|
if (ptep_test_and_clear_uc(gmap->mm, vmaddr, ptep))
|
|
|
|
set_bit(i, bitmap);
|
2023-06-08 19:29:13 +00:00
|
|
|
pte_unmap_unlock(ptep, ptl);
|
2018-07-17 12:21:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
gmap_pmd_op_end(gmap, pmdp);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_sync_dirty_log_pmd);
|
|
|
|
|
2020-07-29 20:22:34 +00:00
|
|
|
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
|
|
|
|
static int thp_split_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
|
|
|
|
unsigned long end, struct mm_walk *walk)
|
|
|
|
{
|
|
|
|
struct vm_area_struct *vma = walk->vma;
|
|
|
|
|
|
|
|
split_huge_pmd(vma, pmd, addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct mm_walk_ops thp_split_walk_ops = {
|
|
|
|
.pmd_entry = thp_split_walk_pmd_entry,
|
|
|
|
};
|
|
|
|
|
2016-03-08 10:49:57 +00:00
|
|
|
static inline void thp_split_mm(struct mm_struct *mm)
|
|
|
|
{
|
|
|
|
struct vm_area_struct *vma;
|
2022-09-06 19:48:54 +00:00
|
|
|
VMA_ITERATOR(vmi, mm, 0);
|
2016-03-08 10:49:57 +00:00
|
|
|
|
2022-09-06 19:48:54 +00:00
|
|
|
for_each_vma(vmi, vma) {
|
2023-01-26 19:37:49 +00:00
|
|
|
vm_flags_mod(vma, VM_NOHUGEPAGE, VM_HUGEPAGE);
|
2020-07-29 20:22:34 +00:00
|
|
|
walk_page_vma(vma, &thp_split_walk_ops, NULL);
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
|
|
|
mm->def_flags |= VM_NOHUGEPAGE;
|
|
|
|
}
|
2020-07-29 20:22:34 +00:00
|
|
|
#else
|
|
|
|
static inline void thp_split_mm(struct mm_struct *mm)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
|
2016-03-08 10:49:57 +00:00
|
|
|
|
2017-08-24 10:55:08 +00:00
|
|
|
/*
|
|
|
|
* Remove all empty zero pages from the mapping for lazy refaulting
|
|
|
|
* - This must be called after mm->context.has_pgste is set, to avoid
|
|
|
|
* future creation of zero pages
|
2023-06-08 19:27:22 +00:00
|
|
|
* - This must be called after THP was disabled.
|
|
|
|
*
|
|
|
|
* mm contracts with s390, that even if mm were to remove a page table,
|
|
|
|
* racing with the loop below and so causing pte_offset_map_lock() to fail,
|
|
|
|
* it will never insert a page table containing empty zero pages once
|
|
|
|
* mm_forbids_zeropage(mm) i.e. mm->context.has_pgste is set.
|
2017-08-24 10:55:08 +00:00
|
|
|
*/
|
|
|
|
static int __zap_zero_pages(pmd_t *pmd, unsigned long start,
|
|
|
|
unsigned long end, struct mm_walk *walk)
|
|
|
|
{
|
|
|
|
unsigned long addr;
|
|
|
|
|
|
|
|
for (addr = start; addr != end; addr += PAGE_SIZE) {
|
|
|
|
pte_t *ptep;
|
|
|
|
spinlock_t *ptl;
|
|
|
|
|
|
|
|
ptep = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
|
2023-06-08 19:27:22 +00:00
|
|
|
if (!ptep)
|
|
|
|
break;
|
2017-08-24 10:55:08 +00:00
|
|
|
if (is_zero_pfn(pte_pfn(*ptep)))
|
|
|
|
ptep_xchg_direct(walk->mm, addr, ptep, __pte(_PAGE_INVALID));
|
|
|
|
pte_unmap_unlock(ptep, ptl);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-28 14:19:54 +00:00
|
|
|
static const struct mm_walk_ops zap_zero_walk_ops = {
|
|
|
|
.pmd_entry = __zap_zero_pages,
|
|
|
|
};
|
2017-08-24 10:55:08 +00:00
|
|
|
|
2016-03-08 10:49:57 +00:00
|
|
|
/*
|
|
|
|
* switch on pgstes for its userspace process (for kvm)
|
|
|
|
*/
|
|
|
|
int s390_enable_sie(void)
|
|
|
|
{
|
|
|
|
struct mm_struct *mm = current->mm;
|
|
|
|
|
|
|
|
/* Do we have pgstes? if yes, we are done */
|
|
|
|
if (mm_has_pgste(mm))
|
|
|
|
return 0;
|
|
|
|
/* Fail if the page tables are 2K */
|
|
|
|
if (!mm_alloc_pgste(mm))
|
|
|
|
return -EINVAL;
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_write_lock(mm);
|
2016-03-08 10:49:57 +00:00
|
|
|
mm->context.has_pgste = 1;
|
|
|
|
/* split thp mappings and disable thp for future mappings */
|
|
|
|
thp_split_mm(mm);
|
2019-08-28 14:19:54 +00:00
|
|
|
walk_page_range(mm, 0, TASK_SIZE, &zap_zero_walk_ops, NULL);
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_write_unlock(mm);
|
2016-03-08 10:49:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(s390_enable_sie);
|
|
|
|
|
2019-07-16 11:08:37 +00:00
|
|
|
int gmap_mark_unmergeable(void)
|
|
|
|
{
|
mm: add new api to enable ksm per process
Patch series "mm: process/cgroup ksm support", v9.
So far KSM can only be enabled by calling madvise for memory regions. To
be able to use KSM for more workloads, KSM needs to have the ability to be
enabled / disabled at the process / cgroup level.
Use case 1:
The madvise call is not available in the programming language. An
example for this are programs with forked workloads using a garbage
collected language without pointers. In such a language madvise cannot
be made available.
In addition the addresses of objects get moved around as they are
garbage collected. KSM sharing needs to be enabled "from the outside"
for these type of workloads.
Use case 2:
The same interpreter can also be used for workloads where KSM brings
no benefit or even has overhead. We'd like to be able to enable KSM on
a workload by workload basis.
Use case 3:
With the madvise call sharing opportunities are only enabled for the
current process: it is a workload-local decision. A considerable number
of sharing opportunities may exist across multiple workloads or jobs (if
they are part of the same security domain). Only a higler level entity
like a job scheduler or container can know for certain if its running
one or more instances of a job. That job scheduler however doesn't have
the necessary internal workload knowledge to make targeted madvise
calls.
Security concerns:
In previous discussions security concerns have been brought up. The
problem is that an individual workload does not have the knowledge about
what else is running on a machine. Therefore it has to be very
conservative in what memory areas can be shared or not. However, if the
system is dedicated to running multiple jobs within the same security
domain, its the job scheduler that has the knowledge that sharing can be
safely enabled and is even desirable.
Performance:
Experiments with using UKSM have shown a capacity increase of around 20%.
Here are the metrics from an instagram workload (taken from a machine
with 64GB main memory):
full_scans: 445
general_profit: 20158298048
max_page_sharing: 256
merge_across_nodes: 1
pages_shared: 129547
pages_sharing: 5119146
pages_to_scan: 4000
pages_unshared: 1760924
pages_volatile: 10761341
run: 1
sleep_millisecs: 20
stable_node_chains: 167
stable_node_chains_prune_millisecs: 2000
stable_node_dups: 2751
use_zero_pages: 0
zero_pages_sharing: 0
After the service is running for 30 minutes to an hour, 4 to 5 million
shared pages are common for this workload when using KSM.
Detailed changes:
1. New options for prctl system command
This patch series adds two new options to the prctl system call.
The first one allows to enable KSM at the process level and the second
one to query the setting.
The setting will be inherited by child processes.
With the above setting, KSM can be enabled for the seed process of a cgroup
and all processes in the cgroup will inherit the setting.
2. Changes to KSM processing
When KSM is enabled at the process level, the KSM code will iterate
over all the VMA's and enable KSM for the eligible VMA's.
When forking a process that has KSM enabled, the setting will be
inherited by the new child process.
3. Add general_profit metric
The general_profit metric of KSM is specified in the documentation,
but not calculated. This adds the general profit metric to
/sys/kernel/debug/mm/ksm.
4. Add more metrics to ksm_stat
This adds the process profit metric to /proc/<pid>/ksm_stat.
5. Add more tests to ksm_tests and ksm_functional_tests
This adds an option to specify the merge type to the ksm_tests.
This allows to test madvise and prctl KSM.
It also adds a two new tests to ksm_functional_tests: one to test
the new prctl options and the other one is a fork test to verify that
the KSM process setting is inherited by client processes.
This patch (of 3):
So far KSM can only be enabled by calling madvise for memory regions. To
be able to use KSM for more workloads, KSM needs to have the ability to be
enabled / disabled at the process / cgroup level.
1. New options for prctl system command
This patch series adds two new options to the prctl system call.
The first one allows to enable KSM at the process level and the second
one to query the setting.
The setting will be inherited by child processes.
With the above setting, KSM can be enabled for the seed process of a
cgroup and all processes in the cgroup will inherit the setting.
2. Changes to KSM processing
When KSM is enabled at the process level, the KSM code will iterate
over all the VMA's and enable KSM for the eligible VMA's.
When forking a process that has KSM enabled, the setting will be
inherited by the new child process.
1) Introduce new MMF_VM_MERGE_ANY flag
This introduces the new flag MMF_VM_MERGE_ANY flag. When this flag
is set, kernel samepage merging (ksm) gets enabled for all vma's of a
process.
2) Setting VM_MERGEABLE on VMA creation
When a VMA is created, if the MMF_VM_MERGE_ANY flag is set, the
VM_MERGEABLE flag will be set for this VMA.
3) support disabling of ksm for a process
This adds the ability to disable ksm for a process if ksm has been
enabled for the process with prctl.
4) add new prctl option to get and set ksm for a process
This adds two new options to the prctl system call
- enable ksm for all vmas of a process (if the vmas support it).
- query if ksm has been enabled for a process.
3. Disabling MMF_VM_MERGE_ANY for storage keys in s390
In the s390 architecture when storage keys are used, the
MMF_VM_MERGE_ANY will be disabled.
Link: https://lkml.kernel.org/r/20230418051342.1919757-1-shr@devkernel.io
Link: https://lkml.kernel.org/r/20230418051342.1919757-2-shr@devkernel.io
Signed-off-by: Stefan Roesch <shr@devkernel.io>
Acked-by: David Hildenbrand <david@redhat.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Rik van Riel <riel@surriel.com>
Cc: Bagas Sanjaya <bagasdotme@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-04-18 05:13:40 +00:00
|
|
|
/*
|
|
|
|
* Make sure to disable KSM (if enabled for the whole process or
|
|
|
|
* individual VMAs). Note that nothing currently hinders user space
|
|
|
|
* from re-enabling it.
|
|
|
|
*/
|
2023-04-22 21:01:56 +00:00
|
|
|
return ksm_disable(current->mm);
|
2019-07-16 11:08:37 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gmap_mark_unmergeable);
|
|
|
|
|
2016-03-08 10:49:57 +00:00
|
|
|
/*
|
|
|
|
* Enable storage key handling from now on and initialize the storage
|
|
|
|
* keys with the default key.
|
|
|
|
*/
|
2018-07-13 10:28:25 +00:00
|
|
|
static int __s390_enable_skey_pte(pte_t *pte, unsigned long addr,
|
|
|
|
unsigned long next, struct mm_walk *walk)
|
2016-03-08 10:49:57 +00:00
|
|
|
{
|
|
|
|
/* Clear storage key */
|
|
|
|
ptep_zap_key(walk->mm, addr, pte);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-30 09:27:05 +00:00
|
|
|
/*
|
|
|
|
* Give a chance to schedule after setting a key to 256 pages.
|
|
|
|
* We only hold the mm lock, which is a rwsem and the kvm srcu.
|
|
|
|
* Both can sleep.
|
|
|
|
*/
|
|
|
|
static int __s390_enable_skey_pmd(pmd_t *pmd, unsigned long addr,
|
|
|
|
unsigned long next, struct mm_walk *walk)
|
|
|
|
{
|
|
|
|
cond_resched();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-13 10:28:25 +00:00
|
|
|
static int __s390_enable_skey_hugetlb(pte_t *pte, unsigned long addr,
|
|
|
|
unsigned long hmask, unsigned long next,
|
|
|
|
struct mm_walk *walk)
|
|
|
|
{
|
|
|
|
pmd_t *pmd = (pmd_t *)pte;
|
|
|
|
unsigned long start, end;
|
2018-07-13 10:28:26 +00:00
|
|
|
struct page *page = pmd_page(*pmd);
|
2018-07-13 10:28:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The write check makes sure we do not set a key on shared
|
|
|
|
* memory. This is needed as the walker does not differentiate
|
|
|
|
* between actual guest memory and the process executable or
|
|
|
|
* shared libraries.
|
|
|
|
*/
|
|
|
|
if (pmd_val(*pmd) & _SEGMENT_ENTRY_INVALID ||
|
|
|
|
!(pmd_val(*pmd) & _SEGMENT_ENTRY_WRITE))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
start = pmd_val(*pmd) & HPAGE_MASK;
|
|
|
|
end = start + HPAGE_SIZE - 1;
|
|
|
|
__storage_key_init_range(start, end);
|
2018-07-13 10:28:26 +00:00
|
|
|
set_bit(PG_arch_1, &page->flags);
|
2022-05-30 09:27:05 +00:00
|
|
|
cond_resched();
|
2018-07-13 10:28:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-28 14:19:54 +00:00
|
|
|
static const struct mm_walk_ops enable_skey_walk_ops = {
|
|
|
|
.hugetlb_entry = __s390_enable_skey_hugetlb,
|
|
|
|
.pte_entry = __s390_enable_skey_pte,
|
2022-05-30 09:27:05 +00:00
|
|
|
.pmd_entry = __s390_enable_skey_pmd,
|
2019-08-28 14:19:54 +00:00
|
|
|
};
|
|
|
|
|
2016-03-08 10:49:57 +00:00
|
|
|
int s390_enable_skey(void)
|
|
|
|
{
|
|
|
|
struct mm_struct *mm = current->mm;
|
|
|
|
int rc = 0;
|
|
|
|
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_write_lock(mm);
|
2018-02-15 15:33:47 +00:00
|
|
|
if (mm_uses_skeys(mm))
|
2016-03-08 10:49:57 +00:00
|
|
|
goto out_up;
|
|
|
|
|
2018-02-15 15:33:47 +00:00
|
|
|
mm->context.uses_skeys = 1;
|
2019-07-16 11:08:37 +00:00
|
|
|
rc = gmap_mark_unmergeable();
|
|
|
|
if (rc) {
|
|
|
|
mm->context.uses_skeys = 0;
|
|
|
|
goto out_up;
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
2019-08-28 14:19:54 +00:00
|
|
|
walk_page_range(mm, 0, TASK_SIZE, &enable_skey_walk_ops, NULL);
|
2016-03-08 10:49:57 +00:00
|
|
|
|
|
|
|
out_up:
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_write_unlock(mm);
|
2016-03-08 10:49:57 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(s390_enable_skey);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset CMMA state, make all pages stable again.
|
|
|
|
*/
|
|
|
|
static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
|
|
|
|
unsigned long next, struct mm_walk *walk)
|
|
|
|
{
|
|
|
|
ptep_zap_unused(walk->mm, addr, pte, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-28 14:19:54 +00:00
|
|
|
static const struct mm_walk_ops reset_cmma_walk_ops = {
|
|
|
|
.pte_entry = __s390_reset_cmma,
|
|
|
|
};
|
|
|
|
|
2016-03-08 10:49:57 +00:00
|
|
|
void s390_reset_cmma(struct mm_struct *mm)
|
|
|
|
{
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_write_lock(mm);
|
2019-08-28 14:19:54 +00:00
|
|
|
walk_page_range(mm, 0, TASK_SIZE, &reset_cmma_walk_ops, NULL);
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_write_unlock(mm);
|
2016-03-08 10:49:57 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(s390_reset_cmma);
|
2019-12-16 15:48:11 +00:00
|
|
|
|
2022-06-28 13:56:05 +00:00
|
|
|
#define GATHER_GET_PAGES 32
|
|
|
|
|
|
|
|
struct reset_walk_state {
|
|
|
|
unsigned long next;
|
|
|
|
unsigned long count;
|
|
|
|
unsigned long pfns[GATHER_GET_PAGES];
|
|
|
|
};
|
|
|
|
|
|
|
|
static int s390_gather_pages(pte_t *ptep, unsigned long addr,
|
|
|
|
unsigned long next, struct mm_walk *walk)
|
2019-12-16 15:48:11 +00:00
|
|
|
{
|
2022-06-28 13:56:05 +00:00
|
|
|
struct reset_walk_state *p = walk->private;
|
2019-12-16 15:48:11 +00:00
|
|
|
pte_t pte = READ_ONCE(*ptep);
|
|
|
|
|
2022-06-28 13:56:05 +00:00
|
|
|
if (pte_present(pte)) {
|
|
|
|
/* we have a reference from the mapping, take an extra one */
|
|
|
|
get_page(phys_to_page(pte_val(pte)));
|
|
|
|
p->pfns[p->count] = phys_to_pfn(pte_val(pte));
|
|
|
|
p->next = next;
|
|
|
|
p->count++;
|
|
|
|
}
|
|
|
|
return p->count >= GATHER_GET_PAGES;
|
2019-12-16 15:48:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 13:56:05 +00:00
|
|
|
static const struct mm_walk_ops gather_pages_ops = {
|
|
|
|
.pte_entry = s390_gather_pages,
|
2019-12-16 15:48:11 +00:00
|
|
|
};
|
|
|
|
|
2022-06-28 13:56:05 +00:00
|
|
|
/*
|
|
|
|
* Call the Destroy secure page UVC on each page in the given array of PFNs.
|
|
|
|
* Each page needs to have an extra reference, which will be released here.
|
|
|
|
*/
|
|
|
|
void s390_uv_destroy_pfns(unsigned long count, unsigned long *pfns)
|
2019-12-16 15:48:11 +00:00
|
|
|
{
|
2022-06-28 13:56:05 +00:00
|
|
|
unsigned long i;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
/* we always have an extra reference */
|
|
|
|
uv_destroy_owned_page(pfn_to_phys(pfns[i]));
|
|
|
|
/* get rid of the extra reference */
|
|
|
|
put_page(pfn_to_page(pfns[i]));
|
|
|
|
cond_resched();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(s390_uv_destroy_pfns);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __s390_uv_destroy_range - Call the destroy secure page UVC on each page
|
|
|
|
* in the given range of the given address space.
|
|
|
|
* @mm: the mm to operate on
|
|
|
|
* @start: the start of the range
|
|
|
|
* @end: the end of the range
|
|
|
|
* @interruptible: if not 0, stop when a fatal signal is received
|
|
|
|
*
|
|
|
|
* Walk the given range of the given address space and call the destroy
|
|
|
|
* secure page UVC on each page. Optionally exit early if a fatal signal is
|
|
|
|
* pending.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -EINTR if the function stopped before completing
|
|
|
|
*/
|
|
|
|
int __s390_uv_destroy_range(struct mm_struct *mm, unsigned long start,
|
|
|
|
unsigned long end, bool interruptible)
|
|
|
|
{
|
|
|
|
struct reset_walk_state state = { .next = start };
|
|
|
|
int r = 1;
|
|
|
|
|
|
|
|
while (r > 0) {
|
|
|
|
state.count = 0;
|
|
|
|
mmap_read_lock(mm);
|
|
|
|
r = walk_page_range(mm, state.next, end, &gather_pages_ops, &state);
|
|
|
|
mmap_read_unlock(mm);
|
|
|
|
cond_resched();
|
|
|
|
s390_uv_destroy_pfns(state.count, state.pfns);
|
|
|
|
if (interruptible && fatal_signal_pending(current))
|
|
|
|
return -EINTR;
|
|
|
|
}
|
|
|
|
return 0;
|
2019-12-16 15:48:11 +00:00
|
|
|
}
|
2022-06-28 13:56:05 +00:00
|
|
|
EXPORT_SYMBOL_GPL(__s390_uv_destroy_range);
|
2022-06-28 13:56:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* s390_unlist_old_asce - Remove the topmost level of page tables from the
|
|
|
|
* list of page tables of the gmap.
|
|
|
|
* @gmap: the gmap whose table is to be removed
|
|
|
|
*
|
|
|
|
* On s390x, KVM keeps a list of all pages containing the page tables of the
|
|
|
|
* gmap (the CRST list). This list is used at tear down time to free all
|
|
|
|
* pages that are now not needed anymore.
|
|
|
|
*
|
|
|
|
* This function removes the topmost page of the tree (the one pointed to by
|
|
|
|
* the ASCE) from the CRST list.
|
|
|
|
*
|
|
|
|
* This means that it will not be freed when the VM is torn down, and needs
|
|
|
|
* to be handled separately by the caller, unless a leak is actually
|
|
|
|
* intended. Notice that this function will only remove the page from the
|
|
|
|
* list, the page will still be used as a top level page table (and ASCE).
|
|
|
|
*/
|
|
|
|
void s390_unlist_old_asce(struct gmap *gmap)
|
|
|
|
{
|
|
|
|
struct page *old;
|
|
|
|
|
|
|
|
old = virt_to_page(gmap->table);
|
|
|
|
spin_lock(&gmap->guest_table_lock);
|
|
|
|
list_del(&old->lru);
|
|
|
|
/*
|
|
|
|
* Sometimes the topmost page might need to be "removed" multiple
|
|
|
|
* times, for example if the VM is rebooted into secure mode several
|
|
|
|
* times concurrently, or if s390_replace_asce fails after calling
|
|
|
|
* s390_remove_old_asce and is attempted again later. In that case
|
|
|
|
* the old asce has been removed from the list, and therefore it
|
|
|
|
* will not be freed when the VM terminates, but the ASCE is still
|
|
|
|
* in use and still pointed to.
|
|
|
|
* A subsequent call to replace_asce will follow the pointer and try
|
|
|
|
* to remove the same page from the list again.
|
|
|
|
* Therefore it's necessary that the page of the ASCE has valid
|
|
|
|
* pointers, so list_del can work (and do nothing) without
|
|
|
|
* dereferencing stale or invalid pointers.
|
|
|
|
*/
|
|
|
|
INIT_LIST_HEAD(&old->lru);
|
|
|
|
spin_unlock(&gmap->guest_table_lock);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(s390_unlist_old_asce);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* s390_replace_asce - Try to replace the current ASCE of a gmap with a copy
|
|
|
|
* @gmap: the gmap whose ASCE needs to be replaced
|
|
|
|
*
|
2023-04-21 08:50:36 +00:00
|
|
|
* If the ASCE is a SEGMENT type then this function will return -EINVAL,
|
|
|
|
* otherwise the pointers in the host_to_guest radix tree will keep pointing
|
|
|
|
* to the wrong pages, causing use-after-free and memory corruption.
|
2022-06-28 13:56:02 +00:00
|
|
|
* If the allocation of the new top level page table fails, the ASCE is not
|
|
|
|
* replaced.
|
|
|
|
* In any case, the old ASCE is always removed from the gmap CRST list.
|
|
|
|
* Therefore the caller has to make sure to save a pointer to it
|
|
|
|
* beforehand, unless a leak is actually intended.
|
|
|
|
*/
|
|
|
|
int s390_replace_asce(struct gmap *gmap)
|
|
|
|
{
|
|
|
|
unsigned long asce;
|
|
|
|
struct page *page;
|
|
|
|
void *table;
|
|
|
|
|
|
|
|
s390_unlist_old_asce(gmap);
|
|
|
|
|
2023-04-21 08:50:36 +00:00
|
|
|
/* Replacing segment type ASCEs would cause serious issues */
|
|
|
|
if ((gmap->asce & _ASCE_TYPE_MASK) == _ASCE_TYPE_SEGMENT)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2022-06-28 13:56:02 +00:00
|
|
|
page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
|
|
|
|
if (!page)
|
|
|
|
return -ENOMEM;
|
2023-07-05 11:19:37 +00:00
|
|
|
page->index = 0;
|
2022-06-28 13:56:02 +00:00
|
|
|
table = page_to_virt(page);
|
|
|
|
memcpy(table, gmap->table, 1UL << (CRST_ALLOC_ORDER + PAGE_SHIFT));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The caller has to deal with the old ASCE, but here we make sure
|
|
|
|
* the new one is properly added to the CRST list, so that
|
|
|
|
* it will be freed when the VM is torn down.
|
|
|
|
*/
|
|
|
|
spin_lock(&gmap->guest_table_lock);
|
|
|
|
list_add(&page->lru, &gmap->crst_list);
|
|
|
|
spin_unlock(&gmap->guest_table_lock);
|
|
|
|
|
|
|
|
/* Set new table origin while preserving existing ASCE control bits */
|
|
|
|
asce = (gmap->asce & ~_ASCE_ORIGIN) | __pa(table);
|
|
|
|
WRITE_ONCE(gmap->asce, asce);
|
|
|
|
WRITE_ONCE(gmap->mm->context.gmap_asce, asce);
|
|
|
|
WRITE_ONCE(gmap->table, table);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(s390_replace_asce);
|