goto done;
        }
 
+       if (test_bit_inv(apid, matrix_mdev->matrix.apm)) {
+               ret = count;
+               goto done;
+       }
+
        set_bit_inv(apid, matrix_mdev->matrix.apm);
 
        ret = vfio_ap_mdev_validate_masks(matrix_mdev);
                goto done;
        }
 
+       if (!test_bit_inv(apid, matrix_mdev->matrix.apm)) {
+               ret = count;
+               goto done;
+       }
+
        clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
        vfio_ap_mdev_hot_unplug_adapter(matrix_mdev, apid);
        ret = count;
                goto done;
        }
 
+       if (test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
+               ret = count;
+               goto done;
+       }
+
        set_bit_inv(apqi, matrix_mdev->matrix.aqm);
 
        ret = vfio_ap_mdev_validate_masks(matrix_mdev);
                goto done;
        }
 
+       if (!test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
+               ret = count;
+               goto done;
+       }
+
        clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
        vfio_ap_mdev_hot_unplug_domain(matrix_mdev, apqi);
        ret = count;
                goto done;
        }
 
+       if (test_bit_inv(id, matrix_mdev->matrix.adm)) {
+               ret = count;
+               goto done;
+       }
+
        /* Set the bit in the ADM (bitmask) corresponding to the AP control
         * domain number (id). The bits in the mask, from most significant to
         * least significant, correspond to IDs 0 up to the one less than the
                goto done;
        }
 
+       if (!test_bit_inv(domid, matrix_mdev->matrix.adm)) {
+               ret = count;
+               goto done;
+       }
+
        clear_bit_inv(domid, matrix_mdev->matrix.adm);
 
        if (test_bit_inv(domid, matrix_mdev->shadow_apcb.adm)) {