From patchwork Tue Dec 10 09:39:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13901129 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 09E1B22332D for ; Tue, 10 Dec 2024 09:39:20 +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=1733823563; cv=none; b=OmNujAlQazvi+wDrf83ihv436auz5MGaSUZZME473VnuvNH+Y3KR7esdYKwSG8r4A6IljYNQS9VVcpPI1UWLZ00ZLggt30jc5nP+ArOh+5bC+HTNTeb7GMMiXJ3YEzsq024+3SmmUf3CheQS1+MQzlTQEVtHONQtbF8QP5PN6Ec= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733823563; c=relaxed/simple; bh=aek/y/cbvMTTZ9r/rORZzJl2SZOCrcfNQ8ty0c60zNo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aDQV3oRT1b2cW7Vk/U2b99SfrqolY0DEfUgM+kumrQN09sCutAozkKhvGtL1xrPA006FQVAD5McpNhX79FpeK+H3nnmbeqlGjhvpSTG4YceMwHBR94S0Le5TsAzAah4yOkKe7q0by/rw7ONmHNcyW2WXWwkJh9bpQ2TBoNsOVaM= 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=p5x6Bsd7; 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="p5x6Bsd7" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-434ed7f596aso23765225e9.2 for ; Tue, 10 Dec 2024 01:39:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733823559; x=1734428359; 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=2kWaVy45L0tBSJtf9uYr4OfEOAz7jglrJk5DHXJ8wYs=; b=p5x6Bsd7FIFan08ers5gnAoYLzMrAhPEOIvQFiskbLZxDXWfuzYhWY42cogynbUzLb 0Qyt+vzOlC+lHq6lPja+TTFsTLiQuMp0RtMZTwjzb2EmU5iCNaJ1WoiPHCTyWh5uorG/ 5prSbbYETswWzG/pBIhNJFlZRwUVE1tprwjxKfz/fiwAmCAUPwLIhrTD9d3pdxehhv/O A9Av19QcQnQvmsquBozkVidQC27dGC4B3WJGeaeZMrM8AzKHVtbUMAiJzFWEHZmtmuK3 7s04Zt+ChXFHVbxvmHa/CZIdETBtlxWjjyZVSX+QGkpVIcZNvr0zYT2zgUo2T09vvY8a ESXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733823559; x=1734428359; 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=2kWaVy45L0tBSJtf9uYr4OfEOAz7jglrJk5DHXJ8wYs=; b=t/khRpBjk9Fj0H9fW+ii0ITAHX14m8akrkMhqKUiixJ8QrfCFyNmhuFky6Eb22/nz2 lwOG3bhnSqMDhXIdxNFH6qh2X7Rn8LYoPubyc4C0YdM5pzuCZvdIqf0AU5pxrndr+/Fv aT7YemSeR641dU+fRDzczikWzv14rLTmRvQMy2emLaQir3sgIdRFQ0aNiMPJhJwHsCBz E0QJHOHr4uTTPEwV313LA03Lwa5tm9I0FqguhgKgP28z+vwOFe76WYCUhflnMzGTsdJy 4ib8VjhdUx6Ql8PaYcYrGfEVa7UomXLQO08ON7THK7++QYx7zRibnQNnunX4yuUlT7F8 fYkA== X-Forwarded-Encrypted: i=1; AJvYcCWItkiBU5JOJq9eWvV3suNnBCZys0OCMC7jL5yFb+5mdjAOvhaYkKYla+FxbVhjPu2+Q3zINbcX5jWN8bnO@vger.kernel.org X-Gm-Message-State: AOJu0Yykgz/suaIkzmo6N8livk8St/qHXoeVxcfMN1xvvFCLdfhErY27 KGrd+t+fIF8ryFvp+jECXE8om95G60/gjV1KLN0wmfqOq2vaxlbWpGXWK140Wmfz4S1BTrtpVdU D8A3eciJg/FKjMQ== X-Google-Smtp-Source: AGHT+IFW3BnNsQzFJv9xXvweoZrl0STZsGsb/9OZ/j7ipAtnEX4zMq/6qndxSYkuWRRnQB92Kkbw3FDvNH4rdQI= X-Received: from wmcu17.prod.google.com ([2002:a7b:c051:0:b0:434:9de6:413a]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:358a:b0:434:fd77:5436 with SMTP id 5b1f17b1804b1-434fff500a4mr35015645e9.15.1733823559619; Tue, 10 Dec 2024 01:39:19 -0800 (PST) Date: Tue, 10 Dec 2024 09:39:00 +0000 In-Reply-To: <20241210-miscdevice-file-param-v3-0-b2a79b666dc5@google.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241210-miscdevice-file-param-v3-0-b2a79b666dc5@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=aek/y/cbvMTTZ9r/rORZzJl2SZOCrcfNQ8ty0c60zNo=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnWAxBrZpLxFs/Jyz7wOv9uc43/vuu33Sa3L2w0 lKlxRSDAA2JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ1gMQQAKCRAEWL7uWMY5 RgseD/9CLIBbvbxAG4DKD7RUsA9cRKhZzhKtiqV9EROvRgA/QuTQDhpYFU3SxObTfD3t2C37uOz 2eHoWKIrE/0BVrnxyGbBsgnjNkikyB2EFdLJW4+Gcyl814mkSqDfluYyzKu6nUy8pbjDU/MVBLJ kn21x1dTIdpz6i9K507CKUW484Y3anF4drN7KGp/EpQGyY8n6VIZryB984Ex5Ab1yN7dfUFPpTk xIA2HlAF6RIYzVgAMGNWEhv3PssvsWX20ILGSS97RRWk5SMttfVcUnJUyhrGmDSs5XNtyFXBY3s JPyO7tjLwusevDOaORDhHxRSQM1dbLKK0loFifShzr2afGsD4DGxH5IFaCFwgbePs8a+wKJIk2s QyHDmV5HZ7EpryN0tGQwFCwSyvSaTA6s6O67EDyDmFxALamQM6zxM1miclSUwJFs6bX5HsHGMaF STljKqPqsi/A0509wWGSj+8PidpAxpdv/mpiY1CsDN0TmRmbyGN5WxHXLowhDJHTWsnDL/Po1kq 8+ZYBZj1lJUKJsud4on4eluYXRe3isXw8MadI9KhYRcilP9yDIuburyP33/rAibJA9v2qsk+xQh 0CPO17bhwSWgpky68xu5EooZaWX435c6qsBLJ5HbyuxX1DmuRBw3FIGxRcIRkCl1zaoyEHYYoG4 U6SV6eK0Lw6DrJQ== X-Mailer: b4 0.13.0 Message-ID: <20241210-miscdevice-file-param-v3-1-b2a79b666dc5@google.com> Subject: [PATCH v3 1/3] 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 , Danilo Krummrich , 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 Reviewed-by: Lee Jones Tested-by: Lee Jones --- 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 Tue Dec 10 09:39:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13901130 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 4AE4F223E7A for ; Tue, 10 Dec 2024 09:39:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733823565; cv=none; b=U9T/adt/72J8DG2HdpDYZLFTAVELYyetJ0tejSCEAJjZLtF7VjsqRL7ajoXCxJT26x1Qn+Vy4cUGIeL1MF9Eei5xAR6M8xzAkyJ5QcYI3q0Vg37KWZ+XJTEaOlbuKsGKblI9ZHjGLqiICK94FqNvq4xw3tS6m1pWG2B21Jhju0c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733823565; c=relaxed/simple; bh=TIZgaELzS4o7ZQ2rRybQ/bDaOlh/wZlUNZbESQXsjQQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HLPotcNG8dWZqFhdn7zCkY8/fntT7GWG82VzOt1exij0h8NLp8gqO3WOAvsfgWFacg/aBYFiBzqXepihp/Gu4LtHw1SaA1W+TaUBlVyQXa5KLkEL241ytsSFwelJG8BJVpDoJ8XqxmencNhLaE3zNfAsXuMRwMB5ak5qXRE4Tcc= 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=SHmVQEka; arc=none smtp.client-ip=209.85.128.74 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="SHmVQEka" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-434f1af9d31so14587045e9.2 for ; Tue, 10 Dec 2024 01:39:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733823561; x=1734428361; 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=YFa4Sx8o03OpDIf4yd72th5gbNsT5Y/F06Nhp3ZErPo=; b=SHmVQEkaC0ZgiaavyLj24Df6FPZJr/SU2t7VwnSWioVcEpPXsT7e6XsZi1lwVK8iTy ldl3ym+YcJPdq0cCPsL/NHUPoc/d+OE71TlMsm/UiAKOlAZvshDqhAZPat3wEFk5+dut /W2euzF3FRKEgG6lC1VOGx9jGSKB33sCgV3p1T5x5xc9Qb7osJX0qK6k1t2PK/h9ukia MppCeUdZV6jC7OZDYZBY6mAjQjxU7D50CyEkUoTNbdaNHIvcC2B1szVwjWfR+9LgRK2Y 1atjo1JUu1cctHLf5SO6qcJ9iMq2gz5Gd2QJ7PMyDJ8EfWx853xRQ3G7kKwOcqEL0K+p s+Hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733823561; x=1734428361; 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=YFa4Sx8o03OpDIf4yd72th5gbNsT5Y/F06Nhp3ZErPo=; b=ICD0E8Hhx6s7Ym3B0405cv/lkX62qur4lGJ8JFjtsvAHkQUeNAX8ABeeY86RAhzrS8 8UGCalqFrphBuOKh94VuwHN2huiaavVjfQW2WHIzrI2PY2DUWByUo9ZPDO1XhQZt9APe iajISOdBvunm+Lg4m1HPlYAZekQUjxqVvbcin0NkyFoHJLdij2NREgnmYPLwUaUgVJ5F yDYElNF0tg1FDBuHqHu5+jc8kzWtoD9WePk/DrzywKQHOgHR4G/rVA7Yj/Sq0hR7VdT/ Bv2Vwx9Gmj6Cdp/WuMs7ZLIGJ21FPhsLtOVgGmqY3LivvxApjQfiHwQ+2ytUVsHJI1/M uN5Q== X-Forwarded-Encrypted: i=1; AJvYcCXVcV5ZjW2ic0NZLAAEiuRyJbh0b1p6V3EQqLdNHox8Wj/NH1shsoYUys/gfI4kwjEn53sa9ubs66+v7+HX@vger.kernel.org X-Gm-Message-State: AOJu0YxuEh1i7Re3X0zZ8INxSHtyBLdCranKat8shBQOJdyreghVCFQI e82JmGtV7Y+YaJPqzaRlIyE2eG2EYzMFoEwhDUPyuYyWlsjTFdrJ5/0DrX8oqRnFeG3+fx7DNkb XjOF9Bd40ku9vEw== X-Google-Smtp-Source: AGHT+IFTAIsoLjCnseunZkxQrOKLy3SO/tU28cLm2zGgylonn6ozjaJ7xFiN74e3V5QM1iTmYhSYvxVlSI32agI= X-Received: from wmsk4.prod.google.com ([2002:a05:600c:1c84:b0:436:1673:20f2]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:4024:b0:385:ebea:969d with SMTP id ffacd0b85a97d-386453dd647mr2609026f8f.22.1733823561743; Tue, 10 Dec 2024 01:39:21 -0800 (PST) Date: Tue, 10 Dec 2024 09:39:01 +0000 In-Reply-To: <20241210-miscdevice-file-param-v3-0-b2a79b666dc5@google.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241210-miscdevice-file-param-v3-0-b2a79b666dc5@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3917; i=aliceryhl@google.com; h=from:subject:message-id; bh=TIZgaELzS4o7ZQ2rRybQ/bDaOlh/wZlUNZbESQXsjQQ=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnWAxCQ1h2WpVVpnD0aW6J6VK3++n+QS/SZnSaX rEfMES1WVCJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ1gMQgAKCRAEWL7uWMY5 RhI7D/46ycYdrqP5Pz7sFmDy42lQFhujE1YwfYpsec2TpPTd4tIK9CIul6vG/mJXJzNjdVainmD KLRHFAv2yIfkmzmGbITsotYwcruhMV9fhjEIIa88SCpPUGoFHH6hHzFzKHFrWzGJxIc1/JRGSbW I7Hj/jHBkG3WSce8/GBA5+8ES78iSWj81cBBMXoBKE00jCU3eShL6lIr93nBK0I5qwPzdN2BH7+ HYBoXmXrHUgHJV51fbRZaP6GGMRLjPclhL6FoIVi3PN2au2a9OFaXu9zHC22IbOWukkcGrH+jfL +hp6fr6jOK2nS0GBRupsJab4Grqkv952VUk+lPtwZi6Gu/yjGCkLvDZNgyoD9OssBqVCe6yzJ/y jf3Gc2mAQOdAJ4uYdn5xsUn0aDxqzM+97iXCSdWPh4xk+A/oxP0IYIs7LXKpvl8ymFBjuSZBjUI ejXvA5cFrnkajiZpGW59mvvXbqroV9tggdDeayGIUF5dnuVVYPmUXddGeLKS2EXDoizBdqMdkRB IjimKnSnCXHHsH8JMgLZXjFjxrchhxnpR9Q0O6xCPGhfKzYzfuRSplXpo+96h4gH2O3OIernJ/Z AdXGX9q7up8xZ6Y8KLlsAX5m/BJmu5d0avcH/r4nBPj9mLhSz+qiU7J5lPKMVH3/ftK3XTdv7x1 aLVfLqf6VWwb3BQ== X-Mailer: b4 0.13.0 Message-ID: <20241210-miscdevice-file-param-v3-2-b2a79b666dc5@google.com> Subject: [PATCH v3 2/3] 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 , Danilo Krummrich , 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 (if misc_deregister is called), only the open call is given access to it. To use `dev_*` printing macros from other fops hooks, take a refcount on `miscdevice->this_device` to keep it alive. See the linked thread for further discussion on the lifetime of `struct miscdevice`. Link: https://lore.kernel.org/r/2024120951-botanist-exhale-4845@gregkh Signed-off-by: Alice Ryhl Reviewed-by: Lee Jones Tested-by: Lee Jones --- rust/kernel/miscdevice.rs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index 0cb79676c139..75a9d26c8001 100644 --- a/rust/kernel/miscdevice.rs +++ b/rust/kernel/miscdevice.rs @@ -97,14 +97,14 @@ fn drop(self: Pin<&mut Self>) { /// Trait implemented by the private data of an open misc device. #[vtable] -pub trait MiscDevice { +pub trait MiscDevice: Sized { /// What kind of pointer should `Self` be wrapped in. type Ptr: ForeignOwnable + Send + Sync; /// 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) { @@ -182,24 +182,38 @@ impl VtableHelper { /// The file must be associated with a `MiscDeviceRegistration`. unsafe extern "C" fn fops_open( inode: *mut bindings::inode, - file: *mut bindings::file, + raw_file: *mut bindings::file, ) -> c_int { // SAFETY: The pointers are valid and for a file being opened. - let ret = unsafe { bindings::generic_file_open(inode, file) }; + let ret = unsafe { bindings::generic_file_open(inode, raw_file) }; if ret != 0 { return ret; } + // SAFETY: The open call of a file can access the private data. + let misc_ptr = unsafe { (*raw_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. + // * This underlying file is valid for (much longer than) the duration of `T::open`. // * 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(raw_file) }; + + let ptr = match T::open(file, misc) { Ok(ptr) => ptr, Err(err) => return err.to_errno(), }; - // SAFETY: The open call of a file owns the private data. - unsafe { (*file).private_data = ptr.into_foreign().cast_mut() }; + // This overwrites the private data with the value specified by the user, changing the type of + // this file's private data. All future accesses to the private data is performed by other + // fops_* methods in this file, which all correctly cast the private data to the new type. + // + // SAFETY: The open call of a file can access the private data. + unsafe { (*raw_file).private_data = ptr.into_foreign().cast_mut() }; 0 } From patchwork Tue Dec 10 09:39:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13901131 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 4B9031991B6 for ; Tue, 10 Dec 2024 09:39:25 +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=1733823566; cv=none; b=g4Mm7PKo/0NGWmhde7UMaLtN4GV4lKtobX4oWg3JErgC3gzMNFUMpuP+4SohuIJw5nMl1CG+AMOeZ77xdooiIex2NCn2lq4kzqG3ZJ/LsvlBEE6I91zvjJqG8Bm6gmXXfSTZ+fcq/L1EbcgPz1xBhImsqqarDbDj6SnLQWVzqzg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733823566; c=relaxed/simple; bh=lnFOkiQFTcM8+coi/kR1zS+3K3Kh9pdLRZYdj3FBLXU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hltWettDsV6e0wtKZKSW9Uylfs+dPrtLfYqbCLb5p09j3d1HuiRggMNPqf7CI6JxzQP09J2pBCQT/2nvH385W3+Zbh8xTw2T3SvLgXt95g9sTJtxSBGFkvAW+r+905Zdb4ph9xg2lC+Uxf4NMC+h7xHBo9o2gydmJZAk7ddW0xs= 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=ehDkAlUV; 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="ehDkAlUV" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4359206e1e4so3716915e9.2 for ; Tue, 10 Dec 2024 01:39:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733823564; x=1734428364; 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=5tOk4o16vH2wNnu69cAh5eCtJYr7jGfSK8UQRzizxqM=; b=ehDkAlUVBBEmIxy5Hy9P/t41SAa+BGGRlAtAUw2E/1y6UGdTdzFeqRZD4l7fAp/heO 6yfOe41hSbUQgioI9rkq9Kucr8zqUpCepMuwGTecgaN6jMso3Gn0ozlbUUIxdKBQgBrp i2udg8MrrfWa36ulG4Y+OP2g7NQv07GzEy6jGjPdGz3zpq1RjzHnRgI8EfQgGe+eZDuf 3Rw12XeIWQ7aMkHBSA7xOOuW3JaKRXSUynz2dBoVhDHMj9+l0nOhOzZhnINz5Ira3lno jQpopv1qoRagfJQfVmxzlstI7I+exM9+S5rk0IjL9AXk2ihjsDIP9LdG2wSP4d23nksJ +8UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733823564; x=1734428364; 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=5tOk4o16vH2wNnu69cAh5eCtJYr7jGfSK8UQRzizxqM=; b=Hl3sWgDAxLzY8PudxwNdZdG3N+Mq7HmjF7IVUoFx4NHJS9zfk/AS6y/vWIAdAlX8fN AO/9HZjj+SXflIbggjAyZjKL23fH3tJRMvkIdCAy2XnuTa6lJk0GAeSIzsP08P8Fy2l7 JDEpOCcnNkuK8H29S4NdCsStLyMZ4Ia/kLNHIsS9lvoMEqI/ulFy+5uNHuHFSQzcBsLD 2xiS3EzVBJdAich7DIF1dbFB+RvbOt1aFO7D8Nw0xTNrbNeG1ZOlNByNwkkaXjCJGRta fzpSyBJWMod7VrXmEclr/1UtcqsbRn9N2PmvO8Bttq2Dvx17d52fHv4soccRgHwi+tnx iFCw== X-Forwarded-Encrypted: i=1; AJvYcCXA54p534J/5BJHhl6vGSCMGujkPt7ZiyFMKyph3SB9ANtXH6+LzIrNiJMcFyOTb7UIl8oBbY9ZPTYA6GFb@vger.kernel.org X-Gm-Message-State: AOJu0Yw/BZnwzd5hJiFnaOpMFpxZgKgw0XjUV4sXCJ63Kkwsctb0cbVo V08VTNxcwjb3toqJtk8bAVqdo0Z5Z+1I6ibejGeIwH6VLYpGGp/NDbQwYVjOpq2eLxf7HiHF+2y w/yzAPbtAzi3N+A== X-Google-Smtp-Source: AGHT+IEQH+bBoeURPhgYuV58AQQWr0dKwAWYHgfBI0gpjuFffFaCPPUpJAjfEuq0+lXKGerWydf7j2auMMI0ZXc= X-Received: from wmhf25.prod.google.com ([2002:a7b:cc19:0:b0:431:1903:8a3e]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:1868:b0:385:fd24:3317 with SMTP id ffacd0b85a97d-386453cf868mr3887389f8f.1.1733823563891; Tue, 10 Dec 2024 01:39:23 -0800 (PST) Date: Tue, 10 Dec 2024 09:39:02 +0000 In-Reply-To: <20241210-miscdevice-file-param-v3-0-b2a79b666dc5@google.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241210-miscdevice-file-param-v3-0-b2a79b666dc5@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=1588; i=aliceryhl@google.com; h=from:subject:message-id; bh=+0BkFqw5a7yRTc6C9JLr0Z5W0YYOHlF+girdQwg1HnE=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnWAxC4+A+/FbfX/LfWzds/ch9RDXzjjGfkFplH pR0KBZZCV2JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ1gMQgAKCRAEWL7uWMY5 RroJEACwpp4Qy0ibi5m1SfviuuuUdhfO1BazAv2k4RZvJi6FynNhfs7ZPZrMkEvZjKpsq5hxi6X O5WBedQbA+zEQ6SgR/OEkX74rnw4QupKbC1FwHUVVZtDEt4leqtz81IlAKpm8tQzD0W7yNzmMK0 qJQS60VAwDFOmfI1FruqPwtNnWFuWAYGs1KlADKlpQtJtdEC3IN5Btis+v8iSKgdxtVJdWBylM4 /4kP8/QzJNMfOPFaQUcNIb1aHLG/3JCCLBKRLb9p82uEloBKapX6/3K/JWyXzZYq5yPUcJCfen2 ictX8XnnmJJaDbCm+CPzp7Wz0SCioFrnXpOVu9D02mIQBBKEXV7IaCe4Ccqxzqzt+HqnlGoe4wB +ZitNdCoWQwOPwlut29o7K7eGnp7uDXRjOF6SN/qgxMndAMb1OtADXsIUvqrKo75p+hP3um3prg 30gd6nuBwGY/SazLJaAU0aUSTMpXt6AfYwaLdkw0GkE/yugdc0HsPo2u+Y/zaxMuYO1hbHOSYpw JP7+7GxjCIePBaYN2MOccaZFhmFOndv2CXZvii83vbhEasCbfsz/6fxbp1FnUC0FzIoxM5IPKOR pyNzNkq1poiOBO0kNIqsc2Vf9f3xqmtt36fVbfkg+VJ0mxZlJq/EhTnsqN4tjgYk/Wp7umSlsX1 VotSHlh+iAxfs6w== X-Mailer: b4 0.13.0 Message-ID: <20241210-miscdevice-file-param-v3-3-b2a79b666dc5@google.com> Subject: [PATCH v3 3/3] rust: miscdevice: Provide accessor to pull out miscdevice::this_device 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 , Danilo Krummrich , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl From: Lee Jones There are situations where a pointer to a `struct device` will become necessary (e.g. for calling into dev_*() functions). This accessor allows callers to pull this out from the `struct miscdevice`. Signed-off-by: Lee Jones Signed-off-by: Alice Ryhl Tested-by: Lee Jones --- rust/kernel/miscdevice.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index 75a9d26c8001..20895e809607 100644 --- a/rust/kernel/miscdevice.rs +++ b/rust/kernel/miscdevice.rs @@ -10,6 +10,7 @@ use crate::{ bindings, + device::Device, error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR}, fs::File, prelude::*, @@ -85,6 +86,16 @@ pub fn register(opts: MiscDeviceOptions) -> impl PinInit { pub fn as_raw(&self) -> *mut bindings::miscdevice { self.inner.get() } + + /// Access the `this_device` field. + pub fn device(&self) -> &Device { + // SAFETY: This can only be called after a successful register(), which always + // initialises `this_device` with a valid device. Furthermore, the signature of this + // function tells the borrow-checker that the `&Device` reference must not outlive the + // `&MiscDeviceRegistration` used to obtain it, so the last use of the reference must be + // before the underlying `struct miscdevice` is destroyed. + unsafe { Device::as_ref((*self.as_raw()).this_device) } + } } #[pinned_drop]