Skip to content

Commit d33d729

Browse files
Anthony KrowiakVasily Gorbik
authored andcommitted
s390/vfio-ap: Fix no AP queue sharing allowed message written to kernel log
An erroneous message is written to the kernel log when either of the following actions are taken by a user: 1. Assign an adapter or domain to a vfio_ap mediated device via its sysfs assign_adapter or assign_domain attributes that would result in one or more AP queues being assigned that are already assigned to a different mediated device. Sharing of queues between mdevs is not allowed. 2. Reserve an adapter or domain for the host device driver via the AP bus driver's sysfs apmask or aqmask attribute that would result in providing host access to an AP queue that is in use by a vfio_ap mediated device. Reserving a queue for a host driver that is in use by an mdev is not allowed. In both cases, the assignment will return an error; however, a message like the following is written to the kernel log: vfio_ap_mdev e1839397-51a0-4e3c-91e0-c3b9c3d3047d: Userspace may not re-assign queue 00.0028 already assigned to \ e1839397-51a0-4e3c-91e0-c3b9c3d3047d Notice the mdev reporting the error is the same as the mdev identified in the message as the one to which the queue is being assigned. It is perfectly okay to assign a queue to an mdev to which it is already assigned; the assignment is simply ignored by the vfio_ap device driver. This patch logs more descriptive and accurate messages for both 1 and 2 above to the kernel log: Example for 1: vfio_ap_mdev 0fe903a0-a323-44db-9daf-134c68627d61: Userspace may not assign queue 00.0033 to mdev: already assigned to \ 62177883-f1bb-47f0-914d-32a22e3a8804 Example for 2: vfio_ap_mdev 62177883-f1bb-47f0-914d-32a22e3a8804: Can not reserve queue 00.0033 for host driver: in use by mdev Signed-off-by: Anthony Krowiak <akrowiak@linux.ibm.com> Link: https://lore.kernel.org/r/20250311103304.1539188-1-akrowiak@linux.ibm.com Signed-off-by: Heiko Carstens <hca@linux.ibm.com> Signed-off-by: Vasily Gorbik <gor@linux.ibm.com>
1 parent 807c274 commit d33d729

File tree

1 file changed

+46
-26
lines changed

1 file changed

+46
-26
lines changed

drivers/s390/crypto/vfio_ap_ops.c

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -873,48 +873,66 @@ static void vfio_ap_mdev_remove(struct mdev_device *mdev)
873873
vfio_put_device(&matrix_mdev->vdev);
874874
}
875875

876-
#define MDEV_SHARING_ERR "Userspace may not re-assign queue %02lx.%04lx " \
877-
"already assigned to %s"
876+
#define MDEV_SHARING_ERR "Userspace may not assign queue %02lx.%04lx to mdev: already assigned to %s"
878877

879-
static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *matrix_mdev,
880-
unsigned long *apm,
881-
unsigned long *aqm)
878+
#define MDEV_IN_USE_ERR "Can not reserve queue %02lx.%04lx for host driver: in use by mdev"
879+
880+
static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *assignee,
881+
struct ap_matrix_mdev *assigned_to,
882+
unsigned long *apm, unsigned long *aqm)
882883
{
883884
unsigned long apid, apqi;
884-
const struct device *dev = mdev_dev(matrix_mdev->mdev);
885-
const char *mdev_name = dev_name(dev);
886885

887-
for_each_set_bit_inv(apid, apm, AP_DEVICES)
886+
for_each_set_bit_inv(apid, apm, AP_DEVICES) {
887+
for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
888+
dev_warn(mdev_dev(assignee->mdev), MDEV_SHARING_ERR,
889+
apid, apqi, dev_name(mdev_dev(assigned_to->mdev)));
890+
}
891+
}
892+
}
893+
894+
static void vfio_ap_mdev_log_in_use_err(struct ap_matrix_mdev *assignee,
895+
unsigned long *apm, unsigned long *aqm)
896+
{
897+
unsigned long apid, apqi;
898+
899+
for_each_set_bit_inv(apid, apm, AP_DEVICES) {
888900
for_each_set_bit_inv(apqi, aqm, AP_DOMAINS)
889-
dev_warn(dev, MDEV_SHARING_ERR, apid, apqi, mdev_name);
901+
dev_warn(mdev_dev(assignee->mdev), MDEV_IN_USE_ERR, apid, apqi);
902+
}
890903
}
891904

892905
/**
893906
* vfio_ap_mdev_verify_no_sharing - verify APQNs are not shared by matrix mdevs
894907
*
908+
* @assignee: the matrix mdev to which @mdev_apm and @mdev_aqm are being
909+
* assigned; or, NULL if this function was called by the AP bus
910+
* driver in_use callback to verify none of the APQNs being reserved
911+
* for the host device driver are in use by a vfio_ap mediated device
895912
* @mdev_apm: mask indicating the APIDs of the APQNs to be verified
896913
* @mdev_aqm: mask indicating the APQIs of the APQNs to be verified
897914
*
898-
* Verifies that each APQN derived from the Cartesian product of a bitmap of
899-
* AP adapter IDs and AP queue indexes is not configured for any matrix
900-
* mediated device. AP queue sharing is not allowed.
915+
* Verifies that each APQN derived from the Cartesian product of APIDs
916+
* represented by the bits set in @mdev_apm and the APQIs of the bits set in
917+
* @mdev_aqm is not assigned to a mediated device other than the mdev to which
918+
* the APQN is being assigned (@assignee). AP queue sharing is not allowed.
901919
*
902920
* Return: 0 if the APQNs are not shared; otherwise return -EADDRINUSE.
903921
*/
904-
static int vfio_ap_mdev_verify_no_sharing(unsigned long *mdev_apm,
922+
static int vfio_ap_mdev_verify_no_sharing(struct ap_matrix_mdev *assignee,
923+
unsigned long *mdev_apm,
905924
unsigned long *mdev_aqm)
906925
{
907-
struct ap_matrix_mdev *matrix_mdev;
926+
struct ap_matrix_mdev *assigned_to;
908927
DECLARE_BITMAP(apm, AP_DEVICES);
909928
DECLARE_BITMAP(aqm, AP_DOMAINS);
910929

911-
list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
930+
list_for_each_entry(assigned_to, &matrix_dev->mdev_list, node) {
912931
/*
913-
* If the input apm and aqm are fields of the matrix_mdev
914-
* object, then move on to the next matrix_mdev.
932+
* If the mdev to which the mdev_apm and mdev_aqm is being
933+
* assigned is the same as the mdev being verified
915934
*/
916-
if (mdev_apm == matrix_mdev->matrix.apm &&
917-
mdev_aqm == matrix_mdev->matrix.aqm)
935+
if (assignee == assigned_to)
918936
continue;
919937

920938
memset(apm, 0, sizeof(apm));
@@ -924,15 +942,16 @@ static int vfio_ap_mdev_verify_no_sharing(unsigned long *mdev_apm,
924942
* We work on full longs, as we can only exclude the leftover
925943
* bits in non-inverse order. The leftover is all zeros.
926944
*/
927-
if (!bitmap_and(apm, mdev_apm, matrix_mdev->matrix.apm,
928-
AP_DEVICES))
945+
if (!bitmap_and(apm, mdev_apm, assigned_to->matrix.apm, AP_DEVICES))
929946
continue;
930947

931-
if (!bitmap_and(aqm, mdev_aqm, matrix_mdev->matrix.aqm,
932-
AP_DOMAINS))
948+
if (!bitmap_and(aqm, mdev_aqm, assigned_to->matrix.aqm, AP_DOMAINS))
933949
continue;
934950

935-
vfio_ap_mdev_log_sharing_err(matrix_mdev, apm, aqm);
951+
if (assignee)
952+
vfio_ap_mdev_log_sharing_err(assignee, assigned_to, apm, aqm);
953+
else
954+
vfio_ap_mdev_log_in_use_err(assigned_to, apm, aqm);
936955

937956
return -EADDRINUSE;
938957
}
@@ -961,7 +980,8 @@ static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev)
961980
matrix_mdev->matrix.aqm))
962981
return -EADDRNOTAVAIL;
963982

964-
return vfio_ap_mdev_verify_no_sharing(matrix_mdev->matrix.apm,
983+
return vfio_ap_mdev_verify_no_sharing(matrix_mdev,
984+
matrix_mdev->matrix.apm,
965985
matrix_mdev->matrix.aqm);
966986
}
967987

@@ -2516,7 +2536,7 @@ int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm)
25162536

25172537
mutex_lock(&matrix_dev->guests_lock);
25182538
mutex_lock(&matrix_dev->mdevs_lock);
2519-
ret = vfio_ap_mdev_verify_no_sharing(apm, aqm);
2539+
ret = vfio_ap_mdev_verify_no_sharing(NULL, apm, aqm);
25202540
mutex_unlock(&matrix_dev->mdevs_lock);
25212541
mutex_unlock(&matrix_dev->guests_lock);
25222542

0 commit comments

Comments
 (0)