From patchwork Wed May 31 14:59:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 101517 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2962075vqr; Wed, 31 May 2023 08:24:06 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7NCIBRoK1v4E1KvMhI47UejlxQZeRT/cKCZrifqtq6FFIbFNUWwbmPcQI3nT6Bw447yykQ X-Received: by 2002:a17:902:e74f:b0:1b0:2658:db00 with SMTP id p15-20020a170902e74f00b001b02658db00mr5932574plf.31.1685546646522; Wed, 31 May 2023 08:24:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685546646; cv=none; d=google.com; s=arc-20160816; b=K/F+rXBaM+4/7VVtUgyARUd3vskcS1ABOWKD2kPckUeyvezFxuoDPMB6VEkZNJpCvk xs8ttZ/qKl/HfSzR/e9n9tqQEIqbeTlrbRIEjCdKCwoHJ9l0kcjfJBMTb9EX/nTSR7R0 kNXfivAZbg34O54Y01RiQifiFWHazk/XUcnbozkeLjOLI22LKc1xZ9uxOE6WOairsCiT DVL5wjlN6wz7L4kB+dJDfT8m9h0eVp/dqXF/YHqGmqb+sj37/kPCdgCR8zvE/UGI1dLD s5p/5PLI1/ZrRek6qgfoSsM+0P6TWvD15E9BRLy6dWS09cPD3wA/urgVI0Hdt+n9t5SC cyCg== 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=VjkkPXwWP2UuAC8i0OxxIXvE50xM+SJ4TuQh4LuQSuA=; b=lyNOOISC7HjHUrfdf2b7IcRteFCNEZgh0HE1IIuDvwjGNRw5ZOEiJ0C9XIHU9SCtk3 1YMP0uN2T3dFRUj/XshFaAsfngsJ7jcyPb2J2HLdbI6YbgzMYZ5r3Hg3QL7KIEeGFj6k xEt2liAQesLjin0dpKtgsC65Img0D4Krqq38FPU64ZMWOO2SEvBA+vKZRYJRLS2B6Jeb kintETrky1G7TzWDjSsf1u5fFCf5qMzO5wXVDsOWxVVHlPTn/OrARmcKLJ0kzfxijANy qm+S8ysC9bNjxtCwOb2ZdepYsCjyjZxxo8GKP5zW6zM0SASIOOeZ/hNK1HVY1cFs6Tul 72lg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=b9HP3fVC; 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 h10-20020a170902748a00b001b045d65af0si101002pll.76.2023.05.31.08.23.51; Wed, 31 May 2023 08:24:06 -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=b9HP3fVC; 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 S233752AbjEaPCo (ORCPT + 99 others); Wed, 31 May 2023 11:02:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233677AbjEaPCm (ORCPT ); Wed, 31 May 2023 11:02:42 -0400 Received: from mail-ej1-x649.google.com (mail-ej1-x649.google.com [IPv6:2a00:1450:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24CAF123 for ; Wed, 31 May 2023 08:02:31 -0700 (PDT) Received: by mail-ej1-x649.google.com with SMTP id a640c23a62f3a-96fd3757c1dso580154266b.1 for ; Wed, 31 May 2023 08:02:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685545349; x=1688137349; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=VjkkPXwWP2UuAC8i0OxxIXvE50xM+SJ4TuQh4LuQSuA=; b=b9HP3fVC1WMLEISsNDvrmAx+7T6OCx606wh51FEYzDPSVAbudgUPILu4wPuhUmSsAj 88aIsX+eUupN+Ztym2x/N0KNysr0nbAL9lK3BHICx7QsVQ/6Br2OqpqKa0O6/zXQteCG aJxkky9PCqkB6uHmZyY29AMStNAa62L34JltEfi5kSJHu7rGR3Gl5nV+Dr0RMmgPSEqV aR6fyBkkqAbBaWy1RiADSlAfOw7OPBdc5KiFCtrObXZcSTIfDsDM99ito93Tm+3YP0IJ En6qoIFZfB2w5hXi7viGmOgheLKG4sXaeBxtF9ByBdMfaAjej+owSATDIm16m5j03RfP 6zIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685545349; x=1688137349; 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=VjkkPXwWP2UuAC8i0OxxIXvE50xM+SJ4TuQh4LuQSuA=; b=E3MCESR7Ij0CuOuOyonSUjo4miBi5K21Ig5+hVmIaRO6dbxNXsrol4S3vOkkEN7H+3 3aiQxlfkjgOeB3NFz3T0dlSxpl3dMrAyvHaaZSSb930f8htxsznZhxvsrid6HSAXqZMb 7ST2pjvi6bGgJ4+hEfcCymbe/8mNE6lDmUudVyLIRX8Un1kJTMmBpHLmQzx6MElwmCCk y865GZYyKWjTT140e0BS4k+C3sDlWTvHG4Mx8959qir/01GDGz3sxE+J9Ikm1a2kXUMb Lx8NCVqFLnjF5RFiWvaXMyHi1mTgMSssUgcK0E4OT9NNOmNSZr4TmHlYha71QU3nZnS+ heMw== X-Gm-Message-State: AC+VfDyKpZbr77VbjuosSJxM6Lk7y1wmDD9lnk+yYLPsCwkV6dCx9d0j bufPLqbyrqs36lHjGs/Jqg+quKBlEqKmffw= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:6c8]) (user=aliceryhl job=sendgmr) by 2002:a17:906:8a7b:b0:962:334b:ee1a with SMTP id hy27-20020a1709068a7b00b00962334bee1amr1499837ejc.1.1685545349753; Wed, 31 May 2023 08:02:29 -0700 (PDT) Date: Wed, 31 May 2023 14:59:38 +0000 In-Reply-To: <20230531145939.3714886-1-aliceryhl@google.com> Mime-Version: 1.0 References: <20230531145939.3714886-1-aliceryhl@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Message-ID: <20230531145939.3714886-4-aliceryhl@google.com> Subject: [PATCH v3 3/4] rust: specify when `ARef` is thread safe From: Alice Ryhl To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Alice Ryhl , Ingo Molnar , Peter Zijlstra , Will Deacon , Mark Rutland , linux-kernel@vger.kernel.org, patches@lists.linux.dev, Andreas Hindborg , Martin Rodriguez Reboredo 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?1767423760078611568?= X-GMAIL-MSGID: =?utf-8?q?1767423760078611568?= 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`]. ///