From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-vk1-f175.google.com (mail-vk1-f175.google.com [209.85.221.175]) (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 A8E4E56B64 for ; Wed, 3 Apr 2024 12:49:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712148565; cv=none; b=V9jnU9pRY3VEeuaOwUjB+6QXriVgVaq4JPvjWpWpIyKXJKQENPGv+tWORJmsIfj44OojOSyPDP5teoTqus++nVQQlgu6ppZFRu0ndYiW2jSVZx4mBRftz/85DzSUYjByWqq92rSK1B80w/zaedeGKEDOxfXQev0tAaE7B9/11j4= 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.175 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cL+MwWSw" Received: by mail-vk1-f175.google.com with SMTP id 71dfb90a1353d-4da702e48e0so1048600e0c.0 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=sReoTOgL2h90qyW3iqdxGcmTLT+0YM3+UkiTc6ZGC8kMXahpE7eenWHLIqeryfIVVX ItkA+linMzITFiTLfzHjsGH9+Qcx7JF9N1Z6cIFKGYrIJzDeHR80RWWDjStDD5vGV/SC dJgt1N/L3F5DfK1yzx0iwIkhbGCLFxzPPk91Rq1Q7vUiO12afQN08IC/DyaIzm1dBrnX yPARFwuCBqViqU+DwICN0SRX897li1uxl+guMuAtea1PprseBHLpWO7oiv0Mcu1AOSqF tMarsJ6BV0owWRNhiiK5zRs45YlKUxPIRlwJ03cVjEt9ys6QzCpG32bTgCM2MZCcCzES 7w0Q== X-Forwarded-Encrypted: i=1; AJvYcCWVKhQIhgUSpV8FIMVfFP5dqJOA18fO5fOZ+vpjVuOotUcwozPp2KrgLjrcJ8volpG98/yXqiKIwmvVoakIJQcLyDeeOHzzbjBm8bRJZO0= X-Gm-Message-State: AOJu0YxoDRuQwr3TD6RV6LkG3pPBJtkSb1U7bMEdj4sz2tYAa85cnFY4 GKNCeEX7iNJHwL/9ri+jxl7tU3YJNRbcYS2dQ9IIAmHd3V1VfQ0xxNZWYO1fc1/ERvFuEWnPjks iNcQ3wYyFVSJv4llQhs5gU1pPZyoVzleEtOsY X-Google-Smtp-Source: AGHT+IGpGV6qmNqrrvcbmVo3EjMPGgz6/E5lSHMdShUkmEzO4ELmFUZsaP3K2B+eJuIeKzDyPL73JKf/NC1FkGRyx1o= 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: rust-for-linux@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