* [PATCH] drm/i915/hwmon: Get rid of devm
@ 2024-04-13 0:10 Ashutosh Dixit
2024-04-13 14:43 ` Armin Wolf
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Ashutosh Dixit @ 2024-04-13 0:10 UTC (permalink / raw)
To: intel-gfx; +Cc: Badal Nilawar, Andi Shyti, Ville Syrjälä, linux-hwmon
When both hwmon and hwmon drvdata (on which hwmon depends) are device
managed resources, the expectation, on device unbind, is that hwmon will be
released before the drvdata. However, it appears devres does not do this
consistently, so that we occasionally see drvdata being released before
hwmon itself. This results in a uaf if hwmon sysfs is accessed during
device unbind.
The only way out of this seems to be do get rid of devm_ and release/free
everything explicitly during device unbind.
Closes: https://gitlab.freedesktop.org/drm/intel/-/issues/10366
Signed-off-by: Ashutosh Dixit <ashutosh.dixit@intel.com>
---
drivers/gpu/drm/i915/i915_hwmon.c | 46 ++++++++++++++++++++++++-------
1 file changed, 36 insertions(+), 10 deletions(-)
diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c
index 8c3f443c8347..5f6022b148d7 100644
--- a/drivers/gpu/drm/i915/i915_hwmon.c
+++ b/drivers/gpu/drm/i915/i915_hwmon.c
@@ -792,7 +792,7 @@ void i915_hwmon_register(struct drm_i915_private *i915)
if (!IS_DGFX(i915))
return;
- hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
+ hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
if (!hwmon)
return;
@@ -818,10 +818,10 @@ void i915_hwmon_register(struct drm_i915_private *i915)
hwm_get_preregistration_info(i915);
/* hwmon_dev points to device hwmon<i> */
- hwmon_dev = devm_hwmon_device_register_with_info(dev, ddat->name,
- ddat,
- &hwm_chip_info,
- hwm_groups);
+ hwmon_dev = hwmon_device_register_with_info(dev, ddat->name,
+ ddat,
+ &hwm_chip_info,
+ hwm_groups);
if (IS_ERR(hwmon_dev)) {
i915->hwmon = NULL;
return;
@@ -838,10 +838,10 @@ void i915_hwmon_register(struct drm_i915_private *i915)
if (!hwm_gt_is_visible(ddat_gt, hwmon_energy, hwmon_energy_input, 0))
continue;
- hwmon_dev = devm_hwmon_device_register_with_info(dev, ddat_gt->name,
- ddat_gt,
- &hwm_gt_chip_info,
- NULL);
+ hwmon_dev = hwmon_device_register_with_info(dev, ddat_gt->name,
+ ddat_gt,
+ &hwm_gt_chip_info,
+ NULL);
if (!IS_ERR(hwmon_dev))
ddat_gt->hwmon_dev = hwmon_dev;
}
@@ -849,5 +849,31 @@ void i915_hwmon_register(struct drm_i915_private *i915)
void i915_hwmon_unregister(struct drm_i915_private *i915)
{
- fetch_and_zero(&i915->hwmon);
+ struct i915_hwmon *hwmon;
+ struct hwm_drvdata *ddat;
+ struct intel_gt *gt;
+ int i;
+
+ hwmon = fetch_and_zero(&i915->hwmon);
+ if (!hwmon)
+ return;
+
+ ddat = &hwmon->ddat;
+
+ for_each_gt(gt, i915, i) {
+ struct hwm_drvdata *ddat_gt;
+
+ ddat_gt = hwmon->ddat_gt + i;
+
+ if (ddat_gt->hwmon_dev) {
+ hwmon_device_unregister(ddat_gt->hwmon_dev);
+ ddat_gt->hwmon_dev = NULL;
+ }
+ }
+
+ if (ddat->hwmon_dev)
+ hwmon_device_unregister(ddat->hwmon_dev);
+
+ mutex_destroy(&hwmon->hwmon_lock);
+ kfree(hwmon);
}
--
2.41.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH] drm/i915/hwmon: Get rid of devm
2024-04-13 0:10 [PATCH] drm/i915/hwmon: Get rid of devm Ashutosh Dixit
@ 2024-04-13 14:43 ` Armin Wolf
2024-04-15 23:21 ` Dixit, Ashutosh
2024-04-14 23:23 ` Dixit, Ashutosh
2024-04-15 20:34 ` ✓ Fi.CI.BAT: success for " Patchwork
2 siblings, 1 reply; 9+ messages in thread
From: Armin Wolf @ 2024-04-13 14:43 UTC (permalink / raw)
To: Ashutosh Dixit, intel-gfx
Cc: Badal Nilawar, Andi Shyti, Ville Syrjälä, linux-hwmon
Am 13.04.24 um 02:10 schrieb Ashutosh Dixit:
> When both hwmon and hwmon drvdata (on which hwmon depends) are device
> managed resources, the expectation, on device unbind, is that hwmon will be
> released before the drvdata. However, it appears devres does not do this
> consistently, so that we occasionally see drvdata being released before
> hwmon itself. This results in a uaf if hwmon sysfs is accessed during
> device unbind.
>
> The only way out of this seems to be do get rid of devm_ and release/free
> everything explicitly during device unbind.
Hi,
could it be that the underlying cause for this is the fact that you are using
devres on a DRM device?
The documentation states that:
devres managed resources like devm_kmalloc() can only be used for resources
directly related to the underlying hardware device, and only used in code
paths fully protected by drm_dev_enter() and drm_dev_exit().
That said, since the i915 driver is already removing the hwmon device manually
with i915_hwmon_unregister(), i agree that not using devres in this case seems
to be the solution.
Thanks,
Armin Wolf
> Closes: https://gitlab.freedesktop.org/drm/intel/-/issues/10366
> Signed-off-by: Ashutosh Dixit <ashutosh.dixit@intel.com>
> ---
> drivers/gpu/drm/i915/i915_hwmon.c | 46 ++++++++++++++++++++++++-------
> 1 file changed, 36 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c
> index 8c3f443c8347..5f6022b148d7 100644
> --- a/drivers/gpu/drm/i915/i915_hwmon.c
> +++ b/drivers/gpu/drm/i915/i915_hwmon.c
> @@ -792,7 +792,7 @@ void i915_hwmon_register(struct drm_i915_private *i915)
> if (!IS_DGFX(i915))
> return;
>
> - hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
> + hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
> if (!hwmon)
> return;
>
> @@ -818,10 +818,10 @@ void i915_hwmon_register(struct drm_i915_private *i915)
> hwm_get_preregistration_info(i915);
>
> /* hwmon_dev points to device hwmon<i> */
> - hwmon_dev = devm_hwmon_device_register_with_info(dev, ddat->name,
> - ddat,
> - &hwm_chip_info,
> - hwm_groups);
> + hwmon_dev = hwmon_device_register_with_info(dev, ddat->name,
> + ddat,
> + &hwm_chip_info,
> + hwm_groups);
> if (IS_ERR(hwmon_dev)) {
> i915->hwmon = NULL;
> return;
> @@ -838,10 +838,10 @@ void i915_hwmon_register(struct drm_i915_private *i915)
> if (!hwm_gt_is_visible(ddat_gt, hwmon_energy, hwmon_energy_input, 0))
> continue;
>
> - hwmon_dev = devm_hwmon_device_register_with_info(dev, ddat_gt->name,
> - ddat_gt,
> - &hwm_gt_chip_info,
> - NULL);
> + hwmon_dev = hwmon_device_register_with_info(dev, ddat_gt->name,
> + ddat_gt,
> + &hwm_gt_chip_info,
> + NULL);
> if (!IS_ERR(hwmon_dev))
> ddat_gt->hwmon_dev = hwmon_dev;
> }
> @@ -849,5 +849,31 @@ void i915_hwmon_register(struct drm_i915_private *i915)
>
> void i915_hwmon_unregister(struct drm_i915_private *i915)
> {
> - fetch_and_zero(&i915->hwmon);
> + struct i915_hwmon *hwmon;
> + struct hwm_drvdata *ddat;
> + struct intel_gt *gt;
> + int i;
> +
> + hwmon = fetch_and_zero(&i915->hwmon);
> + if (!hwmon)
> + return;
> +
> + ddat = &hwmon->ddat;
> +
> + for_each_gt(gt, i915, i) {
> + struct hwm_drvdata *ddat_gt;
> +
> + ddat_gt = hwmon->ddat_gt + i;
> +
> + if (ddat_gt->hwmon_dev) {
> + hwmon_device_unregister(ddat_gt->hwmon_dev);
> + ddat_gt->hwmon_dev = NULL;
> + }
> + }
> +
> + if (ddat->hwmon_dev)
> + hwmon_device_unregister(ddat->hwmon_dev);
> +
> + mutex_destroy(&hwmon->hwmon_lock);
> + kfree(hwmon);
> }
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH] drm/i915/hwmon: Get rid of devm
2024-04-13 14:43 ` Armin Wolf
@ 2024-04-15 23:21 ` Dixit, Ashutosh
0 siblings, 0 replies; 9+ messages in thread
From: Dixit, Ashutosh @ 2024-04-15 23:21 UTC (permalink / raw)
To: Armin Wolf
Cc: intel-gfx, Badal Nilawar, Andi Shyti, Ville Syrjälä,
linux-hwmon
On Sat, 13 Apr 2024 07:43:50 -0700, Armin Wolf wrote:
>
Hi Armin,
> Am 13.04.24 um 02:10 schrieb Ashutosh Dixit:
>
> > When both hwmon and hwmon drvdata (on which hwmon depends) are device
> > managed resources, the expectation, on device unbind, is that hwmon will be
> > released before the drvdata. However, it appears devres does not do this
> > consistently, so that we occasionally see drvdata being released before
> > hwmon itself. This results in a uaf if hwmon sysfs is accessed during
> > device unbind.
> >
> > The only way out of this seems to be do get rid of devm_ and release/free
> > everything explicitly during device unbind.
>
> could it be that the underlying cause for this is the fact that you are using
> devres on a DRM device?
>
> The documentation states that:
>
> devres managed resources like devm_kmalloc() can only be used for resources
> directly related to the underlying hardware device, and only used in code
> paths fully protected by drm_dev_enter() and drm_dev_exit().
I just posted v2 of the patch and updated
https://gitlab.freedesktop.org/drm/intel/-/issues/10366. The updates do
include stack traces for two separate code paths in i915 which release
devres.
Actually I am not sure if this is due to using devres on a DRM device. I
was thinking the PCI device would be more appropriate, but looks like DRM
drivers don't have the parent PCI device available in their data structs.
> That said, since the i915 driver is already removing the hwmon device manually
> with i915_hwmon_unregister(),
Well previously i915_hwmon_unregister() was almost empty (and could
actually be eliminated).
> i agree that not using devres in this case seems to be the solution.
Yeah that seems to me too to be the easiest way out of this situation.
Thanks.
--
Ashutosh
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] drm/i915/hwmon: Get rid of devm
2024-04-13 0:10 [PATCH] drm/i915/hwmon: Get rid of devm Ashutosh Dixit
2024-04-13 14:43 ` Armin Wolf
@ 2024-04-14 23:23 ` Dixit, Ashutosh
2024-04-15 20:34 ` ✓ Fi.CI.BAT: success for " Patchwork
2 siblings, 0 replies; 9+ messages in thread
From: Dixit, Ashutosh @ 2024-04-14 23:23 UTC (permalink / raw)
To: intel-gfx; +Cc: Badal Nilawar, Andi Shyti, Ville Syrjälä, linux-hwmon
On Fri, 12 Apr 2024 17:10:31 -0700, Ashutosh Dixit wrote:
>
> When both hwmon and hwmon drvdata (on which hwmon depends) are device
> managed resources, the expectation, on device unbind, is that hwmon will be
> released before the drvdata. However, it appears devres does not do this
> consistently, so that we occasionally see drvdata being released before
> hwmon itself. This results in a uaf if hwmon sysfs is accessed during
> device unbind.
>
> The only way out of this seems to be do get rid of devm_ and release/free
> everything explicitly during device unbind.
>
> Closes: https://gitlab.freedesktop.org/drm/intel/-/issues/10366
> Signed-off-by: Ashutosh Dixit <ashutosh.dixit@intel.com>
Please don't review this patch yet, I will send a v2 tomorrow. Please
review the v2. Thanks.
^ permalink raw reply [flat|nested] 9+ messages in thread
* ✓ Fi.CI.BAT: success for drm/i915/hwmon: Get rid of devm
2024-04-13 0:10 [PATCH] drm/i915/hwmon: Get rid of devm Ashutosh Dixit
2024-04-13 14:43 ` Armin Wolf
2024-04-14 23:23 ` Dixit, Ashutosh
@ 2024-04-15 20:34 ` Patchwork
2 siblings, 0 replies; 9+ messages in thread
From: Patchwork @ 2024-04-15 20:34 UTC (permalink / raw)
To: Dixit, Ashutosh; +Cc: intel-gfx
[-- Attachment #1: Type: text/plain, Size: 7714 bytes --]
== Series Details ==
Series: drm/i915/hwmon: Get rid of devm
URL : https://patchwork.freedesktop.org/series/132400/
State : success
== Summary ==
CI Bug Log - changes from CI_DRM_14581 -> Patchwork_132400v1
====================================================
Summary
-------
**SUCCESS**
No regressions found.
External URL: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/index.html
Participating hosts (40 -> 33)
------------------------------
Additional (1): bat-arls-3
Missing (8): bat-dg1-7 bat-kbl-2 fi-bsw-n3050 fi-apl-guc fi-snb-2520m fi-elk-e7500 bat-dg2-11 bat-jsl-1
Known issues
------------
Here are the changes found in Patchwork_132400v1 that come from known issues:
### IGT changes ###
#### Issues hit ####
* igt@debugfs_test@basic-hwmon:
- bat-arls-3: NOTRUN -> [SKIP][1] ([i915#9318])
[1]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@debugfs_test@basic-hwmon.html
* igt@gem_lmem_swapping@parallel-random-engines:
- bat-arls-3: NOTRUN -> [SKIP][2] ([i915#10213]) +3 other tests skip
[2]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@gem_lmem_swapping@parallel-random-engines.html
* igt@gem_mmap@basic:
- bat-arls-3: NOTRUN -> [SKIP][3] ([i915#4083])
[3]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@gem_mmap@basic.html
* igt@gem_render_tiled_blits@basic:
- bat-arls-3: NOTRUN -> [SKIP][4] ([i915#10197] / [i915#10211] / [i915#4079])
[4]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@gem_render_tiled_blits@basic.html
* igt@gem_tiled_blits@basic:
- bat-arls-3: NOTRUN -> [SKIP][5] ([i915#10196] / [i915#4077]) +2 other tests skip
[5]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@gem_tiled_blits@basic.html
* igt@gem_tiled_pread_basic:
- bat-arls-3: NOTRUN -> [SKIP][6] ([i915#10206] / [i915#4079])
[6]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@gem_tiled_pread_basic.html
* igt@i915_pm_rps@basic-api:
- bat-arls-3: NOTRUN -> [SKIP][7] ([i915#10209])
[7]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@i915_pm_rps@basic-api.html
* igt@i915_selftest@live@gt_engines:
- bat-adls-6: [PASS][8] -> [TIMEOUT][9] ([i915#10026])
[8]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_14581/bat-adls-6/igt@i915_selftest@live@gt_engines.html
[9]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-adls-6/igt@i915_selftest@live@gt_engines.html
* igt@kms_addfb_basic@addfb25-x-tiled-legacy:
- bat-arls-3: NOTRUN -> [SKIP][10] ([i915#10200]) +9 other tests skip
[10]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@kms_addfb_basic@addfb25-x-tiled-legacy.html
* igt@kms_cursor_legacy@basic-busy-flip-before-cursor-atomic:
- bat-arls-3: NOTRUN -> [SKIP][11] ([i915#10202]) +1 other test skip
[11]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@kms_cursor_legacy@basic-busy-flip-before-cursor-atomic.html
* igt@kms_dsc@dsc-basic:
- bat-arls-3: NOTRUN -> [SKIP][12] ([i915#9886])
[12]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@kms_dsc@dsc-basic.html
* igt@kms_force_connector_basic@force-load-detect:
- bat-arls-3: NOTRUN -> [SKIP][13] ([i915#10207])
[13]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@kms_force_connector_basic@force-load-detect.html
* igt@kms_pm_backlight@basic-brightness:
- bat-arls-3: NOTRUN -> [SKIP][14] ([i915#9812])
[14]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@kms_pm_backlight@basic-brightness.html
* igt@kms_psr@psr-primary-mmap-gtt:
- bat-arls-3: NOTRUN -> [SKIP][15] ([i915#9732]) +3 other tests skip
[15]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@kms_psr@psr-primary-mmap-gtt.html
* igt@kms_setmode@basic-clone-single-crtc:
- bat-arls-3: NOTRUN -> [SKIP][16] ([i915#10208] / [i915#8809])
[16]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@kms_setmode@basic-clone-single-crtc.html
* igt@prime_vgem@basic-fence-mmap:
- bat-arls-3: NOTRUN -> [SKIP][17] ([i915#10196] / [i915#3708] / [i915#4077]) +1 other test skip
[17]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@prime_vgem@basic-fence-mmap.html
* igt@prime_vgem@basic-fence-read:
- bat-arls-3: NOTRUN -> [SKIP][18] ([i915#10212] / [i915#3708])
[18]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@prime_vgem@basic-fence-read.html
* igt@prime_vgem@basic-read:
- bat-arls-3: NOTRUN -> [SKIP][19] ([i915#10214] / [i915#3708])
[19]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@prime_vgem@basic-read.html
* igt@prime_vgem@basic-write:
- bat-arls-3: NOTRUN -> [SKIP][20] ([i915#10216] / [i915#3708])
[20]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/bat-arls-3/igt@prime_vgem@basic-write.html
{name}: This element is suppressed. This means it is ignored when computing
the status of the difference (SUCCESS, WARNING, or FAILURE).
[i915#10026]: https://gitlab.freedesktop.org/drm/intel/issues/10026
[i915#10196]: https://gitlab.freedesktop.org/drm/intel/issues/10196
[i915#10197]: https://gitlab.freedesktop.org/drm/intel/issues/10197
[i915#10200]: https://gitlab.freedesktop.org/drm/intel/issues/10200
[i915#10202]: https://gitlab.freedesktop.org/drm/intel/issues/10202
[i915#10206]: https://gitlab.freedesktop.org/drm/intel/issues/10206
[i915#10207]: https://gitlab.freedesktop.org/drm/intel/issues/10207
[i915#10208]: https://gitlab.freedesktop.org/drm/intel/issues/10208
[i915#10209]: https://gitlab.freedesktop.org/drm/intel/issues/10209
[i915#10211]: https://gitlab.freedesktop.org/drm/intel/issues/10211
[i915#10212]: https://gitlab.freedesktop.org/drm/intel/issues/10212
[i915#10213]: https://gitlab.freedesktop.org/drm/intel/issues/10213
[i915#10214]: https://gitlab.freedesktop.org/drm/intel/issues/10214
[i915#10216]: https://gitlab.freedesktop.org/drm/intel/issues/10216
[i915#10436]: https://gitlab.freedesktop.org/drm/intel/issues/10436
[i915#3708]: https://gitlab.freedesktop.org/drm/intel/issues/3708
[i915#4077]: https://gitlab.freedesktop.org/drm/intel/issues/4077
[i915#4079]: https://gitlab.freedesktop.org/drm/intel/issues/4079
[i915#4083]: https://gitlab.freedesktop.org/drm/intel/issues/4083
[i915#8809]: https://gitlab.freedesktop.org/drm/intel/issues/8809
[i915#9318]: https://gitlab.freedesktop.org/drm/intel/issues/9318
[i915#9732]: https://gitlab.freedesktop.org/drm/intel/issues/9732
[i915#9812]: https://gitlab.freedesktop.org/drm/intel/issues/9812
[i915#9886]: https://gitlab.freedesktop.org/drm/intel/issues/9886
Build changes
-------------
* Linux: CI_DRM_14581 -> Patchwork_132400v1
CI-20190529: 20190529
CI_DRM_14581: 1bfe3965a846936d93b6e69385e53f1bd1c3b889 @ git://anongit.freedesktop.org/gfx-ci/linux
IGT_7806: 849cd963ce7e8222dcf17cc872d355181fd2c2a2 @ https://gitlab.freedesktop.org/drm/igt-gpu-tools.git
Patchwork_132400v1: 1bfe3965a846936d93b6e69385e53f1bd1c3b889 @ git://anongit.freedesktop.org/gfx-ci/linux
### Linux commits
45b0c2e13c27 drm/i915/hwmon: Get rid of devm
== Logs ==
For more details see: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v1/index.html
[-- Attachment #2: Type: text/html, Size: 9111 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH] drm/i915/hwmon: Get rid of devm
@ 2024-04-16 3:55 Ashutosh Dixit
0 siblings, 0 replies; 9+ messages in thread
From: Ashutosh Dixit @ 2024-04-16 3:55 UTC (permalink / raw)
To: intel-gfx
Cc: Badal Nilawar, Andi Shyti, Ville Syrjälä, linux-hwmon,
dri-devel
When both hwmon and hwmon drvdata (on which hwmon depends) are device
managed resources, the expectation, on device unbind, is that hwmon will be
released before drvdata. However, in i915 there are two separate code
paths, which both release either drvdata or hwmon and either can be
released before the other. These code paths (for device unbind) are as
follows (see also the bug referenced below):
Call Trace:
release_nodes+0x11/0x70
devres_release_group+0xb2/0x110
component_unbind_all+0x8d/0xa0
component_del+0xa5/0x140
intel_pxp_tee_component_fini+0x29/0x40 [i915]
intel_pxp_fini+0x33/0x80 [i915]
i915_driver_remove+0x4c/0x120 [i915]
i915_pci_remove+0x19/0x30 [i915]
pci_device_remove+0x32/0xa0
device_release_driver_internal+0x19c/0x200
unbind_store+0x9c/0xb0
and
Call Trace:
release_nodes+0x11/0x70
devres_release_all+0x8a/0xc0
device_unbind_cleanup+0x9/0x70
device_release_driver_internal+0x1c1/0x200
unbind_store+0x9c/0xb0
This means that in i915, if use devm, we cannot gurantee that hwmon will
always be released before drvdata. Which means that we have a uaf if hwmon
sysfs is accessed when drvdata has been released but hwmon hasn't.
The only way out of this seems to be do get rid of devm_ and release/free
everything explicitly during device unbind.
v2: Change commit message and other minor code changes
v3: Cleanup from i915_hwmon_register on error (Armin Wolf)
Closes: https://gitlab.freedesktop.org/drm/intel/-/issues/10366
Signed-off-by: Ashutosh Dixit <ashutosh.dixit@intel.com>
---
drivers/gpu/drm/i915/i915_hwmon.c | 54 ++++++++++++++++++++++---------
1 file changed, 38 insertions(+), 16 deletions(-)
diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c
index b758fd110c20..8cebf6f5b101 100644
--- a/drivers/gpu/drm/i915/i915_hwmon.c
+++ b/drivers/gpu/drm/i915/i915_hwmon.c
@@ -793,7 +793,7 @@ void i915_hwmon_register(struct drm_i915_private *i915)
if (!IS_DGFX(i915))
return;
- hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
+ hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
if (!hwmon)
return;
@@ -819,14 +819,12 @@ void i915_hwmon_register(struct drm_i915_private *i915)
hwm_get_preregistration_info(i915);
/* hwmon_dev points to device hwmon<i> */
- hwmon_dev = devm_hwmon_device_register_with_info(dev, ddat->name,
- ddat,
- &hwm_chip_info,
- hwm_groups);
- if (IS_ERR(hwmon_dev)) {
- i915->hwmon = NULL;
- return;
- }
+ hwmon_dev = hwmon_device_register_with_info(dev, ddat->name,
+ ddat,
+ &hwm_chip_info,
+ hwm_groups);
+ if (IS_ERR(hwmon_dev))
+ goto err;
ddat->hwmon_dev = hwmon_dev;
@@ -839,16 +837,40 @@ void i915_hwmon_register(struct drm_i915_private *i915)
if (!hwm_gt_is_visible(ddat_gt, hwmon_energy, hwmon_energy_input, 0))
continue;
- hwmon_dev = devm_hwmon_device_register_with_info(dev, ddat_gt->name,
- ddat_gt,
- &hwm_gt_chip_info,
- NULL);
- if (!IS_ERR(hwmon_dev))
- ddat_gt->hwmon_dev = hwmon_dev;
+ hwmon_dev = hwmon_device_register_with_info(dev, ddat_gt->name,
+ ddat_gt,
+ &hwm_gt_chip_info,
+ NULL);
+ if (IS_ERR(hwmon_dev))
+ goto err;
+
+ ddat_gt->hwmon_dev = hwmon_dev;
}
+ return;
+err:
+ i915_hwmon_unregister(i915);
}
void i915_hwmon_unregister(struct drm_i915_private *i915)
{
- fetch_and_zero(&i915->hwmon);
+ struct i915_hwmon *hwmon = fetch_and_zero(&i915->hwmon);
+ struct hwm_drvdata *ddat = &hwmon->ddat;
+ struct intel_gt *gt;
+ int i;
+
+ if (!hwmon)
+ return;
+
+ for_each_gt(gt, i915, i) {
+ struct hwm_drvdata *ddat_gt = hwmon->ddat_gt + i;
+
+ if (ddat_gt->hwmon_dev)
+ hwmon_device_unregister(ddat_gt->hwmon_dev);
+ }
+
+ if (ddat->hwmon_dev)
+ hwmon_device_unregister(ddat->hwmon_dev);
+
+ mutex_destroy(&hwmon->hwmon_lock);
+ kfree(hwmon);
}
--
2.41.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH] drm/i915/hwmon: Get rid of devm
@ 2024-04-17 14:56 Ashutosh Dixit
2024-04-18 21:56 ` Andi Shyti
0 siblings, 1 reply; 9+ messages in thread
From: Ashutosh Dixit @ 2024-04-17 14:56 UTC (permalink / raw)
To: intel-gfx
Cc: Badal Nilawar, Andi Shyti, Ville Syrjälä, Rodrigo Vivi,
Jani Nikula, linux-hwmon, dri-devel
When both hwmon and hwmon drvdata (on which hwmon depends) are device
managed resources, the expectation, on device unbind, is that hwmon will be
released before drvdata. However, in i915 there are two separate code
paths, which both release either drvdata or hwmon and either can be
released before the other. These code paths (for device unbind) are as
follows (see also the bug referenced below):
Call Trace:
release_nodes+0x11/0x70
devres_release_group+0xb2/0x110
component_unbind_all+0x8d/0xa0
component_del+0xa5/0x140
intel_pxp_tee_component_fini+0x29/0x40 [i915]
intel_pxp_fini+0x33/0x80 [i915]
i915_driver_remove+0x4c/0x120 [i915]
i915_pci_remove+0x19/0x30 [i915]
pci_device_remove+0x32/0xa0
device_release_driver_internal+0x19c/0x200
unbind_store+0x9c/0xb0
and
Call Trace:
release_nodes+0x11/0x70
devres_release_all+0x8a/0xc0
device_unbind_cleanup+0x9/0x70
device_release_driver_internal+0x1c1/0x200
unbind_store+0x9c/0xb0
This means that in i915, if use devm, we cannot gurantee that hwmon will
always be released before drvdata. Which means that we have a uaf if hwmon
sysfs is accessed when drvdata has been released but hwmon hasn't.
The only way out of this seems to be do get rid of devm_ and release/free
everything explicitly during device unbind.
v2: Change commit message and other minor code changes
v3: Cleanup from i915_hwmon_register on error (Armin Wolf)
v4: Eliminate potential static analyzer warning (Rodrigo)
Eliminate fetch_and_zero (Jani)
v5: Restore previous logic for ddat_gt->hwmon_dev error return (Andi)
Closes: https://gitlab.freedesktop.org/drm/intel/-/issues/10366
Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
Signed-off-by: Ashutosh Dixit <ashutosh.dixit@intel.com>
---
drivers/gpu/drm/i915/i915_hwmon.c | 46 +++++++++++++++++++++----------
1 file changed, 32 insertions(+), 14 deletions(-)
diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c
index b758fd110c20..c0662a022f59 100644
--- a/drivers/gpu/drm/i915/i915_hwmon.c
+++ b/drivers/gpu/drm/i915/i915_hwmon.c
@@ -793,7 +793,7 @@ void i915_hwmon_register(struct drm_i915_private *i915)
if (!IS_DGFX(i915))
return;
- hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
+ hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
if (!hwmon)
return;
@@ -819,14 +819,12 @@ void i915_hwmon_register(struct drm_i915_private *i915)
hwm_get_preregistration_info(i915);
/* hwmon_dev points to device hwmon<i> */
- hwmon_dev = devm_hwmon_device_register_with_info(dev, ddat->name,
- ddat,
- &hwm_chip_info,
- hwm_groups);
- if (IS_ERR(hwmon_dev)) {
- i915->hwmon = NULL;
- return;
- }
+ hwmon_dev = hwmon_device_register_with_info(dev, ddat->name,
+ ddat,
+ &hwm_chip_info,
+ hwm_groups);
+ if (IS_ERR(hwmon_dev))
+ goto err;
ddat->hwmon_dev = hwmon_dev;
@@ -839,16 +837,36 @@ void i915_hwmon_register(struct drm_i915_private *i915)
if (!hwm_gt_is_visible(ddat_gt, hwmon_energy, hwmon_energy_input, 0))
continue;
- hwmon_dev = devm_hwmon_device_register_with_info(dev, ddat_gt->name,
- ddat_gt,
- &hwm_gt_chip_info,
- NULL);
+ hwmon_dev = hwmon_device_register_with_info(dev, ddat_gt->name,
+ ddat_gt,
+ &hwm_gt_chip_info,
+ NULL);
if (!IS_ERR(hwmon_dev))
ddat_gt->hwmon_dev = hwmon_dev;
}
+ return;
+err:
+ i915_hwmon_unregister(i915);
}
void i915_hwmon_unregister(struct drm_i915_private *i915)
{
- fetch_and_zero(&i915->hwmon);
+ struct i915_hwmon *hwmon = i915->hwmon;
+ struct intel_gt *gt;
+ int i;
+
+ if (!hwmon)
+ return;
+
+ for_each_gt(gt, i915, i)
+ if (hwmon->ddat_gt[i].hwmon_dev)
+ hwmon_device_unregister(hwmon->ddat_gt[i].hwmon_dev);
+
+ if (hwmon->ddat.hwmon_dev)
+ hwmon_device_unregister(hwmon->ddat.hwmon_dev);
+
+ mutex_destroy(&hwmon->hwmon_lock);
+
+ kfree(i915->hwmon);
+ i915->hwmon = NULL;
}
--
2.41.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH] drm/i915/hwmon: Get rid of devm
2024-04-17 14:56 Ashutosh Dixit
@ 2024-04-18 21:56 ` Andi Shyti
2024-04-19 1:05 ` Dixit, Ashutosh
0 siblings, 1 reply; 9+ messages in thread
From: Andi Shyti @ 2024-04-18 21:56 UTC (permalink / raw)
To: Ashutosh Dixit
Cc: intel-gfx, Badal Nilawar, Ville Syrjälä, Rodrigo Vivi,
Jani Nikula, linux-hwmon, dri-devel
Hi Ashutosh,
On Wed, Apr 17, 2024 at 07:56:46AM -0700, Ashutosh Dixit wrote:
> When both hwmon and hwmon drvdata (on which hwmon depends) are device
> managed resources, the expectation, on device unbind, is that hwmon will be
> released before drvdata. However, in i915 there are two separate code
> paths, which both release either drvdata or hwmon and either can be
> released before the other. These code paths (for device unbind) are as
> follows (see also the bug referenced below):
>
> Call Trace:
> release_nodes+0x11/0x70
> devres_release_group+0xb2/0x110
> component_unbind_all+0x8d/0xa0
> component_del+0xa5/0x140
> intel_pxp_tee_component_fini+0x29/0x40 [i915]
> intel_pxp_fini+0x33/0x80 [i915]
> i915_driver_remove+0x4c/0x120 [i915]
> i915_pci_remove+0x19/0x30 [i915]
> pci_device_remove+0x32/0xa0
> device_release_driver_internal+0x19c/0x200
> unbind_store+0x9c/0xb0
>
> and
>
> Call Trace:
> release_nodes+0x11/0x70
> devres_release_all+0x8a/0xc0
> device_unbind_cleanup+0x9/0x70
> device_release_driver_internal+0x1c1/0x200
> unbind_store+0x9c/0xb0
>
> This means that in i915, if use devm, we cannot gurantee that hwmon will
> always be released before drvdata. Which means that we have a uaf if hwmon
> sysfs is accessed when drvdata has been released but hwmon hasn't.
>
> The only way out of this seems to be do get rid of devm_ and release/free
> everything explicitly during device unbind.
>
> v2: Change commit message and other minor code changes
> v3: Cleanup from i915_hwmon_register on error (Armin Wolf)
> v4: Eliminate potential static analyzer warning (Rodrigo)
> Eliminate fetch_and_zero (Jani)
> v5: Restore previous logic for ddat_gt->hwmon_dev error return (Andi)
Thanks!
Reviewed-by: Andi Shyti <andi.shyti@linux.intel.com>
Andi
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] drm/i915/hwmon: Get rid of devm
2024-04-18 21:56 ` Andi Shyti
@ 2024-04-19 1:05 ` Dixit, Ashutosh
0 siblings, 0 replies; 9+ messages in thread
From: Dixit, Ashutosh @ 2024-04-19 1:05 UTC (permalink / raw)
To: Andi Shyti
Cc: intel-gfx, Badal Nilawar, Ville Syrjälä, Rodrigo Vivi,
Jani Nikula, linux-hwmon, dri-devel
On Thu, 18 Apr 2024 14:56:58 -0700, Andi Shyti wrote:
>
> > v2: Change commit message and other minor code changes
> > v3: Cleanup from i915_hwmon_register on error (Armin Wolf)
> > v4: Eliminate potential static analyzer warning (Rodrigo)
> > Eliminate fetch_and_zero (Jani)
> > v5: Restore previous logic for ddat_gt->hwmon_dev error return (Andi)
>
> Thanks!
>
> Reviewed-by: Andi Shyti <andi.shyti@linux.intel.com>
Thanks a lot Andi, merged!
Ashutosh
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2024-04-19 1:05 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-13 0:10 [PATCH] drm/i915/hwmon: Get rid of devm Ashutosh Dixit
2024-04-13 14:43 ` Armin Wolf
2024-04-15 23:21 ` Dixit, Ashutosh
2024-04-14 23:23 ` Dixit, Ashutosh
2024-04-15 20:34 ` ✓ Fi.CI.BAT: success for " Patchwork
-- strict thread matches above, loose matches on Subject: below --
2024-04-16 3:55 [PATCH] " Ashutosh Dixit
2024-04-17 14:56 Ashutosh Dixit
2024-04-18 21:56 ` Andi Shyti
2024-04-19 1:05 ` Dixit, Ashutosh
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox