From: Guenter Roeck <linux@roeck-us.net>
To: Gui-Dong Han <hanguidong02@gmail.com>
Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org,
baijiaju1990@gmail.com
Subject: Re: [PATCH] hwmon: submitting-patches: Explain race conditions caused by calculations in macros
Date: Thu, 4 Dec 2025 20:05:26 -0800 [thread overview]
Message-ID: <7d00f67c-298f-4fbf-9b5d-986ac4fa6d77@roeck-us.net> (raw)
In-Reply-To: <20251202175536.12774-1-hanguidong02@gmail.com>
On Wed, Dec 03, 2025 at 01:55:36AM +0800, Gui-Dong Han wrote:
> 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>
Applied.
Side note: this is good enough for me. I'll be happy to accept a
separate patch with a more detailed explanation.
Thanks,
Guenter
> ---
> Documentation/hwmon/submitting-patches.rst | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/Documentation/hwmon/submitting-patches.rst b/Documentation/hwmon/submitting-patches.rst
> index 6482c4f137dc..7f7095951750 100644
> --- a/Documentation/hwmon/submitting-patches.rst
> +++ b/Documentation/hwmon/submitting-patches.rst
> @@ -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
prev parent reply other threads:[~2025-12-05 4:05 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-02 17:55 Gui-Dong Han
2025-12-02 19:43 ` David Laight
2025-12-05 4:05 ` Guenter Roeck [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=7d00f67c-298f-4fbf-9b5d-986ac4fa6d77@roeck-us.net \
--to=linux@roeck-us.net \
--cc=baijiaju1990@gmail.com \
--cc=hanguidong02@gmail.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®