From patchwork Mon Dec 9 07:27:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13898999 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 83C18215057 for ; Mon, 9 Dec 2024 07:27:53 +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=1733729275; cv=none; b=kLz6lwZImOWfJsn68T8WkgTRaQ/8I7go4rcqkGqF5x0ostXozjQmWGQLJk8fG2L9zR2ndf8fm/BI/tf93w5yFWtvIxFaNE0zj6s9O8b24yfnVDimBMppo4jJobh3ZWxgDm4IQbE6ujAOVH7R7xj+NVeyL6PS72Tko5PTZej8n8Y= 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.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="0wAD4YSC" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-385ed79291eso2458017f8f.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=Y3RpTcVbTCuaN/bxEGb+o1czDsFTwLctNUFVIGZoiQ9guDdrtlYlNlX3Qc7GtiOJ+X vNEyXHxDRsReO0LZOW1Byy8CFcLneh24zG6J7nGhMEzxaGl4YKCO9nnfwxCYMEI8GVTk aPpIw19pcI9lpjdVMfcjT3XSgYMzbh0MZL7Q5KNku78K9WGpH0wZS/eryseuEWnVqQ2d Pez2cdQhRRizIBkRblkgwFpRO2dmeVlpwckI/SrBaTDgtfb22rxTB5AphjAtmZ05CY1j CeEzgYPWzl+8iHYJst5Q+tIUxd6vXXAjO0Wy90o8Y7dT8C/hSjhH3YURuZyA+OxfDyf1 K+Cw== X-Forwarded-Encrypted: i=1; AJvYcCViYWyj4RTbuo3bqO9a4KDd3uKoqD8K0iSflR+Gl+ZjdbdZEwzZjYutckPn4w+aeocJv58mkTiNxqSPaL1R@vger.kernel.org X-Gm-Message-State: AOJu0YyT9NaQUysIuju3/qhU24iZ48DQzCKHlcOYjmsBiDN7fbtB7fRL vnNBF/p8bC2m0cdHZW+V8UTgDZ5HDDh38+e5QwtQ0Y90fZbf+OV3adWwX5xxCw6Lk6qed6jAfE1 oYNGn6fnS/RNimg== 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-fsdevel@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 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 file. - fn open() -> Result; + fn open(_file: &File) -> Result; /// Called when the misc device is released. - fn release(device: Self::Ptr) { + fn release(device: Self::Ptr, _file: &File) { drop(device); } @@ -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; } - let ptr = 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 = match T::open(unsafe { File::from_raw_file(file) }) { Ok(ptr) => ptr, Err(err) => return err.to_errno(), }; @@ -211,7 +217,10 @@ impl VtableHelper { // SAFETY: The release call of a file owns the private data. let ptr = unsafe { ::from_foreign(private) }; - 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) }); 0 } @@ -229,7 +238,12 @@ impl VtableHelper { // SAFETY: Ioctl calls can borrow the private data of the file. let device = unsafe { ::borrow(private) }; - 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 = unsafe { File::from_raw_file(file) }; + + match T::ioctl(device, file, cmd, arg as usize) { Ok(ret) => ret as c_long, Err(err) => 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 = unsafe { ::borrow(private) }; - 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 = unsafe { File::from_raw_file(file) }; + + match T::compat_ioctl(device, file, cmd, arg as usize) { Ok(ret) => ret as c_long, Err(err) => err.to_errno() as c_long, } From patchwork Mon Dec 9 07:27:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13899000 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 BC800215075 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=1733729277; cv=none; b=NEukC7osCSbeU792/0w+EoZoEH4dIMdq2j32ryqJi58dxmuNC3PoS2zB+Dl0jqUleL+fgh14jlmcxFofMP6V7FRqZU2uGNtjutz7HgZOCw1Ne2ZSrGAIZRiySYA+oLSyA1FgIAZrfKfGpHllbJRtPSs1FDyxY0uBRykVXaDxmcY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733729277; c=relaxed/simple; bh=FWr1r4ozOgDQM//rzpJtuNZonQdDYCyJwDGoBu/nO3g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hxyc0mDb6P+dbXEPN7YlH4JH7ptrYo5CJkx/iHXrA6QaRP8pY0uQRhVEEru1qIMKvGWapzEW8VgdMcGqt16iZ0v93HW9gXcOSk9itCMQkcmFRD0tutnDQLekV4ES5DNOa+h+PAsnSYQk3vuB89QUnqbSV+G6f7kGaLpB97uWdEA= 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-385dc37cb3eso2228192f8f.0 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=ORQvaSlkPVzxdTE4vXlrS0Dga1+J3clc5/Z4Tlzml+SXQXmijzMrSQyPCEFATC/836 8PrX9k1s6g4N2gUmnpAhivnkgEPgiH1+JwOJh3FuhXqtiUxj6aLIvyY31O5yVKJHqm2V uUvJ8W2xqdFT5oyoxaDoT4/AeyqQ9vhRdij9vc6kE1606/E/iLL5aEXk97a5146NcvA6 LGMsun7XolK+9JBzNOkEgBdAldMojARQ+AW9Lsuag9PxDHaUhddKsUgXrhiXLfH+iDkt wZhFuMw6OQkmSq9zp8a2+T4aQb8sWiaUvKHy6uS6DREqF+Pn0gjcgBnhOSIwvA9dGTcd bg/A== X-Forwarded-Encrypted: i=1; AJvYcCUL7XI/ZKYjjeYTXriSxiGfTxmpjTOS31qhQZNZ29AgbnAV0Ot3Z/VKxxXHrVNi5gt6Jo55u9LM6D154eyi@vger.kernel.org X-Gm-Message-State: AOJu0YzemO5Pzd2rQXbojwQpOX78jDLUgagXT9pW8zqR4RldBNAAl530 c21yLq7thns3IJabLH2yFA5f7SmLMOpoTZEM57UpP7Y9xtVVHCK9z0jp7vpdN4TwqSdzHDFFTvT UR189O9E1zsjafA== 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-fsdevel@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 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 file. - fn open(_file: &File) -> Result; + fn open(_file: &File, _misc: &MiscDeviceRegistration) -> Result; /// Called when the misc device is released. fn release(device: Self::Ptr, _file: &File) { @@ -190,14 +190,27 @@ impl VtableHelper { return ret; } + // SAFETY: The opwn call of a file can access the private data. + let misc_ptr = 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. Furthermore, `misc_open()` + // ensures that the miscdevice can't be unregistered and freed during this call to `fops_open`. + let misc = 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 = match T::open(unsafe { File::from_raw_file(file) }) { + let file = unsafe { File::from_raw_file(file) }; + + let ptr = match T::open(file, misc) { Ok(ptr) => ptr, Err(err) => return err.to_errno(), }; + // 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 operations. + // // SAFETY: The open call of a file owns the private data. unsafe { (*file).private_data = ptr.into_foreign().cast_mut() };