2019-06-04 08:11:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
KVM: page track: add the framework of guest page tracking
The array, gfn_track[mode][gfn], is introduced in memory slot for every
guest page, this is the tracking count for the gust page on different
modes. If the page is tracked then the count is increased, the page is
not tracked after the count reaches zero
We use 'unsigned short' as the tracking count which should be enough as
shadow page table only can use 2^14 (2^3 for level, 2^1 for cr4_pae, 2^2
for quadrant, 2^3 for access, 2^1 for nxe, 2^1 for cr0_wp, 2^1 for
smep_andnot_wp, 2^1 for smap_andnot_wp, and 2^1 for smm) at most, there
is enough room for other trackers
Two callbacks, kvm_page_track_create_memslot() and
kvm_page_track_free_memslot() are implemented in this patch, they are
internally used to initialize and reclaim the memory of the array
Currently, only write track mode is supported
Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-02-24 09:51:09 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kvm_host.h>
|
2017-02-04 00:27:20 +00:00
|
|
|
#include <linux/rculist.h>
|
|
|
|
|
KVM: page track: add the framework of guest page tracking
The array, gfn_track[mode][gfn], is introduced in memory slot for every
guest page, this is the tracking count for the gust page on different
modes. If the page is tracked then the count is increased, the page is
not tracked after the count reaches zero
We use 'unsigned short' as the tracking count which should be enough as
shadow page table only can use 2^14 (2^3 for level, 2^1 for cr4_pae, 2^2
for quadrant, 2^3 for access, 2^1 for nxe, 2^1 for cr0_wp, 2^1 for
smep_andnot_wp, 2^1 for smap_andnot_wp, and 2^1 for smm) at most, there
is enough room for other trackers
Two callbacks, kvm_page_track_create_memslot() and
kvm_page_track_free_memslot() are implemented in this patch, they are
internally used to initialize and reclaim the memory of the array
Currently, only write track mode is supported
Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-02-24 09:51:09 +00:00
|
|
|
#include <asm/kvm_page_track.h>
|
|
|
|
|
2021-07-30 22:04:51 +00:00
|
|
|
#include "mmu.h"
|
2020-06-22 20:20:31 +00:00
|
|
|
#include "mmu_internal.h"
|
KVM: page track: add the framework of guest page tracking
The array, gfn_track[mode][gfn], is introduced in memory slot for every
guest page, this is the tracking count for the gust page on different
modes. If the page is tracked then the count is increased, the page is
not tracked after the count reaches zero
We use 'unsigned short' as the tracking count which should be enough as
shadow page table only can use 2^14 (2^3 for level, 2^1 for cr4_pae, 2^2
for quadrant, 2^3 for access, 2^1 for nxe, 2^1 for cr0_wp, 2^1 for
smep_andnot_wp, 2^1 for smap_andnot_wp, and 2^1 for smm) at most, there
is enough room for other trackers
Two callbacks, kvm_page_track_create_memslot() and
kvm_page_track_free_memslot() are implemented in this patch, they are
internally used to initialize and reclaim the memory of the array
Currently, only write track mode is supported
Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-02-24 09:51:09 +00:00
|
|
|
|
2021-10-15 16:30:21 +00:00
|
|
|
bool kvm_page_track_write_tracking_enabled(struct kvm *kvm)
|
2021-09-22 04:58:59 +00:00
|
|
|
{
|
|
|
|
return IS_ENABLED(CONFIG_KVM_EXTERNAL_WRITE_TRACKING) ||
|
2021-10-15 16:30:21 +00:00
|
|
|
!tdp_enabled || kvm_shadow_root_allocated(kvm);
|
2021-09-22 04:58:59 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 21:07:27 +00:00
|
|
|
void kvm_page_track_free_memslot(struct kvm_memory_slot *slot)
|
KVM: page track: add the framework of guest page tracking
The array, gfn_track[mode][gfn], is introduced in memory slot for every
guest page, this is the tracking count for the gust page on different
modes. If the page is tracked then the count is increased, the page is
not tracked after the count reaches zero
We use 'unsigned short' as the tracking count which should be enough as
shadow page table only can use 2^14 (2^3 for level, 2^1 for cr4_pae, 2^2
for quadrant, 2^3 for access, 2^1 for nxe, 2^1 for cr0_wp, 2^1 for
smep_andnot_wp, 2^1 for smap_andnot_wp, and 2^1 for smm) at most, there
is enough room for other trackers
Two callbacks, kvm_page_track_create_memslot() and
kvm_page_track_free_memslot() are implemented in this patch, they are
internally used to initialize and reclaim the memory of the array
Currently, only write track mode is supported
Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-02-24 09:51:09 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2020-02-18 21:07:27 +00:00
|
|
|
for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
|
|
|
|
kvfree(slot->arch.gfn_track[i]);
|
|
|
|
slot->arch.gfn_track[i] = NULL;
|
|
|
|
}
|
KVM: page track: add the framework of guest page tracking
The array, gfn_track[mode][gfn], is introduced in memory slot for every
guest page, this is the tracking count for the gust page on different
modes. If the page is tracked then the count is increased, the page is
not tracked after the count reaches zero
We use 'unsigned short' as the tracking count which should be enough as
shadow page table only can use 2^14 (2^3 for level, 2^1 for cr4_pae, 2^2
for quadrant, 2^3 for access, 2^1 for nxe, 2^1 for cr0_wp, 2^1 for
smep_andnot_wp, 2^1 for smap_andnot_wp, and 2^1 for smm) at most, there
is enough room for other trackers
Two callbacks, kvm_page_track_create_memslot() and
kvm_page_track_free_memslot() are implemented in this patch, they are
internally used to initialize and reclaim the memory of the array
Currently, only write track mode is supported
Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-02-24 09:51:09 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 04:58:59 +00:00
|
|
|
int kvm_page_track_create_memslot(struct kvm *kvm,
|
|
|
|
struct kvm_memory_slot *slot,
|
KVM: page track: add the framework of guest page tracking
The array, gfn_track[mode][gfn], is introduced in memory slot for every
guest page, this is the tracking count for the gust page on different
modes. If the page is tracked then the count is increased, the page is
not tracked after the count reaches zero
We use 'unsigned short' as the tracking count which should be enough as
shadow page table only can use 2^14 (2^3 for level, 2^1 for cr4_pae, 2^2
for quadrant, 2^3 for access, 2^1 for nxe, 2^1 for cr0_wp, 2^1 for
smep_andnot_wp, 2^1 for smap_andnot_wp, and 2^1 for smm) at most, there
is enough room for other trackers
Two callbacks, kvm_page_track_create_memslot() and
kvm_page_track_free_memslot() are implemented in this patch, they are
internally used to initialize and reclaim the memory of the array
Currently, only write track mode is supported
Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-02-24 09:51:09 +00:00
|
|
|
unsigned long npages)
|
|
|
|
{
|
2021-09-22 04:58:59 +00:00
|
|
|
int i;
|
KVM: page track: add the framework of guest page tracking
The array, gfn_track[mode][gfn], is introduced in memory slot for every
guest page, this is the tracking count for the gust page on different
modes. If the page is tracked then the count is increased, the page is
not tracked after the count reaches zero
We use 'unsigned short' as the tracking count which should be enough as
shadow page table only can use 2^14 (2^3 for level, 2^1 for cr4_pae, 2^2
for quadrant, 2^3 for access, 2^1 for nxe, 2^1 for cr0_wp, 2^1 for
smep_andnot_wp, 2^1 for smap_andnot_wp, and 2^1 for smm) at most, there
is enough room for other trackers
Two callbacks, kvm_page_track_create_memslot() and
kvm_page_track_free_memslot() are implemented in this patch, they are
internally used to initialize and reclaim the memory of the array
Currently, only write track mode is supported
Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-02-24 09:51:09 +00:00
|
|
|
|
|
|
|
for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
|
2021-10-15 16:30:21 +00:00
|
|
|
if (i == KVM_PAGE_TRACK_WRITE &&
|
|
|
|
!kvm_page_track_write_tracking_enabled(kvm))
|
2021-09-22 04:58:59 +00:00
|
|
|
continue;
|
|
|
|
|
treewide: kvzalloc() -> kvcalloc()
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>
2018-06-12 21:04:48 +00:00
|
|
|
slot->arch.gfn_track[i] =
|
|
|
|
kvcalloc(npages, sizeof(*slot->arch.gfn_track[i]),
|
2019-02-11 19:02:50 +00:00
|
|
|
GFP_KERNEL_ACCOUNT);
|
KVM: page track: add the framework of guest page tracking
The array, gfn_track[mode][gfn], is introduced in memory slot for every
guest page, this is the tracking count for the gust page on different
modes. If the page is tracked then the count is increased, the page is
not tracked after the count reaches zero
We use 'unsigned short' as the tracking count which should be enough as
shadow page table only can use 2^14 (2^3 for level, 2^1 for cr4_pae, 2^2
for quadrant, 2^3 for access, 2^1 for nxe, 2^1 for cr0_wp, 2^1 for
smep_andnot_wp, 2^1 for smap_andnot_wp, and 2^1 for smm) at most, there
is enough room for other trackers
Two callbacks, kvm_page_track_create_memslot() and
kvm_page_track_free_memslot() are implemented in this patch, they are
internally used to initialize and reclaim the memory of the array
Currently, only write track mode is supported
Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-02-24 09:51:09 +00:00
|
|
|
if (!slot->arch.gfn_track[i])
|
|
|
|
goto track_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
track_free:
|
2020-02-18 21:07:27 +00:00
|
|
|
kvm_page_track_free_memslot(slot);
|
KVM: page track: add the framework of guest page tracking
The array, gfn_track[mode][gfn], is introduced in memory slot for every
guest page, this is the tracking count for the gust page on different
modes. If the page is tracked then the count is increased, the page is
not tracked after the count reaches zero
We use 'unsigned short' as the tracking count which should be enough as
shadow page table only can use 2^14 (2^3 for level, 2^1 for cr4_pae, 2^2
for quadrant, 2^3 for access, 2^1 for nxe, 2^1 for cr0_wp, 2^1 for
smep_andnot_wp, 2^1 for smap_andnot_wp, and 2^1 for smm) at most, there
is enough room for other trackers
Two callbacks, kvm_page_track_create_memslot() and
kvm_page_track_free_memslot() are implemented in this patch, they are
internally used to initialize and reclaim the memory of the array
Currently, only write track mode is supported
Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-02-24 09:51:09 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2016-02-24 09:51:10 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-10-15 16:30:21 +00:00
|
|
|
int kvm_page_track_write_tracking_alloc(struct kvm_memory_slot *slot)
|
2021-09-22 04:58:59 +00:00
|
|
|
{
|
2021-10-15 16:30:21 +00:00
|
|
|
unsigned short *gfn_track;
|
2021-09-22 04:58:59 +00:00
|
|
|
|
2021-10-15 16:30:21 +00:00
|
|
|
if (slot->arch.gfn_track[KVM_PAGE_TRACK_WRITE])
|
2021-09-22 04:58:59 +00:00
|
|
|
return 0;
|
|
|
|
|
2021-10-15 16:30:21 +00:00
|
|
|
gfn_track = kvcalloc(slot->npages, sizeof(*gfn_track), GFP_KERNEL_ACCOUNT);
|
|
|
|
if (gfn_track == NULL)
|
|
|
|
return -ENOMEM;
|
2021-09-22 04:58:59 +00:00
|
|
|
|
2021-10-15 16:30:21 +00:00
|
|
|
slot->arch.gfn_track[KVM_PAGE_TRACK_WRITE] = gfn_track;
|
2021-09-22 04:58:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-24 09:51:10 +00:00
|
|
|
static void update_gfn_track(struct kvm_memory_slot *slot, gfn_t gfn,
|
|
|
|
enum kvm_page_track_mode mode, short count)
|
|
|
|
{
|
|
|
|
int index, val;
|
|
|
|
|
2020-04-28 00:54:22 +00:00
|
|
|
index = gfn_to_index(gfn, slot->base_gfn, PG_LEVEL_4K);
|
2016-02-24 09:51:10 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-09-22 04:58:59 +00:00
|
|
|
if (WARN_ON(mode == KVM_PAGE_TRACK_WRITE &&
|
2021-10-15 16:30:21 +00:00
|
|
|
!kvm_page_track_write_tracking_enabled(kvm)))
|
2021-09-22 04:58:59 +00:00
|
|
|
return;
|
|
|
|
|
2016-02-24 09:51:10 +00:00
|
|
|
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)
|
2021-04-29 03:41:14 +00:00
|
|
|
if (kvm_mmu_slot_gfn_write_protect(kvm, slot, gfn, PG_LEVEL_4K))
|
2016-02-24 09:51:10 +00:00
|
|
|
kvm_flush_remote_tlbs(kvm);
|
|
|
|
}
|
2016-10-25 07:50:43 +00:00
|
|
|
EXPORT_SYMBOL_GPL(kvm_slot_page_track_add_page);
|
2016-02-24 09:51:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2021-09-22 04:58:59 +00:00
|
|
|
if (WARN_ON(mode == KVM_PAGE_TRACK_WRITE &&
|
2021-10-15 16:30:21 +00:00
|
|
|
!kvm_page_track_write_tracking_enabled(kvm)))
|
2021-09-22 04:58:59 +00:00
|
|
|
return;
|
|
|
|
|
2016-02-24 09:51:10 +00:00
|
|
|
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);
|
|
|
|
}
|
2016-10-25 07:50:43 +00:00
|
|
|
EXPORT_SYMBOL_GPL(kvm_slot_page_track_remove_page);
|
2016-02-24 09:51:11 +00:00
|
|
|
|
2021-08-17 12:46:45 +00:00
|
|
|
/*
|
|
|
|
* check if the corresponding access on the specified guest page is tracked.
|
|
|
|
*/
|
2021-11-15 23:45:53 +00:00
|
|
|
bool kvm_slot_page_track_is_active(struct kvm *kvm,
|
2021-11-15 23:45:58 +00:00
|
|
|
const struct kvm_memory_slot *slot,
|
|
|
|
gfn_t gfn, enum kvm_page_track_mode mode)
|
2016-02-24 09:51:11 +00:00
|
|
|
{
|
2016-03-22 16:25:42 +00:00
|
|
|
int index;
|
2016-02-24 09:51:11 +00:00
|
|
|
|
|
|
|
if (WARN_ON(!page_track_mode_is_valid(mode)))
|
|
|
|
return false;
|
|
|
|
|
2016-03-22 16:25:42 +00:00
|
|
|
if (!slot)
|
|
|
|
return false;
|
|
|
|
|
2021-10-15 16:30:21 +00:00
|
|
|
if (mode == KVM_PAGE_TRACK_WRITE &&
|
2021-11-15 23:45:53 +00:00
|
|
|
!kvm_page_track_write_tracking_enabled(kvm))
|
2021-09-22 04:58:59 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-28 00:54:22 +00:00
|
|
|
index = gfn_to_index(gfn, slot->base_gfn, PG_LEVEL_4K);
|
locking/atomics: COCCINELLE/treewide: Convert trivial ACCESS_ONCE() patterns to READ_ONCE()/WRITE_ONCE()
Please do not apply this to mainline directly, instead please re-run the
coccinelle script shown below and apply its output.
For several reasons, it is desirable to use {READ,WRITE}_ONCE() in
preference to ACCESS_ONCE(), and new code is expected to use one of the
former. So far, there's been no reason to change most existing uses of
ACCESS_ONCE(), as these aren't harmful, and changing them results in
churn.
However, for some features, the read/write distinction is critical to
correct operation. To distinguish these cases, separate read/write
accessors must be used. This patch migrates (most) remaining
ACCESS_ONCE() instances to {READ,WRITE}_ONCE(), using the following
coccinelle script:
----
// Convert trivial ACCESS_ONCE() uses to equivalent READ_ONCE() and
// WRITE_ONCE()
// $ make coccicheck COCCI=/home/mark/once.cocci SPFLAGS="--include-headers" MODE=patch
virtual patch
@ depends on patch @
expression E1, E2;
@@
- ACCESS_ONCE(E1) = E2
+ WRITE_ONCE(E1, E2)
@ depends on patch @
expression E;
@@
- ACCESS_ONCE(E)
+ READ_ONCE(E)
----
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: davem@davemloft.net
Cc: linux-arch@vger.kernel.org
Cc: mpe@ellerman.id.au
Cc: shuah@kernel.org
Cc: snitzer@redhat.com
Cc: thor.thayer@linux.intel.com
Cc: tj@kernel.org
Cc: viro@zeniv.linux.org.uk
Cc: will.deacon@arm.com
Link: http://lkml.kernel.org/r/1508792849-3115-19-git-send-email-paulmck@linux.vnet.ibm.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-10-23 21:07:29 +00:00
|
|
|
return !!READ_ONCE(slot->arch.gfn_track[mode][index]);
|
2016-02-24 09:51:11 +00:00
|
|
|
}
|
2016-02-24 09:51:13 +00:00
|
|
|
|
2017-03-27 15:53:50 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-09-03 02:37:06 +00:00
|
|
|
int kvm_page_track_init(struct kvm *kvm)
|
2016-02-24 09:51:13 +00:00
|
|
|
{
|
|
|
|
struct kvm_page_track_notifier_head *head;
|
|
|
|
|
|
|
|
head = &kvm->arch.track_notifier_head;
|
|
|
|
INIT_HLIST_HEAD(&head->track_notifier_list);
|
2021-09-03 02:37:06 +00:00
|
|
|
return init_srcu_struct(&head->track_srcu);
|
2016-02-24 09:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2021-02-02 18:57:24 +00:00
|
|
|
write_lock(&kvm->mmu_lock);
|
2016-02-24 09:51:13 +00:00
|
|
|
hlist_add_head_rcu(&n->node, &head->track_notifier_list);
|
2021-02-02 18:57:24 +00:00
|
|
|
write_unlock(&kvm->mmu_lock);
|
2016-02-24 09:51:13 +00:00
|
|
|
}
|
2016-10-25 07:50:43 +00:00
|
|
|
EXPORT_SYMBOL_GPL(kvm_page_track_register_notifier);
|
2016-02-24 09:51:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2021-02-02 18:57:24 +00:00
|
|
|
write_lock(&kvm->mmu_lock);
|
2016-02-24 09:51:13 +00:00
|
|
|
hlist_del_rcu(&n->node);
|
2021-02-02 18:57:24 +00:00
|
|
|
write_unlock(&kvm->mmu_lock);
|
2016-02-24 09:51:13 +00:00
|
|
|
synchronize_srcu(&head->track_srcu);
|
|
|
|
}
|
2016-10-25 07:50:43 +00:00
|
|
|
EXPORT_SYMBOL_GPL(kvm_page_track_unregister_notifier);
|
2016-02-24 09:51:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2020-07-12 13:10:03 +00:00
|
|
|
hlist_for_each_entry_srcu(n, &head->track_notifier_list, node,
|
|
|
|
srcu_read_lock_held(&head->track_srcu))
|
2016-02-24 09:51:13 +00:00
|
|
|
if (n->track_write)
|
2016-10-25 07:50:42 +00:00
|
|
|
n->track_write(vcpu, gpa, new, bytes, n);
|
2016-02-24 09:51:13 +00:00
|
|
|
srcu_read_unlock(&head->track_srcu, idx);
|
|
|
|
}
|
2016-10-09 07:41:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2020-07-12 13:10:03 +00:00
|
|
|
hlist_for_each_entry_srcu(n, &head->track_notifier_list, node,
|
|
|
|
srcu_read_lock_held(&head->track_srcu))
|
2016-10-09 07:41:44 +00:00
|
|
|
if (n->track_flush_slot)
|
2016-10-25 07:50:42 +00:00
|
|
|
n->track_flush_slot(kvm, slot, n);
|
2016-10-09 07:41:44 +00:00
|
|
|
srcu_read_unlock(&head->track_srcu, idx);
|
|
|
|
}
|