Skip to contentSkip to conversation

drm/i915/hwmon: Get rid of devm

PATCH Superseded Ashutosh Dixit 5 participants 8 messages Apr 15, 2024 – Apr 16, 2024

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 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

---
 drivers/gpu/drm/i915/i915_hwmon.c | 41 +++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/i915_hwmon.c +31 -10
index 8c3f443c8347..46c24b1ee6df 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,26 @@ 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 = 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);
+			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 16.04.24 um 00:36 schrieb Ashutosh Dixit:

Quoted 61 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 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

---
  drivers/gpu/drm/i915/i915_hwmon.c | 41 +++++++++++++++++++++++--------
  1 file changed, 31 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/i915_hwmon.c +1 -1
index 8c3f443c8347..46c24b1ee6df 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> */

drivers/gpu/drm/i915/i915_hwmon.c void i915_hwmon_register(struct drm_i915_private *i915) · line 826

Quoted 10 lines
drivers/gpu/drm/i915/i915_hwmon.c +4 -4
-	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;

Hi,

you need to free hwmon here, since it is not managed by devres anymore.

Quoted 17 lines
drivers/gpu/drm/i915/i915_hwmon.c +4 -4
  		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;
  	}

drivers/gpu/drm/i915/i915_hwmon.c void i915_hwmon_register(struct drm_i915_private *i915) · line 852

Quoted 5 lines
drivers/gpu/drm/i915/i915_hwmon.c +1 -1
@@ -849,5 +849,26 @@ 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 = fetch_and_zero(&i915->hwmon);

Why is fetch_and_zero() necessary here?

Thanks,
Armin Wolf

Quoted 22 lines
drivers/gpu/drm/i915/i915_hwmon.c +21 -0
+	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);
+			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 4 lines

Am 16.04.24 um 00:36 schrieb Ashutosh Dixit:

Quoted 3 lines
drivers/gpu/drm/i915/i915_hwmon.c +0 -0
@@ -818,10 +818,10 @@ void i915_hwmon_register(struct drm_i915_private *i915)
 	hwm_get_preregistration_info(i915);
 
Quoted 13 lines

drivers/gpu/drm/i915/i915_hwmon.c void i915_hwmon_register(struct drm_i915_private *i915) · line 826

Quoted 11 lines
drivers/gpu/drm/i915/i915_hwmon.c +4 -4
 	/*  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;

you need to free hwmon here, since it is not managed by devres anymore.

Thanks a lot for catching this, I had missed it in v2, it's fixed in v3. I am actually reusing i915_hwmon_unregister() for error unwinding in v3.

Quoted 19 lines
Quoted 19 lines
drivers/gpu/drm/i915/i915_hwmon.c +4 -4
 		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,26 @@ void i915_hwmon_register(struct drm_i915_private *i915)
 
Quoted 6 lines

drivers/gpu/drm/i915/i915_hwmon.c void i915_hwmon_register(struct drm_i915_private *i915) · line 852

Quoted 4 lines
drivers/gpu/drm/i915/i915_hwmon.c +1 -1
  void i915_hwmon_unregister(struct drm_i915_private *i915)
  {
-	fetch_and_zero(&i915->hwmon);
+	struct i915_hwmon *hwmon = fetch_and_zero(&i915->hwmon);

Why is fetch_and_zero() necessary here?

As mentioned, in v3 i915_hwmon_unregister() itself is used for error unwinding so we need to prevent multiple device_unregister's etc. That is the purpose of setting i915->hwmon to NULL. But even earlier, though it is not obvious, i915_hwmon_unregister() is called multiple times. So e.g. it will be called at device unbind as well as module unload. So once again we prevent multiple device_unregister's by setting and checking for NULL i915->hwmon.

Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/i915/i915_hwmon.c +14 -0
+	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);
+			ddat_gt->hwmon_dev = NULL;
+		}
Quoted 8 lines

drivers/gpu/drm/i915/i915_hwmon.c void i915_hwmon_register(struct drm_i915_private *i915) · line 874

Quoted 8 lines
drivers/gpu/drm/i915/i915_hwmon.c +7 -0
+	}
+
+	if (ddat->hwmon_dev)
+		hwmon_device_unregister(ddat->hwmon_dev);
+
+	mutex_destroy(&hwmon->hwmon_lock);
+	kfree(hwmon);
  }

Thanks.
--
Ashutosh

maintainer
#3 lore raw
Quoted 52 lines · Ashutosh
Quoted 1 line · Armin Wolf

Hi Armin,

Quoted 4 lines

Am 16.04.24 um 00:36 schrieb Ashutosh Dixit:

Quoted 3 lines
drivers/gpu/drm/i915/i915_hwmon.c +0 -0
@@ -818,10 +818,10 @@ void i915_hwmon_register(struct drm_i915_private *i915)
 	hwm_get_preregistration_info(i915);
 
Quoted 13 lines

drivers/gpu/drm/i915/i915_hwmon.c void i915_hwmon_register(struct drm_i915_private *i915) · line 826

Quoted 11 lines
drivers/gpu/drm/i915/i915_hwmon.c +4 -4
 	/*  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;

you need to free hwmon here, since it is not managed by devres anymore.

Thanks a lot for catching this, I had missed it in v2, it's fixed in v3. I am actually reusing i915_hwmon_unregister() for error unwinding in v3.

Quoted 19 lines
Quoted 19 lines
drivers/gpu/drm/i915/i915_hwmon.c +4 -4
 		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,26 @@ void i915_hwmon_register(struct drm_i915_private *i915)
 
Quoted 6 lines

drivers/gpu/drm/i915/i915_hwmon.c void i915_hwmon_register(struct drm_i915_private *i915) · line 852

Quoted 4 lines
drivers/gpu/drm/i915/i915_hwmon.c +1 -1
  void i915_hwmon_unregister(struct drm_i915_private *i915)
  {
-	fetch_and_zero(&i915->hwmon);
+	struct i915_hwmon *hwmon = fetch_and_zero(&i915->hwmon);

Why is fetch_and_zero() necessary here?

Quoted 7 lines

As mentioned, in v3 i915_hwmon_unregister() itself is used for error unwinding so we need to prevent multiple device_unregister's etc. That is the purpose of setting i915->hwmon to NULL. But even earlier, though it is not obvious, i915_hwmon_unregister() is called multiple times. So e.g. it will be called at device unbind as well as module unload. So once again we prevent multiple device_unregister's by setting and checking for NULL i915->hwmon.

IMO it's more obvious to set i915->hwmon to NULL separately.

BR,
Jani.

Quoted 28 lines
Quoted 16 lines
Quoted 16 lines
drivers/gpu/drm/i915/i915_hwmon.c +14 -0
+	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);
+			ddat_gt->hwmon_dev = NULL;
+		}
Quoted 8 lines

drivers/gpu/drm/i915/i915_hwmon.c void i915_hwmon_register(struct drm_i915_private *i915) · line 874

Quoted 8 lines
drivers/gpu/drm/i915/i915_hwmon.c +7 -0
+	}
+
+	if (ddat->hwmon_dev)
+		hwmon_device_unregister(ddat->hwmon_dev);
+
+	mutex_destroy(&hwmon->hwmon_lock);
+	kfree(hwmon);
  }

Thanks.
--
Ashutosh

Signature · 1 line
Jani Nikula, Intel
#4 lore raw

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

In reply to Ashutosh Dixit
== Series Details ==

== Series Details ==

Series: drm/i915/hwmon: Get rid of devm (rev2)
URL : https://patchwork.freedesktop.org/series/132400/
State : success

== Summary ==

CI Bug Log - changes from CI_DRM_14582 -> Patchwork_132400v2
====================================================

Summary
-------

**SUCCESS**

No regressions found.

External URL: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/index.html

Participating hosts (38 -> 31)
------------------------------

Additional (1): bat-mtlp-8
Missing (8): bat-kbl-2 bat-mtlp-6 fi-apl-guc fi-snb-2520m fi-glk-j4005 fi-cfl-8109u fi-elk-e7500 bat-arls-3

Possible new issues
-------------------

Here are the unknown changes that may have been introduced in Patchwork_132400v2:

### IGT changes ###

#### Suppressed ####

The following results come from untrusted machines, tests, or statuses. They do not affect the overall result.

* igt@kms_pipe_crc_basic@compare-crc-sanitycheck-xr24@pipe-a-dp-1:
- {bat-rpls-4}: [PASS][1] -> [DMESG-WARN][2]
[1]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_14582/bat-rpls-4/igt@kms_pipe_crc_basic@[email protected]
[2]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-rpls-4/igt@kms_pipe_crc_basic@[email protected]

Known issues
------------

Here are the changes found in Patchwork_132400v2 that come from known issues:

### IGT changes ###

#### Issues hit ####

* igt@debugfs_test@basic-hwmon:
- bat-mtlp-8: NOTRUN -> [SKIP][3] ([i915#9318])
[3]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@gem_lmem_swapping@verify-random:
- bat-mtlp-8: NOTRUN -> [SKIP][4] ([i915#4613]) +3 other tests skip
[4]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@gem_mmap@basic:
- bat-mtlp-8: NOTRUN -> [SKIP][5] ([i915#4083])
[5]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@gem_mmap_gtt@basic:
- bat-mtlp-8: NOTRUN -> [SKIP][6] ([i915#4077]) +2 other tests skip
[6]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@gem_render_tiled_blits@basic:
- bat-mtlp-8: NOTRUN -> [SKIP][7] ([i915#4079]) +1 other test skip
[7]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@i915_pm_rps@basic-api:
- bat-mtlp-8: NOTRUN -> [SKIP][8] ([i915#6621])
[8]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@i915_selftest@live@gem:
- bat-atsm-1: NOTRUN -> [ABORT][9] ([i915#10182] / [i915#10564])
[9]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-atsm-1/igt@i915_selftest@[email protected]

* igt@kms_addfb_basic@addfb25-y-tiled-small-legacy:
- bat-mtlp-8: NOTRUN -> [SKIP][10] ([i915#5190])
[10]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@kms_addfb_basic@basic-y-tiled-legacy:
- bat-mtlp-8: NOTRUN -> [SKIP][11] ([i915#4212]) +8 other tests skip
[11]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@kms_cursor_legacy@basic-busy-flip-before-cursor-legacy:
- bat-mtlp-8: NOTRUN -> [SKIP][12] ([i915#4213]) +1 other test skip
[12]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@kms_dsc@dsc-basic:
- bat-mtlp-8: NOTRUN -> [SKIP][13] ([i915#3555] / [i915#3840] / [i915#9159])
[13]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@kms_force_connector_basic@force-load-detect:
- bat-mtlp-8: NOTRUN -> [SKIP][14]
[14]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@kms_force_connector_basic@prune-stale-modes:
- bat-mtlp-8: NOTRUN -> [SKIP][15] ([i915#5274])
[15]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@kms_psr@psr-primary-mmap-gtt@edp-1:
- bat-mtlp-8: NOTRUN -> [SKIP][16] ([i915#4077] / [i915#9688])
[16]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@kms_psr@[email protected]

* igt@kms_setmode@basic-clone-single-crtc:
- bat-mtlp-8: NOTRUN -> [SKIP][17] ([i915#3555] / [i915#8809])
[17]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@prime_vgem@basic-fence-mmap:
- bat-mtlp-8: NOTRUN -> [SKIP][18] ([i915#3708] / [i915#4077]) +1 other test skip
[18]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@prime_vgem@basic-fence-read:
- bat-mtlp-8: NOTRUN -> [SKIP][19] ([i915#3708]) +1 other test skip
[19]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

* igt@prime_vgem@basic-write:
- bat-mtlp-8: NOTRUN -> [SKIP][20] ([i915#10216] / [i915#3708])
[20]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-mtlp-8/igt@[email protected]

#### Possible fixes ####

* igt@i915_selftest@live@execlists:
- bat-adls-6: [TIMEOUT][21] ([i915#10795]) -> [PASS][22]
[21]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_14582/bat-adls-6/igt@i915_selftest@[email protected]
[22]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-adls-6/igt@i915_selftest@[email protected]

* igt@i915_selftest@live@gt_timelines:
- bat-atsm-1: [INCOMPLETE][23] ([i915#10461]) -> [PASS][24]
[23]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_14582/bat-atsm-1/igt@i915_selftest@live@gt_timelines.html
[24]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/bat-atsm-1/igt@i915_selftest@live@gt_timelines.html

{name}: This element is suppressed. This means it is ignored when computing
the status of the difference (SUCCESS, WARNING, or FAILURE).

[i915#10182]: https://gitlab.freedesktop.org/drm/intel/issues/10182
[i915#10216]: https://gitlab.freedesktop.org/drm/intel/issues/10216
[i915#10435]: https://gitlab.freedesktop.org/drm/intel/issues/10435
[i915#10461]: https://gitlab.freedesktop.org/drm/intel/issues/10461
[i915#10564]: https://gitlab.freedesktop.org/drm/intel/issues/10564
[i915#10795]: https://gitlab.freedesktop.org/drm/intel/issues/10795
[i915#3555]: https://gitlab.freedesktop.org/drm/intel/issues/3555
[i915#3708]: https://gitlab.freedesktop.org/drm/intel/issues/3708
[i915#3840]: https://gitlab.freedesktop.org/drm/intel/issues/3840
[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#4212]: https://gitlab.freedesktop.org/drm/intel/issues/4212
[i915#4213]: https://gitlab.freedesktop.org/drm/intel/issues/4213
[i915#4613]: https://gitlab.freedesktop.org/drm/intel/issues/4613
[i915#5190]: https://gitlab.freedesktop.org/drm/intel/issues/5190
[i915#5274]: https://gitlab.freedesktop.org/drm/intel/issues/5274
[i915#6621]: https://gitlab.freedesktop.org/drm/intel/issues/6621
[i915#8809]: https://gitlab.freedesktop.org/drm/intel/issues/8809
[i915#9159]: https://gitlab.freedesktop.org/drm/intel/issues/9159
[i915#9318]: https://gitlab.freedesktop.org/drm/intel/issues/9318
[i915#9688]: https://gitlab.freedesktop.org/drm/intel/issues/9688

Build changes
-------------

* Linux: CI_DRM_14582 -> Patchwork_132400v2

CI-20190529: 20190529
CI_DRM_14582: 7bc330055f5c2924b42e389887691fea3f401a45 @ git://anongit.freedesktop.org/gfx-ci/linux
IGT_7806: 849cd963ce7e8222dcf17cc872d355181fd2c2a2 @ https://gitlab.freedesktop.org/drm/igt-gpu-tools.git
Patchwork_132400v2: 7bc330055f5c2924b42e389887691fea3f401a45 @ git://anongit.freedesktop.org/gfx-ci/linux

### Linux commits

da6f6c509896 drm/i915/hwmon: Get rid of devm

== Logs ==

For more details see: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_132400v2/index.html

maintainer Reviewed-by on v2 1/1
#5 lore raw
In reply to Ashutosh Dixit
Quoted 89 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 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

---
 drivers/gpu/drm/i915/i915_hwmon.c | 41 +++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/i915_hwmon.c +9 -9
index 8c3f443c8347..46c24b1ee6df 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,26 @@ void i915_hwmon_register(struct drm_i915_private *i915)
 

drivers/gpu/drm/i915/i915_hwmon.c void i915_hwmon_register(struct drm_i915_private *i915) · line 858

Quoted 10 lines
drivers/gpu/drm/i915/i915_hwmon.c +7 -1
 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;

"that's too late", we are going to hear from static analyzer tools.

beter to move ddat = &hwmon->ddat; after this return.

with that,

Quoted 19 lines
drivers/gpu/drm/i915/i915_hwmon.c +15 -0
+
+	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);
+			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
#6 lore raw
Quoted 1 line · Rodrigo Vivi

Hi Rodrigo,

Quoted 15 lines

drivers/gpu/drm/i915/i915_hwmon.c void i915_hwmon_register(struct drm_i915_private *i915) · line 858

Quoted 12 lines
drivers/gpu/drm/i915/i915_hwmon.c +7 -1
@@ -849,5 +849,26 @@ 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 = fetch_and_zero(&i915->hwmon);
+	struct hwm_drvdata *ddat = &hwmon->ddat;
+	struct intel_gt *gt;
+	int i;
+
+	if (!hwmon)
+		return;

"that's too late", we are going to hear from static analyzer tools.

Quoted 1 line

beter to move ddat = &hwmon->ddat; after this return.

Yeah, I worried a lot about it :/ But then finally decided (and verified) that we are never actually dereferencing the (possibly NULL) pointer.

But not sure about static analyzer tools, maybe you are right, I'll move it.

Quoted 3 lines

with that,

Thanks a lot :)

Ashutosh

Quoted 20 lines
Quoted 20 lines
drivers/gpu/drm/i915/i915_hwmon.c +15 -1
+
+	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);
+			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

maintainer
#7 lore raw
Quoted 20 lines · Ashutosh
Quoted 1 line · Rodrigo Vivi

Hi Rodrigo,

Quoted 15 lines

drivers/gpu/drm/i915/i915_hwmon.c void i915_hwmon_register(struct drm_i915_private *i915) · line 858

Quoted 12 lines
drivers/gpu/drm/i915/i915_hwmon.c +7 -1
@@ -849,5 +849,26 @@ 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 = fetch_and_zero(&i915->hwmon);
+	struct hwm_drvdata *ddat = &hwmon->ddat;
+	struct intel_gt *gt;
+	int i;
+
+	if (!hwmon)
+		return;

"that's too late", we are going to hear from static analyzer tools.

Quoted 4 lines
Quoted 1 line

beter to move ddat = &hwmon->ddat; after this return.

Yeah, I worried a lot about it :/ But then finally decided (and verified) that we are never actually dereferencing the (possibly NULL) pointer.

yeap, another acceptable approach is to simply remove this check entirely.

Quoted 32 lines

But not sure about static analyzer tools, maybe you are right, I'll move it.

Quoted 3 lines

with that,

Thanks a lot :)

Ashutosh

Quoted 20 lines
Quoted 20 lines
drivers/gpu/drm/i915/i915_hwmon.c +15 -1
+
+	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);
+			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