Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2318554rdh; Tue, 26 Sep 2023 21:33:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG4Nx/mrIpeZHz6QlVEoWKlP1ynm/FM5eyW9W7sp63nZV+771CUJYIEXiAZzVXJd3b3ZwFb X-Received: by 2002:a17:90a:34c3:b0:276:e9ba:60c7 with SMTP id m3-20020a17090a34c300b00276e9ba60c7mr600316pjf.13.1695789200018; Tue, 26 Sep 2023 21:33:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695789200; cv=none; d=google.com; s=arc-20160816; b=Ea6BvYzf9VStEiS5zFZZ33qY2GBh5Yw3aUeTtTUE4sjI/YObkbTNQbvnX1ELrogxrK iIjWcYK6YvYdz4HhzhTm7k00oCaTjD2lYjAAFO8Qa5702JgXzaJ4fR4hpOOgQVGDbeFp XYWa017h7X+7lkIZEPTD8W/2KuD5JLU2za0IAQYVbndWhRyWQrb5JEx6Or6AP5aUx5Sl XSgl4t7arpbfLq8UDQWALU+glrRc/Nx165AyCWYzqwo1Ys9FtUjjArOLVSd9MUR+lYeu uD9CtQPS6pnhFuskPF6XHc0t30GDw9yp95LSqK+8MwFQ1b8NjivmVgo2gWb2r0f5Byvv GPvw== 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=XDIRBChqlfk/DCrf6Ep9put6x7+MkOaAmB2M/1bJBGg=; fh=EwmF7AOaRUY4Elwzts4Xhy59PU+8YhZ4NsqS/OhL7KI=; b=QlvgaytfZR+bCcV8MP/6w7Nk8r27b+d826G0E5pAFUM5hoqT2HejUn28wTuOfE8Yo6 oMCESxWXTS17M1YhYohWqscISz280dBFv6OIlpZEJjVh6iPMbzcgGeN+pX8TGueLzspP N1wo89keqtaTbAkrG3Ob4u2RV6NPkdPwXDHUjgLsjewL/JOsg3f6HEi0QUcx32lJl731 AHBd6/3z4M9BYBCkKSKTNIzKl2uhnX9I1LBETznafHHjK+LPdGHIrErHyxU2U/cCrnMT WiGUsF9IZGb2q+edZFtSY54oLHGDhsYxClQV6fRfxt+vgufLde5NroQQf28pG0KpKNJU mTxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ryhl.io header.s=fm2 header.b=dlZaLUGR; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=ATQvZS3E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id p12-20020a17090a868c00b00277751629e4si4520066pjn.121.2023.09.26.21.33.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 21:33:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@ryhl.io header.s=fm2 header.b=dlZaLUGR; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=ATQvZS3E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (Postfix) with ESMTP id F2DF780725D4; Tue, 26 Sep 2023 15:11:33 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229736AbjIZWLU (ORCPT + 99 others); Tue, 26 Sep 2023 18:11:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235925AbjIZWF6 (ORCPT ); Tue, 26 Sep 2023 18:05:58 -0400 Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 330EF23108; Tue, 26 Sep 2023 15:05:56 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 500EA5C01AD; Tue, 26 Sep 2023 17:27:47 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Tue, 26 Sep 2023 17:27:47 -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= 1695763667; x=1695850067; bh=XDIRBChqlfk/DCrf6Ep9put6x7+MkOaAmB2 M/1bJBGg=; b=dlZaLUGRzUOqugL8TkI+YnAnX2VMQA7C0U7LcwVOSnUBS8Cr72E DQoHCryWsc98H0IbIZ4U04Ai4hp5g/SjV41blLqHFJYWWACflhiwDhEQXgafiLmv AOEB7YqV0885r7PNkF+3+WkGKyaWwAYxdhdGPHurq2A4d4q5Hn+E0szWN8yd+m3S 3KL/RfLgaQSw+21VgvO64mwdlehzDizhAPy7nFVjGQfWuUhcgSwUYzBzv/7lFg7p NhN3A6quTirUiCAFkWpdhCpSiTifZwLtXJivIZWhxqZ1QmQBrSeZ3xdR0GDt9Vub GOBgMRy8g0Nf3sIpylmWQES3VMdDXjD4SjQ== 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= 1695763667; x=1695850067; bh=XDIRBChqlfk/DCrf6Ep9put6x7+MkOaAmB2 M/1bJBGg=; b=ATQvZS3Eaqd/4vnP+upZ8VK/IVldczOdiSvHwNonuWMftkMCzty +Q4rY2yWBIej9D4Mxn+XlKHFY6jfnJTqYNr+DguEs6T5Q1v59hKTHdUUhCiCJmZZ T2Iyr2Ps+8vqCOX31JtLhsOwMrlhqSqZDIgoYJ+XAJmQRmuJcqmYtUKMK+a+3dCI yn1fe1y3E9K/RVZZjcyTGJplEjPAn37mZCoWwdy8uY+Ozc+tU1P9maGxT0Zdvdiq LU/W/J7aUop+L/WfbXQbuU/2daWYa1NU5yuM3hZB/tbPrU1zI4VGxWYnYZ57jtao aJEzZBcNPA46XxOuUCbosFgEFRdH6dlosSw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvjedrtddtgddufeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvvehfhfgjtgfgsehtjeertddtvdejnecuhfhrohhmpeetlhhi tggvucfthihhlhcuoegrlhhitggvsehrhihhlhdrihhoqeenucggtffrrghtthgvrhhnpe fgffeltddtgfetteeukefgieeufedtkefghffgvdeijedutedvvedvgfdulefffeenucff ohhmrghinhepphhtrhdrrghspdhgvghtrdgrmhenucevlhhushhtvghrufhiiigvpedtne curfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhitggvsehrhihhlhdrihho X-ME-Proxy: Feedback-ID: i56684263:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 26 Sep 2023 17:27:44 -0400 (EDT) Message-ID: Date: Tue, 26 Sep 2023 23:27:42 +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 , Gary Guo , Wedson Almeida Filho , rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , linux-kernel@vger.kernel.org, Wedson Almeida Filho References: <9d6d6c94-5da6-a56d-4e85-fbf8da26a0b0@proton.me> <61ccfb87-54fd-3f1b-105c-253d0350cd56@proton.me> <20230926162659.6555bcdc@gary-lowrisc-laptop> 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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,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 (howler.vger.email [0.0.0.0]); Tue, 26 Sep 2023 15:11:34 -0700 (PDT) On 9/26/23 20:20, Boqun Feng wrote: > 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? Easiest is to wrap the entire thing: struct WithRef(UnsafeCell>); struct ArcInner { refcount: Opaque, data: T, } Alice