All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Emre Cecanpunar <emreleno@gmail.com>
Cc: platform-driver-x86@vger.kernel.org,
	Hans de Goede <hansg@kernel.org>,
	 LKML <linux-kernel@vger.kernel.org>,
	krishna.chomal108@gmail.com,  edip@medip.dev
Subject: Re: [PATCH v3 5/5] platform/x86: hp-wmi: add locking for concurrent hwmon access
Date: Tue, 7 Apr 2026 11:07:54 +0300 (EEST)	[thread overview]
Message-ID: <7809193b-d125-d57f-4875-ce1bc7cbc52f@linux.intel.com> (raw)
In-Reply-To: <20260405144502.24944-6-emreleno@gmail.com>

On Sun, 5 Apr 2026, Emre Cecanpunar wrote:

> hp_wmi_hwmon_priv.mode and .pwm are written by hp_wmi_hwmon_write() in
> sysfs context and read by hp_wmi_hwmon_keep_alive_handler() in a
> workqueue. A concurrent write and keep-alive expiry can observe an
> inconsistent mode/pwm pair (e.g. mode=MANUAL with a stale pwm).
> 
> Add a mutex to hp_wmi_hwmon_priv protecting mode and pwm. Hold it in
> hp_wmi_hwmon_write() across the field update and apply call, and in
> hp_wmi_hwmon_keep_alive_handler() before calling apply.
> 
> In hp_wmi_hwmon_read(), only the pwm_enable path reads priv->mode; use
> scoped_guard() there to avoid holding the lock across unrelated WMI
> calls.
> 
> Fixes: c203c59fb5de ("platform/x86: hp-wmi: implement fan keep-alive")
> Signed-off-by: Emre Cecanpunar <emreleno@gmail.com>
> ---
>  drivers/platform/x86/hp/hp-wmi.c | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/platform/x86/hp/hp-wmi.c b/drivers/platform/x86/hp/hp-wmi.c
> index db72ad9da0a5..1096fb46cbcd 100644
> --- a/drivers/platform/x86/hp/hp-wmi.c
> +++ b/drivers/platform/x86/hp/hp-wmi.c
> @@ -453,6 +453,7 @@ enum pwm_modes {
>  };
>  
>  struct hp_wmi_hwmon_priv {
> +	struct mutex lock;	/* protects mode, pwm */
>  	u8 min_rpm;
>  	u8 max_rpm;
>  	int gpu_delta;
> @@ -2422,6 +2423,7 @@ static int hp_wmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
>  {
>  	struct hp_wmi_hwmon_priv *priv;
>  	int rpm, ret;
> +	u8 mode;
>  
>  	priv = dev_get_drvdata(dev);
>  	switch (type) {
> @@ -2445,11 +2447,13 @@ static int hp_wmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
>  			*val = rpm_to_pwm(rpm / 100, priv);
>  			return 0;
>  		}
> -		switch (priv->mode) {
> +		scoped_guard(mutex, &priv->lock)
> +			mode = priv->mode;
> +		switch (mode) {
>  		case PWM_MODE_MAX:
>  		case PWM_MODE_MANUAL:
>  		case PWM_MODE_AUTO:
> -			*val = priv->mode;
> +			*val = mode;
>  			return 0;
>  		default:
>  			/* shouldn't happen */
> @@ -2467,6 +2471,7 @@ static int hp_wmi_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
>  	int rpm;
>  
>  	priv = dev_get_drvdata(dev);
> +	guard(mutex)(&priv->lock);
>  	switch (type) {
>  	case hwmon_pwm:
>  		if (attr == hwmon_pwm_input) {
> @@ -2535,6 +2540,8 @@ static void hp_wmi_hwmon_keep_alive_handler(struct work_struct *work)
>  
>  	dwork = to_delayed_work(work);
>  	priv = container_of(dwork, struct hp_wmi_hwmon_priv, keep_alive_dwork);
> +
> +	guard(mutex)(&priv->lock);
>  	/*
>  	 * Re-apply the current hwmon context settings.
>  	 * NOTE: hp_wmi_apply_fan_settings will handle the re-scheduling.
> @@ -2592,6 +2599,8 @@ static int hp_wmi_hwmon_init(void)
>  	if (!priv)
>  		return -ENOMEM;
>  
> +	mutex_init(&priv->lock);

Please use devm_mutex_init() so you don't need to deinit it manually with 
mutex_destroy(). While changing that, don't forget that devm_*() can fail 
so remember to handle the error.

-- 
 i.


      reply	other threads:[~2026-04-07  8:07 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-05 14:44 [PATCH v3 0/5] platform/x86: hp-wmi: Victus S fan control fixes Emre Cecanpunar
2026-04-05 14:44 ` [PATCH v3 1/5] platform/x86: hp-wmi: fix ignored return values in fan settings Emre Cecanpunar
2026-04-05 14:44 ` [PATCH v3 2/5] platform/x86: hp-wmi: avoid cancel_delayed_work_sync from work handler Emre Cecanpunar
2026-04-05 14:44 ` [PATCH v3 3/5] platform/x86: hp-wmi: use mod_delayed_work to reset keep-alive timer Emre Cecanpunar
2026-04-05 14:44 ` [PATCH v3 4/5] platform/x86: hp-wmi: fix u8 underflow in gpu_delta calculation Emre Cecanpunar
2026-04-07  8:02   ` Ilpo Järvinen
2026-04-05 14:45 ` [PATCH v3 5/5] platform/x86: hp-wmi: add locking for concurrent hwmon access Emre Cecanpunar
2026-04-07  8:07   ` Ilpo Järvinen [this message]

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=7809193b-d125-d57f-4875-ce1bc7cbc52f@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=edip@medip.dev \
    --cc=emreleno@gmail.com \
    --cc=hansg@kernel.org \
    --cc=krishna.chomal108@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=platform-driver-x86@vger.kernel.org \
    /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.