From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (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 67F0646D560 for ; Fri, 11 Sep 2026 09:20:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.71 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789118405; cv=none; b=N72Q/3uxmlBpBlYs4SgmoWuSVVpEVl8gIWOxVL1HXoO6df9/mXox9rsGMyG4hQ4oTOVJ6tW7X8m2whBbnnblJmF2feVy0maTNw7CItK7JDzsPP7ETX7HNPQ6R7ncOBSTdJbc9k21WtPZtt4wGCiXyl2S4n85N6sdFsz3L20VDRg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789118405; c=relaxed/simple; bh=LQHKUmm2L3VPczAzSrySIOYelc6UVvj0GiEVXj92Iww=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=p7gdnNyoTIFoQA4/hAudhuffSOopHFnRLYvzN4HT0s0Y2V9l2M3OmQhHU6FratQ0NDgeXZ3hZpKBdpRxFLHk03Xc7agTf+SqQp6MZJhKF82zCB62YxwkqXKLLOQNkFDcoUu/fzEcaiTQpKIgZPieuy8PR2JTHdOZE+Re9r/dsfc= 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=WdF9tVpS; arc=none smtp.client-ip=209.85.128.71 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="WdF9tVpS" Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-49d0ae342b9so5315905e9.1 for ; Fri, 11 Sep 2026 02:20:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789118400; x=1789723200; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=NZtTwWLqkTlApSCCjwtFSfuFmwluELYL36xqCuH39yY=; b=WdF9tVpSSXYvnlrzGgh+P+QcLj5u0AX4p8filA66XSzd5O8vBXYmPSBizgExj6oxwL +PmBeHz4t6HSK7AV7IgXCxiDARpO9PyLesXeDRV74ILIaJiw8pgjUKDWW99gqaSTg9PR mypblrub7AceAOqE4EVBGXzbeqINNAIlUOhB6RTkioOFhpNWHZWVFRYw2MFH6eoBVSqn omL+h/BD6+gLLshheuEdkv1T7XJHsKCulUf7JaARnJbYBE4a2oLTZas0g4C+aduyAhZd u2zFNt+Tr5E5wlpz1iwT5lvMsDx57TFBz7QmSnYzDX6HPEh73EvRU/eyhaLMvqmIYSsr GkDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789118400; x=1789723200; h=content-type: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:content-type; bh=NZtTwWLqkTlApSCCjwtFSfuFmwluELYL36xqCuH39yY=; b=A76CpsZfmW/Gc1Wdx2Tt7DkVbPTCsdS0h+s/wAjU2wfonti3Scg9PGmncjwj/z3UJe 5TWU33nhP2Q4UJ5XwxYbK0r5pN4H2Tz6AZB8eO+G+NGCnJPZSA3lwT7+l2k0XqTxYA4A mIbdy1EtVqdd6TDW5rDcoTc2HPa8oZbeZGdk01ppNQtjtlBGYdt/X3OZKD1Oeu3IH7JM wzuvO7NqjT4MPSN3Dvpvg7RwQN3XmvMQcnGwkMZVIttzBxwx44wAjc1cdM3Wv8L4PiDY 0jiH3arFMqgnMJJS44lFOS7WAuIBGt2mKKsgde1+KIeFQls2pX/AgJKz0C+nFM7FH25r VApg== X-Forwarded-Encrypted: i=1; AKwUvBxViE6Z3ZOJ0cQXypSbUv1zc+3oZR7UMkMIpnwwxHg/XpHF4Vn3fvjdiSgk72AxhDvg5jqS4YEWDd6XClsU@vger.kernel.org X-Gm-Message-State: AFuF++lHLfHJEzeDH7DC/AML3FJvB0Nndp+0biWUfV/bExjIiAc7iMYm xMrUFhzjkzgurOnwvqz0PhjqZ7Syga1GTXzHHsDmIzh1D+eE2nFVxbpatOwm4/8+ONlgwgttae9 sqWqdYi3xOGjcfSPmEg== X-Received: from wmga6.prod.google.com ([2002:a05:600c:2d46:b0:499:b8f0:3186]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600d:8490:20b0:49d:e0c:e560 with SMTP id 5b1f17b1804b1-49e61983771mr27226365e9.4.1789118400242; Fri, 11 Sep 2026 02:20:00 -0700 (PDT) Date: Fri, 11 Sep 2026 09:19:58 +0000 In-Reply-To: <20260910-unique-ref-v21-6-e83257373062@kernel.org> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260910-unique-ref-v21-0-e83257373062@kernel.org> <20260910-unique-ref-v21-6-e83257373062@kernel.org> Message-ID: Subject: Re: [PATCH v21 6/9] rust: Add `OwnableRefCounted` From: Alice Ryhl To: Andreas Hindborg Cc: Danilo Krummrich , Lorenzo Stoakes , Vlastimil Babka , "Liam R. Howlett" , Uladzislau Rezki , Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?B?QmrDtnJu?= Roy Baron" , Benno Lossin , Trevor Gross , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , "Onur =?utf-8?B?w5Z6a2Fu?=" , Lyude Paul , Greg Kroah-Hartman , "Arve =?utf-8?B?SGrDuG5uZXbDpWc=?=" , Todd Kjos , Christian Brauner , Carlos Llamas , "Rafael J. Wysocki" , Dave Ertman , Leon Romanovsky , Paul Moore , Serge Hallyn , David Airlie , Simona Vetter , Alexander Viro , Jan Kara , Igor Korotin , Viresh Kumar , Nishanth Menon , Stephen Boyd , Bjorn Helgaas , "Krzysztof =?utf-8?Q?Wilczy=C5=84ski?=" , Pavel Tikhomirov , Michal Wilczynski , Ira Weiny , Matthew Brost , "Thomas =?utf-8?Q?Hellstr=C3=B6m?=" , Philipp Stanner , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, driver-core@lists.linux.dev, linux-block@vger.kernel.org, linux-security-module@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-fsdevel@vger.kernel.org, linux-pm@vger.kernel.org, linux-pci@vger.kernel.org, linux-pwm@vger.kernel.org, linux-usb@vger.kernel.org, Oliver Mangold Content-Type: text/plain; charset="utf-8" On Thu, Sep 10, 2026 at 11:00:10AM +0200, Andreas Hindborg wrote: > From: Oliver Mangold > > Types implementing one of these traits can safely convert between an > `ARef` and an `Owned`. > > This is useful for types which generally are accessed through an `ARef` > but have methods which can only safely be called when the reference is > unique, like e.g. `block::mq::Request::end_ok()`. > > Signed-off-by: Oliver Mangold > [ Andreas: Fix formatting, update documentation, fix error handling in > examples. ] > Assisted-by: LLM > Co-developed-by: Andreas Hindborg > Signed-off-by: Andreas Hindborg > /// Types that specify their own way of performing allocation and destruction. Typically, this trait > /// is implemented on types from the C side. > /// > -/// Implementing this trait allows types to be referenced via the [`Owned`] pointer type. This > -/// is useful when it is desirable to tie the lifetime of the reference to an owned object, rather > -/// than pass around a bare reference. [`Ownable`] types can define custom drop logic that is > -/// executed when the owned reference [`Owned`] pointing to the object is dropped. > +/// Implementing this trait allows types to be referenced via the [`Owned`] pointer type. > +/// - This is useful when it is desirable to tie the lifetime of an object reference to an owned > +/// object, rather than pass around a bare reference. > +/// - [`Ownable`] types can define custom drop logic that is executed when the owned reference > +/// of type [`Owned<_>`] pointing to the object is dropped. This diff looks like it should be in the patch introducing Ownable. > +/// # #![expect(clippy::disallowed_names)] > +/// # use core::ptr::NonNull; > +/// # use kernel::alloc::{flags, kbox::KBox, AllocError}; > +/// # use kernel::sync::aref::{ARef, RefCounted}; > +/// # use kernel::sync::atomic::Acquire; > +/// # use kernel::sync::Refcount; > +/// # use kernel::types::{Owned, Ownable, OwnableRefCounted}; > +/// This newline will appear weirdly in generated docs because no content is shown above it. Consider not hiding the imports. > +pub trait OwnableRefCounted: RefCounted + Ownable + Sized { > + /// Checks if the [`ARef`] is unique and converts it to an [`Owned`] if that is the case. > + /// Otherwise it returns again an [`ARef`] to the same underlying object. > + fn try_from_shared(this: ARef) -> Result, ARef>; > + > + /// Converts the [`Owned`] into an [`ARef`]. > + fn into_shared(this: Owned) -> ARef; > +} This trait provides two methods that have very different conditions for existing, IMO. The into_shared() method is so likely to exist, that I would almost consider placing it on the Ownable trait. pub trait Ownable { unsafe fn release(this: NonNull); fn into_shared(this: Owned) -> ARef where Self: Refcounted; } On the other hand, I think try_from_shared() is more likely to be a tricky operation you cannot necessarily implement, and as such it can be on its own separate trait like you have here. At the very least, I can easily imagine that some types can never become unique again after having been shared. Alice