From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 04180C83F17 for ; Tue, 15 Jul 2025 10:00:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 968A48D0006; Tue, 15 Jul 2025 06:00:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F2C58D0001; Tue, 15 Jul 2025 06:00:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7941C8D0006; Tue, 15 Jul 2025 06:00:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 5E2058D0001 for ; Tue, 15 Jul 2025 06:00:31 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 13F1580375 for ; Tue, 15 Jul 2025 10:00:31 +0000 (UTC) X-FDA: 83666054262.28.595E546 Received: from out-186.mta1.migadu.com (out-186.mta1.migadu.com [95.215.58.186]) by imf04.hostedemail.com (Postfix) with ESMTP id 3A40D40019 for ; Tue, 15 Jul 2025 10:00:29 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=nSpT6uLY; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of hui.zhu@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=hui.zhu@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752573629; a=rsa-sha256; cv=none; b=aqJbVL+7bU+2B9EWpxj5Qsfw0JszxXZEyI8K0MfdHlcbhRE1XRTW0uNdgCklcfddKo1+Bj 1z9CFrXj7HsHS5Bua+3PomFY+jxYkORo/1xqT3+Rs2tnKql0A9ul7R5ooAUIj87i8ey/hQ JXiIzStCZHAebboU8XVHi1XUl4B+urg= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=nSpT6uLY; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of hui.zhu@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=hui.zhu@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1752573629; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=0hMXD4Bvb1YUfWztWBUy/WVwu1WJGbTx3ryS1MpxlDU=; b=tMU0hR6BfoEC9SQPKjMt1o3Z+Rm1EWNwsTNIzpwxxWXwz60n9o6/yAkvbispvuS11Ah4pE rKi6mX5+I2DuS9uw1byFLEZ4N1MM3dEFlwYfNC+bbOWBGoX8W8gek409vUZbqPxUvT7kb7 PlnqVS01jtM3/VTWDx3A4OJCLRnzuSA= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1752573626; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0hMXD4Bvb1YUfWztWBUy/WVwu1WJGbTx3ryS1MpxlDU=; b=nSpT6uLYE5l6za43VDR0XD6aFfHyA9chbD/+oRLXEvpAOhjkomitJTy5toxr/PXV1urzDF sRuFnKpIof8teSp/n5zjeybvYwzdwtuRcs6T5OEWkRXO0fSEBTwSwLhuOrq0i7f9Ms8I+Q kE7a/y0hwYE+jXc88x75fob7giS9KxQ= From: Hui Zhu To: Andrew Morton , Uladzislau Rezki , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , bjorn3_gh@protonmail.com, Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Geliang Tang , Hui Zhu , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org Subject: [PATCH 3/3] rust: add a sample allocator usage Date: Tue, 15 Jul 2025 17:59:48 +0800 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3A40D40019 X-Stat-Signature: dbrco7th7isfh85qgzhh8bzxnp4bg914 X-HE-Tag: 1752573629-333108 X-HE-Meta: U2FsdGVkX1/LeANw3SwiT/JKfX+0qHUwywIfRASBUGt0W/VJHkXTldGRQcv37yUOiWGZpozLMRAJgtH5VQGOvIxMQjD30V+FC/1PjTPyMDMdos/yg+AGtXK4DMfVZN7IA+J7+Li8Oqiz3R29doCuNzK6bOgezK3DP5huFv2HhFbGBUkRVjt3YeSisbXRa61b94oDghUuviNho9hpiEdCw0WuuUDHUqeAAfzGSuAb2M5tko6IylqHfgpToQvzIVBPgUtpaZ+9Inw+NVUGHfCznX2IMJnGmoVtZmwHo9iJcIWe6jSLlFii9Bv6qUQNEWccQpqriTJM7nPxcRynrYdWb64npKRbNIcblQGy/O1wlQlczo/aGptGnjGaJDvg4+0fT3Jndsv0NbeM6T8g1MoPLH/WumFU5cFUelDQ1vrNgPwM3OA1g+KxSnIsBeGkeD1W+bK/YHrykUDaL9oObCIfCg0otYTTh2FAlhMIwGr1QbUqQm7fIC5mn4WH/mUHdfrqe1seY1eqImaC+7CVz+6LERyA3F5nKfjT0gOq3i2rb/sb/Qaua8lI18VZpoPgPXVRWoA2HZzFlGqoNSvZT60SzYJd2CkAzlcTwWusH79pemycFRC6y+drdGnBHPr+++TMJ7F6X5SV1EiYkKft76xzMbUzDlGMrmuja25Iub+/2plDYd4zfbxoIXedx895Yj9s2CiqACCjXuUDDZw4MfJ5AGYdmUlleR0LlR2IWDTsCqj00yPeDyBHbNj3haLfvccvdPFi9Lpuwe1sBWKBxfVScXtUm2PeP5U5CzCKMW4fW7ZWGKD/DPL4OpdQeSxbdh33H4+H6MgSSdAg2Mq8qGBAvk2OjPXT99gKqoW7zo2vuaOdLJpz4gKdSd7BcgC7P8LtwgMxBZ0yP0+lvSSnxvB7zx1FdPWKHdvDWHHa8s3VMJHPocwWE/XrPXGMs69Pha3ulM2PkM5HdpaN63Wo4QF CKyyEFns xHCBcVWXlzTzVp3j22b6zPpYIeKedOXlUUlVNexUFPfe2ot+W2YLQs0rr3rQp0Gy4FgB0gcmHk2moMA5l0k7/lhjZ5RSIPis4kz4SlTfA8Snf3KEpzmOEH6uqhXbBJDWlZ8/A4tfEvC+eifdvDKhncJ+UeaaEJfV8tSCCyDOfZWHO2lfS07MCWG5X+oWm55nS3YkGJI42/wKkVGWIkwglGf3f18/mNkEUeXgSrZOU+LvDlB/deqgVHAURyq3OdkCcVl/h7gGqA71Ljdc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Hui Zhu Add a sample to the samples memory allocator usage. Co-developed-by: Geliang Tang Signed-off-by: Geliang Tang Signed-off-by: Hui Zhu --- samples/rust/Kconfig | 10 ++++ samples/rust/Makefile | 1 + samples/rust/rust_allocator.rs | 104 +++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 samples/rust/rust_allocator.rs diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig index 7f7371a004ee..79c73f6c5216 100644 --- a/samples/rust/Kconfig +++ b/samples/rust/Kconfig @@ -105,6 +105,16 @@ config SAMPLE_RUST_DRIVER_AUXILIARY If unsure, say N. +config SAMPLE_RUST_ALLOCATOR + tristate "Allocator Test Driver" + help + This option builds the Rust allocator Test driver sample. + + To compile this as a module, choose M here: + the module will be called rust_dma. + + If unsure, say N. + config SAMPLE_RUST_HOSTPROGS bool "Host programs" help diff --git a/samples/rust/Makefile b/samples/rust/Makefile index bd2faad63b4f..b378959eab19 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -10,6 +10,7 @@ obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_AUXILIARY) += rust_driver_auxiliary.o obj-$(CONFIG_SAMPLE_RUST_CONFIGFS) += rust_configfs.o +obj-$(CONFIG_SAMPLE_RUST_ALLOCATOR) += rust_allocator.o rust_print-y := rust_print_main.o rust_print_events.o diff --git a/samples/rust/rust_allocator.rs b/samples/rust/rust_allocator.rs new file mode 100644 index 000000000000..13d23cc9d682 --- /dev/null +++ b/samples/rust/rust_allocator.rs @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (c) 2025, Kylin Software + +//! Rust allocator sample. + +use core::{alloc::Layout, ptr::NonNull}; +use kernel::alloc::allocator; +use kernel::alloc::Allocator; +use kernel::bindings; +use kernel::prelude::*; + +module! { + type: RustAllocator, + name: "rust_allocator", + authors: ["Rust for Linux Contributors"], + description: "Rust allocator sample", + license: "GPL", +} + +const VMALLOC_ARG: [(usize, usize); 2] = [ + (bindings::PAGE_SIZE * 4, bindings::PAGE_SIZE * 2), + (1024, 128), +]; + +struct RustAllocator { + vmalloc_vec: KVec<(usize, Layout)>, +} + +fn vmalloc_align(size: usize, align: usize) -> Result<(NonNull<[u8]>, Layout)> { + let layout = Layout::from_size_align(size, align).map_err(|_| EINVAL)?; + + Ok(( + ::alloc(layout, GFP_KERNEL).map_err(|_| EINVAL)?, + layout, + )) +} + +fn vfree(addr: usize, layout: Layout) { + let vmalloc_ptr = NonNull::new(addr as *mut u8); + if let Some(ptr) = vmalloc_ptr { + unsafe { + ::free(ptr, layout); + } + } else { + pr_err!("Failed to vfree: pointer is null\n"); + } +} + +fn check_ptr(ptr: NonNull<[u8]>, size: usize, align: usize) -> (usize, bool) { + let current_size = unsafe { ptr.as_ref().len() }; + if current_size != size { + pr_err!( + "The length to be allocated is {}, and the actually allocated memory length is {}.\n", + size, + current_size + ); + return (0, false); + } + + let addr = ptr.cast::().as_ptr() as usize; + debug_assert!(align.is_power_of_two()); + if addr & (align - 1) != 0 { + pr_err!("Address {:#x} is not aligned with {:#x}.\n", addr, align); + return (0, false); + } + + (addr, true) +} + +fn clear_vmalloc_vec(v: &KVec<(usize, Layout)>) { + for (addr, layout) in v { + vfree(*addr, *layout); + } +} + +impl kernel::Module for RustAllocator { + fn init(_module: &'static ThisModule) -> Result { + pr_info!("Rust allocator sample (init)\n"); + + let mut vmalloc_vec = KVec::new(); + for (size, align) in VMALLOC_ARG { + let (ptr, layout) = vmalloc_align(size, align)?; + + let (addr, is_ok) = check_ptr(ptr, size, align); + if !is_ok { + clear_vmalloc_vec(&vmalloc_vec); + return Err(EINVAL); + } + + vmalloc_vec.push((addr, layout), GFP_KERNEL)?; + } + + Ok(RustAllocator { vmalloc_vec }) + } +} + +impl Drop for RustAllocator { + fn drop(&mut self) { + pr_info!("Rust allocator sample (exit)\n"); + + clear_vmalloc_vec(&self.vmalloc_vec); + } +} -- 2.43.0