| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Domain paging. |
| 4 |
* Copyright (c) 2009 by Citrix Systems, Inc. (Patrick Colp) |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#define _GNU_SOURCE |
| 21 |
|
| 22 |
#include <inttypes.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <errno.h> |
| 26 |
#include <stdarg.h> |
| 27 |
#include <time.h> |
| 28 |
#include <signal.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <poll.h> |
| 31 |
#include <xenstore.h> |
| 32 |
#include <getopt.h> |
| 33 |
|
| 34 |
#include "file_ops.h" |
| 35 |
#include "policy.h" |
| 36 |
#include "xenpaging.h" |
| 37 |
|
| 38 |
/* Defines number of mfns a guest should use at a time, in KiB */ |
| 39 |
#define WATCH_TARGETPAGES "memory/target-tot_pages" |
| 40 |
static char *watch_target_tot_pages; |
| 41 |
static char *dom_path; |
| 42 |
static char watch_token[16]; |
| 43 |
static char *filename; |
| 44 |
static int interrupted; |
| 45 |
|
| 46 |
static void unlink_pagefile(void) |
| 47 |
{ |
| 48 |
if ( filename && filename[0] ) |
| 49 |
{ |
| 50 |
unlink(filename); |
| 51 |
filename[0] = '\0'; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
static void close_handler(int sig) |
| 56 |
{ |
| 57 |
interrupted = sig; |
| 58 |
unlink_pagefile(); |
| 59 |
} |
| 60 |
|
| 61 |
static void xenpaging_mem_paging_flush_ioemu_cache(struct xenpaging *paging) |
| 62 |
{ |
| 63 |
struct xs_handle *xsh = paging->xs_handle; |
| 64 |
domid_t domain_id = paging->vm_event.domain_id; |
| 65 |
char path[80]; |
| 66 |
|
| 67 |
sprintf(path, "/local/domain/0/device-model/%u/command", domain_id); |
| 68 |
|
| 69 |
xs_write(xsh, XBT_NULL, path, "flush-cache", strlen("flush-cache")); |
| 70 |
} |
| 71 |
|
| 72 |
static int xenpaging_wait_for_event_or_timeout(struct xenpaging *paging) |
| 73 |
{ |
| 74 |
xc_interface *xch = paging->xc_handle; |
| 75 |
xenevtchn_handle *xce = paging->vm_event.xce_handle; |
| 76 |
char **vec, *val; |
| 77 |
unsigned int num; |
| 78 |
struct pollfd fd[2]; |
| 79 |
int port; |
| 80 |
int rc; |
| 81 |
int timeout; |
| 82 |
|
| 83 |
/* Wait for event channel and xenstore */ |
| 84 |
fd[0].fd = xenevtchn_fd(xce); |
| 85 |
fd[0].events = POLLIN | POLLERR; |
| 86 |
fd[1].fd = xs_fileno(paging->xs_handle); |
| 87 |
fd[1].events = POLLIN | POLLERR; |
| 88 |
|
| 89 |
/* No timeout while page-out is still in progress */ |
| 90 |
timeout = paging->use_poll_timeout ? 100 : 0; |
| 91 |
rc = poll(fd, 2, timeout); |
| 92 |
if ( rc < 0 ) |
| 93 |
{ |
| 94 |
if (errno == EINTR) |
| 95 |
return 0; |
| 96 |
|
| 97 |
PERROR("Poll exited with an error"); |
| 98 |
return -1; |
| 99 |
} |
| 100 |
|
| 101 |
/* First check for guest shutdown */ |
| 102 |
if ( rc && fd[1].revents & POLLIN ) |
| 103 |
{ |
| 104 |
DPRINTF("Got event from xenstore\n"); |
| 105 |
vec = xs_read_watch(paging->xs_handle, &num); |
| 106 |
if ( vec ) |
| 107 |
{ |
| 108 |
DPRINTF("path '%s' token '%s'\n", vec[XS_WATCH_PATH], vec[XS_WATCH_TOKEN]); |
| 109 |
if ( strcmp(vec[XS_WATCH_TOKEN], watch_token) == 0 ) |
| 110 |
{ |
| 111 |
/* If our guest disappeared, set interrupt flag and fall through */ |
| 112 |
if ( xs_is_domain_introduced(paging->xs_handle, paging->vm_event.domain_id) == false ) |
| 113 |
{ |
| 114 |
xs_unwatch(paging->xs_handle, "@releaseDomain", watch_token); |
| 115 |
interrupted = SIGQUIT; |
| 116 |
/* No further poll result processing */ |
| 117 |
rc = 0; |
| 118 |
} |
| 119 |
} |
| 120 |
else if ( strcmp(vec[XS_WATCH_PATH], watch_target_tot_pages) == 0 ) |
| 121 |
{ |
| 122 |
int ret, target_tot_pages; |
| 123 |
val = xs_read(paging->xs_handle, XBT_NULL, vec[XS_WATCH_PATH], NULL); |
| 124 |
if ( val ) |
| 125 |
{ |
| 126 |
ret = sscanf(val, "%d", &target_tot_pages); |
| 127 |
if ( ret > 0 ) |
| 128 |
{ |
| 129 |
/* KiB to pages */ |
| 130 |
target_tot_pages >>= 2; |
| 131 |
if ( target_tot_pages < 0 || target_tot_pages > paging->max_pages ) |
| 132 |
target_tot_pages = paging->max_pages; |
| 133 |
paging->target_tot_pages = target_tot_pages; |
| 134 |
/* Disable poll() delay while new target is not yet reached */ |
| 135 |
paging->use_poll_timeout = 0; |
| 136 |
DPRINTF("new target_tot_pages %d\n", target_tot_pages); |
| 137 |
} |
| 138 |
free(val); |
| 139 |
} |
| 140 |
} |
| 141 |
free(vec); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
if ( rc && fd[0].revents & POLLIN ) |
| 146 |
{ |
| 147 |
DPRINTF("Got event from evtchn\n"); |
| 148 |
port = xenevtchn_pending(xce); |
| 149 |
if ( port == -1 ) |
| 150 |
{ |
| 151 |
PERROR("Failed to read port from event channel"); |
| 152 |
rc = -1; |
| 153 |
goto err; |
| 154 |
} |
| 155 |
|
| 156 |
rc = xenevtchn_unmask(xce, port); |
| 157 |
if ( rc < 0 ) |
| 158 |
{ |
| 159 |
PERROR("Failed to unmask event channel port"); |
| 160 |
} |
| 161 |
} |
| 162 |
err: |
| 163 |
return rc; |
| 164 |
} |
| 165 |
|
| 166 |
static int xenpaging_get_tot_pages(struct xenpaging *paging) |
| 167 |
{ |
| 168 |
xc_interface *xch = paging->xc_handle; |
| 169 |
xc_domaininfo_t domain_info; |
| 170 |
int rc; |
| 171 |
|
| 172 |
rc = xc_domain_getinfo_single(xch, paging->vm_event.domain_id, &domain_info); |
| 173 |
if ( rc < 0 ) |
| 174 |
{ |
| 175 |
PERROR("Error getting domain info"); |
| 176 |
return -1; |
| 177 |
} |
| 178 |
return domain_info.tot_pages; |
| 179 |
} |
| 180 |
|
| 181 |
static void *init_page(void) |
| 182 |
{ |
| 183 |
void *buffer; |
| 184 |
|
| 185 |
/* Allocated page memory */ |
| 186 |
errno = posix_memalign(&buffer, XC_PAGE_SIZE, XC_PAGE_SIZE); |
| 187 |
if ( errno != 0 ) |
| 188 |
return NULL; |
| 189 |
|
| 190 |
/* Lock buffer in memory so it can't be paged out */ |
| 191 |
if ( mlock(buffer, XC_PAGE_SIZE) < 0 ) |
| 192 |
{ |
| 193 |
free(buffer); |
| 194 |
buffer = NULL; |
| 195 |
} |
| 196 |
|
| 197 |
return buffer; |
| 198 |
} |
| 199 |
|
| 200 |
static void usage(void) |
| 201 |
{ |
| 202 |
printf("usage:\n\n"); |
| 203 |
|
| 204 |
printf(" xenpaging [options] -f <pagefile> -d <domain_id>\n\n"); |
| 205 |
|
| 206 |
printf("options:\n"); |
| 207 |
printf(" -d <domid> --domain=<domid> numerical domain_id of guest. This option is required.\n"); |
| 208 |
printf(" -f <file> --pagefile=<file> pagefile to use. This option is required.\n"); |
| 209 |
printf(" -m <max_memkb> --max_memkb=<max_memkb> maximum amount of memory to handle.\n"); |
| 210 |
printf(" -r <num> --mru_size=<num> number of paged-in pages to keep in memory.\n"); |
| 211 |
printf(" -v --verbose enable debug output.\n"); |
| 212 |
printf(" -h --help this output.\n"); |
| 213 |
} |
| 214 |
|
| 215 |
static int xenpaging_getopts(struct xenpaging *paging, int argc, char *argv[]) |
| 216 |
{ |
| 217 |
int ch; |
| 218 |
static const char sopts[] = "hvd:f:m:r:"; |
| 219 |
static const struct option lopts[] = { |
| 220 |
{"help", 0, NULL, 'h'}, |
| 221 |
{"verbose", 0, NULL, 'v'}, |
| 222 |
{"domain", 1, NULL, 'd'}, |
| 223 |
{"pagefile", 1, NULL, 'f'}, |
| 224 |
{"mru_size", 1, NULL, 'm'}, |
| 225 |
{ } |
| 226 |
}; |
| 227 |
|
| 228 |
while ((ch = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) |
| 229 |
{ |
| 230 |
switch(ch) { |
| 231 |
case 'd': |
| 232 |
paging->vm_event.domain_id = atoi(optarg); |
| 233 |
break; |
| 234 |
case 'f': |
| 235 |
free(filename); |
| 236 |
filename = strdup(optarg); |
| 237 |
break; |
| 238 |
case 'm': |
| 239 |
/* KiB to pages */ |
| 240 |
paging->max_pages = atoi(optarg) >> 2; |
| 241 |
break; |
| 242 |
case 'r': |
| 243 |
paging->policy_mru_size = atoi(optarg); |
| 244 |
break; |
| 245 |
case 'v': |
| 246 |
paging->debug = 1; |
| 247 |
break; |
| 248 |
case 'h': |
| 249 |
case '?': |
| 250 |
usage(); |
| 251 |
return 1; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
argv += optind; argc -= optind; |
| 256 |
|
| 257 |
/* Path to pagefile is required */ |
| 258 |
if ( !filename ) |
| 259 |
{ |
| 260 |
printf("Filename for pagefile missing!\n"); |
| 261 |
usage(); |
| 262 |
return 1; |
| 263 |
} |
| 264 |
|
| 265 |
/* Set domain id */ |
| 266 |
if ( !paging->vm_event.domain_id ) |
| 267 |
{ |
| 268 |
printf("Numerical <domain_id> missing!\n"); |
| 269 |
return 1; |
| 270 |
} |
| 271 |
|
| 272 |
return 0; |
| 273 |
} |
| 274 |
|
| 275 |
static struct xenpaging *xenpaging_init(int argc, char *argv[]) |
| 276 |
{ |
| 277 |
struct xenpaging *paging; |
| 278 |
xc_domaininfo_t domain_info; |
| 279 |
xc_interface *xch = NULL; |
| 280 |
char *p; |
| 281 |
int rc; |
| 282 |
unsigned long ring_pfn, mmap_pfn; |
| 283 |
|
| 284 |
/* Allocate memory */ |
| 285 |
paging = calloc(1, sizeof(struct xenpaging)); |
| 286 |
if ( !paging ) |
| 287 |
goto err; |
| 288 |
|
| 289 |
/* Get cmdline options and domain_id */ |
| 290 |
if ( xenpaging_getopts(paging, argc, argv) ) |
| 291 |
goto err; |
| 292 |
|
| 293 |
paging->logger = (xentoollog_logger *)xtl_createlogger_stdiostream(stderr, |
| 294 |
paging->debug ? XTL_DEBUG : XTL_PROGRESS, 0); |
| 295 |
|
| 296 |
/* Open connection to xen */ |
| 297 |
paging->xc_handle = xch = xc_interface_open(paging->logger, NULL, 0); |
| 298 |
if ( !xch ) |
| 299 |
goto err; |
| 300 |
|
| 301 |
DPRINTF("xenpaging init\n"); |
| 302 |
|
| 303 |
/* Open connection to xenstore */ |
| 304 |
paging->xs_handle = xs_open(0); |
| 305 |
if ( paging->xs_handle == NULL ) |
| 306 |
{ |
| 307 |
PERROR("Error initialising xenstore connection"); |
| 308 |
goto err; |
| 309 |
} |
| 310 |
|
| 311 |
/* write domain ID to watch so we can ignore other domain shutdowns */ |
| 312 |
snprintf(watch_token, sizeof(watch_token), "%u", paging->vm_event.domain_id); |
| 313 |
if ( xs_watch(paging->xs_handle, "@releaseDomain", watch_token) == false ) |
| 314 |
{ |
| 315 |
PERROR("Could not bind to shutdown watch\n"); |
| 316 |
goto err; |
| 317 |
} |
| 318 |
|
| 319 |
/* Watch xenpagings working target */ |
| 320 |
dom_path = xs_get_domain_path(paging->xs_handle, paging->vm_event.domain_id); |
| 321 |
if ( !dom_path ) |
| 322 |
{ |
| 323 |
PERROR("Could not find domain path\n"); |
| 324 |
goto err; |
| 325 |
} |
| 326 |
if ( asprintf(&watch_target_tot_pages, "%s/%s", dom_path, WATCH_TARGETPAGES) < 0 ) |
| 327 |
{ |
| 328 |
PERROR("Could not alloc watch path\n"); |
| 329 |
goto err; |
| 330 |
} |
| 331 |
DPRINTF("watching '%s'\n", watch_target_tot_pages); |
| 332 |
if ( xs_watch(paging->xs_handle, watch_target_tot_pages, "") == false ) |
| 333 |
{ |
| 334 |
PERROR("Could not bind to xenpaging watch\n"); |
| 335 |
goto err; |
| 336 |
} |
| 337 |
|
| 338 |
/* Map the ring page */ |
| 339 |
xc_get_hvm_param(xch, paging->vm_event.domain_id, |
| 340 |
HVM_PARAM_PAGING_RING_PFN, &ring_pfn); |
| 341 |
mmap_pfn = ring_pfn; |
| 342 |
paging->vm_event.ring_page = |
| 343 |
xc_map_foreign_pages(xch, paging->vm_event.domain_id, |
| 344 |
PROT_READ | PROT_WRITE, &mmap_pfn, 1); |
| 345 |
if ( !paging->vm_event.ring_page ) |
| 346 |
{ |
| 347 |
/* Map failed, populate ring page */ |
| 348 |
rc = xc_domain_populate_physmap_exact(paging->xc_handle, |
| 349 |
paging->vm_event.domain_id, |
| 350 |
1, 0, 0, &ring_pfn); |
| 351 |
if ( rc != 0 ) |
| 352 |
{ |
| 353 |
PERROR("Failed to populate ring gfn\n"); |
| 354 |
goto err; |
| 355 |
} |
| 356 |
|
| 357 |
paging->vm_event.ring_page = |
| 358 |
xc_map_foreign_pages(xch, paging->vm_event.domain_id, |
| 359 |
PROT_READ | PROT_WRITE, |
| 360 |
&mmap_pfn, 1); |
| 361 |
if ( !paging->vm_event.ring_page ) |
| 362 |
{ |
| 363 |
PERROR("Could not map the ring page\n"); |
| 364 |
goto err; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
/* Initialise Xen */ |
| 369 |
rc = xc_mem_paging_enable(xch, paging->vm_event.domain_id, |
| 370 |
&paging->vm_event.evtchn_port); |
| 371 |
if ( rc != 0 ) |
| 372 |
{ |
| 373 |
switch ( errno ) { |
| 374 |
case EBUSY: |
| 375 |
ERROR("xenpaging is (or was) active on this domain"); |
| 376 |
break; |
| 377 |
case ENODEV: |
| 378 |
ERROR("xenpaging requires Hardware Assisted Paging"); |
| 379 |
break; |
| 380 |
case EMLINK: |
| 381 |
ERROR("xenpaging not supported while iommu passthrough is enabled"); |
| 382 |
break; |
| 383 |
case EXDEV: |
| 384 |
ERROR("xenpaging not supported in a PoD guest"); |
| 385 |
break; |
| 386 |
default: |
| 387 |
PERROR("Error initialising shared page"); |
| 388 |
break; |
| 389 |
} |
| 390 |
goto err; |
| 391 |
} |
| 392 |
|
| 393 |
/* Open event channel */ |
| 394 |
paging->vm_event.xce_handle = xenevtchn_open(NULL, 0); |
| 395 |
if ( paging->vm_event.xce_handle == NULL ) |
| 396 |
{ |
| 397 |
PERROR("Failed to open event channel"); |
| 398 |
goto err; |
| 399 |
} |
| 400 |
|
| 401 |
/* Bind event notification */ |
| 402 |
rc = xenevtchn_bind_interdomain(paging->vm_event.xce_handle, |
| 403 |
paging->vm_event.domain_id, |
| 404 |
paging->vm_event.evtchn_port); |
| 405 |
if ( rc < 0 ) |
| 406 |
{ |
| 407 |
PERROR("Failed to bind event channel"); |
| 408 |
goto err; |
| 409 |
} |
| 410 |
|
| 411 |
paging->vm_event.port = rc; |
| 412 |
|
| 413 |
/* Initialise ring */ |
| 414 |
SHARED_RING_INIT((vm_event_sring_t *)paging->vm_event.ring_page); |
| 415 |
BACK_RING_INIT(&paging->vm_event.back_ring, |
| 416 |
(vm_event_sring_t *)paging->vm_event.ring_page, |
| 417 |
XC_PAGE_SIZE); |
| 418 |
|
| 419 |
/* Now that the ring is set, remove it from the guest's physmap */ |
| 420 |
if ( xc_domain_decrease_reservation_exact(xch, |
| 421 |
paging->vm_event.domain_id, 1, 0, &ring_pfn) ) |
| 422 |
PERROR("Failed to remove ring from guest physmap"); |
| 423 |
|
| 424 |
/* Get max_pages from guest if not provided via cmdline */ |
| 425 |
if ( !paging->max_pages ) |
| 426 |
{ |
| 427 |
rc = xc_domain_getinfo_single(xch, paging->vm_event.domain_id, |
| 428 |
&domain_info); |
| 429 |
if ( rc < 0 ) |
| 430 |
{ |
| 431 |
PERROR("Error getting domain info"); |
| 432 |
goto err; |
| 433 |
} |
| 434 |
|
| 435 |
/* Record number of max_pages */ |
| 436 |
paging->max_pages = domain_info.max_pages; |
| 437 |
} |
| 438 |
|
| 439 |
/* Allocate bitmap for tracking pages that have been paged out */ |
| 440 |
paging->bitmap = bitmap_alloc(paging->max_pages); |
| 441 |
if ( !paging->bitmap ) |
| 442 |
{ |
| 443 |
PERROR("Error allocating bitmap"); |
| 444 |
goto err; |
| 445 |
} |
| 446 |
DPRINTF("max_pages = %d\n", paging->max_pages); |
| 447 |
|
| 448 |
/* Allocate indicies for pagefile slots */ |
| 449 |
paging->slot_to_gfn = calloc(paging->max_pages, sizeof(*paging->slot_to_gfn)); |
| 450 |
paging->gfn_to_slot = calloc(paging->max_pages, sizeof(*paging->gfn_to_slot)); |
| 451 |
if ( !paging->slot_to_gfn || !paging->gfn_to_slot ) |
| 452 |
goto err; |
| 453 |
|
| 454 |
/* Allocate stack for known free slots in pagefile */ |
| 455 |
paging->free_slot_stack = calloc(paging->max_pages, sizeof(*paging->free_slot_stack)); |
| 456 |
if ( !paging->free_slot_stack ) |
| 457 |
goto err; |
| 458 |
|
| 459 |
/* Initialise policy */ |
| 460 |
rc = policy_init(paging); |
| 461 |
if ( rc != 0 ) |
| 462 |
{ |
| 463 |
PERROR("Error initialising policy"); |
| 464 |
goto err; |
| 465 |
} |
| 466 |
|
| 467 |
paging->paging_buffer = init_page(); |
| 468 |
if ( !paging->paging_buffer ) |
| 469 |
{ |
| 470 |
PERROR("Creating page aligned load buffer"); |
| 471 |
goto err; |
| 472 |
} |
| 473 |
|
| 474 |
/* Open file */ |
| 475 |
paging->fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR); |
| 476 |
if ( paging->fd < 0 ) |
| 477 |
{ |
| 478 |
PERROR("failed to open file"); |
| 479 |
goto err; |
| 480 |
} |
| 481 |
|
| 482 |
return paging; |
| 483 |
|
| 484 |
err: |
| 485 |
if ( paging ) |
| 486 |
{ |
| 487 |
if ( paging->xs_handle ) |
| 488 |
xs_close(paging->xs_handle); |
| 489 |
if ( xch ) |
| 490 |
xc_interface_close(xch); |
| 491 |
if ( paging->logger ) |
| 492 |
xtl_logger_destroy(paging->logger); |
| 493 |
if ( paging->paging_buffer ) |
| 494 |
{ |
| 495 |
munlock(paging->paging_buffer, XC_PAGE_SIZE); |
| 496 |
free(paging->paging_buffer); |
| 497 |
} |
| 498 |
|
| 499 |
if ( paging->vm_event.ring_page ) |
| 500 |
{ |
| 501 |
munmap(paging->vm_event.ring_page, XC_PAGE_SIZE); |
| 502 |
} |
| 503 |
|
| 504 |
free(dom_path); |
| 505 |
free(watch_target_tot_pages); |
| 506 |
free(paging->free_slot_stack); |
| 507 |
free(paging->slot_to_gfn); |
| 508 |
free(paging->gfn_to_slot); |
| 509 |
free(paging->bitmap); |
| 510 |
free(paging); |
| 511 |
} |
| 512 |
|
| 513 |
return NULL; |
| 514 |
} |
| 515 |
|
| 516 |
static void xenpaging_teardown(struct xenpaging *paging) |
| 517 |
{ |
| 518 |
int rc; |
| 519 |
xc_interface *xch = paging->xc_handle; |
| 520 |
|
| 521 |
xs_unwatch(paging->xs_handle, watch_target_tot_pages, ""); |
| 522 |
xs_unwatch(paging->xs_handle, "@releaseDomain", watch_token); |
| 523 |
|
| 524 |
paging->xc_handle = NULL; |
| 525 |
/* Tear down domain paging in Xen */ |
| 526 |
munmap(paging->vm_event.ring_page, XC_PAGE_SIZE); |
| 527 |
rc = xc_mem_paging_disable(xch, paging->vm_event.domain_id); |
| 528 |
if ( rc != 0 ) |
| 529 |
{ |
| 530 |
PERROR("Error tearing down domain paging in xen"); |
| 531 |
} |
| 532 |
|
| 533 |
/* Unbind VIRQ */ |
| 534 |
rc = xenevtchn_unbind(paging->vm_event.xce_handle, paging->vm_event.port); |
| 535 |
if ( rc != 0 ) |
| 536 |
{ |
| 537 |
PERROR("Error unbinding event port"); |
| 538 |
} |
| 539 |
paging->vm_event.port = -1; |
| 540 |
|
| 541 |
/* Close event channel */ |
| 542 |
rc = xenevtchn_close(paging->vm_event.xce_handle); |
| 543 |
if ( rc != 0 ) |
| 544 |
{ |
| 545 |
PERROR("Error closing event channel"); |
| 546 |
} |
| 547 |
paging->vm_event.xce_handle = NULL; |
| 548 |
|
| 549 |
/* Close connection to xenstore */ |
| 550 |
xs_close(paging->xs_handle); |
| 551 |
|
| 552 |
/* Close connection to Xen */ |
| 553 |
xc_interface_close(xch); |
| 554 |
|
| 555 |
xtl_logger_destroy(paging->logger); |
| 556 |
} |
| 557 |
|
| 558 |
static void get_request(struct vm_event *vm_event, vm_event_request_t *req) |
| 559 |
{ |
| 560 |
vm_event_back_ring_t *back_ring; |
| 561 |
RING_IDX req_cons; |
| 562 |
|
| 563 |
back_ring = &vm_event->back_ring; |
| 564 |
req_cons = back_ring->req_cons; |
| 565 |
|
| 566 |
/* Copy request */ |
| 567 |
memcpy(req, RING_GET_REQUEST(back_ring, req_cons), sizeof(*req)); |
| 568 |
req_cons++; |
| 569 |
|
| 570 |
/* Update ring */ |
| 571 |
back_ring->req_cons = req_cons; |
| 572 |
back_ring->sring->req_event = req_cons + 1; |
| 573 |
} |
| 574 |
|
| 575 |
static void put_response(struct vm_event *vm_event, vm_event_response_t *rsp) |
| 576 |
{ |
| 577 |
vm_event_back_ring_t *back_ring; |
| 578 |
RING_IDX rsp_prod; |
| 579 |
|
| 580 |
back_ring = &vm_event->back_ring; |
| 581 |
rsp_prod = back_ring->rsp_prod_pvt; |
| 582 |
|
| 583 |
/* Copy response */ |
| 584 |
memcpy(RING_GET_RESPONSE(back_ring, rsp_prod), rsp, sizeof(*rsp)); |
| 585 |
rsp_prod++; |
| 586 |
|
| 587 |
/* Update ring */ |
| 588 |
back_ring->rsp_prod_pvt = rsp_prod; |
| 589 |
RING_PUSH_RESPONSES(back_ring); |
| 590 |
} |
| 591 |
|
| 592 |
/* Evict a given gfn |
| 593 |
* Returns < 0 on fatal error |
| 594 |
* Returns 0 on successful evict |
| 595 |
* Returns > 0 if gfn can not be evicted |
| 596 |
*/ |
| 597 |
static int xenpaging_evict_page(struct xenpaging *paging, unsigned long gfn, int slot) |
| 598 |
{ |
| 599 |
xc_interface *xch = paging->xc_handle; |
| 600 |
void *page; |
| 601 |
xen_pfn_t victim = gfn; |
| 602 |
int ret; |
| 603 |
struct xen_domctl domctl; |
| 604 |
|
| 605 |
/* Nominate page */ |
| 606 |
ret = xc_mem_paging_nominate(xch, paging->vm_event.domain_id, gfn); |
| 607 |
if ( ret < 0 ) |
| 608 |
{ |
| 609 |
/* unpageable gfn is indicated by EBUSY */ |
| 610 |
if ( errno == EBUSY ) |
| 611 |
ret = 1; |
| 612 |
else |
| 613 |
PERROR("Error nominating page %lx", gfn); |
| 614 |
goto out; |
| 615 |
} |
| 616 |
|
| 617 |
/* Map page */ |
| 618 |
page = xc_map_foreign_pages(xch, paging->vm_event.domain_id, PROT_READ, &victim, 1); |
| 619 |
if ( page == NULL ) |
| 620 |
{ |
| 621 |
PERROR("Error mapping page %lx", gfn); |
| 622 |
ret = -1; |
| 623 |
goto out; |
| 624 |
} |
| 625 |
|
| 626 |
/* Copy page */ |
| 627 |
ret = write_page(paging->fd, page, slot); |
| 628 |
if ( ret < 0 ) |
| 629 |
{ |
| 630 |
PERROR("Error copying page %lx", gfn); |
| 631 |
munmap(page, XC_PAGE_SIZE); |
| 632 |
ret = -1; |
| 633 |
goto out; |
| 634 |
} |
| 635 |
|
| 636 |
/* Release page */ |
| 637 |
munmap(page, XC_PAGE_SIZE); |
| 638 |
|
| 639 |
/* Tell Xen to evict page */ |
| 640 |
ret = xc_mem_paging_evict(xch, paging->vm_event.domain_id, gfn); |
| 641 |
if ( ret < 0 ) |
| 642 |
{ |
| 643 |
/* A gfn in use is indicated by EBUSY */ |
| 644 |
if ( errno == EBUSY ) |
| 645 |
{ |
| 646 |
ret = 1; |
| 647 |
DPRINTF("Nominated page %lx busy", gfn); |
| 648 |
} else |
| 649 |
PERROR("Error evicting page %lx", gfn); |
| 650 |
goto out; |
| 651 |
} |
| 652 |
|
| 653 |
DPRINTF("evict_page > gfn %lx pageslot %d\n", gfn, slot); |
| 654 |
/* Notify policy of page being paged out */ |
| 655 |
policy_notify_paged_out(gfn); |
| 656 |
|
| 657 |
/* Update index */ |
| 658 |
paging->slot_to_gfn[slot] = gfn; |
| 659 |
paging->gfn_to_slot[gfn] = slot; |
| 660 |
|
| 661 |
/* Record number of evicted pages */ |
| 662 |
paging->num_paged_out++; |
| 663 |
|
| 664 |
ret = 0; |
| 665 |
|
| 666 |
out: |
| 667 |
return ret; |
| 668 |
} |
| 669 |
|
| 670 |
static int xenpaging_resume_page(struct xenpaging *paging, vm_event_response_t *rsp, int notify_policy) |
| 671 |
{ |
| 672 |
/* Put the page info on the ring */ |
| 673 |
put_response(&paging->vm_event, rsp); |
| 674 |
|
| 675 |
/* Notify policy of page being paged in */ |
| 676 |
if ( notify_policy ) |
| 677 |
{ |
| 678 |
/* |
| 679 |
* Do not add gfn to mru list if the target is lower than mru size. |
| 680 |
* This allows page-out of these gfns if the target grows again. |
| 681 |
*/ |
| 682 |
if (paging->num_paged_out > paging->policy_mru_size) |
| 683 |
policy_notify_paged_in(rsp->u.mem_paging.gfn); |
| 684 |
else |
| 685 |
policy_notify_paged_in_nomru(rsp->u.mem_paging.gfn); |
| 686 |
|
| 687 |
/* Record number of resumed pages */ |
| 688 |
paging->num_paged_out--; |
| 689 |
} |
| 690 |
|
| 691 |
/* Tell Xen page is ready */ |
| 692 |
return xenevtchn_notify(paging->vm_event.xce_handle, paging->vm_event.port); |
| 693 |
} |
| 694 |
|
| 695 |
static int xenpaging_populate_page(struct xenpaging *paging, unsigned long gfn, int i) |
| 696 |
{ |
| 697 |
xc_interface *xch = paging->xc_handle; |
| 698 |
int ret; |
| 699 |
unsigned char oom = 0; |
| 700 |
|
| 701 |
DPRINTF("populate_page < gfn %lx pageslot %d\n", gfn, i); |
| 702 |
|
| 703 |
/* Read page */ |
| 704 |
ret = read_page(paging->fd, paging->paging_buffer, i); |
| 705 |
if ( ret != 0 ) |
| 706 |
{ |
| 707 |
PERROR("Error reading page"); |
| 708 |
goto out; |
| 709 |
} |
| 710 |
|
| 711 |
do |
| 712 |
{ |
| 713 |
/* Tell Xen to allocate a page for the domain */ |
| 714 |
ret = xc_mem_paging_load(xch, paging->vm_event.domain_id, gfn, paging->paging_buffer); |
| 715 |
if ( ret < 0 ) |
| 716 |
{ |
| 717 |
if ( errno == ENOMEM ) |
| 718 |
{ |
| 719 |
if ( oom++ == 0 ) |
| 720 |
DPRINTF("ENOMEM while preparing gfn %lx\n", gfn); |
| 721 |
sleep(1); |
| 722 |
continue; |
| 723 |
} |
| 724 |
PERROR("Error loading %lx during page-in", gfn); |
| 725 |
ret = -1; |
| 726 |
break; |
| 727 |
} |
| 728 |
} |
| 729 |
while ( ret && !interrupted ); |
| 730 |
|
| 731 |
|
| 732 |
out: |
| 733 |
return ret; |
| 734 |
} |
| 735 |
|
| 736 |
/* Trigger a page-in for a batch of pages */ |
| 737 |
static void resume_pages(struct xenpaging *paging, int num_pages) |
| 738 |
{ |
| 739 |
xc_interface *xch = paging->xc_handle; |
| 740 |
int i, num = 0; |
| 741 |
|
| 742 |
for ( i = 0; i < paging->max_pages && num < num_pages; i++ ) |
| 743 |
{ |
| 744 |
if ( test_bit(i, paging->bitmap) ) |
| 745 |
{ |
| 746 |
paging->pagein_queue[num] = i; |
| 747 |
num++; |
| 748 |
if ( num == XENPAGING_PAGEIN_QUEUE_SIZE ) |
| 749 |
break; |
| 750 |
} |
| 751 |
} |
| 752 |
/* num may be less than num_pages, caller has to try again */ |
| 753 |
if ( num ) |
| 754 |
page_in_trigger(); |
| 755 |
} |
| 756 |
|
| 757 |
/* Evict one gfn and write it to the given slot |
| 758 |
* Returns < 0 on fatal error |
| 759 |
* Returns 0 on successful evict |
| 760 |
* Returns > 0 if no gfn can be evicted |
| 761 |
*/ |
| 762 |
static int evict_victim(struct xenpaging *paging, int slot) |
| 763 |
{ |
| 764 |
xc_interface *xch = paging->xc_handle; |
| 765 |
unsigned long gfn; |
| 766 |
static int num_paged_out; |
| 767 |
int ret; |
| 768 |
|
| 769 |
do |
| 770 |
{ |
| 771 |
gfn = policy_choose_victim(paging); |
| 772 |
if ( gfn == INVALID_MFN ) |
| 773 |
{ |
| 774 |
/* If the number did not change after last flush command then |
| 775 |
* the command did not reach qemu yet, or qemu still processes |
| 776 |
* the command, or qemu has nothing to release. |
| 777 |
* Right now there is no need to issue the command again. |
| 778 |
*/ |
| 779 |
if ( num_paged_out != paging->num_paged_out ) |
| 780 |
{ |
| 781 |
DPRINTF("Flushing qemu cache\n"); |
| 782 |
xenpaging_mem_paging_flush_ioemu_cache(paging); |
| 783 |
num_paged_out = paging->num_paged_out; |
| 784 |
} |
| 785 |
ret = ENOSPC; |
| 786 |
goto out; |
| 787 |
} |
| 788 |
|
| 789 |
if ( interrupted ) |
| 790 |
{ |
| 791 |
ret = EINTR; |
| 792 |
goto out; |
| 793 |
} |
| 794 |
|
| 795 |
ret = xenpaging_evict_page(paging, gfn, slot); |
| 796 |
if ( ret < 0 ) |
| 797 |
goto out; |
| 798 |
} |
| 799 |
while ( ret ); |
| 800 |
|
| 801 |
if ( test_and_set_bit(gfn, paging->bitmap) ) |
| 802 |
ERROR("Page %lx has been evicted before", gfn); |
| 803 |
|
| 804 |
ret = 0; |
| 805 |
|
| 806 |
out: |
| 807 |
return ret; |
| 808 |
} |
| 809 |
|
| 810 |
/* Evict a batch of pages and write them to a free slot in the paging file |
| 811 |
* Returns < 0 on fatal error |
| 812 |
* Returns 0 if no gfn can be evicted |
| 813 |
* Returns > 0 on successful evict |
| 814 |
*/ |
| 815 |
static int evict_pages(struct xenpaging *paging, int num_pages) |
| 816 |
{ |
| 817 |
xc_interface *xch = paging->xc_handle; |
| 818 |
int rc, slot, num = 0; |
| 819 |
|
| 820 |
/* Reuse known free slots */ |
| 821 |
while ( paging->stack_count > 0 && num < num_pages ) |
| 822 |
{ |
| 823 |
slot = paging->free_slot_stack[--paging->stack_count]; |
| 824 |
rc = evict_victim(paging, slot); |
| 825 |
if ( rc ) |
| 826 |
{ |
| 827 |
num = rc < 0 ? -1 : num; |
| 828 |
return num; |
| 829 |
} |
| 830 |
num++; |
| 831 |
} |
| 832 |
|
| 833 |
/* Scan all slots slots for remainders */ |
| 834 |
for ( slot = 0; slot < paging->max_pages && num < num_pages; slot++ ) |
| 835 |
{ |
| 836 |
/* Slot is allocated */ |
| 837 |
if ( paging->slot_to_gfn[slot] ) |
| 838 |
continue; |
| 839 |
|
| 840 |
rc = evict_victim(paging, slot); |
| 841 |
if ( rc ) |
| 842 |
{ |
| 843 |
num = rc < 0 ? -1 : num; |
| 844 |
break; |
| 845 |
} |
| 846 |
|
| 847 |
num++; |
| 848 |
} |
| 849 |
return num; |
| 850 |
} |
| 851 |
|
| 852 |
int main(int argc, char *argv[]) |
| 853 |
{ |
| 854 |
struct sigaction act; |
| 855 |
struct xenpaging *paging; |
| 856 |
vm_event_request_t req; |
| 857 |
vm_event_response_t rsp; |
| 858 |
int num, prev_num = 0; |
| 859 |
int slot; |
| 860 |
int tot_pages; |
| 861 |
int rc; |
| 862 |
xc_interface *xch; |
| 863 |
|
| 864 |
/* Initialise domain paging */ |
| 865 |
paging = xenpaging_init(argc, argv); |
| 866 |
if ( paging == NULL ) |
| 867 |
{ |
| 868 |
fprintf(stderr, "Error initialising paging\n"); |
| 869 |
return 1; |
| 870 |
} |
| 871 |
xch = paging->xc_handle; |
| 872 |
|
| 873 |
DPRINTF("starting %s for domain_id %u with pagefile %s\n", |
| 874 |
argv[0], paging->vm_event.domain_id, filename); |
| 875 |
|
| 876 |
/* ensure that if we get a signal, we'll do cleanup, then exit */ |
| 877 |
act.sa_handler = close_handler; |
| 878 |
act.sa_flags = 0; |
| 879 |
sigemptyset(&act.sa_mask); |
| 880 |
sigaction(SIGHUP, &act, NULL); |
| 881 |
sigaction(SIGTERM, &act, NULL); |
| 882 |
sigaction(SIGINT, &act, NULL); |
| 883 |
sigaction(SIGALRM, &act, NULL); |
| 884 |
|
| 885 |
/* listen for page-in events to stop pager */ |
| 886 |
create_page_in_thread(paging); |
| 887 |
|
| 888 |
/* Swap pages in and out */ |
| 889 |
while ( 1 ) |
| 890 |
{ |
| 891 |
/* Wait for Xen to signal that a page needs paged in */ |
| 892 |
rc = xenpaging_wait_for_event_or_timeout(paging); |
| 893 |
if ( rc < 0 ) |
| 894 |
{ |
| 895 |
ERROR("Error getting event"); |
| 896 |
goto out; |
| 897 |
} |
| 898 |
else if ( rc != 0 ) |
| 899 |
{ |
| 900 |
DPRINTF("Got event from Xen\n"); |
| 901 |
} |
| 902 |
|
| 903 |
while ( RING_HAS_UNCONSUMED_REQUESTS(&paging->vm_event.back_ring) ) |
| 904 |
{ |
| 905 |
/* Indicate possible error */ |
| 906 |
rc = 1; |
| 907 |
|
| 908 |
get_request(&paging->vm_event, &req); |
| 909 |
|
| 910 |
if ( req.u.mem_paging.gfn > paging->max_pages ) |
| 911 |
{ |
| 912 |
ERROR("Requested gfn %"PRIx64" higher than max_pages %x\n", |
| 913 |
req.u.mem_paging.gfn, paging->max_pages); |
| 914 |
goto out; |
| 915 |
} |
| 916 |
|
| 917 |
/* Check if the page has already been paged in */ |
| 918 |
if ( test_and_clear_bit(req.u.mem_paging.gfn, paging->bitmap) ) |
| 919 |
{ |
| 920 |
/* Find where in the paging file to read from */ |
| 921 |
slot = paging->gfn_to_slot[req.u.mem_paging.gfn]; |
| 922 |
|
| 923 |
/* Sanity check */ |
| 924 |
if ( paging->slot_to_gfn[slot] != req.u.mem_paging.gfn ) |
| 925 |
{ |
| 926 |
ERROR("Expected gfn %"PRIx64" in slot %d, but found gfn %lx\n", |
| 927 |
req.u.mem_paging.gfn, slot, paging->slot_to_gfn[slot]); |
| 928 |
goto out; |
| 929 |
} |
| 930 |
|
| 931 |
if ( req.u.mem_paging.flags & MEM_PAGING_DROP_PAGE ) |
| 932 |
{ |
| 933 |
DPRINTF("drop_page ^ gfn %"PRIx64" pageslot %d\n", |
| 934 |
req.u.mem_paging.gfn, slot); |
| 935 |
/* Notify policy of page being dropped */ |
| 936 |
policy_notify_dropped(req.u.mem_paging.gfn); |
| 937 |
} |
| 938 |
else |
| 939 |
{ |
| 940 |
/* Populate the page */ |
| 941 |
if ( xenpaging_populate_page(paging, req.u.mem_paging.gfn, slot) < 0 ) |
| 942 |
{ |
| 943 |
ERROR("Error populating page %"PRIx64"", req.u.mem_paging.gfn); |
| 944 |
goto out; |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
/* Prepare the response */ |
| 949 |
rsp.u.mem_paging.gfn = req.u.mem_paging.gfn; |
| 950 |
rsp.vcpu_id = req.vcpu_id; |
| 951 |
rsp.flags = req.flags; |
| 952 |
|
| 953 |
if ( xenpaging_resume_page(paging, &rsp, 1) < 0 ) |
| 954 |
{ |
| 955 |
PERROR("Error resuming page %"PRIx64"", req.u.mem_paging.gfn); |
| 956 |
goto out; |
| 957 |
} |
| 958 |
|
| 959 |
/* Clear this pagefile slot */ |
| 960 |
paging->slot_to_gfn[slot] = 0; |
| 961 |
|
| 962 |
/* Record this free slot */ |
| 963 |
paging->free_slot_stack[paging->stack_count++] = slot; |
| 964 |
} |
| 965 |
else |
| 966 |
{ |
| 967 |
DPRINTF("page %s populated (domain = %d; vcpu = %d;" |
| 968 |
" gfn = %"PRIx64"; paused = %d; evict_fail = %d)\n", |
| 969 |
req.u.mem_paging.flags & MEM_PAGING_EVICT_FAIL ? "not" : "already", |
| 970 |
paging->vm_event.domain_id, req.vcpu_id, req.u.mem_paging.gfn, |
| 971 |
!!(req.flags & VM_EVENT_FLAG_VCPU_PAUSED) , |
| 972 |
!!(req.u.mem_paging.flags & MEM_PAGING_EVICT_FAIL) ); |
| 973 |
|
| 974 |
/* Tell Xen to resume the vcpu */ |
| 975 |
if (( req.flags & VM_EVENT_FLAG_VCPU_PAUSED ) || |
| 976 |
( req.u.mem_paging.flags & MEM_PAGING_EVICT_FAIL )) |
| 977 |
{ |
| 978 |
/* Prepare the response */ |
| 979 |
rsp.u.mem_paging.gfn = req.u.mem_paging.gfn; |
| 980 |
rsp.vcpu_id = req.vcpu_id; |
| 981 |
rsp.flags = req.flags; |
| 982 |
|
| 983 |
if ( xenpaging_resume_page(paging, &rsp, 0) < 0 ) |
| 984 |
{ |
| 985 |
PERROR("Error resuming page %"PRIx64"", req.u.mem_paging.gfn); |
| 986 |
goto out; |
| 987 |
} |
| 988 |
} |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
/* If interrupted, write all pages back into the guest */ |
| 993 |
if ( interrupted == SIGTERM || interrupted == SIGINT ) |
| 994 |
{ |
| 995 |
/* If no more pages to process, exit loop. */ |
| 996 |
if ( !paging->num_paged_out ) |
| 997 |
break; |
| 998 |
|
| 999 |
/* One more round if there are still pages to process. */ |
| 1000 |
resume_pages(paging, paging->num_paged_out); |
| 1001 |
|
| 1002 |
/* Resume main loop */ |
| 1003 |
continue; |
| 1004 |
} |
| 1005 |
|
| 1006 |
/* Exit main loop on any other signal */ |
| 1007 |
if ( interrupted ) |
| 1008 |
break; |
| 1009 |
|
| 1010 |
/* Indicate possible error */ |
| 1011 |
rc = 1; |
| 1012 |
|
| 1013 |
/* Check if the target has been reached already */ |
| 1014 |
tot_pages = xenpaging_get_tot_pages(paging); |
| 1015 |
if ( tot_pages < 0 ) |
| 1016 |
goto out; |
| 1017 |
|
| 1018 |
/* Resume all pages if paging is disabled or no target was set */ |
| 1019 |
if ( paging->target_tot_pages == 0 ) |
| 1020 |
{ |
| 1021 |
if ( paging->num_paged_out ) |
| 1022 |
resume_pages(paging, paging->num_paged_out); |
| 1023 |
} |
| 1024 |
/* Evict more pages if target not reached */ |
| 1025 |
else if ( tot_pages > paging->target_tot_pages ) |
| 1026 |
{ |
| 1027 |
num = tot_pages - paging->target_tot_pages; |
| 1028 |
if ( num != prev_num ) |
| 1029 |
{ |
| 1030 |
DPRINTF("Need to evict %d pages to reach %d target_tot_pages\n", num, paging->target_tot_pages); |
| 1031 |
prev_num = num; |
| 1032 |
} |
| 1033 |
/* Limit the number of evicts to be able to process page-in requests */ |
| 1034 |
if ( num > 42 ) |
| 1035 |
{ |
| 1036 |
paging->use_poll_timeout = 0; |
| 1037 |
num = 42; |
| 1038 |
} |
| 1039 |
if ( evict_pages(paging, num) < 0 ) |
| 1040 |
goto out; |
| 1041 |
} |
| 1042 |
/* Resume some pages if target not reached */ |
| 1043 |
else if ( tot_pages < paging->target_tot_pages && paging->num_paged_out ) |
| 1044 |
{ |
| 1045 |
num = paging->target_tot_pages - tot_pages; |
| 1046 |
if ( num != prev_num ) |
| 1047 |
{ |
| 1048 |
DPRINTF("Need to resume %d pages to reach %d target_tot_pages\n", num, paging->target_tot_pages); |
| 1049 |
prev_num = num; |
| 1050 |
} |
| 1051 |
resume_pages(paging, num); |
| 1052 |
} |
| 1053 |
/* Now target was reached, enable poll() timeout */ |
| 1054 |
else |
| 1055 |
{ |
| 1056 |
paging->use_poll_timeout = 1; |
| 1057 |
} |
| 1058 |
|
| 1059 |
} |
| 1060 |
|
| 1061 |
/* No error */ |
| 1062 |
rc = 0; |
| 1063 |
|
| 1064 |
DPRINTF("xenpaging got signal %d\n", interrupted); |
| 1065 |
|
| 1066 |
out: |
| 1067 |
close(paging->fd); |
| 1068 |
unlink_pagefile(); |
| 1069 |
|
| 1070 |
/* Tear down domain paging */ |
| 1071 |
xenpaging_teardown(paging); |
| 1072 |
|
| 1073 |
return rc ? 2 : 0; |
| 1074 |
} |
| 1075 |
|
| 1076 |
|
| 1077 |
/* |
| 1078 |
* Local variables: |
| 1079 |
* mode: C |
| 1080 |
* c-file-style: "BSD" |
| 1081 |
* c-basic-offset: 4 |
| 1082 |
* indent-tabs-mode: nil |
| 1083 |
* End: |
| 1084 |
*/ |