Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2603616lqo; Mon, 20 May 2024 10:27:26 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW8tDjCcFKOEej1xbbWu3E+xyAm3mo3ELclWARyFepK+qyHnCqiKKVlYI/S4KZzBqk/bgrKYsMO3t0T57M9BZ9UXzhMJZK9yVBP6ZxnvQ== X-Google-Smtp-Source: AGHT+IEo6lT7R9kDvkWJgD6wyaIYWwbpajrQVUhpHcCd7HTHA8+XPHrXIWtaHmsyUfpYTAyUb/D4 X-Received: by 2002:a17:907:35d4:b0:a5a:7d28:54aa with SMTP id a640c23a62f3a-a5a7d285594mr1905332766b.23.1716226046565; Mon, 20 May 2024 10:27:26 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716226046; cv=pass; d=google.com; s=arc-20160816; b=MPdnJ74NAET3eddXbVzo+fXNls8otQzKsyBEs1QG2J+X/n8tj6cwX8XcvKL7Jc7bbK 0iKSvCfLecorsoVGtHbcaI5sYqtNp9ISvWdjp6lgUfuJOkh7zm331PCoh/mqOIuzXg05 6Y9FSi//2iAd90ZhGi144p65lap1QX2I62MMn3RqRH/wm7VV0CwUyyiuRmgFj20ovarR pbybk68Mw+C1NCbCrvA2nn0tcziAyDva2CEWIUjiQCkWXhPGbUDLTwyOtGmszryj3iuo utw9K9Edp7MzWGKkvjyfsl0fnFIxSzTxqXBe48QP6k/rHqCt6uBDDLoJzUSU9Clq32k4 gRkw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=R91zVu6bvxpu1aXFiFEweUnqMPVUvsNACvUAs6c3Gw8=; fh=WoucnkMx+1VKt8UQJQaaAJSKRBkCi3b99VeERJ/9x0Y=; b=SrVXiGurxXZeqzvzvva5Q36Dm/lViHb84oqmwdGHdENdfGsw7kN71PaoFvdX8l0uQd BPZZQCQCToJ+EtgeZNrAnQJ/HwjbCc1kHCWuH8adIEIomdfmeDWtpOcbRDSH3780mfgl ridHPyKw7SiYMNwqpFHqBGqCGzMvgdWcC5Ah1aK2Vh4otC8XMO0wXcAIKC0x3TP8bI1Z cvnhFnAyDzgYDbMMAEDTA1S6FtWpwy3CK4gprXB3HVQWuRIv40BN4nYUGJIKYHcx2fE8 6w0lKqPyHLjNVIbyKE8q1lYmZ2+RUKqNTfzssxky9c9yKBaLaZYszBr3fKceW/nWvsZq Gl5w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=htO9WfRd; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-184000-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184000-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a17b2188asi1322944266b.430.2024.05.20.10.27.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 10:27:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-184000-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=@redhat.com header.s=mimecast20190719 header.b=htO9WfRd; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-184000-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184000-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 28E271F20ECD for ; Mon, 20 May 2024 17:27:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D8F1A1384A6; Mon, 20 May 2024 17:27:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="htO9WfRd" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6BDD813848D for ; Mon, 20 May 2024 17:27:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716226023; cv=none; b=HxMqvZtHGe1BRFsZNagDSg/0aNPgqguqXHQSi8KpU5pMGVNj+hxt9+CmmyeNoZPBBKA8/8QXOASAqWjs89UEmzxKTr3hZnDIablD+gNlboSu5EFirrdu4TYaKJVZ+F89/zdORf9qMDoHiKLykkAESyRl/TgIf6f6i2Q+zjW3pnk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716226023; c=relaxed/simple; bh=rAhFipo4bK8HmFg4tJSDidtDJaUeETRwaBwak+PMNa8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=i7ghbskCTVDz5MgsDW1Xt8nK2mATKJfEU2o4gGtTvnDZFiTboRtwWO/fCqO+VRZD5aPTITSHyeOeCn/PAP9vneJQK4BQsKl9We/KJgShrJgJ/bcwlBwSeQ9IqS9gBBxvZHlJ+D5EJ+ijWNv9z32ZGX+J5qCOZ3WiYGDqniDAj08= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=htO9WfRd; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716226021; h=from:from: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; bh=R91zVu6bvxpu1aXFiFEweUnqMPVUvsNACvUAs6c3Gw8=; b=htO9WfRdbgrEMp2RuBk3I+y4J0LlwHIKAH9QbQPOIaAwGFmopjdT07YjefqjFJ4A9oPqxM VBns2QsRK1B81OIYSqNmuOhmmunwIXiWE8FqTQDsfioP8htZUdMHCsGZctxTYW2jL7q/MR cGRYGEsfRHPy5GTdD/y/GU9Qi8hc1DY= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-487-exUnV4UoNymy51CgRSf7oQ-1; Mon, 20 May 2024 13:26:59 -0400 X-MC-Unique: exUnV4UoNymy51CgRSf7oQ-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-41fc5c5cc95so51939445e9.0 for ; Mon, 20 May 2024 10:26:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716226018; x=1716830818; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=R91zVu6bvxpu1aXFiFEweUnqMPVUvsNACvUAs6c3Gw8=; b=dhUP3DIsB2LqMSpPp19dSweDvh4zy/4+82vT+zlBozKaYLym5ytvDWuCj3SueF1dmq OOBe0CE8zqxFkgovOxDgEBwpoh0vyCn5iP3avjvu7Ecod/z0dxhkLvOrUZ0P0pMgqN8J 1JNf6otLo9Vd8Ljg3H8BVXMubwVmMiapC5iEKEocprMxvoyC/4RMeCdl0f4g9c0yvgwQ 7Dmvfki/55A9RDdVkbM575OdaPRq2VgGLV4ORQwzmti7lAJJDSQvt+PY3kabPAUWvnii WdfoudiT8WlWwMFGHcek+x6rBn9VAh0Iil0IfvKvMWQ1/qdvRvQat3/+uUn21nLtHD5A iMbQ== X-Forwarded-Encrypted: i=1; AJvYcCWCzINwXWXiARV6Nrlo1MgYoNcxEAwQ7bvxwneiA9wunXUgfOcParGpaS2owuytX5T6N2QylZ8A2/Y6SrkgHEvkPMfrzsojeiY8oNxv X-Gm-Message-State: AOJu0Yyf1kEibdGVoAyycRsAsSFGq5JU1AIelkB9AE2YtQhn6jYAi9Cc T5JHErMkK5x96+5W1gDSDdsfOhGX+sZmby/pOS3C2IdM+znG8DB9C5XMNQLq8D//cowvKbXzwIT t/5rfBUA6GnFSlAH1583Prv0DfO9bwjMJl0xt4NYILwECs8+cVgAKxZHOziJWkw== X-Received: by 2002:a05:6000:1968:b0:34f:96ba:ca3a with SMTP id ffacd0b85a97d-3504a61c7cemr32867603f8f.13.1716226017555; Mon, 20 May 2024 10:26:57 -0700 (PDT) X-Received: by 2002:a05:6000:1968:b0:34f:96ba:ca3a with SMTP id ffacd0b85a97d-3504a61c7cemr32867560f8f.13.1716226016428; Mon, 20 May 2024 10:26:56 -0700 (PDT) Received: from cassiopeiae.. ([2a02:810d:4b3f:ee94:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-354bd2e2a5asm6620842f8f.45.2024.05.20.10.26.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 10:26:55 -0700 (PDT) From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, bhelgaas@google.com, ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, airlied@gmail.com, fujita.tomonori@gmail.com, lina@asahilina.net, pstanner@redhat.com, ajanulgu@redhat.com, lyude@redhat.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Danilo Krummrich Subject: [RFC PATCH 01/11] rust: add abstraction for struct device Date: Mon, 20 May 2024 19:25:38 +0200 Message-ID: <20240520172554.182094-2-dakr@redhat.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240520172554.182094-1-dakr@redhat.com> References: <20240520172554.182094-1-dakr@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add an (always) reference counted abstraction for a generic struct device. This abstraction encapsulates existing struct device instances and manages its reference count. Subsystems may use this abstraction as a base to abstract subsystem specific device instances based on a generic struct device. Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Signed-off-by: Danilo Krummrich --- rust/helpers.c | 1 + rust/kernel/device.rs | 76 +++++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 3 files changed, 78 insertions(+) create mode 100644 rust/kernel/device.rs diff --git a/rust/helpers.c b/rust/helpers.c index 4c8b7b92a4f4..f9d2db1d1f33 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs new file mode 100644 index 000000000000..fafec70effb6 --- /dev/null +++ b/rust/kernel/device.rs @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Generic devices that are part of the kernel's driver model. +//! +//! C header: [`include/linux/device.h`](../../../../include/linux/device.h) + +use crate::{ + bindings, + types::{ARef, Opaque}, +}; +use core::ptr; + +/// A ref-counted device. +/// +/// # Invariants +/// +/// The pointer stored in `Self` is non-null and valid for the lifetime of the ARef instance. In +/// particular, the ARef instance owns an increment on underlying object’s reference count. +#[repr(transparent)] +pub struct Device(Opaque); + +impl Device { + /// Creates a new ref-counted instance of an existing device pointer. + /// + /// # Safety + /// + /// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count. + pub unsafe fn from_raw(ptr: *mut bindings::device) -> ARef { + // SAFETY: By the safety requirements, ptr is valid. + // Initially increase the reference count by one to compensate for the final decrement once + // this newly created `ARef` instance is dropped. + unsafe { bindings::get_device(ptr) }; + + // CAST: `Self` is a `repr(transparent)` wrapper around `bindings::device`. + let ptr = ptr.cast::(); + + // SAFETY: By the safety requirements, ptr is valid. + unsafe { ARef::from_raw(ptr::NonNull::new_unchecked(ptr)) } + } + + /// Obtain the raw `struct device *`. + pub(crate) fn as_raw(&self) -> *mut bindings::device { + self.0.get() + } + + /// Convert a raw `struct device` pointer to a `&Device`. + /// + /// # Safety + /// + /// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count for + /// the entire duration when the returned reference exists. + pub unsafe fn as_ref<'a>(ptr: *mut bindings::device) -> &'a Self { + // SAFETY: Guaranteed by the safety requirements of the function. + unsafe { &*ptr.cast() } + } +} + +// SAFETY: Instances of `Device` are always ref-counted. +unsafe impl crate::types::AlwaysRefCounted for Device { + fn inc_ref(&self) { + // SAFETY: The existence of a shared reference guarantees that the refcount is nonzero. + unsafe { bindings::get_device(self.as_raw()) }; + } + + unsafe fn dec_ref(obj: ptr::NonNull) { + // SAFETY: The safety requirements guarantee that the refcount is nonzero. + unsafe { bindings::put_device(obj.cast().as_ptr()) } + } +} + +// SAFETY: `Device` only holds a pointer to a C device, which is safe to be used from any thread. +unsafe impl Send for Device {} + +// SAFETY: `Device` only holds a pointer to a C device, references to which are safe to be used +// from any thread. +unsafe impl Sync for Device {} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 9a943d99c71a..4ba3d4a49e9c 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -28,6 +28,7 @@ pub mod alloc; mod build_assert; +pub mod device; pub mod error; pub mod init; pub mod ioctl; -- 2.45.1