From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-183.mta0.migadu.com (out-183.mta0.migadu.com [91.218.175.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 63B3F28BAB6 for ; Thu, 17 Jul 2025 10:03:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.183 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752746595; cv=none; b=CTXfC7+p7nADrYKRc73jzHQiJaj7alpLleNOsBq4idJWrSQlodUWJLU02w74Dh8/94FtfBjGUkYqhKcGCava4cvoE1PUSXNbkPAhjwcZOyQDVfX63zs1+h09x9/JI/rp5cR/2BL4R720lLUukebdtjfTuLaDyIKy48eCTgxJ6Ho= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752746595; c=relaxed/simple; bh=3Gb0H09+G7CTyHsB5SCMtnr/71Dw5X1R99dtGXyVEfU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=gishOcPnhr2QpREQ/QSpk9C41AsCO9HpNpXLTsi9pg41n6N9qzp1DBSuZZw4+4IgLFmLhLMq6ftZHCQ3cAwm+6CP6cCVPoZu1WL1SAEFwX8//DxI4ARC2jgAKB5oi5NpVIYCKlnhpgHl36YhQ9No17u/uq9tOvlCCXHB0qHxj/4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=t7wqn1Nd; arc=none smtp.client-ip=91.218.175.183 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="t7wqn1Nd" Date: Thu, 17 Jul 2025 18:02:47 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1752746581; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=vG/QNYhSzyXjAfEHBH+AHpgKWHamu3HkNiSa4nselYQ=; b=t7wqn1NdjP5pTYq7HdNHAN3spwlV2YrAe+YtEyw8isTBU13RAHeX5FQK7GkfUeJkwIT00J zNq37fDzkdJfV9X+c8Jpi1F44Mk48g8uP4zLG4Sv4qdSt3ZOzRKzQCmZFP/WguvMdBBwze fK4HwoB6oa2lvQYvxIChxjvytnQpkZQ= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Your Name To: Danilo Krummrich Cc: Andrew Morton , Uladzislau Rezki , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , bjorn3_gh@protonmail.com, Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Geliang Tang , Hui Zhu , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org Subject: Re: [PATCH 3/3] rust: add a sample allocator usage Message-ID: References: Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Migadu-Flow: FLOW_OUT Hi Danilo, Thanks for your help. On Tue, Jul 15, 2025 at 12:37:52PM +0200, Danilo Krummrich wrote: > On Tue Jul 15, 2025 at 11:59 AM CEST, Hui Zhu wrote: > > +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)?; > > Ok, I think I get the idea, you want to demonstrate how to use the Allocator > trait for raw memory allocations. > > However, doing so is discouraged unless there's really no other way. One obvious > example are Rust's own memory allocation primitives, such as Box and Vec. > > So, instead of this raw allocation, you can just use VBox::new() or > VBox::new_uninit() in the following way. > > [repr(align(ALIGN))] > struct Blob([u8; SIZE]); > > // Creates a vmalloc allocation of size `SIZE` with an alignment of > // `ALIGN`. The allocation is freed once `b` is dropped. > let b = VBox::::new_uninit(GFP_KERNEL)?; > > This way you don't have to handle the layout and the Allocator type yourself and > you also don't have to care about explicitly calling vfree(), VBox does all this > for you. > > > + > > + 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 }) > > + } > > +} > I sent version v2. It only included the sample code and updated to use VBox according to your comments. Best, Hui