Skip to contentSkip to conversation

scsi: sg: Add warning message in source code about non-idempotent SG_IO

PATCH In review Ewan D. Milne 3 participants 3 messages Jan 27 – Feb 4 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: sg: Add warning message in source code about non-idempotent SG_IO

The SG_IO ioctl() is interruptible, and it is not obvious that using it to send arbitrary commands that are non-idempotent can resulted in failures or data corruption due to the syscall being reissued. Add a warning message in the source code of the function to inform potential users of this pitfall.

---
 drivers/scsi/sg.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/scsi/sg.c +11 -0
index b3af9b78fa12..40975f11e256 100644
@@ -924,6 +924,17 @@ sg_ioctl_common(struct file *filp, Sg_device *sdp, Sg_fd *sfp,
 
 	switch (cmd_in) {
 	case SG_IO:
+		/*
+		 * WARNING:
+		 *
+		 * This ioctl() uses an interruptible wait for I/O completion.
+		 * As a result, if it is interrupted by a signal (e.g. SIGSTOP)
+		 * the result will be discarded and the syscall will be retried.
+		 * Caution should be used with issuing commands that are not
+		 * idempotent (e.g. COMPARE AND WRITE, or commands to a sequential
+		 * media device such as a tape device) as this will likely not
+		 * have the desired behavior.  Data corruption could occur.
+		 */
 		if (atomic_read(&sdp->detaching))
 			return -ENODEV;
 		if (!scsi_block_when_processing_errors(sdp->device))
Signature · 1 line
2.43.0
#1 lore raw

drivers/scsi/sg.c sg_ioctl_common(struct file *filp, Sg_device *sdp, Sg_fd *sfp, · line 934

Quoted 9 lines · Ewan D. Milne
drivers/scsi/sg.c +8 -0
 	case SG_IO:
+		/*
+		 * WARNING:
+		 *
+		 * This ioctl() uses an interruptible wait for I/O completion.
+		 * As a result, if it is interrupted by a signal (e.g. SIGSTOP)
+		 * the result will be discarded and the syscall will be retried.
+		 * Caution should be used with issuing commands that are not
+		 * idempotent (e.g. COMPARE AND WRITE, or commands to a sequential

overly long line.

But I don't think a comment in the source code is helpful for applications anyway.

Maybe we need a proper man page for this ioctl or some other kind of official user facing documentation?

A message that is not in the archive (sent off-list or not yet received): CAGtn9rk2TFYEdRh8q7LCZNiCcotU-f+xZLv=N_XbZtzcWgx+9Q@mail.gmail.com

#3 lore raw

Ewan,

Quoted 2 lines
Quoted 2 lines

Maybe we need a proper man page for this ioctl or some other kind of official user facing documentation?

A man page would be appreciated.

Signature · 1 line
Martin K. Petersen