aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/kvm
diff options
context:
space:
mode:
authorJames Morris <jmorris@namei.org>2006-12-22 04:04:55 -0500
committerLinus Torvalds <torvalds@woody.osdl.org>2006-12-22 11:55:45 -0500
commit5aacf0ca4128fdeadf91c0ac50271841b643d1fd (patch)
treeb951292c844b53d8be8c315419a12c86463f6b7c /drivers/kvm
parent5f2a105d5e33a038a717995d2738434f9c25aed2 (diff)
[PATCH] KVM: add valid_vcpu() helper
Consolidate the logic for checking whether a vcpu index is valid. Also, use likely(), as a valid value should be the overwhelmingly common case. Signed-off-by: James Morris <jmorris@namei.org> Signed-off-by: Avi Kivity <avi@qumranet.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'drivers/kvm')
-rw-r--r--drivers/kvm/kvm_main.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/drivers/kvm/kvm_main.c b/drivers/kvm/kvm_main.c
index fd1bb870545c..4621b4ee6744 100644
--- a/drivers/kvm/kvm_main.c
+++ b/drivers/kvm/kvm_main.c
@@ -113,6 +113,11 @@ unsigned long segment_base(u16 selector)
113} 113}
114EXPORT_SYMBOL_GPL(segment_base); 114EXPORT_SYMBOL_GPL(segment_base);
115 115
116static inline int valid_vcpu(int n)
117{
118 return likely(n >= 0 && n < KVM_MAX_VCPUS);
119}
120
116int kvm_read_guest(struct kvm_vcpu *vcpu, 121int kvm_read_guest(struct kvm_vcpu *vcpu,
117 gva_t addr, 122 gva_t addr,
118 unsigned long size, 123 unsigned long size,
@@ -494,7 +499,7 @@ static int kvm_dev_ioctl_create_vcpu(struct kvm *kvm, int n)
494 struct kvm_vcpu *vcpu; 499 struct kvm_vcpu *vcpu;
495 500
496 r = -EINVAL; 501 r = -EINVAL;
497 if (n < 0 || n >= KVM_MAX_VCPUS) 502 if (!valid_vcpu(n))
498 goto out; 503 goto out;
499 504
500 vcpu = &kvm->vcpus[n]; 505 vcpu = &kvm->vcpus[n];
@@ -1179,7 +1184,7 @@ static int kvm_dev_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run)
1179 struct kvm_vcpu *vcpu; 1184 struct kvm_vcpu *vcpu;
1180 int r; 1185 int r;
1181 1186
1182 if (kvm_run->vcpu < 0 || kvm_run->vcpu >= KVM_MAX_VCPUS) 1187 if (!valid_vcpu(kvm_run->vcpu))
1183 return -EINVAL; 1188 return -EINVAL;
1184 1189
1185 vcpu = vcpu_load(kvm, kvm_run->vcpu); 1190 vcpu = vcpu_load(kvm, kvm_run->vcpu);
@@ -1208,7 +1213,7 @@ static int kvm_dev_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs)
1208{ 1213{
1209 struct kvm_vcpu *vcpu; 1214 struct kvm_vcpu *vcpu;
1210 1215
1211 if (regs->vcpu < 0 || regs->vcpu >= KVM_MAX_VCPUS) 1216 if (!valid_vcpu(regs->vcpu))
1212 return -EINVAL; 1217 return -EINVAL;
1213 1218
1214 vcpu = vcpu_load(kvm, regs->vcpu); 1219 vcpu = vcpu_load(kvm, regs->vcpu);
@@ -1254,7 +1259,7 @@ static int kvm_dev_ioctl_set_regs(struct kvm *kvm, struct kvm_regs *regs)
1254{ 1259{
1255 struct kvm_vcpu *vcpu; 1260 struct kvm_vcpu *vcpu;
1256 1261
1257 if (regs->vcpu < 0 || regs->vcpu >= KVM_MAX_VCPUS) 1262 if (!valid_vcpu(regs->vcpu))
1258 return -EINVAL; 1263 return -EINVAL;
1259 1264
1260 vcpu = vcpu_load(kvm, regs->vcpu); 1265 vcpu = vcpu_load(kvm, regs->vcpu);
@@ -1301,7 +1306,7 @@ static int kvm_dev_ioctl_get_sregs(struct kvm *kvm, struct kvm_sregs *sregs)
1301 struct kvm_vcpu *vcpu; 1306 struct kvm_vcpu *vcpu;
1302 struct descriptor_table dt; 1307 struct descriptor_table dt;
1303 1308
1304 if (sregs->vcpu < 0 || sregs->vcpu >= KVM_MAX_VCPUS) 1309 if (!valid_vcpu(sregs->vcpu))
1305 return -EINVAL; 1310 return -EINVAL;
1306 vcpu = vcpu_load(kvm, sregs->vcpu); 1311 vcpu = vcpu_load(kvm, sregs->vcpu);
1307 if (!vcpu) 1312 if (!vcpu)
@@ -1353,7 +1358,7 @@ static int kvm_dev_ioctl_set_sregs(struct kvm *kvm, struct kvm_sregs *sregs)
1353 int i; 1358 int i;
1354 struct descriptor_table dt; 1359 struct descriptor_table dt;
1355 1360
1356 if (sregs->vcpu < 0 || sregs->vcpu >= KVM_MAX_VCPUS) 1361 if (!valid_vcpu(sregs->vcpu))
1357 return -EINVAL; 1362 return -EINVAL;
1358 vcpu = vcpu_load(kvm, sregs->vcpu); 1363 vcpu = vcpu_load(kvm, sregs->vcpu);
1359 if (!vcpu) 1364 if (!vcpu)
@@ -1444,7 +1449,7 @@ static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs,
1444 struct kvm_vcpu *vcpu; 1449 struct kvm_vcpu *vcpu;
1445 int i; 1450 int i;
1446 1451
1447 if (msrs->vcpu < 0 || msrs->vcpu >= KVM_MAX_VCPUS) 1452 if (!valid_vcpu(msrs->vcpu))
1448 return -EINVAL; 1453 return -EINVAL;
1449 1454
1450 vcpu = vcpu_load(kvm, msrs->vcpu); 1455 vcpu = vcpu_load(kvm, msrs->vcpu);
@@ -1537,7 +1542,7 @@ static int kvm_dev_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq)
1537{ 1542{
1538 struct kvm_vcpu *vcpu; 1543 struct kvm_vcpu *vcpu;
1539 1544
1540 if (irq->vcpu < 0 || irq->vcpu >= KVM_MAX_VCPUS) 1545 if (!valid_vcpu(irq->vcpu))
1541 return -EINVAL; 1546 return -EINVAL;
1542 if (irq->irq < 0 || irq->irq >= 256) 1547 if (irq->irq < 0 || irq->irq >= 256)
1543 return -EINVAL; 1548 return -EINVAL;
@@ -1559,7 +1564,7 @@ static int kvm_dev_ioctl_debug_guest(struct kvm *kvm,
1559 struct kvm_vcpu *vcpu; 1564 struct kvm_vcpu *vcpu;
1560 int r; 1565 int r;
1561 1566
1562 if (dbg->vcpu < 0 || dbg->vcpu >= KVM_MAX_VCPUS) 1567 if (!valid_vcpu(dbg->vcpu))
1563 return -EINVAL; 1568 return -EINVAL;
1564 vcpu = vcpu_load(kvm, dbg->vcpu); 1569 vcpu = vcpu_load(kvm, dbg->vcpu);
1565 if (!vcpu) 1570 if (!vcpu)