Skip to contentSkip to conversation

Rust GPU buddy allocator bindings

PATCH Superseded Joel Fernandes 5 participants 17 messages Feb 24 – Mar 3

Subsystems (11)

v11 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Joel Fernandes

author reviewer
#0 lore raw

v11 cover Rust GPU buddy allocator bindings

The series adds Rust GPU buddy allocator bindings for use by nova-core.

Note: Patches 1-3 are included for reference only and should not be merged through this series. They are the DRM buddy allocator code movement patches (already being pulled into upstream via drm-misc-next by Dave Airlie) and the uninitialized buddy fix by Koen Koning (which is going in independently). They are included here as they are prerequisites for the Rust GPU buddy bindings (and could be further revised).

The series along with all dependencies, including clist and nova-core mm patches, are available in:
https://git.kernel.org/pub/scm/linux/kernel/git/jfern/linux.git/log/?h=nova/mm

Changes from v10 to v11:
- Dropped "rust: ffi: Convert pub use to pub mod and create ffi module" patch; the ffi module restructuring will go through a different path.
- Dropped "rust: clist: Add support to interface with C linked lists" patch; the clist module will be submitted separately.
- Dropped "nova-core: Kconfig: Sort select statements alphabetically" cosmetic patch.
- Patches 1-3 (DRM buddy movement and fix) are included as reference only; they are already being pulled into upstream via drm-misc-next.
- Removed clist patches as those can go in independently (Alice).
- Moved the Kconfig GPU_BUDDY selection patch to nova-core mm series to enable it when it is actually used.
- Various nits to comments, etc.

Changes from v9 to v10:
- Absorbed the DRM buddy code movement patches into this series as patches 1-2. Dave Airlie reworked these into two parts for better git history.
- Added "gpu: Fix uninitialized buddy for built-in drivers" fix by Koen Koning, using subsys_initcall instead of module_init to fix NULL pointer dereference when built-in drivers use the buddy allocator before initialization.
- Added "rust: ffi: Convert pub use to pub mod and create ffi module" to prepare the ffi module for hosting clist as a sub-module.
- Moved clist from rust/kernel/clist.rs to rust/kernel/ffi/.
- Added "nova-core: Kconfig: Sort select statements alphabetically" (Danilo).

Changes from v8 to v9:
- Updated nova-core Kconfig patch: addressed sorting of Kconfig options.
- Added Daniel Almeida's Reviewed-by tag to clist patch.
- Minor refinements to GPU buddy bindings.

Changes from v7 to v8:
- Added nova-core Kconfig patch to select GPU_BUDDY for VRAM allocation.
- Various changes suggested by Danilo Krummrich, Gary Guo, and Daniel Almeida.
- Added Acked-by: Gary Guo for clist patch.

Changes from v6 to v7:
- Major restructuring: split the large 26-patch v6 RFC series. v7 only contains the Rust infrastructure patches (clist + GPU buddy bindings), extracted from the full nova-core MM series. The nova-core MM patches follow separately.
- Rebased on linux-next.

Changes from v5 to v6:
- Rebased on drm-rust-kernel/drm-rust-next.
- Expanded from 6 to 26 patches with full nova-core MM infrastructure including page table walker, VMM, BAR1 user interface, TLB flush, and GpuMm manager.

Changes from v4 to v5:
- Added PRAMIN aperture support with documentation and self-tests.
- Improved buddy allocator bindings (fewer lines of code).
- Based on drm-rust-next instead of linux-next.

Changes from v3 to v4:
- Combined the clist and DRM buddy series into a single coherent series.
- Added DRM buddy allocator movement from drivers/gpu/drm/ up to drivers/gpu/, renaming API from drm_buddy to gpu_buddy.
- Added Rust bindings for the GPU buddy allocator.

Changes from v2 to v3:
- Squashed 3 clist patches into one due to inter-dependencies.
- Changed Clist to Clist<'a, T> using const generic offset (Alex Courbot).
- Simplified C helpers to only list_add_tail (Alex Courbot, John Hubbard).
- Added init_list_head() Rust function (Alex Courbot).
- Added FusedIterator, PartialEq/Eq impls.
- Added MAINTAINERS entry (Miguel Ojeda).

Changes from v1 (RFC) to v2:
- Dropped DRM buddy allocator patches; series focuses solely on clist module.
- Dropped sample modules, replaced with doctests.
- Added proper lifetime management similar to scatterlist.
- Split clist into 3 separate patches.

Link to v10: https://lore.kernel.org/all/[email protected]/

Joel Fernandes (3):
gpu: Move DRM buddy allocator one level up (part one)
gpu: Move DRM buddy allocator one level up (part two)
rust: gpu: Add GPU buddy allocator bindings

Koen Koning (1):
gpu: Fix uninitialized buddy for built-in drivers

 Documentation/gpu/drm-mm.rst                  |   10 +-
 MAINTAINERS                                   |   16 +-
 drivers/gpu/Kconfig                           |   13 +
 drivers/gpu/Makefile                          |    3 +-
 drivers/gpu/buddy.c                           | 1322 +++++++++++++++++
 drivers/gpu/drm/Kconfig                       |    5 +-
 drivers/gpu/drm/Kconfig.debug                 |    1 -
 drivers/gpu/drm/Makefile                      |    1 -
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c       |    2 +-
 .../gpu/drm/amd/amdgpu/amdgpu_res_cursor.h    |   12 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c  |   79 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.h  |   20 +-
 drivers/gpu/drm/drm_buddy.c                   | 1277 +---------------
 drivers/gpu/drm/i915/gem/i915_gem_ttm.c       |    2 +-
 drivers/gpu/drm/i915/i915_scatterlist.c       |   10 +-
 drivers/gpu/drm/i915/i915_ttm_buddy_manager.c |   55 +-
 drivers/gpu/drm/i915/i915_ttm_buddy_manager.h |    4 +-
 .../drm/i915/selftests/intel_memory_region.c  |   20 +-
 drivers/gpu/drm/tests/Makefile                |    1 -
 drivers/gpu/drm/tests/drm_exec_test.c         |    2 -
 drivers/gpu/drm/tests/drm_mm_test.c           |    2 -
 .../gpu/drm/ttm/tests/ttm_bo_validate_test.c  |    4 +-
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.c  |   18 +-
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.h  |    4 +-
 drivers/gpu/drm/xe/xe_res_cursor.h            |   34 +-
 drivers/gpu/drm/xe/xe_svm.c                   |   12 +-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.c          |   71 +-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h    |    4 +-
 drivers/gpu/tests/Makefile                    |    4 +
 .../gpu_buddy_test.c}                         |  416 +++---
 .../lib/drm_random.c => tests/gpu_random.c}   |   18 +-
 .../lib/drm_random.h => tests/gpu_random.h}   |   18 +-
 drivers/video/Kconfig                         |    1 +
 include/drm/drm_buddy.h                       |  163 +-
 include/linux/gpu_buddy.h                     |  177 +++
 rust/bindings/bindings_helper.h               |   11 +
 rust/helpers/gpu.c                            |   23 +
 rust/helpers/helpers.c                        |    1 +
 rust/kernel/gpu/buddy.rs                      |  536 +++++++
 rust/kernel/gpu/mod.rs                        |    5 +
 rust/kernel/lib.rs                            |    2 +
 41 files changed, 2536 insertions(+), 1843 deletions(-)
 create mode 100644 drivers/gpu/Kconfig
 create mode 100644 drivers/gpu/buddy.c
 create mode 100644 drivers/gpu/tests/Makefile
 rename drivers/gpu/{drm/tests/drm_buddy_test.c => tests/gpu_buddy_test.c} (67%)
 rename drivers/gpu/{drm/lib/drm_random.c => tests/gpu_random.c} (59%)
 rename drivers/gpu/{drm/lib/drm_random.h => tests/gpu_random.h} (53%)
 create mode 100644 include/linux/gpu_buddy.h
 create mode 100644 rust/helpers/gpu.c
 create mode 100644 rust/kernel/gpu/buddy.rs
 create mode 100644 rust/kernel/gpu/mod.rs

base-commit: 2961f841b025fb234860bac26dfb7fa7cb0fb122
prerequisite-patch-id: f8778e0b72697243cfe40a8bfcc3ca1d66160b12
prerequisite-patch-id: 763b30c69f9806de97eb58bc8e3406feb6bf61b4

Signature · 1 line
2.34.1
author reviewer
#1 lore raw

PATCH v11 1/4 gpu: Move DRM buddy allocator one level up (part one)

Move the DRM buddy allocator one level up so that it can be used by GPU drivers (example, nova-core) that have usecases other than DRM (such as VFIO vGPU support). Modify the API, structures and Kconfigs to use "gpu_buddy" terminology. Adapt the drivers and tests to use the new API.

The commit cannot be split due to bisectability, however no functional change is intended. Verified by running K-UNIT tests and build tested various configurations.

[airlied: I've split this into two so git can find copies easier. I've also just nuked drm_random library, that stuff needs to be done elsewhere and only the buddy tests seem to be using it].

---
 Documentation/gpu/drm-mm.rst                                | 6 +++---
 drivers/gpu/Makefile                                        | 2 +-
 drivers/gpu/{drm/drm_buddy.c => buddy.c}                    | 2 +-
 drivers/gpu/drm/Kconfig                                     | 4 ----
 drivers/gpu/drm/Kconfig.debug                               | 1 -
 drivers/gpu/drm/Makefile                                    | 3 +--
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.h                | 2 +-
 drivers/gpu/drm/i915/gem/i915_gem_ttm.c                     | 2 +-
 drivers/gpu/drm/i915/i915_scatterlist.c                     | 2 +-
 drivers/gpu/drm/i915/i915_ttm_buddy_manager.c               | 2 +-
 drivers/gpu/drm/tests/Makefile                              | 1 -
 drivers/gpu/drm/tests/drm_exec_test.c                       | 2 --
 drivers/gpu/drm/tests/drm_mm_test.c                         | 2 --
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.h                | 2 +-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h                  | 2 +-
 drivers/gpu/tests/Makefile                                  | 4 ++++
 .../{drm/tests/drm_buddy_test.c => tests/gpu_buddy_test.c}  | 4 ++--
 drivers/gpu/{drm/lib/drm_random.c => tests/gpu_random.c}    | 2 +-
 drivers/gpu/{drm/lib/drm_random.h => tests/gpu_random.h}    | 0
 include/{drm/drm_buddy.h => linux/gpu_buddy.h}              | 0
 20 files changed, 19 insertions(+), 26 deletions(-)
 rename drivers/gpu/{drm/drm_buddy.c => buddy.c} (99%)
 create mode 100644 drivers/gpu/tests/Makefile
 rename drivers/gpu/{drm/tests/drm_buddy_test.c => tests/gpu_buddy_test.c} (99%)
 rename drivers/gpu/{drm/lib/drm_random.c => tests/gpu_random.c} (97%)
 rename drivers/gpu/{drm/lib/drm_random.h => tests/gpu_random.h} (100%)
 rename include/{drm/drm_buddy.h => linux/gpu_buddy.h} (100%)
Documentation/gpu/drm-mm.rst +3 -3
index f22433470c76..ceee0e663237 100644
@@ -526,10 +526,10 @@ DRM GPUVM Function References
 DRM Buddy Allocator
 ===================
 
-DRM Buddy Function References
------------------------------
+Buddy Allocator Function References (GPU buddy)
+-----------------------------------------------
 
-.. kernel-doc:: drivers/gpu/drm/drm_buddy.c
+.. kernel-doc:: drivers/gpu/buddy.c
    :export:
 
 DRM Cache Handling and Fast WC memcpy()
drivers/gpu/Makefile +1 -1
index 36a54d456630..c5292ee2c852 100644
@@ -2,7 +2,7 @@
 # drm/tegra depends on host1x, so if both drivers are built-in care must be
 # taken to initialize them in the correct order. Link order is the only way
 # to ensure this currently.
-obj-y			+= host1x/ drm/ vga/
+obj-y			+= host1x/ drm/ vga/ tests/
 obj-$(CONFIG_IMX_IPUV3_CORE)	+= ipu-v3/
 obj-$(CONFIG_TRACE_GPU_MEM)		+= trace/
 obj-$(CONFIG_NOVA_CORE)		+= nova-core/
drivers/gpu/buddy.c renamed from drivers/gpu/drm/drm_buddy.c +1 -1
similarity index 99%
rename from drivers/gpu/drm/drm_buddy.c
rename to drivers/gpu/buddy.c
index fd34d3755f7c..4cc63d961d26 100644
@@ -10,7 +10,7 @@
 #include <linux/module.h>
 #include <linux/sizes.h>
 
-#include <drm/drm_buddy.h>
+#include <linux/gpu_buddy.h>
 #include <drm/drm_print.h>
 
 enum drm_buddy_free_tree {
drivers/gpu/drm/Kconfig +0 -4
index d3d52310c9cc..ca2a2801e77f 100644
@@ -269,10 +269,6 @@ config DRM_SCHED
 config DRM_PANEL_BACKLIGHT_QUIRKS
 	tristate
 
-config DRM_LIB_RANDOM
-	bool
-	default n
-
 config DRM_PRIVACY_SCREEN
 	bool
 	default n
drivers/gpu/drm/Kconfig.debug +0 -1
index 05dc43c0b8c5..3b7886865335 100644
@@ -69,7 +69,6 @@ config DRM_KUNIT_TEST
 	select DRM_EXPORT_FOR_TESTS if m
 	select DRM_GEM_SHMEM_HELPER
 	select DRM_KUNIT_TEST_HELPERS
-	select DRM_LIB_RANDOM
 	select DRM_SYSFB_HELPER
 	select PRIME_NUMBERS
 	default KUNIT_ALL_TESTS
drivers/gpu/drm/Makefile +1 -2
index ec2c5ff82382..5c86bc908955 100644
@@ -78,7 +78,6 @@ drm-$(CONFIG_DRM_CLIENT) += \
 	drm_client_event.o \
 	drm_client_modeset.o \
 	drm_client_sysrq.o
-drm-$(CONFIG_DRM_LIB_RANDOM) += lib/drm_random.o
 drm-$(CONFIG_COMPAT) += drm_ioc32.o
 drm-$(CONFIG_DRM_PANEL) += drm_panel.o
 drm-$(CONFIG_OF) += drm_of.o
@@ -114,7 +113,7 @@ drm_gpusvm_helper-$(CONFIG_ZONE_DEVICE) += \
 
 obj-$(CONFIG_DRM_GPUSVM) += drm_gpusvm_helper.o
 
-obj-$(CONFIG_DRM_BUDDY) += drm_buddy.o
+obj-$(CONFIG_DRM_BUDDY) += ../buddy.o
 
 drm_dma_helper-y := drm_gem_dma_helper.o
 drm_dma_helper-$(CONFIG_DRM_FBDEV_EMULATION) += drm_fbdev_dma.o
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.h +1 -1
index 5f5fd9a911c2..874779618056 100644
@@ -24,7 +24,7 @@
 #ifndef __AMDGPU_VRAM_MGR_H__
 #define __AMDGPU_VRAM_MGR_H__
 
-#include <drm/drm_buddy.h>
+#include <linux/gpu_buddy.h>
 
 struct amdgpu_vram_mgr {
 	struct ttm_resource_manager manager;
drivers/gpu/drm/i915/gem/i915_gem_ttm.c +1 -1
index f65fe86c02b5..eeda5daa544f 100644
@@ -5,7 +5,7 @@
 
 #include <linux/shmem_fs.h>
 
-#include <drm/drm_buddy.h>
+#include <linux/gpu_buddy.h>
 #include <drm/drm_print.h>
 #include <drm/ttm/ttm_placement.h>
 #include <drm/ttm/ttm_tt.h>
drivers/gpu/drm/i915/i915_scatterlist.c +1 -1
index 4d830740946d..30246f02bcfe 100644
@@ -7,7 +7,7 @@
 #include "i915_scatterlist.h"
 #include "i915_ttm_buddy_manager.h"
 
-#include <drm/drm_buddy.h>
+#include <linux/gpu_buddy.h>
 #include <drm/drm_mm.h>
 
 #include <linux/slab.h>
drivers/gpu/drm/i915/i915_ttm_buddy_manager.c +1 -1
index d5c6e6605086..6b256d95badd 100644
@@ -5,7 +5,7 @@
 
 #include <linux/slab.h>
 
-#include <drm/drm_buddy.h>
+#include <linux/gpu_buddy.h>
 #include <drm/drm_print.h>
 #include <drm/ttm/ttm_placement.h>
 #include <drm/ttm/ttm_bo.h>
drivers/gpu/drm/tests/Makefile +0 -1
index 87d5d5f9332a..d2e2e3d8349a 100644
@@ -7,7 +7,6 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \
 	drm_atomic_test.o \
 	drm_atomic_state_test.o \
 	drm_bridge_test.o \
-	drm_buddy_test.o \
 	drm_cmdline_parser_test.o \
 	drm_connector_test.o \
 	drm_damage_helper_test.o \
drivers/gpu/drm/tests/drm_exec_test.c +0 -2
index 3a20c788c51f..2fc47f3b463b 100644
@@ -16,8 +16,6 @@
 #include <drm/drm_gem.h>
 #include <drm/drm_kunit_helpers.h>
 
-#include "../lib/drm_random.h"
-
 struct drm_exec_priv {
 	struct device *dev;
 	struct drm_device *drm;
drivers/gpu/drm/tests/drm_mm_test.c +0 -2
index aec9eccdeae9..e24a619059d8 100644
@@ -16,8 +16,6 @@
 #include <drm/drm_mm.h>
 #include <drm/drm_print.h>
 
-#include "../lib/drm_random.h"
-
 enum {
 	BEST,
 	BOTTOMUP,
drivers/gpu/drm/ttm/tests/ttm_mock_manager.h +1 -1
index e4c95f86a467..96ea8c9aae34 100644
@@ -5,7 +5,7 @@
 #ifndef TTM_MOCK_MANAGER_H
 #define TTM_MOCK_MANAGER_H
 
-#include <drm/drm_buddy.h>
+#include <linux/gpu_buddy.h>
 
 struct ttm_mock_manager {
 	struct ttm_resource_manager man;
drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h +1 -1
index a71e14818ec2..babeec5511d9 100644
@@ -6,7 +6,7 @@
 #ifndef _XE_TTM_VRAM_MGR_TYPES_H_
 #define _XE_TTM_VRAM_MGR_TYPES_H_
 
-#include <drm/drm_buddy.h>
+#include <linux/gpu_buddy.h>
 #include <drm/ttm/ttm_device.h>
 
 /**
drivers/gpu/tests/Makefile new file +4 -0
new file mode 100644
index 000000000000..8e7654e87d82
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0
+
+gpu_buddy_tests-y = gpu_buddy_test.o gpu_random.o
+obj-$(CONFIG_DRM_KUNIT_TEST) += gpu_buddy_tests.o
drivers/gpu/tests/gpu_buddy_test.c renamed from drivers/gpu/drm/tests/drm_buddy_test.c +2 -2
similarity index 99%
rename from drivers/gpu/drm/tests/drm_buddy_test.c
rename to drivers/gpu/tests/gpu_buddy_test.c
index e6f8459c6c54..b905932da990 100644
@@ -10,9 +10,9 @@
 #include <linux/sched/signal.h>
 #include <linux/sizes.h>
 
-#include <drm/drm_buddy.h>
+#include <linux/gpu_buddy.h>
 
-#include "../lib/drm_random.h"
+#include "gpu_random.h"
 
 static unsigned int random_seed;
 
drivers/gpu/tests/gpu_random.c renamed from drivers/gpu/drm/lib/drm_random.c +1 -1
similarity index 97%
rename from drivers/gpu/drm/lib/drm_random.c
rename to drivers/gpu/tests/gpu_random.c
index 0e9dba1ef4af..ddd1f594b5d5 100644
@@ -6,7 +6,7 @@
 #include <linux/slab.h>
 #include <linux/types.h>
 
-#include "drm_random.h"
+#include "gpu_random.h"
 
 u32 drm_prandom_u32_max_state(u32 ep_ro, struct rnd_state *state)
 {
drivers/gpu/tests/gpu_random.h renamed from drivers/gpu/drm/lib/drm_random.h +0 -0
similarity index 100%
rename from drivers/gpu/drm/lib/drm_random.h
rename to drivers/gpu/tests/gpu_random.h
include/linux/gpu_buddy.h renamed from include/drm/drm_buddy.h +0 -0
similarity index 100%
rename from include/drm/drm_buddy.h
rename to include/linux/gpu_buddy.h
Signature · 1 line
2.34.1
author reviewer
#2 lore raw

PATCH v11 2/4 gpu: Move DRM buddy allocator one level up (part two)

In reply to Joel Fernandes

Move the DRM buddy allocator one level up so that it can be used by GPU drivers (example, nova-core) that have usecases other than DRM (such as VFIO vGPU support). Modify the API, structures and Kconfigs to use "gpu_buddy" terminology. Adapt the drivers and tests to use the new API.

The commit cannot be split due to bisectability, however no functional change is intended. Verified by running K-UNIT tests and build tested various configurations.

[airlied: I've split this into two so git can find copies easier. I've also just nuked drm_random library, that stuff needs to be done elsewhere and only the buddy tests seem to be using it].

---
 Documentation/gpu/drm-mm.rst                  |   6 +
 MAINTAINERS                                   |   8 +-
 drivers/gpu/Kconfig                           |  13 +
 drivers/gpu/Makefile                          |   1 +
 drivers/gpu/buddy.c                           | 556 +++++++++---------
 drivers/gpu/drm/Kconfig                       |   1 +
 drivers/gpu/drm/Makefile                      |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c       |   2 +-
 .../gpu/drm/amd/amdgpu/amdgpu_res_cursor.h    |  12 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c  |  79 +--
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.h  |  18 +-
 drivers/gpu/drm/drm_buddy.c                   |  77 +++
 drivers/gpu/drm/i915/i915_scatterlist.c       |   8 +-
 drivers/gpu/drm/i915/i915_ttm_buddy_manager.c |  55 +-
 drivers/gpu/drm/i915/i915_ttm_buddy_manager.h |   4 +-
 .../drm/i915/selftests/intel_memory_region.c  |  20 +-
 .../gpu/drm/ttm/tests/ttm_bo_validate_test.c  |   4 +-
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.c  |  18 +-
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.h  |   2 +-
 drivers/gpu/drm/xe/xe_res_cursor.h            |  34 +-
 drivers/gpu/drm/xe/xe_svm.c                   |  12 +-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.c          |  71 +--
 drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h    |   2 +-
 drivers/gpu/tests/Makefile                    |   2 +-
 drivers/gpu/tests/gpu_buddy_test.c            | 412 ++++++-------
 drivers/gpu/tests/gpu_random.c                |  16 +-
 drivers/gpu/tests/gpu_random.h                |  18 +-
 drivers/video/Kconfig                         |   1 +
 include/drm/drm_buddy.h                       |  18 +
 include/linux/gpu_buddy.h                     | 120 ++--
 30 files changed, 853 insertions(+), 739 deletions(-)
 create mode 100644 drivers/gpu/Kconfig
 create mode 100644 drivers/gpu/drm/drm_buddy.c
 create mode 100644 include/drm/drm_buddy.h
Documentation/gpu/drm-mm.rst +6 -0
index ceee0e663237..32fb506db05b 100644
@@ -532,6 +532,12 @@ Buddy Allocator Function References (GPU buddy)
 .. kernel-doc:: drivers/gpu/buddy.c
    :export:
 
+DRM Buddy Specific Logging Function References
+----------------------------------------------
+
+.. kernel-doc:: drivers/gpu/drm/drm_buddy.c
+   :export:
+
 DRM Cache Handling and Fast WC memcpy()
 =======================================
 
MAINTAINERS +5 -3
index 0dc318c94c99..f1e5a8011d2f 100644
@@ -8905,15 +8905,17 @@ T:	git https://gitlab.freedesktop.org/drm/misc/kernel.git
 F:	drivers/gpu/drm/ttm/
 F:	include/drm/ttm/
 
-DRM BUDDY ALLOCATOR
+GPU BUDDY ALLOCATOR
 M:	Matthew Auld <[email protected]>
 M:	Arun Pravin <[email protected]>
 R:	Christian Koenig <[email protected]>
 L:	[email protected]
 S:	Maintained
 T:	git https://gitlab.freedesktop.org/drm/misc/kernel.git
-F:	drivers/gpu/drm/drm_buddy.c
-F:	drivers/gpu/drm/tests/drm_buddy_test.c
+F:	drivers/gpu/drm_buddy.c
+F:	drivers/gpu/buddy.c
+F:	drivers/gpu/tests/gpu_buddy_test.c
+F:	include/linux/gpu_buddy.h
 F:	include/drm/drm_buddy.h
 
 DRM AUTOMATED TESTING
drivers/gpu/Kconfig new file +13 -0
new file mode 100644
index 000000000000..ebb2ad4b7ea0
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0
+
+config GPU_BUDDY
+       bool
+       help
+         A page based buddy allocator for GPU memory.
+
+config GPU_BUDDY_KUNIT_TEST
+       tristate "KUnit tests for GPU buddy allocator" if !KUNIT_ALL_TESTS
+       depends on GPU_BUDDY && KUNIT
+       default KUNIT_ALL_TESTS
+       help
+         KUnit tests for the GPU buddy allocator.
drivers/gpu/Makefile +1 -0
index c5292ee2c852..5cd54d06e262 100644
@@ -6,3 +6,4 @@ obj-y			+= host1x/ drm/ vga/ tests/
 obj-$(CONFIG_IMX_IPUV3_CORE)	+= ipu-v3/
 obj-$(CONFIG_TRACE_GPU_MEM)		+= trace/
 obj-$(CONFIG_NOVA_CORE)		+= nova-core/
+obj-$(CONFIG_GPU_BUDDY)		+= buddy.o
drivers/gpu/buddy.c +271 -285

1,266 lines not on this page · raw message

drivers/gpu/drm/Kconfig +1 -0
index ca2a2801e77f..f48d00fe28cc 100644
@@ -220,6 +220,7 @@ config DRM_GPUSVM
 config DRM_BUDDY
 	tristate
 	depends on DRM
+	select GPU_BUDDY
 	help
 	  A page based buddy allocator
 
drivers/gpu/drm/Makefile +1 -1
index 5c86bc908955..6ff0f6f10b58 100644
@@ -113,7 +113,7 @@ drm_gpusvm_helper-$(CONFIG_ZONE_DEVICE) += \
 
 obj-$(CONFIG_DRM_GPUSVM) += drm_gpusvm_helper.o
 
-obj-$(CONFIG_DRM_BUDDY) += ../buddy.o
+obj-$(CONFIG_DRM_BUDDY) += drm_buddy.o
 
 drm_dma_helper-y := drm_gem_dma_helper.o
 drm_dma_helper-$(CONFIG_DRM_FBDEV_EMULATION) += drm_fbdev_dma.o
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -1
index f582113d78b7..149f8f942eae 100644
@@ -5663,7 +5663,7 @@ int amdgpu_ras_add_critical_region(struct amdgpu_device *adev,
 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
 	struct amdgpu_vram_mgr_resource *vres;
 	struct ras_critical_region *region;
-	struct drm_buddy_block *block;
+	struct gpu_buddy_block *block;
 	int ret = 0;
 
 	if (!bo || !bo->tbo.resource)
drivers/gpu/drm/amd/amdgpu/amdgpu_res_cursor.h +6 -6
index be2e56ce1355..8908d9e08a30 100644
@@ -55,7 +55,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
 				    uint64_t start, uint64_t size,
 				    struct amdgpu_res_cursor *cur)
 {
-	struct drm_buddy_block *block;
+	struct gpu_buddy_block *block;
 	struct list_head *head, *next;
 	struct drm_mm_node *node;
 
@@ -71,7 +71,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
 		head = &to_amdgpu_vram_mgr_resource(res)->blocks;
 
 		block = list_first_entry_or_null(head,
-						 struct drm_buddy_block,
+						 struct gpu_buddy_block,
 						 link);
 		if (!block)
 			goto fallback;
@@ -81,7 +81,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
 
 			next = block->link.next;
 			if (next != head)
-				block = list_entry(next, struct drm_buddy_block, link);
+				block = list_entry(next, struct gpu_buddy_block, link);
 		}
 
 		cur->start = amdgpu_vram_mgr_block_start(block) + start;
@@ -125,7 +125,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
  */
 static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size)
 {
-	struct drm_buddy_block *block;
+	struct gpu_buddy_block *block;
 	struct drm_mm_node *node;
 	struct list_head *next;
 
@@ -146,7 +146,7 @@ static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size)
 		block = cur->node;
 
 		next = block->link.next;
-		block = list_entry(next, struct drm_buddy_block, link);
+		block = list_entry(next, struct gpu_buddy_block, link);
 
 		cur->node = block;
 		cur->start = amdgpu_vram_mgr_block_start(block);
@@ -175,7 +175,7 @@ static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size)
  */
 static inline bool amdgpu_res_cleared(struct amdgpu_res_cursor *cur)
 {
-	struct drm_buddy_block *block;
+	struct gpu_buddy_block *block;
 
 	switch (cur->mem_type) {
 	case TTM_PL_VRAM:
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +40 -39

259 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.h +9 -9
index 874779618056..429a21a2e9b2 100644
@@ -28,7 +28,7 @@
 
 struct amdgpu_vram_mgr {
 	struct ttm_resource_manager manager;
-	struct drm_buddy mm;
+	struct gpu_buddy mm;
 	/* protects access to buffer objects */
 	struct mutex lock;
 	struct list_head reservations_pending;
@@ -57,19 +57,19 @@ struct amdgpu_vram_mgr_resource {
 	struct amdgpu_vres_task task;
 };
 
-static inline u64 amdgpu_vram_mgr_block_start(struct drm_buddy_block *block)
+static inline u64 amdgpu_vram_mgr_block_start(struct gpu_buddy_block *block)
 {
-	return drm_buddy_block_offset(block);
+	return gpu_buddy_block_offset(block);
 }
 
-static inline u64 amdgpu_vram_mgr_block_size(struct drm_buddy_block *block)
+static inline u64 amdgpu_vram_mgr_block_size(struct gpu_buddy_block *block)
 {
-	return (u64)PAGE_SIZE << drm_buddy_block_order(block);
+	return (u64)PAGE_SIZE << gpu_buddy_block_order(block);
 }
 
-static inline bool amdgpu_vram_mgr_is_cleared(struct drm_buddy_block *block)
+static inline bool amdgpu_vram_mgr_is_cleared(struct gpu_buddy_block *block)
 {
-	return drm_buddy_block_is_clear(block);
+	return gpu_buddy_block_is_clear(block);
 }
 
 static inline struct amdgpu_vram_mgr_resource *
@@ -82,8 +82,8 @@ static inline void amdgpu_vram_mgr_set_cleared(struct ttm_resource *res)
 {
 	struct amdgpu_vram_mgr_resource *ares = to_amdgpu_vram_mgr_resource(res);
 
-	WARN_ON(ares->flags & DRM_BUDDY_CLEARED);
-	ares->flags |= DRM_BUDDY_CLEARED;
+	WARN_ON(ares->flags & GPU_BUDDY_CLEARED);
+	ares->flags |= GPU_BUDDY_CLEARED;
 }
 
 int amdgpu_vram_mgr_query_address_block_info(struct amdgpu_vram_mgr *mgr,
drivers/gpu/drm/drm_buddy.c new file +77 -0

77 lines not on this page · raw message

drivers/gpu/drm/i915/i915_scatterlist.c +4 -4
index 30246f02bcfe..6a34dae13769 100644
@@ -167,9 +167,9 @@ struct i915_refct_sgt *i915_rsgt_from_buddy_resource(struct ttm_resource *res,
 	struct i915_ttm_buddy_resource *bman_res = to_ttm_buddy_resource(res);
 	const u64 size = res->size;
 	const u32 max_segment = round_down(UINT_MAX, page_alignment);
-	struct drm_buddy *mm = bman_res->mm;
+	struct gpu_buddy *mm = bman_res->mm;
 	struct list_head *blocks = &bman_res->blocks;
-	struct drm_buddy_block *block;
+	struct gpu_buddy_block *block;
 	struct i915_refct_sgt *rsgt;
 	struct scatterlist *sg;
 	struct sg_table *st;
@@ -202,8 +202,8 @@ struct i915_refct_sgt *i915_rsgt_from_buddy_resource(struct ttm_resource *res,
 	list_for_each_entry(block, blocks, link) {
 		u64 block_size, offset;
 
-		block_size = min_t(u64, size, drm_buddy_block_size(mm, block));
-		offset = drm_buddy_block_offset(block);
+		block_size = min_t(u64, size, gpu_buddy_block_size(mm, block));
+		offset = gpu_buddy_block_offset(block);
 
 		while (block_size) {
 			u64 len;
drivers/gpu/drm/i915/i915_ttm_buddy_manager.c +28 -27

175 lines not on this page · raw message

drivers/gpu/drm/i915/i915_ttm_buddy_manager.h +2 -2
index d64620712830..1cff018c1689 100644
@@ -13,7 +13,7 @@
 
 struct ttm_device;
 struct ttm_resource_manager;
-struct drm_buddy;
+struct gpu_buddy;
 
 /**
  * struct i915_ttm_buddy_resource
@@ -33,7 +33,7 @@ struct i915_ttm_buddy_resource {
 	struct list_head blocks;
 	unsigned long flags;
 	unsigned long used_visible_size;
-	struct drm_buddy *mm;
+	struct gpu_buddy *mm;
 };
 
 /**
drivers/gpu/drm/i915/selftests/intel_memory_region.c +10 -10
index 7b856b5090f9..8307390943a2 100644
@@ -6,7 +6,7 @@
 #include <linux/prime_numbers.h>
 #include <linux/sort.h>
 
-#include <drm/drm_buddy.h>
+#include <linux/gpu_buddy.h>
 
 #include "../i915_selftest.h"
 
@@ -371,7 +371,7 @@ static int igt_mock_splintered_region(void *arg)
 	struct drm_i915_private *i915 = mem->i915;
 	struct i915_ttm_buddy_resource *res;
 	struct drm_i915_gem_object *obj;
-	struct drm_buddy *mm;
+	struct gpu_buddy *mm;
 	unsigned int expected_order;
 	LIST_HEAD(objects);
 	u64 size;
@@ -447,8 +447,8 @@ static int igt_mock_max_segment(void *arg)
 	struct drm_i915_private *i915 = mem->i915;
 	struct i915_ttm_buddy_resource *res;
 	struct drm_i915_gem_object *obj;
-	struct drm_buddy_block *block;
-	struct drm_buddy *mm;
+	struct gpu_buddy_block *block;
+	struct gpu_buddy *mm;
 	struct list_head *blocks;
 	struct scatterlist *sg;
 	I915_RND_STATE(prng);
@@ -487,8 +487,8 @@ static int igt_mock_max_segment(void *arg)
 	mm = res->mm;
 	size = 0;
 	list_for_each_entry(block, blocks, link) {
-		if (drm_buddy_block_size(mm, block) > size)
-			size = drm_buddy_block_size(mm, block);
+		if (gpu_buddy_block_size(mm, block) > size)
+			size = gpu_buddy_block_size(mm, block);
 	}
 	if (size < max_segment) {
 		pr_err("%s: Failed to create a huge contiguous block [> %u], largest block %lld\n",
@@ -527,14 +527,14 @@ static u64 igt_object_mappable_total(struct drm_i915_gem_object *obj)
 	struct intel_memory_region *mr = obj->mm.region;
 	struct i915_ttm_buddy_resource *bman_res =
 		to_ttm_buddy_resource(obj->mm.res);
-	struct drm_buddy *mm = bman_res->mm;
-	struct drm_buddy_block *block;
+	struct gpu_buddy *mm = bman_res->mm;
+	struct gpu_buddy_block *block;
 	u64 total;
 
 	total = 0;
 	list_for_each_entry(block, &bman_res->blocks, link) {
-		u64 start = drm_buddy_block_offset(block);
-		u64 end = start + drm_buddy_block_size(mm, block);
+		u64 start = gpu_buddy_block_offset(block);
+		u64 end = start + gpu_buddy_block_size(mm, block);
 
 		if (start < resource_size(&mr->io))
 			total += min_t(u64, end, resource_size(&mr->io)) - start;
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +2 -2
index 6d95447a989d..e32f3c8d7b84 100644
@@ -251,7 +251,7 @@ static void ttm_bo_validate_basic(struct kunit *test)
 				   NULL, &dummy_ttm_bo_destroy);
 	KUNIT_EXPECT_EQ(test, err, 0);
 
-	snd_place = ttm_place_kunit_init(test, snd_mem, DRM_BUDDY_TOPDOWN_ALLOCATION);
+	snd_place = ttm_place_kunit_init(test, snd_mem, GPU_BUDDY_TOPDOWN_ALLOCATION);
 	snd_placement = ttm_placement_kunit_init(test, snd_place, 1);
 
 	err = ttm_bo_validate(bo, snd_placement, &ctx_val);
@@ -263,7 +263,7 @@ static void ttm_bo_validate_basic(struct kunit *test)
 	KUNIT_EXPECT_TRUE(test, ttm_tt_is_populated(bo->ttm));
 	KUNIT_EXPECT_EQ(test, bo->resource->mem_type, snd_mem);
 	KUNIT_EXPECT_EQ(test, bo->resource->placement,
-			DRM_BUDDY_TOPDOWN_ALLOCATION);
+			GPU_BUDDY_TOPDOWN_ALLOCATION);
 
 	ttm_bo_fini(bo);
 	ttm_mock_manager_fini(priv->ttm_dev, snd_mem);
drivers/gpu/drm/ttm/tests/ttm_mock_manager.c +9 -9
index dd395229e388..294d56d9067e 100644
@@ -31,7 +31,7 @@ static int ttm_mock_manager_alloc(struct ttm_resource_manager *man,
 {
 	struct ttm_mock_manager *manager = to_mock_mgr(man);
 	struct ttm_mock_resource *mock_res;
-	struct drm_buddy *mm = &manager->mm;
+	struct gpu_buddy *mm = &manager->mm;
 	u64 lpfn, fpfn, alloc_size;
 	int err;
 
@@ -47,14 +47,14 @@ static int ttm_mock_manager_alloc(struct ttm_resource_manager *man,
 	INIT_LIST_HEAD(&mock_res->blocks);
 
 	if (place->flags & TTM_PL_FLAG_TOPDOWN)
-		mock_res->flags |= DRM_BUDDY_TOPDOWN_ALLOCATION;
+		mock_res->flags |= GPU_BUDDY_TOPDOWN_ALLOCATION;
 
 	if (place->flags & TTM_PL_FLAG_CONTIGUOUS)
-		mock_res->flags |= DRM_BUDDY_CONTIGUOUS_ALLOCATION;
+		mock_res->flags |= GPU_BUDDY_CONTIGUOUS_ALLOCATION;
 
 	alloc_size = (uint64_t)mock_res->base.size;
 	mutex_lock(&manager->lock);
-	err = drm_buddy_alloc_blocks(mm, fpfn, lpfn, alloc_size,
+	err = gpu_buddy_alloc_blocks(mm, fpfn, lpfn, alloc_size,
 				     manager->default_page_size,
 				     &mock_res->blocks,
 				     mock_res->flags);
@@ -67,7 +67,7 @@ static int ttm_mock_manager_alloc(struct ttm_resource_manager *man,
 	return 0;
 
 error_free_blocks:
-	drm_buddy_free_list(mm, &mock_res->blocks, 0);
+	gpu_buddy_free_list(mm, &mock_res->blocks, 0);
 	ttm_resource_fini(man, &mock_res->base);
 	mutex_unlock(&manager->lock);
 
@@ -79,10 +79,10 @@ static void ttm_mock_manager_free(struct ttm_resource_manager *man,
 {
 	struct ttm_mock_manager *manager = to_mock_mgr(man);
 	struct ttm_mock_resource *mock_res = to_mock_mgr_resource(res);
-	struct drm_buddy *mm = &manager->mm;
+	struct gpu_buddy *mm = &manager->mm;
 
 	mutex_lock(&manager->lock);
-	drm_buddy_free_list(mm, &mock_res->blocks, 0);
+	gpu_buddy_free_list(mm, &mock_res->blocks, 0);
 	mutex_unlock(&manager->lock);
 
 	ttm_resource_fini(man, res);
@@ -106,7 +106,7 @@ int ttm_mock_manager_init(struct ttm_device *bdev, u32 mem_type, u32 size)
 
 	mutex_init(&manager->lock);
 
-	err = drm_buddy_init(&manager->mm, size, PAGE_SIZE);
+	err = gpu_buddy_init(&manager->mm, size, PAGE_SIZE);
 
 	if (err) {
 		kfree(manager);
@@ -142,7 +142,7 @@ void ttm_mock_manager_fini(struct ttm_device *bdev, u32 mem_type)
 	ttm_resource_manager_set_used(man, false);
 
 	mutex_lock(&mock_man->lock);
-	drm_buddy_fini(&mock_man->mm);
+	gpu_buddy_fini(&mock_man->mm);
 	mutex_unlock(&mock_man->lock);
 
 	ttm_set_driver_manager(bdev, mem_type, NULL);
drivers/gpu/drm/ttm/tests/ttm_mock_manager.h +1 -1
index 96ea8c9aae34..08710756fd8e 100644
@@ -9,7 +9,7 @@
 
 struct ttm_mock_manager {
 	struct ttm_resource_manager man;
-	struct drm_buddy mm;
+	struct gpu_buddy mm;
 	u64 default_page_size;
 	/* protects allocations of mock buffer objects */
 	struct mutex lock;
drivers/gpu/drm/xe/xe_res_cursor.h +17 -17
index 4e00008b7081..5f4ab08c0686 100644
@@ -58,7 +58,7 @@ struct xe_res_cursor {
 	/** @dma_addr: Current element in a struct drm_pagemap_addr array */
 	const struct drm_pagemap_addr *dma_addr;
 	/** @mm: Buddy allocator for VRAM cursor */
-	struct drm_buddy *mm;
+	struct gpu_buddy *mm;
 	/**
 	 * @dma_start: DMA start address for the current segment.
 	 * This may be different to @dma_addr.addr since elements in
@@ -69,7 +69,7 @@ struct xe_res_cursor {
 	u64 dma_seg_size;
 };
 
-static struct drm_buddy *xe_res_get_buddy(struct ttm_resource *res)
+static struct gpu_buddy *xe_res_get_buddy(struct ttm_resource *res)
 {
 	struct ttm_resource_manager *mgr;
 
@@ -104,30 +104,30 @@ static inline void xe_res_first(struct ttm_resource *res,
 	case XE_PL_STOLEN:
 	case XE_PL_VRAM0:
 	case XE_PL_VRAM1: {
-		struct drm_buddy_block *block;
+		struct gpu_buddy_block *block;
 		struct list_head *head, *next;
-		struct drm_buddy *mm = xe_res_get_buddy(res);
+		struct gpu_buddy *mm = xe_res_get_buddy(res);
 
 		head = &to_xe_ttm_vram_mgr_resource(res)->blocks;
 
 		block = list_first_entry_or_null(head,
-						 struct drm_buddy_block,
+						 struct gpu_buddy_block,
 						 link);
 		if (!block)
 			goto fallback;
 
-		while (start >= drm_buddy_block_size(mm, block)) {
-			start -= drm_buddy_block_size(mm, block);
+		while (start >= gpu_buddy_block_size(mm, block)) {
+			start -= gpu_buddy_block_size(mm, block);
 
 			next = block->link.next;
 			if (next != head)
-				block = list_entry(next, struct drm_buddy_block,
+				block = list_entry(next, struct gpu_buddy_block,
 						   link);
 		}
 
 		cur->mm = mm;
-		cur->start = drm_buddy_block_offset(block) + start;
-		cur->size = min(drm_buddy_block_size(mm, block) - start,
+		cur->start = gpu_buddy_block_offset(block) + start;
+		cur->size = min(gpu_buddy_block_size(mm, block) - start,
 				size);
 		cur->remaining = size;
 		cur->node = block;
@@ -259,7 +259,7 @@ static inline void xe_res_first_dma(const struct drm_pagemap_addr *dma_addr,
  */
 static inline void xe_res_next(struct xe_res_cursor *cur, u64 size)
 {
-	struct drm_buddy_block *block;
+	struct gpu_buddy_block *block;
 	struct list_head *next;
 	u64 start;
 
@@ -295,18 +295,18 @@ static inline void xe_res_next(struct xe_res_cursor *cur, u64 size)
 		block = cur->node;
 
 		next = block->link.next;
-		block = list_entry(next, struct drm_buddy_block, link);
+		block = list_entry(next, struct gpu_buddy_block, link);
 
 
-		while (start >= drm_buddy_block_size(cur->mm, block)) {
-			start -= drm_buddy_block_size(cur->mm, block);
+		while (start >= gpu_buddy_block_size(cur->mm, block)) {
+			start -= gpu_buddy_block_size(cur->mm, block);
 
 			next = block->link.next;
-			block = list_entry(next, struct drm_buddy_block, link);
+			block = list_entry(next, struct gpu_buddy_block, link);
 		}
 
-		cur->start = drm_buddy_block_offset(block) + start;
-		cur->size = min(drm_buddy_block_size(cur->mm, block) - start,
+		cur->start = gpu_buddy_block_offset(block) + start;
+		cur->size = min(gpu_buddy_block_size(cur->mm, block) - start,
 				cur->remaining);
 		cur->node = block;
 		break;
drivers/gpu/drm/xe/xe_svm.c +6 -6
index 213f0334518a..cda3bf7e2418 100644
@@ -747,7 +747,7 @@ static u64 block_offset_to_pfn(struct drm_pagemap *dpagemap, u64 offset)
 	return PHYS_PFN(offset + xpagemap->hpa_base);
 }
 
-static struct drm_buddy *vram_to_buddy(struct xe_vram_region *vram)
+static struct gpu_buddy *vram_to_buddy(struct xe_vram_region *vram)
 {
 	return &vram->ttm.mm;
 }
@@ -758,17 +758,17 @@ static int xe_svm_populate_devmem_pfn(struct drm_pagemap_devmem *devmem_allocati
 	struct xe_bo *bo = to_xe_bo(devmem_allocation);
 	struct ttm_resource *res = bo->ttm.resource;
 	struct list_head *blocks = &to_xe_ttm_vram_mgr_resource(res)->blocks;
-	struct drm_buddy_block *block;
+	struct gpu_buddy_block *block;
 	int j = 0;
 
 	list_for_each_entry(block, blocks, link) {
 		struct xe_vram_region *vr = block->private;
-		struct drm_buddy *buddy = vram_to_buddy(vr);
+		struct gpu_buddy *buddy = vram_to_buddy(vr);
 		u64 block_pfn = block_offset_to_pfn(devmem_allocation->dpagemap,
-						    drm_buddy_block_offset(block));
+						    gpu_buddy_block_offset(block));
 		int i;
 
-		for (i = 0; i < drm_buddy_block_size(buddy, block) >> PAGE_SHIFT; ++i)
+		for (i = 0; i < gpu_buddy_block_size(buddy, block) >> PAGE_SHIFT; ++i)
 			pfn[j++] = block_pfn + i;
 	}
 
@@ -1033,7 +1033,7 @@ static int xe_drm_pagemap_populate_mm(struct drm_pagemap *dpagemap,
 	struct dma_fence *pre_migrate_fence = NULL;
 	struct xe_device *xe = vr->xe;
 	struct device *dev = xe->drm.dev;
-	struct drm_buddy_block *block;
+	struct gpu_buddy_block *block;
 	struct xe_validation_ctx vctx;
 	struct list_head *blocks;
 	struct drm_exec exec;
drivers/gpu/drm/xe/xe_ttm_vram_mgr.c +36 -35

207 lines not on this page · raw message

drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h +1 -1
index babeec5511d9..9106da056b49 100644
@@ -18,7 +18,7 @@ struct xe_ttm_vram_mgr {
 	/** @manager: Base TTM resource manager */
 	struct ttm_resource_manager manager;
 	/** @mm: DRM buddy allocator which manages the VRAM */
-	struct drm_buddy mm;
+	struct gpu_buddy mm;
 	/** @visible_size: Proped size of the CPU visible portion */
 	u64 visible_size;
 	/** @visible_avail: CPU visible portion still unallocated */
drivers/gpu/tests/Makefile +1 -1
index 8e7654e87d82..4183e6e2de45 100644
@@ -1,4 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0
 
 gpu_buddy_tests-y = gpu_buddy_test.o gpu_random.o
-obj-$(CONFIG_DRM_KUNIT_TEST) += gpu_buddy_tests.o
+obj-$(CONFIG_GPU_BUDDY_KUNIT_TEST) += gpu_buddy_tests.o
drivers/gpu/tests/gpu_buddy_test.c +206 -206

541 lines not on this page · raw message

drivers/gpu/tests/gpu_random.c +8 -8
index ddd1f594b5d5..6356372f7e52 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/tests/gpu_random.h +9 -9
index 9f827260a89d..b68cf3448264 100644

Lines of this file are not shown. View the raw message.

drivers/video/Kconfig +1 -0
index 9884f003247d..a7144d275f54 100644

Lines of this file are not shown. View the raw message.

include/drm/drm_buddy.h new file +18 -0
new file mode 100644
index 000000000000..3054369bebff

Lines of this file are not shown. View the raw message.

include/linux/gpu_buddy.h +63 -57

120 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.34.1

Part of the diff is not shown here. View the raw message.

author reviewer
#3 lore raw

PATCH v11 3/4 gpu: Fix uninitialized buddy for built-in drivers

In reply to Joel Fernandes

From: Koen Koning <[email protected]>

Use subsys_initcall instead of module_init for the GPU buddy allocator, so its initialization code runs before any gpu drivers. Otherwise, a built-in driver that tries to use the buddy allocator will run into a kernel NULL pointer dereference because slab_blocks is uninitialized.

Specifically, this fixes drm/xe (as built-in) running into a kernel panic during boot, because it uses buddy during device probe.

Fixes ba110db8e1bc ("gpu: Move DRM buddy allocator one level up (part two)")
Cc Joel Fernandes <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Peter Senna Tschudin <[email protected]>
Cc Matthew Auld <[email protected]>

---
Please note: Greg and Danilo suggest fixing this in the Makefile like Koen initially did, so please do so. I am just including this patch here for reference.

 drivers/gpu/buddy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/buddy.c +1 -1
index 603c59a2013a..81f57fdf913b 100644
@@ -1315,7 +1315,7 @@ static int __init gpu_buddy_module_init(void)
 	return 0;
 }
 
-module_init(gpu_buddy_module_init);
+subsys_initcall(gpu_buddy_module_init);
 module_exit(gpu_buddy_module_exit);
 
 MODULE_DESCRIPTION("GPU Buddy Allocator");
Signature · 1 line
2.34.1
author reviewer
#4 lore raw

PATCH v11 4/4 rust: gpu: Add GPU buddy allocator bindings

In reply to Joel Fernandes

Add safe Rust abstractions over the Linux kernel's GPU buddy allocator for physical memory management. The GPU buddy allocator implements a binary buddy system useful for GPU physical memory allocation. nova-core will use it for physical memory allocation.

---
 MAINTAINERS                     |   8 +
 rust/bindings/bindings_helper.h |  11 +
 rust/helpers/gpu.c              |  23 ++
 rust/helpers/helpers.c          |   1 +
 rust/kernel/gpu/buddy.rs        | 536 ++++++++++++++++++++++++++++++++
 rust/kernel/gpu/mod.rs          |   5 +
 rust/kernel/lib.rs              |   2 +
 7 files changed, 586 insertions(+)
 create mode 100644 rust/helpers/gpu.c
 create mode 100644 rust/kernel/gpu/buddy.rs
 create mode 100644 rust/kernel/gpu/mod.rs
MAINTAINERS +8 -0
index f1e5a8011d2f..6befe3890f78 100644
@@ -23191,6 +23191,14 @@ S:	Maintained
 T:	git https://github.com/Rust-for-Linux/linux.git ffi-next
 F:	rust/kernel/ffi/
 
+RUST [GPU BUDDY]
+M:	Joel Fernandes <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	rust/helpers/gpu.c
+F:	rust/kernel/gpu/
+
 RUST [NUM]
 M:	Alexandre Courbot <[email protected]>
 R:	Yury Norov <[email protected]>
rust/bindings/bindings_helper.h +11 -0
index 083cc44aa952..dbb765a9fdbd 100644
@@ -29,6 +29,7 @@
 #include <linux/hrtimer_types.h>
 
 #include <linux/acpi.h>
+#include <linux/gpu_buddy.h>
 #include <drm/drm_device.h>
 #include <drm/drm_drv.h>
 #include <drm/drm_file.h>
@@ -146,6 +147,16 @@ const vm_flags_t RUST_CONST_HELPER_VM_MIXEDMAP = VM_MIXEDMAP;
 const vm_flags_t RUST_CONST_HELPER_VM_HUGEPAGE = VM_HUGEPAGE;
 const vm_flags_t RUST_CONST_HELPER_VM_NOHUGEPAGE = VM_NOHUGEPAGE;
 
+#if IS_ENABLED(CONFIG_GPU_BUDDY)
+const unsigned long RUST_CONST_HELPER_GPU_BUDDY_RANGE_ALLOCATION = GPU_BUDDY_RANGE_ALLOCATION;
+const unsigned long RUST_CONST_HELPER_GPU_BUDDY_TOPDOWN_ALLOCATION = GPU_BUDDY_TOPDOWN_ALLOCATION;
+const unsigned long RUST_CONST_HELPER_GPU_BUDDY_CONTIGUOUS_ALLOCATION =
+								GPU_BUDDY_CONTIGUOUS_ALLOCATION;
+const unsigned long RUST_CONST_HELPER_GPU_BUDDY_CLEAR_ALLOCATION = GPU_BUDDY_CLEAR_ALLOCATION;
+const unsigned long RUST_CONST_HELPER_GPU_BUDDY_CLEARED = GPU_BUDDY_CLEARED;
+const unsigned long RUST_CONST_HELPER_GPU_BUDDY_TRIM_DISABLE = GPU_BUDDY_TRIM_DISABLE;
+#endif
+
 #if IS_ENABLED(CONFIG_ANDROID_BINDER_IPC_RUST)
 #include "../../drivers/android/binder/rust_binder.h"
 #include "../../drivers/android/binder/rust_binder_events.h"
rust/helpers/gpu.c new file +23 -0
new file mode 100644
index 000000000000..38b1a4e6bef8
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/gpu_buddy.h>
+
+#ifdef CONFIG_GPU_BUDDY
+
+__rust_helper u64 rust_helper_gpu_buddy_block_offset(const struct gpu_buddy_block *block)
+{
+	return gpu_buddy_block_offset(block);
+}
+
+__rust_helper unsigned int rust_helper_gpu_buddy_block_order(struct gpu_buddy_block *block)
+{
+	return gpu_buddy_block_order(block);
+}
+
+__rust_helper u64 rust_helper_gpu_buddy_block_size(struct gpu_buddy *mm,
+						   struct gpu_buddy_block *block)
+{
+	return gpu_buddy_block_size(mm, block);
+}
+
+#endif /* CONFIG_GPU_BUDDY */
rust/helpers/helpers.c +1 -0
index 724fcb8240ac..a53929ce52a3 100644
@@ -32,6 +32,7 @@
 #include "err.c"
 #include "irq.c"
 #include "fs.c"
+#include "gpu.c"
 #include "io.c"
 #include "jump_label.c"
 #include "kunit.c"
rust/kernel/gpu/buddy.rs new file +536 -0

536 lines not on this page · raw message

rust/kernel/gpu/mod.rs new file +5 -0
new file mode 100644
index 000000000000..8f25e6367edc
@@ -0,0 +1,5 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! GPU subsystem abstractions.
+
+pub mod buddy;
rust/kernel/lib.rs +2 -0
index 58dbb02c5197..b8287601f464 100644
@@ -97,6 +97,8 @@
 pub mod firmware;
 pub mod fmt;
 pub mod fs;
+#[cfg(CONFIG_GPU_BUDDY)]
+pub mod gpu;
 #[cfg(CONFIG_I2C = "y")]
 pub mod i2c;
 pub mod id_pool;
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v11 4/4] rust: gpu: Add GPU buddy allocator bindings

Quoted 7 lines · Joel Fernandes

Add safe Rust abstractions over the Linux kernel's GPU buddy allocator for physical memory management. The GPU buddy allocator implements a binary buddy system useful for GPU physical memory allocation. nova-core will use it for physical memory allocation.

<snip>

Quoted 11 lines
rust/kernel/gpu/buddy.rs new file +5 -0
new file mode 100644
index 000000000000..4383f82c0fc1
@@ -0,0 +1,536 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! GPU buddy allocator bindings.
+//!
+//! C header: [`include/linux/gpu_buddy.h`](srctree/include/linux/gpu_buddy.h)

rust/kernel/gpu/buddy.rs line 15

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+//!
+//! This module provides Rust abstractions over the Linux kernel's GPU buddy
+//! allocator, which implements a binary buddy memory allocator.
+//!
+//! The buddy allocator manages a contiguous address space and allocates blocks
+//! in power-of-two sizes, useful for GPU physical memory management.
+//!
+//! # Examples
+//!
+//! ```

This is a very long example illustrating many use-cases. It is long enough that it is difficult to grasp where each example start. Can I suggest to aerate it a bit by splitting it into several examples, with a bit of regular text explaining what each example does, similarly to the documentation of the Bounded type?

You can hide the creation of the GpuBuddy after the first example to keep things concise.

Quoted 15 lines
rust/kernel/gpu/buddy.rs new file +15 -0
+//! use kernel::{
+//!     gpu::buddy::{BuddyFlags, GpuBuddy, GpuBuddyAllocParams, GpuBuddyParams},
+//!     prelude::*,
+//!     sizes::*, //
+//! };
+//!
+//! // Create a 1GB buddy allocator with 4KB minimum chunk size.
+//! let buddy = GpuBuddy::new(GpuBuddyParams {
+//!     base_offset_bytes: 0,
+//!     physical_memory_size_bytes: SZ_1G as u64,
+//!     chunk_size_bytes: SZ_4K as u64,
+//! })?;
+//!
+//! // Verify initial state.
+//! assert_eq!(buddy.size(), SZ_1G as u64);

rust/kernel/gpu/buddy.rs line 40

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+//! assert_eq!(buddy.chunk_size(), SZ_4K as u64);
+//! let initial_free = buddy.free_memory_bytes();
+//!
+//! // Base allocation params - mutated between calls for field overrides.
+//! let mut params = GpuBuddyAllocParams {
+//!     start_range_address: 0,
+//!     end_range_address: 0,   // Entire range.
+//!     size_bytes: SZ_16M as u64,
+//!     min_block_size_bytes: SZ_16M as u64,
+//!     buddy_flags: BuddyFlags::try_new(BuddyFlags::RANGE_ALLOCATION)?,

Why can a BuddyFlags creation fail if we give it a valid value? It looks like its consts should be of the type BuddyFlags themselves so we can use them directly. Actually, we should probably use impl_flags! for it.

rust/kernel/gpu/buddy.rs line 52

Quoted 12 lines
rust/kernel/gpu/buddy.rs new file +12 -0
+//! };
+//!
+//! // Test top-down allocation (allocates from highest addresses).
+//! params.buddy_flags = BuddyFlags::try_new(BuddyFlags::TOPDOWN_ALLOCATION)?;
+//! let topdown = KBox::pin_init(buddy.alloc_blocks(&params), GFP_KERNEL)?;
+//! assert_eq!(buddy.free_memory_bytes(), initial_free - SZ_16M as u64);
+//!
+//! for block in topdown.iter() {
+//!     assert_eq!(block.offset(), (SZ_1G - SZ_16M) as u64);
+//!     assert_eq!(block.order(), 12); // 2^12 pages
+//!     assert_eq!(block.size(), SZ_16M as u64);
+//! }

IIUC there should be only one block here, right? That for loop should be replaced by a call to next() followed by another one checking that the result is None to be a valid test.

rust/kernel/gpu/buddy.rs line 53

Quoted 1 line
rust/kernel/gpu/buddy.rs new file +1 -0
+//! drop(topdown);

Here is a good chance to mention that dropping the allocation will return it - it's expected, but not entirely obvious when you read this for the first time.

rust/kernel/gpu/buddy.rs line 57

Quoted 4 lines
rust/kernel/gpu/buddy.rs new file +4 -0
+//! assert_eq!(buddy.free_memory_bytes(), initial_free);
+//!
+//! // Allocate 16MB - should result in a single 16MB block at offset 0.
+//! params.buddy_flags = BuddyFlags::try_new(BuddyFlags::RANGE_ALLOCATION)?;

Let's recreate the params for each example to make it self-contained instead of modifying the first one.

Quoted 114 lines
rust/kernel/gpu/buddy.rs new file +114 -0
+//! let allocated = KBox::pin_init(buddy.alloc_blocks(&params), GFP_KERNEL)?;
+//! assert_eq!(buddy.free_memory_bytes(), initial_free - SZ_16M as u64);
+//!
+//! for block in allocated.iter() {
+//!     assert_eq!(block.offset(), 0);
+//!     assert_eq!(block.order(), 12); // 2^12 pages
+//!     assert_eq!(block.size(), SZ_16M as u64);
+//! }
+//! drop(allocated);
+//! assert_eq!(buddy.free_memory_bytes(), initial_free);
+//!
+//! // Test non-contiguous allocation with fragmented memory.
+//! // Create fragmentation by allocating 4MB blocks at [0,4M) and [8M,12M).
+//! params.end_range_address = SZ_4M as u64;
+//! params.size_bytes = SZ_4M as u64;
+//! params.min_block_size_bytes = SZ_4M as u64;
+//! let frag1 = KBox::pin_init(buddy.alloc_blocks(&params), GFP_KERNEL)?;
+//! assert_eq!(buddy.free_memory_bytes(), initial_free - SZ_4M as u64);
+//!
+//! params.start_range_address = SZ_8M as u64;
+//! params.end_range_address = (SZ_8M + SZ_4M) as u64;
+//! let frag2 = KBox::pin_init(buddy.alloc_blocks(&params), GFP_KERNEL)?;
+//! assert_eq!(buddy.free_memory_bytes(), initial_free - SZ_8M as u64);
+//!
+//! // Allocate 8MB without CONTIGUOUS - should return 2 blocks from the holes.
+//! params.start_range_address = 0;
+//! params.end_range_address = SZ_16M as u64;
+//! params.size_bytes = SZ_8M as u64;
+//! let fragmented = KBox::pin_init(buddy.alloc_blocks(&params), GFP_KERNEL)?;
+//! assert_eq!(buddy.free_memory_bytes(), initial_free - (SZ_16M) as u64);
+//!
+//! let (mut count, mut total) = (0u32, 0u64);
+//! for block in fragmented.iter() {
+//!     // The 8MB allocation should return 2 blocks, each 4MB.
+//!     assert_eq!(block.size(), SZ_4M as u64);
+//!     total += block.size();
+//!     count += 1;
+//! }
+//! assert_eq!(total, SZ_8M as u64);
+//! assert_eq!(count, 2);
+//! drop(fragmented);
+//! drop(frag2);
+//! drop(frag1);
+//! assert_eq!(buddy.free_memory_bytes(), initial_free);
+//!
+//! // Test CONTIGUOUS failure when only fragmented space available.
+//! // Create a small buddy allocator with only 16MB of memory.
+//! let small = GpuBuddy::new(GpuBuddyParams {
+//!     base_offset_bytes: 0,
+//!     physical_memory_size_bytes: SZ_16M as u64,
+//!     chunk_size_bytes: SZ_4K as u64,
+//! })?;
+//!
+//! // Allocate 4MB blocks at [0,4M) and [8M,12M) to create fragmented memory.
+//! params.start_range_address = 0;
+//! params.end_range_address = SZ_4M as u64;
+//! params.size_bytes = SZ_4M as u64;
+//! let hole1 = KBox::pin_init(small.alloc_blocks(&params), GFP_KERNEL)?;
+//!
+//! params.start_range_address = SZ_8M as u64;
+//! params.end_range_address = (SZ_8M + SZ_4M) as u64;
+//! let hole2 = KBox::pin_init(small.alloc_blocks(&params), GFP_KERNEL)?;
+//!
+//! // 8MB contiguous should fail - only two non-contiguous 4MB holes exist.
+//! params.start_range_address = 0;
+//! params.end_range_address = 0;
+//! params.size_bytes = SZ_8M as u64;
+//! params.buddy_flags = BuddyFlags::try_new(BuddyFlags::CONTIGUOUS_ALLOCATION)?;
+//! let result = KBox::pin_init(small.alloc_blocks(&params), GFP_KERNEL);
+//! assert!(result.is_err());
+//! drop(hole2);
+//! drop(hole1);
+//!
+//! # Ok::<(), Error>(())
+//! ```
+
+use crate::{
+    bindings,
+    clist_create,
+    error::to_result,
+    ffi::clist::CListHead,
+    new_mutex,
+    prelude::*,
+    sync::{
+        lock::mutex::MutexGuard,
+        Arc,
+        Mutex, //
+    },
+    types::Opaque, //
+};
+
+/// Flags for GPU buddy allocator operations.
+///
+/// These flags control the allocation behavior of the buddy allocator.
+#[derive(Clone, Copy, Default, PartialEq, Eq)]
+pub struct BuddyFlags(usize);
+
+impl BuddyFlags {
+    /// Range-based allocation from start to end addresses.
+    pub const RANGE_ALLOCATION: usize = bindings::GPU_BUDDY_RANGE_ALLOCATION;
+
+    /// Allocate from top of address space downward.
+    pub const TOPDOWN_ALLOCATION: usize = bindings::GPU_BUDDY_TOPDOWN_ALLOCATION;
+
+    /// Allocate physically contiguous blocks.
+    pub const CONTIGUOUS_ALLOCATION: usize = bindings::GPU_BUDDY_CONTIGUOUS_ALLOCATION;
+
+    /// Request allocation from the cleared (zeroed) memory. The zero'ing is not
+    /// done by the allocator, but by the caller before freeing old blocks.
+    pub const CLEAR_ALLOCATION: usize = bindings::GPU_BUDDY_CLEAR_ALLOCATION;
+
+    /// Disable trimming of partially used blocks.
+    pub const TRIM_DISABLE: usize = bindings::GPU_BUDDY_TRIM_DISABLE;
+

rust/kernel/gpu/buddy.rs line 181

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+    /// Mark blocks as cleared (zeroed) when freeing. When set during free,
+    /// indicates that the caller has already zeroed the memory.
+    pub const CLEARED: usize = bindings::GPU_BUDDY_CLEARED;
+
+    /// Create [`BuddyFlags`] from a raw value with validation.
+    ///
+    /// Use `|` operator to combine flags if needed, before calling this method.
+    pub fn try_new(flags: usize) -> Result<Self> {
+        // Flags must not exceed u32::MAX to satisfy the GPU buddy allocator C API.
+        if flags > u32::MAX as usize {

These as conversions are unfortunate - I will try to graduate the infallible convertors from Nova into kernel soon so we can avoid them, but for now I guess there's nothing we can do unfortunately.

rust/kernel/gpu/buddy.rs line 189

Quoted 8 lines
rust/kernel/gpu/buddy.rs new file +8 -0
+            return Err(EINVAL);
+        }
+
+        // `TOPDOWN_ALLOCATION` only works without `RANGE_ALLOCATION`. When both are
+        // set, `TOPDOWN_ALLOCATION` is silently ignored by the allocator. Reject this.
+        if (flags & Self::RANGE_ALLOCATION) != 0 && (flags & Self::TOPDOWN_ALLOCATION) != 0 {
+            return Err(EINVAL);
+        }

This indicates that we should use the type system to prevent such constructs from even being attempted - more on this on GpuBuddyAllocParams.

Quoted 5 lines
rust/kernel/gpu/buddy.rs new file +5 -0
+
+        Ok(Self(flags))
+    }
+
+    /// Get raw value of the flags.

rust/kernel/gpu/buddy.rs line 204

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+    pub(crate) fn as_raw(self) -> usize {
+        self.0
+    }
+}
+
+/// Parameters for creating a GPU buddy allocator.
+pub struct GpuBuddyParams {
+    /// Base offset in bytes where the managed memory region starts.
+    /// Allocations will be offset by this value.
+    pub base_offset_bytes: u64,

Let's remove the _bytes suffix. Units can be specified in the doccomment so they are readily available without making the code heavier (dma.rs for instance does this).

rust/kernel/gpu/buddy.rs line 209

Quoted 5 lines
rust/kernel/gpu/buddy.rs new file +5 -0
+    /// Total physical memory size managed by the allocator in bytes.
+    pub physical_memory_size_bytes: u64,
+    /// Minimum allocation unit / chunk size in bytes, must be >= 4KB.
+    pub chunk_size_bytes: u64,
+}

This structure doesn't seem to be useful. I would understand using one if GpuBuddyParams had lots of members, some of which have a sensible default value - then we could implement Default and let users fill in the parameters they need.

But this structure has no constructor of any sort, requiring users to fill its 3 members manually - which is actually heavier than having 3 parameters to GpuBuddy::new. It is even deconstructed in GpuBuddyInner to store its members as 3 different fields! So let's skip it.

Quoted 4 lines
rust/kernel/gpu/buddy.rs new file +4 -0
+
+/// Parameters for allocating blocks from a GPU buddy allocator.
+pub struct GpuBuddyAllocParams {
+    /// Start of allocation range in bytes. Use 0 for beginning.

rust/kernel/gpu/buddy.rs line 223

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+    pub start_range_address: u64,
+    /// End of allocation range in bytes. Use 0 for entire range.
+    pub end_range_address: u64,
+    /// Total size to allocate in bytes.
+    pub size_bytes: u64,
+    /// Minimum block size for fragmented allocations in bytes.
+    pub min_block_size_bytes: u64,
+    /// Buddy allocator behavior flags.
+    pub buddy_flags: BuddyFlags,
+}

This one also feels like it could be rustified some more.

By this I mean that it e.g. allows the user to specify a range even if RANGE_ALLOCATION is not set. A C API rejects invalid combinations at runtime. A Rust API should make it impossible to even express them.

IIUC the flags mix the allocation type (simple, range, topdown) with some orthogonal properties (contiguous, cleared, trim_disable). There is also one bit (GPU_BUDDY_CLEARED) that is not relevant for allocation, but is freeing a block. We want to use the type system to only allow states that make sense to be constructed.

The allocation type can be expressed using a three-state enum. Start and end range only make sense for range allocations, so they would be part of the Range variant.

size and min_block_size (let's move the unit from the name into the doccomment) are always relevant, and should be regular arguments of their own. If min_block_sizes only accepts certain values (the C documentation says "alignment", so power of two?) then it should also use the relevant type for that (our own kernel::ptr::Alignment type?).

flags should be its own type (using impl_flags again?) allowing a combination of Contiguous, Cleared, and TrimDisable.

That would turn alloc_blocks into something like:

fn alloc_blocks(&self, alloc: AllocType, size: u64, min_block_size: Alignment, flags: AllocBlocksFlags)

(min_block_size is a u64 in the C API, but for an alignment a usize is enough and the conversion can be lossless).

rust/kernel/gpu/buddy.rs line 230

Quoted 7 lines
rust/kernel/gpu/buddy.rs new file +7 -0
+
+/// Inner structure holding the actual buddy allocator.
+///
+/// # Synchronization
+///
+/// The C `gpu_buddy` API requires synchronization (see `include/linux/gpu_buddy.h`).
+/// The internal [`GpuBuddyGuard`] ensures that the lock is held for all

GpuBuddyGuard is exported and not internal though.

Quoted 7 lines
rust/kernel/gpu/buddy.rs new file +7 -0
+/// allocator and free operations, preventing races between concurrent allocations
+/// and the freeing that occurs when [`AllocatedBlocks`] is dropped.
+///
+/// # Invariants
+///
+/// The inner [`Opaque`] contains a valid, initialized buddy allocator.
+#[pin_data(PinnedDrop)]

rust/kernel/gpu/buddy.rs line 247

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+struct GpuBuddyInner {
+    #[pin]
+    inner: Opaque<bindings::gpu_buddy>,
+
+    // TODO: Replace `Mutex<()>` with `Mutex<Opaque<..>>` once `Mutex::new()`
+    // accepts `impl PinInit<T>`.
+    #[pin]
+    lock: Mutex<()>,
+    /// Base offset for all allocations (does not change after init).
+    base_offset: u64,

This does not appear to be used in the C API - does it belong here? It looks like an additional convenience, but I'm not convinced that's the role of this type to provide this. But if it really is needed by all users (guess I'll find out after looking the Nova code :)), then keeping it is fair I guess.

Quoted 49 lines
rust/kernel/gpu/buddy.rs new file +49 -0
+    /// Cached chunk size (does not change after init).
+    chunk_size: u64,
+    /// Cached total size (does not change after init).
+    size: u64,
+}
+
+impl GpuBuddyInner {
+    /// Create a pin-initializer for the buddy allocator.
+    fn new(params: GpuBuddyParams) -> impl PinInit<Self, Error> {
+        let base_offset = params.base_offset_bytes;
+        let size = params.physical_memory_size_bytes;
+        let chunk_size = params.chunk_size_bytes;
+
+        try_pin_init!(Self {
+            inner <- Opaque::try_ffi_init(|ptr| {
+                // SAFETY: ptr points to valid uninitialized memory from the pin-init
+                // infrastructure. gpu_buddy_init will initialize the structure.
+                to_result(unsafe { bindings::gpu_buddy_init(ptr, size, chunk_size) })
+            }),
+            lock <- new_mutex!(()),
+            base_offset: base_offset,
+            chunk_size: chunk_size,
+            size: size,
+        })
+    }
+
+    /// Lock the mutex and return a guard for accessing the allocator.
+    fn lock(&self) -> GpuBuddyGuard<'_> {
+        GpuBuddyGuard {
+            inner: self,
+            _guard: self.lock.lock(),
+        }
+    }
+}
+
+#[pinned_drop]
+impl PinnedDrop for GpuBuddyInner {
+    fn drop(self: Pin<&mut Self>) {
+        let guard = self.lock();
+
+        // SAFETY: guard provides exclusive access to the allocator.
+        unsafe {
+            bindings::gpu_buddy_fini(guard.as_raw());
+        }
+    }
+}
+
+// SAFETY: GpuBuddyInner can be sent between threads.
+unsafe impl Send for GpuBuddyInner {}

rust/kernel/gpu/buddy.rs line 306

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+
+// SAFETY: GpuBuddyInner is `Sync` because the internal GpuBuddyGuard
+// serializes all access to the C allocator, preventing data races.
+unsafe impl Sync for GpuBuddyInner {}
+
+/// Guard that proves the lock is held, enabling access to the allocator.
+///
+/// # Invariants
+///
+/// The inner `_guard` holds the lock for the duration of this guard's lifetime.

Private members should not be mentioned in public documentation. Also is this invariant ever referenced when enforced and to justify an unsafe block? If not I don't think there is a point in having it.

rust/kernel/gpu/buddy.rs line 307

Quoted 1 line
rust/kernel/gpu/buddy.rs new file +1 -0
+pub(crate) struct GpuBuddyGuard<'a> {

IIUC this type can be private.

Quoted 9 lines
rust/kernel/gpu/buddy.rs new file +9 -0
+    inner: &'a GpuBuddyInner,
+    _guard: MutexGuard<'a, ()>,
+}
+
+impl GpuBuddyGuard<'_> {
+    /// Get a raw pointer to the underlying C `gpu_buddy` structure.
+    fn as_raw(&self) -> *mut bindings::gpu_buddy {
+        self.inner.inner.get()
+    }

rust/kernel/gpu/buddy.rs line 326

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+}
+
+/// GPU buddy allocator instance.
+///
+/// This structure wraps the C `gpu_buddy` allocator using reference counting.
+/// The allocator is automatically cleaned up when all references are dropped.
+///
+/// # Invariants
+///
+/// The inner [`Arc`] points to a valid, initialized GPU buddy allocator.

Most people looking for the documentation will reach it through GpuBuddy. I think we should either move the module-level documentation to this type, or add a reference to the module so users can easily find how to use it.

Quoted 18 lines
rust/kernel/gpu/buddy.rs new file +18 -0
+pub struct GpuBuddy(Arc<GpuBuddyInner>);
+
+impl GpuBuddy {
+    /// Create a new buddy allocator.
+    ///
+    /// Creates a buddy allocator that manages a contiguous address space of the given
+    /// size, with the specified minimum allocation unit (chunk_size must be at least 4KB).
+    pub fn new(params: GpuBuddyParams) -> Result<Self> {
+        Ok(Self(Arc::pin_init(GpuBuddyInner::new(params), GFP_KERNEL)?))
+    }
+
+    /// Get the base offset for allocations.
+    pub fn base_offset(&self) -> u64 {
+        self.0.base_offset
+    }
+
+    /// Get the chunk size (minimum allocation unit).
+    pub fn chunk_size(&self) -> u64 {

rust/kernel/gpu/buddy.rs line 354

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+        self.0.chunk_size
+    }
+
+    /// Get the total managed size.
+    pub fn size(&self) -> u64 {
+        self.0.size
+    }
+
+    /// Get the available (free) memory in bytes.
+    pub fn free_memory_bytes(&self) -> u64 {

Same as struct members, the unit doesn't need to be in the method name - the doccomment is sufficient.

Quoted 12 lines
rust/kernel/gpu/buddy.rs new file +12 -0
+        let guard = self.0.lock();
+
+        // SAFETY: guard provides exclusive access to the allocator.
+        unsafe { (*guard.as_raw()).avail }
+    }
+
+    /// Allocate blocks from the buddy allocator.
+    ///
+    /// Returns a pin-initializer for [`AllocatedBlocks`].
+    ///
+    /// Takes `&self` instead of `&mut self` because the internal [`Mutex`] provides
+    /// synchronization - no external `&mut` exclusivity needed.

rust/kernel/gpu/buddy.rs line 376

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+    pub fn alloc_blocks(
+        &self,
+        params: &GpuBuddyAllocParams,
+    ) -> impl PinInit<AllocatedBlocks, Error> {
+        let buddy_arc = Arc::clone(&self.0);
+        let start = params.start_range_address;
+        let end = params.end_range_address;
+        let size = params.size_bytes;
+        let min_block_size = params.min_block_size_bytes;
+        let flags = params.buddy_flags;

These local variables are required so the closure below is not confused by the lifetime of params. But since you are copying its content anyway, you could just make GpuBuddyAllocParams derive Copy, pass params by value, and use its members directly in the closure.

That probably won't be needed if we split GpuBuddyAllocParams as I suggested though.

Quoted 47 lines
rust/kernel/gpu/buddy.rs new file +47 -0
+
+        // Create pin-initializer that initializes list and allocates blocks.
+        try_pin_init!(AllocatedBlocks {
+            buddy: buddy_arc,
+            list <- CListHead::new(),
+            flags: flags,
+            _: {
+                // Lock while allocating to serialize with concurrent frees.
+                let guard = buddy.lock();
+
+                // SAFETY: `guard` provides exclusive access to the buddy allocator.
+                to_result(unsafe {
+                    bindings::gpu_buddy_alloc_blocks(
+                        guard.as_raw(),
+                        start,
+                        end,
+                        size,
+                        min_block_size,
+                        list.as_raw(),
+                        flags.as_raw(),
+                    )
+                })?
+            }
+        })
+    }
+}
+
+/// Allocated blocks from the buddy allocator with automatic cleanup.
+///
+/// This structure owns a list of allocated blocks and ensures they are
+/// automatically freed when dropped. Use `iter()` to iterate over all
+/// allocated [`Block`] structures.
+///
+/// # Invariants
+///
+/// - `list` is an initialized, valid list head containing allocated blocks.
+#[pin_data(PinnedDrop)]
+pub struct AllocatedBlocks {
+    #[pin]
+    list: CListHead,
+    buddy: Arc<GpuBuddyInner>,
+    flags: BuddyFlags,
+}
+
+impl AllocatedBlocks {
+    /// Check if the block list is empty.
+    pub fn is_empty(&self) -> bool {

rust/kernel/gpu/buddy.rs line 433

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+        // An empty list head points to itself.
+        !self.list.is_linked()
+    }
+
+    /// Iterate over allocated blocks.
+    ///
+    /// Returns an iterator yielding [`AllocatedBlock`] values. Each [`AllocatedBlock`]
+    /// borrows `self` and is only valid for the duration of that borrow.
+    pub fn iter(&self) -> impl Iterator<Item = AllocatedBlock<'_>> + '_ {
+        // SAFETY: list contains gpu_buddy_block items linked via __bindgen_anon_1.link.

IIUC the type invariant should be invoked explicitly as we are using it to justify this unsafe block (i.e. "per the type invariant, ...").

Quoted 17 lines
rust/kernel/gpu/buddy.rs new file +17 -0
+        let clist = clist_create!(unsafe {
+            self.list.as_raw(),
+            Block,
+            bindings::gpu_buddy_block,
+            __bindgen_anon_1.link
+        });
+
+        clist
+            .iter()
+            .map(|block| AllocatedBlock { block, alloc: self })
+    }
+}
+
+#[pinned_drop]
+impl PinnedDrop for AllocatedBlocks {
+    fn drop(self: Pin<&mut Self>) {
+        let guard = self.buddy.lock();

rust/kernel/gpu/buddy.rs line 460

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+
+        // SAFETY:
+        // - list is valid per the type's invariants.
+        // - guard provides exclusive access to the allocator.
+        // CAST: BuddyFlags were validated to fit in u32 at construction.
+        unsafe {
+            bindings::gpu_buddy_free_list(
+                guard.as_raw(),
+                self.list.as_raw(),
+                self.flags.as_raw() as u32,

You won't need this as if you make BuddyFlags wrap a u32 instead of a usize.

Quoted 27 lines
rust/kernel/gpu/buddy.rs new file +27 -0
+            );
+        }
+    }
+}
+
+/// A GPU buddy block.
+///
+/// Transparent wrapper over C `gpu_buddy_block` structure. This type is returned
+/// as references during iteration over [`AllocatedBlocks`].
+///
+/// # Invariants
+///
+/// The inner [`Opaque`] contains a valid, allocated `gpu_buddy_block`.
+#[repr(transparent)]
+pub struct Block(Opaque<bindings::gpu_buddy_block>);
+
+impl Block {
+    /// Get a raw pointer to the underlying C block.
+    fn as_raw(&self) -> *mut bindings::gpu_buddy_block {
+        self.0.get()
+    }
+
+    /// Get the block's offset in the address space.
+    pub(crate) fn offset(&self) -> u64 {
+        // SAFETY: self.as_raw() is valid per the type's invariants.
+        unsafe { bindings::gpu_buddy_block_offset(self.as_raw()) }
+    }

rust/kernel/gpu/buddy.rs line 497

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+
+    /// Get the block order.
+    pub(crate) fn order(&self) -> u32 {
+        // SAFETY: self.as_raw() is valid per the type's invariants.
+        unsafe { bindings::gpu_buddy_block_order(self.as_raw()) }
+    }
+}
+
+// SAFETY: `Block` is not modified after allocation for the lifetime
+// of `AllocatedBlock`.

This safety comment should not need to reference another type - how about something like "Block is a wrapper around gpu_buddy_block which can be sent across threads safely".

rust/kernel/gpu/buddy.rs line 501

Quoted 4 lines
rust/kernel/gpu/buddy.rs new file +4 -0
+unsafe impl Send for Block {}
+
+// SAFETY: `Block` is not modified after allocation for the lifetime
+// of `AllocatedBlock`.

Here as well. I'd say that the block is only accessed through shared references after allocation, and thus safe to access concurrently across threads.

author reviewer
#6 lore raw

Re: [PATCH v11 4/4] rust: gpu: Add GPU buddy allocator bindings

Hi Alex,

Quoted 10 lines · Alexandre Courbot

rust/kernel/gpu/buddy.rs

Quoted 3 lines
rust/kernel/gpu/buddy.rs new file +3 -0
+//! # Examples
+//!
+//! ``` [..]

This is a very long example illustrating many use-cases. It is long enough that it is difficult to grasp where each example start. Can I suggest to aerate it a bit by splitting it into several examples, with a bit of regular text explaining what each example does, similarly to the documentation of the Bounded type?

Quoted 2 lines

You can hide the creation of the GpuBuddy after the first example to keep things concise.

Done. Split into four separate examples with descriptive text between them. Subsequent examples hide imports and buddy creation with # lines, and adjust based on your other suggestions.

Quoted 6 lines
Quoted 1 line

+//! buddy_flags: BuddyFlags::try_new(BuddyFlags::RANGE_ALLOCATION)?,

Why can a BuddyFlags creation fail if we give it a valid value? It looks like its consts should be of the type BuddyFlags themselves so we can use them directly. Actually, we should probably use impl_flags! for it.

Good point. Switched to impl_flags!. I made it wrap u32 and individual flags are BuddyFlag enum variants. Construction is infallible. Invalid combinations will be caught by the C allocator at alloc time.

Quoted 9 lines

rust/kernel/gpu/buddy.rs line 52

Quoted 5 lines
rust/kernel/gpu/buddy.rs new file +5 -0
+//! for block in topdown.iter() {
+//!     assert_eq!(block.offset(), (SZ_1G - SZ_16M) as u64);
+//!     assert_eq!(block.order(), 12); // 2^12 pages
+//!     assert_eq!(block.size(), SZ_16M as u64);
+//! }

IIUC there should be only one block here, right? That for loop should be replaced by a call to next() followed by another one checking that the result is None to be a valid test.

Ah, good point, fixed as follows:
let block = topdown.iter().next().expect("expected one block"); assert_eq!(block.offset(), (SZ_1G - SZ_16M) as u64); assert_eq!(block.order(), 12); assert_eq!(block.size(), SZ_16M as u64);

Quoted 5 lines
Quoted 1 line

+//! drop(topdown);

Here is a good chance to mention that dropping the allocation will return it - it's expected, but not entirely obvious when you read this for the first time.

Added a comment: "Dropping the allocation returns the memory to the buddy allocator."

Quoted 4 lines
Quoted 1 line

+//! params.buddy_flags = BuddyFlags::try_new(BuddyFlags::RANGE_ALLOCATION)?;

Let's recreate the params for each example to make it self-contained instead of modifying the first one.

Done, each example now creates its own self-contained params.

Quoted 5 lines
Quoted 1 line

+ if flags > u32::MAX as usize {

These as conversions are unfortunate - I will try to graduate the infallible convertors from Nova into kernel soon so we can avoid them, but for now I guess there's nothing we can do unfortunately.

Since I switched to impl_flags! with u32, the u32::MAX check is gone.

Quoted 7 lines

rust/kernel/gpu/buddy.rs line 189

Quoted 3 lines
rust/kernel/gpu/buddy.rs new file +3 -0
+        if (flags & Self::RANGE_ALLOCATION) != 0 && (flags & Self::TOPDOWN_ALLOCATION) != 0 {
+            return Err(EINVAL);
+        }

This indicates that we should use the type system to prevent such constructs from even being attempted - more on this on GpuBuddyAllocParams.

The C API supports flag combinations (e.g. RANGE+CLEAR, TOPDOWN+CLEAR), so we model flags as combinable bitflags via impl_flags! as you suggested, rather than a mutually-exclusive enum. Invalid combinations are caught by the C allocator at runtime. Also fixed a bug here: RANGE+TOPDOWN is valid in C (TOPDOWN is just unused in the range path), so the old rejection was wrong. Removed it.

Quoted 5 lines
Quoted 1 line

+ pub base_offset_bytes: u64,

Let's remove the _bytes suffix. Units can be specified in the doccomment so they are readily available without making the code heavier (dma.rs for instance does this).

Done.

Quoted 7 lines
Quoted 1 line

+pub struct GpuBuddyParams {

This structure doesn't seem to be useful. I would understand using one if GpuBuddyParams had lots of members, some of which have a sensible default value - then we could implement Default and let users fill in the parameters they need.

Quoted 5 lines

But this structure has no constructor of any sort, requiring users to fill its 3 members manually - which is actually heavier than having 3 parameters to GpuBuddy::new. It is even deconstructed in GpuBuddyInner to store its members as 3 different fields! So let's skip it.

I'd prefer to keep the struct -- all three parameters are u64, so positional arguments would be easy to silently misorder. The struct also makes call sites more readable since Rust has no named function call parameters.

Quoted 8 lines
Quoted 1 line

+pub struct GpuBuddyAllocParams {

This one also feels like it could be rustified some more.

By this I mean that it e.g. allows the user to specify a range even if RANGE_ALLOCATION is not set. A C API rejects invalid combinations at runtime. A Rust API should make it impossible to even express them.

Quoted 5 lines

[...]

That would turn alloc_blocks into something like:

fn alloc_blocks(&self, alloc: AllocType, size: u64, min_block_size: Alignment, flags: AllocBlocksFlags)

The C API supports combining allocation modes with modifiers (e.g. RANGE+CLEAR, TOPDOWN+CLEAR), so modeling the mode as a mutually-exclusive enum would lose valid combinations. More importantly, if the C allocator evolves its flag semantics (new combinations become valid, or existing constraints change), an enum on the Rust side would break. It's simpler and more maintainable to pass combinable flags and let the C allocator validate -- which it already does. The switch to impl_flags! will work for us without over-constraining.

Quoted 3 lines
Quoted 1 line

+/// The internal [GpuBuddyGuard] ensures that the lock is held for all

GpuBuddyGuard is exported and not internal though.

Fixed, removed "internal" wording.

Quoted 7 lines
Quoted 1 line

+ base_offset: u64,

This does not appear to be used in the C API - does it belong here? It looks like an additional convenience, but I'm not convinced that's the role of this type to provide this. But if it really is needed by all users (guess I'll find out after looking the Nova code :)), then keeping it is fair I guess.

Yes, base_offset is needed by nova-core. The GPU's usable VRAM starts at usable_vram_start from the GSP firmware parameters:

    GpuBuddyParams {
        base_offset: params.usable_vram_start,
        physical_memory_size: params.usable_vram_size,
        chunk_size: SZ_4K.into_safe_cast(),
    }

AllocatedBlock::offset() then adds base_offset to return absolute VRAM addresses, so callers don't need to track the offset themselves.

Quoted 11 lines

rust/kernel/gpu/buddy.rs line 306

Quoted 3 lines
rust/kernel/gpu/buddy.rs new file +3 -0
+/// # Invariants
+///
+/// The inner `_guard` holds the lock for the duration of this guard's lifetime.

Private members should not be mentioned in public documentation. Also is this invariant ever referenced when enforced and to justify an unsafe block? If not I don't think there is a point in having it.

Quoted 2 lines

+pub(crate) struct GpuBuddyGuard<'a> {

IIUC this type can be private.

Done, made GpuBuddyGuard private and converted to a regular // comment.

Quoted 4 lines
Quoted 1 line

+ pub fn free_memory_bytes(&self) -> u64 {

Same as struct members, the unit doesn't need to be in the method name - the doccomment is sufficient.

Renamed.

Quoted 6 lines
Quoted 1 line

+pub struct GpuBuddy(Arc<GpuBuddyInner>);

Most people looking for the documentation will reach it through GpuBuddy. I think we should either move the module-level documentation to this type, or add a reference to the module so users can easily find how to use it.

Ok, I refer to the module-level doc on the struct.

Quoted 10 lines

rust/kernel/gpu/buddy.rs line 376

Quoted 5 lines
rust/kernel/gpu/buddy.rs new file +5 -0
+        let start = params.start_range_address;
+        let end = params.end_range_address;
+        let size = params.size_bytes;
+        let min_block_size = params.min_block_size_bytes;
+        let flags = params.buddy_flags;

These local variables are required so the closure below is not confused by the lifetime of params. But since you are copying its content anyway, you could just make GpuBuddyAllocParams derive Copy, pass params by value, and use its members directly in the closure.

What I will do is just pass it by value (instead of the reference that I'm currently passing), and then let the compiler decide if it needs to make copies or not. In the future, if we change it to not making copies inside the function, then it will just fallback to a non-copy move. However, if implemented as copy trait, it might always be copied.

Actually, it turns out that when I pass it by value, I can also get rid of the intermediate variables so this is great and has the effect you were intending!

Quoted 4 lines
Quoted 1 line

+ // SAFETY: list contains gpu_buddy_block items linked via __bindgen_anon_1.link.

IIUC the type invariant should be invoked explicitly as we are using it to justify this unsafe block (i.e. "per the type invariant, ...").

Fixed.

Quoted 4 lines
Quoted 1 line

+ self.flags.as_raw() as u32,

You won't need this as if you make BuddyFlags wrap a u32 instead of a usize.

Yes! Done.

Quoted 7 lines

rust/kernel/gpu/buddy.rs line 498

Quoted 3 lines
rust/kernel/gpu/buddy.rs new file +3 -0
+// SAFETY: `Block` is not modified after allocation for the lifetime
+// of `AllocatedBlock`.
+unsafe impl Send for Block {}

This safety comment should not need to reference another type - how about something like "Block is a wrapper around gpu_buddy_block which can be sent across threads safely".

Fixed.

Quoted 7 lines

rust/kernel/gpu/buddy.rs line 502

Quoted 3 lines
rust/kernel/gpu/buddy.rs new file +3 -0
+// SAFETY: `Block` is not modified after allocation for the lifetime
+// of `AllocatedBlock`.
+unsafe impl Sync for Block {}

Here as well. I'd say that the block is only accessed through shared references after allocation, and thus safe to access concurrently across threads.

Fixed.

Thanks for the thorough review! Looks a lot better now, and I am looking forward to sending the next revision soon.

Signature · 1 line
Joel Fernandes
#7 lore raw

Re: [PATCH v11 4/4] rust: gpu: Add GPU buddy allocator bindings

Quoted 11 lines · Joel Fernandes
Quoted 10 lines

This structure doesn't seem to be useful. I would understand using one if GpuBuddyParams had lots of members, some of which have a sensible default value - then we could implement Default and let users fill in the parameters they need.

But this structure has no constructor of any sort, requiring users to fill its 3 members manually - which is actually heavier than having 3 parameters to GpuBuddy::new. It is even deconstructed in GpuBuddyInner to store its members as 3 different fields! So let's skip it.

Quoted 4 lines

I'd prefer to keep the struct -- all three parameters are u64, so positional arguments would be easy to silently misorder. The struct also makes call sites more readable since Rust has no named function call parameters.

Fair point about the 3 parameters being easily confused. If you keep it, can you also store it in GpuBuddyInner instead of deconstructing it into 3 members?

Quoted 11 lines
Quoted 11 lines
Quoted 2 lines

+pub struct GpuBuddyAllocParams {

This one also feels like it could be rustified some more.

By this I mean that it e.g. allows the user to specify a range even if RANGE_ALLOCATION is not set. A C API rejects invalid combinations at runtime. A Rust API should make it impossible to even express them.

[...]

Quoted 7 lines
Quoted 3 lines

That would turn alloc_blocks into something like:

fn alloc_blocks(&self, alloc: AllocType, size: u64, min_block_size: Alignment, flags: AllocBlocksFlags)

The C API supports combining allocation modes with modifiers (e.g. RANGE+CLEAR, TOPDOWN+CLEAR), so modeling the mode as a mutually-exclusive enum would lose valid combinations. More importantly,

What I suggested does allow you to combine allocation modes with modifiers. I should have pasted a bit of code for clarity, so here goes:

    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    pub enum GpuBuddyAllocMode {
        Simple,
        Range { start: u64, end: u64 },
        TopDown,
    }

    impl GpuBuddyAllocMode {
        // Returns the flag corresponding to the allocation mode.
        //
        // Intentionally private - for internal use.
        fn into_flags(self) -> usize {
            match self {
                Self::Simple => 0,
                Self::Range { .. } => bindings::GPU_BUDDY_RANGE_ALLOCATION,
                Self::TopDown => bindings::GPU_BUDDY_TOPDOWN_ALLOCATION,
            }
        }
    }

    impl_flags!(
        #[derive(Copy, Clone, PartialEq, Eq, Default)]
        pub struct GpuBuddyAllocFlags(u32);

        #[derive(Copy, Clone, PartialEq, Eq)]
        pub enum GpuBuddyAllocFlag {
            Contiguous = bindings::GPU_BUDDY_CONTIGUOUS_ALLOCATION as u32,
            Clear = bindings::GPU_BUDDY_CLEAR_ALLOCATION as u32,
            TrimDisable = bindings::GPU_BUDDY_TRIM_DISABLE as u32,
        }
    );

    pub struct GpuBuddyAllocParams {
        mode: GpuBuddyAllocMode,
        size: u64,
        min_block_size: u64,
        flags: GpuBuddyAllocFlags,
    }

Now instead of doing something like:

    let params = GpuBuddyAllocParams {
        start_range_address: 0,
        end_range_address: 0,
        size: SZ_16M as u64,
        min_block_size: SZ_16M as u64,
        buddy_flags: BuddyFlag::TopdownAllocation.into(),
    };

You would have:

    let params = GpuBuddyAllocParams {
        // No unneeded `start_range` and `end_range`!
        mode: GpuBuddyAllocMode::TopDown,
        size: SZ_16M as u64,
        min_block_size: SZ_16M as u64,
        flags: Default::default(),
    };

And for a cleared range allocation:

    let params = GpuBuddyAllocParams {
        mode: GpuBuddyAllocMode::Range {
            start: 0,
            end: SZ_16M as u64,
        },
        size: SZ_16M as u64,
        min_block_size: SZ_16M as u64,
        flags: GpuBuddyAllocFlag::Clear,
    };

Actually the parameters are now distinct enough that you don't need a type to prevent confusion. A block allocation now just reads like a nice sentence:

    buddy.alloc_blocks(
        GpuBuddyAllocMode::Range {
            start: 0,
            end: SZ_16M,
        },
        SZ_16M,
        // `min_block_size` should be an `Alignment`, the C API even
        // returns an error if it is not a power of 2.
        Alignment::new::<SZ_16M>(),
        GpuBuddyAllocFlag::Clear,
    )?;

And the job of alloc_blocks is also simplified:

    let (start, end) = match mode {
        GpuBuddyAllocMode::Range { start, end } => (start, end),
        _ => (0, 0),
    };
    let flags = mode.into_flags() | u32::from(flags) as usize;
    // ... and just invoke the C API with these parameters.
Quoted 5 lines

if the C allocator evolves its flag semantics (new combinations become valid, or existing constraints change), an enum on the Rust side would break. It's simpler and more maintainable to pass combinable flags and let the C allocator validate -- which it already does. The switch to impl_flags! will work for us without over-constraining.

The evolution you describe is speculative and unlikely to happen as it would break all C users just the same. And if the C API adds new flags or allocation modes, we will have to update the Rust abstraction either way.

Rust abstractions should model the C API correctly. By hardening the way the C API can be used and stripping out invalid uses, we save headaches to users of the API who don't need to worry about whether the flag they pass will result in an error or simply be ignored, and we also save maintainer time who don't have to explain the intricacies of their APIs to confused users. :)

Quoted 18 lines
Quoted 4 lines
Quoted 2 lines

+/// The internal [GpuBuddyGuard] ensures that the lock is held for all

GpuBuddyGuard is exported and not internal though.

Fixed, removed "internal" wording.

Quoted 7 lines
Quoted 1 line

+ base_offset: u64,

This does not appear to be used in the C API - does it belong here? It looks like an additional convenience, but I'm not convinced that's the role of this type to provide this. But if it really is needed by all users (guess I'll find out after looking the Nova code :)), then keeping it is fair I guess.

Yes, base_offset is needed by nova-core. The GPU's usable VRAM starts at usable_vram_start from the GSP firmware parameters:

Quoted 8 lines
    GpuBuddyParams {
        base_offset: params.usable_vram_start,
        physical_memory_size: params.usable_vram_size,
        chunk_size: SZ_4K.into_safe_cast(),
    }

AllocatedBlock::offset() then adds base_offset to return absolute VRAM addresses, so callers don't need to track the offset themselves.

Sounds fair, I'll check how this is used in Nova.

Ah, another thing I've noticed while writing the example above:

Quoted 4 lines
rust/kernel/gpu/buddy.rs new file +4 -0
+#[pinned_drop]
+impl PinnedDrop for AllocatedBlocks {
+    fn drop(self: Pin<&mut Self>) {
+        let guard = self.buddy.lock();

rust/kernel/gpu/buddy.rs line 460

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+
+        // SAFETY:
+        // - list is valid per the type's invariants.
+        // - guard provides exclusive access to the allocator.
+        // CAST: BuddyFlags were validated to fit in u32 at construction.
+        unsafe {
+            bindings::gpu_buddy_free_list(
+                guard.as_raw(),
+                self.list.as_raw(),
+                self.flags.as_raw() as u32,

gpu_buddy_free_list only expects the CLEARED flag - actually it silently masks other flags. So you probably want to just pass 0 here - adding a Cleared field to GpuBuddyAllocFlag would also do the trick, but it looks risky to me as it relies on the promise that the user has cleared the buffer, which is not something we can guarantee. So I don't think we can support this safely.

If you just pass 0, then the flags member of AllocatedBlocks becomes unused and you can just drop it.

And another small one - some methods of Block are pub(crate) - I believe they should either be pub or kept private.

author reviewer
#8 lore raw

Re: [PATCH v11 4/4] rust: gpu: Add GPU buddy allocator bindings

Quoted 12 lines · Alexandre Courbot
Quoted 11 lines · Joel Fernandes
Quoted 10 lines

This structure doesn't seem to be useful. I would understand using one if GpuBuddyParams had lots of members, some of which have a sensible default value - then we could implement Default and let users fill in the parameters they need.

But this structure has no constructor of any sort, requiring users to fill its 3 members manually - which is actually heavier than having 3 parameters to GpuBuddy::new. It is even deconstructed in GpuBuddyInner to store its members as 3 different fields! So let's skip it.

Quoted 8 lines
Quoted 4 lines

I'd prefer to keep the struct -- all three parameters are u64, so positional arguments would be easy to silently misorder. The struct also makes call sites more readable since Rust has no named function call parameters.

Fair point about the 3 parameters being easily confused. If you keep it, can you also store it in GpuBuddyInner instead of deconstructing it into 3 members?

Done, good idea.

Quoted 34 lines
Quoted 12 lines
Quoted 12 lines
Quoted 3 lines

+pub struct GpuBuddyAllocParams {

This one also feels like it could be rustified some more.

By this I mean that it e.g. allows the user to specify a range even if RANGE_ALLOCATION is not set. A C API rejects invalid combinations at runtime. A Rust API should make it impossible to even express them.

[...]

Quoted 7 lines
Quoted 3 lines

That would turn alloc_blocks into something like:

fn alloc_blocks(&self, alloc: AllocType, size: u64, min_block_size: Alignment, flags: AllocBlocksFlags)

The C API supports combining allocation modes with modifiers (e.g. RANGE+CLEAR, TOPDOWN+CLEAR), so modeling the mode as a mutually-exclusive enum would lose valid combinations. More importantly,

What I suggested does allow you to combine allocation modes with modifiers. I should have pasted a bit of code for clarity, so here goes:

    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    pub enum GpuBuddyAllocMode {
        Simple,
        Range { start: u64, end: u64 },
        TopDown,
    }

    impl GpuBuddyAllocMode {
        // Returns the flag corresponding to the allocation mode.
        //
        // Intentionally private - for internal use.
Quoted 8 lines
        fn into_flags(self) -> usize {
            match self {
                Self::Simple => 0,
                Self::Range { .. } => bindings::GPU_BUDDY_RANGE_ALLOCATION,
                Self::TopDown => bindings::GPU_BUDDY_TOPDOWN_ALLOCATION,
            }
        }
    }

I took this bit from yours(more comments below).

Quoted 11 lines
    impl_flags!(
        #[derive(Copy, Clone, PartialEq, Eq, Default)]
        pub struct GpuBuddyAllocFlags(u32);

        #[derive(Copy, Clone, PartialEq, Eq)]
        pub enum GpuBuddyAllocFlag {
            Contiguous = bindings::GPU_BUDDY_CONTIGUOUS_ALLOCATION as u32,
            Clear = bindings::GPU_BUDDY_CLEAR_ALLOCATION as u32,
            TrimDisable = bindings::GPU_BUDDY_TRIM_DISABLE as u32,
        }
Quoted 9 lines
    );

    pub struct GpuBuddyAllocParams {
        mode: GpuBuddyAllocMode,
        size: u64,
        min_block_size: u64,
        flags: GpuBuddyAllocFlags,
    }

I took this bit from yours(more comments below).

Quoted 12 lines

Now instead of doing something like:

    let params = GpuBuddyAllocParams {
        start_range_address: 0,
        end_range_address: 0,
        size: SZ_16M as u64,
        min_block_size: SZ_16M as u64,
        buddy_flags: BuddyFlag::TopdownAllocation.into(),
    };

You would have:

Quoted 8 lines
    let params = GpuBuddyAllocParams {
        // No unneeded `start_range` and `end_range`!
        mode: GpuBuddyAllocMode::TopDown,
        size: SZ_16M as u64,
        min_block_size: SZ_16M as u64,
        flags: Default::default(),
    };

I took this bit from yours(more comments below).

Quoted 16 lines

And for a cleared range allocation:

    let params = GpuBuddyAllocParams {
        mode: GpuBuddyAllocMode::Range {
            start: 0,
            end: SZ_16M as u64,
        },
        size: SZ_16M as u64,
        min_block_size: SZ_16M as u64,
        flags: GpuBuddyAllocFlag::Clear,
    };

Actually the parameters are now distinct enough that you don't need a type to prevent confusion. A block allocation now just reads like a nice sentence:

Quoted 11 lines
    buddy.alloc_blocks(
        GpuBuddyAllocMode::Range {
            start: 0,
            end: SZ_16M,
        },
        SZ_16M,
        // `min_block_size` should be an `Alignment`, the C API even
        // returns an error if it is not a power of 2.
        Alignment::new::<SZ_16M>(),
        GpuBuddyAllocFlag::Clear,
    )?;

Makes sense, this is indeed better, I'll do it this way.

Quoted 16 lines

And the job of alloc_blocks is also simplified:

    let (start, end) = match mode {
        GpuBuddyAllocMode::Range { start, end } => (start, end),
        _ => (0, 0),
    };
    let flags = mode.into_flags() | u32::from(flags) as usize;
    // ... and just invoke the C API with these parameters.
Quoted 5 lines

if the C allocator evolves its flag semantics (new combinations become valid, or existing constraints change), an enum on the Rust side would break. It's simpler and more maintainable to pass combinable flags and let the C allocator validate -- which it already does. The switch to impl_flags! will work for us without over-constraining.

Quoted 4 lines

The evolution you describe is speculative and unlikely to happen as it would break all C users just the same. And if the C API adds new flags or allocation modes, we will have to update the Rust abstraction either way.

How/why would it break C users? Currently top down + range is silently ignored, implementing it is unlikely to break them.

I also wouldn't call it speculative: top-down within a range is a natural feature the C allocator could add right? By modeling modes as a mutually exclusive enum, we're disallowing a flag combination that could become valid in the future. That's fine for now, but something to keep in mind as we choose this design. We could add a new RangeTopDown mode variant in the future, though. That said, I've made the switch to the enum as
you suggested since it is cleaner code! And is more Rust-like as you pointed.

Quoted 8 lines

Rust abstractions should model the C API correctly. By hardening the way the C API can be used and stripping out invalid uses, we save headaches to users of the API who don't need to worry about whether the flag they pass will result in an error or simply be ignored, and we also save maintainer time who don't have to explain the intricacies of their APIs to confused users. :)

Sure, no argument on that one. ;-)

[...]

Quoted 46 lines
Quoted 11 lines
Quoted 7 lines
Quoted 1 line

+ base_offset: u64,

This does not appear to be used in the C API - does it belong here? It looks like an additional convenience, but I'm not convinced that's the role of this type to provide this. But if it really is needed by all users (guess I'll find out after looking the Nova code :)), then keeping it is fair I guess.

Yes, base_offset is needed by nova-core. The GPU's usable VRAM starts at usable_vram_start from the GSP firmware parameters:

Quoted 8 lines
    GpuBuddyParams {
        base_offset: params.usable_vram_start,
        physical_memory_size: params.usable_vram_size,
        chunk_size: SZ_4K.into_safe_cast(),
    }

AllocatedBlock::offset() then adds base_offset to return absolute VRAM addresses, so callers don't need to track the offset themselves.

Sounds fair, I'll check how this is used in Nova.

Ah, another thing I've noticed while writing the example above:

Quoted 4 lines
rust/kernel/gpu/buddy.rs new file +4 -0
+#[pinned_drop]
+impl PinnedDrop for AllocatedBlocks {
+    fn drop(self: Pin<&mut Self>) {
+        let guard = self.buddy.lock();

rust/kernel/gpu/buddy.rs line 460

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+
+        // SAFETY:
+        // - list is valid per the type's invariants.
+        // - guard provides exclusive access to the allocator.
+        // CAST: BuddyFlags were validated to fit in u32 at construction.
+        unsafe {
+            bindings::gpu_buddy_free_list(
+                guard.as_raw(),
+                self.list.as_raw(),
+                self.flags.as_raw() as u32,

gpu_buddy_free_list only expects the CLEARED flag - actually it silently masks other flags. So you probably want to just pass 0 here - adding a Cleared field to GpuBuddyAllocFlag would also do the trick, but it looks risky to me as it relies on the promise that the user has cleared the buffer, which is not something we can guarantee. So I don't think we can support this safely.

Quoted 2 lines

If you just pass 0, then the flags member of AllocatedBlocks becomes unused and you can just drop it.

Good catch, done!

Quoted 3 lines

And another small one - some methods of Block are pub(crate) - I believe they should either be pub or kept private.

Changed to pub. thanks,

Signature · 1 line
Joel Fernandes
#9 lore raw

Re: [PATCH v11 4/4] rust: gpu: Add GPU buddy allocator bindings

On Fri Feb 27, 2026 at 6:42 AM JST, Joel Fernandes wrote:
<snip>

Quoted 24 lines
Quoted 16 lines

And the job of alloc_blocks is also simplified:

    let (start, end) = match mode {
        GpuBuddyAllocMode::Range { start, end } => (start, end),
        _ => (0, 0),
    };
    let flags = mode.into_flags() | u32::from(flags) as usize;
    // ... and just invoke the C API with these parameters.
Quoted 5 lines

if the C allocator evolves its flag semantics (new combinations become valid, or existing constraints change), an enum on the Rust side would break. It's simpler and more maintainable to pass combinable flags and let the C allocator validate -- which it already does. The switch to impl_flags! will work for us without over-constraining.

Quoted 4 lines

The evolution you describe is speculative and unlikely to happen as it would break all C users just the same. And if the C API adds new flags or allocation modes, we will have to update the Rust abstraction either way.

How/why would it break C users? Currently top down + range is silently ignored, implementing it is unlikely to break them.

Quoted 7 lines

I also wouldn't call it speculative: top-down within a range is a natural feature the C allocator could add right? By modeling modes as a mutually exclusive enum, we're disallowing a flag combination that could become valid in the future. That's fine for now, but something to keep in mind as we choose this design. We could add a new RangeTopDown mode variant in the future, though. That said, I've made the switch to the enum as
you suggested since it is cleaner code! And is more Rust-like as you pointed.

Ah, I thought you were talking about new flags - the case you are quoting is different, and indeed more concerning. I guess if things were to change in that direction we would have to move top-down into its own flag and update our users. But for now we should try to support the existing actual semantics as closely as possible, and without any surprise if possible - what you get is exactly what you get, without anything swept under the rug (the silent masking done on the C side freaks me out a little bit tbh ^_^;).

Should top-down become a flag, our current API also wouldn't become incorrect - it would just be incomplete in that it doesn't support the new use-case until we update it, so thankfully that wouldn't be a critical issue.

maintainer
#10 lore raw

Re: [PATCH v11 4/4] rust: gpu: Add GPU buddy allocator bindings

In reply to Joel Fernandes

(Cc: Arun)

MAINTAINERS S: Maintained · line 23200

Quoted 7 lines · Joel Fernandes
MAINTAINERS +7 -0
+RUST [GPU BUDDY]
+M:	Joel Fernandes <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	rust/helpers/gpu.c
+F:	rust/kernel/gpu/

What about adding this to the existing GPU BUDDY ALLOCATOR entry? Maybe you can offer Matthew and Arun your help.

Also, this wouldn't be a subentry of "RUST", but the rust version of GPU BUDDY, so it would be "GPU BUDDY [RUST]".

Also, please add rust/kernel/gpu/ to the "DRM DRIVERS AND COMMON INFRASTRUCTURE [RUST]" entry.

rust/kernel/gpu/buddy.rs line 236

Quoted 12 lines
rust/kernel/gpu/buddy.rs new file +12 -0
+/// Inner structure holding the actual buddy allocator.
+///
+/// # Synchronization
+///
+/// The C `gpu_buddy` API requires synchronization (see `include/linux/gpu_buddy.h`).
+/// The internal [`GpuBuddyGuard`] ensures that the lock is held for all
+/// allocator and free operations, preventing races between concurrent allocations
+/// and the freeing that occurs when [`AllocatedBlocks`] is dropped.
+///
+/// # Invariants
+///
+/// The inner [`Opaque`] contains a valid, initialized buddy allocator.

The invariant should be justified in the constructor. Also, where is it used?

Quoted 1 line

+// SAFETY: GpuBuddyInner is Sync because the internal GpuBuddyGuard

I think it's more precise to refer to GpuBuddyInner::lock.

Quoted 18 lines
rust/kernel/gpu/buddy.rs new file +18 -0
+// serializes all access to the C allocator, preventing data races.
+unsafe impl Sync for GpuBuddyInner {}
+
+/// Guard that proves the lock is held, enabling access to the allocator.
+///
+/// # Invariants
+///
+/// The inner `_guard` holds the lock for the duration of this guard's lifetime.
+pub(crate) struct GpuBuddyGuard<'a> {
+    inner: &'a GpuBuddyInner,
+    _guard: MutexGuard<'a, ()>,
+}
+
+impl GpuBuddyGuard<'_> {
+    /// Get a raw pointer to the underlying C `gpu_buddy` structure.
+    fn as_raw(&self) -> *mut bindings::gpu_buddy {
+        self.inner.inner.get()
+    }

rust/kernel/gpu/buddy.rs line 326

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+}
+
+/// GPU buddy allocator instance.
+///
+/// This structure wraps the C `gpu_buddy` allocator using reference counting.
+/// The allocator is automatically cleaned up when all references are dropped.
+///
+/// # Invariants
+///
+/// The inner [`Arc`] points to a valid, initialized GPU buddy allocator.

Do we need this invariant? Isn't this implied by the GpuBuddyInner type?

rust/kernel/gpu/buddy.rs line 434

Quoted 7 lines
rust/kernel/gpu/buddy.rs new file +7 -0
+    /// Iterate over allocated blocks.
+    ///
+    /// Returns an iterator yielding [`AllocatedBlock`] values. Each [`AllocatedBlock`]
+    /// borrows `self` and is only valid for the duration of that borrow.
+    pub fn iter(&self) -> impl Iterator<Item = AllocatedBlock<'_>> + '_ {
+        // SAFETY: list contains gpu_buddy_block items linked via __bindgen_anon_1.link.
+        let clist = clist_create!(unsafe {

This macro has three separate safety requirements, please justify all of them. Also, please also use markdown in safety comments. Personally, I don't care too much, but it would be good to keep things consistent.

Quoted 31 lines
rust/kernel/gpu/buddy.rs new file +31 -0
+            self.list.as_raw(),
+            Block,
+            bindings::gpu_buddy_block,
+            __bindgen_anon_1.link
+        });
+
+        clist
+            .iter()
+            .map(|block| AllocatedBlock { block, alloc: self })
+    }
+}
+
+#[pinned_drop]
+impl PinnedDrop for AllocatedBlocks {
+    fn drop(self: Pin<&mut Self>) {
+        let guard = self.buddy.lock();
+
+        // SAFETY:
+        // - list is valid per the type's invariants.
+        // - guard provides exclusive access to the allocator.
+        // CAST: BuddyFlags were validated to fit in u32 at construction.
+        unsafe {
+            bindings::gpu_buddy_free_list(
+                guard.as_raw(),
+                self.list.as_raw(),
+                self.flags.as_raw() as u32,
+            );
+        }
+    }
+}
+

rust/kernel/gpu/buddy.rs line 475

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+/// A GPU buddy block.
+///
+/// Transparent wrapper over C `gpu_buddy_block` structure. This type is returned
+/// as references during iteration over [`AllocatedBlocks`].
+///
+/// # Invariants
+///
+/// The inner [`Opaque`] contains a valid, allocated `gpu_buddy_block`.
+#[repr(transparent)]
+pub struct Block(Opaque<bindings::gpu_buddy_block>);

Does this need to be public? I don't see it being exposed by any API.

Quoted 19 lines
rust/kernel/gpu/buddy.rs new file +19 -0
+
+impl Block {
+    /// Get a raw pointer to the underlying C block.
+    fn as_raw(&self) -> *mut bindings::gpu_buddy_block {
+        self.0.get()
+    }
+
+    /// Get the block's offset in the address space.
+    pub(crate) fn offset(&self) -> u64 {
+        // SAFETY: self.as_raw() is valid per the type's invariants.
+        unsafe { bindings::gpu_buddy_block_offset(self.as_raw()) }
+    }
+
+    /// Get the block order.
+    pub(crate) fn order(&self) -> u32 {
+        // SAFETY: self.as_raw() is valid per the type's invariants.
+        unsafe { bindings::gpu_buddy_block_order(self.as_raw()) }
+    }
+}

rust/kernel/gpu/buddy.rs line 504

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+
+// SAFETY: `Block` is not modified after allocation for the lifetime
+// of `AllocatedBlock`.
+unsafe impl Send for Block {}
+
+// SAFETY: `Block` is not modified after allocation for the lifetime
+// of `AllocatedBlock`.
+unsafe impl Sync for Block {}
+
+/// An allocated block with access to the GPU buddy allocator.

This needs a better description, i.e. what makes an `AllocatedBlock different compared to a "normal" Block.

rust/kernel/gpu/buddy.rs line 512

Quoted 8 lines
rust/kernel/gpu/buddy.rs new file +8 -0
+///
+/// It is returned by [`AllocatedBlocks::iter()`] and provides access to the
+/// GPU buddy allocator required for some accessors.
+///
+/// # Invariants
+///
+/// - `block` is a valid reference to an allocated [`Block`].
+/// - `alloc` is a valid reference to the [`AllocatedBlocks`] that owns this block.

References should always be valid, no need to mention that.

rust/kernel/gpu/buddy.rs line 514

Quoted 2 lines
rust/kernel/gpu/buddy.rs new file +2 -0
+pub struct AllocatedBlock<'a> {
+    block: &'a Block,

NIT: What about just naming it "this" and

Quoted 1 line

+ alloc: &'a AllocatedBlocks,

"blocks"?

Quoted 21 lines
rust/kernel/gpu/buddy.rs new file +21 -0
+}
+
+impl AllocatedBlock<'_> {
+    /// Get the block's offset in the address space.
+    ///
+    /// Returns the absolute offset including the allocator's base offset.
+    /// This is the actual address to use for accessing the allocated memory.
+    pub fn offset(&self) -> u64 {
+        self.alloc.buddy.base_offset + self.block.offset()
+    }
+
+    /// Get the block order (size = chunk_size << order).
+    pub fn order(&self) -> u32 {
+        self.block.order()
+    }
+
+    /// Get the block's size in bytes.
+    pub fn size(&self) -> u64 {
+        self.alloc.buddy.chunk_size << self.block.order()
+    }
+}
author reviewer
#11 lore raw

Re: [PATCH v11 4/4] rust: gpu: Add GPU buddy allocator bindings

Hi Danilo,

Quoted 12 lines · Danilo Krummrich

MAINTAINERS S: Maintained · line 23200

Quoted 8 lines
MAINTAINERS +7 -0
+RUST [GPU BUDDY]
+M:	Joel Fernandes <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	rust/helpers/gpu.c
+F:	rust/kernel/gpu/

What about adding this to the existing GPU BUDDY ALLOCATOR entry? Maybe you can offer Matthew and Arun your help.

Quoted 5 lines

Also, this wouldn't be a subentry of "RUST", but the rust version of GPU BUDDY, so it would be "GPU BUDDY [RUST]".

Also, please add rust/kernel/gpu/ to the "DRM DRIVERS AND COMMON INFRASTRUCTURE [RUST]" entry.

Both these additions sound good to me. Just to list all the options below so we save time on what we all agree on:

1. New GPU BUDDY [RUST] entry.

2. Add it under the existing "DRM DRIVERS AND COMMON INFRASTRUCTURE [RUST]"

3. Add the rust files under the existing "GPU BUDDY ALLOCATOR" and add myself and Alex as R:/M:

Which of these options make sense? Personally, I think #2 makes sense since we are already suggesting to add it to the existing DRM RUST entry anyway - we can just drop the new record and add rust/helpers/gpu.c there as well. That's what I've done for now. Does that work for you/others?

Quoted 5 lines

rust/kernel/gpu/buddy.rs line 236

Quoted 3 lines
rust/kernel/gpu/buddy.rs new file +3 -0
+/// # Invariants
+///
+/// The inner [`Opaque`] contains a valid, initialized buddy allocator.

The invariant should be justified in the constructor. Also, where is it used?

Done. Added // INVARIANT: justification in GpuBuddyInner::new() and cited at all usage sites (ex, alloc_blocks).

Quoted 4 lines

rust/kernel/gpu/buddy.rs line 299

Quoted 2 lines
rust/kernel/gpu/buddy.rs new file +2 -0
+// SAFETY: GpuBuddyInner is `Sync` because the internal GpuBuddyGuard
+// serializes all access to the C allocator, preventing data races.

I think it's more precise to refer to GpuBuddyInner::lock.

Agreed. Fixed to reference GpuBuddyInner::lock.

Quoted 5 lines

rust/kernel/gpu/buddy.rs line 326

Quoted 3 lines
rust/kernel/gpu/buddy.rs new file +3 -0
+/// # Invariants
+///
+/// The inner [`Arc`] points to a valid, initialized GPU buddy allocator.

Do we need this invariant? Isn't this implied by the GpuBuddyInner type?

Right, removed.

Quoted 5 lines

rust/kernel/gpu/buddy.rs line 434

Quoted 2 lines
rust/kernel/gpu/buddy.rs new file +2 -0
+        // SAFETY: list contains gpu_buddy_block items linked via __bindgen_anon_1.link.
+        let clist = clist_create!(unsafe {

This macro has three separate safety requirements, please justify all of them. Also, please also use markdown in safety comments.

Done. Expanded to justify all three safety requirements.

Quoted 3 lines
Quoted 1 line

+pub struct Block(Opaque<bindings::gpu_buddy_block>);

Does this need to be public? I don't see it being exposed by any API.

You're right. Made it private.

Quoted 4 lines
Quoted 1 line

+/// An allocated block with access to the GPU buddy allocator.

This needs a better description, i.e. what makes an `AllocatedBlock different compared to a "normal" Block.

Updated to the following:

/// A [Block] paired with its owning [AllocatedBlocks] context. ///
/// Unlike a raw [Block], which only knows its offset within the buddy address /// space, an [AllocatedBlock] also has access to the allocator's /// base_offset and chunk_size, enabling it to compute absolute offsets and /// byte sizes.
///
/// Returned by [AllocatedBlocks::iter()].
pub struct AllocatedBlock<'a> {
this: &'a Block,
blocks: &'a AllocatedBlocks,
}

Quoted 4 lines

rust/kernel/gpu/buddy.rs line 512

Quoted 2 lines
rust/kernel/gpu/buddy.rs new file +2 -0
+/// - `block` is a valid reference to an allocated [`Block`].
+/// - `alloc` is a valid reference to the [`AllocatedBlocks`] that owns this block.

References should always be valid, no need to mention that.

Removed.

Quoted 7 lines
Quoted 1 line

+ block: &'a Block,

NIT: What about just naming it "this" and

Quoted 1 line

+ alloc: &'a AllocatedBlocks,

"blocks"?

Done, that's better.

thanks,

--
Joel Fernandes

maintainer
#12 lore raw

Re: [PATCH v11 4/4] rust: gpu: Add GPU buddy allocator bindings

Quoted 25 lines · Joel Fernandes

Hi Danilo,

Quoted 12 lines · Danilo Krummrich

MAINTAINERS S: Maintained · line 23200

Quoted 8 lines
MAINTAINERS +7 -0
+RUST [GPU BUDDY]
+M: Joel Fernandes <[email protected]>
+L: [email protected]
+L: [email protected]
+S: Maintained
+F: rust/helpers/gpu.c
+F: rust/kernel/gpu/

What about adding this to the existing GPU BUDDY ALLOCATOR entry? Maybe you can offer Matthew and Arun your help.

Quoted 5 lines

Also, this wouldn't be a subentry of "RUST", but the rust version of GPU BUDDY, so it would be "GPU BUDDY [RUST]".

Also, please add rust/kernel/gpu/ to the "DRM DRIVERS AND COMMON INFRASTRUCTURE [RUST]" entry.

Both these additions sound good to me. Just to list all the options below so we save time on what we all agree on:

Quoted 8 lines

1. New GPU BUDDY [RUST] entry.

2. Add it under the existing "DRM DRIVERS AND COMMON INFRASTRUCTURE [RUST]"

3. Add the rust files under the existing "GPU BUDDY ALLOCATOR" and add myself and Alex as R:/M:

Number 3 please,

Dave.

maintainer
#13 lore raw

Re: [PATCH v11 4/4] rust: gpu: Add GPU buddy allocator bindings

Quoted 28 lines · Dave Airlie
Quoted 27 lines · Joel Fernandes

Hi Danilo,

Quoted 12 lines · Danilo Krummrich

MAINTAINERS S: Maintained · line 23200

Quoted 8 lines
MAINTAINERS +7 -0
+RUST [GPU BUDDY]
+M: Joel Fernandes <[email protected]>
+L: [email protected]
+L: [email protected]
+S: Maintained
+F: rust/helpers/gpu.c
+F: rust/kernel/gpu/

What about adding this to the existing GPU BUDDY ALLOCATOR entry? Maybe you can offer Matthew and Arun your help.

Quoted 5 lines

Also, this wouldn't be a subentry of "RUST", but the rust version of GPU BUDDY, so it would be "GPU BUDDY [RUST]".

Also, please add rust/kernel/gpu/ to the "DRM DRIVERS AND COMMON INFRASTRUCTURE [RUST]" entry.

Both these additions sound good to me. Just to list all the options below so we save time on what we all agree on:

1. New GPU BUDDY [RUST] entry.

Quoted 8 lines
Quoted 6 lines

2. Add it under the existing "DRM DRIVERS AND COMMON INFRASTRUCTURE [RUST]"

3. Add the rust files under the existing "GPU BUDDY ALLOCATOR" and add myself and Alex as R:/M:

Number 3 please,

Just in case it wasn't clear from my suggestion above, please also do 2 in addition.

Thanks,
Danilo

maintainer
#14 lore raw

Re: [PATCH v11 4/4] rust: gpu: Add GPU buddy allocator bindings

In reply to Joel Fernandes

rust/kernel/lib.rs line 100

Quoted 9 lines · Joel Fernandes
rust/kernel/lib.rs +1 -0
index 58dbb02c5197..b8287601f464 100644
@@ -97,6 +97,8 @@
 pub mod firmware;
 pub mod fmt;
 pub mod fs;
+#[cfg(CONFIG_GPU_BUDDY)]

Has to be #[cfg(CONFIG_GPU_BUDDY = "y")] for now.

Quoted 6 lines
rust/kernel/lib.rs +1 -0
+pub mod gpu;
 #[cfg(CONFIG_I2C = "y")]
 pub mod i2c;
 pub mod id_pool;
Signature · 1 line
2.34.1
author reviewer
#15 lore raw

Re: [PATCH v11 4/4] rust: gpu: Add GPU buddy allocator bindings

Quoted 3 lines · Danilo Krummrich
Quoted 1 line

+#[cfg(CONFIG_GPU_BUDDY)]

Has to be #[cfg(CONFIG_GPU_BUDDY = "y")] for now.

Fixed, thanks.

--
Joel Fernandes

#16 lore raw

✗ Fi.CI.BUILD: failure for Rust GPU buddy allocator bindings

In reply to Joel Fernandes
== Series Details ==

== Series Details ==

Series: Rust GPU buddy allocator bindings
URL : https://patchwork.freedesktop.org/series/162097/
State : failure

== Summary ==

Error: patch https://patchwork.freedesktop.org/api/1.0/series/162097/revisions/1/mbox/ not applied
Applying: gpu: Move DRM buddy allocator one level up (part one)
error: mode change for drivers/gpu/drm/lib/drm_random.h, which is not in current HEAD
error: could not build fake ancestor
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 gpu: Move DRM buddy allocator one level up (part one)
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Build failed, no error log produced