From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from AM0PR02CU008.outbound.protection.outlook.com (mail-westeuropeazon11023095.outbound.protection.outlook.com [52.101.72.95]) by lore.virtuozzo.com (Postfix) with ESMTPS id E7280807D1 for ; Tue, 6 Oct 2026 16:28:04 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=P23FnXAYzyxyTdn6bdmtw1ROOFqYN7ySjhOQOzJQCCbXkloPSwUYeFPHxhmXcvYjrYeG+42T7W92XdSeDZ+OUCTkoR/03s+nh2/AG6ca+9eurFY/1rWS8WhpE7Nc4Kpt/yITmEZGO9ak2um4c+qMNomKcL0XmzPmqtsUyCtVuqnDFyFPGun0L/Un2Tb4sk8mKCSeRXerWyPJ07ZRzi4JWpc2usx4bLpFtIKGlKp+m/++5/RLxtC7d//j/gMcWcP0wfjX0NXno3iZFKtUc95wrTEAWgYLr1JZLCm6DDJXf6HQmQnw0rxdciztficDx52ow7FRRKjURQ2dURoayH6UOg== 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=4ydpngSSu7l8NKESBCkC/5mwylX5oLX0W5H1+ATqcaY=; b=hNAMY9issZbXWtOWHdh98UTFupvE9Wfmm9TgkGICuOaah88u90d6zAFhoWh3IICwS2araJ3xsS4AKmsWhtVrT/vOn8ckKCIvLD8VoptAwOxILAMzH8/6XEiJEQ5nfOsZXw69O0Rt8fYDegA75clWO+aIAv9qx7BNqQ9hYlyimQSkFKILpZXpFjKm306GZIXRuZi5PlhvgcE+P5tL3z1SDhPkVzTA/Spqn+wNQ61GU0nQ9NhkjOdWA2AD/aJOj/dAaZB5L+EAvgOFVD6jZ5Fvt3yTUHuvmC8AATwCftF7dHluJxuGmiwX5QTdDx8vBzA8PAQQGWnvlHQIHm5S6O8rAg== 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=4ydpngSSu7l8NKESBCkC/5mwylX5oLX0W5H1+ATqcaY=; b=jcEtLmdSR5HYg/f/ta+hdUcg23TVD9jWvdSZTkGu2qyccfUQzjcX395iv6x5/y4lm4X8PZsAio1pobQ9I1Pu1b85D9l7yqKxZFDeRrUH9dCYSqhLiO85NY9XUAXj2oQxtGlbbSW+0qBLPvgTCioyHZI0OmmJH3+ixzxWnJ5c5hJ5DrL7yevvuREWelsPlHktDJYAAevKlIQEndT+OOGbFAXU5nGkMRKFDaYYOmxvihmFonNhE1+x44/GXglza564mf4KXrMe53gRe+dhsGSOKJz12WZ2DtCmYhEJIDR56Bjzn413nJNTLSUYFncMwvqP3mEQg7P9m6MaSw9V8xOdfQ== 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 AS8PR08MB8899.eurprd08.prod.outlook.com (2603:10a6:20b:5b6::19) 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 16:28: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 16:28:00 +0000 From: Andrey Drobyshev Date: Tue, 06 Oct 2026 19:28:01 +0300 Subject: [PATCH QEMU HCI-8.0 v8 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-v8-10-72ae4fb8977b@virtuozzo.com> References: <20261006-usb-host_v3-v8-0-72ae4fb8977b@virtuozzo.com> In-Reply-To: <20261006-usb-host_v3-v8-0-72ae4fb8977b@virtuozzo.com> To: svt-core@virtuozzo.com Cc: andrey.drobyshev@virtuozzo.com, den@openvz.org X-Mailer: b4 0.16.0 X-ClientProxiedBy: VI1PR08CA0230.eurprd08.prod.outlook.com (2603:10a6:802:15::39) To VI0PR08MB10656.eurprd08.prod.outlook.com (2603:10a6:800:20a::12) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VI0PR08MB10656:EE_|AS8PR08MB8899:EE_ X-MS-Office365-Filtering-Correlation-Id: 65f75cb5-36ea-40b4-6c41-08df23c6c871 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|1800799024|366016|52116014|376014|23010399003|5113699003|22082099003|18002099003|38350700014|56012099006|10067099003; X-Microsoft-Antispam-Message-Info: +mZiXxRThoRkDggR8CGXMuqponALI5eUtmaLOgxOE+TGy1bj0mCwvj3lEHVQqV+Q2v8jSQhZEHQHkzGzzUx2ry6+IygAxCs0oNeVa7jt+j3EDVcWW8qGs1EP/DCkvamHAev2JEp3EjmL8GnYd9smx3EhcFiD0woYUvB6SwPKGDiNNof8WyzyW+tF25a8m57eL4DD8eN3dGpSf7aHntbEV6IqqgYZN1YORxG8Vvc6pOhFjBWYb36H1whxOrE6NuhDLkXZEIW+rY2fogYdA4RD+LIy7gcL1TgDskRBpBvCV+rrBolIU6MSEWUSq28UoFs0NiPlmQGHXNyonM6nBBiqxt5dGBA9/LPfiaOxjzI6SQVt2cUSqrwYYqqTbPJX5e4cljMIPqGMvay9TiQ9d2OD4ahifiGlHtGpe2QgeG1bljOPCSzNxN2LHV/nGKthPxp1GFCOt17wo0YaPB0yFDKvC/4v9f0Xnl1nX2oLY3sa4h9RNy8NGA1pta5MPaCy7AnzTDrGptsCFHmLRLKUrSvPTi2ObIeGOQc5fC3r0bSIWVYo2w+oEpnmeM/1J/sFvEsAHGsUHsmVSgynrJXyTe35s1NlJCjBhas22TB39VNcXoVPi2nUUDqM2f+Y6bwN4Xt03KHlfy5aJ8HV75We4H8W+Ofo6B7DoG2oMyuNHa5XnekS/fwccME+NnV9ZxeOZbr42RHkMQK0R9TwMXXW5OcbMZtPLuJcEt1Dtiykh/xSygw= 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)(1800799024)(366016)(52116014)(376014)(23010399003)(5113699003)(22082099003)(18002099003)(38350700014)(56012099006)(10067099003);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?MStLQ3U1M2lJTzNpZTNDTWh2alh4Rm1UTGZRSkM5UXFJYlh5cDBzbHJBQWt3?= =?utf-8?B?eWV4SzdWYW1vWmc1L3VPd0pwTlhyVmNueVBaUWZObldaSG1OUXNuSnVzWFJG?= =?utf-8?B?KzgyMHZWNHYybUdsM0R6YzRVTC9XZGpmM0tEOXVVVmhnOW5hbEtkNlMyN3c2?= =?utf-8?B?blgvUHQ5TkJKNW9pdTRIVXdsam0xVkdtRUJ5RS8rNWYvUDlwbHc3RXNnUUxa?= =?utf-8?B?WjZaUk52Z3Z4dE13bUg0anlFR0ZVT1lhaE9vWDFCVVJsMWdnRmtQZGVlS3Z0?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2FVeGU4azRqRjVzdms5ZEluc09hZFJsZTdD?= =?utf-8?B?ZFNkSzV6MWtiKzdlSVpVOFp6b2VzUzdRMlFpQjQxV2t0dklMSlY2RkdQbkt2?= =?utf-8?B?VTd2ejZudm5oNjRqZTJFdjJIS001Ulh0bU9aY2ROR3VyYjAwRFVTbldFTWdF?= =?utf-8?B?VUhTZVpGcURPTjlBdnpMUmxUQlI2dThCUXdsQ1lNT2g4cU53b2luZHd1OExm?= =?utf-8?B?RTBYMHIzRVNtakhqR0JwSi9jaXM5UnFPQnJ6dHoyeHdEaHVBVHMwZ1hxNURo?= =?utf-8?Q?AIf7bW6vgoqV9edM=3D?= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 65f75cb5-36ea-40b4-6c41-08df23c6c871 X-MS-Exchange-CrossTenant-AuthSource: VI0PR08MB10656.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Oct 2026 16:27:58.4574 (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: KS3hPekaoXcbgH1iC5QJx+8bOr4vs4EeCc4tc6k7OeUnWvvaoHnVTI5n6dZMNz9LkltFGcWjP/BNQGxetGqBKQ1Jp6UYlPl0pJyw0l8JsBk= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR08MB8899 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(). Until then it must not touch the device at all: if the incoming migration fails, the target exits while the source resumes, so the target's exit must not reset the device or release the interfaces underneath the source. * The source stops reaping when it quiesces the device on the stop-for-CPR transition (or in the .pre_save() fallback for an already-paused VM) 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 | 91 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 12 deletions(-) diff --git a/hw/usb/host-libusb.c b/hw/usb/host-libusb.c index 70ae0a38167..1412f92c12e 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) @@ -1285,13 +1306,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; } @@ -1310,6 +1336,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; /* @@ -1384,16 +1415,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; @@ -1428,10 +1462,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); } } @@ -1500,6 +1541,18 @@ static void usb_host_realize(USBDevice *udev, Error **errp) error_setg(errp, "failed to open host usb device %s", s->hostdevice); return; } + if (cpr_get_incoming_mode() == MIG_MODE_CPR_TRANSFER) { + /* + * 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, and don't reset or release the device on an early + * exit either -- if the incoming migration fails, the source + * resumes with it. .post_load() restarts events and takes + * ownership once the source has handed the fd off. + */ + usb_host_stop_events(); + s->cpr_handed_off = true; + } } else #endif if (s->match.addr && s->match.bus_num && @@ -1563,6 +1616,17 @@ 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): restart the reaping + * stopped by the handoff and clear the flag to force a full + * reset/release in usb_host_close(). Except on a CPR target that + * hasn't taken over yet -- there the source still drives the fd. + */ + if (s->cpr_handed_off && !cpr_is_incoming()) { + usb_host_start_events(); + s->cpr_handed_off = false; + } usb_host_close(s); migrate_del_blocker(&s->cpr_blocker); if (s->cpr_vmse) { @@ -2046,6 +2110,9 @@ 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(); + /* the takeover is complete: from here on this process owns the fd */ + dev->cpr_handed_off = false; return 0; } -- 2.47.1