Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1374239rdh; Mon, 25 Sep 2023 10:41:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHf8OMQcnd1o4eyUVwyw6S76+70y5zHOOpVTx4+lEtBHv2BREbDMCYpNiJpGXEfD9YmQ2U6 X-Received: by 2002:a17:90a:ca86:b0:268:b682:23da with SMTP id y6-20020a17090aca8600b00268b68223damr4976787pjt.34.1695663695233; Mon, 25 Sep 2023 10:41:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695663695; cv=none; d=google.com; s=arc-20160816; b=ry5BLJrNBj/7VJheShtdit/5bJAYWNn/Ejr1k2Zd9blsW9BMwt4giDnDKwN0W/iS0Y CuXMjyva2cZY+3fCLfJ6IlhFzJt+6nVlrf/x9MxMVd22SFyhfUK+aDqlYRlLZS0pEF5B z6V4j2KBB0QEtm4zngx+swQkdatMNeECvC0IFrIRcx6s4EHlz11qq89So+WS615hywj3 wLFfGpA21DrpE/JrTOiLQM6HMWKSHHkrJuPkaWSR6FrUnl28qi3CWl11Qt3SYYGW4Qie kYnyb7C7RCxxaXUwtoTqja0rS0TGJRhraao9/g1KQrvRfQ9ptHxD8PRrnKuVlso8K7nP KzCg== 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=FHZe6kdKIqazFPSKKhdkgcbV19fe0HcjGBTZ7ywLRWA=; fh=NzxGoz2C8fG311LKiw1sBH0veILnB/F87TKwH1iLN4k=; b=b0IltVH1dGpMbl5g7tMt2Tq/yxXO7h6/RZrfrZwQjyTQR6CH7fI9M/eDmctHh5VTNR UUxlmk09VQ++OUuG8qd6L47QQ79FyJCMzQs+4Lpc1oEkEEvBWKkukdPV8PP8DNJsxm1X dC4RZ3u9ytsFCbPVQQnRpQAzNMLeTBCti0PL0HFflvVu4FrADw8REobbqS/QnsOYFi0z /ywJdi2eABVl4H3cMdRx9R3oUmXp2yWRIhJ2L6d1H8PxJgnqaW9HjwxZPPLRKmOukowC 9fUCLWs4M7Uvr6s53KEFedxtRslEX7iOLKrhrZLADZ4/+/765vaNhpFx1YbJrEDk5RVp 3cXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=y7eej5fl4jd6vjgp4a2o7ly5qi.protonmail header.b=a2uK+brs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id mu18-20020a17090b389200b002747da1ef66si11520319pjb.53.2023.09.25.10.41.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 10:41:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=y7eej5fl4jd6vjgp4a2o7ly5qi.protonmail header.b=a2uK+brs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id 885228182DC2; Mon, 25 Sep 2023 08:09:54 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232696AbjIYPJr (ORCPT + 99 others); Mon, 25 Sep 2023 11:09:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232405AbjIYPJp (ORCPT ); Mon, 25 Sep 2023 11:09:45 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B02E710C for ; Mon, 25 Sep 2023 08:09:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=y7eej5fl4jd6vjgp4a2o7ly5qi.protonmail; t=1695654576; x=1695913776; bh=FHZe6kdKIqazFPSKKhdkgcbV19fe0HcjGBTZ7ywLRWA=; 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=a2uK+brsqwOWC0ALmAmVM/RFq1+BFpG5Z6GFcrllZWUa9DLTRMbFO4oRS8TzVEUW5 vj1Cm8b/jq5SbdtapHFsHxLuWHuGp6XuslwziMeiCx1X+BpaptCBhQbIscjwQPRRVD Al3FA+HnOcqsn2kEq3bSc6jAWVth0pjGNSNHhk67EFcn/rp8K7hje9nBbIiTkYmcIk yLQImqY1n660lJge1/5HabzqAiUW+E0TGnLljLzsCv1kNimdTmGdxr+ZzTj3npvCXE xp8L5sRHQqSmi3KnZy6wYeoIGq6kF8N4lk6CMhA27R5cqTa1o0k5aRzUHvcprMpVjp BfDz5mTnuQrvA== Date: Mon, 25 Sep 2023 15:07:44 +0000 To: Boqun Feng From: 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 Subject: Re: [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef` Message-ID: <14513589-cc31-8985-8ff6-a97d2882f593@proton.me> In-Reply-To: References: <20230923144938.219517-1-wedsonaf@gmail.com> <20230923144938.219517-3-wedsonaf@gmail.com> <969eab7f-ad40-0dfb-18b9-6002fc54e12b@proton.me> 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 pete.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 (pete.vger.email [0.0.0.0]); Mon, 25 Sep 2023 08:09:54 -0700 (PDT) On 25.09.23 16:49, Boqun Feng wrote: > On Mon, Sep 25, 2023 at 09:14:50AM +0000, Benno Lossin wrote: >> On 25.09.23 08:29, Alice Ryhl wrote: >>> On Sat, Sep 23, 2023 at 4:50=E2=80=AFPM Wedson Almeida Filho wrote: >>>> >>>> From: Wedson Almeida Filho >>>> >>>> With GATs, we don't need a separate type to represent a borrowed objec= t >>>> with a refcount, we can just use Rust's regular shared borrowing. In >>>> this case, we use `&WithRef` instead of `ArcBorrow<'_, T>`. >>>> >>>> Co-developed-by: Boqun Feng >>>> Signed-off-by: Boqun Feng >>>> Signed-off-by: Wedson Almeida Filho >>>> --- >>>> rust/kernel/sync.rs | 2 +- >>>> rust/kernel/sync/arc.rs | 134 ++++++++++++-------------------------= --- >>>> 2 files changed, 39 insertions(+), 97 deletions(-) >>> >>> I'm concerned about this change, because an `&WithRef` only has >>> immutable permissions for the allocation. No pointer derived from it >>> 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 optimizat= ions. >> >=20 > Not sure I understand the problem here, why do we allow modifying the > value in the Arc if you only have a shared ownership? Also I fail to see > why `ArcBorrow` doesn't have the problem. Maybe I'm missing something > subtle here? Could you provide an example? Sure, here is the problem: ```rust struct MutatingDrop { value: i32, } impl Drop for MutatingDrop { fn drop(&mut self) { self.value =3D 0; } } let arc =3D Arc::new(MutatingDrop { value: 42 }); let wr =3D arc.as_with_ref(); // this creates a shared `&` reference to the= MutatingDrop let arc2: Arc =3D wr.into(); // increments the reference coun= t to 2 drop(arc); // this decrements the reference count to 1 drop(arc2); // this decrements the reference count to 0, so it will drop it ``` When dropping `arc2` it will run the destructor for `MutatingDrop`, which mutates `value`. This is a problem, because the mutable reference supplied was derived from a `&`, that is not allowed in Rust. --=20 Cheers, Benno