← Documents Documentation/arch/s390/vfio-ap-locking.rst GitHub 원문 ↗

Linux 6.18.37 · Architecture

VFIO AP Locks Overview

vfio_ap의 matrix mediated-device, KVM guest state, guest-attached list와 PQAP hook을 보호하는 잠금 규칙입니다.

Source pathDocumentation/arch/s390/vfio-ap-locking.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약과 해설

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`을 잡습니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

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

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-34

Matrix 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-53

KVM 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-87

Guests 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/unplugdevice 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-96

KVM 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-115

PQAP 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