요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
.. SPDX-License-Identifier: GPL-2.0
======================
VFIO AP Locks Overview
======================
This document describes the locks that are pertinent to the secure operation
of the vfio_ap device driver. Throughout this document, the following variables
will be used to denote instances of the structures herein described:
.. code-block:: c
struct ap_matrix_dev *matrix_dev;
struct ap_matrix_mdev *matrix_mdev;
struct kvm *kvm;
The Matrix Devices Lock (drivers/s390/crypto/vfio_ap_private.h)
---------------------------------------------------------------
.. code-block:: c
struct ap_matrix_dev {
...
struct list_head mdev_list;
struct mutex mdevs_lock;
...
}
The Matrix Devices Lock (matrix_dev->mdevs_lock) is implemented as a global
mutex contained within the single object of struct ap_matrix_dev. This lock
controls access to all fields contained within each matrix_mdev
(matrix_dev->mdev_list). This lock must be held while reading from, writing to
or using the data from a field contained within a matrix_mdev instance
representing one of the vfio_ap device driver's mediated devices.
The KVM Lock (include/linux/kvm_host.h)
---------------------------------------
.. code-block:: c
struct kvm {
...
struct mutex lock;
...
}
The KVM Lock (kvm->lock) controls access to the state data for a KVM guest. This
lock must be held by the vfio_ap device driver while one or more AP adapters,
domains or control domains are being plugged into or unplugged from the guest.
The KVM pointer is stored in the in the matrix_mdev instance
(matrix_mdev->kvm = kvm) containing the state of the mediated device that has
been attached to the KVM guest.
The Guests Lock (drivers/s390/crypto/vfio_ap_private.h)
-----------------------------------------------------------
.. code-block:: c
struct ap_matrix_dev {
...
struct list_head mdev_list;
struct mutex guests_lock;
...
}
The Guests Lock (matrix_dev->guests_lock) controls access to the
matrix_mdev instances (matrix_dev->mdev_list) that represent mediated devices
that hold the state for the mediated devices that have been attached to a
KVM guest. This lock must be held:
1. To control access to the KVM pointer (matrix_mdev->kvm) while the vfio_ap
device driver is using it to plug/unplug AP devices passed through to the KVM
guest.
2. To add matrix_mdev instances to or remove them from matrix_dev->mdev_list.
This is necessary to ensure the proper locking order when the list is perused
to find an ap_matrix_mdev instance for the purpose of plugging/unplugging
AP devices passed through to a KVM guest.
For example, when a queue device is removed from the vfio_ap device driver,
if the adapter is passed through to a KVM guest, it will have to be
unplugged. In order to figure out whether the adapter is passed through,
the matrix_mdev object to which the queue is assigned will have to be
found. The KVM pointer (matrix_mdev->kvm) can then be used to determine if
the mediated device is passed through (matrix_mdev->kvm != NULL) and if so,
to unplug the adapter.
It is not necessary to take the Guests Lock to access the KVM pointer if the
pointer is not used to plug/unplug devices passed through to the KVM guest;
however, in this case, the Matrix Devices Lock (matrix_dev->mdevs_lock) must be
held in order to access the KVM pointer since it is set and cleared under the
protection of the Matrix Devices Lock. A case in point is the function that
handles interception of the PQAP(AQIC) instruction sub-function. This handler
needs to access the KVM pointer only for the purposes of setting or clearing IRQ
resources, so only the matrix_dev->mdevs_lock needs to be held.
The PQAP Hook Lock (arch/s390/include/asm/kvm_host.h)
-----------------------------------------------------
.. code-block:: c
typedef int (*crypto_hook)(struct kvm_vcpu *vcpu);
struct kvm_s390_crypto {
...
struct rw_semaphore pqap_hook_rwsem;
crypto_hook *pqap_hook;
...
};
The PQAP Hook Lock is a r/w semaphore that controls access to the function
pointer of the handler ``(*kvm->arch.crypto.pqap_hook)`` to invoke when the
PQAP(AQIC) instruction sub-function is intercepted by the host. The lock must be
held in write mode when pqap_hook value is set, and in read mode when the
pqap_hook function is called.
3. 한국어 전문 번역
영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.
VFIO AP 잠금 개요
1-15이 문서는 `GPL-2.0` 라이선스로 제공되며 `vfio_ap` device driver를 안전하게 운용하는 데 관련된 잠금을 설명합니다. 문서 전체에서 다음 변수는 설명 대상 구조체 instance를 나타냅니다.
struct ap_matrix_dev *matrix_dev;
struct ap_matrix_mdev *matrix_mdev;
struct kvm *kvm;
Matrix Devices Lock
16-34Matrix Devices Lock은 `drivers/s390/crypto/vfio_ap_private.h`의 `struct ap_matrix_dev`에 정의됩니다.
struct ap_matrix_dev {
...
struct list_head mdev_list;
struct mutex mdevs_lock;
...
}
`matrix_dev->mdevs_lock`은 유일한 `struct ap_matrix_dev` object 안에 있는 global mutex입니다. 이 잠금은 `matrix_dev->mdev_list`에 속한 각 `matrix_mdev`의 모든 field 접근을 제어합니다.
`vfio_ap` device driver의 mediated device를 나타내는 `matrix_mdev` instance의 field를 읽거나 쓰거나, 그 field에서 얻은 data를 사용할 때는 이 잠금을 보유해야 합니다.
KVM Lock
35-53KVM Lock은 `include/linux/kvm_host.h`의 `struct kvm`에 정의됩니다.
struct kvm {
...
struct mutex lock;
...
}
`kvm->lock`은 KVM guest의 state data 접근을 제어합니다. `vfio_ap` device driver가 AP adapter, domain 또는 control domain 하나 이상을 guest에 plug하거나 guest에서 unplug하는 동안 이 잠금을 보유해야 합니다.
KVM pointer는 KVM guest에 attach된 mediated device의 state를 담는 `matrix_mdev` instance에 `matrix_mdev->kvm = kvm` 형태로 저장됩니다.
Guests Lock
54-87Guests Lock은 `drivers/s390/crypto/vfio_ap_private.h`의 `struct ap_matrix_dev`에 정의됩니다.
struct ap_matrix_dev {
...
struct list_head mdev_list;
struct mutex guests_lock;
...
}
`matrix_dev->guests_lock`은 KVM guest에 attach된 mediated device의 state를 보유하는 `matrix_mdev` instance, 즉 `matrix_dev->mdev_list` 접근을 제어합니다.
| 잠금이 필요한 경우 | 이유 |
|---|---|
| `matrix_mdev->kvm`으로 passthrough AP device를 KVM guest에 plug/unplug | device driver가 guest 연결 상태를 바꾸는 동안 KVM pointer 접근을 보호합니다. |
| `matrix_dev->mdev_list`에 `matrix_mdev` 추가 또는 제거 | passthrough AP device를 plug/unplug하기 위해 list를 순회해 `ap_matrix_mdev`를 찾을 때 올바른 locking order를 보장합니다. |
예를 들어 queue device가 `vfio_ap` device driver에서 제거될 때 adapter가 KVM guest로 passthrough 중이면 이를 unplug해야 합니다. 먼저 queue가 할당된 `matrix_mdev` object를 찾고, `matrix_mdev->kvm != NULL`인지 확인해 mediated device가 passthrough 중인지 판별한 뒤 adapter를 unplug합니다.
Guests Lock이 필요하지 않은 KVM pointer 접근
88-96KVM pointer를 passthrough device의 plug/unplug에 사용하지 않는다면 pointer 접근을 위해 Guests Lock을 잡을 필요는 없습니다. 대신 KVM pointer가 Matrix Devices Lock 보호 아래 설정·해제되므로 `matrix_dev->mdevs_lock`을 보유해야 합니다.
대표 사례는 `PQAP(AQIC)` instruction sub-function interception handler입니다. 이 handler는 IRQ resource를 설정하거나 해제하는 목적으로만 KVM pointer에 접근하므로 `matrix_dev->mdevs_lock`만 보유하면 됩니다.
구분 기준은 pointer를 읽는다는 사실 자체가 아니라 그 pointer를 passthrough AP device의 plug/unplug에 사용하는지 여부입니다.
PQAP Hook Lock
97-115PQAP Hook Lock은 `arch/s390/include/asm/kvm_host.h`의 `struct kvm_s390_crypto`에 정의됩니다.
typedef int (*crypto_hook)(struct kvm_vcpu *vcpu);
struct kvm_s390_crypto {
...
struct rw_semaphore pqap_hook_rwsem;
crypto_hook *pqap_hook;
...
};
`pqap_hook_rwsem`은 host가 `PQAP(AQIC)` instruction sub-function을 intercept할 때 호출할 handler function pointer `(*kvm->arch.crypto.pqap_hook)`의 접근을 제어하는 read/write semaphore입니다.
| 동작 | 잠금 mode |
|---|---|
| `pqap_hook` 값을 설정 | write mode |
| `pqap_hook` 함수를 호출 | read mode |
요약과 해설
vfio-ap-locking.rst:1-115`mdevs_lock`은 모든 `matrix_mdev` field와 그 data 사용을 보호하고, `kvm->lock`은 AP adapter·domain·control domain을 guest에 plug/unplug할 때 KVM state를 보호합니다. `guests_lock`은 passthrough 작업에 쓰는 KVM pointer와 guest-attached `mdev_list` 변경을 보호합니다.
KVM pointer를 IRQ resource 설정처럼 plug/unplug 이외의 용도로만 읽으면 `guests_lock` 대신 pointer 설정·해제를 보호하는 `mdevs_lock`만 필요합니다. PQAP hook pointer는 설정 시 write, 호출 시 read mode로 `pqap_hook_rwsem`을 잡습니다.