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.52.63 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796200642; b=1Nk4VN6yBskl6VZN5AkxXKgb+PgJtq/29USRLv0oHSJK4MuFylmlwxPuHDJzVk1dpVdd XN0M7SMIX01c6rcPmkdsfQxVQHUaguYeM6fh+vcriS9E3AKbIvQjtAMcFC3CMaAgZ/cRQ SGpN50dWtGVMXuweVwoDXif6qak+sCkYrQuBE5cUwMpv6zCufH0CXGTdNn4LgQI9ydOe/ R2P8bh/idYUq6syneGlWFP646f2ioZtpexLklR7js1YZ3PoxeIpuHMKDTLYpzKheqrz3W jg4zUXPhHbDkdycZNWKzwtOP1kvym3aw67QhWTHhRYas2yxIO91bdY/Ie6A19hbT29A== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796200642; h=DMARC-Filter:DKIM-Signature:Received:Received: X-MS-Exchange-Authentication-Results:Received-SPF:Received:Received: Received:From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding:Content-Type: X-Originating-IP:X-ClientProxiedBy:X-EOPAttributedMessage: 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-OriginalArrivalTime: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-Id: X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=tHk5JyYW2OeZK4P85Aww2ZO0kAcMbJZCR3gad88XGVc=; b=2izPkgMy8epXGfosgzSDCxBnsVG028hhDJcdCC+FTFR0vuZOOrnCazAffvPIwj/0+ZNK HnQBP7B/+Hyg0ehuoMk6q2acMu0uy6Jl7WOg8yav7BHhHix6T6eqZdMI3QrCLjw33xQhX drn66jGr1R+f7NdO1l3XGfVsgAyEsBE8KorAphwgJ2Zg0wEVCmGkrbc8GQFkuu3vk3+TS Jz2jbiItkKZJrgJgPJ9iK6AUxWdxi7viz8sYffx2JGZuLAWfPF1ryzbKLtlpBRLHjkOQ7 /TFzhxcZqMHn4Uf66nFfwBO47kK9DdgVQ+ACz+h0n5mY7llL+PZEvOYCloe3OITiYVw== 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=pDduEiEg; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.52.63 Received: from BL2PR02CU003.outbound.protection.outlook.com (mail-eastusazon11011063.outbound.protection.outlook.com [52.101.52.63]) (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 3784D606A323; Wed, 2 Dec 2026 08:37:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3784D606A323 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=pDduEiEg ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Kx3KEFfJW2VvZVz9Swt+spTpxQQ8bL462G9NUf/qQRRvTtpPRKdskA/P4OAwd7pz25R6P+avlYgh6IttUlw2TOKgOeqZdNGv3469EgbKtifmtADW+3YlzHO0GJhxTi5V3s+5APx5G1/cxlzmnpmgEvL5y6mZJwZ30al9OgcV32+pZZ5vI5ZmY3aqKd5gMehd6Av8MEJvB+EKIDdNWGCW+eex3VujJ/dSQy7DswB8d869isW9YvrekDBnZOymPyJP5+M4jCl6+dvW/pt1jOL4X2yNHqkJUzPOOwLTSm+ti5u5hY8enjiQ6RWQVK/LqOyrRNTR7zReVfFByowaFy7WyA== 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=tHk5JyYW2OeZK4P85Aww2ZO0kAcMbJZCR3gad88XGVc=; b=bP/Ezs2VM5kQatsg6M5IIHA3Zx2hyBrGNvecMb68Ucch1m0gLUFvzgLYcDNfmEWQoKJw+3nwtluan0Rw4Uy6iKmRvDYwabAbhnnJbYPaxkJGRT+mOQ0jq0kwWYnwBOKcNK70YpdD3GDTAfzAVrfgHX/J6VcOLUmIg8GjQx8o3KwG0usm9UkO/2EAZHc69G6SZR32XW+xGZNO5BWO/z3vh07HK3xv+VqMe0j5gttBxXDdrdD/+1rc+mFUmFie2dpkoKq4EuYQ7tfxFOv5LLK/5YLzmuySwctFPWwlU0lpc9N3WQoQxgOQGZTy/Ox/32E/Y+EY+jlJhbOd1Y04THUXeQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=tHk5JyYW2OeZK4P85Aww2ZO0kAcMbJZCR3gad88XGVc=; b=pDduEiEg2guFga7NaPUdYnoZ4+JlBfPYtRKuSjo20azYMLR280olm51SryFejVopa6kuO/HMX1dk0O1MgVnetJJg7ASC2ez1ak0FzXX2Jfgn4+CwJ+pfOXwlsTwBoUHDKnUSo/vXNgPIRdzYR767/Lte0S/TZR8YD4Z6fpgpHLTST8/WOcUDPUD4QKAFYzidz4DNXDd+LvwynsZtjtWFHxqP9lVdiO6cxW5QBGzlM9u6X3gnqtl6laIC2ulfYLZe6vfr39YEPuqxuRJGxONyo0lsYyMNcDxPgyH6fUyQoJVd2XTl1Oeui7fmPr5Eqtx83dPRIdx79yfOh7Y9pLdRmg== Received: from BN0PR02CA0054.namprd02.prod.outlook.com (2603:10b6:408:e5::29) by AT6PR12MB529233.namprd12.prod.outlook.com (2603:10b6:170:115::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.11; Wed, 2 Dec 2026 08:37:14 +0000 Received: from LV8PEPF0000005C.namprd02.prod.outlook.com (2603:10b6:408:e5:cafe::80) by BN0PR02CA0054.outlook.office365.com (2603:10b6:408:e5::29) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.688.10 via Frontend Transport; Wed, 2 Dec 2026 08:37:14 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by LV8PEPF0000005C.mail.protection.outlook.com (10.167.245.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.5 via Frontend Transport; Wed, 2 Dec 2026 08:37:14 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Wed, 2 Dec 2026 00:36:56 -0800 Received: from kalok-SPC741D16QM3.nvidia.com (10.126.231.37) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Wed, 2 Dec 2026 00:36:42 -0800 From: Alok Kumar To: , , , , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v6 20/34] gpu: nova-core: vgpu: add VRAM slot pool Date: Wed, 2 Dec 2026 13:58:44 +0530 Message-ID: <626c495fc37f20e1543cc04ce5daad049e5b7484.1796191007.git.alkumar@nvidia.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: rnnvmail202.nvidia.com (10.129.68.7) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV8PEPF0000005C:EE_|AT6PR12MB529233:EE_ X-MS-Office365-Filtering-Correlation-Id: adb3a6c5-a45a-40e0-3953-08df504f774d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|23010399003|36860700016|1800799024|82310400026|10067099003|56012099006|11063799006|261021121427099003|261009223027099003|261102123227099003|261022075727099003|261022113327099003|261026121527099003|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: y28KomfmbP+gGz3UhgIUlZ2oER4j75wmeiRAv7/Ta1XIyZaScNc94wKTIz+9b3JUd7T2/ePINxfOEIgmvyIV21XkPxbt4WzLeVcKboLsAkNe8sdpRnC/SbklfipQnAtCAsSRZCclBLBmi18BLPoUhAPs3lXKPCdFDNyVssgp/5iso5/qU/UOc3iWWNGKjypgt/ze8CYXDCluiMSIAiTAv8+TExTgFoKAeVfJpmyTLYWDEEosUhnn/yz7ucvwrvV5hXxVwVviHuoZU0bW75SDyc4vPwdGgdvL3sqs7aopEezhyhdBCZ4rDMo+3Qdz0fR+bBM+tl6BLMcUQxFfdQeByCIZPQGBJEJi2nw2z789ZLG1vbllPjRoOn1ukz+VTdG8Egr6zzuiC9Ny/rixWe/WZmsYWW3Yr7JCSCfqGY+hEKt2ezTnFYIwFQUUSxPK4uaYxbdPSdMtX8VG2jpjY96yrdQU5Mw48knd0SS9JwkRHaehLD2oJjfsppOcL7fMjZ53QMROMF5GJaEn0K3C9TowtnFINGZFgBFKNrp4f/m4HPRTr5vQUqxIyUuxGnLfaOBRTnIv/c9Nq8FRP/tI0TKYkctBv2UOxjLrDMWMzUKlWp6Gnigq1S6DH7FV13UMJ6xkuRt9+b5VmRT1LaFCugCAqOwjus2cnXqqnt70Af2OwQL+IQQ97gNwjg2/B2VuUkq48ecoHPlFKRpQ4UhkdZFfiw== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(376014)(7416014)(23010399003)(36860700016)(1800799024)(82310400026)(10067099003)(56012099006)(11063799006)(261021121427099003)(261009223027099003)(261102123227099003)(261022075727099003)(261022113327099003)(261026121527099003)(18002099003)(22082099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qzGUx5UZpbffmXjzOAlcOZFI9h31KpWdzh/6291p7B/d2ousvuRwNQu6UwTNHGU88GEkQ6uWMAl+6LQ1bKkL6suryZfM8Jla2RXFdjwPuGEO3mNhHiYdvFnN0LLNVmDddWvALaEJXq5pUuh2f0Cpm0/xKw3+MBAUNX7YJ7sFvT2R/yi+q95N9tHzEGnWKHzqVW4wzhCzeHOqz0WWhLlmOVb0oxDv7CQQQ9gV3EjHqQiCgh/rcfoVkSoC123it9DVtUqe1G04aXO9Gr99B1V9gGLo3/3KsDIIbwwQYo30Sx1DWDD/TlyT/myrd0+QIfBS0Kiz4+cGAGTVm/2FhrCnbHtlkj3NWMl5k7xsyzPbQSPyB2E1rFHJebmbaptO57u71ITKEzQB7ZdYHqVeGESMisLw18Mr0vikql1OjJ5WJNtoQUCVaKYR65CHkZmK4J5Q X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Dec 2026 08:37:14.2663 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: adb3a6c5-a45a-40e0-3953-08df504f774d X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: LV8PEPF0000005C.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AT6PR12MB529233 A vGPU type specifies the guest VRAM and GSP plugin management-heap sizes for each instance. Allocating these regions independently fragments VRAM and does not preserve a stable layout for the vGPU type. Add a standalone pinned slot pool that reserves one exact VRAM range for all slots of one layout. Embed its VramBlock directly in the pool and provide borrowed framebuffer and management-heap regions for each slot. Carry the pool borrow in VgpuVramSlot so a slot cannot outlive its backing allocation. Dropping the pool drops its block and returns the reservation to the buddy allocator after all borrowers and device accesses have ended. Co-developed-by: Zhi Wang Signed-off-by: Zhi Wang Co-developed-by: Alexandre Courbot Signed-off-by: Alexandre Courbot Signed-off-by: Alok Kumar --- drivers/gpu/nova-core/vgpu.rs | 1 + drivers/gpu/nova-core/vgpu/vram.rs | 159 +++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 drivers/gpu/nova-core/vgpu/vram.rs diff --git a/drivers/gpu/nova-core/vgpu.rs b/drivers/gpu/nova-core/vgpu.rs index b2b595003f8f..bf5d00a9e1cf 100644 --- a/drivers/gpu/nova-core/vgpu.rs +++ b/drivers/gpu/nova-core/vgpu.rs @@ -23,6 +23,7 @@ mod fw; mod hal; +mod vram; /// vGPU state detected during GPU construction. #[derive(Debug, Clone, Copy)] diff --git a/drivers/gpu/nova-core/vgpu/vram.rs b/drivers/gpu/nova-core/vgpu/vram.rs new file mode 100644 index 000000000000..b6356a9765e2 --- /dev/null +++ b/drivers/gpu/nova-core/vgpu/vram.rs @@ -0,0 +1,159 @@ +// SPDX-License-Identifier: GPL-2.0 +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + +//! VRAM slot allocation for vGPU instances. + +use kernel::{ + prelude::*, + sizes::SizeConstants, // +}; + +use crate::mm::{ + vram::{ + VramBlock, + VramRegion, // + }, + GpuMm, // +}; + +const VRAM_SLOT_MIN_ALIGN: u64 = u64::SZ_4K; +// `VRAM_SLOT_MIN_ALIGN` is used for alignment. +static_assert!(VRAM_SLOT_MIN_ALIGN.is_power_of_two()); + +/// Describes the VRAM layout used by vGPU instances. +#[derive(Clone, Copy, PartialEq)] +pub(super) struct VgpuVramLayout { + // Firmware ID of the vGPU type this layout was built for. + type_id: u32, + // Maximum number of slots with this layout. + max_slots: u32, + // Size of the framebuffer for a vGPU instance. + fb_size: u64, + // Size of heap for a vGPU instance. + heap_size: u64, + // Alignment of the framebuffer for vGPU instances. + fb_align: u64, +} + +#[expect(dead_code)] +impl VgpuVramLayout { + pub(super) fn new( + type_id: u32, + max_slots: u32, + fb_size: u64, + heap_size: u64, + fb_align: u64, + ) -> Result { + if max_slots == 0 || fb_size == 0 || heap_size == 0 { + return Err(EINVAL); + } + + let fb_align = core::cmp::max(fb_align, VRAM_SLOT_MIN_ALIGN); + + if !fb_align.is_power_of_two() + || !fb_size.is_multiple_of(fb_align) + || !heap_size.is_multiple_of(VRAM_SLOT_MIN_ALIGN) + { + return Err(EINVAL); + } + + Ok(Self { + type_id, + max_slots, + fb_size, + heap_size, + fb_align, + }) + } + + pub(super) fn max_slots(&self) -> u32 { + self.max_slots + } +} + +/// A description of a VRAM slot. +/// +/// All device accesses and mappings of its regions must end before dropping the slot. +#[must_use] +#[expect(dead_code)] +pub(super) struct VgpuVramSlot<'pool> { + /// Framebuffer VRAM region. + pub(super) fbmem: VramRegion<'pool>, + /// Management heap VRAM region. + pub(super) mgmt_heap: VramRegion<'pool>, + /// Index of this slot. + pub(super) index: u32, +} + +/// A pool of VRAM slots, split according to a [`VgpuVramLayout`]. +#[pin_data] +pub(super) struct VgpuVramSlotPool { + #[pin] + backing: VramBlock, + layout: VgpuVramLayout, + fb_region_size: u64, +} + +#[expect(dead_code)] +impl VgpuVramSlotPool { + /// Reserve the VRAM for all possible slots of `layout`, and returns a [`VgpuVramSlotPool`] that + /// can provide these slots. + pub(super) fn new(mm: &GpuMm<'_>, layout: VgpuVramLayout) -> Result> { + let max_slots = u64::from(layout.max_slots); + // Keep guest VRAM slots contiguous so each starts at `fb_align`; + // interleaving page-aligned heaps could misalign later slots. + let fb_region_size = layout.fb_size.checked_mul(max_slots).ok_or(EINVAL)?; + let heap_region_size = layout.heap_size.checked_mul(max_slots).ok_or(EINVAL)?; + let pool_size = fb_region_size.checked_add(heap_region_size).ok_or(EINVAL)?; + + let backing = mm.alloc_vram_range(0..pool_size, cv!(VRAM_SLOT_MIN_ALIGN))?; + Ok(try_pin_init!(Self { + backing <- backing, + layout, + fb_region_size, + _: { + if !backing.range().start.is_multiple_of(layout.fb_align) { + Err::<(), Error>(EINVAL)?; + } + }, + })) + } + + pub(super) fn max_slots(&self) -> u32 { + self.layout.max_slots() + } + + /// Returns `true` if `layout` is identical to the layout used by this pool. + pub(super) fn matches_layout(&self, layout: VgpuVramLayout) -> bool { + self.layout == layout + } + + /// Returns the VRAM slot corresponding to `index`, or `EINVAL` if `index` is not a valid slot + /// index for the pool's layout. + pub(super) fn slot(&self, index: u32) -> Result> { + if index >= self.layout.max_slots { + return Err(EINVAL); + } + + let fb_size = self.layout.fb_size; + let fb_offset = fb_size.checked_mul(index.into()).ok_or(EINVAL)?; + let fb_end = fb_offset.checked_add(fb_size).ok_or(EINVAL)?; + + let heap_size = self.layout.heap_size; + let heap_slot_offset = heap_size.checked_mul(index.into()).ok_or(EINVAL)?; + let heap_offset = self + .fb_region_size + .checked_add(heap_slot_offset) + .ok_or(EINVAL)?; + let heap_end = heap_offset.checked_add(heap_size).ok_or(EINVAL)?; + + let fbmem = self.backing.region(fb_offset..fb_end)?; + let mgmt_heap = self.backing.region(heap_offset..heap_end)?; + + Ok(VgpuVramSlot { + fbmem, + mgmt_heap, + index, + }) + } +}