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 0F9F6C3DA64 for ; Tue, 6 Aug 2024 15:00:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B9DA6B0089; Tue, 6 Aug 2024 11:00:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 86A416B008A; Tue, 6 Aug 2024 11:00:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 731C86B008C; Tue, 6 Aug 2024 11:00:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 555A46B0089 for ; Tue, 6 Aug 2024 11:00:18 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id EDBFA1202C2 for ; Tue, 6 Aug 2024 15:00:17 +0000 (UTC) X-FDA: 82422131274.21.CBE0FED Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) by imf05.hostedemail.com (Postfix) with ESMTP id F177A10003F for ; Tue, 6 Aug 2024 15:00:14 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=t1mgvLNI; spf=pass (imf05.hostedemail.com: domain of aliceryhl@google.com designates 209.85.221.46 as permitted sender) smtp.mailfrom=aliceryhl@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722956352; 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=Z3lPYNCqUQ0CBq9NAtWdv9x9dDXsAeH8iPTFshxwRXw=; b=lViQLNJYqi1xmygPMZiUF9Y44D0Du8FhN1OW4IQ2KNcjFyVAn4PbjmdnuSOKKuo9Rop+9w 7wNIrErEU66wKGuyXAdoQ1XAzbf/ITY2PmivEEZSuZ0oH4YvcZJWlBZtXwQ6FrdNT08r5p 9NW5TMX/xsHsdHpCLCZUIceUcxPXUd8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722956352; a=rsa-sha256; cv=none; b=Nhgt0qOPGJEAgtw4gOVmhkCSGPR0OJPRT2co/si0xAiILkqim2Ul4z7STdwT9YZ/N3nR12 la9eVgOmKv5192cZRp+VMAq7q9Zf2BvRGH1weLxrNs6qkP5l3TFEFVAY2zWqjBweh3o7Ql aKCjbbeSgCrHiyZz7iYLGPAqEl/q+ak= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=t1mgvLNI; spf=pass (imf05.hostedemail.com: domain of aliceryhl@google.com designates 209.85.221.46 as permitted sender) smtp.mailfrom=aliceryhl@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-3685a5e7d3cso471634f8f.1 for ; Tue, 06 Aug 2024 08:00:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722956413; x=1723561213; 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=Z3lPYNCqUQ0CBq9NAtWdv9x9dDXsAeH8iPTFshxwRXw=; b=t1mgvLNIcevfh9kd9Oo5cyZnIgYdEo/Ir3waRzIQywb5KAfWt98Riy1ZqnIedtz8vW j9sLRAKuiS8YAWCBeqWQOo9AnwuAMtPWTiboYboZbxlyqP1RHu3ObC10sMBf9xzug7be krEfplmjViSOX8PGnQ0F1/Q5nH4rAlzL6MRa1Cp+4w1QVMzRgDpXJtJCufgzp1hReo1a Qxh4XZqwspbnuDn41TMeEiE1LIayPrpUqMkXCbiH+iIHNW61BuUx+rRghn+AZDfbdWmE cOpgBT0zZlgxISoz3S4dOSc7FSRRNCQ8cmiQsp727rYfDAZdBUuFQY+hEu93PbCgN1OH 4U8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722956413; x=1723561213; 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=Z3lPYNCqUQ0CBq9NAtWdv9x9dDXsAeH8iPTFshxwRXw=; b=Er2jjAMHgfjIEkEqRNgnS0P2SvxpqckslVU5fh1Bk0KXmHw2N7aUc/4n69VDwfIs5r yCZPAIj56MjM8WYebYO22wUYYVUHV/32jwLyJkuxDjh9VEYse4q8XCY3qjEFSo03wF4+ 2xEEiZsvNrdCnqPhLLuwUV2LwGEZuHD7uslIzeLFYtoV1y2d3PFvIqLlV87Qx2zFpE2m mN3hU/bBph4CXwG4T8a9EHeY3hKYvJEWwlWKnPTFVk9DsLxaNO93RMh6XcXdrFeMioHJ 16gl2EGZLtRel93W/EOxv7JYSllW11qhgyz+G26cCtiZ5hQlAQ9N5LodbOoyZjDEyto1 hd0w== X-Forwarded-Encrypted: i=1; AJvYcCWFic/oHxj2y3JHHsTudgzV66LOhkyB+mwr/O7rwGD0H1RXy8V87wlIcSeSxAMw6xbE+K3FNGDCb+rA/aBpdvPdSN8= X-Gm-Message-State: AOJu0YzCTjF0Zr1rVlskl6+pRUQy7Mgrq2bf4rdHzLf+Srj3q84qcvbK vG6sqEMz8kztOBQYy89D8XMtm04wW/Z0BVUKLKiy9LzLWZNYdgDJAT4Hvn0/1tymL94dBuK+HF1 FAsHPg2C+x59p2nacgR//fc4tE3+bICBxpMg4 X-Google-Smtp-Source: AGHT+IE/CVhS1wESVTuZ5jEcviM63X/Js9/n1/UOV4np0a6p1yHbV1w2VwEd+yTFq+QbD1M6s8WKCwNsuvaj3ZgfgSY= X-Received: by 2002:a5d:5749:0:b0:367:8fee:443b with SMTP id ffacd0b85a97d-36bbc1911a4mr11189866f8f.41.1722956412869; Tue, 06 Aug 2024 08:00:12 -0700 (PDT) MIME-Version: 1.0 References: <20240805152004.5039-1-dakr@kernel.org> <20240805152004.5039-2-dakr@kernel.org> In-Reply-To: <20240805152004.5039-2-dakr@kernel.org> From: Alice Ryhl Date: Tue, 6 Aug 2024 17:00:00 +0200 Message-ID: Subject: Re: [PATCH v4 01/28] 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-Stat-Signature: 9rrpte17neqczgg4sgjzo4jrgoyc67x7 X-Rspamd-Queue-Id: F177A10003F X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1722956414-823030 X-HE-Meta: U2FsdGVkX19j6pl1SXrFN6v0rGF44DrgFkE0+UVtYS99eeb5b9jydF9k1hPhk6wWVAS03NkF5vrPs7JrVzswlgw6RUizh3afeZTmvLbBBERauDtxx19p+aX3/TUcOkrqhDv04gKDUEl969gjs39W56cqLrvbahqNzuyMXUgrJ5O9IWHqvRFB1hpImCPGHkMbOo9J9qjv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YjMGSxmHHHwPOjhCcUJbUNEyWsmKrvAsTgFX5bGF5j4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.009267, 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 Mon, Aug 5, 2024 at 5:20=E2=80=AFPM 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 > --- > rust/kernel/alloc.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 79 insertions(+) > > diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs > index 1966bd407017..8a71a589469d 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,81 @@ 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. > +/// > +/// In order to be able to support `#[derive(SmartPointer)]` later on, w= e need to avoid a design > +/// that requires an `Allocator` to be instantiated, hence its functions= must not contain any kind > +/// of `self` parameter. > +/// > +/// # 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 of the same type. 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 > + /// > + /// `Some(ptr)` must point to an existing and valid memory allocatio= n created by this allocator > + /// instance. The alignment encoded in `layout` must be smaller than= or equal to the alignment > + /// requested in the previous `alloc` or `realloc` call of the same = allocation. > + /// > + /// Additionally, `ptr` is allowed to be `None`; in this case a new = memory allocation is > + /// created. > + /// > + unsafe fn realloc( > + ptr: Option>, > + layout: Layout, > + flags: Flags, > + ) -> Result, AllocError>; > + > + /// 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. It can't actually be NULL since it's a NonNull. I would reword to: // SAFETY: The caller guarantees that `ptr` points at a valid allocation created by this allocator. We are passing a Layout with the smallest possible alignment, so it is smaller than or equal to the alignment previously used with this allocation. Anyway, otherwise this LGTM. Reviewed-by: Alice Ryhl Alice