From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB4D8C02192 for ; Mon, 3 Feb 2025 09:14:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 51DCD28000E; Mon, 3 Feb 2025 04:14:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4CD28280002; Mon, 3 Feb 2025 04:14:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3947528000E; Mon, 3 Feb 2025 04:14:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 1C132280002 for ; Mon, 3 Feb 2025 04:14:14 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7660882369 for ; Mon, 3 Feb 2025 09:14:11 +0000 (UTC) X-FDA: 83078071902.07.397AEF6 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) by imf28.hostedemail.com (Postfix) with ESMTP id 68E42C000C for ; Mon, 3 Feb 2025 09:14:09 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=y7z8MZdc; spf=pass (imf28.hostedemail.com: domain of aliceryhl@google.com designates 209.85.128.51 as permitted sender) smtp.mailfrom=aliceryhl@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738574049; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gTigYTji7snmwZ76B8KAzY+h5eJthq2lyVwSFYmcfo0=; b=2/fUlH6KB8hlE+/UxyUsqeeLw8fKCja62fWOHrbL5+CjTcd5jv42Wv7MNMVXg92oivA4iI B9BLzt0EWcZueIa9DTPRgTwcbx85WqfNctTA/C3JYwW2TIdYDcAVQUEIkShoE7akTUW1QI 4CzPSpbwxJj1y4PK7s56wmVjlgWKB2E= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738574049; a=rsa-sha256; cv=none; b=TGiG3/+lDVT9OXXExxMAdF8rNvw2BjIRDn4Z4NsatVMIkOSI7wqS2qwIo7W4LGs7Em4NCi hBFsFnt2ksVH4LUhHijiKkrYGhuGeqloAHVAJbjHmGMmOeppDt0iLkkFSP2XTYObu9+te9 f04HoAU1Pf90YNmZJvmqlXhGyEpNZDg= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=y7z8MZdc; spf=pass (imf28.hostedemail.com: domain of aliceryhl@google.com designates 209.85.128.51 as permitted sender) smtp.mailfrom=aliceryhl@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-438a39e659cso27954085e9.2 for ; Mon, 03 Feb 2025 01:14:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738574048; x=1739178848; darn=kvack.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=gTigYTji7snmwZ76B8KAzY+h5eJthq2lyVwSFYmcfo0=; b=y7z8MZdcs2o7s1Mhp4HRe8rjJd4rObbfexxydE1D11rsH9fuaJuOzfWRRWMF7pLzzV me75ucKJBVdFvBM6GrqeZyu0iRYbh/IbElThtnit8WqrAdOT3DQL89iKhmTETcVCjAp5 2r4j7JSJZR/OiNQKgqB+98H2gRXZfXHjnBLx/Pb3bKf5HditHeFY1Z6F1PlUDHdPQsdA J5nsdY1sLJ54oQ3ws8dPitWQWc2EjED/3NRUdTxct21IfmAD4o+ppRmhtujCU8+Rh/QS qOvot100+sseAwo9C4m3/2X/uLDDhC79My1C8FvhHK+S2WG7YBdALsXADB8hRI9/YX5j Zb5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738574048; x=1739178848; 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=gTigYTji7snmwZ76B8KAzY+h5eJthq2lyVwSFYmcfo0=; b=NGBIvfXqoMp4hpIjia+NrP7snFACdfIgIYBH4CeVMg9rPN95tv1pfWsMxoJHmRxJ9f yhNMzG9Xpe4kK6/7rLypUzdtAKE42tXvsjiwHZqQtlqWs5RmPG3yTos7vblOeBCktfSc I1QldAEJ9yuwlozOFWr/Kdym/H4vLMHDtcB1n3eE1OEOdTIM+RKa9hRrhVnlxW+vHAYs 7MWKciPNyLzFPovIhFUnlw+M/4wVuVBmqPcbj8VtxVV9fUSw9o21ZWGmsBPkq8jT1p2z Nbc5viXS8H6nQ4/ebISrrp5LiLcafLv71kQuJQ3AVUblRBqh/CneLFzCJLl8s3fcUsL9 MmMg== X-Forwarded-Encrypted: i=1; AJvYcCVKAK3fuFVItF+AUmDyZ0OCVDjLWjH39zd/Fqa/GLiDXIhfVh7TF4o5bv1/TSUSRzpSdRuUsd9llw==@kvack.org X-Gm-Message-State: AOJu0Yxn7+TYOgI1tQaaleq/9pcqSCx6fxNthOTTW9/Udk0hwHjlNuPq Vg64EJ6IPByounIBgwIBiFS2lIipHfr4iDsh5rvm+4m+3WM6tCAZWEFIP6CzIvNTGzTGzOfAOAS gGVucz7B6cdwKm606mFAWtXNRRzJinfFonmac X-Gm-Gg: ASbGnctuOjQlY5017Ud9Q2LcS9/xMF7kG0lSBv1TAax/LNvvVkGlkmDs+jQSeWbG3Wj iTBIGdezkA8aNhQTqLoRrXOssURgI80JvSjZRCVIFN2uGOzf7Iida9SwpWqSmUdy4e5HMLHRKcS RbQgSrvZs0cu/z7+UnIHifzP1A X-Google-Smtp-Source: AGHT+IHWLWAheWBxDTzneWVhDeDHu4iI6ZSKajtVpaOuk8Iz68fBhZfpC7OkVmaSh79YpfnCMc7hqeCMjDs76NrY250= X-Received: by 2002:a05:6000:2ac:b0:38c:5cd0:ecd4 with SMTP id ffacd0b85a97d-38c5cd0f0f5mr12252107f8f.6.1738574047978; Mon, 03 Feb 2025 01:14:07 -0800 (PST) MIME-Version: 1.0 References: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> <20250202-rust-page-v1-1-e3170d7fe55e@asahilina.net> In-Reply-To: <20250202-rust-page-v1-1-e3170d7fe55e@asahilina.net> From: Alice Ryhl Date: Mon, 3 Feb 2025 10:13:56 +0100 X-Gm-Features: AWEUYZmohLxz7yr6HsBu7wP0vZHEbxTNHW_DFOP7aKlEhrQya1tqMhpQ9Fx3RtI Message-ID: Subject: Re: [PATCH 1/6] rust: types: Add Ownable/Owned types To: Asahi Lina Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Jann Horn , Matthew Wilcox , Paolo Bonzini , Danilo Krummrich , Wedson Almeida Filho , Valentin Obst , Andrew Morton , linux-mm@kvack.org, airlied@redhat.com, Abdiel Janulgue , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 68E42C000C X-Stat-Signature: xwa1a5baqmpazw561fd3pqhihrbrmgfj X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1738574049-601828 X-HE-Meta: U2FsdGVkX1/Pu/HTZmtXpkpbNwFSjeni3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l9WCSuodxPEENtfMTWp0MVJws7HNMTpt3X7Ztb6z7QBr4WFhh28anlgJoiG1DKVgW1AlJ7jIYTxxuy3o57VUOHzxxvqCSyUG3JTbrT0w8ohgYyNoWegaaNk4368C7pzylBpxx9zyVpegsuHSjgcxi/iqw7xYMFs/UlzOZyYBb6eYjkJh9RM1roJFUUW1HfGteIMg2f9oLn6m9hSuwemdbu+L1IJRTsY8lBKLwU= X-Bogosity: Unsure, tests=bogofilter, spamicity=0.500000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Sun, Feb 2, 2025 at 2:06=E2=80=AFPM Asahi Lina wrot= e: > > By analogy to AlwaysRefCounted and ARef, an Ownable type is a (typically > C FFI) type that *may* be owned by Rust, but need not be. Unlike > AlwaysRefCounted, this mechanism expects the reference to be unique > within Rust, and does not allow cloning. > > Conceptually, this is similar to a KBox, except that it delegates > resource management to the T instead of using a generic allocator. > > Signed-off-by: Asahi Lina Overall LGTM. > +/// A subtrait of Ownable that asserts that an `Owned` Rust reference= is not only unique > +/// within Rust and keeps the `T` alive, but also guarantees that the C = code follows the > +/// usual mutable reference requirements. That is, the kernel will never= mutate the > +/// `T` (excluding internal mutability that follows the usual rules) whi= le Rust owns it. > +/// > +/// When this type is implemented for an [`Ownable`] type, it allows `Ow= ned` to be > +/// dereferenced into a &mut T. > +/// > +/// # Safety > +/// > +/// Implementers must ensure that the kernel never mutates the underlyin= g type while > +/// Rust owns it. > +pub unsafe trait OwnableMut: Ownable {} Giving out mutable references allows users to call core::mem::swap on the object. We must require that this is allowed. > +impl Owned { > + /// Creates a new instance of [`Owned`]. > + /// > + /// It takes over ownership of the underlying object. > + /// > + /// # Safety > + /// > + /// Callers must ensure that the underlying object is acquired and c= an be considered owned by > + /// Rust. > + pub(crate) unsafe fn from_raw(ptr: NonNull) -> Self { > + // INVARIANT: The safety requirements guarantee that the new ins= tance now owns the > + // reference. > + Self { > + ptr, > + _p: PhantomData, > + } > + } > + > + /// Consumes the `Owned`, returning a raw pointer. > + /// > + /// This function does not actually relinquish ownership of the obje= ct. > + /// After calling this function, the caller is responsible for owner= ship previously managed > + /// by the `Owned`. > + #[allow(dead_code)] > + pub(crate) fn into_raw(me: Self) -> NonNull { I would just make these methods public, like the ARef ones. Then you can drop the #[allow(dead_code)] annotation. Alice