firmware: exynos-acpm: Use __counted_by()
Subsystems (2)
v2 cover firmware: exynos-acpm: Use __counted_by()
Changes in v2:
- Add tags
- Patch 2/3: typo, single ioread32 lines, merge the two if() for
txcnt/rxcnt checks
- Link to v1: https://patch.msgid.link/[email protected]
Improve safety of the code by using __counted_by/__counted_by_ptr.
Best regards,
Krzysztof
---
Krzysztof Kozlowski (3):
firmware: exynos-acpm: Use unsigned int for acpm_pmic_linux_errmap index
firmware: exynos-acpm: Count number of commands in acpm_xfer
firmware: exynos-acpm: Count acpm_xfer buffers with __counted_by_ptr
drivers/firmware/samsung/exynos-acpm-dvfs.c | 9 +++++---- drivers/firmware/samsung/exynos-acpm-pmic.c | 16 ++++++++-------- drivers/firmware/samsung/exynos-acpm.c | 14 +++++++------- drivers/firmware/samsung/exynos-acpm.h | 8 ++++---- 4 files changed, 24 insertions(+), 23 deletions(-)
---
base-commit: 635c467cc14ebdffab3f77610217c1dacaf88e8c
change-id: 20260214-firmare-acpm-counted-255f287c3dce
Best regards,
Signature · 1 line
Krzysztof Kozlowski <[email protected]>
PATCH v2 1/3 firmware: exynos-acpm: Use unsigned int for acpm_pmic_linux_errmap index
acpm_pmic_to_linux_err() uses an unsigned integer obtained from messages as index of array to map them to error codes. Array index cannot be negative, so make that explicit.
--- drivers/firmware/samsung/exynos-acpm-pmic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/samsung/exynos-acpm-pmic.c +1 -1
@@ -41,7 +41,7 @@ static const int acpm_pmic_linux_errmap[] = { [2] = -EACCES, /* Write register can't be accessed or issues to access it. */ }; -static int acpm_pmic_to_linux_err (int err) +static int acpm_pmic_to_linux_err (unsigned int err) {if (err >=0 && err <ARRAY_SIZE (acpm_pmic_linux_errmap))return acpm_pmic_linux_errmap[err];
Signature · 1 line
2.51.0
PATCH v2 2/3 firmware: exynos-acpm: Count number of commands in acpm_xfer
In reply to Krzysztof KozlowskiStruct acpm_xfer holds two buffers with u32 commands - rxd and txd - and counts their size by rxlen and txlen. "len" suffix is here ambiguous, so could mean length of the buffer or length of commands, and these are not the same since each command is u32. Rename these to rxcnt and txcnt, and change their usage to count the number of commands in each buffer.
This will have a benefit of allowing to use __counted_by_ptr later.
--- drivers/firmware/samsung/exynos-acpm-dvfs.c | 9 +++++---- drivers/firmware/samsung/exynos-acpm-pmic.c | 14 +++++++------- drivers/firmware/samsung/exynos-acpm.c | 14 +++++++------- drivers/firmware/samsung/exynos-acpm.h | 4 ++-- 4 files changed, 21 insertions(+), 20 deletions(-)
drivers/firmware/samsung/exynos-acpm-dvfs.c +5 -4
@@ -5,6 +5,7 @@* Copyright 2025 Linaro Ltd. */ +#include <linux/array_size.h> #include <linux/bitfield.h> #include <linux/firmware/samsung/exynos-acpm-protocol.h> #include <linux/ktime.h>
@@ -25,11 +26,11 @@ static void acpm_dvfs_set_xfer(struct acpm_xfer *xfer, u32 *cmd, size_t cmdlen, { xfer->acpm_chan_id = acpm_chan_id; xfer->txd = cmd; - xfer->txlen = cmdlen; + xfer->txcnt = cmdlen;if (response) { xfer->rxd = cmd; - xfer->rxlen = cmdlen; + xfer->rxcnt = cmdlen; } }
@@ -50,7 +51,7 @@ int acpm_dvfs_set_rate(const struct acpm_handle *handle,u32 cmd[4 ];acpm_dvfs_init_set_rate_cmd (cmd, clk_id, rate); -acpm_dvfs_set_xfer (&xfer, cmd,sizeof (cmd), acpm_chan_id, false); +acpm_dvfs_set_xfer (&xfer, cmd,ARRAY_SIZE (cmd), acpm_chan_id, false);return acpm_do_xfer (handle, &xfer); }
@@ -70,7 +71,7 @@ unsigned long acpm_dvfs_get_rate(const struct acpm_handle *handle,int ret;acpm_dvfs_init_get_rate_cmd (cmd, clk_id); -acpm_dvfs_set_xfer (&xfer, cmd,sizeof (cmd), acpm_chan_id, true); +acpm_dvfs_set_xfer (&xfer, cmd,ARRAY_SIZE (cmd), acpm_chan_id, true); ret =acpm_do_xfer (handle, &xfer);if (ret)
drivers/firmware/samsung/exynos-acpm-pmic.c +7 -7
@@ -63,8 +63,8 @@ static void acpm_pmic_set_xfer(struct acpm_xfer *xfer, u32 *cmd, size_t cmdlen, { xfer->txd = cmd; xfer->rxd = cmd; - xfer->txlen = cmdlen; - xfer->rxlen = cmdlen; + xfer->txcnt = cmdlen; + xfer->rxcnt = cmdlen; xfer->acpm_chan_id = acpm_chan_id; }
@@ -86,7 +86,7 @@ int acpm_pmic_read_reg(const struct acpm_handle *handle,int ret;acpm_pmic_init_read_cmd (cmd, type, reg, chan); -acpm_pmic_set_xfer (&xfer, cmd,sizeof (cmd), acpm_chan_id); +acpm_pmic_set_xfer (&xfer, cmd,ARRAY_SIZE (cmd), acpm_chan_id); ret =acpm_do_xfer (handle, &xfer);if (ret)
@@ -119,7 +119,7 @@ int acpm_pmic_bulk_read(const struct acpm_handle *handle,return -EINVAL ;acpm_pmic_init_bulk_read_cmd (cmd, type, reg, chan, count); -acpm_pmic_set_xfer (&xfer, cmd,sizeof (cmd), acpm_chan_id); +acpm_pmic_set_xfer (&xfer, cmd,ARRAY_SIZE (cmd), acpm_chan_id); ret =acpm_do_xfer (handle, &xfer);if (ret)
@@ -159,7 +159,7 @@ int acpm_pmic_write_reg(const struct acpm_handle *handle,int ret;acpm_pmic_init_write_cmd (cmd, type, reg, chan, value); -acpm_pmic_set_xfer (&xfer, cmd,sizeof (cmd), acpm_chan_id); +acpm_pmic_set_xfer (&xfer, cmd,ARRAY_SIZE (cmd), acpm_chan_id); ret =acpm_do_xfer (handle, &xfer);if (ret)
@@ -199,7 +199,7 @@ int acpm_pmic_bulk_write(const struct acpm_handle *handle,return -EINVAL ;acpm_pmic_init_bulk_write_cmd (cmd, type, reg, chan, count, buf); -acpm_pmic_set_xfer (&xfer, cmd,sizeof (cmd), acpm_chan_id); +acpm_pmic_set_xfer (&xfer, cmd,ARRAY_SIZE (cmd), acpm_chan_id); ret =acpm_do_xfer (handle, &xfer);if (ret)
@@ -229,7 +229,7 @@ int acpm_pmic_update_reg(const struct acpm_handle *handle,int ret;acpm_pmic_init_update_cmd (cmd, type, reg, chan, value, mask); -acpm_pmic_set_xfer (&xfer, cmd,sizeof (cmd), acpm_chan_id); +acpm_pmic_set_xfer (&xfer, cmd,ARRAY_SIZE (cmd), acpm_chan_id); ret =acpm_do_xfer (handle, &xfer);if (ret)
drivers/firmware/samsung/exynos-acpm.c +7 -7
@@ -205,7 +205,7 @@ static void acpm_get_saved_rx(struct acpm_chan *achan, rx_seqnum =FIELD_GET (ACPM_PROTOCOL_SEQNUM , rx_data->cmd[0 ]);if (rx_seqnum == tx_seqnum) { -memcpy (xfer->rxd, rx_data->cmd, xfer->rxlen); +memcpy (xfer->rxd, rx_data->cmd, xfer->rxcnt *sizeof (*xfer->rxd));clear_bit (rx_seqnum -1 , achan->bitmap_seqnum); } }
@@ -258,8 +258,7 @@ static int acpm_get_rx(struct acpm_chan *achan, const struct acpm_xfer *xfer)if (rx_data->response) {if (rx_seqnum == tx_seqnum) { -__ioread32_copy (xfer->rxd, addr, - xfer->rxlen /4 ); +__ioread32_copy (xfer->rxd, addr, xfer->rxcnt); rx_set = true;clear_bit (seqnum, achan->bitmap_seqnum); }else {
@@ -269,8 +268,7 @@ static int acpm_get_rx(struct acpm_chan *achan, const struct acpm_xfer *xfer)* clear yet the bitmap. It will be cleared * after the response is copied to the request. */ -__ioread32_copy (rx_data->cmd, addr, - xfer->rxlen /4 ); +__ioread32_copy (rx_data->cmd, addr, xfer->rxcnt); } }else {clear_bit (seqnum, achan->bitmap_seqnum);
@@ -425,7 +423,9 @@ int acpm_do_xfer(const struct acpm_handle *handle, const struct acpm_xfer *xfer) achan = &acpm->chans[xfer->acpm_chan_id]; -if (!xfer->txd || xfer->txlen > achan->mlen || xfer->rxlen > achan->mlen) +if (!xfer->txd || + (xfer->txcnt *sizeof (*xfer->txd) > achan->mlen) || + (xfer->rxcnt *sizeof (*xfer->rxd) > achan->mlen))return -EINVAL ;if (!achan->poll_completion) {
@@ -448,7 +448,7 @@ int acpm_do_xfer(const struct acpm_handle *handle, const struct acpm_xfer *xfer)/* Write TX command. */ __iowrite32_copy (achan->tx.base + achan->mlen * tx_front, - xfer->txd, xfer->txlen /4 ); + xfer->txd, xfer->txcnt);/* Advance TX front. */ writel (idx, achan->tx.front);
drivers/firmware/samsung/exynos-acpm.h +2 -2
@@ -10,8 +10,8 @@struct acpm_xfer {const u32 *txd;u32 *rxd; -size_t txlen; -size_t rxlen; +size_t txcnt; +size_t rxcnt;unsigned int acpm_chan_id; };
Signature · 1 line
2.51.0
Re: [PATCH v2 2/3] firmware: exynos-acpm: Count number of commands in acpm_xfer
Quoted 10 lines · Krzysztof Kozlowski
Struct
acpm_xferholds two buffers with u32 commands - rxd and txd - and counts their size by rxlen and txlen. "len" suffix is here ambiguous, so could mean length of the buffer or length of commands, and these are not the same since each command is u32. Rename these to rxcnt and txcnt, and change their usage to count the number of commands in each buffer.This will have a benefit of allowing to use
__counted_by_ptrlater.Signed-off-by Krzysztof Kozlowski <[email protected]>
Thank you!
PATCH v2 3/3 firmware: exynos-acpm: Count acpm_xfer buffers with __counted_by_ptr
In reply to Krzysztof KozlowskiUse __counted_by_ptr() attribute on the acpm_xfer buffers so UBSAN will validate runtime that we do not pass over the buffer size, thus making code safer.
Usage of __counted_by_ptr() (or actually __counted_by()) requires that counter is initialized before counted array.
---
__counted_by_ptr() actually maps to __counted_by() for clang v20. Alternatively we could introduce new __sized_by(), already supported by clang v20, but it is not available for GCC, AFAIU.
RFT, testing would need clang=20+ with COMNFIG_UBSAN and CONFIG_UBSAN_BOUNDS enabled.
--- drivers/firmware/samsung/exynos-acpm-dvfs.c | 4 ++-- drivers/firmware/samsung/exynos-acpm.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/firmware/samsung/exynos-acpm-dvfs.c +2 -2
@@ -25,12 +25,12 @@ static void acpm_dvfs_set_xfer(struct acpm_xfer *xfer, u32 *cmd, size_t cmdlen, unsigned int acpm_chan_id, bool response) { xfer->acpm_chan_id = acpm_chan_id; - xfer->txd = cmd; xfer->txcnt = cmdlen; + xfer->txd = cmd;if (response) { - xfer->rxd = cmd; xfer->rxcnt = cmdlen; + xfer->rxd = cmd; } }
drivers/firmware/samsung/exynos-acpm.h +2 -2
@@ -8,8 +8,8 @@#define __EXYNOS_ACPM_H__struct acpm_xfer { -const u32 *txd; -u32 *rxd; + const u32 *txd __counted_by_ptr(txcnt); + u32 *rxd __counted_by_ptr(rxcnt);size_t txcnt;size_t rxcnt;unsigned int acpm_chan_id;
Signature · 1 line
2.51.0
Quoted 9 lines · Krzysztof Kozlowski
Changes in v2:
- Add tags
- Patch 2/3: typo, single ioread32 lines, merge the twoif()for
txcnt/rxcnt checks
- Link to v1: https://patch.msgid.link/[email protected]Improve safety of the code by using __counted_by/__counted_by_ptr.
[...]
Applied, thanks!
[1/3] firmware: exynos-acpm: Use unsigned int for acpm_pmic_linux_errmap index
https://git.kernel.org/krzk/linux/c/910e91a6496f7e0af80e945a6b850b589b90669e [2/3] firmware: exynos-acpm: Count number of commands in acpm_xfer https://git.kernel.org/krzk/linux/c/00808ae2e679a97dccc5cf0ee4474ba1e2e8a21a [3/3] firmware: exynos-acpm: Count acpm_xfer buffers with __counted_by_ptr https://git.kernel.org/krzk/linux/c/951b8eee0581bbf39e7b0464d679eee8cb9da3e0
Best regards,
Signature · 1 line
Krzysztof Kozlowski <[email protected]>