sg: add v4 interface
Subsystems (2)
Douglas Gilbert
maintainerPATCH v24 36/46 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 | 181 ++++++++++++++++++++++++++++------------------ 1 file changed, 109 insertions(+), 72 deletions(-)
drivers/scsi/sg.c +109 -72
@@ -119,6 +119,7 @@ enum sg_rq_state { /* N.B. sg_rq_state_arr assumes SG_RS_AWAIT_RCV==2 */#define SG_FRQ_NO_US_XFER 3/* no user space transfer of data */ #define SG_FRQ_DEACT_ORPHAN 6/* not keeping orphan so de-activate */ #define SG_FRQ_RECEIVING 7/* guard against multiple receivers */ +#define SG_FRQ_FOR_MMAP 8/* 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 */
@@ -707,7 +708,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;
@@ -1761,6 +1762,8 @@ sg_set_reserved_sz(struct sg_fd *sfp, int want_rsv_sz) srp = sfp->rsv_srp;if (!srp)return -EPROTO ; +if (test_bit (SG_FRQ_FOR_MMAP , srp->frq_bm)) +return -EBUSY ; new_sz =min_t (int, want_rsv_sz, sfp->parentdp->max_sgat_sz); new_sz =max_t (int, new_sz, sfp->sgat_elem_sz); blen = srp->sgat_h.buflen;
@@ -2152,13 +2155,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;
@@ -2184,7 +2213,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) {
@@ -2192,44 +2221,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;
@@ -2240,40 +2262,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->sgat_h, 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
@@ -2852,7 +2882,7 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) goto fini; scmd->cmd_len = cwrp->cmd_len; srp->cmd_opcode = scmd->cmnd[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;
@@ -2978,10 +3008,11 @@ sg_mk_sgat(struct sg_scatter_hold *schp, struct sg_fd *sfp, int minlen) {int j, k, rem_sz, align_sz, order, o_order;int mx_sgat_elems = sfp->parentdp->max_sgat_elems; -unsigned int elem_sz; +unsigned int elem_sz = sfp->sgat_elem_sz;const size_t ptr_sz =sizeof (struct page *);gfp_t mask_ap =GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN | __GFP_ZERO;gfp_t mask_kz =GFP_ATOMIC | __GFP_NOWARN; +struct page **pgp;if (unlikely (minlen <=0 )) {if (minlen <0 )
@@ -2996,30 +3027,35 @@ sg_mk_sgat(struct sg_scatter_hold *schp, struct sg_fd *sfp, int minlen) mx_sgat_elems * ptr_sz, schp->pages);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 */ 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);
@@ -3027,6 +3063,7 @@ sg_mk_sgat(struct sg_scatter_hold *schp, struct sg_fd *sfp, int minlen) elem_sz >>=1 ; goto again; } +b4_alloc_pages:kfree (schp->pages); schp->pages =NULL ;return -ENOMEM ;
@@ -3042,7 +3079,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);
@@ -3172,7 +3209,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; }
@@ -3368,7 +3405,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:
@@ -3395,7 +3432,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 v24 37/46 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 | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-)
drivers/scsi/sg.c +7 -16
@@ -145,7 +145,7 @@ static 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 ;
@@ -2786,17 +2786,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) -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)
@@ -2815,6 +2804,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;
@@ -2882,7 +2872,7 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) goto fini; scmd->cmd_len = cwrp->cmd_len; srp->cmd_opcode = scmd->cmnd[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;
@@ -2893,7 +2883,7 @@ 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 &&blk_rq_aligned (q, (unsigned long )up, dxfer_len)) { srp->rq_info |=SG_INFO_DIRECT_IO ; md =NULL ;
@@ -2904,7 +2894,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 ;
@@ -3781,6 +3771,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)
@@ -4401,6 +4392,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
Douglas Gilbert
maintainerPATCH v24 38/46 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
@@ -644,6 +644,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: https://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 v24 39/46 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.
Remove SG_FFD_MMAP_CALLED bit as it can be deduced from sfp->mmap_sz where a value of 0 implies no mmap() call active.
--- drivers/scsi/sg.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
drivers/scsi/sg.c +13 -9
@@ -125,8 +125,7 @@ enum sg_rq_state { /* N.B. sg_rq_state_arr assumes SG_RS_AWAIT_RCV==2 */#define SG_FFD_FORCE_PACKID 0/* receive only given pack_id/tag */ #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_Q_AT_TAIL 5/* set: queue reqs at tail of blk q */ +#define SG_FFD_Q_AT_TAIL 3/* set: queue reqs at tail of blk q */ /* Bit positions (flags) for sg_device::fdev_bm bitmask follow */ #define SG_FDEV_EXCLUDE 0/* have fd open with O_EXCL */
@@ -235,6 +234,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() */
@@ -724,10 +724,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 (unlikely (sfp->mmap_sz ==0 )) +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 ;
@@ -1759,13 +1763,12 @@ sg_set_reserved_sz(struct sg_fd *sfp, int want_rsv_sz)int new_sz, blen, res;unsigned long iflags;struct sg_scatter_hold n_schp, o_schp; -struct sg_request *srp; +struct sg_request *srp = sfp->rsv_srp;struct xarray *xafp = &sfp->srp_arr; - srp = sfp->rsv_srp;if (!srp)return -EPROTO ; -if (test_bit (SG_FRQ_FOR_MMAP , srp->frq_bm)) +if (SG_RS_ACTIVE (srp) || sfp->mmap_sz >0 )return -EBUSY ; new_sz =min_t (int, want_rsv_sz, sfp->parentdp->max_sgat_sz); new_sz =max_t (int, new_sz, sfp->sgat_elem_sz);
@@ -2179,6 +2182,7 @@ sg_vma_close(struct vm_area_struct *vma)pr_warn ("%s: sfp null\n" , __func__);return ; } + sfp->mmap_sz =0 ;kref_put (&sfp->f_ref, sg_remove_sfp);/* get in: sg_vma_open() */ }
@@ -2269,7 +2273,7 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma) 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) { +if (unlikely (vma->vm_pgoff || req_sz <SG_DEF_SECTOR_SZ )) { res = -EINVAL ;/* only an offset of 0 accepted */ goto fini; }
@@ -2283,7 +2287,7 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma) res = -ENOMEM ; goto fini; } -if (test_and_set_bit (SG_FFD_MMAP_CALLED , sfp->ffd_bm)) { +if (sfp->mmap_sz >0 ) {SG_LOG (1 , sfp,"%s: multiple invocations on this fd\n" , __func__); res = -EADDRINUSE ;
@@ -2300,6 +2304,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;
@@ -3944,8 +3949,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
Douglas Gilbert
maintainerPATCH v24 40/46 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 | 166 +++++++++++++++++++++++++++------------------- 1 file changed, 96 insertions(+), 70 deletions(-)
drivers/scsi/sg.c +96 -70
@@ -94,7 +94,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 */ };
@@ -229,6 +228,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 */
@@ -302,7 +302,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)
@@ -872,20 +874,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
@@ -929,8 +929,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
@@ -945,39 +945,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) {
@@ -998,8 +1006,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)
@@ -1200,10 +1207,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);
@@ -1214,7 +1217,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__,
@@ -1249,10 +1252,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 ;
@@ -1448,7 +1447,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;
@@ -1622,7 +1620,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 */
@@ -1693,9 +1690,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
@@ -2457,8 +2458,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
@@ -3156,9 +3156,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;
@@ -3170,8 +3171,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 */
@@ -3181,14 +3181,37 @@ 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 ) { + }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 )) {if (test_bit (SG_FRQ_SYNC_INVOC , srp->frq_bm))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; +#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 */
@@ -3306,8 +3329,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 ;
@@ -3320,45 +3344,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;
@@ -3431,7 +3458,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);
@@ -3522,7 +3549,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__,
@@ -3671,8 +3698,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 :
@@ -3949,7 +3974,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
Douglas Gilbert
maintainerPATCH v24 41/46 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.
Rename the struct request pointer from rq to rqq and when it read and written to sg_request::rqq use READ_ONCE() and WRITE_ONCE() macros.
--- drivers/scsi/sg.c | 354 +++++++++++++++++++++++++++------------------- 1 file changed, 206 insertions(+), 148 deletions(-)
drivers/scsi/sg.c +206 -148
@@ -119,6 +119,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_FOR_MMAP 8/* request needs PAGE_SIZE elements */ +#define SG_FRQ_COUNT_ACTIVE 9/* 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 */
@@ -217,7 +218,7 @@ struct sg_request { /* active SCSI command or inactive request */unsigned long frq_bm[1 ];/* see SG_FRQ_* defines above */ u8 *sense_bp;/* mempool alloc-ed sense buffer, as needed */ struct sg_fd *parentfp;/* pointer to owning fd, even when on fl */ -struct request *rq;/* released in sg_rq_end_io(), bio kept */ +struct request *rqq;/* released in sg_rq_end_io(), bio kept */ struct bio *bio;/* kept until this req -->SG_RS_INACTIVE */ struct execute_work ew_orph;/* harvest orphan request */ };
@@ -228,11 +229,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 */
@@ -889,11 +891,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 );
@@ -907,15 +911,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 );
@@ -944,45 +954,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); }
@@ -1006,12 +1024,14 @@ 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); -blk_execute_rq_nowait (srp->rq, (int )at_head, sg_rq_end_io); +set_bit (SG_FRQ_COUNT_ACTIVE , srp->frq_bm); + } +blk_execute_rq_nowait (READ_ONCE (srp->rqq), (int )at_head, sg_rq_end_io); }/*
@@ -1086,7 +1106,7 @@ sg_common_write(struct sg_comm_wr_t *cwrp) res = -ENODEV ; goto err_out; } - srp->rq->timeout = cwrp->timeout; +READ_ONCE (srp->rqq)->timeout = cwrp->timeout;sg_execute_cmd (fp, srp);return srp; err_out:
@@ -1655,7 +1675,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)); }
@@ -1671,6 +1691,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,
@@ -1683,14 +1705,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__);
@@ -1948,7 +1972,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);
@@ -2136,11 +2163,16 @@ sg_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)))
@@ -2344,7 +2376,7 @@ 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_cmnd *scmd =blk_mq_rq_to_pdu (srp->rq); +struct scsi_cmnd *scmd =blk_mq_rq_to_pdu (READ_ONCE (srp->rqq));u8 *sbp = scmd ? scmd->sense_buffer :NULL ;if (!sbp)
@@ -2379,32 +2411,18 @@ sg_check_sense(struct sg_device *sdp, struct sg_request *srp, int sense_len)* (sync) usage, sg_ctl_sg_io() waits to be woken up by this callback. */ static void -sg_rq_end_io (struct request *rq,blk_status_t status) +sg_rq_end_io (struct request *rqq,blk_status_t status) {enum sg_rq_state rqq_state =SG_RS_AWAIT_RCV ;int a_resid, slen; -struct scsi_cmnd *scmd =blk_mq_rq_to_pdu (rq); -struct sg_request *srp = rq->end_io_data; +unsigned long iflags; +struct scsi_cmnd *scmd =blk_mq_rq_to_pdu (rqq); +struct sg_request *srp = rqq->end_io_data;struct sg_device *sdp;struct sg_fd *sfp; -if (!srp) { -WARN_ONCE ("%s: srp unexpectedly NULL\n" , __func__); -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 = scmd->result; slen =min_t (int, scmd->sense_len,SCSI_SENSE_BUFFERSIZE );
@@ -2412,7 +2430,7 @@ sg_rq_end_io(struct request *rq, blk_status_t status)if (a_resid) {if (test_bit (SG_FRQ_IS_V4I , srp->frq_bm)) { -if (rq_data_dir (rq) ==READ ) +if (rq_data_dir (rqq) ==READ ) srp->in_resid = a_resid;else srp->s_hdr4.out_resid = a_resid;
@@ -2456,16 +2474,29 @@ 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__); +xa_lock_irqsave (&sfp->srp_arr, iflags); +sg_rq_chg_state_force_ulck (srp, rqq_state); +WRITE_ONCE (srp->rqq,NULL ); +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); + } + } +xa_unlock_irqrestore (&sfp->srp_arr, iflags); +/* * 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. */ - srp->rq =NULL ; -blk_mq_free_request (rq); +blk_mq_free_request (rqq);if (likely (rqq_state ==SG_RS_AWAIT_RCV )) {/* Wake any sg_read()/ioctl(SG_IORECEIVE) awaiting this req */
@@ -2477,10 +2508,6 @@ sg_rq_end_io(struct request *rq, blk_status_t status)schedule_work (&srp->ew_orph.work); }return ; - -early_err: - srp->rq =NULL ; -blk_mq_free_request (rq); }static const struct file_operations sg_fops = {
@@ -2815,7 +2842,7 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir)u32 rq_flags = srp->rq_flags;unsigned int iov_count =0 ;void __user *up; -struct request *rq; +struct request *rqq;struct scsi_cmnd *scmd;struct sg_fd *sfp = cwrp->sfp;struct sg_device *sdp;
@@ -2864,12 +2891,12 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir)* do not want to use BLK_MQ_REQ_NOWAIT here because userspace might * not expect an EWOULDBLOCK from this condition. */ - rq =scsi_alloc_request (q, (r0w ?REQ_OP_DRV_OUT :REQ_OP_DRV_IN ),0 ); -if (IS_ERR (rq)) -return PTR_ERR (rq); + rqq =scsi_alloc_request (q, (r0w ?REQ_OP_DRV_OUT :REQ_OP_DRV_IN ),0 ); +if (IS_ERR (rqq)) +return PTR_ERR (rqq);/* current sg_request protected by SG_RS_BUSY state */ - scmd =blk_mq_rq_to_pdu (rq); - srp->rq = rq; + scmd =blk_mq_rq_to_pdu (rqq); +WRITE_ONCE (srp->rqq, rqq);if (cwrp->u_cmdp) res = sg_fetch_cmnd(cwrp->filp, sfp, cwrp->u_cmdp,
@@ -2883,7 +2910,7 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) 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; + rqq->end_io_data = srp; scmd->retries =SG_DEFAULT_RETRIES ; req_schp = &srp->sgat_h;
@@ -2934,24 +2961,24 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) }if (us_xfer) - res =blk_rq_map_user_iov (q, rq, md, &i,GFP_ATOMIC ); + res =blk_rq_map_user_iov (q, rqq, md, &i,GFP_ATOMIC );kfree (iov);if (IS_ENABLED (CONFIG_SCSI_PROC_FS )) cp ="iov_count > 0" ; }else if (us_xfer) {/* setup for transfer data to/from user space */ - res =blk_rq_map_user (q, rq, md, up, dxfer_len,GFP_ATOMIC ); + res =blk_rq_map_user (q, rqq, md, up, dxfer_len,GFP_ATOMIC );if (IS_ENABLED (CONFIG_SCSI_PROC_FS ) && res)SG_LOG (1 , sfp,"%s: blk_rq_map_user() res=%d\n" , __func__, res); } fini:if (unlikely (res)) {/* failure, free up resources */ - srp->rq =NULL ; -if (us_xfer && rq->bio) -blk_rq_unmap_user (rq->bio); -blk_mq_free_request (rq); +WRITE_ONCE (srp->rqq,NULL ); +if (us_xfer && rqq->bio) +blk_rq_unmap_user (rqq->bio); +blk_mq_free_request (rqq); }else { - srp->bio = rq->bio; + srp->bio = rqq->bio; }SG_LOG ((res ?1 :4 ), sfp,"%s: %s res=%d [0x%pK]\n" , __func__, cp, res, srp);
@@ -2970,19 +2997,19 @@ sg_finish_scsi_blk_rq(struct sg_request *srp) {int ret;struct sg_fd *sfp = srp->parentfp; -struct request *rq = srp->rq; +struct request *rqq =READ_ONCE (srp->rqq);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); } -/* Expect blk_mq_free_request(rq) already called in sg_rq_end_io() */ -if (rq) {/* blk_get_request() may have failed */ - srp->rq =NULL ; -blk_mq_free_request (rq); +/* Expect blk_mq_free_request(rqq) already called in sg_rq_end_io() */ +if (rqq) {/* scsi_alloc_request() may have failed */ +WRITE_ONCE (srp->rqq,NULL ); +blk_mq_free_request (rqq); }if (srp->bio) {bool us_xfer = !test_bit (SG_FRQ_NO_US_XFER , srp->frq_bm);
@@ -3159,19 +3186,30 @@ 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 ) { +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 (test_bit (SG_FRQ_SYNC_INVOC , srp->frq_bm))continue ;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 */
@@ -3181,6 +3219,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
@@ -3189,29 +3235,27 @@ 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 )) {if (test_bit (SG_FRQ_SYNC_INVOC , srp->frq_bm))continue ; - 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 */
@@ -3330,11 +3374,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;
@@ -3343,49 +3388,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;
@@ -3394,7 +3460,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 ) {
@@ -3406,16 +3471,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",
@@ -3458,7 +3521,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);
@@ -3477,7 +3541,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)
@@ -3494,8 +3557,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').
@@ -3507,7 +3568,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__);
@@ -3519,8 +3580,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)) { err =PTR_ERR (srp);
@@ -3536,8 +3595,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",
@@ -3549,20 +3607,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);
@@ -3974,12 +4031,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
Douglas Gilbert
maintainerPATCH v24 42/46 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
@@ -2371,39 +2371,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_cmnd *scmd =blk_mq_rq_to_pdu (READ_ONCE (srp->rqq)); -u8 *sbp = scmd ? scmd->sense_buffer :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
@@ -2415,6 +2382,7 @@ sg_rq_end_io(struct request *rqq, blk_status_t status) {enum sg_rq_state rqq_state =SG_RS_AWAIT_RCV ;int a_resid, slen; +u32 rq_result;unsigned long iflags;struct scsi_cmnd *scmd =blk_mq_rq_to_pdu (rqq);struct sg_request *srp = rqq->end_io_data;
@@ -2424,7 +2392,8 @@ sg_rq_end_io(struct request *rqq, blk_status_t status) sfp = srp->parentfp; sdp = sfp->parentdp; - srp->rq_result = scmd->result; + rq_result = scmd->result; + srp->rq_result = rq_result; slen =min_t (int, scmd->sense_len,SCSI_SENSE_BUFFERSIZE ); a_resid = scmd->resid_len;
@@ -2440,10 +2409,16 @@ sg_rq_end_io(struct request *rqq, 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__, scmd->sense_buffer, slen); + }if (slen >0 ) {if (scmd->sense_buffer && !srp->sense_bp) { srp->sense_bp =mempool_alloc (sg_sense_pool,GFP_ATOMIC );
Signature · 1 line
2.25.1
Douglas Gilbert
maintainerPATCH v24 43/46 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 | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)
drivers/scsi/sg.c +55 -0
@@ -2796,6 +2796,59 @@ 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_VECS ) +return NULL ; + biop =bio_kmalloc (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); + +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); +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 ; + } + } +/* used blk_rq_append_bio() before but this is simpler */ +blk_rq_bio_prep (rqq, bio, num_sgat); + rqq->nr_phys_segments = (1 << schp->page_order) * num_sgat; +return 0 ; +} +static inline void sg_set_map_data (const struct sg_scatter_hold *schp,bool up_valid,struct rq_map_data *mdp)
@@ -2945,6 +2998,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, rqq, r0w); } fini:if (unlikely (res)) {/* failure, free up resources */
Signature · 1 line
2.25.1
Douglas Gilbert
maintainerPATCH v24 44/46 sg: add bio_poll support
In reply to Douglas GilbertAdds the SGV4_FLAG_POLLED command flag which causes REQ_POLLED to be set on the request. This will attempt to do polling in the associated SCSI low level driver (LLD) for completion. An example of an LLD is the driver for a SAS HBA (host bus adapter) such as mpt3sas.
Before waiting on an inflight request, a check is made to see if the SGV4_FLAG_POLLED is set and if so, bio_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_POLLED set, have bio_poll() called on them.
It is important to know blk_execute_rq_nowait() has finished before sending bio_poll() on that request. The SG_RS_INFLIGHT state is set just before blk_execute_rq_nowait() is called so a new bit setting SG_FRQ_ISSUED has been added that is set just after that calls returns.
bio_poll() is char device/driver unfriendly. It needs information such as the associated request_queue placed in a struct block_device object, something the a char device (e.g. a sg device) does not normally have. Enter the new dummy_bdev pointer placed in struct sg_device. It remains NULL until the first time a SGV4_FLAG_POLLED flag is set on a command for a given device. Only then is the dummy block_object created. It would be much easier if bio_poll() just accepted a request_queue argument.
Note that the implementation of bio_poll() calls mq_poll() in the LLD associated with the request. Then for any request found to be ready, bio_poll() invokes the scsi_done() callback. When bio_poll() returns > 0 , sg_rq_end_io() may have been called on the given request. If so the given request will be in await_rcv state.
--- drivers/scsi/sg.c | 169 +++++++++++++++++++++++++++++++++++++++-- include/uapi/scsi/sg.h | 1 + 2 files changed, 164 insertions(+), 6 deletions(-)
drivers/scsi/sg.c +163 -6
@@ -120,12 +120,15 @@ 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_FOR_MMAP 8/* request needs PAGE_SIZE elements */ #define SG_FRQ_COUNT_ACTIVE 9/* sfp->submitted + waiting active */ +#define SG_FRQ_ISSUED 10/* blk_execute_rq_nowait() finished */ +#define SG_FRQ_POLLING 11/* to stop co-incident bio_poll()s */ /* Bit positions (flags) for sg_fd::ffd_bm bitmask follow */ #define SG_FFD_FORCE_PACKID 0/* receive only given pack_id/tag */ #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_Q_AT_TAIL 3/* set: queue reqs at tail of blk q */ +#define SG_FFD_POLLED_SEEN 3/* could have POLLED requests active */ +#define SG_FFD_Q_AT_TAIL 4/* set: queue reqs at tail of blk q */ /* Bit positions (flags) for sg_device::fdev_bm bitmask follow */ #define SG_FDEV_EXCLUDE 0/* have fd open with O_EXCL */
@@ -259,6 +262,7 @@ struct sg_device { /* holds the state of each scsi generic device */unsigned long fdev_bm[1 ];/* see SG_FDEV_* defines above */ char name[DISK_NAME_LEN ];struct cdev *cdev; +struct block_device *dummy_bdev;/* hack for REQ_POLLED */ struct xarray sfp_arr;struct kref d_ref; };
@@ -304,6 +308,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); +static int sg_sfp_bio_poll (struct sg_fd *sfp,int loop_count); +static int sg_srp_q_bio_poll (struct sg_request *srp,struct request_queue *q, +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
@@ -1008,6 +1015,7 @@ static void sg_execute_cmd(struct sg_fd *sfp, struct sg_request *srp) { bool at_head, is_v4h, sync; +struct request *rqq =READ_ONCE (srp->rqq); is_v4h =test_bit (SG_FRQ_IS_V4I , srp->frq_bm); sync =test_bit (SG_FRQ_SYNC_INVOC , srp->frq_bm);
@@ -1031,7 +1039,21 @@ sg_execute_cmd(struct sg_fd *sfp, struct sg_request *srp)atomic_inc (&sfp->submitted);set_bit (SG_FRQ_COUNT_ACTIVE , srp->frq_bm); } -blk_execute_rq_nowait (READ_ONCE (srp->rqq), (int )at_head, sg_rq_end_io); +if (srp->rq_flags &SGV4_FLAG_POLLED ) { +if (test_bit (QUEUE_FLAG_POLL , &rqq->q->queue_flags)) { +set_bit (SG_FFD_POLLED_SEEN , sfp->ffd_bm); + rqq->cmd_flags |=REQ_POLLED ; +if (srp->bio) + srp->bio->bi_opf |=REQ_POLLED ; + }else { +if (srp->bio && (srp->bio->bi_opf &REQ_POLLED )) + srp->bio->bi_opf &= ~REQ_POLLED ; +clear_bit (SG_FFD_POLLED_SEEN , sfp->ffd_bm); + srp->rq_flags &= ~SGV4_FLAG_POLLED ; + } + } +blk_execute_rq_nowait (rqq, (int )at_head, sg_rq_end_io); +set_bit (SG_FRQ_ISSUED , srp->frq_bm); }/*
@@ -1693,6 +1715,13 @@ 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_POLLED ) { +/* call bio_poll(), spinning till found */ + res =sg_srp_q_bio_poll (srp, sdp->device->request_queue,-1 ); +if (res != -ENODATA &&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,
@@ -1972,6 +2001,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_POLLED_SEEN , sfp->ffd_bm)) +sg_sfp_bio_poll (sfp,0 );/* LLD may have some ready */ val =atomic_read (&sfp->waiting);if (val)return put_user (val, ip);
@@ -2156,6 +2187,102 @@ sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)return scsi_ioctl (sdp->device, filp->f_mode, cmd_in, p); } +/* +* If the sg_request object is in SG_RS_AWAIT_RCV state, return 1. Otherwise, +* if it is not inflight, return -ENODATA. This function returns 1 if the +* given object was in inflight state and is in await_rcv state after +* bio_poll() returns 1 or more. If bio_poll() fails, then that (negative) +* value is returned. Otherwise returns 0. Note that bio_poll() may complete +* unrelated requests that share the same q. +*/ +static int +sg_srp_q_bio_poll (struct sg_request *srp,struct request_queue *q,int loop_count) +{ +int n =0 ; +int k, num; +struct bio *biop; +struct sg_fd *sfp = srp->parentfp; +enum sg_rq_state sr_st =atomic_read (&srp->rq_st); + +if (sr_st ==SG_RS_AWAIT_RCV ) +return 1 ; +else if (sr_st !=SG_RS_INFLIGHT ) +return -ENODATA ; +if (test_and_set_bit (SG_FRQ_POLLING , srp->frq_bm)) +return 0 ;/* something else is polling this request */ + biop =READ_ONCE (srp->bio); +if (!biop) { +clear_bit (SG_FRQ_POLLING , srp->frq_bm); +return 0 ; + } +bio_get (biop); +if (READ_ONCE (biop->bi_cookie) ==BLK_QC_T_NONE ) { +if (atomic_read (&srp->rq_st) ==SG_RS_AWAIT_RCV ) + n =1 ; +else +SG_LOG (6 , sfp,"%s: cookie==BLK_QC_T_NONE\n" , __func__); + goto fini; + } +if (!READ_ONCE (biop->bi_bdev)) +WRITE_ONCE (biop->bi_bdev, sfp->parentdp->dummy_bdev); + + num = (loop_count <1 ) ?1 : loop_count; +for (n =0 , k =0 ; k < num; ++k) { + n =bio_poll (biop,NULL , (loop_count <0 ) ?BLK_POLL_NOSLEEP :0 ); +if (n >0 ) { + n = !!(atomic_read (&srp->rq_st) ==SG_RS_AWAIT_RCV ); +break ; + }else if (n <0 ) { +break ; + } +if (atomic_read (&srp->rq_st) !=SG_RS_INFLIGHT ) +break ; + } +fini: +clear_bit (SG_FRQ_POLLING , srp->frq_bm); +bio_put (biop); +return n; +} + +/* +* Check all requests on this sfp that are both inflight and POLLED. That check involves calling +* bio_poll(spin<-false) loop_count times. If loop_count is 0 then call bio_poll once. +* If loop_count is negative then call bio_poll(spin <- true)) once for each request. +* Returns number found (could be 0) or a negated errno value. +*/ +static int +sg_sfp_bio_poll (struct sg_fd *sfp,int loop_count) +{ +int res =0 ; +int n; +unsigned long idx, iflags; +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_lock_irqsave (xafp, iflags); +xa_for_each (xafp, idx, srp) { +if ((srp->rq_flags &SGV4_FLAG_POLLED ) && + !test_bit (SG_FRQ_SYNC_INVOC , srp->frq_bm) && +atomic_read (&srp->rq_st) ==SG_RS_INFLIGHT && +test_bit (SG_FRQ_ISSUED , srp->frq_bm)) { +xa_unlock_irqrestore (xafp, iflags); + n =sg_srp_q_bio_poll (srp, q, loop_count); +if (n == -ENODATA ) + n =0 ; +if (unlikely (n <0 )) +return n; +xa_lock_irqsave (xafp, iflags); + res += n; + } + } +xa_unlock_irqrestore (xafp, iflags); +return res; +} +/* * Implements the poll(2) system call for this driver. Returns various EPOLL* * flags OR-ed together.
@@ -2167,6 +2294,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_POLLED_SEEN , sfp->ffd_bm)) +sg_sfp_bio_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);
@@ -2450,6 +2579,7 @@ sg_rq_end_io(struct request *rqq, blk_status_t status) } }xa_lock_irqsave (&sfp->srp_arr, iflags); +__set_bit (SG_FRQ_ISSUED , srp->frq_bm);sg_rq_chg_state_force_ulck (srp, rqq_state);WRITE_ONCE (srp->rqq,NULL );if (test_bit (SG_FRQ_COUNT_ACTIVE , srp->frq_bm)) {
@@ -2475,7 +2605,8 @@ sg_rq_end_io(struct request *rqq, 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_POLLED )) +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 */
@@ -2515,6 +2646,7 @@ sg_add_device_helper(struct scsi_device *scsidp) sdp =kzalloc (sizeof (*sdp),GFP_KERNEL );if (!sdp)return ERR_PTR (-ENOMEM ); +/* sdp->dummy_bdev starts as NULL until a POLLED command sent on this device */ idr_preload (GFP_KERNEL );write_lock_irqsave (&sg_index_lock, iflags);
@@ -2620,6 +2752,7 @@ sg_add_device(struct device *cl_dev, struct class_interface *cl_intf)write_lock_irqsave (&sg_index_lock, iflags);idr_remove (&sg_index_idr, sdp->index);write_unlock_irqrestore (&sg_index_lock, iflags); +kfree (sdp->dummy_bdev);kfree (sdp); out:
@@ -2648,6 +2781,7 @@ sg_device_destroy(struct kref *kref)idr_remove (&sg_index_idr, sdp->index);write_unlock_irqrestore (&sg_index_lock, flags); +kfree (sdp->dummy_bdev);kfree (sdp); }
@@ -3009,6 +3143,25 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir)blk_mq_free_request (rqq); }else { srp->bio = rqq->bio; +/* check if first POLLED command on this device */ +if (unlikely ((rq_flags &SGV4_FLAG_POLLED ) && !READ_ONCE (sdp->dummy_bdev))) { +bool success = false; +unsigned long iflags; +struct xarray *xadp = &sdp->sfp_arr; +struct block_device *bdevp =kzalloc (sizeof (*bdevp),GFP_KERNEL ); + +xa_lock_irqsave (xadp, iflags); +if (bdevp && !sdp->dummy_bdev) { + bdevp->bd_queue = sdp->device->request_queue; + sdp->dummy_bdev = bdevp; + success = true; + } +xa_unlock_irqrestore (xadp, iflags); +if (!bdevp) + res = -ENOMEM ; +if (!success) +kfree (bdevp); + } }SG_LOG ((res ?1 :4 ), sfp,"%s: %s res=%d [0x%pK]\n" , __func__, cp, res, srp);
@@ -3032,7 +3185,8 @@ 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); +if (atomic_dec_and_test (&sfp->submitted)) +clear_bit (SG_FFD_POLLED_SEEN , sfp->ffd_bm);atomic_dec (&sfp->waiting); }
@@ -3222,6 +3376,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_POLLED_SEEN , sfp->ffd_bm)) +sg_sfp_bio_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 )
@@ -4030,8 +4186,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_POLLED ) ?"polled " :"" ; + 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
@@ -109,6 +109,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_POLLED 0x800/* request polls LLD for completion */ /* 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
maintainerPATCH v24 45/46 sg: add statistics similar to st
In reply to Douglas GilbertUsing the existing statistics gathering framework from the st driver, collect statistics for access via sysfs. The sysstat package already has a utility called tapestat for presenting st statistics. Its author is keen to use the existing tapestat code for showing sg statistics (rather than write a new utility).
In keeping with the sg driver being SCSI command agnostic, the "read" statistics are compiled for requests that have "data-in" user data while write statistics are compiled for requests that have "data-out" user data.
--- drivers/scsi/sg.c | 260 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 228 insertions(+), 32 deletions(-)
drivers/scsi/sg.c +228 -32
@@ -217,7 +217,7 @@ struct sg_request { /* active SCSI command or inactive request */int sense_len;/* actual sense buffer length (data-in) */ atomic_t rq_st;/* request state, holds a enum sg_rq_state */ u8 cmd_opcode;/* first byte of SCSI cdb */ -u64 start_ns;/* starting point of command duration calc */ +ktime_t start_dur;/* start time if before completion */ unsigned long frq_bm[1 ];/* see SG_FRQ_* defines above */ u8 *sense_bp;/* mempool alloc-ed sense buffer, as needed */ struct sg_fd *parentfp;/* pointer to owning fd, even when on fl */
@@ -260,9 +260,11 @@ struct sg_device { /* holds the state of each scsi generic device */u32 index;/* device index number */ atomic_t open_cnt;/* count of opens (perhaps < num(sfds) ) */ unsigned long fdev_bm[1 ];/* see SG_FDEV_* defines above */ +spinlock_t stats_lock;char name[DISK_NAME_LEN ];struct cdev *cdev;struct block_device *dummy_bdev;/* hack for REQ_POLLED */ +struct sg_dev_stats *statsp;struct xarray sfp_arr;struct kref d_ref; };
@@ -280,6 +282,19 @@ struct sg_comm_wr_t { /* arguments to sg_common_write() */const u8 __user *u_cmdp; }; +struct sg_dev_stats {/* copied from drivers/scsi/st.h scsi_tape_stats */ +u64 read_byte_cnt;/* data-in bytes */ +u64 write_byte_cnt;/* data-out bytes */ +u64 read_cnt;/* Count of data-in requests */ +u64 write_cnt;/* Count of data-out requests */ +u64 other_cnt;/* Count of non-data requests */ +u64 resid_cnt;/* Count of cmds with resid_len > 0 */ +u64 tot_read_time;/* time spent completing data-in requests */ +u64 tot_write_time;/* time spent completing data-out requests */ +u64 tot_ndata_time;/* time spent completing non-data requests */ +atomic_t in_flight;/* Number of I/Os in flight */ +}; +/* tasklet or soft irq callback */ static void sg_rq_end_io (struct request *rq,blk_status_t status);/* Declarations of other static functions used before they are defined */
@@ -311,6 +326,8 @@ static struct sg_request *sg_mk_srp_sgat(struct sg_fd *sfp, bool first,static int sg_sfp_bio_poll (struct sg_fd *sfp,int loop_count);static int sg_srp_q_bio_poll (struct sg_request *srp,struct request_queue *q,int loop_count); +static u32 sg_get_dur (struct sg_request *srp,const enum sg_rq_state *sr_stp, +bool *is_durp);#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
@@ -1016,11 +1033,17 @@ sg_execute_cmd(struct sg_fd *sfp, struct sg_request *srp) {bool at_head, is_v4h, sync;struct request *rqq =READ_ONCE (srp->rqq); +struct sg_device *sdp = sfp->parentdp; is_v4h =test_bit (SG_FRQ_IS_V4I , srp->frq_bm); sync =test_bit (SG_FRQ_SYNC_INVOC , srp->frq_bm);SG_LOG (3 , sfp,"%s: is_v4h=%d\n" , __func__, (int )is_v4h); - srp->start_ns =ktime_get_boottime_ns (); +if (sdp->statsp) { +atomic_inc (&sdp->statsp->in_flight); +WRITE_ONCE (srp->start_dur,ktime_get_boottime ()); + }else { +WRITE_ONCE (srp->start_dur,0 ); + } srp->duration =0 ;if (!is_v4h && srp->s_hdr3.interface_id =='\0' )
@@ -1197,11 +1220,47 @@ sg_copy_sense(struct sg_request *srp, bool v4_active)return sb_len_ret; } +static void +sg_do_stats (struct sg_fd *sfp,struct sg_request *srp,bool v4_active) +{ +int dir = v4_active ? srp->s_hdr4.dir : srp->s_hdr3.dxfer_direction; +ktime_t kt =READ_ONCE (srp->start_dur); +u64 ns = (kt >0 ) ?ktime_to_ns (kt) :0 ; +struct sg_device *sdp = sfp->parentdp; +struct sg_dev_stats *statsp = sdp->statsp; + +if (!statsp) +return ; +spin_lock (&sdp->stats_lock); +if (dir ==SG_DXFER_TO_DEV ) {/* data-out, write-like */ + statsp->tot_write_time += ns; + ++statsp->write_cnt; + statsp->write_byte_cnt += srp->sgat_h.dlen; + }else if (dir ==SG_DXFER_FROM_DEV ) {/* data-in, read-like */ + statsp->tot_read_time += ns; + ++statsp->read_cnt; + statsp->read_byte_cnt += srp->sgat_h.dlen; +if (srp->in_resid >0 ) + ++statsp->resid_cnt; + }else {/* no data transfer (e.g. TEST UNIT READY) */ + statsp->tot_ndata_time += ns; + ++statsp->other_cnt; + } +atomic_dec (&statsp->in_flight); +spin_unlock (&sdp->stats_lock); +} +static int sg_rec_state_v3v4 (struct sg_fd *sfp,struct sg_request *srp,bool v4_active) {u32 rq_res = srp->rq_result; +if (sfp->parentdp->statsp) { +const enum sg_rq_state sr_st =SG_RS_BUSY ; + +sg_do_stats (sfp, srp, v4_active); + srp->duration =sg_get_dur (srp, &sr_st,NULL ); + }if (unlikely (srp->rq_result &0xff )) {int sb_len_wr =sg_copy_sense (srp, v4_active);
@@ -1629,49 +1688,41 @@ sg_calc_sgat_param(struct sg_device *sdp) sdp->max_sgat_sz = sz; } -static u32 -sg_calc_rq_dur (const struct sg_request *srp) -{ -ktime_t ts0 =ns_to_ktime (srp->start_ns); -ktime_t now_ts; -s64 diff; - -if (ts0 ==0 ) -return 0 ; -if (unlikely (ts0 ==S64_MAX ))/* _prior_ to issuing req */ -return 999999999 ;/* eye catching */ - now_ts =ktime_get_boottime (); -if (unlikely (ts0 > now_ts)) -return 999999998 ; -/* unlikely req duration will exceed 2**32 milliseconds */ - diff =ktime_ms_delta (now_ts, ts0); -return (diff > (s64 )U32_MAX ) ?3999999999U : (u32 )diff; -} - -/* Return of U32_MAX means srp is inactive state */ static u32 sg_get_dur (struct sg_request *srp,const enum sg_rq_state *sr_stp,bool *is_durp) {bool is_dur = false; -u32 res =U32_MAX ; +s64 dur_ns; +ktime_t start_dur =READ_ONCE (srp->start_dur); +if (ktime_to_ns (start_dur) <=0 ) { + is_dur = true; + dur_ns =0 ; + goto fini; + }switch (sr_stp ? *sr_stp :atomic_read (&srp->rq_st)) { -case SG_RS_INFLIGHT :case SG_RS_BUSY : - res =sg_calc_rq_dur (srp); +if (test_bit (SG_FRQ_ISSUED , srp->frq_bm)) { + dur_ns =ktime_to_ns (start_dur); + is_dur = true; +break ; + } + dur_ns =1 ; +break ; +case SG_RS_INFLIGHT : + dur_ns =ktime_sub (ktime_get_boottime (), start_dur);break ;case SG_RS_AWAIT_RCV :case SG_RS_INACTIVE : - res = srp->duration; + dur_ns =ktime_to_ns (start_dur); is_dur = true;/* completion has occurred, timing finished */ break ; -default : -break ; } +fini:if (is_durp) *is_durp = is_dur; -return res; +return ktime_to_ms (ns_to_ktime (dur_ns)); } static void
@@ -1682,8 +1733,6 @@ sg_fill_request_element(struct sg_fd *sfp, struct sg_request *srp,xa_lock_irqsave (&sfp->srp_arr, iflags); rip->duration =sg_get_dur (srp,NULL ,NULL ); -if (rip->duration ==U32_MAX ) - rip->duration =0 ; rip->orphan =test_bit (SG_FRQ_IS_ORPHAN , srp->frq_bm); rip->sg_io_owned =test_bit (SG_FRQ_SYNC_INVOC , srp->frq_bm); rip->problem = !!(srp->rq_result &SG_ML_RESULT_MSK );
@@ -2513,6 +2562,7 @@ sg_rq_end_io(struct request *rqq, blk_status_t status)int a_resid, slen;u32 rq_result;unsigned long iflags; +ktime_t start_tm;struct scsi_cmnd *scmd =blk_mq_rq_to_pdu (rqq);struct sg_request *srp = rqq->end_io_data;struct sg_device *sdp;
@@ -2539,7 +2589,9 @@ sg_rq_end_io(struct request *rqq, blk_status_t status)SG_LOG (6 , sfp,"%s: pack_id=%d, res=0x%x\n" , __func__, srp->pack_id, rq_result); - srp->duration =sg_calc_rq_dur (srp); + start_tm =READ_ONCE (srp->start_dur); +if (start_tm >0 ) +WRITE_ONCE (srp->start_dur,ktime_sub (ktime_get_boottime (), start_tm));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 ;
@@ -2687,9 +2739,12 @@ sg_add_device_helper(struct scsi_device *scsidp)kfree (sdp);return ERR_PTR (error); } +spin_lock_init (&sdp->stats_lock);return sdp; } +static const struct attribute_group *sg_dev_groups[]; +static int sg_add_device (struct device *cl_dev,struct class_interface *cl_intf) {
@@ -2714,6 +2769,8 @@ sg_add_device(struct device *cl_dev, struct class_interface *cl_intf) error =PTR_ERR (sdp); goto out; } + sdp->statsp =kzalloc (sizeof (*sdp->statsp),GFP_KERNEL ); +/* don't worry if NULL, probably a lot of devices */ error =cdev_add (cdev,MKDEV (SCSI_GENERIC_MAJOR , sdp->index),1 );if (error)
@@ -2723,6 +2780,8 @@ sg_add_device(struct device *cl_dev, struct class_interface *cl_intf)if (sg_sysfs_valid) {struct device *sg_class_member; +if (sdp->statsp) + sg_sysfs_class->dev_groups = sg_dev_groups; sg_class_member = device_create(sg_sysfs_class, cl_dev->parent, MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
@@ -2749,6 +2808,7 @@ sg_add_device(struct device *cl_dev, struct class_interface *cl_intf)return 0 ; cdev_add_err: +kfree (sdp->statsp);write_lock_irqsave (&sg_index_lock, iflags);idr_remove (&sg_index_idr, sdp->index);write_unlock_irqrestore (&sg_index_lock, iflags);
@@ -2777,6 +2837,7 @@ sg_device_destroy(struct kref *kref)*/ xa_destroy (&sdp->sfp_arr); +kfree (sdp->statsp);write_lock_irqsave (&sg_index_lock, flags);idr_remove (&sg_index_idr, sdp->index);write_unlock_irqrestore (&sg_index_lock, flags);
@@ -3957,6 +4018,141 @@ sg_rq_st_str(enum sg_rq_state rq_st, bool long_str) }#endif +static ssize_t read_cnt_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct sg_device *sdp =dev_get_drvdata (dev); +struct sg_dev_stats *sp = sdp->statsp; + +if (!sdp || !sp) +return -EINVAL ; +return sysfs_emit (buf,"%llu\n" , sp->read_cnt); +} +static DEVICE_ATTR_RO (read_cnt); + +static ssize_t read_byte_cnt_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct sg_device *sdp =dev_get_drvdata (dev); +struct sg_dev_stats *sp = sdp->statsp; + +if (!sdp || !sp) +return -EINVAL ; +return sysfs_emit (buf,"%llu\n" , sp->read_byte_cnt); +} +static DEVICE_ATTR_RO (read_byte_cnt); + +static ssize_t read_ns_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct sg_device *sdp =dev_get_drvdata (dev); +struct sg_dev_stats *sp = sdp->statsp; + +if (!sdp || !sp) +return -EINVAL ; +return sysfs_emit (buf,"%llu\n" , sp->tot_read_time); +} +static DEVICE_ATTR_RO (read_ns); + +static ssize_t write_cnt_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct sg_device *sdp =dev_get_drvdata (dev); +struct sg_dev_stats *sp = sdp->statsp; + +if (!sdp || !sp) +return -EINVAL ; +return sysfs_emit (buf,"%llu\n" , sp->write_cnt); +} +static DEVICE_ATTR_RO (write_cnt); + +static ssize_t write_byte_cnt_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct sg_device *sdp =dev_get_drvdata (dev); +struct sg_dev_stats *sp = sdp->statsp; + +if (!sdp || !sp) +return -EINVAL ; +return sysfs_emit (buf,"%llu\n" , sp->write_byte_cnt); +} +static DEVICE_ATTR_RO (write_byte_cnt); + +static ssize_t write_ns_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct sg_device *sdp =dev_get_drvdata (dev); +struct sg_dev_stats *sp = sdp->statsp; + +if (!sdp || !sp) +return -EINVAL ; +return sysfs_emit (buf,"%llu\n" , sp->tot_write_time); +} +static DEVICE_ATTR_RO (write_ns); + +static ssize_t in_flight_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct sg_device *sdp =dev_get_drvdata (dev); +struct sg_dev_stats *sp = sdp->statsp; + +if (!sdp || !sp) +return -EINVAL ; +return sysfs_emit (buf,"%d\n" ,atomic_read (&sp->in_flight)); +} +static DEVICE_ATTR_RO (in_flight); + +static ssize_t io_ns_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct sg_device *sdp =dev_get_drvdata (dev); +struct sg_dev_stats *sp = sdp->statsp; + +if (!sdp || !sp) +return -EINVAL ; +return sysfs_emit (buf,"%llu\n" , + sp->tot_read_time + sp->tot_write_time + sp->tot_ndata_time); +} +static DEVICE_ATTR_RO (io_ns); + +static ssize_t other_cnt_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct sg_device *sdp =dev_get_drvdata (dev); +struct sg_dev_stats *sp = sdp->statsp; + +if (!sdp || !sp) +return -EINVAL ; +return sysfs_emit (buf,"%llu\n" , sp->other_cnt); +} +static DEVICE_ATTR_RO (other_cnt); + +static ssize_t resid_cnt_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct sg_device *sdp =dev_get_drvdata (dev); +struct sg_dev_stats *sp = sdp->statsp; + +if (!sdp || !sp) +return -EINVAL ; +return sysfs_emit (buf,"%llu\n" , sp->resid_cnt); +} +static DEVICE_ATTR_RO (resid_cnt); + +static struct attribute *sg_stats_attrs[] = { + &dev_attr_read_cnt.attr, + &dev_attr_read_byte_cnt.attr, + &dev_attr_read_ns.attr, + &dev_attr_write_cnt.attr, + &dev_attr_write_byte_cnt.attr, + &dev_attr_write_ns.attr, + &dev_attr_in_flight.attr, + &dev_attr_io_ns.attr, + &dev_attr_other_cnt.attr, + &dev_attr_resid_cnt.attr, +NULL , +}; + +static struct attribute_group sg_stats_group = { + .name ="stats" , + .attrs = sg_stats_attrs, +}; + +static const struct attribute_group *sg_dev_groups[] = { + &sg_stats_group, +NULL , +}; +#if IS_ENABLED (SG_PROC_OR_DEBUG_FS )#define SG_SNAPSHOT_DEV_MAX 4
Signature · 1 line
2.25.1
Douglas Gilbert
maintainerPATCH v24 46/46 sg: bump version to 4.0.13
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 - 2022 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 =40013 ;/* [x]xyyzz where [x] empty when x=0 */ +#define SG_VERSION_STR "4.0.13"/* [x]x.[y]y.zz */ +static char *sg_version_date ="20220410" ;#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 - 2022 Douglas Gilbert * -* Version 4.0.11 (20190502) +* Version 4.0.13 (20220406) * This version is for Linux 4 and 5 series kernels. * * Documentation
Signature · 1 line
2.25.1