All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Karthik Poosa" <karthik.poosa@intel.com>,
	intel-xe@lists.freedesktop.org
Subject: Re: [PATCH v4 09/10] drm/xe/hwmon: Update fan info after late binding
Date: Tue, 24 Nov 2026 06:11:22 +0000	[thread overview]
Message-ID: <sashiko-outbox-230578@kernel.org> (raw)
In-Reply-To: <20261124054811.798763-10-karthik.poosa@intel.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] drm/xe/hwmon: known fans stay in full-speed mode after late
  binding
- [Medium] drm/xe/hwmon: post-late-bind fan updates miss sysfs and tach
  init
- [Medium] drm/xe/hwmon: use-after-free race in
  xe_hwmon_fan_update_post_lb()
- [Medium] drm/xe/hwmon: probe race between xe_hwmon_register() and late
  bind
- [Low] drm/xe/hwmon: unlocked max_rps read in xe_hwmon_fan_read()
--

commit 2b5439814ad9ed08f4a6cb30739b8d99fd5d96cb
Author: Karthik Poosa <karthik.poosa@intel.com>

drm/xe/hwmon: Update fan info after late binding

This commit adds xe_hwmon_fan_update_post_lb() to re-read the fan enable
mask and stock fan tables once the fan-control late-binding firmware is
loaded, initializing newly enabled fans in automatic mode.

> diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
> index 8efb5d2ce9ac2..c34f13b6e3ce8 100644
> --- a/drivers/gpu/drm/xe/xe_hwmon.c
> +++ b/drivers/gpu/drm/xe/xe_hwmon.c

[ ... ]

> @@ -1825,7 +1827,7 @@ static void xe_hwmon_init_curve_attrs(struct xe_hwmon *hwmon)
>  	hwmon->curve_group.is_visible = xe_hwmon_curve_attributes_visible;
>  }
>  
> -static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
> +static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon, u32 new_fan_mask)
>  {
>  	int point;
>  	int fan;
> @@ -1897,8 +1899,9 @@ static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
>  		if (!fi->fan_table[STOCK_FAN_TABLE].point_count)
>  			continue;
>  
> -		/* Set PWM enable mode to automatic i.e stock table in use. */
> -		fi->pwm_enable_mode = XE_FAN_PWM_AUTO_STOCK_TABLE;
> +		/* Start new fans in automatic mode; keep the user's mode for known fans. */
> +		if (new_fan_mask & BIT(fan))
> +			fi->pwm_enable_mode = XE_FAN_PWM_AUTO_STOCK_TABLE;
>  
>  		/* Read minimum fan PWM */
>  		ret = xe_hwmon_pcode_read_fan_control(hwmon, FSC_READ_FAN_MIN_PWM, fan,

[Severity: Medium]
Does gating fi->pwm_enable_mode initialization on new_fan_mask leave known
fans stuck at 0 (XE_FAN_PWM_FULL_SPEED) after late binding?

Before late binding completes, a fan can already be set in
hwmon->fan_enable_mask (which __xe_late_bind_fw_init() requires before even
loading XE_LB_FW_FAN_CONTROL) while its stock fan table is not yet
available in pcode.  During xe_hwmon_get_preregistration_info(),
xe_hwmon_read_fan_control_info() hits the early continue when
FSC_READ_STOCK_FAN_CONTROL_POINTS fails or returns 0, leaving the
zero-allocated fi->pwm_enable_mode at XE_FAN_PWM_FULL_SPEED.

When xe_hwmon_fan_update_post_lb() later runs, BIT(fan) is already in
old_mask, so hwmon->fan_enable_mask & ~old_mask clears BIT(fan) and
fi->pwm_enable_mode is never set to XE_FAN_PWM_AUTO_STOCK_TABLE.  As a
result, pwmN_enable reports 0, reading the fan curve selects USER_FAN_TABLE
instead of STOCK_FAN_TABLE, and xe_hwmon_suspend() treats the fan as using
a manual user table.

> @@ -2824,7 +2827,7 @@ xe_hwmon_get_preregistration_info(struct xe_hwmon *hwmon)
>  				xe_hwmon_fan_input_read(hwmon, channel, &fan_speed);
>  
>  		/* Fan control tables initialization */
> -		if (xe_hwmon_read_fan_control_info(hwmon))
> +		if (xe_hwmon_read_fan_control_info(hwmon, hwmon->fan_enable_mask))
>  			xe_warn(hwmon->xe, "Fan control tables are not available\n");
>  	}
>  	return xe_hwmon_init_temp_info(hwmon);

[Severity: Medium]
Can xe_hwmon_get_preregistration_info() and hwmon_device_register_with_info()
race with xe_hwmon_fan_update_post_lb() during device probe?

In xe_device_probe(), xe_late_bind_init() queues xe_late_bind_work() before
xe_hwmon_register() runs, and xe_hwmon_register() assigns xe->hwmon = hwmon
before calling xe_hwmon_get_preregistration_info() and
hwmon_device_register_with_info() without holding hwmon->hwmon_lock:

CPU0                                  CPU1 (late_bind->wq)
xe_hwmon_register()
  xe->hwmon = hwmon
  xe_hwmon_get_preregistration_info()
                                      xe_late_bind_work()
                                        xe_hwmon_fan_update_post_lb()
                                          mutex_lock(&hwmon->hwmon_lock)
                                          xe_hwmon_get_fan_enable_mask()
                                          xe_hwmon_read_fan_control_info()
                                          mutex_unlock(&hwmon->hwmon_lock)
    xe_hwmon_get_fan_enable_mask()
    xe_hwmon_read_fan_control_info()  <-- unlocked data race / overwrite

Concurrent execution can overwrite the post-late-binding fan state or
transiently zero hwmon->fan_enable_mask while
hwmon_device_register_with_info() is evaluating sysfs attribute visibility.

> @@ -3033,4 +3036,34 @@ void xe_hwmon_resume(struct xe_device *xe, bool runtime)
>  	mutex_unlock(&hwmon->hwmon_lock);
>  }
>  
> +/**
> + * xe_hwmon_fan_update_post_lb - Update fan info after late binding
> + * @xe: xe device instance
> + *
> + * This function reads the number of fans and their stock table after late binding.
> + */
> +void xe_hwmon_fan_update_post_lb(struct xe_device *xe)
> +{
> +	struct xe_hwmon *hwmon = xe->hwmon;
> +	u32 old_mask;
> +
> +	if (!hwmon || !xe->info.has_fan_control)
> +		return;
> +
> +	mutex_lock(&hwmon->hwmon_lock);

[Severity: Medium]
Can xe_hwmon_fan_update_post_lb() race with device removal or probe unwind
and cause a use-after-free on struct xe_hwmon?

In xe_device_probe(), xe_late_bind_init() registers its devm cleanup action
(xe_late_bind_remove(), which flushes late_bind->wq) before
xe_hwmon_register() allocates struct xe_hwmon and initializes
hwmon->hwmon_lock via devm.  On teardown, xe_hwmon_unregister() clears
xe->hwmon = NULL without waiting for late_bind->wq, and devres releases
resources in reverse order, freeing struct xe_hwmon before
xe_late_bind_remove() runs:

CPU0                                  CPU1 (late_bind->wq)
                                      xe_late_bind_work()
                                        xe_hwmon_fan_update_post_lb()
                                          hwmon = xe->hwmon
xe_device_remove()
  xe_hwmon_unregister()
    xe->hwmon = NULL
devres_release_all()
  /* frees hwmon & hwmon_lock */
                                          mutex_lock(&hwmon->hwmon_lock)
                                            <-- use-after-free
  xe_late_bind_remove()
    wait_for_worker_completion()

> +
> +	old_mask = hwmon->fan_enable_mask;
> +	if (xe_hwmon_get_fan_enable_mask(hwmon)) {
> +		xe_warn(hwmon->xe, "keeping fan enable mask %#x after late binding\n", old_mask);
> +		hwmon->fan_enable_mask = old_mask;
> +		goto unlock;
> +	}
> +
> +	if (xe_hwmon_read_fan_control_info(hwmon, hwmon->fan_enable_mask & ~old_mask))
> +		xe_warn(hwmon->xe, "failed to read stock fan tables after late binding\n");
> +
> +unlock:
> +	mutex_unlock(&hwmon->hwmon_lock);
> +}

[Severity: Medium]
When xe_hwmon_fan_update_post_lb() runs after
hwmon_device_register_with_info() has already completed, how do newly
discovered fans or newly populated fi->max_rps values become visible in
sysfs and have their tachometer state initialized?

hwmon_device_register_with_info() evaluates xe_hwmon_is_visible() (which
gates fan and pwm attributes on IS_FAN_ENABLED() and fanN_max on
hwmon->fi[channel].max_rps) and xe_hwmon_curve_attributes_visible() only
once during registration.  Any fan added to hwmon->fan_enable_mask or any
fi->max_rps populated after late binding will not have its sysfs attributes
created, and newly enabled fans also miss the initial
xe_hwmon_fan_input_read() call that xe_hwmon_get_preregistration_info()
uses to seed fi->reg_val_prev and fi->time_prev.

[Severity: Low]
Does updating fi->max_rps in xe_hwmon_read_fan_control_info() after hwmon
registration race with unlocked reads of hwmon->fi[channel].max_rps in
xe_hwmon_fan_read()?

drivers/gpu/drm/xe/xe_hwmon.c:xe_hwmon_fan_read() {
    ...
	case hwmon_fan_max:
		*val = mul_u32_u32(hwmon->fi[channel].max_rps, 60);
		return 0;
    ...
}

Because __pcode_mailbox_rw() writes the raw PCODE_DATA0 register into
&fi->max_rps before checking pcode_mailbox_status() and
xe_hwmon_read_fan_control_info() only zeroes fi->max_rps after
xe_hwmon_pcode_read_fan_control() returns an error, a concurrent reader in
xe_hwmon_fan_read() without hwmon->hwmon_lock can observe an unvalidated
register value.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124054811.798763-1-karthik.poosa@intel.com?part=9

  reply	other threads:[~2026-11-24  6:11 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  5:48 [PATCH v4 00/10] drm/xe/hwmon: Add fan control support Karthik Poosa
2026-11-24  5:48 ` [PATCH v4 01/10] drm/xe/hwmon: Initialize fan-control backend and table cache Karthik Poosa
2026-11-27 21:31   ` Purkait, Soham
2026-11-24  5:48 ` [PATCH v4 02/10] drm/xe/hwmon: Expose fanN_max Karthik Poosa
2026-11-25 18:26   ` Rodrigo Vivi
2026-11-24  5:48 ` [PATCH v4 03/10] drm/xe/pcode: Add ordered write batch helper Karthik Poosa
2026-11-25 18:29   ` Rodrigo Vivi
2026-11-26 13:48     ` Poosa, Karthik
2026-11-24  5:48 ` [PATCH v4 04/10] drm/xe/hwmon: Expose pwm[1-3] Karthik Poosa
2026-11-24  5:48 ` [PATCH v4 05/10] drm/xe/hwmon: Expose pwm[1-3]_enable Karthik Poosa
2026-11-24  6:01   ` sashiko-bot
2026-11-25 13:26     ` Poosa, Karthik
2026-11-24  5:48 ` [PATCH v4 06/10] drm/xe/hwmon: Enable fan curve control support Karthik Poosa
2026-11-24  6:02   ` sashiko-bot
2026-11-26 17:05     ` Poosa, Karthik
2026-11-24  5:48 ` [PATCH v4 07/10] drm/xe/hwmon: Add kernel-doc for fan control Karthik Poosa
2026-11-24  5:53   ` sashiko-bot
2026-11-26 12:33     ` Poosa, Karthik
2026-11-24  5:48 ` [PATCH v4 08/10] drm/xe/hwmon: Preserve fan user table across suspend resume Karthik Poosa
2026-11-24  6:07   ` sashiko-bot
2026-11-26 13:35     ` Poosa, Karthik
2026-11-24  5:48 ` [PATCH v4 09/10] drm/xe/hwmon: Update fan info after late binding Karthik Poosa
2026-11-24  6:11   ` sashiko-bot [this message]
2026-11-24  5:48 ` [PATCH v4 10/10] drm/xe/hwmon: Enable fan support for CRI Karthik Poosa
2026-11-25 18:30   ` Rodrigo Vivi
2026-11-24  5:56 ` ✗ CI.KUnit: failure for drm/xe/hwmon: Add fan control support Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-230578@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=karthik.poosa@intel.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.