Skip to contentSkip to conversation

hwmon: Serialize accesses in hwmon core

PATCH New Guenter Roeck 1 participant 1 message Sep 7, 2025

Subsystems (2)
lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 1/1 hwmon: Serialize accesses in hwmon core

Implement locking in the hardware monitoring core for drivers using the _with_info() API functions.

Most hardware monitoring drivers need to support locking to protect against parallel accesses from userspace. With older API functions, such locking had to be implemented in the driver code since sysfs attributes were created by the driver. However, the _with_info() API creates sysfs attributes in the hardware monitoring core. This makes it easy to move the locking primitives into that code. This has the benefit of simplifying driver code while at the same time reducing the risk of incomplete of bad locking implementations in hardware monitoring drivers.

While this means that all accesses are forced to be synchronized, this has little if any practical impact since accesses are expected to be low frequency and are typically synchronized from userspace anyway since only a single process is accessing the data. On top of that, many drivers use regmap, which also has its own locking scheme and already serializes accesses.

---
 Documentation/hwmon/hwmon-kernel-api.rst | 10 ++++++
 drivers/hwmon/hwmon.c                    | 42 ++++++++++++++++++++----
 include/linux/hwmon.h                    |  3 ++
 3 files changed, 48 insertions(+), 7 deletions(-)
Documentation/hwmon/hwmon-kernel-api.rst +10 -0
index e47fc757e63e..de74bdb4f5fc 100644
@@ -42,6 +42,9 @@ register/unregister functions::
 
   char *devm_hwmon_sanitize_name(struct device *dev, const char *name);
 
+  void hwmon_lock(struct device *dev);
+  void hwmon_unlock(struct device *dev);
+
 hwmon_device_register_with_info registers a hardware monitoring device.
 It creates the standard sysfs attributes in the hardware monitoring core,
 letting the driver focus on reading from and writing to the chip instead
@@ -79,6 +82,13 @@ devm_hwmon_sanitize_name is the resource managed version of
 hwmon_sanitize_name; the memory will be freed automatically on device
 removal.
 
+When using [devm_]hwmon_device_register_with_info() to register the
+ardware monitoring device, accesses using the associated access functions
+are serialised by the hardware monitoring core. If a driver needs locking
+for other functions such as interrupt handlers or for attributes which are
+fully implemented in the driver, hwmon_lock() and hwmon_unlock() can be used
+to ensure that those functions are serialized.
+
 Using devm_hwmon_device_register_with_info()
 --------------------------------------------
 
drivers/hwmon/hwmon.c +35 -7

135 lines not on this page · raw message

include/linux/hwmon.h +3 -0
index 3a63dff62d03..8d7a85868d93 100644
@@ -491,6 +491,9 @@ int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
 char *hwmon_sanitize_name(const char *name);
 char *devm_hwmon_sanitize_name(struct device *dev, const char *name);
 
+void hwmon_lock(struct device *dev);
+void hwmon_unlock(struct device *dev);
+
 /**
  * hwmon_is_bad_char - Is the char invalid in a hwmon name
  * @ch: the char to be considered
Signature · 1 line
2.45.2