]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
hwmon: submitting-patches: Explain race conditions caused by calculations in macros
authorGui-Dong Han <hanguidong02@gmail.com>
Tue, 2 Dec 2025 17:55:36 +0000 (01:55 +0800)
committerGuenter Roeck <linux@roeck-us.net>
Sat, 31 Jan 2026 15:38:30 +0000 (07:38 -0800)
The current documentation advises against calculations in macros
primarily to avoid code obfuscation. It misses the risk of concurrency
issues.

Add a note explaining that macros evaluating arguments multiple times
can lead to race conditions when accessing shared data.

Link: https://lore.kernel.org/all/CALbr=LYJ_ehtp53HXEVkSpYoub+XYSTU8Rg=o1xxMJ8=5z8B-g@mail.gmail.com/
Signed-off-by: Gui-Dong Han <hanguidong02@gmail.com>
Link: https://lore.kernel.org/r/20251202175536.12774-1-hanguidong02@gmail.com
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Documentation/hwmon/submitting-patches.rst

index 6482c4f137dcb4052a4286dc2094c2274f5a89b1..7f7095951750529954b7d09ce46b374822914448 100644 (file)
@@ -82,7 +82,10 @@ increase the chances of your change being accepted.
 * Avoid calculations in macros and macro-generated functions. While such macros
   may save a line or so in the source, it obfuscates the code and makes code
   review more difficult. It may also result in code which is more complicated
-  than necessary. Use inline functions or just regular functions instead.
+  than necessary. Such macros may also evaluate their arguments multiple times.
+  This leads to Time-of-Check to Time-of-Use (TOCTOU) race conditions when
+  accessing shared data without locking, for example when calculating values in
+  sysfs show functions. Use inline functions or just regular functions instead.
 
 * Limit the number of kernel log messages. In general, your driver should not
   generate an error message just because a runtime operation failed. Report