Skip to contentSkip to conversation

drm/i915/hwmon: Get rid of devm

PATCH Superseded Ashutosh Dixit 3 participants 5 messages Apr 13, 2024 – Apr 15, 2024

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/i915/hwmon: Get rid of devm

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.

---
 drivers/gpu/drm/i915/i915_hwmon.c | 46 ++++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/i915_hwmon.c +36 -10
index 8c3f443c8347..5f6022b148d7 100644
@@ -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);
 }
Signature · 1 line
2.41.0
#1 lore raw

Am 13.04.24 um 02:10 schrieb Ashutosh Dixit:

Quoted 9 lines

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

Quoted 82 lines
---
  drivers/gpu/drm/i915/i915_hwmon.c | 46 ++++++++++++++++++++++++-------
  1 file changed, 36 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/i915_hwmon.c +36 -10
index 8c3f443c8347..5f6022b148d7 100644
@@ -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 lore raw
Quoted 1 line · Armin Wolf

Hi Armin,

Quoted 12 lines

Am 13.04.24 um 02:10 schrieb Ashutosh Dixit:

Quoted 9 lines

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.

Quoted 8 lines

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.

Quoted 2 lines

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).

Quoted 1 line

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

#3 lore raw
In reply to Ashutosh Dixit
Quoted 8 lines · 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.

Quoted 5 lines

The only way out of this seems to be do get rid of devm_ and release/free everything explicitly during device unbind.

Please don't review this patch yet, I will send a v2 tomorrow. Please review the v2. Thanks.

#4 lore raw

✓ Fi.CI.BAT: success for drm/i915/hwmon: Get rid of devm

In reply to Ashutosh Dixit
== Series Details ==

== 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@[email protected]

* 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@[email protected]

* 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@[email protected]

* 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@[email protected]

* 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@[email protected]

* 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@[email protected]

* 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@[email protected]

* 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@[email protected]

* 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@[email protected]

* 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@[email protected]

* 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@[email protected]

* 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@[email protected]

* 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@[email protected]

* 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@[email protected]

* 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@[email protected]

* 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@[email protected]

* 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@[email protected]

{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