From: Guenter Roeck <linux@roeck-us.net>
To: Nicolin Chen <nicoleotsuka@gmail.com>,
jdelvare@suse.com, robh+dt@kernel.org, mark.rutland@arm.com,
corbet@lwn.net
Cc: afd@ti.com, linux-hwmon@vger.kernel.org,
devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-doc@vger.kernel.org
Subject: Re: [PATCH v8 2/2] hwmon: ina3221: Read channel input source info from DT
Date: Sun, 30 Sep 2018 13:55:18 -0700 [thread overview]
Message-ID: <ab3a36dc-fa5a-4669-2f99-aa55a4083db2@roeck-us.net> (raw)
In-Reply-To: <20180929013937.29289-3-nicoleotsuka@gmail.com>
Hi Nicolin,
On 09/28/2018 06:39 PM, Nicolin Chen wrote:
> An ina3221 chip has three input ports. Each port is used
> to measure the voltage and current of its input source.
>
> The DT binding now has defined bindings for their input
> sources, so the driver should read these information and
> handle accordingly.
>
> This patch adds a new structure of input source specific
> information including input source label, shunt resistor
> value and its connection status. It exposes these labels
> via in[123]_label sysfs nodes upon available, and also
> disables those channels where there are no input source
> being connected. Meanwhile, it also adds in[123]_enable
> sysfs nodes for users to get control of three channels,
> and returns -ENODATA code for any sensor read according
> to hwmon ABI.
>
> Signed-off-by: Nicolin Chen <nicoleotsuka@gmail.com>
[ ... ]
> +
> +static ssize_t ina3221_set_enable(struct device *dev,
> + struct device_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
> + struct ina3221_data *ina = dev_get_drvdata(dev);
> + unsigned int channel = sd_attr->index;
> + u16 config = ina->reg_config;
> + bool enable;
> + int ret;
> +
> + ret = kstrtobool(buf, &enable);
> + if (ret)
> + return ret;
> +
> + if (enable)
> + config |= INA3221_CONFIG_CHx_EN(channel);
> + else
> + config &= ~INA3221_CONFIG_CHx_EN(channel);
> +
> + ret = regmap_write(ina->regmap, INA3221_CONFIG, config);
> + if (ret)
> + return ret;
> +
> + ina->reg_config = config;
> +
Sorry it too me so long to realize this: The code above is racy.
There could be multiple threads enabling and disabling a channel.
Without a mutex, there is no guarantee that the final value of
reg_config matches the value written into the chip.
You'll have to introduce a mutex and implement something like
...
ret = kstrtobool(buf, &enable);
if (ret)
return ret;
mutex_lock(&ina->update_lock);
config = ina->reg_config;
...
ret = regmap_write(ina->regmap, INA3221_CONFIG, config);
if (ret) {
count = ret;
goto error;
}
ina->reg_config = config;
error:
mutex_unlock(&ina->update_lock);
return count;
Guenter
next prev parent reply other threads:[~2018-09-30 20:55 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-29 1:39 [PATCH v8 0/2] Add an initial DT binding doc for ina3221 Nicolin Chen
2018-09-29 1:39 ` [PATCH v8 1/2] dt-bindings: hwmon: Add ina3221 documentation Nicolin Chen
2018-09-29 1:39 ` [PATCH v8 2/2] hwmon: ina3221: Read channel input source info from DT Nicolin Chen
2018-09-30 20:55 ` Guenter Roeck [this message]
2018-10-01 3:28 ` Nicolin Chen
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=ab3a36dc-fa5a-4669-2f99-aa55a4083db2@roeck-us.net \
--to=linux@roeck-us.net \
--cc=afd@ti.com \
--cc=corbet@lwn.net \
--cc=devicetree@vger.kernel.org \
--cc=jdelvare@suse.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=nicoleotsuka@gmail.com \
--cc=robh+dt@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;
as well as URLs for NNTP newsgroup(s).