bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
 }
 
+/**
+ * ap_owned_by_def_drv: indicates whether an AP adapter is reserved for the
+ *                     default host driver or not.
+ * @card: the APID of the adapter card to check
+ * @queue: the APQI of the queue to check
+ *
+ * Note: the ap_perms_mutex must be locked by the caller of this function.
+ *
+ * Return: an int specifying whether the AP adapter is reserved for the host (1)
+ *        or not (0).
+ */
 int ap_owned_by_def_drv(int card, int queue)
 {
        int rc = 0;
        if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
                return -EINVAL;
 
-       mutex_lock(&ap_perms_mutex);
-
        if (test_bit_inv(card, ap_perms.apm) &&
            test_bit_inv(queue, ap_perms.aqm))
                rc = 1;
 
-       mutex_unlock(&ap_perms_mutex);
-
        return rc;
 }
 EXPORT_SYMBOL(ap_owned_by_def_drv);
 
+/**
+ * ap_apqn_in_matrix_owned_by_def_drv: indicates whether every APQN contained in
+ *                                    a set is reserved for the host drivers
+ *                                    or not.
+ * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check
+ * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check
+ *
+ * Note: the ap_perms_mutex must be locked by the caller of this function.
+ *
+ * Return: an int specifying whether each APQN is reserved for the host (1) or
+ *        not (0)
+ */
 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
                                       unsigned long *aqm)
 {
        int card, queue, rc = 0;
 
-       mutex_lock(&ap_perms_mutex);
-
        for (card = 0; !rc && card < AP_DEVICES; card++)
                if (test_bit_inv(card, apm) &&
                    test_bit_inv(card, ap_perms.apm))
                                    test_bit_inv(queue, ap_perms.aqm))
                                        rc = 1;
 
-       mutex_unlock(&ap_perms_mutex);
-
        return rc;
 }
 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
 
        return 0;
 }
 
+/**
+ * vfio_ap_mdev_validate_masks - verify that the APQNs assigned to the mdev are
+ *                              not reserved for the default zcrypt driver and
+ *                              are not assigned to another mdev.
+ *
+ * @matrix_mdev: the mdev to which the APQNs being validated are assigned.
+ *
+ * Return: One of the following values:
+ * o the error returned from the ap_apqn_in_matrix_owned_by_def_drv() function,
+ *   most likely -EBUSY indicating the ap_perms_mutex lock is already held.
+ * o EADDRNOTAVAIL if an APQN assigned to @matrix_mdev is reserved for the
+ *                zcrypt default driver.
+ * o EADDRINUSE if an APQN assigned to @matrix_mdev is assigned to another mdev
+ * o A zero indicating validation succeeded.
+ */
 static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev)
 {
        if (ap_apqn_in_matrix_owned_by_def_drv(matrix_mdev->matrix.apm,
  *        An APQN derived from the cross product of the APID being assigned
  *        and the APQIs previously assigned is being used by another mediated
  *        matrix device
+ *
+ *     5. -EAGAIN
+ *        A lock required to validate the mdev's AP configuration could not
+ *        be obtained.
  */
 static ssize_t assign_adapter_store(struct device *dev,
                                    struct device_attribute *attr,
        DECLARE_BITMAP(apm_delta, AP_DEVICES);
        struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
 
+       mutex_lock(&ap_perms_mutex);
        get_update_locks_for_mdev(matrix_mdev);
 
        ret = kstrtoul(buf, 0, &apid);
        ret = count;
 done:
        release_update_locks_for_mdev(matrix_mdev);
+       mutex_unlock(&ap_perms_mutex);
 
        return ret;
 }
  *        An APQN derived from the cross product of the APQI being assigned
  *        and the APIDs previously assigned is being used by another mediated
  *        matrix device
+ *
+ *     5. -EAGAIN
+ *        The lock required to validate the mdev's AP configuration could not
+ *        be obtained.
  */
 static ssize_t assign_domain_store(struct device *dev,
                                   struct device_attribute *attr,
        DECLARE_BITMAP(aqm_delta, AP_DOMAINS);
        struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
 
+       mutex_lock(&ap_perms_mutex);
        get_update_locks_for_mdev(matrix_mdev);
 
        ret = kstrtoul(buf, 0, &apqi);
        ret = count;
 done:
        release_update_locks_for_mdev(matrix_mdev);
+       mutex_unlock(&ap_perms_mutex);
 
        return ret;
 }
        kfree(q);
        release_update_locks_for_mdev(matrix_mdev);
 }
+
+/**
+ * vfio_ap_mdev_resource_in_use: check whether any of a set of APQNs is
+ *                              assigned to a mediated device under the control
+ *                              of the vfio_ap device driver.
+ *
+ * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check.
+ * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check.
+ *
+ * Return:
+ *     * -EADDRINUSE if one or more of the APQNs specified via @apm/@aqm are
+ *       assigned to a mediated device under the control of the vfio_ap
+ *       device driver.
+ *     * Otherwise, return 0.
+ */
+int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm)
+{
+       int ret;
+
+       mutex_lock(&matrix_dev->guests_lock);
+       mutex_lock(&matrix_dev->mdevs_lock);
+       ret = vfio_ap_mdev_verify_no_sharing(apm, aqm);
+       mutex_unlock(&matrix_dev->mdevs_lock);
+       mutex_unlock(&matrix_dev->guests_lock);
+
+       return ret;
+}