{"id":2229205,"url":"http://patchwork.ozlabs.org/api/1.1/covers/2229205/?format=json","web_url":"http://patchwork.ozlabs.org/project/linux-pwm/cover/20260427221155.2144848-1-dakr@kernel.org/","project":{"id":38,"url":"http://patchwork.ozlabs.org/api/1.1/projects/38/?format=json","name":"Linux PWM development","link_name":"linux-pwm","list_id":"linux-pwm.vger.kernel.org","list_email":"linux-pwm@vger.kernel.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20260427221155.2144848-1-dakr@kernel.org>","date":"2026-04-27T22:10:58","name":"[00/24] rust: device: Higher-Ranked Lifetime Types for device drivers","submitter":{"id":89037,"url":"http://patchwork.ozlabs.org/api/1.1/people/89037/?format=json","name":"Danilo Krummrich","email":"dakr@kernel.org"},"mbox":"http://patchwork.ozlabs.org/project/linux-pwm/cover/20260427221155.2144848-1-dakr@kernel.org/mbox/","series":[{"id":501734,"url":"http://patchwork.ozlabs.org/api/1.1/series/501734/?format=json","web_url":"http://patchwork.ozlabs.org/project/linux-pwm/list/?series=501734","date":"2026-04-27T22:10:58","name":"rust: device: Higher-Ranked Lifetime Types for device drivers","version":1,"mbox":"http://patchwork.ozlabs.org/series/501734/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/covers/2229205/comments/","headers":{"Return-Path":"\n <linux-pwm+bounces-8703-incoming=patchwork.ozlabs.org@vger.kernel.org>","X-Original-To":["incoming@patchwork.ozlabs.org","linux-pwm@vger.kernel.org"],"Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256\n header.s=k20201202 header.b=Z7nbSyXy;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=172.232.135.74; helo=sto.lore.kernel.org;\n envelope-from=linux-pwm+bounces-8703-incoming=patchwork.ozlabs.org@vger.kernel.org;\n receiver=patchwork.ozlabs.org)","smtp.subspace.kernel.org;\n\tdkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org\n header.b=\"Z7nbSyXy\"","smtp.subspace.kernel.org;\n arc=none smtp.client-ip=10.30.226.201"],"Received":["from sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g4HqJ0FSZz1xrS\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 28 Apr 2026 08:12:11 +1000 (AEST)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby sto.lore.kernel.org (Postfix) with ESMTP id 5700F3004F12\n\tfor <incoming@patchwork.ozlabs.org>; Mon, 27 Apr 2026 22:12:09 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id 825263A7F45;\n\tMon, 27 Apr 2026 22:12:08 +0000 (UTC)","from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org\n [10.30.226.201])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby smtp.subspace.kernel.org (Postfix) with ESMTPS id 5ADA43A6B81;\n\tMon, 27 Apr 2026 22:12:08 +0000 (UTC)","by smtp.kernel.org (Postfix) with ESMTPSA id 49D48C19425;\n\tMon, 27 Apr 2026 22:12:02 +0000 (UTC)"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1777327928; cv=none;\n b=kRHGdOhG3yLOlFvJ0sStknNUPVzXWtI4CAmoF4p7fPta1qlH8q0nr2aGQViq3UjdGMxZMOe2lNUocT8OU4I7xlB0ifV7IG4Cz9B9PImX78kWguAbVHBjoePbPvJkBvn+Cb2MYvnb4YBNX+3V+9FeUDUbQ0oNgaXAOE14hOVJ1Wg=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1777327928; c=relaxed/simple;\n\tbh=0thXPdShB3t5adcBz1VkWyZ33OoOiYeBGZx1pE9VSCE=;\n\th=From:To:Cc:Subject:Date:Message-ID:MIME-Version;\n b=bcIrznXy4Fbv2wVTXg02DT8cUU+DXiZC0ovZh5hNFr1z4eWUdqG8224FAxlUnorx1Q1lOTLUN+UYCPKupe3aXGbD5vMAkpgrkZrCpHFxrNfkoFSQtAa/f0Y21Igvabg/M4viNv3b2C1oDp6AEC2b22Jfw30w02mq6eSA/XMtKXc=","ARC-Authentication-Results":"i=1; smtp.subspace.kernel.org;\n dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org\n header.b=Z7nbSyXy; arc=none smtp.client-ip=10.30.226.201","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;\n\ts=k20201202; t=1777327928;\n\tbh=0thXPdShB3t5adcBz1VkWyZ33OoOiYeBGZx1pE9VSCE=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=Z7nbSyXy34HFoImwriB49Cku0ZZoA0kXlQ6jBhSYj9BVkWR7Sn4VlBPH3sZXjPE2g\n\t m8dDBnZVc9okYF8//vnRWpYKv+KYnEwt15cL/SiYb52pls2wZlLE6lBEIOl45Mx5Zj\n\t HRBPuaBrspJtiVYckLKyxs3+F3dl5L819mk1eokoINgUZkq37Ca5e6nGwXM1KvaqrB\n\t 7UqWXRUUhhm5V/a0HyxMqqjubV2YBMvL81XPNZ/piO9nWLFwyVjwUYT6PwzsLsNTbz\n\t HUtlrVffhg+N9YbCvVgdnQVM5Tpznoon8co4YJxeDtDTMw9z6O2kfn/XUXwQMiPp00\n\t I2RG39QWsSeoA==","From":"Danilo Krummrich <dakr@kernel.org>","To":"gregkh@linuxfoundation.org,\n\trafael@kernel.org,\n\tacourbot@nvidia.com,\n\taliceryhl@google.com,\n\tdavid.m.ertman@intel.com,\n\tira.weiny@intel.com,\n\tleon@kernel.org,\n\tviresh.kumar@linaro.org,\n\tm.wilczynski@samsung.com,\n\tukleinek@kernel.org,\n\tbhelgaas@google.com,\n\tkwilczynski@kernel.org,\n\tabdiel.janulgue@gmail.com,\n\trobin.murphy@arm.com,\n\tmarkus.probst@posteo.de,\n\tojeda@kernel.org,\n\tboqun@kernel.org,\n\tgary@garyguo.net,\n\tbjorn3_gh@protonmail.com,\n\tlossin@kernel.org,\n\ta.hindborg@kernel.org,\n\ttmgross@umich.edu","Cc":"driver-core@lists.linux.dev,\n\tlinux-kernel@vger.kernel.org,\n\tnova-gpu@lists.linux.dev,\n\tdri-devel@lists.freedesktop.org,\n\tlinux-pm@vger.kernel.org,\n\tlinux-pwm@vger.kernel.org,\n\tlinux-pci@vger.kernel.org,\n\trust-for-linux@vger.kernel.org,\n\tDanilo Krummrich <dakr@kernel.org>","Subject":"[PATCH 00/24] rust: device: Higher-Ranked Lifetime Types for device\n drivers","Date":"Tue, 28 Apr 2026 00:10:58 +0200","Message-ID":"<20260427221155.2144848-1-dakr@kernel.org>","X-Mailer":"git-send-email 2.54.0","Precedence":"bulk","X-Mailing-List":"linux-pwm@vger.kernel.org","List-Id":"<linux-pwm.vger.kernel.org>","List-Subscribe":"<mailto:linux-pwm+subscribe@vger.kernel.org>","List-Unsubscribe":"<mailto:linux-pwm+unsubscribe@vger.kernel.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit"},"content":"Currently, Rust device drivers access device resources such as PCI BAR mappings\nand I/O memory regions through Devres<T>.\n\nDevres::access() provides zero-overhead access by taking a &Device<Bound>\nreference as proof that the device is still bound. Since a &Device<Bound> is\navailable in almost all contexts by design, Devres is mostly a type-system level\nproof that the resource is valid, but it can also be used from scopes without\nthis guarantee through its try_access() accessor.\n\nThis works well in general, but has a few limitations:\n\n  - Every access to a device resource goes through Devres::access(), which\n    despite zero cost, adds boilerplate to every access site.\n\n  - Destructors do not receive a &Device<Bound>, so they must use try_access(),\n    which can fail. In practice the access succeeds if teardown ordering is\n    correct, but the type system can't express this, forcing drivers to handle a\n    failure path that should never be taken.\n\n  - Sharing a resource across components (e.g. passing a BAR to a sub-component)\n    requires Arc<Devres<T>>.\n\n  - Device references must be stored as ARef<Device> rather than plain &Device\n    borrows.\n\nThese limitations stem from the driver's bus device private data being 'static\n-- the driver struct cannot borrow from the device reference it receives in\nprobe(), even though it structurally cannot outlive the device binding.\n\nThis series introduces Higher-Ranked Lifetime Types (HRT) for Rust device\ndrivers. An HRT is a type that is generic over a lifetime -- it does not have a\nfixed lifetime, but can be instantiated with any lifetime chosen by the caller.\n\nRust does not directly support types that are generic over a lifetime as type\nparameters; the ForLt trait (contributed by Gary Guo) encodes this internally.\n\nThe module_*_driver! macros handle the wrapping, so driver authors just write\nstruct MyDriver<'a> and impl Driver<'a>.\n\nWith HRT, driver structs carry a lifetime parameter tied to the device binding\nscope -- the interval of a bus device being bound to a driver. Device resources\nlike pci::Bar<'a> and IoMem<'a> are handed out with this lifetime, so the\ncompiler enforces at build time that they do not escape the binding scope.\n\nBefore:\n\n\tstruct MyDriver {\n\t    pdev: ARef<pci::Device>,\n\t    bar: Devres<pci::Bar<BAR_SIZE>>,\n\t}\n\n\tlet io = self.bar.access(dev)?;\n\tio.read32(OFFSET);\n\nAfter:\n\n\tstruct MyDriver<'a> {\n\t    pdev: &'a pci::Device,\n\t    bar: pci::Bar<'a, BAR_SIZE>,\n\t}\n\n\tself.bar.read32(OFFSET);\n\nLifetime-parameterized device resources can be put into a Devres at any point\nvia Bar::into_devres() / IoMem::into_devres(), providing the exact same\nsemantics as before. This is useful for resources shared across subsystem\nboundaries where revocation is needed.\n\nThis also synergizes with the upcoming self-referential initialization support\nin pin-init, which allows one field of the driver struct to borrow another\nduring initialization without unsafe code.\n\nThe same pattern is applied to auxiliary device registration data as a first\nexample beyond bus device private data. Registration<F: ForLt> can hold\nlifetime-parameterized data tied to the parent driver's binding scope. Since the\nauxiliary bus guarantees that the parent remains bound while the auxiliary\ndevice is registered, the registration data can safely borrow the parent's\ndevice resources.\n\nMore generally, binding resource lifetimes to a registration scope applies to\nevery registration that is scoped to a driver binding -- auxiliary devices,\nclass devices, IRQ handlers, workqueues.\n\nA follow-up series extends this to class device registrations, starting with\nDRM, so that class device callbacks (IOCTLs, etc.) can safely access device\nresources through the separate registration data bound to the registration's\nlifetime without Devres indirection.\n\nThe series contains a few driver patches for reference, indicated by the REF\nsuffix.\n\nThanks to Gary for coming up with the ForLt implementation; thanks to Alice for\nthe early discussions around lifetime-parameterized private data that helped\nshape the direction of this work.\n\nThis series depends on [1].\n\n[1] https://lore.kernel.org/driver-core/20260427221002.2143861-1-dakr@kernel.org/\n\nDanilo Krummrich (23):\n  rust: driver core: drop drvdata before devres release\n  rust: devres: add ForLt support to Devres\n  rust: device: generalize drvdata methods over ForLt\n  rust: driver: make Adapter trait lifetime-parameterized\n  rust: pci: implement Sync for Device<Bound>\n  rust: platform: implement Sync for Device<Bound>\n  rust: auxiliary: implement Sync for Device<Bound>\n  rust: usb: implement Sync for Device<Bound>\n  rust: device: implement Sync for Device<Bound>\n  rust: pci: make Driver trait lifetime-parameterized\n  rust: platform: make Driver trait lifetime-parameterized\n  rust: auxiliary: make Driver trait lifetime-parameterized\n  rust: auxiliary: generalize Registration over ForLt\n  samples: rust: rust_driver_auxiliary: showcase lifetime-bound\n    registration data\n  rust: usb: make Driver trait lifetime-parameterized\n  rust: i2c: make Driver trait lifetime-parameterized\n  rust: pci: make Bar lifetime-parameterized\n  rust: io: make IoMem and ExclusiveIoMem lifetime-parameterized\n  samples: rust: rust_driver_pci: use HRT lifetime for Bar\n  gpu: nova-core: use HRT lifetime for Bar\n  gpu: nova-core: unregister sysmem flush page from Drop\n  gpu: nova-core: replace ARef<Device> with &'a Device in SysmemFlush\n  gpu: drm: tyr: use HRT lifetime for IoMem\n\nGary Guo (1):\n  rust: types: add `ForLt` trait for higher-ranked lifetime support\n\n drivers/base/dd.c                     |   2 +-\n drivers/cpufreq/rcpufreq_dt.rs        |  10 +-\n drivers/gpu/drm/nova/driver.rs        |   9 +-\n drivers/gpu/drm/tyr/driver.rs         |  24 ++-\n drivers/gpu/drm/tyr/gpu.rs            |  62 ++++---\n drivers/gpu/drm/tyr/regs.rs           |  21 +--\n drivers/gpu/nova-core/driver.rs       |  48 ++---\n drivers/gpu/nova-core/fb.rs           |  31 ++--\n drivers/gpu/nova-core/gpu.rs          |  32 +---\n drivers/gpu/nova-core/nova_core.rs    |   4 +-\n drivers/pwm/pwm_th1520.rs             |  14 +-\n include/linux/device/driver.h         |   4 +-\n rust/Makefile                         |   1 +\n rust/kernel/auxiliary.rs              | 144 ++++++++++-----\n rust/kernel/cpufreq.rs                |   8 +-\n rust/kernel/device.rs                 |  84 ++++++---\n rust/kernel/devres.rs                 |  31 +++-\n rust/kernel/driver.rs                 |  44 +++--\n rust/kernel/i2c.rs                    | 121 ++++++++-----\n rust/kernel/io/mem.rs                 | 118 ++++++-------\n rust/kernel/pci.rs                    |  88 +++++++---\n rust/kernel/pci/io.rs                 |  50 +++---\n rust/kernel/platform.rs               | 101 +++++++----\n rust/kernel/types.rs                  |   4 +\n rust/kernel/types/for_lt.rs           | 117 +++++++++++++\n rust/kernel/usb.rs                    |  93 ++++++----\n rust/macros/for_lt.rs                 | 242 ++++++++++++++++++++++++++\n rust/macros/lib.rs                    |  12 ++\n samples/rust/rust_debugfs.rs          |  10 +-\n samples/rust/rust_dma.rs              |   9 +-\n samples/rust/rust_driver_auxiliary.rs |  53 ++++--\n samples/rust/rust_driver_i2c.rs       |  18 +-\n samples/rust/rust_driver_pci.rs       |  93 +++++-----\n samples/rust/rust_driver_platform.rs  |  12 +-\n samples/rust/rust_driver_usb.rs       |  14 +-\n samples/rust/rust_i2c_client.rs       |  12 +-\n samples/rust/rust_soc.rs              |  12 +-\n 37 files changed, 1182 insertions(+), 570 deletions(-)\n create mode 100644 rust/kernel/types/for_lt.rs\n create mode 100644 rust/macros/for_lt.rs"}