linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Anderson <sean.anderson@linux.dev>
To: Andy Shevchenko <andriy.shevchenko@intel.com>
Cc: "Jonathan Cameron" <jic23@kernel.org>,
	"Jean Delvare" <jdelvare@suse.com>,
	"Guenter Roeck" <linux@roeck-us.net>,
	linux-iio@vger.kernel.org, linux-hwmon@vger.kernel.org,
	"Andy Shevchenko" <andy@kernel.org>,
	"Nuno Sá" <nuno.sa@analog.com>,
	linux-kernel@vger.kernel.org,
	"David Lechner" <dlechner@baylibre.com>
Subject: Re: [PATCH 7/7] hwmon: iio: Add alarm support
Date: Mon, 21 Jul 2025 10:24:13 -0400	[thread overview]
Message-ID: <1ad78d64-ab6a-41fd-b149-dd52ea08e4e3@linux.dev> (raw)
In-Reply-To: <aH3vcye29TrG8s2Z@smile.fi.intel.com>

On 7/21/25 03:42, Andy Shevchenko wrote:
> On Thu, Jul 17, 2025 at 12:23:58PM -0400, Sean Anderson wrote:
>> On 7/16/25 06:08, Andy Shevchenko wrote:
>> > On Tue, Jul 15, 2025 at 12:20:24PM -0400, Sean Anderson wrote:
>> >> On 7/15/25 04:50, Andy Shevchenko wrote:
>> >> > On Mon, Jul 14, 2025 at 09:20:23PM -0400, Sean Anderson wrote:
> 
> ...
> 
>> >> >>  #include <linux/hwmon-sysfs.h>
>> >> > 
>> >> > + blank line here..
>> >> 
>> >> why?
>> > 
>> > To group the subsystem related headers (which are more custom and less generic).
>> > This allows to follow what the subsystems are in use and what APIs / types are
>> > taken.
>> 
>> Then you should send a patch for coding-style.rst.
> 
> Does any of the common sense approach need to be written in the documentation?

Yes! My base assumption would be that includes should be alphabetized,
but that no other ordering or spacing is necessary. Your "common sense"
is not so common.

>> >> >>  #include <linux/iio/consumer.h>
>> >> >> +#include <linux/iio/events.h>
>> >> >> +#include <linux/iio/iio.h>
>> >> >>  #include <linux/iio/types.h>
>> >> > 
>> >> > ...and here?
>> >> 
>> >> OK
>> >> 
>> >> >> +#include <uapi/linux/iio/events.h>
>> > 
>> > As similar here, to visually split uAPI and the rest. This increases
>> > readability and maintenance.
> 
> ...
> 
>> >> >> +static ssize_t iio_hwmon_lookup_alarm(struct iio_hwmon_listener *listener,
>> >> >> +				      u64 id)
>> >> >> +{
>> >> >> +	ssize_t i;
>> >> >> +
>> >> >> +	for (i = 0; i < listener->num_alarms; i++)
>> >> >> +		if (listener->ids[i] == id)
>> >> >> +			return i;
>> >> > 
>> >> >> +	return -1;
>> >> > 
>> >> > -ENOENT ?
>> >> > This will allow to propagate an error code to the upper layer(s).
>> >> 
>> >> I suppose. But I think
>> >> 
>> >> alarm = iio_hwmon_lookup_alarm(...);
>> >> if (alarm < 0)
>> >> 	return -ENOENT;
>> >> 
>> >> is clearer than
>> > 
>> > I disagree. This makes it worth as it shadows other possible code(s), if any,
>> > and makes harder to follow as reader has to check the callee implementation.
>> > 
>> > The shadow error codes need a justification.
>> 
>> OK, I will return a bool next time to avoid any misconceptions that the return
>> code means anything other than "found" or "not found"
> 
> This makes sense. And IIRC it's even documented.
> 
>> >> alarm = iio_hwmon_lookup_alarm(...);
>> >> if (alarm < 0)
>> >> 	return alarm;
>> >> 
>> >> because you don't have to read the definition of iio_hwmon_lookup_alarm
>> >> to determine what the return value is.
>> > 
>> > Exactly my point!
>> 
>> your point is that you want readers to have to read the definition of
>> iio_hwmon_lookup_alarm in order to determine that ENOENT is a possible
>> error from add_alarm_attr? I don't follow.
> 
> No, my point is that readers should not care about error code. If it's
> propagated to the upper layer, the upper layer will decide on how to proceed.
> And -ENOENT is de facto standard for "entity not found".
> 
>> >> >> +}
> 
> ...
> 
>> >> >> +err_alarms:
>> >> >> +	kfree(listener->alarms);
>> >> >> +	kfree(listener->ids);
>> >> >> +err_listener:
>> >> >> +	kfree(listener);
>> >> >> +err_unlock:
>> >> >> +	mutex_unlock(&iio_hwmon_listener_lock);
>> >> >> +	return ERR_PTR(err);
>> >> > 
>> >> > What about using __free()?
>> >> 
>> >> That works for listener, but not for alarms or ids.
>> > 
>> > Why not?
> 
> No answer? Have you checked how cleanup.h suggests to avoid cleaning the memory
> when it's supposed to be used later on?

Sorry, I missed this the first time. Anyway the reason it doesn't work
for alarms/ids is that those are members of listener and not separate
variables. And I think it's more concise this way. Compare the existing

	listener->alarms = bitmap_zalloc(listener->num_alarms, GFP_KERNEL);
	if (!listener->alarms)
		goto err_listener;

	/* ... */
	return listener;

err_alarms:
	kfree(listener->alarms);
	/* ... */

vs

	unsigned long __free alarms = NULL;

	alarms = bitmap_zalloc(listener->num_alarms, GFP_KERNEL);
	if (!alarms)
		return -ENOMEM;
	listener->alarms = alarms;
	
	/* ... */
	no_free_ptr(alarms);
	return listener;

I don't really think there's an advantage.

>> >> >> +static void iio_hwmon_listener_put(void *data)
>> >> >> +{
>> >> >> +	struct iio_hwmon_listener *listener = data;
>> >> >> +
>> >> >> +	scoped_guard(mutex, &iio_hwmon_listener_lock) {
>> >> >> +		if (unlikely(listener->refcnt == UINT_MAX))
>> >> >> +			return;
>> >> >> +
>> >> >> +		if (--listener->refcnt)
>> >> >> +			return;
>> >> > 
>> >> > Can the refcount_t be used with the respective APIs? Or even kref?
>> >> 
>> >> Why? We do all the manipulation under a mutex, so there is no point in
>> >> atomic access. Instead of the games refcnt_t has to play to try and
>> >> prevent overflow we can just check for it directly.
>> > 
>> > refcount_t provides a facility of overflow/underflow.
>> 
>> refcount_t can't prevent underflow because it's atomic. All it can do is
>> warn after the fact. And of course overflow is handled properly here.
>> But it can't occur in practice unless you specifically load multiple
>> devicetrees at runtime. So we don't need it anyway.
> 
> It will warn the user in such cases. Your code won't do it, even if it's not a
> big deal or never happens situation, it's still better to use in-kernel
> standard ways of handling these things.

It's not the standard for refcounts protected by a mutex. There are
literally hundreds of existing examples of this.

--Sean 

  reply	other threads:[~2025-07-21 14:24 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-15  1:20 [PATCH 0/7] hwmon: iio: Add alarm support Sean Anderson
2025-07-15  1:20 ` [PATCH 1/7] math64: Add div64_s64_rem Sean Anderson
2025-07-15  8:03   ` Andy Shevchenko
2025-07-15 17:36     ` Sean Anderson
2025-07-16 10:15       ` Andy Shevchenko
2025-07-15  1:20 ` [PATCH 2/7] iio: inkern: Add API for reading/writing events Sean Anderson
2025-07-15  8:18   ` Andy Shevchenko
2025-07-15 15:42     ` Sean Anderson
2025-07-16  9:28       ` Andy Shevchenko
2025-07-17 16:42         ` Sean Anderson
2025-07-27 15:55           ` Jonathan Cameron
2025-07-15 10:35   ` Nuno Sá
2025-07-15 15:43     ` Sean Anderson
2025-07-16  6:23       ` Nuno Sá
2025-07-27 16:13   ` Jonathan Cameron
2025-07-15  1:20 ` [PATCH 3/7] iio: Add in-kernel API for events Sean Anderson
2025-07-15  8:20   ` Andy Shevchenko
2025-07-15 15:47     ` Sean Anderson
2025-07-16  9:47       ` Andy Shevchenko
2025-07-15 11:09   ` Nuno Sá
2025-07-15 16:52     ` Sean Anderson
2025-07-27 16:21       ` Jonathan Cameron
2025-07-28 22:44         ` Sean Anderson
2025-07-29 18:33           ` Jonathan Cameron
2025-07-29 20:09             ` Sean Anderson
2025-07-31 12:59               ` Jonathan Cameron
2025-07-27 16:24   ` Jonathan Cameron
2025-07-15  1:20 ` [PATCH 4/7] hwmon: iio: Refactor scale calculation into helper Sean Anderson
2025-07-15  8:35   ` Andy Shevchenko
2025-07-15  1:20 ` [PATCH 5/7] hwmon: iio: Add helper function for creating attributes Sean Anderson
2025-07-15  8:38   ` Andy Shevchenko
2025-07-15 15:55     ` Sean Anderson
2025-07-16 10:00       ` Andy Shevchenko
2025-07-27 16:31   ` Jonathan Cameron
2025-07-15  1:20 ` [PATCH 6/7] hwmon: iio: Add min/max support Sean Anderson
2025-07-15  8:41   ` Andy Shevchenko
2025-07-15 16:05     ` Sean Anderson
2025-07-16 10:01       ` Andy Shevchenko
2025-07-17 16:11         ` Sean Anderson
2025-07-27 16:35   ` Jonathan Cameron
2025-07-28 22:32     ` Sean Anderson
2025-07-29 18:37       ` Jonathan Cameron
2025-07-15  1:20 ` [PATCH 7/7] hwmon: iio: Add alarm support Sean Anderson
2025-07-15  8:50   ` Andy Shevchenko
2025-07-15 16:20     ` Sean Anderson
2025-07-16 10:08       ` Andy Shevchenko
2025-07-17 16:23         ` Sean Anderson
2025-07-21  7:42           ` Andy Shevchenko
2025-07-21 14:24             ` Sean Anderson [this message]
2025-07-15 11:28   ` Nuno Sá
2025-07-15 17:02     ` Sean Anderson
2025-07-15 19:26       ` Guenter Roeck
2025-07-15 19:40         ` Sean Anderson
2025-07-16  6:37       ` Nuno Sá
2025-07-17 16:00         ` Sean Anderson
2025-07-31 10:52           ` Nuno Sá
2025-08-02 10:53             ` Jonathan Cameron
2025-07-15 16:13   ` kernel test robot
2025-07-15 19:34   ` Guenter Roeck
2025-07-15 20:08     ` Sean Anderson
2025-07-16  7:44   ` kernel test robot
2025-07-27 16:50   ` Jonathan Cameron

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=1ad78d64-ab6a-41fd-b149-dd52ea08e4e3@linux.dev \
    --to=sean.anderson@linux.dev \
    --cc=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jdelvare@suse.com \
    --cc=jic23@kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=nuno.sa@analog.com \
    /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;
as well as URLs for NNTP newsgroup(s).