Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1642880rdh; Mon, 25 Sep 2023 21:33:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFhsEeEqX+I/HxsjZeynamNSzMo1xoPlxDqGIMfELQRYHpYd4SPYZpvpsgap1T/i0XNl2MZ X-Received: by 2002:a05:6808:2120:b0:3a7:5557:16c2 with SMTP id r32-20020a056808212000b003a7555716c2mr12967370oiw.0.1695702837141; Mon, 25 Sep 2023 21:33:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695702837; cv=none; d=google.com; s=arc-20160816; b=B2saHQgZz7wgbLr1BAXcAA/9qJqtIggWGxtZzGpLhuaT5sEiZUuDznN2UiBCvG+Nus QplMRGGGECXxm7INDF5bcTi97+waLw8XQaQrbJbegoB6YNhh+p983IsM5+iNuCHFCCJU GfPL3H0BlBMVFEmfN2OG6pFgbjk5J77r1EBwR1rmvfKnQBoZ1cDspUH+v7AEZ/cOQXoV yrZ0FpNRjWMh0+ciC/lGeTxsMGVQM6t074z0Mb+9RsfqqoMwk4XPJjtHe2/oGuFnCxhf jVGmPQ7hq4O6M2GEPay9oD7Sx7ZvRgDHveNG/ehla8Sg1l5GMFhVO/Tqp7STULZDUETe qE9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:feedback-id :dkim-signature; bh=JDhVxKt0fuJsO1ywUyi0HgdMgyRyKvfcjAEXNVSEPJk=; fh=i+8td0jeA0x37MnAp4+fELDjZGKpzLURUmZ3wDctG90=; b=p53op109iRdFnLNq9KPFLZnS9RkrbsBWy3tQIHSsnV1E7/bupkormoIcrlUHBErcxG DgE3LOyl/eDWv2SQds5K1j8n6jAtjrMHIUxc7AJEoNWC54NR82BTyxufaNAcG93hNady vbybe6wh4lhSLmYan0iIteY8QQL8YsF0vnsMESEGAzybWylITRd25p1ojm+IKnuoeLUP WqouZ9FZxl9OhNgOR78TGrbGeHW1VMTwafcVNjkm6wUS4QH7xOI2q1AUNotf9Yt9KRSp SP/63sLSLLzeMGoRreVBJyHU2FxmpXQf0bCWsVAbtmnqnY1yc+JToSeae2DecgMepL87 lROw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=OZTY32TN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id l191-20020a6388c8000000b0057884435a71si11874082pgd.396.2023.09.25.21.33.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 21:33:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=OZTY32TN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 04BAA8163C31; Mon, 25 Sep 2023 14:56:23 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230086AbjIYV4Q (ORCPT + 99 others); Mon, 25 Sep 2023 17:56:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229509AbjIYV4P (ORCPT ); Mon, 25 Sep 2023 17:56:15 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0479FAF; Mon, 25 Sep 2023 14:56:08 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-98377c5d53eso888721666b.0; Mon, 25 Sep 2023 14:56:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695678966; x=1696283766; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=JDhVxKt0fuJsO1ywUyi0HgdMgyRyKvfcjAEXNVSEPJk=; b=OZTY32TNHJyjiI8/1nk9jF7XXBzv4hyjh11HedGFhR0H0gzz16fgQDd/bhPNxRmJO5 s8MAP12qFFCxmDwOYOeBRAkWiwfI3hZIpvFq2uxrGEL7mU4tOeMlPd4EP4TpWkSBdzzL lFuv5AYB7f6N6Cp/K7gBcH8M8HSXzpRZBb4I+Ecrq0Zl+7JURy06Si7JOFiQxY0ILnSZ 9gedljH6LQS2aEJUDYnAiQiiRauIWYI01br1xgX6Gqpdvc28tjCjntWC2rcmA3HNr4V1 K++mbkja05TzZlmio9AIlQOOML+5bx9Qj6eA65QLMcAj0DZr3gwOd4YmSG1YE/8qBnHj pzfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695678966; x=1696283766; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JDhVxKt0fuJsO1ywUyi0HgdMgyRyKvfcjAEXNVSEPJk=; b=ejfJt6/wUZfMduZhI+nbCsOmaXdlaEY9vybY2Ux9cvYSjFL8fhKrirh6Nhj5sm16Ke +QfIsEJDuUMlj0G/INa46RVR1bPhSz5iL1tJTStpsQ4wh7O/HL6eyYfv3sJ0SB7WHZxP jdbu0IUlK1BERZsoUiC+Fwyq+gGsEUQq/PDz2IOtCcGdg0kQZVQVpVU1zUAxoDDXwvXa s/hKB5IEf958PSQIN5V1M8z5fRKD632n5R5F40T3GGs8jOk1Gv2cE3LXXuuvkX4ufsI5 03/B53VOlDl0LpPshkc6lrB7C9XOAUMreFjYf36OD/z3gYRNJsYK2No7pjTqz+dLF1pJ 46VQ== X-Gm-Message-State: AOJu0YzfQ1Myn3tHiATc1hjkhn6qIRy87YcJjso62HXTsmdK2LPBgj0X Nb/T2jHuaFaeqce0fK1gwhA= X-Received: by 2002:a05:6402:c84:b0:533:c75a:6f6 with SMTP id cm4-20020a0564020c8400b00533c75a06f6mr5940837edb.12.1695678966097; Mon, 25 Sep 2023 14:56:06 -0700 (PDT) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id dm20-20020a05640222d400b0053414427690sm1726377edb.41.2023.09.25.14.56.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 14:56:05 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailauth.nyi.internal (Postfix) with ESMTP id 1967C27C0054; Mon, 25 Sep 2023 17:56:03 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Mon, 25 Sep 2023 17:56:03 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudelhedgtdefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeehudfgudffffetuedtvdehueevledvhfelleeivedtgeeuhfegueeviedu ffeivdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe gsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdei gedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfih igmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 25 Sep 2023 17:56:02 -0400 (EDT) Date: Mon, 25 Sep 2023 14:55:30 -0700 From: Boqun Feng To: Benno Lossin Cc: Alice Ryhl , Wedson Almeida Filho , rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Andreas Hindborg , linux-kernel@vger.kernel.org, Wedson Almeida Filho Subject: Re: [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef` Message-ID: 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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,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 agentk.vger.email 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 (agentk.vger.email [0.0.0.0]); Mon, 25 Sep 2023 14:56:23 -0700 (PDT) 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. Regards, Boqun > -- > Cheers, > Benno > >