From nobody Mon Jun 8 19:54:09 2026 Received: from out162-62-57-64.mail.qq.com (out162-62-57-64.mail.qq.com [162.62.57.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BD3C346AC3; Wed, 27 May 2026 02:23:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.62.57.64 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779848635; cv=none; b=VVRNpSAlnzTTVM26wiMx0Aixrnw+LJNZqfVQFMGVFqSkVbvswk5vOmzbLbDw01mt1ucAp+DX5yB7PkB7Dtb7kKHjuuOkRZfqs7zDi2J6lWxVgF+3zfdg0KylvJGxr23EKj0f3BAklPuK1opFgAv1j7A0Klsp6jADIR0vHtVd46E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779848635; c=relaxed/simple; bh=DrCc3UoiDLT5DOG1ABYX25zy5ZWwzI8aTjH3XApC8lg=; h=Message-ID:From:To:Cc:Subject:Date:MIME-Version; b=NWeQfiGsFZ1DpMJAeFZf5qa+4S9as88AM8x1s0MgqFIqmbvOj74zDh34mkLEt/3XkPhJr1LJYChN98SwbikoCizBfZPsKKT7/6MgTqgFzCqnyI3SP3Ai9cI+Si0YYhd5lLG3n5UxlU8F5yKpLsVt79Kpzy8+/AKCmpO1UT6IeKc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=qq.com; spf=pass smtp.mailfrom=qq.com; dkim=pass (1024-bit key) header.d=qq.com header.i=@qq.com header.b=VQzsoYO7; arc=none smtp.client-ip=162.62.57.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=qq.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=qq.com header.i=@qq.com header.b="VQzsoYO7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1779848628; bh=VQCNA6/BlPPG6PdcMmXX/pBbFD+2uu5dKEqOPB5RDlg=; h=From:To:Cc:Subject:Date; b=VQzsoYO7emM8GqaCJvWHni11/d21xYta4Y6TVmRK0RAK8G1aBQtTZqFrnlQlsBtwr 7flDvAzEesnt4zKlEafdnQQS5WFX1Kg5i5FOjOiVUFWgwwwhqDiRlTDBHxQKTlNUbd YYRn3+BIY8/qH+qFZe3dMh++7R1BUvSugE1ewkpk= Received: from NTT-kernel-dev ([60.247.85.88]) by newxmesmtplogicsvrszb51-0.qq.com (NewEsmtp) with SMTP id 59F0C03B; Wed, 27 May 2026 10:22:31 +0800 X-QQ-mid: xmsmtpt1779848551t8p8g3ixw Message-ID: X-QQ-XMAILINFO: OATpkVjS499uU1pcYb472MMx8Bay9mHHtiJGHvs4zW/Ksa4WhBFfdDYhsyDkM9 x9QKXHya+QkKgs1A7NjQ3hcKCWzNzVRGQmsZs3uyWPUj4yiKvRB8mO/vH+WZ4tyI1lvoD9b1jlq2 ekAVbwDKb3ph3haEJss2LIbat09l67APDa/O5YXAVyB8XXi2javQQe7QaJNaXS4pr8PXjP1QvMjF LPe6zzVyelSOuKFcGAKSNcb1tx7OVxAfFqVoG/1wAOOWhnhT8UdJ6BUxg13j9eGE+Zpw01wleA0u G3arOZf4HiF5WSi0A4R7lpHg4jhCfMbgoKZc1riDzsY9rerfZ6HbCpS5xXaklobDSHjeoOfLNK3Z y3NIOQdAZLk0eKwyX4zH6nEbx86WGBXdKqqkiyRF4phT+bpmN1hc6LNbcBYsRfs6WGbMVn+fyVoe NMGxqgOkkG9logVEnkla2JYtA8PDaXWmfVM8MidAvaVohp2qd3I9W8n1k/kcvYrXFpYJTMzpgV3P 65TtvEsHGx2B1NpIPk6FCkLbBrIL7K8acFJ4FPQ+guqP6G6+iElAjBEt5IGoyKO3qQu1vv8MHIIu CY1KxlZfd77aQfTYOBtI7bxF8+MD3Aiyux0sxoagOh+xsvlzHWXK4F/MwaWiyLHYI53Vu7uxB0m+ PdJhYgJyqC9+pWFJ+EQ1+GbxMHtSFeZzVa5Z3G/0coMWstGPm4gyk8NDnUN/U7BSOnhgE4mndfzG FRRJbCzjLoao6zYlND/zNmySgT7Yl4dSREJ6n3s/miUI+0AqJu7UFOuGu1HTr4cqCEbg5iNvCvTN t87A6dHI1iyaMbpr3E+RwcqaC+YtJKAmOULz9Hmz1y8uQFt+pD38Vz5OBn0CgnPZkDNGAxsWI6wA 4toDhezZiaUQdWUr4ILetJlAgKszBTPKxtssqz8VnqshEHtzAUj3btWgzyDBQ9N6DUmty8eJowX6 5Mz9zFdabpMBHnuQCxWG6pyQ8vzr5UpXQGmlHf9VK9bgWB+1UdqqIyyv47HSF0t3fv6itT2/TvFg ZJDwE8pzvJ3DpcR6l6ZkHsPH0iDHnlBXpQ17OgDMRX1cVvV6GVpy159i9NbnE= X-QQ-XMRINFO: MPJ6Tf5t3I/ylTmHUqvI8+Wpn+Gzalws3A== From: Fang Wang <32840572@qq.com> To: gregkh@linuxfoundation.org, stable@vger.kernel.org Cc: patches@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 6.12.y] hwmon: (pmbus/core) Protect regulator operations with mutex Date: Wed, 27 May 2026 10:22:30 +0800 X-OQ-MSGID: <20260527022230.3212045-1-32840572@qq.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Guenter Roeck [ Upstream commit 754bd2b4a084b90b5e7b630e1f423061a9b9b761 ] The regulator operations pmbus_regulator_get_voltage(), pmbus_regulator_set_voltage(), and pmbus_regulator_list_voltage() access PMBus registers and shared data but were not protected by the update_lock mutex. This could lead to race conditions. However, adding mutex protection directly to these functions causes a deadlock because pmbus_regulator_notify() (which calls regulator_notifier_call_chain()) is often called with the mutex already held (e.g., from pmbus_fault_handler()). If a regulator callback then calls one of the now-protected voltage functions, it will attempt to acquire the same mutex. Rework pmbus_regulator_notify() to utilize a worker function to send notifications outside of the mutex protection. Events are stored as atomics in a per-page bitmask and processed by the worker. Initialize the worker and its associated data during regulator registration, and ensure it is cancelled on device removal using devm_add_action_or_reset(). While at it, remove the unnecessary include of linux/of.h. Cc: Sanman Pradhan Fixes: ddbb4db4ced1b ("hwmon: (pmbus) Add regulator support") Reviewed-by: Sanman Pradhan Signed-off-by: Guenter Roeck Signed-off-by: Fang Wang <32840572@qq.com> --- drivers/hwmon/pmbus/pmbus_core.c | 117 ++++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 26 deletions(-) diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_c= ore.c index 41c66ece5177..e37fd206510a 100644 --- a/drivers/hwmon/pmbus/pmbus_core.c +++ b/drivers/hwmon/pmbus/pmbus_core.c @@ -6,6 +6,7 @@ * Copyright (c) 2012 Guenter Roeck */ =20 +#include #include #include #include @@ -20,8 +21,8 @@ #include #include #include -#include #include +#include #include "pmbus.h" =20 /* @@ -102,6 +103,11 @@ struct pmbus_data { =20 struct mutex update_lock; =20 +#if IS_ENABLED(CONFIG_REGULATOR) + atomic_t regulator_events[PMBUS_PAGES]; + struct work_struct regulator_notify_work; +#endif + bool has_status_word; /* device uses STATUS_WORD register */ int (*read_status)(struct i2c_client *client, int page); =20 @@ -3181,12 +3187,19 @@ static int pmbus_regulator_get_voltage(struct regul= ator_dev *rdev) .class =3D PSC_VOLTAGE_OUT, .convert =3D true, }; + int ret; =20 + mutex_lock(&data->update_lock); s.data =3D _pmbus_read_word_data(client, s.page, 0xff, PMBUS_READ_VOUT); - if (s.data < 0) - return s.data; + if (s.data < 0) { + ret =3D s.data; + goto unlock; + } =20 - return (int)pmbus_reg2data(data, &s) * 1000; /* unit is uV */ + ret =3D (int)pmbus_reg2data(data, &s) * 1000; /* unit is uV */ +unlock: + mutex_unlock(&data->update_lock); + return ret; } =20 static int pmbus_regulator_set_voltage(struct regulator_dev *rdev, int min= _uv, @@ -3203,16 +3216,22 @@ static int pmbus_regulator_set_voltage(struct regul= ator_dev *rdev, int min_uv, }; int val =3D DIV_ROUND_CLOSEST(min_uv, 1000); /* convert to mV */ int low, high; + int ret; =20 *selector =3D 0; =20 + mutex_lock(&data->update_lock); low =3D pmbus_regulator_get_low_margin(client, s.page); - if (low < 0) - return low; + if (low < 0) { + ret =3D low; + goto unlock; + } =20 high =3D pmbus_regulator_get_high_margin(client, s.page); - if (high < 0) - return high; + if (high < 0) { + ret =3D high; + goto unlock; + } =20 /* Make sure we are within margins */ if (low > val) @@ -3222,7 +3241,10 @@ static int pmbus_regulator_set_voltage(struct regula= tor_dev *rdev, int min_uv, =20 val =3D pmbus_data2reg(data, &s, val); =20 - return _pmbus_write_word_data(client, s.page, PMBUS_VOUT_COMMAND, (u16)va= l); + ret =3D _pmbus_write_word_data(client, s.page, PMBUS_VOUT_COMMAND, (u16)v= al); +unlock: + mutex_unlock(&data->update_lock); + return ret; } =20 static int pmbus_regulator_list_voltage(struct regulator_dev *rdev, @@ -3230,7 +3252,9 @@ static int pmbus_regulator_list_voltage(struct regula= tor_dev *rdev, { struct device *dev =3D rdev_get_dev(rdev); struct i2c_client *client =3D to_i2c_client(dev->parent); + struct pmbus_data *data =3D i2c_get_clientdata(client); int val, low, high; + int ret; =20 if (selector >=3D rdev->desc->n_voltages || selector < rdev->desc->linear_min_sel) @@ -3240,18 +3264,29 @@ static int pmbus_regulator_list_voltage(struct regu= lator_dev *rdev, val =3D DIV_ROUND_CLOSEST(rdev->desc->min_uV + (rdev->desc->uV_step * selector), 1000); /* convert to mV */ =20 + mutex_lock(&data->update_lock); + low =3D pmbus_regulator_get_low_margin(client, rdev_get_id(rdev)); - if (low < 0) - return low; + if (low < 0) { + ret =3D low; + goto unlock; + } =20 high =3D pmbus_regulator_get_high_margin(client, rdev_get_id(rdev)); - if (high < 0) - return high; + if (high < 0) { + ret =3D high; + goto unlock; + } =20 - if (val >=3D low && val <=3D high) - return val * 1000; /* unit is uV */ + if (val >=3D low && val <=3D high) { + ret =3D val * 1000; /* unit is uV */ + goto unlock; + } =20 - return 0; + ret =3D 0; +unlock: + mutex_unlock(&data->update_lock); + return ret; } =20 const struct regulator_ops pmbus_regulator_ops =3D { @@ -3266,12 +3301,42 @@ const struct regulator_ops pmbus_regulator_ops =3D { }; EXPORT_SYMBOL_NS_GPL(pmbus_regulator_ops, PMBUS); =20 +static void pmbus_regulator_notify_work_cancel(void *data) +{ + struct pmbus_data *pdata =3D data; + + cancel_work_sync(&pdata->regulator_notify_work); +} + +static void pmbus_regulator_notify_worker(struct work_struct *work) +{ + struct pmbus_data *data =3D + container_of(work, struct pmbus_data, regulator_notify_work); + int i, j; + + for (i =3D 0; i < data->info->pages; i++) { + int event; + + event =3D atomic_xchg(&data->regulator_events[i], 0); + if (!event) + continue; + + for (j =3D 0; j < data->info->num_regulators; j++) { + if (i =3D=3D rdev_get_id(data->rdevs[j])) { + regulator_notifier_call_chain(data->rdevs[j], + event, NULL); + break; + } + } + } +} + static int pmbus_regulator_register(struct pmbus_data *data) { struct device *dev =3D data->dev; const struct pmbus_driver_info *info =3D data->info; const struct pmbus_platform_data *pdata =3D dev_get_platdata(dev); - int i; + int i, ret; =20 data->rdevs =3D devm_kzalloc(dev, sizeof(struct regulator_dev *) * info->= num_regulators, GFP_KERNEL); @@ -3295,20 +3360,20 @@ static int pmbus_regulator_register(struct pmbus_da= ta *data) info->reg_desc[i].name); } =20 + INIT_WORK(&data->regulator_notify_work, pmbus_regulator_notify_worker); + + ret =3D devm_add_action_or_reset(dev, pmbus_regulator_notify_work_cancel,= data); + if (ret) + return ret; + return 0; } =20 static int pmbus_regulator_notify(struct pmbus_data *data, int page, int e= vent) { - int j; - - for (j =3D 0; j < data->info->num_regulators; j++) { - if (page =3D=3D rdev_get_id(data->rdevs[j])) { - regulator_notifier_call_chain(data->rdevs[j], event, NULL); - break; - } - } - return 0; + atomic_or(event, &data->regulator_events[page]); + schedule_work(&data->regulator_notify_work); + return 0; } #else static int pmbus_regulator_register(struct pmbus_data *data) --=20 2.34.1