From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from AM0PR83CU005.outbound.protection.outlook.com (mail-westeuropeazon11020077.outbound.protection.outlook.com [52.101.69.77]) by lore.virtuozzo.com (Postfix) with ESMTPS id C5DA680688 for ; Fri, 25 Sep 2026 14:11:30 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=FO9Ani39SGoD4QtPSNRvFIo5o3UCeZYMwFZ/aj1bMU5G+NTGQSwaMNgc9J8SYyTmbHC7VA3djEw0/WsdItnziz3zd1NRGruBqEqxwRZYQjjR2+8zy6LpPC6vfClDeE/h5RRD/xdCOPgZmgxAYAOBjiCRDtYTNczdB9aC3/pqOOhnQnf1izL+giHUF5+JlQO7E5zhAug7pHELYD2c0XMwSd150t3+09O1V1uiQx9//kflmAm3zKTjuZAkbIhCQCHoXkAYYvoqW6nM5WzcVBOTJcxdpPmXzNrJn2uX+WKrUavmkz3LekyDWLUDhtCCGsSXdHQ41rME3AEs1WbRG+0uIw== 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=6g1FZ62MGOuc1YBFbveg3ba/Vq4qqCo1HDTj9O5eYJQ=; b=AVINq4/yzRPWAu39xP5aOR/Ue/MA8XX91uzo85pxdsOKrBfU7QQjSfo9No3SFJKzYdwA6fKT6RD/G4aQ3E1KQ87aGtIC+VFu6pMvNpbP/jrKBQinbKztBAsZn8UEv9WtgAFYDuk242lwg91MZ7iXpAsIV2rYjUKP1g42CAaXKNQahCMcCtnGD8/xSvPAlKYtLOTzY0h3upYRWXBKHnSF3L4ps5MrXgGXDkRHCwhamzYDtqjytCOAswmsLcaeZtq3Aki8zoN6X32yTU6juEApY/wG5HcxfMI9MZopPu/cAO68oEt+XUXw3ntcveHbciEQXtQZI/U060PZSrLXtgIV4w== 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=6g1FZ62MGOuc1YBFbveg3ba/Vq4qqCo1HDTj9O5eYJQ=; b=bFGE2WdDaNIxzgpPGOaLxTgr8VYiZqJczaQkHCV5pt3iq0oQYSx0oFIHuQ7nsIZV0TIMtZhGkR2kXfeVFZwXSPbB7b4gTkuaYegSNsE4sf3QI+BahXfgcVRzM9lMHeV0wXfFrxMMSl68XRLAV6NVmkISeDeJJvbnmjug69m3w0aKkyzccSUyh44EVz73eiOE3uq5WROnPCjlwWJ5nvwyCcBuTufiN5Dkxd3FE708fQkDP2CnXbJ1Fn0L9g9pdolWAAhmG5XXArKb9LtbOjiyehA0djAg0k38UcvSbR+toG0f1nf+Te5FRhOdQ0jx2l9QDH6ztwQN+3jTt1qa6k9D/Q== 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 AMDPR08MB11336.eurprd08.prod.outlook.com (2603:10a6:20b:716::15) 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:28 +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:28 +0000 From: Andrey Drobyshev Date: Fri, 25 Sep 2026 17:11:14 +0300 Subject: [PATCH QEMU HCI-8.0 v5 05/13] usb-host: drain in-flight URBs across CPR #VSTOR-137800 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260925-usb-host_v3-v5-5-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_|AMDPR08MB11336:EE_ X-MS-Office365-Filtering-Correlation-Id: 75b6b075-1612-4f97-babc-08df1b0ee475 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|376014|23010399003|52116014|366016|1800799024|10067099003|56012099006|5023799004|5113699003|6133799003|18002099003|22082099003|38350700014; X-Microsoft-Antispam-Message-Info: CSoAh7fYZpTgTAxfZEo+Pm39TZ/Da/8cF6aYSmfaMaxbDwdJFfkX40o4IlRdS/4n5ZsQqwVwlkglMfKwwjl6/XRenUb7RARc24Y6MNm/kRbLxSjWDBfw6XKXNDqeEWYCFsb6UYVgQyBBTl7whgEs014r3/DUETfhpQlV4D/+R3Nfa6RD7Y4LkSfPPS6b9yyyk68GVWUW5/f2JZNc8tKLtSvHb2tInx/K7FDzzpmWf1wcA+bWOLqWtQj7LQLFjP/Z1a7mesB/h86Q6twf1XYd3VC7kuvJgFiAJGGXS8fQA2LpKRl25xD9EvE09mIr5lWYN2ns/XPqqWQVC1PqXnFAWiaxz0JiqcQFgHVVRjG2AP1J+u6Fv4rpSD1KvhcdMxjPwk5ffNcUli2k7142TyxnXh6TJCT9lKczbHVcRdYKbxtpxZUwLOjfVqze440bp9x4n4qm1GsWVYaPooUvChyfvCIdjwdumIiDs+2Yyf6Tf3QsOpU2WHeQ/4m9ckFeQcAgOc387Qg+Ueb/a9h2uG/B5EQ64NA1rUfoYWCsR2y83kmSMs4/k4qiCjYK7PkvCxSdxCvQC2xTDiHZkiUK9uowd5zdkA9uUjefbe1WmUlZ5ObK+Wrf9SXT4UJjWJS0kR9aFF8w0w9afOG6p1824NIlnqxVCfmIh+MQJVaxnPPegKHLDQmflhe5q3vXSxNFWrSHP6/99v4gD34dsv6IjiW0TQ== 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)(376014)(23010399003)(52116014)(366016)(1800799024)(10067099003)(56012099006)(5023799004)(5113699003)(6133799003)(18002099003)(22082099003)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?Z0VHVFUyNk5jOHQ1ZXRSWmZRYlU1eldYL0NWd1dRdkN6bDNQTHBoeU9iWEYv?= =?utf-8?B?dzRtS0dlMHJQdXRFTnN6WVlFb1VSemJJSHFKZjB1Q2NMekZHL0pra3NrcUxm?= =?utf-8?B?Q2RtTUZJci9lNnFVT01EQTJaM0doZWh3cUhNWWdHYjJQU2diL05uWG1OOHdn?= =?utf-8?B?N2RTMC9uMlRoajN2a2FQVGNVb0xnSndnc2N6a2lNWWdhTTlHV0RQc3ZBZVVv?= =?utf-8?B?SmZTdVlkQWZZY2ZlUis0SVdyVG01bHhwSG9WQnZ6bDRMVn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dTEyZzJadW5NTlZ3UE5HMkl4YzY5NnZaYks2ekZtNzh6bURKWUV5aGsv?= =?utf-8?B?V0pDNVVucXlRMW85aUVxdVVucnNIa0J0dU9iNnQ2L0JiSGF4UStBWHhka3Fl?= =?utf-8?B?TGE5SU5PQS9TK056aXI4RVQwdVgwaWVWajRWOWh6eGUzZ0lOUllRWXJMd1pY?= =?utf-8?B?THQrV0xRNXJzRXJXOStWSnJEKzZScjBQTFM0c1hEYWZRbVl0Z3hZVUZjZTUx?= =?utf-8?B?V0NqRVBYNkJUdy9YODBBSitXdFJON3Y4UmNTdzJMdjhrRzlxSDdZeFVHeVpk?= =?utf-8?Q?UOs9n3CFJrGk5Euo=3D?= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 75b6b075-1612-4f97-babc-08df1b0ee475 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:28.7233 (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: 31twfZ5a5PhNG7L0zDmI76rEkDh8drzTUNwJtxGhOlGp473ZVaw/l9VqhOzcfqDHkXiaGFGjlRnUuLSJbzTbthgf7jRVmMbjPcCm+oIVAjs= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AMDPR08MB11336 URBs submitted by the old QEMU must not outlive it across the switchover: the kernel would complete them into buffers of an address space which is gone after exec, and the new libusb context would reap URB pointers it never submitted. So, in .pre_save() for the CPR modes, cancel and reap every pending transfer. One that already completed in the kernel is delivered normally - its data is real and the guest must see it. One still in flight is suppressed: its packet stays ASYNC and its TD stays on the transfer ring, so the target re-executes it from the start of the TD when usb_xhci_post_load() kicks the endpoints. Completing a transfer can make xHCI queue the next TD, so keep cancelling until the request list stays empty. If the device won't release its URBs within the bounded wait, error the still in-flight packets out -> the resumed guest retries them -- leak the requests, since libusb still owns their transfers, and abort the migration. Isochronous URBs are not on the request list but on their own rings, and freeing them while in-flight would leave them queued on the preserved FD. Cancel them so they are unlinked and reaped: an iso URB in flight when the guest stops may never complete on its own. The target reconstructs the iso stream from the guest's transfer ring. Signed-off-by: Andrey Drobyshev --- hw/usb/host-libusb.c | 158 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 135 insertions(+), 23 deletions(-) diff --git a/hw/usb/host-libusb.c b/hw/usb/host-libusb.c index 9b70d6deef9..ebad04ec305 100644 --- a/hw/usb/host-libusb.c +++ b/hw/usb/host-libusb.c @@ -48,6 +48,7 @@ #include "qapi/error.h" #include "migration/blocker.h" #include "migration/cpr.h" +#include "migration/misc.h" #include "migration/vmstate.h" #include "monitor/monitor.h" #include "qemu/error-report.h" @@ -403,7 +404,10 @@ static void usb_host_req_free(USBHostRequest *r) #ifdef CONFIG_WIN32 request_count--; #endif - QTAILQ_REMOVE(&r->host->requests, r, next); + /* Skip if a timed-out reap already unlinked it (avoid a double remove) */ + if (QTAILQ_IN_USE(r, next)) { + QTAILQ_REMOVE(&r->host->requests, r, next); + } libusb_free_transfer(r->xfer); g_free(r->buffer); g_free(r); @@ -430,6 +434,9 @@ static void LIBUSB_CALL usb_host_req_complete_ctrl(struct libusb_transfer *xfer) if (r->p == NULL) { goto out; /* request was canceled */ } + if (xfer->status == LIBUSB_TRANSFER_CANCELLED) { + goto out; /* CPR drain: the target will replay this packet */ + } r->p->status = status_map[xfer->status]; r->p->actual_length = xfer->actual_length; @@ -480,6 +487,9 @@ static void LIBUSB_CALL usb_host_req_complete_data(struct libusb_transfer *xfer) if (r->p == NULL) { goto out; /* request was canceled */ } + if (xfer->status == LIBUSB_TRANSFER_CANCELLED) { + goto out; /* CPR drain: the target will replay this packet */ + } r->p->status = status_map[xfer->status]; if (r->in && xfer->actual_length) { @@ -500,22 +510,29 @@ out: } } -static void usb_host_req_abort(USBHostRequest *r) +/* Fail r's in-flight packet with an error status and detach it from r. */ +static void usb_host_req_complete_err(USBHostRequest *r, int status) { - USBHostDevice *s = r->host; - bool inflight = (r->p && r->p->state == USB_PACKET_ASYNC); + USBHostDevice *s = r->host; - if (inflight) { - r->p->status = USB_RET_NODEV; - trace_usb_host_req_complete(s->bus_num, s->addr, r->p, - r->p->status, r->p->actual_length); - if (r->p->ep->nr == 0) { - usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p); - } else { - usb_packet_complete(USB_DEVICE(s), r->p); - } - r->p = NULL; + if (!r->p || r->p->state != USB_PACKET_ASYNC) { + return; + } + r->p->status = status; + trace_usb_host_req_complete(s->bus_num, s->addr, r->p, + r->p->status, r->p->actual_length); + if (r->p->ep->nr == 0) { + usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p); + } else { + usb_packet_complete(USB_DEVICE(s), r->p); + } + r->p = NULL; +} +static void usb_host_req_abort(USBHostRequest *r) +{ + if (r->p && r->p->state == USB_PACKET_ASYNC) { + usb_host_req_complete_err(r, USB_RET_NODEV); libusb_cancel_transfer(r->xfer); } } @@ -1096,9 +1113,18 @@ fail: return -1; } -static void usb_host_reap_xfers(USBHostDevice *s) +/* libusb still owns cancelled-but-unreaped transfers: unlink, don't free. */ +static void usb_host_drop_requests(USBHostDevice *s) { USBHostRequest *r, *rtmp; + + QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) { + QTAILQ_REMOVE(&s->requests, r, next); + } +} + +static void usb_host_reap_xfers(USBHostDevice *s) +{ int limit = 100; while (QTAILQ_FIRST(&s->requests) != NULL) { @@ -1108,15 +1134,10 @@ static void usb_host_reap_xfers(USBHostDevice *s) libusb_handle_events_timeout(ctx, &tv); if (--limit == 0) { /* - * Don't wait forever for libusb calling the complete - * callback (which will unlink and free the request). - * - * Leaking memory here, to make sure libusb will not - * access memory which we have released already. + * Don't wait forever for libusb to call the complete callback + * (which unlinks and frees the request). */ - QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) { - QTAILQ_REMOVE(&s->requests, r, next); - } + usb_host_drop_requests(s); return; } } @@ -1132,6 +1153,81 @@ static void usb_host_abort_xfers(USBHostDevice *s) usb_host_reap_xfers(s); } +static bool usb_host_iso_inflight(USBHostDevice *s) +{ + USBHostIsoRing *ring; + + QTAILQ_FOREACH(ring, &s->isorings, next) { + if (!QTAILQ_EMPTY(&ring->inflight)) { + return true; + } + } + return false; +} + +/* + * Quiesce the device for a CPR switchover. Cancel and reap all URBs, + * so that no URB submitted by this process can outlive it. + */ +static int usb_host_cpr_drain_xfers(USBHostDevice *s) +{ + USBHostRequest *r, *rtmp; + USBHostIsoRing *ring; + USBHostIsoXfer *xfer; + int limit = 100; + + /* + * Cancel and reap every URB. Completing a reaped URB can make xHCI queue + * the next TD, so re-cancel whatever reappears until the queue stays empty. + */ + while (!QTAILQ_EMPTY(&s->requests)) { + struct timeval tv = { .tv_usec = 2500 }; + + QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) { + libusb_cancel_transfer(r->xfer); + } + libusb_handle_events_timeout(ctx, &tv); + if (--limit == 0) { + /* + * The device won't release these URBs. Error the packets out so + * the resumed guest retries them, leak the requests (libusb still + * owns their transfers) and abort the migration. + */ + QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) { + usb_host_req_complete_err(r, USB_RET_IOERROR); + } + usb_host_drop_requests(s); + return -1; + } + } + + /* + * Iso URBs are on the rings, not s->requests. Cancel them: one in + * flight when the guest stops may never complete on its own, and none + * may be left on the preserved fd. The target replays from the ring. + */ + QTAILQ_FOREACH(ring, &s->isorings, next) { + QTAILQ_FOREACH(xfer, &ring->inflight, next) { + libusb_cancel_transfer(xfer->xfer); + } + } + + /* + * Cap the reap at 2x a full ring: + * iso_urb_count URBs x iso_urb_frames packets each + */ + limit = 2 * s->iso_urb_count * s->iso_urb_frames; + while (usb_host_iso_inflight(s)) { + struct timeval tv = { .tv_usec = 1000 }; + libusb_handle_events_timeout(ctx, &tv); + if (limit-- == 0) { + return -1; + } + } + usb_host_iso_free_all(s); + return 0; +} + static int usb_host_close(USBHostDevice *s) { USBDevice *udev = USB_DEVICE(s); @@ -1807,10 +1903,26 @@ static int usb_host_post_load(void *opaque, int version_id) return 0; } +static int usb_host_pre_save(void *opaque) +{ + USBHostDevice *s = opaque; + MigMode mode = migrate_mode(); + + /* URBs submitted by this process must not outlive CPR migration */ + if ((mode == MIG_MODE_CPR_EXEC || mode == MIG_MODE_CPR_TRANSFER) && + s->dh) { + if (usb_host_cpr_drain_xfers(s) < 0) { + return -1; + } + } + return 0; +} + static const VMStateDescription vmstate_usb_host = { .name = "usb-host", .version_id = 1, .minimum_version_id = 1, + .pre_save = usb_host_pre_save, .post_load = usb_host_post_load, .fields = (const VMStateField[]) { VMSTATE_USB_DEVICE(parent_obj, USBHostDevice), -- 2.47.1