s390/vfio-ap: prepare for dynamic update of guest's APCB on assign/unassign

The functions backing the matrix mdev's sysfs attribute interfaces to
assign/unassign adapters, domains and control domains must take and
release the locks required to perform a dynamic update of a guest's APCB
in the proper order.

The proper order for taking the locks is:

matrix_dev->guests_lock => kvm->lock => matrix_dev->mdevs_lock

The proper order for releasing the locks is:

matrix_dev->mdevs_lock => kvm->lock => matrix_dev->guests_lock

Two new macros are introduced for this purpose: One to take the locks and
the other to release the locks. These macros will be used by the
assignment/unassignment functions to prepare for dynamic update of
the KVM guest's APCB.

Signed-off-by: Tony Krowiak <akrowiak@linux.ibm.com>
Signed-off-by: Alexander Gordeev <agordeev@linux.ibm.com>
This commit is contained in:
Tony Krowiak 2022-03-16 22:21:38 -04:00 committed by Alexander Gordeev
parent b84eb8e050
commit 8ee13ad993

View File

@ -77,6 +77,53 @@ static inline void release_update_locks_for_kvm(struct kvm *kvm)
mutex_unlock(&matrix_dev->guests_lock);
}
/**
* get_update_locks_for_mdev: Acquire the locks required to dynamically update a
* KVM guest's APCB in the proper order.
*
* @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
* configuration data to use to update a KVM guest's APCB.
*
* The proper locking order is:
* 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
* guest's APCB.
* 2. matrix_mdev->kvm->lock: required to update a guest's APCB
* 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
*
* Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
* lock will not be taken.
*/
static inline void get_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
{
mutex_lock(&matrix_dev->guests_lock);
if (matrix_mdev && matrix_mdev->kvm)
mutex_lock(&matrix_mdev->kvm->lock);
mutex_lock(&matrix_dev->mdevs_lock);
}
/**
* release_update_locks_for_mdev: Release the locks used to dynamically update a
* KVM guest's APCB in the proper order.
*
* @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
* configuration data to use to update a KVM guest's APCB.
*
* The proper unlocking order is:
* 1. matrix_dev->mdevs_lock
* 2. matrix_mdev->kvm->lock
* 3. matrix_dev->guests_lock
*
* Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
* lock will not be released.
*/
static inline void release_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
{
mutex_unlock(&matrix_dev->mdevs_lock);
if (matrix_mdev && matrix_mdev->kvm)
mutex_unlock(&matrix_mdev->kvm->lock);
mutex_unlock(&matrix_dev->guests_lock);
}
/**
* vfio_ap_mdev_get_queue - retrieve a queue with a specific APQN from a
* hash table of queues assigned to a matrix mdev
@ -832,7 +879,7 @@ static ssize_t assign_adapter_store(struct device *dev,
struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
mutex_lock(&matrix_dev->mdevs_lock);
get_update_locks_for_mdev(matrix_mdev);
/* If the KVM guest is running, disallow assignment of adapter */
if (matrix_mdev->kvm) {
@ -864,7 +911,7 @@ static ssize_t assign_adapter_store(struct device *dev,
matrix_mdev->matrix.aqm, matrix_mdev);
ret = count;
done:
mutex_unlock(&matrix_dev->mdevs_lock);
release_update_locks_for_mdev(matrix_mdev);
return ret;
}
@ -907,7 +954,7 @@ static ssize_t unassign_adapter_store(struct device *dev,
unsigned long apid;
struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
mutex_lock(&matrix_dev->mdevs_lock);
get_update_locks_for_mdev(matrix_mdev);
/* If the KVM guest is running, disallow unassignment of adapter */
if (matrix_mdev->kvm) {
@ -932,7 +979,7 @@ static ssize_t unassign_adapter_store(struct device *dev,
ret = count;
done:
mutex_unlock(&matrix_dev->mdevs_lock);
release_update_locks_for_mdev(matrix_mdev);
return ret;
}
static DEVICE_ATTR_WO(unassign_adapter);
@ -987,7 +1034,7 @@ static ssize_t assign_domain_store(struct device *dev,
struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
unsigned long max_apqi = matrix_mdev->matrix.aqm_max;
mutex_lock(&matrix_dev->mdevs_lock);
get_update_locks_for_mdev(matrix_mdev);
/* If the KVM guest is running, disallow assignment of domain */
if (matrix_mdev->kvm) {
@ -1018,7 +1065,7 @@ static ssize_t assign_domain_store(struct device *dev,
matrix_mdev);
ret = count;
done:
mutex_unlock(&matrix_dev->mdevs_lock);
release_update_locks_for_mdev(matrix_mdev);
return ret;
}
@ -1061,7 +1108,7 @@ static ssize_t unassign_domain_store(struct device *dev,
unsigned long apqi;
struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
mutex_lock(&matrix_dev->mdevs_lock);
get_update_locks_for_mdev(matrix_mdev);
/* If the KVM guest is running, disallow unassignment of domain */
if (matrix_mdev->kvm) {
@ -1087,7 +1134,7 @@ static ssize_t unassign_domain_store(struct device *dev,
ret = count;
done:
mutex_unlock(&matrix_dev->mdevs_lock);
release_update_locks_for_mdev(matrix_mdev);
return ret;
}
static DEVICE_ATTR_WO(unassign_domain);
@ -1114,7 +1161,7 @@ static ssize_t assign_control_domain_store(struct device *dev,
unsigned long id;
struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
mutex_lock(&matrix_dev->mdevs_lock);
get_update_locks_for_mdev(matrix_mdev);
/* If the KVM guest is running, disallow assignment of control domain */
if (matrix_mdev->kvm) {
@ -1140,7 +1187,7 @@ static ssize_t assign_control_domain_store(struct device *dev,
vfio_ap_mdev_filter_cdoms(matrix_mdev);
ret = count;
done:
mutex_unlock(&matrix_dev->mdevs_lock);
release_update_locks_for_mdev(matrix_mdev);
return ret;
}
static DEVICE_ATTR_WO(assign_control_domain);
@ -1168,7 +1215,7 @@ static ssize_t unassign_control_domain_store(struct device *dev,
struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
unsigned long max_domid = matrix_mdev->matrix.adm_max;
mutex_lock(&matrix_dev->mdevs_lock);
get_update_locks_for_mdev(matrix_mdev);
/* If a KVM guest is running, disallow unassignment of control domain */
if (matrix_mdev->kvm) {
@ -1191,7 +1238,7 @@ static ssize_t unassign_control_domain_store(struct device *dev,
ret = count;
done:
mutex_unlock(&matrix_dev->mdevs_lock);
release_update_locks_for_mdev(matrix_mdev);
return ret;
}
static DEVICE_ATTR_WO(unassign_control_domain);