Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp271215lql; Mon, 11 Mar 2024 02:04:18 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWXKE9OT51OQCRcPzhLXsjYD9kCqu/H/ZAxw9+h07bjIr0sb/qwFHhPWE/kX+BAD1NoSEv6ZAwGScGwE0VVmMPmYu96aiPKxoVmC/3weQ== X-Google-Smtp-Source: AGHT+IGp8FWc8seh2GkNPTNP4fDLe5OKop/3RrEFM3OZIqPfnqaVW1XfFzT+Wg+OgNXzh02lALUT X-Received: by 2002:a05:6402:35cc:b0:566:6b3e:c965 with SMTP id z12-20020a05640235cc00b005666b3ec965mr3135635edc.4.1710147858687; Mon, 11 Mar 2024 02:04:18 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710147858; cv=pass; d=google.com; s=arc-20160816; b=L+TTincuBbg8AqL49wm5iJYSrn16KjjSluXaui/D+XTZrqzo81ItAzgIwpzA5+BhHO cvrzkRwnjG6iqCLW1EPG0plpC023SdMAK3K6mTIBsD7wXzbzZMTZ8UsdZH8T+w0rYRaO kwA2guZ/YIBCYCN0UPLxyi1eglXuGjm/lmmTm6iz1GJas3Lj0wekR+AYsLzHSZZAxVgs jjdkSGejYsRUeIMXzt6Pl97WV6h3k/7vQ66XDW1SlO/VnrrhQz/G6fQBgGFGQiqp2d5F zN3Nj3TlOABrTVLwq6FgoCoI4OLLNBwe6P6ST7pw+gDS0Zb2pKBKCG+/9Di4TaCuE8yz MCOg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=B6BaIj82ne96lSVNCUMkWu0rka7gtZJhD+DKobLwFLE=; fh=U7qwZo4djjE8bfpL/FoDVQ8XvHPFb2nhROtMBwvzXK0=; b=urSEDS8kBR9K3kMOK9uLE9xKei+iLZlo2cch66ZD0a2hN4MwhD/2LtYKvV84sgGe4q Tfum424/Gqiu3NyC/mFuwFXDd3+KZSZSjCA9XBkkIp0UGpwvDGg/koU4PWwQT/SMF6HP DyTwXwyuPgs4oz3W3HVCA3KFP4kkVrVwrTBrCq16D2KxmXvzB8T/ayGzIi4wGfatSq9F 2i6xuT4UjiHOo0RaFt7wccldXRbLXRpKbj9soliLvRdmgjv3/8T7crpEXkUQ0+N+TGDF 7DQMYbPTfgU23KU4N4+6Yjbu8tsM7oJRd6PeEewXh8JOHZY1XzWArjnkYGebpPU8bajF q41g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=QOxwuDhu; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-98574-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-98574-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id b16-20020a0564021f1000b00568601bbc0esi575421edb.419.2024.03.11.02.04.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 02:04:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-98574-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=QOxwuDhu; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-98574-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-98574-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 40E321F210BB for ; Mon, 11 Mar 2024 09:04:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 87C3317999; Mon, 11 Mar 2024 09:04:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QOxwuDhu" Received: from mail-ua1-f45.google.com (mail-ua1-f45.google.com [209.85.222.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 08D91171D8 for ; Mon, 11 Mar 2024 09:04:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710147848; cv=none; b=dQ2qTpxXCA2jlnt9vHJm0No2Ntpf1ip0/uwcoBYZWrzH1vynYnX10UprHwXeAmAU7Qsyx0CJ1Y2rf9Y1tcy5YqxPT5I1J+8yd7/3R2VmkSnwf7nSqe0npchR8TTTzaq2U9pcLdM9m0p+HtTXbXBkjJ7i/h7htrS4bRiuIUwtpWk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710147848; c=relaxed/simple; bh=YZIvk45Qdwe+WgoeWiUCSU3e5i9+mC93jSqXbug2wgc=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=jhGnX4qpEbbo+27DBZrwXER2TM+7P+9hhPBXMEvQWn+xmC/0YrvxEYN6zrYN90L+lE3fmvGTk30xS/WBE0fUmhlibhotXV6qcA0aVfS1mU6ELw0sfA20fc07PLyD09RuWki2hRX264Hc6ss2WQNLh6Sl2LExP6OlRCXalgAf4WE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QOxwuDhu; arc=none smtp.client-ip=209.85.222.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-ua1-f45.google.com with SMTP id a1e0cc1a2514c-7ce4512d308so2444933241.1 for ; Mon, 11 Mar 2024 02:04:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710147846; x=1710752646; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=B6BaIj82ne96lSVNCUMkWu0rka7gtZJhD+DKobLwFLE=; b=QOxwuDhucLDB8NXZdhw0Zh9iLejJHSoZGB98Zz7gAr/FsCaE5zCPCGchhebiazyx4l HQ+AGSSUVHw2igVW8Pj+P4ZbM4GTVyvMgd16cmEmlpLvIioTSJ003CrntGkcOWdGMaCo gJ7Tt45wulVxHrvF4PT92F41kiz9YRKu1/dVWPImSfJdwZ9YVrf+aJ17m4Kc99SQsAJZ ZS/f28mPg5uxG4igMYVfKtaou0v6FjPvf6jEimnZV2mZXLAGs6RrzVx+H+QL8OW2Drne QRxjC9QRDZp76KGA6F05MQQYoN69YMEHfyLBCB6mQiYPyBthCGQBMIRSeWz7n+dBu5hO seig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710147846; x=1710752646; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=B6BaIj82ne96lSVNCUMkWu0rka7gtZJhD+DKobLwFLE=; b=VShxuTMZCJo9S+S8ZCep3thG6JJKPj9Dn6zE+cwpJ4GzX99TzSPMEqEEnAtdtOZVba J5YAqA7qSjwy/wfeKg3A/KspFjY8tFkNTotIP0N1RwRpwmi81fjw7WuAed1wOmDnIXnR WIEI0yIxgbAdrEc+OXNP2CnsPeiOypOdLRAL1H+jmpYSccoTMqO9HG/1Zkkqk49Hg6Lg WG5GWYCjBOTVVUHuq99CM2P83AF052Lvk+izRwSG4Ev9swt06jt3nN/BeHrfIG8HkDA8 Ik36FJMvUZj5EDiAVcUJb20OXjWtXLN8W+XE0uzhkc6UvtkwkQ2LnhhNIO3hGhzMNDrt t2cw== X-Forwarded-Encrypted: i=1; AJvYcCWgZC7VZm3q8SwumvZP7Tp7F3yRG5OIYWhxHb5Qp4HxluzeM4I2N5Y/+HFqDjAnGXycivhmYu3n2hcgi1aKXvvnNYB9y4E+3YxC1eqW X-Gm-Message-State: AOJu0Yxfsvc5NJNe2tF0L8IX8nk5vnFlb9/QnA6ldTAUwbDFpCSxftHB COmPxQov/SYWG2dMoH0/zxiLY9TMqozBuOpYmqn+nj8ySkzZkGe5kZgbH4j6VfdR2vJFNBB3lA7 ArXM599z1ymI0AvaSa/Z+yJJOY10SPm1JkdTy X-Received: by 2002:a67:f607:0:b0:472:65bf:9f14 with SMTP id k7-20020a67f607000000b0047265bf9f14mr4320102vso.5.1710147845811; Mon, 11 Mar 2024 02:04:05 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240228-arc-for-list-v2-0-ae93201426b4@google.com> <20240228-arc-for-list-v2-2-ae93201426b4@google.com> In-Reply-To: From: Alice Ryhl Date: Mon, 11 Mar 2024 10:03:54 +0100 Message-ID: Subject: Re: [PATCH v2 2/2] rust: sync: add `Arc::into_unique_or_drop` To: Benno Lossin Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, Mar 9, 2024 at 2:02=E2=80=AFPM Benno Lossin wrote: > > On 2/28/24 14:00, Alice Ryhl wrote: > > + // SAFETY: If the refcount reaches a non-zero value, then we h= ave destroyed this `Arc` and > > + // will return without further touching the `Arc`. If the refc= ount reaches zero, then there > > + // are no other arcs, and we can create a `UniqueArc`. > > This comment is not explaining why it is safe to call > `refcount_dec_and_test` on `refcount`. > It dose however explain what you are going to do, so please keep it, but > not as a SAFETY comment. I'll reword. > > + let is_zero =3D unsafe { bindings::refcount_dec_and_test(refco= unt) }; > > + if is_zero { > > + // SAFETY: We have exclusive access to the arc, so we can = perform unsynchronized > > + // accesses to the refcount. > > + unsafe { core::ptr::write(refcount, bindings::REFCOUNT_INI= T(1)) }; > > + > > + // SAFETY: We own one refcount, so we can create a `Unique= Arc`. It needs to be pinned, > > + // since an `Arc` is pinned. > > The `unsafe` block is only needed due to the `new_unchecked` call, which > you could avoid by using `.into()`. The `SAFETY` should also be an > `INVARIANT` comment instead. > > > + unsafe { > > + Some(Pin::new_unchecked(UniqueArc { > > + inner: Arc::from_inner(me.ptr), > > + })) > > + } The from_inner method is also unsafe. I think that using new_unchecked here makes more sense. That method is usually used in the case where something is already pinned, whereas into() is usually used to pin something that was not previously pinned. Alice