-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy path0613-libxl-Properly-suspend-stubdomains.patch
311 lines (291 loc) · 12.1 KB
/
0613-libxl-Properly-suspend-stubdomains.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
From 32cdc76ed616df3de6ee1fb6ff4e01beb8c7696e Mon Sep 17 00:00:00 2001
From: Demi Marie Obenour <[email protected]>
Date: Fri, 16 Sep 2022 07:31:57 -0400
Subject: [PATCH] libxl: Properly suspend stubdomains
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Currently, libxl neither pauses nor suspends a stubdomain when
suspending the domain it serves. Qubes OS has an out-of-tree patch that
just pauses the stubdomain, but that is also insufficient: sys-net (an
HVM with an attached PCI device) does not properly resume from suspend
on some systems, and the stubdomain considers the TSC clocksource to be
unstable after resume.
This patch properly suspends the stubdomain. Doing so requires creating
a nested libxl__domain_suspend_state structure and freeing it when
necessary. Additionally, a new callback function is added that runs
when the stubdomain has been suspended. libxl__qmp_suspend_save() is
called by this new callback.
Saving the state doesn't work on Qubes for two reasons:
- save/restore consoles are not enabled (as requiring qemu in dom0)
- avoid using QMP
Link: https://github.com/QubesOS/qubes-issues/issues/7404
Co-authored-by: Marek Marczykowski-Górecki <[email protected]>
Signed-off-by: Marek Marczykowski-Górecki <[email protected]>
Signed-off-by: Demi Marie Obenour <[email protected]>
---
tools/libs/light/libxl_dom_suspend.c | 176 +++++++++++++++++++++++----
tools/libs/light/libxl_internal.h | 1 +
2 files changed, 154 insertions(+), 23 deletions(-)
diff --git a/tools/libs/light/libxl_dom_suspend.c b/tools/libs/light/libxl_dom_suspend.c
index ce501a932643..1c7ceee60dbf 100644
--- a/tools/libs/light/libxl_dom_suspend.c
+++ b/tools/libs/light/libxl_dom_suspend.c
@@ -19,9 +19,9 @@
/*====================== Domain suspend =======================*/
-int libxl__domain_suspend_init(libxl__egc *egc,
- libxl__domain_suspend_state *dsps,
- libxl_domain_type type)
+static int libxl__domain_suspend_init_inner(libxl__egc *egc,
+ libxl__domain_suspend_state *dsps,
+ libxl_domain_type type)
{
STATE_AO_GC(dsps->ao);
int rc = ERROR_FAIL;
@@ -35,6 +35,7 @@ int libxl__domain_suspend_init(libxl__egc *egc,
libxl__ev_xswatch_init(&dsps->guest_watch);
libxl__ev_time_init(&dsps->guest_timeout);
libxl__ev_qmp_init(&dsps->qmp);
+ dsps->dm_dsps = dsps->parent_dsps = NULL;
if (type == LIBXL_DOMAIN_TYPE_INVALID) goto out;
dsps->type = type;
@@ -67,18 +68,95 @@ out:
return rc;
}
+static void domain_suspend_device_model_domain_callback(libxl__egc *egc,
+ libxl__domain_suspend_state *dsps,
+ int rc);
+
+int libxl__domain_suspend_init(libxl__egc *egc,
+ libxl__domain_suspend_state *dsps,
+ libxl_domain_type type)
+{
+ STATE_AO_GC(dsps->ao);
+ uint32_t const domid = dsps->domid;
+ int rc = libxl__domain_suspend_init_inner(egc, dsps, type);
+
+ LOGD(DEBUG, domid, "Initialized suspend state");
+ if (type != LIBXL_DOMAIN_TYPE_HVM ||
+ !libxl__stubdomain_is_linux_running(gc, domid))
+ return rc;
+
+ LOGD(DEBUG, domid, "Need to suspend stubdomain too");
+ /* need to suspend the stubdomain too */
+ uint32_t const dm_domid = libxl_get_stubdom_id(CTX, domid);
+ if (rc == 0 && dm_domid != 0) {
+ libxl__domain_suspend_state *dm_dsps;
+
+ GCNEW(dm_dsps);
+ dm_dsps->domid = dm_domid;
+ dm_dsps->ao = dsps->ao;
+
+ dm_dsps->type = libxl__domain_type(gc, dm_domid);
+ if (dm_dsps->type == LIBXL_DOMAIN_TYPE_PV ||
+ dm_dsps->type == LIBXL_DOMAIN_TYPE_PVH) {
+ rc = libxl__domain_suspend_init_inner(egc, dm_dsps, dm_dsps->type);
+ } else {
+ LOGD(ERROR, domid, "Stubdomain %" PRIu32 " detected as neither PV "
+ "nor PVH (got %d), cannot suspend", dm_domid, dm_dsps->type);
+ rc = ERROR_FAIL;
+ }
+ if (rc)
+ libxl__domain_suspend_dispose(gc, dsps);
+ else {
+ dm_dsps->callback_common_done = domain_suspend_device_model_domain_callback;
+ dsps->dm_dsps = dm_dsps;
+ dm_dsps->parent_dsps = dsps;
+ }
+ }
+ return rc;
+}
+
void libxl__domain_suspend_dispose(libxl__gc *gc,
libxl__domain_suspend_state *dsps)
{
- libxl__xswait_stop(gc, &dsps->pvcontrol);
- libxl__ev_evtchn_cancel(gc, &dsps->guest_evtchn);
- libxl__ev_xswatch_deregister(gc, &dsps->guest_watch);
- libxl__ev_time_deregister(gc, &dsps->guest_timeout);
- libxl__ev_qmp_dispose(gc, &dsps->qmp);
+ for (;;) {
+ libxl__xswait_stop(gc, &dsps->pvcontrol);
+ libxl__ev_evtchn_cancel(gc, &dsps->guest_evtchn);
+ libxl__ev_xswatch_deregister(gc, &dsps->guest_watch);
+ libxl__ev_time_deregister(gc, &dsps->guest_timeout);
+ libxl__ev_qmp_dispose(gc, &dsps->qmp);
+ if (dsps->dm_dsps == NULL)
+ break;
+ assert(dsps->parent_dsps == NULL);
+ assert(dsps->dm_dsps->parent_dsps == dsps);
+ dsps = dsps->dm_dsps;
+ assert(dsps->dm_dsps == NULL);
+ }
}
/*----- callbacks, called by xc_domain_save -----*/
+static void domain_suspend_device_model_domain_callback(libxl__egc *egc,
+ libxl__domain_suspend_state *dm_dsps,
+ int rc)
+{
+ STATE_AO_GC(dm_dsps->ao);
+ libxl__domain_suspend_state *dsps = dm_dsps->parent_dsps;
+ assert(dm_dsps->dm_dsps == NULL);
+ assert(dsps);
+ assert(dsps->dm_dsps == dm_dsps);
+ if (rc) {
+ LOGD(ERROR, dsps->domid,
+ "failed to suspend device model (stubdom id %d), rc=%d", dm_dsps->domid, rc);
+ } else {
+ LOGD(DEBUG, dsps->domid,
+ "Successfully suspended stubdomain (stubdom id %d)", dm_dsps->domid);
+ }
+ dsps->callback_device_model_done(egc, dsps, rc); /* must be last */
+}
+
+static void domain_suspend_callback_common(libxl__egc *egc,
+ libxl__domain_suspend_state *dsps);
+
void libxl__domain_suspend_device_model(libxl__egc *egc,
libxl__domain_suspend_state *dsps)
{
@@ -86,6 +164,7 @@ void libxl__domain_suspend_device_model(libxl__egc *egc,
int rc = 0;
uint32_t const domid = dsps->domid;
const char *const filename = dsps->dm_savefile;
+ libxl__domain_suspend_state *dm_dsps = dsps->dm_dsps;
switch (libxl__device_model_version_running(gc, domid)) {
case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL: {
@@ -95,15 +174,24 @@ void libxl__domain_suspend_device_model(libxl__egc *egc,
break;
}
case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN:
- /* calls dsps->callback_device_model_done when done */
- libxl__qmp_suspend_save(egc, dsps); /* must be last */
+ if (dm_dsps) {
+ assert(dm_dsps->type == LIBXL_DOMAIN_TYPE_PVH ||
+ dm_dsps->type == LIBXL_DOMAIN_TYPE_PV);
+ LOGD(DEBUG, domid, "Suspending stubdomain (domid %" PRIu32 ")",
+ dm_dsps->domid);
+ /* calls dm_dsps->callback_common_done when done */
+ domain_suspend_callback_common(egc, dm_dsps); /* must be last */
+ } else {
+ LOGD(DEBUG, domid, "Stubdomain not in use");
+ /* calls dsps->callback_device_model_done when done */
+ libxl__qmp_suspend_save(egc, dsps); /* must be last */
+ }
return;
default:
rc = ERROR_INVAL;
- goto out;
+ break;
}
-out:
if (rc)
LOGD(ERROR, dsps->domid,
"failed to suspend device model, rc=%d", rc);
@@ -130,8 +218,6 @@ static void domain_suspend_common_done(libxl__egc *egc,
libxl__domain_suspend_state *dsps,
int rc);
-static void domain_suspend_callback_common(libxl__egc *egc,
- libxl__domain_suspend_state *dsps);
static void domain_suspend_callback_common_done(libxl__egc *egc,
libxl__domain_suspend_state *dsps, int rc);
@@ -308,6 +394,7 @@ static void domain_suspend_common_wait_guest(libxl__egc *egc,
suspend_common_wait_guest_timeout,
60*1000);
if (rc) goto err;
+
return;
err:
@@ -514,6 +601,7 @@ void libxl__dm_resume(libxl__egc *egc,
{
STATE_AO_GC(dmrs->ao);
int rc = 0;
+ uint32_t dm_domid = libxl_get_stubdom_id(CTX, dmrs->domid);
/* Convenience aliases */
libxl_domid domid = dmrs->domid;
@@ -529,7 +617,6 @@ void libxl__dm_resume(libxl__egc *egc,
switch (libxl__device_model_version_running(gc, domid)) {
case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL: {
- uint32_t dm_domid = libxl_get_stubdom_id(CTX, domid);
const char *path, *state;
path = DEVICE_MODEL_XS_PATH(gc, dm_domid, domid, "/state");
@@ -549,14 +636,57 @@ void libxl__dm_resume(libxl__egc *egc,
if (rc) goto out;
break;
}
- case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN:
- qmp->ao = dmrs->ao;
- qmp->domid = domid;
- qmp->callback = dm_resume_qmp_done;
- qmp->payload_fd = -1;
- rc = libxl__ev_qmp_send(egc, qmp, "cont", NULL);
- if (rc) goto out;
- break;
+ case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN: {
+ xc_domaininfo_t dm_info;
+
+ if (dm_domid == 0 /* || !libxl__stubdomain_is_linux_running() */) {
+ LOGD(DEBUG, domid, "Resuming dom0 device model using QMP");
+ qmp->ao = dmrs->ao;
+ qmp->domid = domid;
+ qmp->callback = dm_resume_qmp_done;
+ qmp->payload_fd = -1;
+ rc = libxl__ev_qmp_send(egc, qmp, "cont", NULL);
+ if (rc) goto out;
+ return;
+ }
+
+ LOGD(DEBUG, domid, "Resuming modern stubdomain: ID %" PRIu32, dm_domid);
+
+ rc = xc_domain_getinfo_single(CTX->xch, dm_domid, &dm_info);
+ if (rc < 0) {
+ LOGED(ERROR, dm_domid, "guest we were resuming has been destroyed");
+ goto out;
+ }
+
+ if ((dm_info.flags & XEN_DOMINF_paused)) {
+ rc = xc_domain_unpause(CTX->xch, dm_domid);
+ if (rc < 0) {
+ LOGED(ERROR, domid,
+ "xc_domain_unpause failed for stubdomain %" PRIu32,
+ dm_domid);
+ goto out;
+ }
+ LOGD(DEBUG, domid,
+ "xc_domain_unpause succeeded for stubdomain %" PRIu32,
+ dm_domid);
+ }
+
+ if ((dm_info.flags & XEN_DOMINF_shutdown)) {
+ int shutdown_reason =
+ (dm_info.flags >> XEN_DOMINF_shutdownshift)
+ & XEN_DOMINF_shutdownmask;
+ if (shutdown_reason != SHUTDOWN_suspend) {
+ LOGD(ERROR, domid, "stubdomain %d being resumed shut down"
+ " with unexpected reason code %d",
+ dm_domid, shutdown_reason);
+ rc = ERROR_FAIL;
+ goto out;
+ }
+
+ rc = domain_resume_raw(gc, dm_domid, dmrs->suspend_cancel);
+ }
+ goto out;
+ }
default:
rc = ERROR_INVAL;
goto out;
diff --git a/tools/libs/light/libxl_internal.h b/tools/libs/light/libxl_internal.h
index 6e2363d03196..d19174cbd727 100644
--- a/tools/libs/light/libxl_internal.h
+++ b/tools/libs/light/libxl_internal.h
@@ -3630,6 +3630,7 @@ struct libxl__domain_suspend_state {
struct libxl__domain_suspend_state*, int rc);
void (*callback_common_done)(libxl__egc*,
struct libxl__domain_suspend_state*, int ok);
+ struct libxl__domain_suspend_state *dm_dsps, *parent_dsps;
};
int libxl__domain_suspend_init(libxl__egc *egc,
libxl__domain_suspend_state *dsps,
--
2.44.0