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 257F2C3DA64 for ; Thu, 1 Aug 2024 08:19:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B08AF6B0095; Thu, 1 Aug 2024 04:19:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AB7E46B0096; Thu, 1 Aug 2024 04:19:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A7C26B0098; Thu, 1 Aug 2024 04:19:57 -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 7A0D96B0095 for ; Thu, 1 Aug 2024 04:19:57 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 277AEA656A for ; Thu, 1 Aug 2024 08:19:57 +0000 (UTC) X-FDA: 82402978434.12.1A0327F Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) by imf29.hostedemail.com (Postfix) with ESMTP id 4C097120006 for ; Thu, 1 Aug 2024 08:19:55 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=0A58BNrC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of aliceryhl@google.com designates 209.85.221.54 as permitted sender) smtp.mailfrom=aliceryhl@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722500332; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=z48QEimQdUdR9cUUTzAn0JtO3vTcfoiQ1GyipJs6wJY=; b=H3WzF8zzu1zOx8TetLS7EdzlxCFO0hGsV6UkDW8hGIbIYD5+VpggdVkFP8mA32kOBW8mNT Q4aeoA/WxtHWFFMaCjp/CcFjikRzv3aYaVbtQ6IGbIdm/QQ46W97klS0zAZ36CXSIO0NNa rdM7L1qPo4WWFNqa3njozhOyzvvWB94= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722500332; a=rsa-sha256; cv=none; b=OSZnakDeXvCn8fNEaWiL+fe071t3PgIPFHFhgWFKdktwnkvyt5EdVyRc7EXtNpyUICOsJo c7Tz9+ramzkTaW0j0WU3p2hpQt8T+KBUlwOsVlDz5AbTsvio9bZOOkMWIIEsoXb0Zd5La1 XriH3apyyFQMCj93cYH7RH7zzM+KvXI= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=0A58BNrC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of aliceryhl@google.com designates 209.85.221.54 as permitted sender) smtp.mailfrom=aliceryhl@google.com Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-36868fcb919so3292795f8f.2 for ; Thu, 01 Aug 2024 01:19:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722500394; x=1723105194; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=z48QEimQdUdR9cUUTzAn0JtO3vTcfoiQ1GyipJs6wJY=; b=0A58BNrCq1Id/ah+Iq8oECz+jGXWhIMqWC5AGeJrPiFXcmeF0+cREx8oEQ3WCAv+ji GcY7XVS1hUmHj0lVX+TShODKoe5g9tlsSM/VO+wOQ9ffgG0HD/ExPANbw5ySRp3vFv6n mHtjYhQtqWRSv6oyxP0U6kbQcbtyTeFVYgM+xpNlNjzUNVddFzUQRp38wjISUhd0cBp5 DRdcTY/gYKf0hEJc4Turp+Mw7czx39+gvy2nPTfzMIGDOcJ1ROQFtxeE1ZXw6beM8Dxn d5F1zUwuU2de1pyWUor9W9Ew1XivLucHPM+T9Gk7RZoTE1baJ7h3M5Bg8pjDnXkVD2Cs OOHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722500394; x=1723105194; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=z48QEimQdUdR9cUUTzAn0JtO3vTcfoiQ1GyipJs6wJY=; b=AVVNz9NctCXb/w9TYD24e+wXkgxRJFVF4Vf9cZKZKKQ4/Z00ssmj2Tg2lxDFKESt0U sCZSYoNgzHiUf0OL0V1S3AWk/dE8uYYiSItoHF6cH2tKHRQB7aHpfCY/arM4PWDSzvV1 raBxWXfp/JcrpakUkXf+Gkl/ll7AER142VBpc+bRxMTk24ZcGHcOZiITKexq2pBg7IuK Ku120jXbHjPf2lvyFsPEutBcEy5G4kCwHNoAzrcrQ3eJ2M0/L/Ypo+VBM/N/eyHQgEVi FTUoDun25Np9ahtVC5b/Vx1wZJd2VfD4k6NBHRlzDz5cZ6eVA1i/cv145uBeCsi231+2 s8Og== X-Forwarded-Encrypted: i=1; AJvYcCXMW5/gwOptdcQj1JysS3yJ1Vx5E9f0pTk59IM8EIGYQm3meBNBkLNmSIEs4AJo9S+Mmhw0IDljRjm7JRDLrNQcavo= X-Gm-Message-State: AOJu0YyyyNJTL/Z5f8jZBzXVBh/OOoyEGJOZ7K0CFGLQYVMHzy9mlPsC 9YsZgjgWc8haGtaJv6xyQOmsRgBR7EnAjfCtgeET5Z6gBo2e5wOlaGNhpmr1rUU+Png47Zki+yQ Q5gKG0JK72BnzbjxLFTXTyzz+1zJo5gFumkVZ X-Google-Smtp-Source: AGHT+IEMfwhKTMBK0cnCuAuh+rhXBm29w7tYDwKoyPqOZyNudYkoi3YBiIcjx3+7V/3Jvthfm7RnbXLZ9LOcULLTUV8= X-Received: by 2002:adf:e890:0:b0:366:ee9b:847 with SMTP id ffacd0b85a97d-36baacc81b9mr1395000f8f.14.1722500393355; Thu, 01 Aug 2024 01:19:53 -0700 (PDT) MIME-Version: 1.0 References: <20240801000641.1882-1-dakr@kernel.org> <20240801000641.1882-2-dakr@kernel.org> In-Reply-To: <20240801000641.1882-2-dakr@kernel.org> From: Alice Ryhl Date: Thu, 1 Aug 2024 10:19:41 +0200 Message-ID: Subject: Re: [PATCH v3 01/25] rust: alloc: add `Allocator` trait To: Danilo Krummrich Cc: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, akpm@linux-foundation.org, daniel.almeida@collabora.com, faith.ekstrand@collabora.com, boris.brezillon@collabora.com, lina@asahilina.net, mcanal@igalia.com, zhiw@nvidia.com, acurrid@nvidia.com, cjia@nvidia.com, jhubbard@nvidia.com, airlied@redhat.com, ajanulgu@redhat.com, lyude@redhat.com, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 4C097120006 X-Stat-Signature: 7bfn5338k94nzwznpircdown1tdh5e33 X-Rspam-User: X-HE-Tag: 1722500395-518002 X-HE-Meta: U2FsdGVkX1976jPfwlXyNpYZ64IjHRSNwonvvkWKjLAnZVTKfRE14Y9MXjNIKx0eG96trO0+Vf6RDh4SfCnTCrZBwJr+GEdWKmo/t/hG2txjV603vEznbaFWLgkjxGqjfANBSLC8IqaJ33ai4KJQdmbcVZW1mrHcH1YpKniFIApzfJHKAF69zD+VoBx4C0GkMcrKa82GSs99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lhmU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000030, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Thu, Aug 1, 2024 at 2:07=E2=80=AFAM Danilo Krummrich w= rote: > > Add a kernel specific `Allocator` trait, that in contrast to the one in > Rust's core library doesn't require unstable features and supports GFP > flags. > > Subsequent patches add the following trait implementors: `Kmalloc`, > `Vmalloc` and `KVmalloc`. > > Signed-off-by: Danilo Krummrich It could be nice to mention that the design without `self` parameters is necessary for compatibility with #[derive(SmartPointer)]. > rust/kernel/alloc.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 73 insertions(+) > > diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs > index 1966bd407017..b79dd2c49277 100644 > --- a/rust/kernel/alloc.rs > +++ b/rust/kernel/alloc.rs > @@ -11,6 +11,7 @@ > /// Indicates an allocation error. > #[derive(Copy, Clone, PartialEq, Eq, Debug)] > pub struct AllocError; > +use core::{alloc::Layout, ptr::NonNull}; > > /// Flags to be used when allocating memory. > /// > @@ -86,3 +87,75 @@ pub mod flags { > /// small allocations. > pub const GFP_NOWAIT: Flags =3D Flags(bindings::GFP_NOWAIT); > } > + > +/// The kernel's [`Allocator`] trait. > +/// > +/// An implementation of [`Allocator`] can allocate, re-allocate and fre= e memory buffer described > +/// via [`Layout`]. > +/// > +/// [`Allocator`] is designed to be implemented as a ZST; [`Allocator`] = functions do not operate on > +/// an object instance. > +/// > +/// # Safety > +/// > +/// Memory returned from an allocator must point to a valid memory buffe= r and remain valid until > +/// it is explicitly freed. > +/// > +/// Any pointer to a memory buffer which is currently allocated must be = valid to be passed to any > +/// other [`Allocator`] function. The same applies for a NULL pointer. > +/// > +/// If `realloc` is called with: > +/// - a size of zero, the given memory allocation, if any, must be fre= ed > +/// - a NULL pointer, a new memory allocation must be created > +pub unsafe trait Allocator { > + /// Allocate memory based on `layout` and `flags`. > + /// > + /// On success, returns a buffer represented as `NonNull<[u8]>` that= satisfies the layout > + /// constraints (i.e. minimum size and alignment as specified by `la= yout`). > + /// > + /// This function is equivalent to `realloc` when called with a NULL= pointer. > + fn alloc(layout: Layout, flags: Flags) -> Result, Allo= cError> { > + // SAFETY: Passing a NULL pointer to `realloc` is valid by it's = safety requirements and asks > + // for a new memory allocation. > + unsafe { Self::realloc(None, layout, flags) } > + } > + > + /// Re-allocate an existing memory allocation to satisfy the request= ed `layout`. If the > + /// requested size is zero, `realloc` behaves equivalent to `free`. > + /// > + /// If the requested size is larger than the size of the existing al= location, a successful call > + /// to `realloc` guarantees that the new or grown buffer has at leas= t `Layout::size` bytes, but > + /// may also be larger. > + /// > + /// If the requested size is smaller than the size of the existing a= llocation, `realloc` may or > + /// may not shrink the buffer; this is implementation specific to th= e allocator. > + /// > + /// On allocation failure, the existing buffer, if any, remains vali= d. > + /// > + /// The buffer is represented as `NonNull<[u8]>`. > + /// > + /// # Safety > + /// > + /// `ptr` must point to an existing and valid memory allocation crea= ted by this allocator > + /// instance. > + /// > + /// Additionally, `ptr` is allowed to be a NULL pointer; in this cas= e a new memory allocation is > + /// created. > + unsafe fn realloc( > + ptr: Option>, > + layout: Layout, > + flags: Flags, > + ) -> Result, AllocError>; Is it intentional that this allows you to change the alignment of an allocation? If so, that could use a note in the docs. > + /// Free an existing memory allocation. > + /// > + /// # Safety > + /// > + /// `ptr` must point to an existing and valid memory allocation crea= ted by this `Allocator` > + /// instance. > + unsafe fn free(ptr: NonNull) { > + // SAFETY: `ptr` is guaranteed to be previously allocated with t= his `Allocator` or NULL. > + // Calling `realloc` with a buffer size of zero, frees the buffe= r `ptr` points to. > + let _ =3D unsafe { Self::realloc(Some(ptr), Layout::new::<()>(),= Flags(0)) }; > + } At the very least, the provided implementation of `free` changes the alignment when it calls `realloc`. Alice