All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Manaf Meethalavalappu Pallikunhi" <manaf.pallikunhi@oss.qualcomm.com>
Cc: linux-hwmon@vger.kernel.org, devicetree@vger.kernel.org,
	mfd@lists.linux.dev, robh@kernel.org, conor+dt@kernel.org,
	lee@kernel.org
Subject: Re: [PATCH v5 02/12] hwmon: Add Qualcomm PMIC BCL driver
Date: Mon, 23 Nov 2026 21:11:13 +0000	[thread overview]
Message-ID: <sashiko-outbox-230063@kernel.org> (raw)
In-Reply-To: <20261124-qcom-bcl-hwmon-v5-2-747d5383d538@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] hwmon: qcom-bcl: infinite poll loop in
  bcl_alarm_enable_poll() on reassertion
- [Medium] hwmon: qcom-bcl: max_mu exceeds 6-bit L1/L2 field in
  bcl_thresh_write()
- [Medium] hwmon: qcom-bcl: unlocked enable_irq_wake() race in
  bcl_alarm_irq_init()
- [Low] hwmon: qcom-bcl: 32-bit jiffies wraparound in
  bcl_curr_input_read() cache
- [Low] hwmon: qcom-bcl: redundant private mutex bcl->lock duplicating
  hwmon_lock()
--

commit ad4e95352ce7aa5ad76440eaf33e8bd9c53a0538
Author: Manaf Meethalavalappu Pallikunhi <manaf.pallikunhi@oss.qualcomm.com>

hwmon: Add Qualcomm PMIC BCL driver

This commit adds a hwmon driver for the Qualcomm SPMI PMIC Battery Current
Limiting (BCL) peripheral to monitor battery voltage and current and report
threshold alarms.

> diff --git a/drivers/hwmon/qcom-bcl-hwmon.c b/drivers/hwmon/qcom-bcl-hwmon.c
> new file mode 100644
> index 0000000000000..a5a3a59484571
> --- /dev/null
> +++ b/drivers/hwmon/qcom-bcl-hwmon.c

[ ... ]

> +struct bcl_device {
> +	struct device		*dev;
> +	struct regmap		*regmap;
> +	u32			base;
> +	struct regmap_field	*fields[F_MAX_FIELDS];
> +	struct bcl_alarm_data	bcl_alarms[BCL_LIMIT_ALARM_MAX];
> +	/* Protects hardware register access and device state */
> +	struct mutex		lock;

[Severity: Low]
This isn't a bug, but the hwmon core already serializes .read and .write
callbacks under the hwmon device lock, and regmap serializes individual
register accesses.  Could this use hwmon_lock() and hwmon_unlock() instead
of a driver-private mutex?

> +	u32			last_in_input;
> +	s32			last_curr_input;
> +	unsigned long		last_in_updated;
> +	unsigned long		last_curr_updated;

[ ... ]

> +static const struct reg_field bcl_smb2360_reg_fields[] = {
> +	[F_V_MAJOR]	= REG_FIELD(REVISION2, 0, 7),
> +	[F_V_MINOR]	= REG_FIELD(REVISION1, 0, 7),
> +	[F_CTL_EN]	= REG_FIELD(EN_CTL1, 7, 7),
> +	[F_LVL0_ALARM]	= REG_FIELD(STATUS, 0, 0),
> +	[F_LVL1_ALARM]	= REG_FIELD(STATUS, 1, 1),
> +	[F_LVL2_ALARM]	= REG_FIELD(STATUS, 2, 2),
> +	[F_IN_L0_THR]	= REG_FIELD(VADC_L0_THR, 0, 7),
> +	[F_IN_L1_THR]	= REG_FIELD(VCMP_L1_THR, 0, 5),
> +	[F_IN_L2_THR]	= REG_FIELD(VCMP_L2_THR, 0, 5),
> +	[F_IN_INPUT]	= REG_FIELD(VADC_DATA1, 0, 7),
> +	[F_CURR_H0_THR]	= REG_FIELD(IADC_H0_THR, 0, 7),
> +	[F_CURR_H1_THR]	= REG_FIELD(IADC_H1_THR_GEN3, 0, 7),
> +	[F_CURR_INPUT]	= REG_FIELD(IADC_DATA1, 0, 7),
> +};
> +
> +static const struct reg_field bcl_smb2370_reg_fields[] = {
> +	[F_V_MAJOR]	= REG_FIELD(REVISION2, 0, 7),
> +	[F_V_MINOR]	= REG_FIELD(REVISION1, 0, 7),
> +	[F_CTL_EN]	= REG_FIELD(EN_CTL1, 7, 7),
> +	[F_LVL0_ALARM]	= REG_FIELD(STATUS, 0, 0),
> +	[F_LVL1_ALARM]	= REG_FIELD(STATUS, 1, 1),
> +	[F_LVL2_ALARM]	= REG_FIELD(STATUS, 2, 2),
> +	[F_IN_L0_THR]	= REG_FIELD(VADC_L0_THR, 0, 7),
> +	[F_IN_L1_THR]	= REG_FIELD(VCMP_L1_THR, 0, 5),
> +	[F_IN_L2_THR]	= REG_FIELD(VCMP_L2_THR, 0, 5),

[ ... ]

> +/* Register 0x2a50 mapping: 0 -> 2S, 1 -> 3S */
> +static const struct bcl_desc smb2360_data = {
> +	.reg_fields = bcl_smb2360_reg_fields,
> +	.num_reg_fields = F_CURR_INPUT + 1,
> +	.data_field_bits_size = 8,
> +	.thresh_field_bits_size = 8,
> +	.battery_config_field = REG_FIELD(0x2a50, 0, 1),
> +	.features = BCL_IN_MON | BCL_IN_INPUT | BCL_CURR_MON,
> +	.channel_cfg[BCL_BATT_2S][CHANNEL_IN] = {
> +		.base_mu = 4500,
> +		.max_mu = 8400,
> +		.step_mu = 50,
> +		.default_scale_nu = 432918,
> +		.thresh_type = {THRESH_TYPE_ADC, THRESH_TYPE_INDEX, THRESH_TYPE_INDEX},
> +	},
> +	.channel_cfg[BCL_BATT_2S][CHANNEL_CURR] = {
> +		.max_mu = 17575,
> +		.default_scale_nu = 540679,
> +		.thresh_type = {THRESH_TYPE_ADC, THRESH_TYPE_ADC},
> +	},
> +	.channel_cfg[BCL_BATT_3S][CHANNEL_IN] = {
> +		.base_mu = 6750,
> +		.max_mu = 12600,
> +		.step_mu = 75,
> +		.default_scale_nu = 648790,
> +		.thresh_type = {THRESH_TYPE_ADC, THRESH_TYPE_INDEX, THRESH_TYPE_INDEX},
> +	},

[ ... ]

> +static int bcl_thresh_write(struct bcl_device *bcl, long value, enum bcl_limit_alarm lvl,
> +			    enum bcl_channel channel)
> +{
> +	const struct bcl_desc *desc = bcl->desc;
> +	const struct bcl_channel_cfg *cfg = &desc->channel_cfg[bcl->batt_config][channel];
> +	enum bcl_fields base_field = channel == CHANNEL_IN ? F_IN_L0_THR : F_CURR_H0_THR;
> +	struct regmap_field *field;
> +	long thresh = clamp_val(value, cfg->base_mu, cfg->max_mu);
> +	u32 raw_val;
> +	int ret;
> +
> +	if (WARN_ON(channel == CHANNEL_CURR && lvl >= BCL_LIMIT_ALARM_LVL2))
> +		return -EINVAL;
> +
> +	field = bcl->fields[base_field + lvl];
> +
> +	if (cfg->thresh_type[lvl] == THRESH_TYPE_ADC)
> +		raw_val = bcl_convert_milliunit_to_raw(bcl, thresh, channel,
> +						       desc->thresh_field_bits_size);
> +	else
> +		raw_val = bcl_convert_milliunit_to_index(bcl, thresh, channel);
> +
> +	ret = bcl_validate_threshold_fits(bcl, &desc->reg_fields[base_field + lvl],
> +					  raw_val, thresh, channel == CHANNEL_IN ? "mV" : "mA");
> +	if (ret)
> +		return ret;

[Severity: Medium]
As reported by Daniel Lezcano on v4 [1], can bcl_thresh_write() reject
valid clamped thresholds on SMB2360 and SMB2370?

For both chips, F_IN_L1_THR and F_IN_L2_THR are 6-bit fields (max index
63, which corresponds to 7650 mV on 2S and 11475 mV on 3S), whereas
channel_cfg sets max_mu to 8400 mV on 2S and 12600 mV on 3S (index 78).
Writing a value >= 7675 mV on 2S or >= 11513 mV on 3S to in0_lcrit or
in0_lemergency clamps to at most index 78, causing
bcl_validate_threshold_fits() to log a dev_err() and fail with -EINVAL
instead of clamping to the hardware limit.

[ ... ]

> +static int bcl_curr_input_read(struct bcl_device *bcl, long *out)
> +{
> +	int ret;
> +	u32 raw_val = 0;
> +	s32 signed_val;
> +	const struct bcl_desc *desc = bcl->desc;
> +
> +	/* Return cached value if read too soon after last update */
> +	if (bcl->last_curr_updated &&
> +	    time_before(jiffies, bcl->last_curr_updated + HZ)) {
> +		*out = bcl->last_curr_input;
> +		return 0;
> +	}

[Severity: Low]
On 32-bit platforms, can time_before(jiffies, bcl->last_curr_updated + HZ)
wrap around if curr1_input (or in0_input in bcl_in_input_read()) is not
read for more than LONG_MAX jiffies (~24.8 days at HZ=1000)?  During the
following ~24.8 days, time_before() evaluates to true and returns the
stale cached reading without updating last_curr_updated.  Using u64
timestamps with get_jiffies_64() and time_before64() would avoid the wrap.

[ ... ]

> +static void bcl_alarm_enable_poll(struct work_struct *work)
> +{
> +	struct bcl_alarm_data *alarm = container_of(work, struct bcl_alarm_data,
> +							 alarm_poll_work.work);
> +	struct bcl_device *bcl = (struct bcl_device *)alarm->device;
> +	long status;
> +	int ret;
> +	bool notify = false;
> +
> +	scoped_guard(mutex, &bcl->lock)
> +		ret = bcl_read_alarm_status(bcl, alarm->type, &status);
> +
> +	/* If read failed or shutting down, reschedule */
> +	if (ret || READ_ONCE(alarm->shutting_down)) {
> +		if (!READ_ONCE(alarm->shutting_down)) {
> +			dev_err_ratelimited(bcl->dev,
> +					    "Failed to read alarm %d status: %d\n",
> +					    alarm->type, ret);
> +			schedule_delayed_work(&alarm->alarm_poll_work,
> +					      msecs_to_jiffies(BCL_ALARM_POLLING_MS));
> +		}
> +		return;
> +	}
> +
> +	/* Check if alarm cleared and IRQ needs re-enabling */
> +	scoped_guard(mutex, &alarm->a_lock) {
> +		if (alarm->shutting_down)
> +			return;
> +
> +		if (!status && !alarm->irq_enabled) {
> +			alarm->irq_enabled = true;
> +			enable_irq(alarm->irq);
> +
> +			if (!alarm->irq_wake_enabled && !enable_irq_wake(alarm->irq))
> +				alarm->irq_wake_enabled = true;
> +
> +			notify = true;
> +		}
> +	}
> +
> +	if (notify) {
> +		/* Reassertion racing enable_irq() is dropped by the PMIC on unmask; re-check. */
> +		scoped_guard(mutex, &bcl->lock)
> +			ret = bcl_read_alarm_status(bcl, alarm->type, &status);
> +
> +		if (ret || status) {
> +			if (!READ_ONCE(alarm->shutting_down))
> +				schedule_delayed_work(&alarm->alarm_poll_work,
> +						      msecs_to_jiffies(BCL_ALARM_POLLING_MS));
> +			return;
> +		}
> +
> +		/* Notify outside a_lock: sysfs_notify()/kobject_uevent_env() may sleep */
> +		bcl_hwmon_notify_event(bcl, alarm->type);
> +		return;
> +	}
> +
> +	/* Alarm still active, reschedule polling */
> +	if (!READ_ONCE(alarm->shutting_down))
> +		schedule_delayed_work(&alarm->alarm_poll_work,
> +				      msecs_to_jiffies(BCL_ALARM_POLLING_MS));
> +}

[Severity: Medium]
Does bcl_alarm_enable_poll() enter an infinite 50 ms polling loop if the
post-unmask re-check sees ret != 0 or status != 0?

When the first read observes status == 0, bcl_alarm_enable_poll() sets
alarm->irq_enabled = true, enables the IRQ, and sets notify = true.  If
the alarm reasserted before enable_irq() unmasked the line, the PMIC drops
that edge (so bcl_handle_alarm() will not run to clear
alarm->irq_enabled), and the re-check under if (notify) reschedules
alarm_poll_work and returns with alarm->irq_enabled still true.

On all later runs once status clears back to 0, (!status &&
!alarm->irq_enabled) is false, so notify stays false: the clear
notification is never sent and the function falls through to reschedule
alarm_poll_work every 50 ms forever.

[ ... ]

> +static int bcl_alarm_irq_init(struct platform_device *pdev,
> +			      struct bcl_device *bcl)
> +{
> +	int ret, irq_num, i;
> +	struct bcl_alarm_data *alarm;
> +
> +	for (i = 0; i < ARRAY_SIZE(bcl->bcl_alarms); i++) {
> +		alarm = &bcl->bcl_alarms[i];
> +		alarm->type = i;
> +		alarm->device = bcl;
> +		ret = devm_mutex_init(bcl->dev, &alarm->a_lock);
> +		if (ret)
> +			return ret;
> +
> +		/* Initialize work before IRQ request */
> +		INIT_DELAYED_WORK(&alarm->alarm_poll_work, bcl_alarm_enable_poll);
> +
> +		irq_num = platform_get_irq_byname(pdev, bcl_lvl_info[i].irq_name);
> +		if (irq_num < 0)
> +			return irq_num;
> +
> +		alarm->irq = irq_num;
> +		alarm->irq_enabled = true;
> +
> +		ret = devm_request_threaded_irq(&pdev->dev, irq_num, NULL,
> +						bcl_handle_alarm, IRQF_ONESHOT,
> +						bcl_lvl_info[i].irq_name, alarm);
> +		if (ret) {
> +			alarm->irq_enabled = false;
> +			return ret;
> +		}
> +
> +		/*
> +		 * Register the cleanup action right after the IRQ is
> +		 * requested, before any other fallible step in this loop, so
> +		 * a delayed work scheduled by the IRQ handler is guaranteed
> +		 * to be canceled on teardown. Keeping it after the IRQ
> +		 * request also preserves safe devm LIFO teardown order.
> +		 */
> +		ret = devm_add_action_or_reset(&pdev->dev, bcl_alarm_cleanup_action,
> +					       alarm);
> +		if (ret)
> +			return ret;
> +
> +		if (!enable_irq_wake(irq_num))
> +			alarm->irq_wake_enabled = true;
> +	}

[Severity: Medium]
Can enable_irq_wake() in bcl_alarm_irq_init() race with bcl_handle_alarm()
once devm_request_threaded_irq() activates the interrupt?

CPU0                              CPU1
bcl_alarm_irq_init()
  devm_request_threaded_irq()
                                  bcl_handle_alarm()
                                    mutex_lock(&alarm->a_lock)
                                    alarm->irq_enabled = false
                                    disable_irq_nosync(alarm->irq)
                                    /* irq_wake_enabled is still false */
                                    mutex_unlock(&alarm->a_lock)
  enable_irq_wake(irq_num)
  alarm->irq_wake_enabled = true  <-- wake enabled while IRQ is disabled

If bcl_handle_alarm() also runs between enable_irq_wake() and
alarm->irq_wake_enabled = true (or after bcl_alarm_enable_poll() has
already enabled wake), enable_irq_wake() can be called twice while
irq_wake_enabled only tracks a single boolean, leaving wake_depth
unbalanced on teardown.  Acquiring alarm->a_lock and checking
alarm->irq_enabled && !alarm->irq_wake_enabled before calling
enable_irq_wake() would prevent this.

[1] https://lore.kernel.org/r/5598422c-8830-4fcf-be2b-e4486ea64539@oss.qualcomm.com

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124-qcom-bcl-hwmon-v5-0-747d5383d538@oss.qualcomm.com?part=2

  reply	other threads:[~2026-11-23 21:11 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 20:54 [PATCH v5 00/12] hwmon: Add Qualcomm SPMI BCL driver Manaf Meethalavalappu Pallikunhi
2026-11-23 20:54 ` [PATCH v5 01/12] dt-bindings: hwmon: Describe Qualcomm BCL hardware Manaf Meethalavalappu Pallikunhi
2026-11-23 20:54 ` [PATCH v5 02/12] hwmon: Add Qualcomm PMIC BCL driver Manaf Meethalavalappu Pallikunhi
2026-11-23 21:11   ` sashiko-bot [this message]
2026-11-24 15:07     ` Guenter Roeck
2026-11-24 13:15   ` Daniel Lezcano
2026-11-23 20:54 ` [PATCH v5 03/12] arm64: dts: qcom: hamoa-pmic: Enable BCL sensor node Manaf Meethalavalappu Pallikunhi
2026-11-23 20:54 ` [PATCH v5 04/12] arm64: dts: qcom: pm7250b: " Manaf Meethalavalappu Pallikunhi
2026-11-23 20:54 ` [PATCH v5 05/12] arm64: dts: qcom: pm7550-eliza: " Manaf Meethalavalappu Pallikunhi
2026-11-23 20:54 ` [PATCH v5 06/12] arm64: dts: qcom: pm7550ba-eliza: " Manaf Meethalavalappu Pallikunhi
2026-11-23 20:54 ` [PATCH v5 07/12] arm64: dts: qcom: pm8350c: " Manaf Meethalavalappu Pallikunhi
2026-11-23 20:54 ` [PATCH v5 08/12] arm64: dts: qcom: pm8550: " Manaf Meethalavalappu Pallikunhi
2026-11-23 20:54 ` [PATCH v5 09/12] arm64: dts: qcom: pmh0101: " Manaf Meethalavalappu Pallikunhi
2026-11-23 20:54 ` [PATCH v5 10/12] arm64: dts: qcom: pmih0108-kaanapali: " Manaf Meethalavalappu Pallikunhi
2026-11-23 20:54 ` [PATCH v5 11/12] arm64: dts: qcom: pmih0108: " Manaf Meethalavalappu Pallikunhi
2026-11-23 20:54 ` [PATCH v5 12/12] arm64: dts: qcom: smb2370: " Manaf Meethalavalappu Pallikunhi
2026-11-23 21:11 ` [PATCH v5 00/12] hwmon: Add Qualcomm SPMI BCL driver sashiko-bot

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=sashiko-outbox-230063@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=lee@kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=manaf.pallikunhi@oss.qualcomm.com \
    --cc=mfd@lists.linux.dev \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.