Demonstrates usage of the DRM buddy allocator bindings through
a simple test module that initializes the allocator, performs
allocations, and prints information about the allocated blocks.
Signed-off-by: Joel Fernandes <joelagnelf@nvidia.com>
---
samples/rust/Kconfig | 14 +++++
samples/rust/Makefile | 1 +
samples/rust/rust_drm_buddy.rs | 106 +++++++++++++++++++++++++++++++++
3 files changed, 121 insertions(+)
create mode 100644 samples/rust/rust_drm_buddy.rs
diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
index b45631e2593c..8ccb4064ba91 100644
--- a/samples/rust/Kconfig
+++ b/samples/rust/Kconfig
@@ -21,6 +21,20 @@ config SAMPLE_RUST_CLIST
If unsure, say N.
+config SAMPLE_RUST_DRM_BUDDY
+ tristate "DRM buddy allocator sample"
+ depends on DRM_BUDDY
+ help
+ This option builds the Rust DRM buddy allocator sample.
+
+ The sample demonstrates using the DRM buddy allocator bindings
+ to allocate and free memory blocks.
+
+ To compile this as a module, choose M here:
+ the module will be called rust_drm_buddy.
+
+ If unsure, say N.
+
config SAMPLE_RUST_CONFIGFS
tristate "Configfs sample"
depends on CONFIGFS_FS
diff --git a/samples/rust/Makefile b/samples/rust/Makefile
index f8899c0df762..a56204ee4e96 100644
--- a/samples/rust/Makefile
+++ b/samples/rust/Makefile
@@ -2,6 +2,7 @@
ccflags-y += -I$(src) # needed for trace events
obj-$(CONFIG_SAMPLE_RUST_CLIST) += rust_clist.o
+obj-$(CONFIG_SAMPLE_RUST_DRM_BUDDY) += rust_drm_buddy.o
obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) += rust_misc_device.o
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
diff --git a/samples/rust/rust_drm_buddy.rs b/samples/rust/rust_drm_buddy.rs
new file mode 100644
index 000000000000..96907bc19243
--- /dev/null
+++ b/samples/rust/rust_drm_buddy.rs
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Rust DRM buddy allocator sample.
+//!
+//! This sample demonstrates using the DRM buddy allocator from Rust.
+
+use kernel::{
+ drm::buddy::{
+ BuddyFlags,
+ DrmBuddy, //
+ },
+ prelude::*,
+ sizes::*, //
+};
+
+module! {
+ type: RustDrmBuddySample,
+ name: "rust_drm_buddy",
+ authors: ["Joel Fernandes"],
+ description: "DRM buddy allocator sample",
+ license: "GPL",
+}
+
+struct RustDrmBuddySample;
+
+impl kernel::Module for RustDrmBuddySample {
+ fn init(_module: &'static ThisModule) -> Result<Self> {
+ // Create a buddy allocator managing 1GB with 4KB chunks.
+ let buddy = DrmBuddy::new(SZ_1G, SZ_4K)?;
+
+ pr_info!("=== Test 1: Single 16MB block ===\n");
+ let allocated = buddy.alloc_blocks(
+ 0,
+ 0,
+ SZ_16M,
+ SZ_4K,
+ BuddyFlags::RANGE_ALLOCATION,
+ GFP_KERNEL,
+ )?;
+
+ let mut count = 0;
+ for block in &allocated {
+ pr_info!(
+ " Block {}: offset=0x{:x}, order={}, size={}\n",
+ count,
+ block.offset(),
+ block.order(),
+ block.size(&buddy)
+ );
+ count += 1;
+ }
+ pr_info!(" Total: {} blocks\n", count);
+ drop(allocated);
+
+ pr_info!("=== Test 2: Three 4MB blocks ===\n");
+ let allocated = buddy.alloc_blocks(
+ 0,
+ 0,
+ SZ_4M * 3,
+ SZ_4K,
+ BuddyFlags::RANGE_ALLOCATION,
+ GFP_KERNEL,
+ )?;
+
+ count = 0;
+ for block in &allocated {
+ pr_info!(
+ " Block {}: offset=0x{:x}, order={}, size={}\n",
+ count,
+ block.offset(),
+ block.order(),
+ block.size(&buddy)
+ );
+ count += 1;
+ }
+ pr_info!(" Total: {} blocks\n", count);
+ drop(allocated);
+
+ pr_info!("=== Test 3: Two 8MB blocks ===\n");
+ let allocated = buddy.alloc_blocks(
+ 0,
+ 0,
+ SZ_8M * 2,
+ SZ_4K,
+ BuddyFlags::RANGE_ALLOCATION,
+ GFP_KERNEL,
+ )?;
+
+ count = 0;
+ for block in &allocated {
+ pr_info!(
+ " Block {}: offset=0x{:x}, order={}, size={}\n",
+ count,
+ block.offset(),
+ block.order(),
+ block.size(&buddy)
+ );
+ count += 1;
+ }
+ pr_info!(" Total: {} blocks\n", count);
+
+ pr_info!("=== All tests passed! ===\n");
+
+ Ok(RustDrmBuddySample {})
+ }
+}
--
2.34.1
On Fri Oct 31, 2025 at 4:06 AM JST, Joel Fernandes wrote: > Demonstrates usage of the DRM buddy allocator bindings through > a simple test module that initializes the allocator, performs > allocations, and prints information about the allocated blocks. > > Signed-off-by: Joel Fernandes <joelagnelf@nvidia.com> If this can be run as a kunit test (and it looks like it does?), I think this would be more useful as a module-level doctest of `buddy.rs`, where it can also be useful for educational purposes.
On 30/10/2025 19:06, Joel Fernandes wrote:
> Demonstrates usage of the DRM buddy allocator bindings through
> a simple test module that initializes the allocator, performs
> allocations, and prints information about the allocated blocks.
>
> Signed-off-by: Joel Fernandes <joelagnelf@nvidia.com>
> ---
> samples/rust/Kconfig | 14 +++++
> samples/rust/Makefile | 1 +
> samples/rust/rust_drm_buddy.rs | 106 +++++++++++++++++++++++++++++++++
> 3 files changed, 121 insertions(+)
> create mode 100644 samples/rust/rust_drm_buddy.rs
>
> diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
> index b45631e2593c..8ccb4064ba91 100644
> --- a/samples/rust/Kconfig
> +++ b/samples/rust/Kconfig
> @@ -21,6 +21,20 @@ config SAMPLE_RUST_CLIST
>
> If unsure, say N.
>
> +config SAMPLE_RUST_DRM_BUDDY
> + tristate "DRM buddy allocator sample"
> + depends on DRM_BUDDY
> + help
> + This option builds the Rust DRM buddy allocator sample.
> +
> + The sample demonstrates using the DRM buddy allocator bindings
> + to allocate and free memory blocks.
> +
> + To compile this as a module, choose M here:
> + the module will be called rust_drm_buddy.
> +
> + If unsure, say N.
> +
> config SAMPLE_RUST_CONFIGFS
> tristate "Configfs sample"
> depends on CONFIGFS_FS
> diff --git a/samples/rust/Makefile b/samples/rust/Makefile
> index f8899c0df762..a56204ee4e96 100644
> --- a/samples/rust/Makefile
> +++ b/samples/rust/Makefile
> @@ -2,6 +2,7 @@
> ccflags-y += -I$(src) # needed for trace events
>
> obj-$(CONFIG_SAMPLE_RUST_CLIST) += rust_clist.o
> +obj-$(CONFIG_SAMPLE_RUST_DRM_BUDDY) += rust_drm_buddy.o
> obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
> obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) += rust_misc_device.o
> obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
> diff --git a/samples/rust/rust_drm_buddy.rs b/samples/rust/rust_drm_buddy.rs
> new file mode 100644
> index 000000000000..96907bc19243
> --- /dev/null
> +++ b/samples/rust/rust_drm_buddy.rs
> @@ -0,0 +1,106 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Rust DRM buddy allocator sample.
> +//!
> +//! This sample demonstrates using the DRM buddy allocator from Rust.
> +
> +use kernel::{
> + drm::buddy::{
> + BuddyFlags,
> + DrmBuddy, //
> + },
> + prelude::*,
> + sizes::*, //
> +};
> +
> +module! {
> + type: RustDrmBuddySample,
> + name: "rust_drm_buddy",
> + authors: ["Joel Fernandes"],
> + description: "DRM buddy allocator sample",
> + license: "GPL",
> +}
> +
> +struct RustDrmBuddySample;
> +
> +impl kernel::Module for RustDrmBuddySample {
> + fn init(_module: &'static ThisModule) -> Result<Self> {
> + // Create a buddy allocator managing 1GB with 4KB chunks.
> + let buddy = DrmBuddy::new(SZ_1G, SZ_4K)?;
> +
> + pr_info!("=== Test 1: Single 16MB block ===\n");
> + let allocated = buddy.alloc_blocks(
> + 0,
> + 0,
Does this map to the start/end? Surprised that this works with
RANGE_ALLOCATION below. I guess it works because of the end-1, but I'm
not sure if that was intended.
Anyway, probably you didn't really want RANGE_ALLOCATION here? That is
only if you want something at a specific offset or within a special bias
range. So here I think it will give you a massive bias range covering
everything due to end-1, but all you wanted was any available 16M block,
which is the typical flow? It still technically works, but looks a bit
non-standard and will internally take the bias range path, which is not
ideal :)
Also I guess worth updating the example in buddy.rs, which also does this?
> + SZ_16M,
> + SZ_4K,
> + BuddyFlags::RANGE_ALLOCATION,
> + GFP_KERNEL,
> + )?;
> +
> + let mut count = 0;
> + for block in &allocated {
> + pr_info!(
> + " Block {}: offset=0x{:x}, order={}, size={}\n",
> + count,
> + block.offset(),
> + block.order(),
> + block.size(&buddy)
> + );
> + count += 1;
> + }
> + pr_info!(" Total: {} blocks\n", count);
> + drop(allocated);
> +
> + pr_info!("=== Test 2: Three 4MB blocks ===\n");
> + let allocated = buddy.alloc_blocks(
> + 0,
> + 0,
> + SZ_4M * 3,
> + SZ_4K,
> + BuddyFlags::RANGE_ALLOCATION,
> + GFP_KERNEL,
> + )?;
> +
> + count = 0;
> + for block in &allocated {
> + pr_info!(
> + " Block {}: offset=0x{:x}, order={}, size={}\n",
> + count,
> + block.offset(),
> + block.order(),
> + block.size(&buddy)
> + );
> + count += 1;
> + }
> + pr_info!(" Total: {} blocks\n", count);
> + drop(allocated);
> +
> + pr_info!("=== Test 3: Two 8MB blocks ===\n");
> + let allocated = buddy.alloc_blocks(
> + 0,
> + 0,
> + SZ_8M * 2,
> + SZ_4K,
> + BuddyFlags::RANGE_ALLOCATION,
> + GFP_KERNEL,
> + )?;
> +
> + count = 0;
> + for block in &allocated {
> + pr_info!(
> + " Block {}: offset=0x{:x}, order={}, size={}\n",
> + count,
> + block.offset(),
> + block.order(),
> + block.size(&buddy)
> + );
> + count += 1;
> + }
> + pr_info!(" Total: {} blocks\n", count);
> +
> + pr_info!("=== All tests passed! ===\n");
> +
> + Ok(RustDrmBuddySample {})
> + }
> +}
On Thu Oct 30, 2025 at 8:06 PM CET, Joel Fernandes wrote: > Demonstrates usage of the DRM buddy allocator bindings through > a simple test module that initializes the allocator, performs > allocations, and prints information about the allocated blocks. I don't think this should be a sample module either, the code looks a bit like it tries to reinvents kunit tests.
© 2016 - 2026 Red Hat, Inc.