diff mbox series

[v6,31/37] sg: add sg_iosubmit_v3 and sg_ioreceive_v3 ioctls

Message ID 20200112235755.14197-32-dgilbert@interlog.com (mailing list archive)
State Deferred
Headers show
Series sg: add v4 interface | expand

Commit Message

Douglas Gilbert Jan. 12, 2020, 11:57 p.m. UTC
Add ioctl(SG_IOSUBMIT_V3) and ioctl(SG_IORECEIVE_V3). These ioctls
are meant to be (almost) drop-in replacements for the write()/read()
async version 3 interface. They only accept the version 3 interface.

See the webpage at: http://sg.danny.cz/sg/sg_v40.html
specifically the table in the section titled: "13 SG interface
support changes".

If sgv3 is a struct sg_io_hdr object, suitably configured, then
    res = write(sg_fd, &sgv3, sizeof(sgv3));
and
    res = ioctl(sg_fd, SG_IOSUBMIT_V3, &sgv3);
are equivalent. Dito for read() and ioctl(SG_IORECEIVE_V3).

Signed-off-by: Douglas Gilbert <dgilbert@interlog.com>
---
 drivers/scsi/sg.c      | 77 +++++++++++++++++++++++++++++++++++++++++-
 include/uapi/scsi/sg.h |  6 ++++
 2 files changed, 82 insertions(+), 1 deletion(-)

Comments

Bart Van Assche Jan. 13, 2020, 12:50 a.m. UTC | #1
On 2020-01-12 15:57, Douglas Gilbert wrote:
> Add ioctl(SG_IOSUBMIT_V3) and ioctl(SG_IORECEIVE_V3). These ioctls
> are meant to be (almost) drop-in replacements for the write()/read()
> async version 3 interface. They only accept the version 3 interface.
> 
> See the webpage at: http://sg.danny.cz/sg/sg_v40.html
> specifically the table in the section titled: "13 SG interface
> support changes".
> 
> If sgv3 is a struct sg_io_hdr object, suitably configured, then
>     res = write(sg_fd, &sgv3, sizeof(sgv3));
> and
>     res = ioctl(sg_fd, SG_IOSUBMIT_V3, &sgv3);
> are equivalent. Dito for read() and ioctl(SG_IORECEIVE_V3).

The Linux kernel already supports several interfaces for submitting I/O
requests asynchronously, e.g. libaio and io_uring. Do we really need yet
another interface for submitting I/O requests? Has it been considered to
add SG I/O support to one of the existing asynchronous I/O request
interfaces?

Thanks,

Bart.
Douglas Gilbert Jan. 13, 2020, 10:39 a.m. UTC | #2
On 2020-01-13 1:50 a.m., Bart Van Assche wrote:
> On 2020-01-12 15:57, Douglas Gilbert wrote:
>> Add ioctl(SG_IOSUBMIT_V3) and ioctl(SG_IORECEIVE_V3). These ioctls
>> are meant to be (almost) drop-in replacements for the write()/read()
>> async version 3 interface. They only accept the version 3 interface.
>>
>> See the webpage at: http://sg.danny.cz/sg/sg_v40.html
>> specifically the table in the section titled: "13 SG interface
>> support changes".
>>
>> If sgv3 is a struct sg_io_hdr object, suitably configured, then
>>      res = write(sg_fd, &sgv3, sizeof(sgv3));
>> and
>>      res = ioctl(sg_fd, SG_IOSUBMIT_V3, &sgv3);
>> are equivalent. Dito for read() and ioctl(SG_IORECEIVE_V3).
> 
> The Linux kernel already supports several interfaces for submitting I/O
> requests asynchronously, e.g. libaio and io_uring. Do we really need yet
> another interface for submitting I/O requests? Has it been considered to
> add SG I/O support to one of the existing asynchronous I/O request
> interfaces?

Linux policy toward new ioctl()s has changed over the years. Where adding new
ones was once forbidden or strongly discouraged they are now preferred
over unconstrained methods such as adding new system calls or using write()
and read(). The SG_IOSUBMIT and SG_IORECEIVE ioctls have now been proposed
on several occasions by L. Torvalds as replacements for write() and read()
that together with the sg v3 interface (based on struct sg_io_hdr) predate
both libaio and io_uring. So I view these two new ioctls for the sg v3
interfaces as allowing the existing write() and read() interface to be first
deprecated, then removed with the these ioctls as drop in replacements. I view
this as more end-user friendly that just removing the write() and read() calls
as the bsg driver did (and thus removing its async interface and breaking code
that used it).

As my second stage patchset demonstrates, the superior user data handling
architecture *** of the SCSI command sets (as compared to say NVMe and ATA)
allows for major performance wins in areas such as copying and comparing
disk-to-disk operations that are not available in libaio and io_uring.

Douglas Gilbert


*** that being: do _not_ specify the details of how near end user data is
     handled in the command set, leave it to lower levels. So in the case
     of a disk to disk copy, the commands may come asynchronously from the
     user space but there is no need for the user data to leave the kernel
     level. Offloaded copies now being discussed throw the problem "over
     the fence" but ultimately some system needs to do the copy, and there
     is a good chance that is a Linux embedded system, especially if it has
     good support for copying.
diff mbox series

Patch

diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
index 7b29e36a130b..adb4e2d3b1c6 100644
--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -874,6 +874,24 @@  sg_ctl_iosubmit(struct file *filp, struct sg_fd *sfp, void __user *p)
 	return -EPERM;
 }
 
+static int
+sg_ctl_iosubmit_v3(struct file *filp, struct sg_fd *sfp, void __user *p)
+{
+	int res;
+	u8 hdr_store[SZ_SG_IO_V4];      /* max(v3interface, v4interface) */
+	struct sg_io_hdr *h3p = (struct sg_io_hdr *)hdr_store;
+	struct sg_device *sdp = sfp->parentdp;
+
+	res = sg_allow_if_err_recovery(sdp, (filp->f_flags & O_NONBLOCK));
+	if (unlikely(res))
+		return res;
+	if (copy_from_user(h3p, p, SZ_SG_IO_HDR))
+		return -EFAULT;
+	if (h3p->interface_id == 'S')
+		return sg_v3_submit(filp, sfp, h3p, false, NULL);
+	return -EPERM;
+}
+
 #if IS_ENABLED(SG_LOG_ACTIVE)
 static void
 sg_rq_state_fail(struct sg_fd *sfp, enum sg_rq_state exp_old_st,
@@ -1259,7 +1277,7 @@  sg_receive_v4(struct sg_fd *sfp, struct sg_request *srp, void __user *p,
 	h4p->din_resid = srp->in_resid;
 	h4p->dout_resid = srp->s_hdr4.out_resid;
 	h4p->usr_ptr = srp->s_hdr4.usr_ptr;
-	h4p->response = (u64)srp->s_hdr4.sbp;
+	h4p->response = (u64)(unsigned long)srp->s_hdr4.sbp;
 	h4p->request_extra = srp->pack_id;
 	if (p) {
 		if (copy_to_user(p, h4p, SZ_SG_IO_V4))
@@ -1323,6 +1341,57 @@  sg_ctl_ioreceive(struct file *filp, struct sg_fd *sfp, void __user *p)
 	return sg_receive_v4(sfp, srp, p, h4p);
 }
 
+/*
+ * Called when ioctl(SG_IORECEIVE_V3) received. Expects a v3 interface.
+ * Checks if O_NONBLOCK file flag given, if not checks given flags field
+ * to see if SGV4_FLAG_IMMED is set. Either of these implies non blocking.
+ * When non-blocking and there is no request waiting, yields EAGAIN;
+ * otherwise it waits.
+ */
+static int
+sg_ctl_ioreceive_v3(struct file *filp, struct sg_fd *sfp, void __user *p)
+{
+	bool non_block = !!(filp->f_flags & O_NONBLOCK);
+	int res;
+	int pack_id = SG_PACK_ID_WILDCARD;
+	u8 v3_holder[SZ_SG_IO_HDR];
+	struct sg_io_hdr *h3p = (struct sg_io_hdr *)v3_holder;
+	struct sg_device *sdp = sfp->parentdp;
+	struct sg_request *srp;
+
+	res = sg_allow_if_err_recovery(sdp, non_block);
+	if (unlikely(res))
+		return res;
+	/* Get first three 32 bit integers: guard, proto+subproto */
+	if (copy_from_user(h3p, p, SZ_SG_IO_HDR))
+		return -EFAULT;
+	/* for v3: interface_id=='S' (in a 32 bit int) */
+	if (h3p->interface_id != 'S')
+		return -EPERM;
+	if (h3p->flags & SGV4_FLAG_IMMED)
+		non_block = true;	/* set by either this or O_NONBLOCK */
+	SG_LOG(3, sfp, "%s: non_block(+IMMED)=%d\n", __func__, non_block);
+
+	if (test_bit(SG_FFD_FORCE_PACKID, sfp->ffd_bm))
+		pack_id = h3p->pack_id;
+
+	srp = sg_find_srp_by_id(sfp, pack_id);
+	if (!srp) {     /* nothing available so wait on packet or */
+		if (unlikely(SG_IS_DETACHING(sdp)))
+			return -ENODEV;
+		if (non_block)
+			return -EAGAIN;
+		res = wait_event_interruptible
+				(sfp->read_wait,
+				 sg_get_ready_srp(sfp, &srp, pack_id));
+		if (unlikely(SG_IS_DETACHING(sdp)))
+			return -ENODEV;
+		if (unlikely(res))
+			return res;	/* signal --> -ERESTARTSYS */
+	}	/* now srp should be valid */
+	return sg_receive_v3(sfp, srp, p);
+}
+
 static int
 sg_read_v1v2(void __user *buf, int count, struct sg_fd *sfp,
 	   struct sg_request *srp)
@@ -1924,9 +1993,15 @@  sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
 	case SG_IOSUBMIT:
 		SG_LOG(3, sfp, "%s:    SG_IOSUBMIT\n", __func__);
 		return sg_ctl_iosubmit(filp, sfp, p);
+	case SG_IOSUBMIT_V3:
+		SG_LOG(3, sfp, "%s:    SG_IOSUBMIT_V3\n", __func__);
+		return sg_ctl_iosubmit_v3(filp, sfp, p);
 	case SG_IORECEIVE:
 		SG_LOG(3, sfp, "%s:    SG_IORECEIVE\n", __func__);
 		return sg_ctl_ioreceive(filp, sfp, p);
+	case SG_IORECEIVE_V3:
+		SG_LOG(3, sfp, "%s:    SG_IORECEIVE_V3\n", __func__);
+		return sg_ctl_ioreceive_v3(filp, sfp, p);
 	case SG_GET_SCSI_ID:
 		return sg_ctl_scsi_id(sdev, sfp, p);
 	case SG_SET_FORCE_PACK_ID:
diff --git a/include/uapi/scsi/sg.h b/include/uapi/scsi/sg.h
index 1696d3499b0b..dcfac0ad805c 100644
--- a/include/uapi/scsi/sg.h
+++ b/include/uapi/scsi/sg.h
@@ -356,6 +356,12 @@  struct sg_header {
 /* Gives some v4 identifying info to driver, receives associated response */
 #define SG_IORECEIVE _IOWR(SG_IOCTL_MAGIC_NUM, 0x42, struct sg_io_v4)
 
+/* Submits a v3 interface object to driver */
+#define SG_IOSUBMIT_V3 _IOWR(SG_IOCTL_MAGIC_NUM, 0x45, struct sg_io_hdr)
+
+/* Gives some v3 identifying info to driver, receives associated response */
+#define SG_IORECEIVE_V3 _IOWR(SG_IOCTL_MAGIC_NUM, 0x46, struct sg_io_hdr)
+
 /* command queuing is always on when the v3 or v4 interface is used */
 #define SG_DEF_COMMAND_Q 0