sg: add v4 interface
Subsystems (2)
Douglas Gilbert
maintainerPATCH v13 35/45 sg: first debugfs support
In reply to Douglas GilbertDuplicate the semantics of 'cat /proc/scsi/sg/debug' on 'cat /sys/kernel/debug/scsi_generic/snapshot'. Make code that generates the snapshot conditional on either CONFIG_SCSI_PROC_FS or CONFIG_DEBUG_FS being defined.
Also add snapshot_devs which can be written with a list of comma separated integers corresponding to sg (minor) device numbers. That file can also be read showing that list. Minus one (or any negative number) means accept all when in the first position (the default) or means the end of the list in a later position. When a subsequent
cat /sys/kernel/debug/scsi_generic/snapshot
is performed, only sg device numbers matching an element in that list are output.
--- drivers/scsi/sg.c | 414 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 333 insertions(+), 81 deletions(-)
drivers/scsi/sg.c +333 -81
@@ -43,6 +43,7 @@ static char *sg_version_date = "20190606";#include <linux/cred.h> /* for sg_check_file_access() */ #include <linux/proc_fs.h> #include <linux/xarray.h> +#include <linux/debugfs.h> #include <scsi/scsi.h> #include <scsi/scsi_eh.h>
@@ -67,6 +68,10 @@ static char *sg_version_date = "20190606";#endif #endif +#if IS_ENABLED (CONFIG_SCSI_PROC_FS ) ||IS_ENABLED (CONFIG_DEBUG_FS ) +#define SG_PROC_OR_DEBUG_FS 1 +#endif +/* SG_MAX_CDB_SIZE should be 260 (spc4r37 section 3.1.30) however the type * of sg_io_hdr::cmd_len can only represent 255. All SCSI commands greater * than 16 bytes are "variable length" whose length is a multiple of 4, so:
@@ -269,6 +274,8 @@ struct sg_comm_wr_t { /* arguments to sg_common_write() */static void sg_rq_end_io (struct request *rq,blk_status_t status);/* Declarations of other static functions used before they are defined */ static int sg_proc_init (void ); +static void sg_dfs_init (void ); +static void sg_dfs_exit (void );static int sg_start_req (struct sg_request *srp,struct sg_comm_wr_t *cwrp,int dxfer_dir);static void sg_finish_scsi_blk_rq (struct sg_request *srp);
@@ -338,7 +345,7 @@ static const char *sg_rq_st_str(enum sg_rq_state rq_st, bool long_str); pr_info("sg: sdp or sfp NULL, " fmt, ##a); \ } while (0)#else -#define SG_LOG(depth, sfp, fmt, a...) { } +#define SG_LOG(depth, sfp, fmt, a...) do { } while (0)#endif /* end of CONFIG_SCSI_LOGGING && SG_DEBUG conditional */
@@ -2748,22 +2755,6 @@ sg_remove_device(struct device *cl_dev, struct class_interface *cl_intf)kref_put (&sdp->d_ref, sg_device_destroy); } -module_param_named (scatter_elem_sz, scatter_elem_sz, int,S_IRUGO |S_IWUSR ); -module_param_named (def_reserved_size, def_reserved_size, int, -S_IRUGO |S_IWUSR ); -module_param_named (allow_dio, sg_allow_dio, int,S_IRUGO |S_IWUSR ); - -MODULE_AUTHOR ("Douglas Gilbert" ); -MODULE_DESCRIPTION ("SCSI generic (sg) driver" ); -MODULE_LICENSE ("GPL" ); -MODULE_VERSION (SG_VERSION_STR ); -MODULE_ALIAS_CHARDEV_MAJOR (SCSI_GENERIC_MAJOR ); - -MODULE_PARM_DESC (scatter_elem_sz,"scatter gather element " -"size (default: max(SG_SCATTER_SZ, PAGE_SIZE))" ); -MODULE_PARM_DESC (def_reserved_size,"size of buffer reserved for each fd" ); -MODULE_PARM_DESC (allow_dio,"allow direct I/O (default: 0 (disallow))" ); -static int __init init_sg (void ) {
@@ -2813,6 +2804,7 @@ init_sg(void) rc =scsi_register_interface (&sg_interface);if (rc ==0 ) {sg_proc_init (); +sg_dfs_init ();return 0 ; }class_destroy (sg_sysfs_class);
@@ -2826,17 +2818,10 @@ init_sg(void)return rc; } -#if !IS_ENABLED (CONFIG_SCSI_PROC_FS ) -static int -sg_proc_init (void ) -{ -return 0 ; -} -#endif - static void __exitexit_sg (void ) { +sg_dfs_exit ();if (IS_ENABLED (CONFIG_SCSI_PROC_FS ))remove_proc_subtree ("scsi/sg" ,NULL );scsi_unregister_interface (&sg_interface);
@@ -3279,7 +3264,7 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id)/* here if one of above loops does _not_ find a match */ if (IS_ENABLED (CONFIG_SCSI_PROC_FS )) {if (search_for_1) { -const char *cptp ="pack_id=" ; +__maybe_unused const char *cptp ="pack_id=" ;if (is_bad_st) SG_LOG(1, sfp, "%s: %s%d wrong state: %s\n",
@@ -3718,17 +3703,6 @@ sg_remove_sfp(struct kref *kref)schedule_work (&sfp->ew_fd.work); } -static int -sg_idr_max_id (int id,void *p,void *data) -__must_hold (sg_index_lock) -{ -int *k = data; - -if (*k < id) - *k = id; -return 0 ; -} -/* must be called with sg_index_lock held */ static struct sg_device *sg_lookup_dev (int dev)
@@ -3758,7 +3732,7 @@ sg_get_dev(int dev)return sdp; } -#if IS_ENABLED (CONFIG_SCSI_PROC_FS ) +#if IS_ENABLED (SG_PROC_OR_DEBUG_FS )static const char *sg_rq_st_str (enum sg_rq_state rq_st,bool long_str) {
@@ -3787,7 +3761,35 @@ sg_rq_st_str(enum sg_rq_state rq_st, bool long_str) }#endif -#if IS_ENABLED (CONFIG_SCSI_PROC_FS )/* long, almost to end of file */ +#if IS_ENABLED (SG_PROC_OR_DEBUG_FS ) + +#define SG_SNAPSHOT_DEV_MAX 4 + +/* +* For snapshot_devs array, -1 or two adjacent the same is terminator. +* -1 in first element of first two elements the same implies all. +*/ +static struct sg_dfs_context_t { +struct dentry *dfs_rootdir; +int snapshot_devs[SG_SNAPSHOT_DEV_MAX ]; +} sg_dfs_cxt; + +struct sg_proc_deviter { +loff_t index; +size_t max; +int fd_index; +}; + +static int +sg_idr_max_id (int id,void *p,void *data) +__must_hold (sg_index_lock) +{ +int *k = data; + +if (*k < id) + *k = id; +return 0 ; +}static int sg_last_dev (void )
@@ -3801,6 +3803,41 @@ sg_last_dev(void)return k +1 ;/* origin 1 */ } +static void * +dev_seq_start (struct seq_file *s,loff_t *pos) +{ +struct sg_proc_deviter *it =kzalloc (sizeof (*it),GFP_KERNEL ); + + s->private = it; +if (!it) +return NULL ; + + it->index = *pos; + it->max =sg_last_dev (); +if (it->index >= it->max) +return NULL ; +return it; +} + +static void * +dev_seq_next (struct seq_file *s,void *v,loff_t *pos) +{ +struct sg_proc_deviter *it = s->private; + + *pos = ++it->index; +return (it->index < it->max) ? it :NULL ; +} + +static void +dev_seq_stop (struct seq_file *s,void *v) +{ +kfree (s->private); +} + +#endif /* SG_PROC_OR_DEBUG_FS */ + +#if IS_ENABLED (CONFIG_SCSI_PROC_FS )/* around 100 lines */ +static int sg_proc_seq_show_int (struct seq_file *s,void *v) {
@@ -3814,7 +3851,7 @@ sg_proc_single_open_adio(struct inode *inode, struct file *filp)return single_open (filp, sg_proc_seq_show_int, &sg_allow_dio); } -static ssize_t +static ssize_t sg_proc_write_adio (struct file *filp,const char __user *buffer,size_t count,loff_t *off) {
@@ -3836,7 +3873,7 @@ sg_proc_single_open_dressz(struct inode *inode, struct file *filp)return single_open (filp, sg_proc_seq_show_int, &sg_big_buff); } -static ssize_t +static ssize_t sg_proc_write_dressz (struct file *filp,const char __user *buffer,size_t count,loff_t *off) {
@@ -3871,43 +3908,6 @@ sg_proc_seq_show_devhdr(struct seq_file *s, void *v)return 0 ; } -struct sg_proc_deviter { -loff_t index; -size_t max; -int fd_index; -}; - -static void * -dev_seq_start (struct seq_file *s,loff_t *pos) -{ -struct sg_proc_deviter *it =kzalloc (sizeof (*it),GFP_KERNEL ); - - s->private = it; -if (! it) -return NULL ; - - it->index = *pos; - it->max =sg_last_dev (); -if (it->index >= it->max) -return NULL ; -return it; -} - -static void * -dev_seq_next (struct seq_file *s,void *v,loff_t *pos) -{ -struct sg_proc_deviter *it = s->private; - - *pos = ++it->index; -return (it->index < it->max) ? it :NULL ; -} - -static void -dev_seq_stop (struct seq_file *s,void *v) -{ -kfree (s->private); -} -static int sg_proc_seq_show_dev (struct seq_file *s,void *v) {
@@ -3954,6 +3954,10 @@ sg_proc_seq_show_devstrs(struct seq_file *s, void *v) return 0; } +#endif /* CONFIG_SCSI_PROC_FS (~100 lines back) */ + +#if IS_ENABLED (SG_PROC_OR_DEBUG_FS ) +/* Writes debug info for one sg_request in obp buffer */ static int sg_proc_debug_sreq (struct sg_request *srp,int to,char *obp,int len)
@@ -4096,18 +4100,20 @@ sg_proc_seq_show_debug(struct seq_file *s, void *v)bool found = false;bool trunc = false;const int bp_len =SG_PROC_DEBUG_SZ ; +int j, sd_n;int n =0 ;int k =0 ;unsigned long iflags;struct sg_proc_deviter *it = (struct sg_proc_deviter *)v;struct sg_device *sdp;int *fdi_p; +const int *dev_arr = sg_dfs_cxt.snapshot_devs;char *bp;char *disk_name;char b1[128 ]; b1[0 ] ='\0' ; -if (it && (0 == it->index)) +if (it && it->index ==0 )seq_printf (s,"max_active_device=%d def_reserved_size=%d\n" , (int )it->max, def_reserved_size); fdi_p = it ? &it->fd_index : &k;
@@ -4119,8 +4125,31 @@ sg_proc_seq_show_debug(struct seq_file *s, void *v) }read_lock_irqsave (&sg_index_lock, iflags); sdp = it ?sg_lookup_dev (it->index) :NULL ; -if (NULL == sdp) +if (!sdp) goto skip; + sd_n = dev_arr[0 ]; +if (sd_n !=-1 && sd_n != sdp->index && sd_n != dev_arr[1 ]) { +for (j =1 ; j <SG_SNAPSHOT_DEV_MAX ; ) { + sd_n = dev_arr[j]; +if (sd_n <0 ) + goto skip; + ++j; +if (j >=SG_SNAPSHOT_DEV_MAX ) { +if (sd_n == sdp->index) { + found = true; +break ; + } + }else if (sd_n == dev_arr[j]) { + goto skip; + }else if (sd_n == sdp->index) { + found = true; +break ; + } + } +if (!found) + goto skip; + found = false; + }if (!xa_empty (&sdp->sfp_arr)) { found = true; disk_name = (sdp->disk ? sdp->disk->disk_name :"?_?" );
@@ -4160,6 +4189,10 @@ sg_proc_seq_show_debug(struct seq_file *s, void *v)return 0 ; } +#endif /* SG_PROC_OR_DEBUG_FS */ + +#if IS_ENABLED (CONFIG_SCSI_PROC_FS ) +static const struct proc_ops adio_proc_ops = { .proc_open = sg_proc_single_open_adio, .proc_read = seq_read,
@@ -4218,7 +4251,226 @@ sg_proc_init(void)/* remove_proc_subtree("scsi/sg", NULL) in exit_sg() does cleanup */ -#endif /* CONFIG_SCSI_PROC_FS (~400 lines back) */ +#else + +static int +sg_proc_init (void ) +{ +return 0 ; +} + +#endif /* CONFIG_SCSI_PROC_FS */ + +#if IS_ENABLED (CONFIG_DEBUG_FS ) + +struct sg_dfs_attr { +const char *name; +umode_t mode; +int (*show)(void *d,struct seq_file *m); +ssize_t (*write)(void *d,const char __user *b,size_t s,loff_t *o); +/* Set either .show or .seq_ops. */ +const struct seq_operations *seq_ops; +}; +static int +sg_dfs_snapshot_devs_show (void *data,struct seq_file *m) +{ +bool last; +int k, d; +struct sg_dfs_context_t *ctxp = data; + +for (k =0 ; k <SG_SNAPSHOT_DEV_MAX ; ++k) { + d = ctxp->snapshot_devs[k]; + last = (k +1 ==SG_SNAPSHOT_DEV_MAX ); +if (d <0 ) { +if (k ==0 ) +seq_puts (m,"-1" ); +break ; + } +if (!last && d == ctxp->snapshot_devs[k +1 ]) { +if (k ==0 ) +seq_puts (m,"-1" ); +break ; + } +if (k !=0 ) +seq_puts (m,"," ); +seq_printf (m,"%d" , d); + } +seq_puts (m,"\n" ); +return 0 ; +} + +static ssize_t +sg_dfs_snapshot_devs_write (void *data,const char __user *buf,size_t count, +loff_t *ppos) +{ +bool trailing_comma; +int k, n; +struct sg_dfs_context_t *cxtp = data; +char lbuf[64 ] = { }, *cp, *c2p; + +if (count >=sizeof (lbuf)) { +pr_err ("%s: operation too long\n" , __func__); +return -EINVAL ; + } +if (copy_from_user (lbuf, buf, count)) +return -EFAULT ; +for (k =0 , n =0 , cp = lbuf; k <SG_SNAPSHOT_DEV_MAX ; + ++k, cp = c2p +1 ) { + c2p =strchr (cp,',' ); +if (c2p) + *c2p ='\0' ; + trailing_comma = !!c2p; +/* sscanf is easier to use that this ... */ +if (kstrtoint (cp,10 , cxtp->snapshot_devs + k)) +break ; + ++n; +if (!trailing_comma) +break ; + } +if (n ==0 ) { +return -EINVAL ; + }else if (k >=SG_SNAPSHOT_DEV_MAX && trailing_comma) { +pr_err ("%s: only %d elements in snapshot array\n" , __func__, +SG_SNAPSHOT_DEV_MAX ); +return -EINVAL ; + } +if (n <SG_SNAPSHOT_DEV_MAX ) + cxtp->snapshot_devs[n] =-1 ; +return count; +} + +static int +sg_dfs_show (struct seq_file *m,void *v) +{ +const struct sg_dfs_attr *attr = m->private; +void *data =d_inode (m->file->f_path.dentry->d_parent)->i_private; + +return attr->show (data, m); +} + +static ssize_t +sg_dfs_write (struct file *file,const char __user *buf,size_t count, +loff_t *ppos) +{ +struct seq_file *m = file->private_data; +const struct sg_dfs_attr *attr = m->private; +void *data =d_inode (file->f_path.dentry->d_parent)->i_private; + +/* +* Attributes that only implement .seq_ops are read-only and 'attr' is +* the same with 'data' in this case. +*/ +if (attr == data || !attr->write) +return -EPERM ; +return attr->write (data, buf, count, ppos); +} + +static int +sg_dfs_open (struct inode *inode,struct file *file) +{ +const struct sg_dfs_attr *attr = inode->i_private; +void *data =d_inode (file->f_path.dentry->d_parent)->i_private; +struct seq_file *m; +int ret; + +if (attr->seq_ops) { + ret =seq_open (file, attr->seq_ops); +if (!ret) { + m = file->private_data; + m->private = data; + } +return ret; + } +if (WARN_ON_ONCE (!attr->show)) +return -EPERM ; +return single_open (file, sg_dfs_show, inode->i_private); +} + +static int +sg_dfs_release (struct inode *inode,struct file *file) +{ +const struct sg_dfs_attr *attr = inode->i_private; + +if (attr->show) +return single_release (inode, file); +return seq_release (inode, file); +} + +static const struct file_operations sg_dfs_fops = { + .owner =THIS_MODULE , + .open = sg_dfs_open, + .read = seq_read, + .write = sg_dfs_write, + .llseek = seq_lseek, + .release = sg_dfs_release, +}; + +static void sg_dfs_mk_files (struct dentry *parent,void *data, +const struct sg_dfs_attr *attr) +{ +if (IS_ERR_OR_NULL (parent)) +return ; + +d_inode (parent)->i_private = data; +for (; attr->name; ++attr) +debugfs_create_file (attr->name, attr->mode, parent, + (void *)attr, &sg_dfs_fops); +} + +static const struct seq_operations sg_snapshot_seq_ops = { + .start = dev_seq_start, + .next = dev_seq_next, + .stop = dev_seq_stop, + .show = sg_proc_seq_show_debug, +}; + +static const struct sg_dfs_attr sg_dfs_attrs[] = { + {"snapshot" ,0400 , .seq_ops = &sg_snapshot_seq_ops}, + {"snapshot_devs" ,0600 , sg_dfs_snapshot_devs_show, + sg_dfs_snapshot_devs_write}, + { }, +}; + +static void +sg_dfs_init (void ) +{ +/* create and populate /sys/kernel/debug/scsi_generic directory */ +if (!sg_dfs_cxt.dfs_rootdir) { + sg_dfs_cxt.dfs_rootdir =debugfs_create_dir ("scsi_generic" , +NULL ); +sg_dfs_mk_files (sg_dfs_cxt.dfs_rootdir, &sg_dfs_cxt, + sg_dfs_attrs); + } + sg_dfs_cxt.snapshot_devs[0 ] =-1 ;/* show all sg devices */ +} + +static void +sg_dfs_exit (void ) +{ +debugfs_remove_recursive (sg_dfs_cxt.dfs_rootdir); + sg_dfs_cxt.dfs_rootdir =NULL ; +} + +#else /* not defined: CONFIG_DEBUG_FS */ + +static void sg_dfs_init (void ) {} +static void sg_dfs_exit (void ) {} + +#endif /* CONFIG_DEBUG_FS */ + +module_param_named (scatter_elem_sz, scatter_elem_sz, int,0644 ); +module_param_named (def_reserved_size, def_reserved_size, int,0644 ); +module_param_named (allow_dio, sg_allow_dio, int,0644 ); + +MODULE_AUTHOR ("Douglas Gilbert" ); +MODULE_DESCRIPTION ("SCSI generic (sg) driver" ); +MODULE_LICENSE ("GPL" ); +MODULE_VERSION (SG_VERSION_STR ); +MODULE_ALIAS_CHARDEV_MAJOR (SCSI_GENERIC_MAJOR ); + +MODULE_PARM_DESC (scatter_elem_sz,"scatter gather element size (default: max(SG_SCATTER_SZ, PAGE_SIZE))" ); +MODULE_PARM_DESC (def_reserved_size,"size of buffer reserved for each fd" ); +MODULE_PARM_DESC (allow_dio,"allow direct I/O (default: 0 (disallow))" );module_init (init_sg);module_exit (exit_sg);
Signature · 1 line
2.25.1
Douglas Gilbert
maintainerPATCH v13 36/45 sg: rework mmap support
In reply to Douglas GilbertLinux has an issue with mmap-ed multiple pages issued by alloc_pages() [with order > 0]. So when mmap(2) is called if the reserve request's scatter gather (sgat) list is either:
- not big enough, or
- made up of elements of order > 0 (i.e. size > PAGE_SIZE)
then throw away reserve requests sgat list and rebuild it meeting those requirements.
Clean up related code and stop doing mmap+indirect_io.
This new mmap implementation is marginally more flexible (but still compatible with) the production driver. Previously if the user wanted a larger mmap(2) 'length' than the reserve request's size, then they needed to use ioctl(SG_SET_RESERVED_SIZE) to set the new size first. Now mmap(2) called on a sg device node will adjust to the size given by 'length' [mmap's second parameter].
Tweak some SG_LOG() levels to control the amount of debug output. Add some WRITE_ONCE() macros when bitop integers are being initialised.
--- drivers/scsi/sg.c | 177 +++++++++++++++++++++++++++------------------- 1 file changed, 106 insertions(+), 71 deletions(-)
drivers/scsi/sg.c +106 -71
@@ -116,6 +116,7 @@ enum sg_rq_state { /* N.B. sg_rq_state_arr assumes SG_RS_AWAIT_RCV==2 */#define SG_FRQ_DEACT_ORPHAN 6/* not keeping orphan so de-activate */ #define SG_FRQ_RECEIVING 7/* guard against multiple receivers */ #define SG_FRQ_BLK_PUT_REQ 8/* set when blk_put_request() called */ +#define SG_FRQ_FOR_MMAP 9/* request needs PAGE_SIZE elements */ /* Bit positions (flags) for sg_fd::ffd_bm bitmask follow */ #define SG_FFD_FORCE_PACKID 0/* receive only given pack_id/tag */
@@ -708,7 +709,7 @@ sg_write(struct file *filp, const char __user *p, size_t count, loff_t *ppos) input_size, (unsigned int)opcode, current->comm); } cwr.h3p = h3p; - cwr.frq_bm[0 ] =0 ; +WRITE_ONCE (cwr.frq_bm[0 ],0 ); cwr.timeout = sfp->timeout; cwr.cmd_len = cmd_size; cwr.filp = filp;
@@ -769,7 +770,7 @@ sg_v3_submit(struct file *filp, struct sg_fd *sfp, struct sg_io_hdr *hp,/* when v3 seen, allow cmd_q on this fd (def: no cmd_q) */ set_bit (SG_FFD_CMD_Q , sfp->ffd_bm); ul_timeout =msecs_to_jiffies (hp->timeout); - cwr.frq_bm[0 ] =0 ; +WRITE_ONCE (cwr.frq_bm[0 ],0 );__assign_bit (SG_FRQ_SYNC_INVOC , cwr.frq_bm, (int )sync); cwr.h3p = hp; cwr.timeout = min_t(unsigned long, ul_timeout, INT_MAX);
@@ -810,7 +811,7 @@ sg_submit_v4(struct file *filp, struct sg_fd *sfp, void __user *p, ul_timeout =msecs_to_jiffies (h4p->timeout); cwr.filp = filp; cwr.sfp = sfp; - cwr.frq_bm[0 ] =0 ; +WRITE_ONCE (cwr.frq_bm[0 ],0 );__assign_bit (SG_FRQ_SYNC_INVOC , cwr.frq_bm, (int )sync);__set_bit (SG_FRQ_IS_V4I , cwr.frq_bm); cwr.h4p = h4p;
@@ -2237,13 +2238,39 @@ sg_fasync(int fd, struct file *filp, int mode)return fasync_helper (fd, filp, mode, &sfp->async_qp); } +static void +sg_vma_open (struct vm_area_struct *vma) +{ +struct sg_fd *sfp = vma->vm_private_data; + +if (unlikely (!sfp)) { +pr_warn ("%s: sfp null\n" , __func__); +return ; + } +kref_get (&sfp->f_ref); +} + +static void +sg_vma_close (struct vm_area_struct *vma) +{ +struct sg_fd *sfp = vma->vm_private_data; + +if (unlikely (!sfp)) { +pr_warn ("%s: sfp null\n" , __func__); +return ; + } +kref_put (&sfp->f_ref, sg_remove_sfp);/* get in: sg_vma_open() */ +} +/* Note: the error return: VM_FAULT_SIGBUS causes a "bus error" */ static vm_fault_t sg_vma_fault (struct vm_fault *vmf) { -int k, length; -unsigned long offset, len, sa, iflags; +int k, n, length; +int res =VM_FAULT_SIGBUS ; +unsigned long offset;struct vm_area_struct *vma = vmf->vma; +struct page *page;struct sg_scatter_hold *rsv_schp;struct sg_request *srp;struct sg_device *sdp;
@@ -2269,7 +2296,7 @@ sg_vma_fault(struct vm_fault *vmf)SG_LOG (1 , sfp,"%s: srp%s\n" , __func__, nbp); goto out_err; } -xa_lock_irqsave (&sfp->srp_arr, iflags); +mutex_lock (&sfp->f_mutex); rsv_schp = &srp->sgat_h; offset = vmf->pgoff <<PAGE_SHIFT ;if (offset >= (unsigned int )rsv_schp->buflen) {
@@ -2277,44 +2304,37 @@ sg_vma_fault(struct vm_fault *vmf) offset); goto out_err_unlock; } - sa = vma->vm_start; -SG_LOG (3 , sfp,"%s: vm_start=0x%lx, off=%lu\n" , __func__, sa, offset); +SG_LOG (5 , sfp,"%s: vm_start=0x%lx, off=%lu\n" , __func__, + vma->vm_start, offset); length =1 << (PAGE_SHIFT + rsv_schp->page_order); -for (k =0 ; k < rsv_schp->num_sgat && sa < vma->vm_end; ++k) { - len = vma->vm_end - sa; - len =min_t (int, len, (int )length); -if (offset < len) { -struct page *page; -struct page *pp; - - pp = rsv_schp->pages[k]; -xa_unlock_irqrestore (&sfp->srp_arr, iflags); - page =nth_page (pp, offset >>PAGE_SHIFT ); -get_page (page);/* increment page count */ - vmf->page = page; -return 0 ;/* success */ - } - sa += len; - offset -= len; - } + k = (int )offset / length; + n = ((int )offset % length) >>PAGE_SHIFT ; + page =nth_page (rsv_schp->pages[k], n); +get_page (page); + vmf->page = page; + res =0 ; out_err_unlock: -xa_unlock_irqrestore (&sfp->srp_arr, iflags); +mutex_unlock (&sfp->f_mutex); out_err: -return VM_FAULT_SIGBUS ; +return res; }static const struct vm_operations_struct sg_mmap_vm_ops = { .fault = sg_vma_fault, + .open = sg_vma_open, + .close = sg_vma_close, }; -/* Entry point for mmap(2) system call */ +/* +* Entry point for mmap(2) system call. For mmap(2) to work, request's +* scatter gather list needs to be order 0 which it is unlikely to be +* by default. mmap(2) cannot be called more than once per fd. +*/ static int sg_mmap (struct file *filp,struct vm_area_struct *vma) { -int k, length; -int ret =0 ; -unsigned long req_sz, len, sa, iflags; -struct sg_scatter_hold *rsv_schp; +int res =0 ; +unsigned long req_sz;struct sg_fd *sfp;struct sg_request *srp;
@@ -2325,40 +2345,48 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma)pr_warn ("sg: %s: sfp is NULL\n" , __func__);return -ENXIO ; } +mutex_lock (&sfp->f_mutex); req_sz = vma->vm_end - vma->vm_start;SG_LOG (3 , sfp,"%s: vm_start=%pK, len=%d\n" , __func__, (void *)vma->vm_start, (int )req_sz); -if (vma->vm_pgoff) -return -EINVAL ;/* only an offset of 0 accepted */ +if (vma->vm_pgoff) { + res = -EINVAL ;/* only an offset of 0 accepted */ + goto fini; + }/* Check reserve request is inactive and has large enough buffer */ -mutex_lock (&sfp->f_mutex); srp = sfp->rsv_srp; -xa_lock_irqsave (&sfp->srp_arr, iflags);if (SG_RS_ACTIVE (srp)) { - ret = -EBUSY ; - goto out; + res = -EBUSY ; + goto fini; } - rsv_schp = &srp->sgat_h; -if (req_sz > (unsigned long )rsv_schp->buflen) { - ret = -ENOMEM ; - goto out; +if (req_sz >SG_WRITE_COUNT_LIMIT ) {/* sanity check */ + res = -ENOMEM ; + goto fini; } - sa = vma->vm_start; - length =1 << (PAGE_SHIFT + rsv_schp->page_order); -for (k =0 ; k < rsv_schp->num_sgat && sa < vma->vm_end; ++k) { - len = vma->vm_end - sa; - len = min_t(unsigned long, len, (unsigned long)length); - sa += len; +if (test_and_set_bit (SG_FFD_MMAP_CALLED , sfp->ffd_bm)) { +SG_LOG (1 , sfp,"%s: multiple invocations on this fd\n" , + __func__); + res = -EADDRINUSE ; + goto fini; + } +if (srp->sgat_h.page_order >0 || + req_sz > (unsigned long )srp->sgat_h.buflen) { +sg_remove_sgat (srp); +set_bit (SG_FRQ_FOR_MMAP , srp->frq_bm); + res =sg_mk_sgat (srp, sfp, req_sz); +if (res) { +SG_LOG (1 , sfp,"%s: sg_mk_sgat failed, wanted=%lu\n" , + __func__, req_sz); + goto fini; + } } - -set_bit (SG_FFD_MMAP_CALLED , sfp->ffd_bm); vma->vm_flags |=VM_IO |VM_DONTEXPAND |VM_DONTDUMP ; vma->vm_private_data = sfp; vma->vm_ops = &sg_mmap_vm_ops; -out: -xa_unlock_irqrestore (&sfp->srp_arr, iflags); +sg_vma_open (vma); +fini:mutex_unlock (&sfp->f_mutex); -return ret; +return res; } static void
@@ -2945,7 +2973,7 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) } scsi_rp->cmd_len = cwrp->cmd_len; srp->cmd_opcode = scsi_rp->cmd[0 ]; - us_xfer = !(srp->rq_flags &SG_FLAG_NO_DXFER ); + us_xfer = !(srp->rq_flags & (SG_FLAG_NO_DXFER |SG_FLAG_MMAP_IO ));assign_bit (SG_FRQ_NO_US_XFER , srp->frq_bm, !us_xfer); reserved = (sfp->rsv_srp == srp); rq->end_io_data = srp;
@@ -3084,6 +3112,7 @@ sg_mk_sgat(struct sg_request *srp, struct sg_fd *sfp, int minlen)gfp_t mask_kz =GFP_ATOMIC | __GFP_NOWARN;struct sg_device *sdp = sfp->parentdp;struct sg_scatter_hold *schp = &srp->sgat_h; +struct page **pgp;if (unlikely (minlen <=0 )) {if (minlen <0 )
@@ -3099,32 +3128,37 @@ sg_mk_sgat(struct sg_request *srp, struct sg_fd *sfp, int minlen)if (unlikely (!schp->pages))return -ENOMEM ; - elem_sz = sfp->sgat_elem_sz;/* power of 2 and >= PAGE_SIZE */ +/* elem_sz must be power of 2 and >= PAGE_SIZE */ + elem_sz =test_bit (SG_FRQ_FOR_MMAP , srp->frq_bm) ? (int )PAGE_SIZE : sfp->sgat_elem_sz;if (sdp &&unlikely (sdp->device->host->unchecked_isa_dma)) mask_ap |=GFP_DMA ; o_order =get_order (elem_sz); order = o_order; again: -for (k =0 , rem_sz = align_sz; rem_sz >0 && k < mx_sgat_elems; - ++k, rem_sz -= elem_sz) { - schp->pages[k] =alloc_pages (mask_ap, order); -if (!schp->pages[k]) +if (elem_sz * mx_sgat_elems < align_sz) {/* misfit ? */ +SG_LOG (1 , sfp,"%s: align_sz=%d too big\n" , __func__, + align_sz); + goto b4_alloc_pages; + } + rem_sz = align_sz; +for (pgp = schp->pages; rem_sz >0 ; ++pgp, rem_sz -= elem_sz) { + *pgp =alloc_pages (mask_ap, order); +if (unlikely (!*pgp)) goto err_out; -SG_LOG (5 , sfp,"%s: k=%d, order=%d [0x%pK ++]\n" , __func__, k, - order, schp->pages[k]); +SG_LOG (6 , sfp,"%s: elem_sz=%d [0x%pK ++]\n" , __func__, + elem_sz, *pgp); } + k = pgp - schp->pages; +SG_LOG (((order != o_order || rem_sz >0 ) ?2 :5 ), sfp, +"%s: num_sgat=%d, order=%d,%d rem_sz=%d\n" , __func__, k, + o_order, order, rem_sz); schp->page_order = order; schp->num_sgat = k; -SG_LOG (((order != o_order || rem_sz >0 ) ?2 :5 ), sfp, -"%s: num_sgat=%d, order=%d,%d\n" , __func__, k, o_order, order); -if (unlikely (rem_sz >0 )) {/* hit mx_sgat_elems */ - order =0 ;/* force exit */ - goto err_out; - } schp->buflen = align_sz;return 0 ; err_out: + k = pgp - schp->pages;for (j =0 ; j < k; ++j)__free_pages (schp->pages[j], order);
@@ -3132,6 +3166,7 @@ sg_mk_sgat(struct sg_request *srp, struct sg_fd *sfp, int minlen) elem_sz >>=1 ; goto again; } +b4_alloc_pages:kfree (schp->pages); schp->pages =NULL ;return -ENOMEM ;
@@ -3147,7 +3182,7 @@ sg_remove_sgat_helper(struct sg_fd *sfp, struct sg_scatter_hold *schp)return ;for (k =0 ; k < schp->num_sgat; ++k) { p = schp->pages[k]; -SG_LOG (5 , sfp,"%s: pg[%d]=0x%pK --\n" , __func__, k, p); +SG_LOG (6 , sfp,"%s: pg[%d]=0x%pK --\n" , __func__, k, p);if (unlikely (!p))continue ;__free_pages (p, schp->page_order);
@@ -3277,7 +3312,7 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id) }return NULL ; good: -SG_LOG (6 , sfp,"%s: %s%d found [srp=0x%pK]\n" , __func__,"pack_id=" , +SG_LOG (5 , sfp,"%s: %s%d found [srp=0x%pK]\n" , __func__,"pack_id=" , pack_id, srp);return srp; }
@@ -3473,7 +3508,7 @@ sg_setup_req(struct sg_comm_wr_t *cwrp, int dxfr_len) r_srp->parentfp = fp;SG_LOG (4 , fp,"%s: mk_new_srp=0x%pK ++\n" , __func__, r_srp); } - r_srp->frq_bm[0 ] = cwrp->frq_bm[0 ];/* assumes <= 32 req flags */ +WRITE_ONCE (r_srp->frq_bm[0 ], cwrp->frq_bm[0 ]);/* assumes <= 32 req flags */ r_srp->sgat_h.dlen = dxfr_len;/* must be <= r_srp->sgat_h.buflen */ r_srp->cmd_opcode =0xff ;/* set invalid opcode (VS), 0x0 is TUR */ fini:
@@ -3500,7 +3535,7 @@ sg_deact_request(struct sg_fd *sfp, struct sg_request *srp)return ; sbp = srp->sense_bp; srp->sense_bp =NULL ; - srp->frq_bm[0 ] =0 ; +WRITE_ONCE (srp->frq_bm[0 ],0 );sg_rq_state_chg (srp,0 ,SG_RS_INACTIVE , true/* force */ , __func__);/* maybe orphaned req, thus never read */ if (sbp)
Signature · 1 line
2.25.1
Douglas Gilbert
maintainerPATCH v13 37/45 sg: defang allow_dio
In reply to Douglas GilbertBefore direct IO was permitted by this driver, the user either had to give 'allow_dio=1' as a module parameter or write to procfs with 'echo 1 > /proc/scsi/sg/allow_dio'. The user also needs to set the SG_FLAG_DIRECT_IO flag in the flags field of either the sg v3 or v3 interface. The reason this "belts and braces" approach was taken is lost in the mists of time (done over 20 years ago). So this patch keeps the allow_dio attribute for backward compatibility but ignores it, relying on the SG_FLAG_DIRECT_IO flag alone. This brings the use of the SG_FLAG_DIRECT_IO flag into line with the SG_FLAG_MMAP_IO flag; the two mechanisms are no more, or less, safe than one another in recent Linux kernels.
--- drivers/scsi/sg.c | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-)
drivers/scsi/sg.c +8 -18
@@ -142,7 +142,7 @@ int sg_big_buff = SG_DEF_RESERVED_SIZE;* not enough memory) will be reserved for use by this file descriptor. */ static int def_reserved_size =-1 ;/* picks up init parameter */ -static int sg_allow_dio =SG_ALLOW_DIO_DEF ; +static int sg_allow_dio =SG_ALLOW_DIO_DEF ;/* ignored by code */ static int scatter_elem_sz =SG_SCATTER_SZ ;
@@ -2862,19 +2862,6 @@ exit_sg(void)idr_destroy (&sg_index_idr); } -static inline bool -sg_chk_dio_allowed (struct sg_device *sdp,struct sg_request *srp, -int iov_count,int dir) -{ -if (sg_allow_dio && (srp->rq_flags &SG_FLAG_DIRECT_IO )) { -if (dir !=SG_DXFER_UNKNOWN && !iov_count) { -if (!sdp->device->host->unchecked_isa_dma) -return true; - } - } -return false; -} -static inline void sg_set_map_data (const struct sg_scatter_hold *schp,bool up_valid,struct rq_map_data *mdp)
@@ -2893,6 +2880,7 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir)int res =0 ;int dxfer_len =0 ;int r0w =READ ; +u32 rq_flags = srp->rq_flags;unsigned int iov_count =0 ;void __user *up;struct request *rq;
@@ -2973,7 +2961,7 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) } scsi_rp->cmd_len = cwrp->cmd_len; srp->cmd_opcode = scsi_rp->cmd[0 ]; - us_xfer = !(srp->rq_flags & (SG_FLAG_NO_DXFER |SG_FLAG_MMAP_IO )); + us_xfer = !(rq_flags & (SG_FLAG_NO_DXFER |SG_FLAG_MMAP_IO ));assign_bit (SG_FRQ_NO_US_XFER , srp->frq_bm, !us_xfer); reserved = (sfp->rsv_srp == srp); rq->end_io_data = srp;
@@ -2984,7 +2972,8 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir)SG_LOG (4 , sfp,"%s: no data xfer [0x%pK]\n" , __func__, srp);set_bit (SG_FRQ_NO_US_XFER , srp->frq_bm); goto fini;/* path of reqs with no din nor dout */ - }else if (sg_chk_dio_allowed (sdp, srp, iov_count, dxfer_dir) && + }else if ((rq_flags &SG_FLAG_DIRECT_IO ) && iov_count ==0 && + !sdp->device->host->unchecked_isa_dma &&blk_rq_aligned (q, (unsigned long )up, dxfer_len)) { srp->rq_info |=SG_INFO_DIRECT_IO ; md =NULL ;
@@ -2995,7 +2984,7 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) }if (likely (md)) {/* normal, "indirect" IO */ -if (unlikely (srp->rq_flags &SG_FLAG_MMAP_IO )) { +if (unlikely (rq_flags &SG_FLAG_MMAP_IO )) {/* mmap IO must use and fit in reserve request */ if (!reserved || dxfer_len > req_schp->buflen) res = reserved ? -ENOMEM : -EBUSY ;
@@ -3886,6 +3875,7 @@ sg_proc_single_open_adio(struct inode *inode, struct file *filp)return single_open (filp, sg_proc_seq_show_int, &sg_allow_dio); } +/* Kept for backward compatibility. sg_allow_dio is now ignored. */ static ssize_t sg_proc_write_adio (struct file *filp,const char __user *buffer,size_t count,loff_t *off)
@@ -4506,6 +4496,6 @@ MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);MODULE_PARM_DESC (scatter_elem_sz,"scatter gather element size (default: max(SG_SCATTER_SZ, PAGE_SIZE))" );MODULE_PARM_DESC (def_reserved_size,"size of buffer reserved for each fd" ); -MODULE_PARM_DESC (allow_dio,"allow direct I/O (default: 0 (disallow))" ); +MODULE_PARM_DESC (allow_dio,"allow direct I/O (default: 0 (disallow)); now ignored" );module_init (init_sg);module_exit (exit_sg);
Signature · 1 line
2.25.1
Re: [PATCH v13 37/45] sg: defang allow_dio
Quoted 13 lines · Douglas Gilbert
Before direct IO was permitted by this driver, the user either had to give 'allow_dio=1' as a module parameter or write to procfs with 'echo 1 >
/proc/scsi/sg/allow_dio'. The user also needs to set theSG_FLAG_DIRECT_IOflag in the flags field of either the sg v3 or v3 interface. The reason this "belts and braces" approach was taken is lost in the mists of time (done over 20 years ago). So this patch keeps theallow_dioattribute for backward compatibility but ignores it, relying on theSG_FLAG_DIRECT_IOflag alone. This brings the use of theSG_FLAG_DIRECT_IOflag into line with theSG_FLAG_MMAP_IOflag; the two mechanisms are no more, or less, safe than one another in recent Linux kernels.
Quoted 5 lines
Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerPATCH v13 38/45 sg: warn v3 write system call users
In reply to Douglas GilbertShould generate one log message per kernel run when the write() system call is used with the sg interface version 3. Due to security concerns suggest that they use ioctl(SG_SUBMIT_v3) instead.
Sg interface version 1 or 2 based code may also be calling write() in this context. There is no easy solution for them (short of upgrading their interface to version 3 or 4), so don't produce a warning suggesting the conversion will be simple.
--- drivers/scsi/sg.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/scsi/sg.c +3 -0
@@ -645,6 +645,9 @@ sg_write(struct file *filp, const char __user *p, size_t count, loff_t *ppos) __func__);return -EPERM ; } +pr_warn_once ("Please use %s instead of write(),\n%s\n" , +"ioctl(SG_SUBMIT_V3)" , +" See: http://sg.danny.cz/sg/sg_v40.html" ); res =sg_v3_submit (filp, sfp, h3p, false,NULL );return res <0 ? res : (int )count; }
Signature · 1 line
2.25.1
Douglas Gilbert
maintainerPATCH v13 39/45 sg: add mmap_sz tracking
In reply to Douglas GilbertTrack mmap_sz from prior mmap(2) call, per sg file descriptor. Also reset this value whenever munmap(2) is called. Fail SG_FLAG_MMAP_IO uses if mmap(2) hasn't been called or the memory associated with it is not large enough for the current request.
--- drivers/scsi/sg.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/scsi/sg.c +9 -2
@@ -232,6 +232,7 @@ struct sg_fd { /* holds the state of a file descriptor */atomic_t waiting;/* number of requests awaiting receive */ atomic_t req_cnt;/* number of requests */ int sgat_elem_sz;/* initialized to scatter_elem_sz */ +int mmap_sz;/* byte size of previous mmap() call */ unsigned long ffd_bm[1 ];/* see SG_FFD_* defines above */ pid_t tid;/* thread id when opened */ u8 next_cmd_len;/* 0: automatic, >0: use on next write() */
@@ -725,10 +726,14 @@ sg_write(struct file *filp, const char __user *p, size_t count, loff_t *ppos)static inline int sg_chk_mmap (struct sg_fd *sfp,int rq_flags,int len) { +if (!test_bit (SG_FFD_MMAP_CALLED , sfp->ffd_bm)) +return -EBADFD ;if (atomic_read (&sfp->submitted) >0 )return -EBUSY ;/* already active requests on fd */ if (len > sfp->rsv_srp->sgat_h.buflen)return -ENOMEM ;/* MMAP_IO size must fit in reserve */ +if (unlikely (len > sfp->mmap_sz)) +return -ENOMEM ;/* MMAP_IO size can't exceed mmap() size */ if (rq_flags &SG_FLAG_DIRECT_IO )return -EINVAL ;/* not both MMAP_IO and DIRECT_IO */ return 0 ;
@@ -2262,6 +2267,8 @@ sg_vma_close(struct vm_area_struct *vma)pr_warn ("%s: sfp null\n" , __func__);return ; } + sfp->mmap_sz =0 ; +clear_bit (SG_FFD_MMAP_CALLED , sfp->ffd_bm);kref_put (&sfp->f_ref, sg_remove_sfp);/* get in: sg_vma_open() */ }
@@ -2383,6 +2390,7 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma) goto fini; } } + sfp->mmap_sz = req_sz; vma->vm_flags |=VM_IO |VM_DONTEXPAND |VM_DONTDUMP ; vma->vm_private_data = sfp; vma->vm_ops = &sg_mmap_vm_ops;
@@ -4048,8 +4056,7 @@ sg_proc_debug_fd(struct sg_fd *fp, char *obp, int len, unsigned long idx) (int )test_bit (SG_FFD_FORCE_PACKID , fp->ffd_bm), (int )test_bit (SG_FFD_KEEP_ORPHAN , fp->ffd_bm), fp->ffd_bm[0]); - n +=scnprintf (obp + n, len - n," mmap_called=%d\n" , -test_bit (SG_FFD_MMAP_CALLED , fp->ffd_bm)); + n +=scnprintf (obp + n, len - n," mmap_sz=%d\n" , fp->mmap_sz); n += scnprintf(obp + n, len - n, " submitted=%d waiting=%d open thr_id=%d\n", atomic_read(&fp->submitted),
Signature · 1 line
2.25.1
Re: [PATCH v13 39/45] sg: add mmap_sz tracking
Quoted 56 lines · Douglas Gilbert
Track
mmap_szfrom prior mmap(2) call, per sg file descriptor. Also reset this value whenever munmap(2) is called. FailSG_FLAG_MMAP_IOuses if mmap(2) hasn't been called or the memory associated with it is not large enough for the current request.Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)drivers/scsi/sg.c +8
-0@@ -232,6 +232,7 @@ struct sg_fd { /* holds the state of a file descriptor */atomic_t waiting;/* number of requests awaiting receive */ atomic_t req_cnt;/* number of requests */ int sgat_elem_sz;/* initialized to scatter_elem_sz */ +int mmap_sz;/* byte size of previous mmap() call */ unsigned long ffd_bm[1 ];/* see SG_FFD_* defines above */ pid_t tid;/* thread id when opened */ u8 next_cmd_len;/* 0: automatic, >0: use on next write() */ @@ -725,10 +726,14 @@ sg_write(struct file *filp, const char __user *p, size_t count, loff_t *ppos)static inline int sg_chk_mmap (struct sg_fd *sfp,int rq_flags,int len) { +if (!test_bit (SG_FFD_MMAP_CALLED , sfp->ffd_bm)) +return -EBADFD ;if (atomic_read (&sfp->submitted) >0 )return -EBUSY ;/* already active requests on fd */ if (len > sfp->rsv_srp->sgat_h.buflen)return -ENOMEM ;/* MMAP_IO size must fit in reserve */ +if (unlikely (len > sfp->mmap_sz)) +return -ENOMEM ;/* MMAP_IO size can't exceed mmap() size */ if (rq_flags &SG_FLAG_DIRECT_IO )return -EINVAL ;/* not both MMAP_IO and DIRECT_IO */ return 0 ;@@ -2262,6 +2267,8 @@ sg_vma_close(struct vm_area_struct *vma)pr_warn ("%s: sfp null\n" , __func__);return ; } + sfp->mmap_sz =0 ; +clear_bit (SG_FFD_MMAP_CALLED , sfp->ffd_bm);kref_put (&sfp->f_ref, sg_remove_sfp);/* get in: sg_vma_open() */ }@@ -2383,6 +2390,7 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma) goto fini; } } + sfp->mmap_sz = req_sz; vma->vm_flags |=VM_IO |VM_DONTEXPAND |VM_DONTDUMP ; vma->vm_private_data = sfp; vma->vm_ops = &sg_mmap_vm_ops;
drivers/scsi/sg.c sg_proc_debug_fd(struct sg_fd *fp, char *obp, int len, unsigned long idx) · line 4062
Quoted 9 lines
drivers/scsi/sg.c +1
-2@@ -4048,8 +4056,7 @@ sg_proc_debug_fd(struct sg_fd *fp, char *obp, int len, unsigned long idx) (int )test_bit (SG_FFD_FORCE_PACKID , fp->ffd_bm), (int )test_bit (SG_FFD_KEEP_ORPHAN , fp->ffd_bm), fp->ffd_bm[0]); - n +=scnprintf (obp + n, len - n," mmap_called=%d\n" , -test_bit (SG_FFD_MMAP_CALLED , fp->ffd_bm)); + n +=scnprintf (obp + n, len - n," mmap_sz=%d\n" , fp->mmap_sz); n += scnprintf(obp + n, len - n, " submitted=%d waiting=%d open thr_id=%d\n", atomic_read(&fp->submitted),
What is the point of having the SG_FFD_MMAP_CALLED bit now? Doesn't the new 'mmap_sz' value carry the same information?
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerRe: [PATCH v13 39/45] sg: add mmap_sz tracking
Quoted 62 lines · Hannes Reinecke
Quoted 61 lines · Douglas Gilbert
Track
mmap_szfrom prior mmap(2) call, per sg file descriptor. Also reset this value whenever munmap(2) is called. FailSG_FLAG_MMAP_IOuses if mmap(2) hasn't been called or the memory associated with it is not large enough for the current request.Signed-off-by Douglas Gilbert <[email protected]>---
drivers/scsi/sg.c| 11 +++++++++--1 file changed, 9 insertions(+), 2 deletions(-)drivers/scsi/sg.c +0
-0@@ -232,6 +232,7 @@ struct sg_fd { /* holds the state of a filedescriptor */
atomic_twaiting; /* number of requests awaiting receive */atomic_treq_cnt; /* number of requests */
int sgat_elem_sz; /* initialized toscatter_elem_sz*/ + int mmap_sz; /* byte size of previousmmap()call */ unsigned long ffd_bm[1]; /* see SG_FFD_* defines above */pid_ttid; /* thread id when opened */
u8 next_cmd_len; /* 0: automatic, >0: use on nextwrite()*/drivers/scsi/sg.c +0
-0@@ -725,10 +726,14 @@ sg_write(struct file *filp, const char __user *p, size_t count, loff_t *ppos)static inline int
sg_chk_mmap(struct sg_fd *sfp, int rq_flags, int len)
{
+ if (!test_bit(SG_FFD_MMAP_CALLED,sfp->ffd_bm))
+ return-EBADFD;
if (atomic_read(&sfp->submitted)> 0)
return -EBUSY; /* already active requests on fd */ if (len >sfp->rsv_srp->sgat_h.buflen)
return-ENOMEM; /*MMAP_IOsize must fit in reserve */ + if (unlikely(len > sfp->mmap_sz))
+ return-ENOMEM; /*MMAP_IOsize can't exceedmmap()size */ if (rq_flags&SG_FLAG_DIRECT_IO)
return-EINVAL; /* not bothMMAP_IOandDIRECT_IO*/ return 0;drivers/scsi/sg.c +2
-0@@ -2262,6 +2267,8 @@ sg_vma_close(struct vm_area_struct *vma) pr_warn("%s: sfp null\n", __func__); return; } + sfp->mmap_sz = 0; + clear_bit(SG_FFD_MMAP_CALLED, sfp->ffd_bm);
kref_put(&sfp->f_ref, sg_remove_sfp); /* get in:sg_vma_open()*/ }drivers/scsi/sg.c +1
-1@@ -2383,6 +2390,7 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma) goto fini; } } + sfp->mmap_sz = req_sz; vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP; vma->vm_private_data = sfp; vma->vm_ops = &sg_mmap_vm_ops;@@ -4048,8 +4056,7 @@ sg_proc_debug_fd(struct sg_fd *fp, char *obp, int len, unsigned long idx) (int)test_bit(SG_FFD_FORCE_PACKID, fp->ffd_bm), (int)test_bit(SG_FFD_KEEP_ORPHAN, fp->ffd_bm), fp->ffd_bm[0]); - n += scnprintf(obp + n, len - n, " mmap_called=%d\n",
Quoted 8 lines
Quoted 6 lines
drivers/scsi/sg.c +1
-1- test_bit(SG_FFD_MMAP_CALLED, fp->ffd_bm)); + n += scnprintf(obp + n, len - n, " mmap_sz=%d\n", fp->mmap_sz); n += scnprintf(obp + n, len - n, " submitted=%d waiting=%d open thr_id=%d\n",
atomic_read(&fp->submitted),What is the point of having the
SG_FFD_MMAP_CALLEDbit now? Doesn't the new 'mmap_sz' value carry the same information?
Touché! Will fix.
Doug Gilbert
Douglas Gilbert
maintainerPATCH v13 40/45 sg: remove rcv_done request state
In reply to Douglas GilbertRemove SG_RQ_RCV_DONE request state. Also remember the position of the last used request array element and start subsequent searches for completed requests and new requests from that index.
--- drivers/scsi/sg.c | 168 ++++++++++++++++++++++++++-------------------- 1 file changed, 96 insertions(+), 72 deletions(-)
drivers/scsi/sg.c +96 -72
@@ -90,7 +90,6 @@ enum sg_rq_state { /* N.B. sg_rq_state_arr assumes SG_RS_AWAIT_RCV==2 */SG_RS_INACTIVE =0 ,/* request not in use (e.g. on fl) */ SG_RS_INFLIGHT ,/* active: cmd/req issued, no response yet */ SG_RS_AWAIT_RCV ,/* have response from LLD, awaiting receive */ -SG_RS_RCV_DONE ,/* receive is ongoing or done */ SG_RS_BUSY, /* temporary state should rarely be seen */ };
@@ -227,6 +226,7 @@ struct sg_fd { /* holds the state of a file descriptor */struct mutex f_mutex;/* serialize ioctls on this fd */ int timeout;/* defaults to SG_DEFAULT_TIMEOUT */ int timeout_user;/* defaults to SG_DEFAULT_TIMEOUT_USER */ +int prev_used_idx;/* previous used index */ u32 idx;/* my index within parent's sfp_arr */ atomic_t submitted;/* number inflight or awaiting receive */ atomic_t waiting;/* number of requests awaiting receive */
@@ -299,7 +299,9 @@ static struct sg_device *sg_get_dev(int min_dev);static void sg_device_destroy (struct kref *kref);static struct sg_request *sg_mk_srp_sgat (struct sg_fd *sfp,bool first,int db_len); +#if IS_ENABLED (CONFIG_SCSI_LOGGING ) &&IS_ENABLED (SG_DEBUG )static const char *sg_rq_st_str (enum sg_rq_state rq_st,bool long_str); +#endif #define SG_WRITE_COUNT_LIMIT (32 * 1024 * 1024)
@@ -873,20 +875,18 @@ sg_ctl_iosubmit_v3(struct file *filp, struct sg_fd *sfp, void __user *p)#if IS_ENABLED (SG_LOG_ACTIVE )static void sg_rq_state_fail_msg (struct sg_fd *sfp,enum sg_rq_state exp_old_st, -enum sg_rq_state want_st,enum sg_rq_state act_old_st, -const char *fromp) +enum sg_rq_state want_st,const char *fromp) { -const char *eaw_rs ="expected_old,actual_old,wanted rq_st" ; +const char *eaw_rs ="expected_old,wanted rq_st" ;if (IS_ENABLED (CONFIG_SCSI_PROC_FS )) -SG_LOG (1 , sfp,"%s: %s: %s: %s,%s,%s\n" , +SG_LOG (1 , sfp,"%s: %s: %s,%s,%s\n" , __func__, fromp, eaw_rs,sg_rq_st_str (exp_old_st, false), -sg_rq_st_str (act_old_st, false),sg_rq_st_str (want_st, false));else -pr_info ("sg: %s: %s: %s: %d,%d,%d\n" , __func__, fromp, eaw_rs, - (int )exp_old_st, (int )act_old_st, (int )want_st); +pr_info ("sg: %s: %s: %s: %d,%d\n" , __func__, fromp, eaw_rs, + (int )exp_old_st, (int )want_st); }#endif
@@ -930,8 +930,8 @@ sg_rq_state_helper(struct xarray *xafp, struct sg_request *srp, int indic) }/* Following array indexed by enum sg_rq_state, 0 means no xa mark change */ -static const int sg_rq_state_arr[] = {1 ,0 ,4 ,0 ,0 }; -static const int sg_rq_state_mul2arr[] = {2 ,0 ,8 ,0 ,0 }; +static const int sg_rq_state_arr[] = {1 ,0 ,4 ,0 }; +static const int sg_rq_state_mul2arr[] = {2 ,0 ,8 ,0 };/* * This function keeps the srp->rq_st state and associated marks on the
@@ -946,39 +946,47 @@ static const int sg_rq_state_mul2arr[] = {2, 0, 8, 0, 0};*/ static int sg_rq_state_chg (struct sg_request *srp,enum sg_rq_state old_st, -enum sg_rq_state new_st,bool force,const char *fromp) +enum sg_rq_state new_st) {enum sg_rq_state act_old_st;int indic;unsigned long iflags; -struct xarray *xafp = &srp->parentfp->srp_arr; +struct sg_fd *sfp = srp->parentfp; +struct xarray *xafp = &sfp->srp_arr; -if (force) { -xa_lock_irqsave (xafp, iflags); -sg_rq_state_force (srp, new_st); -xa_unlock_irqrestore (xafp, iflags); -return 0 ; - } indic = sg_rq_state_arr[(int )old_st] + sg_rq_state_mul2arr[(int )new_st]; act_old_st = (enum sg_rq_state )atomic_cmpxchg (&srp->rq_st, old_st, new_st);if (act_old_st != old_st) { -#if IS_ENABLED (SG_LOG_ACTIVE ) -if (fromp) -sg_rq_state_fail_msg (srp->parentfp, old_st, new_st, - act_old_st, fromp); -#endif +SG_LOG (1 , sfp,"%s: unexpected old state: %s\n" , __func__, +sg_rq_st_str (act_old_st, false));return -EPROTOTYPE ;/* only used for this error type */ }if (indic) {xa_lock_irqsave (xafp, iflags); +if (new_st ==SG_RS_INACTIVE ) +WRITE_ONCE (sfp->prev_used_idx, srp->rq_idx);sg_rq_state_helper (xafp, srp, indic);xa_unlock_irqrestore (xafp, iflags); }return 0 ; } +static void +sg_rq_state_chg_force (struct sg_request *srp,enum sg_rq_state new_st) +{ +unsigned long iflags; +struct sg_fd *sfp = srp->parentfp; +struct xarray *xafp = &sfp->srp_arr; + +xa_lock_irqsave (xafp, iflags); +if (new_st ==SG_RS_INACTIVE ) +WRITE_ONCE (sfp->prev_used_idx, srp->rq_idx); +sg_rq_state_force (srp, new_st); +xa_unlock_irqrestore (xafp, iflags); +} +static void sg_execute_cmd (struct sg_fd *sfp,struct sg_request *srp) {
@@ -1000,8 +1008,7 @@ sg_execute_cmd(struct sg_fd *sfp, struct sg_request *srp) at_head = !(srp->rq_flags &SG_FLAG_Q_AT_TAIL );kref_get (&sfp->f_ref);/* sg_rq_end_io() does kref_put(). */ -sg_rq_state_chg (srp,SG_RS_BUSY /* ignored */ ,SG_RS_INFLIGHT , - true, __func__); +sg_rq_state_chg_force (srp,SG_RS_INFLIGHT );/* >>>>>>> send cmd/req off to other levels <<<<<<<< */ if (!sync)
@@ -1213,10 +1220,6 @@ sg_receive_v3(struct sg_fd *sfp, struct sg_request *srp, size_t count, hp->driver_status =driver_byte (rq_result); err2 =put_sg_io_hdr (hp, p); err = err ? err : err2; - err2 =sg_rq_state_chg (srp,atomic_read (&srp->rq_st),SG_RS_RCV_DONE , - false, __func__); -if (err2) - err = err ? err : err2; err_out:sg_finish_scsi_blk_rq (srp);sg_deact_request (sfp, srp);
@@ -1227,7 +1230,7 @@ static int sg_receive_v4(struct sg_fd *sfp, struct sg_request *srp, void __user *p, struct sg_io_v4 *h4p) { - int err, err2; + int err;u32 rq_result = srp->rq_result; SG_LOG(3, sfp, "%s: p=%s, h4p=%s\n", __func__,
@@ -1262,10 +1265,6 @@ sg_receive_v4(struct sg_fd *sfp, struct sg_request *srp, void __user *p,if (copy_to_user (p, h4p,SZ_SG_IO_V4 )) err = err ? err : -EFAULT ; } - err2 =sg_rq_state_chg (srp,atomic_read (&srp->rq_st),SG_RS_RCV_DONE , - false, __func__); -if (err2) - err = err ? err : err2;sg_finish_scsi_blk_rq (srp);sg_deact_request (sfp, srp);return err <0 ? err :0 ;
@@ -1460,7 +1459,6 @@ sg_read_v1v2(void __user *buf, int count, struct sg_fd *sfp, res = (h2p->result ==0 ) ?0 : -EIO ; } fini: -atomic_set (&srp->rq_st,SG_RS_RCV_DONE );sg_finish_scsi_blk_rq (srp);sg_deact_request (sfp, srp);return res;
@@ -1634,7 +1632,6 @@ sg_get_dur(struct sg_request *srp, const enum sg_rq_state *sr_stp, res =sg_calc_rq_dur (srp);break ;case SG_RS_AWAIT_RCV : -case SG_RS_RCV_DONE :case SG_RS_INACTIVE : res = srp->duration; is_dur = true;/* completion has occurred, timing finished */
@@ -1705,9 +1702,13 @@ sg_wait_event_srp(struct file *filp, struct sg_fd *sfp, void __user *p, sr_st =atomic_read (&srp->rq_st);if (unlikely (sr_st !=SG_RS_AWAIT_RCV ))return -EPROTO ;/* Logic error */ - res =sg_rq_state_chg (srp, sr_st,SG_RS_BUSY , false, __func__); -if (unlikely (res)) + res =sg_rq_state_chg (srp, sr_st,SG_RS_BUSY ); +if (unlikely (res)) { +#if IS_ENABLED (SG_LOG_ACTIVE ) +sg_rq_state_fail_msg (sfp, sr_st,SG_RS_BUSY , __func__); +#endif return res; + }if (test_bit (SG_FRQ_IS_V4I , srp->frq_bm)) res =sg_receive_v4 (sfp, srp, p, h4p);else
@@ -2553,8 +2554,7 @@ sg_rq_end_io(struct request *rq, blk_status_t status) }if (!test_bit (SG_FRQ_SYNC_INVOC , srp->frq_bm))atomic_inc (&sfp->waiting); -if (unlikely (sg_rq_state_chg (srp,SG_RS_INFLIGHT , rqq_state, - false, __func__))) +if (unlikely (sg_rq_state_chg (srp,SG_RS_INFLIGHT , rqq_state)))pr_warn ("%s: can't set rq_st\n" , __func__);/* * Free the mid-level resources apart from the bio (if any). The bio's
@@ -3261,9 +3261,10 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id)__maybe_unused bool is_bad_st = false;__maybe_unused enum sg_rq_state bad_sr_st =SG_RS_INACTIVE ;bool search_for_1 = (pack_id !=SG_PACK_ID_WILDCARD ); +bool second = false;int res;int num_waiting =atomic_read (&sfp->waiting); -unsigned long idx; +unsigned long idx, start_idx, end_idx;struct sg_request *srp =NULL ;struct xarray *xafp = &sfp->srp_arr;
@@ -3275,8 +3276,7 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id)continue ;if (srp->pack_id != pack_id)continue ; - res =sg_rq_state_chg (srp,SG_RS_AWAIT_RCV ,SG_RS_BUSY , - false, __func__); + res =sg_rq_state_chg (srp,SG_RS_AWAIT_RCV ,SG_RS_BUSY );if (likely (res ==0 )) goto good;/* else another caller got it, move on */
@@ -3286,14 +3286,35 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id) }break ; } - }else {/* search for any request is more likely */ -xa_for_each_marked (xafp, idx, srp,SG_XA_RQ_AWAIT ) { -if (!srp) -continue ; - res =sg_rq_state_chg (srp,SG_RS_AWAIT_RCV ,SG_RS_BUSY , - false, __func__); + }else { +/* +* Searching for _any_ request is the more likely usage. Start searching with the +* last xarray index that was used. In the case of a large-ish IO depth, it is +* likely that the second (relative) position will be the request we want, if it +* is ready. If there is no queuing and the "last used" has been re-used then the +* first (relative) position will be the request we want. +*/ + start_idx =READ_ONCE (sfp->prev_used_idx); + end_idx =ULONG_MAX ; +second_time: + idx = start_idx; +for (srp =xa_find (xafp, &idx, end_idx,SG_XA_RQ_AWAIT ); + srp; + srp =xa_find_after (xafp, &idx, end_idx,SG_XA_RQ_AWAIT )) { + res =sg_rq_state_chg (srp,SG_RS_AWAIT_RCV ,SG_RS_BUSY );if (likely (res ==0 )) goto good; +#if IS_ENABLED (SG_LOG_ACTIVE ) + else +sg_rq_state_fail_msg (sfp ,SG_RS_AWAIT_RCV ,SG_RS_BUSY ,__func__ ); +#endif + } +/* If not found so far, need to wrap around and search [0 ... start_idx) */ +if (!srp && !second && start_idx >0 ) { + end_idx = start_idx -1 ; + start_idx =0 ; + second = true; + goto second_time; } }/* here if one of above loops does _not_ find a match */
@@ -3411,8 +3432,9 @@ sg_setup_req(struct sg_comm_wr_t *cwrp, int dxfr_len)bool found = false;bool mk_new_srp = true;bool try_harder = false; +bool second = false;int num_inactive =0 ; -unsigned long idx, last_idx, iflags; +unsigned long idx, start_idx, end_idx, iflags;struct sg_fd *fp = cwrp->sfp;struct sg_request *r_srp =NULL ;/* request to return */ struct sg_request *last_srp =NULL ;
@@ -3425,45 +3447,48 @@ sg_setup_req(struct sg_comm_wr_t *cwrp, int dxfr_len) act_empty = true; mk_new_srp = true; }else if (!try_harder && dxfr_len <SG_DEF_SECTOR_SZ ) { - last_idx = ~0UL ;xa_for_each_marked (xafp, idx, r_srp,SG_XA_RQ_INACTIVE ) { -if (!r_srp) -continue ; ++num_inactive; -if (dxfr_len <SG_DEF_SECTOR_SZ ) { - last_idx = idx; +if (dxfr_len <SG_DEF_SECTOR_SZ ) last_srp = r_srp; -continue ; - } }/* If dxfr_len is small, use last inactive request */ -if (last_idx != ~0UL && last_srp) { +if (last_srp) { r_srp = last_srp; -if (sg_rq_state_chg (r_srp,SG_RS_INACTIVE ,SG_RS_BUSY , - false, __func__)) +if (sg_rq_state_chg (r_srp,SG_RS_INACTIVE ,SG_RS_BUSY )) goto start_again;/* gone to another thread */ cp ="toward end of srp_arr" ; found = true; } }else { -xa_for_each_marked (xafp, idx, r_srp,SG_XA_RQ_INACTIVE ) { -if (!r_srp) -continue ; + start_idx =READ_ONCE (fp->prev_used_idx); + end_idx =ULONG_MAX ; +second_time: + idx = start_idx; +for (r_srp =xa_find (xafp, &idx, end_idx,SG_XA_RQ_INACTIVE ); + r_srp; + r_srp =xa_find_after (xafp, &idx, end_idx,SG_XA_RQ_INACTIVE )) {if (r_srp->sgat_h.buflen >= dxfr_len) { -if (sg_rq_state_chg - (r_srp,SG_RS_INACTIVE ,SG_RS_BUSY , - false, __func__)) +if (sg_rq_state_chg (r_srp,SG_RS_INACTIVE ,SG_RS_BUSY ))continue ; - cp ="from front of srp_arr" ; + cp ="near front of srp_arr" ; found = true;break ; } } +/* If not found so far, need to wrap around and search [0 ... start_idx) */ +if (!r_srp && !second && start_idx >0 ) { + end_idx = start_idx -1 ; + start_idx =0 ; + second = true; + goto second_time; + } }if (found) { r_srp->in_resid =0 ; r_srp->rq_info =0 ; r_srp->sense_len =0 ; +WRITE_ONCE (fp->prev_used_idx, r_srp->rq_idx); mk_new_srp = false; }else { mk_new_srp = true;
@@ -3536,7 +3561,7 @@ sg_deact_request(struct sg_fd *sfp, struct sg_request *srp) sbp = srp->sense_bp; srp->sense_bp =NULL ;WRITE_ONCE (srp->frq_bm[0 ],0 ); -sg_rq_state_chg (srp,0 ,SG_RS_INACTIVE , true/* force */ , __func__); +sg_rq_state_chg_force (srp,SG_RS_INACTIVE );/* maybe orphaned req, thus never read */ if (sbp)mempool_free (sbp, sg_sense_pool);
@@ -3627,7 +3652,7 @@ sg_add_sfp(struct sg_device *sdp) } srp->rq_idx = idx; srp->parentfp = sfp; -sg_rq_state_chg (srp,0 ,SG_RS_INACTIVE , true, __func__); +sg_rq_state_chg_force (srp,SG_RS_INACTIVE ); }if (!reduced) { SG_LOG(4, sfp, "%s: built reserve buflen=%d\n", __func__,
@@ -3778,8 +3803,6 @@ sg_rq_st_str(enum sg_rq_state rq_st, bool long_str)return long_str ?"inflight" :"act" ;case SG_RS_AWAIT_RCV :return long_str ?"await_receive" :"rcv" ; -case SG_RS_RCV_DONE : -return long_str ?"receive_done" :"fin" ;case SG_RS_BUSY :return long_str ?"busy" :"bsy" ;default :
@@ -4056,7 +4079,8 @@ sg_proc_debug_fd(struct sg_fd *fp, char *obp, int len, unsigned long idx) (int )test_bit (SG_FFD_FORCE_PACKID , fp->ffd_bm), (int )test_bit (SG_FFD_KEEP_ORPHAN , fp->ffd_bm), fp->ffd_bm[0]); - n +=scnprintf (obp + n, len - n," mmap_sz=%d\n" , fp->mmap_sz); + n +=scnprintf (obp + n, len - n," mmap_sz=%d prev_used_idx=%d\n" , + fp->mmap_sz, fp->prev_used_idx); n += scnprintf(obp + n, len - n, " submitted=%d waiting=%d open thr_id=%d\n", atomic_read(&fp->submitted),
Signature · 1 line
2.25.1
Re: [PATCH v13 40/45] sg: remove rcv_done request state
Quoted 9 lines · Douglas Gilbert
Remove
SG_RQ_RCV_DONErequest state. Also remember the position of the last used request array element and start subsequent searches for completed requests and new requests from that index.Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 168 ++++++++++++++++++++++++++-------------------- 1 file changed, 96 insertions(+), 72 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerPATCH v13 41/45 sg: track lowest inactive and await indexes
In reply to Douglas GilbertUse two integers in the sg_fd structure to track recent and lowest xarray indexes that have become inactive or await a foreground receive. This is used to shorten the number of xarray iterations required prior to a match when queue (IO) depths are large, say 128.
Replace the req_cnt atomic in struct sg_fd with the inactives atomic. With large queues cycles were wasted checking the request xarray for any inactives when there were none to be found.
Rename the sg_rq_state_chg_*() functions to sg_rq_chg_state_*() since too many things start with "sg_rq_state". Also the new function names emphasize the change part a little more.
--- drivers/scsi/sg.c | 304 ++++++++++++++++++++++++++-------------------- 1 file changed, 175 insertions(+), 129 deletions(-)
drivers/scsi/sg.c +175 -129
@@ -116,6 +116,7 @@ enum sg_rq_state { /* N.B. sg_rq_state_arr assumes SG_RS_AWAIT_RCV==2 */#define SG_FRQ_RECEIVING 7/* guard against multiple receivers */ #define SG_FRQ_BLK_PUT_REQ 8/* set when blk_put_request() called */ #define SG_FRQ_FOR_MMAP 9/* request needs PAGE_SIZE elements */ +#define SG_FRQ_COUNT_ACTIVE 10/* sfp->submitted + waiting active */ /* Bit positions (flags) for sg_fd::ffd_bm bitmask follow */ #define SG_FFD_FORCE_PACKID 0/* receive only given pack_id/tag */
@@ -226,11 +227,12 @@ struct sg_fd { /* holds the state of a file descriptor */struct mutex f_mutex;/* serialize ioctls on this fd */ int timeout;/* defaults to SG_DEFAULT_TIMEOUT */ int timeout_user;/* defaults to SG_DEFAULT_TIMEOUT_USER */ -int prev_used_idx;/* previous used index */ +int low_used_idx;/* previous or lower used index */ +int low_await_idx;/* previous or lower await index */ u32 idx;/* my index within parent's sfp_arr */ atomic_t submitted;/* number inflight or awaiting receive */ atomic_t waiting;/* number of requests awaiting receive */ -atomic_t req_cnt;/* number of requests */ +atomic_t inactives;/* number of inactive requests */ int sgat_elem_sz;/* initialized to scatter_elem_sz */ int mmap_sz;/* byte size of previous mmap() call */ unsigned long ffd_bm[1 ];/* see SG_FFD_* defines above */
@@ -890,11 +892,13 @@ sg_rq_state_fail_msg(struct sg_fd *sfp, enum sg_rq_state exp_old_st, }#endif +/* Functions ending in '_ulck' assume sfp->xa_lock held by caller. */ static void -sg_rq_state_force (struct sg_request *srp,enum sg_rq_state new_st) +sg_rq_chg_state_force_ulck (struct sg_request *srp,enum sg_rq_state new_st) {bool prev, want; -struct xarray *xafp = &srp->parentfp->srp_arr; +struct sg_fd *sfp = srp->parentfp; +struct xarray *xafp = &sfp->srp_arr;atomic_set (&srp->rq_st, new_st); want = (new_st ==SG_RS_AWAIT_RCV );
@@ -908,15 +912,21 @@ sg_rq_state_force(struct sg_request *srp, enum sg_rq_state new_st) want = (new_st ==SG_RS_INACTIVE ); prev =xa_get_mark (xafp, srp->rq_idx,SG_XA_RQ_INACTIVE );if (prev != want) { -if (want) +if (want) { +int prev_idx =READ_ONCE (sfp->low_used_idx); + +if (prev_idx <0 || srp->rq_idx < prev_idx || + !xa_get_mark (xafp, prev_idx,SG_XA_RQ_INACTIVE )) +WRITE_ONCE (sfp->low_used_idx, srp->rq_idx);__xa_set_mark (xafp, srp->rq_idx,SG_XA_RQ_INACTIVE ); -else + }else {__xa_clear_mark (xafp, srp->rq_idx,SG_XA_RQ_INACTIVE ); + } } }static void -sg_rq_state_helper (struct xarray *xafp,struct sg_request *srp,int indic) +sg_rq_chg_state_help (struct xarray *xafp,struct sg_request *srp,int indic) {if (indic &1 )/* from inactive state */ __xa_clear_mark (xafp, srp->rq_idx,SG_XA_RQ_INACTIVE );
@@ -945,45 +955,53 @@ static const int sg_rq_state_mul2arr[] = {2, 0, 8, 0};* spinlock is held. */ static int -sg_rq_state_chg (struct sg_request *srp,enum sg_rq_state old_st, +sg_rq_chg_state (struct sg_request *srp,enum sg_rq_state old_st,enum sg_rq_state new_st) {enum sg_rq_state act_old_st; -int indic; -unsigned long iflags; +int indic = sg_rq_state_arr[(int )old_st] + sg_rq_state_mul2arr[(int )new_st];struct sg_fd *sfp = srp->parentfp; -struct xarray *xafp = &sfp->srp_arr; - indic = sg_rq_state_arr[(int )old_st] + - sg_rq_state_mul2arr[(int )new_st]; - act_old_st = (enum sg_rq_state )atomic_cmpxchg (&srp->rq_st, old_st, - new_st); -if (act_old_st != old_st) { -SG_LOG (1 , sfp,"%s: unexpected old state: %s\n" , __func__, -sg_rq_st_str (act_old_st, false)); -return -EPROTOTYPE ;/* only used for this error type */ - }if (indic) { +unsigned long iflags; +struct xarray *xafp = &sfp->srp_arr; +xa_lock_irqsave (xafp, iflags); -if (new_st ==SG_RS_INACTIVE ) -WRITE_ONCE (sfp->prev_used_idx, srp->rq_idx); -sg_rq_state_helper (xafp, srp, indic); + act_old_st = (enum sg_rq_state )atomic_cmpxchg_relaxed (&srp->rq_st, old_st, new_st); +if (unlikely (act_old_st != old_st)) { +xa_unlock_irqrestore (xafp, iflags); +SG_LOG (1 , sfp,"%s: unexpected old state: %s\n" , __func__, +sg_rq_st_str (act_old_st, false)); +return -EPROTOTYPE ;/* only used for this error type */ + } +if (new_st ==SG_RS_INACTIVE ) { +int prev_idx =READ_ONCE (sfp->low_used_idx); + +if (prev_idx <0 || srp->rq_idx < prev_idx || + !xa_get_mark (xafp, prev_idx,SG_XA_RQ_INACTIVE )) +WRITE_ONCE (sfp->low_used_idx, srp->rq_idx); + } +sg_rq_chg_state_help (xafp, srp, indic);xa_unlock_irqrestore (xafp, iflags); + }else { + act_old_st = (enum sg_rq_state )atomic_cmpxchg (&srp->rq_st, old_st, new_st); +if (unlikely (act_old_st != old_st)) { +SG_LOG (1 , sfp,"%s: unexpected old state: %s\n" , __func__, +sg_rq_st_str (act_old_st, false)); +return -EPROTOTYPE ;/* only used for this error type */ + } }return 0 ; }static void -sg_rq_state_chg_force (struct sg_request *srp,enum sg_rq_state new_st) +sg_rq_chg_state_force (struct sg_request *srp,enum sg_rq_state new_st) {unsigned long iflags; -struct sg_fd *sfp = srp->parentfp; -struct xarray *xafp = &sfp->srp_arr; +struct xarray *xafp = &srp->parentfp->srp_arr;xa_lock_irqsave (xafp, iflags); -if (new_st ==SG_RS_INACTIVE ) -WRITE_ONCE (sfp->prev_used_idx, srp->rq_idx); -sg_rq_state_force (srp, new_st); +sg_rq_chg_state_force_ulck (srp, new_st);xa_unlock_irqrestore (xafp, iflags); }
@@ -1008,11 +1026,13 @@ sg_execute_cmd(struct sg_fd *sfp, struct sg_request *srp) at_head = !(srp->rq_flags &SG_FLAG_Q_AT_TAIL );kref_get (&sfp->f_ref);/* sg_rq_end_io() does kref_put(). */ -sg_rq_state_chg_force (srp,SG_RS_INFLIGHT ); +sg_rq_chg_state_force (srp,SG_RS_INFLIGHT );/* >>>>>>> send cmd/req off to other levels <<<<<<<< */ -if (!sync) +if (!sync) {atomic_inc (&sfp->submitted); +set_bit (SG_FRQ_COUNT_ACTIVE , srp->frq_bm); + }blk_execute_rq_nowait (sdp->device->request_queue, sdp->disk, srp->rq, (int )at_head, sg_rq_end_io); }
@@ -1667,7 +1687,7 @@ sg_fill_request_element(struct sg_fd *sfp, struct sg_request *srp,static inline bool sg_rq_landed (struct sg_device *sdp,struct sg_request *srp) { -return atomic_read (&srp->rq_st) !=SG_RS_INFLIGHT || +return atomic_read_acquire (&srp->rq_st) !=SG_RS_INFLIGHT ||unlikely (SG_IS_DETACHING (sdp)); }
@@ -1683,6 +1703,8 @@ sg_wait_event_srp(struct file *filp, struct sg_fd *sfp, void __user *p,enum sg_rq_state sr_st;struct sg_device *sdp = sfp->parentdp; +if (atomic_read (&srp->rq_st) !=SG_RS_INFLIGHT ) + goto skip_wait;/* and skip _acquire() */ SG_LOG (3 , sfp,"%s: about to wait_event...()\n" , __func__);/* usually will be woken up by sg_rq_end_io() callback */ res = wait_event_interruptible(sfp->read_wait,
@@ -1695,14 +1717,16 @@ sg_wait_event_srp(struct file *filp, struct sg_fd *sfp, void __user *p, __func__, res);return res; } +skip_wait:if (unlikely (SG_IS_DETACHING (sdp))) { -atomic_set (&srp->rq_st,SG_RS_INACTIVE ); +sg_rq_chg_state_force (srp,SG_RS_INACTIVE ); +atomic_inc (&sfp->inactives);return -ENODEV ; } sr_st =atomic_read (&srp->rq_st);if (unlikely (sr_st !=SG_RS_AWAIT_RCV ))return -EPROTO ;/* Logic error */ - res =sg_rq_state_chg (srp, sr_st,SG_RS_BUSY ); + res =sg_rq_chg_state (srp, sr_st,SG_RS_BUSY );if (unlikely (res)) {#if IS_ENABLED (SG_LOG_ACTIVE )sg_rq_state_fail_msg (sfp, sr_st,SG_RS_BUSY , __func__);
@@ -1798,7 +1822,6 @@ sg_set_reserved_sz(struct sg_fd *sfp, int want_rsv_sz) n_srp =sg_mk_srp_sgat (sfp, true/* can take time */ , new_sz);if (IS_ERR (n_srp))return PTR_ERR (n_srp); -sg_rq_state_force (n_srp,SG_RS_INACTIVE );/* new sg_request object, sized correctly is now available */ try_again: o_srp = sfp->rsv_srp;
@@ -1830,7 +1853,8 @@ sg_set_reserved_sz(struct sg_fd *sfp, int want_rsv_sz) cxc_srp =__xa_cmpxchg (xafp, idx, o_srp, n_srp,GFP_ATOMIC );if (o_srp == cxc_srp) { sfp->rsv_srp = n_srp; -sg_rq_state_force (n_srp,SG_RS_INACTIVE ); +sg_rq_chg_state_force_ulck (n_srp,SG_RS_INACTIVE ); +/* don't bump inactives, since replaced an inactive */ xa_unlock_irqrestore (xafp, iflags);SG_LOG (6 , sfp,"%s: new rsv srp=0x%pK ++\n" , __func__, n_srp);
@@ -2008,7 +2032,10 @@ sg_ioctl_common(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,SG_LOG (3 , sfp,"%s: SG_GET_PACK_ID=%d\n" , __func__, val);return put_user (val, ip);case SG_GET_NUM_WAITING : -return put_user (atomic_read (&sfp->waiting), ip); + val =atomic_read (&sfp->waiting); +if (val) +return put_user (val, ip); +return put_user (atomic_read_acquire (&sfp->waiting), ip);case SG_GET_SG_TABLESIZE :SG_LOG (3 , sfp,"%s: SG_GET_SG_TABLESIZE=%d\n" , __func__, sdp->max_sgat_sz);
@@ -2221,11 +2248,16 @@ sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)static __poll_t sg_poll (struct file *filp,poll_table * wait) { +int num;__poll_t p_res =0 ;struct sg_fd *sfp = filp->private_data; -poll_wait (filp, &sfp->read_wait, wait); -if (atomic_read (&sfp->waiting) >0 ) + num =atomic_read (&sfp->waiting); +if (num <1 ) { +poll_wait (filp, &sfp->read_wait, wait); + num =atomic_read (&sfp->waiting); + } +if (num >0 ) p_res =EPOLLIN |EPOLLRDNORM ;if (unlikely (SG_IS_DETACHING (sfp->parentdp)))
@@ -2474,32 +2506,13 @@ sg_rq_end_io(struct request *rq, blk_status_t status)struct sg_device *sdp;struct sg_fd *sfp; -if (!scsi_rp) { -WARN_ONCE ("%s: scsi_req(rq) unexpectedly NULL\n" , __func__); -return ; - } -if (!srp) { -WARN_ONCE ("%s: srp unexpectedly NULL\n" , __func__); -return ; - }/* Expect 0 --> 1 transition, otherwise processed elsewhere */ if (unlikely (test_and_set_bit (SG_FRQ_BLK_PUT_REQ , srp->frq_bm))) {pr_info ("%s: srp=%pK already completed\n" , __func__, srp);return ; } -if (WARN_ON (atomic_read (&srp->rq_st) !=SG_RS_INFLIGHT )) { -pr_warn ("%s: bad rq_st=%d\n" , __func__, -atomic_read (&srp->rq_st)); - goto early_err; - } sfp = srp->parentfp; -if (unlikely (!sfp)) { -WARN_ONCE (1 ,"%s: sfp unexpectedly NULL" , __func__); - goto early_err; - } sdp = sfp->parentdp; -if (unlikely (SG_IS_DETACHING (sdp))) -pr_info ("%s: device detaching\n" , __func__); srp->rq_result = scsi_rp->result; slen =min_t (int, scsi_rp->sense_len,SCSI_SENSE_BUFFERSIZE );
@@ -2552,10 +2565,20 @@ sg_rq_end_io(struct request *rq, blk_status_t status)set_bit (SG_FRQ_DEACT_ORPHAN , srp->frq_bm); } } -if (!test_bit (SG_FRQ_SYNC_INVOC , srp->frq_bm)) -atomic_inc (&sfp->waiting); -if (unlikely (sg_rq_state_chg (srp,SG_RS_INFLIGHT , rqq_state))) -pr_warn ("%s: can't set rq_st\n" , __func__); +if (test_bit (SG_FRQ_COUNT_ACTIVE , srp->frq_bm)) { +int num =atomic_inc_return (&sfp->waiting); + +if (num <2 ) { +WRITE_ONCE (sfp->low_await_idx, srp->rq_idx); + }else { +int l_await_idx =READ_ONCE (sfp->low_await_idx); + +if (l_await_idx <0 || srp->rq_idx < l_await_idx || + !xa_get_mark (&sfp->srp_arr, l_await_idx,SG_XA_RQ_AWAIT )) +WRITE_ONCE (sfp->low_await_idx, srp->rq_idx); + } + } +sg_rq_chg_state_force (srp, rqq_state);/* * Free the mid-level resources apart from the bio (if any). The bio's * blk_rq_unmap_user() can be called later from user context.
@@ -2574,12 +2597,6 @@ sg_rq_end_io(struct request *rq, blk_status_t status)schedule_work (&srp->ew_orph.work); }return ; - -early_err: - srp->rq =NULL ; -if (scsi_rp) -scsi_req_free_cmd (scsi_rp); -blk_put_request (rq); }static const struct file_operations sg_fops = {
@@ -3068,7 +3085,7 @@ sg_finish_scsi_blk_rq(struct sg_request *srp)SG_LOG (4 , sfp,"%s: srp=0x%pK%s\n" , __func__, srp, (srp->parentfp->rsv_srp == srp) ?" rsv" :"" ); -if (!test_bit (SG_FRQ_SYNC_INVOC , srp->frq_bm)) { +if (test_and_clear_bit (SG_FRQ_COUNT_ACTIVE , srp->frq_bm)) {atomic_dec (&sfp->submitted);atomic_dec (&sfp->waiting); }
@@ -3264,19 +3281,28 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id)bool second = false;int res;int num_waiting =atomic_read (&sfp->waiting); -unsigned long idx, start_idx, end_idx; +int l_await_idx =READ_ONCE (sfp->low_await_idx); +unsigned long idx, s_idx; +unsigned long end_idx =ULONG_MAX ;struct sg_request *srp =NULL ;struct xarray *xafp = &sfp->srp_arr; -if (num_waiting <1 ) -return NULL ; +if (num_waiting <1 ) { + num_waiting =atomic_read_acquire (&sfp->waiting); +if (num_waiting <1 ) +return NULL ; + } + + s_idx = (l_await_idx <0 ) ?0 : l_await_idx; + idx = s_idx;if (unlikely (search_for_1)) { -xa_for_each_marked (xafp, idx, srp,SG_XA_RQ_AWAIT ) { -if (!srp) -continue ; +second_time: +for (srp =xa_find (xafp, &idx, end_idx,SG_XA_RQ_AWAIT ); + srp; + srp =xa_find_after (xafp, &idx, end_idx,SG_XA_RQ_AWAIT )) {if (srp->pack_id != pack_id)continue ; - res =sg_rq_state_chg (srp,SG_RS_AWAIT_RCV ,SG_RS_BUSY ); + res =sg_rq_chg_state (srp,SG_RS_AWAIT_RCV ,SG_RS_BUSY );if (likely (res ==0 )) goto good;/* else another caller got it, move on */
@@ -3286,6 +3312,14 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id) }break ; } +/* If not found so far, need to wrap around and search [0 ... s_idx) */ +if (!srp && !second && s_idx >0 ) { + end_idx = s_idx -1 ; + s_idx =0 ; + idx = s_idx; + second = true; + goto second_time; + } }else {/* * Searching for _any_ request is the more likely usage. Start searching with the
@@ -3294,27 +3328,25 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id)* is ready. If there is no queuing and the "last used" has been re-used then the * first (relative) position will be the request we want. */ - start_idx =READ_ONCE (sfp->prev_used_idx); - end_idx =ULONG_MAX ; -second_time: - idx = start_idx; +second_time2:for (srp =xa_find (xafp, &idx, end_idx,SG_XA_RQ_AWAIT ); srp; srp =xa_find_after (xafp, &idx, end_idx,SG_XA_RQ_AWAIT )) { - res =sg_rq_state_chg (srp,SG_RS_AWAIT_RCV ,SG_RS_BUSY ); -if (likely (res ==0 )) + res =sg_rq_chg_state (srp,SG_RS_AWAIT_RCV ,SG_RS_BUSY ); +if (likely (res ==0 )) { +WRITE_ONCE (sfp->low_await_idx, idx +1 ); goto good; + }#if IS_ENABLED (SG_LOG_ACTIVE ) - else -sg_rq_state_fail_msg (sfp ,SG_RS_AWAIT_RCV ,SG_RS_BUSY ,__func__ ); +sg_rq_state_fail_msg (sfp,SG_RS_AWAIT_RCV ,SG_RS_BUSY , __func__);#endif } -/* If not found so far, need to wrap around and search [0 ... start_idx) */ -if (!srp && !second && start_idx >0 ) { - end_idx = start_idx -1 ; - start_idx =0 ; +if (!srp && !second && s_idx >0 ) { + end_idx = s_idx -1 ; + s_idx =0 ; + idx = s_idx; second = true; - goto second_time; + goto second_time2; } }/* here if one of above loops does _not_ find a match */
@@ -3433,11 +3465,12 @@ sg_setup_req(struct sg_comm_wr_t *cwrp, int dxfr_len)bool mk_new_srp = true;bool try_harder = false;bool second = false; -int num_inactive =0 ; -unsigned long idx, start_idx, end_idx, iflags; +bool has_inactive = false; +int l_used_idx; +unsigned long idx, s_idx, end_idx, iflags;struct sg_fd *fp = cwrp->sfp;struct sg_request *r_srp =NULL ;/* request to return */ -struct sg_request *last_srp =NULL ; +struct sg_request *low_srp =NULL ;struct xarray *xafp = &fp->srp_arr;__maybe_unused const char *cp;
@@ -3446,49 +3479,70 @@ sg_setup_req(struct sg_comm_wr_t *cwrp, int dxfr_len)if (xa_empty (xafp)) { act_empty = true; mk_new_srp = true; + }else if (atomic_read (&fp->inactives) <=0 ) { + mk_new_srp = true; }else if (!try_harder && dxfr_len <SG_DEF_SECTOR_SZ ) { + l_used_idx =READ_ONCE (fp->low_used_idx); + s_idx = (l_used_idx <0 ) ?0 : l_used_idx; +if (l_used_idx >=0 &&xa_get_mark (xafp, s_idx,SG_XA_RQ_INACTIVE )) { + r_srp =xa_load (xafp, s_idx); +if (r_srp && r_srp->sgat_h.buflen <=SG_DEF_SECTOR_SZ ) { +if (sg_rq_chg_state (r_srp,SG_RS_INACTIVE ,SG_RS_BUSY ) ==0 ) { + found = true; +atomic_dec (&fp->inactives); + goto have_existing; + } + } + }xa_for_each_marked (xafp, idx, r_srp,SG_XA_RQ_INACTIVE ) { - ++num_inactive; -if (dxfr_len <SG_DEF_SECTOR_SZ ) - last_srp = r_srp; + has_inactive = true; +if (!low_srp && dxfr_len <SG_DEF_SECTOR_SZ ) { + low_srp = r_srp; +break ; + } } -/* If dxfr_len is small, use last inactive request */ -if (last_srp) { - r_srp = last_srp; -if (sg_rq_state_chg (r_srp,SG_RS_INACTIVE ,SG_RS_BUSY )) +/* If dxfr_len is small, use lowest inactive request */ +if (low_srp) { + r_srp = low_srp; +if (sg_rq_chg_state (r_srp,SG_RS_INACTIVE ,SG_RS_BUSY )) goto start_again;/* gone to another thread */ +atomic_dec (&fp->inactives); cp ="toward end of srp_arr" ; found = true; } }else { - start_idx =READ_ONCE (fp->prev_used_idx); + l_used_idx =READ_ONCE (fp->low_used_idx); + s_idx = (l_used_idx <0 ) ?0 : l_used_idx; + idx = s_idx; end_idx =ULONG_MAX ; second_time: - idx = start_idx;for (r_srp =xa_find (xafp, &idx, end_idx,SG_XA_RQ_INACTIVE ); r_srp; r_srp =xa_find_after (xafp, &idx, end_idx,SG_XA_RQ_INACTIVE )) {if (r_srp->sgat_h.buflen >= dxfr_len) { -if (sg_rq_state_chg (r_srp,SG_RS_INACTIVE ,SG_RS_BUSY )) +if (sg_rq_chg_state (r_srp,SG_RS_INACTIVE ,SG_RS_BUSY ))continue ; +atomic_dec (&fp->inactives); +WRITE_ONCE (fp->low_used_idx, idx +1 ); cp ="near front of srp_arr" ; found = true;break ; } }/* If not found so far, need to wrap around and search [0 ... start_idx) */ -if (!r_srp && !second && start_idx >0 ) { - end_idx = start_idx -1 ; - start_idx =0 ; +if (!r_srp && !second && s_idx >0 ) { + end_idx = s_idx -1 ; + s_idx =0 ; + idx = s_idx; second = true; goto second_time; } } +have_existing:if (found) { r_srp->in_resid =0 ; r_srp->rq_info =0 ; r_srp->sense_len =0 ; -WRITE_ONCE (fp->prev_used_idx, r_srp->rq_idx); mk_new_srp = false; }else { mk_new_srp = true;
@@ -3497,7 +3551,6 @@ sg_setup_req(struct sg_comm_wr_t *cwrp, int dxfr_len)bool allow_cmd_q =test_bit (SG_FFD_CMD_Q , fp->ffd_bm);int res;u32 n_idx; -struct xa_limit xal = { .max =0 , .min =0 }; cp ="new" ;if (!allow_cmd_q &&atomic_read (&fp->submitted) >0 ) {
@@ -3509,16 +3562,14 @@ sg_setup_req(struct sg_comm_wr_t *cwrp, int dxfr_len) r_srp =sg_mk_srp_sgat (fp, act_empty, dxfr_len);if (IS_ERR (r_srp)) {if (!try_harder && dxfr_len <SG_DEF_SECTOR_SZ && - num_inactive >0 ) { + has_inactive) { try_harder = true; goto start_again; } goto fini; } -atomic_set (&r_srp->rq_st,SG_RS_BUSY );xa_lock_irqsave (xafp, iflags); - xal.max =atomic_inc_return (&fp->req_cnt); - res =__xa_alloc (xafp, &n_idx, r_srp, xal,GFP_KERNEL ); + res =__xa_alloc (xafp, &n_idx, r_srp, xa_limit_32b,GFP_KERNEL );xa_unlock_irqrestore (xafp, iflags);if (res <0 ) { SG_LOG(1, fp, "%s: xa_alloc() failed, errno=%d\n",
@@ -3561,7 +3612,8 @@ sg_deact_request(struct sg_fd *sfp, struct sg_request *srp) sbp = srp->sense_bp; srp->sense_bp =NULL ;WRITE_ONCE (srp->frq_bm[0 ],0 ); -sg_rq_state_chg_force (srp,SG_RS_INACTIVE ); +sg_rq_chg_state_force (srp,SG_RS_INACTIVE ); +atomic_inc (&sfp->inactives);/* maybe orphaned req, thus never read */ if (sbp)mempool_free (sbp, sg_sense_pool);
@@ -3580,7 +3632,6 @@ sg_add_sfp(struct sg_device *sdp)struct sg_request *srp =NULL ;struct xarray *xadp = &sdp->sfp_arr;struct xarray *xafp; -struct xa_limit xal; sfp =kzalloc (sizeof (*sfp),GFP_ATOMIC | __GFP_NOWARN);if (!sfp)
@@ -3597,8 +3648,6 @@ sg_add_sfp(struct sg_device *sdp)__assign_bit (SG_FFD_CMD_Q , sfp->ffd_bm,SG_DEF_COMMAND_Q );__assign_bit (SG_FFD_KEEP_ORPHAN , sfp->ffd_bm,SG_DEF_KEEP_ORPHAN );__assign_bit (SG_FFD_Q_AT_TAIL , sfp->ffd_bm,SG_DEFAULT_Q_AT ); -atomic_set (&sfp->submitted,0 ); -atomic_set (&sfp->waiting,0 );/* * SG_SCATTER_SZ initializes scatter_elem_sz but different value may * be given as driver/module parameter (e.g. 'scatter_elem_sz=8192').
@@ -3610,7 +3659,7 @@ sg_add_sfp(struct sg_device *sdp) sfp->parentdp = sdp;atomic_set (&sfp->submitted,0 );atomic_set (&sfp->waiting,0 ); -atomic_set (&sfp->req_cnt,0 ); +atomic_set (&sfp->inactives,0 );if (unlikely (SG_IS_DETACHING (sdp))) {SG_LOG (1 , sfp,"%s: detaching\n" , __func__);
@@ -3622,8 +3671,6 @@ sg_add_sfp(struct sg_device *sdp) rbuf_len =min_t (int, sg_big_buff, sdp->max_sgat_sz);if (rbuf_len >0 ) { -struct xa_limit xalrq = { .max =0 , .min =0 }; - srp =sg_build_reserve (sfp, rbuf_len);if (IS_ERR (srp)) {kfree (sfp);
@@ -3639,8 +3686,7 @@ sg_add_sfp(struct sg_device *sdp) __func__, rbuf_len, srp->sgat_h.buflen); }xa_lock_irqsave (xafp, iflags); - xalrq.max =atomic_inc_return (&sfp->req_cnt); - res =__xa_alloc (xafp, &idx, srp, xalrq,GFP_ATOMIC ); + res =__xa_alloc (xafp, &idx, srp, xa_limit_32b,GFP_ATOMIC );xa_unlock_irqrestore (xafp, iflags);if (res <0 ) { SG_LOG(1, sfp, "%s: xa_alloc(srp) bad, errno=%d\n",
@@ -3652,20 +3698,19 @@ sg_add_sfp(struct sg_device *sdp) } srp->rq_idx = idx; srp->parentfp = sfp; -sg_rq_state_chg_force (srp,SG_RS_INACTIVE ); +sg_rq_chg_state_force (srp,SG_RS_INACTIVE ); +atomic_inc (&sfp->inactives); }if (!reduced) {SG_LOG (4 , sfp,"%s: built reserve buflen=%d\n" , __func__, rbuf_len); }xa_lock_irqsave (xadp, iflags); - xal.min =0 ; - xal.max =atomic_read (&sdp->open_cnt); - res =__xa_alloc (xadp, &idx, sfp, xal,GFP_KERNEL ); + res =__xa_alloc (xadp, &idx, sfp, xa_limit_32b,GFP_KERNEL );xa_unlock_irqrestore (xadp, iflags);if (res <0 ) {pr_warn ("%s: xa_alloc(sdp) bad, o_count=%d, errno=%d\n" , - __func__, xal.max, -res); + __func__,atomic_read (&sdp->open_cnt), -res);if (srp) {sg_remove_sgat (srp);kfree (srp);
@@ -4079,12 +4124,13 @@ sg_proc_debug_fd(struct sg_fd *fp, char *obp, int len, unsigned long idx) (int )test_bit (SG_FFD_FORCE_PACKID , fp->ffd_bm), (int )test_bit (SG_FFD_KEEP_ORPHAN , fp->ffd_bm), fp->ffd_bm[0]); - n +=scnprintf (obp + n, len - n," mmap_sz=%d prev_used_idx=%d\n" , - fp->mmap_sz, fp->prev_used_idx); n +=scnprintf (obp + n, len - n, -" submitted=%d waiting=%d open thr_id=%d\n" , +" mmap_sz=%d low_used_idx=%d low_await_idx=%d\n" , + fp->mmap_sz,READ_ONCE (fp->low_used_idx),READ_ONCE (fp->low_await_idx)); + n +=scnprintf (obp + n, len - n, +" submitted=%d waiting=%d inactives=%d open thr_id=%d\n" ,atomic_read (&fp->submitted), -atomic_read (&fp->waiting), fp->tid); +atomic_read (&fp->waiting),atomic_read (&fp->inactives), fp->tid); k =0 ;xa_lock_irqsave (&fp->srp_arr, iflags);xa_for_each (&fp->srp_arr, idx, srp) {
Signature · 1 line
2.25.1
Re: [PATCH v13 41/45] sg: track lowest inactive and await indexes
Quoted 10 lines · Douglas Gilbert
Use two integers in the
sg_fdstructure to track recent and lowest xarray indexes that have become inactive or await a foreground receive. This is used to shorten the number of xarray iterations required prior to a match when queue (IO) depths are large, say 128.Replace the
req_cntatomic in structsg_fdwith the inactives atomic. With large queues cycles were wasted checking the request xarray for any inactives when there were none to be found.
Quoted 9 lines
Rename the sg_rq_state_chg_*() functions to sg_rq_chg_state_*() since too many things start with "
sg_rq_state". Also the new function names emphasize the change part a little more.Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 304 ++++++++++++++++++++++++++-------------------- 1 file changed, 175 insertions(+), 129 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerPATCH v13 42/45 sg: remove unit attention check for device changed
In reply to Douglas GilbertThe SCSI mid-layer now checks for SCSI UNIT ATTENTIONs and takes the appropriate actions. This means that the sg driver no longer needs to do this check.
--- drivers/scsi/sg.c | 49 ++++++++++++----------------------------------- 1 file changed, 12 insertions(+), 37 deletions(-)
drivers/scsi/sg.c +12 -37
@@ -2457,39 +2457,6 @@ sg_rq_end_io_usercontext(struct work_struct *work)kref_put (&sfp->f_ref, sg_remove_sfp); } -static void -sg_check_sense (struct sg_device *sdp,struct sg_request *srp,int sense_len) -{ -int driver_stat; -u32 rq_res = srp->rq_result; -struct scsi_request *scsi_rp =scsi_req (srp->rq); -u8 *sbp = scsi_rp ? scsi_rp->sense :NULL ; - -if (!sbp) -return ; - driver_stat =driver_byte (rq_res); -if (driver_stat &DRIVER_SENSE ) { -struct scsi_sense_hdr ssh; - -if (scsi_normalize_sense (sbp, sense_len, &ssh)) { -if (!scsi_sense_is_deferred (&ssh)) { -if (ssh.sense_key ==UNIT_ATTENTION ) { -if (sdp->device->removable) - sdp->device->changed =1 ; - } - } - } - } -if (test_bit (SG_FDEV_LOG_SENSE , sdp->fdev_bm) >0 ) { -int scsi_stat = rq_res &0xff ; - -if (scsi_stat ==SAM_STAT_CHECK_CONDITION || - scsi_stat ==SAM_STAT_COMMAND_TERMINATED ) -__scsi_print_sense (sdp->device, __func__, sbp, - sense_len); - } -} -/* * This "bottom half" (soft interrupt) handler is called by the mid-level * when a request has completed or failed. This callback is registered in a
@@ -2501,6 +2468,7 @@ sg_rq_end_io(struct request *rq, blk_status_t status) {enum sg_rq_state rqq_state =SG_RS_AWAIT_RCV ;int a_resid, slen; +u32 rq_result;struct sg_request *srp = rq->end_io_data;struct scsi_request *scsi_rp =scsi_req (rq);struct sg_device *sdp;
@@ -2514,7 +2482,8 @@ sg_rq_end_io(struct request *rq, blk_status_t status) sfp = srp->parentfp; sdp = sfp->parentdp; - srp->rq_result = scsi_rp->result; + rq_result = scsi_rp->result; + srp->rq_result = rq_result; slen =min_t (int, scsi_rp->sense_len,SCSI_SENSE_BUFFERSIZE ); a_resid = scsi_rp->resid_len;
@@ -2530,10 +2499,16 @@ sg_rq_end_io(struct request *rq, blk_status_t status) }SG_LOG (6 , sfp,"%s: pack_id=%d, res=0x%x\n" , __func__, srp->pack_id, - srp->rq_result); + rq_result); srp->duration =sg_calc_rq_dur (srp); -if (unlikely ((srp->rq_result &SG_ML_RESULT_MSK ) && slen >0 )) -sg_check_sense (sdp, srp, slen); +if (unlikely ((rq_result &SG_ML_RESULT_MSK ) && slen >0 && +test_bit (SG_FDEV_LOG_SENSE , sdp->fdev_bm))) { +u32 scsi_stat = rq_result &0xff ; + +if (scsi_stat ==SAM_STAT_CHECK_CONDITION || + scsi_stat ==SAM_STAT_COMMAND_TERMINATED ) +__scsi_print_sense (sdp->device, __func__, scsi_rp->sense, slen); + }if (slen >0 ) {if (scsi_rp->sense && !srp->sense_bp) { srp->sense_bp = mempool_alloc(sg_sense_pool,
Signature · 1 line
2.25.1
Re: [PATCH v13 42/45] sg: remove unit attention check for device changed
Quoted 9 lines · Douglas Gilbert
The SCSI mid-layer now checks for SCSI UNIT ATTENTIONs and takes the appropriate actions. This means that the sg driver no longer needs to do this check.
Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 49 ++++++++++++----------------------------------- 1 file changed, 12 insertions(+), 37 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerPATCH v13 43/45 sg: no_dxfer: move to/from kernel buffers
In reply to Douglas GilbertWhen the NO_DXFER flag is use on a command/request, the data-in and data-out buffers (if present) should not be ignored. Add sg_rq_map_kern() function to handle this. Uses a single bio with multiple bvec_s usually each holding multiple pages, if necessary. The driver default element size is 32 KiB so if PAGE_SIZE is 4096 then get_order()==3 .
--- drivers/scsi/sg.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)
drivers/scsi/sg.c +59 -0
@@ -2865,6 +2865,63 @@ exit_sg(void)idr_destroy (&sg_index_idr); } +static struct bio * +sg_mk_kern_bio (int bvec_cnt) +{ +struct bio *biop; + +if (bvec_cnt >BIO_MAX_PAGES ) +return NULL ; + biop =bio_alloc (GFP_ATOMIC , bvec_cnt); +if (!biop) +return NULL ; + biop->bi_end_io = bio_put; +return biop; +} + +/* +* Setup to move data between kernel buffers managed by this driver and a SCSI device. Note that +* there is no corresponding 'unmap' call as is required by blk_rq_map_user() . Uses a single +* bio with an expanded appended bvec if necessary. +*/ +static int +sg_rq_map_kern (struct sg_request *srp,struct request_queue *q,struct request *rqq,int rw_ind) +{ +struct sg_scatter_hold *schp = &srp->sgat_h; +struct bio *bio; +int k, ln; +int op_flags =0 ; +int num_sgat = schp->num_sgat; +int dlen = schp->dlen; +int pg_sz =1 << (PAGE_SHIFT + schp->page_order); +int num_segs = (1 << schp->page_order) * num_sgat; +int res =0 ; + +SG_LOG (4 , srp->parentfp,"%s: dlen=%d, pg_sz=%d\n" , __func__, dlen, pg_sz); +if (num_sgat <=0 ) +return 0 ; +if (rw_ind ==WRITE ) + op_flags =REQ_SYNC |REQ_IDLE ; + bio =sg_mk_kern_bio (num_sgat - k); +if (!bio) +return -ENOMEM ; + bio->bi_opf =req_op (rqq) | op_flags; + +for (k =0 ; k < num_sgat && dlen >0 ; ++k, dlen -= ln) { + ln =min_t (int, dlen, pg_sz); +if (bio_add_pc_page (q, bio, schp->pages[k], ln,0 ) < ln) { +bio_put (bio); +return -EINVAL ; + } + } + res =blk_rq_append_bio (rqq, &bio); +if (unlikely (res)) +bio_put (bio); +else + rqq->nr_phys_segments = num_segs; +return res; +} +static inline void sg_set_map_data (const struct sg_scatter_hold *schp,bool up_valid,struct rq_map_data *mdp)
@@ -3028,6 +3085,8 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir)if (IS_ENABLED (CONFIG_SCSI_PROC_FS ) && res)SG_LOG (1 , sfp,"%s: blk_rq_map_user() res=%d\n" , __func__, res); + }else {/* transfer data to/from kernel buffers */ + res =sg_rq_map_kern (srp, q, rq, r0w); } fini:if (unlikely (res)) {/* failure, free up resources */
Signature · 1 line
2.25.1
Re: [PATCH v13 43/45] sg: no_dxfer: move to/from kernel buffers
Quoted 21 lines · Douglas Gilbert
When the
NO_DXFERflag is use on a command/request, the data-in and data-out buffers (if present) should not be ignored. Addsg_rq_map_kern()function to handle this. Uses a single bio with multiplebvec_susually each holding multiple pages, if necessary. The driver default element size is 32 KiB so ifPAGE_SIZEis 4096 then get_order()==3 .Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)drivers/scsi/sg.c +1
-0@@ -2865,6 +2865,63 @@ exit_sg(void)idr_destroy (&sg_index_idr); } +static struct bio *
drivers/scsi/sg.c exit_sg(void) · line 2878
Quoted 10 lines
drivers/scsi/sg.c +10
-0+sg_mk_kern_bio(int bvec_cnt) +{ +struct bio *biop; + +if (bvec_cnt >BIO_MAX_PAGES ) +return NULL ; + biop =bio_alloc (GFP_ATOMIC , bvec_cnt); +if (!biop) +return NULL ; + biop->bi_end_io = bio_put;
Huh? That is the default action, is it not?
So why specify it separately?
Quoted 51 lines
drivers/scsi/sg.c +46
-0+return biop; +} + +/* +* Setup to move data between kernel buffers managed by this driver and a SCSI device. Note that +* there is no corresponding 'unmap' call as is required by blk_rq_map_user() . Uses a single +* bio with an expanded appended bvec if necessary. +*/ +static int +sg_rq_map_kern (struct sg_request *srp,struct request_queue *q,struct request *rqq,int rw_ind) +{ +struct sg_scatter_hold *schp = &srp->sgat_h; +struct bio *bio; +int k, ln; +int op_flags =0 ; +int num_sgat = schp->num_sgat; +int dlen = schp->dlen; +int pg_sz =1 << (PAGE_SHIFT + schp->page_order); +int num_segs = (1 << schp->page_order) * num_sgat; +int res =0 ; + +SG_LOG (4 , srp->parentfp,"%s: dlen=%d, pg_sz=%d\n" , __func__, dlen, pg_sz); +if (num_sgat <=0 ) +return 0 ; +if (rw_ind ==WRITE ) + op_flags =REQ_SYNC |REQ_IDLE ; + bio =sg_mk_kern_bio (num_sgat - k); +if (!bio) +return -ENOMEM ; + bio->bi_opf =req_op (rqq) | op_flags; + +for (k =0 ; k < num_sgat && dlen >0 ; ++k, dlen -= ln) { + ln =min_t (int, dlen, pg_sz); +if (bio_add_pc_page (q, bio, schp->pages[k], ln,0 ) < ln) { +bio_put (bio); +return -EINVAL ; + } + } + res =blk_rq_append_bio (rqq, &bio); +if (unlikely (res)) +bio_put (bio); +else + rqq->nr_phys_segments = num_segs; +return res; +} +static inline void sg_set_map_data (const struct sg_scatter_hold *schp,bool up_valid,struct rq_map_data *mdp)
drivers/scsi/sg.c sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) · line 3092
Quoted 8 lines
drivers/scsi/sg.c +2
-0@@ -3028,6 +3085,8 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir)if (IS_ENABLED (CONFIG_SCSI_PROC_FS ) && res)SG_LOG (1 , sfp,"%s: blk_rq_map_user() res=%d\n" , __func__, res); + }else {/* transfer data to/from kernel buffers */ + res =sg_rq_map_kern (srp, q, rq, r0w); } fini:if (unlikely (res)) {/* failure, free up resources */
Hmm. I must say I fail to see the rationale here.
Why do you need to do the additional mapping?
And doens't the 'NO_DXFER' flag indicate that _no_ data should be transferred?
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerRe: [PATCH v13 43/45] sg: no_dxfer: move to/from kernel buffers
Quoted 26 lines · Hannes Reinecke
Quoted 25 lines · Douglas Gilbert
When the
NO_DXFERflag is use on a command/request, the data-in and data-out buffers (if present) should not be ignored. Addsg_rq_map_kern()function to handle this. Uses a single bio with multiplebvec_susually each holding multiple pages, if necessary. The driver default element size is 32 KiB so ifPAGE_SIZEis 4096 then get_order()==3 .Signed-off-by Douglas Gilbert <[email protected]>---
drivers/scsi/sg.c| 59 +++++++++++++++++++++++++++++++++++++++++++++++1 file changed, 59 insertions(+)drivers/scsi/sg.c +6
-0@@ -2865,6 +2865,63 @@ exit_sg(void) idr_destroy(&sg_index_idr); } +static struct bio * +sg_mk_kern_bio (int bvec_cnt) +{ + struct bio *biop; + + if (bvec_cnt > BIO_MAX_PAGES)
Quoted 8 lines
drivers/scsi/sg.c exit_sg(void) · line 2878
Quoted 5 lines
drivers/scsi/sg.c +5
-0+ return NULL; + biop = bio_alloc(GFP_ATOMIC, bvec_cnt); + if (!biop) + return NULL; + biop->bi_end_io = bio_put;Huh? That is the default action, is it not?
So why specify it separately?
I'll check. That code snippet is copied from NVMe which has equivalent
code: moving storage data to/from _kernel_ buffers. Later in the driver rewrite, bios are re-used, so if any earlier path puts a different value in biop->bi_end_io, I'm screwed without that line. So I assumed the NVMe code did it for a good reason.
Quoted 56 lines
Quoted 56 lines
drivers/scsi/sg.c +5
-0+ return biop; +} + +/* +* Setup to move data between kernel buffers managed by this driver and a SCSI device. Note that
+ * there is no corresponding 'unmap' call as is required byblk_rq_map_user(). Uses a single
+ * bio with an expanded appended bvec if necessary.
+ */drivers/scsi/sg.c +2
-0+static int +sg_rq_map_kern(struct sg_request *srp, struct request_queue *q, structrequest *rqq, int
rw_ind)drivers/scsi/sg.c +36
-0+{ + struct sg_scatter_hold *schp = &srp->sgat_h; + struct bio *bio; + int k, ln; + int op_flags = 0; + int num_sgat = schp->num_sgat; + int dlen = schp->dlen; + int pg_sz = 1 << (PAGE_SHIFT + schp->page_order); + int num_segs = (1 << schp->page_order) * num_sgat; + int res = 0; + + SG_LOG(4, srp->parentfp, "%s: dlen=%d, pg_sz=%d\n", __func__, dlen, pg_sz); + if (num_sgat <= 0) + return 0; + if (rw_ind == WRITE) + op_flags = REQ_SYNC | REQ_IDLE; + bio = sg_mk_kern_bio(num_sgat - k); + if (!bio) + return -ENOMEM; + bio->bi_opf = req_op(rqq) | op_flags; + + for (k = 0; k < num_sgat && dlen > 0; ++k, dlen -= ln) { + ln = min_t(int, dlen, pg_sz); + if (bio_add_pc_page(q, bio, schp->pages[k], ln, 0) < ln) { + bio_put(bio); + return -EINVAL; + } + } + res = blk_rq_append_bio(rqq, &bio); + if (unlikely(res)) + bio_put(bio); + else + rqq->nr_phys_segments = num_segs; + return res; +} +static inline void
sg_set_map_data(const struct sg_scatter_hold *schp, bool up_valid, struct rq_map_data *mdp)drivers/scsi/sg.c +0
-0@@ -3028,6 +3085,8 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) if (IS_ENABLED(CONFIG_SCSI_PROC_FS) && res) SG_LOG(1, sfp, "%s: blk_rq_map_user() res=%d\n",
Quoted 8 lines
Quoted 7 lines
drivers/scsi/sg.c +2
-0__func__, res); + } else { /* transfer data to/from kernel buffers */ + res = sg_rq_map_kern(srp, q, rq, r0w); }fini:
if (unlikely(res)) { /* failure, free up resources */Hmm. I must say I fail to see the rationale here.
See below.
Quoted 1 line
Why do you need to do the additional mapping?
I don't understand this question.
Quoted 1 line
And doens't the '
NO_DXFER' flag indicate that _no_ data should be transferred?
NO, it absolutely does not mean that! With indirect IO (i.e. the default) there are two transfers, taking a READ operation is an example:
1) transfer user data from the device (a LU) to the internal buffer allocated by the sg driver. LLD arranges that transfer.
2) transfer that user data from the internal buffer to the user space as indicated by the call to ioctl(SG_IO) or its alternatives. This transfer is driven by the sg driver using copy_to_user().
The SG_FLAG_NO_DXFER flag skips step 2) _not_ 1) .
The SG_FLAG_NO_DXFER flag has been in the sg driver since 1998. Sometime between 2010 and now that functionality was quietly dropped. Tony Battersby for one seemed quite peeved when I told him that functionality had been silently dropped.
Why have the SG_FLAG_NO_DXFER flag?
- originally to test read speed; no need to allocate and manage a user space buffer that isn't going to be used
- force a RAID to properly check the validity of user data, especially if one doesn't trust the VERIFY command which can cheat and simply
say: "Looks good, next"
- on mirrored disks: full READ on primary, NO_DXFER READ on secondary; checks secondary mirror is in "good order" ***
- implementation overlaps with mmap()-ed IO in which the "internal buffer" is visible to the user space
- and the big one, for the sg driver rewrite: it is the basis of fast copy and compare: that READ internal buffer (a bio) is the perfect candidate for the internal buffer of the corresponding WRITE operation, both in space and time. In space: one bio for a bound READ and WRITE; in time: that WRITE cannot start until the READ completes.
And please RTFM, not just the code. Every time I present this driver I refer
to: https://doug-gilbert.github.io/sg_v40.html and/or the "danny' url. This from table 2 on the flags field in the v3 and v4 interface:
SG_FLAG_NO_DXFER [0x10000]SGV4_FLAG_NO_DXFER
With indirect IO, data is "bounced" through a kernel buffer as it passes from user space memory to the storage device (or vice versa). This flag instructs the driver not to do the portion of the copy between the kernel buffers and user memory. There are several cases where this is useful. It is used on WRITE side of request sharing because the data to be written is already sitting in that kernel buffer (placed there by the preceding READ). Another case is when data is mirrored on two disks, it only needs to be actually read back from one of the disks, but it may be a good idea to read it back from the other disk at the same time to see if a MEDIUM ERROR is reported (which would indicate the mirror is no longer safe). If the data is not going to be compared, then the second READ could use this flag.
Doug Gilbert
*** I have run the idea of returning a CRC value (T10 algo?) from a NO_DXFER
READ which is produced from the returned user data. In the mirrored disk scenario it would be a way to not only check the secondary was in "good order" but that the CRC over the secondary data was the same as the CRC over the primary data which has been returned to the user space in full. Underwhelming response to date for that idea.
Re: [PATCH v13 43/45] sg: no_dxfer: move to/from kernel buffers
Quoted 36 lines · Douglas Gilbert
Quoted 26 lines · Hannes Reinecke
Quoted 25 lines · Douglas Gilbert
When the
NO_DXFERflag is use on a command/request, the data-in and data-out buffers (if present) should not be ignored. Addsg_rq_map_kern()function to handle this. Uses a single bio with multiplebvec_susually each holding multiple pages, if necessary. The driver default element size is 32 KiB so ifPAGE_SIZEis 4096 then get_order()==3 .Signed-off-by Douglas Gilbert <[email protected]>---
drivers/scsi/sg.c| 59 +++++++++++++++++++++++++++++++++++++++++++++++1 file changed, 59 insertions(+)drivers/scsi/sg.c +6
-0@@ -2865,6 +2865,63 @@ exit_sg(void) idr_destroy(&sg_index_idr); } +static struct bio * +sg_mk_kern_bio (int bvec_cnt) +{ + struct bio *biop; + + if (bvec_cnt > BIO_MAX_PAGES)Quoted 8 lines
drivers/scsi/sg.c exit_sg(void) · line 2878
Quoted 5 lines
drivers/scsi/sg.c +5
-0+ return NULL; + biop = bio_alloc(GFP_ATOMIC, bvec_cnt); + if (!biop) + return NULL; + biop->bi_end_io = bio_put;Huh? That is the default action, is it not?
So why specify it separately?
Quoted 6 lines
I'll check. That code snippet is copied from NVMe which has equivalent
code: moving storage data to/from _kernel_ buffers. Later in the driver rewrite, bios are re-used, so if any earlier path puts a different value inbiop->bi_end_io, I'm screwed without that line. So I assumed the NVMe code did it for a good reason.
Re-used? Uh-oh.
But okay, then it kinda makes sense.
[ .. ]
Quoted 17 lines
Quoted 1 line
Why do you need to do the additional mapping?
I don't understand this question.
Quoted 1 line
And doens't the '
NO_DXFER' flag indicate that _no_ data should be transferred?NO, it absolutely does not mean that! With indirect IO (i.e. the default) there are two transfers, taking a READ operation is an example:
1) transfer user data from the device (a LU) to the internal buffer
allocated
by the sg driver. LLD arranges that transfer.
2) transfer that user data from the internal buffer to the user
space as
indicated by the call toioctl(SG_IO)or its alternatives. This
transfer
is driven by the sg driver usingcopy_to_user().
Quoted 7 lines
The
SG_FLAG_NO_DXFERflag skips step 2) _not_ 1) .The
SG_FLAG_NO_DXFERflag has been in the sg driver since 1998. Sometime between 2010 and now that functionality was quietly dropped. Tony Battersby for one seemed quite peeved when I told him that functionality had been silently dropped.
Ah. Now that makes sense. Data transfer with not data transfer. You should've said :-)
I'll have another look with that in mind.
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerPATCH v13 44/45 sg: [RFC] add blk_poll support
In reply to Douglas GilbertThe support is added via the new SGV4_FLAG_HIPRI command flag which causes REQ_HIPRI to be set on the request. Before waiting on an inflight request, it is checked to see if it has SGV4_FLAG_HIPRI, and if so blk_poll() is called instead of the wait. In situations where only the file descriptor is known (e.g. sg_poll() and ioctl(SG_GET_NUM_WAITING)) all inflight requests associated with the file descriptor that have SGV4_FLAG_HIPRI set, have sg_poll() called on them.
Note that the implementation of blk_poll() calls mq_poll() in the LLD associated with the request. Then for any request found to be ready, blk_poll() invokes the scsi_done() callback. So this means if blk_poll() returns 1 then sg_rq_end_io() has already been called for the polled request.
--- drivers/scsi/sg.c | 87 ++++++++++++++++++++++++++++++++++++++++-- include/uapi/scsi/sg.h | 1 + 2 files changed, 84 insertions(+), 4 deletions(-)
drivers/scsi/sg.c +83 -4
@@ -123,6 +123,7 @@ enum sg_rq_state { /* N.B. sg_rq_state_arr assumes SG_RS_AWAIT_RCV==2 */#define SG_FFD_CMD_Q 1/* clear: only 1 active req per fd */ #define SG_FFD_KEEP_ORPHAN 2/* policy for this fd */ #define SG_FFD_MMAP_CALLED 3/* mmap(2) system call made on fd */ +#define SG_FFD_HIPRI_SEEN 4/* could have HIPRI requests active */ #define SG_FFD_Q_AT_TAIL 5/* set: queue reqs at tail of blk q */ /* Bit positions (flags) for sg_device::fdev_bm bitmask follow */
@@ -301,6 +302,8 @@ static struct sg_device *sg_get_dev(int min_dev);static void sg_device_destroy (struct kref *kref);static struct sg_request *sg_mk_srp_sgat (struct sg_fd *sfp,bool first,int db_len); +static int sg_sfp_blk_poll (struct sg_fd *sfp,int loop_count); +static int sg_srp_blk_poll (struct sg_request *srp,int loop_count);#if IS_ENABLED (CONFIG_SCSI_LOGGING ) &&IS_ENABLED (SG_DEBUG )static const char *sg_rq_st_str (enum sg_rq_state rq_st,bool long_str);#endif
@@ -1033,6 +1036,8 @@ sg_execute_cmd(struct sg_fd *sfp, struct sg_request *srp)atomic_inc (&sfp->submitted);set_bit (SG_FRQ_COUNT_ACTIVE , srp->frq_bm); } +if (srp->rq_flags &SGV4_FLAG_HIPRI ) + srp->rq->cmd_flags |=REQ_HIPRI ;blk_execute_rq_nowait (sdp->device->request_queue, sdp->disk, srp->rq, (int )at_head, sg_rq_end_io); }
@@ -1705,6 +1710,12 @@ sg_wait_event_srp(struct file *filp, struct sg_fd *sfp, void __user *p,if (atomic_read (&srp->rq_st) !=SG_RS_INFLIGHT ) goto skip_wait;/* and skip _acquire() */ +if (srp->rq_flags &SGV4_FLAG_HIPRI ) { + res =sg_srp_blk_poll (srp,-1 );/* spin till found */ +if (unlikely (res <0 )) +return res; + goto skip_wait; + }SG_LOG (3 , sfp,"%s: about to wait_event...()\n" , __func__);/* usually will be woken up by sg_rq_end_io() callback */ res = wait_event_interruptible(sfp->read_wait,
@@ -2032,6 +2043,8 @@ sg_ioctl_common(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,SG_LOG (3 , sfp,"%s: SG_GET_PACK_ID=%d\n" , __func__, val);return put_user (val, ip);case SG_GET_NUM_WAITING : +if (test_bit (SG_FFD_HIPRI_SEEN , sfp->ffd_bm)) +sg_sfp_blk_poll (sfp,0 );/* LLD may have some ready push */ val =atomic_read (&sfp->waiting);if (val)return put_user (val, ip);
@@ -2241,6 +2254,59 @@ sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg) }#endif +static int +sg_srp_q_blk_poll (struct sg_request *srp,struct request_queue *q,int loop_count) +{ +int k, n, num; +blk_qc_t cookie =request_to_qc_t (srp->rq->mq_hctx, srp->rq); + + num = (loop_count <1 ) ?1 : loop_count; +for (k =0 ; k < num; ++k) { + n =blk_poll (q, cookie, loop_count <0 /* spin if negative */ ); +if (n !=0 ) +return n; + } +return 0 ; +} + +/* +* Check all requests on this sfp that are both inflight and HIPRI. That check involves calling +* blk_poll(spin<-false) loop_count times. If loop_count is 0 then call blk_poll once. +* If loop_count is negative then call blk_poll(spin<-true)) once for each request. +* Returns number found (could be 0) or a negated errno value. +*/ +static int +sg_sfp_blk_poll (struct sg_fd *sfp,int loop_count) +{ +int res =0 ; +int n; +unsigned long idx; +struct sg_request *srp; +struct scsi_device *sdev = sfp->parentdp->device; +struct request_queue *q = sdev ? sdev->request_queue :NULL ; +struct xarray *xafp = &sfp->srp_arr; + +if (!q) +return -EINVAL ; +xa_for_each (xafp, idx, srp) { +if (atomic_read (&srp->rq_st) !=SG_RS_INFLIGHT ) +continue ; +if (!(srp->rq_flags &SGV4_FLAG_HIPRI )) +continue ; + n =sg_srp_q_blk_poll (srp, q, loop_count); +if (unlikely (n <0 )) +return n; + res += n; + } +return res; +} + +static inline int +sg_srp_blk_poll (struct sg_request *srp,int loop_count) +{ +return sg_srp_q_blk_poll (srp, srp->parentfp->parentdp->device->request_queue, loop_count); +} +/* * Implements the poll(2) system call for this driver. Returns various EPOLL* * flags OR-ed together.
@@ -2252,6 +2318,8 @@ sg_poll(struct file *filp, poll_table * wait)__poll_t p_res =0 ;struct sg_fd *sfp = filp->private_data; +if (test_bit (SG_FFD_HIPRI_SEEN , sfp->ffd_bm)) +sg_sfp_blk_poll (sfp,0 );/* LLD may have some ready to push up */ num =atomic_read (&sfp->waiting);if (num <1 ) {poll_wait (filp, &sfp->read_wait, wait);
@@ -2564,7 +2632,8 @@ sg_rq_end_io(struct request *rq, blk_status_t status)if (likely (rqq_state ==SG_RS_AWAIT_RCV )) {/* Wake any sg_read()/ioctl(SG_IORECEIVE) awaiting this req */ -wake_up_interruptible (&sfp->read_wait); +if (!(srp->rq_flags &SGV4_FLAG_HIPRI )) +wake_up_interruptible (&sfp->read_wait);kill_fasync (&sfp->async_qp,SIGPOLL ,POLL_IN );kref_put (&sfp->f_ref, sg_remove_sfp); }else {/* clean up orphaned request that aren't being kept */
@@ -3007,6 +3076,10 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir)/* current sg_request protected by SG_RS_BUSY state */ scsi_rp =scsi_req (rq); srp->rq = rq; +if (rq_flags &SGV4_FLAG_HIPRI ) { +if (!test_bit (SG_FFD_HIPRI_SEEN , sfp->ffd_bm)) +set_bit (SG_FFD_HIPRI_SEEN , sfp->ffd_bm); + }if (cwrp->cmd_len >BLK_MAX_CDB ) scsi_rp->cmd = long_cmdp;
@@ -3120,7 +3193,10 @@ sg_finish_scsi_blk_rq(struct sg_request *srp)SG_LOG (4 , sfp,"%s: srp=0x%pK%s\n" , __func__, srp, (srp->parentfp->rsv_srp == srp) ?" rsv" :"" );if (test_and_clear_bit (SG_FRQ_COUNT_ACTIVE , srp->frq_bm)) { -atomic_dec (&sfp->submitted); +bool now_zero = !atomic_dec_and_test (&sfp->submitted); + +if (now_zero &&test_bit (SG_FFD_HIPRI_SEEN , sfp->ffd_bm)) +clear_bit (SG_FFD_HIPRI_SEEN , sfp->ffd_bm);atomic_dec (&sfp->waiting); }
@@ -3321,6 +3397,8 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id)struct sg_request *srp =NULL ;struct xarray *xafp = &sfp->srp_arr; +if (test_bit (SG_FFD_HIPRI_SEEN , sfp->ffd_bm)) +sg_sfp_blk_poll (sfp,0 );/* LLD may have some ready to push up */ if (num_waiting <1 ) { num_waiting =atomic_read_acquire (&sfp->waiting);if (num_waiting <1 )
@@ -4127,8 +4205,9 @@ sg_proc_debug_sreq(struct sg_request *srp, int to, char *obp, int len)else if (dur <U32_MAX )/* in-flight or busy (so ongoing) */ n +=scnprintf (obp + n, len - n," t_o/elap=%us/%ums" , to /1000 , dur); - n +=scnprintf (obp + n, len - n," sgat=%d op=0x%02x\n" , - srp->sgat_h.num_sgat, srp->cmd_opcode); + cp = (srp->rq_flags &SGV4_FLAG_HIPRI ) ?"hipri " :"" ; + n +=scnprintf (obp + n, len - n," sgat=%d %sop=0x%02x\n" , + srp->sgat_h.num_sgat, cp, srp->cmd_opcode);return n; }
include/uapi/scsi/sg.h +1 -0
@@ -110,6 +110,7 @@ typedef struct sg_io_hdr {#define SGV4_FLAG_Q_AT_TAIL SG_FLAG_Q_AT_TAIL#define SGV4_FLAG_Q_AT_HEAD SG_FLAG_Q_AT_HEAD#define SGV4_FLAG_IMMED 0x400/* for polling with SG_IOR, ignored in SG_IOS */ +#define SGV4_FLAG_HIPRI 0x800/* request will use blk_poll to complete */ /* Output (potentially OR-ed together) in v3::info or v4::info field */ #define SG_INFO_OK_MASK 0x1
Signature · 1 line
2.25.1
Douglas Gilbert
maintainerRe: [PATCH v13 44/45] sg: [RFC] add blk_poll support
Quoted 7 lines · Douglas Gilbert
The support is added via the new
SGV4_FLAG_HIPRIcommand flag which causesREQ_HIPRIto be set on the request. Before waiting on an inflight request, it is checked to see if it hasSGV4_FLAG_HIPRI, and if soblk_poll()is called instead of the wait. In situations where only the file descriptor is known (e.g.sg_poll()andioctl(SG_GET_NUM_WAITING)) all inflight requests associated with the file descriptor that haveSGV4_FLAG_HIPRIset, havesg_poll()
s/have sg_poll/have blk_poll/
Quoted 7 lines
called on them.
Note that the implementation of
blk_poll()callsmq_poll()in the LLD associated with the request. Then for any request found to be ready,blk_poll()invokes thescsi_done()callback. So this means ifblk_poll()returns 1 thensg_rq_end_io()has already been called for the polled request.
<snip>
Re: [PATCH v13 44/45] sg: [RFC] add blk_poll support
In reply to Douglas GilbertQuoted 153 lines · Douglas Gilbert
The support is added via the new
SGV4_FLAG_HIPRIcommand flag which causesREQ_HIPRIto be set on the request. Before waiting on an inflight request, it is checked to see if it hasSGV4_FLAG_HIPRI, and if soblk_poll()is called instead of the wait. In situations where only the file descriptor is known (e.g.sg_poll()andioctl(SG_GET_NUM_WAITING)) all inflight requests associated with the file descriptor that haveSGV4_FLAG_HIPRIset, havesg_poll()called on them.Note that the implementation of
blk_poll()callsmq_poll()in the LLD associated with the request. Then for any request found to be ready,blk_poll()invokes thescsi_done()callback. So this means ifblk_poll()returns 1 thensg_rq_end_io()has already been called for the polled request.Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 87 ++++++++++++++++++++++++++++++++++++++++-- include/uapi/scsi/sg.h | 1 + 2 files changed, 84 insertions(+), 4 deletions(-)drivers/scsi/sg.c +70
-1@@ -123,6 +123,7 @@ enum sg_rq_state { /* N.B. sg_rq_state_arr assumes SG_RS_AWAIT_RCV==2 */#define SG_FFD_CMD_Q 1/* clear: only 1 active req per fd */ #define SG_FFD_KEEP_ORPHAN 2/* policy for this fd */ #define SG_FFD_MMAP_CALLED 3/* mmap(2) system call made on fd */ +#define SG_FFD_HIPRI_SEEN 4/* could have HIPRI requests active */ #define SG_FFD_Q_AT_TAIL 5/* set: queue reqs at tail of blk q */ /* Bit positions (flags) for sg_device::fdev_bm bitmask follow */ @@ -301,6 +302,8 @@ static struct sg_device *sg_get_dev(int min_dev);static void sg_device_destroy (struct kref *kref);static struct sg_request *sg_mk_srp_sgat (struct sg_fd *sfp,bool first,int db_len); +static int sg_sfp_blk_poll (struct sg_fd *sfp,int loop_count); +static int sg_srp_blk_poll (struct sg_request *srp,int loop_count);#if IS_ENABLED (CONFIG_SCSI_LOGGING ) &&IS_ENABLED (SG_DEBUG )static const char *sg_rq_st_str (enum sg_rq_state rq_st,bool long_str);#endif @@ -1033,6 +1036,8 @@ sg_execute_cmd(struct sg_fd *sfp, struct sg_request *srp)atomic_inc (&sfp->submitted);set_bit (SG_FRQ_COUNT_ACTIVE , srp->frq_bm); } +if (srp->rq_flags &SGV4_FLAG_HIPRI ) + srp->rq->cmd_flags |=REQ_HIPRI ;blk_execute_rq_nowait (sdp->device->request_queue, sdp->disk, srp->rq, (int )at_head, sg_rq_end_io); }@@ -1705,6 +1710,12 @@ sg_wait_event_srp(struct file *filp, struct sg_fd *sfp, void __user *p,if (atomic_read (&srp->rq_st) !=SG_RS_INFLIGHT ) goto skip_wait;/* and skip _acquire() */ +if (srp->rq_flags &SGV4_FLAG_HIPRI ) { + res =sg_srp_blk_poll (srp,-1 );/* spin till found */ +if (unlikely (res <0 )) +return res; + goto skip_wait; + }SG_LOG (3 , sfp,"%s: about to wait_event...()\n" , __func__);/* usually will be woken up by sg_rq_end_io() callback */ res = wait_event_interruptible(sfp->read_wait,@@ -2032,6 +2043,8 @@ sg_ioctl_common(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,SG_LOG (3 , sfp,"%s: SG_GET_PACK_ID=%d\n" , __func__, val);return put_user (val, ip);case SG_GET_NUM_WAITING : +if (test_bit (SG_FFD_HIPRI_SEEN , sfp->ffd_bm)) +sg_sfp_blk_poll (sfp,0 );/* LLD may have some ready push */ val =atomic_read (&sfp->waiting);if (val)return put_user (val, ip);@@ -2241,6 +2254,59 @@ sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg) }#endif +static int +sg_srp_q_blk_poll (struct sg_request *srp,struct request_queue *q,int loop_count) +{ +int k, n, num; +blk_qc_t cookie =request_to_qc_t (srp->rq->mq_hctx, srp->rq); + + num = (loop_count <1 ) ?1 : loop_count; +for (k =0 ; k < num; ++k) { + n =blk_poll (q, cookie, loop_count <0 /* spin if negative */ ); +if (n !=0 ) +return n; + } +return 0 ; +} + +/* +* Check all requests on this sfp that are both inflight and HIPRI. That check involves calling +* blk_poll(spin<-false) loop_count times. If loop_count is 0 then call blk_poll once. +* If loop_count is negative then call blk_poll(spin<-true)) once for each request. +* Returns number found (could be 0) or a negated errno value. +*/ +static int +sg_sfp_blk_poll (struct sg_fd *sfp,int loop_count) +{ +int res =0 ; +int n; +unsigned long idx; +struct sg_request *srp; +struct scsi_device *sdev = sfp->parentdp->device; +struct request_queue *q = sdev ? sdev->request_queue :NULL ; +struct xarray *xafp = &sfp->srp_arr; + +if (!q) +return -EINVAL ; +xa_for_each (xafp, idx, srp) { +if (atomic_read (&srp->rq_st) !=SG_RS_INFLIGHT ) +continue ; +if (!(srp->rq_flags &SGV4_FLAG_HIPRI )) +continue ; + n =sg_srp_q_blk_poll (srp, q, loop_count); +if (unlikely (n <0 )) +return n; + res += n; + } +return res; +} + +static inline int +sg_srp_blk_poll (struct sg_request *srp,int loop_count) +{ +return sg_srp_q_blk_poll (srp, srp->parentfp->parentdp->device->request_queue, loop_count); +} +/* * Implements the poll(2) system call for this driver. Returns various EPOLL* * flags OR-ed together. @@ -2252,6 +2318,8 @@ sg_poll(struct file *filp, poll_table * wait)__poll_t p_res =0 ;struct sg_fd *sfp = filp->private_data; +if (test_bit (SG_FFD_HIPRI_SEEN , sfp->ffd_bm)) +sg_sfp_blk_poll (sfp,0 );/* LLD may have some ready to push up */ num =atomic_read (&sfp->waiting);if (num <1 ) {poll_wait (filp, &sfp->read_wait, wait);@@ -2564,7 +2632,8 @@ sg_rq_end_io(struct request *rq, blk_status_t status)if (likely (rqq_state ==SG_RS_AWAIT_RCV )) {/* Wake any sg_read()/ioctl(SG_IORECEIVE) awaiting this req */ -wake_up_interruptible (&sfp->read_wait); +if (!(srp->rq_flags &SGV4_FLAG_HIPRI )) +wake_up_interruptible (&sfp->read_wait);kill_fasync (&sfp->async_qp,SIGPOLL ,POLL_IN );kref_put (&sfp->f_ref, sg_remove_sfp); }else {/* clean up orphaned request that aren't being kept */
drivers/scsi/sg.c sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) · line 3083
Quoted 8 lines
drivers/scsi/sg.c +4
-0@@ -3007,6 +3076,10 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir)/* current sg_request protected by SG_RS_BUSY state */ scsi_rp =scsi_req (rq); srp->rq = rq; +if (rq_flags &SGV4_FLAG_HIPRI ) { +if (!test_bit (SG_FFD_HIPRI_SEEN , sfp->ffd_bm)) +set_bit (SG_FFD_HIPRI_SEEN , sfp->ffd_bm); + }
test_and_set_bit()?
drivers/scsi/sg.c sg_finish_scsi_blk_rq(struct sg_request *srp) · line 3199
Quoted 11 lines
drivers/scsi/sg.c +4
-1if (cwrp->cmd_len >BLK_MAX_CDB ) scsi_rp->cmd = long_cmdp;@@ -3120,7 +3193,10 @@ sg_finish_scsi_blk_rq(struct sg_request *srp)SG_LOG (4 , sfp,"%s: srp=0x%pK%s\n" , __func__, srp, (srp->parentfp->rsv_srp == srp) ?" rsv" :"" );if (test_and_clear_bit (SG_FRQ_COUNT_ACTIVE , srp->frq_bm)) { -atomic_dec (&sfp->submitted); +bool now_zero = !atomic_dec_and_test (&sfp->submitted); + +if (now_zero &&test_bit (SG_FFD_HIPRI_SEEN , sfp->ffd_bm)) +clear_bit (SG_FFD_HIPRI_SEEN , sfp->ffd_bm);
test_and_clear_bit()?
Quoted 30 lines
drivers/scsi/sg.c +5
-2atomic_dec (&sfp->waiting); }@@ -3321,6 +3397,8 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id)struct sg_request *srp =NULL ;struct xarray *xafp = &sfp->srp_arr; +if (test_bit (SG_FFD_HIPRI_SEEN , sfp->ffd_bm)) +sg_sfp_blk_poll (sfp,0 );/* LLD may have some ready to push up */ if (num_waiting <1 ) { num_waiting =atomic_read_acquire (&sfp->waiting);if (num_waiting <1 )@@ -4127,8 +4205,9 @@ sg_proc_debug_sreq(struct sg_request *srp, int to, char *obp, int len)else if (dur <U32_MAX )/* in-flight or busy (so ongoing) */ n +=scnprintf (obp + n, len - n," t_o/elap=%us/%ums" , to /1000 , dur); - n +=scnprintf (obp + n, len - n," sgat=%d op=0x%02x\n" , - srp->sgat_h.num_sgat, srp->cmd_opcode); + cp = (srp->rq_flags &SGV4_FLAG_HIPRI ) ?"hipri " :"" ; + n +=scnprintf (obp + n, len - n," sgat=%d %sop=0x%02x\n" , + srp->sgat_h.num_sgat, cp, srp->cmd_opcode);return n; }include/uapi/scsi/sg.h +0
-0
include/uapi/scsi/sg.h typedef struct sg_io_hdr { · line 116
Quoted 7 lines
include/uapi/scsi/sg.h +1
-0@@ -110,6 +110,7 @@ typedef struct sg_io_hdr {#define SGV4_FLAG_Q_AT_TAIL SG_FLAG_Q_AT_TAIL#define SGV4_FLAG_Q_AT_HEAD SG_FLAG_Q_AT_HEAD#define SGV4_FLAG_IMMED 0x400/* for polling with SG_IOR, ignored in SG_IOS */ +#define SGV4_FLAG_HIPRI 0x800/* request will use blk_poll to complete */ /* Output (potentially OR-ed together) in v3::info or v4::info field */ #define SG_INFO_OK_MASK 0x1
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerRe: [PATCH v13 44/45] sg: [RFC] add blk_poll support
See inline replies ...
Quoted 160 lines · Hannes Reinecke
Quoted 159 lines · Douglas Gilbert
The support is added via the new
SGV4_FLAG_HIPRIcommand flag which causesREQ_HIPRIto be set on the request. Before waiting on an inflight request, it is checked to see if it hasSGV4_FLAG_HIPRI, and if soblk_poll()is called instead of the wait. In situations where only the file descriptor is known (e.g.sg_poll()andioctl(SG_GET_NUM_WAITING)) all inflight requests associated with the file descriptor that haveSGV4_FLAG_HIPRIset, havesg_poll()called on them.Note that the implementation of
blk_poll()callsmq_poll()in the LLD associated with the request. Then for any request found to be ready,blk_poll()invokes thescsi_done()callback. So this means ifblk_poll()returns 1 thensg_rq_end_io()has already been called for the polled request.Signed-off-by Douglas Gilbert <[email protected]>---
drivers/scsi/sg.c | 87 ++++++++++++++++++++++++++++++++++++++++--include/uapi/scsi/sg.h| 1 +2 files changed, 84 insertions(+), 4 deletions(-)drivers/scsi/sg.c +0
-0@@ -123,6 +123,7 @@ enum sg_rq_state { /* N.B. sg_rq_state_arr assumesSG_RS_AWAIT_RCV==2 */
#define SG_FFD_CMD_Q 1 /* clear: only 1 active req per fd */ #define SG_FFD_KEEP_ORPHAN 2 /* policy for this fd */ #define SG_FFD_MMAP_CALLED 3 /* mmap(2) system call made on fd */ +#define SG_FFD_HIPRI_SEEN 4 /* could have HIPRI requests active */ #define SG_FFD_Q_AT_TAIL 5 /* set: queue reqs at tail of blk q */ /* Bit positions (flags) forsg_device::fdev_bmbitmask follow */drivers/scsi/sg.c +4
-0@@ -301,6 +302,8 @@ static struct sg_device *sg_get_dev(int min_dev); static void sg_device_destroy(struct kref *kref); static struct sg_request *sg_mk_srp_sgat(struct sg_fd *sfp, bool first, int db_len); +static int sg_sfp_blk_poll (struct sg_fd *sfp,int loop_count); +static int sg_srp_blk_poll (struct sg_request *srp,int loop_count); #if IS_ENABLED(CONFIG_SCSI_LOGGING) && IS_ENABLED(SG_DEBUG) static const char *sg_rq_st_str(enum sg_rq_state rq_st, bool long_str); #endif@@ -1033,6 +1036,8 @@ sg_execute_cmd(struct sg_fd *sfp, struct sg_request *srp) atomic_inc(&sfp->submitted); set_bit(SG_FRQ_COUNT_ACTIVE, srp->frq_bm); } + if (srp->rq_flags & SGV4_FLAG_HIPRI) + srp->rq->cmd_flags |= REQ_HIPRI; blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk, srp->rq, (int)at_head, sg_rq_end_io); }@@ -1705,6 +1710,12 @@ sg_wait_event_srp(struct file *filp, struct sg_fd *sfp, void __user *p, if (atomic_read(&srp->rq_st) != SG_RS_INFLIGHT)goto skip_wait; /* and skip
_acquire()*/ + if (srp->rq_flags&SGV4_FLAG_HIPRI) {
+ res =sg_srp_blk_poll(srp, -1); /* spin till found */ + if (unlikely(res < 0))
+ return res;
+ gotoskip_wait;
+ }
SG_LOG(3, sfp, "%s: about to wait_event...()\n", __func__); /* usually will be woken up bysg_rq_end_io()callback */ res = wait_event_interruptible(sfp->read_wait,drivers/scsi/sg.c +0
-0@@ -2032,6 +2043,8 @@ sg_ioctl_common(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp, SG_LOG(3, sfp, "%s: SG_GET_PACK_ID=%d\n", __func__, val); return put_user(val, ip);case
SG_GET_NUM_WAITING:
+ if (test_bit(SG_FFD_HIPRI_SEEN, sfp->ffd_bm))
+sg_sfp_blk_poll(sfp, 0); /* LLD may have some ready push */ val =atomic_read(&sfp->waiting);
if (val)
returnput_user(val, ip);drivers/scsi/sg.c +17
-0@@ -2241,6 +2254,59 @@ sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg) } #endif +static int +sg_srp_q_blk_poll (struct sg_request *srp,struct request_queue *q,int loop_count) +{ + int k, n, num; + blk_qc_t cookie = request_to_qc_t(srp->rq->mq_hctx, srp->rq); + + num = (loop_count < 1) ? 1 : loop_count; + for (k = 0; k < num; ++k) { + n = blk_poll(q, cookie, loop_count < 0 /* spin if negative */); + if (n != 0) + return n; + } + return 0; +} + +/* +* Check all requests on this sfp that are both inflight and HIPRI. That check involves calling
+ *blk_poll(spin<-false)loop_counttimes. Ifloop_countis 0 then callblk_pollonce.
+ * Ifloop_countis negative then callblk_poll(spin<-true)) once for each request.
+ * Returns number found (could be 0) or a negated errno value.
+ */drivers/scsi/sg.c +30
-0+static int +sg_sfp_blk_poll (struct sg_fd *sfp,int loop_count) +{ + int res = 0; + int n; + unsigned long idx; + struct sg_request *srp; + struct scsi_device *sdev = sfp->parentdp->device; + struct request_queue *q = sdev ? sdev->request_queue : NULL; + struct xarray *xafp = &sfp->srp_arr; + + if (!q) + return -EINVAL; + xa_for_each(xafp, idx, srp) { + if (atomic_read(&srp->rq_st) != SG_RS_INFLIGHT) + continue; + if (!(srp->rq_flags & SGV4_FLAG_HIPRI)) + continue; + n = sg_srp_q_blk_poll(srp, q, loop_count); + if (unlikely(n < 0)) + return n; + res += n; + } + return res; +} + +static inline int +sg_srp_blk_poll (struct sg_request *srp,int loop_count) +{ + return sg_srp_q_blk_poll(srp,
srp->parentfp->parentdp->device->request_queue,loop_count);drivers/scsi/sg.c +2
-0+} +/*
* Implements the poll(2) system call for this driver. Returns various EPOLL*
* flags OR-ed together.drivers/scsi/sg.c +4
-1@@ -2252,6 +2318,8 @@ sg_poll(struct file *filp, poll_table * wait) __poll_t p_res = 0; struct sg_fd *sfp = filp->private_data; + if (test_bit(SG_FFD_HIPRI_SEEN, sfp->ffd_bm)) + sg_sfp_blk_poll(sfp, 0); /* LLD may have some ready to push up */ num = atomic_read(&sfp->waiting); if (num < 1) { poll_wait(filp, &sfp->read_wait, wait);@@ -2564,7 +2632,8 @@ sg_rq_end_io(struct request *rq, blk_status_t status) if (likely(rqq_state == SG_RS_AWAIT_RCV)) { /* Wake any sg_read()/ioctl(SG_IORECEIVE) awaiting this req */ - wake_up_interruptible(&sfp->read_wait); + if (!(srp->rq_flags & SGV4_FLAG_HIPRI)) + wake_up_interruptible(&sfp->read_wait); kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN); kref_put(&sfp->f_ref, sg_remove_sfp); } else { /* clean up orphaned request that aren't being kept */@@ -3007,6 +3076,10 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) /* current sg_request protected by SG_RS_BUSY state */
Quoted 8 lines
drivers/scsi/sg.c sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) · line 3082
Quoted 6 lines
drivers/scsi/sg.c +4
-0scsi_rp = scsi_req(rq); srp->rq = rq; + if (rq_flags & SGV4_FLAG_HIPRI) { + if (!test_bit(SG_FFD_HIPRI_SEEN, sfp->ffd_bm)) + set_bit(SG_FFD_HIPRI_SEEN, sfp->ffd_bm); + }
test_and_set_bit()?
I really would like to see the pseudo code for what test_and_set_bit() does. I don't want to set the bit if it is already set. IOW why have an atomic write operation and its associated baggage if it is already set.
If I follow your logic the conditional reduces to:
test_and_set_bit() { }
which may further reduce to
set_bit();
Quoted 14 lines
drivers/scsi/sg.c sg_finish_scsi_blk_rq(struct sg_request *srp) · line 3199
Quoted 12 lines
drivers/scsi/sg.c +4
-1if (cwrp->cmd_len > BLK_MAX_CDB) scsi_rp->cmd = long_cmdp;@@ -3120,7 +3193,10 @@ sg_finish_scsi_blk_rq(struct sg_request *srp) SG_LOG(4, sfp, "%s: srp=0x%pK%s\n", __func__, srp, (srp->parentfp->rsv_srp == srp) ? " rsv" : ""); if (test_and_clear_bit(SG_FRQ_COUNT_ACTIVE, srp->frq_bm)) { - atomic_dec(&sfp->submitted); + bool now_zero = !atomic_dec_and_test(&sfp->submitted); + + if (now_zero && test_bit(SG_FFD_HIPRI_SEEN, sfp->ffd_bm)) + clear_bit(SG_FFD_HIPRI_SEEN, sfp->ffd_bm);
test_and_clear_bit()?
See above.
Quoted 37 lines
Quoted 27 lines
drivers/scsi/sg.c +2
-0atomic_dec(&sfp->waiting); }@@ -3321,6 +3397,8 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id) struct sg_request *srp = NULL; struct xarray *xafp = &sfp->srp_arr; + if (test_bit(SG_FFD_HIPRI_SEEN, sfp->ffd_bm)) + sg_sfp_blk_poll(sfp, 0); /* LLD may have some ready to push up */ if (num_waiting < 1) { num_waiting = atomic_read_acquire(&sfp->waiting); if (num_waiting < 1)@@ -4127,8 +4205,9 @@ sg_proc_debug_sreq(struct sg_request *srp, int to, char *obp, int len)else if (dur < U32_MAX) /* in-flight or busy (so ongoing) */ n +=
scnprintf(obp + n, len - n, " t_o/elap=%us/%ums", to / 1000, dur);
- n += scnprintf(obp + n, len - n, " sgat=%d op=0x%02x\n", -srp->sgat_h.num_sgat,srp->cmd_opcode);
+ cp = (srp->rq_flags&SGV4_FLAG_HIPRI) ? "hipri " : ""; + n += scnprintf(obp + n, len - n, " sgat=%d %sop=0x%02x\n", +srp->sgat_h.num_sgat, cp,srp->cmd_opcode); return n;
}include/uapi/scsi/sg.h +0
-0include/uapi/scsi/sg.h typedef struct sg_io_hdr { · line 116
Quoted 7 lines
include/uapi/scsi/sg.h +1
-0@@ -110,6 +110,7 @@ typedef struct sg_io_hdr { #define SGV4_FLAG_Q_AT_TAIL SG_FLAG_Q_AT_TAIL #define SGV4_FLAG_Q_AT_HEAD SG_FLAG_Q_AT_HEAD #define SGV4_FLAG_IMMED 0x400 /* for polling with SG_IOR, ignored in SG_IOS */ +#define SGV4_FLAG_HIPRI 0x800/* request will use blk_poll to complete */ /* Output (potentially OR-ed together) in v3::info or v4::info field */ #define SG_INFO_OK_MASK 0x1Cheers,
Hannes
Re: [PATCH v13 44/45] sg: [RFC] add blk_poll support
Quoted 182 lines · Douglas Gilbert
See inline replies ...
Quoted 165 lines · Hannes Reinecke
Quoted 164 lines · Douglas Gilbert
The support is added via the new
SGV4_FLAG_HIPRIcommand flag which causesREQ_HIPRIto be set on the request. Before waiting on an inflight request, it is checked to see if it hasSGV4_FLAG_HIPRI, and if soblk_poll()is called instead of the wait. In situations where only the file descriptor is known (e.g.sg_poll()andioctl(SG_GET_NUM_WAITING)) all inflight requests associated with the file descriptor that haveSGV4_FLAG_HIPRIset, havesg_poll()called on them.Note that the implementation of
blk_poll()callsmq_poll()in the LLD associated with the request. Then for any request found to be ready,blk_poll()invokes thescsi_done()callback. So this means ifblk_poll()returns 1 thensg_rq_end_io()has already been called for the polled request.Signed-off-by Douglas Gilbert <[email protected]>---
drivers/scsi/sg.c | 87 ++++++++++++++++++++++++++++++++++++++++--include/uapi/scsi/sg.h| 1 +2 files changed, 84 insertions(+), 4 deletions(-)drivers/scsi/sg.c +0
-0@@ -123,6 +123,7 @@ enum sg_rq_state { /* N.B. sg_rq_state_arrassumes SG_RS_AWAIT_RCV==2 */
#define SG_FFD_CMD_Q 1 /* clear: only 1 active req per fd */ #define SG_FFD_KEEP_ORPHAN 2 /* policy for this fd */ #define SG_FFD_MMAP_CALLED 3 /* mmap(2) system call made on fd */ +#define SG_FFD_HIPRI_SEEN 4 /* could have HIPRI requests active */
#define SG_FFD_Q_AT_TAIL 5 /* set: queue reqs at tail of blk q */ /* Bit positions (flags) forsg_device::fdev_bmbitmask follow */drivers/scsi/sg.c +4
-0@@ -301,6 +302,8 @@ static struct sg_device *sg_get_dev(int min_dev); static void sg_device_destroy(struct kref *kref); static struct sg_request *sg_mk_srp_sgat(struct sg_fd *sfp, bool first, int db_len); +static int sg_sfp_blk_poll (struct sg_fd *sfp,int loop_count); +static int sg_srp_blk_poll (struct sg_request *srp,int loop_count); #if IS_ENABLED(CONFIG_SCSI_LOGGING) && IS_ENABLED(SG_DEBUG) static const char *sg_rq_st_str(enum sg_rq_state rq_st, bool long_str); #endif@@ -1033,6 +1036,8 @@ sg_execute_cmd(struct sg_fd *sfp, struct sg_request *srp) atomic_inc(&sfp->submitted); set_bit(SG_FRQ_COUNT_ACTIVE, srp->frq_bm); } + if (srp->rq_flags & SGV4_FLAG_HIPRI) + srp->rq->cmd_flags |= REQ_HIPRI; blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk, srp->rq, (int)at_head, sg_rq_end_io);}
drivers/scsi/sg.c +0
-0@@ -1705,6 +1710,12 @@ sg_wait_event_srp(struct file *filp, struct sg_fd *sfp, void__user *p, if (atomic_read(&srp->rq_st) != SG_RS_INFLIGHT)goto skip_wait; /* and skip
_acquire()*/ + if (srp->rq_flags&SGV4_FLAG_HIPRI) {
+ res =sg_srp_blk_poll(srp, -1); /* spin till found */ + if (unlikely(res < 0))
+ return res;
+ gotoskip_wait;
+ }
SG_LOG(3, sfp, "%s: about to wait_event...()\n", __func__); /* usually will be woken up bysg_rq_end_io()callback */ res = wait_event_interruptible(sfp->read_wait,drivers/scsi/sg.c +0
-0@@ -2032,6 +2043,8 @@ sg_ioctl_common(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp, SG_LOG(3, sfp, "%s: SG_GET_PACK_ID=%d\n", __func__, val); return put_user(val, ip);case
SG_GET_NUM_WAITING:
+ if (test_bit(SG_FFD_HIPRI_SEEN, sfp->ffd_bm))
+sg_sfp_blk_poll(sfp, 0); /* LLD may have some ready push */
val =atomic_read(&sfp->waiting);
if (val)
returnput_user(val, ip);drivers/scsi/sg.c +17
-0@@ -2241,6 +2254,59 @@ sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg) } #endif +static int +sg_srp_q_blk_poll (struct sg_request *srp,struct request_queue *q,int loop_count) +{ + int k, n, num; + blk_qc_t cookie = request_to_qc_t(srp->rq->mq_hctx, srp->rq); + + num = (loop_count < 1) ? 1 : loop_count; + for (k = 0; k < num; ++k) { + n = blk_poll(q, cookie, loop_count < 0 /* spin if negative */); + if (n != 0) + return n; + } + return 0; +} + +/* +* Check all requests on this sfp that are both inflight and HIPRI. That check involves calling
+ *blk_poll(spin<-false)loop_counttimes. Ifloop_countis 0 then callblk_pollonce.
+ * Ifloop_countis negative then callblk_poll(spin<-true)) once for each request.
+ * Returns number found (could be 0) or a negated errno value.
+ */drivers/scsi/sg.c +30
-0+static int +sg_sfp_blk_poll (struct sg_fd *sfp,int loop_count) +{ + int res = 0; + int n; + unsigned long idx; + struct sg_request *srp; + struct scsi_device *sdev = sfp->parentdp->device; + struct request_queue *q = sdev ? sdev->request_queue : NULL; + struct xarray *xafp = &sfp->srp_arr; + + if (!q) + return -EINVAL; + xa_for_each(xafp, idx, srp) { + if (atomic_read(&srp->rq_st) != SG_RS_INFLIGHT) + continue; + if (!(srp->rq_flags & SGV4_FLAG_HIPRI)) + continue; + n = sg_srp_q_blk_poll(srp, q, loop_count); + if (unlikely(n < 0)) + return n; + res += n; + } + return res; +} + +static inline int +sg_srp_blk_poll (struct sg_request *srp,int loop_count) +{ + return sg_srp_q_blk_poll(srp,
srp->parentfp->parentdp->device->request_queue,loop_count);drivers/scsi/sg.c +2
-0+} +/*
* Implements the poll(2) system call for this driver. Returns various EPOLL*
* flags OR-ed together.drivers/scsi/sg.c +2
-0@@ -2252,6 +2318,8 @@ sg_poll(struct file *filp, poll_table * wait) __poll_t p_res = 0; struct sg_fd *sfp = filp->private_data; + if (test_bit(SG_FFD_HIPRI_SEEN, sfp->ffd_bm)) + sg_sfp_blk_poll(sfp, 0); /* LLD may have some ready topush up */
num =atomic_read(&sfp->waiting);
if (num < 1) {
poll_wait(filp, &sfp->read_wait, wait);drivers/scsi/sg.c +2
-1@@ -2564,7 +2632,8 @@ sg_rq_end_io(struct request *rq, blk_status_t status) if (likely(rqq_state == SG_RS_AWAIT_RCV)) { /* Wake any sg_read()/ioctl(SG_IORECEIVE) awaiting this req */ - wake_up_interruptible(&sfp->read_wait); + if (!(srp->rq_flags & SGV4_FLAG_HIPRI)) + wake_up_interruptible(&sfp->read_wait); kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN); kref_put(&sfp->f_ref, sg_remove_sfp); } else { /* clean up orphaned request that aren't being kept */@@ -3007,6 +3076,10 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) /* current sg_request protected by SG_RS_BUSY state */Quoted 8 lines
drivers/scsi/sg.c sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) · line 3082
Quoted 6 lines
drivers/scsi/sg.c +4
-0scsi_rp = scsi_req(rq); srp->rq = rq; + if (rq_flags & SGV4_FLAG_HIPRI) { + if (!test_bit(SG_FFD_HIPRI_SEEN, sfp->ffd_bm)) + set_bit(SG_FFD_HIPRI_SEEN, sfp->ffd_bm); + }
test_and_set_bit()?I really would like to see the pseudo code for what
test_and_set_bit()does. I don't want to set the bit if it is already set. IOW why have an atomic write operation and its associated baggage if it is already set.
Quoted 5 lines
If I follow your logic the conditional reduces to:
test_and_set_bit(){ }
which may further reduce to
set_bit();
It would, but then we'd lose the advantage that test_and_set_bit() provides a memory barrier, which set_bit() doesn't.
If we can live with that, fine, set_bit() is it.
But I sincerely doubt we can measure the performance difference between test_and_set_bit() and a simple test_bit(); (on k7 it's two cpu cycles vs. one), but we have to do a branch, too, so it might devolve to the same amount of cycles. In the end, I sincerely doubt it's worth it.
The more important point is:
Does it matter if the bit is already set?
If not I'd just go for the simple 'set_bit()'.
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerPATCH v13 45/45 sg: bump version to 4.0.12
In reply to Douglas GilbertNow that the sg version 4 interface is supported:
- with ioctl(SG_IO) for synchronous/blocking use
- with ioctl(SG_IOSUBMIT) and ioctl(SG_IORECEIVE) for async/non-blocking use
Plus new ioctl(SG_IOSUBMIT_V3) and ioctl(SG_IORECEIVE_V3) potentially replace write() and read() for the sg version 3 interface. Bump major driver version number from 3 to 4.
The main new feature is the removal of the fixed 16 element array of requests per file descriptor. It is replaced by a xarray (eXtensible array) in their parent which is a sg_fd object (i.e. a file descriptor). The sg_request objects are not freed until the owning file descriptor is closed; instead these objects are re-used when multiple commands are sent to the same file descriptor.
--- drivers/scsi/sg.c | 11 ++++++----- include/uapi/scsi/sg.h | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-)
drivers/scsi/sg.c +6 -5
@@ -7,13 +7,14 @@* * Original driver (sg.c): * Copyright (C) 1992 Lawrence Foard -* Version 2 and 3 extensions to driver: -* Copyright (C) 1998 - 2019 Douglas Gilbert +* Version 2, 3 and 4 extensions to driver: +* Copyright (C) 1998 - 2021 Douglas Gilbert +* */ -static int sg_version_num =30901 ;/* [x]xyyzz where [x] empty when x=0 */ -#define SG_VERSION_STR "3.9.01"/* [x]x.[y]y.zz */ -static char *sg_version_date ="20190606" ; +static int sg_version_num =40012 ;/* [x]xyyzz where [x] empty when x=0 */ +#define SG_VERSION_STR "4.0.12"/* [x]x.[y]y.zz */ +static char *sg_version_date ="20210111" ;#include <linux/module.h>
include/uapi/scsi/sg.h +2 -2
@@ -12,9 +12,9 @@* Copyright (C) 1992 Lawrence Foard * * Later extensions (versions 2, 3 and 4) to driver: -* Copyright (C) 1998 - 2018 Douglas Gilbert +* Copyright (C) 1998 - 2021 Douglas Gilbert * -* Version 4.0.11 (20190502) +* Version 4.0.12 (20210111) * This version is for Linux 4 and 5 series kernels. * * Documentation
Signature · 1 line
2.25.1