Skip to content

Commit ec10b72

Browse files
committed
Merge tag 'kvm-s390-20140715' of git://git.kernel.org/pub/scm/linux/kernel/git/kvms390/linux into kvm-next
This series enables the "KVM_(S|G)ET_MP_STATE" ioctls on s390 to make the cpu state settable by user space. This is necessary to avoid races in s390 SIGP/reset handling which happen because some SIGPs are handled in QEMU, while others are handled in the kernel. Together with the busy conditions as return value of SIGP races happen especially in areas like starting and stopping of CPUs. (For example, there is a program 'cpuplugd', that runs on several s390 distros which does automatic onlining and offlining on cpus.) As soon as the MPSTATE interface is used, user space takes complete control of the cpu states. Otherwise the kernel will use the old way. Therefore, the new kernel continues to work fine with old QEMUs.
2 parents 963fee1 + 6352e4d commit ec10b72

8 files changed

Lines changed: 98 additions & 45 deletions

File tree

‎Documentation/virtual/kvm/api.txt‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ for vm-wide capabilities.
988988
4.38 KVM_GET_MP_STATE
989989

990990
Capability: KVM_CAP_MP_STATE
991-
Architectures: x86, ia64
991+
Architectures: x86, ia64, s390
992992
Type: vcpu ioctl
993993
Parameters: struct kvm_mp_state (out)
994994
Returns: 0 on success; -1 on error
@@ -1002,33 +1002,42 @@ uniprocessor guests).
10021002

10031003
Possible values are:
10041004

1005-
- KVM_MP_STATE_RUNNABLE: the vcpu is currently running
1005+
- KVM_MP_STATE_RUNNABLE: the vcpu is currently running [x86, ia64]
10061006
- KVM_MP_STATE_UNINITIALIZED: the vcpu is an application processor (AP)
1007-
which has not yet received an INIT signal
1007+
which has not yet received an INIT signal [x86,
1008+
ia64]
10081009
- KVM_MP_STATE_INIT_RECEIVED: the vcpu has received an INIT signal, and is
1009-
now ready for a SIPI
1010+
now ready for a SIPI [x86, ia64]
10101011
- KVM_MP_STATE_HALTED: the vcpu has executed a HLT instruction and
1011-
is waiting for an interrupt
1012+
is waiting for an interrupt [x86, ia64]
10121013
- KVM_MP_STATE_SIPI_RECEIVED: the vcpu has just received a SIPI (vector
1013-
accessible via KVM_GET_VCPU_EVENTS)
1014+
accessible via KVM_GET_VCPU_EVENTS) [x86, ia64]
1015+
- KVM_MP_STATE_STOPPED: the vcpu is stopped [s390]
1016+
- KVM_MP_STATE_CHECK_STOP: the vcpu is in a special error state [s390]
1017+
- KVM_MP_STATE_OPERATING: the vcpu is operating (running or halted)
1018+
[s390]
1019+
- KVM_MP_STATE_LOAD: the vcpu is in a special load/startup state
1020+
[s390]
10141021

1015-
This ioctl is only useful after KVM_CREATE_IRQCHIP. Without an in-kernel
1016-
irqchip, the multiprocessing state must be maintained by userspace.
1022+
On x86 and ia64, this ioctl is only useful after KVM_CREATE_IRQCHIP. Without an
1023+
in-kernel irqchip, the multiprocessing state must be maintained by userspace on
1024+
these architectures.
10171025

10181026

10191027
4.39 KVM_SET_MP_STATE
10201028

10211029
Capability: KVM_CAP_MP_STATE
1022-
Architectures: x86, ia64
1030+
Architectures: x86, ia64, s390
10231031
Type: vcpu ioctl
10241032
Parameters: struct kvm_mp_state (in)
10251033
Returns: 0 on success; -1 on error
10261034

10271035
Sets the vcpu's current "multiprocessing state"; see KVM_GET_MP_STATE for
10281036
arguments.
10291037

1030-
This ioctl is only useful after KVM_CREATE_IRQCHIP. Without an in-kernel
1031-
irqchip, the multiprocessing state must be maintained by userspace.
1038+
On x86 and ia64, this ioctl is only useful after KVM_CREATE_IRQCHIP. Without an
1039+
in-kernel irqchip, the multiprocessing state must be maintained by userspace on
1040+
these architectures.
10321041

10331042

10341043
4.40 KVM_SET_IDENTITY_MAP_ADDR

‎arch/s390/include/asm/kvm_host.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ struct kvm_arch{
418418
int css_support;
419419
int use_irqchip;
420420
int use_cmma;
421+
int user_cpu_state_ctrl;
421422
struct s390_io_adapter *adapters[MAX_S390_IO_ADAPTERS];
422423
wait_queue_head_t ipte_wq;
423424
spinlock_t start_stop_lock;

‎arch/s390/kvm/diag.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
176176
return -EOPNOTSUPP;
177177
}
178178

179-
kvm_s390_vcpu_stop(vcpu);
179+
if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
180+
kvm_s390_vcpu_stop(vcpu);
180181
vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
181182
vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
182183
vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;

‎arch/s390/kvm/intercept.c‎

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,26 @@ static int handle_noop(struct kvm_vcpu *vcpu)
5656
static int handle_stop(struct kvm_vcpu *vcpu)
5757
{
5858
int rc = 0;
59+
unsigned int action_bits;
5960

6061
vcpu->stat.exit_stop_request++;
61-
spin_lock_bh(&vcpu->arch.local_int.lock);
62-
6362
trace_kvm_s390_stop_request(vcpu->arch.local_int.action_bits);
6463

65-
if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
66-
kvm_s390_vcpu_stop(vcpu);
67-
vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
68-
VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
69-
rc = -EOPNOTSUPP;
70-
}
64+
action_bits = vcpu->arch.local_int.action_bits;
7165

72-
if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
73-
vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
74-
/* store status must be called unlocked. Since local_int.lock
75-
* only protects local_int.* and not guest memory we can give
76-
* up the lock here */
77-
spin_unlock_bh(&vcpu->arch.local_int.lock);
66+
if (!(action_bits & ACTION_STOP_ON_STOP))
67+
return 0;
68+
69+
if (action_bits & ACTION_STORE_ON_STOP) {
7870
rc = kvm_s390_vcpu_store_status(vcpu,
7971
KVM_S390_STORE_STATUS_NOADDR);
80-
if (rc >= 0)
81-
rc = -EOPNOTSUPP;
82-
} else
83-
spin_unlock_bh(&vcpu->arch.local_int.lock);
84-
return rc;
72+
if (rc)
73+
return rc;
74+
}
75+
76+
if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
77+
kvm_s390_vcpu_stop(vcpu);
78+
return -EOPNOTSUPP;
8579
}
8680

8781
static int handle_validity(struct kvm_vcpu *vcpu)

‎arch/s390/kvm/kvm-s390.c‎

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ int kvm_dev_ioctl_check_extension(long ext)
167167
case KVM_CAP_DEVICE_CTRL:
168168
case KVM_CAP_ENABLE_CAP_VM:
169169
case KVM_CAP_VM_ATTRIBUTES:
170+
case KVM_CAP_MP_STATE:
170171
r = 1;
171172
break;
172173
case KVM_CAP_NR_VCPUS:
@@ -595,7 +596,8 @@ static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
595596
vcpu->arch.sie_block->pp = 0;
596597
vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
597598
kvm_clear_async_pf_completion_queue(vcpu);
598-
kvm_s390_vcpu_stop(vcpu);
599+
if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
600+
kvm_s390_vcpu_stop(vcpu);
599601
kvm_s390_clear_local_irqs(vcpu);
600602
}
601603

@@ -926,7 +928,7 @@ static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
926928
{
927929
int rc = 0;
928930

929-
if (!(atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_STOPPED))
931+
if (!is_vcpu_stopped(vcpu))
930932
rc = -EBUSY;
931933
else {
932934
vcpu->run->psw_mask = psw.mask;
@@ -980,13 +982,34 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
980982
int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
981983
struct kvm_mp_state *mp_state)
982984
{
983-
return -EINVAL; /* not implemented yet */
985+
/* CHECK_STOP and LOAD are not supported yet */
986+
return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
987+
KVM_MP_STATE_OPERATING;
984988
}
985989

986990
int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
987991
struct kvm_mp_state *mp_state)
988992
{
989-
return -EINVAL; /* not implemented yet */
993+
int rc = 0;
994+
995+
/* user space knows about this interface - let it control the state */
996+
vcpu->kvm->arch.user_cpu_state_ctrl = 1;
997+
998+
switch (mp_state->mp_state) {
999+
case KVM_MP_STATE_STOPPED:
1000+
kvm_s390_vcpu_stop(vcpu);
1001+
break;
1002+
case KVM_MP_STATE_OPERATING:
1003+
kvm_s390_vcpu_start(vcpu);
1004+
break;
1005+
case KVM_MP_STATE_LOAD:
1006+
case KVM_MP_STATE_CHECK_STOP:
1007+
/* fall through - CHECK_STOP and LOAD are not supported yet */
1008+
default:
1009+
rc = -ENXIO;
1010+
}
1011+
1012+
return rc;
9901013
}
9911014

9921015
bool kvm_s390_cmma_enabled(struct kvm *kvm)
@@ -1284,7 +1307,13 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
12841307
if (vcpu->sigset_active)
12851308
sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
12861309

1287-
kvm_s390_vcpu_start(vcpu);
1310+
if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
1311+
kvm_s390_vcpu_start(vcpu);
1312+
} else if (is_vcpu_stopped(vcpu)) {
1313+
pr_err_ratelimited("kvm-s390: can't run stopped vcpu %d\n",
1314+
vcpu->vcpu_id);
1315+
return -EINVAL;
1316+
}
12881317

12891318
switch (kvm_run->exit_reason) {
12901319
case KVM_EXIT_S390_SIEIC:
@@ -1413,11 +1442,6 @@ int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
14131442
return kvm_s390_store_status_unloaded(vcpu, addr);
14141443
}
14151444

1416-
static inline int is_vcpu_stopped(struct kvm_vcpu *vcpu)
1417-
{
1418-
return atomic_read(&(vcpu)->arch.sie_block->cpuflags) & CPUSTAT_STOPPED;
1419-
}
1420-
14211445
static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
14221446
{
14231447
kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
@@ -1494,7 +1518,15 @@ void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
14941518
spin_lock_bh(&vcpu->kvm->arch.start_stop_lock);
14951519
online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
14961520

1521+
/* Need to lock access to action_bits to avoid a SIGP race condition */
1522+
spin_lock_bh(&vcpu->arch.local_int.lock);
14971523
atomic_set_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
1524+
1525+
/* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
1526+
vcpu->arch.local_int.action_bits &=
1527+
~(ACTION_STOP_ON_STOP | ACTION_STORE_ON_STOP);
1528+
spin_unlock_bh(&vcpu->arch.local_int.lock);
1529+
14981530
__disable_ibs_on_vcpu(vcpu);
14991531

15001532
for (i = 0; i < online_vcpus; i++) {

‎arch/s390/kvm/kvm-s390.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ do { \
4545
d_args); \
4646
} while (0)
4747

48-
static inline int __cpu_is_stopped(struct kvm_vcpu *vcpu)
48+
static inline int is_vcpu_stopped(struct kvm_vcpu *vcpu)
4949
{
50-
return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_STOP_INT;
50+
return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_STOPPED;
5151
}
5252

5353
static inline int kvm_is_ucontrol(struct kvm *kvm)
@@ -129,6 +129,12 @@ static inline void kvm_s390_set_psw_cc(struct kvm_vcpu *vcpu, unsigned long cc)
129129
vcpu->arch.sie_block->gpsw.mask |= cc << 44;
130130
}
131131

132+
/* are cpu states controlled by user space */
133+
static inline int kvm_s390_user_cpu_state_ctrl(struct kvm *kvm)
134+
{
135+
return kvm->arch.user_cpu_state_ctrl != 0;
136+
}
137+
132138
int kvm_s390_handle_wait(struct kvm_vcpu *vcpu);
133139
enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer);
134140
void kvm_s390_tasklet(unsigned long parm);

‎arch/s390/kvm/sigp.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ static int __inject_sigp_stop(struct kvm_s390_local_interrupt *li, int action)
136136
inti->type = KVM_S390_SIGP_STOP;
137137

138138
spin_lock_bh(&li->lock);
139+
if (li->action_bits & ACTION_STOP_ON_STOP) {
140+
/* another SIGP STOP is pending */
141+
rc = SIGP_CC_BUSY;
142+
goto out;
143+
}
139144
if ((atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) {
140145
kfree(inti);
141146
if ((action & ACTION_STORE_ON_STOP) != 0)
@@ -144,8 +149,8 @@ static int __inject_sigp_stop(struct kvm_s390_local_interrupt *li, int action)
144149
}
145150
list_add_tail(&inti->list, &li->list);
146151
atomic_set(&li->active, 1);
147-
atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
148152
li->action_bits |= action;
153+
atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
149154
if (waitqueue_active(li->wq))
150155
wake_up_interruptible(li->wq);
151156
out:

‎include/uapi/linux/kvm.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,18 @@ struct kvm_vapic_addr {
399399
__u64 vapic_addr;
400400
};
401401

402-
/* for KVM_SET_MPSTATE */
402+
/* for KVM_SET_MP_STATE */
403403

404+
/* not all states are valid on all architectures */
404405
#define KVM_MP_STATE_RUNNABLE 0
405406
#define KVM_MP_STATE_UNINITIALIZED 1
406407
#define KVM_MP_STATE_INIT_RECEIVED 2
407408
#define KVM_MP_STATE_HALTED 3
408409
#define KVM_MP_STATE_SIPI_RECEIVED 4
410+
#define KVM_MP_STATE_STOPPED 5
411+
#define KVM_MP_STATE_CHECK_STOP 6
412+
#define KVM_MP_STATE_OPERATING 7
413+
#define KVM_MP_STATE_LOAD 8
409414

410415
struct kvm_mp_state {
411416
__u32 mp_state;

0 commit comments

Comments
 (0)