From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from DUZPR83CU001.outbound.protection.outlook.com (mail-northeuropeazon11022082.outbound.protection.outlook.com [52.101.66.82]) by lore.virtuozzo.com (Postfix) with ESMTPS id 95570807C7 for ; Tue, 6 Oct 2026 15:36:02 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=eNSzhGfqNvXK2HYB+R6Evv2XL1TKsYBBb2SRlzDeyPRssUjRDEzCvI6OLQdnqs0iRTmorGwqa0n0yruCsKXeXzNXod2XLfZx+Ij57JA2YBbnTgQ28MvWTv337ii19iyWz6olXk+/xD9+UF4AK4HY/N2XPQmmB912y/aUyD2li9EmgxBIKCgsdN/tG/bWBjIvzSv0gqkVYpQqaB5pZLBFT5GMw6xtUaaSrHgatDMbzLETjJk3g4PPk/B9FDcfCxoJEo80NHsXr4tJMUY4Xl+gvnEjI5fCFicMPvzEAOf853b3iZAzbeR1IA5kZIbwxYwsez4KRafRIM0KyckVzqFexg== 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=DG478ttgXpnBFJ5QVKbKBPvhSVai1TK+8AClKCL5w/g=; b=Q4KZHZ2iMig1o85LqqZbYfMpq/A2atCN/nHmanGLDH1IL2kdnuonIBBqk2GoPfQimDINsVrHSyzNUA7HJuWKwBKNTKcUlVbudhm13+1bYpG60UJWdwIX9I+xoQ9b2Csj3lqT0rIJfp2aowd6pGWaHn0JND5nejifCpVfASWvNknsv1My78zAl5mACvw51eCpHmU1ZspGwIOEoA9xQ3iPD2tmyTQFSKrgeRann99FBXIqGd16xcwq+B6vmgVJuLL6X2f1fZ9x8yXTs2+a1MZWlkQVuSD/I4GVVpO6//Uyx9rGQaAd3FAjIFY1EJ6l3uI+4cxCnssx62joyih1fCcyIg== 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=DG478ttgXpnBFJ5QVKbKBPvhSVai1TK+8AClKCL5w/g=; b=W+Ypg+TvkIrZYIcAUtbVcH83E97KYFusinmkcdlx4CNgLbP6WMhLyWRq/K2CVBCBvKPZ1JeZDdvWJ1HinBVxMg8pW77D05QYun7u+MzG0s4EJ4VYJX3KbW+f7bAf5t1XY8eCwvp+OiTwASrYTaOU141aa35WtyzQ8DCD/gi6/I4N71hUU/z6ilE6AQa4apxrhCSKzreI6tPmyNwF2p0kcHzHVJ9KVkZX/WGRNO6YyOZokWEzPYZxPM1/JQTsP/qPLKMDEHiK4i5VzvEfXzIBycejp1pGROeleSOvI9XKxyL/HBu4FVOQdoNCKxYiOpZ8n55WxklaZo43ovgXdC+Cww== 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 DU0PR08MB9774.eurprd08.prod.outlook.com (2603:10a6:10:445::18) 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 15:36:00 +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 15:36:00 +0000 From: Andrey Drobyshev Date: Tue, 06 Oct 2026 18:36:01 +0300 Subject: [PATCH QEMU HCI-8.0 v7 10/14] 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-v7-10-044f668789a1@virtuozzo.com> References: <20261006-usb-host_v3-v7-0-044f668789a1@virtuozzo.com> In-Reply-To: <20261006-usb-host_v3-v7-0-044f668789a1@virtuozzo.com> To: svt-core@virtuozzo.com Cc: den@openvz.org, andrey.drobyshev@virtuozzo.com X-Mailer: b4 0.16.0 X-ClientProxiedBy: VI5PEPF00000985.AUTP296.PROD.OUTLOOK.COM (2603:10a6:808:1::81a) To VI0PR08MB10656.eurprd08.prod.outlook.com (2603:10a6:800:20a::12) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VI0PR08MB10656:EE_|DU0PR08MB9774:EE_ X-MS-Office365-Filtering-Correlation-Id: d46c2996-b08a-4a93-441b-08df23bf8599 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|23010399003|1800799024|366016|376014|52116014|5113699003|56012099006|10067099003|22082099003|18002099003|38350700014; X-Microsoft-Antispam-Message-Info: zC/XhhD8yVx+Gpil3rT6iaRH764unMW0DpjcTLChSbMZ7EIq3Sssh/lqlTA+zn7lNAzjfkCKC9y+1wtK7oojs8vJ2wtaOur6CKh7OQTnAqDJ9BqU6QXZkt8rozVDKw1p574UcjBivywCUYp61jQuAghsSDaKevARVPzOo0nEzqjd7N8Ktp4DvuAu585Ar73VTTR5tt9kZyoEzqfrC5CWjm6ov0uIqxhjkJqGFN5Mm4HRpdA8dmn553DEUUYeoOu1to/hlwkHNTkIOE7ECT+EQS6mJI5Rds/6eFpMwiJJroreXvKU3sWCu3Jm8eCLjnHYpn20wECRPc5dMlJmc6W3wFfyTSZyGh1QrQ5ldJNfkJG+7/bsQE0nAqWErU5tqdhhcmVNxTFg4K9BBeOOqwcowyro4rHjGGYcUXbr97o0nifu6ce17LTIp1K/JD4do0CEu+K1mORdycrtSH7tBpyl438uzzjGA9tESbHP0bzqcY0U3eX19EkCK+lpgvVilb6oI0oe3N0E2ue3tDT+03HhW16Axpm5ITBYhazphFFlYhPbK3yToWWWxGcg767Uct/CzRSbJym10OzJsmyHdTkvSOzocrkn2SRt4IH/9UuhIzRQCOSifSzLftmlFVPU7/vAOuBQiHcmYWPqfzwtXte1p3fEN8DfhoA6LQTz3g1zvKCvLtld9nz9xpezmZ3MRrzHju1ssRRyGL1RufEaHqOqm42W9oJKuieRpOhZzMZ1nbE= 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)(1800799024)(366016)(376014)(52116014)(5113699003)(56012099006)(10067099003)(22082099003)(18002099003)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?VFg5TlVhbXJQR1FaZ1Z3VndtQkhxQ2JidTQ5VVkwTEJIenRlbWNxZUV0VnIv?= =?utf-8?B?WVdJQTNCQ25QRjZxOHN1QXRCYm00UmtTZ2pYdnppYU5XcmRlVUY3SlBPbGZK?= =?utf-8?B?UkJ2eWlnUEpIQTZNdG1HTFQxS29OUE43aGd3Y0FTR2FZdE1uaU9oQ2RkekJ5?= =?utf-8?B?Kzd1a2pFWHp5ckJvTDBhSDFRSWhzR2l1bTlpUGYvanluNjVDeS80S1ZoNkNM?= =?utf-8?B?ay9YOW1JSlZLSzdpb25PVFRRcGEzaXNUMEhXcEI4VURhdDIwNEtkbVRUVzZt?= =?utf-8?B?czIrTW1QOXBmOVd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cGZ3WDZMTUt5RllD?= =?utf-8?B?aFlkU3F0Mk5aVlRCSzUvOW40R0ZQTTN6T0ptMG5Rb2NXMHlQbGh4RXRNTW1Y?= =?utf-8?B?YnZML2NZZk1nUktQTDVKcGFhRkUyOUFNMHFYOHlaVGxkall0TjF2cFNxbGVr?= =?utf-8?B?bWwzYlpiUS9FUE56Zm5QVEVTcElCb1A4ZmJOV3pmMUxITmNNdVJYN0lqa1lF?= =?utf-8?B?eU4wQXRLeWw3b2FBd0duYlNOSlA1ZE5aRlFVY2gzUjVVQkNqTzRMaFYyV2U1?= =?utf-8?Q?8YbpU1Hx1a4ql/Ko=3D?= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-Network-Message-Id: d46c2996-b08a-4a93-441b-08df23bf8599 X-MS-Exchange-CrossTenant-AuthSource: VI0PR08MB10656.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Oct 2026 15:35:59.8407 (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: UyH27szuoGDlrIrem0BprLJ2+jOM2HnSJ2q5E2gY+wR6hrcmqHbqSRNEc9rVzjj81ZQCXGSXem9MNsYu8NIe0XncYHBIuVCAa9Y1Sbol2MQ= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU0PR08MB9774 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. Reaping is stopped and restarted on the whole libusb context, which is shared by all usb-host devices, so cpr-transfer currently supports a single hostdevice= usb-host at a time. Signed-off-by: Andrey Drobyshev --- hw/usb/host-libusb.c | 80 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 12 deletions(-) diff --git a/hw/usb/host-libusb.c b/hw/usb/host-libusb.c index 09ffb5d48f7..387040d6e1a 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; @@ -283,6 +284,23 @@ static void usb_host_start_events(void) } } +/* + * cpr-transfer shares the usbfs fd with the new QEMU, so stop reaping URBs on + * it here -- only one process may drive the device. + */ +static void usb_host_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); + } +} + #else static QEMUTimer *poll_timer; @@ -306,6 +324,9 @@ static void usb_host_timer(void *opaque) usb_host_timer_kick(); } +static void usb_host_start_events(void) {} +static void usb_host_stop_events(void) {} + #endif /* !CONFIG_WIN32 */ static int usb_host_init(void) @@ -1272,13 +1293,18 @@ static int usb_host_cpr_drain_xfers(USBHostDevice *s) /* * Quiesce the URB data plane around a CPR switchover: drain the in-flight * transfers before the device/irqchip snapshot, so completions raised during - * the drain are migrated too. + * the drain are migrated too. For cpr-transfer, also hand reaping of the + * shared fd off to the target by stopping events on this side. */ 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_stop_events(); + s->cpr_handed_off = true; + } return 0; } @@ -1297,6 +1323,11 @@ static void usb_host_cpr_reissue(USBHostDevice *s) return; } + if (s->cpr_handed_off) { + usb_host_start_events(); + s->cpr_handed_off = false; + } + for (it = s->cpr_inflight; it; it = it->next) { p = it->data; /* @@ -1371,16 +1402,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; @@ -1415,10 +1449,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); } } @@ -1487,6 +1528,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_stop_events(); + } } else #endif if (s->match.addr && s->match.bus_num && @@ -1550,6 +1599,12 @@ static void usb_host_unrealize(USBDevice *udev) if (s->needs_autoscan) { QTAILQ_REMOVE(&hostdevs, s, next); } + /* + * An explicit device_del means we still own the device (a successful + * cpr-transfer handoff exits the process instead), so clear the handoff + * flag to force a full reset/release in usb_host_close(). + */ + s->cpr_handed_off = false; usb_host_close(s); migrate_del_blocker(&s->cpr_blocker); if (s->cpr_vmse) { @@ -2033,6 +2088,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_start_events(); return 0; } -- 2.47.1