From mboxrd@z Thu Jan 1 00:00:00 1970 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 9996042E8FB for ; Wed, 5 Aug 2026 12:20:25 +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=1785932427; cv=none; b=NKm8BlgH1lHl7UAbgbHO9lukONpHAXxqKJByXaOohxPvRgHl+stYaVfqVkp9h1YTjtBUfECSmh33oR2+76JiEFzQbpYnG3KIaO6V7mg2O8cPlsoqPxmt94y2CtD5XDyTm35qKd7wp12IHPW+cqWyYE1rJYjtuKz966hLyuWhg5I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785932427; c=relaxed/simple; bh=7GYie+sje6vYUEu4nfQt7Cuk2MmcmlQNrMLtJybe3qE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=HSBXY9dnuilXL5ds4lRDFx4dl2xipvuuIOwOUv7T7+/IwFoBwuUiuiLHgUJpn9WEIGjip00YRmiWPXTjo8aB++12djKmtLZpDFg6JXGqydzqnEz02wWAX3JrmGXElZa1G+nGhXowWNfbx1bvxK/0hAhvmzBCVZ/XTg1+rcJJ/5k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=wyliodrin.com; spf=pass smtp.mailfrom=wyliodrin.com; dkim=pass (1024-bit key) header.d=wyliodrin.com header.i=@wyliodrin.com header.b=klPTLvYp; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=wyliodrin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=wyliodrin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=wyliodrin.com header.i=@wyliodrin.com header.b="klPTLvYp" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-47f71156e1aso431681f8f.3 for ; Wed, 05 Aug 2026 05:20:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wyliodrin.com; s=google; t=1785932424; x=1786537224; darn=vger.kernel.org; h=cc:to:message-id:content-transfer-encoding:content-type :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to:content-type; bh=lOuY5JDUypH3xF3cJHSxCPv7eqlTAbf9vmoap0AQkDE=; b=klPTLvYpbXhYTuiI8aEBr6Jt/VW37xxPTFWNxXN6+VIFGSBne4ja097OUJEyNx+xMA 1zNEa/nJBRux/WsNuPNi4p6Nvsry2srJDQ4qn5cy9BG28xENRP/4BNB3lfWHhQKiUt1I pFbDHEolAHLfSZucV6Y1pOWWFb3+9UAopa4Ys= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785932424; x=1786537224; h=cc:to:message-id:content-transfer-encoding:content-type :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to:content-type; bh=lOuY5JDUypH3xF3cJHSxCPv7eqlTAbf9vmoap0AQkDE=; b=k674jh74hMKzpDJHEXaCjw3AYy3ZCZjEp07w3SrkNwzb57VtSGrkw0xHFlnXrEe9TJ ktoN/YNV1SM+Mj94IX0GNNlUjm331xxw27THBJ5oioaBbeUIQWzVSVogHikyJS8k9Yl4 NrR5iGfy1WleBUhquVjVK2vybbftLHUjgtSlIHUZqO++p4/+s4PW5s1Ic+S3mJciuXJl XHKZO1EYp33ufHESGrwxK0i2olyQqToQoy5pxrLHhPz/26fIfZtOh897oCQ9HTSDHb5K //XHH4RgNEztxxy+1xiRDkXgSeN+KfYGN8Vm9Kduv+Y9VZ+6itpXWOUh3Xp55pqTDzvs qpTA== X-Forwarded-Encrypted: i=1; AHgh+RriPgS0GHSbSs2N3ILA1uHrQmYrNJKynhW6qCuGoBQVOVkHtezl5U77kvic+4kyFh4svGUJnRv+6TJUAWA=@vger.kernel.org X-Gm-Message-State: AOJu0YzHpgPEwmHI+kUKX3U+76hkgvkkY2ZZPKAScT2VvgM8K3JcKU0a n7gJl7mWXToj/MflPyRLfpTAVzFv57otB2Q6gYB+Hk3xNtPkZzJA4wNuXzDR0s9ECy8= X-Gm-Gg: AR+sD11fwKTlh9ko1HK/+zWob8WDKRqV8olVsibmzbjLgtgPLkKkhy9hgKPi12AOF69 nOk2JNebqwiLO4nPDqBlVuxkXC6I9dxY/ZZUWHd1I3tqopAQvMapAsrmdh2V2uRuq5jGSryJV5m bH25vSNyTA8XNSUf4DTEOFMoLuJlAqvlHIZPr4kmwy1mpcWfkXdVD2+vCZov5BYQAa7Gd4vYsZj EW4eNeyjy1IVq52sEpJwHE9/jP+E+7i8RomKUJ4nYSPTV2/DcFwM3JJGba3JyUyd9hdHj237Fv0 PaZkfzLtPPRhpq3T+79+LbG8pklXnzpGKBDyLiXl+njOKZBgW/mJAtNxTDiDr+n0qIj64FiK0Yi Ph3TwRNDUyzrtvPZ2HK6M4CNE5K3zCXvZ+03Lnyhgnc4zvLMc2TnXQGtGLqbojUQyR1mPe+SyCi 3SU/wTdFE+MELEwWJCA7CIORgBJzbAcSMaIpDUzQasuODj7hn9XY7SHGfnQtRRI+KnWsJ6b2/m7 4UQ2DaHzGra9nV0fWreesFB77FEkBG+9gx1vo2WI3wMVmA9 X-Received: by 2002:a05:6000:46cd:b0:47f:77dc:60c6 with SMTP id ffacd0b85a97d-47fec502d33mr8037515f8f.1.1785932423038; Wed, 05 Aug 2026 05:20:23 -0700 (PDT) Received: from [10.0.36.57] ([217.73.170.83]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47febf7e5bdsm8209157f8f.0.2026.08.05.05.20.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Aug 2026 05:20:22 -0700 (PDT) From: Alexandru Radovici Date: Wed, 05 Aug 2026 15:20:07 +0300 Subject: [PATCH] rust: misc: add OWNER field to the `MiscDevice` trait 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: 7bit Message-Id: <20260805-rust_tmp-v1-1-7851cc1c00e8@wyliodrin.com> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/6tWKk4tykwtVrJSqFYqSi3LLM7MzwNyDHUUlJIzE vPSU3UzU4B8JSMDIzMDCwNT3aLS4pL4ktwCXQNLU0tLEzMzQ0tTMyWg8oKi1LTMCrBR0bG1tQC ewsLOWgAAAA== X-Change-ID: 20260805-rust_tmp-095994661956 To: Arnd Bergmann , Greg Kroah-Hartman , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandru Radovici X-Mailer: b4 0.14.3 Add `OWNER` constant to the `MiscDevice` trait to allow modules to prevent unloading while the device is in use. Set the `OWNER` field in the `rust_misc_device` example. Signed-off-by: Alexandru Radovici --- This patch exposes the `owner` field of `file_operations` to the Rust API `MiscDevice` trait. It allows users to prevent the unloading of their loadable modules written in Rust while their `MiscDevice` is still in use. Without this ability, writing a misc loadable module and running the following commands will Oops the kernel. # insmod misc_module.ko # sleep 10 > /dev/misc_device # rmmod misc_module ... after the sleep, the kernel Oops I added the `OWNER` field to the `rust_misc_device` example. --- rust/kernel/miscdevice.rs | 13 ++++++++++++- samples/rust/rust_misc_device.rs | 4 ++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index 83ce50def5ac..21c5d88c5dc4 100644 --- a/rust/kernel/miscdevice.rs +++ b/rust/kernel/miscdevice.rs @@ -31,7 +31,7 @@ Opaque, // }, }; -use core::marker::PhantomData; +use core::{marker::PhantomData, ptr}; /// Options for creating a misc device. #[derive(Copy, Clone)] @@ -126,6 +126,12 @@ pub trait MiscDevice: Sized { /// What kind of pointer should `Self` be wrapped in. type Ptr: ForeignOwnable + Send + Sync; + /// Set the owner of this `MiscDevice` instance. + /// + /// In case this is compiled as a module, setting this prevents + /// the module unloading while the device is still in use. + const OWNER: Option<&'static ThisModule> = None; + /// Called when the misc device is opened. /// /// The returned pointer will be stored as the private data for the file. @@ -430,6 +436,11 @@ impl MiscdeviceVTable { } else { None }, + #[cfg(CONFIG_MODULES)] + owner: match T::OWNER { + Some(module) => module.as_ptr(), + None => ptr::null_mut(), + }, ..pin_init::zeroed() }; diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_device.rs index 41e26c825060..bf8801ea3cb2 100644 --- a/samples/rust/rust_misc_device.rs +++ b/samples/rust/rust_misc_device.rs @@ -177,6 +177,10 @@ struct RustMiscDevice { impl MiscDevice for RustMiscDevice { type Ptr = Pin>; + // Set the OWNER constant to the current module to prevent the unloading + // of this module while this device is in use. + const OWNER: Option<&'static ThisModule> = Some(&crate::THIS_MODULE); + fn open(_file: &File, misc: &MiscDeviceRegistration) -> Result>> { let dev = ARef::from(misc.device()); --- base-commit: c21bb4193868a8de71fc4693fa741e195fdf5d86 change-id: 20260805-rust_tmp-095994661956 Best regards, -- Alexandru Radovici