Received: by 2002:ab2:7a55:0:b0:1f4:4a7d:290d with SMTP id u21csp90578lqp; Thu, 4 Apr 2024 07:52:14 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXsERKaDMGBud/GdHy01JWTz51iMv+FqA8jM1uGoqaX3i9AdXK/ofLa/fVSlWVe9QfT6XmdopRqJ5aM1UmkQvB+6JSq4dPQK7T3jIOS4Q== X-Google-Smtp-Source: AGHT+IEIJoKL7hNve0lDs7m1qKyPLYQdZgXyZ4McxBsIDuI9theEZfmyIngte0mi8lg61YBKZY5q X-Received: by 2002:a05:6a20:4309:b0:1a3:bdd2:a9a0 with SMTP id h9-20020a056a20430900b001a3bdd2a9a0mr4109056pzk.20.1712242333856; Thu, 04 Apr 2024 07:52:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712242333; cv=pass; d=google.com; s=arc-20160816; b=kwXkpnQ/2mfr4FgwsbDqVHVmbtZfd6D7k6l4yrTkEjLFIzY3c9CF9qRHENLGygcE5A jV7D/QkC5u0mnfKyWQb12AQR+TJjefX2bRHEe8XNYC/urk2hivrISc+Qrs3nsmEzqXuP IQ3xu1zo4xheuy4ZC/XldqnPL33fqhs1Wci+8JdWho+p5Q11pBwNqLMczfZAqG8LAdxC VLkuCFEi3Eo0dVbZJlv6dReZctmiPQTDSZl/BB4lTM7ZKEKqqeKGin0haN+m55cogI0H DmWOJYRcpvWvNa14yfQA6VtfBLRNzU5zz2P/5rIOaMO216MrIZftYq0y+8uBf77OhkUN 1NXw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=0Jzi1SJxdrZbAiksLrRC8eagzCOdwlYfNKWFhLLRZ30=; fh=NVLHYNxTFxZg90CuAUl5EFs9nACqSf1JvKaCbeMsn7U=; b=GKBNtJ6wBt1tNk9XtNw/MSRvY//LnJc6Y0STNIh8cl7f7lriWc5UuG2Km8qCB0yM+q UyO+D/kNmU0gBLfTI1Eof8uegR6op1ZPObZfzAFJl7eaO0gVl3Afwl1Hgory+XGu55/Z PVKN9u2nbQZhus026hMhKKseiOOGzoxJcsKHPCXsYSHe0UKvV5KjdbrdroH3etpYzJx0 PQ2eX0rSNES35Hq3rUV/vDKvC+cbIsLkQO7MOUtqnyI5mozc/SQ3Yr+jLI1kh5LE3Hl9 6DGA3Dy9P2rOM6wExA0wCFtegNULwuCo/tP8zxcvkXkfIZQOBC5dKdeJquWYCE/8/6se JV0Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=46r6zabnkjcrdj3jci6p6oyzwa.protonmail header.b="HiyOn/bU"; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-131651-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131651-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 1-20020a056a00072100b006ead466f3d9si15226984pfm.18.2024.04.04.07.52.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 07:52:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-131651-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=@proton.me header.s=46r6zabnkjcrdj3jci6p6oyzwa.protonmail header.b="HiyOn/bU"; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-131651-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131651-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me 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 76E0A290FD1 for ; Thu, 4 Apr 2024 14:51:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 04E1D1C698; Thu, 4 Apr 2024 14:51:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="HiyOn/bU" Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6FE7315EA6; Thu, 4 Apr 2024 14:51:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712242284; cv=none; b=L/JVzS+0Nf4fvW6oq9OqAh6n0RhufX59Y9tRAYzhsVD21BY1U4mG8jKqOJRCZE9Za6xiUKT3fQedEirHDlctR6YMs7/h6qpfHgT86MPMe1X0Dj3NLgoEqjgih91Z8kCEd0uYwUQi0H/ArM0e0uKare8EGLLCRwH2+uwzJiYMDvA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712242284; c=relaxed/simple; bh=9F4bE6RFEhkRy0ov/bMHMaKCv6iNYmCPVd5GJvpQkj0=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=YzMI+XpQriKwsPWiZp7EQu1D02WL1rsjozJVuLbPvNTIdgjkdbggN+liy/rFy5nuhYhCbqQP+v+zfP5J408X2tjeOCHoJr9wTmVCj7PAxKPF8RJrcaAqYsQ8sGCmCzuRvBleaErZriwg1XbaISzqFhSWys1u8rdVyazo+haPNkA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=HiyOn/bU; arc=none smtp.client-ip=185.70.43.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=46r6zabnkjcrdj3jci6p6oyzwa.protonmail; t=1712242280; x=1712501480; bh=0Jzi1SJxdrZbAiksLrRC8eagzCOdwlYfNKWFhLLRZ30=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=HiyOn/bUh/O0/cUk95114wulc/Dt1gYbWDYi+OG0mACzWIchMlHepzmIIHH/kFuWW cMz9PxPSkdOJv6xNO6KW2oSpJXUGUrPGAZMGfxihyjZherYPP+eF6xxPg1qTNb5Y6X 6X7KZ2uklWfx/tkvBlLcHk/GMwQ4SRJpPE3rEIzbjtRPp6TWTX/dUQMKIsOU2Mcd3g 7kpGojHWNE5mFVCeCO1mGEvfVbMx+vTpYSYjiMA+pqmIhrXMv2IFvwo0dc6u+forkG l6qaEFe2nfSL53SsxW8N4cjnRGv1HkveWidTMaZ+MzogxIkYLH3C7ZyWF7G2mnnq6n 65nstalLpIEGA== Date: Thu, 04 Apr 2024 14:51:16 +0000 To: Alice Ryhl From: 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 Subject: Re: [PATCH 5/9] rust: list: add List Message-ID: <3f3cf5ae-30df-4032-b752-77126035784c@proton.me> In-Reply-To: References: <20240402-linked-list-v1-0-b1c59ba7ae3b@google.com> <20240402-linked-list-v1-5-b1c59ba7ae3b@google.com> Feedback-ID: 71780778:user:proton Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 04.04.24 16:12, Alice Ryhl wrote: > On Thu, Apr 4, 2024 at 4:03=E2=80=AFPM Benno Lossin wrote: >> On 02.04.24 14:17, Alice Ryhl wrote: >>> + >>> + if self.first.is_null() { >>> + self.first =3D item; >>> + // SAFETY: The caller just gave us ownership of these fiel= ds. >>> + // INVARIANT: A linked list with one item should be cyclic= . >>> + unsafe { >>> + (*item).next =3D item; >>> + (*item).prev =3D item; >>> + } >>> + } else { >>> + let next =3D self.first; >>> + // SAFETY: We just checked that `next` is non-null. >> >> Missing mention of the type invariant. >=20 > SAFETY: By the type invariant, this pointer is valid or null. We just > checked that it's not null, so it must be valid. Sounds good. [...] >>> + /// Removes the first item from this list. >>> + pub fn pop_front(&mut self) -> Option> { >>> + if self.first.is_null() { >>> + return None; >>> + } >>> + >>> + // SAFETY: The first item of this list is in this list. >>> + Some(unsafe { self.remove_internal(self.first) }) >>> + } >>> + >>> + /// Removes the provided item from this list and returns it. >>> + /// >>> + /// This returns `None` if the item is not in the list. >> >> I think this should say "Returns `None` if the item is not in a list.". >> (Technically it should be "is not in a `List`", since it *can* be >> in another list with a different ID.) >=20 > I'm not really convinced. The phrases "the list" and "a list" are > equivalent given the safety requirement for this method, but "the > list" seems more natural to me. The `remove` method of any other > collection would say "the list" too. They are equivalent, but saying "the list" has the potential for this confusion: "If the function returns `None` if the item is not in the list, then why do I need to ensure that it is not in a different list?". >=20 >>> + /// >>> + /// # Safety >>> + /// >>> + /// The provided item must not be in a different linked list. >>> + pub unsafe fn remove(&mut self, item: &T) -> Option= > { >>> + let mut item =3D unsafe { ListLinks::fields(T::view_links(item= )) }; >>> + // SAFETY: The user provided a reference, and reference are ne= ver dangling. >>> + // >>> + // As for why this is not a data race, there are two cases: >>> + // >>> + // * If `item` is not in any list, then these fields are read= -only and null. >>> + // * If `item` is in this list, then we have exclusive access= to these fields since we >>> + // have a mutable reference to the list. >>> + // >>> + // In either case, there's no race. >>> + let ListLinksFields { next, prev } =3D unsafe { *item }; >>> + >>> + debug_assert_eq!(next.is_null(), prev.is_null()); >>> + if !next.is_null() { >>> + // This is really a no-op, but this ensures that `item` is= a raw pointer that was >>> + // obtained without going through a pointer->reference->po= inter conversion rountrip. >>> + // This ensures that the list is valid under the more rest= rictive strict provenance >>> + // ruleset. >>> + // >>> + // SAFETY: We just checked that `next` is not null, and it= 's not dangling by the >>> + // list invariants. >>> + unsafe { >>> + debug_assert_eq!(item, (*next).prev); >>> + item =3D (*next).prev; >>> + } >>> + >>> + // SAFETY: We just checked that `item` is in a list, so th= e caller guarantees that it >>> + // is in this list. The pointers are in the right order. >>> + Some(unsafe { self.remove_internal_inner(item, next, prev)= }) >>> + } else { >>> + None >>> + } >>> + } >>> + >>> + /// Removes the provided item from the list. >>> + /// >>> + /// # Safety >>> + /// >>> + /// The pointer must point at an item in this list. >>> + unsafe fn remove_internal(&mut self, item: *mut ListLinksFields) -= > ListArc { >>> + // SAFETY: The caller promises that this pointer is not dangli= ng, and there's no data race >>> + // since we have a mutable reference to the list containing `i= tem`. >>> + let ListLinksFields { next, prev } =3D unsafe { *item }; >>> + // SAFETY: The pointers are ok and in the right order. >>> + unsafe { self.remove_internal_inner(item, next, prev) } >>> + } >>> + >>> + /// Removes the provided item from the list. >>> + /// >>> + /// # Safety >>> + /// >>> + /// The `item` pointer must point at an item in this list, and we = must have `(*item).next =3D=3D >>> + /// next` and `(*item).prev =3D=3D prev`. >>> + unsafe fn remove_internal_inner( >>> + &mut self, >>> + item: *mut ListLinksFields, >>> + next: *mut ListLinksFields, >>> + prev: *mut ListLinksFields, >>> + ) -> ListArc { >>> + // SAFETY: We have exclusive access to items in the list, and = prev/next pointers are >> >> I think you mean that you have exclusive access to the prev/next fields >> of the `ListLinks` associated with `ID`... But that is rather long. >> Does anyone have any idea to shorten this? >=20 > SAFETY: We have exclusive access to the pointers of items in the list, > and the prev/next pointers are never null for items in a list. I would say that they are valid instead of never null, since you dereference them below. Otherwise sounds good. >=20 >>> + // never null for items in a list. >>> + // >>> + // INVARIANT: There are three cases: >>> + // * If the list has at least three items, then after removin= g the item, `prev` and `next` >>> + // will be next to each other. >>> + // * If the list has two items, then the remaining item will = point at itself. >>> + // * If the list has one item, then `next =3D=3D prev =3D=3D = item`, so these writes have no effect >>> + // due to the writes to `item` below. >> >> I think the writes do not have an effect. (no need to reference the >> writes to `item` below) >=20 > ? The first write is (*next).prev =3D prev; Using the fact that `next =3D=3D prev =3D=3D item` we have (*item).prev =3D prev; But that is already true, since the function requirement is that `(*item).prev =3D=3D prev`. So the write has no effect. The same should hold for `(*prev).next =3D next`. >=20 >>> + unsafe { >>> + (*next).prev =3D prev; >>> + (*prev).next =3D next; >>> + } >>> + // SAFETY: We have exclusive access to items in the list. >>> + // INVARIANT: The item is no longer in a list, so the pointers= should be null. >>> + unsafe { >>> + (*item).prev =3D ptr::null_mut(); >>> + (*item).next =3D ptr::null_mut(); >>> + } >>> + // INVARIANT: There are three cases: >>> + // * If `item` was not the first item, then `self.first` shou= ld remain unchanged. >>> + // * If `item` was the first item and there is another item, = then we just updated >>> + // `prev->next` to `next`, which is the new first item, and= setting `item->next` to null >>> + // did not modify `prev->next`. >>> + // * If `item` was the only item in the list, then `prev =3D= =3D item`, and we just set >>> + // `item->next` to null, so this correctly sets `first` to = null now that the list is >>> + // empty. >>> + if self.first =3D=3D item { >>> + // SAFETY: The `prev` field of an item in a list is never = dangling. >> >> I don't think this SAFETY comment makes sense. >> >>> + self.first =3D unsafe { (*prev).next }; >=20 > SAFETY: `prev` is the `prev` pointer from a `ListLinks` in a `List`, > so the pointer is valid. There's no race, since we have exclusive > access to the list. Sounds good. --=20 Cheers, Benno