From patchwork Wed May 31 14:59:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 101511 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2957952vqr; Wed, 31 May 2023 08:18:09 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4D/R4wKHvLBzCEV1yqXKWYmmc0tLtKlpiHqPNEOzvVjbqvpSDlaWe1Dh6CTpEazl8iPVzW X-Received: by 2002:a17:902:bd04:b0:1af:bb99:d590 with SMTP id p4-20020a170902bd0400b001afbb99d590mr5130304pls.7.1685546289183; Wed, 31 May 2023 08:18:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685546289; cv=none; d=google.com; s=arc-20160816; b=widpXChNud+AQCzcfKI46rpFxwvlggvpAckrqiohC08nAhYjLeZBJKh+2stHhTg30y 1+re2XkggXc2C5OvboiN54lfakpAvP4xxvZwOjKkNwU2DVoR2Mu5jsomBD6Gc29hNX4J EXzaHlphfLmomJ2TtwXmjxANULLZPiGVPzdkYQz7bhwEoVJGpXXp/Nck77SZt4wrTBF/ n0wz85IDbPd5BxgLaJsuhY7MszPnIfWFEVtzzQQYe0uvskY/sxoKp4/TAJCIMywPOrMA PXldcyvjc0b5CEX0GZ66vw/dtyYTtC1H+iRa5R1tBeQZrayAG9vUeIcbMQfB5OZdIu8t 1BFw== 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=v7AwI9/GAng9F6JU881/rPRp6iZXP9LwiNNuLduw3ys=; b=lE5Uv3XFV2t6GUFIWri0AaFmXYXedF7Kg1a1XaAdgS5P1tgyKwVY195No8KZimUgf1 wxOnL0uQ7HQENKSU0VEM/LbpVzI10BT+UDXj0ROpOLCivEYNWzmon94dIDFkV4MB9p9I RJ/M+flltLLFBjmtq18Se8jsoA/3DSrva+0anhkmxgoggHkZNZ67EkbLMu4fBb6iNpTe nD26OQdEhYWV+cg+8jmZ7e5FHEHH356K2iyb+3nXe6qaYf6NK0INtYQ124t1Sc1j2ZaH +ILQQ87XkxLGbYJsKldT0oz1cXMv73zus20qeKK9GC/4v649v4k4puPQuQJ1CB7M4fud zqng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b="OMfl8/Z1"; 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 h8-20020a170902680800b001aedad06360si1013959plk.255.2023.05.31.08.17.53; Wed, 31 May 2023 08:18:09 -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="OMfl8/Z1"; 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 S232478AbjEaPAv (ORCPT + 99 others); Wed, 31 May 2023 11:00:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233543AbjEaPAr (ORCPT ); Wed, 31 May 2023 11:00:47 -0400 Received: from mail-ed1-x54a.google.com (mail-ed1-x54a.google.com [IPv6:2a00:1450:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0441B107 for ; Wed, 31 May 2023 08:00:42 -0700 (PDT) Received: by mail-ed1-x54a.google.com with SMTP id 4fb4d7f45d1cf-514b8d2b21fso893088a12.1 for ; Wed, 31 May 2023 08:00:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685545240; x=1688137240; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=v7AwI9/GAng9F6JU881/rPRp6iZXP9LwiNNuLduw3ys=; b=OMfl8/Z1UwxPwIyaHYkJ8H7XXz53ExBje25D3fSggmAEgqJKexyNv/r05uILaNVDH3 T5pW/a/d7fWKUhDGhaJRx9W6nzSAhQEKxq4NCO9CY3nxTH5muUNEJCcdJEwE4FDKS5YE mYtD4BXfm5MS0N3+F6W6wTY8BVSX/OtnsTIl/ym9yDrzKwkqbJuxky5teH0ZIc6hZGV+ 4zu2xJq1E0BdirdqaHNQC6za3BZrmsP+frmwlo6ZL3kMQZ+wZwP9hNv7crfRZDRjX8oC c0qI0l8MuhuhbctU5kMcULymtY0J4dWQT3OiUwRNBTlaEv30nTmYbag6K13T0WiAy6ga xXMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685545240; x=1688137240; 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=v7AwI9/GAng9F6JU881/rPRp6iZXP9LwiNNuLduw3ys=; b=al7UXEMwXm+nJZj+px7Ron3P3P9L3hpYHBGA8oofgTcaHcmhmlVgWOaCxTRgVwBbTo lT/f3ZCi2FYoae5DH/KfgsUCEjGkpd7Rue16Zt++B7nKV5gh+YJe4EzkfZXse+8UL3h6 +IJ9+Wh6rS1WGKVH4cq3Hbwi2l+t6Nvcc1xI9q9dtWDnNjbryIN2s+Bku70hC3hf64S4 p86aJN3s1nz4SlN1F2/0cFrgiinhxOUqeKnQ9brBs8NmbKl9AANnRoHJ7XaYzjKgPs40 gDPtfMRRze6lURduZeXi9Eb1yipbe9SnCetau8wh5OYiAdWVmPdTi2YDX2Zb2RjxaDvK 2+OA== X-Gm-Message-State: AC+VfDxwIQA1oVnnQHTFdayIZ3ZsYtcCmbnCQ4B5FVPQ5oyJbY1U4P0J sIPognhAHI7O1trkCRdYUSP4D1YesnhGR/s= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:6c8]) (user=aliceryhl job=sendgmr) by 2002:a50:cdc1:0:b0:504:fcf5:63ee with SMTP id h1-20020a50cdc1000000b00504fcf563eemr1888238edj.0.1685545240591; Wed, 31 May 2023 08:00:40 -0700 (PDT) Date: Wed, 31 May 2023 14:59:36 +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-2-aliceryhl@google.com> Subject: [PATCH v3 1/4] rust: sync: reword the `Arc` safety comment for `Send` 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?1767423385758524272?= X-GMAIL-MSGID: =?utf-8?q?1767423385758524272?= The safety comment on `impl Send for Arc` talks about "directly" accessing the value, when it really means "accessing the value with a mutable reference". This commit clarifies that. Suggested-by: Boqun Feng Signed-off-by: Alice Ryhl Reviewed-by: Andreas Hindborg Reviewed-by: Boqun Feng Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Benno Lossin --- rust/kernel/sync/arc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index e6d206242465..3ad831603cf4 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -146,8 +146,8 @@ impl, U: ?Sized> core::ops::DispatchFromDyn> for Ar // SAFETY: It is safe to send `Arc` 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 `Arc` may ultimately access `T` directly, for -// example, when the reference count reaches zero and `T` is dropped. +// `T` to be `Send` because any thread that has an `Arc` may ultimately access `T` using a +// mutable reference when the reference count reaches zero and `T` is dropped. unsafe impl Send for Arc {} // SAFETY: It is safe to send `&Arc` to another thread when the underlying `T` is `Sync` for the From patchwork Wed May 31 14:59:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 101515 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2961472vqr; Wed, 31 May 2023 08:23:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4qTXAP5d7jHE0nSqjkwasv4znC2H84aJg39UZcaJos+AgAYBUFYkWmUg7d0nuUbBB/IbgV X-Received: by 2002:a05:6358:5923:b0:123:3408:6da6 with SMTP id g35-20020a056358592300b0012334086da6mr2277428rwf.2.1685546597006; Wed, 31 May 2023 08:23:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685546596; cv=none; d=google.com; s=arc-20160816; b=KeS9vv6j2AWLH9I5x1xq4+rWeWQny1ScRLzhdYgC3qgaBI8zxbdbplaoViBvUAsT+p 5UJOdD8H1w++nvLDaihiKmt/W8KEvLSOU62tQTQ1giZv/L4soFWO4eIQVh682vXYY9VA szWKVuEzAZ++teENSk0/J6M/FOHWpRC3W05O2h++MOtCdysUPlekWEtKm/uDyxHKgCjQ TzoxA+5/WvDobY8e9J6QAPsjkUpLwe/otFWv5pz7f3VDjL1YpIPXnXo/iffFo84bnHY2 Fz7Wiy/jO4kdM01cYa3dkZnvHeTP4Tmpv7kQ0ZydAdTZw77aEXf69FzeNaw6jfAZTI/S BRHg== 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=1Q+2I5zSt9CeXJ84cI2DkRkCdZ4Svm4rmosqyRFgB4Q=; b=0UvYHSNJk5VFUWdz6pR9eptzSt4NMaL44hYZ9+wy/DMTUG+FPm+WmQXevM9id8QnWR W5X7iVFzihzZ65KEuSk8NxbqoWJr9WUVHJeDii8VyGnfc0mVKbTXS3UxIxXNbDzJuK4n skd6XImG97tnnfE6V6ISqBDql1tTBWMS6RnHzLCQYbx5iYUbfS655NQ/Qy7mSv4ZPzmb BjyrRhWGFdzGic3/uTiRfGcUpiB6uxuWqBQyM5EjuZB85vkyF8dgjE8t5fODzzUXW4DR bJx6W7kqDG1DJ8Nly3H+T3i+XF17QbkhZjxIpHJH/GUm2DsepUCq27xnW93+RGvLp5NH gjGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=bnNa4BzN; 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 dw21-20020a17090b095500b00255ab0ed47asi1127453pjb.49.2023.05.31.08.23.01; Wed, 31 May 2023 08:23:16 -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=bnNa4BzN; 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 S233614AbjEaPBh (ORCPT + 99 others); Wed, 31 May 2023 11:01:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233543AbjEaPBg (ORCPT ); Wed, 31 May 2023 11:01:36 -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 F236EC0 for ; Wed, 31 May 2023 08:01:34 -0700 (PDT) Received: by mail-ej1-x649.google.com with SMTP id a640c23a62f3a-96fb396ee3dso480062566b.1 for ; Wed, 31 May 2023 08:01:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685545293; x=1688137293; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=1Q+2I5zSt9CeXJ84cI2DkRkCdZ4Svm4rmosqyRFgB4Q=; b=bnNa4BzNnMkAV8sNhlJArseXSoa/Cp3RaANBWBzd9VxjyIDTlr4hV7uLR/Fqr9sPFg V3T4M1u/Cqm0TGKtaApBthh754cK9SVYKk7sf7q3cjbDyz6YZOGdIstimjRAUVz5u2hR 8m671arTsCNlICRB/UITLp9bQ0bfiVv7ZPXC8NfTxKukxFdruO1EVpr8lAweg52Zhtt/ L3D8bfG+9M6OBkd6fuW1h7I971Cedkt4wE2UQtCSpAXNcu0eSmZhV39uhvD0x4mx1YER ls/kFUZu9/H1vEW7IHInniok08Iozig+z3zNg4JUoo/DWBFqr6kTLIvM4uP263wWuHu9 tIyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685545293; x=1688137293; 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=1Q+2I5zSt9CeXJ84cI2DkRkCdZ4Svm4rmosqyRFgB4Q=; b=OxGD6oaR3LfwYl4fudrd7WPoDkbbQs0N/W0ljMnjw6YvdJveJRQwW2fZYtx2AF1AOc ErxWh9tFKdTGfEG3pz5yhZ9wj9wfCZhxdUFklQo6z5S9clQtGghPL8L7B8PDGQZpF/Hw elk88LFTeM9lO6qTWPWd/IxpfT6bwpl8pPcRGZ0BscXzg0wF9mo4U/Ec/m13ci/eYben V2xlc3XqSw7xGZDLVVpZ2ezxUSCQfBbFpvlb9DwJw++uCgL/SyuY5IFtRveGF9/cujVf KBFkEhbnv2fr5YV6qb4il4oKvk+aV3UGk4Viz5Sp+JmZSoWq47tfkuYw0LfauSOveMpr k4tA== X-Gm-Message-State: AC+VfDz+rJqyQuEGKI4j/6UELh9AumNwcO6JdpuWmkyUaN/7QNlDvxlw w+dmRYiKK0KmvyF/QmSWNFX8Ya7aOLLQBMo= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:6c8]) (user=aliceryhl job=sendgmr) by 2002:a17:906:846d:b0:96f:5348:6ce0 with SMTP id hx13-20020a170906846d00b0096f53486ce0mr2119526ejc.5.1685545293508; Wed, 31 May 2023 08:01:33 -0700 (PDT) Date: Wed, 31 May 2023 14:59:37 +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-3-aliceryhl@google.com> Subject: [PATCH v3 2/4] rust: sync: reword the `Arc` safety comment for `Sync` 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?1767423708413589043?= X-GMAIL-MSGID: =?utf-8?q?1767423708413589043?= The safety comment on `impl Sync for Arc` references the Send safety comment. This commit avoids that in case the two comments drift apart in the future. Suggested-by: Andreas Hindborg Signed-off-by: Alice Ryhl Reviewed-by: Andreas Hindborg Reviewed-by: Boqun Feng Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Benno Lossin --- rust/kernel/sync/arc.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 3ad831603cf4..3da84187ecf4 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -150,9 +150,11 @@ impl, U: ?Sized> core::ops::DispatchFromDyn> for Ar // mutable reference when the reference count reaches zero and `T` is dropped. unsafe impl Send for Arc {} -// SAFETY: It is safe to send `&Arc` to another thread when the underlying `T` is `Sync` for the -// same reason as above. `T` needs to be `Send` as well because a thread can clone an `&Arc` -// into an `Arc`, which may lead to `T` being accessed by the same reasoning as above. +// SAFETY: It is safe to send `&Arc` 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 `&Arc` may clone it and get an +// `Arc` on that thread, so the thread may ultimately access `T` using a mutable reference when +// the reference count reaches zero and `T` is dropped. unsafe impl Sync for Arc {} impl Arc { 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`]. /// From patchwork Wed May 31 14:59:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 101504 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2950209vqr; Wed, 31 May 2023 08:08:09 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4MQuVyV6jCuZs/bgO2BBm+1shpdo1q8JO4pgpImMnLcVSm96snyORnwM/GsiKorhsODywV X-Received: by 2002:a05:6358:2929:b0:126:3906:64e5 with SMTP id y41-20020a056358292900b00126390664e5mr2946460rwb.28.1685545688648; Wed, 31 May 2023 08:08:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685545688; cv=none; d=google.com; s=arc-20160816; b=AlR1FlKvBfYONzzMcoP3HuZCXIwyZ9eDzU0WcAknRlcsa9T/EwJ8H5imxDCkCphu3e AUNxA3WsOEBpKw1MWE/b3JsHm/SSi5RTmEXWlJcvRtftYyMZD6pokYlRBKSL5spv8PNW w6QWZkIOgZo6Htz3vBxxex8Auw+LNSHS+CzEbzVugvahaf+v7GqZI1xNl47aGhJhydTY oTM//qZi6iOrLu0KScjjJWjxJWZ82ClfAoJ1qAA3ViqQa+W/9aGQ9XVYwppc3o5IcOFq l6576N7RpGf5xoVNLMbhErH4UbevjTqOgf6gewmxK/Df3SKOLOY2j7qjQqpMD5Or0HyW HpJA== 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=1lJ5Uf+EcwGNh0b/3i4Z2YdTIWfA6+IGf6dwAn0jzq8=; b=J0IHGkTml0ZF87CIceCm27x++LMxZa4mjXQL4qGOv77Zp8Vo+0veW+SoFORIF5zj3i 7NX1syOg1VXsiFc5PKbjuJuvaGDUkiVa7ma2kgXingUk9YBtxWXjhTPz3BW2ZzMuZ2vH WGz1TXz08VngZvey9eBUB8VEou31g1kxGn+Qont23i5UY3Lufzonj2si3wEALOyzRRAO jlqROQxlM0S/SEcUQ3TFOoaZhnGh7DvTpY5IpucKcalCvO4HtBHPyJtQGV6fYEdxuQJo 64K2KrgSGkv2NV6GwxfYZ0/u86PPMWWHeT9yrUpKB4nr03+WRnFNAKgp+/qHV8YNPT3f wQtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=5OgM51JS; 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 s30-20020a63af5e000000b0053fd0221f0bsi1097394pgo.200.2023.05.31.08.07.56; Wed, 31 May 2023 08:08:08 -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=5OgM51JS; 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 S233785AbjEaPDO (ORCPT + 99 others); Wed, 31 May 2023 11:03:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233817AbjEaPDM (ORCPT ); Wed, 31 May 2023 11:03:12 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F06E18F for ; Wed, 31 May 2023 08:03:00 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-bacfa4eefcbso2289453276.1 for ; Wed, 31 May 2023 08:03:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685545380; x=1688137380; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=1lJ5Uf+EcwGNh0b/3i4Z2YdTIWfA6+IGf6dwAn0jzq8=; b=5OgM51JSEe6PSOdk5CNCvcME9wVy09vhid7JxBNb7qJiDjF7E4J7ZYoqTcZXK7G/4Q fBVGQRIPzqWJ4sY8Qc07prjIwDuUtokHF0Ed2I4HNciIkLqmXpEkND10y5X5aZpnha+V JLsxaK5gm0+gDI/poxo2GLrXFxNVTsbSH75EjWelp+iT0fqpaAFlShM/OEEAy9Q/Z1oH U4OocqQZEmuNGhc3RV03kZUW3fYLSdLdiSfHU6Vi4FDgN/sFLOHJKfa+8/E/hJkpUra5 Sjmd9/kBBv4MLQcWszMrplLIURaRI+p5FKBw8o4fAZ4CJcaOMFqdKw5U+Lr+aVFM4OvC h9Og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685545380; x=1688137380; 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=1lJ5Uf+EcwGNh0b/3i4Z2YdTIWfA6+IGf6dwAn0jzq8=; b=TXQ5zhCBLl8D16BUdJpJ98fSGVeWI+efEzG9yZrNo6XDfCRw8aWDxkH1RgH7ghWVxD hmewCQTYiWKil3oXoaCkIbR5LbwrGXQNeURVHR9Dtqu9MW6SvBzheO5p4HZyL0lZUlIV Z1wwuIe4q6Zp9j+M2kKHDlRlHGPLELvzn24bTZ31MhKh9EpFGSvbOwPdsGWMN2amTK/q t2HsxfqnAdYf0w1rfD7//4afzO7pGqfO2XhAOaFtSK+mPJNGeUMewb+dKGBKmfE+QZGR zE7dwcOcaS2ZUTBdXAQyb6c2hqtnbyjS5hJ/qwxf0bti5eb51Tlpj/BgR1xZQuO4emfA Q2VA== X-Gm-Message-State: AC+VfDyLrOdsIXDgbsXwiO3IYjfEa7tS9KWqGvFOF7cynFmm4pIBlKyb OMR0Woo4TfcQrtf+DS1JoR++yM6iSNh2hsY= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:6c8]) (user=aliceryhl job=sendgmr) by 2002:a05:6902:100e:b0:bb1:569c:f381 with SMTP id w14-20020a056902100e00b00bb1569cf381mr1335253ybt.1.1685545379836; Wed, 31 May 2023 08:02:59 -0700 (PDT) Date: Wed, 31 May 2023 14:59:39 +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-5-aliceryhl@google.com> Subject: [PATCH v3 4/4] rust: task: add `Send` marker to `Task` 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?1767422756478515995?= X-GMAIL-MSGID: =?utf-8?q?1767422756478515995?= When a type also implements `Sync`, the meaning of `Send` is just "this type may be accessed mutably from threads other than the one it is created on". That's ok for this type. Signed-off-by: Alice Ryhl Reviewed-by: Andreas Hindborg Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Benno Lossin --- rust/kernel/task.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs index 526d29a0ae27..7eda15e5f1b3 100644 --- a/rust/kernel/task.rs +++ b/rust/kernel/task.rs @@ -64,8 +64,14 @@ macro_rules! current { #[repr(transparent)] pub struct Task(pub(crate) Opaque); -// SAFETY: It's OK to access `Task` through references from other threads because we're either -// accessing properties that don't change (e.g., `pid`, `group_leader`) or that are properly +// SAFETY: By design, the only way to access a `Task` is via the `current` function or via an +// `ARef` obtained through the `AlwaysRefCounted` impl. This means that the only situation in +// which a `Task` can be accessed mutably is when the refcount drops to zero and the destructor +// runs. It is safe for that to happen on any thread, so it is ok for this type to be `Send`. +unsafe impl Send for Task {} + +// SAFETY: It's OK to access `Task` through shared references from other threads because we're +// either accessing properties that don't change (e.g., `pid`, `group_leader`) or that are properly // synchronised by C code (e.g., `signal_pending`). unsafe impl Sync for Task {}