From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D09226F2BE; Mon, 27 Apr 2026 22:12:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777327934; cv=none; b=c3eckqKjr15VOdAf+CsfstMljBZdQBWpENWpGfqrAxSZVFa3g9o96fHkbSIfEHqEvU0/eSj0K1QKMZVmxQR1Ae7M6YIMW+MmyrLtQ47XAw+nY69bbA872jBXlmxEi8AgJfXQRaz/kJwwKQQh6cHHInYdhXHjcFSJCVMlYOe+T7g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777327934; c=relaxed/simple; bh=ju+F7o8Yl0nyozmN8SThTNujs1B0GOs/rWyZh4FMitg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=K7jRC9E0QPx4ICGqiA3JeTPrkoLcMYfmwj74Aq0vp/hDTJZhwABw7V/zpOrVtmYU6OgJ32sSE3p54HAushcwMnTEXfmCdA21eZ/zA40DDQTKJ6+AMXWBJlGfu8b2HSOSTtuyS2K8+hqSegopyk5coW4X2Vsdrj2koa01TD/+TeM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=B0DI94d2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="B0DI94d2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F904C19425; Mon, 27 Apr 2026 22:12:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777327934; bh=ju+F7o8Yl0nyozmN8SThTNujs1B0GOs/rWyZh4FMitg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B0DI94d2eRFKDr3/VwZbkkFsvz36IGsGDJhc8SmdCK5Z5cARqCTNNFlEfE9EwcpKg IqSQS7X0la8Qy4Uu9wC5Z47YmGqMDrIdAB6CIl7Kk2CYVGhaghWLzWUiChXNp6wGK3 sGHygogvD8WI7CopuAM5NyRz9NXzAEZ2ipxldU+Ssx0W/HgaH3OWWeAc2NDKawW9FY NY9+I9h8GlXTG1y/UVjx2uovH8iyQ0xKNgmXJQmPg844taSQlRztSkbNfwkrxs9aY1 IyaoJNYxpsYna35Tk2JTSso2ezu9t73QM1O73Zhb6F2DLvze3BSEujdZ+SjyNc+Zjd 7nyxZcE1D64lQ== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, acourbot@nvidia.com, aliceryhl@google.com, david.m.ertman@intel.com, ira.weiny@intel.com, leon@kernel.org, viresh.kumar@linaro.org, m.wilczynski@samsung.com, ukleinek@kernel.org, bhelgaas@google.com, kwilczynski@kernel.org, abdiel.janulgue@gmail.com, robin.murphy@arm.com, markus.probst@posteo.de, ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, tmgross@umich.edu Cc: driver-core@lists.linux.dev, linux-kernel@vger.kernel.org, nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org, linux-pm@vger.kernel.org, linux-pwm@vger.kernel.org, linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, Danilo Krummrich Subject: [PATCH 01/24] rust: driver core: drop drvdata before devres release Date: Tue, 28 Apr 2026 00:10:59 +0200 Message-ID: <20260427221155.2144848-2-dakr@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260427221155.2144848-1-dakr@kernel.org> References: <20260427221155.2144848-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Move the post_unbind_rust callback before devres_release_all() in device_unbind_cleanup(). With drvdata() removed, the driver's bus device private data is only accessible by the owning driver itself. It is hence safe to drop the driver's bus device private data before devres actions are released. This reordering is the key enabler for Higher-Ranked Lifetime Types (HRT) in Rust device drivers -- it allows driver structs to hold direct references to devres-managed resources, because the bus device private data (and with it all such references) is guaranteed to be dropped while the underlying devres resources are still alive. Without this change, devres resources would be freed first, leaving the driver's bus device private data with dangling references during its destructor. Signed-off-by: Danilo Krummrich --- drivers/base/dd.c | 2 +- include/linux/device/driver.h | 4 ++-- rust/kernel/driver.rs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/base/dd.c b/drivers/base/dd.c index 5799a60fd058..be59d2e13a15 100644 --- a/drivers/base/dd.c +++ b/drivers/base/dd.c @@ -593,9 +593,9 @@ static DEVICE_ATTR_RW(state_synced); static void device_unbind_cleanup(struct device *dev) { - devres_release_all(dev); if (dev->driver->p_cb.post_unbind_rust) dev->driver->p_cb.post_unbind_rust(dev); + devres_release_all(dev); arch_teardown_dma_ops(dev); kfree(dev->dma_range_map); dev->dma_range_map = NULL; diff --git a/include/linux/device/driver.h b/include/linux/device/driver.h index bbc67ec513ed..38e9a4679447 100644 --- a/include/linux/device/driver.h +++ b/include/linux/device/driver.h @@ -123,8 +123,8 @@ struct device_driver { struct driver_private *p; struct { /* - * Called after remove() and after all devres entries have been - * processed. This is a Rust only callback. + * Called after remove() but before devres entries are released. + * This is a Rust only callback. */ void (*post_unbind_rust)(struct device *dev); } p_cb; diff --git a/rust/kernel/driver.rs b/rust/kernel/driver.rs index 36de8098754d..8f0e50729215 100644 --- a/rust/kernel/driver.rs +++ b/rust/kernel/driver.rs @@ -189,8 +189,8 @@ extern "C" fn post_unbind_callback(dev: *mut bindings::device) { // INVARIANT: `dev` is valid for the duration of the `post_unbind_callback()`. let dev = unsafe { &*dev.cast::>() }; - // `remove()` and all devres callbacks have been completed at this point, hence drop the - // driver's device private data. + // `remove()` has been completed at this point; devres resources are still valid and will + // be released after the driver's bus device private data is dropped. // // SAFETY: By the safety requirements of the `Driver` trait, `T::DriverData` is the // driver's device private data type. -- 2.54.0