Re: [PATCH v2] treewide, serdev: change receive_buf() return type to size_t

Alex Elder
2024-01-24T21:59:50Z

On 1/22/24 12:05 PM, Francesco Dolcini wrote:
> From: Francesco Dolcini <francesco.dolcini@toradex.com>
>
> receive_buf() is called from ttyport_receive_buf() that expects values
> ">= 0" from serdev_controller_receive_buf(), change its return type from
> ssize_t to size_t.
>
> The need for this clean-up was noticed while fixing a warning, see
> commit 94d053942544 ("Bluetooth: btnxpuart: fix recv_buf() return value").
> Changing the callback prototype to return an unsigned seems the best way
> to document the API and ensure that is properly used.
>
> GNSS drivers implementation of serdev receive_buf() callback return
> directly the return value of gnss_insert_raw(). gnss_insert_raw()
> returns a signed int, however this is not an issue since the value
> returned is always positive, because of the kfifo_in() implementation.

Agreed.

> gnss_insert_raw() could be changed to return also an unsigned, however
> this is not implemented here as request by the GNSS maintainer Johan
> Hovold.

I was going to suggest this, and suggest changing the "ret" in
gnss_insert_raw() to return size_t. But to really do that right
it would include some other changes as well. Leaving it as an
int as Johan suggests preserves correct behavior.

One minor point below, plus a couple comments affirming that
an int return value is OK because it's always non-negative.

Reviewed-by: Alex Elder <elder@linaro.org>

> Suggested-by: Jiri Slaby <jirislaby@kernel.org>
> Link: https://lore.kernel.org/all/087be419-ec6b-47ad-851a-5e1e3ea5cfcc@kernel.org/
> Signed-off-by: Francesco Dolcini <francesco.dolcini@toradex.com>
> Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> #for-iio
> ---
> v1:
> - https://lore.kernel.org/all/20231214170146.641783-1-francesco@dolcini.it/
> v2:
> - rebased on 6.8-rc1
> - add acked-by Jonathan
> - do not change gnss_insert_raw()
> - do not change the code style of the gnss code
> - commit message improvements, explain the reasons for doing only minimal
> changes on the GNSS part
> ---
> drivers/bluetooth/btmtkuart.c | 4 ++--
> drivers/bluetooth/btnxpuart.c | 4 ++--
> drivers/bluetooth/hci_serdev.c | 4 ++--
> drivers/gnss/serial.c | 2 +-
> drivers/gnss/sirf.c | 2 +-
> drivers/greybus/gb-beagleplay.c | 6 +++---
> drivers/iio/chemical/pms7003.c | 4 ++--
> drivers/iio/chemical/scd30_serial.c | 4 ++--
> drivers/iio/chemical/sps30_serial.c | 4 ++--
> drivers/iio/imu/bno055/bno055_ser_core.c | 4 ++--
> drivers/mfd/rave-sp.c | 4 ++--
> drivers/net/ethernet/qualcomm/qca_uart.c | 2 +-
> drivers/nfc/pn533/uart.c | 4 ++--
> drivers/nfc/s3fwrn5/uart.c | 4 ++--
> drivers/platform/chrome/cros_ec_uart.c | 4 ++--
> drivers/platform/surface/aggregator/core.c | 4 ++--
> drivers/tty/serdev/serdev-ttyport.c | 10 ++++------
> include/linux/serdev.h | 8 ++++----
> sound/drivers/serial-generic.c | 4 ++--
> 19 files changed, 40 insertions(+), 42 deletions(-)
>

. . .

> diff --git a/drivers/mfd/rave-sp.c b/drivers/mfd/rave-sp.c
> index 6ff84b2600c5..62a6613fb070 100644
> --- a/drivers/mfd/rave-sp.c
> +++ b/drivers/mfd/rave-sp.c
> @@ -471,8 +471,8 @@ static void rave_sp_receive_frame(struct rave_sp *sp,
> rave_sp_receive_reply(sp, data, length);
> }
>
> -static ssize_t rave_sp_receive_buf(struct serdev_device *serdev,
> - const u8 *buf, size_t size)
> +static size_t rave_sp_receive_buf(struct serdev_device *serdev,
> + const u8 *buf, size_t size)
> {
> struct device *dev = &serdev->dev;
> struct rave_sp *sp = dev_get_drvdata(dev);

One return path in this function returns (src - buf), which is
*almost* guaranteed to be positive. The one case it wouldn't
be is if the assignment of end wraps around, and that's not
checked.

I think it's fine, but... That seems theoretically possible.

> diff --git a/drivers/net/ethernet/qualcomm/qca_uart.c b/drivers/net/ethernet/qualcomm/qca_uart.c
> index 223321897b96..20f50bde82ac 100644

. . .

> diff --git a/drivers/platform/surface/aggregator/core.c b/drivers/platform/surface/aggregator/core.c
> index 9591a28bc38a..ba550eaa06fc 100644
> --- a/drivers/platform/surface/aggregator/core.c
> +++ b/drivers/platform/surface/aggregator/core.c
> @@ -227,8 +227,8 @@ EXPORT_SYMBOL_GPL(ssam_client_bind);
>
> /* -- Glue layer (serdev_device -> ssam_controller). ------------------------ */
>
> -static ssize_t ssam_receive_buf(struct serdev_device *dev, const u8 *buf,
> - size_t n)
> +static size_t ssam_receive_buf(struct serdev_device *dev, const u8 *buf,
> + size_t n)
> {
> struct ssam_controller *ctrl;
> int ret;

Here you the return value will be positive despite ret being
a signed int. So like the GNSS case, this is OK.

> diff --git a/drivers/tty/serdev/serdev-ttyport.c b/drivers/tty/serdev/serdev-ttyport.c
> index e94e090cf0a1..3d7ae7fa5018 100644

. . .

> diff --git a/sound/drivers/serial-generic.c b/sound/drivers/serial-generic.c
> index d6e5aafd697c..36409a56c675 100644
> --- a/sound/drivers/serial-generic.c
> +++ b/sound/drivers/serial-generic.c
> @@ -100,8 +100,8 @@ static void snd_serial_generic_write_wakeup(struct serdev_device *serdev)
> snd_serial_generic_tx_wakeup(drvdata);
> }
>
> -static ssize_t snd_serial_generic_receive_buf(struct serdev_device *serdev,
> - const u8 *buf, size_t count)
> +static size_t snd_serial_generic_receive_buf(struct serdev_device *serdev,
> + const u8 *buf, size_t count)
> {
> int ret;
> struct snd_serial_generic *drvdata = serdev_device_get_drvdata(serdev);

Same thing here.