Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp2023917lqz; Tue, 2 Apr 2024 05:18:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUXXPhkIMtFLLcFibdRpZA3b+Wvroo1GKFXKjOyMzWv1Vo98uG8YYfErlNYdxD2qFDZiNJ9TrNJhzw9hhWNBhvWNf4KxOcSpPj6CJN4EA== X-Google-Smtp-Source: AGHT+IEW9yi/dignmtwUYMhdHdu8EPjwA/S4ncO6cjWiKoDr0PPwlubG63FtiWHSha4Dyit7rGnF X-Received: by 2002:a05:6359:4c02:b0:17f:58cb:2d3a with SMTP id kj2-20020a0563594c0200b0017f58cb2d3amr14469005rwc.19.1712060312026; Tue, 02 Apr 2024 05:18:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712060311; cv=pass; d=google.com; s=arc-20160816; b=Om44RlLC5q3XHQRjFeNymF0oXiD8hN0M+21uRDirO1Oh81dat9ME3ybD2YmSJfTd/i lzviTeHkF5/SsVmyJVvpRCbU5DxygVc8QX7ZJ9tyMsKq21DDezEx1QhZXGZkNIrAGjk5 9ibqO5BYM8a6OFm8i/yeIXrnmWm4ToayWIub2Mgq9MrD103CMj0GWVHKq5jbk9zyLSCY Sr7Ksr0fcv/IagRyts3WzTUrlN9zz/qcofJLSMcUGqVeMocm6uUhuMwgegdvOg1298JU yLNC82kkJ2iypHXf93XS9CDWacX0ghfj6AOUPIhWOWvqFGTegVrtB7Kz1u315o6J/oRY X6xw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=Vhv4LxRJZ6OxQtv+kEEl5Wq7dqUJQo6ze35BbQzNlO4=; fh=v00tbabZ8+7qjI/pV3V7bzkW5x/y2r/jyMsEcwdctvQ=; b=Aflecn/19Amu1V6AzDqQwbL8sm8y96eyvn5l2gbc49tpOcJJfvoGwLtGz2PkRhWT1q G1joUsOEg9uR3VyhZLylUytvRtd/r6QSPW3JCwRsmvSTn3mv6uO07QBeriZoSSKKc6SP mA7w6657eXXO1FZfT5KvIQ5tOSPxAFW6VVpwbEOeE2iaWjRA5hPbnnr/b3y5wWdAaKBW 9qQ8ESRB1dP75zVW/ZuOSfhlEcIxMbJNucfV4jMiGoUElFfMGXWYPXV+y3wrOG1RG1vf 3I5dylLBOv72eiXNU0mZ+ylS9XMqM4uwZ0gEZctVU1pccw6WME58xy+9RJop987gBlaY uqfA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ydrkxjoa; arc=pass (i=1 spf=pass spfdomain=flex--aliceryhl.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-127917-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-127917-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id bv66-20020a632e45000000b005e83b7f54d1si11040136pgb.736.2024.04.02.05.18.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Apr 2024 05:18:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-127917-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ydrkxjoa; arc=pass (i=1 spf=pass spfdomain=flex--aliceryhl.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-127917-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-127917-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 89C5D28665D for ; Tue, 2 Apr 2024 12:18:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6F60782C67; Tue, 2 Apr 2024 12:17:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ydrkxjoa" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5BE27F7EA for ; Tue, 2 Apr 2024 12:17:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712060251; cv=none; b=jtrHOjMxVXfWtFjfTF5anGeFHrVNASvhowOKJ/Sn7ebTBEPl7brab5+uuVK7M4LNwi0y3sByHJL+KB+ph04dETKHAky+1M3EIHA38Z6QLTpQK9j68MIo2rNgMkOiTA3oSVQc83+Kf1PCL2sIh2W2AzN4RRNYYC7Iygu+GRgp6Ug= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712060251; c=relaxed/simple; bh=2PIcpJp9DT7VacmhqXAmL7iXpjNRwDR3ul+Ch/uIEhU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=E7SSUUDDV8YBSo0kGsFSHHEC5lwXiaGvnEVwqVl692sOvtEv22nQCuXqmqVVCuMNdSYrBoZthMGNxFzzIf/xHfA1/CUu0RNAgYeT8kmgegIiJcKocvlrI/+nGjOY6utWgBI32m8MrZHSHUMVUk/Qp1b5DxAPsXGDtiKChlm0XK4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ydrkxjoa; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc6b269686aso7108540276.1 for ; Tue, 02 Apr 2024 05:17:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712060248; x=1712665048; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Vhv4LxRJZ6OxQtv+kEEl5Wq7dqUJQo6ze35BbQzNlO4=; b=ydrkxjoaY/TlM8e9PL1P0qbWcShjQjKiWS+TL/D17jJRylybrXYKFHByLmyXm3an2M h5n6zthcuCVsjUFVq48N05h4JrmvJOsez9m3TVHS6aeoFKmohhHfADpU+Fg6T717+Jcr 2q0n7Yjy+xWRTvRZcK/M8UKgy/QD69cpxq3/o5ZtJpEnE23W0VFG146ZLvByqv2DOe9L pVitP2skwuYankQ7oPb8dlesEvR4cxWtXHJ+N9jW/wS6Q65eej1BdVACEq0+bMGAHzm5 f6GLSLp1jdVrD9toP6IW6B5ULNpKsGU1ip8Y+qPOLKghtTmGMSr5kONvVBGz9of8XmrG cLvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712060248; x=1712665048; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Vhv4LxRJZ6OxQtv+kEEl5Wq7dqUJQo6ze35BbQzNlO4=; b=EgGxbRVxL5CdU9z7/rCdVv/2fAA3BwyMMVwwznDP52yG8Z3Eo2TDplqVC9x0FHq4XR Sa8YP3baPs5FTrhycIvsP+IwkN4k9VrqIXmU4DTdqCzCI/de8w9GcyOoarhE3FQlieg7 E5LdeSjK5lu0j4qddJ4THX/ha8L6byVlgC2U7fASBsywe2dspBaSypY8mUmNSfQd0mat /Iky6QeFMRgwAWKZ9jUzUaoabsxs7E4bCMRqtv+FboJ4D8Ydf/Yg1A5C6qW8y1jxOwhO 1RhthTftazDQNlkqaM6GJik6L3n8HGzzj3XUREUoIhddUAtvwEbr8Db5NO1RcAWztb6G FEJA== X-Forwarded-Encrypted: i=1; AJvYcCXQWhYCo2tXjdLjQdiNUX7SMk/jSxfbKynPToWUHXotseTJuoVjjFX2/Xqm7yaW5XKM9mKaX8PN+XGzdTTl3q1TolCVMfikJB1u5l2u X-Gm-Message-State: AOJu0YyCaiMro+lvUDr9fVMjorTXkQ9+8NEKK5sjV8BRJ3SOL7SrfzQY MC0UhdKNN6aUKx0pQkb0v3H8gSYZhQ7QzYjE6dVeM1iaQvf2N3b0CjfMoEVfeJ5F1PCVVVbm5Z7 64qswu9BuwRaeBQ== X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6902:1883:b0:dce:30f5:6bc5 with SMTP id cj3-20020a056902188300b00dce30f56bc5mr954191ybb.4.1712060247742; Tue, 02 Apr 2024 05:17:27 -0700 (PDT) Date: Tue, 02 Apr 2024 12:17:00 +0000 In-Reply-To: <20240402-linked-list-v1-0-b1c59ba7ae3b@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240402-linked-list-v1-0-b1c59ba7ae3b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=5967; i=aliceryhl@google.com; h=from:subject:message-id; bh=2PIcpJp9DT7VacmhqXAmL7iXpjNRwDR3ul+Ch/uIEhU=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmC/dJexiium8UT/OewJm7DPd0VgrTKGLZevB6+ tW4etfZDoKJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZgv3SQAKCRAEWL7uWMY5 RpazD/4/uDlpm2FRIQJKp/aRWdG4z3cc4Dmds/xdrritb9hgsytKePUaWn43aBKudy+TqUgwywk GLl8g2S675hNtyAfEO8ppFQU3sRVRiGtSouPyeacJzW5hw63j+PLHznz6BP09t+Sl/k8eGpF/oT febc7azfxUrpOV44CT5JAxXeGhca7ebYsQpoO3YlidgHhB5BelV8EFX000wMym4AZKrxtIJUHT+ +WCxUjO9+HE4R1XVqlhZEI8TpTbTYNJ9TzNnzWxnvWgS5RXNMRisaSiC3QMh4jsHRb0nccO7sD3 G8eB7g4I5wf794iKi7K9GflmCAsBV9N/H7dNRydlvvLY6lRrK/epaVGxPcb77MXieVkxDUDVrao 7YyGR0HpsQjqjoapiXpNyAMUNr1jAjHthEmxtO0kWEAGBvps4P+mOvVpOoXNTHYqkG6kiM/kif9 CZ6Zu5ra6Mpvd/CGjYujV43z8srPvbwCFelbZ4tl7XH9FmByJtcvQqZxX+exfkroG51/Uzq+j4P Hu2mmdwBslps6ShnZEQO6gBwL74lBFkKaEcnglZqQk7WUoEOvGs4TjLU+oYgXyr949TPln6cFCP IYWl/aDxUxumQnPwbtiy8Y45H7NIBIQ/Y7F31UJum8wRdW58zIIDcEDNp10cDhhTk9ewm14oK73 qzMwlEqoZ7kITaw== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240402-linked-list-v1-3-b1c59ba7ae3b@google.com> Subject: [PATCH 3/9] rust: list: add struct with prev/next pointers From: Alice Ryhl To: Miguel Ojeda , Andrew Morton Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Marco Elver , Kees Cook , Coly Li , Paolo Abeni , Pierre Gondois , Ingo Molnar , Jakub Kicinski , Wei Yang , Matthew Wilcox , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Define the ListLinks struct, which wraps the prev/next pointers that will be used to insert values into a List in a future patch. Also define the ListItem trait, which is implemented by structs that have a ListLinks field. The ListItem trait provides four different methods that are all essentially container_of or the reverse of container_of. Two of them are used before inserting/after removing an item from the list, and the two others are used when looking at a value without changing whether it is in a list. This distinction is introduced because it is needed for the patch that adds support for heterogeneous lists, which are implemented by adding a third pointer field with a fat pointer to the full struct. When inserting into the heterogeneous list, the pointer-to-self is updated to have the right vtable, and the container_of operation is implemented by just returning that pointer instead of using the real container_of operation. Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index c5caa0f6105c..76597c49fa56 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -4,7 +4,122 @@ //! A linked list implementation. +use crate::init::PinInit; +use crate::types::Opaque; +use core::ptr; + mod arc; pub use self::arc::{ impl_list_arc_safe, AtomicListArcTracker, ListArc, ListArcSafe, TryNewListArc, }; + +/// Implemented by types where a [`ListArc`] can be inserted into a `List`. +/// +/// # Safety +/// +/// Implementers must ensure that they provide the guarantees documented on the three methods +/// below. +/// +/// [`ListArc`]: ListArc +pub unsafe trait ListItem: ListArcSafe { + /// Views the [`ListLinks`] for this value. + /// + /// # Guarantees + /// + /// * If there is a currently active call to `prepare_to_insert`, then this returns the same + /// pointer as the one returned by the currently active call to `prepare_to_insert`. + /// * If there is no currently active call to `prepare_to_insert`, then the returned pointer + /// points at a read-only [`ListLinks`] with two null pointers. + /// + /// # Safety + /// + /// The provided pointer must point at a valid value. (It need not be in an `Arc`.) + unsafe fn view_links(me: *const Self) -> *mut ListLinks; + + /// View the full value given its [`ListLinks`] field. + /// + /// Can only be used when the value is in a list. + /// + /// # Guarantees + /// + /// * Returns the same pointer as the one passed to the previous call to `prepare_to_insert`. + /// * The returned pointer is valid until the next call to `post_remove`. + /// + /// # Safety + /// + /// * The provided pointer must originate from the previous call to `prepare_to_insert`, or + /// from a call to `view_links` that happened after the previous call to `prepare_to_insert`. + /// * Since the previous call to `prepare_to_insert`, the `post_remove` method must not have + /// been called. + unsafe fn view_value(me: *mut ListLinks) -> *const Self; + + /// This is called when an item is inserted into a `List`. + /// + /// # Guarantees + /// + /// The caller is granted exclusive access to the returned [`ListLinks`] until `post_remove` is + /// called. + /// + /// # Safety + /// + /// * The provided pointer must point at a valid value in an [`Arc`]. + /// * Calls to `prepare_to_insert` and `post_remove` on the same value must alternate. + /// * The caller must own the [`ListArc`] for this value. + /// * The caller must not give up ownership of the [`ListArc`] unless `post_remove` has been + /// called after this call to `prepare_to_insert`. + /// + /// [`Arc`]: crate::sync::Arc + unsafe fn prepare_to_insert(me: *const Self) -> *mut ListLinks; + + /// This undoes a previous call to `prepare_to_insert`. + /// + /// # Guarantees + /// + /// The returned pointer is the pointer that was originally passed to `prepare_to_insert`. + /// + /// The caller is free to recreate the `ListArc` after this call. + /// + /// # Safety + /// + /// The provided pointer must be the pointer returned by the previous call to + /// `prepare_to_insert`. + unsafe fn post_remove(me: *mut ListLinks) -> *const Self; +} + +#[repr(C)] +#[derive(Copy, Clone)] +struct ListLinksFields { + next: *mut ListLinksFields, + prev: *mut ListLinksFields, +} + +/// The prev/next pointers for an item in a linked list. +/// +/// # Invariants +/// +/// The fields are null if and only if this item is not in a list. +#[repr(transparent)] +pub struct ListLinks { + #[allow(dead_code)] + inner: Opaque, +} + +// SAFETY: The next/prev fields of a ListLinks can be moved across thread boundaries. +unsafe impl Send for ListLinks {} +// SAFETY: The type is opaque so immutable references to a ListLinks are useless. Therefore, it's +// okay to have immutable access to a ListLinks from several threads at once. +unsafe impl Sync for ListLinks {} + +impl ListLinks { + /// Creates a new initializer for this type. + pub fn new() -> impl PinInit { + // INVARIANT: Pin-init initializers can't be used on an existing `Arc`, so this value will + // not be constructed in an `Arc` that already has a `ListArc`. + ListLinks { + inner: Opaque::new(ListLinksFields { + prev: ptr::null_mut(), + next: ptr::null_mut(), + }), + } + } +} -- 2.44.0.478.gd926399ef9-goog