qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging

* xsetbv fix (x86 targets TCG)
* remove unused functions
* qht segfault and memory leak fixes
* NBD fixes
* Fix for non-power-of-2 discard granularity
* Memory hotplug fixes
* Migration regressions
* IOAPIC fixes and (disabled by default) EOI register support
* Various other small fixes

# gpg: Signature made Wed 03 Aug 2016 18:01:05 BST
# gpg: using RSA key 0xBFFBD25F78C7AE83
# gpg: Good signature from "Paolo Bonzini <bonzini@gnu.org>"
# gpg: aka "Paolo Bonzini <pbonzini@redhat.com>"
# Primary key fingerprint: 46F5 9FBD 57D6 12E7 BFD4 E2F7 7E15 100C CD36 69B1
# Subkey fingerprint: F133 3857 4B66 2389 866C 7682 BFFB D25F 78C7 AE83

* remotes/bonzini/tags/for-upstream: (25 commits)
util: Fix assertion in iov_copy() upon zero 'bytes' and non-zero 'offset'
qdev: Fix use after free in qdev_init_nofail error path
Reorganize help output of '-display' option
x86: ioapic: add support for explicit EOI
x86: ioapic: ignore level irq during processing
apic: fix broken migration for kvm-apic
fw_cfg: Make base type "fw_cfg" abstract
block: Cater to iscsi with non-power-of-2 discard
osdep: Document differences in rounding macros
nbd: Limit nbdflags to 16 bits
nbd: Fix bad flag detection on server
i2c: fix migration regression introduced by broadcast support
mptsas: really fix migration compatibility
qdist: return "(empty)" instead of NULL when printing an empty dist
qdist: use g_renew and g_new instead of g_realloc and g_malloc.
qdist: fix memory leak during binning
target-i386: fix typo in xsetbv implementation
qht: do not segfault when gathering stats from an uninitialized qht
util: Drop inet_listen()
util: drop unix_nonblocking_connect()
...

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

+251 -217
+14 -4
backends/hostmem.c
··· 203 203 static void host_memory_backend_set_prealloc(Object *obj, bool value, 204 204 Error **errp) 205 205 { 206 + Error *local_err = NULL; 206 207 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 207 208 208 209 if (backend->force_prealloc) { ··· 223 224 void *ptr = memory_region_get_ram_ptr(&backend->mr); 224 225 uint64_t sz = memory_region_size(&backend->mr); 225 226 226 - os_mem_prealloc(fd, ptr, sz); 227 + os_mem_prealloc(fd, ptr, sz, &local_err); 228 + if (local_err) { 229 + error_propagate(errp, local_err); 230 + return; 231 + } 227 232 backend->prealloc = true; 228 233 } 229 234 } ··· 286 291 if (bc->alloc) { 287 292 bc->alloc(backend, &local_err); 288 293 if (local_err) { 289 - error_propagate(errp, local_err); 290 - return; 294 + goto out; 291 295 } 292 296 293 297 ptr = memory_region_get_ram_ptr(&backend->mr); ··· 343 347 * specified NUMA policy in place. 344 348 */ 345 349 if (backend->prealloc) { 346 - os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz); 350 + os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz, 351 + &local_err); 352 + if (local_err) { 353 + goto out; 354 + } 347 355 } 348 356 } 357 + out: 358 + error_propagate(errp, local_err); 349 359 } 350 360 351 361 static bool
+9 -6
block/io.c
··· 1180 1180 int alignment = MAX(bs->bl.pwrite_zeroes_alignment, 1181 1181 bs->bl.request_alignment); 1182 1182 1183 - assert(is_power_of_2(alignment)); 1184 - head = offset & (alignment - 1); 1185 - tail = (offset + count) & (alignment - 1); 1186 - max_write_zeroes &= ~(alignment - 1); 1183 + assert(alignment % bs->bl.request_alignment == 0); 1184 + head = offset % alignment; 1185 + tail = (offset + count) % alignment; 1186 + max_write_zeroes = QEMU_ALIGN_DOWN(max_write_zeroes, alignment); 1187 + assert(max_write_zeroes >= bs->bl.request_alignment); 1187 1188 1188 1189 while (count > 0 && !ret) { 1189 1190 int num = count; ··· 2429 2430 2430 2431 /* Discard is advisory, so ignore any unaligned head or tail */ 2431 2432 align = MAX(bs->bl.pdiscard_alignment, bs->bl.request_alignment); 2432 - assert(is_power_of_2(align)); 2433 - head = MIN(count, -offset & (align - 1)); 2433 + assert(align % bs->bl.request_alignment == 0); 2434 + head = offset % align; 2434 2435 if (head) { 2436 + head = MIN(count, align - head); 2435 2437 count -= head; 2436 2438 offset += head; 2437 2439 } ··· 2449 2451 2450 2452 max_pdiscard = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_pdiscard, INT_MAX), 2451 2453 align); 2454 + assert(max_pdiscard); 2452 2455 2453 2456 while (count > 0) { 2454 2457 int ret;
+1 -1
block/nbd-client.h
··· 20 20 typedef struct NbdClientSession { 21 21 QIOChannelSocket *sioc; /* The master data channel */ 22 22 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */ 23 - uint32_t nbdflags; 23 + uint16_t nbdflags; 24 24 off_t size; 25 25 26 26 CoMutex send_mutex;
+8 -2
exec.c
··· 1226 1226 char *filename; 1227 1227 char *sanitized_name; 1228 1228 char *c; 1229 - void *area; 1229 + void *area = MAP_FAILED; 1230 1230 int fd = -1; 1231 1231 int64_t page_size; 1232 1232 ··· 1314 1314 } 1315 1315 1316 1316 if (mem_prealloc) { 1317 - os_mem_prealloc(fd, area, memory); 1317 + os_mem_prealloc(fd, area, memory, errp); 1318 + if (errp && *errp) { 1319 + goto error; 1320 + } 1318 1321 } 1319 1322 1320 1323 block->fd = fd; 1321 1324 return area; 1322 1325 1323 1326 error: 1327 + if (area != MAP_FAILED) { 1328 + qemu_ram_munmap(area, memory); 1329 + } 1324 1330 if (unlink_on_error) { 1325 1331 unlink(path); 1326 1332 }
+2
hw/core/qdev.c
··· 354 354 355 355 assert(!dev->realized); 356 356 357 + object_ref(OBJECT(dev)); 357 358 object_property_set_bool(OBJECT(dev), true, "realized", &err); 358 359 if (err) { 359 360 error_reportf_err(err, "Initialization of device %s failed: ", 360 361 object_get_typename(OBJECT(dev))); 361 362 exit(1); 362 363 } 364 + object_unref(OBJECT(dev)); 363 365 } 364 366 365 367 void qdev_machine_creation_done(void)
+7 -3
hw/i2c/core.c
··· 17 17 QLIST_ENTRY(I2CNode) next; 18 18 }; 19 19 20 + #define I2C_BROADCAST 0x00 21 + 20 22 struct I2CBus 21 23 { 22 24 BusState qbus; ··· 47 49 if (!QLIST_EMPTY(&bus->current_devs)) { 48 50 if (!bus->broadcast) { 49 51 bus->saved_address = QLIST_FIRST(&bus->current_devs)->elt->address; 52 + } else { 53 + bus->saved_address = I2C_BROADCAST; 50 54 } 51 55 } 52 56 } ··· 58 62 .pre_save = i2c_bus_pre_save, 59 63 .fields = (VMStateField[]) { 60 64 VMSTATE_UINT8(saved_address, I2CBus), 61 - VMSTATE_BOOL(broadcast, I2CBus), 62 65 VMSTATE_END_OF_LIST() 63 66 } 64 67 }; ··· 93 96 I2CSlaveClass *sc; 94 97 I2CNode *node; 95 98 96 - if (address == 0x00) { 99 + if (address == I2C_BROADCAST) { 97 100 /* 98 101 * This is a broadcast, the current_devs will be all the devices of the 99 102 * bus. ··· 221 224 I2CNode *node; 222 225 223 226 bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev))); 224 - if ((bus->saved_address == dev->address) || (bus->broadcast)) { 227 + if ((bus->saved_address == dev->address) || 228 + (bus->saved_address == I2C_BROADCAST)) { 225 229 node = g_malloc(sizeof(struct I2CNode)); 226 230 node->elt = dev; 227 231 QLIST_INSERT_HEAD(&bus->current_devs, node, next);
+30 -6
hw/intc/ioapic.c
··· 21 21 */ 22 22 23 23 #include "qemu/osdep.h" 24 + #include "qemu/error-report.h" 24 25 #include "monitor/monitor.h" 25 26 #include "hw/hw.h" 26 27 #include "hw/i386/pc.h" ··· 117 118 s->ioredtbl[i] |= IOAPIC_LVT_REMOTE_IRR; 118 119 } 119 120 121 + if (coalesce) { 122 + /* We are level triggered interrupts, and the 123 + * guest should be still working on previous one, 124 + * so skip it. */ 125 + continue; 126 + } 127 + 120 128 #ifdef CONFIG_KVM 121 129 if (kvm_irqchip_is_split()) { 122 130 if (info.trig_mode == IOAPIC_TRIGGER_EDGE) { 123 131 kvm_set_irq(kvm_state, i, 1); 124 132 kvm_set_irq(kvm_state, i, 0); 125 133 } else { 126 - if (!coalesce) { 127 - kvm_set_irq(kvm_state, i, 1); 128 - } 134 + kvm_set_irq(kvm_state, i, 1); 129 135 } 130 136 continue; 131 137 } 132 - #else 133 - (void)coalesce; 134 138 #endif 139 + 135 140 /* No matter whether IR is enabled, we translate 136 141 * the IOAPIC message into a MSI one, and its 137 142 * address space will decide whether we need a ··· 265 270 val = s->id << IOAPIC_ID_SHIFT; 266 271 break; 267 272 case IOAPIC_REG_VER: 268 - val = IOAPIC_VERSION | 273 + val = s->version | 269 274 ((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT); 270 275 break; 271 276 default: ··· 354 359 } 355 360 } 356 361 break; 362 + case IOAPIC_EOI: 363 + /* Explicit EOI is only supported for IOAPIC version 0x20 */ 364 + if (size != 4 || s->version != 0x20) { 365 + break; 366 + } 367 + ioapic_eoi_broadcast(val); 368 + break; 357 369 } 358 370 359 371 ioapic_update_kvm_routes(s); ··· 387 399 { 388 400 IOAPICCommonState *s = IOAPIC_COMMON(dev); 389 401 402 + if (s->version != 0x11 && s->version != 0x20) { 403 + error_report("IOAPIC only supports version 0x11 or 0x20 " 404 + "(default: 0x11)."); 405 + exit(1); 406 + } 407 + 390 408 memory_region_init_io(&s->io_memory, OBJECT(s), &ioapic_io_ops, s, 391 409 "ioapic", 0x1000); 392 410 ··· 397 415 qemu_add_machine_init_done_notifier(&s->machine_done); 398 416 } 399 417 418 + static Property ioapic_properties[] = { 419 + DEFINE_PROP_UINT8("version", IOAPICCommonState, version, 0x11), 420 + DEFINE_PROP_END_OF_LIST(), 421 + }; 422 + 400 423 static void ioapic_class_init(ObjectClass *klass, void *data) 401 424 { 402 425 IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass); ··· 404 427 405 428 k->realize = ioapic_realize; 406 429 dc->reset = ioapic_reset_common; 430 + dc->props = ioapic_properties; 407 431 } 408 432 409 433 static const TypeInfo ioapic_info = {
+1
hw/nvram/fw_cfg.c
··· 990 990 static const TypeInfo fw_cfg_info = { 991 991 .name = TYPE_FW_CFG, 992 992 .parent = TYPE_SYS_BUS_DEVICE, 993 + .abstract = true, 993 994 .instance_size = sizeof(FWCfgState), 994 995 .class_init = fw_cfg_class_init, 995 996 };
+3 -1
hw/scsi/mptsas.c
··· 1295 1295 /* With msi=auto, we fall back to MSI off silently */ 1296 1296 error_free(err); 1297 1297 1298 + /* Only used for migration. */ 1299 + s->msi_in_use = (ret == 0); 1298 1300 } 1299 1301 1300 1302 memory_region_init_io(&s->mmio_io, OBJECT(s), &mptsas_mmio_ops, s, ··· 1370 1372 .post_load = mptsas_post_load, 1371 1373 .fields = (VMStateField[]) { 1372 1374 VMSTATE_PCI_DEVICE(dev, MPTSASState), 1373 - VMSTATE_UNUSED(sizeof(bool)), /* Was msi_in_use */ 1375 + VMSTATE_BOOL(msi_in_use, MPTSASState), 1374 1376 VMSTATE_UINT32(state, MPTSASState), 1375 1377 VMSTATE_UINT8(who_init, MPTSASState), 1376 1378 VMSTATE_UINT8(doorbell_state, MPTSASState),
+2
hw/scsi/mptsas.h
··· 31 31 OnOffAuto msi; 32 32 uint64_t sas_addr; 33 33 34 + bool msi_in_use; 35 + 34 36 /* Doorbell register */ 35 37 uint32_t state; 36 38 uint8_t who_init;
+20 -17
include/block/block_int.h
··· 330 330 * otherwise. */ 331 331 uint32_t request_alignment; 332 332 333 - /* maximum number of bytes that can be discarded at once (since it 334 - * is signed, it must be < 2G, if set), should be multiple of 333 + /* Maximum number of bytes that can be discarded at once (since it 334 + * is signed, it must be < 2G, if set). Must be multiple of 335 335 * pdiscard_alignment, but need not be power of 2. May be 0 if no 336 336 * inherent 32-bit limit */ 337 337 int32_t max_pdiscard; 338 338 339 - /* optimal alignment for discard requests in bytes, must be power 340 - * of 2, less than max_pdiscard if that is set, and multiple of 341 - * bl.request_alignment. May be 0 if bl.request_alignment is good 342 - * enough */ 339 + /* Optimal alignment for discard requests in bytes. A power of 2 340 + * is best but not mandatory. Must be a multiple of 341 + * bl.request_alignment, and must be less than max_pdiscard if 342 + * that is set. May be 0 if bl.request_alignment is good enough */ 343 343 uint32_t pdiscard_alignment; 344 344 345 - /* maximum number of bytes that can zeroized at once (since it is 346 - * signed, it must be < 2G, if set), should be multiple of 345 + /* Maximum number of bytes that can zeroized at once (since it is 346 + * signed, it must be < 2G, if set). Must be multiple of 347 347 * pwrite_zeroes_alignment. May be 0 if no inherent 32-bit limit */ 348 348 int32_t max_pwrite_zeroes; 349 349 350 - /* optimal alignment for write zeroes requests in bytes, must be 351 - * power of 2, less than max_pwrite_zeroes if that is set, and 352 - * multiple of bl.request_alignment. May be 0 if 353 - * bl.request_alignment is good enough */ 350 + /* Optimal alignment for write zeroes requests in bytes. A power 351 + * of 2 is best but not mandatory. Must be a multiple of 352 + * bl.request_alignment, and must be less than max_pwrite_zeroes 353 + * if that is set. May be 0 if bl.request_alignment is good 354 + * enough */ 354 355 uint32_t pwrite_zeroes_alignment; 355 356 356 - /* optimal transfer length in bytes (must be power of 2, and 357 - * multiple of bl.request_alignment), or 0 if no preferred size */ 357 + /* Optimal transfer length in bytes. A power of 2 is best but not 358 + * mandatory. Must be a multiple of bl.request_alignment, or 0 if 359 + * no preferred size */ 358 360 uint32_t opt_transfer; 359 361 360 - /* maximal transfer length in bytes (need not be power of 2, but 361 - * should be multiple of opt_transfer), or 0 for no 32-bit limit. 362 - * For now, anything larger than INT_MAX is clamped down. */ 362 + /* Maximal transfer length in bytes. Need not be power of 2, but 363 + * must be multiple of opt_transfer and bl.request_alignment, or 0 364 + * for no 32-bit limit. For now, anything larger than INT_MAX is 365 + * clamped down. */ 363 366 uint32_t max_transfer; 364 367 365 368 /* memory alignment, in bytes so that no bounce buffer is needed */
+3 -3
include/block/nbd.h
··· 90 90 size_t niov, 91 91 size_t length, 92 92 bool do_read); 93 - int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags, 93 + int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags, 94 94 QCryptoTLSCreds *tlscreds, const char *hostname, 95 95 QIOChannel **outioc, 96 96 off_t *size, Error **errp); 97 - int nbd_init(int fd, QIOChannelSocket *sioc, uint32_t flags, off_t size); 97 + int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size); 98 98 ssize_t nbd_send_request(QIOChannel *ioc, struct nbd_request *request); 99 99 ssize_t nbd_receive_reply(QIOChannel *ioc, struct nbd_reply *reply); 100 100 int nbd_client(int fd); ··· 104 104 typedef struct NBDClient NBDClient; 105 105 106 106 NBDExport *nbd_export_new(BlockBackend *blk, off_t dev_offset, off_t size, 107 - uint32_t nbdflags, void (*close)(NBDExport *), 107 + uint16_t nbdflags, void (*close)(NBDExport *), 108 108 Error **errp); 109 109 void nbd_export_close(NBDExport *exp); 110 110 void nbd_export_get(NBDExport *exp);
+2 -2
include/hw/i386/ioapic_internal.h
··· 29 29 30 30 #define MAX_IOAPICS 1 31 31 32 - #define IOAPIC_VERSION 0x11 33 - 34 32 #define IOAPIC_LVT_DEST_SHIFT 56 35 33 #define IOAPIC_LVT_DEST_IDX_SHIFT 48 36 34 #define IOAPIC_LVT_MASKED_SHIFT 16 ··· 71 69 72 70 #define IOAPIC_IOREGSEL 0x00 73 71 #define IOAPIC_IOWIN 0x10 72 + #define IOAPIC_EOI 0x40 74 73 75 74 #define IOAPIC_REG_ID 0x00 76 75 #define IOAPIC_REG_VER 0x01 ··· 109 108 uint32_t irr; 110 109 uint64_t ioredtbl[IOAPIC_NUM_PINS]; 111 110 Notifier machine_done; 111 + uint8_t version; 112 112 }; 113 113 114 114 void ioapic_reset_common(DeviceState *dev);
+1 -1
include/hw/i386/pc.h
··· 388 388 .value = "off",\ 389 389 },\ 390 390 {\ 391 - .driver = "apic",\ 391 + .driver = "apic-common",\ 392 392 .property = "legacy-instance-id",\ 393 393 .value = "on",\ 394 394 },
+6 -2
include/qemu/osdep.h
··· 158 158 /* Round number down to multiple */ 159 159 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m)) 160 160 161 - /* Round number up to multiple */ 161 + /* Round number up to multiple. Safe when m is not a power of 2 (see 162 + * ROUND_UP for a faster version when a power of 2 is guaranteed) */ 162 163 #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m)) 163 164 164 165 /* Check if n is a multiple of m */ ··· 175 176 /* Check if pointer p is n-bytes aligned */ 176 177 #define QEMU_PTR_IS_ALIGNED(p, n) QEMU_IS_ALIGNED((uintptr_t)(p), (n)) 177 178 179 + /* Round number up to multiple. Requires that d be a power of 2 (see 180 + * QEMU_ALIGN_UP for a safer but slower version on arbitrary 181 + * numbers) */ 178 182 #ifndef ROUND_UP 179 183 #define ROUND_UP(n,d) (((n) + (d) - 1) & -(d)) 180 184 #endif ··· 379 383 380 384 void qemu_set_tty_echo(int fd, bool echo); 381 385 382 - void os_mem_prealloc(int fd, char *area, size_t sz); 386 + void os_mem_prealloc(int fd, char *area, size_t sz, Error **errp); 383 387 384 388 int qemu_read_password(char *buf, int buf_size); 385 389
+5
include/qemu/qht.h
··· 69 69 * Attempting to insert a NULL @p is a bug. 70 70 * Inserting the same pointer @p with different @hash values is a bug. 71 71 * 72 + * In case of successful operation, smp_wmb() is implied before the pointer is 73 + * inserted into the hash table. 74 + * 72 75 * Returns true on sucess. 73 76 * Returns false if the @p-@hash pair already exists in the hash table. 74 77 */ ··· 82 85 * @hash: hash of the pointer to be looked up 83 86 * 84 87 * Needs to be called under an RCU read-critical section. 88 + * 89 + * smp_read_barrier_depends() is implied before the call to @func. 85 90 * 86 91 * The user-provided @func compares pointers in QHT against @userp. 87 92 * If the function returns true, a match has been found.
-8
include/qemu/sockets.h
··· 33 33 typedef void NonBlockingConnectHandler(int fd, Error *err, void *opaque); 34 34 35 35 InetSocketAddress *inet_parse(const char *str, Error **errp); 36 - int inet_listen(const char *str, char *ostr, int olen, 37 - int socktype, int port_offset, Error **errp); 38 36 int inet_connect(const char *str, Error **errp); 39 - int inet_nonblocking_connect(const char *str, 40 - NonBlockingConnectHandler *callback, 41 - void *opaque, Error **errp); 42 37 43 38 NetworkAddressFamily inet_netfamily(int family); 44 39 45 40 int unix_listen(const char *path, char *ostr, int olen, Error **errp); 46 41 int unix_connect(const char *path, Error **errp); 47 - int unix_nonblocking_connect(const char *str, 48 - NonBlockingConnectHandler *callback, 49 - void *opaque, Error **errp); 50 42 51 43 SocketAddress *socket_parse(const char *str, Error **errp); 52 44 int socket_connect(SocketAddress *addr, Error **errp,
+15 -13
nbd/client.c
··· 408 408 } 409 409 410 410 411 - int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags, 411 + int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags, 412 412 QCryptoTLSCreds *tlscreds, const char *hostname, 413 413 QIOChannel **outioc, 414 414 off_t *size, Error **errp) ··· 468 468 uint32_t opt; 469 469 uint32_t namesize; 470 470 uint16_t globalflags; 471 - uint16_t exportflags; 472 471 bool fixedNewStyle = false; 473 472 474 473 if (read_sync(ioc, &globalflags, sizeof(globalflags)) != ··· 477 476 goto fail; 478 477 } 479 478 globalflags = be16_to_cpu(globalflags); 480 - *flags = globalflags << 16; 481 479 TRACE("Global flags are %" PRIx32, globalflags); 482 480 if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) { 483 481 fixedNewStyle = true; ··· 545 543 goto fail; 546 544 } 547 545 *size = be64_to_cpu(s); 548 - TRACE("Size is %" PRIu64, *size); 549 546 550 - if (read_sync(ioc, &exportflags, sizeof(exportflags)) != 551 - sizeof(exportflags)) { 547 + if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) { 552 548 error_setg(errp, "Failed to read export flags"); 553 549 goto fail; 554 550 } 555 - exportflags = be16_to_cpu(exportflags); 556 - *flags |= exportflags; 557 - TRACE("Export flags are %" PRIx16, exportflags); 551 + be16_to_cpus(flags); 558 552 } else if (magic == NBD_CLIENT_MAGIC) { 553 + uint32_t oldflags; 554 + 559 555 if (name) { 560 556 error_setg(errp, "Server does not support export names"); 561 557 goto fail; ··· 572 568 *size = be64_to_cpu(s); 573 569 TRACE("Size is %" PRIu64, *size); 574 570 575 - if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) { 571 + if (read_sync(ioc, &oldflags, sizeof(oldflags)) != sizeof(oldflags)) { 576 572 error_setg(errp, "Failed to read export flags"); 577 573 goto fail; 578 574 } 579 - *flags = be32_to_cpu(*flags); 575 + be32_to_cpus(&oldflags); 576 + if (oldflags & ~0xffff) { 577 + error_setg(errp, "Unexpected export flags %0x" PRIx32, oldflags); 578 + goto fail; 579 + } 580 + *flags = oldflags; 580 581 } else { 581 582 error_setg(errp, "Bad magic received"); 582 583 goto fail; 583 584 } 584 585 586 + TRACE("Size is %" PRIu64 ", export flags %" PRIx16, *size, *flags); 585 587 if (read_sync(ioc, &buf, 124) != 124) { 586 588 error_setg(errp, "Failed to read reserved block"); 587 589 goto fail; ··· 593 595 } 594 596 595 597 #ifdef __linux__ 596 - int nbd_init(int fd, QIOChannelSocket *sioc, uint32_t flags, off_t size) 598 + int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size) 597 599 { 598 600 unsigned long sectors = size / BDRV_SECTOR_SIZE; 599 601 if (size / BDRV_SECTOR_SIZE != sectors) { ··· 689 691 } 690 692 691 693 #else 692 - int nbd_init(int fd, QIOChannelSocket *ioc, uint32_t flags, off_t size) 694 + int nbd_init(int fd, QIOChannelSocket *ioc, uint16_t flags, off_t size) 693 695 { 694 696 return -ENOTSUP; 695 697 }
+6 -7
nbd/server.c
··· 63 63 char *name; 64 64 off_t dev_offset; 65 65 off_t size; 66 - uint32_t nbdflags; 66 + uint16_t nbdflags; 67 67 QTAILQ_HEAD(, NBDClient) clients; 68 68 QTAILQ_ENTRY(NBDExport) next; 69 69 ··· 544 544 NBDClient *client = data->client; 545 545 char buf[8 + 8 + 8 + 128]; 546 546 int rc; 547 - const int myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM | 548 - NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA); 547 + const uint16_t myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM | 548 + NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA); 549 549 bool oldStyle; 550 550 551 551 /* Old style negotiation header without options ··· 575 575 576 576 oldStyle = client->exp != NULL && !client->tlscreds; 577 577 if (oldStyle) { 578 - assert ((client->exp->nbdflags & ~65535) == 0); 579 578 TRACE("advertising size %" PRIu64 " and flags %x", 580 579 client->exp->size, client->exp->nbdflags | myflags); 581 580 stq_be_p(buf + 8, NBD_CLIENT_MAGIC); ··· 606 605 goto fail; 607 606 } 608 607 609 - assert ((client->exp->nbdflags & ~65535) == 0); 610 608 TRACE("advertising size %" PRIu64 " and flags %x", 611 609 client->exp->size, client->exp->nbdflags | myflags); 612 610 stq_be_p(buf + 18, client->exp->size); ··· 810 808 } 811 809 812 810 NBDExport *nbd_export_new(BlockBackend *blk, off_t dev_offset, off_t size, 813 - uint32_t nbdflags, void (*close)(NBDExport *), 811 + uint16_t nbdflags, void (*close)(NBDExport *), 814 812 Error **errp) 815 813 { 816 814 NBDExport *exp = g_malloc0(sizeof(NBDExport)); ··· 1057 1055 if (request->type & ~NBD_CMD_MASK_COMMAND & ~NBD_CMD_FLAG_FUA) { 1058 1056 LOG("unsupported flags (got 0x%x)", 1059 1057 request->type & ~NBD_CMD_MASK_COMMAND); 1060 - return -EINVAL; 1058 + rc = -EINVAL; 1059 + goto out; 1061 1060 } 1062 1061 1063 1062 rc = 0;
+1
numa.c
··· 463 463 exit(1); 464 464 } 465 465 466 + host_memory_backend_set_mapped(backend, true); 466 467 memory_region_add_subregion(mr, addr, seg); 467 468 vmstate_register_ram_global(seg); 468 469 addr += size;
+2 -2
qemu-nbd.c
··· 251 251 { 252 252 char *device = arg; 253 253 off_t size; 254 - uint32_t nbdflags; 254 + uint16_t nbdflags; 255 255 QIOChannelSocket *sioc; 256 256 int fd; 257 257 int ret; ··· 465 465 BlockBackend *blk; 466 466 BlockDriverState *bs; 467 467 off_t dev_offset = 0; 468 - uint32_t nbdflags = 0; 468 + uint16_t nbdflags = 0; 469 469 bool disconnect = false; 470 470 const char *bindto = "0.0.0.0"; 471 471 const char *port = NULL;
+22 -7
qemu-options.hx
··· 927 927 928 928 DEF("display", HAS_ARG, QEMU_OPTION_display, 929 929 "-display sdl[,frame=on|off][,alt_grab=on|off][,ctrl_grab=on|off]\n" 930 - " [,window_close=on|off]|curses|none|\n" 931 - " gtk[,grab_on_hover=on|off]|\n" 932 - " vnc=<display>[,<optargs>]\n" 933 - " select display type\n", QEMU_ARCH_ALL) 930 + " [,window_close=on|off][,gl=on|off]|curses|none|\n" 931 + "-display gtk[,grab_on_hover=on|off][,gl=on|off]|\n" 932 + "-display vnc=<display>[,<optargs>]\n" 933 + "-display curses\n" 934 + "-display none" 935 + " select display type\n" 936 + "The default display is equivalent to\n" 937 + #if defined(CONFIG_GTK) 938 + "\t\"-display gtk\"\n" 939 + #elif defined(CONFIG_SDL) 940 + "\t\"-display sdl\"\n" 941 + #elif defined(CONFIG_COCOA) 942 + "\t\"-display cocoa\"\n" 943 + #elif defined(CONFIG_VNC) 944 + "\t\"-vnc localhost:0,to=99,id=default\"\n" 945 + #else 946 + "\t\"-display none\"\n" 947 + #endif 948 + , QEMU_ARCH_ALL) 934 949 STEXI 935 950 @item -display @var{type} 936 951 @findex -display ··· 977 992 ETEXI 978 993 979 994 DEF("curses", 0, QEMU_OPTION_curses, 980 - "-curses use a curses/ncurses interface instead of SDL\n", 995 + "-curses shorthand for -display curses\n", 981 996 QEMU_ARCH_ALL) 982 997 STEXI 983 998 @item -curses ··· 1027 1042 ETEXI 1028 1043 1029 1044 DEF("sdl", 0, QEMU_OPTION_sdl, 1030 - "-sdl enable SDL\n", QEMU_ARCH_ALL) 1045 + "-sdl shorthand for -display sdl\n", QEMU_ARCH_ALL) 1031 1046 STEXI 1032 1047 @item -sdl 1033 1048 @findex -sdl ··· 1224 1239 ETEXI 1225 1240 1226 1241 DEF("vnc", HAS_ARG, QEMU_OPTION_vnc , 1227 - "-vnc display start a VNC server on display\n", QEMU_ARCH_ALL) 1242 + "-vnc <display> shorthand for -display vnc=<display>\n", QEMU_ARCH_ALL) 1228 1243 STEXI 1229 1244 @item -vnc @var{display}[,@var{option}[,@var{option}[,...]]] 1230 1245 @findex -vnc
+4 -1
scripts/checkpatch.pl
··· 2544 2544 } 2545 2545 } 2546 2546 2547 - # check for non-portable ffs() calls that have portable alternatives in QEMU 2547 + # check for non-portable libc calls that have portable alternatives in QEMU 2548 2548 if ($line =~ /\bffs\(/) { 2549 2549 ERROR("use ctz32() instead of ffs()\n" . $herecurr); 2550 2550 } ··· 2553 2553 } 2554 2554 if ($line =~ /\bffsll\(/) { 2555 2555 ERROR("use ctz64() instead of ffsll()\n" . $herecurr); 2556 + } 2557 + if ($line =~ /\bbzero\(/) { 2558 + ERROR("use memset() instead of bzero()\n" . $herecurr); 2556 2559 } 2557 2560 } 2558 2561
+1 -1
target-i386/translate.c
··· 7176 7176 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_ECX]); 7177 7177 gen_helper_xsetbv(cpu_env, cpu_tmp2_i32, cpu_tmp1_i64); 7178 7178 /* End TB because translation flags may change. */ 7179 - gen_jmp_im(s->pc - pc_start); 7179 + gen_jmp_im(s->pc - s->cs_base); 7180 7180 gen_eob(s); 7181 7181 break; 7182 7182
+8 -2
tests/test-qdist.c
··· 360 360 g_assert(isnan(qdist_xmax(&dist))); 361 361 362 362 pr = qdist_pr_plain(&dist, 0); 363 - g_assert(pr == NULL); 363 + g_assert_cmpstr(pr, ==, "(empty)"); 364 + g_free(pr); 364 365 365 366 pr = qdist_pr_plain(&dist, 2); 366 - g_assert(pr == NULL); 367 + g_assert_cmpstr(pr, ==, "(empty)"); 368 + g_free(pr); 369 + 370 + pr = qdist_pr(&dist, 0, QDIST_PR_BORDER); 371 + g_assert_cmpstr(pr, ==, "(empty)"); 372 + g_free(pr); 367 373 368 374 qdist_destroy(&dist); 369 375 }
+4
tests/test-qht.c
··· 95 95 96 96 static void qht_do_test(unsigned int mode, size_t init_entries) 97 97 { 98 + /* under KVM we might fetch stats from an uninitialized qht */ 99 + check_n(0); 100 + 98 101 qht_init(&ht, 0, mode); 99 102 103 + check_n(0); 100 104 insert(0, N); 101 105 check(0, N, true); 102 106 check_n(N);
+39 -31
translate-all.c
··· 1663 1663 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *)); 1664 1664 } 1665 1665 1666 + static void print_qht_statistics(FILE *f, fprintf_function cpu_fprintf, 1667 + struct qht_stats hst) 1668 + { 1669 + uint32_t hgram_opts; 1670 + size_t hgram_bins; 1671 + char *hgram; 1672 + 1673 + if (!hst.head_buckets) { 1674 + return; 1675 + } 1676 + cpu_fprintf(f, "TB hash buckets %zu/%zu (%0.2f%% head buckets used)\n", 1677 + hst.used_head_buckets, hst.head_buckets, 1678 + (double)hst.used_head_buckets / hst.head_buckets * 100); 1679 + 1680 + hgram_opts = QDIST_PR_BORDER | QDIST_PR_LABELS; 1681 + hgram_opts |= QDIST_PR_100X | QDIST_PR_PERCENT; 1682 + if (qdist_xmax(&hst.occupancy) - qdist_xmin(&hst.occupancy) == 1) { 1683 + hgram_opts |= QDIST_PR_NODECIMAL; 1684 + } 1685 + hgram = qdist_pr(&hst.occupancy, 10, hgram_opts); 1686 + cpu_fprintf(f, "TB hash occupancy %0.2f%% avg chain occ. Histogram: %s\n", 1687 + qdist_avg(&hst.occupancy) * 100, hgram); 1688 + g_free(hgram); 1689 + 1690 + hgram_opts = QDIST_PR_BORDER | QDIST_PR_LABELS; 1691 + hgram_bins = qdist_xmax(&hst.chain) - qdist_xmin(&hst.chain); 1692 + if (hgram_bins > 10) { 1693 + hgram_bins = 10; 1694 + } else { 1695 + hgram_bins = 0; 1696 + hgram_opts |= QDIST_PR_NODECIMAL | QDIST_PR_NOBINRANGE; 1697 + } 1698 + hgram = qdist_pr(&hst.chain, hgram_bins, hgram_opts); 1699 + cpu_fprintf(f, "TB hash avg chain %0.3f buckets. Histogram: %s\n", 1700 + qdist_avg(&hst.chain), hgram); 1701 + g_free(hgram); 1702 + } 1703 + 1666 1704 void dump_exec_info(FILE *f, fprintf_function cpu_fprintf) 1667 1705 { 1668 1706 int i, target_code_size, max_target_code_size; 1669 1707 int direct_jmp_count, direct_jmp2_count, cross_page; 1670 1708 TranslationBlock *tb; 1671 1709 struct qht_stats hst; 1672 - uint32_t hgram_opts; 1673 - size_t hgram_bins; 1674 - char *hgram; 1675 1710 1676 1711 target_code_size = 0; 1677 1712 max_target_code_size = 0; ··· 1724 1759 tcg_ctx.tb_ctx.nb_tbs : 0); 1725 1760 1726 1761 qht_statistics_init(&tcg_ctx.tb_ctx.htable, &hst); 1727 - 1728 - cpu_fprintf(f, "TB hash buckets %zu/%zu (%0.2f%% head buckets used)\n", 1729 - hst.used_head_buckets, hst.head_buckets, 1730 - (double)hst.used_head_buckets / hst.head_buckets * 100); 1731 - 1732 - hgram_opts = QDIST_PR_BORDER | QDIST_PR_LABELS; 1733 - hgram_opts |= QDIST_PR_100X | QDIST_PR_PERCENT; 1734 - if (qdist_xmax(&hst.occupancy) - qdist_xmin(&hst.occupancy) == 1) { 1735 - hgram_opts |= QDIST_PR_NODECIMAL; 1736 - } 1737 - hgram = qdist_pr(&hst.occupancy, 10, hgram_opts); 1738 - cpu_fprintf(f, "TB hash occupancy %0.2f%% avg chain occ. Histogram: %s\n", 1739 - qdist_avg(&hst.occupancy) * 100, hgram); 1740 - g_free(hgram); 1741 - 1742 - hgram_opts = QDIST_PR_BORDER | QDIST_PR_LABELS; 1743 - hgram_bins = qdist_xmax(&hst.chain) - qdist_xmin(&hst.chain); 1744 - if (hgram_bins > 10) { 1745 - hgram_bins = 10; 1746 - } else { 1747 - hgram_bins = 0; 1748 - hgram_opts |= QDIST_PR_NODECIMAL | QDIST_PR_NOBINRANGE; 1749 - } 1750 - hgram = qdist_pr(&hst.chain, hgram_bins, hgram_opts); 1751 - cpu_fprintf(f, "TB hash avg chain %0.3f buckets. Histogram: %s\n", 1752 - qdist_avg(&hst.chain), hgram); 1753 - g_free(hgram); 1754 - 1762 + print_qht_statistics(f, cpu_fprintf, hst); 1755 1763 qht_statistics_destroy(&hst); 1756 1764 1757 1765 cpu_fprintf(f, "\nStatistics:\n");
+2 -1
util/iov.c
··· 247 247 { 248 248 size_t len; 249 249 unsigned int i, j; 250 - for (i = 0, j = 0; i < iov_cnt && j < dst_iov_cnt && bytes; i++) { 250 + for (i = 0, j = 0; 251 + i < iov_cnt && j < dst_iov_cnt && (offset || bytes); i++) { 251 252 if (offset >= iov[i].iov_len) { 252 253 offset -= iov[i].iov_len; 253 254 continue;
+13 -13
util/oslib-posix.c
··· 318 318 siglongjmp(sigjump, 1); 319 319 } 320 320 321 - void os_mem_prealloc(int fd, char *area, size_t memory) 321 + void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) 322 322 { 323 323 int ret; 324 324 struct sigaction act, oldact; ··· 330 330 331 331 ret = sigaction(SIGBUS, &act, &oldact); 332 332 if (ret) { 333 - perror("os_mem_prealloc: failed to install signal handler"); 334 - exit(1); 333 + error_setg_errno(errp, errno, 334 + "os_mem_prealloc: failed to install signal handler"); 335 + return; 335 336 } 336 337 337 338 /* unblock SIGBUS */ ··· 340 341 pthread_sigmask(SIG_UNBLOCK, &set, &oldset); 341 342 342 343 if (sigsetjmp(sigjump, 1)) { 343 - fprintf(stderr, "os_mem_prealloc: Insufficient free host memory " 344 - "pages available to allocate guest RAM\n"); 345 - exit(1); 344 + error_setg(errp, "os_mem_prealloc: Insufficient free host memory " 345 + "pages available to allocate guest RAM\n"); 346 346 } else { 347 347 int i; 348 348 size_t hpagesize = qemu_fd_getpagesize(fd); ··· 352 352 for (i = 0; i < numpages; i++) { 353 353 memset(area + (hpagesize * i), 0, 1); 354 354 } 355 - 356 - ret = sigaction(SIGBUS, &oldact, NULL); 357 - if (ret) { 358 - perror("os_mem_prealloc: failed to reinstall signal handler"); 359 - exit(1); 360 - } 355 + } 361 356 362 - pthread_sigmask(SIG_SETMASK, &oldset, NULL); 357 + ret = sigaction(SIGBUS, &oldact, NULL); 358 + if (ret) { 359 + /* Terminate QEMU since it can't recover from error */ 360 + perror("os_mem_prealloc: failed to reinstall signal handler"); 361 + exit(1); 363 362 } 363 + pthread_sigmask(SIG_SETMASK, &oldset, NULL); 364 364 } 365 365 366 366
+1 -1
util/oslib-win32.c
··· 539 539 return system_info.dwPageSize; 540 540 } 541 541 542 - void os_mem_prealloc(int fd, char *area, size_t memory) 542 + void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) 543 543 { 544 544 int i; 545 545 size_t pagesize = getpagesize();
+7 -6
util/qdist.c
··· 14 14 #define NAN (0.0 / 0.0) 15 15 #endif 16 16 17 + #define QDIST_EMPTY_STR "(empty)" 18 + 17 19 void qdist_init(struct qdist *dist) 18 20 { 19 - dist->entries = g_malloc(sizeof(*dist->entries)); 21 + dist->entries = g_new(struct qdist_entry, 1); 20 22 dist->size = 1; 21 23 dist->n = 0; 22 24 } ··· 62 64 63 65 if (unlikely(dist->n == dist->size)) { 64 66 dist->size *= 2; 65 - dist->entries = g_realloc(dist->entries, 66 - sizeof(*dist->entries) * (dist->size)); 67 + dist->entries = g_renew(struct qdist_entry, dist->entries, dist->size); 67 68 } 68 69 dist->n++; 69 70 entry = &dist->entries[dist->n - 1]; ··· 188 189 } 189 190 } 190 191 /* they're equally spaced, so copy the dist and bail out */ 191 - to->entries = g_new(struct qdist_entry, from->n); 192 + to->entries = g_renew(struct qdist_entry, to->entries, n); 192 193 to->n = from->n; 193 194 memcpy(to->entries, from->entries, sizeof(*to->entries) * to->n); 194 195 return; ··· 234 235 char *ret; 235 236 236 237 if (dist->n == 0) { 237 - return NULL; 238 + return g_strdup(QDIST_EMPTY_STR); 238 239 } 239 240 qdist_bin__internal(&binned, dist, n); 240 241 ret = qdist_pr_internal(&binned); ··· 309 310 GString *s; 310 311 311 312 if (dist->n == 0) { 312 - return NULL; 313 + return g_strdup(QDIST_EMPTY_STR); 313 314 } 314 315 315 316 s = g_string_new("");
-74
util/qemu-sockets.c
··· 624 624 return NULL; 625 625 } 626 626 627 - int inet_listen(const char *str, char *ostr, int olen, 628 - int socktype, int port_offset, Error **errp) 629 - { 630 - char *optstr; 631 - int sock = -1; 632 - InetSocketAddress *addr; 633 - 634 - addr = inet_parse(str, errp); 635 - if (addr != NULL) { 636 - sock = inet_listen_saddr(addr, port_offset, true, errp); 637 - if (sock != -1 && ostr) { 638 - optstr = strchr(str, ','); 639 - if (addr->ipv6) { 640 - snprintf(ostr, olen, "[%s]:%s%s", 641 - addr->host, 642 - addr->port, 643 - optstr ? optstr : ""); 644 - } else { 645 - snprintf(ostr, olen, "%s:%s%s", 646 - addr->host, 647 - addr->port, 648 - optstr ? optstr : ""); 649 - } 650 - } 651 - qapi_free_InetSocketAddress(addr); 652 - } 653 - return sock; 654 - } 655 627 656 628 /** 657 629 * Create a blocking socket and connect it to an address. ··· 669 641 addr = inet_parse(str, errp); 670 642 if (addr != NULL) { 671 643 sock = inet_connect_saddr(addr, errp, NULL, NULL); 672 - qapi_free_InetSocketAddress(addr); 673 - } 674 - return sock; 675 - } 676 - 677 - /** 678 - * Create a non-blocking socket and connect it to an address. 679 - * Calls the callback function with fd in case of success or -1 in case of 680 - * error. 681 - * 682 - * @str: address string 683 - * @callback: callback function that is called when connect completes, 684 - * cannot be NULL. 685 - * @opaque: opaque for callback function 686 - * @errp: set in case of an error 687 - * 688 - * Returns: -1 on immediate error, file descriptor on success. 689 - **/ 690 - int inet_nonblocking_connect(const char *str, 691 - NonBlockingConnectHandler *callback, 692 - void *opaque, Error **errp) 693 - { 694 - int sock = -1; 695 - InetSocketAddress *addr; 696 - 697 - g_assert(callback != NULL); 698 - 699 - addr = inet_parse(str, errp); 700 - if (addr != NULL) { 701 - sock = inet_connect_saddr(addr, errp, callback, opaque); 702 644 qapi_free_InetSocketAddress(addr); 703 645 } 704 646 return sock; ··· 892 834 return sock; 893 835 } 894 836 895 - 896 - int unix_nonblocking_connect(const char *path, 897 - NonBlockingConnectHandler *callback, 898 - void *opaque, Error **errp) 899 - { 900 - UnixSocketAddress *saddr; 901 - int sock = -1; 902 - 903 - g_assert(callback != NULL); 904 - 905 - saddr = g_new0(UnixSocketAddress, 1); 906 - saddr->path = g_strdup(path); 907 - sock = unix_connect_saddr(saddr, errp, callback, opaque); 908 - qapi_free_UnixSocketAddress(saddr); 909 - return sock; 910 - } 911 837 912 838 SocketAddress *socket_parse(const char *str, Error **errp) 913 839 {
+12 -2
util/qht.c
··· 445 445 do { 446 446 for (i = 0; i < QHT_BUCKET_ENTRIES; i++) { 447 447 if (b->hashes[i] == hash) { 448 - void *p = atomic_read(&b->pointers[i]); 448 + /* The pointer is dereferenced before seqlock_read_retry, 449 + * so (unlike qht_insert__locked) we need to use 450 + * atomic_rcu_read here. 451 + */ 452 + void *p = atomic_rcu_read(&b->pointers[i]); 449 453 450 454 if (likely(p) && likely(func(p, userp))) { 451 455 return p; ··· 535 539 atomic_rcu_set(&prev->next, b); 536 540 } 537 541 b->hashes[i] = hash; 542 + /* smp_wmb() implicit in seqlock_write_begin. */ 538 543 atomic_set(&b->pointers[i], p); 539 544 seqlock_write_end(&head->sequence); 540 545 return true; ··· 784 789 785 790 map = atomic_rcu_read(&ht->map); 786 791 787 - stats->head_buckets = map->n_buckets; 788 792 stats->used_head_buckets = 0; 789 793 stats->entries = 0; 790 794 qdist_init(&stats->chain); 791 795 qdist_init(&stats->occupancy); 796 + /* bail out if the qht has not yet been initialized */ 797 + if (unlikely(map == NULL)) { 798 + stats->head_buckets = 0; 799 + return; 800 + } 801 + stats->head_buckets = map->n_buckets; 792 802 793 803 for (i = 0; i < map->n_buckets; i++) { 794 804 struct qht_bucket *head = &map->buckets[i];