mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2024-12-17 07:17:02 +07:00
778e1cdd81
The kvzalloc() function has a 2-factor argument form, kvcalloc(). This patch replaces cases of: kvzalloc(a * b, gfp) with: kvcalloc(a * b, gfp) as well as handling cases of: kvzalloc(a * b * c, gfp) with: kvzalloc(array3_size(a, b, c), gfp) as it's slightly less ugly than: kvcalloc(array_size(a, b), c, gfp) This does, however, attempt to ignore constant size factors like: kvzalloc(4 * 1024, gfp) though any constants defined via macros get caught up in the conversion. Any factors with a sizeof() of "unsigned char", "char", and "u8" were dropped, since they're redundant. The Coccinelle script used for this was: // Fix redundant parens around sizeof(). @@ type TYPE; expression THING, E; @@ ( kvzalloc( - (sizeof(TYPE)) * E + sizeof(TYPE) * E , ...) | kvzalloc( - (sizeof(THING)) * E + sizeof(THING) * E , ...) ) // Drop single-byte sizes and redundant parens. @@ expression COUNT; typedef u8; typedef __u8; @@ ( kvzalloc( - sizeof(u8) * (COUNT) + COUNT , ...) | kvzalloc( - sizeof(__u8) * (COUNT) + COUNT , ...) | kvzalloc( - sizeof(char) * (COUNT) + COUNT , ...) | kvzalloc( - sizeof(unsigned char) * (COUNT) + COUNT , ...) | kvzalloc( - sizeof(u8) * COUNT + COUNT , ...) | kvzalloc( - sizeof(__u8) * COUNT + COUNT , ...) | kvzalloc( - sizeof(char) * COUNT + COUNT , ...) | kvzalloc( - sizeof(unsigned char) * COUNT + COUNT , ...) ) // 2-factor product with sizeof(type/expression) and identifier or constant. @@ type TYPE; expression THING; identifier COUNT_ID; constant COUNT_CONST; @@ ( - kvzalloc + kvcalloc ( - sizeof(TYPE) * (COUNT_ID) + COUNT_ID, sizeof(TYPE) , ...) | - kvzalloc + kvcalloc ( - sizeof(TYPE) * COUNT_ID + COUNT_ID, sizeof(TYPE) , ...) | - kvzalloc + kvcalloc ( - sizeof(TYPE) * (COUNT_CONST) + COUNT_CONST, sizeof(TYPE) , ...) | - kvzalloc + kvcalloc ( - sizeof(TYPE) * COUNT_CONST + COUNT_CONST, sizeof(TYPE) , ...) | - kvzalloc + kvcalloc ( - sizeof(THING) * (COUNT_ID) + COUNT_ID, sizeof(THING) , ...) | - kvzalloc + kvcalloc ( - sizeof(THING) * COUNT_ID + COUNT_ID, sizeof(THING) , ...) | - kvzalloc + kvcalloc ( - sizeof(THING) * (COUNT_CONST) + COUNT_CONST, sizeof(THING) , ...) | - kvzalloc + kvcalloc ( - sizeof(THING) * COUNT_CONST + COUNT_CONST, sizeof(THING) , ...) ) // 2-factor product, only identifiers. @@ identifier SIZE, COUNT; @@ - kvzalloc + kvcalloc ( - SIZE * COUNT + COUNT, SIZE , ...) // 3-factor product with 1 sizeof(type) or sizeof(expression), with // redundant parens removed. @@ expression THING; identifier STRIDE, COUNT; type TYPE; @@ ( kvzalloc( - sizeof(TYPE) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kvzalloc( - sizeof(TYPE) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kvzalloc( - sizeof(TYPE) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kvzalloc( - sizeof(TYPE) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kvzalloc( - sizeof(THING) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kvzalloc( - sizeof(THING) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kvzalloc( - sizeof(THING) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kvzalloc( - sizeof(THING) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) ) // 3-factor product with 2 sizeof(variable), with redundant parens removed. @@ expression THING1, THING2; identifier COUNT; type TYPE1, TYPE2; @@ ( kvzalloc( - sizeof(TYPE1) * sizeof(TYPE2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | kvzalloc( - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | kvzalloc( - sizeof(THING1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | kvzalloc( - sizeof(THING1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | kvzalloc( - sizeof(TYPE1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) | kvzalloc( - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) ) // 3-factor product, only identifiers, with redundant parens removed. @@ identifier STRIDE, SIZE, COUNT; @@ ( kvzalloc( - (COUNT) * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kvzalloc( - COUNT * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kvzalloc( - COUNT * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kvzalloc( - (COUNT) * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kvzalloc( - COUNT * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kvzalloc( - (COUNT) * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kvzalloc( - (COUNT) * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kvzalloc( - COUNT * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) ) // Any remaining multi-factor products, first at least 3-factor products, // when they're not all constants... @@ expression E1, E2, E3; constant C1, C2, C3; @@ ( kvzalloc(C1 * C2 * C3, ...) | kvzalloc( - (E1) * E2 * E3 + array3_size(E1, E2, E3) , ...) | kvzalloc( - (E1) * (E2) * E3 + array3_size(E1, E2, E3) , ...) | kvzalloc( - (E1) * (E2) * (E3) + array3_size(E1, E2, E3) , ...) | kvzalloc( - E1 * E2 * E3 + array3_size(E1, E2, E3) , ...) ) // And then all remaining 2 factors products when they're not all constants, // keeping sizeof() as the second factor argument. @@ expression THING, E1, E2; type TYPE; constant C1, C2, C3; @@ ( kvzalloc(sizeof(THING) * C2, ...) | kvzalloc(sizeof(TYPE) * C2, ...) | kvzalloc(C1 * C2 * C3, ...) | kvzalloc(C1 * C2, ...) | - kvzalloc + kvcalloc ( - sizeof(TYPE) * (E2) + E2, sizeof(TYPE) , ...) | - kvzalloc + kvcalloc ( - sizeof(TYPE) * E2 + E2, sizeof(TYPE) , ...) | - kvzalloc + kvcalloc ( - sizeof(THING) * (E2) + E2, sizeof(THING) , ...) | - kvzalloc + kvcalloc ( - sizeof(THING) * E2 + E2, sizeof(THING) , ...) | - kvzalloc + kvcalloc ( - (E1) * E2 + E1, E2 , ...) | - kvzalloc + kvcalloc ( - (E1) * (E2) + E1, E2 , ...) | - kvzalloc + kvcalloc ( - E1 * E2 + E1, E2 , ...) ) Signed-off-by: Kees Cook <keescook@chromium.org>
268 lines
6.7 KiB
C
268 lines
6.7 KiB
C
/*
|
|
* Support KVM gust page tracking
|
|
*
|
|
* This feature allows us to track page access in guest. Currently, only
|
|
* write access is tracked.
|
|
*
|
|
* Copyright(C) 2015 Intel Corporation.
|
|
*
|
|
* Author:
|
|
* Xiao Guangrong <guangrong.xiao@linux.intel.com>
|
|
*
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
* the COPYING file in the top-level directory.
|
|
*/
|
|
|
|
#include <linux/kvm_host.h>
|
|
#include <linux/rculist.h>
|
|
|
|
#include <asm/kvm_host.h>
|
|
#include <asm/kvm_page_track.h>
|
|
|
|
#include "mmu.h"
|
|
|
|
void kvm_page_track_free_memslot(struct kvm_memory_slot *free,
|
|
struct kvm_memory_slot *dont)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < KVM_PAGE_TRACK_MAX; i++)
|
|
if (!dont || free->arch.gfn_track[i] !=
|
|
dont->arch.gfn_track[i]) {
|
|
kvfree(free->arch.gfn_track[i]);
|
|
free->arch.gfn_track[i] = NULL;
|
|
}
|
|
}
|
|
|
|
int kvm_page_track_create_memslot(struct kvm_memory_slot *slot,
|
|
unsigned long npages)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
|
|
slot->arch.gfn_track[i] =
|
|
kvcalloc(npages, sizeof(*slot->arch.gfn_track[i]),
|
|
GFP_KERNEL);
|
|
if (!slot->arch.gfn_track[i])
|
|
goto track_free;
|
|
}
|
|
|
|
return 0;
|
|
|
|
track_free:
|
|
kvm_page_track_free_memslot(slot, NULL);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
static inline bool page_track_mode_is_valid(enum kvm_page_track_mode mode)
|
|
{
|
|
if (mode < 0 || mode >= KVM_PAGE_TRACK_MAX)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
static void update_gfn_track(struct kvm_memory_slot *slot, gfn_t gfn,
|
|
enum kvm_page_track_mode mode, short count)
|
|
{
|
|
int index, val;
|
|
|
|
index = gfn_to_index(gfn, slot->base_gfn, PT_PAGE_TABLE_LEVEL);
|
|
|
|
val = slot->arch.gfn_track[mode][index];
|
|
|
|
if (WARN_ON(val + count < 0 || val + count > USHRT_MAX))
|
|
return;
|
|
|
|
slot->arch.gfn_track[mode][index] += count;
|
|
}
|
|
|
|
/*
|
|
* add guest page to the tracking pool so that corresponding access on that
|
|
* page will be intercepted.
|
|
*
|
|
* It should be called under the protection both of mmu-lock and kvm->srcu
|
|
* or kvm->slots_lock.
|
|
*
|
|
* @kvm: the guest instance we are interested in.
|
|
* @slot: the @gfn belongs to.
|
|
* @gfn: the guest page.
|
|
* @mode: tracking mode, currently only write track is supported.
|
|
*/
|
|
void kvm_slot_page_track_add_page(struct kvm *kvm,
|
|
struct kvm_memory_slot *slot, gfn_t gfn,
|
|
enum kvm_page_track_mode mode)
|
|
{
|
|
|
|
if (WARN_ON(!page_track_mode_is_valid(mode)))
|
|
return;
|
|
|
|
update_gfn_track(slot, gfn, mode, 1);
|
|
|
|
/*
|
|
* new track stops large page mapping for the
|
|
* tracked page.
|
|
*/
|
|
kvm_mmu_gfn_disallow_lpage(slot, gfn);
|
|
|
|
if (mode == KVM_PAGE_TRACK_WRITE)
|
|
if (kvm_mmu_slot_gfn_write_protect(kvm, slot, gfn))
|
|
kvm_flush_remote_tlbs(kvm);
|
|
}
|
|
EXPORT_SYMBOL_GPL(kvm_slot_page_track_add_page);
|
|
|
|
/*
|
|
* remove the guest page from the tracking pool which stops the interception
|
|
* of corresponding access on that page. It is the opposed operation of
|
|
* kvm_slot_page_track_add_page().
|
|
*
|
|
* It should be called under the protection both of mmu-lock and kvm->srcu
|
|
* or kvm->slots_lock.
|
|
*
|
|
* @kvm: the guest instance we are interested in.
|
|
* @slot: the @gfn belongs to.
|
|
* @gfn: the guest page.
|
|
* @mode: tracking mode, currently only write track is supported.
|
|
*/
|
|
void kvm_slot_page_track_remove_page(struct kvm *kvm,
|
|
struct kvm_memory_slot *slot, gfn_t gfn,
|
|
enum kvm_page_track_mode mode)
|
|
{
|
|
if (WARN_ON(!page_track_mode_is_valid(mode)))
|
|
return;
|
|
|
|
update_gfn_track(slot, gfn, mode, -1);
|
|
|
|
/*
|
|
* allow large page mapping for the tracked page
|
|
* after the tracker is gone.
|
|
*/
|
|
kvm_mmu_gfn_allow_lpage(slot, gfn);
|
|
}
|
|
EXPORT_SYMBOL_GPL(kvm_slot_page_track_remove_page);
|
|
|
|
/*
|
|
* check if the corresponding access on the specified guest page is tracked.
|
|
*/
|
|
bool kvm_page_track_is_active(struct kvm_vcpu *vcpu, gfn_t gfn,
|
|
enum kvm_page_track_mode mode)
|
|
{
|
|
struct kvm_memory_slot *slot;
|
|
int index;
|
|
|
|
if (WARN_ON(!page_track_mode_is_valid(mode)))
|
|
return false;
|
|
|
|
slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
|
|
if (!slot)
|
|
return false;
|
|
|
|
index = gfn_to_index(gfn, slot->base_gfn, PT_PAGE_TABLE_LEVEL);
|
|
return !!READ_ONCE(slot->arch.gfn_track[mode][index]);
|
|
}
|
|
|
|
void kvm_page_track_cleanup(struct kvm *kvm)
|
|
{
|
|
struct kvm_page_track_notifier_head *head;
|
|
|
|
head = &kvm->arch.track_notifier_head;
|
|
cleanup_srcu_struct(&head->track_srcu);
|
|
}
|
|
|
|
void kvm_page_track_init(struct kvm *kvm)
|
|
{
|
|
struct kvm_page_track_notifier_head *head;
|
|
|
|
head = &kvm->arch.track_notifier_head;
|
|
init_srcu_struct(&head->track_srcu);
|
|
INIT_HLIST_HEAD(&head->track_notifier_list);
|
|
}
|
|
|
|
/*
|
|
* register the notifier so that event interception for the tracked guest
|
|
* pages can be received.
|
|
*/
|
|
void
|
|
kvm_page_track_register_notifier(struct kvm *kvm,
|
|
struct kvm_page_track_notifier_node *n)
|
|
{
|
|
struct kvm_page_track_notifier_head *head;
|
|
|
|
head = &kvm->arch.track_notifier_head;
|
|
|
|
spin_lock(&kvm->mmu_lock);
|
|
hlist_add_head_rcu(&n->node, &head->track_notifier_list);
|
|
spin_unlock(&kvm->mmu_lock);
|
|
}
|
|
EXPORT_SYMBOL_GPL(kvm_page_track_register_notifier);
|
|
|
|
/*
|
|
* stop receiving the event interception. It is the opposed operation of
|
|
* kvm_page_track_register_notifier().
|
|
*/
|
|
void
|
|
kvm_page_track_unregister_notifier(struct kvm *kvm,
|
|
struct kvm_page_track_notifier_node *n)
|
|
{
|
|
struct kvm_page_track_notifier_head *head;
|
|
|
|
head = &kvm->arch.track_notifier_head;
|
|
|
|
spin_lock(&kvm->mmu_lock);
|
|
hlist_del_rcu(&n->node);
|
|
spin_unlock(&kvm->mmu_lock);
|
|
synchronize_srcu(&head->track_srcu);
|
|
}
|
|
EXPORT_SYMBOL_GPL(kvm_page_track_unregister_notifier);
|
|
|
|
/*
|
|
* Notify the node that write access is intercepted and write emulation is
|
|
* finished at this time.
|
|
*
|
|
* The node should figure out if the written page is the one that node is
|
|
* interested in by itself.
|
|
*/
|
|
void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
|
|
int bytes)
|
|
{
|
|
struct kvm_page_track_notifier_head *head;
|
|
struct kvm_page_track_notifier_node *n;
|
|
int idx;
|
|
|
|
head = &vcpu->kvm->arch.track_notifier_head;
|
|
|
|
if (hlist_empty(&head->track_notifier_list))
|
|
return;
|
|
|
|
idx = srcu_read_lock(&head->track_srcu);
|
|
hlist_for_each_entry_rcu(n, &head->track_notifier_list, node)
|
|
if (n->track_write)
|
|
n->track_write(vcpu, gpa, new, bytes, n);
|
|
srcu_read_unlock(&head->track_srcu, idx);
|
|
}
|
|
|
|
/*
|
|
* Notify the node that memory slot is being removed or moved so that it can
|
|
* drop write-protection for the pages in the memory slot.
|
|
*
|
|
* The node should figure out it has any write-protected pages in this slot
|
|
* by itself.
|
|
*/
|
|
void kvm_page_track_flush_slot(struct kvm *kvm, struct kvm_memory_slot *slot)
|
|
{
|
|
struct kvm_page_track_notifier_head *head;
|
|
struct kvm_page_track_notifier_node *n;
|
|
int idx;
|
|
|
|
head = &kvm->arch.track_notifier_head;
|
|
|
|
if (hlist_empty(&head->track_notifier_list))
|
|
return;
|
|
|
|
idx = srcu_read_lock(&head->track_srcu);
|
|
hlist_for_each_entry_rcu(n, &head->track_notifier_list, node)
|
|
if (n->track_flush_slot)
|
|
n->track_flush_slot(kvm, slot, n);
|
|
srcu_read_unlock(&head->track_srcu, idx);
|
|
}
|