KVM
Functions
dirty_ring.c File Reference
#include <linux/kvm_host.h>
#include <linux/kvm.h>
#include <linux/vmalloc.h>
#include <linux/kvm_dirty_ring.h>
#include <trace/events/kvm.h>
#include "kvm_mm.h"
Include dependency graph for dirty_ring.c:

Go to the source code of this file.

Functions

int __weak kvm_cpu_dirty_log_size (void)
 
u32 kvm_dirty_ring_get_rsvd_entries (void)
 
bool kvm_use_dirty_bitmap (struct kvm *kvm)
 
bool kvm_arch_allow_write_without_running_vcpu (struct kvm *kvm)
 
static u32 kvm_dirty_ring_used (struct kvm_dirty_ring *ring)
 
static bool kvm_dirty_ring_soft_full (struct kvm_dirty_ring *ring)
 
static bool kvm_dirty_ring_full (struct kvm_dirty_ring *ring)
 
static void kvm_reset_dirty_gfn (struct kvm *kvm, u32 slot, u64 offset, u64 mask)
 
int kvm_dirty_ring_alloc (struct kvm_dirty_ring *ring, int index, u32 size)
 
static void kvm_dirty_gfn_set_invalid (struct kvm_dirty_gfn *gfn)
 
static void kvm_dirty_gfn_set_dirtied (struct kvm_dirty_gfn *gfn)
 
static bool kvm_dirty_gfn_harvested (struct kvm_dirty_gfn *gfn)
 
int kvm_dirty_ring_reset (struct kvm *kvm, struct kvm_dirty_ring *ring)
 
void kvm_dirty_ring_push (struct kvm_vcpu *vcpu, u32 slot, u64 offset)
 
bool kvm_dirty_ring_check_request (struct kvm_vcpu *vcpu)
 
struct page * kvm_dirty_ring_get_page (struct kvm_dirty_ring *ring, u32 offset)
 
void kvm_dirty_ring_free (struct kvm_dirty_ring *ring)
 

Function Documentation

◆ kvm_arch_allow_write_without_running_vcpu()

bool kvm_arch_allow_write_without_running_vcpu ( struct kvm *  kvm)

Definition at line 32 of file dirty_ring.c.

33 {
34  return false;
35 }

◆ kvm_cpu_dirty_log_size()

int __weak kvm_cpu_dirty_log_size ( void  )

Definition at line 14 of file dirty_ring.c.

15 {
16  return 0;
17 }
Here is the caller graph for this function:

◆ kvm_dirty_gfn_harvested()

static bool kvm_dirty_gfn_harvested ( struct kvm_dirty_gfn *  gfn)
inlinestatic

Definition at line 99 of file dirty_ring.c.

100 {
101  return smp_load_acquire(&gfn->flags) & KVM_DIRTY_GFN_F_RESET;
102 }
Here is the caller graph for this function:

◆ kvm_dirty_gfn_set_dirtied()

static void kvm_dirty_gfn_set_dirtied ( struct kvm_dirty_gfn *  gfn)
inlinestatic

Definition at line 94 of file dirty_ring.c.

95 {
96  gfn->flags = KVM_DIRTY_GFN_F_DIRTY;
97 }
Here is the caller graph for this function:

◆ kvm_dirty_gfn_set_invalid()

static void kvm_dirty_gfn_set_invalid ( struct kvm_dirty_gfn *  gfn)
inlinestatic

Definition at line 89 of file dirty_ring.c.

90 {
91  smp_store_release(&gfn->flags, 0);
92 }
Here is the caller graph for this function:

◆ kvm_dirty_ring_alloc()

int kvm_dirty_ring_alloc ( struct kvm_dirty_ring *  ring,
int  index,
u32  size 
)

Definition at line 74 of file dirty_ring.c.

75 {
76  ring->dirty_gfns = vzalloc(size);
77  if (!ring->dirty_gfns)
78  return -ENOMEM;
79 
80  ring->size = size / sizeof(struct kvm_dirty_gfn);
81  ring->soft_limit = ring->size - kvm_dirty_ring_get_rsvd_entries();
82  ring->dirty_index = 0;
83  ring->reset_index = 0;
84  ring->index = index;
85 
86  return 0;
87 }
u32 kvm_dirty_ring_get_rsvd_entries(void)
Definition: dirty_ring.c:19
size_t size
Definition: gen-hyprel.c:133
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_dirty_ring_check_request()

bool kvm_dirty_ring_check_request ( struct kvm_vcpu *  vcpu)

Definition at line 194 of file dirty_ring.c.

195 {
196  /*
197  * The VCPU isn't runnable when the dirty ring becomes soft full.
198  * The KVM_REQ_DIRTY_RING_SOFT_FULL event is always set to prevent
199  * the VCPU from running until the dirty pages are harvested and
200  * the dirty ring is reset by userspace.
201  */
202  if (kvm_check_request(KVM_REQ_DIRTY_RING_SOFT_FULL, vcpu) &&
203  kvm_dirty_ring_soft_full(&vcpu->dirty_ring)) {
204  kvm_make_request(KVM_REQ_DIRTY_RING_SOFT_FULL, vcpu);
205  vcpu->run->exit_reason = KVM_EXIT_DIRTY_RING_FULL;
206  trace_kvm_dirty_ring_exit(vcpu);
207  return true;
208  }
209 
210  return false;
211 }
static bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring *ring)
Definition: dirty_ring.c:43
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_dirty_ring_free()

void kvm_dirty_ring_free ( struct kvm_dirty_ring *  ring)

Definition at line 218 of file dirty_ring.c.

219 {
220  vfree(ring->dirty_gfns);
221  ring->dirty_gfns = NULL;
222 }
Here is the caller graph for this function:

◆ kvm_dirty_ring_full()

static bool kvm_dirty_ring_full ( struct kvm_dirty_ring *  ring)
static

Definition at line 48 of file dirty_ring.c.

49 {
50  return kvm_dirty_ring_used(ring) >= ring->size;
51 }
static u32 kvm_dirty_ring_used(struct kvm_dirty_ring *ring)
Definition: dirty_ring.c:38
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_dirty_ring_get_page()

struct page* kvm_dirty_ring_get_page ( struct kvm_dirty_ring *  ring,
u32  offset 
)

Definition at line 213 of file dirty_ring.c.

214 {
215  return vmalloc_to_page((void *)ring->dirty_gfns + offset * PAGE_SIZE);
216 }
Here is the caller graph for this function:

◆ kvm_dirty_ring_get_rsvd_entries()

u32 kvm_dirty_ring_get_rsvd_entries ( void  )

Definition at line 19 of file dirty_ring.c.

20 {
21  return KVM_DIRTY_RING_RSVD_ENTRIES + kvm_cpu_dirty_log_size();
22 }
int __weak kvm_cpu_dirty_log_size(void)
Definition: dirty_ring.c:14
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_dirty_ring_push()

void kvm_dirty_ring_push ( struct kvm_vcpu *  vcpu,
u32  slot,
u64  offset 
)

Definition at line 169 of file dirty_ring.c.

170 {
171  struct kvm_dirty_ring *ring = &vcpu->dirty_ring;
172  struct kvm_dirty_gfn *entry;
173 
174  /* It should never get full */
175  WARN_ON_ONCE(kvm_dirty_ring_full(ring));
176 
177  entry = &ring->dirty_gfns[ring->dirty_index & (ring->size - 1)];
178 
179  entry->slot = slot;
180  entry->offset = offset;
181  /*
182  * Make sure the data is filled in before we publish this to
183  * the userspace program. There's no paired kernel-side reader.
184  */
185  smp_wmb();
187  ring->dirty_index++;
188  trace_kvm_dirty_ring_push(ring, slot, offset);
189 
190  if (kvm_dirty_ring_soft_full(ring))
191  kvm_make_request(KVM_REQ_DIRTY_RING_SOFT_FULL, vcpu);
192 }
static bool kvm_dirty_ring_full(struct kvm_dirty_ring *ring)
Definition: dirty_ring.c:48
static void kvm_dirty_gfn_set_dirtied(struct kvm_dirty_gfn *gfn)
Definition: dirty_ring.c:94
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_dirty_ring_reset()

int kvm_dirty_ring_reset ( struct kvm *  kvm,
struct kvm_dirty_ring *  ring 
)

Definition at line 104 of file dirty_ring.c.

105 {
106  u32 cur_slot, next_slot;
107  u64 cur_offset, next_offset;
108  unsigned long mask;
109  int count = 0;
110  struct kvm_dirty_gfn *entry;
111  bool first_round = true;
112 
113  /* This is only needed to make compilers happy */
114  cur_slot = cur_offset = mask = 0;
115 
116  while (true) {
117  entry = &ring->dirty_gfns[ring->reset_index & (ring->size - 1)];
118 
119  if (!kvm_dirty_gfn_harvested(entry))
120  break;
121 
122  next_slot = READ_ONCE(entry->slot);
123  next_offset = READ_ONCE(entry->offset);
124 
125  /* Update the flags to reflect that this GFN is reset */
127 
128  ring->reset_index++;
129  count++;
130  /*
131  * Try to coalesce the reset operations when the guest is
132  * scanning pages in the same slot.
133  */
134  if (!first_round && next_slot == cur_slot) {
135  s64 delta = next_offset - cur_offset;
136 
137  if (delta >= 0 && delta < BITS_PER_LONG) {
138  mask |= 1ull << delta;
139  continue;
140  }
141 
142  /* Backwards visit, careful about overflows! */
143  if (delta > -BITS_PER_LONG && delta < 0 &&
144  (mask << -delta >> -delta) == mask) {
145  cur_offset = next_offset;
146  mask = (mask << -delta) | 1;
147  continue;
148  }
149  }
150  kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask);
151  cur_slot = next_slot;
152  cur_offset = next_offset;
153  mask = 1;
154  first_round = false;
155  }
156 
157  kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask);
158 
159  /*
160  * The request KVM_REQ_DIRTY_RING_SOFT_FULL will be cleared
161  * by the VCPU thread next time when it enters the guest.
162  */
163 
164  trace_kvm_dirty_ring_reset(ring);
165 
166  return count;
167 }
static void kvm_dirty_gfn_set_invalid(struct kvm_dirty_gfn *gfn)
Definition: dirty_ring.c:89
static void kvm_reset_dirty_gfn(struct kvm *kvm, u32 slot, u64 offset, u64 mask)
Definition: dirty_ring.c:53
static bool kvm_dirty_gfn_harvested(struct kvm_dirty_gfn *gfn)
Definition: dirty_ring.c:99
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_dirty_ring_soft_full()

static bool kvm_dirty_ring_soft_full ( struct kvm_dirty_ring *  ring)
static

Definition at line 43 of file dirty_ring.c.

44 {
45  return kvm_dirty_ring_used(ring) >= ring->soft_limit;
46 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_dirty_ring_used()

static u32 kvm_dirty_ring_used ( struct kvm_dirty_ring *  ring)
static

Definition at line 38 of file dirty_ring.c.

39 {
40  return READ_ONCE(ring->dirty_index) - READ_ONCE(ring->reset_index);
41 }
Here is the caller graph for this function:

◆ kvm_reset_dirty_gfn()

static void kvm_reset_dirty_gfn ( struct kvm *  kvm,
u32  slot,
u64  offset,
u64  mask 
)
static

Definition at line 53 of file dirty_ring.c.

54 {
55  struct kvm_memory_slot *memslot;
56  int as_id, id;
57 
58  as_id = slot >> 16;
59  id = (u16)slot;
60 
61  if (as_id >= kvm_arch_nr_memslot_as_ids(kvm) || id >= KVM_USER_MEM_SLOTS)
62  return;
63 
64  memslot = id_to_memslot(__kvm_memslots(kvm, as_id), id);
65 
66  if (!memslot || (offset + __fls(mask)) >= memslot->npages)
67  return;
68 
69  KVM_MMU_LOCK(kvm);
70  kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot, offset, mask);
71  KVM_MMU_UNLOCK(kvm);
72 }
#define KVM_MMU_LOCK(kvm)
Definition: kvm_mm.h:19
#define KVM_MMU_UNLOCK(kvm)
Definition: kvm_mm.h:20
void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn_offset, unsigned long mask)
Definition: mmu.c:1185
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_use_dirty_bitmap()

bool kvm_use_dirty_bitmap ( struct kvm *  kvm)

Definition at line 24 of file dirty_ring.c.

25 {
26  lockdep_assert_held(&kvm->slots_lock);
27 
28  return !kvm->dirty_ring_size || kvm->dirty_ring_with_bitmap;
29 }
Here is the caller graph for this function: