Skip to content

Commit 6c12a63

Browse files
Tony KrowiakVasily Gorbik
authored andcommitted
s390/vfio-ap: No need to disable IRQ after queue reset
The queues assigned to a matrix mediated device are currently reset when: * The VFIO_DEVICE_RESET ioctl is invoked * The mdev fd is closed by userspace (QEMU) * The mdev is removed from sysfs. Immediately after the reset of a queue, a call is made to disable interrupts for the queue. This is entirely unnecessary because the reset of a queue disables interrupts, so this will be removed. Furthermore, vfio_ap_irq_disable() does an unconditional PQAP/AQIC which can result in a specification exception (when the corresponding facility is not available), so this is actually a bugfix. Signed-off-by: Tony Krowiak <[email protected]> [[email protected]: minor rework before merging] Signed-off-by: Halil Pasic <[email protected]> Fixes: ec89b55 ("s390: ap: implement PAPQ AQIC interception in kernel") Cc: <[email protected]> Signed-off-by: Vasily Gorbik <[email protected]>
1 parent f21916e commit 6c12a63

File tree

3 files changed

+69
-49
lines changed

3 files changed

+69
-49
lines changed

drivers/s390/crypto/vfio_ap_drv.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,11 @@ static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
7171
static void vfio_ap_queue_dev_remove(struct ap_device *apdev)
7272
{
7373
struct vfio_ap_queue *q;
74-
int apid, apqi;
7574

7675
mutex_lock(&matrix_dev->lock);
7776
q = dev_get_drvdata(&apdev->device);
77+
vfio_ap_mdev_reset_queue(q, 1);
7878
dev_set_drvdata(&apdev->device, NULL);
79-
apid = AP_QID_CARD(q->apqn);
80-
apqi = AP_QID_QUEUE(q->apqn);
81-
vfio_ap_mdev_reset_queue(apid, apqi, 1);
82-
vfio_ap_irq_disable(q);
8379
kfree(q);
8480
mutex_unlock(&matrix_dev->lock);
8581
}

drivers/s390/crypto/vfio_ap_ops.c

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"
2626

2727
static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev);
28+
static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
2829

2930
static int match_apqn(struct device *dev, const void *data)
3031
{
@@ -49,20 +50,15 @@ static struct vfio_ap_queue *vfio_ap_get_queue(
4950
int apqn)
5051
{
5152
struct vfio_ap_queue *q;
52-
struct device *dev;
5353

5454
if (!test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm))
5555
return NULL;
5656
if (!test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm))
5757
return NULL;
5858

59-
dev = driver_find_device(&matrix_dev->vfio_ap_drv->driver, NULL,
60-
&apqn, match_apqn);
61-
if (!dev)
62-
return NULL;
63-
q = dev_get_drvdata(dev);
64-
q->matrix_mdev = matrix_mdev;
65-
put_device(dev);
59+
q = vfio_ap_find_queue(apqn);
60+
if (q)
61+
q->matrix_mdev = matrix_mdev;
6662

6763
return q;
6864
}
@@ -119,13 +115,18 @@ static void vfio_ap_wait_for_irqclear(int apqn)
119115
*/
120116
static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
121117
{
122-
if (q->saved_isc != VFIO_AP_ISC_INVALID && q->matrix_mdev)
118+
if (!q)
119+
return;
120+
if (q->saved_isc != VFIO_AP_ISC_INVALID &&
121+
!WARN_ON(!(q->matrix_mdev && q->matrix_mdev->kvm))) {
123122
kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
124-
if (q->saved_pfn && q->matrix_mdev)
123+
q->saved_isc = VFIO_AP_ISC_INVALID;
124+
}
125+
if (q->saved_pfn && !WARN_ON(!q->matrix_mdev)) {
125126
vfio_unpin_pages(mdev_dev(q->matrix_mdev->mdev),
126127
&q->saved_pfn, 1);
127-
q->saved_pfn = 0;
128-
q->saved_isc = VFIO_AP_ISC_INVALID;
128+
q->saved_pfn = 0;
129+
}
129130
}
130131

131132
/**
@@ -144,7 +145,7 @@ static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
144145
* Returns if ap_aqic function failed with invalid, deconfigured or
145146
* checkstopped AP.
146147
*/
147-
struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
148+
static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
148149
{
149150
struct ap_qirq_ctrl aqic_gisa = {};
150151
struct ap_queue_status status;
@@ -1126,70 +1127,93 @@ static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
11261127
return notify_rc;
11271128
}
11281129

1129-
static void vfio_ap_irq_disable_apqn(int apqn)
1130+
static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
11301131
{
11311132
struct device *dev;
1132-
struct vfio_ap_queue *q;
1133+
struct vfio_ap_queue *q = NULL;
11331134

11341135
dev = driver_find_device(&matrix_dev->vfio_ap_drv->driver, NULL,
11351136
&apqn, match_apqn);
11361137
if (dev) {
11371138
q = dev_get_drvdata(dev);
1138-
vfio_ap_irq_disable(q);
11391139
put_device(dev);
11401140
}
1141+
1142+
return q;
11411143
}
11421144

1143-
int vfio_ap_mdev_reset_queue(unsigned int apid, unsigned int apqi,
1145+
int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q,
11441146
unsigned int retry)
11451147
{
11461148
struct ap_queue_status status;
1149+
int ret;
11471150
int retry2 = 2;
1148-
int apqn = AP_MKQID(apid, apqi);
11491151

1150-
do {
1151-
status = ap_zapq(apqn);
1152-
switch (status.response_code) {
1153-
case AP_RESPONSE_NORMAL:
1154-
while (!status.queue_empty && retry2--) {
1155-
msleep(20);
1156-
status = ap_tapq(apqn, NULL);
1157-
}
1158-
WARN_ON_ONCE(retry2 <= 0);
1159-
return 0;
1160-
case AP_RESPONSE_RESET_IN_PROGRESS:
1161-
case AP_RESPONSE_BUSY:
1152+
if (!q)
1153+
return 0;
1154+
1155+
retry_zapq:
1156+
status = ap_zapq(q->apqn);
1157+
switch (status.response_code) {
1158+
case AP_RESPONSE_NORMAL:
1159+
ret = 0;
1160+
break;
1161+
case AP_RESPONSE_RESET_IN_PROGRESS:
1162+
if (retry--) {
11621163
msleep(20);
1163-
break;
1164-
default:
1165-
/* things are really broken, give up */
1166-
return -EIO;
1164+
goto retry_zapq;
11671165
}
1168-
} while (retry--);
1166+
ret = -EBUSY;
1167+
break;
1168+
case AP_RESPONSE_Q_NOT_AVAIL:
1169+
case AP_RESPONSE_DECONFIGURED:
1170+
case AP_RESPONSE_CHECKSTOPPED:
1171+
WARN_ON_ONCE(status.irq_enabled);
1172+
ret = -EBUSY;
1173+
goto free_resources;
1174+
default:
1175+
/* things are really broken, give up */
1176+
WARN(true, "PQAP/ZAPQ completed with invalid rc (%x)\n",
1177+
status.response_code);
1178+
return -EIO;
1179+
}
1180+
1181+
/* wait for the reset to take effect */
1182+
while (retry2--) {
1183+
if (status.queue_empty && !status.irq_enabled)
1184+
break;
1185+
msleep(20);
1186+
status = ap_tapq(q->apqn, NULL);
1187+
}
1188+
WARN_ON_ONCE(retry2 <= 0);
11691189

1170-
return -EBUSY;
1190+
free_resources:
1191+
vfio_ap_free_aqic_resources(q);
1192+
1193+
return ret;
11711194
}
11721195

11731196
static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev)
11741197
{
11751198
int ret;
11761199
int rc = 0;
11771200
unsigned long apid, apqi;
1201+
struct vfio_ap_queue *q;
11781202
struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
11791203

11801204
for_each_set_bit_inv(apid, matrix_mdev->matrix.apm,
11811205
matrix_mdev->matrix.apm_max + 1) {
11821206
for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
11831207
matrix_mdev->matrix.aqm_max + 1) {
1184-
ret = vfio_ap_mdev_reset_queue(apid, apqi, 1);
1208+
q = vfio_ap_find_queue(AP_MKQID(apid, apqi));
1209+
ret = vfio_ap_mdev_reset_queue(q, 1);
11851210
/*
11861211
* Regardless whether a queue turns out to be busy, or
11871212
* is not operational, we need to continue resetting
11881213
* the remaining queues.
11891214
*/
11901215
if (ret)
11911216
rc = ret;
1192-
vfio_ap_irq_disable_apqn(AP_MKQID(apid, apqi));
11931217
}
11941218
}
11951219

drivers/s390/crypto/vfio_ap_private.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,17 @@ struct ap_matrix_mdev {
8888
struct mdev_device *mdev;
8989
};
9090

91-
extern int vfio_ap_mdev_register(void);
92-
extern void vfio_ap_mdev_unregister(void);
93-
int vfio_ap_mdev_reset_queue(unsigned int apid, unsigned int apqi,
94-
unsigned int retry);
95-
9691
struct vfio_ap_queue {
9792
struct ap_matrix_mdev *matrix_mdev;
9893
unsigned long saved_pfn;
9994
int apqn;
10095
#define VFIO_AP_ISC_INVALID 0xff
10196
unsigned char saved_isc;
10297
};
103-
struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q);
98+
99+
int vfio_ap_mdev_register(void);
100+
void vfio_ap_mdev_unregister(void);
101+
int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q,
102+
unsigned int retry);
103+
104104
#endif /* _VFIO_AP_PRIVATE_H_ */

0 commit comments

Comments
 (0)