Linux-SPI Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v1 1/2] spi: Allow to have all native CSs in use along with GPIOs
@ 2021-04-20 14:10 Andy Shevchenko
  2021-04-20 14:10 ` [PATCH v1 2/2] spi: Avoid potential UB when counting unused native CSs Andy Shevchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2021-04-20 14:10 UTC (permalink / raw)
  To: Mark Brown, linux-spi, linux-kernel; +Cc: Andy Shevchenko

The commit 7d93aecdb58d ("spi: Add generic support for unused native cs
with cs-gpios") excludes the valid case for the controllers that doesn't
need to switch native CS in order to perform the transfer, i.e. when

  0		native
  ...		...
  <n> - 1	native
  <n>		GPIO
  <n> + 1	GPIO
  ...		...

where <n> defines maximum of native CSs supported by the controller.

To allow this, bail out from spi_get_gpio_descs() conditionally for
the controllers which explicitly marked with SPI_MASTER_GPIO_SS.

Fixes: 7d93aecdb58d ("spi: Add generic support for unused native cs with cs-gpios")
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/spi/spi.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 36c46feab6d4..9c3730a9f7d5 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -2610,8 +2610,9 @@ static int spi_get_gpio_descs(struct spi_controller *ctlr)
 	}
 
 	ctlr->unused_native_cs = ffz(native_cs_mask);
-	if (num_cs_gpios && ctlr->max_native_cs &&
-	    ctlr->unused_native_cs >= ctlr->max_native_cs) {
+
+	if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios &&
+	    ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) {
 		dev_err(dev, "No unused native chip select available\n");
 		return -EINVAL;
 	}
-- 
2.30.2


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v1 2/2] spi: Avoid potential UB when counting unused native CSs
  2021-04-20 14:10 [PATCH v1 1/2] spi: Allow to have all native CSs in use along with GPIOs Andy Shevchenko
@ 2021-04-20 14:10 ` Andy Shevchenko
  2021-04-20 14:56   ` Mark Brown
  0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2021-04-20 14:10 UTC (permalink / raw)
  To: Mark Brown, linux-spi, linux-kernel; +Cc: Andy Shevchenko

ffz(), that has been used to count unused native CSs, might produce UB
when called against ~0U. To fix that, open code it with ffs(~value) - 1.

Fixes: 7d93aecdb58d ("spi: Add generic support for unused native cs with cs-gpios")
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/spi/spi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 9c3730a9f7d5..01f95bee2ac8 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -2609,7 +2609,7 @@ static int spi_get_gpio_descs(struct spi_controller *ctlr)
 		native_cs_mask |= BIT(i);
 	}
 
-	ctlr->unused_native_cs = ffz(native_cs_mask);
+	ctlr->unused_native_cs = ffs(~native_cs_mask) - 1;
 
 	if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios &&
 	    ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) {
-- 
2.30.2


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1 2/2] spi: Avoid potential UB when counting unused native CSs
  2021-04-20 14:10 ` [PATCH v1 2/2] spi: Avoid potential UB when counting unused native CSs Andy Shevchenko
@ 2021-04-20 14:56   ` Mark Brown
  2021-04-20 15:07     ` Andy Shevchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Mark Brown @ 2021-04-20 14:56 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-spi, linux-kernel


[-- Attachment #1: Type: text/plain, Size: 161 bytes --]

On Tue, Apr 20, 2021 at 05:10:04PM +0300, Andy Shevchenko wrote:
> ffz(), that has been used to count unused native CSs, might produce UB

Bit of an IA there...

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1 2/2] spi: Avoid potential UB when counting unused native CSs
  2021-04-20 14:56   ` Mark Brown
@ 2021-04-20 15:07     ` Andy Shevchenko
  2021-04-20 15:22       ` Mark Brown
  0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2021-04-20 15:07 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel

On Tue, Apr 20, 2021 at 03:56:16PM +0100, Mark Brown wrote:
> On Tue, Apr 20, 2021 at 05:10:04PM +0300, Andy Shevchenko wrote:
> > ffz(), that has been used to count unused native CSs, might produce UB
> 
> Bit of an IA there...

UB -- undefined behaviour.
I'll decode it. Should I decode CS as well?

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1 2/2] spi: Avoid potential UB when counting unused native CSs
  2021-04-20 15:07     ` Andy Shevchenko
@ 2021-04-20 15:22       ` Mark Brown
  0 siblings, 0 replies; 5+ messages in thread
From: Mark Brown @ 2021-04-20 15:22 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-spi, linux-kernel


[-- Attachment #1: Type: text/plain, Size: 389 bytes --]

On Tue, Apr 20, 2021 at 06:07:06PM +0300, Andy Shevchenko wrote:
> On Tue, Apr 20, 2021 at 03:56:16PM +0100, Mark Brown wrote:
> > On Tue, Apr 20, 2021 at 05:10:04PM +0300, Andy Shevchenko wrote:
> > > ffz(), that has been used to count unused native CSs, might produce UB

> > Bit of an IA there...

> UB -- undefined behaviour.
> I'll decode it. Should I decode CS as well?

CS is fine.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-20 14:10 [PATCH v1 1/2] spi: Allow to have all native CSs in use along with GPIOs Andy Shevchenko
2021-04-20 14:10 ` [PATCH v1 2/2] spi: Avoid potential UB when counting unused native CSs Andy Shevchenko
2021-04-20 14:56   ` Mark Brown
2021-04-20 15:07     ` Andy Shevchenko
2021-04-20 15:22       ` Mark Brown

Linux-SPI Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-spi/0 linux-spi/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-spi linux-spi/ https://lore.kernel.org/linux-spi \
		linux-spi@vger.kernel.org
	public-inbox-index linux-spi

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-spi


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git