From nobody Wed Dec 17 17:40:03 2025 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 995BA1EB3D for ; Mon, 9 Dec 2024 07:27:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733729275; cv=none; b=WgNDnWBGh3V9iiQRacpku1PZpI3YqQX0k+73lrdnyxCmFLW4/jzy8K6MifQrOSqM9Dp3/iQ4mP6v8Ww/c6BQz+tsalcdZe/Iwbw3kkvav1lOSJYzA5g8NK5jIfuRM6qDKddQ828zMgBwsaZGOFzT265UYmGvl8QqyUD1fscl/mY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733729275; c=relaxed/simple; bh=4Wv8XaavpPEkhYsV9Ea5hOf7zaXq9QQ7WtxjCJrCTT4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=f7EYnjPXoBohwuEqkscY5pjl12CX5Xt2qzsnBkkd/fuD4p8KFecCMMJBQhYqVbLRV71Gn+hoKMZOR7bfmlvHDJ/SPBU69aWJy6DnEh4v926GUtiLVhut8lXAgvj0DAZ+9NUGzTKgwnBkHjWePjQffSyyKm/DNmLFNxCwCyRKQrI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=0wAD4YSC; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0wAD4YSC" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-434e8beee61so7580025e9.0 for ; Sun, 08 Dec 2024 23:27:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733729272; x=1734334072; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/S+CUGTEWVFRiC7zZipkdW5Fs9z3rjt6UxWYN0Sev4w=; b=0wAD4YSC8D3r+QHTF5Aey6xdT9so0ScTEyxJ4Rf5tWTF2zkrQHQBnP7csvJ3vSkq7A fJX7E65J/eTSEhoBSKq/U8STh8AygvaM3ANfcDANf6e8saPOMHjdWVgtqB6HH7X1FRcn DnQT2bTw4AQ12g+9PUxQAhhlvDhyMP4Q13feo76gYFq11vgkhxpptoPmy8ITJfOmoedU I/GOBz3QuEnFSumG6kFRuX1mlOt061dNaJUuU8tsjF4baCIr9V9cPz9lNt6EQOT77eHv ryMXBkFXMabDz+sskq/iomBKRWB+W57W3OiRvglcXECxaqxl9JkKrpiVSAYVsCFrmUiR Dk/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733729272; x=1734334072; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/S+CUGTEWVFRiC7zZipkdW5Fs9z3rjt6UxWYN0Sev4w=; b=axrivZ1qsyiAlPcp24M40qZhD0isPtJS7rq4QAeaFXY+wOJbFSvsGWrb5/SscGDLdN uYyXS+xGQ7bgS8jJpiYh5jYwGb16+oOT5CW9XapTGL7VaEN64+gaXe41K+2FB0MkhPCf dCs4z+7M8JFVGC1aKbaVkOVYPTMPXlJiG2iPHGys9pCWzv7YrQ+Prj6WwzDkpkctI+Yl JPy1n9q0ShOwxiAJBbwr8mZo+1AEmtMsZ1kqvTtRy1D+phzXOTFedqOfSXv/zYOVE+mX ZzoY02sZRD4Pmt4tPf3yjUZ5rIccpDvDXLHDD1MojPxB0aM0kc3acxhnOhh3bOczgZiD +0qw== X-Forwarded-Encrypted: i=1; AJvYcCV3QDGpmLxEZNLhbncZI/cqqUyBgI21DRZ8qqBh/K5YB/kivN3131v0C4DySwWUiNyE1kNAkfAJxtUgFDw=@vger.kernel.org X-Gm-Message-State: AOJu0Yw+wAY/qZ9nnFonqB1VdAYPzAk4l9ONHbSUav6cuvOT2hzASAWz XTgzQYjRjk7Noi1WgkTC3dggMAj99gM/1+9xQUnmrBAIwu9pOMOYSgxb4cvyR1HCL6Ay9o94SBA D4GVS38nIYiTfkQ== X-Google-Smtp-Source: AGHT+IF4fXcegEacu/CEqu7D13ezG4G2Jp3/NH06VIcf5I8sePb0lG3RnpXzlOzTCeTlh2Jn2mA5dk9zEBliIBE= X-Received: from wmbjz10.prod.google.com ([2002:a05:600c:580a:b0:434:a10e:91b5]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4f4d:b0:434:fe3c:c662 with SMTP id 5b1f17b1804b1-434fe3cc9d8mr1637385e9.12.1733729272035; Sun, 08 Dec 2024 23:27:52 -0800 (PST) Date: Mon, 09 Dec 2024 07:27:46 +0000 In-Reply-To: <20241209-miscdevice-file-param-v2-0-83ece27e9ff6@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241209-miscdevice-file-param-v2-0-83ece27e9ff6@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3884; i=aliceryhl@google.com; h=from:subject:message-id; bh=4Wv8XaavpPEkhYsV9Ea5hOf7zaXq9QQ7WtxjCJrCTT4=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnVpvyrUjxyVkMoE8OuerED5jSUGVlD3r+ZHL01 HkkyF49nx2JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ1ab8gAKCRAEWL7uWMY5 RpcfD/sHaTU4u+iSb8IEIAjxXOQLmHD2uiKdzbATrwH/DrWuC6jpN5wp0szwgcTGjP1fBSkWv9c c0x2fDlE2/8QCHdfeaNNVZUi2H9VY489h16ZEJB8TaTiWIh4ot6fO9SMH8nGWbSVQpKTWXIFRcF jJNk4YOeeIsenfKem2FvZ0lWoqw0QcuuRDxTOb6ZfahUPb8jSdr1HvBVhQffCNehosVGoNm05/y Jkps7qk/1NiQWxMveJmny1NQtf40VqvnoCy2iyflATfT7r3+3SUdEQ1qyW8RVItd8Hn39qkR+SP CmnBY53Ei5W0IQ/T0fpprPqDupoWQQw0w7aChO9I4to6Wu6h2pxbTBPI2mNr8Zn3H3KYsQhw/0g Wyw3qj0dZc5o4vsz4KUMAVnNUWLjsuy6fMuphGAvG+TPwYBfh4SC+zwytQYUJOwiQpb87R7UoY9 IXuol4uu1M6gF8sMM8QIoMBc3ws3M9oHimxO2KNOqZM+LqPQDPEe09qPGKHiugVyc//u3dbcxQw RnZMZK2u8+FgWDpV0UQYXKl5/Rl4ItTKt+NVWlxV3XuHASmWVjbdfVt9xhVnQBGTCz+2L1FVsBZ 7kr4GjVbHNVxQmAjErRMfaZl/PlicIM3OguWXEEJg/+owL8A8ubv/hUHJw2GfyN6A0M5yBUF1To 1GBJkGjguSDwFSA== X-Mailer: b4 0.13.0 Message-ID: <20241209-miscdevice-file-param-v2-1-83ece27e9ff6@google.com> Subject: [PATCH v2 1/2] rust: miscdevice: access file in fops From: Alice Ryhl To: Arnd Bergmann , Greg Kroah-Hartman Cc: Alexander Viro , Christian Brauner , Jan Kara , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Lee Jones , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This allows fops to access information about the underlying struct file for the miscdevice. For example, the Binder driver needs to inspect the O_NONBLOCK flag inside the fops->ioctl() hook. Signed-off-by: Alice Ryhl --- rust/kernel/miscdevice.rs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index 7e2a79b3ae26..0cb79676c139 100644 --- a/rust/kernel/miscdevice.rs +++ b/rust/kernel/miscdevice.rs @@ -11,6 +11,7 @@ use crate::{ bindings, error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR}, + fs::File, prelude::*, str::CStr, types::{ForeignOwnable, Opaque}, @@ -103,10 +104,10 @@ pub trait MiscDevice { /// Called when the misc device is opened. /// /// The returned pointer will be stored as the private data for the fi= le. - fn open() -> Result; + fn open(_file: &File) -> Result; =20 /// Called when the misc device is released. - fn release(device: Self::Ptr) { + fn release(device: Self::Ptr, _file: &File) { drop(device); } =20 @@ -117,6 +118,7 @@ fn release(device: Self::Ptr) { /// [`kernel::ioctl`]: mod@crate::ioctl fn ioctl( _device: ::Borrowed<'_>, + _file: &File, _cmd: u32, _arg: usize, ) -> Result { @@ -133,6 +135,7 @@ fn ioctl( #[cfg(CONFIG_COMPAT)] fn compat_ioctl( _device: ::Borrowed<'_>, + _file: &File, _cmd: u32, _arg: usize, ) -> Result { @@ -187,7 +190,10 @@ impl VtableHelper { return ret; } =20 - let ptr =3D match T::open() { + // SAFETY: + // * The file is valid for the duration of this call. + // * There is no active fdget_pos region on the file on this thread. + let ptr =3D match T::open(unsafe { File::from_raw_file(file) }) { Ok(ptr) =3D> ptr, Err(err) =3D> return err.to_errno(), }; @@ -211,7 +217,10 @@ impl VtableHelper { // SAFETY: The release call of a file owns the private data. let ptr =3D unsafe { ::from_foreign(private)= }; =20 - T::release(ptr); + // SAFETY: + // * The file is valid for the duration of this call. + // * There is no active fdget_pos region on the file on this thread. + T::release(ptr, unsafe { File::from_raw_file(file) }); =20 0 } @@ -229,7 +238,12 @@ impl VtableHelper { // SAFETY: Ioctl calls can borrow the private data of the file. let device =3D unsafe { ::borrow(private) }; =20 - match T::ioctl(device, cmd, arg as usize) { + // SAFETY: + // * The file is valid for the duration of this call. + // * There is no active fdget_pos region on the file on this thread. + let file =3D unsafe { File::from_raw_file(file) }; + + match T::ioctl(device, file, cmd, arg as usize) { Ok(ret) =3D> ret as c_long, Err(err) =3D> err.to_errno() as c_long, } @@ -249,7 +263,12 @@ impl VtableHelper { // SAFETY: Ioctl calls can borrow the private data of the file. let device =3D unsafe { ::borrow(private) }; =20 - match T::compat_ioctl(device, cmd, arg as usize) { + // SAFETY: + // * The file is valid for the duration of this call. + // * There is no active fdget_pos region on the file on this thread. + let file =3D unsafe { File::from_raw_file(file) }; + + match T::compat_ioctl(device, file, cmd, arg as usize) { Ok(ret) =3D> ret as c_long, Err(err) =3D> err.to_errno() as c_long, } --=20 2.47.1.545.g3c1d2e2a6a-goog From nobody Wed Dec 17 17:40:03 2025 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (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 BC78C215074 for ; Mon, 9 Dec 2024 07:27:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733729278; cv=none; b=CrddiAGfpECdvY1bQZfm2kQKYw4A1E/3HY3fc1P9W8yEStz5OvStefI/yI9fHzkQkY0N8T8lGu5jgCqfkVpcB5bnaz1dn6zm6Kn/VEGLJtE0KTg0D2F1gMZxldcdynFyFb6AuooWf6/UgZMaP+PgywhuH7+VBZisvbAVbuuCHKA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733729278; c=relaxed/simple; bh=FWr1r4ozOgDQM//rzpJtuNZonQdDYCyJwDGoBu/nO3g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CdtZv0GyVMaZVeGaN4UQGO+oQuQ7tDIOxpwre3xDjKCxu+7L9IBR+V7lNW1sa08gVaxsMIxKQ4NI8E3W34iA/x9ifHm5PowpfgexO5Sgg09V/eoG/VJf8cWcgT7lkh6SS4g46Oni/87s3G1h4LZJkXFbISYNwQj55vKHgLxF93Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eewrZq4X; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eewrZq4X" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-3862be3bfc9so1447313f8f.3 for ; Sun, 08 Dec 2024 23:27:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733729274; x=1734334074; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4XPOCESBsP0vZv1p3EyO3nbPR+4ED4LDct2uLEPo3zM=; b=eewrZq4XXgwyfTZV+aY7SaMRFMUDnPAUaUU/GkxMRavWzRpEsOcOc86SlCb+HFGxkn 1whA5wmUAwhGxzgxtn6gyau/sl8eteN26gCme7DpWv29YXhU2GBajLy2sf0n8AZTw5et iQipwDgkI7urJUYFtVKatdjv5pS1s448KIksXdcUFDer2FTygMrRj+dPZFJV/+HEL2TM 2fsdy6xXs/lJ8H2o+h1iiM3aIrT/iUQwvcD0vah7YMZCv8dzk3BeWUgufRmClk5g7fdx neRQabyqwnXDLv3R+le56mDmHZWSBfexD6nQV4HNhzjjCgRXRZSElYooqjil8VKntq7b VAUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733729274; x=1734334074; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4XPOCESBsP0vZv1p3EyO3nbPR+4ED4LDct2uLEPo3zM=; b=s5z7jCQam3+r8Me39vvo6t7uJcwfjC4d0YYGJeNm/X86T/rRoG5BVjHn0QBzL9vaSi jGQsFUvh0YMSJog57AUT3+VyaVRZEzFcf5bi9nhAcQ2ohIfgCrYIMoWufyjcofmJENtc IXlTCHzBqM639hpcaqsENNdmt3gPqM+k8PlPkaINdPKsb6Hb83zS2VYOTvrWGMlmAqkZ FbwaTcqxyKPzxw2KX0jn52hzTFsZC8VksaWvdjakxV7nun1Y+EnFPJiWZ8GjzahSpDYB AKx6G+bWTpNnI/z/welCy5WDlzzKkH10pwuupKgy63BsNRpaWNOfTlcgh1VewDwx5szt M4zA== X-Forwarded-Encrypted: i=1; AJvYcCWvzAXsIStbGl6dEs0wqjOvQcS2cAOQVUi7wBWtLrfHU3IXREzw/poO/mbBGnzfts+9kx04BtL3Tgsc14M=@vger.kernel.org X-Gm-Message-State: AOJu0YzxHn/+WpDfmqhG37xZyIb/zcsY1Ioas1zRYZJejwAiHr/1LvZX 9JKjH3WOn8Uo42KntpO2G4nvHM1kWrUJNeQIIZ/J7sCynRMguA7dfqbQZ19z0ALMnDlslHIQrhM HVGp9voD5skGubg== X-Google-Smtp-Source: AGHT+IE+8n+YNaesbwQgkF7DuZhPvB/qUurkVyjBccXautnlKjRjbmYOfixA2dZ4HsDJ8MCVrCjM+1C8pCdaZw0= X-Received: from wmbjx11.prod.google.com ([2002:a05:600c:578b:b0:434:a0d3:2d57]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:1886:b0:386:32ea:e70d with SMTP id ffacd0b85a97d-38632eae7d6mr5955297f8f.50.1733729274252; Sun, 08 Dec 2024 23:27:54 -0800 (PST) Date: Mon, 09 Dec 2024 07:27:47 +0000 In-Reply-To: <20241209-miscdevice-file-param-v2-0-83ece27e9ff6@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241209-miscdevice-file-param-v2-0-83ece27e9ff6@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2748; i=aliceryhl@google.com; h=from:subject:message-id; bh=FWr1r4ozOgDQM//rzpJtuNZonQdDYCyJwDGoBu/nO3g=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnVpvzczpItN+OHuxwE7AplYvqF8F4ygxK71kpY +1momvuEi+JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ1ab8wAKCRAEWL7uWMY5 Rq80D/0fW3WTcWRksYkWcUvY/qViSnYfrUeKeXWmxAEGqeMdv7TEkHhe/uF8uSrsQOZoXhJvNpk 2mHN0Ei5SXxfGjXp66h332dEJ6/H5BXQIKJRH5rQkVXH7LoqyiZzykJ2RxTmWz1mDrWQ99D1+9u BGvPNAOYM2kRp1FYHlKJXcTJCz1Ay6GCwyq+vC5qSbDyIFemehOFdIW5NfToeZcvop67DbMa/Mv 70/vrALK/EqMXa4Or0xhYPS8wQKV82vRgwLjLWCt3GQzhN9OGnfw74ymRNlsgXpdJBK2anhExW0 xeq9RDGLw/g+KI7WGzk07iK2zFQB3zto2KmVZblwZJquHekcD/X6F+k1npnt0AQFP6xmmerQfXh R3U7qIDzdyW0KhOz6SCSINL39hzMkihYtY0y3S2Q9iKIKFPOsDYZccN4/SU/s/7Db6hRb9xb+79 KaHUjM8WWND6V4biwumgn+Wn8d9lnB2uOAVyIrgBf34fUCFSM1o1aDQafg+e2CyNOlEo6jybPq7 Uvm+oDmSLf/mjnC1x46YAbMGGzPYYmz2pXLxiFrWlO2RfUM8rEZ/DhWTW0NxXnCvJMyKVDYCLJe IKWpfxj5s9l79z9S5xpVQT88ol0DADaNNYPQsQ1h7i3bvl9gk5icUYQ4oM5j8aJS7A68Y9uKFEV QLmXlkBJJFxY93w== X-Mailer: b4 0.13.0 Message-ID: <20241209-miscdevice-file-param-v2-2-83ece27e9ff6@google.com> Subject: [PATCH v2 2/2] rust: miscdevice: access the `struct miscdevice` from fops->open() From: Alice Ryhl To: Arnd Bergmann , Greg Kroah-Hartman Cc: Alexander Viro , Christian Brauner , Jan Kara , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Lee Jones , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Providing access to the underlying `struct miscdevice` is useful for various reasons. For example, this allows you access the miscdevice's internal `struct device` for use with the `dev_*` printing macros. Note that since the underlying `struct miscdevice` could get freed at any point after the fops->open() call, only the open call is given access to it. To print from other calls, they should take a refcount on the device to keep it alive. Signed-off-by: Alice Ryhl --- rust/kernel/miscdevice.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index 0cb79676c139..c5af1d5ec4be 100644 --- a/rust/kernel/miscdevice.rs +++ b/rust/kernel/miscdevice.rs @@ -104,7 +104,7 @@ pub trait MiscDevice { /// Called when the misc device is opened. /// /// The returned pointer will be stored as the private data for the fi= le. - fn open(_file: &File) -> Result; + fn open(_file: &File, _misc: &MiscDeviceRegistration) -> Result<= Self::Ptr>; =20 /// Called when the misc device is released. fn release(device: Self::Ptr, _file: &File) { @@ -190,14 +190,27 @@ impl VtableHelper { return ret; } =20 + // SAFETY: The opwn call of a file can access the private data. + let misc_ptr =3D unsafe { (*file).private_data }; + // 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. Fur= thermore, `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::>() }; + // SAFETY: - // * The file is valid for the duration of this call. + // * The file is valid for the duration of the `T::open` call. // * There is no active fdget_pos region on the file on this thread. - let ptr =3D match T::open(unsafe { File::from_raw_file(file) }) { + let file =3D unsafe { File::from_raw_file(file) }; + + let ptr =3D match T::open(file, misc) { Ok(ptr) =3D> ptr, Err(err) =3D> return err.to_errno(), }; =20 + // This overwrites the private data from above. It makes sense to not = hold on to the misc + // pointer since the `struct miscdevice` can get unregistered as soon = as we return from this + // call, so the misc pointer might be dangling on future file operatio= ns. + // // SAFETY: The open call of a file owns the private data. unsafe { (*file).private_data =3D ptr.into_foreign().cast_mut() }; =20 --=20 2.47.1.545.g3c1d2e2a6a-goog