Skip to content

Commit c298125

Browse files
sashalevinavikivity
authored andcommitted
KVM: MMIO: Lock coalesced device when checking for available entry
Move the check whether there are available entries to within the spinlock. This allows working with larger amount of VCPUs and reduces premature exits when using a large number of VCPUs. Cc: Avi Kivity <avi@redhat.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Marcelo Tosatti <mtosatti@redhat.com> Cc: Pekka Enberg <penberg@kernel.org> Signed-off-by: Sasha Levin <levinsasha928@gmail.com> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
1 parent 22388a3 commit c298125

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

‎virt/kvm/coalesced_mmio.c‎

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,8 @@ static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
2525
gpa_t addr, int len)
2626
{
2727
struct kvm_coalesced_mmio_zone *zone;
28-
struct kvm_coalesced_mmio_ring *ring;
29-
unsigned avail;
3028
int i;
3129

32-
/* Are we able to batch it ? */
33-
34-
/* last is the first free entry
35-
* check if we don't meet the first used entry
36-
* there is always one unused entry in the buffer
37-
*/
38-
ring = dev->kvm->coalesced_mmio_ring;
39-
avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX;
40-
if (avail < KVM_MAX_VCPUS) {
41-
/* full */
42-
return 0;
43-
}
44-
4530
/* is it in a batchable area ? */
4631

4732
for (i = 0; i < dev->nb_zones; i++) {
@@ -58,16 +43,43 @@ static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
5843
return 0;
5944
}
6045

46+
static int coalesced_mmio_has_room(struct kvm_coalesced_mmio_dev *dev)
47+
{
48+
struct kvm_coalesced_mmio_ring *ring;
49+
unsigned avail;
50+
51+
/* Are we able to batch it ? */
52+
53+
/* last is the first free entry
54+
* check if we don't meet the first used entry
55+
* there is always one unused entry in the buffer
56+
*/
57+
ring = dev->kvm->coalesced_mmio_ring;
58+
avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX;
59+
if (avail == 0) {
60+
/* full */
61+
return 0;
62+
}
63+
64+
return 1;
65+
}
66+
6167
static int coalesced_mmio_write(struct kvm_io_device *this,
6268
gpa_t addr, int len, const void *val)
6369
{
6470
struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
6571
struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
72+
6673
if (!coalesced_mmio_in_range(dev, addr, len))
6774
return -EOPNOTSUPP;
6875

6976
spin_lock(&dev->lock);
7077

78+
if (!coalesced_mmio_has_room(dev)) {
79+
spin_unlock(&dev->lock);
80+
return -EOPNOTSUPP;
81+
}
82+
7183
/* copy data in first free entry of the ring */
7284

7385
ring->coalesced_mmio[ring->last].phys_addr = addr;

0 commit comments

Comments
 (0)