From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from PA4PR04CU001.outbound.protection.outlook.com (mail-francecentralazon11023103.outbound.protection.outlook.com [40.107.162.103]) by lore.virtuozzo.com (Postfix) with ESMTPS id 0C57B80067 for ; Thu, 24 Sep 2026 16:41:44 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=oWeutNhheNBFBPt16kNHvuIcza/SFzeP5SYqmukECXIYLkRXRp1FJMIM8fkMYYLc7UV5Q6Qoqm+tQEJdEZSwk5QCDDdxG8bfp3DVUePJhzhFSAzQdTkY9Z0k8FTidhFj7B76ifbmRzOTHAoFwLsV/fza8xGQVfrftagNG4pxoyNeJYUn9hODr5B2d1a/LoFMFbMEZ3MgoTjC5kKOKZgBVHkvoLqFRkm16lBMkxxx5SEykzqbBDQRlkqlE404dVtXA9MswyhTsHZ/A0drw99LSI+RrTP7a8USsEDrUm/sOpyPJy3aGcQ+JYeEcX9BfdoLM57H9krju1SRagenmGNWUA== 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=2fAF79rMPOuVdOYR+y7CM5O+TQgNnkoU+aJKL2PqYG8=; b=ciIHTxRBaEmPsNSbULzFzgDzS05DSIIntgybdh9b7F8ifaqVNHk18JZgUFzlAVkm+Xqw0rd+/kIdOlqwe9jc9Mtbdfzqn7smALV4jV8FK3ebK3WgtXyC6vGHyjQhC3Aic29r8qtfHm0M+/hSasoaEcIar8ancxk+CNIzn1wqEiENFPLOrdT7XERlcoUMff6mbbNFaUUyHF7mF+e0ki+ph0DY3CkdMAbHcIwtJ2RGR+wUt5apcwcGvARPuogKHdVChQ4kVD3jCSTDz6x/h99WRkU/xhABZ+2+y3LV77vhYGWUErfpbtnQYFDzRD5bE+gthrjHJB7xSwYGvfA8MGeATw== 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=2fAF79rMPOuVdOYR+y7CM5O+TQgNnkoU+aJKL2PqYG8=; b=weIoUsfN7tjsQtoB3XqIZ6OZrEiGo/TZLFFPxxcLXaAp2ACdVGg9K8RoMTqMt+aX98Q0v7A1L6gCXhhqCxV4Cizjb04j8WVUOhbukWemUMvYVCfofP1fV/oUF6Dbj2yAevA8iBWVXFqo0d7akt6T+XE7YZvYy0pJUPJo5PPzW/lqiAS0PC2TFIjXdwJhnkmtcmZqqriJxYOH1lq+CRRDQ8VN4NcjywzqJ3/ndfQsOb8UGrGA9S0ktfUYd0O5YJM/Eia7FX6gDQVbIDxO9KxEGixsS8j/gwP5Tuzecf+9InwTO51Sgi0JUFX7h7h+wkFLki8CBYnFcktWNPxrjwsKWQ== 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 BR0PR08MB151385.eurprd08.prod.outlook.com (2603:10a6:2c8:30::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.18; Thu, 24 Sep 2026 16:41:43 +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; Thu, 24 Sep 2026 16:41:42 +0000 From: Andrey Drobyshev Date: Thu, 24 Sep 2026 19:41:33 +0300 Subject: [PATCH QEMU HCI-8.0 v4 10/12] usb-host: reconfigure endpoints on CPR incoming #VSTOR-137800 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260924-usb-host_v3-v4-10-1cc48bf630e7@virtuozzo.com> References: <20260924-usb-host_v3-v4-0-1cc48bf630e7@virtuozzo.com> In-Reply-To: <20260924-usb-host_v3-v4-0-1cc48bf630e7@virtuozzo.com> To: svt-core@virtuozzo.com Cc: Andrey Drobyshev , den@openvz.org X-Mailer: b4 0.16.0 X-ClientProxiedBy: VIVP296CA0030.AUTP296.PROD.OUTLOOK.COM (2603:10a6:800:356::12) To VI0PR08MB10656.eurprd08.prod.outlook.com (2603:10a6:800:20a::12) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VI0PR08MB10656:EE_|BR0PR08MB151385:EE_ X-MS-Office365-Filtering-Correlation-Id: c252cd4a-40d0-44d2-4719-08df1a5ab6a9 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|52116014|376014|1800799024|366016|10067099003|5023799004|5113699003|6133799003|18002099003|22082099003|56012099006|38350700014; X-Microsoft-Antispam-Message-Info: G4MvT3VGZgyYjmHw7bdAx/unnzcefNrVQ8fLuwPhK0B1jipZfOxQBmG9fphjVP/uY8BmEHE7O1jcwqLqNi/XO6axVyGaJvsPz1FVeZp7hwm7ak5lwF+pGzSZD5/Xjxa0L8diGvZNLZnx7BlhiqgdWvghiMYJZsZpnZpgkz2Qs4gOZ+z5zyrPIJAE3hM9yT93s1EWukFr3ZkMwVo6vokv+EEe1KxQL8B8L05BDUqR97qJuDse/KoHFIlsc+cxfnRuOx/Z4ob8kV4AFhCo1YzPo+iqHAx57Ql0jhfnOBoPivCDkWHFbqzXrinj65MvyZAaVbQli7Nb9RmD526IVTE4F2fM3Ps7Hcf+z/e+g5tqR7/nLjV+ctn/KJ+ULRzN9JWbHlXQ8mfxdHyEbOmBtIwmkxJwF/nXFCox9i5W5lXt3yFEkCBFfjgAv2As3B6oTFp7b0ZTi6Xnf0HYX9irjirFY2SpISD7zfptHV3x7GfkgOgz6RsugxDqXxFBO+LuqLjvCNjcfpPUn7h5rZsK4jmgkquYCi75NNQ9TsIyN+7CVxYvr593MJLwE/cQfAW+wLhyXArtjNvtmUdc/5rthGyL2VBNE/2hSxfD8FM0+osKnGimPAgRftcyyYmEK+9RpoAG+XypEpS2c28AWNKvRxZ7KWrYBwV/05P3q1TYZxsMLkxGHNpSj0YuelsR5j7WqsqtMxNN7GYdAQjZKIHWDA4lg1PHs98wCzJGfJcLGV+n7Gc= 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)(52116014)(376014)(1800799024)(366016)(10067099003)(5023799004)(5113699003)(6133799003)(18002099003)(22082099003)(56012099006)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?MVBWVkZlWVhDN3dPK3Q2OFpvMjRRM1dRZVlJUDBoNFNMb2pKWXYySFFaVTBB?= =?utf-8?B?N2ZKd1FIVHVwZFlLWnE4eC8reUhDTENZaHBuYklSU2wwVXlFQnYwbDBIVVhF?= =?utf-8?B?cWVISDJnNlhpSHFsb3QrSGRTZEJFZ1lieUE5bzMrMXhNcXl6WEpCSS9LY1BH?= =?utf-8?B?MHFmK2tYR1h1bWlkd0xDTFphN0YwUDBoY01FaXBualJWVlJoWFdvb0Z2MCtN?= =?utf-8?B?ZFVDWk9RV2ZlRVd6Vm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2R1l4MWpxeXZOQ3g4VytFQzZ0K3Jkd3ZlWER0Yi9NOUFuWTJhN3Jn?= =?utf-8?B?a25mOFY4QVVvWHdTTkNVaUhUWDUzSG1hc1ZxQjY1blRvTE1BODBKNVhia0pT?= =?utf-8?B?ZkcyeGVlL1JIbG84YkRaZ2VCNHUwOS9aRXN3dmhzZEJaWVpsZFdIL1kwSXdx?= =?utf-8?B?aHlWa01acUxNOTEyQnIrd05nb0hYK2tWSUNTV0pSVU9iYnNzR0pGNVZGQ2x2?= =?utf-8?B?WG0rSjRFTlNFZHIzQmZZblZwRUxlYmlIMk05SndtTS8xQk93MXAvc1FZN1Aw?= =?utf-8?Q?3EthXqkAxnN4K1YM=3D?= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-Network-Message-Id: c252cd4a-40d0-44d2-4719-08df1a5ab6a9 X-MS-Exchange-CrossTenant-AuthSource: VI0PR08MB10656.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Sep 2026 16:41:42.7776 (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: Pvg87+Q31am/CXTB+Q6v3XxUczuFTzv6y3hywMGySMhOKTWmFpO1AizwkltDanlX2AyQDit78CbzAzdugZYVcoD2T2EU3zSuUCeF4GaDZ7Q= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BR0PR08MB151385 On CPR incoming we skip the usb_host_open() rescan, so libusb only sets up the endpoints from the device's default configuration. If the guest had enabled an endpoint by selecting a non-zero altsetting (e.g. an isochronous streaming endpoint), that endpoint is now missing, and the guest's transfers to it simply stall. The altsetting itself is restored by vmload now, so let's call usb_host_ep_update() on the CPR target to rebuild the endpoints out of it. The kernel claims are still there on the preserved FD, but libusb's own handle knows nothing about them, so a later SET_INTERFACE would fail with LIBUSB_ERROR_NOT_FOUND. Let's re-claim the interfaces on the target as well to bring libusb's view back in sync - on the preserved FD this is a no-op at the usbfs level anyway. Signed-off-by: Andrey Drobyshev --- hw/usb/host-libusb.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/hw/usb/host-libusb.c b/hw/usb/host-libusb.c index 57984e72e45..eb349915eee 100644 --- a/hw/usb/host-libusb.c +++ b/hw/usb/host-libusb.c @@ -907,7 +907,7 @@ static void usb_host_ep_update(USBHostDevice *s) alt = udev->altsetting[intf->bInterfaceNumber]; if (alt != 0) { - assert(alt < conf->interface[i].num_altsetting); + assert(alt >= 0 && alt < conf->interface[i].num_altsetting); intf = &conf->interface[i].altsetting[alt]; } @@ -1959,6 +1959,75 @@ static void usb_host_post_load_bh(void *opaque) usb_host_auto_check(NULL); } +/* + * On the CPR target libusb knows nothing about the interface claims the + * preserved fd still holds, so a later SET_INTERFACE would fail with + * NOT_FOUND. Re-claim them to bring libusb's view back in sync; on the + * preserved fd it's a no-op at the usbfs level anyway. + */ +static int usb_host_cpr_reclaim_interfaces(USBHostDevice *s) +{ + USBDevice *udev = USB_DEVICE(s); + struct libusb_config_descriptor *conf; + int n, rc; + + rc = libusb_get_active_config_descriptor(s->dev, &conf); + if (rc != 0) { + usb_host_libusb_error("libusb_get_active_config_descriptor", rc); + return -1; + } + + n = MIN(conf->bNumInterfaces, USB_MAX_INTERFACES); + for (int i = 0; i < n; i++) { + rc = libusb_claim_interface(s->dh, i); + if (rc != 0) { + usb_host_libusb_error("libusb_claim_interface", rc); + libusb_free_config_descriptor(conf); + return -1; + } + s->ifs[i].claimed = true; + } + + udev->ninterfaces = conf->bNumInterfaces; + udev->configuration = conf->bConfigurationValue; + libusb_free_config_descriptor(conf); + + return 0; +} + +/* + * During CPR the altsetting array is included in the migration stream. + * Reject a value this device can't honour, so that usb_host_ep_update() + * on the target never sees an out of range altsetting. In this case + * we fail the incoming migration instead of aborting on it. + */ +static int usb_host_cpr_check_altsettings(USBHostDevice *s) +{ + USBDevice *udev = USB_DEVICE(s); + struct libusb_config_descriptor *conf; + + if (libusb_get_active_config_descriptor(s->dev, &conf) != 0) { + error_report("usb-host: cannot read config descriptor on " + "CPR incoming"); + return -1; + } + + for (int i = 0; i < conf->bNumInterfaces; i++) { + int ifnum = conf->interface[i].altsetting[0].bInterfaceNumber; + int alt = udev->altsetting[ifnum]; + + if (alt < 0 || alt >= conf->interface[i].num_altsetting) { + error_report("usb-host: migrated altsetting %d out of range for " + "interface %d", alt, ifnum); + libusb_free_config_descriptor(conf); + return -1; + } + } + + libusb_free_config_descriptor(conf); + return 0; +} + static int usb_host_post_load(void *opaque, int version_id) { USBHostDevice *dev = opaque; @@ -1970,6 +2039,18 @@ static int usb_host_post_load(void *opaque, int version_id) * Skip it for CPR. */ if (cpr_is_incoming()) { + /* + * We kept the device open across CPR, so libusb still needs its + * interface claims and endpoints rebuilt here. And we only start + * reaping now, once the source has handed the fd off to us. + */ + if (usb_host_cpr_check_altsettings(dev) < 0) { + return -1; + } + if (usb_host_cpr_reclaim_interfaces(dev) < 0) { + return -1; + } + usb_host_ep_update(dev); usb_host_cpr_restart_events(); return 0; } -- 2.47.1