vfio-ap-locking.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ======================
  3. VFIO AP Locks Overview
  4. ======================
  5. This document describes the locks that are pertinent to the secure operation
  6. of the vfio_ap device driver. Throughout this document, the following variables
  7. will be used to denote instances of the structures herein described:
  8. .. code-block:: c
  9. struct ap_matrix_dev *matrix_dev;
  10. struct ap_matrix_mdev *matrix_mdev;
  11. struct kvm *kvm;
  12. The Matrix Devices Lock (drivers/s390/crypto/vfio_ap_private.h)
  13. ---------------------------------------------------------------
  14. .. code-block:: c
  15. struct ap_matrix_dev {
  16. ...
  17. struct list_head mdev_list;
  18. struct mutex mdevs_lock;
  19. ...
  20. }
  21. The Matrix Devices Lock (matrix_dev->mdevs_lock) is implemented as a global
  22. mutex contained within the single object of struct ap_matrix_dev. This lock
  23. controls access to all fields contained within each matrix_mdev
  24. (matrix_dev->mdev_list). This lock must be held while reading from, writing to
  25. or using the data from a field contained within a matrix_mdev instance
  26. representing one of the vfio_ap device driver's mediated devices.
  27. The KVM Lock (include/linux/kvm_host.h)
  28. ---------------------------------------
  29. .. code-block:: c
  30. struct kvm {
  31. ...
  32. struct mutex lock;
  33. ...
  34. }
  35. The KVM Lock (kvm->lock) controls access to the state data for a KVM guest. This
  36. lock must be held by the vfio_ap device driver while one or more AP adapters,
  37. domains or control domains are being plugged into or unplugged from the guest.
  38. The KVM pointer is stored in the in the matrix_mdev instance
  39. (matrix_mdev->kvm = kvm) containing the state of the mediated device that has
  40. been attached to the KVM guest.
  41. The Guests Lock (drivers/s390/crypto/vfio_ap_private.h)
  42. -----------------------------------------------------------
  43. .. code-block:: c
  44. struct ap_matrix_dev {
  45. ...
  46. struct list_head mdev_list;
  47. struct mutex guests_lock;
  48. ...
  49. }
  50. The Guests Lock (matrix_dev->guests_lock) controls access to the
  51. matrix_mdev instances (matrix_dev->mdev_list) that represent mediated devices
  52. that hold the state for the mediated devices that have been attached to a
  53. KVM guest. This lock must be held:
  54. 1. To control access to the KVM pointer (matrix_mdev->kvm) while the vfio_ap
  55. device driver is using it to plug/unplug AP devices passed through to the KVM
  56. guest.
  57. 2. To add matrix_mdev instances to or remove them from matrix_dev->mdev_list.
  58. This is necessary to ensure the proper locking order when the list is perused
  59. to find an ap_matrix_mdev instance for the purpose of plugging/unplugging
  60. AP devices passed through to a KVM guest.
  61. For example, when a queue device is removed from the vfio_ap device driver,
  62. if the adapter is passed through to a KVM guest, it will have to be
  63. unplugged. In order to figure out whether the adapter is passed through,
  64. the matrix_mdev object to which the queue is assigned will have to be
  65. found. The KVM pointer (matrix_mdev->kvm) can then be used to determine if
  66. the mediated device is passed through (matrix_mdev->kvm != NULL) and if so,
  67. to unplug the adapter.
  68. It is not necessary to take the Guests Lock to access the KVM pointer if the
  69. pointer is not used to plug/unplug devices passed through to the KVM guest;
  70. however, in this case, the Matrix Devices Lock (matrix_dev->mdevs_lock) must be
  71. held in order to access the KVM pointer since it is set and cleared under the
  72. protection of the Matrix Devices Lock. A case in point is the function that
  73. handles interception of the PQAP(AQIC) instruction sub-function. This handler
  74. needs to access the KVM pointer only for the purposes of setting or clearing IRQ
  75. resources, so only the matrix_dev->mdevs_lock needs to be held.
  76. The PQAP Hook Lock (arch/s390/include/asm/kvm_host.h)
  77. -----------------------------------------------------
  78. .. code-block:: c
  79. typedef int (*crypto_hook)(struct kvm_vcpu *vcpu);
  80. struct kvm_s390_crypto {
  81. ...
  82. struct rw_semaphore pqap_hook_rwsem;
  83. crypto_hook *pqap_hook;
  84. ...
  85. };
  86. The PQAP Hook Lock is a r/w semaphore that controls access to the function
  87. pointer of the handler ``(*kvm->arch.crypto.pqap_hook)`` to invoke when the
  88. PQAP(AQIC) instruction sub-function is intercepted by the host. The lock must be
  89. held in write mode when pqap_hook value is set, and in read mode when the
  90. pqap_hook function is called.