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 5F434C3DA7F for ; Sun, 4 Aug 2024 12:30:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CAB7C6B007B; Sun, 4 Aug 2024 08:30:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C34BF6B0082; Sun, 4 Aug 2024 08:30:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AAE266B0085; Sun, 4 Aug 2024 08:30:08 -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 8C4A86B007B for ; Sun, 4 Aug 2024 08:30:08 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3A43F121A0F for ; Sun, 4 Aug 2024 12:30:08 +0000 (UTC) X-FDA: 82414495296.22.ECAD5E7 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf17.hostedemail.com (Postfix) with ESMTP id E3DA44001D for ; Sun, 4 Aug 2024 12:30:05 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nhqWbwrC; spf=pass (imf17.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722774547; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ZjlVd21kb7hj6l+QbbBOMPaSi3cAYxWC4mYsZfbXj9I=; b=n/bKqOUOpFWT0RNfdhTKoDZModED1jxH+wc9gWqIuxHvs/95i90tycP3asHgPr10MR4iAY E5v+5Il1CfcUuU70cAQKscG9POdY9KIfFfLF4C6xXP9DON25NGH+Lvp8lwQ/seEUrIqrQF nsnyQ3iJKF9EvHvMzMoYSBR9VO2fwoI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722774547; a=rsa-sha256; cv=none; b=VmidSLhvSQMscuQMagtqtimT0f1CpO/oNP53GolH6NmCq+4SHUws6O3/ZM45BsfsRmlyWv Q9WCf146x570GPjkV+ol/EO2fQB/64a5EFPWC3FjgKRHss+ROi+R0NcNAIi3TQEsG4qZTS fmGr62HjCLGxQ+3NYFXg5+hug2fl+o0= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nhqWbwrC; spf=pass (imf17.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 4248DCE0948; Sun, 4 Aug 2024 12:30:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 791D7C32786; Sun, 4 Aug 2024 12:29:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722774601; bh=VnyEDyp+Og0b+BRBM/aqZK971zINnbDM9dhZN85qgRU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=nhqWbwrClYv7Eg7mLZgSuH/101q/4cByDn2FVtT9qBr1sbyUBqlGZPgPV1SVzA5i3 7gdXLeS9j+ReHj5PMpmOKvmmtpZ/UTv0xlbFMzn6buKY7C5uWdCSdRlPRm4jjVP6PD qAtYbzcz6TzTJ6odNnPP2uNsqLwX8v5mcqPUBgupd2AX0Ii77Z1lRmPMHv11j2T9HT p8dYLaa6EghMtLxaRsioC7vcof37hnsUcdbEYYYMxZ2Z43Vw2IadpaY5OhFI6OKqDL owPsNR7FjEsXUErQpRI0Cq5v819495VuM5PY765XLKBlKU8XZDImrjiMyf/sKN6bXi 2PfCRdiG7bpYA== Date: Sun, 4 Aug 2024 14:29:53 +0200 From: Danilo Krummrich To: Boqun Feng Cc: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.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 Subject: Re: [PATCH v3 01/25] rust: alloc: add `Allocator` trait Message-ID: References: <20240801000641.1882-1-dakr@kernel.org> <20240801000641.1882-2-dakr@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: E3DA44001D X-Stat-Signature: 1kpbzsphxtarbxphn75hjirh4n7uyg6i X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1722774605-947717 X-HE-Meta: U2FsdGVkX19u4PC3pOx/yJhUOzcOfSJ3EE96C0aLavaJbrdtnrdCxo8m8T+V7nP/lbXvC88xyuJhvOvuyxvvcTtOyWAmiH9Sg+KQM0V/ewwLAvBsRlLUXzrA1zi1Cn/+6+m1v4Hpk/nhvfbtP5kY+z9R+ack+84wwhqOamaDjJ1iRTWgJel8bHi0d/sGkkIT26tPefgSp/zuOfy7z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1 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: On Sat, Aug 03, 2024 at 11:21:21PM -0700, Boqun Feng wrote: > On Thu, Aug 01, 2024 at 02:02:00AM +0200, Danilo Krummrich wrote: > [...] > > +/// The kernel's [`Allocator`] trait. > > +/// > > +/// An implementation of [`Allocator`] can allocate, re-allocate and free 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 buffer 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. > > +/// > > Are you saying you could kmalloc() a memory buffer and pass it to a > vfree()? Or am I missing something here? I will extend it to: "valid to be passed to any other [`Allocator`] function" of the same type. > > Regards, > Boqun > > > +/// If `realloc` is called with: > > +/// - a size of zero, the given memory allocation, if any, must be freed > > +/// - 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 `layout`). > > + /// > > + /// This function is equivalent to `realloc` when called with a NULL pointer. > > + fn alloc(layout: Layout, flags: Flags) -> Result, AllocError> { > > + // 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 requested `layout`. If the > > + /// requested size is zero, `realloc` behaves equivalent to `free`. > > + /// > > + /// If the requested size is larger than the size of the existing allocation, a successful call > > + /// to `realloc` guarantees that the new or grown buffer has at least `Layout::size` bytes, but > > + /// may also be larger. > > + /// > > + /// If the requested size is smaller than the size of the existing allocation, `realloc` may or > > + /// may not shrink the buffer; this is implementation specific to the allocator. > > + /// > > + /// On allocation failure, the existing buffer, if any, remains valid. > > + /// > > + /// The buffer is represented as `NonNull<[u8]>`. > > + /// > > + /// # Safety > > + /// > > + /// `ptr` must point to an existing and valid memory allocation created by this allocator > > + /// instance. > > + /// > > + /// Additionally, `ptr` is allowed to be a NULL pointer; 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 created by this `Allocator` > > + /// instance. > > + unsafe fn free(ptr: NonNull) { > > + // SAFETY: `ptr` is guaranteed to be previously allocated with this `Allocator` or NULL. > > + // Calling `realloc` with a buffer size of zero, frees the buffer `ptr` points to. > > + let _ = unsafe { Self::realloc(Some(ptr), Layout::new::<()>(), Flags(0)) }; > > + } > > +} > > -- > > 2.45.2 > > >