linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] spi: bcm2835: fix overflow in calculation of transfer time
@ 2015-07-29  7:34 kernel-TqfNSX0MhmxHKSADF0wUEw
       [not found] ` <1438155250-14476-1-git-send-email-kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
  0 siblings, 1 reply; 7+ messages in thread
From: kernel-TqfNSX0MhmxHKSADF0wUEw @ 2015-07-29  7:34 UTC (permalink / raw)
  To: Mark Brown, Stephen Warren, Lee Jones,
	linux-spi-u79uwXL29TY76Z2rM5mHXA,
	linux-rpi-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	noralf-L59+Z2yzLopAfugRpC6u6w
  Cc: Martin Sperl

From: Martin Sperl <kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>

This resulted in the use of polling mode when other approaches
(dma or interrupts) would have been more appropriate.

Happened for transfers longer than 477 bytes.

Reported-by: Noralf Tronnes <noralf-L59+Z2yzLopAfugRpC6u6w@public.gmane.org>
Signed-off-by: Martin Sperl <kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
---
 drivers/spi/spi-bcm2835.c |   10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/spi/spi-bcm2835.c b/drivers/spi/spi-bcm2835.c
index c9357bb..e7874a6 100644
--- a/drivers/spi/spi-bcm2835.c
+++ b/drivers/spi/spi-bcm2835.c
@@ -480,7 +480,7 @@ static int bcm2835_spi_transfer_one_poll(struct spi_master *master,
 					 struct spi_device *spi,
 					 struct spi_transfer *tfr,
 					 u32 cs,
-					 unsigned long xfer_time_us)
+					 unsigned long long xfer_time_us)
 {
 	struct bcm2835_spi *bs = spi_master_get_devdata(master);
 	unsigned long timeout;
@@ -531,7 +531,8 @@ static int bcm2835_spi_transfer_one(struct spi_master *master,
 {
 	struct bcm2835_spi *bs = spi_master_get_devdata(master);
 	unsigned long spi_hz, clk_hz, cdiv;
-	unsigned long spi_used_hz, xfer_time_us;
+	unsigned long spi_used_hz;
+	unsigned long long xfer_time_us;
 	u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
 
 	/* set clock */
@@ -573,9 +574,10 @@ static int bcm2835_spi_transfer_one(struct spi_master *master,
 	bs->rx_len = tfr->len;
 
 	/* calculate the estimated time in us the transfer runs */
-	xfer_time_us = tfr->len
+	xfer_time_us = (unsigned long long)tfr->len
 		* 9 /* clocks/byte - SPI-HW waits 1 clock after each byte */
-		* 1000000 / spi_used_hz;
+		* 1000000;
+	do_div(xfer_time_us, spi_used_hz);
 
 	/* for short requests run polling*/
 	if (xfer_time_us <= BCM2835_SPI_POLLING_LIMIT_US)
-- 
1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH] spi: bcm2835: fix overflow in calculation of transfer time
       [not found] ` <1438155250-14476-1-git-send-email-kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
@ 2015-07-29 14:03   ` Mark Brown
       [not found]     ` <20150729140331.GC11082-GFdadSzt00ze9xe1eoZjHA@public.gmane.org>
  2015-07-29 14:14   ` Applied "spi: bcm2835: fix overflow in calculation of transfer time" to the spi tree Mark Brown
  2015-08-14  0:01   ` Mark Brown
  2 siblings, 1 reply; 7+ messages in thread
From: Mark Brown @ 2015-07-29 14:03 UTC (permalink / raw)
  To: kernel-TqfNSX0MhmxHKSADF0wUEw
  Cc: Stephen Warren, Lee Jones, linux-spi-u79uwXL29TY76Z2rM5mHXA,
	linux-rpi-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	noralf-L59+Z2yzLopAfugRpC6u6w

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

On Wed, Jul 29, 2015 at 07:34:10AM +0000, kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org wrote:
> From: Martin Sperl <kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
> 
> This resulted in the use of polling mode when other approaches
> (dma or interrupts) would have been more appropriate.

Please describe the actual issue and how it's being fixed in the commit
message.  In this case the fix appears to be converting the timeout
variable to a long long which will make things better but obviously
isn't a complete fix for overflows - presumably the ideal thing here is
to handle saturation better?

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

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

* Applied "spi: bcm2835: fix overflow in calculation of transfer time" to the spi tree
       [not found] ` <1438155250-14476-1-git-send-email-kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
  2015-07-29 14:03   ` Mark Brown
@ 2015-07-29 14:14   ` Mark Brown
  2015-08-14  0:01   ` Mark Brown
  2 siblings, 0 replies; 7+ messages in thread
From: Mark Brown @ 2015-07-29 14:14 UTC (permalink / raw)
  To: Noralf Tronnes, Martin Sperl, Mark Brown; +Cc: linux-spi-u79uwXL29TY76Z2rM5mHXA

The patch

   spi: bcm2835: fix overflow in calculation of transfer time

has been applied to the spi tree at

   git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git 

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.  

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark

>From c020e378b2b388b712a40dce2da6d437d2b6e0e0 Mon Sep 17 00:00:00 2001
From: Martin Sperl <kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
Date: Wed, 29 Jul 2015 07:34:10 +0000
Subject: [PATCH] spi: bcm2835: fix overflow in calculation of transfer time

This resulted in the use of polling mode when other approaches
(dma or interrupts) would have been more appropriate.

Happened for transfers longer than 477 bytes.

Reported-by: Noralf Tronnes <noralf-L59+Z2yzLopAfugRpC6u6w@public.gmane.org>
Signed-off-by: Martin Sperl <kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
Signed-off-by: Mark Brown <broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
---
 drivers/spi/spi-bcm2835.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/spi/spi-bcm2835.c b/drivers/spi/spi-bcm2835.c
index 59705ab..767aa00 100644
--- a/drivers/spi/spi-bcm2835.c
+++ b/drivers/spi/spi-bcm2835.c
@@ -480,7 +480,7 @@ static int bcm2835_spi_transfer_one_poll(struct spi_master *master,
 					 struct spi_device *spi,
 					 struct spi_transfer *tfr,
 					 u32 cs,
-					 unsigned long xfer_time_us)
+					 unsigned long long xfer_time_us)
 {
 	struct bcm2835_spi *bs = spi_master_get_devdata(master);
 	unsigned long timeout;
@@ -531,7 +531,8 @@ static int bcm2835_spi_transfer_one(struct spi_master *master,
 {
 	struct bcm2835_spi *bs = spi_master_get_devdata(master);
 	unsigned long spi_hz, clk_hz, cdiv;
-	unsigned long spi_used_hz, xfer_time_us;
+	unsigned long spi_used_hz;
+	unsigned long long xfer_time_us;
 	u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
 
 	/* set clock */
@@ -575,9 +576,10 @@ static int bcm2835_spi_transfer_one(struct spi_master *master,
 	bs->rx_len = tfr->len;
 
 	/* calculate the estimated time in us the transfer runs */
-	xfer_time_us = tfr->len
+	xfer_time_us = (unsigned long long)tfr->len
 		* 9 /* clocks/byte - SPI-HW waits 1 clock after each byte */
-		* 1000000 / spi_used_hz;
+		* 1000000;
+	do_div(xfer_time_us, spi_used_hz);
 
 	/* for short requests run polling*/
 	if (xfer_time_us <= BCM2835_SPI_POLLING_LIMIT_US)
-- 
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH] spi: bcm2835: fix overflow in calculation of transfer time
       [not found]     ` <20150729140331.GC11082-GFdadSzt00ze9xe1eoZjHA@public.gmane.org>
@ 2015-07-29 14:31       ` Martin Sperl
       [not found]         ` <0D1C5BFD-04A0-4074-8424-DE941B00F887-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
  0 siblings, 1 reply; 7+ messages in thread
From: Martin Sperl @ 2015-07-29 14:31 UTC (permalink / raw)
  To: Mark Brown
  Cc: Stephen Warren, Lee Jones, linux-spi-u79uwXL29TY76Z2rM5mHXA,
	linux-rpi-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	noralf-L59+Z2yzLopAfugRpC6u6w


> On 29.07.2015, at 16:03, Mark Brown <broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org> wrote:
> 
> On Wed, Jul 29, 2015 at 07:34:10AM +0000, kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org wrote:
>> From: Martin Sperl <kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
>> 
>> This resulted in the use of polling mode when other approaches
>> (dma or interrupts) would have been more appropriate.
> 
> Please describe the actual issue and how it's being fixed in the commit
> message.  In this case the fix appears to be converting the timeout
> variable to a long long which will make things better but obviously
> isn't a complete fix for overflows - presumably the ideal thing here is
> to handle saturation better?

Will change the description of the commit and resubmit.

As for saturation I guess that nobody will ever run a spi-transfer
of len=2049638230413 bytes (at which point it would wrap) which:

a) would take 4679 days to reach at 125MHz 
  (the max frequency of the bcm2835 SPI HW limit)
b) would not fit spi_transfer->len (defined as unsigned)

So handling saturation is not an issue when using "long long”
in the context of this device.

Martin--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH] spi: bcm2835: fix overflow in calculation of transfer time
       [not found]         ` <0D1C5BFD-04A0-4074-8424-DE941B00F887-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
@ 2015-07-29 14:35           ` Martin Sperl
       [not found]             ` <0C2A92A8-867B-4BCE-8AD4-F2D297DB918F-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
  0 siblings, 1 reply; 7+ messages in thread
From: Martin Sperl @ 2015-07-29 14:35 UTC (permalink / raw)
  To: Mark Brown
  Cc: Stephen Warren, Lee Jones, linux-spi-u79uwXL29TY76Z2rM5mHXA,
	linux-rpi-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	noralf-L59+Z2yzLopAfugRpC6u6w

Hi Mark!

> On 29.07.2015, at 16:31, Martin Sperl <kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org> wrote:
> 
> Will change the description of the commit and resubmit.

Now that you have applied the patch already, do I need to
resubmit the commit with a changed description?

Martin
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH] spi: bcm2835: fix overflow in calculation of transfer time
       [not found]             ` <0C2A92A8-867B-4BCE-8AD4-F2D297DB918F-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
@ 2015-07-29 16:03               ` Mark Brown
  0 siblings, 0 replies; 7+ messages in thread
From: Mark Brown @ 2015-07-29 16:03 UTC (permalink / raw)
  To: Martin Sperl
  Cc: Stephen Warren, Lee Jones, linux-spi-u79uwXL29TY76Z2rM5mHXA,
	linux-rpi-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	noralf-L59+Z2yzLopAfugRpC6u6w

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

On Wed, Jul 29, 2015 at 04:35:03PM +0200, Martin Sperl wrote:

> Now that you have applied the patch already, do I need to
> resubmit the commit with a changed description?

No, it's fine.

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

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

* Applied "spi: bcm2835: fix overflow in calculation of transfer time" to the spi tree
       [not found] ` <1438155250-14476-1-git-send-email-kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
  2015-07-29 14:03   ` Mark Brown
  2015-07-29 14:14   ` Applied "spi: bcm2835: fix overflow in calculation of transfer time" to the spi tree Mark Brown
@ 2015-08-14  0:01   ` Mark Brown
  2 siblings, 0 replies; 7+ messages in thread
From: Mark Brown @ 2015-08-14  0:01 UTC (permalink / raw)
  To: Noralf Tronnes, Martin Sperl, Mark Brown; +Cc: linux-spi-u79uwXL29TY76Z2rM5mHXA

The patch

   spi: bcm2835: fix overflow in calculation of transfer time

has been applied to the spi tree at

   git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git 

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.  

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark

>From 0122a5183088e3117bb9c8fbe248914efb502f3f Mon Sep 17 00:00:00 2001
From: Martin Sperl <kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
Date: Wed, 29 Jul 2015 07:34:10 +0000
Subject: [PATCH] spi: bcm2835: fix overflow in calculation of transfer time

This resulted in the use of polling mode when other approaches
(dma or interrupts) would have been more appropriate.

Happened for transfers longer than 477 bytes.

Reported-by: Noralf Tronnes <noralf-L59+Z2yzLopAfugRpC6u6w@public.gmane.org>
Signed-off-by: Martin Sperl <kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
Signed-off-by: Mark Brown <broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
---
 drivers/spi/spi-bcm2835.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/spi/spi-bcm2835.c b/drivers/spi/spi-bcm2835.c
index c9357bb..e7874a6 100644
--- a/drivers/spi/spi-bcm2835.c
+++ b/drivers/spi/spi-bcm2835.c
@@ -480,7 +480,7 @@ static int bcm2835_spi_transfer_one_poll(struct spi_master *master,
 					 struct spi_device *spi,
 					 struct spi_transfer *tfr,
 					 u32 cs,
-					 unsigned long xfer_time_us)
+					 unsigned long long xfer_time_us)
 {
 	struct bcm2835_spi *bs = spi_master_get_devdata(master);
 	unsigned long timeout;
@@ -531,7 +531,8 @@ static int bcm2835_spi_transfer_one(struct spi_master *master,
 {
 	struct bcm2835_spi *bs = spi_master_get_devdata(master);
 	unsigned long spi_hz, clk_hz, cdiv;
-	unsigned long spi_used_hz, xfer_time_us;
+	unsigned long spi_used_hz;
+	unsigned long long xfer_time_us;
 	u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
 
 	/* set clock */
@@ -573,9 +574,10 @@ static int bcm2835_spi_transfer_one(struct spi_master *master,
 	bs->rx_len = tfr->len;
 
 	/* calculate the estimated time in us the transfer runs */
-	xfer_time_us = tfr->len
+	xfer_time_us = (unsigned long long)tfr->len
 		* 9 /* clocks/byte - SPI-HW waits 1 clock after each byte */
-		* 1000000 / spi_used_hz;
+		* 1000000;
+	do_div(xfer_time_us, spi_used_hz);
 
 	/* for short requests run polling*/
 	if (xfer_time_us <= BCM2835_SPI_POLLING_LIMIT_US)
-- 
2.5.0

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

end of thread, other threads:[~2015-08-14  0:01 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-29  7:34 [PATCH] spi: bcm2835: fix overflow in calculation of transfer time kernel-TqfNSX0MhmxHKSADF0wUEw
     [not found] ` <1438155250-14476-1-git-send-email-kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-07-29 14:03   ` Mark Brown
     [not found]     ` <20150729140331.GC11082-GFdadSzt00ze9xe1eoZjHA@public.gmane.org>
2015-07-29 14:31       ` Martin Sperl
     [not found]         ` <0D1C5BFD-04A0-4074-8424-DE941B00F887-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-07-29 14:35           ` Martin Sperl
     [not found]             ` <0C2A92A8-867B-4BCE-8AD4-F2D297DB918F-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-07-29 16:03               ` Mark Brown
2015-07-29 14:14   ` Applied "spi: bcm2835: fix overflow in calculation of transfer time" to the spi tree Mark Brown
2015-08-14  0:01   ` Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).