Skip to content

Commit 84c8da1

Browse files
committed
Merge: KVM: backport improvements to dirty_log_test
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-10/-/merge_requests/558 Backport a series of improvements to dirty_log_test that make this test more robust, and in particular make it work in L1 Also backport a minor fix to KVM which needs to be present in L1 kernel to avoid the test failing, when run on Intel. JIRA: https://issues.redhat.com/browse/RHEL-58941 Tested: run the test few times Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com> Approved-by: Vitaly Kuznetsov <vkuznets@redhat.com> Approved-by: Cathy Avery <cavery@redhat.com> Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com> Merged-by: Julio Faracco <jfaracco@redhat.com>
2 parents 6b9ed21 + 7fd3ef3 commit 84c8da1

File tree

5 files changed

+273
-291
lines changed

5 files changed

+273
-291
lines changed

arch/x86/kvm/vmx/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
126126
.check_intercept = vmx_check_intercept,
127127
.handle_exit_irqoff = vmx_handle_exit_irqoff,
128128

129-
.cpu_dirty_log_size = PML_ENTITY_NUM,
129+
.cpu_dirty_log_size = PML_LOG_NR_ENTRIES,
130130
.update_cpu_dirty_logging = vmx_update_cpu_dirty_logging,
131131

132132
.nested_ops = &vmx_nested_ops,

arch/x86/kvm/vmx/nested.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3442,7 +3442,7 @@ static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
34423442
if (!nested_cpu_has_pml(vmcs12))
34433443
return 0;
34443444

3445-
if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
3445+
if (vmcs12->guest_pml_index >= PML_LOG_NR_ENTRIES) {
34463446
vmx->nested.pml_full = true;
34473447
return 1;
34483448
}

arch/x86/kvm/vmx/vmx.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4830,7 +4830,7 @@ static void init_vmcs(struct vcpu_vmx *vmx)
48304830

48314831
if (enable_pml) {
48324832
vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
4833-
vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
4833+
vmcs_write16(GUEST_PML_INDEX, PML_HEAD_INDEX);
48344834
}
48354835

48364836
vmx_write_encls_bitmap(&vmx->vcpu, NULL);
@@ -6210,32 +6210,40 @@ static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
62106210
static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
62116211
{
62126212
struct vcpu_vmx *vmx = to_vmx(vcpu);
6213+
u16 pml_idx, pml_tail_index;
62136214
u64 *pml_buf;
6214-
u16 pml_idx;
6215+
int i;
62156216

62166217
pml_idx = vmcs_read16(GUEST_PML_INDEX);
62176218

62186219
/* Do nothing if PML buffer is empty */
6219-
if (pml_idx == (PML_ENTITY_NUM - 1))
6220+
if (pml_idx == PML_HEAD_INDEX)
62206221
return;
6222+
/*
6223+
* PML index always points to the next available PML buffer entity
6224+
* unless PML log has just overflowed.
6225+
*/
6226+
pml_tail_index = (pml_idx >= PML_LOG_NR_ENTRIES) ? 0 : pml_idx + 1;
62216227

6222-
/* PML index always points to next available PML buffer entity */
6223-
if (pml_idx >= PML_ENTITY_NUM)
6224-
pml_idx = 0;
6225-
else
6226-
pml_idx++;
6227-
6228+
/*
6229+
* PML log is written backwards: the CPU first writes the entry 511
6230+
* then the entry 510, and so on.
6231+
*
6232+
* Read the entries in the same order they were written, to ensure that
6233+
* the dirty ring is filled in the same order the CPU wrote them.
6234+
*/
62286235
pml_buf = page_address(vmx->pml_pg);
6229-
for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
6236+
6237+
for (i = PML_HEAD_INDEX; i >= pml_tail_index; i--) {
62306238
u64 gpa;
62316239

6232-
gpa = pml_buf[pml_idx];
6240+
gpa = pml_buf[i];
62336241
WARN_ON(gpa & (PAGE_SIZE - 1));
62346242
kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
62356243
}
62366244

62376245
/* reset PML index */
6238-
vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6246+
vmcs_write16(GUEST_PML_INDEX, PML_HEAD_INDEX);
62396247
}
62406248

62416249
static void vmx_dump_sel(char *name, uint32_t sel)

arch/x86/kvm/vmx/vmx.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,10 @@ struct vcpu_vmx {
332332
bool ple_window_dirty;
333333

334334
/* Support for PML */
335-
#define PML_ENTITY_NUM 512
335+
#define PML_LOG_NR_ENTRIES 512
336+
/* PML is written backwards: this is the first entry written by the CPU */
337+
#define PML_HEAD_INDEX (PML_LOG_NR_ENTRIES-1)
338+
336339
struct page *pml_pg;
337340

338341
/* apic deadline value in host tsc */

0 commit comments

Comments
 (0)