From nobody Fri Dec 19 07:32:35 2025 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (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 3D058A31; Sat, 7 Jun 2025 12:07:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749298076; cv=none; b=FqTbXbIJL4DqAaVJVn4UwVDh+LhgzH/4I8Ipabam+XOBL8oNPrT0SNZURErs/JkNf4Fh4H/J2DGgn2DkeqbXKSDnO/DF2ZsCq1l5ySXS3b9Yqwk1VZvam/4xN1yHoGmPPHZZ09wMU7ecWBeuJBmu07eFKXg1ICbWC3uTZMVe0E4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749298076; c=relaxed/simple; bh=hWIHDYZlo+TxrAFSAAxaeABB8dsvBETnveG9vkSkrH4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XLr5OljPiqnbX6C9Ao5WlUteWL3x9Fyzt2XrAoePwooh9oZsSsdZwUWKAgKlgOgG92pAeZE91cqg7c4dn7tM7BgZi8kZs5XOL7bzFftpdGQwz2m7xNQTAp1U7TVhq1T3f52SF+oGSxXdAKKDs61BONDcJvqjjTwSk7dOJu5I6kw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EZrw3cNU; arc=none smtp.client-ip=209.85.221.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EZrw3cNU" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-3a528243636so1657975f8f.3; Sat, 07 Jun 2025 05:07:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749298072; x=1749902872; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=59Q3SK7XyIYNXuzgz58DhQfTbMyEnTcQg5z5xjQeKdk=; b=EZrw3cNUw4ScAB79UOI8rZlEfTdSrSCxPWg9kg7TS5QD+D6AabYC7Su8e+J1N5e6vH XPbQebLG+S8BjiwOVBhtvSaS9EjKjouyspim3jVk1PXshMxDlh75TeJjpZ6HXLA6aZhS vGIMfuwgFt6CYl05I9G4JOtCSrA2vw9Gv4EhQgiOIMsEYg9QBtGfwibXCR8BGKeP7FFk AIRb6xkSvQQkov7HRyoDKdaRi+uN2G0nuq7vEjcUWKYcTw3Zk9mfqm6knAmrMmK0Kb7d GERzRPUJpyBRA7Bm2ktsDLU0D3geNxkH/RVhjNRF9DKBY2gle/+cl6kwI0hNyHO8ZDK2 7QDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749298072; x=1749902872; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=59Q3SK7XyIYNXuzgz58DhQfTbMyEnTcQg5z5xjQeKdk=; b=GW3bPBzriR/bCbCtK5Hp30WOAjsSqC07ZY5SQmd8EgamN4onz8SVDZ/l3U7qyAf8r9 B1twYUTxN0Z9rGHDy5S+RFvMbo9z60E+xDZaChBZvz0OUCXKBafJbvn18URzK8TFIbJF dkQq0kbYRQ2cClAzW86YEagoMpqK6wGRgOm0LuKTAEGbAqKAOXxhTQE1Xd/K+M0muwdT psz8Z2E9k/NNq+QqheEm3TbYnHxf08QnXn+vpkMyceEJS0eg1+8IkU814GedcezyhaZT TAqxtDRBGkv8gcih+EJsL8YnMraTt24fvIS7bqcQ3uMX4TNp3o9cpWB9FMCflCJJ99aC 9FYw== X-Forwarded-Encrypted: i=1; AJvYcCXItT+x0QyGp5gKn+YAv6ZcuGtQPD4t0Pv1lA0dbffzONP8TA4rxSAGN5mr9p3Wfw+oXNwSbY7nQV/ur36ArNw=@vger.kernel.org, AJvYcCXxDiZWPLO31BD9H4mzGMF+WsG7ujyij5wM7pWGw4wmafc35IJ7kyM11hAi/TIWu2l4Xl6wj7vP5AzGg04=@vger.kernel.org X-Gm-Message-State: AOJu0YxTFyp6Q5LRmwbajeNCb4DTeS/rTxhPqty966Hem1+9UC5Ci+n3 bQ4ufK6HeF9BQ6RbSUCLcMeCWhndMiqIHf33/q88GTG3j9v5jtnAYcFDR0uSQg== X-Gm-Gg: ASbGncs6NdwEX0raoYxjV+F0DSYsuCEd6ELa3JMrE4iJ0raVX+88mIuW4BeVuEt0uaU zrNnuUpKBhyvEazv7GsNN+VGNpeDDUuPb9XEGM/L64+GfY2hMsegaOtga1d/s7tRZ4jOGYfRPG9 hXqT9P1PZR9ewyl4iDxa3Ntn5ynrDPWhNPSFk/qi94Oo+LxLV2OqJt39wcBubxuZtbxDTTpMiom OF5LZXoceP+oYB1BAMe+yVc8VmXuW8K4cA/gF1a/yQ5MD84G1dhBhA/8F3Kl9v57OHUJqDQM96C QvBGh4VqfbdgSaiBP0Pdb7bN+Ko75N290xXWgfrItwaKaXhj9+w= X-Google-Smtp-Source: AGHT+IHDYKgtPx8HblRV7SW1vLFu4HmsgrjaqnaPc/Pqi3Ejuh0P5+AzwSEtyAwrncwTLZ1PdJ3UWQ== X-Received: by 2002:a05:6000:290d:b0:3a4:f918:9d4e with SMTP id ffacd0b85a97d-3a5319b64b0mr4458048f8f.41.1749298072138; Sat, 07 Jun 2025 05:07:52 -0700 (PDT) Received: from [10.0.1.160] ([2001:871:22a:3372::171c]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-3a532462d93sm4538965f8f.91.2025.06.07.05.07.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Jun 2025 05:07:51 -0700 (PDT) From: Christian Schrefl Date: Sat, 07 Jun 2025 14:07:30 +0200 Subject: [PATCH v5 1/3] rust: implement `Wrapper` for `Opaque` 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: quoted-printable Message-Id: <20250607-b4-rust_miscdevice_registrationdata-v5-1-b77b5b7aab5b@gmail.com> References: <20250607-b4-rust_miscdevice_registrationdata-v5-0-b77b5b7aab5b@gmail.com> In-Reply-To: <20250607-b4-rust_miscdevice_registrationdata-v5-0-b77b5b7aab5b@gmail.com> To: Miguel Ojeda , Danilo Krummrich , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , Trevor Gross , Arnd Bergmann , Greg Kroah-Hartman , Lee Jones , Daniel Almeida , Benno Lossin , Benno Lossin Cc: =?utf-8?q?Gerald_Wisb=C3=B6ck?= , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Christian Schrefl X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1749298070; l=3282; i=chrisi.schrefl@gmail.com; s=20250119; h=from:subject:message-id; bh=hWIHDYZlo+TxrAFSAAxaeABB8dsvBETnveG9vkSkrH4=; b=KoyXDB+HIec1iDCfMwXAN0FbQbGzUvy4r4Q1GrNxfgvu2ktDbjfZ1CT/bDFdlc6hJMuyrU5L4 m20uJ/GyPblCChwE3gnnOBGn37l9NRtodcEIlEmzZnJv4UVVQc9/rbb X-Developer-Key: i=chrisi.schrefl@gmail.com; a=ed25519; pk=EIyitYCrzxWlybrqoGqiL2jyvO7Vp9X40n0dQ6HE4oU= Moves the implementation for `pin-init` from an associated function to the trait function of the `Wrapper` trait and extends the implementation to support pin-initializers with error types. Adds a use for the `Wrapper` trait in `revocable.rs`, to use the new `pin-init` function. This is currently the only usage in the kernel. Reviewed-by: Danilo Krummrich Reviewed-by: Gerald Wisb=C3=B6ck Reviewed-by: Alice Ryhl Reviewed-by: Benno Lossin Signed-off-by: Christian Schrefl --- rust/kernel/revocable.rs | 2 ++ rust/kernel/types.rs | 25 +++++++++++++------------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/rust/kernel/revocable.rs b/rust/kernel/revocable.rs index 1e5a9d25c21b279b01f90b02997492aa4880d84f..4db68ea2207ebafcc09d082fdc1= e281f31846a38 100644 --- a/rust/kernel/revocable.rs +++ b/rust/kernel/revocable.rs @@ -5,6 +5,8 @@ //! The [`Revocable`] type wraps other types and allows access to them to = be revoked. The existence //! of a [`RevocableGuard`] ensures that objects remain valid. =20 +use pin_init::Wrapper; + use crate::{bindings, prelude::*, sync::rcu, types::Opaque}; use core::{ marker::PhantomData, diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 86562e738eac85480a048041e979335b81c5e3c9..7ab70d5f76099c3442dce5b02c6= b226fc74c851e 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -9,7 +9,7 @@ ops::{Deref, DerefMut}, ptr::NonNull, }; -use pin_init::{PinInit, Zeroable}; +use pin_init::{PinInit, Wrapper, Zeroable}; =20 /// Used to transfer ownership to and from foreign (non-Rust) languages. /// @@ -345,17 +345,6 @@ pub const fn uninit() -> Self { } } =20 - /// Create an opaque pin-initializer from the given pin-initializer. - pub fn pin_init(slot: impl PinInit) -> impl PinInit { - Self::ffi_init(|ptr: *mut T| { - // SAFETY: - // - `ptr` is a valid pointer to uninitialized memory, - // - `slot` is not accessed on error; the call is infallible, - // - `slot` is pinned in memory. - let _ =3D unsafe { PinInit::::__pinned_init(slot, ptr) }; - }) - } - /// Creates a pin-initializer from the given initializer closure. /// /// The returned initializer calls the given closure with the pointer = to the inner `T` of this @@ -406,6 +395,18 @@ pub const fn raw_get(this: *const Self) -> *mut T { UnsafeCell::raw_get(this.cast::>>()).cas= t::() } } +impl Wrapper for Opaque { + /// Create an opaque pin-initializer from the given pin-initializer. + fn pin_init(slot: impl PinInit) -> impl PinInit { + Self::try_ffi_init(|ptr: *mut T| { + // SAFETY: + // - `ptr` is a valid pointer to uninitialized memory, + // - `slot` is not accessed on error, + // - `slot` is pinned in memory. + unsafe { PinInit::::__pinned_init(slot, ptr) } + }) + } +} =20 /// Types that are _always_ reference counted. /// --=20 2.49.0 From nobody Fri Dec 19 07:32:35 2025 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (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 644BB21CA03; Sat, 7 Jun 2025 12:07:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749298077; cv=none; b=k5zPpmtrccX9gy9PFCjNxzhIa6JoSRSaer7kHCKLJ4XQiBCFXdFD9bKmBH4f9cUyxRUAux/I2wt67ehVkB6vnGt553gWKVqRdZYRAMRhIrroMBcJhRtZBNRTs7ARV0NDtX/xj6c1D5ppQ2jtYOZXa4URz5OgjbZVLrH7zq/dCeY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749298077; c=relaxed/simple; bh=GL3nRSHbsTV87E5n/f46m8vlJIIIzFEhUXcuWfSf1dM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TzaEahuSaQGDwJbZ5jGdcTgOc5umLS5rhoBnwJ2ckCg+Zrh3p+zSy3B5Y/1tKE/yn6bOvFKCvpTOD5C7t9U0P5TPeO8dAy4FQorG/UIU/Ilfp2B1KEB/M/+qVsNnNM9H72i9Irdsz/PcymrBuqkyVuFHikJRcbdkh4QsbPxfWBw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ETY+UU1q; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ETY+UU1q" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-3a50956e5d3so2476544f8f.1; Sat, 07 Jun 2025 05:07:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749298073; x=1749902873; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=LZAIPeOGdi6pfv+NA6DzdyEVVOCz9njuOHR0OKp/r98=; b=ETY+UU1qcO4zSCdrpr1WQgQlxZn37wrvEnW+vXcn+bSlIwT7RTaJkpMbWck6NgjmSU +aDoyV3R2w+DShEjHeNW/UhU1ZXyRpjg0eIdqs1zI9azJvYffj81XaFB68maHVa/KLg1 wb9YOV15e7qpQFF55svOEeXaRR1XpYzAh/c6dkKqFb3J764cdXJqKYT5EEA77p3UE6js +a3QkUzz1ENUBukma7BwILduKBUF7JgCTFDxhSyWIG2Y8+cb0h1pK4xa7GOryJH6fSi4 pbhktt+3dsP92Yj4gVvrSj6OTHhRymV0e7eObrYlLgBuW7l+h5QCOlC16mY1h6Nag5fZ OVhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749298073; x=1749902873; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LZAIPeOGdi6pfv+NA6DzdyEVVOCz9njuOHR0OKp/r98=; b=WYHxrcdhvj0u5BYqQCmHjnI2zRTO1nHjtpn0QODitLwoHIISFocGvObsPZnZfcym6P YeXNh/F5TrjeHwZ0IVJu0j08IhAKOvezjXAFVlJSXs5K/UF0m8TgleozfT5kSPeNHB3N l4j74ycLmN2rEFzmNZZrXg/9CPqmIrIoW34/23LcR2OLa6iou7FbyG1wLb4mKPAZRGhW PnRfemIfiJj2YvfEJvNHI4hYGUXQm0ICxp4BKqHIHj16qxFGEPu8yCjKPlmPlXrGkmQ3 98lGVamZRmJtapYrS/N5whpZNd2mPml5YnA5MaAd5bTnfxtmNskmK8v/KYLuF9tfEkcv ccNQ== X-Forwarded-Encrypted: i=1; AJvYcCV4XqdpfkEWt5f6Go1lS83tfmvLBHPfnjK2WqvBAcUukr7tuc3Aqvnx2A8hOgfwVC9N8l8OJtcqC8t0rbGgRtU=@vger.kernel.org, AJvYcCVYVHhEd6vNVLqDGaEhyH4vTP3252P0TWNVB1sz2mifsCqM21ZBUVAJMWOnOKaief5rcKCw4qk6aeSCtfA=@vger.kernel.org X-Gm-Message-State: AOJu0Yw7pFJl39dUG3NmOp79QkGaf72jxDvi8BB9Xvr2ssknEeDvYuwt CJpj20RynsskxCvUJjhRpMjKRqTag9lqL3ZdJ4NrfUwx2/NtA6swtB/wK2KRNQ== X-Gm-Gg: ASbGncvFoqgiio50n0kv6XS44oEPPnag46432QM3vKaLDBNeBrLiwHwWxSl+sNwZery yEdDn3d5vzf2ooG/bzGvSu3AvUIDeekU8ZoA6Lrj4v3SoSTro08CJJgAcu4PZEsg1n5KRMMxyvN TyKEoZP1jA1HxmSHYYj0rrPvqLvkGmGJvR9mAOUnQetyZIOlMxsx/j2dA/Fwr8ZAVIUzqOJywgx jGj1JMpiuRMxisQpLtM9ACM9mTFRh3vrU8RX8U2mB99ySgJ2NMCtMpWQLkhhUd75M//0uFC4AMW YY7qjcnmpXnWAjKOF27Md2WOY51vAV6f7BiMXdyt X-Google-Smtp-Source: AGHT+IFxKGtLgmLTwnQWNI7W1safsQA4a5mTr54LGj722R/LF3kN6ne0v9oBxSevtuh3zCW9hunutw== X-Received: by 2002:a05:6000:230c:b0:3a5:2ef8:34f9 with SMTP id ffacd0b85a97d-3a5319a0d8bmr5652350f8f.27.1749298073169; Sat, 07 Jun 2025 05:07:53 -0700 (PDT) Received: from [10.0.1.160] ([2001:871:22a:3372::171c]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-3a532462d93sm4538965f8f.91.2025.06.07.05.07.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Jun 2025 05:07:52 -0700 (PDT) From: Christian Schrefl Date: Sat, 07 Jun 2025 14:07:31 +0200 Subject: [PATCH v5 2/3] rust: miscdevice: add additional data to `MiscDeviceRegistration` 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: quoted-printable Message-Id: <20250607-b4-rust_miscdevice_registrationdata-v5-2-b77b5b7aab5b@gmail.com> References: <20250607-b4-rust_miscdevice_registrationdata-v5-0-b77b5b7aab5b@gmail.com> In-Reply-To: <20250607-b4-rust_miscdevice_registrationdata-v5-0-b77b5b7aab5b@gmail.com> To: Miguel Ojeda , Danilo Krummrich , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , Trevor Gross , Arnd Bergmann , Greg Kroah-Hartman , Lee Jones , Daniel Almeida , Benno Lossin , Benno Lossin Cc: =?utf-8?q?Gerald_Wisb=C3=B6ck?= , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Christian Schrefl X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1749298070; l=11357; i=chrisi.schrefl@gmail.com; s=20250119; h=from:subject:message-id; bh=GL3nRSHbsTV87E5n/f46m8vlJIIIzFEhUXcuWfSf1dM=; b=PXSXIKCELGk+v/PlMRqpjQNrM/Sc3YfcydF9g78Wq+2vel/MW/fKs8rj/Q79O50PwOixvd3Fs M/ZZv8QcXnEAR1ZnRoo2dt/CTygbQRMKZBMkLAbw05RkrFap9Q1Qr7C X-Developer-Key: i=chrisi.schrefl@gmail.com; a=ed25519; pk=EIyitYCrzxWlybrqoGqiL2jyvO7Vp9X40n0dQ6HE4oU= When using the Rust miscdevice bindings, you generally embed the `MiscDeviceRegistration` within another struct: struct MyDriverData { data: SomeOtherData, misc: MiscDeviceRegistration } In the `fops->open` callback of the miscdevice, you are given a reference to the registration, which allows you to access its fields. For example, as of commit 284ae0be4dca ("rust: miscdevice: Provide accessor to pull out miscdevice::this_device") you can access the internal `struct device`. However, there is still no way to access the `data` field in the above example, because you only have a reference to the registration. Using `container_of` is also not possible to do safely. For example, if the destructor of `MyDriverData` runs, then the destructor of `data` would run before the miscdevice is deregistered, so using `container_of` to access `data` from `fops->open` could result in a UAF. A similar problem can happen on initialization if `misc` is not the last field to be initialized. To provide a safe way to access user-defined data stored next to the `struct miscdevice`, make `MiscDeviceRegistration` into a container that can store a user-provided piece of data. This way, `fops->open` can access that data via the registration, since the data is stored inside the registration. The container enforces that the additional user data is initialized before the miscdevice is registered, and that the miscdevice is deregistered before the user data is destroyed. This ensures that access to the userdata is safe. For the same reasons as in commit 88441d5c6d17 ("rust: miscdevice: access the `struct miscdevice` from fops->open()"), you cannot access the user data in any other fops callback than open. This is because a miscdevice can be deregistered while there are still open files. A situation where this user data might be required is when a platform driver acquires a resource in `probe` and wants to use this resource in the `fops` implementation of a `MiscDevice`. This solution is similar to the approach used by the initial downstream Rust-for-Linux/Rust branch [0]. Link: https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/miscdev= .rs#L108 [0] Suggested-by: Alice Ryhl Reviewed-by: Alice Ryhl Signed-off-by: Christian Schrefl --- rust/kernel/miscdevice.rs | 93 ++++++++++++++++++++++++++++++------= ---- samples/rust/rust_misc_device.rs | 4 +- 2 files changed, 73 insertions(+), 24 deletions(-) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index b4c5f74de23d6f4fbcdebfe408d6954884609e8f..92b1b39c9728c7f18cc1ea3bd26= 839664600f9df 100644 --- a/rust/kernel/miscdevice.rs +++ b/rust/kernel/miscdevice.rs @@ -9,7 +9,7 @@ //! Reference: =20 use crate::{ - bindings, + bindings, container_of, device::Device, error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR}, ffi::{c_int, c_long, c_uint, c_ulong}, @@ -20,6 +20,7 @@ types::{ForeignOwnable, Opaque}, }; use core::{marker::PhantomData, mem::MaybeUninit, pin::Pin}; +use pin_init::Wrapper; =20 /// Options for creating a misc device. #[derive(Copy, Clone)] @@ -44,38 +45,55 @@ pub const fn into_raw(self) -> bindings:= :miscdevice { /// /// # Invariants /// -/// `inner` is a registered misc device. -#[repr(transparent)] +/// - `inner` is a registered misc device. +/// - `data` contains a valid `T::Data` for the whole lifetime of [`MiscDe= viceRegistration`] +/// - `data` must be valid until `misc_deregister` (called when dropped) h= as returned. +/// - no mutable references to `data` may be created. #[pin_data(PinnedDrop)] -pub struct MiscDeviceRegistration { +pub struct MiscDeviceRegistration { #[pin] inner: Opaque, - _t: PhantomData, + #[pin] + data: Opaque, } =20 -// SAFETY: It is allowed to call `misc_deregister` on a different thread f= rom where you called -// `misc_register`. -unsafe impl Send for MiscDeviceRegistration {} -// SAFETY: All `&self` methods on this type are written to ensure that it = is safe to call them in -// parallel. -unsafe impl Sync for MiscDeviceRegistration {} +// SAFETY: +// - It is allowed to call `misc_deregister` on a different thread from wh= ere you called +// `misc_register`. +// - Only implements `Send` if `MiscDevice::Data` is also `Send`. +unsafe impl Send for MiscDeviceRegistration where T::Dat= a: Send {} + +// SAFETY: +// - All `&self` methods on this type are written to ensure that it is saf= e to call them in +// parallel. +// - Only implements `Sync` if `MiscDevice::Data` is also `Sync`. +unsafe impl Sync for MiscDeviceRegistration where T::Dat= a: Sync {} =20 impl MiscDeviceRegistration { /// Register a misc device. - pub fn register(opts: MiscDeviceOptions) -> impl PinInit { + pub fn register( + opts: MiscDeviceOptions, + data: impl PinInit, + ) -> impl PinInit + where + Self: Sync, + { try_pin_init!(Self { + data <- Opaque::pin_init(data), inner <- Opaque::try_ffi_init(move |slot: *mut bindings::miscd= evice| { // SAFETY: The initializer can write to the provided `slot= `. unsafe { slot.write(opts.into_raw::()) }; =20 - // SAFETY: We just wrote the misc device options to the sl= ot. The miscdevice will - // get unregistered before `slot` is deallocated because t= he memory is pinned and - // the destructor of this type deallocates the memory. + // SAFETY: + // * We just wrote the misc device options to the slot. Th= e miscdevice will + // get unregistered before `slot` is deallocated because= the memory is pinned and + // the destructor of this type deallocates the memory. + // * `data` is Initialized before `misc_register` so no ra= ce with `fops->open()` + // is possible. // INVARIANT: If this returns `Ok(())`, then the `slot` wi= ll contain a registered // misc device. to_result(unsafe { bindings::misc_register(slot) }) }), - _t: PhantomData, }) } =20 @@ -93,13 +111,24 @@ pub fn device(&self) -> &Device { // before the underlying `struct miscdevice` is destroyed. unsafe { Device::as_ref((*self.as_raw()).this_device) } } + + /// Access the additional data stored in this registration. + pub fn data(&self) -> &T::Data { + // SAFETY: + // * No mutable reference to the value contained by `self.data` ca= n ever be created. + // * The value contained by `self.data` is valid for the entire li= fetime of `&self`. + unsafe { &*self.data.get() } + } } =20 #[pinned_drop] -impl PinnedDrop for MiscDeviceRegistration { +impl PinnedDrop for MiscDeviceRegistration { fn drop(self: Pin<&mut Self>) { // SAFETY: We know that the device is registered by the type invar= iants. unsafe { bindings::misc_deregister(self.inner.get()) }; + + // SAFETY: `self.data` contains a valid `Data` and does not need t= o be valid anymore. + unsafe { core::ptr::drop_in_place(self.data.get()) }; } } =20 @@ -109,6 +138,13 @@ pub trait MiscDevice: Sized { /// What kind of pointer should `Self` be wrapped in. type Ptr: ForeignOwnable + Send + Sync; =20 + /// Additional data carried by the [`MiscDeviceRegistration`] for this= [`MiscDevice`]. + /// If no additional data is required than the unit type `()` should b= e used. + /// + /// This can be accessed in [`MiscDevice::open()`] using + /// [`MiscDeviceRegistration::data()`]. + type Data; + /// Called when the misc device is opened. /// /// The returned pointer will be stored as the private data for the fi= le. @@ -178,18 +214,29 @@ impl MiscdeviceVTable { // SAFETY: The open call of a file can access the private data. let misc_ptr =3D unsafe { (*raw_file).private_data }; =20 - // SAFETY: This is a miscdevice, so `misc_open()` set the private = data to a pointer to the - // associated `struct miscdevice` before calling into this method.= Furthermore, - // `misc_open()` ensures that the miscdevice can't be unregistered= and freed during this - // call to `fops_open`. - let misc =3D unsafe { &*misc_ptr.cast::>= () }; + // This is a miscdevice, so `misc_open()` sets the private data to= a pointer to the + // associated `struct miscdevice` before calling into this method. + let misc_ptr =3D misc_ptr.cast::>(); + + // SAFETY: + // * `misc_open()` ensures that the `struct miscdevice` can't be u= nregistered and freed + // during this call to `fops_open`. + // * The `misc_ptr` always points to the `inner` field of a `MiscD= eviceRegistration`. + // * The `MiscDeviceRegistration` is valid until the `struct mi= scdevice` was + // unregistered. + // * `MiscDeviceRegistration` is `Send` since `MiscDeviceRegist= ration::register` has a + // `Self: Send` bound and is the only way to create a `MiscDevic= eRegistration`. This + // means that a reference to it can be shared between contexts. + // TODO: add `assert_sync` for `MiscDeviceRegistration` and + // `MiscDeviceRegistration::Data`. + let registration =3D unsafe { &*container_of!(misc_ptr, MiscDevice= Registration, inner) }; =20 // SAFETY: // * This underlying file is valid for (much longer than) the dura= tion of `T::open`. // * There is no active fdget_pos region on the file on this threa= d. let file =3D unsafe { File::from_raw_file(raw_file) }; =20 - let ptr =3D match T::open(file, misc) { + let ptr =3D match T::open(file, registration) { Ok(ptr) =3D> ptr, Err(err) =3D> return err.to_errno(), }; diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_devi= ce.rs index c881fd6dbd08cf4308fe1bd37d11d28374c1f034..c0b912920d6c4b60e747d9d2989= 00ad64df67339 100644 --- a/samples/rust/rust_misc_device.rs +++ b/samples/rust/rust_misc_device.rs @@ -137,7 +137,7 @@ fn init(_module: &'static ThisModule) -> impl PinInit { }; =20 try_pin_init!(Self { - _miscdev <- MiscDeviceRegistration::register(options), + _miscdev <- MiscDeviceRegistration::register(options, ()), }) } } @@ -157,6 +157,8 @@ struct RustMiscDevice { impl MiscDevice for RustMiscDevice { type Ptr =3D Pin>; =20 + type Data =3D (); + fn open(_file: &File, misc: &MiscDeviceRegistration) -> Result>> { let dev =3D ARef::from(misc.device()); =20 --=20 2.49.0 From nobody Fri Dec 19 07:32:35 2025 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 47DC4221FA0; Sat, 7 Jun 2025 12:07:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749298078; cv=none; b=f+P4zQgEy8/+GKyAWSav3rIRA8SwsvGLfkeuhp2ZZ9QzZSQgIMCo85j2F7o+ZeL+TzOYYIa0B36Jc++4Vg+UaZ55bUH94K0RtcyIYCN1OHZFmSKP09urriToLrpB3nZKlFx7P4zmr2HtGGgRXwk2y6PUA92YmDK/LLMIV0NJK9w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749298078; c=relaxed/simple; bh=DZa3JdiBu214uY9Tt/JtVOPrAsMdXuGUNMm9XNgGI3o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sEPE4R0EcOkCbvAJgk5VOzFLEzUUWjPGDUnJFrIe38zG3oCUDkbtmE5wZD/uNfsjSlxZvJr/c9Z/wTofbQU1rJ24feYEY+/acBYTsTddP2OdS+J286VpNsR0OXyCd2XEKqDG6TRbyPU3TTAFyHPHUlG0K3YFMdl22FhfrIRM1dI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=iT1p9kch; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iT1p9kch" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-442e9c00bf4so23855835e9.3; Sat, 07 Jun 2025 05:07:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749298074; x=1749902874; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=07TVQMLxCVF1Rat4olwfbCqsRTrqGNZ3C/Ht2jMQZbM=; b=iT1p9kchtH0VEYrq1zNA8HAC4b1nXYrlaonCWRsaqSm8UxLe6ADOi8bVxJTrHpEyEw PIk12j2M0lpRlqm8+erhSgctTsxz9B+vgRtDrmgvOIeNCezaNiDrkiN8K/bOyEkKWy+E 6K4ZK1Gxk3Zhwwjie48WI2nsg+AImE9VSpOO1SxB6XXOcDSFPOjtpWhmcn2+yFeHo+rc VufuDxbfHZSNnpNtMVBMfYUgcxie4wFpMH8tC+zq8V6xQhUyKCVzazdCjgv6crXn1Z8U XvzT7bC0oqRcOtXqFsIYtTe9eHgK1mNRcabJkKV7TCkRq2ei1OSKxIPtWSgBNjBVLo53 Qb/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749298074; x=1749902874; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=07TVQMLxCVF1Rat4olwfbCqsRTrqGNZ3C/Ht2jMQZbM=; b=Raef2wAL5eipmDfMadVyawuRT4xQlV3Xgf9jgUqthU8KkhBF+9/spoNqpzSZi/FrJl pBMLHKzi5/NQhqHtqIpjqu5Hp+q71IrLP5XVJa77QQybM/T1vvRg6DPtret9j/SuSUCe tiLeLBDe9FbqbQmXIKehRiqAmDVGgtAKJBQ1Sz3v1n+tWmt+5fY9113WN4y7VhBJ2MzV bCpT0wXo/PygdxTRUgjYmz7GPf/7ySF3aXR4qTq0nBSFJ3fwmmP5GZta5j2oHWWvHvFf PY34y8yZclK8CY3PLGHi15Mf1Nn5DoNzbP5MHtpERpwqhg1XH8W+G8wsaWuFnoUUua1s Sesg== X-Forwarded-Encrypted: i=1; AJvYcCWY+z6h2FKBHBGIeRfPH8akjmQS5pcgNw+nWSpa4llJThltAeDeb8XT2UxCc9nTuNxOKHNH9cLQ4TMR2dYvQ6Q=@vger.kernel.org, AJvYcCXr1M62pjvQO7D2lnF3tRKm+loab4dm2G9vCF4mgt64LsswNF49J/L7Zkimf1shgqXWkoR0Bjt5ctv30DA=@vger.kernel.org X-Gm-Message-State: AOJu0YyHwHYrhzv7J0CFZjC7iFAWstjDpz5LC/8IG3FHeNMqbpBLmg9I hgj2HT9f93KFgYU/pzJ+2w+3jobXqckWCQaVsw2axblsSMVCJMYx+vF5LhhHlQ== X-Gm-Gg: ASbGncuPG7DtSfd0ReoGUNqR27ll70BOGF0lShvkAjVJA/Tjdln8KPcczb1L1+yMbjv O1VQJnG9DTvVzDshwQH6ofV3/C1mMCAWJTHVCpcUruDgawVucpRwKdGuSP6FQvndLIjLQisZL8M PsGC6Nb58aA9KxCGD1yyU5FaNwbulNeMD/e1IhZg9xRBa4+uuw6teqvwir4UE7Nfq5hRYAY7YWF ShWZAhZTqRYIEj2R9ucu2ZVAeK9b4WhT3rZsMXfH5h8/KRMyT+EvsgjH6eoSHRF8SxgwynxA8Js IGOc9zwd8pPtaXoxShrJ5hbCSUTq4NpyWtMHz3HA X-Google-Smtp-Source: AGHT+IE7n4gjCOE3epBBVqjFinYJFlGKFod+74ExHLrgD548M10Dcn8f9XkH9neTz2BOYCEzP2ucSA== X-Received: by 2002:a05:6000:2204:b0:3a4:eecd:f4d2 with SMTP id ffacd0b85a97d-3a5319b5aafmr5912572f8f.38.1749298074265; Sat, 07 Jun 2025 05:07:54 -0700 (PDT) Received: from [10.0.1.160] ([2001:871:22a:3372::171c]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-3a532462d93sm4538965f8f.91.2025.06.07.05.07.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Jun 2025 05:07:53 -0700 (PDT) From: Christian Schrefl Date: Sat, 07 Jun 2025 14:07:32 +0200 Subject: [PATCH v5 3/3] rust: miscdevice: adjust the `rust_misc_device` sample to use `Data`. 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: quoted-printable Message-Id: <20250607-b4-rust_miscdevice_registrationdata-v5-3-b77b5b7aab5b@gmail.com> References: <20250607-b4-rust_miscdevice_registrationdata-v5-0-b77b5b7aab5b@gmail.com> In-Reply-To: <20250607-b4-rust_miscdevice_registrationdata-v5-0-b77b5b7aab5b@gmail.com> To: Miguel Ojeda , Danilo Krummrich , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , Trevor Gross , Arnd Bergmann , Greg Kroah-Hartman , Lee Jones , Daniel Almeida , Benno Lossin , Benno Lossin Cc: =?utf-8?q?Gerald_Wisb=C3=B6ck?= , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Christian Schrefl X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1749298070; l=7426; i=chrisi.schrefl@gmail.com; s=20250119; h=from:subject:message-id; bh=DZa3JdiBu214uY9Tt/JtVOPrAsMdXuGUNMm9XNgGI3o=; b=2SATMg4MDJOQ/ZrVtcO+nSGqVdznYIlzFz+Zz9lQ1Ay2W/0s39ZXGXHcQA0aMx9Meyd4KTq6g LXUkgO90jQaCpCB8vzC8qclaZHknMwDmA0ssUunouiIMVEYknKwHaAb X-Developer-Key: i=chrisi.schrefl@gmail.com; a=ed25519; pk=EIyitYCrzxWlybrqoGqiL2jyvO7Vp9X40n0dQ6HE4oU= Add a second mutex to the `RustMiscDevice``, which is shared between all instances of the device using an `Arc` and the `Data` of `MiscDeviceRegistration`. This is mostly to demonstrate the capability to share data in this way. Reviewed-by: Alice Ryhl Signed-off-by: Christian Schrefl --- samples/rust/rust_misc_device.rs | 116 +++++++++++++++++++++++++++++++++++= +--- 1 file changed, 109 insertions(+), 7 deletions(-) diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_devi= ce.rs index c0b912920d6c4b60e747d9d298900ad64df67339..7519ff6a79985e8bdbb7f4c79d8= a6ebf160ef8cc 100644 --- a/samples/rust/rust_misc_device.rs +++ b/samples/rust/rust_misc_device.rs @@ -18,6 +18,8 @@ //! #define RUST_MISC_DEV_HELLO _IO('|', 0x80) //! #define RUST_MISC_DEV_GET_VALUE _IOR('|', 0x81, int) //! #define RUST_MISC_DEV_SET_VALUE _IOW('|', 0x82, int) +//! #define RUST_MISC_DEV_GET_SHARED_VALUE _IOR('|', 0x83, int) +//! #define RUST_MISC_DEV_SET_SHARED_VALUE _IOW('|', 0x84, int) //! //! int main() { //! int value, new_value; @@ -86,6 +88,62 @@ //! return -1; //! } //! +//! value++; +//! +//! // Set shared value to something different +//! printf("Submitting new shared value (%d)\n", value); +//! ret =3D ioctl(fd, RUST_MISC_DEV_SET_SHARED_VALUE, &value); +//! if (ret < 0) { +//! perror("ioctl: Failed to submit new value"); +//! close(fd); +//! return errno; +//! } +//! +//! // Close the device file +//! printf("Closing /dev/rust-misc-device\n"); +//! close(fd); +//! +//! // Open the device file again +//! printf("Opening /dev/rust-misc-device again for reading\n"); +//! fd =3D open("/dev/rust-misc-device", O_RDWR); +//! if (fd < 0) { +//! perror("open"); +//! return errno; +//! } +//! +//! // Ensure new value was applied +//! printf("Fetching new value\n"); +//! ret =3D ioctl(fd, RUST_MISC_DEV_GET_SHARED_VALUE, &new_value); +//! if (ret < 0) { +//! perror("ioctl: Failed to fetch the new value"); +//! close(fd); +//! return errno; +//! } +//! +//! if (value !=3D new_value) { +//! printf("Failed: Committed and retrieved values are different (%d -= %d)\n", +//! value, new_value); +//! close(fd); +//! return -1; +//! } +//! +//! value =3D 0; +//! // Ensure non-shared value is still 0 +//! printf("Fetching new value\n"); +//! ret =3D ioctl(fd, RUST_MISC_DEV_GET_VALUE, &new_value); +//! if (ret < 0) { +//! perror("ioctl: Failed to fetch the new value"); +//! close(fd); +//! return errno; +//! } +//! +//! if (value !=3D new_value) { +//! printf("Failed: Committed and retrieved values are different (%d -= %d)\n", +//! value, new_value); +//! close(fd); +//! return -1; +//! } +//! //! // Close the device file //! printf("Closing /dev/rust-misc-device\n"); //! close(fd); @@ -105,7 +163,7 @@ miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration}, new_mutex, prelude::*, - sync::Mutex, + sync::{Arc, Mutex}, types::ARef, uaccess::{UserSlice, UserSliceReader, UserSliceWriter}, }; @@ -113,6 +171,8 @@ const RUST_MISC_DEV_HELLO: u32 =3D _IO('|' as u32, 0x80); const RUST_MISC_DEV_GET_VALUE: u32 =3D _IOR::('|' as u32, 0x81); const RUST_MISC_DEV_SET_VALUE: u32 =3D _IOW::('|' as u32, 0x82); +const RUST_MISC_DEV_GET_SHARED_VALUE: u32 =3D _IOR::('|' as u32, 0x83= ); +const RUST_MISC_DEV_SET_SHARED_VALUE: u32 =3D _IOW::('|' as u32, 0x84= ); =20 module! { type: RustMiscDeviceModule, @@ -137,7 +197,10 @@ fn init(_module: &'static ThisModule) -> impl PinInit<= Self, Error> { }; =20 try_pin_init!(Self { - _miscdev <- MiscDeviceRegistration::register(options, ()), + _miscdev <- MiscDeviceRegistration::register( + options, + Arc::pin_init(new_mutex!(Inner { value: 0_i32 }), GFP_KERN= EL)? + ), }) } } @@ -148,8 +211,9 @@ struct Inner { =20 #[pin_data(PinnedDrop)] struct RustMiscDevice { + shared: Arc>, #[pin] - inner: Mutex, + unique: Mutex, dev: ARef, } =20 @@ -157,7 +221,7 @@ struct RustMiscDevice { impl MiscDevice for RustMiscDevice { type Ptr =3D Pin>; =20 - type Data =3D (); + type Data =3D Arc>; =20 fn open(_file: &File, misc: &MiscDeviceRegistration) -> Result>> { let dev =3D ARef::from(misc.device()); @@ -167,7 +231,8 @@ fn open(_file: &File, misc: &MiscDeviceRegistration) -> Result, _file: &File, cmd: = u32, arg: usize) -> Result match cmd { RUST_MISC_DEV_GET_VALUE =3D> me.get_value(UserSlice::new(arg, = size).writer())?, RUST_MISC_DEV_SET_VALUE =3D> me.set_value(UserSlice::new(arg, = size).reader())?, + RUST_MISC_DEV_GET_SHARED_VALUE =3D> { + me.get_shared_value(UserSlice::new(arg, size).writer())? + } + RUST_MISC_DEV_SET_SHARED_VALUE =3D> { + me.set_shared_value(UserSlice::new(arg, size).reader())? + } RUST_MISC_DEV_HELLO =3D> me.hello()?, _ =3D> { dev_err!(me.dev, "-> IOCTL not recognised: {}\n", cmd); @@ -204,7 +275,7 @@ fn drop(self: Pin<&mut Self>) { impl RustMiscDevice { fn set_value(&self, mut reader: UserSliceReader) -> Result { let new_value =3D reader.read::()?; - let mut guard =3D self.inner.lock(); + let mut guard =3D self.unique.lock(); =20 dev_info!( self.dev, @@ -217,7 +288,38 @@ fn set_value(&self, mut reader: UserSliceReader) -> Re= sult { } =20 fn get_value(&self, mut writer: UserSliceWriter) -> Result { - let guard =3D self.inner.lock(); + let guard =3D self.unique.lock(); + let value =3D guard.value; + + // Free-up the lock and use our locally cached instance from here + drop(guard); + + dev_info!( + self.dev, + "-> Copying data to userspace (value: {})\n", + &value + ); + + writer.write::(&value)?; + Ok(0) + } + + fn set_shared_value(&self, mut reader: UserSliceReader) -> Result { + let new_value =3D reader.read::()?; + let mut guard =3D self.shared.lock(); + + dev_info!( + self.dev, + "-> Copying data from userspace (value: {})\n", + new_value + ); + + guard.value =3D new_value; + Ok(0) + } + + fn get_shared_value(&self, mut writer: UserSliceWriter) -> Result { + let guard =3D self.shared.lock(); let value =3D guard.value; =20 // Free-up the lock and use our locally cached instance from here --=20 2.49.0