Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1590787rdh; Mon, 25 Sep 2023 18:53:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEXddGDxs9bv1fCrClfHupuzlbtg01ISNQ+u9BcIFlYolWztrRMzuKuxTh7BMCcRljBa13B X-Received: by 2002:a05:6a00:847:b0:68a:5e5b:e450 with SMTP id q7-20020a056a00084700b0068a5e5be450mr10367373pfk.26.1695693199014; Mon, 25 Sep 2023 18:53:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695693198; cv=none; d=google.com; s=arc-20160816; b=dVqTnNxif79II5ZAHr20MNIFQgVgPXScpE8mIl8hI+4KkA5owXOZhXgHXWSlzIJxOy IB5TXEOCecvxWQGbu+N7wK91n+a1BF6lumyYQf5Vno2EJ4C48eZALXGwTFYyt+II3A0n kRBlvCbW/ZmHIOGTBqFTmkwoBfHlW/yicz3wdaeE83ljjjHi93uxpbsHF9undtWk6Eng CBdiId6kISEYz9kmUahx1antb896tWNu0QMjv+QpsLQE2sfJM5Kxfeu0upxe6nkt7xtm EKxYrV+bAO+obUecNslUhzSpBypHlt/3/hpxJad5UhCcHpss74rFAmTC2OfirUtZmX7G EIbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:feedback-id:dkim-signature:dkim-signature; bh=kUnVeKR6NPzam27Z8wdb0Fiy0PexlWX7F9qWzLELfaw=; fh=P2Lfcrns1VHLOfxDy7MqwJV8VeNMlojSML0dqr0g/xM=; b=XKsiR7xtcKZslOT+fkBoWzisC1xq3q/x5JTrrjuNzqknu/dptpN6650JHpYkjiZFnE DnJJBCafdu83z1H+mydM5J9Ey6zVhI8c73JsKy0rwyi22CVYMvJGd5RZ8sMRqIioxEvy YCRDCJVuu/JsPAfu4FCX3ZHUNUySkqWm0Ih3lvvxp3/RJZ6TSKyi1qM13sjez8HDYuds uWvqm3UaP3uJh5mv5aFVlXxpz+2WamM0mY/0qQh3F1McdatsrzVMgBtKtJyhOq7L7Qc5 oFXO/MFpt1NiRVYGR8yDbnvSuQOr6UrL5RYL9YoyzuwNsUBsNuwYsul5YAcVqyKVu5Fg +GZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ryhl.io header.s=fm2 header.b=TiJYuPI+; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=bM6SeA6r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ryhl.io Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id n24-20020a056a000d5800b0069109ee0b59si10789475pfv.231.2023.09.25.18.53.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 18:53:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@ryhl.io header.s=fm2 header.b=TiJYuPI+; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=bM6SeA6r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ryhl.io Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id AD8848104395; Mon, 25 Sep 2023 14:59:00 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230227AbjIYV7C (ORCPT + 99 others); Mon, 25 Sep 2023 17:59:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229481AbjIYV7A (ORCPT ); Mon, 25 Sep 2023 17:59:00 -0400 Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97F39AF; Mon, 25 Sep 2023 14:58:53 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 4E7415C0152; Mon, 25 Sep 2023 17:58:51 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Mon, 25 Sep 2023 17:58:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ryhl.io; h=cc:cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm2; t= 1695679131; x=1695765531; bh=kUnVeKR6NPzam27Z8wdb0Fiy0PexlWX7F9q WzLELfaw=; b=TiJYuPI+QfPcppWOD0lBVoZo9nqssKqzgw7Eh9vnsgI+CdLpKr1 QXs9bFpgEOiKo3FcQ6sohoN3IWDvu2dNHcGoL17L8fpSpEBWZbhKnK53pjnObQfY l0nDthoMRCKih/avUk1xe283HbXCsW/+dSziAu+PTNPCWaKxU/VKb78QP61fPdkK ejooTcxnrckeEp57zGO3gtUTk42QZYvUR6IqIFDbHS3EDvcJPnu22IYqO/i2gI/d BE5peWAkAxinydwYAzothp9u78+66VLxnJFMV0A8du06jsX28fOeqTvsruKhfbR3 rEKsr+f0BzyqM5dxkzxesw1aPIPATjxjqhw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1695679131; x=1695765531; bh=kUnVeKR6NPzam27Z8wdb0Fiy0PexlWX7F9q WzLELfaw=; b=bM6SeA6rOT9sCg21OHnkrZP8OA7AS7SezWXHgRFh9h4tB+cnPGL aiUR6S8PoieScU6VXbBr5dQY5w8+vzEztX51i+ACxLFDlqoiQLr0wMNBCT1NJWB9 ENSANK1cdGoAtm0sk0G48kerxV7cyn+N4Hcq4kcWkdd37ULBwIqiNsRt8NupZee/ jvUBxxaVZ/Klm48ZQIKFHZ2MvvgV45HOWBV0Z5xdezU0nrakCJ7n8cC2QkOUvZFg CPuAoYCTYo63CrTFXH/fBN+ykL33y44rDRTeHexc/zdOo3Wyq+t+rE2qGd7b2xgD WTk3a8pnia7E+UjVHC2MmvSmw29FGb06Paw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudelhedgtdefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvvehfhfgjtgfgsehtjeertddtvdejnecuhfhrohhmpeetlhhi tggvucfthihhlhcuoegrlhhitggvsehrhihhlhdrihhoqeenucggtffrrghtthgvrhhnpe efgfeugfdtgefhueeiudffjefhveeuheeuheekvdetfefgheehtdffuddtgfdujeenucev lhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhitggvse hrhihhlhdrihho X-ME-Proxy: Feedback-ID: i56684263:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 25 Sep 2023 17:58:49 -0400 (EDT) Message-ID: Date: Mon, 25 Sep 2023 23:58:46 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef` Content-Language: en-US-large To: Boqun Feng , Benno Lossin Cc: Alice Ryhl , Wedson Almeida Filho , rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , linux-kernel@vger.kernel.org, Wedson Almeida Filho References: <20230923144938.219517-1-wedsonaf@gmail.com> <20230923144938.219517-3-wedsonaf@gmail.com> <969eab7f-ad40-0dfb-18b9-6002fc54e12b@proton.me> <14513589-cc31-8985-8ff6-a97d2882f593@proton.me> <9d6d6c94-5da6-a56d-4e85-fbf8da26a0b0@proton.me> From: Alice Ryhl In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 25 Sep 2023 14:59:01 -0700 (PDT) On 9/25/23 23:55, Boqun Feng wrote: > On Mon, Sep 25, 2023 at 09:03:52PM +0000, Benno Lossin wrote: >> On 25.09.23 20:51, Boqun Feng wrote: >>> On Mon, Sep 25, 2023 at 05:00:45PM +0000, Benno Lossin wrote: >>>> On 25.09.23 18:16, Boqun Feng wrote: >>>>> On Mon, Sep 25, 2023 at 03:07:44PM +0000, Benno Lossin wrote: >>>>>> ```rust >>>>>> struct MutatingDrop { >>>>>> value: i32, >>>>>> } >>>>>> >>>>>> impl Drop for MutatingDrop { >>>>>> fn drop(&mut self) { >>>>>> self.value = 0; >>>>>> } >>>>>> } >>>>>> >>>>>> let arc = Arc::new(MutatingDrop { value: 42 }); >>>>>> let wr = arc.as_with_ref(); // this creates a shared `&` reference to the MutatingDrop >>>>>> let arc2: Arc = wr.into(); // increments the reference count to 2 >>>>> >>>>> More precisely, here we did a >>>>> >>>>> &WithRef<_> -> NonNull> >>>>> >>>>> conversion, and later on, we may use the `NonNull>` in >>>>> `drop` to get a `Box>`. >>>> >>>> Indeed. >>>> >>> >>> Can we workaround this issue by (ab)using the `UnsafeCell` inside >>> `WithRef`? >>> >>> impl From<&WithRef> for Arc { >>> fn from(b: &WithRef) -> Self { >>> // SAFETY: The existence of the references proves that >>> // `b.refcount.get()` is a valid pointer to `WithRef`. >>> let ptr = unsafe { NonNull::new_unchecked(b.refcount.get().cast::>()) }; >>> >>> // SAFETY: see the SAFETY above `let ptr = ..` line. >>> ManuallyDrop::new(unsafe { Arc::from_inner(ptr) }) >>> .deref() >>> .clone() >>> } >>> } >>> >>> This way, the raw pointer in the new Arc no longer derives from the >>> reference of `WithRef`. >> >> No, the code above only obtains a pointer that has provenance valid >> for a `bindings::refcount_t` (or type with the same layout, such as >> `Opaque`). But not the whole `WithRef`, so accessing >> it by reading/writing will still be UB. >> > > Hmm... but we do the similar thing in `Arc::from_raw()`, right? > > pub unsafe fn from_raw(ptr: *const T) -> Self { > .. > } > > , what we have is a pointer to T, and we construct a pointer to > `ArcInner/WithRef`, in that function. Because the `sub` on pointer > gets away from provenance? If so, we can also do a sub(0) in the above > code. Not sure what you mean. Operations on raw pointers leave provenance unchanged. Alice