Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1033052rwd; Thu, 25 May 2023 07:16:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4jfrfP2ifYid8KPzHs0Ouh8uzKLUJH7M3iEJ+fFcRgt/tT/AUGbOK7rUkSPPtjeCAis7Rx X-Received: by 2002:a05:6a20:3d8a:b0:10b:e88f:597c with SMTP id s10-20020a056a203d8a00b0010be88f597cmr14577296pzi.44.1685024164751; Thu, 25 May 2023 07:16:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685024164; cv=none; d=google.com; s=arc-20160816; b=Z41Vu5qt9iG12AdudCmiISAzBsLGNIaRqWSRs8J8bYlQQRXO4Tfje6kdPN0OGWHp4o t91pkHNcMLgpOIIOZFJq4cPIVtj+ipj34cNhUXi84J8ThGKn/ytXQz7EyOXo7IzZgSIU ii9rdtFcmJE5FuTa7JKkaPLMSLFWuHA+9Z9HzKc9DL+0gq+62JxUeHHqf9/GnLtjZ+7t 66RibykG0sZOFnosj07+EUs4kzYL+u2Rnv6V4vjNrRTbuLUDkipCBN7vbxl00P/9wgOa s4ClLQpUhMrW/OoyUpMh2j/W1a3HB4KvYq4jsKZwul5sNnK/SZZTkDO74lGqLDD8xV2T Zorg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=n19llmgYWxTDw2crXfdj3ubR5MdoXByCzP69vRRATwg=; b=fLEep6EbL1yfXWB6zJtmVKDy9q8sqErCTvMlcgqAL2WewIFFtT9e54VI0YCJAr7bms T93l/xj7erWjBI81fEwXHQRliCfHQ5a9amvx6DmTHih9Q39kpMSoOBlEUkCQFLgC20nT 3LFXfCYI9e+sVhhsVjen9Tq7yQ0j6WQnlty8nmTspXfUJlsMYz7B/lWYoLcwfvTdek8O 6cIi7bhoxM+Lgo2KKYiuaV896OghdSiO1tlml1QVKWcrzGhefvrH2cd1h1DABpcowLbc zp/N8eb49AlRjvFtnDh6VqA0ysZBTHaYfZTTYTvemnCzVj1YCPUtqolhhA+9jI45n6Bb 64OA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=Jq+Wt+KD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k9-20020aa79989000000b0064d740b7459si1558103pfh.102.2023.05.25.07.15.47; Thu, 25 May 2023 07:16:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=Jq+Wt+KD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241373AbjEYNpe (ORCPT + 99 others); Thu, 25 May 2023 09:45:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241241AbjEYNpd (ORCPT ); Thu, 25 May 2023 09:45:33 -0400 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 389631A1 for ; Thu, 25 May 2023 06:45:25 -0700 (PDT) Date: Thu, 25 May 2023 13:45:12 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1685022324; x=1685281524; bh=n19llmgYWxTDw2crXfdj3ubR5MdoXByCzP69vRRATwg=; 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=Jq+Wt+KDlamfvOFx0M4kTWbRgwTJwMlfmU0ieBHde19C/yBBXEgRn83wLD+QqjF73 PoPKvkK1Ouxy8kh/aEURmvgieDfrk2PubgsJermdxj2OPazN0mcF26KrFCIzzatYmm /Q91RIKxKMTkupeM8UDnScLwProp8mDJ81Pipr9aNJx82OzNzxfpS/9Ij2nrcm898R zFtv6HRgQRx9pcIEm3ik97g5ahfXrRtLcwM2U05nn0h3kp9UkA2Wc4gjSl15oe/+ur zpNLpfcmCT1ho5Boa2JFEFxtTOKNRKIJaWHUatusbkXsrct499JHRsyWrDHVV3SgFb oO1AXFGV8hRWw== To: Martin Rodriguez Reboredo From: Benno Lossin Cc: Alice Ryhl , Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Ingo Molnar , Peter Zijlstra , Will Deacon , Mark Rutland , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Andreas Hindborg Subject: Re: [PATCH v2 3/4] rust: specify when `ARef` is thread safe Message-ID: In-Reply-To: <0b52529c-5570-bfe8-eb53-f51ae00c2e05@gmail.com> References: <20230523144418.1250547-1-aliceryhl@google.com> <20230523144418.1250547-4-aliceryhl@google.com> <0b52529c-5570-bfe8-eb53-f51ae00c2e05@gmail.com> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 5/23/23 16:44, Alice Ryhl wrote: > An `ARef` behaves just like the `Arc` when it comes to thread safety, so > we can reuse the thread safety comments from `Arc` here. >=20 > This is necessary because without this change, the Rust compiler will > assume that things are not thread safe even though they are. >=20 > Signed-off-by: Alice Ryhl > Reviewed-by: Andreas Hindborg > Reviewed-by: Boqun Feng Reviewed-by: Benno Lossin > --- > rust/kernel/types.rs | 13 +++++++++++++ > 1 file changed, 13 insertions(+) >=20 > diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs > index 29db59d6119a..1e5380b16ed5 100644 > --- a/rust/kernel/types.rs > +++ b/rust/kernel/types.rs > @@ -321,6 +321,19 @@ pub struct ARef { > _p: PhantomData, > } >=20 > +// SAFETY: It is safe to send `ARef` to another thread when the under= lying `T` is `Sync` because > +// it effectively means sharing `&T` (which is safe because `T` is `Sync= `); additionally, it needs > +// `T` to be `Send` because any thread that has an `ARef` may ultimat= ely access `T` using a > +// mutable reference, for example, when the reference count reaches zero= and `T` is dropped. > +unsafe impl Send for ARef {} > + > +// SAFETY: It is safe to send `&ARef` to another thread when the unde= rlying `T` is `Sync` > +// because it effectively means sharing `&T` (which is safe because `T` = is `Sync`); additionally, > +// it needs `T` to be `Send` because any thread that has a `&ARef` ma= y clone it and get an > +// `ARef` on that thread, so the thread may ultimately access `T` usi= ng a mutable reference, for > +// example, when the reference count reaches zero and `T` is dropped. > +unsafe impl Sync for ARef {} > + > impl ARef { > /// Creates a new instance of [`ARef`]. > /// > -- > 2.40.1.698.g37aff9b760-goog >=20 --=20 Cheers, Benno