Skip to contentSkip to conversation

power: supply: qcom_battmgr: remove bogus do_div()

PATCH In review Arnd Bergmann 9 participants 10 messages Feb 14, 2023 – Mar 6, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 power: supply: qcom_battmgr: remove bogus do_div()

From: Arnd Bergmann <[email protected]>

The argument to do_div() is a 32-bit integer, and it was read from a 32-bit register so there is no point in doing a 64-bit division on it.

On 32-bit arm, do_div() causes a compile-time warning here:

include/asm-generic/div64.h:238:22: error: passing argument 1 of '__div64_32' from incompatible pointer type [-Werror=incompatible-pointer-types]

  238 |   __rem = __div64_32(&(n), __base); \
      |                      ^~~~
      |                      |
      |                      unsigned int *

drivers/power/supply/qcom_battmgr.c:1130:4: note: in expansion of macro 'do_div'
1130 | do_div(battmgr->status.percent, 100);

---
 drivers/power/supply/qcom_battmgr.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/power/supply/qcom_battmgr.c +1 -2
index ec31f887184f..de77df97b3a4 100644
@@ -1126,8 +1126,7 @@ static void qcom_battmgr_sm8350_callback(struct qcom_battmgr *battmgr,
 			battmgr->info.charge_type = le32_to_cpu(resp->intval.value);
 			break;
 		case BATT_CAPACITY:
-			battmgr->status.percent = le32_to_cpu(resp->intval.value);
-			do_div(battmgr->status.percent, 100);
+			battmgr->status.percent = le32_to_cpu(resp->intval.value) / 100;
 			break;
 		case BATT_VOLT_OCV:
 			battmgr->status.voltage_ocv = le32_to_cpu(resp->intval.value);
Signature · 1 line
2.39.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The argument to do_div() is a 32-bit integer, and it was read from a 32-bit register so there is no point in doing a 64-bit division on it.

On 32-bit arm, do_div() causes a compile-time warning here:

include/asm-generic/div64.h:238:22: error: passing argument 1 of '__div64_32' from incompatible pointer type [-Werror=incompatible-pointer-types]

  238 |   __rem = __div64_32(&(n), __base); \
      |                      ^~~~
      |                      |
      |                      unsigned int *
Quoted 5 lines

drivers/power/supply/qcom_battmgr.c:1130:4: note: in expansion of macro 'do_div'
1130 | do_div(battmgr->status.percent, 100);

---

Konrad

Quoted 17 lines
 drivers/power/supply/qcom_battmgr.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/power/supply/qcom_battmgr.c +1 -2
index ec31f887184f..de77df97b3a4 100644
@@ -1126,8 +1126,7 @@ static void qcom_battmgr_sm8350_callback(struct qcom_battmgr *battmgr,
 			battmgr->info.charge_type = le32_to_cpu(resp->intval.value);
 			break;
 		case BATT_CAPACITY:
-			battmgr->status.percent = le32_to_cpu(resp->intval.value);
-			do_div(battmgr->status.percent, 100);
+			battmgr->status.percent = le32_to_cpu(resp->intval.value) / 100;
 			break;
 		case BATT_VOLT_OCV:
 			battmgr->status.voltage_ocv = le32_to_cpu(resp->intval.value);
#2 lore raw

Hi,

Quoted 16 lines · Konrad Dybcio
Quoted 15 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The argument to do_div() is a 32-bit integer, and it was read from a 32-bit register so there is no point in doing a 64-bit division on it.

On 32-bit arm, do_div() causes a compile-time warning here:

include/asm-generic/div64.h:238:22: error: passing argument 1 of '__div64_32' from incompatible pointer type [-Werror=incompatible-pointer-types]

  238 |   __rem = __div64_32(&(n), __base); \
      |                      ^~~~
      |                      |
      |                      unsigned int *

drivers/power/supply/qcom_battmgr.c:1130:4: note: in expansion of macro 'do_div'
1130 | do_div(battmgr->status.percent, 100);

Quoted 3 lines
Quoted 2 lines

---

Needs to go through the Qualcomm tree:

-- Sebastian

Quoted 17 lines
Quoted 17 lines
 drivers/power/supply/qcom_battmgr.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/power/supply/qcom_battmgr.c +1 -2
index ec31f887184f..de77df97b3a4 100644
@@ -1126,8 +1126,7 @@ static void qcom_battmgr_sm8350_callback(struct qcom_battmgr *battmgr,
 			battmgr->info.charge_type = le32_to_cpu(resp->intval.value);
 			break;
 		case BATT_CAPACITY:
-			battmgr->status.percent = le32_to_cpu(resp->intval.value);
-			do_div(battmgr->status.percent, 100);
+			battmgr->status.percent = le32_to_cpu(resp->intval.value) / 100;
 			break;
 		case BATT_VOLT_OCV:
 			battmgr->status.voltage_ocv = le32_to_cpu(resp->intval.value);
#8 lore raw

"proximity" attribute for input devices

[Public]

Hello,

A problem was found that a Wacom device's stylus battery showed up in GNOME even though the stylus wasn't near by. As part of discussing how to handle it Bastien proposes a new sysfs attribute "proximity" to let userspace know this.

He asked[1] to start the discussion on Linux-input ML.

[1] https://bugzilla.kernel.org/show_bug.cgi?id=217062#c7

Thanks,

Reviewed-by on v1 1/1
#3 lore raw
In reply to Arnd Bergmann
Quoted 8 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The argument to do_div() is a 32-bit integer, and it was read from a 32-bit register so there is no point in doing a 64-bit division on it.

On 32-bit arm, do_div() causes a compile-time warning here:

include/asm-generic/div64.h:238:22: error: passing argument 1 of '__div64_32' from incompatible pointer type [-Werror=incompatible-pointer-types]

Quoted 8 lines
  238 |   __rem = __div64_32(&(n), __base); \
      |                      ^~~~
      |                      |
      |                      unsigned int *

drivers/power/supply/qcom_battmgr.c:1130:4: note: in expansion of macro 'do_div'
1130 | do_div(battmgr->status.percent, 100);

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#4 lore raw
In reply to Arnd Bergmann

Hi Linus,

Quoted 37 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The argument to do_div() is a 32-bit integer, and it was read from a 32-bit register so there is no point in doing a 64-bit division on it.

On 32-bit arm, do_div() causes a compile-time warning here:

include/asm-generic/div64.h:238:22: error: passing argument 1 of '__div64_32' from incompatible pointer type [-Werror=incompatible-pointer-types]

  238 |   __rem = __div64_32(&(n), __base); \
      |                      ^~~~
      |                      |
      |                      unsigned int *

drivers/power/supply/qcom_battmgr.c:1130:4: note: in expansion of macro 'do_div'
1130 | do_div(battmgr->status.percent, 100);

---
 drivers/power/supply/qcom_battmgr.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/power/supply/qcom_battmgr.c +1 -2
index ec31f887184f..de77df97b3a4 100644
@@ -1126,8 +1126,7 @@ static void qcom_battmgr_sm8350_callback(struct qcom_battmgr *battmgr,
 			battmgr->info.charge_type = le32_to_cpu(resp->intval.value);
 			break;
 		case BATT_CAPACITY:
-			battmgr->status.percent = le32_to_cpu(resp->intval.value);
-			do_div(battmgr->status.percent, 100);
+			battmgr->status.percent = le32_to_cpu(resp->intval.value) / 100;
 			break;
 		case BATT_VOLT_OCV:
 			battmgr->status.voltage_ocv = le32_to_cpu(resp->intval.value);
Signature · 1 line
2.39.1

Would you be able to take this patch directly? It seems obviously correctTM, has an ack from Sebastian [1], and without it, 32-bit allmodconfig builds are broken [2] (the other warning in that log has a fix on the way to you soon).

[1]: https://lore.kernel.org/[email protected]/
[2]: https://storage.tuxsuite.com/public/clangbuiltlinux/continuous-integration2/builds/2MPmxwvmQ7FdpiMhdQN2ZJhcoUP/build.log

Cheers,
Nathan

#5 lore raw
Quoted 5 lines · Nathan Chancellor

Would you be able to take this patch directly? It seems obviously correctTM, has an ack from Sebastian [1], and without it, 32-bit allmodconfig builds are broken [2] (the other warning in that log has a fix on the way to you soon).

Ok, I've taken it directly.

However, the whole "seems obviously correct" is true in the sense that it now doesn't complain (and doesn't overwrite an "int" with a 64-bit value.

The actual code still looks odd. Is that return value for BATT_CAPACITY truly in that odd "hundredths of percent" format, where dividing it by 100 gives you whole percent?

Because "hundredths of percent" strikes me as a very odd interface. Even for some firmware interface. I realize that anything is possible with strange firmware, but still...

Linus

#9 lore raw

Re: "proximity" attribute for input devices

In reply to Limonciello, Mario

Apologies for the delay in replying.

First off: as an immediate action, I'm thinking of changing the Wacom driver to do lazy creation of the power_supply device. This would mean that rather than creating it at the same time as the input device, we would wait until we receive some kind of affirmative indication of a battery being present. Most Wacom peripherals report battery status in a "heartbeat" report that is sent every few seconds, so there wouldn't be much change for the typical user (just a few-second delay between connecting the hardware and a power_supply device being created). In the case of the "component" devices that are built into laptops and other computers, however, the battery status is only reported while the pen is actually in proximity. For users like you who don't own (or never use) a pen, this means that our driver would never create a power_supply device -- preventing GNOME from showing a battery that isn't relevant. I'm working on a patch set that does this but need a bit more time to finish it.

That's only a partial solution, however. If a component user were to bring a pen into prox even just briefly, then a power_supply device would be created and not go away until the user reboots. The pen's battery status will become stale at some point in time though -- self-discharge, use of the pen on another device, and even just simple irrelevance if the user hasn't used the pen in a while (do I care that the pen which I left at home is at 50% battery?). I agree that it makes sense for userspace to hide the battery after a while due to things like this. Are there new signals that the kernel should be providing to userspace (e.g. an attribute that indicates if we're in communication with power_supply? an attribute signaling that data might be stale)? Or are the signals that are already provided sufficient (e.g. should GNOME just hide power_supply devices that are in an "Unknown" state? or maybe hide power_supplies which haven't been updated in more than 1 hour?)

I've added the power_supply list and maintainer for some additional viewpoints on the second paragraph... If there are questions about how the Wacom hardware and its battery reporting works, I'm happy to provide answers...

Jason
---
Now instead of four in the eights place /
you’ve got three, ‘Cause you added one /
(That is to say, eight) to the two, /
But you can’t take seven from three, /
So you look at the sixty-fours....

Quoted 13 lines · Mario

[Public]

Hello,

A problem was found that a Wacom device's stylus battery showed up in GNOME even though the stylus wasn't near by. As part of discussing how to handle it Bastien proposes a new sysfs attribute "proximity" to let userspace know this.

He asked[1] to start the discussion on Linux-input ML.

[1] https://bugzilla.kernel.org/show_bug.cgi?id=217062#c7

Thanks,

#6 lore raw
In reply to Linus Torvalds

Hi,

Quoted 18 lines · Jason Gerecke

Apologies for the delay in replying.

First off: as an immediate action, I'm thinking of changing the Wacom driver to do lazy creation of the power_supply device. This would mean that rather than creating it at the same time as the input device, we would wait until we receive some kind of affirmative indication of a battery being present. Most Wacom peripherals report battery status in a "heartbeat" report that is sent every few seconds, so there wouldn't be much change for the typical user (just a few-second delay between connecting the hardware and a power_supply device being created). In the case of the "component" devices that are built into laptops and other computers, however, the battery status is only reported while the pen is actually in proximity. For users like you who don't own (or never use) a pen, this means that our driver would never create a power_supply device -- preventing GNOME from showing a battery that isn't relevant. I'm working on a patch set that does this but need a bit more time to finish it.

Quoted 15 lines

That's only a partial solution, however. If a component user were to bring a pen into prox even just briefly, then a power_supply device would be created and not go away until the user reboots. The pen's battery status will become stale at some point in time though -- self-discharge, use of the pen on another device, and even just simple irrelevance if the user hasn't used the pen in a while (do I care that the pen which I left at home is at 50% battery?). I agree that it makes sense for userspace to hide the battery after a while due to things like this. Are there new signals that the kernel should be providing to userspace (e.g. an attribute that indicates if we're in communication with power_supply? an attribute signaling that data might be stale)? Or are the signals that are already provided sufficient (e.g. should GNOME just hide power_supply devices that are in an "Unknown" state? or maybe hide power_supplies which haven't been updated in more than 1 hour?)

Can't you just unregister the power-supply device if there hasn't been any heartbeat for some minutes and go back to the init state (i.e. the one where you are waiting for a heartbeat to appear for the first time)?

Quoted 4 lines

I've added the power_supply list and maintainer for some additional viewpoints on the second paragraph... If there are questions about how the Wacom hardware and its battery reporting works, I'm happy to provide answers...

I think the problem you have is quite specific to the Wacom hardware. Usually wireless devices are either connected or disconnected and drivers unregister the battery device on disconnect. So I think this logic belongs into Wacom specific code and not in generic power-supply core code.

-- Sebastian

#7 lore raw

RE: "proximity" attribute for input devices

[Public]

Quoted 58 lines

-----Original Message-----
From: Sebastian Reichel <[email protected]>
Sent: Friday, March 3, 2023 19:09
To: Jason Gerecke <[email protected]>
Cc: Limonciello, Mario <[email protected]>; linux- [email protected]; Bastien Nocera <[email protected]>; [email protected]; [email protected]; linux- [email protected]
Subject: Re: "proximity" attribute for input devices

Hi,

Quoted 18 lines · Jason Gerecke

Apologies for the delay in replying.

First off: as an immediate action, I'm thinking of changing the Wacom driver to do lazy creation of the power_supply device. This would mean that rather than creating it at the same time as the input device, we would wait until we receive some kind of affirmative indication of a battery being present. Most Wacom peripherals report battery status in a "heartbeat" report that is sent every few seconds, so there wouldn't be much change for the typical user (just a few-second delay between connecting the hardware and a power_supply device being created). In the case of the "component" devices that are built into laptops and other computers, however, the battery status is only reported while the pen is actually in proximity. For users like you who don't own (or never use) a pen, this means that our driver would never create a power_supply device -- preventing GNOME from showing a battery that isn't relevant. I'm working on a patch set that does this but need a bit more time to finish it.

Quoted 14 lines

That's only a partial solution, however. If a component user were to bring a pen into prox even just briefly, then a power_supply device would be created and not go away until the user reboots. The pen's battery status will become stale at some point in time though -- self-discharge, use of the pen on another device, and even just simple irrelevance if the user hasn't used the pen in a while (do I care that the pen which I left at home is at 50% battery?). I agree that it makes sense for userspace to hide the battery after a while due to things like this. Are there new signals that the kernel should be providing to userspace (e.g. an attribute that indicates if we're in communication with power_supply? an attribute signaling that data might be stale)? Or are the signals that are already provided sufficient (e.g. should GNOME just hide power_supply devices that are in an "Unknown" state? or maybe hide power_supplies which haven't

been

Quoted 1 line

updated in more than 1 hour?)

Can't you just unregister the power-supply device if there hasn't been any heartbeat for some minutes and go back to the init state (i.e. the one where you are waiting for a heartbeat to appear for the first time)?

Quoted 4 lines

I've added the power_supply list and maintainer for some additional viewpoints on the second paragraph... If there are questions about how the Wacom hardware and its battery reporting works, I'm happy to provide answers...

Quoted 5 lines

I think the problem you have is quite specific to the Wacom hardware. Usually wireless devices are either connected or disconnected and drivers unregister the battery device on disconnect. So I think this logic belongs into Wacom specific code and not in generic power-supply core code.

Conceivably can the input device come and go too when in and out of proximity? If so, you may be able to just tie the power supply creation and destruction directly to the creation and destruction of that input device.