Skip to contentSkip to conversation

spi: fix the read path in spidev

PATCH In review Sebastian Siewior 3 participants 4 messages Jul 1, 2008 – Jul 2, 2008 2 messages not in the archive SPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: fix the read path in spidev

This got broken by the recent "fix rmmod $spi_driver while spidev-user is active". I tested the rmmod & write path but didn't check the read path. I am sorry. The read logic changed and spidev_sync_read() + spidev_sync_write() do not return zero on success anymore but the number of bytes that has been transfered over the bus.
This patch changes the logic and copy_to_user() gets called again.

The write path returns the number of bytes which are written to the underlying device what may be less than the requested size. This patch makes the same change to the read path or else we request a read of 20 bytes, get 10, don't call copy to user and report to the user that we read 10 bytes.

---
 drivers/spi/spidev.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)
drivers/spi/spidev.c +4 -6
index 72282cd..60ec6f7 100644
@@ -167,14 +167,14 @@ spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
 
 	mutex_lock(&spidev->buf_lock);
 	status = spidev_sync_read(spidev, count);
-	if (status == 0) {
+	if (status > 0) {
 		unsigned long	missing;
 
-		missing = copy_to_user(buf, spidev->buffer, count);
-		if (count && missing == count)
+		missing = copy_to_user(buf, spidev->buffer, status);
+		if (status && missing == status)
 			status = -EFAULT;
 		else
-			status = count - missing;
+			status = status - missing;
 	}
 	mutex_unlock(&spidev->buf_lock);
 
@@ -200,8 +200,6 @@ spidev_write(struct file *filp, const char __user *buf,
 	missing = copy_from_user(spidev->buffer, buf, count);
 	if (missing == 0) {
 		status = spidev_sync_write(spidev, count);
-		if (status == 0)
-			status = count;
 	} else
 		status = -EFAULT;
 	mutex_unlock(&spidev->buf_lock);
Signature · 8 lines
1.5.5.2


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
#2 lore raw
In reply to a message not in the archive
Quoted 7 lines · On Tue, 1 Jul 2008 17:45:04 +0200 Sebastian Siewior…

This got broken by the recent "fix rmmod $spi_driver while spidev-user is active". I tested the rmmod & write path but didn't check the read path. I am sorry. The read logic changed and spidev_sync_read() + spidev_sync_write() do not return zero on success anymore but the number of bytes that has been transfered over the bus.
This patch changes the logic and copy_to_user() gets called again.

Quoted 5 lines

The write path returns the number of bytes which are written to the underlying device what may be less than the requested size. This patch makes the same change to the read path or else we request a read of 20 bytes, get 10, don't call copy to user and report to the user that we read 10 bytes.

Looks OK. I'll queue it for 2.6.26.

drivers/spi/spidev.c spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) · line 170

Quoted 11 lines
drivers/spi/spidev.c +1 -1
index 72282cd..60ec6f7 100644
@@ -167,14 +167,14 @@ spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
 
 	mutex_lock(&spidev->buf_lock);
 	status = spidev_sync_read(spidev, count);
-	if (status == 0) {
+	if (status > 0) {

`status' is a poorly chosen identifier. But Iworked it out.

drivers/spi/spidev.c spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) · line 174

Quoted 6 lines
drivers/spi/spidev.c +2 -2
 		unsigned long	missing;
 
-		missing = copy_to_user(buf, spidev->buffer, count);
-		if (count && missing == count)
+		missing = copy_to_user(buf, spidev->buffer, status);
+		if (status && missing == status)

The test for status!=0 is unneeded - I'll remove it.

Quoted 18 lines
drivers/spi/spidev.c +1 -3
 			status = -EFAULT;
 		else
-			status = count - missing;
+			status = status - missing;
 	}
 	mutex_unlock(&spidev->buf_lock);
 
@@ -200,8 +200,6 @@ spidev_write(struct file *filp, const char __user *buf,
 	missing = copy_from_user(spidev->buffer, buf, count);
 	if (missing == 0) {
 		status = spidev_sync_write(spidev, count);
-		if (status == 0)
-			status = count;
 	} else
 		status = -EFAULT;
 	mutex_unlock(&spidev->buf_lock);
Signature · 1 line
1.5.5.2

-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW! Studies have shown that voting for your favorite open source project, along with a healthy diet, reduces your potential for chronic lameness and boredom. Vote Now at http://www.sourceforge.net/community/cca08

#3 lore raw
In reply to a message not in the archive
Quoted 6 lines · Sebastian Siewior

This got broken by the recent "fix rmmod $spi_driver while spidev-user is active". I tested the rmmod & write path but didn't check the read path. I am sorry. The read logic changed and spidev_sync_read() + spidev_sync_write() do not return zero on success anymore but the number of bytes that has been transfered over the bus.
This patch changes the logic and copy_to_user() gets called again.

Good catch ... did you observe this failure happening "in the wild" or was this a code-inspection kind of thing?

Quoted 7 lines

The write path returns the number of bytes which are written to the underlying device what may be less than the requested size. This patch makes the same change to the read path or else we request a read of 20 bytes, get 10, don't call copy to user and report to the user that we read 10 bytes.

Quoted 40 lines
Cc David Brownell <[email protected]>
---
 drivers/spi/spidev.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)
drivers/spi/spidev.c +4 -6
index 72282cd..60ec6f7 100644
@@ -167,14 +167,14 @@ spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
 
 	mutex_lock(&spidev->buf_lock);
 	status = spidev_sync_read(spidev, count);
-	if (status == 0) {
+	if (status > 0) {
 		unsigned long	missing;
 
-		missing = copy_to_user(buf, spidev->buffer, count);
-		if (count && missing == count)
+		missing = copy_to_user(buf, spidev->buffer, status);
+		if (status && missing == status)
 			status = -EFAULT;
 		else
-			status = count - missing;
+			status = status - missing;
 	}
 	mutex_unlock(&spidev->buf_lock);
 
@@ -200,8 +200,6 @@ spidev_write(struct file *filp, const char __user *buf,
 	missing = copy_from_user(spidev->buffer, buf, count);
 	if (missing == 0) {
 		status = spidev_sync_write(spidev, count);
-		if (status == 0)
-			status = count;
 	} else
 		status = -EFAULT;
 	mutex_unlock(&spidev->buf_lock);
Signature · 1 line
1.5.5.2

-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW! Studies have shown that voting for your favorite open source project, along with a healthy diet, reduces your potential for chronic lameness and boredom. Vote Now at http://www.sourceforge.net/community/cca08

#5 lore raw
In reply to a message not in the archive
Quoted 11 lines · David Brownell
Quoted 6 lines · Sebastian Siewior

This got broken by the recent "fix rmmod $spi_driver while spidev-user is active". I tested the rmmod & write path but didn't check the read path. I am sorry. The read logic changed and spidev_sync_read() + spidev_sync_write() do not return zero on success anymore but the number of bytes that has been transfered over the bus.
This patch changes the logic and copy_to_user() gets called again.

Good catch ... did you observe this failure happening "in the wild" or was this a code-inspection kind of thing?

"in the wild". I've been testing HW testing after I rebased to -rc8 and somehow I got no error but the receive buffer was empty....

Sebastian

-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW! Studies have shown that voting for your favorite open source project, along with a healthy diet, reduces your potential for chronic lameness and boredom. Vote Now at http://www.sourceforge.net/community/cca08