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.61.35 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796350400; b=g17GfDsn+m8EboXMC8GpHbAXRhANB5A1gK7EmscyqHevwSVdrRiF7vK8NCj66sY85EwN rzFocnjQ7nl5LF7+DKMYs0N5xX5PdrGJMfBzQHPv9rs0CN4rGPXD5g9nUn9s+edcCq9eA ZfSaDNDZdnAFNSGoOxNy7pJb0p2ScKLBzR1nd76kYaBVx2V8wCw8ZjrWvdU7XIzYpeHnk U+WmhmEvo2eE9+Ms29eFjQcuOvEJxxQ6J4KTTi3Hs+j0gkef2cgJkX9l8onvJwp8HXgjD 2XzZ8bHUrdslPKyIRpPAnKXIU4kUuILOz+q8oVsCQODNUI4WL0v3DEJ6k3Sw1fTr6Xw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796350400; 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=iIuYHiDMzUPNxDE85lwAKoGL4oAOY3X4Lgf+Irl5Rgs=; b=x08biqrqTq4zX7BNqundFoQjYt9cZXzDrBNnuKUvWXqfly/n+yx/mZ5/e6XVd7d9kM+Z 4rY9yS+IEvksaCNklzO7TQbb10qMImbX06co4L/xQPSw9FiLMRk01axXBZoPc/HpqrPoy yUgVpQNXYzyGw+7q91tqLi4moJbecfBo9zoax+3AMaNauVaUaU09M97rdDyCeZ4JjgSiu 2arPlDooNZChHdOewyDZm9QxXgTlJrSBHfAk1Nt5szCOggchhsoyuXbQI6ALsGQF9PYnX iQY9M7NVYgnJh3PtMGnwAE5ld3PejcRvhYYI7ARm5sWlEpdQ0f1mXfKDc/QyMG2fD7Q== 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=BMS6SiqT; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.61.35 Received: from DM1PR04CU001.outbound.protection.outlook.com (mail-centralusazon11010035.outbound.protection.outlook.com [52.101.61.35]) (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 536C7606A32F; Fri, 4 Dec 2026 02:13:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 536C7606A32F 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=BMS6SiqT ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=w1xMCETvQQz48GdYSFpr3y/A8VOHjeNbrSzw46IqhB1MSbjeSToOzrmHgKRd/W1oK+rsAz9FC7ZvFNi5GxRKNqlmc6LtiQeIXXfdrb8eQe9ua7IqdnwjubCF1eTsALgc9GYaJ66WK9F9Sw18U6MrMT+d2unZ/xFLjd4n1rDYoQjNsk47aqdE93ys4RhpOW6wEY55dzpfB8nford0PUoU+6+2bScAbKuDcuOkZErAmf5SQQXK0+FsZA9ol3gwD52EYH5kh5Dh+V/DmsxQgqwTalM6fFQVKs+lH99uqEsUhoTXi0jGvkv0nMFh5+oz6FB7N+JEjiWb9AWZN2yt5r8Edw== 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=iIuYHiDMzUPNxDE85lwAKoGL4oAOY3X4Lgf+Irl5Rgs=; b=gPsdw6wJ0ZedEU9rO4HDq+i6+B/20wmEQe4g8OsDBqOhw3IWWfU28DuZKBeSCqCrAI2LnmzVOADeFUxY9XBY2L0gdm5gFtVcvr96pl1oa4w5fPEqIarBXJjuLJBz3f/5ZXVlE3g+YTO8gcpSpc7RmkTnD/6e0MwQe1lcqWsz/mycpGHHp+lCNSH0OCxe/kBfhMq3HJYW5PgCCBI3ozp3oRFn8mJMwU5tfg+fa95++LZnQQ/ZV5tG6pYi4pfonewpBqdzFBtUmw4qJXsiJGS+rD67ii5Wd2cXaabYan7DSUypuweQzXxDNv8GniSnlwToBKjtH79cc4m50CkR+1CkfQ== 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=iIuYHiDMzUPNxDE85lwAKoGL4oAOY3X4Lgf+Irl5Rgs=; b=BMS6SiqT0qRG5MtG83D2zy4Tr4j9js22LsUrGWthYxYS27SB6yyGPZcT5+ycb/S0dKY25OaF1jCDHF/9bfRlkiji7SBSAL4IaXdp9ypCf3GbSKlKFLwJVSPE9o2Jih2VZUFyM0UCGHnlQpkpLf+zuZsMZZJPScKXq7STyAvVJTab4M2PAmXUWv5l2tM9LgnK2JvSpfTn7VlAWVuVEhNpZdemKoxclIQGACjR7GGAejf3r8kTywTqf5K2wh2XBBSEAoq2T1hCN1jCrWnKYQDf1Kr0fYO8cJqOcAHlv1AJoCTnsndqMDS7qUPvhfpAfJ7zMq7CbraJ1rCsczm4kDclwQ== 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 CYYPR12MB8750.namprd12.prod.outlook.com (2603:10b6:930:be::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.12; Fri, 4 Dec 2026 02:13:14 +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; Fri, 4 Dec 2026 02:13:14 +0000 Message-ID: <70bdfa64-2988-42eb-a940-8d022fab45f6@nvidia.com> Date: Thu, 3 Dec 2026 21:13:01 -0500 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 3/5] rust: vfio: add PCI variant driver abstractions 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: 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, zhiw@nvidia.com, 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-4-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: SJ0PR05CA0087.namprd05.prod.outlook.com (2603:10b6:a03:332::32) To CY5PR12MB6549.namprd12.prod.outlook.com (2603:10b6:930:43::6) X-Mailing-List: kvm@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_|CYYPR12MB8750:EE_ X-MS-Office365-Filtering-Correlation-Id: e97e4555-0ae6-4b68-9b3c-08df51ac270c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|23010399003|366016|1800799024|376014|7416014|18002099003|22082099003|10067099003|261026121527099003|56012099006|260925022911599003|260925021311599003|261102123227099003|260925021911599003|261028144727099003|261026140227099003|261022075727099003|261022105027099003|261021123027099003|261021121427099003|261102130727099003|261009223027099003|5023799004|11063799006|4143699003|6133799003; X-Microsoft-Antispam-Message-Info: 2FQpSGwMJKYbLTG8EfM3ZFJ/6LgqWRgEtL9d+Fi1LyCRoctong8BeyFLap0j2itoW7i4PPT72aA52Xe6pOflvRlXmZD5OJxQPBuDJTn0WDPviGhKjnx8Q60nV/ITSdqbNMeo6Kke+zmKtwpIC4DjJvxBXM4c3dvYlVsxatv6u123wbpolGdkrNf2jd6l128XbtLD538/qQMqTFazhgHWFxxJErh+eQwhbH6hXIWDxIblceUqyLLAumU4hPyyeP5ppTR1ndP7jE6LKhfOcyXrZE/WBiWyKqinhl62Xz9tDpEqBF45NCVEXcCot4YTmTup7wlP/DjLKnU7+iKo6L88VTmbWdMKWOeVOsFVV1KDSJUMMMdEU/heI3m9CdzstV9hx1tS9cCAxCuS545IO2Apjc0N5iAryIyI+XFc4k8EdZSsdIt4tZoepUG7DT+afILZvMQ/HXWJ0eeWlU54W0bwh7rf0w409HCNOQcztj8q1myPr1+kW48aL2IblMWOsyrNTB7ea2F/9j+9FyXDuvKF6yng8QVD2+P5VCszTSBmqVrh8BqfDGeFvJg+U222pTbd+317VJ19MsRl7QyKkA7xjIxfASuAJZUurXiWI8DlNqtClmaaw68guTWfplxkDVDzMBuWi+oPl9Gais2w2NcttdvDwBWid1/oM41G3uaG0pw= 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)(23010399003)(366016)(1800799024)(376014)(7416014)(18002099003)(22082099003)(10067099003)(261026121527099003)(56012099006)(260925022911599003)(260925021311599003)(261102123227099003)(260925021911599003)(261028144727099003)(261026140227099003)(261022075727099003)(261022105027099003)(261021123027099003)(261021121427099003)(261102130727099003)(261009223027099003)(5023799004)(11063799006)(4143699003)(6133799003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?SzVnR1l2YVVNaWZTbzVTTGZqcmYvbTAwa2EzcHdUeWFTVGpPK3c5d21PWCtI?= =?utf-8?B?eTk0Vit2c3lPL3d6N3YyNjI2Lzk4elZabFVLUElMTlRwUlJ0K0xqZWJSa0dn?= =?utf-8?B?UUVIbG50OEF2U2JlZHV1N3F4SFZDb2xaN1VzVzFhVkxsdnVtelpYNkZ6dFZm?= =?utf-8?B?c0haMHdwdTZTTDFmL1ZGZ1pJNVdZWllFcFp6TUs1NG03VUN2U1Q4VFkyMGlC?= =?utf-8?B?TlM3MWcrc05rbE4yMFZIaUI3MEpKYTQ5R1p0TGJLMzJVL1A5U0Q1a05UY3gz?= =?utf-8?B?UUYvL2xoQTJPOXRHSUZmUGgzcmJmUW9aaEFWZXZ3T2ovb1pHV3JQdHJ2U1ZJ?= =?utf-8?B?ZFRYUEdNRnZZcVU5bUFYelIrRmJJVllienhMeXowa0Y3dUlLL1lCTnp6UnBl?= =?utf-8?B?RmhMUTcxOHlQek1YeEpJL2ljc0k0TFgwZnNxVC9RMXMvSm51SWpRYW1yYlZw?= =?utf-8?B?RUxJUjh5TXRaaERuRHdrODY1Z0toVkI3cUdHYXBkYmp0VnlIT1JSbkEybkR2?= =?utf-8?B?OVZ4Y05ESWJaL1FaZk9wenBOQi9QN0pSb2svNkVwN1RrNkdjMHhSMFRLMXdM?= =?utf-8?B?RDRCU014alFJem83Nnlkd0g3QnNQdXVTd01yZjFycndqOW1lYUNlZFFMeExM?= =?utf-8?B?ZUJ3WXNDTlM1UlpQNkdZWmsvZ1BCSStCdzVsakNUdkFUazZnaE85V3JLdlNK?= =?utf-8?B?all0MzdtejdONkdROFltT2ZTbjhxSVgvUTd5RGlUVHRvcG43NENuK0x0Y2I0?= =?utf-8?B?Z1h3aFkyNlBCUStaTkRORFF5b3RmQmp3eU5MT0lJRkJOZzFZNzJLd0pEZzdC?= =?utf-8?B?akgxcEl5MnNTaEljSWV6SWlwOTNtNkF3YXhrZ0crUDg1bEVtSndtRTJtbDFK?= =?utf-8?B?dm5HK0tBV0JBK1k1ZitWUnNvQXRZd1RQZHB0WmRzNVRPQlJuME9qdDlYSGNK?= =?utf-8?B?TVhzbHY3NVJzSzdFYXFvSWZXSXRpaHNLWTRQN0xpTEl4TmNQTU5obkJtUzdu?= =?utf-8?B?QUU1eWZmd2lHeUFKbzFCTDNsemlZbDYvTVhkY0dzZmZkYXdKWjJJRzNOMHl0?= =?utf-8?B?cDQ4NTFQSUVyYmd2eXg0YXhBZjJkb3pnRGpSMW12UkJzMG5XNDVGek5oY2tx?= =?utf-8?B?S1pKZ3JxUFF6NGhDb3ZmOFZ5RGI5OTN0SVFpNlFHbGwvcUNtY1loK3RHQ2Uy?= =?utf-8?B?dkVKblpGb3RtM1lxUkVpeGxOMkNXYy9JQ2IzcmdxN015TlYzS00xTG5EamVy?= =?utf-8?B?RUxjUStRajRES1RIb3ZFeVJnSEVhb0NBanpZZ0w4Y3lQZHRFTjFsNm83K2Fz?= =?utf-8?B?NVhMeGl3bjIzcDlpZUxwMXFra2sxYVhVZEpXYzJwZ0d2L3RXWmZzbUhBZEwr?= =?utf-8?B?dEZFZG5PbkRPYkJnK2puU045Z2MyUkdkNzdrNExweG9QSk54V0p3c3dzVFlL?= =?utf-8?B?TUI0N3p5SEtKVG4vcXRScnpzOUhiMDRSMFI3MUxOTjV4ZkxhcGVkd2x1UXJC?= =?utf-8?B?TkV4QVZUYThtMXNLYWRmRk5aWXdWNkdWNnNkSzVwcFE3dHhHZkIxS3k0ZlVN?= =?utf-8?B?QWlrdCtXNnA1cjdhTXMzYjd1VllkY0VQak9nVWQ0WnJ0dDRYUWc0WFlxQ1Zp?= =?utf-8?B?Tk01SHNRNllRKzJLR2Z6ajZFOVMvNnJEOTJqdVdjeGh0U21UVEdGOGZDaDJj?= =?utf-8?B?Y0RabFJEeW5PeW92M3RLLzNGSlU5UUZST2s4TmxoY3V2TnpaZE1mTGpHZ29U?= =?utf-8?B?UnJlSjJoTkw0Sno2KzcreWpoZWRRSzdGRG9nUzFXdUtsK0prbVpQd2NneEhi?= =?utf-8?B?bHdLWkY0V0twWVNNSDBCSmQzYU1HVFEyalNuM0ZDOWxrRjNpWmZiVVMwVGVH?= =?utf-8?B?b1Y4V3F1MDQ5ekkxbW1UVUtMR0hYTy9vUXpTOGJCM3JhUU5xWmpWTENFOWpD?= =?utf-8?B?eHdaRXh2Tm95VndzVnhGQnRWakZhYjhzYkVyVzRWL1Yzc1EydGlHZDVxbDNt?= =?utf-8?B?RU5rUTIrczMzUTJpK0RtZjliYXlsazc0UGxGTjZ4TUhHUDVSTmFOTmF3aWZj?= =?utf-8?B?Q0tRTjFwRXhiNFF6cFlOWUwzb0Z3QWtkaFdiZUZabzQ0MHdmcWZMMFE5Vm1S?= =?utf-8?B?TXVFaEdTZjQrU0FwaWpWODNCWStBWkFaWjNJZGZPNmRhRmJ6aS9EUUJudXRo?= =?utf-8?B?dU1NMUVQTnBOWDBnamwyRU5GVnNyRDBLUGNuMVNBZkxleHpPQncrSXdZYVNw?= =?utf-8?B?Q0R5Mm5neVN6UEcxSE9WcSs2bkNlZCt6L210ZTBDcjJBWFkwUDQ5dS9mZFVz?= =?utf-8?B?UWFPUUFiaElTdlRuL0d6V1BTNUpKNlJRaitVUGlVL0RIWmZNNUFMQT09?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e97e4555-0ae6-4b68-9b3c-08df51ac270c X-MS-Exchange-CrossTenant-AuthSource: CY5PR12MB6549.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2026 02:13:14.2615 (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: npMKwb3SYkUipKH7SlOF342zo+Nyjz767hAIYyveaj9Q/J3ehjVq/060SxAMtxjos8AnUbKtwBYQuGGtN0fOUA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CYYPR12MB8750 On 12/2/2026 11:19 AM, Gary Guo wrote: > On Fri Nov 27, 2026 at 10:49 PM GMT, Simon Song wrote: >> From: Danilo Krummrich >> >> Add Rust abstractions for PCI drivers that extend vfio-pci-core with >> device-specific operations. >> >> Provide Operations, Device and Registration with separate registration >> and first-open/last-close data. Allow open data to borrow registration >> data, undo PCI enablement if open fails, and drop open data before >> closing the core device. >> >> Let a driver register its VFIO device with Registration::new(), for >> example from its PCI probe callback, and unregister it by dropping the >> Registration. Let drivers forward PCI reset and error callbacks through >> the Registration. Let vfio-pci-core select its VGA arbitration >> callbacks. Because runtime PM support follows in a separate patch >> series, keep the device active while it is registered. >> >> Restrict core I/O helpers to their corresponding callback contexts. Add >> region mapping helpers, and wire up VFIO core support for device >> features, matching, IOMMUFD and PASID. Allow drivers to export physical >> BARs as DMA buffers with per-region length limits. >> >> Add pci::ErsResult::from_raw() for the result of the VFIO PCI core's >> error handler. >> >> Signed-off-by: Danilo Krummrich >> Signed-off-by: Zhi Wang >> Signed-off-by: Simon Song >> --- >> rust/bindings/bindings_helper.h | 2 + >> rust/kernel/pci.rs | 12 + >> rust/kernel/vfio.rs | 24 +- >> rust/kernel/vfio/pci.rs | 812 ++++++++++++++++++++++++++++++++ >> 4 files changed, 842 insertions(+), 8 deletions(-) >> create mode 100644 rust/kernel/vfio/pci.rs >> >> diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h >> index a5781bc1ea85f..b052f1e874e25 100644 >> --- a/rust/bindings/bindings_helper.h >> +++ b/rust/bindings/bindings_helper.h >> @@ -94,6 +94,7 @@ >> #include >> #include >> #include >> +#include >> #include >> #include >> #include >> @@ -117,6 +118,7 @@ const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN; >> const size_t RUST_CONST_HELPER_ARCH_KMALLOC_MINALIGN = ARCH_KMALLOC_MINALIGN; >> const size_t RUST_CONST_HELPER_PAGE_SIZE = PAGE_SIZE; >> const unsigned int RUST_CONST_HELPER_VFIO_DEVICE_RESET = VFIO_DEVICE_RESET; >> +const unsigned int RUST_CONST_HELPER_VFIO_DEVICE_PCI_HOT_RESET = VFIO_DEVICE_PCI_HOT_RESET; >> const size_t RUST_CONST_HELPER_GENLMSG_DEFAULT_SIZE = GENLMSG_DEFAULT_SIZE; >> const gfp_t RUST_CONST_HELPER_GFP_ATOMIC = GFP_ATOMIC; >> const gfp_t RUST_CONST_HELPER_GFP_KERNEL = GFP_KERNEL; >> diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs >> index 3fa6831fe34d8..d6f5b0a9dfc7f 100644 >> --- a/rust/kernel/pci.rs >> +++ b/rust/kernel/pci.rs >> @@ -428,6 +428,18 @@ pub enum ErsResult { >> } >> >> impl ErsResult { >> + #[cfg_attr(not(CONFIG_VFIO_PCI_CORE = "y"), expect(dead_code))] >> + pub(crate) fn from_raw(result: bindings::pci_ers_result_t) -> Option { >> + match result { >> + bindings::pci_ers_result_PCI_ERS_RESULT_NONE => Some(Self::None), >> + bindings::pci_ers_result_PCI_ERS_RESULT_CAN_RECOVER => Some(Self::CanRecover), >> + bindings::pci_ers_result_PCI_ERS_RESULT_NEED_RESET => Some(Self::NeedReset), >> + bindings::pci_ers_result_PCI_ERS_RESULT_DISCONNECT => Some(Self::Disconnect), >> + bindings::pci_ers_result_PCI_ERS_RESULT_RECOVERED => Some(Self::Recovered), >> + _ => None, >> + } >> + } >> + >> fn as_raw(self) -> bindings::pci_ers_result_t { >> match self { >> Self::None => bindings::pci_ers_result_PCI_ERS_RESULT_NONE, >> diff --git a/rust/kernel/vfio.rs b/rust/kernel/vfio.rs >> index f49ff5931df56..ff8b17819ca58 100644 >> --- a/rust/kernel/vfio.rs >> +++ b/rust/kernel/vfio.rs >> @@ -3,7 +3,12 @@ >> //! 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`. >> +//! [`Device`] and the bus-specific devices are aliases of it, such as >> +#![cfg_attr( >> + CONFIG_VFIO_PCI_CORE = "y", >> + doc = "[`vfio::pci::Device`](kernel::vfio::pci::Device)." >> +)] >> +#![cfg_attr(not(CONFIG_VFIO_PCI_CORE = "y"), doc = "`vfio::pci::Device`.")] >> //! >> //! C header: [`include/linux/vfio.h`](srctree/include/linux/vfio.h) >> >> @@ -28,6 +33,9 @@ >> ptr::NonNull, // >> }; >> >> +#[cfg(CONFIG_VFIO_PCI_CORE = "y")] >> +pub mod pci; >> + >> /// Reset the VFIO device. >> pub const DEVICE_RESET: u32 = bindings::VFIO_DEVICE_RESET; >> >> @@ -91,7 +99,7 @@ impl IoContext for GetRegionInfo {} >> >> /// The data the driver of a [`Device`] shares with its callbacks. >> /// >> -/// Bus-specific operations traits extend this trait. >> +/// Bus-specific operations traits, such as `vfio::pci::Operations`, extend this trait. >> pub trait Operations: Sized + Send + Sync + 'static { >> /// Data owned by the registration and passed to VFIO callbacks. >> /// >> @@ -127,7 +135,7 @@ struct CallbackData { >> /// `raw` is the first field, so the `vfio_device` at the beginning of `Raw` is also at the >> /// beginning of `Self`, as `vfio_alloc_device()` requires. >> /// >> -/// Use the alias [`Device`]. >> +/// Use the aliases [`Device`] and `vfio::pci::Device`. >> /// >> /// # Invariants >> /// >> @@ -148,7 +156,7 @@ pub struct DeviceBase { >> /// A VFIO device, wrapping `struct vfio_device`. >> pub type Device = DeviceBase; >> >> -#[expect(dead_code)] >> +#[cfg_attr(not(CONFIG_VFIO_PCI_CORE = "y"), expect(dead_code))] > > There're so many of these attr. > > How about only enabling vfio abstraction if it's not going to be dead_code, or > do a global `#![cfg_attr(not(CONFIG_VFIO_PCI_CORE = "y"), expect(dead_code)]`? > Thanks, i will use file-wide attributed as you suggested. >> impl DeviceBase { >> /// Initializes the Rust fields of a newly allocated device. >> /// >> @@ -193,7 +201,7 @@ fn as_raw_vfio(&self) -> *mut bindings::vfio_device { >> } >> } >> >> -#[expect(dead_code)] >> +#[cfg_attr(not(CONFIG_VFIO_PCI_CORE = "y"), expect(dead_code))] >> impl DeviceBase { >> /// Publishes the registration data passed to callbacks. >> /// >> @@ -240,7 +248,7 @@ unsafe fn close(&self) { >> } >> } >> >> -#[expect(dead_code)] >> +#[cfg_attr(not(CONFIG_VFIO_PCI_CORE = "y"), expect(dead_code))] >> impl DeviceBase { >> /// Creates the open data from the registration data. >> /// >> @@ -284,7 +292,7 @@ unsafe fn open<'a, 'reg: 'a, I>( >> } >> } >> >> -#[expect(dead_code)] >> +#[cfg_attr(not(CONFIG_VFIO_PCI_CORE = "y"), expect(dead_code))] >> impl DeviceBase { >> /// Returns the registration data and the open data, for an I/O callback. >> /// >> @@ -345,7 +353,7 @@ unsafe impl Sync for DeviceBase {} >> >> /// Capability buffer supplied by VFIO for a region-info callback. >> pub struct InfoCap<'a> { >> - #[expect(dead_code)] >> + #[cfg_attr(not(CONFIG_VFIO_PCI_CORE = "y"), expect(dead_code))] >> raw: &'a mut bindings::vfio_info_cap, >> } >> >> diff --git a/rust/kernel/vfio/pci.rs b/rust/kernel/vfio/pci.rs >> new file mode 100644 >> index 0000000000000..6805915414b55 >> --- /dev/null >> +++ b/rust/kernel/vfio/pci.rs >> @@ -0,0 +1,812 @@ >> +// SPDX-License-Identifier: GPL-2.0 >> + >> +//! VFIO PCI variant driver abstractions. >> +//! >> +//! Provides Rust abstractions for writing VFIO PCI variant drivers (also known >> +//! as "VFIO PCI core" drivers). A [`Registration`] struct registers the VFIO >> +//! device and owns its registration data, tying resource lifetimes to the PCI >> +//! driver's binding scope. >> +//! >> +//! C header: [`include/linux/vfio_pci_core.h`](srctree/include/linux/vfio_pci_core.h) >> + >> +use super::{ >> + DeviceBase, >> + InfoCap, >> + UserBuf, // >> +}; >> +pub use super::{ >> + DeviceContext, >> + GetRegionInfo, >> + IoContext, >> + Ioctl, >> + Mmap, >> + Normal, >> + Open, >> + Read, >> + Write, // >> +}; >> + >> +use crate::{ >> + alloc::allocator::Kmalloc, >> + bindings, >> + device, >> + error::{ >> + from_err_ptr, >> + from_result, >> + to_result, // >> + }, >> + mm::virt::VmaNew, >> + pci, >> + prelude::*, >> + sync::aref::ARef, >> + types::NotThreadSafe, >> + uaccess::UserPtr, // >> +}; >> +use core::{ >> + alloc::Layout, >> + ptr::NonNull, // >> +}; >> + >> +/// Reset the PCI slot or bus containing a VFIO device. >> +pub const DEVICE_PCI_HOT_RESET: u32 = bindings::VFIO_DEVICE_PCI_HOT_RESET; >> + >> +/// Index of the PCI configuration-space region. >> +pub const CONFIG_REGION_INDEX: u32 = bindings::VFIO_PCI_CONFIG_REGION_INDEX; > > Can these be new types? > ah, DEVICE_PCI_HOT_RESET should actually be removed since we no longer should intercept HOT_RESET as previous v3 had reviewed for CONFIG_REGION_INDEX a newtype feels a bit overkill for now, it would adding conversion where index is used as a BAR number, i would keep it for now. >> +impl Device { >> + /// Allocate a VFIO PCI device for subsequent registration. >> + pub fn new(pdev: &pci::Device>) -> Result> { >> + let size = Kmalloc::aligned_layout(Layout::new::()).size(); >> + >> + // SAFETY: The bound PCI device and static ops are valid. The allocation >> + // includes the full Rust wrapper with kmalloc-compatible size and alignment. >> + let raw = from_err_ptr(unsafe { >> + bindings::_vfio_alloc_device(size, pdev.as_ref().as_raw(), &Self::OPS) >> + })?; > > This doesn't make sure alignment is correct. > The current wrapper's alignment is within a page; I've made that a compile-time assertion and documented how both kvzalloc() paths satisfy it. >> + let this = NonNull::new(raw.cast::()).ok_or(ENOMEM)?; >> + >> + // SAFETY: `vfio_pci_core_init_dev()` initialized the C device in this new allocation, >> + // which nothing else can access yet. `vfio_pci_core_device` begins with its >> + // `vfio_device`, as asserted above. >> + // INVARIANT: The layout and the initialized `raw` are as just described. >> + unsafe { Self::init(this.as_ptr()) }; >> + >> + // SAFETY: No registration or callbacks can observe this allocation yet. >> + unsafe { (*this.as_ref().as_raw()).pci_ops = &Self::PCI_OPS }; >> + >> + // SAFETY: `this` owns the initial VFIO device reference. >> + Ok(unsafe { ARef::from_raw(this) }) >> + } >> +} >> >> [snip] >> >> + >> +/// Build the `vfio_device_ops` vtable for a variant driver `T`. >> +/// >> +/// The open callback and the implemented I/O callbacks dispatch to `T`; unimplemented I/O >> +/// callbacks are not installed. Other installed callbacks use VFIO core functions or wrappers. >> +macro_rules! build_ops { >> + ($T:ty) => { >> + bindings::vfio_device_ops { >> + #[allow(clippy::disallowed_methods)] >> + name: <$T>::NAME.as_ptr().cast_mut().cast(), >> + init: Some(bindings::vfio_pci_core_init_dev), >> + release: Some(bindings::vfio_pci_core_release_dev), >> + open_device: Some(open_device_cb::<$T>), >> + close_device: Some(close_device_cb::<$T>), >> + ioctl: if <$T>::HAS_IOCTL { >> + Some(ioctl_cb::<$T>) >> + } else { >> + None >> + }, > > None here (and below) means returning EINVAL instead of forwarding to core > callbacks. Is this what's whated? Most existing vfio pci variant drivers forward > un-implemented callbacks. > > Perhaps it's worth changing the C side to switch the meaning, but regardless we > should consider what's actually the desired behaviour when people use Rust > abstraction's trait "provided methods". > this is intended and follows the C API, as Danilo wrote in v3