Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2189962rdh; Tue, 26 Sep 2023 15:51:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEKycu8g1QKLNyETs6jWr8ZqIBl0v5YXSBFvhXZYj2Sct736gBUSYMWMiahQx/x/xqnfYhE X-Received: by 2002:a05:6358:6f81:b0:143:9bc0:a975 with SMTP id s1-20020a0563586f8100b001439bc0a975mr372950rwn.7.1695768696541; Tue, 26 Sep 2023 15:51:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695768696; cv=none; d=google.com; s=arc-20160816; b=FpUG/ebJV2THReFouuJQ8CiNtYxxKIe1zWCt7VtJVNq5FSywLLPgydSG5g9sbIRsY5 nLK2qr1RgIRU22R2QxltQuZAz7hfs3prxsL0dr5Xt3+GdBRzMd3517bwJmEBH7PBxWTP MoG4UfkXgyROW+BEHS8fMh8A6QJj3zqgQxDa7OKcIhgfWRPndRXVb16K2VNtQIN0w0rn Ab6K068iqVaEbKI1/D+8dgc0Eaz8K1tzvvgabYE9eBXNU9nzQKijWxuc/ZUUniUnOF2u JH+WfIps+ZlOtIkbYJil1TWW3C7RyROa9wZB9Mjfx836TCKoZkcXpAb5G/vVy5r4rwQX 97eg== 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=IFaFCx2eXhg+kYgVes9eDxQpvEDaU+z47GYjWRCNqSM=; fh=ZghOmOrlEMw62Z9AKvM/K7B9BuWiCL8YtmV3OEZoi5o=; b=UsK6BlPlK2cK2ARrGh2VXcx+Mp3e/ZNCG+iznu6bAQWKTMHVtJqLmYsrFQN1BsBnJl 8MPJHQmw+ZiTOnjgqrpArY7Hw9/c7LWT1oKjZ9ufa7qijz98LXrGo29HjCTUx9616QXD T/t3fQiaVWNfSbUZ+D+gKSS1LJQghJ5mdFPAbdKvGv8NiPtZHzPJXjr3bc1W/+6ASgHG 5qIb0ZVrtEuOAfMjpIXId0aMiHqN4Enh25WViTX0kYt5AaObIhlnfNBNFu9AazfE337c 9T/sJDmkyFhtqHs8fOOsJkuA64PdwuCx5iDRK7hnMKnPRWCASY6OkGOoKTrm1kSt/QfZ ZK4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=MzKSnanZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id w20-20020a63fb54000000b00564beb6145fsi13104245pgj.765.2023.09.26.15.51.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 15:51:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=MzKSnanZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id 445B3806AFDB; Tue, 26 Sep 2023 11:21:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235191AbjIZSVW (ORCPT + 99 others); Tue, 26 Sep 2023 14:21:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229809AbjIZSVV (ORCPT ); Tue, 26 Sep 2023 14:21:21 -0400 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92DFAE5; Tue, 26 Sep 2023 11:21:14 -0700 (PDT) Received: by mail-ed1-x52a.google.com with SMTP id 4fb4d7f45d1cf-533edb5ac54so6579968a12.0; Tue, 26 Sep 2023 11:21:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695752473; x=1696357273; 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=IFaFCx2eXhg+kYgVes9eDxQpvEDaU+z47GYjWRCNqSM=; b=MzKSnanZc14lbNKrjRbqvjQVGfq6qiAFqpFgAuLAg8E4TxIIj6Lm2nn3XNmGwi09Pk ef9WB4sHL6QtGogLy6FYuh1hin8CZyAZ8a6h1R7KAjbFm60iLG5FHYAv/fR5BEg6JT/b PNnJVfOOJcMpjBB9jMjeYN3FHEp5SAJqNly+4omhF5hIWPaFB26KGed39YoYM6tbjRcZ vB9xc7FXlWpiugKhBGkUIPtgK2uJNoeTKP5/iRF2f48tgcaxCG1ecZBJArtiJU/2dPWa vUl/GVPOVHb/nIxHfhKUqeE0jtSbEP42bpmucAG+p1ELK38fbU2+hZs/zi9Ax3kBT18V 8cTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695752473; x=1696357273; 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=IFaFCx2eXhg+kYgVes9eDxQpvEDaU+z47GYjWRCNqSM=; b=Sxk+Pmh2o52L5kIqP0X0fcCuRZlfblbjJgmpxB9C8nGgoZhRB+fvPAL5Pjq/4sg4W0 LIHPwIye+/r+QBCFPAzUCwGY1VTEylgvPE4StQ8rLs4tSgCgxnBVMi63drMMZ/6HSuJM 07bb9bgCKBLKl7OgmGgYAW9tjz9fAhpdO9EBNd6If2kRAkntGADXmyp1sHPFQPs5+A1h 3cujgMzriM9LXCnqBrD3xFyBlV7/YxxQrdO25f+pDEdFYrDoMtJG0MAHeKbnO3FKfyLB NgC9t5N/ZpuKQCeOjNAXwAgFMKcUAB3+pSfkqMD5D3nzZvTwTPjscj8pmYUG4gW09Trk LJTA== X-Gm-Message-State: AOJu0Yx/C2KjSsupsBxnSf7nlnObPJ4UoX/amf4+kBDK8l+K4L3DS76c 9GLbCimZg7Jezyf2Fo1+AwM= X-Received: by 2002:a17:906:9b92:b0:9a1:d915:637a with SMTP id dd18-20020a1709069b9200b009a1d915637amr11218733ejc.61.1695752472831; Tue, 26 Sep 2023 11:21:12 -0700 (PDT) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id p20-20020a1709060e9400b009ae587ce135sm8062937ejf.223.2023.09.26.11.21.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 11:21:12 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailauth.nyi.internal (Postfix) with ESMTP id 7256A27C005B; Tue, 26 Sep 2023 14:21:10 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Tue, 26 Sep 2023 14:21:10 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvjedrtddtgdelgecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeeuohhquhhn ucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrth htvghrnheptdfffeetffffjefhkeeujeelgeegtddtfeeiffefhefhudffgfduleejudfg geejnecuffhomhgrihhnpehpthhrrdgrshdpghgvthdrrghmnecuvehluhhsthgvrhfuih iivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghu thhhphgvrhhsohhnrghlihhthidqieelvdeghedtieegqddujeejkeehheehvddqsghoqh hunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 26 Sep 2023 14:21:08 -0400 (EDT) Date: Tue, 26 Sep 2023 11:20:35 -0700 From: Boqun Feng To: Benno Lossin Cc: Alice Ryhl , Gary Guo , Alice Ryhl , Wedson Almeida Filho , rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , =?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: <9d6d6c94-5da6-a56d-4e85-fbf8da26a0b0@proton.me> <61ccfb87-54fd-3f1b-105c-253d0350cd56@proton.me> <20230926162659.6555bcdc@gary-lowrisc-laptop> 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 morse.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 (morse.vger.email [0.0.0.0]); Tue, 26 Sep 2023 11:21:21 -0700 (PDT) On Tue, Sep 26, 2023 at 05:15:52PM +0000, Benno Lossin wrote: [...] > > > > But here the difference it that we only derive a `*mut` from a `&`, > > rather than transmute to a `&mut`, right? We only use `&` to get a > > pointer value (a usize), so I don't think that rule applies here? Or in > > other words, does the following implemenation look good to you? > > > > impl Arc { > > pub fn as_with_ref(&self) -> &WithRef { > > // expose > > let _ = self.ptr.as_ptr() as usize; > > unsafe { self.ptr.as_ref() } > > } > > } > > > > impl From<&WithRef> for Arc { > > fn from(b: &WithRef) -> Self { > > // from exposed > > let ptr = unsafe { NonNull::new_unchecked(b as *const _ as usize as *mut _) }; > > // SAFETY: The existence of `b` guarantees that the refcount is non-zero. `ManuallyDrop` > > // guarantees that `drop` isn't called, so it's ok that the temporary `Arc` doesn't own the > > // increment. > > ManuallyDrop::new(unsafe { Arc::from_inner(ptr) }) > > .deref() > > .clone() > > } > > } > > > > > > An equivalent code snippet is as below (in case anyone wants to try it > > in miri): > > ```rust > > let raw = Box::into_raw(arc); > > > > // as_with_ref() > > let _ = raw as usize; > > let reference = unsafe { &*raw }; > > > > // from() > > let raw: *mut T = reference as *const _ as usize as *mut _ ; > > > > // drop() > > let arc = unsafe { Box::from_raw(raw) }; > > ``` > > I don't understand why we are trying to use ptr2int to fix this. > Simply wrapping the `T` field inside `WithRef` with `UnsafeCell` > should be enough. > BTW, how do you fix this with only wrapping `T` field in `WithRef`? Let say `WithRef` is defined as: struct WithRef { refcount: Opaque, data: UnsafeCell, } impl From<&WithRef> for Arc { fn from(b: &WithRef) -> Self { let data_ptr: *mut T = b.data.get(); let ptr = ?; // how to get a pointer to `WithRef` with the // provenance to the whole data? ManuallyDrop::new(unsafe { Arc::from_inner(ptr) }) .deref() .clone() } } The `data_ptr` above only has provenance to part of the struct for the similar reason that my proposal of (ab)using `b.refcount.get()`. Am I missing something here? Regards, Boqun > -- > Cheers, > Benno > >