From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from PA4PR04CU001.outbound.protection.outlook.com (mail-francecentralazon11023079.outbound.protection.outlook.com [40.107.162.79]) by lore.virtuozzo.com (Postfix) with ESMTPS id 46FCB8067F for ; Fri, 25 Sep 2026 14:11:33 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=IlLaSSlGdJNtm1eHmgoBgRhKn4DokTYPt1V1OQWSKYz3MkwHNhvn3LOEkYFW9F6R185iz/jfjLrzzDDIhWWeRhZ07soZMo+a0tV4+kh9l8k1eTPekCh+KFLQFCDxi6SjVD1MXN+6CTkO6kWAJLi4MaabAb+hL+nSPhR7RY4kTU1YTFBdrK8X2vS76GFouq9Rgu8JyczcUY+4QWyz1eCrRqOJt3/wwqFAF92OTMyI+cmeXL3fC4NftswVMoD5plbLYHDFnXgDRQqsm2XU2v+qavhhfFAnBUQ4TirGklxC2X8WNN9XzUaEAM1+BnrA130Jx7dN9KctoTlVFSq94ri0Ow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=MlR2v+ffVklgHINKoFv3iBlHaY4OXKDXx/dNkspj9hc=; b=Tc3yARzEjJPq+gu7TYFpmnxaOJN5icMLxk8gYHGAQhHWYrx+lQ8ZB/3eeNx4YTVzLyqaUZ8+U6Ijj1DBOi94Zal6Xn57gjsF517BwXLJRpJTWDn3Ma5fifo9vuMLFR6t5BeXVY04nTE9oKJmaRdm0MjBEwOct8jK6Vk4+Ht2UtBFI4XKxU+uxivEht37RhrN2HDK5BJLBOUCfQ8sC8NZNx9DzoEWmtjx8uSeiDDic7GxyKA13BWmogorkZbUutfE413QGlHYgulxbkXuHc/qrcOHKbJbLbTR0WhsrA/r2AmlGeYEZ1SXRMN1I9QuqR06PzN8evZKNB2wfIZ+EaKESA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=virtuozzo.com; dmarc=pass action=none header.from=virtuozzo.com; dkim=pass header.d=virtuozzo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=MlR2v+ffVklgHINKoFv3iBlHaY4OXKDXx/dNkspj9hc=; b=w/Mvad4rTTPK/q0BF7lq7OpRiuYEOF57wcDs6p33rr6N48IjAL3ScwTl4DucA4IVa7iV0fLDxI0CgsfkYV9UDqX2sKNy+JbcZgOImzJk6i3yqGbmlQf+dR+PcP4QEx6boZsCqz6u0BYP8rqe4gH9/DCmivaI4uQMVR4/f9wLHIVX92oZiJLhmZzksdczaWsC3eseGC2tbclWaJBaEFGaFN+R4Nz4dFt1h9DYCdyhSWvINogdeMlaCc8rqrOmSptNDBzPSdRDq0n/ZJJ2vkGdhMLJoOFCjJYdNORErYEVWB12yS0jKFb0NV08KMe/gZRbfKu38oeLYTbATm7H2d8cig== Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=virtuozzo.com; Received: from VI0PR08MB10656.eurprd08.prod.outlook.com (2603:10a6:800:20a::12) by GVXPR08MB11398.eurprd08.prod.outlook.com (2603:10a6:150:2e5::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.18; Fri, 25 Sep 2026 14:11:30 +0000 Received: from VI0PR08MB10656.eurprd08.prod.outlook.com ([fe80::4e37:b189:ddcd:3dd8]) by VI0PR08MB10656.eurprd08.prod.outlook.com ([fe80::4e37:b189:ddcd:3dd8%7]) with mapi id 15.21.0451.014; Fri, 25 Sep 2026 14:11:30 +0000 From: Andrey Drobyshev Date: Fri, 25 Sep 2026 17:11:18 +0300 Subject: [PATCH QEMU HCI-8.0 v5 09/13] usb-host: hand off the device across cpr-transfer #VSTOR-137800 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260925-usb-host_v3-v5-9-e5b21724db4d@virtuozzo.com> References: <20260925-usb-host_v3-v5-0-e5b21724db4d@virtuozzo.com> In-Reply-To: <20260925-usb-host_v3-v5-0-e5b21724db4d@virtuozzo.com> To: svt-core@virtuozzo.com Cc: andrey.drobyshev@virtuozzo.com, den@openvz.org X-Mailer: b4 0.16.0 X-ClientProxiedBy: VI1PR06CA0178.eurprd06.prod.outlook.com (2603:10a6:803:c8::35) To VI0PR08MB10656.eurprd08.prod.outlook.com (2603:10a6:800:20a::12) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VI0PR08MB10656:EE_|GVXPR08MB11398:EE_ X-MS-Office365-Filtering-Correlation-Id: 2eea64ad-a8e0-4b97-7828-08df1b0ee571 X-LD-Processed: 0bc7f26d-0264-416e-a6fc-8352af79c58f,ExtAddr List-Id: svt-core@virtuozzo.com X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|23010399003|366016|376014|52116014|1800799024|38350700014|5113699003|10067099003|56012099006|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: 93RU2Ha7p2NDtOLn0oOtl3h73euk8g4alNXzWN6KQdOQaBMkqGree6Md5ZULDXpT2GmIwtD9UXoBbKHzjtJDoTAf5oxtA2uyU2ys5UuBE2MJkEdBZMlolvzJpkuYf+/kWpPDRx4JVoB5bBGUPYKfd5TktBITjmE5j8UE+muVlqnyP11H8J64WR1WeT62Q8/EWhJ9nck7xGdjyX4WbPDIcpShFfm+1eDeiISPyhOiUOLWI9U+c6YhMS1xbVTYjpOgUidSihgcikdfoHrAKboJXsrijaYLnRkcH8Ic2OtILAsmoco3hWRpuYIyJDVJCl7koVC8dOBG+GaxRHSjOPdQ0yca6qKfNunDwOlCAaGKPf3uWlk0Niv7ElFrgieivSiDmkfszOqpTgGrENYzZkMpAibvc7K7UxJ1S0xisnQgioMag2/PFAS0OvK62VxFI/DAqSY9Nh7sDvfp9jdYAhq3WQxs/kiqsjhRYsnKjUHX97vFJv5sdi68Yag3XxmrSsS+nW+3i8nq/Zho+wMwn5PZ1ps/zHG7XWivSmWxdhdVXuGsAYqJR8UnBFV3DtqYh4QT5by41DQG9QECfsQQK64QyUjFxlsMRLtUKt6muG6ahcwSI+YV0HrdKHZjLGk9U/izH0B+pIuEFDHtjBckUV0Obt311R3aLA8t2FMlBUPqvm+GDAS9ay4dfQtRLNerRbsCyyBK6UYm75lWgfkOG7ha4EMvq8xD2pxpUOdtNgtAIJw= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VI0PR08MB10656.eurprd08.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(23010399003)(366016)(376014)(52116014)(1800799024)(38350700014)(5113699003)(10067099003)(56012099006)(18002099003)(22082099003);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?Y2lDdk9CTmNGMHZmTVJ5WW9Ba25ORmVEbEpTNklUY0ovSnMycmYzQkxDNnhp?= =?utf-8?B?SXhaMTRyZkN5T0RBbFZJcDZ1NFgyMzAvNXNrSVNQYkc3MFV6M1cyaFFZT1dG?= =?utf-8?B?ZDVyVHZ0UmJtVTRwd3ZaM1JobWFPeG0xeHhMbW9XZzNTdDNCTUw5OWxBcGZm?= =?utf-8?B?THJSVFZlbmxYQnE3amplWkdWT3M3QVBaT0ZocmxabVRkRm9yc2dEZTVZQU1H?= =?utf-8?B?ckxpb3FrM1hPRUtEZ3MwRGs2L3I0MDVSS2d3c3dIRXFQWE9pNEcwRnd3ZFZN?= =?utf-8?B?QWU2bzJ2W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NcHpvRGdObnQwZWVTdXZpT1NmQ2ds?= =?utf-8?B?a080emRvd09kemVYWE1kTFA2RW5ZL0NlN1dFTVR3ZFhoeGJZcHU1VVVOV1J2?= =?utf-8?B?VFU1UXRwckQ2ZGg1NmJOa0R5TFQyWGNhVVJDRGFMZW5LNFY2WGQ0VHpyY2N1?= =?utf-8?B?REE3NnhCcXZOeEpiK1ZyaVBWWGxLbUYvRDh0RURpNDBPWmZCMGZGbGtXZmR6?= =?utf-8?B?R1Q1Q2gyUEthcklzVklMdkhuQTRQUUlqOTJXaUthVmk2MDdJbnRGTm9aejN1?= =?utf-8?Q?ZeBhDa1o0/6M2X8E=3D?= X-Auto-Response-Suppress: DR, OOF, AutoReply X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2eea64ad-a8e0-4b97-7828-08df1b0ee571 X-MS-Exchange-CrossTenant-AuthSource: VI0PR08MB10656.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Sep 2026 14:11:30.4295 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: gCv2CKvH2wME0zTYqRuDOwFdNlFnol55xvoBsfSO7ILcPIsPjJLkQON3nd+3ttXL2hBprVNmRqeDVpoiBF7bC02iDUU4bZxI0roA4s4ocjE= X-MS-Exchange-Transport-CrossTenantHeadersStamped: GVXPR08MB11398 Unlike cpr-exec, cpr-transfer keeps the source QEMU alive: it hands the usbfs FD to the new QEMU over the migration channel and only exits once the target is up. While the handoff is in flight both processes hold the same FD, and only one may reap URB events on it - if both do, one reaps the other's in-flight completions and wedges an isochronous transfer that cannot be retried. So reaping is handed off along with the FD: * The target doesn't reap until it takes over: it stops events right after opening the FD in .realize() and restarts in .post_load(). * The source stops reaping in .pre_save() and, since it stays alive, restarts from the MIG_EVENT_PRECOPY_FAILED notifier on failure. * On success the source's .exit() leaves the device untouched - no reset, interface release or host-driver rebind - and just closes its own copy of the FD. Signed-off-by: Andrey Drobyshev --- hw/usb/host-libusb.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 11 deletions(-) diff --git a/hw/usb/host-libusb.c b/hw/usb/host-libusb.c index 6b8c0cf0a82..742844ce426 100644 --- a/hw/usb/host-libusb.c +++ b/hw/usb/host-libusb.c @@ -110,6 +110,7 @@ struct USBHostDevice { NotifierWithReturn cpr_notifier; GSList *cpr_inflight; bool cpr_drained; + bool cpr_handed_off; libusb_device *dev; libusb_device_handle *dh; struct libusb_device_descriptor ddesc; @@ -1265,16 +1266,19 @@ static int usb_host_close(USBHostDevice *s) trace_usb_host_close(s->bus_num, s->addr); - usb_host_abort_xfers(s); - usb_host_iso_free_all(s); + if (!s->cpr_handed_off) { + usb_host_abort_xfers(s); + usb_host_iso_free_all(s); - if (udev->attached) { - usb_device_detach(udev); + if (udev->attached) { + usb_device_detach(udev); + } + + usb_host_release_interfaces(s); + libusb_reset_device(s->dh); + usb_host_attach_kernel(s); } - usb_host_release_interfaces(s); - libusb_reset_device(s->dh); - usb_host_attach_kernel(s); libusb_close(s->dh); s->dh = NULL; s->dev = NULL; @@ -1309,10 +1313,17 @@ static void usb_host_exit_notifier(struct Notifier *n, void *data) USBHostDevice *s = container_of(n, USBHostDevice, exit); if (s->dh) { - usb_host_abort_xfers(s); - usb_host_release_interfaces(s); - libusb_reset_device(s->dh); - usb_host_attach_kernel(s); + /* + * Handed to new QEMU across cpr-transfer: do not reset, release + * interfaces or rebind the host driver underneath it; just close + * our handle. + */ + if (!s->cpr_handed_off) { + usb_host_abort_xfers(s); + usb_host_release_interfaces(s); + libusb_reset_device(s->dh); + usb_host_attach_kernel(s); + } libusb_close(s->dh); } } @@ -1336,6 +1347,8 @@ static libusb_device *usb_host_find_ref(int bus, int addr) } static void usb_host_handle_data(USBDevice *udev, USBPacket *p); +static void usb_host_cpr_stop_events(void); +static void usb_host_cpr_restart_events(void); /* * A failed CPR migration resumes the source VM with the drained @@ -1356,6 +1369,11 @@ static int usb_host_cpr_notifier(NotifierWithReturn *notifier, return 0; } + if (s->cpr_handed_off) { + usb_host_cpr_restart_events(); + s->cpr_handed_off = false; + } + for (it = s->cpr_inflight; it; it = it->next) { p = it->data; /* Replay from the start of the TD */ @@ -1428,6 +1446,14 @@ static void usb_host_realize(USBDevice *udev, Error **errp) error_setg(errp, "failed to open host usb device %s", s->hostdevice); return; } + if (cpr_is_incoming()) { + /* + * The source still owns the shared fd until it hands off. Don't + * reap URBs on it yet, or we would steal the source's in-flight + * completions; .post_load() restarts events once we take over. + */ + usb_host_cpr_stop_events(); + } } else #endif if (s->match.addr && s->match.bus_num && @@ -1979,6 +2005,7 @@ static int usb_host_post_load(void *opaque, int version_id) * Skip it for CPR. */ if (cpr_is_incoming()) { + usb_host_cpr_restart_events(); return 0; } @@ -1991,6 +2018,47 @@ static int usb_host_post_load(void *opaque, int version_id) return 0; } +#ifndef CONFIG_WIN32 + +/* + * cpr-transfer: the usbfs fd is shared with the new QEMU via SCM_RIGHTS. + * Stop this (source) process from reaping URBs on it, so the new QEMU can + * drive the device without both processes racing on the same fd. Restart + * on migration failure, when the source resumes. + */ +static void usb_host_cpr_stop_events(void) +{ + const struct libusb_pollfd **poll = libusb_get_pollfds(ctx); + + libusb_set_pollfd_notifiers(ctx, NULL, NULL, NULL); + if (poll) { + for (int i = 0; poll[i] != NULL; i++) { + usb_host_del_fd(poll[i]->fd, ctx); + } + free(poll); + } +} + +static void usb_host_cpr_restart_events(void) +{ + const struct libusb_pollfd **poll = libusb_get_pollfds(ctx); + + libusb_set_pollfd_notifiers(ctx, usb_host_add_fd, usb_host_del_fd, ctx); + if (poll) { + for (int i = 0; poll[i] != NULL; i++) { + usb_host_add_fd(poll[i]->fd, poll[i]->events, ctx); + } + free(poll); + } +} + +#else + +static void usb_host_cpr_stop_events(void) {} +static void usb_host_cpr_restart_events(void) {} + +#endif + static int usb_host_pre_save(void *opaque) { USBHostDevice *s = opaque; @@ -2002,6 +2070,10 @@ static int usb_host_pre_save(void *opaque) if (usb_host_cpr_drain_xfers(s) < 0) { return -1; } + if (mode == MIG_MODE_CPR_TRANSFER) { + usb_host_cpr_stop_events(); + s->cpr_handed_off = true; + } } return 0; } -- 2.47.1