From patchwork Tue May 23 14:44:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 98031 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6358:3046:b0:115:7a1d:dabb with SMTP id p6csp2200621rwl; Tue, 23 May 2023 08:14:08 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ44K8f/rt+3ZyaUees8wYXX2wU66AYsvIC/aU9hZ3PHlQ34RTsmAYSDgFgST0YMkP/hQzec X-Received: by 2002:a05:6a00:a91:b0:64a:fa71:a98f with SMTP id b17-20020a056a000a9100b0064afa71a98fmr18238794pfl.13.1684854847763; Tue, 23 May 2023 08:14:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684854847; cv=none; d=google.com; s=arc-20160816; b=ORWaLYRK6QG5PwSQ2uflRCzRuCLz1l+/QyIv5d5tH3UhsA+vv8WPe3Ge3sbq1fsXdX lrhymNq2vAInqDvmNXcvWDDQt2Z9fijEM5UeM3qB43U+DCX6Gp1cu/a3+wHBX20Tdnsk 3BrvZ4h7SGILOLc0l7uEq3Kls+/qonpSj9C6PXaU9ZGtvhqOjSY1agN3C4+BE/msuodf Pvldr68PVa+4Mn9ke6wAX49opz37JGdjI4Fe2Wqn0knfIHZscQCWMBAmKQYdNjKr0RA3 bwVfCVHmbor8Lf2aW16yPHoNxX4sRox4GRikcLFYbyV86frNUhLJNwunfM/Cs5I0/8LK m8Ng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=Cirt2ON6amstA/3YNx8Dl/DrSfD+lKUlEarHqMF6nxw=; b=OjApjwOUskONJza1b/rOWz3oUrC+uVHBV8R+k4BPsr2pKkGD5FfiNyg1WWj6R+GiRl Mtm0xp6ktvRiGm2yACExDaOEogg7VntjWWpRHE9CmRC6i+eckCetKFcuWkD7FzX1jrJ0 CD/rQyTsRlgh1Qcx6PiKr1lBJIM41cFi2dzIfGjnRkDwNm9ylUhHGoRKnWfH5Mv+Phf5 sh71wmTXGzngqJZO8C85RERo8Sp3IwGLrbw6EZ+jZZ7o2x4ZFcSEn67HpEdEp74PmLHx kiRUzBGNLTWm7u70kiqPjqk4hY8GUQSEi7Skkp454tkgPB7jQjzov+Az7CT+53SZsFT4 RnOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=DqYCWPhP; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j187-20020a638bc4000000b0053eef99f924si1498521pge.261.2023.05.23.08.13.47; Tue, 23 May 2023 08:14:07 -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=@google.com header.s=20221208 header.b=DqYCWPhP; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237359AbjEWOpw (ORCPT + 99 others); Tue, 23 May 2023 10:45:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234406AbjEWOpu (ORCPT ); Tue, 23 May 2023 10:45:50 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CC17132 for ; Tue, 23 May 2023 07:45:48 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-babb5e91ab4so6581201276.0 for ; Tue, 23 May 2023 07:45:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684853147; x=1687445147; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Cirt2ON6amstA/3YNx8Dl/DrSfD+lKUlEarHqMF6nxw=; b=DqYCWPhParszen3Hd8C6NALzsNq4RE0GYoUKErPQQT/Rk8O/vXKcFFl4bB4qWAWbtP kkux8OKOZOKVkJsvkJ2Ny+BMhW6JZ8Lp8rG5soKSuqkKXV72FR+NUz5qjTBaOP88KeGN H0AC4WDcLBg1UrXmePmLeZyRcirBCEwzjJ7wSBiRt+97j9WoXoaqf2oKm7wLH7DlYFfe 1Pv8kbdl6HkciPp6/Wmquuc5gUJx5SC+dKxW9tuwanpD0iX6llPgQm/VSavm+1y3b3PR h/Y217d0niQUL9S2/ueC8YV1UCjcINwLp02CAsb6llA5VM/mPUIIgFMI864kmpC1XiE7 d/Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684853147; x=1687445147; 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=Cirt2ON6amstA/3YNx8Dl/DrSfD+lKUlEarHqMF6nxw=; b=gT4VRumqx1sE+kOPxtag72k6C0sA7veD5KwigwuUfIqKuk5D67Uu5zLpH+zfKu8Lo2 2noa3t3koBZXxP7lMmr6095nDBvNLgpUSvB3NzoezcjtMcY0NEiFhukmvNIXRaQm6q+u 1yxqukaG+vguechAGSSLf9hcjYYIJPvyxwUmfUZx6BuRHG62Qvd2Ww+LMqQ1CqvPICth 0t/kragJAtGjPyMkPbC5gwLeMEwF4aciDnKjKKkGggW/sTnCdWNiDc3j0fpJugfRv79t uc9EbKXViCBrkz/dx+Q7GEDR0P40Y78yGOqbGbv6JD2mm/wM2A4/4ad1vSjRjV0SyWeF UuVw== X-Gm-Message-State: AC+VfDxBcU1+Ufe623f6HxrRss8Ws2w+ECHiZ91VOsRhyViKX4bYNAtx P4b9Gp7P5WqIYGThJzuBCovGLAz8bCjeLhE= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:6c8]) (user=aliceryhl job=sendgmr) by 2002:a25:949:0:b0:ba8:6422:bbec with SMTP id u9-20020a250949000000b00ba86422bbecmr8780701ybm.4.1684853147290; Tue, 23 May 2023 07:45:47 -0700 (PDT) Date: Tue, 23 May 2023 14:44:17 +0000 In-Reply-To: <20230523144418.1250547-1-aliceryhl@google.com> Mime-Version: 1.0 References: <20230523144418.1250547-1-aliceryhl@google.com> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog Message-ID: <20230523144418.1250547-4-aliceryhl@google.com> Subject: [PATCH v2 3/4] rust: specify when `ARef` is thread safe From: Alice Ryhl To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor Cc: Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Alice Ryhl , 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 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED, USER_IN_DEF_DKIM_WL autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766698356727999865?= X-GMAIL-MSGID: =?utf-8?q?1766698356727999865?= An `ARef` behaves just like the `Arc` when it comes to thread safety, so we can reuse the thread safety comments from `Arc` here. This is necessary because without this change, the Rust compiler will assume that things are not thread safe even though they are. Signed-off-by: Alice Ryhl Reviewed-by: Andreas Hindborg Reviewed-by: Boqun Feng Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Benno Lossin --- rust/kernel/types.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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, } +// SAFETY: It is safe to send `ARef` to another thread when the underlying `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 ultimately 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 underlying `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` may clone it and get an +// `ARef` on that thread, so the thread may ultimately access `T` using 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`]. ///