From patchwork Sun Feb 2 13:05:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 13956519 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 0324FC02193 for ; Sun, 2 Feb 2025 13:06:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 065676B0085; Sun, 2 Feb 2025 08:06:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 014946B0088; Sun, 2 Feb 2025 08:06:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA8A86B0089; Sun, 2 Feb 2025 08:06:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id BD6586B0085 for ; Sun, 2 Feb 2025 08:06:45 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 5C3E381F92 for ; Sun, 2 Feb 2025 13:06:45 +0000 (UTC) X-FDA: 83075029170.21.3009D4D Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by imf20.hostedemail.com (Postfix) with ESMTP id 6B0831C000F for ; Sun, 2 Feb 2025 13:06:43 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=asahilina.net header.s=default header.b=j13+77B4; dmarc=pass (policy=quarantine) header.from=asahilina.net; spf=pass (imf20.hostedemail.com: domain of lina@asahilina.net designates 212.63.210.85 as permitted sender) smtp.mailfrom=lina@asahilina.net ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738501603; a=rsa-sha256; cv=none; b=W0V+SEI/LtWga4ikQQKVBxJ1byGdIcfhSzrTGfmYd1Ja1OQX7JhctKjGPaJaxd4tIbBcx6 roDxss6riN9Cc0EYOvew7gLnms4NS1RFj0dlFy8xMqDunZaNn7bkK2V0P/ui/T4RmWorw6 33hy8cUtwza+ibTnGhmVQ1HrkGT+9Bk= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=asahilina.net header.s=default header.b=j13+77B4; dmarc=pass (policy=quarantine) header.from=asahilina.net; spf=pass (imf20.hostedemail.com: domain of lina@asahilina.net designates 212.63.210.85 as permitted sender) smtp.mailfrom=lina@asahilina.net ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738501603; 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=PVqe0RlXqOVlirCUFA1CMDAUAAVS3t+d1iTU1+IF6fk=; b=qjMLRaoildy5KVSfFKF74TfD3X0Xq50WbHpxBppV9+3l/YfgBc6m0QOhcqCIEdjK69EEoi yZib123O3c+Tpjs//RrlfmXeZY3VGkS884jUiJwbeAanrGOtSK4Itc9Xx7IvE8bVjR3MZv Pi3lM8vv+1tcPoGlyUJ2NjX7i1VxfaQ= Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sendonly@marcansoft.com) by mail.marcansoft.com (Postfix) with ESMTPSA id 0AE3C4269B; Sun, 2 Feb 2025 13:06:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1738501601; bh=i4Ab3HjgvvGhPot4YFk4jEpbbo5OwUVep0fKUcItVqY=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=j13+77B4uDEiUiAv4VmriWW3KBa28+PJ+rGSbk9sG6oe+/Vc9YDMzmV7eNxU4Ovbe fcZvQetCX39EHEG00wlsexJZddtS+9Q0eYfuh+ZpU/Itzhea8VcX+R07bC5utcvT7v UL096wFWUozQwwAur7vKnZvzJ69/LuSXUmiP514WLvkNQb1OXmuodgNTxNI3s/WEjc QBbr6bR2qpm7Kpe92vLmd24v4QKLjhjKs8O/Xjobmulm5S/oEk9r7ipJKAoSLcAmq7 0ek/0u6XcL/YLJ1hpUkz3zcAgRBVmKPlWUB9p9RA2cO9tz98sdjH0jBx+6Yvkz2U6m 9FiA+JGRbSyow== From: Asahi Lina Date: Sun, 02 Feb 2025 22:05:44 +0900 Subject: [PATCH 2/6] rust: page: Convert to Ownable MIME-Version: 1.0 Message-Id: <20250202-rust-page-v1-2-e3170d7fe55e@asahilina.net> References: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> In-Reply-To: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Cc: 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, Asahi Lina X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738501594; l=3247; i=lina@asahilina.net; s=20240902; h=from:subject:message-id; bh=i4Ab3HjgvvGhPot4YFk4jEpbbo5OwUVep0fKUcItVqY=; b=gUHcqq+q5mqcWHe6iq9f1yxIulhbcJU+/xJBVOGRrtI5rZfJKz3keo8IzgHn3owhyqq53MKvh /PP7nIbYuJVA+cau3fDKRYqaejphd+pg0QPqP6nq+mCd5L8wHvID/MN X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=tpv7cWfUnHNw5jwf6h4t0gGgglt3/xcwlfs0+A/uUu8= X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 6B0831C000F X-Stat-Signature: tf5cckpz7w74dydgw47dxm9mo9pnd8fq X-Rspam-User: X-HE-Tag: 1738501603-566730 X-HE-Meta: U2FsdGVkX18DMoIrGxqaFvkq2B4Ym5PpWd6VgRHJvpfCGBnHujc2CZuE1B5B9RJZaFj+fHnplt8oNH5Y9VP0/hak/7sOTKO9TgPkpOI3RHHqB6N7UlKUXWbsiPWRYk0YEp6vKMmBDhcL6pEcHaaMJ2kblJSKxM3A6skfjof3VmmJWx++iycrvj9R5boDJXdgKYKrbhqwQ7RBfp+nmNKjOtTVGenFE8VWfOvgSr8RtV3j7jNvtm2buXXmJbuQh5p24m52CaNGiCFaNlMeRuZwuAhwWeyIRnD5lhV6Kj9Bsapg6rKwRXlkveDKJ1OIVaVMPnWOV5Ww7dzIrmFLbIWub8jAnIaP69DiqHAdu/mLAPrhp4fQ08ccY7BI3q4CAGCRRQp9iB0oAzB2lDCOIQ+5SnRhLOCsCMM8HDFlsW0YfCCPMu5KFmeG7wjO8GbrtH1UDShpvj+ElDJDZBEejh4msEjoZG+IZelG6Bl5A7s4RA0CfZk0yMN7mvJ2IR5mdz3TR2lFBDAS4Gvq8vdNLU6B8Tby3K93S9691FVx6ojSm5e1SWEPBQAvSlRZRhEfMcGDIsc5ZKw1EHSVO+E4oEPu5QXABmKbZpm3VPaIAB8CwKZaRAozDehJG/HYKNLFqEJ9xJiAsYTlvhYxoQNX38P+F8/CyNemNjVofLw3GjIaUl8vf8gS0VJnNfbR8ofHpSAV49c63TvZuWeDgGrepi8Wz1Pp29ALR1qiy0V67SJatKZ0O2YU7jKYJibrzG90uLDBSj08tNS2aFTep2zblHKGtI68E1zA79D+JE/l5oEpgZkzLv2VktVmLMAFN8TAm6fbgW3GrsCI7uesYwulZEAwu0UJgMscHYHeIT6wco0I4PeK9EIiGiYK6oEVOgox8QaIZkODpRn1b3YYtVsef7WaqbjX8SVAEFi9Rl6rzpZt/BSGcQZs4uqjijetjnYe0hnY6WQNr9TttU7Rclg6jo/ eh2UPutc qms0b4YC8TDYGh/LIfr1GgSGDxSws6HkkZ6UpLSHToV7JbArhh2d8R7wf7IHp276awY94FT9Ie3G4R+w9K1vS/vUISd9zTmORHbUv+NQmAUSf2cLddnmWEnXQkvLorHm0gFYqjxHE4tITfbclohEbt7zpOAPYUVwrTL7iUhf9O0nh0hkB4ByrwF5fhGA4NNxaJfzJLjK+CPQjF06pQ+9QQCImSZWRcnzc35+9YLF75jVi6BzYtMLFa1gkJy7G8CFCK5CRsYAuMG0GrNJ9FAPrtrjI2ix14L+S4r7sf0F6DgA0wDOUjH7HJ3Cj/7JtXrIURWPaQ+NTZgRZPgR8c0Vta6Qu+4EgsPa5CDbSz72wHLwt/Bqau315xTi4NdQIT+B1T4bB3Td8VjlyMT9KJQvQt8wVfPk2IXa5KzH9 X-Bogosity: Unsure, tests=bogofilter, spamicity=0.499980, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This allows Page references to be returned as borrowed references, without necessarily owning the struct page. Signed-off-by: Asahi Lina Reviewed-by: Alice Ryhl Reviewed-by: Fiona Behrens --- rust/kernel/page.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs index fdac6c375fe46e1ba589f1640afeae3e001e39ae..0b6cbe02522ab6e6e1810288ad23af4e4aa587d8 100644 --- a/rust/kernel/page.rs +++ b/rust/kernel/page.rs @@ -7,6 +7,7 @@ bindings, error::code::*, error::Result, + types::{Opaque, Ownable, Owned}, uaccess::UserSliceReader, }; use core::ptr::{self, NonNull}; @@ -30,13 +31,10 @@ pub const fn page_align(addr: usize) -> usize { (addr + (PAGE_SIZE - 1)) & PAGE_MASK } -/// A pointer to a page that owns the page allocation. -/// -/// # Invariants -/// -/// The pointer is valid, and has ownership over the page. +/// An object representing a memory page in the kernel (`struct page`). +#[repr(transparent)] pub struct Page { - page: NonNull, + page: Opaque, } // SAFETY: Pages have no logic that relies on them staying on a given thread, so moving them across @@ -71,19 +69,20 @@ impl Page { /// let page = Page::alloc_page(GFP_KERNEL | __GFP_ZERO)?; /// # Ok(()) } /// ``` - pub fn alloc_page(flags: Flags) -> Result { + pub fn alloc_page(flags: Flags) -> Result, AllocError> { // SAFETY: Depending on the value of `gfp_flags`, this call may sleep. Other than that, it // is always safe to call this method. let page = unsafe { bindings::alloc_pages(flags.as_raw(), 0) }; let page = NonNull::new(page).ok_or(AllocError)?; - // INVARIANT: We just successfully allocated a page, so we now have ownership of the newly - // allocated page. We transfer that ownership to the new `Page` object. - Ok(Self { page }) + // SAFETY: We just successfully allocated a page, so we now have ownership of the newly + // allocated page. We transfer that ownership to the new `Owned` object. + // Since `Page` is transparent, we can cast the pointer directly. + Ok(unsafe { Owned::from_raw(page.cast()) }) } /// Returns a raw pointer to the page. pub fn as_ptr(&self) -> *mut bindings::page { - self.page.as_ptr() + Opaque::raw_get(&self.page) } /// Runs a piece of code with this page mapped to an address. @@ -252,9 +251,12 @@ pub unsafe fn copy_from_user_slice_raw( } } -impl Drop for Page { - fn drop(&mut self) { +// SAFETY: `Owned` objects returned by Page::alloc_page() follow the requirements of +// the Ownable abstraction. +unsafe impl Ownable for Page { + unsafe fn release(this: NonNull) { // SAFETY: By the type invariants, we have ownership of the page and can free it. - unsafe { bindings::__free_pages(self.page.as_ptr(), 0) }; + // Since Page is transparent, we can cast the raw pointer directly. + unsafe { bindings::__free_pages(this.cast().as_ptr(), 0) }; } }