Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1507251rdh; Mon, 25 Sep 2023 15:16:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGDoobY2Ckq/8wpESwrFkWof0THN4xr7Fw/Uq/hGOkHzSzIThGocrNlUvmzsmgHsGWTQV4Y X-Received: by 2002:a05:6358:3388:b0:12b:da97:aba6 with SMTP id i8-20020a056358338800b0012bda97aba6mr10263956rwd.24.1695680183801; Mon, 25 Sep 2023 15:16:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695680183; cv=none; d=google.com; s=arc-20160816; b=Dowzsu/dZHsbNdd8qCefVC6U7FkDsw4+qIhRv4k8WK1A068N3gOxxkP2nqoNduSNbe WGILA98B5fU8+COp2sj38mFif8a7Gh5h7XyVBVs1r4gGzIlhUVyeWDXgmMUqQ3FxpNQP sfXx831f8aadAelhlGqfog2WC+cGXzczey3QXXxfCzWn+ryf4G5vZXvehpBybCKG46PQ PPtzCz6Cqo1vPygE9ZJM2qqhT146kSO8tnqQOFpC7mykNyZNh7iFNTyRxg0Rs3Qrsnyf Ju92nxfuRgnlSx7qD4tBOgisnNxHFHQF+QHMXjnT/pMVTAgvUPDeimkD73bFy0tJxFF9 6/dQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :date:dkim-signature; bh=p+y8Uvz3T+M8u/ypH3Z3Elqa3JxcnYzJ7Axg24zZQR8=; fh=O0WSxmRGWoEfh1lOfBQVwDR0DNs/bp8kHvsU40adEAo=; b=NfJSEYTVDvAaBgqcPdS2fyOD86bfh0HOElzCrGn+adKYU1w/MyT8hkEgkVUdd+7MiC I8YLGc+dBE5A3xxZ1hXsuEMdzQpiObrKWYFeuTqO/XYzNm4rxdP6wktG1fYJD+KxsrAh ijz2kS5DkVX4GbC/jfltAs6PQPm0wHJLR7Hzzz7UvCj5/AFbzDYRo3uL9HxXwVsu7V/u rgRaz/VhKg+V2dQ7sUk7qeRh/S1I6E8CZAvZ2CauqsL+J+zE0dOkOVr6FOdMWGE40Mqt Z52GZveuW///BLk4zmMNcL7qhtIzHPyF87ycqxqSn//6VF/duk3M56sxVxtORCByYxA3 vCZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=aI95lrsH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id d19-20020a637353000000b00577f80ddb2esi10912232pgn.705.2023.09.25.15.16.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 15:16:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=aI95lrsH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 1B5F681FADAD; Mon, 25 Sep 2023 09:12:25 -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 S233209AbjIYQMV (ORCPT + 99 others); Mon, 25 Sep 2023 12:12:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233252AbjIYQMT (ORCPT ); Mon, 25 Sep 2023 12:12:19 -0400 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 089ECB6; Mon, 25 Sep 2023 09:12:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1695658328; x=1695917528; bh=p+y8Uvz3T+M8u/ypH3Z3Elqa3JxcnYzJ7Axg24zZQR8=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=aI95lrsHAPKHpHbHX9eL8V8iPAQRjeWonfYapialCoypgs09+B+hOPiZUK5UpFbK3 /FvX1Ps6f/JoByvWINUGMFh4gEby2cPGBXtMrgYbvWVDq8cfPhUPkJ00pdbv4cuP7s Y5q3xJ9T9bbUuTc+kR+ppQb2jpDRsQiiTzBnrfAeKqd8odcBhwOyoWFzUIYVIbHjoz knFKGlvfKkqeRXNL//S2O6aTEHfeuGh6VgOEOv+Zk29294m+hMJmYgAQ5WqWP3KsCz eirjxam8AndTi36sGdDULbOm0w6F00h1+qfbkiXNSXRTskjp+2pCFz+KrYR6FiqTKz 083vbnt+SRKsA== Date: Mon, 25 Sep 2023 16:11:51 +0000 To: Boqun Feng , Alice Ryhl From: Benno Lossin Cc: Alice Ryhl , a.hindborg@samsung.com, alex.gaynor@gmail.com, bjorn3_gh@protonmail.com, gary@garyguo.net, linux-kernel@vger.kernel.org, ojeda@kernel.org, rust-for-linux@vger.kernel.org, walmeida@microsoft.com, wedsonaf@gmail.com Subject: Re: [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef` Message-ID: <88c86434-f9af-6fc7-0fdc-ec1265aed94f@proton.me> In-Reply-To: References: <20230925150047.1961646-1-aliceryhl@google.com> <0b7fc71c-b3c7-4c29-92a9-587daa46ad59@ryhl.io> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,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]); Mon, 25 Sep 2023 09:12:25 -0700 (PDT) On 25.09.23 18:02, Boqun Feng wrote: > On Mon, Sep 25, 2023 at 05:30:05PM +0200, Alice Ryhl wrote: >> On 9/25/23 17:17, Boqun Feng wrote: >>> On Mon, Sep 25, 2023 at 03:00:47PM +0000, Alice Ryhl wrote: >>>>>>> I'm concerned about this change, because an `&WithRef` only has >>>>>>> immutable permissions for the allocation. No pointer derived from i= t >>>>>>> may be used to modify the value in the Arc, however, the drop >>>>>>> implementation of Arc will do exactly that. >>>>>> >>>>>> That is indeed a problem. We could put the value in an `UnsafeCell`,= but >>>>>> that would lose us niche optimizations and probably also other optim= izations. >>>>>> >>>>> >>>>> Not sure I understand the problem here, why do we allow modifying the >>>>> value in the Arc if you only have a shared ownership? >>>> >>>> Well, usually it's when you have exclusive access even though the valu= e >>>> is in an `Arc`. >>>> >>>> The main example of this is the destructor of the `Arc`. When the last >>>> refcount drops to zero, this gives you exclusive access. This lets you >>>> run the destructor. The destructor requires mutable access. >>>> >>>> Another example would be converting the `Arc` back into an `UniqueArc` >>>> by checking that the refcount is 1. Once you have a `UniqueArc`, you c= an >>>> use it to mutate the inner value. >>>> >>>> Finally, there are methods like `Arc::get_mut_unchecked`, where you >>>> unsafely assert that nobody else is using the value while you are >>>> modifying it. We don't have that in our version of `Arc` right now, bu= t >>>> we might want to add it later. >>>> >>> >>> Hmm.. but the only way to get an `Arc` from `&WithRef` is >>> >>> =09impl From<&WithRef> for Arc { >>> =09 ... >>> =09} >>> >>> , and we clone `Arc` in the that function (i.e. copying the raw >>> pointer), so we are still good? >>> >> >> No, the raw pointer in the Arc was created from the immutable reference,= so >> the raw pointer has the same restrictions as the immutable reference did= . >> >=20 > I see, this was the part I was missing. Thanks! >=20 > Looks like the only fix is replacing `&WithRef` with > `&UnsafeCell>`? But that's a bit wordy and I'm not sure > whether it's better than `ArcBorrow<'_, T>`... It should be sufficient to change only the type of the field within `WithRef`, so no visible API change needed. -- Cheers, Benno