sg: add v4 interface
Subsystems (2)
Douglas Gilbert
maintainerPATCH v25 26/44 sg: add sg v4 interface support
In reply to Douglas GilbertAdd support for the sg v4 interface based on struct sg_io_v4 found in include/uapi/linux/bsg.h and only previously supported by the bsg driver. Add ioctl(SG_IOSUBMIT) and ioctl(SG_IORECEIVE) for async (non-blocking) usage of the sg v4 interface. Do not accept the v3 interface with these ioctls. Do not accept the v4 interface with this driver's existing write() and read() system calls.
For sync (blocking) usage expand the existing ioctl(SG_IO) to additionally accept the sg v4 interface object.
--- drivers/scsi/sg.c | 436 +++++++++++++++++++++++++++++++++-------- include/uapi/scsi/sg.h | 37 +++- 2 files changed, 394 insertions(+), 79 deletions(-)
drivers/scsi/sg.c +358 -78
@@ -41,7 +41,7 @@ static char *sg_version_date = "20190606";#include <linux/atomic.h> #include <linux/ratelimit.h> #include <linux/uio.h> -#include <linux/cred.h> /* for sg_check_file_access() */ +#include <linux/cred.h> /* for sg_check_file_access() */ #include <linux/proc_fs.h> #include <linux/xarray.h>
@@ -81,6 +81,9 @@ static struct kmem_cache *sg_sense_cache;#define SG_MEMPOOL_MIN_NR 4static mempool_t *sg_sense_pool; +#define uptr64 (usp_val) ((void __user *)(uintptr_t)(usp_val)) +#define cuptr64 (usp_val) ((const void __user *)(uintptr_t)(usp_val)) +/* Following enum contains the states of sg_request::rq_st */ 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) */
@@ -104,6 +107,7 @@ enum sg_rq_state { /* N.B. sg_rq_state_arr assumes SG_RS_AWAIT_RCV==2 */#define SG_ADD_RQ_MAX_RETRIES 40/* to stop infinite _trylock(s) */ /* Bit positions (flags) for sg_request::frq_bm bitmask follow */ +#define SG_FRQ_IS_V4I 0/* true (set) when is v4 interface */ #define SG_FRQ_IS_ORPHAN 1/* owner of request gone */ #define SG_FRQ_SYNC_INVOC 2/* synchronous (blocking) invocation */ #define SG_FRQ_NO_US_XFER 3/* no user space transfer of data */
@@ -167,6 +171,15 @@ struct sg_slice_hdr3 {void __user *usr_ptr; }; +struct sg_slice_hdr4 {/* parts of sg_io_v4 object needed in async usage */ +void __user *sbp;/* derived from sg_io_v4::response */ +u64 usr_ptr;/* hold sg_io_v4::usr_ptr as given (u64) */ +int out_resid; +s16 dir;/* data xfer direction; SG_DXFER_* */ +u16 cmd_len;/* truncated of sg_io_v4::request_len */ +u16 max_sb_len;/* truncated of sg_io_v4::max_response_len */ +}; +struct sg_scatter_hold {/* holding area for scsi scatter gather info */ struct page **pages;/* num_sgat element array of struct page* */ int buflen;/* capacity in bytes (dlen<=buflen) */
@@ -180,7 +193,10 @@ struct sg_fd;struct sg_request {/* active SCSI command or inactive request */ struct sg_scatter_hold sgat_h;/* hold buffer, perhaps scatter list */ -struct sg_slice_hdr3 s_hdr3;/* subset of sg_io_hdr */ +union { +struct sg_slice_hdr3 s_hdr3;/* subset of sg_io_hdr */ +struct sg_slice_hdr4 s_hdr4;/* reduced size struct sg_io_v4 */ + };u32 duration;/* cmd duration in milliseconds */ u32 rq_flags;/* hold user supplied flags */ u32 rq_idx;/* my index within parent's srp_arr */
@@ -240,7 +256,10 @@ struct sg_device { /* holds the state of each scsi generic device */struct sg_comm_wr_t {/* arguments to sg_common_write() */ int timeout;unsigned long frq_bm[1 ];/* see SG_FRQ_* defines above */ -struct sg_io_hdr *h3p; +union {/* selector is frq_bm.SG_FRQ_IS_V4I */ +struct sg_io_hdr *h3p; +struct sg_io_v4 *h4p; + };u8 *cmnd; };
@@ -249,13 +268,13 @@ static enum rq_end_io_ret sg_rq_end_io(struct request *rq, blk_status_t status);/* Declarations of other static functions used before they are defined */ static int sg_proc_init (void );static int sg_start_req (struct sg_request *srp,u8 *cmd,int cmd_len, -int dxfer_dir); +struct sg_io_v4 *h4p,int dxfer_dir);static void sg_finish_scsi_blk_rq (struct sg_request *srp);static int sg_mk_sgat (struct sg_scatter_hold *schp,struct sg_fd *sfp,int minlen);static void sg_remove_sgat_helper (struct sg_fd *sfp,struct sg_scatter_hold *schp); -static int sg_submit (struct file *filp,struct sg_fd *sfp, -struct sg_io_hdr *hp,bool sync, -struct sg_request **o_srp); +static int sg_v3_submit (struct file *filp,struct sg_fd *sfp, +struct sg_io_hdr *hp,bool sync, +struct sg_request **o_srp);static struct sg_request *sg_common_write (struct sg_fd *sfp,struct sg_comm_wr_t *cwrp); static int sg_read_append(struct sg_request *srp, void __user *outp,
@@ -263,12 +282,12 @@ static int sg_read_append(struct sg_request *srp, void __user *outp,static void sg_remove_sgat (struct sg_request *srp);static struct sg_fd *sg_add_sfp (struct sg_device *sdp);static void sg_remove_sfp (struct kref *); -static struct sg_request *sg_find_srp_by_id (struct sg_fd *sfp,int pack_id, +static struct sg_request *sg_find_srp_by_id (struct sg_fd *sfp,int id,bool *busy);static struct sg_request *sg_setup_req (struct sg_fd *sfp,int dxfr_len,struct sg_comm_wr_t *cwrp);static void sg_deact_request (struct sg_fd *sfp,struct sg_request *srp); -static struct sg_device *sg_get_dev (int dev); +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);
@@ -278,8 +297,11 @@ static const char *sg_rq_st_str(enum sg_rq_state rq_st, bool long_str);#define SZ_SG_HEADER ((int)sizeof(struct sg_header))/* v1 and v2 header */ #define SZ_SG_IO_HDR ((int)sizeof(struct sg_io_hdr))/* v3 header */ +#define SZ_SG_IO_V4 ((int)sizeof(struct sg_io_v4))/* v4 header (in bsg.h) */ #define SZ_SG_REQ_INFO ((int)sizeof(struct sg_req_info)) +/* There is a assert that SZ_SG_IO_V4 >= SZ_SG_IO_HDR in first function */ +#define SG_IS_DETACHING (sdp) test_bit(SG_FDEV_DETACHING, (sdp)->fdev_bm)#define SG_HAVE_EXCLUDE (sdp) test_bit(SG_FDEV_EXCLUDE, (sdp)->fdev_bm)#define SG_RS_ACTIVE (srp) (atomic_read(&(srp)->rq_st) != SG_RS_INACTIVE)
@@ -336,6 +358,10 @@ static const char *sg_rq_st_str(enum sg_rq_state rq_st, bool long_str);static int sg_check_file_access (struct file *filp,const char *caller) { +/* can't put following in declarations where it belongs */ +compiletime_assert (SZ_SG_IO_V4 >=SZ_SG_IO_HDR , +"struct sg_io_v4 should be larger than sg_io_hdr" ); +if (filp->f_cred !=current_real_cred ()) {pr_err_once ("%s: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n" , caller,task_tgid_vnr (current), current->comm);
@@ -425,21 +451,18 @@ sg_open(struct inode *inode, struct file *filp) o_excl = !!(op_flags &O_EXCL ); non_block = !!(op_flags &O_NONBLOCK );if (o_excl && ((op_flags &O_ACCMODE ) ==O_RDONLY )) -return -EPERM ;/* Can't lock it with read only access */ +return -EPERM ;/* not permitted, need write access for O_EXCL */ sdp =sg_get_dev (min_dev);/* increments sdp->d_ref */ if (IS_ERR (sdp))return PTR_ERR (sdp); -/* This driver's module count bumped by fops_get in <linux/fs.h> */ /* Prevent the device driver from vanishing while we sleep */ res =scsi_device_get (sdp->device);if (res) goto sg_put; - res =scsi_autopm_get_device (sdp->device);if (res) goto sdp_put; - res =sg_allow_if_err_recovery (sdp, non_block);if (res) goto error_out;
@@ -476,9 +499,10 @@ sg_open(struct inode *inode, struct file *filp) } filp->private_data = sfp; + sfp->tid = (current ? current->pid :-1 );mutex_unlock (&sdp->open_rel_lock); -SG_LOG (3 , sfp,"%s: minor=%d, op_flags=0x%x; %s count after=%d%s\n" , - __func__, min_dev, op_flags,"device open" , o_count, +SG_LOG (3 , sfp,"%s: o_count after=%d on minor=%d, op_flags=0x%x%s\n" , + __func__, o_count, min_dev, op_flags, ((op_flags &O_NONBLOCK ) ?" O_NONBLOCK" :"" )); res =0 ;
@@ -501,8 +525,13 @@ sg_open(struct inode *inode, struct file *filp) goto sg_put; } -/* Release resources associated with a successful sg_open() -* Returns 0 on success, else a negated errno value */ +/* +* Release resources associated with a prior, successful sg_open(). It can be +* seen as the (final) close() call on a sg device file descriptor in the user +* space. The real work releasing all resources associated with this file +* descriptor is done by sg_remove_sfp_usercontext() which is scheduled by +* sg_remove_sfp(). +*/ static int sg_release (struct inode *inode,struct file *filp) {
@@ -604,7 +633,7 @@ sg_write(struct file *filp, const char __user *p, size_t count, loff_t *ppos) __func__);return -EPERM ; } - res =sg_submit (filp, sfp, h3p, false,NULL ); + res =sg_v3_submit (filp, sfp, h3p, false,NULL );return res <0 ? res : (int )count; } to_v2:
@@ -681,7 +710,7 @@ 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 (!xa_empty (&sfp->srp_arr)) +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 */
@@ -713,8 +742,8 @@ sg_fetch_cmnd(struct file *filp, struct sg_fd *sfp, const u8 __user *u_cdbp, }static int -sg_submit (struct file *filp,struct sg_fd *sfp,struct sg_io_hdr *hp, -bool sync,struct sg_request **o_srp) +sg_v3_submit (struct file *filp,struct sg_fd *sfp,struct sg_io_hdr *hp, +bool sync,struct sg_request **o_srp) {int res, timeout;unsigned long ul_timeout;
@@ -748,6 +777,67 @@ sg_submit(struct file *filp, struct sg_fd *sfp, struct sg_io_hdr *hp,return 0 ; } +static int +sg_submit_v4 (struct file *filp,struct sg_fd *sfp,void __user *p, +struct sg_io_v4 *h4p,bool sync,struct sg_request **o_srp) +{ +int timeout, res; +unsigned long ul_timeout; +struct sg_request *srp; +struct sg_comm_wr_t cwr; +u8 cmnd[SG_MAX_CDB_SIZE ]; + +if (h4p->flags &SG_FLAG_MMAP_IO ) { +int len =0 ; + +if (h4p->din_xferp) + len = h4p->din_xfer_len; +else if (h4p->dout_xferp) + len = h4p->dout_xfer_len; + res =sg_chk_mmap (sfp, h4p->flags, len); +if (res) +return res; + } +/* once v4 (or 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 (h4p->timeout); + timeout = min_t(unsigned long, ul_timeout, INT_MAX); + res =sg_fetch_cmnd (filp, sfp,cuptr64 (h4p->request), h4p->request_len, + cmnd); +if (res) +return res; + 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; + cwr.timeout = timeout; + cwr.cmnd = cmnd; + srp =sg_common_write (sfp, &cwr); +if (IS_ERR (srp)) +return PTR_ERR (srp); +if (o_srp) + *o_srp = srp; +return res; +} + +static int +sg_ctl_iosubmit (struct file *filp,struct sg_fd *sfp,void __user *p) +{ +int res; +u8 hdr_store[SZ_SG_IO_V4 ]; +struct sg_io_v4 *h4p = (struct sg_io_v4 *)hdr_store; +struct sg_device *sdp = sfp->parentdp; + + res =sg_allow_if_err_recovery (sdp, (filp->f_flags &O_NONBLOCK )); +if (res) +return res; +if (copy_from_user (hdr_store, p,SZ_SG_IO_V4 )) +return -EFAULT ; +if (h4p->guard =='Q' ) +return sg_submit_v4 (filp, sfp, p, h4p, false,NULL ); +return -EPERM ; +} +#if IS_ENABLED (CONFIG_SG_LOG_ACTIVE ) static void sg_rq_state_fail_msg(struct sg_fd *sfp, enum sg_rq_state exp_old_st,
@@ -857,16 +947,46 @@ sg_rq_state_chg(struct sg_request *srp, enum sg_rq_state old_st,return 0 ; } +static void +sg_execute_cmd (struct sg_fd *sfp,struct sg_request *srp) +{ +bool at_head, is_v4h, sync; + + 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 (); + srp->duration =0 ; + +if (!is_v4h && srp->s_hdr3.interface_id =='\0' ) + at_head = true;/* backward compatibility: v1+v2 interfaces */ +else if (test_bit (SG_FFD_Q_AT_TAIL , sfp->ffd_bm)) +/* cmd flags can override sfd setting */ + at_head = !!(srp->rq_flags &SG_FLAG_Q_AT_HEAD ); +else /* this sfd is defaulting to head */ + 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__); + +/* >>>>>>> send cmd/req off to other levels <<<<<<<< */ +if (!sync) +atomic_inc (&sfp->submitted); + srp->rq->end_io = sg_rq_end_io; +blk_execute_rq_nowait (srp->rq, at_head); +} +/* * All writes and submits converge on this function to launch the SCSI * command/request (via blk_execute_rq_nowait). Returns a pointer to a * sg_request object holding the request just issued or a negated errno * value twisted by ERR_PTR. +* N.B. pack_id placed in sg_io_v4::request_extra field. */ static struct sg_request *sg_common_write (struct sg_fd *sfp,struct sg_comm_wr_t *cwrp) { -bool at_head;int res =0 ;int dxfr_len, dir, cmd_len;int pack_id =SG_PACK_ID_WILDCARD ;
@@ -874,12 +994,32 @@ sg_common_write(struct sg_fd *sfp, struct sg_comm_wr_t *cwrp)struct sg_device *sdp = sfp->parentdp;struct sg_request *srp;struct sg_io_hdr *hi_p; - - hi_p = cwrp->h3p; - dir = hi_p->dxfer_direction; - dxfr_len = hi_p->dxfer_len; - rq_flags = hi_p->flags; - pack_id = hi_p->pack_id; +struct sg_io_v4 *h4p; + +if (test_bit (SG_FRQ_IS_V4I , cwrp->frq_bm)) { + h4p = cwrp->h4p; + hi_p =NULL ; + dxfr_len =0 ; + dir =SG_DXFER_NONE ; + rq_flags = h4p->flags; + pack_id = h4p->request_extra; +if (h4p->din_xfer_len && h4p->dout_xfer_len) { +return ERR_PTR (-EOPNOTSUPP ); + }else if (h4p->din_xfer_len) { + dxfr_len = h4p->din_xfer_len; + dir =SG_DXFER_FROM_DEV ; + }else if (h4p->dout_xfer_len) { + dxfr_len = h4p->dout_xfer_len; + dir =SG_DXFER_TO_DEV ; + } + }else {/* sg v3 interface so hi_p valid */ + h4p =NULL ; + hi_p = cwrp->h3p; + dir = hi_p->dxfer_direction; + dxfr_len = hi_p->dxfer_len; + rq_flags = hi_p->flags; + pack_id = hi_p->pack_id; + }if (dxfr_len >=SZ_256M )return ERR_PTR (-EINVAL );
@@ -889,13 +1029,23 @@ sg_common_write(struct sg_fd *sfp, struct sg_comm_wr_t *cwrp) srp->rq_flags = rq_flags; srp->pack_id = pack_id; - cmd_len = hi_p->cmd_len; -memcpy (&srp->s_hdr3, hi_p,sizeof (srp->s_hdr3)); +if (h4p) { +memset (&srp->s_hdr4,0 ,sizeof (srp->s_hdr4)); + srp->s_hdr4.usr_ptr = h4p->usr_ptr; + srp->s_hdr4.sbp =uptr64 (h4p->response); + srp->s_hdr4.max_sb_len = h4p->max_response_len; + srp->s_hdr4.cmd_len = h4p->request_len; + srp->s_hdr4.dir = dir; + cmd_len = h4p->request_len; + }else {/* v3 interface active */ + cmd_len = hi_p->cmd_len; +memcpy (&srp->s_hdr3, hi_p,sizeof (srp->s_hdr3)); + } srp->cmd_opcode = cwrp->cmnd[0 ];/* hold opcode of command for debug */ SG_LOG (4 , sfp,"%s: opcode=0x%02x, cdb_sz=%d, pack_id=%d\n" , __func__, (int )cwrp->cmnd[0 ], cmd_len, pack_id); - res =sg_start_req (srp, cwrp->cmnd, cmd_len, dir); + res =sg_start_req (srp, cwrp->cmnd, cmd_len, h4p, dir);if (res <0 )/* probably out of space --> -ENOMEM */ goto err_out;if (unlikely (SG_IS_DETACHING (sdp))) {
@@ -903,25 +1053,7 @@ sg_common_write(struct sg_fd *sfp, struct sg_comm_wr_t *cwrp) goto err_out; } srp->rq->timeout = cwrp->timeout; -kref_get (&sfp->f_ref);/* sg_rq_end_io() does kref_put(). */ - res =sg_rq_state_chg (srp,SG_RS_BUSY ,SG_RS_INFLIGHT , false, - __func__); -if (res) - goto err_out; - srp->start_ns =ktime_get_boottime_ns (); - srp->duration =0 ; - -if (srp->s_hdr3.interface_id =='\0' ) - at_head = true;/* backward compatibility: v1+v2 interfaces */ -else if (test_bit (SG_FFD_Q_AT_TAIL , sfp->ffd_bm)) -/* cmd flags can override sfd setting */ - at_head = !!(srp->rq_flags &SG_FLAG_Q_AT_HEAD ); -else /* this sfd is defaulting to head */ - at_head = !(srp->rq_flags &SG_FLAG_Q_AT_TAIL ); -if (!test_bit (SG_FRQ_SYNC_INVOC , srp->frq_bm)) -atomic_inc (&sfp->submitted); - srp->rq->end_io = sg_rq_end_io; -blk_execute_rq_nowait (srp->rq, at_head); +sg_execute_cmd (sfp, srp);return srp; err_out:sg_deact_request (sfp, srp);
@@ -933,7 +1065,6 @@ sg_common_write(struct sg_fd *sfp, struct sg_comm_wr_t *cwrp)* sg_ctl_ioreceive(). wait_event_interruptible will return if this one * returns true (or an event like a signal (e.g. control-C) occurs). */ -static inline bool sg_get_ready_srp (struct sg_fd *sfp,struct sg_request **srpp,int pack_id,bool *busy)
@@ -949,7 +1080,7 @@ sg_get_ready_srp(struct sg_fd *sfp, struct sg_request **srpp, int pack_id,* negated errno value. */ static int -sg_copy_sense (struct sg_request *srp) +sg_copy_sense (struct sg_request *srp,bool v4_active) {int sb_len_ret =0 ;int scsi_stat;
@@ -959,11 +1090,18 @@ sg_copy_sense(struct sg_request *srp)if ((scsi_stat &SAM_STAT_CHECK_CONDITION ) || (srp->sense_bp[0 ] &0x70 ) ==0x70 ) {int sb_len =min_t (int,SCSI_SENSE_BUFFERSIZE , srp->sense_len); -int mx_sb_len = srp->s_hdr3.mx_sb_len; +int mx_sb_len;u8 *sbp = srp->sense_bp; -void __user *up = srp->s_hdr3.sbp; +void __user *up; srp->sense_bp =NULL ; +if (v4_active) { + up =uptr64 (srp->s_hdr4.sbp); + mx_sb_len = srp->s_hdr4.max_sb_len; + }else { + up = (void __user *)srp->s_hdr3.sbp; + mx_sb_len = srp->s_hdr3.mx_sb_len; + }if (up && mx_sb_len >0 && sbp) { sb_len =min_t (int, mx_sb_len, sb_len);/* Additional sense length field */
@@ -980,14 +1118,16 @@ sg_copy_sense(struct sg_request *srp) }static int -sg_rec_state_v3 (struct sg_fd *sfp,struct sg_request *srp) +sg_rec_state_v3v4 (struct sg_fd *sfp,struct sg_request *srp,bool v4_active) { -int sb_len_wr;u32 rq_res = srp->rq_result; - sb_len_wr =sg_copy_sense (srp); -if (sb_len_wr <0 ) -return sb_len_wr; +if (unlikely (srp->rq_result &0xff )) { +int sb_len_wr =sg_copy_sense (srp, v4_active); + +if (sb_len_wr <0 ) +return sb_len_wr; + }if (rq_res &SG_ML_RESULT_MSK ) srp->rq_info |=SG_INFO_CHECK ;if (unlikely (SG_IS_DETACHING (sfp->parentdp)))
@@ -1014,7 +1154,7 @@ sg_receive_v3(struct sg_fd *sfp, struct sg_request *srp, size_t count, goto err_out; }SG_LOG (3 , sfp,"%s: srp=0x%pK\n" , __func__, srp); - err =sg_rec_state_v3 (sfp, srp); + err =sg_rec_state_v3v4 (sfp, srp, false);memset (hp,0 ,sizeof (*hp));memcpy (hp, &srp->s_hdr3,sizeof (srp->s_hdr3)); hp->sb_len_wr = srp->sense_len;
@@ -1038,11 +1178,104 @@ sg_receive_v3(struct sg_fd *sfp, struct sg_request *srp, size_t count,return err; } +static int +sg_receive_v4 (struct sg_fd *sfp,struct sg_request *srp,void __user *p, +struct sg_io_v4 *h4p) +{ +int err, err2; +u32 rq_result = srp->rq_result; + +SG_LOG (3 , sfp,"%s: p=%s, h4p=%s\n" , __func__, + (p ?"given" :"NULL" ), (h4p ?"given" :"NULL" )); + err =sg_rec_state_v3v4 (sfp, srp, true); + h4p->guard ='Q' ; + h4p->protocol =0 ; + h4p->subprotocol =0 ; + h4p->device_status = rq_result &0xff ; + h4p->driver_status =driver_byte (rq_result); + h4p->transport_status =host_byte (rq_result); + h4p->response_len = srp->sense_len; + h4p->info = srp->rq_info; + h4p->flags = srp->rq_flags; + h4p->duration = srp->duration; +switch (srp->s_hdr4.dir) { +case SG_DXFER_FROM_DEV : + h4p->din_xfer_len = srp->sgat_h.dlen; +break ; +case SG_DXFER_TO_DEV : + h4p->dout_xfer_len = srp->sgat_h.dlen; +break ; +default : +break ; + } + h4p->din_resid = srp->in_resid; + h4p->dout_resid = srp->s_hdr4.out_resid; + h4p->usr_ptr = srp->s_hdr4.usr_ptr; + h4p->response = (u64 )srp->s_hdr4.sbp; + h4p->request_extra = srp->pack_id; +if (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_INACTIVE , + false, __func__); +if (err2) + err = err ? err : err2; +sg_finish_scsi_blk_rq (srp); +sg_deact_request (sfp, srp); +return err <0 ? err :0 ; +} +/* -* Completes a v3 request/command. Called from sg_read {v2 or v3}, -* ioctl(SG_IO) {for v3}, or from ioctl(SG_IORECEIVE) when its -* completing a v3 request/command. +* Called when ioctl(SG_IORECEIVE) received. Expects a v4 interface object. +* Checks if O_NONBLOCK file flag given, if not checks given 'flags' field +* to see if SGV4_FLAG_IMMED is set. Either of these implies non blocking. +* When non-blocking and there is no request waiting, yields EAGAIN; +* otherwise it waits (i.e. it "blocks"). */ +static int +sg_ctl_ioreceive (struct file *filp,struct sg_fd *sfp,void __user *p) +{ +bool non_block = !!(filp->f_flags &O_NONBLOCK ); +bool busy; +int res, id; +int pack_id =SG_PACK_ID_WILDCARD ; +u8 v4_holder[SZ_SG_IO_V4 ]; +struct sg_io_v4 *h4p = (struct sg_io_v4 *)v4_holder; +struct sg_device *sdp = sfp->parentdp; +struct sg_request *srp; + + res =sg_allow_if_err_recovery (sdp, non_block); +if (res) +return res; +/* Get first three 32 bit integers: guard, proto+subproto */ +if (copy_from_user (h4p, p,SZ_SG_IO_V4 )) +return -EFAULT ; +/* for v4: protocol=0 --> SCSI; subprotocol=0 --> SPC++ */ +if (h4p->guard !='Q' || h4p->protocol !=0 || h4p->subprotocol !=0 ) +return -EPERM ; +if (h4p->flags &SGV4_FLAG_IMMED ) + non_block = true;/* set by either this or O_NONBLOCK */ +SG_LOG (3 , sfp,"%s: non_block(+IMMED)=%d\n" , __func__, non_block); +/* read in part of v3 or v4 header for pack_id or tag based find */ + id = pack_id; + srp =sg_find_srp_by_id (sfp, id, &busy); +while (!srp) {/* nothing available so wait on packet or */ +if (unlikely (!busy &&SG_IS_DETACHING (sdp))) +return -ENODEV ; +if (non_block) +return -EAGAIN ; + res =wait_event_interruptible (sfp->read_wait, +sg_get_ready_srp (sfp, &srp, + id, &busy)); +if (unlikely (!busy &&SG_IS_DETACHING (sdp))) +return -ENODEV ; +if (res)/* -ERESTARTSYS as signal hit process */ +return res; + } +return sg_receive_v4 (sfp, srp, p, h4p); +} +static int sg_read_v1v2 (void __user *buf,int count,struct sg_fd *sfp,struct sg_request *srp)
@@ -1316,6 +1549,8 @@ sg_fill_request_element(struct sg_fd *sfp, struct sg_request *srp, rip->sg_io_owned =test_bit (SG_FRQ_SYNC_INVOC , srp->frq_bm); rip->problem = !!(srp->rq_result &SG_ML_RESULT_MSK ); rip->pack_id = srp->pack_id; + rip->usr_ptr =test_bit (SG_FRQ_IS_V4I , srp->frq_bm) ? +uptr64 (srp->s_hdr4.usr_ptr) : srp->s_hdr3.usr_ptr; rip->usr_ptr = srp->s_hdr3.usr_ptr;xa_unlock_irqrestore (&sfp->srp_arr, iflags); }
@@ -1333,7 +1568,7 @@ sg_rq_landed(struct sg_device *sdp, struct sg_request *srp)*/ static int sg_wait_event_srp (struct file *filp,struct sg_fd *sfp,void __user *p, -struct sg_request *srp) +struct sg_io_v4 *h4p,struct sg_request *srp) {int res;enum sg_rq_state sr_st;
@@ -1361,7 +1596,10 @@ sg_wait_event_srp(struct file *filp, struct sg_fd *sfp, void __user *p, res =sg_rq_state_chg (srp, sr_st,SG_RS_BUSY , false, __func__);if (unlikely (res))return res; - res =sg_receive_v3 (sfp, srp,SZ_SG_IO_HDR , p); +if (test_bit (SG_FRQ_IS_V4I , srp->frq_bm)) + res =sg_receive_v4 (sfp, srp, p, h4p); +else + res =sg_receive_v3 (sfp, srp,SZ_SG_IO_HDR , p);return (res <0 ) ? res :0 ; }
@@ -1375,8 +1613,9 @@ sg_ctl_sg_io(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp, {int res;struct sg_request *srp =NULL ; -u8 hu8arr[SZ_SG_IO_HDR ]; +u8 hu8arr[SZ_SG_IO_V4 ];struct sg_io_hdr *h3p = (struct sg_io_hdr *)hu8arr; +struct sg_io_v4 *h4p = (struct sg_io_v4 *)hu8arr;SG_LOG (3 , sfp,"%s: SG_IO%s\n" , __func__, ((filp->f_flags &O_NONBLOCK ) ?" O_NONBLOCK ignored" :"" ));
@@ -1385,15 +1624,25 @@ sg_ctl_sg_io(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,return res;if (get_sg_io_hdr (h3p, buf))return -EFAULT ; -if (h3p->interface_id =='S' ) - res =sg_submit (filp, sfp, h3p, true, &srp); -else +if (h3p->interface_id =='Q' ) { +/* copy in rest of sg_io_v4 object */ +if (copy_from_user (hu8arr +SZ_SG_IO_HDR , + ((u8 __user *)buf) +SZ_SG_IO_HDR , +SZ_SG_IO_V4 -SZ_SG_IO_HDR )) +return -EFAULT ; + res =sg_submit_v4 (filp, sfp, buf, h4p, true, &srp); + }else if (h3p->interface_id =='S' ) { + res =sg_v3_submit (filp, sfp, h3p, true, &srp); + }else { +pr_info_once ("sg: %s: v3 or v4 interface only here\n" , + __func__);return -EPERM ; + }if (unlikely (res <0 ))return res;if (!srp)/* mrq case: already processed all responses */ return res; - res =sg_wait_event_srp (filp, sfp, buf, srp); + res =sg_wait_event_srp (filp, sfp, buf, h4p, srp);if (res) SG_LOG(1, sfp, "%s: %s=0x%pK state: %s\n", __func__, "unexpected srp", srp,
@@ -1562,6 +1811,12 @@ sg_ioctl_common(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,switch (cmd_in) {case SG_IO :return sg_ctl_sg_io (filp, sdp, sfp, p); +case SG_IOSUBMIT : +SG_LOG (3 , sfp,"%s: SG_IOSUBMIT\n" , __func__); +return sg_ctl_iosubmit (filp, sfp, p); +case SG_IORECEIVE : +SG_LOG (3 , sfp,"%s: SG_IORECEIVE\n" , __func__); +return sg_ctl_ioreceive (filp, sfp, p);case SG_GET_SCSI_ID :return sg_ctl_scsi_id (sdev, sfp, p);case SG_SET_FORCE_PACK_ID :
@@ -2015,8 +2270,16 @@ sg_rq_end_io(struct request *rq, blk_status_t status) slen =min_t (int, scmd->sense_len,SCSI_SENSE_BUFFERSIZE ); a_resid = scmd->resid_len; -if (a_resid) - srp->in_resid = a_resid; +if (a_resid) { +if (test_bit (SG_FRQ_IS_V4I , srp->frq_bm)) { +if (rq_data_dir (rq) ==READ ) + srp->in_resid = a_resid; +else + srp->s_hdr4.out_resid = a_resid; + }else { + srp->in_resid = a_resid; + } + }SG_LOG (6 , sfp,"%s: pack_id=%d, res=0x%x\n" , __func__, srp->pack_id, srp->rq_result);
@@ -2427,7 +2690,8 @@ sg_set_map_data(const struct sg_scatter_hold *schp, bool up_valid, }static int -sg_start_req (struct sg_request *srp,u8 *cmd,int cmd_len,int dxfer_dir) +sg_start_req (struct sg_request *srp,u8 *cmd,int cmd_len, +struct sg_io_v4 *h4p,int dxfer_dir) {bool reserved, us_xfer;int res =0 ;
@@ -2443,14 +2707,30 @@ sg_start_req(struct sg_request *srp, u8 *cmd, int cmd_len, int dxfer_dir)struct request_queue *q;struct rq_map_data *md = (void *)srp;/* want any non-NULL value */ __maybe_unused const char *cp ="" ; -struct sg_slice_hdr3 *sh3p = &srp->s_hdr3;struct rq_map_data map_data; sdp = sfp->parentdp; - up = sh3p->dxferp; - dxfer_len = (int )sh3p->dxfer_len; - iov_count = sh3p->iovec_count; - r0w = dxfer_dir ==SG_DXFER_TO_DEV ?WRITE :READ ; +if (h4p) { +if (dxfer_dir ==SG_DXFER_TO_DEV ) { + r0w =WRITE ; + up =uptr64 (h4p->dout_xferp); + dxfer_len = (int )h4p->dout_xfer_len; + iov_count = h4p->dout_iovec_count; + }else if (dxfer_dir ==SG_DXFER_FROM_DEV ) { + up =uptr64 (h4p->din_xferp); + dxfer_len = (int )h4p->din_xfer_len; + iov_count = h4p->din_iovec_count; + }else { + up =NULL ; + } + }else { +struct sg_slice_hdr3 *sh3p = &srp->s_hdr3; + + up = sh3p->dxferp; + dxfer_len = (int )sh3p->dxfer_len; + iov_count = sh3p->iovec_count; + r0w = dxfer_dir ==SG_DXFER_TO_DEV ?WRITE :READ ; + }SG_LOG (4 , sfp,"%s: dxfer_len=%d, data-%s\n" , __func__, dxfer_len, (r0w ?"OUT" :"IN" )); q = sdp->device->request_queue;
include/uapi/scsi/sg.h +36 -1
@@ -98,6 +98,18 @@ typedef struct sg_io_hdr {#define SG_FLAG_Q_AT_TAIL 0x10#define SG_FLAG_Q_AT_HEAD 0x20 +/* +* Flags used by ioctl(SG_IOSUBMIT) [abbrev: SG_IOS] and ioctl(SG_IORECEIVE) +* [abbrev: SG_IOR] OR-ed into sg_io_v4::flags. The sync v4 interface uses +* ioctl(SG_IO) and can take these new flags, as can the v3 interface. +* These flags apply for SG_IOS unless otherwise noted. May be OR-ed together. +*/ +#define SGV4_FLAG_DIRECT_IO SG_FLAG_DIRECT_IO +#define SGV4_FLAG_MMAP_IO SG_FLAG_MMAP_IO +#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 */ +/* Output (potentially OR-ed together) in v3::info or v4::info field */ #define SG_INFO_OK_MASK 0x1#define SG_INFO_OK 0x0/* no sense, host nor driver "noise" */
@@ -167,7 +179,6 @@ typedef struct sg_req_info { /* used by SG_GET_REQUEST_TABLE ioctl() *//* sg_io_owned set imples synchronous, clear implies asynchronous */ char sg_io_owned;/* 0 -> complete with read(), 1 -> owned by SG_IO */ char problem;/* 0 -> no problem detected, 1 -> error to report */ -/* If SG_CTL_FLAGM_TAG_FOR_PACK_ID set on fd then next field is tag */ int pack_id;/* pack_id, in v4 driver may be tag instead */ void __user *usr_ptr;/* user provided pointer in v3+v4 interface */ unsigned int duration;
@@ -196,6 +207,13 @@ typedef struct sg_req_info { /* used by SG_GET_REQUEST_TABLE ioctl() */#define SG_SET_RESERVED_SIZE 0x2275/* request new reserved buffer size */ #define SG_GET_RESERVED_SIZE 0x2272/* actual size of reserved buffer */ +/* +* Historically the scsi/sg driver has used 0x22 as it ioctl base number. +* Add a define for that value and use it for several new ioctls added in +* version 4.0.01 sg driver and later. +*/ +#define SG_IOCTL_MAGIC_NUM 0x22 +/* The following ioctl has a 'sg_scsi_id_t *' object as its 3rd argument. */ #define SG_GET_SCSI_ID 0x2276/* Yields fd's bus, chan, dev, lun + type */ /* SCSI id information can also be obtained from SCSI_IOCTL_GET_IDLUN */
@@ -352,6 +370,23 @@ struct sg_header {*/ #define SG_NEXT_CMD_LEN 0x2283 +/* +* New ioctls to replace async (non-blocking) write()/read() interface. +* Present in version 4 and later of the sg driver [>20190427]. The +* SG_IOSUBMIT and SG_IORECEIVE ioctls accept the sg_v4 interface based on +* struct sg_io_v4 found in <include/uapi/linux/bsg.h>. These objects are +* passed by a pointer in the third argument of the ioctl. +* +* Data may be transferred both from the user space to the driver by these +* ioctls. Hence the _IOWR macro is used here to generate the ioctl number +* rather than _IOW or _IOR. +*/ +/* Submits a v4 interface object to driver, optionally receive tag back */ +#define SG_IOSUBMIT _IOWR(SG_IOCTL_MAGIC_NUM, 0x41, struct sg_io_v4) + +/* Gives some v4 identifying info to driver, receives associated response */ +#define SG_IORECEIVE _IOWR(SG_IOCTL_MAGIC_NUM, 0x42, struct sg_io_v4) +/* command queuing is always on when the v3 or v4 interface is used */ #define SG_DEF_COMMAND_Q 0
Signature · 1 line
2.37.3
Douglas Gilbert
maintainerPATCH v25 40/44 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 | 78 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-)
drivers/scsi/sg.c +76 -2
@@ -2278,6 +2278,24 @@ static const struct vm_operations_struct sg_mmap_vm_ops = { .close = sg_vma_close, }; +static inline void +sg_bio_get (struct bio *bio) +{ +bio_get (bio); +} + +static void +sg_bio_put (struct bio *bio) +{ +if (unlikely (bio_flagged (bio,BIO_REFFED ))) { +WARN_ON (!atomic_read (&bio->__bi_cnt)); +if (!atomic_dec_and_test (&bio->__bi_cnt)) +return ; + } +bio_uninit (bio); +kfree (bio); +} +/* * 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
@@ -2792,6 +2810,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 (bvec_cnt,GFP_ATOMIC ); +if (!biop) +return NULL ; +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_init (bio,NULL , bio->bi_inline_vecs, num_sgat, (req_op (rqq) | op_flags)); + bio->bi_end_io = sg_bio_put; + +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) { +sg_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)
@@ -2916,8 +2987,11 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) md->from_user = (dxfer_dir ==SG_DXFER_TO_FROM_DEV ); } - res =blk_rq_map_user_io (rqq, md, up, dxfer_len,GFP_ATOMIC , - iov_count, iov_count,1 , r0w); +if (us_xfer) + res =blk_rq_map_user_io (rqq, md, up, dxfer_len,GFP_ATOMIC , + iov_count, iov_count,1 , r0w); +else /* transfer data to/from kernel buffers */ + res =sg_rq_map_kern (srp, q, rqq, r0w); fini:if (unlikely (res)) {/* failure, free up resources */ WRITE_ONCE (srp->rqq,NULL );
Signature · 1 line
2.37.3
Douglas Gilbert
maintainerPATCH v25 18/44 sg: introduce request state machine
In reply to Douglas GilbertThe introduced request state machine is not wired in so that the size of one of the following patches is reduced. Bit operation defines for the request and file descriptor level are also introduced. Minor rework og sg_read_append() function.
--- drivers/scsi/sg.c | 176 +++++++++++++++++++++++++++++++--------------- 1 file changed, 120 insertions(+), 56 deletions(-)
drivers/scsi/sg.c +120 -56
@@ -77,7 +77,40 @@ static char *sg_version_date = "20190606";*/ #define SG_MAX_CDB_SIZE 252 +/* Following enum contains the states of sg_request::rq_st */ +enum sg_rq_state { +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_BUSY ,/* temporary state should rarely be seen */ +}; + +#define SG_TIME_UNIT_MS 0/* milliseconds */ +#define SG_DEF_TIME_UNIT SG_TIME_UNIT_MS#define SG_DEFAULT_TIMEOUT mult_frac(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ) +#define SG_FD_Q_AT_HEAD 0 +#define SG_DEFAULT_Q_AT SG_FD_Q_AT_HEAD/* for backward compatibility */ +#define SG_FL_MMAP_DIRECT (SG_FLAG_MMAP_IO | SG_FLAG_DIRECT_IO) + +/* Only take lower 4 bits of driver byte, all host byte and sense byte */ +#define SG_ML_RESULT_MSK 0x0fff00ff/* mid-level's 32 bit result value */ + +#define SG_PACK_ID_WILDCARD (-1) + +#define SG_ADD_RQ_MAX_RETRIES 40/* to stop infinite _trylock(s) */ + +/* Bit positions (flags) for sg_request::frq_bm bitmask follow */ +#define SG_FRQ_IS_ORPHAN 1/* owner of request gone */ +#define SG_FRQ_SYNC_INVOC 2/* synchronous (blocking) invocation */ +#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 */ + +/* 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_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 */ /* Bit positions (flags) for sg_device::fdev_bm bitmask follow */ #define SG_FDEV_EXCLUDE 0/* have fd open with O_EXCL */
@@ -85,12 +118,11 @@ static char *sg_version_date = "20190606";#define SG_FDEV_LOG_SENSE 2/* set by ioctl(SG_SET_DEBUG) */ static int sg_big_buff =SG_DEF_RESERVED_SIZE ; -/* N.B. This variable is readable and writeable via -/proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer -of this size (or less if there is not enough memory) will be reserved -for use by this file descriptor. [Deprecated usage: this variable is also -readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into -the kernel (i.e. it is not a module).] */ +/* +* This variable is accessible via /proc/scsi/sg/def_reserved_size . Each +* time sg_open() is called a sg_request of this size (or less if there is +* 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 ;
@@ -134,6 +166,7 @@ struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */char sg_io_owned;/* 1 -> packet belongs to SG_IO */ /* done protected by rq_list_lock */ char done;/* 0->before bh, 1->before read, 2->read */ +atomic_t rq_st;/* request state, holds a enum sg_rq_state */ struct request *rq;/* 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 */
@@ -212,10 +245,11 @@ static void sg_remove_sfp(struct kref *);static struct sg_request *sg_get_rq_mark (struct sg_fd *sfp,int pack_id,bool *busy);static struct sg_request *sg_setup_req (struct sg_fd *sfp); -static int sg_deact_request (struct sg_fd *sfp,struct sg_request *srp); +static void sg_deact_request (struct sg_fd *sfp,struct sg_request *srp);static struct sg_device *sg_get_dev (int dev);static void sg_device_destroy (struct kref *kref);static void sg_calc_sgat_param (struct sg_device *sdp); +static const char *sg_rq_st_str (enum sg_rq_state rq_st,bool long_str);#define SZ_SG_HEADER ((int)sizeof(struct sg_header))/* v1 and v2 header */ #define SZ_SG_IO_HDR ((int)sizeof(struct sg_io_hdr))/* v3 header */
@@ -223,6 +257,8 @@ static void sg_calc_sgat_param(struct sg_device *sdp);#define SG_IS_DETACHING (sdp) test_bit(SG_FDEV_DETACHING, (sdp)->fdev_bm)#define SG_HAVE_EXCLUDE (sdp) test_bit(SG_FDEV_EXCLUDE, (sdp)->fdev_bm) +#define SG_RS_ACTIVE (srp) (atomic_read(&(srp)->rq_st) != SG_RS_INACTIVE) +#define SG_RS_AWAIT_READ (srp) (atomic_read(&(srp)->rq_st) == SG_RS_AWAIT_RCV)/* * Kernel needs to be built with CONFIG_SCSI_LOGGING to see log messages.
@@ -381,15 +417,6 @@ sg_open(struct inode *inode, struct file *filp) res =sg_allow_if_err_recovery (sdp, non_block);if (res) goto error_out; -/* scsi_block_when_processing_errors() may block so bypass -* check if O_NONBLOCK. Permits SCSI commands to be issued -* during error recovery. Tread carefully. */ -if (!((op_flags &O_NONBLOCK ) || -scsi_block_when_processing_errors (sdp->device))) { - res = -ENXIO ; -/* we are in error recovery for this device */ - goto error_out; - }mutex_lock (&sdp->open_rel_lock);if (op_flags &O_NONBLOCK ) {
@@ -488,12 +515,12 @@ sg_write(struct file *filp, const char __user *p, size_t count, loff_t *ppos)struct sg_device *sdp;struct sg_fd *sfp;struct sg_request *srp; +u8 cmnd[SG_MAX_CDB_SIZE ];struct sg_header ov2hdr;struct sg_io_hdr v3hdr;struct sg_header *ohp = &ov2hdr;struct sg_io_hdr *h3p = &v3hdr;struct sg_comm_wr_t cwr; -u8 cmnd[SG_MAX_CDB_SIZE ]; res =sg_check_file_access (filp, __func__);if (res)
@@ -1283,7 +1310,7 @@ sg_ioctl_common(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,case SG_GET_LOW_DMA :SG_LOG (3 , sfp,"%s: SG_GET_LOW_DMA\n" , __func__);return put_user (0 , ip); -case SG_NEXT_CMD_LEN : +case SG_NEXT_CMD_LEN :/* active only in v2 interface */ SG_LOG (3 , sfp,"%s: SG_NEXT_CMD_LEN\n" , __func__); result =get_user (val, ip);if (result)
@@ -2173,48 +2200,37 @@ sg_remove_sgat(struct sg_fd *sfp, struct sg_scatter_hold *schp)static int sg_read_append (struct sg_request *srp,void __user *outp,int num_xfer) { +int k, num, res; +struct page *pgp;struct sg_scatter_hold *schp = &srp->data; -int k, num;SG_LOG (4 , srp->parentfp,"%s: num_xfer=%d\n" , __func__, num_xfer); -if (!outp || num_xfer <=0 ) -return 0 ; +if (unlikely (!outp || num_xfer <=0 )) +return (num_xfer ==0 && outp) ?0 : -EINVAL ; num =1 << (PAGE_SHIFT + schp->page_order); -for (k =0 ; k < schp->num_sgat && schp->pages[k]; k++) { +for (k =0 , res =0 ; k < schp->num_sgat; ++k) { + pgp = schp->pages[k]; +if (unlikely (!pgp)) { + res = -ENXIO ; +break ; + }if (num > num_xfer) { -if (copy_to_user (outp,page_address (schp->pages[k]), - num_xfer)) -return -EFAULT ; +if (__copy_to_user (outp,page_address (pgp), num_xfer)) + res = -EFAULT ;break ; }else { -if (copy_to_user (outp,page_address (schp->pages[k]), - num)) -return -EFAULT ; +if (__copy_to_user (outp,page_address (pgp), num)) { + res = -EFAULT ; +break ; + } num_xfer -= num;if (num_xfer <=0 )break ; outp += num; } } -return 0 ; -} - -static void -sg_build_reserve (struct sg_fd *sfp,int req_size) -{ -struct sg_scatter_hold *schp = &sfp->reserve; - -SG_LOG (3 , sfp,"%s: buflen=%d\n" , __func__, req_size); -do { -if (req_size <PAGE_SIZE ) - req_size =PAGE_SIZE ; -if (sg_mk_sgat (schp, sfp, req_size) ==0 ) -return ; -else -sg_remove_sgat (sfp, schp); - req_size >>=1 ;/* divide by 2 */ - }while (req_size > (PAGE_SIZE /2 )); +return res; } static void
@@ -2292,6 +2308,22 @@ sg_get_rq_mark(struct sg_fd *sfp, int pack_id, bool *busy)return NULL ; } +static void +sg_build_reserve (struct sg_fd *sfp,int req_size) +{ +struct sg_scatter_hold *schp = &sfp->reserve; + +SG_LOG (3 , sfp,"%s: buflen=%d\n" , __func__, req_size); +do { +if (req_size <PAGE_SIZE ) + req_size =PAGE_SIZE ; +if (sg_mk_sgat (schp, sfp, req_size) ==0 ) +return ; +sg_remove_sgat (sfp, schp); + req_size >>=1 ;/* divide by 2 */ + }while (req_size > (PAGE_SIZE /2 )); +} +/* always adds to end of list */ static struct sg_request *sg_setup_req (struct sg_fd *sfp)
@@ -2323,20 +2355,19 @@ sg_setup_req(struct sg_fd *sfp)return NULL ; } -/* Return of 1 for found; 0 for not found */ -static int +static void sg_deact_request (struct sg_fd *sfp,struct sg_request *srp) {unsigned long iflags; -int res =0 ; -if (!sfp || !srp ||list_empty (&sfp->rq_list)) -return res; +if (WARN_ON (!sfp || !srp)) +return ; +if (list_empty (&sfp->rq_list)) +return ;spin_lock_irqsave (&sfp->rq_list_lock, iflags);if (!list_empty (&srp->entry)) {list_del (&srp->entry); srp->parentfp =NULL ; - res =1 ; }spin_unlock_irqrestore (&sfp->rq_list_lock, iflags);
@@ -2347,8 +2378,6 @@ sg_deact_request(struct sg_fd *sfp, struct sg_request *srp)*/ if (unlikely (SG_IS_DETACHING (sfp->parentdp)))wake_up_interruptible_all (&sfp->read_wait); - -return res; } static struct sg_fd *
@@ -2406,6 +2435,15 @@ sg_add_sfp(struct sg_device *sdp)return sfp; } +/* +* A successful call to sg_release() will result, at some later time, to this +* function being invoked. All requests associated with this file descriptor +* should be completed or cancelled when this function is called (due to +* sfp->f_ref). Also the file descriptor itself has not been accessible since +* it was list_del()-ed by the preceding sg_remove_sfp() call. So no locking +* is required. sdp should never be NULL but to make debugging more robust, +* this function will not blow up in that case. +*/ static void sg_remove_sfp_usercontext (struct work_struct *work) {
@@ -2499,6 +2537,31 @@ sg_get_dev(int dev)return sdp; } +#if IS_ENABLED (CONFIG_SCSI_PROC_FS ) +static const char * +sg_rq_st_str (enum sg_rq_state rq_st,bool long_str) +{ +switch (rq_st) {/* request state */ +case SG_RS_INACTIVE : +return long_str ?"inactive" :"ina" ; +case SG_RS_INFLIGHT : +return long_str ?"inflight" :"act" ; +case SG_RS_AWAIT_RCV : +return long_str ?"await_receive" :"rcv" ; +case SG_RS_BUSY : +return long_str ?"busy" :"bsy" ; +default : +return long_str ?"unknown" :"unk" ; + } +} +#else +static const char * +sg_rq_st_str (enum sg_rq_state rq_st,bool long_str) +{ +return "" ; +} +#endif +#if IS_ENABLED (CONFIG_SCSI_PROC_FS )/* long, almost to end of file */ static int sg_proc_seq_show_int (struct seq_file *s,void *v);
@@ -2795,8 +2858,9 @@ sg_proc_debug_helper(struct seq_file *s, struct sg_device *sdp) jiffies_to_msecs(fp->timeout)), (ms > hp->duration ? ms - hp->duration : 0)); } -seq_printf (s,"ms sgat=%d op=0x%02x\n" , usg, - (int ) srp->data.cmd_opcode); +seq_printf (s,"ms sgat=%d op=0x%02x dummy: %s\n" , usg, + (int )srp->data.cmd_opcode, +sg_rq_st_str (SG_RS_INACTIVE , false)); }if (list_empty (&fp->rq_list))seq_puts (s," No requests active\n" );
Signature · 1 line
2.37.3
Douglas Gilbert
maintainerPATCH v25 25/44 sg: sense buffer rework
In reply to Douglas GilbertThe biggest single item in the sg_request object is the sense buffer array which is SCSI_SENSE_BUFFERSIZE bytes long. That constant started out at 18 bytes 20 years ago and is 96 bytes now and might grow in the future. On the other hand the sense buffer is only used by a small number of SCSI commands: those that fail and those that want to return more information other than a SCSI status of GOOD.
Set up a small mempool called "sg_sense" that is only used as required and released back to the mempool as soon as practical.
--- drivers/scsi/sg.c | 117 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 89 insertions(+), 28 deletions(-)
drivers/scsi/sg.c +89 -28
@@ -77,6 +77,10 @@ static char *sg_version_date = "20190606";*/ #define SG_MAX_CDB_SIZE 252 +static struct kmem_cache *sg_sense_cache; +#define SG_MEMPOOL_MIN_NR 4 +static mempool_t *sg_sense_pool; +/* Following enum contains the states of sg_request::rq_st */ 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) */
@@ -177,7 +181,6 @@ struct sg_fd;struct sg_request {/* active SCSI command or inactive request */ struct sg_scatter_hold sgat_h;/* hold buffer, perhaps scatter list */ struct sg_slice_hdr3 s_hdr3;/* subset of sg_io_hdr */ -u8 sense_b[SCSI_SENSE_BUFFERSIZE ];u32 duration;/* cmd duration in milliseconds */ u32 rq_flags;/* hold user supplied flags */ u32 rq_idx;/* my index within parent's srp_arr */
@@ -190,6 +193,7 @@ struct sg_request { /* active SCSI command or inactive request */u8 cmd_opcode;/* first byte of SCSI cdb */ u64 start_ns;/* starting point of command duration calc */ 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 bio *bio;/* kept until this req -->SG_RS_INACTIVE */
@@ -953,21 +957,24 @@ sg_copy_sense(struct sg_request *srp)/* If need be, copy the sense buffer to the user space */ scsi_stat = srp->rq_result &0xff ;if ((scsi_stat &SAM_STAT_CHECK_CONDITION ) || - (srp->sense_b[0 ] &0x70 ) ==0x70 ) { + (srp->sense_bp[0 ] &0x70 ) ==0x70 ) {int sb_len =min_t (int,SCSI_SENSE_BUFFERSIZE , srp->sense_len);int mx_sb_len = srp->s_hdr3.mx_sb_len; +u8 *sbp = srp->sense_bp;void __user *up = srp->s_hdr3.sbp; -if (up && mx_sb_len >0 ) { + srp->sense_bp =NULL ; +if (up && mx_sb_len >0 && sbp) { sb_len =min_t (int, mx_sb_len, sb_len);/* Additional sense length field */ - sb_len_ret =8 + (int )srp->sense_b[7 ]; + sb_len_ret =8 + (int )sbp[7 ]; sb_len_ret =min_t (int, sb_len_ret, sb_len); -if (copy_to_user (up, srp->sense_b, sb_len_ret)) +if (copy_to_user (up, sbp, sb_len_ret)) sb_len_ret = -EFAULT ; }else { sb_len_ret =0 ; } +mempool_free (sbp, sg_sense_pool); }return sb_len_ret; }
@@ -1057,10 +1064,16 @@ sg_read_v1v2(void __user *buf, int count, struct sg_fd *sfp, h2p->host_status =host_byte (rq_result); h2p->driver_status =driver_byte (rq_result);if ((CHECK_CONDITION &status_byte (rq_result)) || - (srp->sense_b[0 ] &0x70 ) ==0x70 ) { + (srp->sense_bp[0 ] &0x70 ) ==0x70 ) { h2p->driver_status =DRIVER_SENSE ; -memcpy (h2p->sense_buffer, srp->sense_b, -sizeof (h2p->sense_buffer)); +if (srp->sense_bp) { +u8 *sbp = srp->sense_bp; + + srp->sense_bp =NULL ; +memcpy (h2p->sense_buffer, sbp, +sizeof (h2p->sense_buffer)); +mempool_free (sbp, sg_sense_pool); + } }switch (host_byte (rq_result)) {/*
@@ -1095,18 +1108,22 @@ sg_read_v1v2(void __user *buf, int count, struct sg_fd *sfp,/* Now copy the result back to the user buffer. */ if (count >=SZ_SG_HEADER ) { -if (copy_to_user (buf, h2p,SZ_SG_HEADER )) -return -EFAULT ; +if (copy_to_user (buf, h2p,SZ_SG_HEADER )) { + res = -EFAULT ; + goto fini; + } buf +=SZ_SG_HEADER ;if (count > h2p->reply_len) count = h2p->reply_len;if (count >SZ_SG_HEADER ) { -if (sg_read_append (srp, buf, count -SZ_SG_HEADER )) -return -EFAULT ; + res =sg_read_append (srp, buf, count -SZ_SG_HEADER ); +if (res) + goto fini; } }else { res = (h2p->result ==0 ) ?0 : -EIO ; } +fini:sg_finish_scsi_blk_rq (srp);sg_deact_request (sfp, srp);return res;
@@ -2006,8 +2023,24 @@ sg_rq_end_io(struct request *rq, blk_status_t status) 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 (slen >0 ) -memcpy (srp->sense_b, scmd->sense_buffer, slen); +if (slen >0 ) { +if (scmd->sense_buffer) { + srp->sense_bp =mempool_alloc (sg_sense_pool,GFP_ATOMIC ); +if (srp->sense_bp) { +memcpy (srp->sense_bp, scmd->sense_buffer, slen); +if (slen <SCSI_SENSE_BUFFERSIZE ) +memset (srp->sense_bp + slen,0 , +SCSI_SENSE_BUFFERSIZE - slen); + }else { + slen =0 ; +pr_warn ("%s: sense but can't alloc buffer\n" , + __func__); + } + }else { + slen =0 ; +pr_warn ("%s: sense_len>0 but sense==NULL\n" , __func__); + } + } srp->sense_len = slen;if (unlikely (test_bit (SG_FRQ_IS_ORPHAN , srp->frq_bm))) {if (test_bit (SG_FFD_KEEP_ORPHAN , sfp->ffd_bm)) {
@@ -2311,13 +2344,30 @@ init_sg(void)if (rc)return rc; + sg_sense_cache =kmem_cache_create_usercopy + ("sg_sense" ,SCSI_SENSE_BUFFERSIZE ,0 , +SLAB_HWCACHE_ALIGN ,0 , +SCSI_SENSE_BUFFERSIZE ,NULL ); +if (!sg_sense_cache) { +pr_err ("sg: can't init sense cache\n" ); + rc = -ENOMEM ; + goto err_out_unreg; + } + sg_sense_pool =mempool_create_slab_pool (SG_MEMPOOL_MIN_NR , + sg_sense_cache); +if (!sg_sense_pool) { +pr_err ("sg: can't init sense pool\n" ); + rc = -ENOMEM ; + goto err_out_cache; + } +pr_info ("Registered %s[char major=0x%x], version: %s, date: %s\n" ,"sg device " ,SCSI_GENERIC_MAJOR ,SG_VERSION_STR , sg_version_date); sg_sysfs_class =class_create (THIS_MODULE ,"scsi_generic" );if (IS_ERR (sg_sysfs_class)) { rc =PTR_ERR (sg_sysfs_class); - goto err_out_unreg; + goto err_out_pool; } sg_sysfs_valid = true; rc =scsi_register_interface (&sg_interface);
@@ -2328,6 +2378,10 @@ init_sg(void)class_destroy (sg_sysfs_class);register_sg_sysctls (); +err_out_pool: +mempool_destroy (sg_sense_pool); +err_out_cache: +kmem_cache_destroy (sg_sense_cache); err_out_unreg:unregister_chrdev_region (MKDEV (SCSI_GENERIC_MAJOR ,0 ),SG_MAX_DEVS );return rc;
@@ -2340,6 +2394,8 @@ exit_sg(void)if (IS_ENABLED (CONFIG_SCSI_PROC_FS ))remove_proc_subtree ("scsi/sg" ,NULL );scsi_unregister_interface (&sg_interface); +mempool_destroy (sg_sense_pool); +kmem_cache_destroy (sg_sense_cache);class_destroy (sg_sysfs_class); sg_sysfs_valid = false; unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
@@ -2831,6 +2887,7 @@ sg_setup_req(struct sg_fd *sfp, int dxfr_len, struct sg_comm_wr_t *cwrp)int num_inactive =0 ;unsigned long idx, last_idx, iflags;struct sg_request *r_srp =NULL ;/* request to return */ +struct sg_request *last_srp =NULL ;struct xarray *xafp = &sfp->srp_arr;__maybe_unused const char *cp;
@@ -2847,19 +2904,17 @@ sg_setup_req(struct sg_fd *sfp, int dxfr_len, struct sg_comm_wr_t *cwrp) ++num_inactive;if (dxfr_len <SG_DEF_SECTOR_SZ ) { last_idx = idx; + last_srp = r_srp;continue ; } }/* If dxfr_len is small, use last inactive request */ -if (last_idx != ~0UL ) { - idx = last_idx; - r_srp =xa_load (xafp, idx); -if (!r_srp) - goto start_again; +if (last_idx != ~0UL && last_srp) { + r_srp = last_srp;if (sg_rq_state_chg (r_srp,SG_RS_INACTIVE ,SG_RS_BUSY , false, __func__)) goto start_again;/* gone to another thread */ - cp ="toward back of srp_arr" ; + cp ="toward end of srp_arr" ; found = true; } }else {
@@ -2944,15 +2999,16 @@ sg_setup_req(struct sg_fd *sfp, int dxfr_len, struct sg_comm_wr_t *cwrp)static void sg_deact_request (struct sg_fd *sfp,struct sg_request *srp) { -unsigned long iflags; +u8 *sbp;if (WARN_ON (!sfp || !srp))return ; -atomic_set (&srp->rq_st,SG_RS_INACTIVE ); -xa_lock_irqsave (&sfp->srp_arr, iflags); -__xa_set_mark (&sfp->srp_arr, srp->rq_idx,SG_XA_RQ_INACTIVE ); -__xa_clear_mark (&sfp->srp_arr, srp->rq_idx,SG_XA_RQ_AWAIT ); -xa_unlock_irqrestore (&sfp->srp_arr, iflags); + sbp = srp->sense_bp; + srp->sense_bp =NULL ; +sg_rq_state_chg (srp,0 ,SG_RS_INACTIVE , true/* force */ , __func__); +/* maybe orphaned req, thus never read */ +if (sbp) +mempool_free (sbp, sg_sense_pool); }/* Returns pointer to sg_fd object or negated errno twisted by ERR_PTR */
@@ -3101,7 +3157,12 @@ sg_remove_sfp_usercontext(struct work_struct *work)continue ;if (!xa_get_mark (xafp, srp->rq_idx,SG_XA_RQ_INACTIVE ))sg_finish_scsi_blk_rq (srp); -sg_remove_sgat (srp); +if (srp->sgat_h.buflen >0 ) +sg_remove_sgat (srp); +if (srp->sense_bp) { +mempool_free (srp->sense_bp, sg_sense_pool); + srp->sense_bp =NULL ; + }xa_lock_irqsave (xafp, iflags); e_srp =__xa_erase (xafp, srp->rq_idx);xa_unlock_irqrestore (xafp, iflags);
Signature · 1 line
2.37.3
Douglas Gilbert
maintainerPATCH v25 41/44 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 | 205 ++++++++++++++++++++++++++++++++++++----- include/uapi/scsi/sg.h | 1 + 2 files changed, 182 insertions(+), 24 deletions(-)
drivers/scsi/sg.c +181 -24
@@ -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; };
@@ -305,6 +309,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 (CONFIG_SG_DEBUG )static const char *sg_rq_st_str (enum sg_rq_state rq_st,bool long_str);#endif
@@ -1009,6 +1016,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);
@@ -1032,8 +1040,22 @@ sg_execute_cmd(struct sg_fd *sfp, struct sg_request *srp)atomic_inc (&sfp->submitted);set_bit (SG_FRQ_COUNT_ACTIVE , srp->frq_bm); } +if (srp->rq_flags &SGV4_FLAG_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 ; + } + } srp->rqq->end_io = sg_rq_end_io; -blk_execute_rq_nowait (READ_ONCE (srp->rqq), (int )at_head); +blk_execute_rq_nowait (rqq, (int )at_head); +set_bit (SG_FRQ_ISSUED , srp->frq_bm); }/*
@@ -1689,6 +1711,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,
@@ -1968,6 +1997,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);
@@ -2152,6 +2183,120 @@ sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)return scsi_ioctl (sdp->device, filp->f_mode, cmd_in, p); } +static inline void +sg_bio_get (struct bio *bio) +{ +bio_get (bio); +} + +static void +sg_bio_put (struct bio *bio) +{ +if (unlikely (bio_flagged (bio,BIO_REFFED ))) { +WARN_ON (!atomic_read (&bio->__bi_cnt)); +if (!atomic_dec_and_test (&bio->__bi_cnt)) +return ; + } +bio_uninit (bio); +kfree (bio); +} + +/* +* 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 ; + } +sg_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); +sg_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.
@@ -2163,6 +2308,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);
@@ -2278,24 +2425,6 @@ static const struct vm_operations_struct sg_mmap_vm_ops = { .close = sg_vma_close, }; -static inline void -sg_bio_get (struct bio *bio) -{ -bio_get (bio); -} - -static void -sg_bio_put (struct bio *bio) -{ -if (unlikely (bio_flagged (bio,BIO_REFFED ))) { -WARN_ON (!atomic_read (&bio->__bi_cnt)); -if (!atomic_dec_and_test (&bio->__bi_cnt)) -return ; - } -bio_uninit (bio); -kfree (bio); -} -/* * 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
@@ -2464,6 +2593,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)) {
@@ -2489,7 +2619,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 */
@@ -2529,6 +2660,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);
@@ -2634,6 +2766,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:
@@ -2662,6 +2795,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); }
@@ -3000,6 +3134,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);
@@ -3023,7 +3176,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); }
@@ -3218,6 +3372,8 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id, bool *busy)struct xarray *xafp = &sfp->srp_arr; *busy = false; +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 )
@@ -4036,8 +4192,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.37.3
Douglas Gilbert
maintainerPATCH v25 42/44 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 enum rq_end_io_ret sg_rq_end_io (struct request *rq,blk_status_t status);/* Declarations of other static functions used before they are defined */
@@ -312,6 +327,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 (CONFIG_SG_DEBUG )static const char *sg_rq_st_str (enum sg_rq_state rq_st,bool long_str);#endif
@@ -1017,11 +1034,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' )
@@ -1195,11 +1218,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);
@@ -1625,49 +1684,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
@@ -1678,8 +1729,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 );
@@ -2527,6 +2576,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;
@@ -2553,7 +2603,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 ;
@@ -2701,9 +2753,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) {
@@ -2728,6 +2783,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)
@@ -2737,6 +2794,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),
@@ -2763,6 +2822,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);
@@ -2791,6 +2851,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);
@@ -3963,6 +4024,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 (CONFIG_SG_PROC_OR_DEBUG_FS )#define SG_SNAPSHOT_DEV_MAX 4
Signature · 1 line
2.37.3
Douglas Gilbert
maintainerPATCH v25 43/44 sg: rework command completion when removed device
In reply to Douglas GilbertOn advice from Tony Battersby, the author of mainline patch:3455607fd7be10b449f5135c00dc306b85dc0d21
rework the logic introduced in that patch. Previously when there were no requests in SG_RS_AWAIT_RCV state, and IS_DETACHING() is true, and there was at least one request in SG_RS_INFLIGHT state then the passed output parameter *busy was set to true. Replace *busy with ERR_PTR(-ENODEV), which is returned only when SG_IS_DETACHING() and there are no SG_RS_AWAIT_RCV requests to return or SG_RS_INFLIGHT requests to wait for.
To summarize, if IS_DETACHING() is true:
- don't accept any new requests on that device
- process all requests already in SG_RS_AWAIT_RCV state
- if there are any requests in SG_RS_INFLIGHT state spin until they transition to SG_RS_AWAIT_RCV state or time out
--- drivers/scsi/sg.c | 180 +++++++++++++++++++++++++--------------------- 1 file changed, 97 insertions(+), 83 deletions(-)
drivers/scsi/sg.c +97 -83
@@ -315,8 +315,7 @@ static int sg_read_append(struct sg_request *srp, void __user *outp,static void sg_remove_sgat (struct sg_request *srp);static struct sg_fd *sg_add_sfp (struct sg_device *sdp);static void sg_remove_sfp (struct kref *); -static struct sg_request *sg_find_srp_by_id (struct sg_fd *sfp,int id, -bool *busy); +static struct sg_request *sg_find_srp_by_id (struct sg_fd *sfp,int id);static struct sg_request *sg_setup_req (struct sg_comm_wr_t *cwrp,int dxfr_len);static void sg_deact_request (struct sg_fd *sfp,struct sg_request *srp);
@@ -457,11 +456,11 @@ sg_wait_open_event(struct sg_device *sdp, bool o_excl)* Returns 0 for ok (i.e. allow), -EPROTO if sdp is NULL, otherwise -ENXIO . */ static inline int -sg_allow_if_err_recovery (struct sg_device *sdp,bool non_block) +sg_allow_if_err_recovery (struct sg_device *sdp,bool non_block,bool ioreceive) {if (!sdp)return -EPROTO ; -if (SG_IS_DETACHING (sdp)) +if (SG_IS_DETACHING (sdp) && !ioreceive)return -ENODEV ;if (non_block)return 0 ;
@@ -504,7 +503,7 @@ sg_open(struct inode *inode, struct file *filp) res =scsi_autopm_get_device (sdp->device);if (res) goto sdp_put; - res =sg_allow_if_err_recovery (sdp, non_block); + res =sg_allow_if_err_recovery (sdp, non_block, false);if (res) goto error_out;
@@ -625,7 +624,7 @@ sg_write(struct file *filp, const char __user *p, size_t count, loff_t *ppos) sfp = filp->private_data; sdp = sfp->parentdp;SG_LOG (3 , sfp,"%s: write(3rd arg) count=%d\n" , __func__, (int )count); - res =sg_allow_if_err_recovery (sdp, !!(filp->f_flags &O_NONBLOCK )); + res =sg_allow_if_err_recovery (sdp, !!(filp->f_flags &O_NONBLOCK ), false);if (res)return res;if (count <SZ_SG_HEADER || count >SG_WRITE_COUNT_LIMIT )
@@ -870,7 +869,7 @@ sg_ctl_iosubmit(struct file *filp, struct sg_fd *sfp, void __user *p)struct sg_io_v4 *h4p = (struct sg_io_v4 *)hdr_store;struct sg_device *sdp = sfp->parentdp; - res =sg_allow_if_err_recovery (sdp, (filp->f_flags &O_NONBLOCK )); + res =sg_allow_if_err_recovery (sdp, (filp->f_flags &O_NONBLOCK ), false);if (res)return res;if (copy_from_user (hdr_store, p,SZ_SG_IO_V4 ))
@@ -888,7 +887,7 @@ sg_ctl_iosubmit_v3(struct file *filp, struct sg_fd *sfp, void __user *p)struct sg_io_hdr *h3p = (struct sg_io_hdr *)hdr_store;struct sg_device *sdp = sfp->parentdp; - res =sg_allow_if_err_recovery (sdp, (filp->f_flags &O_NONBLOCK )); + res =sg_allow_if_err_recovery (sdp, (filp->f_flags &O_NONBLOCK ), false);if (unlikely (res))return res;if (copy_from_user (h3p, p,SZ_SG_IO_HDR ))
@@ -1167,10 +1166,9 @@ sg_common_write(struct sg_comm_wr_t *cwrp)* returns true (or an event like a signal (e.g. control-C) occurs). */ static inline bool -sg_get_ready_srp (struct sg_fd *sfp,struct sg_request **srpp,int pack_id, -bool *busy) +sg_get_ready_srp (struct sg_fd *sfp,struct sg_request **srpp,int pack_id) { -struct sg_request *srp =sg_find_srp_by_id (sfp, pack_id, busy); +struct sg_request *srp =sg_find_srp_by_id (sfp, pack_id); *srpp = srp;return !!srp;
@@ -1367,7 +1365,6 @@ static int sg_ctl_ioreceive(struct file *filp, struct sg_fd *sfp, void __user *p) { bool non_block = !!(filp->f_flags & O_NONBLOCK); -bool busy;int res, id;int pack_id =SG_PACK_ID_WILDCARD ;u8 v4_holder[SZ_SG_IO_V4 ];
@@ -1375,7 +1372,7 @@ sg_ctl_ioreceive(struct file *filp, struct sg_fd *sfp, void __user *p)struct sg_device *sdp = sfp->parentdp;struct sg_request *srp; - res =sg_allow_if_err_recovery (sdp, non_block); + res =sg_allow_if_err_recovery (sdp, non_block, true);if (res)return res;/* Get first three 32 bit integers: guard, proto+subproto */
@@ -1390,19 +1387,19 @@ sg_ctl_ioreceive(struct file *filp, struct sg_fd *sfp, void __user *p)/* read in part of v3 or v4 header for pack_id or tag based find */ id = pack_id; try_again: - srp =sg_find_srp_by_id (sfp, id, &busy); -while (!srp) {/* nothing available so wait on packet or */ -if (unlikely (!busy &&SG_IS_DETACHING (sdp))) -return -ENODEV ; + srp =sg_find_srp_by_id (sfp, id); +if (IS_ERR (srp)) +return PTR_ERR (srp); +if (!srp) {/* nothing available so wait on packet or */ if (non_block)return -EAGAIN ; res =wait_event_interruptible (sfp->read_wait,sg_get_ready_srp (sfp, &srp, - id, &busy)); -if (unlikely (!busy &&SG_IS_DETACHING (sdp))) -return -ENODEV ; -if (res) -return res;/* signal --> -ERESTARTSYS */ + id)); +if (IS_ERR (srp)) +return PTR_ERR (srp); +if (unlikely (!srp)) +return res;/* signal --> -ERESTARTSYS */ }if (test_and_set_bit (SG_FRQ_RECEIVING , srp->frq_bm)) {cpu_relax ();
@@ -1421,7 +1418,6 @@ sg_ctl_ioreceive(struct file *filp, struct sg_fd *sfp, void __user *p)static int sg_ctl_ioreceive_v3 (struct file *filp,struct sg_fd *sfp,void __user *p) { -bool busy;bool non_block = !!(filp->f_flags &O_NONBLOCK );int res;int pack_id =SG_PACK_ID_WILDCARD ;
@@ -1430,7 +1426,7 @@ sg_ctl_ioreceive_v3(struct file *filp, struct sg_fd *sfp, void __user *p)struct sg_device *sdp = sfp->parentdp;struct sg_request *srp; - res =sg_allow_if_err_recovery (sdp, non_block); + res =sg_allow_if_err_recovery (sdp, non_block, true);if (unlikely (res))return res;/* Get first three 32 bit integers: guard, proto+subproto */
@@ -1446,17 +1442,19 @@ sg_ctl_ioreceive_v3(struct file *filp, struct sg_fd *sfp, void __user *p)if (test_bit (SG_FFD_FORCE_PACKID , sfp->ffd_bm)) pack_id = h3p->pack_id; try_again: - srp =sg_find_srp_by_id (sfp, pack_id, &busy); -while (!srp) {/* nothing available so wait on packet or */ -if (unlikely (!busy &&SG_IS_DETACHING (sdp))) -return -ENODEV ; + srp =sg_find_srp_by_id (sfp, pack_id); +if (IS_ERR (srp)) +return PTR_ERR (srp); +if (!srp) {/* nothing available so wait on packet or */ if (non_block)return -EAGAIN ; res =wait_event_interruptible (sfp->read_wait, -sg_get_ready_srp (sfp, &srp, pack_id, &busy)); -if (unlikely (res)) -return res;/* signal --> -ERESTARTSYS */ +sg_get_ready_srp (sfp, &srp, pack_id)); +if (IS_ERR (srp)) +return PTR_ERR (srp); +if (unlikely (!srp)) +return res;/* signal --> -ERESTARTSYS */ }if (test_and_set_bit (SG_FRQ_RECEIVING , srp->frq_bm)) {cpu_relax ();
@@ -1587,10 +1585,8 @@ static ssize_t sg_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) { bool non_block = !!(filp->f_flags & O_NONBLOCK); -bool busy;int ret;int req_pack_id =-1 ; -struct sg_device *sdp;struct sg_fd *sfp;struct sg_request *srp;
@@ -1603,7 +1599,6 @@ sg_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)return ret; sfp = filp->private_data; - sdp = sfp->parentdp;SG_LOG (3 , sfp,"%s: read() count=%d\n" , __func__, (int )count);if (test_bit (SG_FFD_FORCE_PACKID , sfp->ffd_bm)) {
@@ -1613,20 +1608,19 @@ sg_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) } try_again: - srp =sg_find_srp_by_id (sfp, req_pack_id, &busy); -while (!srp) {/* now wait on packet to arrive */ -if (unlikely (!busy &&SG_IS_DETACHING (sdp))) -return -ENODEV ; + srp =sg_find_srp_by_id (sfp, req_pack_id); +if (IS_ERR (srp)) +return PTR_ERR (srp); +if (!srp) {/* now wait on packet to arrive */ if (non_block)/* O_NONBLOCK or v3::flags & SGV4_FLAG_IMMED */ return -EAGAIN ; ret =wait_event_interruptible (sfp->read_wait, - ((srp =sg_find_srp_by_id (sfp, req_pack_id, &busy)) || - (!busy &&SG_IS_DETACHING (sdp)))); -if (unlikely (SG_IS_DETACHING (sdp))) -return -ENODEV ; -if (ret)/* -ERESTARTSYS as signal hit process */ -return ret; -/* otherwise srp should be valid */ +sg_get_ready_srp (sfp, &srp, + req_pack_id)); +if (IS_ERR (srp)) +return PTR_ERR (srp); +if (unlikely (!srp)) +return ret;/* signal --> -ERESTARTSYS */ }if (test_and_set_bit (SG_FRQ_RECEIVING , srp->frq_bm)) {cpu_relax ();
@@ -1740,10 +1734,9 @@ 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) +sg_rq_landed (struct sg_request *srp) { -return atomic_read_acquire (&srp->rq_st) !=SG_RS_INFLIGHT || -unlikely (SG_IS_DETACHING (sdp)); +return atomic_read_acquire (&srp->rq_st) !=SG_RS_INFLIGHT ; }/*
@@ -1770,7 +1763,7 @@ sg_wait_event_srp(struct file *filp, struct sg_fd *sfp, void __user *p,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, -sg_rq_landed (sdp, srp)); +sg_rq_landed (srp));if (unlikely (res)) {/* -ERESTARTSYS because signal hit thread */ set_bit (SG_FRQ_IS_ORPHAN , srp->frq_bm);/* orphans harvested when sfp->keep_orphan is false */
@@ -1780,11 +1773,6 @@ sg_wait_event_srp(struct file *filp, struct sg_fd *sfp, void __user *p,return res; } skip_wait: -if (unlikely (SG_IS_DETACHING (sdp))) { -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 */
@@ -1818,7 +1806,7 @@ sg_ctl_sg_io(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,SG_LOG (3 , sfp,"%s: SG_IO%s\n" , __func__, ((filp->f_flags &O_NONBLOCK ) ?" O_NONBLOCK ignored" :"" )); - res =sg_allow_if_err_recovery (sdp, false); + res =sg_allow_if_err_recovery (sdp, false, false);if (res)return res;if (get_sg_io_hdr (h3p, buf))
@@ -2007,8 +1995,6 @@ sg_ioctl_common(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,SG_LOG (6 , sfp,"%s: cmd=0x%x, O_NONBLOCK=%d\n" , __func__, cmd_in, !!(filp->f_flags &O_NONBLOCK )); -if (unlikely (SG_IS_DETACHING (sdp))) -return -ENODEV ; sdev = sdp->device;switch (cmd_in) {
@@ -2027,6 +2013,8 @@ sg_ioctl_common(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,SG_LOG (3 , sfp,"%s: SG_IORECEIVE_V3\n" , __func__);return sg_ctl_ioreceive_v3 (filp, sfp, p);case SG_GET_SCSI_ID : +if (unlikely (SG_IS_DETACHING (sdp))) +return -ENODEV ;return sg_ctl_scsi_id (sdev, sfp, p);case SG_SET_FORCE_PACK_ID :SG_LOG (3 , sfp,"%s: SG_SET_FORCE_PACK_ID\n" , __func__);
@@ -2158,6 +2146,8 @@ sg_ioctl_common(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,return put_user (sdev->host->hostt->emulated, ip);case SCSI_IOCTL_SEND_COMMAND :SG_LOG (3 , sfp,"%s: SCSI_IOCTL_SEND_COMMAND\n" , __func__); +if (unlikely (SG_IS_DETACHING (sdp))) +return -ENODEV ;return scsi_ioctl (sdp->device, filp->f_mode, cmd_in, p);case SG_SET_DEBUG :SG_LOG (3 , sfp,"%s: SG_SET_DEBUG\n" , __func__);
@@ -2207,7 +2197,7 @@ sg_ioctl_common(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,return -EPERM ;/* don't know, so take safer approach */ break ; } - result =sg_allow_if_err_recovery (sdp, filp->f_flags &O_NDELAY ); + result =sg_allow_if_err_recovery (sdp, filp->f_flags &O_NDELAY , false);if (unlikely (result))return result;return -ENOIOCTLCMD ;
@@ -2422,7 +2412,6 @@ sg_vma_fault(struct vm_fault *vmf)struct page *page;struct sg_scatter_hold *rsv_schp;struct sg_request *srp; -struct sg_device *sdp;struct sg_fd *sfp;const char *nbp ="==NULL, bad" ;
@@ -2435,11 +2424,6 @@ sg_vma_fault(struct vm_fault *vmf)pr_warn ("%s: sfp%s\n" , __func__, nbp); goto out_err; } - sdp = sfp->parentdp; -if (sdp &&unlikely (SG_IS_DETACHING (sdp))) { -SG_LOG (1 , sfp,"%s: device detaching\n" , __func__); - goto out_err; - } srp = sfp->rsv_srp;if (!srp) {SG_LOG (1 , sfp,"%s: srp%s\n" , __func__, nbp);
@@ -3237,8 +3221,17 @@ 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)) { -if (atomic_dec_and_test (&sfp->submitted)) +if (atomic_dec_and_test (&sfp->submitted)) {clear_bit (SG_FFD_POLLED_SEEN , sfp->ffd_bm); + +/* +* If the device is detaching, wakeup any ioreceives in +* case we just finished the last response, which would +* leave nothing for them to return other than -ENODEV. +*/ +if (unlikely (SG_IS_DETACHING (sfp->parentdp))) +wake_up_interruptible_all (&sfp->read_wait); + }atomic_dec (&sfp->waiting); }
@@ -3410,15 +3403,14 @@ sg_read_append(struct sg_request *srp, void __user *outp, int num_xfer)* If there are multiple requests outstanding, the speed of this function is * important. SG_PACK_ID_WILDCARD is -1 and that case is typically the fast * path. This function is only used in the non-blocking cases. Returns pointer -* to (first) matching sg_request or NULL. If found, sg_request state is moved -* from SG_RS_AWAIT_RCV to SG_RS_BUSY. -* The 'busy' pointer is only set to true when no requests are found in the -* SG_RS_AWAIT_RCV state and SG_IS_DETACHING() is true and there is at least -* one request in SG_RS_INFLIGHT state; in all other cases *busy is set false. +* to (first) matching sg_request, or NULL if the caller should wait for +* request completion, or an ERR_PTR if the caller should return an error +* without waiting for request completion. If a request is found, sg_request +* state is moved from SG_RS_AWAIT_RCV to SG_RS_BUSY. */ static struct sg_request * -sg_find_srp_by_id (struct sg_fd *sfp,int pack_id,bool *busy) +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 ;
@@ -3432,13 +3424,12 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id, bool *busy)struct sg_request *srp =NULL ;struct xarray *xafp = &sfp->srp_arr; - *busy = false;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 ) -return NULL ; + goto check_detach; } s_idx = (l_await_idx <0 ) ?0 : l_await_idx;
@@ -3502,16 +3493,6 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id, bool *busy) } }/* here if one of above loops does _not_ find a match */ -if (unlikely (SG_IS_DETACHING (sfp->parentdp))) { -xa_for_each (xafp, idx, srp) { -if (!srp) -continue ; -if (atomic_read (&srp->rq_st) ==SG_RS_INFLIGHT ) { - *busy = true; -return NULL ; - } - } - }if (IS_ENABLED (CONFIG_SCSI_PROC_FS )) {if (search_for_1) {__maybe_unused const char *cptp ="pack_id=" ;
@@ -3525,6 +3506,39 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id, bool *busy) __func__, cptp, pack_id); } } +check_detach: +if (unlikely (SG_IS_DETACHING (sfp->parentdp))) { +/* +* The device is being removed, but we still want to allow +* inflight commands to complete. Return NULL when there are +* still inflight commands that will generate responses to +* receive. Return ERR_PTR(-ENODEV) when there are no commands +* left. +*/ +if (atomic_read (&sfp->submitted) ==0 ) +return ERR_PTR (-ENODEV ); +if (search_for_1) { +xa_for_each (xafp, idx, srp) { +if (srp && pack_id == srp->pack_id) { +switch (atomic_read (&srp->rq_st)) { +case SG_RS_INFLIGHT : +case SG_RS_AWAIT_RCV : +/* +* Wait to return this +* response. +*/ +return NULL ; + +default : +break ; + } + } + } +/* No command matching pack_id. */ +return ERR_PTR (-ENODEV ); + } +/* else return NULL below */ + }return NULL ; good: SG_LOG(5, sfp, "%s: %s%d found [srp=0x%pK]\n", __func__, "pack_id=",
Signature · 1 line
2.37.3
Douglas Gilbert
maintainerPATCH v25 44/44 sg: bump version to 4.0.14
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 | 6 +++--- 2 files changed, 9 insertions(+), 8 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 =40014 ;/* [x]xyyzz where [x] empty when x=0 */ +#define SG_VERSION_STR "4.0.14"/* [x]x.[y]y.zz */ +static char *sg_version_date ="20221016" ;#include <linux/module.h>
include/uapi/scsi/sg.h +3 -3
@@ -12,10 +12,10 @@* 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) -* This version is for Linux 4 and 5 series kernels. +* Version 4.0.14 (20221009) +* This version is for Linux 5 and 6 series kernels. * * Documentation * =============
Signature · 1 line
2.37.3