lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <bd28d718-ab7e-4470-a4de-22b995db8b94@baylibre.com>
Date: Wed, 16 Apr 2025 09:56:12 -0500
From: David Lechner <dlechner@...libre.com>
To: Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
 Mark Brown <broonie@...nel.org>, linux-spi@...r.kernel.org,
 linux-kernel@...r.kernel.org
Subject: Re: [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it

On 4/16/25 1:16 AM, Andy Shevchenko wrote:
> This helper converts the given bits per word to bytes. The result
> will always be power-of-two (e.g. for 37 bits it returns 8 bytes)
> or 0 for 0 input.
> 
> There are a couple of cases in SPI that are using the same approach
> and at least one more (in IIO) would benefit of it. Add a helper
> for everyone.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@...ux.intel.com>
> ---
>  drivers/spi/spi.c       |  2 +-
>  include/linux/spi/spi.h | 15 +++++++++++++++
>  2 files changed, 16 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
> index b0e7702951fe..1bc0fdbb1bd7 100644
> --- a/drivers/spi/spi.c
> +++ b/drivers/spi/spi.c
> @@ -3800,7 +3800,7 @@ int spi_split_transfers_maxwords(struct spi_controller *ctlr,
>  		size_t maxsize;
>  		int ret;
>  
> -		maxsize = maxwords * roundup_pow_of_two(BITS_TO_BYTES(xfer->bits_per_word));
> +		maxsize = maxwords * spi_bpw_to_bytes(xfer->bits_per_word);
>  		if (xfer->len > maxsize) {
>  			ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
>  							   maxsize);
> diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
> index 834a09bd8ccc..abfc7f5e19e4 100644
> --- a/include/linux/spi/spi.h
> +++ b/include/linux/spi/spi.h
> @@ -1340,6 +1340,21 @@ static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw)
>  	return false;
>  }
>  
> +/**
> + * spi_bpw_to_bytes - Covert bits per word to bytes
> + * @bpw: Bits per word
> + *
> + * This function converts the given @bpw to bytes. The result is always
> + * power-of-two (e.g. for 37 bits it returns 8 bytes) or 0 for 0 input.

The SPI subsystem currently only supports bpw up to 32, so perhaps not
the best choice of value for the example. I would go with 20 bits getting
rounded up to 4 bytes to match the existing docs for @bits_per_word.

> + *
> + * Returns:
> + * Bytes for the given @bpw.
> + */
> +static inline u32 spi_bpw_to_bytes(u32 bpw)
> +{
> +	return roundup_pow_of_two(BITS_TO_BYTES(bpw));

Do we need to #include <linux/log2.h> for roundup_pow_of_two()?

> +}
> +
>  /**
>   * spi_controller_xfer_timeout - Compute a suitable timeout value
>   * @ctlr: SPI device


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ