From patchwork Mon Apr 3 10:01:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 78480 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2200134vqo; Mon, 3 Apr 2023 03:36:42 -0700 (PDT) X-Google-Smtp-Source: AKy350adLbnFmRO9MwT3b+3/4a7GCCxzuxpdlen3zolyQm1C2OuGUZ6fce+t+WfgvUFg6wMgehhw X-Received: by 2002:a05:6402:752:b0:4aa:a280:55b5 with SMTP id p18-20020a056402075200b004aaa28055b5mr16021455edy.20.1680518202665; Mon, 03 Apr 2023 03:36:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680518202; cv=none; d=google.com; s=arc-20160816; b=MYDdBr3kv2YiU0LFXy9Zra+yp7G5eCj77taTtM0oI4K/C18OYozmCjjcGSVRI1PL7v YK+mOXT88mGN155a7EiqNg4V59F8BJlSdCWAL2QfuGTkxAbVMrnC2IIvz2DrQRpxUFrX ADUvxf0CgKKv/O97EHUhP+n+Tes/FeqWRMYyxYlYidScTd5MIMKZHN243qxHJ6MBaIxB ++tgTTfH8t3l0Ijtbct1mlSjs+7N0egb71gQNozHEzY30N4JbScqYMTchWv1JufuLbJl 7yrX1XAYfDb+gPMwqENmpqnracVDH1n8NF2DgFQZp/r53LyrBYnQkeyYPtIsRgWKp1CO EO/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=XP2ZcyANLC83/ESrUlBBXBRtJgAKuXBxCR78hD4IioE=; b=K5pg/vLgY2UvVGa4c69xB6cXHi2F6ZBhiJhbIMPxWWIJic6/nfriU48s2FSpEosY+G chFaEJbCQxAbhnsDIe1JBYKPn4ksF68/sM3HRBGbGZq9Q4QHYbZo0qKzzQaOuKTAg6bA xF8OlPLEJI07p9vLLwvwwp8fc2RzzoNDU5X0AYD9PLAP8jk5GuolmrKqViDcBHcPHWUU cPOWpuBJlL0nRdbzMYlJe+HhdbM4yo0y5KJY9c2OPwqJVkcd8v3ihB7Z4sCvdd/oKiy/ unhr5FJEHrTA/0B8sm6FGgCKa+Fn9agrPvral1RGwMVINXau7jgcQ2xKrzGkwJLM0f9w FWJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=PqipZl7A; 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=asahilina.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x11-20020aa7d6cb000000b004acbd4990e5si7681693edr.158.2023.04.03.03.36.18; Mon, 03 Apr 2023 03:36:42 -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=@asahilina.net header.s=default header.b=PqipZl7A; 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=asahilina.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232164AbjDCKCN (ORCPT + 99 others); Mon, 3 Apr 2023 06:02:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232157AbjDCKBv (ORCPT ); Mon, 3 Apr 2023 06:01:51 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF5CD83F0; Mon, 3 Apr 2023 03:01:45 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: linasend@asahilina.net) by mail.marcansoft.com (Postfix) with ESMTPSA id D8E5142118; Mon, 3 Apr 2023 10:01:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680516104; bh=mt96Ry31EMWGGv3xrCXuVnmwVeuczytGDrnki4wvMRY=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=PqipZl7AWWAdCe5Yzxssc80eVykz40viy3qcLHZ62UHBx/iPgJuhXz2D79idamOaI NjMH16vDg2ZOGI62I+F7GkXLjwbkPmSUFgbQ0tlddmi97ND25mlUd9mjqoQZvkVJMW 5lNKqqbWg6Tew9DZcNqgVPjsnW8yZNbOB/gHGKAMjc3WjD8lPyIwO+791nJ+sw2g4G 0zFxXfk6ga0yVxkWVRqkGOdZGPxC34X890SYtMogMLy1bT9VEMokw0SLpZB3X8EWCn XG4avnkZjk6wl5W3+ReAjnFCMNjIAIDYyb2xHMp2sBE9CQIg6fKpiB4W9rkzkWOaSE hrlEZ6LzmpRRg== From: Asahi Lina Date: Mon, 03 Apr 2023 19:01:11 +0900 Subject: [PATCH v2 1/2] rust: sync: arc: Implement Arc::downcast() MIME-Version: 1.0 Message-Id: <20230224-rust-arc-v2-1-5c97a865b276@asahilina.net> References: <20230224-rust-arc-v2-0-5c97a865b276@asahilina.net> In-Reply-To: <20230224-rust-arc-v2-0-5c97a865b276@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: Andreas Hindborg , Vincenzo Palazzo , Martin Rodriguez Reboredo , Benno Lossin , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680516096; l=2373; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=mt96Ry31EMWGGv3xrCXuVnmwVeuczytGDrnki4wvMRY=; b=CZTlhuH+UKIX+dfSA8mpbpmUrwOHpedGgcAWg9ihdc3Zse8Z/baeEqCP51bezEj85mQaJMvmE r9NNAIqSPQZA7JYdmy2VtJ2yQlX+qirhipwZ+r9tGnKWWX4oAUrDI9y X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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?1762151054757586229?= X-GMAIL-MSGID: =?utf-8?q?1762151054757586229?= This mirrors the standard library's alloc::sync::Arc::downcast(). Based on the Rust standard library implementation, ver 1.62.0, licensed under "Apache-2.0 OR MIT", from: https://github.com/rust-lang/rust/tree/1.62.0/library/alloc/src For copyright details, please see: https://github.com/rust-lang/rust/blob/1.62.0/COPYRIGHT Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Andreas Hindborg Reviewed-by: Vincenzo Palazzo Reviewed-by: Gary Guo Signed-off-by: Asahi Lina --- rust/kernel/sync/arc.rs | 2 ++ rust/kernel/sync/arc/std_vendor.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index f2f1c83d72ba..b904ed898d3a 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -522,3 +522,5 @@ impl DerefMut for UniqueArc { unsafe { &mut self.inner.ptr.as_mut().data } } } + +mod std_vendor; diff --git a/rust/kernel/sync/arc/std_vendor.rs b/rust/kernel/sync/arc/std_vendor.rs new file mode 100644 index 000000000000..a66a0c2831b3 --- /dev/null +++ b/rust/kernel/sync/arc/std_vendor.rs @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +//! The contents of this file come from the Rust standard library, hosted in +//! the repository, licensed under +//! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details, +//! see . + +use crate::sync::{arc::ArcInner, Arc}; +use core::any::Any; + +impl Arc { + /// Attempt to downcast the `Arc` to a concrete type. + pub fn downcast(self) -> core::result::Result, Self> + where + T: Any + Send + Sync, + { + if (*self).is::() { + // SAFETY: We have just checked that the type is correct, so we can cast the pointer. + unsafe { + let ptr = self.ptr.cast::>(); + core::mem::forget(self); + Ok(Arc::from_inner(ptr)) + } + } else { + Err(self) + } + } +} From patchwork Mon Apr 3 10:01:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 78478 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2199746vqo; Mon, 3 Apr 2023 03:35:49 -0700 (PDT) X-Google-Smtp-Source: AKy350bR94c8+CDsyjpjbY1w1pUrSMvdIxL3wPUF5/+quL1iUYq6AaqxaU9uXEReB4n/p3dT4Fe8 X-Received: by 2002:a17:907:6e06:b0:947:c201:df9b with SMTP id sd6-20020a1709076e0600b00947c201df9bmr9324495ejc.9.1680518148908; Mon, 03 Apr 2023 03:35:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680518148; cv=none; d=google.com; s=arc-20160816; b=ylRTHsLjnuwFtfv0L84+UFZTmm+Z2uIuJ6GyRFeBmR9oMr7YRUeNbblopeQJwLTruu VB7ruA5kdj+g+pmdgX62HkRZ0CbcLyBmGUcg4MG3/PMdebrvXuwWPIUUNIUtblzpUtkA LzZRX7ii8xfGeSxMDur2spHtngwjMbTbArIqoDSBsnLD/3Wqcy4mUGCujrnxs0QDuGQF X8a19YVRguJOtYOac5RvN5ZS4zqu1qRV24ri1XbSo5IxJSmsHulzXXtl9qcBgB2akbGk w/6cGN26GU9WZ4MFwFZCgOh7Ko8oJYiw3PuOO/M6xqbUSkrnVwXjD5YEPgpSkB/EGvhl Ezqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=QKnNO0YGC34XiCu/Oreu5B8FEvTqv24bPNAO90yOF/0=; b=i/Og+G6XNZ4GNT8WGI6VL3L9ZeDfrJ9RCDIO/3GWMcEZ7wYKOtWeR5wq+39I1gJ8RW 3gykE8u7jQPeRVqPaainrz+mMAPJZzrOlINo+N+gO4mt2cy0cUPaTZ90D/XJDANfj9Hn c1N1b28fyFEH0UZpZcoS26cNS4I3yac2dJQdf9GRKQcbHcFkuMclEFIp71A+naokj+81 9PXgha+17b/aYvn1UZFYzfgcibkpQuc2zM8odNa/HaeGg3dCzE4uT+Llssp5vrZOjN3q LT0POOFaKPKqa56rPqdH+nFtJuxlMYmax6Sav/goArsDHQCHnxptYOKhMv6vKZ8t6JnQ xYfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b="PEBvY//M"; 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=asahilina.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p4-20020a170906228400b009334be3d5fcsi7630754eja.296.2023.04.03.03.35.25; Mon, 03 Apr 2023 03:35:48 -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=@asahilina.net header.s=default header.b="PEBvY//M"; 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=asahilina.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232039AbjDCKCJ (ORCPT + 99 others); Mon, 3 Apr 2023 06:02:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232163AbjDCKBv (ORCPT ); Mon, 3 Apr 2023 06:01:51 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8778524D; Mon, 3 Apr 2023 03:01:49 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: linasend@asahilina.net) by mail.marcansoft.com (Postfix) with ESMTPSA id D7606423BA; Mon, 3 Apr 2023 10:01:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680516108; bh=+HaSZDGbivQJSY7YEFbLidqycutVbcVcWgZLNHkg3sc=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=PEBvY//MxjhXn7GofQK56jt6e0nLl+Cz8rwP5nuTiXfdMcbDcl9MNXNzupCuKIyOS EeWYAjJgQRMUkPkzVUbeKo9E+NXlQ8LiPxACOmWFafGWMFfc8xhPlXlL9Tjenfpv75 W31kEJZh3fji0IuhFrEFOPY+AkbrHjz48RQyWPC32fQJOE8VRmENcvKcxg2B2QhZ9d nA9sc+MvZ31pV60s9ZdwnPR7RQ5WBjqadpmcNxYsQbZRFV16grxPLJyeUNT88xaWYc CSxtgq8EkkCoYGtpoa/K39A+gnuBigcUCxasY9M1sDflFEnMRO+0on/e9qorkY7Jbc GEJgHiUoTEwAg== From: Asahi Lina Date: Mon, 03 Apr 2023 19:01:12 +0900 Subject: [PATCH v2 2/2] rust: sync: arc: Add UniqueArc::assume_init() MIME-Version: 1.0 Message-Id: <20230224-rust-arc-v2-2-5c97a865b276@asahilina.net> References: <20230224-rust-arc-v2-0-5c97a865b276@asahilina.net> In-Reply-To: <20230224-rust-arc-v2-0-5c97a865b276@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: Andreas Hindborg , Vincenzo Palazzo , Martin Rodriguez Reboredo , Benno Lossin , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680516096; l=1928; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=+HaSZDGbivQJSY7YEFbLidqycutVbcVcWgZLNHkg3sc=; b=D/SmqTHwoV04N4AZ+pjn10VxfxZKRJYh1xmzLnOtmSG/S67qFVwAk3MdCshzPNeavWKr3bfxk fxnk5n1jxLuCUZL+ITvXD8CsT/RYmCobT9gRBt8mc0zEpd/tyYjEIkt X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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?1762150998429505172?= X-GMAIL-MSGID: =?utf-8?q?1762150998429505172?= We can already create `UniqueArc>` instances with `UniqueArc::try_new_uninit()` and write to them with `write()`. Add the missing unsafe `assume_init()` function to promote it to `UniqueArc`, so users can do piece-wise initialization of the contents instead of doing it all at once as long as they keep the invariants (the same requirements as `MaybeUninit::assume_init()`). This mirrors the std `Arc::assume_init()` function. In the kernel, since we have `UniqueArc`, arguably this only belongs there since most use cases will initialize it immediately after creating it, before demoting it to `Arc` to share it. Co-authored-by: Benno Lossin Signed-off-by: Benno Lossin Signed-off-by: Asahi Lina Reviewed-by: Gary Guo --- rust/kernel/sync/arc.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index b904ed898d3a..b664b41e473d 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -489,6 +489,17 @@ impl UniqueArc> { /// Converts a `UniqueArc>` into a `UniqueArc` by writing a value into it. pub fn write(mut self, value: T) -> UniqueArc { self.deref_mut().write(value); + // SAFETY: We have just written the contents fully. + unsafe { self.assume_init() } + } + + /// Unsafely assume that `self` is initialized. + /// + /// # Safety + /// + /// The caller guarantees that the value behind this pointer has been initialized. It is + /// *immediate* UB to call this when the value is not initialized. + pub unsafe fn assume_init(self) -> UniqueArc { let inner = ManuallyDrop::new(self).inner.ptr; UniqueArc { // SAFETY: The new `Arc` is taking over `ptr` from `self.inner` (which won't be