Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp2680728lqz; Wed, 3 Apr 2024 05:49:35 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXzD0cvJkCDkRJybPvM5dClyq9r6i5OGHsHkGA7zZUuEAA0j8s6J5pgAyFXaXGkqrL70M1xRUtnt50eced1X9a+jp1tAXN8V8lUpb1heg== X-Google-Smtp-Source: AGHT+IHWMr7+oNUG7UXHnu18kF8vd9hGZL2mddYR/2wRn7373A05UyDn/s8GF+aBpb16WKxUg94o X-Received: by 2002:a05:622a:1889:b0:431:5130:7434 with SMTP id v9-20020a05622a188900b0043151307434mr3012753qtc.11.1712148575116; Wed, 03 Apr 2024 05:49:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712148575; cv=pass; d=google.com; s=arc-20160816; b=Fk65rgb5r3rkyEK7toKUwgRsC8SMsQoomxo7uU4MwF32LjEmWR0uAJb5RuntueFzR7 2+2KE/gB41Y6QPQBSvZfGqzIara9PXfnlN5R6vEMqkd5q0I0y4+K26Gy+b3DhCgKVv8+ VQHtnU46XmV2zgA49+Ftp+36gajXYNllAHj7E32ITM9qfwhKST+UESg03fyt16yEpImR ScQ00gdPIjBsrzW2woe57LiyqBHOq/zJ4V2rTmWL/2litc6dNaW6F3+b3e1BfhIpMzco 68S1kM9I3RjWh9FaGjOgDWjKnc5dcV4K2xVQf9BDAwIIKXqthInmpFGE/qoiDSdGR4Kt AxnA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=t2Ars11R43M3g3ak506J2E1E1SdVC3fdtd6pwR1tGRQ=; fh=I3zesgujwwRftTm48faPnJzBskB7aSOrlXfAI+Eo3Uc=; b=TflygENy/lBcRMECNfV8v80TqvkWoWQodVACpdkGmkV+e0ihQH7+TugK1Xksg6dfFX P+DrWebsJXxkmcz3AZP4OpgnROVAPMdyDQhoFeQ5ySx1RSwKqJapHxDiHxTFNF9tWNi3 DoS4UdKgE6YqqWvcqazymIaD4hO/QSc4xxU27BvbJnRWboEuGRyG25WaWwUIxZLcmWVI nFSONAJAS99v/zyy5QJoiucPieoCrG58fWv0SjMuhizxIcOMPDEzp8izThrz/fwtKWov N0mfGEjXQmhXx8PTZ7iIZPl+fT1QmZsc670uXB8bpnBbwqkWBirJUWcgILU7ISMf0E5g Sbtg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cL+MwWSw; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-129735-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-129735-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id z16-20020a05622a061000b00432be924686si12418934qta.173.2024.04.03.05.49.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 05:49:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-129735-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cL+MwWSw; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-129735-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-129735-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id C667A1C21C7D for ; Wed, 3 Apr 2024 12:49:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 026C456B64; Wed, 3 Apr 2024 12:49:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cL+MwWSw" Received: from mail-vk1-f179.google.com (mail-vk1-f179.google.com [209.85.221.179]) (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 9D9AF44C97 for ; Wed, 3 Apr 2024 12:49:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712148565; cv=none; b=GdGadXlO+IyeOXpK+ZGQ01iu+IxUBKTtmwna5CqacDXn5bbFwg7wMl7X/FnOjpa4POCPnLl3wOkiFXDKEcvyuHL7Fw5CIJOkZdseEj9uDRRn/N5G4mM1IB1gTKOD/uhHErBnM+HcOMaGqcKmiCeef24yZ+7Y4fqC5EM7bUn0+BE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712148565; c=relaxed/simple; bh=ROVG0xiHSnIs7KwRvfSwwYBgADFkDIzMA1GXsgZfjG8=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=gONXJKHeJKnWdIGqpnolKuuODe2lW7VCntJHUtNk/8VHBULHGcIpGIRVVs2QZx5J22yb3zJGjXxdLjj8qvykVSYo7q5ryAwVUy4pLHX29bHFFPfx+YH1NRxPY1WPoC3bbxNEGpUYv+nlzOYyeKkOlWurXdyf91wgZPdm8mMZW9Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cL+MwWSw; arc=none smtp.client-ip=209.85.221.179 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=google.com Received: by mail-vk1-f179.google.com with SMTP id 71dfb90a1353d-4d44f565284so1974251e0c.2 for ; Wed, 03 Apr 2024 05:49:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712148562; x=1712753362; darn=vger.kernel.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=t2Ars11R43M3g3ak506J2E1E1SdVC3fdtd6pwR1tGRQ=; b=cL+MwWSwzNVWBigFR/rg1aENaXWlU2/dlbDGdjYhVZW5Md+8KiJAU0rwpLnO/YWTal oYOb93BREIVbIeYolIynQu9QeeEG+6oVT0DQUVRCcka1a9lsKzGUJuAN7PUGA8bd4HEI EALQUfUI68jY14LaDwlkc4ZZtVE9ICyQSTTEbqVYXk1pp0PXK6ov9iceae/+t0z0mAU3 qOCqS+HHnY3nKoeeVE3WAzvbIKBO1LyDAeYlqCD7BWDWt6BMd4xGpWzy0MVR5eHVy2+q bYF3pM/9+3gWkVJpfZDwIjyq3TGaRD9Oi04GTE7F8iXFGptS7t6s3ltSyowuaBQ/WVbw +eYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712148562; x=1712753362; 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=t2Ars11R43M3g3ak506J2E1E1SdVC3fdtd6pwR1tGRQ=; b=t9TWZapKiqEjjRsxZJaO0y7fmpCORqbnB+yYW6bVxghBZnADfSwpjzvW2MexQijThd NGf36H8AJmxdIr5WG9JGQxUusDRwR4NQDuLc+/39GYztdd91rTa6zw4nCvCue7mhTWv4 2G8y6R0duceLObcBee2rnjFbZ16ZfybVcqD/TaMm/uXi21X5fHe7h9u/Z40dkbXD49Ml ASqwPEQQO0FopPcKdHqXGheAuFw/b4ibqq/1IbnzAKswDhr1DQliyYT/nIzH2ibzHja/ 71+XwyHfC4Kr4KWw63FhGU7gsLYg6Pa9xyb9doXEJod1euJejxddX6y78f+6hRDUPndP xqhQ== X-Forwarded-Encrypted: i=1; AJvYcCWQZ2+ckF5kxOg3SW8rD5Cgh1c/iqAGDq9tbMSmSCoAoRSdmLCqNUrni8GebwDpd5tScmPcFB7IUZtMa09cTzTbbohKc85i/220rD+F X-Gm-Message-State: AOJu0Yz2nYi7eUdlWn8F7e6trR0rJmTK18ZDN+ENGkRFt5pgI0lLiyl3 0SY2dXdzp0dEBgWq6FHuFUHFqIJF8Zkbdx7C22ovZpQHwEN7y6YiLVLQSVSOdfPapSxZsYEDgmJ lPEd82yxJrDqt/F5R0RcPLV2TZ61PwxDbD3X8 X-Received: by 2002:a1f:cdc2:0:b0:4d3:36b9:2c26 with SMTP id d185-20020a1fcdc2000000b004d336b92c26mr2131961vkg.14.1712148562485; Wed, 03 Apr 2024 05:49:22 -0700 (PDT) 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> <20240402-linked-list-v1-7-b1c59ba7ae3b@google.com> <4aaf7fd7-49ed-4b3e-b691-0d8d7d426d3a@proton.me> In-Reply-To: <4aaf7fd7-49ed-4b3e-b691-0d8d7d426d3a@proton.me> From: Alice Ryhl Date: Wed, 3 Apr 2024 14:49:11 +0200 Message-ID: Subject: Re: [PATCH 7/9] rust: list: add cursor To: Benno Lossin Cc: Miguel Ojeda , Andrew Morton , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Apr 3, 2024 at 2:19=E2=80=AFPM Benno Lossin wrote: > > On 02.04.24 14:17, Alice Ryhl wrote: > > diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs > > index 892705dd0571..47e52818c7bd 100644 > > --- a/rust/kernel/list.rs > > +++ b/rust/kernel/list.rs > > @@ -408,6 +408,20 @@ pub fn push_all_back(&mut self, other: &mut List) { > > other.first =3D ptr::null_mut(); > > } > > > > + /// Returns a cursor to the first element of the list. > > + /// > > + /// If the list is empty, this returns `None`. > > + pub fn cursor_front(&mut self) -> Option> { > > + if self.first.is_null() { > > + None > > + } else { > > + Some(Cursor { > > Missing INVARIANT comment. > > > + current: self.first, > > + list: self, > > + }) > > + } > > + } > > + > > /// Creates an iterator over the list. > > pub fn iter(&self) -> Iter<'_, T, ID> { > > // INVARIANT: If the list is empty, both pointers are null. Ot= herwise, both pointers point > > @@ -476,6 +490,69 @@ fn next(&mut self) -> Option> { > > } > > } > > > > +/// A cursor into a [`List`]. > > +/// > > +/// # Invariants > > +/// > > +/// The `current` pointer points a value in `list`. > > +pub struct Cursor<'a, T: ?Sized + ListItem, const ID: u64 =3D 0> { > > + current: *mut ListLinksFields, > > + list: &'a mut List, > > +} > > + > > +impl<'a, T: ?Sized + ListItem, const ID: u64> Cursor<'a, T, ID> { > > + /// Access the current element of this cursor. > > + pub fn current(&self) -> ArcBorrow<'_, T> { > > + // SAFETY: The `current` pointer points a value in the list. > > + let me =3D unsafe { T::view_value(ListLinks::from_fields(self.= current)) }; > > + // SAFETY: > > + // * All values in a list are stored in an `Arc`. > > + // * The value cannot be removed from the list for the duratio= n of the lifetime annotated > > + // on the returned `ArcBorrow`, because removing it from the= list would require mutable > > + // access to the cursor or the list. However, the `ArcBorrow= ` holds an immutable borrow > > + // on the cursor, which in turn holds an immutable borrow on= the list, so any such > > The cursor has a mutable borrow on the list. > > > > + // mutable access requires first releasing the immutable bor= row on the cursor. > > + // * Values in a list never have a `UniqueArc` reference. > > Is there some type invariant guaranteeing this? The List owns a ListArc reference to the value. It would be unsound for there to also be a UniqueArc reference to it. Alice