From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 112ED2080EE for ; Thu, 6 Mar 2025 09:35:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741253759; cv=none; b=K7Gbf6OMKBAjEr4yiejUg6RsBRu+dtIr0CX0JNCkwfhtv7PinwwMlkA//1s/S2dQmBjTfyP45TJkld1T+OKXUwbGOnNC96YPs1DSpPhxl4JK66g7ESq3sqOdbRZSkrkabQOp32y6Rjfob+WlSlz7vYVPhOdC1nPhJVWBRjKXKrQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741253759; c=relaxed/simple; bh=zAfupfnmXgAPKBf7N+VUINqI3bvcjO+DlUy81THOReY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mx+UC+Mvl4G1ejRd3Qdd7McOMLyJ6MAaGzWkDb+b88r2ymJY/YdfSpXxHyW4JRx0rV6ikyZbpQCioKnL4vhwpEQiyQlyQdNwIUSd/yh+a6BOfdCdcjHBQvchsNGApVCh3RY92Njs/EWB8K3a8QH2wJz4iU0MfxlLLS6tqhezmDA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RZ6hLYq6; arc=none smtp.client-ip=209.85.128.73 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=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RZ6hLYq6" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4393e89e910so1906805e9.0 for ; Thu, 06 Mar 2025 01:35:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741253756; x=1741858556; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=80/MuYHXzFOlY04/5DfNbTTbOenEqnxQ83fIcfIWstI=; b=RZ6hLYq6FQnB3CTlZ+3pQYLBpPUkVcwBgmSmq8fiMCuyxHif/0+sGbcS+ZamZIyryQ xtzQwckV1kD8BVGwH4P3UuMQdb/Ik3Y6buuEaYmkSIrPAasHPBdv+1MW8U0+6pQrFust rmdNYQJBXiU2nn/EWFPSg0YrGAZUOuenLkJfYKEIQ3GbCt6rnEYtagR2UGA7Vwy3NMbf 5j3EYrIGa2FkJi+epdyRFi0ZMyMWmHnIzoXpUE1LNIrF5hyo0PsgcYqCU3ouqAWgeNiW PIP5YUjd9BuZ4uPqphQV1RgC4/xOxH9301u0epVjnxqKjNDHSzTfxeD/e9Az3+5Z6eGg AbkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741253756; x=1741858556; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=80/MuYHXzFOlY04/5DfNbTTbOenEqnxQ83fIcfIWstI=; b=wPbkd+TV+JZSI8mosXqxCk+9SFCROQvQbDSD+Fra5ZLNcYqSUYg0v2ZZfa82gPzBL5 0+hcvh11u6pce4BHIlAVmn9s3eA9tm6fUFywgrVpbEejUCEXuyObBXpBsmWDV2/vRwZs PShv8mmywmsDgCa1NgL5g6fvOAWtbxEJ2hZN5WzAGO0Yke31BwKeudbMt8nBDjh0xyan IGCX/8JWQzfC7f4oGj2gVwQgcQ3DZFB6ntVesFWTc2aKthHiwbwSDAw4muGZxTOdefEo WdpMa25gVENP0f64Dob+pxuoE44Gnei8xCMXlWLZfJh+7+PvcOOU+7xKOCRi8k0K5igF 1fZA== X-Forwarded-Encrypted: i=1; AJvYcCVYttG77zsVci7uW9rgLhgaYThu3/JFZOYW2aW43tEdskeZEdSd9XP/EZ0QRtHoG9/C2W1rOmbZvZU9LWCrug==@vger.kernel.org X-Gm-Message-State: AOJu0YyyKP78NkU+Vab812ReqHe0wWsTg+yb6XkSd5McXtlnTYfHdMor aq+DiHRk2tP4eCWcFg0ROexKt+rDYnYaAOreFQwAIo/tDVl62uZlwEYssMFVMIDb7veYSGusl6a D1i9YFsXyoQULMg== X-Google-Smtp-Source: AGHT+IFxl8xciWrl2CQHn8RoJKN3DeV4Zim2lKi3GQnZzsbzv7UugqHqo9gKtYFc9LBV4rU4WNoxZYH203ol5XE= X-Received: from wmbek10.prod.google.com ([2002:a05:600c:3eca:b0:43b:c450:ea70]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4ed2:b0:439:a5e6:73ff with SMTP id 5b1f17b1804b1-43bd29d8441mr46790355e9.17.1741253756313; Thu, 06 Mar 2025 01:35:56 -0800 (PST) Date: Thu, 6 Mar 2025 09:35:54 +0000 In-Reply-To: <875xkn8k5z.fsf@kernel.org> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250305-unique-ref-v4-1-a8fdef7b1c2c@pm.me> <87ldtj8p2m.fsf@kernel.org> <875xkn8k5z.fsf@kernel.org> Message-ID: Subject: Re: [PATCH v4] rust: adding UniqueRefCounted and UniqueRef types From: Alice Ryhl To: Andreas Hindborg Cc: Oliver Mangold , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?B?QmrDtnJu?= Roy Baron" , Benno Lossin , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Wed, Mar 05, 2025 at 06:24:56PM +0100, Andreas Hindborg wrote: > "Alice Ryhl" writes: >=20 > > On Wed, Mar 5, 2025 at 4:39=E2=80=AFPM Andreas Hindborg wrote: > >> > >> "Alice Ryhl" writes: > >> > >> > On Wed, Mar 5, 2025 at 3:56=E2=80=AFPM Oliver Mangold wrote: > >> >> > >> >> Hi Alice, > >> >> > >> >> On 250305 1339, Alice Ryhl wrote: > >> >> > On Wed, Mar 05, 2025 at 11:31:44AM +0000, Oliver Mangold wrote: > >> >> > > >> >> > > +impl Deref for UniqueRef { > >> >> > > + type Target =3D T; > >> >> > > + > >> >> > > + fn deref(&self) -> &Self::Target { > >> >> > > + // SAFETY: The type invariants guarantee that the obje= ct is valid. > >> >> > > + unsafe { self.ptr.as_ref() } > >> >> > > + } > >> >> > > +} > >> >> > > >> >> > What stops people from doing this? > >> >> > > >> >> > let my_unique: UniqueRef =3D ...; > >> >> > let my_ref: &T =3D &*my_unique; > >> >> > let my_shared: ARef =3D ARef::from(my_ref); > >> >> > > >> >> > Now it is no longer unique. > >> >> > > >> >> Oh, indeed. That's a serious problem. I see 2 options to deal with = that: > >> >> > >> >> 1. remove ARef::From<&T> > >> >> > >> >> I checked the users of this, and it looks to me like there is rathe= r > >> >> a limited number and they are easy to fix by replacing the &T with = ARef. > >> >> But I assume that wouldn't be welcome as it is intrusive nonetheles= s > >> >> and of course there is ergonomic value in having the function aroun= d. > >> > > >> > Definitely not an option. There are many users of this function that > >> > are in the process of being upstreamed. The ability to go &T -> > >> > ARef is pretty fundamental for ARef. > >> > >> Not having `impl From<&T> for UniqueArc` seems to work out fine. > >> > >> It would be unfortunate if `impl From<&T> for ARef` would prevent u= s > >> from having a unique version of `ARef`. I would say that is a valid > >> reason to consider removing that impl. > > > > I think the impl is really important. It's required to do things such a= s: > > > > let mm =3D ARef::from(&*current!().mm()); > > > > Without the impl (or something equivalent), it's not possible to > > increment the refcount of the &Mm returned by `current!().mm()`. There > > are many other examples of this. >=20 > Right. Let's see what we can figure out of other solutions then. Ultimately, if a struct implements AlwaysRefcounted, then you can always increments its refcount. If you want a version of the struct where that is not the case, then you need a different struct that does *not* implement AlwaysRefcounted. I do things like that in the mm_struct series. The VmaNew struct is an example of that. Alice