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 E691DCA0EDC for ; Wed, 20 Aug 2025 10:16:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B4028E004E; Wed, 20 Aug 2025 06:16:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 78BD98E0003; Wed, 20 Aug 2025 06:16:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C86E8E004E; Wed, 20 Aug 2025 06:16:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5AE9E8E0003 for ; Wed, 20 Aug 2025 06:16:28 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D623C117A21 for ; Wed, 20 Aug 2025 10:16:27 +0000 (UTC) X-FDA: 83796731214.26.7A1A418 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf05.hostedemail.com (Postfix) with ESMTP id 4A876100006 for ; Wed, 20 Aug 2025 10:16:26 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=DhaTPXI8; spf=pass (imf05.hostedemail.com: domain of dakr@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1755684986; 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=B6XpIDmRuqCNJaOVpSIBkkEEDY3NPmRbhE/AxafAF5M=; b=6yOg1kr5rPHi2H06PQWHNww8yAuanhrTm4TY32KG/RqcBL4ZzW1SU23V9bwISzi8CaQO+2 ornet1bYz90gd321EgUqXEIaZnrvVkYjxw0pgnMxaa4ceWNjdmy1NqHpLQYHNVBQQV44vI kh+ExUa92LWKrrGcnh7VdBJLKuWM5Xw= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=DhaTPXI8; spf=pass (imf05.hostedemail.com: domain of dakr@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1755684986; a=rsa-sha256; cv=none; b=BgpAJoB2Ch+29yIPoXZwQFWarJRhr4t1ZCL/ub3Z3JohBBk0DxdWxajTV0xlbE1PG7VmXM A9Ycru4HrWTMabqBeLLYDy5/eqWfODC9dufcUUNDH1/wSKw6S0gYRp/aRD2KBspjo/2zPM 9IhFL9D7NJqj81XULQ1WIj1i3U1YY+8= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by tor.source.kernel.org (Postfix) with ESMTP id 7F7E961415; Wed, 20 Aug 2025 10:16:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 49561C4CEEB; Wed, 20 Aug 2025 10:16:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755684985; bh=XzpGJTqPVcOFUMqIMIlLu/4sIUSnKk+XD+8t8pnJ85U=; h=Date:Subject:Cc:To:From:References:In-Reply-To:From; b=DhaTPXI8AFgELTg78k1LUty/cXh66N97H/gR9L42Ak9QJ29mB3qnyWoZycO/twy4R NcLenIqirpvssRdFKaoZRuQCjcr9HyPd206BovO//gZhThDSPgmoXlNAWN1OMhGAfE utJK7wnAjwA3WPcx26TVP8ZcDJF3Nv4IBpfVeZ3j5mhOmBNAYkPD9wfSCs8R9VlHJJ h+Fyjdwo6VYSRZZ0hhUaZGjWBguhA3nQjdeWV3B3+6HCTv29vKpxRX2DrvR6gddoC5 nllKvmYOeH1bvHZTL6kR2yJd6TpEY0Sxdhiuhp30QsrzvonqtBruOrJyYvsjxE8c7J qrPR5q1lovOnw== Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Wed, 20 Aug 2025 12:16:19 +0200 Message-Id: Subject: Re: [PATCH] rust: zpool: add abstraction for zpool drivers Cc: , , "Uladzislau Rezki" , "Alice Ryhl" , "Vlastimil Babka" , "Lorenzo Stoakes" , "Liam R . Howlett" , "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , "Bjorn Roy Baron" , "Benno Lossin" , "Andreas Hindborg" , "Trevor Gross" , "Johannes Weiner" , "Yosry Ahmed" , "Nhat Pham" , To: "Vitaly Wool" From: "Danilo Krummrich" References: <20250820091543.4165305-1-vitaly.wool@konsulko.se> In-Reply-To: <20250820091543.4165305-1-vitaly.wool@konsulko.se> X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 4A876100006 X-Stat-Signature: a9ad3afoexenk991o1tknjouxgdkeyxw X-Rspam-User: X-HE-Tag: 1755684986-681911 X-HE-Meta: U2FsdGVkX1/WpyD4mGSo3eyp3HwldftpPnfFINw6f8JwkMYgrwmUmfRcsvmnF7OIzHS59mjESy7++jvgOJ6KjHNr3P6KltZj57a6bYJVavjQUBrJOXSLz92lCll2NVMniOzWrjQosli/kXTRcj34TpnspM6UqMQrcmNkgiPQOe2kR8OdfVdHX3V/b7jGZnC/Qb8OaYkg5BlSBJY3vkLbPa/YgKKqhMMzlfFTOfWewkRuf7ugkJjY8m22GOiZmdZ678DM3f0CS7iKe1Ha9RAPujmMkrZdN6n62WexJGr4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ity: 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 Wed Aug 20, 2025 at 11:15 AM CEST, Vitaly Wool wrote: > Zpool is a common frontend for memory storage pool implementations. > These pools are typically used to store compressed memory objects, > e. g. for Zswap, the lightweight compressed cache for swap pages. > > This patch provides the interface to use Zpool in Rust kernel code, > thus enabling Rust implementations of Zpool allocators for Zswap. Do you work on such a user? Do you have code using this API already? More specifically, do you plan to re-implement Zswap in Rust? > Signed-off-by: Vitaly Wool > Signed-off-by: Alice Ryhl If Alice contributed to the patch you may want to add a Co-developed-by: ta= g, otherwise this doesn't need her SoB. > +/// zpool API It would be nice to have some more documentation on this trait, including a doc-test illustrating some example usage. > +pub trait Zpool { > + /// Opaque Rust representation of `struct zpool`. > + type Pool: ForeignOwnable; Something that embedds a struct zpool, such as struct zswap_pool? If so, is= n't this type simply Self? > + > + /// Create a pool. > + fn create(name: *const c_uchar, gfp: Flags) -> Result; This shouldn't be a raw pointer, but rather &CStr. > + > + /// Destroy the pool. > + fn destroy(pool: Self::Pool); > + > + /// Allocate an object of size `size` using GFP flags `gfp` from the= pool `pool`, wuth the > + /// preferred NUMA node `nid`. If the allocation is successful, an o= paque handle is returned. > + fn malloc( > + pool: ::BorrowedMut<'_>, > + size: usize, > + gfp: Flags, > + nid: NumaNode, > + ) -> Result; > + > + /// Free a previously allocated from the `pool` object, represented = by `handle`. > + fn free(pool: ::Borrowed<'_>, handle: = usize); > + > + /// Make all the necessary preparations for the caller to be able to= read from the object > + /// represented by `handle` and return a valid pointer to the `handl= e` memory to be read. > + fn read_begin(pool: ::Borrowed<'_>, ha= ndle: usize) > + -> *mut c_void; Why does this return a raw pointer? I think this needs a proper type representation. > + > + /// Finish reading from a previously allocated `handle`. `handle_mem= ` must be the pointer > + /// previously returned by `read_begin`. > + fn read_end( > + pool: ::Borrowed<'_>, > + handle: usize, > + handle_mem: *mut c_void, > + ); Same here... > + > + /// Write to the object represented by a previously allocated `handl= e`. `handle_mem` points > + /// to the memory to copy data from, and `mem_len` defines the lengt= h of the data block to > + /// be copied. > + fn write( > + pool: ::Borrowed<'_>, > + handle: usize, > + handle_mem: *mut c_void, ...and here. > + mem_len: usize, > + ); > + > + /// Get the number of pages used by the `pool`. > + fn total_pages(pool: ::Borrowed<'_>) -= > u64; > +} > + > +/// Zpool driver registration trait. > +pub trait Registration { I think you should use the kernel::driver::Registration instead, it's specifically for the purpose you defined this trait and ZpoolDriver for. As for the C callbacks, they should go into the Adapter type (which impleme= nts kernel::driver::RegistrationOps) directly, they don't need to be in a trait= . This way a new Zpool Registration is created with: driver::Registration::new() This also allows you to take advantage of the module_driver!() macro to pro= vide your own module_zpool_driver!() macro. > + /// Register a zpool driver. > + fn register(&self, name: &'static CStr, module: &'static ThisModule)= -> Result; > + > + /// Pool creation callback. > + extern "C" fn _create(name: *const c_uchar, gfp: u32) -> *mut c_void= ; > + > + /// Pool destruction callback. > + /// > + /// # Safety > + /// > + /// The caller must ensure that `pool` is a valid pointer to `struct= zpool`. > + unsafe extern "C" fn _destroy(pool: *mut c_void); > + > + /// Callback for object allocation in the pool. > + /// > + /// # Safety > + /// > + /// The caller must ensure that `pool` is a valid pointer to `struct= zpool` and that `handle` > + /// is a valid pointer to usize. > + unsafe extern "C" fn _malloc( > + pool: *mut c_void, > + size: usize, > + gfp: u32, > + handle: *mut usize, > + nid: c_int, > + ) -> c_int; > + > + /// Callback for object release. > + /// > + /// # Safety > + /// > + /// The caller must ensure that `pool` is a valid pointer to `struct= zpool`. > + unsafe extern "C" fn _free(pool: *mut c_void, handle: usize); > + > + /// Callback to prepare the object for reading. > + /// > + /// # Safety > + /// > + /// The caller must ensure that `pool` is a valid pointer to `struct= zpool`. > + unsafe extern "C" fn _obj_read_begin( > + pool: *mut c_void, > + handle: usize, > + local_copy: *mut c_void, > + ) -> *mut c_void; > + > + /// Callback to signal the end of reading from an object. > + /// > + /// # Safety > + /// > + /// The caller must ensure that `pool` is a valid pointer to `struct= zpool`. > + unsafe extern "C" fn _obj_read_end(pool: *mut c_void, handle: usize,= handle_mem: *mut c_void); > + > + /// Callback for writing to an object. > + /// > + /// # Safety > + /// > + /// The caller must ensure that `pool` is a valid pointer to `struct= zpool`. > + unsafe extern "C" fn _obj_write( > + pool: *mut c_void, > + handle: usize, > + handle_mem: *mut c_void, > + mem_len: usize, > + ); > + > + /// Callback to return the number of pages in the pool. > + /// > + /// # Safety > + /// > + /// The caller must ensure that `pool` is a valid pointer to `struct= zpool`. > + unsafe extern "C" fn _total_pages(pool: *mut c_void) -> u64; > +} > + > +/// Zpool driver structure. > +pub struct ZpoolDriver { > + inner: Opaque, I think this needs pin-init, another reason to use kernel::driver::Registra= tion instead. :) > + > + /// Zpool callback functions that a zpool driver must provide > + pub callbacks: T, > +} > + > +impl Clone for ZpoolDriver { > + fn clone(&self) -> Self { > + todo!() > + } > +} Cloning the driver structure? Why? Please also consider that struct zpool_d= river needs to be pinned.