From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from MRWPR03CU001.outbound.protection.outlook.com (mail-francesouthazon11021138.outbound.protection.outlook.com [40.107.130.138]) by lore.virtuozzo.com (Postfix) with ESMTPS id 5512D8017D for ; Tue, 6 Oct 2026 10:34:44 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Nl1MMf+RddGeiN2a5NMkQOyLoqT1kODGb7ls65pR037KrNfVAPVbS8RbYMoJBZFdJGRxTriPfZVYDFM4mrMma785yfSCCY5fDQ53tPTa+BDAgXcMaWz2su2J6jbfDamE2QETGgKgHEDCTVLQE8Mih+TiREMb5HAZuWWb26KxzjaKoBsVTkLJUaXeJ6PjdytbqMDuu7SPDyeJuBHz0ioFNGUvKxw6wjfEy03CK5vRNM5pDHrZ+lqs13pEiEKdmiw1+4db6umJz/nulImoOjp39PhxGo70ZzP7DYeSY7LJsr5wpaBs/HliN7fKic+mUhsYgrNzz5TZcoyzO1sKDhkvrw== 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=bobvqUv2uBP4x1wME1Ql41BT8kEHARBYTG3I3Vb0l+0=; b=k72fPwPCJI3FNPbh9mCFDRKO5FfBIMuHflrKlnbC1rO1hDsfNlYtAdkzt6JyKkAoG/5oBPHEfxtXavzhwF83KXODZUhmB4BHCx3WCgtj8e+B5288Cyc3KFg/1XN5rWEqfwTrqJZBSksak6qkH5ALPuZ1fNd+nmCmI5dr6IVPBJywubtG11bymHSKwPz1oDBTxY0lAVZEs/ZxcxwE8rp/KPVW/Vx69oNF4TivKBWE9Dx0fPXrPtwuUpqug6pJXsAoeBdlEsgQ6lQp4FDDUrFbxIjZ6xLCSRVTu8fEB/RVDKDgzctNIU8t91YFGs5ULA23VvQ9Dw93Jp7NyYqe1u6Ttg== 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=bobvqUv2uBP4x1wME1Ql41BT8kEHARBYTG3I3Vb0l+0=; b=ow7U6XD4bvnorNUI6zTaqJYNb2N18eE9vvsgVbc1xsR3UhZtRRlvgoHK0P5SvYQrlY8V/0aVgqDR8fltcBj/gblWlGXy8PAlX52eOjYpGKJmyX7jD+wQ0Bf1xArY2j+gUxA037uUIb4OZHGNlbd3ZBudfnHprwa3Ad2KRHCF5sPm1n83RqjqaBW6okfE1Em5piftNHFERuOma6IVysHFankX8dNyQUTzmWRB+EA8KApH7rcI+GCimtM/syhr88SMEnSjGeIZel5gWM5NtK4Bjxpzy+cls159vlW3ySBATmp2Ak9/P3+jDQle5RWSY3o2raIpH0IAI7GXPqmKs1eXVA== 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 GV1PR08MB8692.eurprd08.prod.outlook.com (2603:10a6:150:86::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.496.14; Tue, 6 Oct 2026 10:34:37 +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.0496.010; Tue, 6 Oct 2026 10:34:37 +0000 From: Andrey Drobyshev Date: Tue, 06 Oct 2026 13:34:32 +0300 Subject: [PATCH QEMU HCI-8.0 v6 08/12] usb-host: hand off the device across cpr-transfer #VSTOR-137800 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261006-usb-host_v3-v6-8-8688ec9576d1@virtuozzo.com> References: <20261006-usb-host_v3-v6-0-8688ec9576d1@virtuozzo.com> In-Reply-To: <20261006-usb-host_v3-v6-0-8688ec9576d1@virtuozzo.com> To: svt-core@virtuozzo.com Cc: andrey.drobyshev@virtuozzo.com, den@openvz.org X-Mailer: b4 0.16.0 X-ClientProxiedBy: VI1PR09CA0115.eurprd09.prod.outlook.com (2603:10a6:803:78::38) To VI0PR08MB10656.eurprd08.prod.outlook.com (2603:10a6:800:20a::12) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VI0PR08MB10656:EE_|GV1PR08MB8692:EE_ X-MS-Office365-Filtering-Correlation-Id: 82b184f3-2d2c-4f01-5e98-08df23956a07 X-LD-Processed: 0bc7f26d-0264-416e-a6fc-8352af79c58f,ExtAddr List-Id: svt-core@virtuozzo.com X-MS-Exchange-Transport-Rules-Loop: 1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|52116014|376014|23010399003|1800799024|366016|10067099003|56012099006|5113699003|22082099003|18002099003|38350700014; X-Microsoft-Antispam-Message-Info: vafuEGMABEqNAUdAe74uVfIXcR9Ol5h6hf/hT8BPKAcubc1znV4cB2aR03IEt1JmNsXsCEeaOkHh1ymfC9lA17XHh4KstZgkhrH+4eA+tfvAdlGOKtGsee5XsWi8Zqvg5ZjytpH9CO60qGb9wosvxCUsNfKj/Jjcc/b0ZtCBiv3QLx68G3fySzw8QTeT5UGgPC0mJocmtLe84jsvc1wgMk6RBai964IkZheuv1FaLzto4W0wMxBCR9zUsFJWxuk2g7gB4cJbIIrDByJOH2y7sQkbBl6Jf3pHCKqJXOBUb0H4JT2rRfmWSqEOA5u8zdDwfKoQCgeqvw0mOwNHNC8YkG3bsVFRCyoY5HHnlx9MFqf2O/75Af7i6+RP8YmSf5luoIflhgfizAbA3uVrzq6QcJS2wKpLAwpSFEXGNlcjUMYt5Eicws5kyDlystKkve8cd1ps67rRP54K0XAKoxPzZXsH3L/ZVYknLkh5r4vzFRBfph77cN8ZEYlu9cB+Uo8FDNzSl3O5W3MXoKqJGP5gT54QMZCv/RL0t0sVmPR9AWQv3CWVtGkIqNxzyxf1N+vHxOc5ZmpA0sFtysVkobRensDiicaB2G9gvot+nmMrGSu7P0PV5WmAd6J0BKo0o9XFLlMZC2qEFABKc3fRwcm2ulsodQn9SvInr6bGyaQbMc4Q8KZSz22zTv9Tf4WHBKOawXYWylIA2UiL0kq/7ibQ0eIdKRBDjcBkWGtgODRv7x8= 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)(52116014)(376014)(23010399003)(1800799024)(366016)(10067099003)(56012099006)(5113699003)(22082099003)(18002099003)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?M1ZFVVlaVENTV3NRQmFrN0hPVWxZVlUrSXowTFQ3c2hJUDZ5R2Q4VFBmL2Iy?= =?utf-8?B?aUFmSzVuWGU0TSsyRFN0WUxybnFmKzJwNkhUYks1YUFlUDZrRE53a1J1aWVo?= =?utf-8?B?cDNXd05YWHFKZzZzTHRadlJFamRGcnduVWwwMFpGeVFiR0Y5M2xUMzY4M3U2?= =?utf-8?B?REJmVjhwUFFwS0IwRXRFekc5VlpUSVZqcjdjVzJWOGZwVFZPcmRvUkVRZG1C?= =?utf-8?B?aXBXajYrM2tpUi9aWnBxS3YyY0xmTDY3WjB4MVYzMEhuWGxjY0d3cU1Wc3ND?= =?utf-8?B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cVFFK0M5d1RXM1Y0UG1OWmpPempWSGpYajlP?= =?utf-8?B?aXFNdEFtRzE2Y0xNdDBXSWJzNlFkUU91eWREbDlVTUQ1dG5GN1dzVHk1dHVB?= =?utf-8?B?V2s4R2ZrRkZhajVJcFEyUytxUzV4cUtBaTQyTThQUGwraW9rZ2czSEVzWnZv?= =?utf-8?B?d3RWT2VEOERIakhvMXJwcnVXRlJlUzFnL0FUMXNIOXhOb2hraHFDeUJjMnQ5?= =?utf-8?B?TjhNOUtzMFRqMTBHSzdHb2R2WWNJcjFQUTRBeW1iRnp4d0dSTjRkbTlDMFE5?= =?utf-8?Q?tMB1rB70zJJ8Seg0=3D?= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 82b184f3-2d2c-4f01-5e98-08df23956a07 X-MS-Exchange-CrossTenant-AuthSource: VI0PR08MB10656.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Oct 2026 10:34:34.7129 (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: isDQLHEfEwqwTG7q3TgLWj1vOml4nt/hnZt0Y5MXHGcGp8JXyRLk5b5mqD7NaVwqXEsEf91olQSCntaJFcFxhGI9tpzE/3quPSx+UCbN0vg= X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV1PR08MB8692 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 reaping from the vm-state handler's resume branch 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 | 109 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 96 insertions(+), 13 deletions(-) diff --git a/hw/usb/host-libusb.c b/hw/usb/host-libusb.c index 020a685aa41..492f0442974 100644 --- a/hw/usb/host-libusb.c +++ b/hw/usb/host-libusb.c @@ -127,6 +127,7 @@ struct USBHostDevice { GSList *cpr_inflight; bool cpr_drained; bool cpr_replay_all; + bool cpr_handed_off; /* request queues */ QTAILQ_HEAD(, USBHostRequest) requests; @@ -1260,6 +1261,23 @@ static int usb_host_cpr_drain_xfers(USBHostDevice *s) } static void usb_host_cpr_reissue(USBHostDevice *s); +static void usb_host_cpr_stop_events(void); + +/* + * Drain the device for a CPR switchover and, for cpr-transfer, hand reaping of + * the shared fd off to the target. Returns -1 if the drain couldn't complete. + */ +static int usb_host_cpr_quiesce(USBHostDevice *s, MigMode mode) +{ + 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; +} /* * Quiesce the URB data plane around a CPR switchover: drain when the VM stops @@ -1285,7 +1303,7 @@ static void usb_host_cpr_vm_state(void *opaque, bool running, RunState state) if (mode != MIG_MODE_CPR_EXEC && mode != MIG_MODE_CPR_TRANSFER) { return; } - if (usb_host_cpr_drain_xfers(s) < 0) { + if (usb_host_cpr_quiesce(s, mode) < 0) { migration_file_set_error(-EIO, NULL); } } @@ -1300,16 +1318,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; @@ -1344,10 +1365,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); } } @@ -1371,6 +1399,7 @@ 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_restart_events(void); /* * If CPR is aborted the source resumes with the drained packets still owned @@ -1387,6 +1416,11 @@ static void usb_host_cpr_reissue(USBHostDevice *s) return; } + 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 */ @@ -1455,6 +1489,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 && @@ -2000,6 +2042,7 @@ static int usb_host_post_load(void *opaque, int version_id) * rescan in usb_host_post_load_bh() -- those are for cross-host migration. */ if (cpr_is_incoming()) { + usb_host_cpr_restart_events(); return 0; } @@ -2012,6 +2055,46 @@ static int usb_host_post_load(void *opaque, int version_id) return 0; } +#ifndef CONFIG_WIN32 + +/* + * cpr-transfer shares the usbfs fd with the new QEMU, so stop reaping URBs on + * it here -- only one process may drive the device. Restart if migration + * fails and 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; @@ -2025,7 +2108,7 @@ static int usb_host_pre_save(void *opaque) if ((mode == MIG_MODE_CPR_EXEC || mode == MIG_MODE_CPR_TRANSFER) && s->dh && !s->cpr_drained) { s->cpr_replay_all = true; - if (usb_host_cpr_drain_xfers(s) < 0) { + if (usb_host_cpr_quiesce(s, mode) < 0) { return -1; } } -- 2.47.1