From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.48.66 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796313320; b=R9oqu9vHClndmzahsjS9SHmL5n7f2N8EKcB9vC2aQUEWteg3QNWfK2UmPgJqvEM0xX74 MqcWS9CbaRupe2VzwAOQ9cjLqAmbLqAfER7fncIMtqjn40a7YP0W4ieWZaGGWrNP7X1yW LauUXkwNDm8e1ZZ9PDA7hBO2K6LCmD5+mYbgtELsPMrsDC6JQ6Je3TYlLe02DCI81Zidu QXiCFME9MX0MAUVKggfi034MdoG3e74yUnsNfTJhoiKtL5E1xc+pruuHb4Ff9mKwHGM02 9yeuQ7bzXOcbLcahKck7eMZRV+luuE+HGNacyGnLhLA1duM9yoUTcNKyOiB+9paxpog== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796313320; h=DMARC-Filter:DKIM-Signature:Received:Received:Message-ID:Date: User-Agent:Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding:X-ClientProxiedBy:MIME-Version: X-MS-PublicTrafficType:X-MS-TrafficTypeDiagnostic: X-MS-Office365-Filtering-Correlation-Id:X-MS-Exchange-SenderADCheck: X-MS-Exchange-AntiSpam-Relay:X-Microsoft-Antispam: X-Microsoft-Antispam-Message-Info:X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-CrossTenant-Id:X-MS-Exchange-CrossTenant-MailboxType: X-MS-Exchange-CrossTenant-UserPrincipalName: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=XvLWKNI4svFrm36gJ4hwAp0FE3p/HSMuok8LJ4Gvl0s=; b=TbMz6T8y2u1Gmyy+jVOUGEF8ywkvGZdrNKeQ3oyxoGd1OliOH03SnwDqnAN0hUSbeFKr UFN3ZDKGNoQkHLX4CRx1D/V2Y9Sxm38dM0nLEEO/xP65Pad39ETo0WuecbEAg7Bu+u+hR 2SMoWr+LiZZGtxnsPIdCasbbfrg1tcraWmBFEBMDo8O5eMNXnEf0ZVy7SmK22OVnM3ck2 TB7ehLUOHIkcrCGg00KF0uL1kKvv0A3qRTkEdq1f9tWFP233uR3ZFY07dzDS8G3FkojE5 J8RwNTBqUo0qrn3C8sffJ5jbgfzi8PEAwVOHXIeLSk5eheY8wtL8KqohGYk6N2EmDFw== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=p9gmokqY; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.48.66 Received: from MW6PR02CU001.outbound.protection.outlook.com (mail-westus2azon11012066.outbound.protection.outlook.com [52.101.48.66]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7CCFA606A322; Thu, 3 Dec 2026 15:55:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7CCFA606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=p9gmokqY ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=l5bCxFuB6LJuapvnDhTxHs3SMR8I0lq8Zsr0UMSBq7uazGruRpjuHhG+/jYFPADWmsgLhiDSZxoicL8OLGkmvtAFP/31rBuo7IIPh++RX8zQ56eLcb0QFHVdVRa8nTaCXEkE4AZe1Akt9U8chQY+L28htXSeUHVKjvS/3tGGdZJudBs40hVCypN8U6r3mT3sP0iRju4zW38+Urfip3p0NIkldA91vu+guZsV0HjemFm9kIm2i+lGYq2138i876soqmWBoFplWAuSumNtjKOpLWdl7xU9UzKNO1iTYRhPYIQePY48hzTTUeyHONIUysuKBVprDeOk0rR7/ZIJH64JAg== 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=XvLWKNI4svFrm36gJ4hwAp0FE3p/HSMuok8LJ4Gvl0s=; b=mzYA6osu3L37W7YxlBFS2jSDWfqAW4iaUwGCm3goFnhnLaIMfAIR2pCAUMjWhmkkxEPeebfFMcxi0kgj6vhtgbT2lvCFzlitb91La8F9T+6wm8cuuND9uO3D+FOI7HtDeA5aj6jBcs28fj+sZO8KpAHSIayQG644RHOOCnFNezWiqQ0sTt6E75ySFXwJVwtAhtkKKAC7greF3lv0CoPpk0xPHceST2pQgs9m+JBVl+BY7bByidBopQixhH1LK+m3UCWb2OBIeopJN5j2NtznSE28kHPMVeZ0172kOXk2yzx2NTVH6i/F9vvHle4qBiponn0heD0AER3C7BVVOZRcSQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=XvLWKNI4svFrm36gJ4hwAp0FE3p/HSMuok8LJ4Gvl0s=; b=p9gmokqYsVYJ3G9BGzkn2oYCzTd9+G+GYAuQcU2QMbF8wWuDt4ADMvNta6J8K2FxVKJfsChPsdhRzs3UU35v8TWA5EH2GfkUauI/g4MeL5yksjvX9xXZaGZDAWXL8buqFP6lQC53w62RIDGxgKpZ1UNXccciEKtXk9A42RkvljU/C+drOi2a9cdMetY70vvfhGECs978OIOd7baNfR08+7AUIl6wT+NGmhhp9H+ke/p1z11LEF1QFYqG8yGd5Qq0uXE3GO5hzm+mFnSTUI1rzANeesVrKtOwKaOaxlWd6o3RrOJCNmgXwMA+hNnk5YpPrrEEcXM+JLojDdAEXKmmEQ== Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CY5PR12MB6549.namprd12.prod.outlook.com (2603:10b6:930:43::6) by IA1PR12MB6387.namprd12.prod.outlook.com (2603:10b6:208:389::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.12; Thu, 3 Dec 2026 15:55:11 +0000 Received: from CY5PR12MB6549.namprd12.prod.outlook.com ([fe80::2cdd:a549:db13:202b]) by CY5PR12MB6549.namprd12.prod.outlook.com ([fe80::2cdd:a549:db13:202b%4]) with mapi id 15.21.0688.009; Thu, 3 Dec 2026 15:55:10 +0000 Message-ID: Date: Thu, 3 Dec 2026 10:55:04 -0500 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 1/5] rust: vfio: add common VFIO device handling To: Gary Guo , dakr@kernel.org, acourbot@nvidia.com, kvm@vger.kernel.org, rust-for-linux@vger.kernel.org, nova-gpu@lists.linux.dev, linux-pci@vger.kernel.org Cc: Zhi Wang , alex@shazbot.org, jgg@nvidia.com, yishaih@nvidia.com, skolothumtho@nvidia.com, kevin.tian@intel.com, airlied@gmail.com, simona@ffwll.ch, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, jhubbard@nvidia.com, ecourtney@nvidia.com, cjia@nvidia.com, smitra@nvidia.com, kjaju@nvidia.com, alkumar@nvidia.com, ankita@nvidia.com, aniketa@nvidia.com, kwankhede@nvidia.com, targupta@nvidia.com, linux-kernel@vger.kernel.org, zhiwang@kernel.org, boqun@kernel.org, daniel.almeida@collabora.com, tamird@kernel.org, work@onurozkan.dev, jgg@ziepe.ca, bhelgaas@google.com, kwilczynski@kernel.org, dri-devel@lists.freedesktop.org, peter@colberg.org, "Liam R. Howlett" , linux-mm@kvack.org, Lorenzo Stoakes References: <20261127224928.1511382-1-xinmengs@nvidia.com> <20261127224928.1511382-2-xinmengs@nvidia.com> Content-Language: en-US From: Simon Song In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-ClientProxiedBy: DS7P220CA0102.NAMP220.PROD.OUTLOOK.COM (2603:10b6:8:25d::10) To CY5PR12MB6549.namprd12.prod.outlook.com (2603:10b6:930:43::6) X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY5PR12MB6549:EE_|IA1PR12MB6387:EE_ X-MS-Office365-Filtering-Correlation-Id: 6dec2af4-a015-4c4f-3a39-08df5155cebd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|376014|7416014|23010399003|261026121527099003|10067099003|56012099006|11063799006|5023799004|261021123027099003|261021121427099003|261022075727099003|261022105027099003|261009223027099003|261102123227099003|261026140227099003|261028144727099003|22082099003|18002099003|4143699003|6133799003; X-Microsoft-Antispam-Message-Info: mgsK19cTMHH+g1nAvihxZYKRLckOC2r6Pv9aOxXt3votMaBuR1h8YMIV9v2v8AcF+vgi3e6I0qdotxr/P4FVV71X65THh0rlLQDHcVuECKU/usBlEAG0LKYUSsafg6+/HBtUPcPQObt/qF4gTG3WhwtDsSUdRqyQw8e9DUo8mJQukbQGq1T3QYMb5g8wSaIQZXHELEe42u/dCqruLfAWynm0e9Gd/tYFy9ZkHGcelaV6Yp5o5ZB/BYyByvKK7rmt/Wh44dkpXGQlR1V6wC565Z2kVIyCeGKfWUyawxRxbl7Al/zBl//4d1f4eQb/AKepmtIv8ZZ35hFdHkKtZeLhl3m9bjXQA/YLWcw+FHwxEYus0beXuX46I3EkaXWjff1gmj/zwdqnt5/ABP280ZmSYtmcbTsjtI+l7PiLBM6RhEEZ2UEKBpzm1y7P7eGSJZz+WXnCvkHIdR/uTn1qy+4GI7m3vtp5V3XZ3dSdgvp4eFJuXHMISwtpFlT7iZE43IQjW72t09osJvyxE+kA3czVO/qxGhyutr9OI7aQ8x7Rr3dryn2xV2jkzkScjFzCGDJ6TiNy4BAkUtHn817nWFXXebESt1l9dFWtPQU6OUKJtqDVJDH4RJ3QC1VQ22B/heoNha8+wRsKVbvVt3iUeax+WA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CY5PR12MB6549.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(376014)(7416014)(23010399003)(261026121527099003)(10067099003)(56012099006)(11063799006)(5023799004)(261021123027099003)(261021121427099003)(261022075727099003)(261022105027099003)(261009223027099003)(261102123227099003)(261026140227099003)(261028144727099003)(22082099003)(18002099003)(4143699003)(6133799003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?UkdSUWt6cFczU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qm8yUnQxYXg2Umdxc25xNnRj?= =?utf-8?B?MnFVdGRXY2VQY2xlMVMwWFg1LzJUaFZPZzhYb0tkTlcvS1diaGovQWpZSnBJ?= =?utf-8?B?ZDhSQlBaa3FwSlFDblhPTzNYeThHNzZxM1E4Q2IvN2JXM0sxWFhUOEpCcVBM?= =?utf-8?B?bDlkaUhlb2ZYNGhqVlc3VUZvUkhvanhxSkROalFyZEs0WHROem5UNmNlSTll?= =?utf-8?B?dDBoZ29XNit1WVpscU04NFlFWG5EN0RZSG1ISVFvTTliVjdacVBUL204K05j?= =?utf-8?B?S0YxVEdzT3AvSVBVUndBZ3kyRUM1RUlwZjVYY0NScURyYjNQOE5CUzBRTHF5?= =?utf-8?B?cTk5UXpxM2NtVnQwUnpkMC84bkRhbFFFQ1Y0L09TR1JiL0srSWJMSHJlcUJp?= =?utf-8?B?VGR0Wk5pbEpKK1NheVEyZ01pY1JsTkJVVlVmL1Z0d052RTlIeUtTdVZGdWJn?= =?utf-8?B?K0NaZ1JWclVaYldGT0xMQkxWeXRhaDRuRDJPUDNRWHp1Q3I4dkcyTzFHcmhx?= =?utf-8?B?TVREbnlFY29xZ1BETndBMHZYVEM1V0s1Mlp6QjVFWXFaaEk3VTM4QT09?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6dec2af4-a015-4c4f-3a39-08df5155cebd X-MS-Exchange-CrossTenant-AuthSource: CY5PR12MB6549.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2026 15:55:09.7580 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: y1PcYG2mJL3NVJOcxbaUFUQBelk0z52sPYW/yGvLCs9WX6LXvfCKvE0keVfzdB+ICXAEyrDR9z8fwmfNdtbX2w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6387 On 12/2/2026 10:58 AM, Gary Guo wrote: ... >> +//! Virtual Function I/O (VFIO) devices and callback data. >> +//! >> +//! `DeviceBase` combines a C VFIO device structure with the data its callbacks use. >> +//! [`Device`] is an alias of it for `struct vfio_device`. > > `DeviceBase` is mentioned here in public-facing documentation while the type > itself is hidden. > Good catch, I will describe Device directly and no longer names DeviceBase >> +//! >> +//! C header: [`include/linux/vfio.h`](srctree/include/linux/vfio.h) >> + >> +use crate::{ >> + bindings, >> + prelude::*, >> + sync::aref::AlwaysRefCounted, >> + types::{ >> + ForeignOwnable, >> + NotThreadSafe, >> + Opaque, // >> + }, >> + uaccess::{ >> + UserPtr, >> + UserSlice, // >> + }, // >> +}; >> +use core::{ >> + cell::UnsafeCell, >> + marker::PhantomData, >> + ops::Range, >> + ptr::NonNull, // >> +}; >> + >> +/// Reset the VFIO device. >> +pub const DEVICE_RESET: u32 = bindings::VFIO_DEVICE_RESET; >> + >> +/// The context in which a VFIO device reference is valid. >> +/// >> +/// Callback views can only be borrowed from the corresponding VFIO trampoline. >> +/// They cannot be refcounted or shared across threads. >> +pub trait DeviceContext: private::Sealed {} >> + >> +/// A callback context in which VFIO keeps the device open. >> +/// >> +/// Registration data and open data are available throughout such a callback. >> +pub trait IoContext: DeviceContext {} >> + >> +/// An ordinary device reference, without callback-specific operations. >> +pub struct Normal; >> + >> +/// The device is undergoing its exclusive first-open callback. >> +pub struct Open; >> + >> +/// The device is borrowed for a VFIO ioctl callback on the current thread. >> +pub struct Ioctl; >> + >> +/// The device is borrowed for a VFIO read callback on the current thread. >> +pub struct Read; >> + >> +/// The device is borrowed for a VFIO write callback on the current thread. >> +pub struct Write; >> + >> +/// The device is borrowed for a VFIO mmap callback on the current thread. >> +pub struct Mmap; >> + >> +/// The device is borrowed for a VFIO region-info callback on the current thread. >> +pub struct GetRegionInfo; > > That's a lot of type states. It looks like all of these are only used to ensure > only corresponding vfio_pci_core callbacks can be delegated to. Are these really > needed for soundness? I can understand that the `Open` one might be needed, but > the rest might be mergeable? > As Danilo wrote, they're not needed for soundness, but to limit a callback to its own core function: for example, core_write() cannot be called from read() ... >> +#[expect(dead_code)] >> +impl DeviceBase { >> + /// Creates the open data from the registration data. >> + /// >> + /// The [`Open`] context guarantees an exclusive first open of a registered device, whose >> + /// registration data remains valid until the last close. >> + /// >> + /// # Safety >> + /// >> + /// - No open data may exist. >> + /// - The caller chooses `'a` and `'reg`; this function cannot tie either to the time the >> + /// data is valid. `init` must therefore use the registration data parametrically, as a >> + /// function generic over its lifetimes does: nothing borrowed from it may escape, other >> + /// than into the returned open data. >> + /// - `init` must not capture references that are only valid for the callback and store >> + /// them in the open data, which outlives the callback. >> + /// - The open data must be destroyed with [`DeviceBase::close`] at the last close. >> + unsafe fn open<'a, 'reg: 'a, I>( >> + &'a self, >> + init: impl FnOnce(&'a T::RegistrationData<'reg>) -> I, >> + ) -> Result >> + where >> + I: PinInit, Error>, > > `Device` can escape into `T::OpenData` and break the invariants. You need > to change `pci::Operations::open_device` to give `dev` an anonymous lifetime. > will make this change as your suggestion in patch 3 >> + { >> + // SAFETY: By the type invariants, an `Open` reference only exists for a registered >> + // device, which has published registration data that nothing changes anymore. The >> + // data remains valid until the last close. >> + let reg_data = unsafe { *self.data.reg_data.get() } >> + .expect("VFIO registration data is expected to be published."); >> + // SAFETY: By the same reasoning above, the registration data is valid; only its >> + // lifetime is named here. >> + let reg_data = unsafe { reg_data.cast::>().as_ref() }; >> + >> + // Allocate before calling the driver so allocation failure cannot follow open. >> + let data = KBox::pin_init(pin_init::pin_init_scope(|| Ok(init(reg_data))), GFP_KERNEL)?; >> + // The stored type only differs in lifetimes, which do not affect the layout. >> + let raw = data.into_foreign().cast::>(); >> + // SAFETY: The first open is exclusive, and no I/O callback runs before it returns. >> + // INVARIANT: The allocation is pinned and owned by the slot until `close()`. >> + unsafe { *self.data.open_data.get() = NonNull::new(raw) }; >> + Ok(()) >> + } >> +} >> + >> +#[expect(dead_code)] >> +impl DeviceBase { >> + /// Returns the registration data and the open data, for an I/O callback. >> + /// >> + /// # Safety >> + /// >> + /// The data is named with the placeholder lifetime `'static`, which is not the time the >> + /// data is valid. The caller must use both parametrically, as a function generic over >> + /// their lifetimes does: nothing borrowed from them may outlive the callback, and nothing >> + /// borrowed for the callback may be stored in them. >> + unsafe fn callback_data( >> + &self, >> + ) -> ( >> + &T::RegistrationData<'static>, >> + Pin<&T::OpenData<'static, 'static>>, >> + ) { >> + // SAFETY: By the type invariants, an `IoContext` reference only exists during an I/O >> + // callback on an open, registered device. VFIO excludes the last close and >> + // unregistration for its duration, so by the type invariants of `CallbackData` both >> + // slots hold valid data that nothing changes, and the open data is pinned. >> + let reg_data = unsafe { *self.data.reg_data.get() } >> + .expect("VFIO registration data is expected to be published."); >> + // SAFETY: By the same reasoning above, nothing changes the open data slot. >> + let open_data = unsafe { *self.data.open_data.get() } >> + .expect("VFIO OpenData is expected to be populated."); >> + // SAFETY: By the same reasoning above, the registration data is valid for this callback. >> + let reg_data = unsafe { reg_data.as_ref() }; >> + // SAFETY: By the same reasoning above, the open data is valid for this callback. >> + let open_data = unsafe { open_data.as_ref() }; >> + // SAFETY: By the same reasoning above, the open data is pinned until the last close. >> + (reg_data, unsafe { Pin::new_unchecked(open_data) }) >> + } >> +} >> + >> +// SAFETY: VFIO uses the embedded class device's refcount to own the full allocation. >> +unsafe impl AlwaysRefCounted for DeviceBase { >> + fn inc_ref(&self) { >> + // SAFETY: A shared reference keeps the initialized VFIO device alive. >> + unsafe { bindings::get_device(&raw mut (*self.as_raw_vfio()).device) }; >> + } >> + >> + unsafe fn dec_ref(obj: NonNull) { >> + // By the type invariants, `Raw` begins with its `vfio_device`, and `raw` is the first >> + // field of `Self`. >> + let vdev = obj.as_ptr().cast::(); >> + // SAFETY: The caller owns the VFIO reference being released. >> + unsafe { bindings::put_device(&raw mut (*vdev).device) }; >> + } >> +} >> + >> +// SAFETY: VFIO serializes open/close transitions, and callback data is `Send + Sync`. >> +unsafe impl Send for DeviceBase {} >> + >> +// SAFETY: Shared access only changes the embedded class device's reference count. I/O callbacks >> +// share `Sync` data; VFIO excludes them from mutations of `open_data` during first open and >> +// last close, and registration data follows the registration lifetime. Mutable C state is >> +// managed by the VFIO core. >> +unsafe impl Sync for DeviceBase {} >> + >> +/// Capability buffer supplied by VFIO for a region-info callback. >> +pub struct InfoCap<'a> { >> + #[expect(dead_code)] >> + raw: &'a mut bindings::vfio_info_cap, >> +} > > Any reason that this isn't > > #[repr(transparent) > pub struct InfoCap { > raw: Opaque, > } > > ? > good catch, will include in next respin