sg: add v4 interface
Subsystems (2)
Re: [PATCH v7 27/38] sg: add sg v4 interface support
In reply to Douglas GilbertQuoted 22 lines · Douglas Gilbert
Add support for the sg v4 interface based on struct
sg_io_v4found ininclude/uapi/linux/bsg.hand only previously supported by the bsg driver. Addioctl(SG_IOSUBMIT)andioctl(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 existingwrite()andread()system calls.For sync (blocking) usage expand the existing
ioctl(SG_IO)to additionally accept the sg v4 interface object.Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 458 +++++++++++++++++++++++++++++++++-------- include/uapi/scsi/sg.h | 37 +++- 2 files changed, 405 insertions(+), 90 deletions(-)drivers/scsi/sg.c +0
-0
drivers/scsi/sg.c line 15
Quoted 10 lines
drivers/scsi/sg.c +2
-1@@ -7,8 +7,9 @@* * Original driver (sg.c): * Copyright (C) 1992 Lawrence Foard -* Version 2 and 3 extensions to driver: +* Version 2, 3 and 4 extensions to driver: * Copyright (C) 1998 - 2019 Douglas Gilbert +* */ static int sg_version_num =30901 ;/* [x]xyyzz where [x] empty when x=0 */
As you modify the copyright you might want to update the year, too ...
Quoted 171 lines
drivers/scsi/sg.c +43
-17@@ -40,11 +41,12 @@ 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> -#include "scsi.h" +#include <scsi/scsi.h> +#include <scsi/scsi_eh.h> #include <scsi/scsi_dbg.h> #include <scsi/scsi_host.h> #include <scsi/scsi_driver.h> @@ -76,6 +78,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) */ @@ -100,6 +105,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_DIO_IN_USE 3/* false->indirect_IO,mmap; 1->dio */ @@ -165,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) */ @@ -178,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 */ @@ -238,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; };@@ -247,12 +268,12 @@ static void sg_rq_end_io(struct request *rq, blk_status_t status);/* Declarations of other static functions used before they are defined */ static int sg_proc_init (void );static 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_request *srp,struct sg_fd *sfp,int minlen); -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,@@ -260,11 +281,11 @@ 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);static struct sg_request *sg_add_request (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);@@ -274,8 +295,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)@@ -332,6 +356,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);@@ -350,7 +378,7 @@ sg_wait_open_event(struct sg_device *sdp, bool o_excl) {int res =0 ; -if (o_excl) { +if (unlikely (o_excl)) {while (atomic_read (&sdp->open_cnt) >0 ) {mutex_unlock (&sdp->open_rel_lock); res = wait_event_interruptible@@ -359,13 +387,13 @@ sg_wait_open_event(struct sg_device *sdp, bool o_excl) atomic_read(&sdp->open_cnt) == 0));mutex_lock (&sdp->open_rel_lock); -if (res)/* -ERESTARTSYS */ +if (unlikely (res))/* -ERESTARTSYS */ return res; -if (SG_IS_DETACHING (sdp)) +if (unlikely (SG_IS_DETACHING (sdp)))return -ENODEV ; } }else { -while (SG_HAVE_EXCLUDE (sdp)) { +while (unlikely (SG_HAVE_EXCLUDE (sdp))) {mutex_unlock (&sdp->open_rel_lock); res = wait_event_interruptible (sdp->open_wait,@@ -373,13 +401,12 @@ sg_wait_open_event(struct sg_device *sdp, bool o_excl) !SG_HAVE_EXCLUDE(sdp)));mutex_lock (&sdp->open_rel_lock); -if (res)/* -ERESTARTSYS */ +if (unlikely (res))/* -ERESTARTSYS */ return res; -if (SG_IS_DETACHING (sdp)) +if (unlikely (SG_IS_DETACHING (sdp)))return -ENODEV ; } } -return res; }@@ -393,9 +420,9 @@ sg_wait_open_event(struct sg_device *sdp, bool o_excl) static inline int
drivers/scsi/sg.c sg_wait_open_event(struct sg_device *sdp, bool o_excl) · line 428
Quoted 10 lines
drivers/scsi/sg.c +2
-2sg_allow_if_err_recovery(struct sg_device *sdp, bool non_block) { -if (!sdp) +if (unlikely (!sdp))return -EPROTO ; -if (SG_IS_DETACHING (sdp)) +if (unlikely (SG_IS_DETACHING (sdp)))return -ENODEV ;if (non_block)return 0 ;
Please move the likely/unlikely statements into a different patch. They don't really relate to the subject of this patch.
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerRe: [PATCH v7 27/38] sg: add sg v4 interface support
Quoted 18 lines · Hannes Reinecke
Quoted 17 lines · Douglas Gilbert
Add support for the sg v4 interface based on struct
sg_io_v4found ininclude/uapi/linux/bsg.hand only previously supported by the bsg driver. Addioctl(SG_IOSUBMIT)andioctl(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 existingwrite()andread()system calls.For sync (blocking) usage expand the existing
ioctl(SG_IO)to additionally accept the sg v4 interface object.Signed-off-by Douglas Gilbert <[email protected]>---
drivers/scsi/sg.c | 458 +++++++++++++++++++++++++++++++++--------include/uapi/scsi/sg.h| 37 +++-2 files changed, 405 insertions(+), 90 deletions(-)
Quoted 16 lines
Quoted 13 lines
drivers/scsi/sg.c +2
-1@@ -7,8 +7,9 @@* * Original driver (sg.c): * Copyright (C) 1992 Lawrence Foard -* Version 2 and 3 extensions to driver: +* Version 2, 3 and 4 extensions to driver: * Copyright (C) 1998 - 2019 Douglas Gilbert +* */ Quoted 1 line
static int
sg_version_num= 30901; /* [x]xyyzz where [x] empty when x=0 */As you modify the copyright you might want to update the year, too ...
I'll do all that housekeeping in the final patch of this series [38].
Quoted 170 lines
Quoted 170 lines
drivers/scsi/sg.c +6
-2@@ -40,11 +41,12 @@ 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> -#include "scsi.h" +#include <scsi/scsi.h> +#include <scsi/scsi_eh.h> #include <scsi/scsi_dbg.h> #include <scsi/scsi_host.h> #include <scsi/scsi_driver.h>@@ -76,6 +78,9 @@ static struct kmem_cache *sg_sense_cache; #define SG_MEMPOOL_MIN_NR 4 static 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_arrassumes SG_RS_AWAIT_RCV==2 */SG_RS_INACTIVE= 0, /* request not in use (e.g. on fl) */drivers/scsi/sg.c +0
-0@@ -100,6 +105,7 @@ enum sg_rq_state { /* N.B. sg_rq_state_arr assumesSG_RS_AWAIT_RCV==2 */
#defineSG_ADD_RQ_MAX_RETRIES40 /* to stop infinite_trylock(s)*/ /* Bit positions (flags) forsg_request::frq_bmbitmask 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_DIO_IN_USE 3 /* false->indirect_IO,mmap; 1->dio */drivers/scsi/sg.c +9
-0@@ -165,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_sgatelement array of struct page* */ int buflen; /* capacity in bytes (dlen<=buflen) */drivers/scsi/sg.c +0
-0@@ -178,7 +193,10 @@ struct sg_fd;struct
sg_request{ /* active SCSI command or inactive request */ structsg_scatter_holdsgat_h; /* hold buffer, perhaps scatter list */ - structsg_slice_hdr3s_hdr3; /* subset ofsg_io_hdr*/
+ union {
+ structsg_slice_hdr3s_hdr3; /* subset ofsg_io_hdr*/
+ structsg_slice_hdr4s_hdr4; /* reduced size structsg_io_v4*/ + };
u32 duration; /* cmd duration in milliseconds */
u32 rq_flags; /* hold user supplied flags */
u32 rq_idx; /* my index within parent'ssrp_arr*/drivers/scsi/sg.c +0
-0@@ -238,7 +256,10 @@ struct sg_device { /* holds the state of each scsigeneric device */
structsg_comm_wr_t{ /* arguments tosg_common_write()*/ int timeout;
unsigned long frq_bm[1]; /* see SG_FRQ_* defines above */ - structsg_io_hdr*h3p;
+ union { /* selector is frq_bm.SG_FRQ_IS_V4I */ + structsg_io_hdr*h3p;
+ structsg_io_v4*h4p;
+ };
u8 *cmnd;
};drivers/scsi/sg.c +4
-4@@ -247,12 +268,12 @@ static void sg_rq_end_io(struct request *rq, blk_status_t status); /* Declarations of other static functions used before they are defined */ static int sg_proc_init(void); static 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_request *srp, struct sg_fd *sfp, int minlen); -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,drivers/scsi/sg.c +2
-2@@ -260,11 +281,11 @@ 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); static struct sg_request *sg_add_request(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);drivers/scsi/sg.c +3
-0@@ -274,8 +295,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)@@ -332,6 +356,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,
+ "structsg_io_v4should be larger thansg_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);drivers/scsi/sg.c +1
-1@@ -350,7 +378,7 @@ sg_wait_open_event(struct sg_device *sdp, bool o_excl) { int res = 0; - if (o_excl) { + if (unlikely(o_excl)) { while (atomic_read(&sdp->open_cnt) > 0) { mutex_unlock(&sdp->open_rel_lock);res =
wait_event_interruptibledrivers/scsi/sg.c +3
-3@@ -359,13 +387,13 @@ sg_wait_open_event(struct sg_device *sdp, bool o_excl) atomic_read(&sdp->open_cnt) == 0)); mutex_lock(&sdp->open_rel_lock); - if (res) /* -ERESTARTSYS */ + if (unlikely(res)) /* -ERESTARTSYS */ return res; - if (SG_IS_DETACHING(sdp)) + if (unlikely(SG_IS_DETACHING(sdp))) return -ENODEV; } } else { - while (SG_HAVE_EXCLUDE(sdp)) { + while (unlikely(SG_HAVE_EXCLUDE(sdp))) { mutex_unlock(&sdp->open_rel_lock);res =
wait_event_interruptible
(sdp->open_wait,drivers/scsi/sg.c +2
-3@@ -373,13 +401,12 @@ sg_wait_open_event(struct sg_device *sdp, bool o_excl) !SG_HAVE_EXCLUDE(sdp))); mutex_lock(&sdp->open_rel_lock); - if (res) /* -ERESTARTSYS */ + if (unlikely(res)) /* -ERESTARTSYS */ return res; - if (SG_IS_DETACHING(sdp)) + if (unlikely(SG_IS_DETACHING(sdp))) return -ENODEV; } } - return res; }@@ -393,9 +420,9 @@ sg_wait_open_event(struct sg_device *sdp, bool o_excl)static inline int
sg_allow_if_err_recovery(struct sg_device *sdp, bool non_block){
- if (!sdp)
+ if (unlikely(!sdp))
return-EPROTO;
Quoted 8 lines
Quoted 5 lines
- if (
SG_IS_DETACHING(sdp))
+ if (unlikely(SG_IS_DETACHING(sdp)))
return-ENODEV;
if (non_block)
return 0;Please move the likely/unlikely statements into a different patch. They don't really relate to the subject of this patch.
There is a patch for precisely that but since it's at 47, it is in the second patchset. There has been some leakage which I'll try to get out.
There are still some there from new code, earlier patches in this series and patches that other people have applied to this driver. It seems a bit perverse to take them all out just so they can all be "cleanly" added back in, within a single patch.
Doug Gilbert
Douglas Gilbert
maintainerPATCH v7 28/38 sg: rework debug info
In reply to Douglas GilbertSince the version 2 driver, the state of the driver can be found with 'cat /proc/scsi/sg/debug'. As the driver becomes more threaded and IO faster (e.g. scsi_debug with a command timer of 5 microseconds), the existing state dump can become misleading as the state can change during the "snapshot". The new approach in this patch is to allocate a buffer of SG_PROC_DEBUG_SZ bytes and use scnprintf() to populate it. Only when the whole state is captured (or the buffer fills) is the output to the caller's terminal performed. The previous approach was line based: assemble a line of information and then output it.
Locks are taken as required for short periods and should not interfere with a disk IO intensive program. Operations such as closing a sg file descriptor or removing a sg device may be held up for a short while (microseconds).
--- drivers/scsi/sg.c | 256 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 177 insertions(+), 79 deletions(-)
drivers/scsi/sg.c +177 -79
@@ -2398,7 +2398,7 @@ sg_rq_end_io(struct request *rq, blk_status_t status)if (unlikely ((srp->rq_result &SG_ML_RESULT_MSK ) && slen >0 ))sg_check_sense (sdp, srp, slen);if (slen >0 ) { -if (scsi_rp->sense) { +if (scsi_rp->sense && !srp->sense_bp) { srp->sense_bp =mempool_alloc (sg_sense_pool,GFP_ATOMIC );if (srp->sense_bp) {
@@ -2411,6 +2411,9 @@ sg_rq_end_io(struct request *rq, blk_status_t status)pr_warn ("%s: sense but can't alloc buffer\n" , __func__); } + }else if (srp->sense_bp) { + slen =0 ; +pr_warn ("%s: non-NULL srp->sense_bp ? ?\n" , __func__); }else { slen =0 ;pr_warn ("%s: sense_len>0 but sense==NULL\n" , __func__);
@@ -3939,116 +3942,211 @@ sg_proc_seq_show_devstrs(struct seq_file *s, void *v) return 0; } -/* must be called while holding sg_index_lock */ -static void -sg_proc_debug_helper (struct seq_file *s,struct sg_device *sdp) +/* Writes debug info for one sg_request in obp buffer */ +static int +sg_proc_debug_sreq (struct sg_request *srp,int to,char *obp,int len) { -int k; -unsigned long idx, idx2; +bool is_v3v4, v4, is_dur; +int n =0 ; +u32 dur; +enum sg_rq_state rq_st; +const char *cp; + +if (len <1 ) +return 0 ; + v4 =test_bit (SG_FRQ_IS_V4I , srp->frq_bm); + is_v3v4 = v4 ? true : (srp->s_hdr3.interface_id !='\0' ); +if (srp->parentfp->rsv_srp == srp) + cp = (is_v3v4 && (srp->rq_flags &SG_FLAG_MMAP_IO )) ? +" mmap>> " :" rsv>> " ; +else + cp = (srp->rq_info &SG_INFO_DIRECT_IO_MASK ) ? +" dio>> " :" " ; + rq_st =atomic_read (&srp->rq_st); + dur =sg_get_dur (srp, &rq_st, &is_dur); + n +=scnprintf (obp + n, len - n,"%s%s: dlen=%d/%d id=%d" , cp, +sg_rq_st_str (rq_st, false), srp->sgat_h.dlen, + srp->sgat_h.buflen, (int )srp->pack_id); +if (is_dur)/* cmd/req has completed, waiting for ... */ + n +=scnprintf (obp + n, len - n," dur=%ums" , dur); +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); +return n; +} + +/* Writes debug info for one sg fd (including its sg requests) in obp buffer */ +static int +sg_proc_debug_fd (struct sg_fd *fp,char *obp,int len,unsigned long idx) +{ +int n =0 ; +int to, k; +unsigned long iflags;struct sg_request *srp; -struct sg_fd *fp; -const char * cp; -unsigned int ms; +/* sgat=-1 means unavailable */ + to = (fp->timeout >=0 ) ?jiffies_to_msecs (fp->timeout) :-999 ; +if (to <0 ) + n +=scnprintf (obp + n, len - n,"BAD timeout=%d" , + fp->timeout); +else if (to %1000 ) + n +=scnprintf (obp + n, len - n,"timeout=%dms rs" , to); +else + n +=scnprintf (obp + n, len - n,"timeout=%ds rs" , to /1000 ); + n +=scnprintf (obp + n, len - n,"v_buflen=%d idx=%lu\n cmd_q=%d " , + fp->rsv_srp->sgat_h.buflen, idx, + (int )test_bit (SG_FFD_CMD_Q , fp->ffd_bm)); + n +=scnprintf (obp + n, len - n, +"f_packid=%d k_orphan=%d ffd_bm=0x%lx\n" , + (int )test_bit (SG_FFD_FORCE_PACKID , fp->ffd_bm), + (int )test_bit (SG_FFD_KEEP_ORPHAN , fp->ffd_bm), + fp->ffd_bm[0 ]); + n +=scnprintf (obp + n, len - n," mmap_called=%d\n" , +test_bit (SG_FFD_MMAP_CALLED , fp->ffd_bm)); + n +=scnprintf (obp + n, len - n, +" submitted=%d waiting=%d open thr_id=%d\n" , +atomic_read (&fp->submitted), +atomic_read (&fp->waiting), fp->tid); + k =0 ; +xa_lock_irqsave (&fp->srp_arr, iflags); +xa_for_each (&fp->srp_arr, idx, srp) { +if (!srp) +continue ; +if (xa_get_mark (&fp->srp_arr, idx,SG_XA_RQ_INACTIVE )) +continue ; + n +=sg_proc_debug_sreq (srp, fp->timeout, obp + n, len - n); + ++k; +if ((k %8 ) ==0 ) {/* don't hold up isr_s too long */ +xa_unlock_irqrestore (&fp->srp_arr, iflags); +cpu_relax (); +xa_lock_irqsave (&fp->srp_arr, iflags); + } + } +if (k ==0 ) + n +=scnprintf (obp + n, len - n," No requests active\n" ); k =0 ; +xa_for_each_marked (&fp->srp_arr, idx, srp,SG_XA_RQ_INACTIVE ) { +if (!srp) +continue ; +if (k ==0 ) + n +=scnprintf (obp + n, len - n," Inactives:\n" ); + n +=sg_proc_debug_sreq (srp, fp->timeout, obp + n, len - n); + ++k; +if ((k %8 ) ==0 ) {/* don't hold up isr_s too long */ +xa_unlock_irqrestore (&fp->srp_arr, iflags); +cpu_relax (); +xa_lock_irqsave (&fp->srp_arr, iflags); + } + } +xa_unlock_irqrestore (&fp->srp_arr, iflags); +return n; +} + +/* Writes debug info for one sg device (including its sg fds) in obp buffer */ +static int +sg_proc_debug_sdev (struct sg_device *sdp,char *obp,int len,int *fd_counterp) +{ +int n =0 ; +int my_count =0 ; +unsigned long idx; +struct scsi_device *ssdp = sdp->device; +struct sg_fd *fp; +char *disk_name; +int *countp; + + countp = fd_counterp ? fd_counterp : &my_count; + disk_name = (sdp->disk ? sdp->disk->disk_name :"?_?" ); + n +=scnprintf (obp + n, len - n," >>> device=%s " , disk_name); + n +=scnprintf (obp + n, len - n,"%d:%d:%d:%llu " , ssdp->host->host_no, + ssdp->channel, ssdp->id, ssdp->lun); + n +=scnprintf (obp + n, len - n, +" max_sgat_sz,elems=2^%d,%d excl=%d open_cnt=%d\n" , +ilog2 (sdp->max_sgat_sz), sdp->max_sgat_elems, +SG_HAVE_EXCLUDE (sdp),atomic_read (&sdp->open_cnt));xa_for_each (&sdp->sfp_arr, idx, fp) {if (!fp)continue ; - k++; -seq_printf (s," FD(%d): timeout=%dms buflen=%d (res)sgat=%d low_dma=%d idx=%lu\n" , - k,jiffies_to_msecs (fp->timeout), - fp->rsv_srp->sgat_h.buflen, - (int )fp->rsv_srp->sgat_h.num_sgat, - (int )sdp->device->host->unchecked_isa_dma, idx); -seq_printf (s," cmd_q=%d f_packid=%d k_orphan=%d closed=0\n" , - (int )test_bit (SG_FFD_CMD_Q , fp->ffd_bm), - (int )test_bit (SG_FFD_FORCE_PACKID , fp->ffd_bm), - (int )test_bit (SG_FFD_KEEP_ORPHAN , fp->ffd_bm)); -seq_printf (s," submitted=%d waiting=%d\n" , -atomic_read (&fp->submitted), -atomic_read (&fp->waiting)); -xa_for_each (&fp->srp_arr, idx2, srp) { -const struct sg_slice_hdr3 *sh3p = &srp->s_hdr3; -bool is_v3 = (sh3p->interface_id !='\0' ); -enum sg_rq_state rq_st =atomic_read (&srp->rq_st); - -if (!srp) -continue ; -if (srp->parentfp->rsv_srp == srp) { -if (is_v3 && (SG_FLAG_MMAP_IO & sh3p->flags)) - cp =" mmap>> " ; -else - cp =" rb>> " ; - }else { -if (SG_INFO_DIRECT_IO_MASK & srp->rq_info) - cp =" dio>> " ; -else - cp =" " ; - } -seq_puts (s, cp); -seq_puts (s,sg_rq_st_str (rq_st, false)); -seq_printf (s,": id=%d len/blen=%d/%d" , - srp->pack_id, srp->sgat_h.dlen, - srp->sgat_h.buflen); -if (rq_st ==SG_RS_AWAIT_RCV || - rq_st ==SG_RS_RCV_DONE ) { -seq_printf (s," dur=%d" , srp->duration); - goto fin_line; - } - ms =jiffies_to_msecs (jiffies); -seq_printf (s," t_o/elap=%d/%d" , - (is_v3 ? sh3p->timeout : -jiffies_to_msecs (fp->timeout)), - (ms > srp->duration ? ms - srp->duration : -0 )); -fin_line: -seq_printf (s,"ms sgat=%d op=0x%02x\n" , - srp->sgat_h.num_sgat, (int )srp->cmd_opcode); - } -if (xa_empty (&fp->srp_arr)) -seq_puts (s," No requests active\n" ); + ++*countp; + n +=scnprintf (obp + n, len - n," FD(%d): " , *countp); + n +=sg_proc_debug_fd (fp, obp + n, len - n, idx); } +return n; } +/* Called via dbg_seq_ops once for each sg device */ static int sg_proc_seq_show_debug (struct seq_file *s,void *v) { +bool found = false; +bool trunc = false; +const int bp_len =SG_PROC_DEBUG_SZ ; +int n =0 ; +int k =0 ; +unsigned long iflags;struct sg_proc_deviter *it = (struct sg_proc_deviter *)v;struct sg_device *sdp; -unsigned long iflags; +int *fdi_p; +char *bp; +char *disk_name; +char b1[128 ]; + b1[0 ] ='\0' ;if (it && (0 == it->index))seq_printf (s,"max_active_device=%d def_reserved_size=%d\n" , - (int )it->max, sg_big_buff); - + (int )it->max, def_reserved_size); + fdi_p = it ? &it->fd_index : &k; + bp =kzalloc (bp_len, __GFP_NOWARN |GFP_KERNEL ); +if (!bp) { +seq_printf (s,"%s: Unable to allocate %d on heap, finish\n" , + __func__, bp_len); +return -1 ; + }read_lock_irqsave (&sg_index_lock, iflags); sdp = it ?sg_lookup_dev (it->index) :NULL ;if (NULL == sdp) goto skip;if (!xa_empty (&sdp->sfp_arr)) { -seq_printf (s," >>> device=%s " , sdp->disk->disk_name); + found = true; + disk_name = (sdp->disk ? sdp->disk->disk_name :"?_?" );if (SG_IS_DETACHING (sdp)) -seq_puts (s,"detaching pending close " ); +snprintf (b1,sizeof (b1)," >>> device=%s %s\n" , + disk_name,"detaching pending close\n" );else if (sdp->device) { -struct scsi_device *scsidp = sdp->device; - -seq_printf (s,"%d:%d:%d:%llu em=%d" , - scsidp->host->host_no, - scsidp->channel, scsidp->id, - scsidp->lun, - scsidp->host->hostt->emulated); + n =sg_proc_debug_sdev (sdp, bp, bp_len, fdi_p); +if (n >= bp_len -1 ) { + trunc = true; +if (bp[bp_len -2 ] !='\n' ) + bp[bp_len -2 ] ='\n' ; + } + }else { +snprintf (b1,sizeof (b1)," >>> device=%s %s\n" , + disk_name,"sdp->device==NULL, skip" ); } -seq_printf (s," max_sgat_sz=%d excl=%d open_cnt=%d\n" , - sdp->max_sgat_sz,SG_HAVE_EXCLUDE (sdp), -atomic_read (&sdp->open_cnt)); -sg_proc_debug_helper (s, sdp); } skip:read_unlock_irqrestore (&sg_index_lock, iflags); +if (found) { +if (n >0 ) { +seq_puts (s, bp); +if (seq_has_overflowed (s)) + goto s_ovfl; +if (trunc) +seq_printf (s," >> Output truncated %s\n" , +"due to buffer size" ); + }else if (b1[0 ]) { +seq_puts (s, b1); +if (seq_has_overflowed (s)) + goto s_ovfl; + } + } +s_ovfl: +kfree (bp);return 0 ; } -#endif /* CONFIG_SCSI_PROC_FS (~300 lines back) */ +#endif /* CONFIG_SCSI_PROC_FS (~400 lines back) */ module_init (init_sg);module_exit (exit_sg);
Signature · 1 line
2.25.1
Re: [PATCH v7 28/38] sg: rework debug info
Quoted 17 lines · Douglas Gilbert
Since the version 2 driver, the state of the driver can be found with 'cat
/proc/scsi/sg/debug'. As the driver becomes more threaded and IO faster (e.g.scsi_debugwith a command timer of 5 microseconds), the existing state dump can become misleading as the state can change during the "snapshot". The new approach in this patch is to allocate a buffer ofSG_PROC_DEBUG_SZbytes and usescnprintf()to populate it. Only when the whole state is captured (or the buffer fills) is the output to the caller's terminal performed. The previous approach was line based: assemble a line of information and then output it.Locks are taken as required for short periods and should not interfere with a disk IO intensive program. Operations such as closing a sg file descriptor or removing a sg device may be held up for a short while (microseconds).
Quoted 5 lines
Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 256 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 177 insertions(+), 79 deletions(-)
Personally, I'm not a big fan of using procfs to display driver status.
I would rather move this interface to debugfs and declare the existing interface as deprecated.
We probably would need to observe the usual rules etc (compat functions, compile-time config etc), but I really would like to deprecate using procfs in SCSI...
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerPATCH v7 29/38 sg: add 8 byte SCSI LUN to sg_scsi_id
In reply to Douglas GilbertThe existing ioctl(SG_GET_SCSI_ID) fills a object of type struct sg_scsi_id whose last field is int unused[2]. Add an anonymous union with u8 scsi_lun[8] sharing those last 8 bytes. This patch will place the current device's full LUN in the scsi_lun array using T10's preferred LUN format (i.e. an array of 8 bytes) when ioctl(SG_GET_SCSI_ID) is called.
Note that structure already contains a 'lun' field but that is a 32 bit integer. Users of this upgrade should choose the scsi_lun array field henceforth but existing code can remain as it is and will get the same 'lun' value with the version 3 or version 4 driver.
--- drivers/scsi/sg.c | 6 ++++-- include/uapi/scsi/sg.h | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-)
drivers/scsi/sg.c +4 -2
@@ -1843,6 +1843,7 @@ static int sg_ctl_scsi_id(struct scsi_device *sdev, struct sg_fd *sfp, void __user *p) { struct sg_scsi_id ss_id; +struct scsi_lun lun8b;SG_LOG (3 , sfp,"%s: SG_GET_SCSI_ID\n" , __func__); ss_id.host_no = sdev->host->host_no;
@@ -1852,8 +1853,9 @@ sg_ctl_scsi_id(struct scsi_device *sdev, struct sg_fd *sfp, void __user *p) ss_id.scsi_type = sdev->type; ss_id.h_cmd_per_lun = sdev->host->cmd_per_lun; ss_id.d_queue_depth = sdev->queue_depth; - ss_id.unused[0 ] =0 ; - ss_id.unused[1 ] =0 ; +int_to_scsilun (sdev->lun, &lun8b); +/* ss_id.scsi_lun is in an anonymous union with 'int unused[2]' */ +memcpy (ss_id.scsi_lun, lun8b.scsi_lun,8 );if (copy_to_user (p, &ss_id,sizeof (struct sg_scsi_id )))return -EFAULT ;return 0 ;
include/uapi/scsi/sg.h +4 -1
@@ -136,7 +136,10 @@ typedef struct sg_scsi_id {int scsi_type;/* TYPE_... defined in scsi/scsi.h */ short h_cmd_per_lun;/* host (adapter) maximum commands per lun */ short d_queue_depth;/* device (or adapter) maximum queue length */ -int unused[2 ]; +union { +int unused[2 ];/* as per version 3 driver */ +__u8 scsi_lun[8 ];/* full 8 byte SCSI LUN [in v4 driver] */ + }; } sg_scsi_id_t;/* For backward compatibility v4 driver yields at most SG_MAX_QUEUE of these */
Signature · 1 line
2.25.1
Re: [PATCH v7 29/38] sg: add 8 byte SCSI LUN to sg_scsi_id
Quoted 14 lines · Douglas Gilbert
The existing
ioctl(SG_GET_SCSI_ID)fills a object of type structsg_scsi_idwhose last field is int unused[2]. Add an anonymous union with u8 scsi_lun[8] sharing those last 8 bytes. This patch will place the current device's full LUN in thescsi_lunarray using T10's preferred LUN format (i.e. an array of 8 bytes) whenioctl(SG_GET_SCSI_ID)is called.Note that structure already contains a 'lun' field but that is a 32 bit integer. Users of this upgrade should choose the
scsi_lunarray field henceforth but existing code can remain as it is and will get the same 'lun' value with the version 3 or version 4 driver.
Quoted 7 lines
Reviewed-by Hannes Reinecke <[email protected]>Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 6 ++++-- include/uapi/scsi/sg.h | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
kbuild test robot
build botRe: [PATCH v7 29/38] sg: add 8 byte SCSI LUN to sg_scsi_id
In reply to Douglas GilbertThank you for the patch! Perhaps something to improve:
Hi Douglas,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on mkp-scsi/for-next]
[also build test WARNING on scsi/for-next linus/master v5.6-rc3 next-20200228] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Douglas-Gilbert/sg-add-v4-interface/20200228-205828 base: https://git.kernel.org/pub/scm/linux/kernel/git/mkp/scsi.git for-next config: i386-allyesconfig (attached as .config) compiler: gcc-7 (Debian 7.5.0-5) 7.5.0 reproduce: # save the attached .config to linux build tree make ARCH=i386
If you fix the issue, kindly add following tag
All warnings (new ones prefixed by >>):
drivers//scsi/sg.c: In function 'sg_receive_v4':
drivers//scsi/sg.c:1234:18: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
h4p->response = (u64)srp->s_hdr4.sbp;
^
drivers//scsi/sg.c: In function 'sg_ioctl_common':
>> drivers//scsi/sg.c:2074:1: warning: the frame size of 2056 bytes is larger than 2048 bytes [-Wframe-larger-than=]
}
^vim +2074 drivers//scsi/sg.c
e9a6fc36629575 Douglas Gilbert 2020-02-27 186337b9d1e0017b2d Jörn Engel 2012-04-12 1864 static long
2e69ea3df607a4 Douglas Gilbert 2020-02-27 1865 sg_ioctl_common(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,
d320a9551e394c Arnd Bergmann 2019-03-15 1866 unsigned int cmd_in, void __user *p)
^1da177e4c3f41 Linus Torvalds 2005-04-16 1867 {
e9a6fc36629575 Douglas Gilbert 2020-02-27 1868 bool read_only = O_RDWR != (filp->f_flags & O_ACCMODE);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1869 int val;
e9a6fc36629575 Douglas Gilbert 2020-02-27 1870 int result = 0;
^1da177e4c3f41 Linus Torvalds 2005-04-16 1871 int __user *ip = p;
2e69ea3df607a4 Douglas Gilbert 2020-02-27 1872 struct sg_request *srp;
e9a6fc36629575 Douglas Gilbert 2020-02-27 1873 struct scsi_device *sdev;
f6652476691f24 Douglas Gilbert 2020-02-27 1874 unsigned long idx;
e9a6fc36629575 Douglas Gilbert 2020-02-27 1875 __maybe_unused const char *pmlp = ", pass to mid-level";
^1da177e4c3f41 Linus Torvalds 2005-04-16 1876
d92b69363fbad1 Douglas Gilbert 2020-02-27 1877 SG_LOG(6, sfp, "%s: cmd=0x%x, O_NONBLOCK=%d\n", __func__, cmd_in,
d92b69363fbad1 Douglas Gilbert 2020-02-27 1878 !!(filp->f_flags & O_NONBLOCK));
e9a6fc36629575 Douglas Gilbert 2020-02-27 1879 if (unlikely(SG_IS_DETACHING(sdp)))
e9a6fc36629575 Douglas Gilbert 2020-02-27 1880 return -ENODEV;
e9a6fc36629575 Douglas Gilbert 2020-02-27 1881 sdev = sdp->device;
^1da177e4c3f41 Linus Torvalds 2005-04-16 1882
^1da177e4c3f41 Linus Torvalds 2005-04-16 1883 switch (cmd_in) {
^1da177e4c3f41 Linus Torvalds 2005-04-16 1884 case SG_IO:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1885 return sg_ctl_sg_io(filp, sdp, sfp, p);
164fadf16aae69 Douglas Gilbert 2020-02-27 1886 case SG_IOSUBMIT:
164fadf16aae69 Douglas Gilbert 2020-02-27 1887 SG_LOG(3, sfp, "%s: SG_IOSUBMIT\n", __func__);
164fadf16aae69 Douglas Gilbert 2020-02-27 1888 return sg_ctl_iosubmit(filp, sfp, p);
164fadf16aae69 Douglas Gilbert 2020-02-27 1889 case SG_IORECEIVE:
164fadf16aae69 Douglas Gilbert 2020-02-27 1890 SG_LOG(3, sfp, "%s: SG_IORECEIVE\n", __func__);
164fadf16aae69 Douglas Gilbert 2020-02-27 1891 return sg_ctl_ioreceive(filp, sfp, p);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1892 case SG_GET_SCSI_ID:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1893 return sg_ctl_scsi_id(sdev, sfp, p);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1894 case SG_SET_FORCE_PACK_ID:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1895 SG_LOG(3, sfp, "%s: SG_SET_FORCE_PACK_ID\n", __func__);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1896 result = get_user(val, ip);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1897 if (result)
^1da177e4c3f41 Linus Torvalds 2005-04-16 1898 return result;
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1899 assign_bit(SG_FFD_FORCE_PACKID, sfp->ffd_bm, !!val);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1900 return 0;
5c0a9d591daedc Douglas Gilbert 2020-02-27 1901 case SG_GET_PACK_ID: /* or tag of oldest "read"-able, -1 if none */
e9a6fc36629575 Douglas Gilbert 2020-02-27 1902 val = -1;
f6652476691f24 Douglas Gilbert 2020-02-27 1903 xa_for_each_marked(&sfp->srp_arr, idx, srp, SG_XA_RQ_AWAIT) {
f6652476691f24 Douglas Gilbert 2020-02-27 1904 if (!srp)
f6652476691f24 Douglas Gilbert 2020-02-27 1905 continue;
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1906 val = srp->pack_id;
e9a6fc36629575 Douglas Gilbert 2020-02-27 1907 break;
^1da177e4c3f41 Linus Torvalds 2005-04-16 1908 }
e9a6fc36629575 Douglas Gilbert 2020-02-27 1909 SG_LOG(3, sfp, "%s: SG_GET_PACK_ID=%d\n", __func__, val);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1910 return put_user(val, ip);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1911 case SG_GET_NUM_WAITING:
dafd31b0fcfdc9 Douglas Gilbert 2020-02-27 1912 return put_user(atomic_read(&sfp->waiting), ip);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1913 case SG_GET_SG_TABLESIZE:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1914 SG_LOG(3, sfp, "%s: SG_GET_SG_TABLESIZE=%d\n", __func__,
e9a6fc36629575 Douglas Gilbert 2020-02-27 1915 sdp->max_sgat_sz);
1ee385d070dc3b Douglas Gilbert 2020-02-27 1916 return put_user(sdp->max_sgat_sz, ip);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1917 case SG_SET_RESERVED_SIZE:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1918 result = get_user(val, ip);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1919 if (!result) {
e9a6fc36629575 Douglas Gilbert 2020-02-27 1920 if (val >= 0 && val <= (1024 * 1024 * 1024)) {
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1921 mutex_lock(&sfp->f_mutex);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1922 result = sg_set_reserved_sz(sfp, val);
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1923 mutex_unlock(&sfp->f_mutex);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1924 } else {
e9a6fc36629575 Douglas Gilbert 2020-02-27 1925 SG_LOG(3, sfp, "%s: invalid size\n", __func__);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1926 result = -EINVAL;
1bc0eb0446158c Hannes Reinecke 2017-04-07 1927 }
^1da177e4c3f41 Linus Torvalds 2005-04-16 1928 }
e9a6fc36629575 Douglas Gilbert 2020-02-27 1929 return result;
^1da177e4c3f41 Linus Torvalds 2005-04-16 1930 case SG_GET_RESERVED_SIZE:
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1931 mutex_lock(&sfp->f_mutex);
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1932 val = min_t(int, sfp->rsv_srp->sgat_h.buflen,
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1933 sdp->max_sgat_sz);
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1934 mutex_unlock(&sfp->f_mutex);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1935 SG_LOG(3, sfp, "%s: SG_GET_RESERVED_SIZE=%d\n",
e9a6fc36629575 Douglas Gilbert 2020-02-27 1936 __func__, val);
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1937 result = put_user(val, ip);
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1938 return result;
^1da177e4c3f41 Linus Torvalds 2005-04-16 1939 case SG_SET_COMMAND_Q:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1940 SG_LOG(3, sfp, "%s: SG_SET_COMMAND_Q\n", __func__);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1941 result = get_user(val, ip);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1942 if (result)
^1da177e4c3f41 Linus Torvalds 2005-04-16 1943 return result;
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1944 assign_bit(SG_FFD_CMD_Q, sfp->ffd_bm, !!val);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1945 return 0;
^1da177e4c3f41 Linus Torvalds 2005-04-16 1946 case SG_GET_COMMAND_Q:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1947 SG_LOG(3, sfp, "%s: SG_GET_COMMAND_Q\n", __func__);
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1948 return put_user(test_bit(SG_FFD_CMD_Q, sfp->ffd_bm), ip);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1949 case SG_SET_KEEP_ORPHAN:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1950 SG_LOG(3, sfp, "%s: SG_SET_KEEP_ORPHAN\n", __func__);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1951 result = get_user(val, ip);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1952 if (result)
^1da177e4c3f41 Linus Torvalds 2005-04-16 1953 return result;
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1954 assign_bit(SG_FFD_KEEP_ORPHAN, sfp->ffd_bm, !!val);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1955 return 0;
^1da177e4c3f41 Linus Torvalds 2005-04-16 1956 case SG_GET_KEEP_ORPHAN:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1957 SG_LOG(3, sfp, "%s: SG_GET_KEEP_ORPHAN\n", __func__);
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1958 return put_user(test_bit(SG_FFD_KEEP_ORPHAN, sfp->ffd_bm),
20d8ebcd78db48 Douglas Gilbert 2020-02-27 1959 ip);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1960 case SG_GET_VERSION_NUM:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1961 SG_LOG(3, sfp, "%s: SG_GET_VERSION_NUM\n", __func__);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1962 return put_user(sg_version_num, ip);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1963 case SG_GET_REQUEST_TABLE:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1964 return sg_ctl_req_tbl(sfp, p);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1965 case SG_SCSI_RESET:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1966 SG_LOG(3, sfp, "%s: SG_SCSI_RESET\n", __func__);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1967 break;
e9a6fc36629575 Douglas Gilbert 2020-02-27 1968 case SG_SET_TIMEOUT:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1969 SG_LOG(3, sfp, "%s: SG_SET_TIMEOUT\n", __func__);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1970 result = get_user(val, ip);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1971 if (result)
e9a6fc36629575 Douglas Gilbert 2020-02-27 1972 return result;
e9a6fc36629575 Douglas Gilbert 2020-02-27 1973 if (val < 0)
e9a6fc36629575 Douglas Gilbert 2020-02-27 1974 return -EIO;
e9a6fc36629575 Douglas Gilbert 2020-02-27 1975 if (val >= mult_frac((s64)INT_MAX, USER_HZ, HZ))
e9a6fc36629575 Douglas Gilbert 2020-02-27 1976 val = min_t(s64, mult_frac((s64)INT_MAX, USER_HZ, HZ),
e9a6fc36629575 Douglas Gilbert 2020-02-27 1977 INT_MAX);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1978 sfp->timeout_user = val;
e9a6fc36629575 Douglas Gilbert 2020-02-27 1979 sfp->timeout = mult_frac(val, HZ, USER_HZ);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1980 return 0;
e9a6fc36629575 Douglas Gilbert 2020-02-27 1981 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
e9a6fc36629575 Douglas Gilbert 2020-02-27 1982 /* strange ..., for backward compatibility */
e9a6fc36629575 Douglas Gilbert 2020-02-27 1983 SG_LOG(3, sfp, "%s: SG_GET_TIMEOUT\n", __func__);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1984 return sfp->timeout_user;
e9a6fc36629575 Douglas Gilbert 2020-02-27 1985 case SG_SET_FORCE_LOW_DMA:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1986 /*
e9a6fc36629575 Douglas Gilbert 2020-02-27 1987 * N.B. This ioctl never worked properly, but failed to
e9a6fc36629575 Douglas Gilbert 2020-02-27 1988 * return an error value. So returning '0' to keep
e9a6fc36629575 Douglas Gilbert 2020-02-27 1989 * compatibility with legacy applications.
e9a6fc36629575 Douglas Gilbert 2020-02-27 1990 */
e9a6fc36629575 Douglas Gilbert 2020-02-27 1991 SG_LOG(3, sfp, "%s: SG_SET_FORCE_LOW_DMA\n", __func__);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1992 return 0;
e9a6fc36629575 Douglas Gilbert 2020-02-27 1993 case SG_GET_LOW_DMA:
e9a6fc36629575 Douglas Gilbert 2020-02-27 1994 SG_LOG(3, sfp, "%s: SG_GET_LOW_DMA\n", __func__);
e9a6fc36629575 Douglas Gilbert 2020-02-27 1995 return put_user((int)sdev->host->unchecked_isa_dma, ip);
b2b2e4df0305e7 Douglas Gilbert 2020-02-27 1996 case SG_NEXT_CMD_LEN: /* active only in v2 interface */
e9a6fc36629575 Douglas Gilbert 2020-02-27 1997 SG_LOG(3, sfp, "%s: SG_NEXT_CMD_LEN\n", __func__);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1998 result = get_user(val, ip);
^1da177e4c3f41 Linus Torvalds 2005-04-16 1999 if (result)
^1da177e4c3f41 Linus Torvalds 2005-04-16 2000 return result;
bf33f87dd04c37 peter chang 2017-02-15 2001 if (val > SG_MAX_CDB_SIZE)
bf33f87dd04c37 peter chang 2017-02-15 2002 return -ENOMEM;
671c65317016c2 Douglas Gilbert 2020-02-27 2003 mutex_lock(&sfp->f_mutex);
671c65317016c2 Douglas Gilbert 2020-02-27 2004 sfp->next_cmd_len = max_t(int, val, 0);
671c65317016c2 Douglas Gilbert 2020-02-27 2005 mutex_unlock(&sfp->f_mutex);
^1da177e4c3f41 Linus Torvalds 2005-04-16 2006 return 0;
^1da177e4c3f41 Linus Torvalds 2005-04-16 2007 case SG_GET_ACCESS_COUNT:
e9a6fc36629575 Douglas Gilbert 2020-02-27 2008 SG_LOG(3, sfp, "%s: SG_GET_ACCESS_COUNT\n", __func__);
^1da177e4c3f41 Linus Torvalds 2005-04-16 2009 /* faked - we don't have a real access count anymore */
e9a6fc36629575 Douglas Gilbert 2020-02-27 2010 val = (sdev ? 1 : 0);
^1da177e4c3f41 Linus Torvalds 2005-04-16 2011 return put_user(val, ip);
^1da177e4c3f41 Linus Torvalds 2005-04-16 2012 case SG_EMULATED_HOST:
e9a6fc36629575 Douglas Gilbert 2020-02-27 2013 SG_LOG(3, sfp, "%s: SG_EMULATED_HOST\n", __func__);
20d8ebcd78db48 Douglas Gilbert 2020-02-27 2014 if (unlikely(SG_IS_DETACHING(sdp)))
20d8ebcd78db48 Douglas Gilbert 2020-02-27 2015 return -ENODEV;
e9a6fc36629575 Douglas Gilbert 2020-02-27 2016 return put_user(sdev->host->hostt->emulated, ip);
^1da177e4c3f41 Linus Torvalds 2005-04-16 2017 case SCSI_IOCTL_SEND_COMMAND:
e9a6fc36629575 Douglas Gilbert 2020-02-27 2018 SG_LOG(3, sfp, "%s: SCSI_IOCTL_SEND_COMMAND\n", __func__);
e9a6fc36629575 Douglas Gilbert 2020-02-27 2019 return sg_scsi_ioctl(sdev->request_queue, NULL, filp->f_mode,
e9a6fc36629575 Douglas Gilbert 2020-02-27 2020 p);
^1da177e4c3f41 Linus Torvalds 2005-04-16 2021 case SG_SET_DEBUG:
e9a6fc36629575 Douglas Gilbert 2020-02-27 2022 SG_LOG(3, sfp, "%s: SG_SET_DEBUG\n", __func__);
^1da177e4c3f41 Linus Torvalds 2005-04-16 2023 result = get_user(val, ip);
^1da177e4c3f41 Linus Torvalds 2005-04-16 2024 if (result)
^1da177e4c3f41 Linus Torvalds 2005-04-16 2025 return result;
20d8ebcd78db48 Douglas Gilbert 2020-02-27 2026 assign_bit(SG_FDEV_LOG_SENSE, sdp->fdev_bm, !!val);
0b192c5441f451 Douglas Gilbert 2020-02-27 2027 if (val == 0) /* user can force recalculation */
0b192c5441f451 Douglas Gilbert 2020-02-27 2028 sg_calc_sgat_param(sdp);
^1da177e4c3f41 Linus Torvalds 2005-04-16 2029 return 0;
44ec95425c1d9d Alan Stern 2007-02-20 2030 case BLKSECTGET:
e9a6fc36629575 Douglas Gilbert 2020-02-27 2031 SG_LOG(3, sfp, "%s: BLKSECTGET\n", __func__);
e9a6fc36629575 Douglas Gilbert 2020-02-27 2032 return put_user(max_sectors_bytes(sdev->request_queue), ip);
6da127ad0918f9 Christof Schmitt 2008-01-11 2033 case BLKTRACESETUP:
e9a6fc36629575 Douglas Gilbert 2020-02-27 2034 SG_LOG(3, sfp, "%s: BLKTRACESETUP\n", __func__);
e9a6fc36629575 Douglas Gilbert 2020-02-27 2035 return blk_trace_setup(sdev->request_queue,
6da127ad0918f9 Christof Schmitt 2008-01-11 2036 sdp->disk->disk_name,
76e3a19d0691bb Martin Peschke 2009-01-30 2037 MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
7475c8ae1b7bfc Bart Van Assche 2017-08-25 2038 NULL, p);
6da127ad0918f9 Christof Schmitt 2008-01-11 2039 case BLKTRACESTART:
e9a6fc36629575 Douglas Gilbert 2020-02-27 2040 SG_LOG(3, sfp, "%s: BLKTRACESTART\n", __func__);
e9a6fc36629575 Douglas Gilbert 2020-02-27 2041 return blk_trace_startstop(sdev->request_queue, 1);
6da127ad0918f9 Christof Schmitt 2008-01-11 2042 case BLKTRACESTOP:
e9a6fc36629575 Douglas Gilbert 2020-02-27 2043 SG_LOG(3, sfp, "%s: BLKTRACESTOP\n", __func__);
e9a6fc36629575 Douglas Gilbert 2020-02-27 2044 return blk_trace_startstop(sdev->request_queue, 0);
6da127ad0918f9 Christof Schmitt 2008-01-11 2045 case BLKTRACETEARDOWN:
e9a6fc36629575 Douglas Gilbert 2020-02-27 2046 SG_LOG(3, sfp, "%s: BLKTRACETEARDOWN\n", __func__);
e9a6fc36629575 Douglas Gilbert 2020-02-27 2047 return blk_trace_remove(sdev->request_queue);
906d15fbd23c12 Christoph Hellwig 2014-10-11 2048 case SCSI_IOCTL_GET_IDLUN:
e9a6fc36629575 Douglas Gilbert 2020-02-27 2049 SG_LOG(3, sfp, "%s: SCSI_IOCTL_GET_IDLUN %s\n", __func__,
e9a6fc36629575 Douglas Gilbert 2020-02-27 2050 pmlp);
e9a6fc36629575 Douglas Gilbert 2020-02-27 2051 break;
906d15fbd23c12 Christoph Hellwig 2014-10-11 2052 case SCSI_IOCTL_GET_BUS_NUMBER:
e9a6fc36629575 Douglas Gilbert 2020-02-27 2053 SG_LOG(3, sfp, "%s: SCSI_IOCTL_GET_BUS_NUMBER%s\n",
e9a6fc36629575 Douglas Gilbert 2020-02-27 2054 __func__, pmlp);
e9a6fc36629575 Douglas Gilbert 2020-02-27 2055 break;
906d15fbd23c12 Christoph Hellwig 2014-10-11 2056 case SCSI_IOCTL_PROBE_HOST:
e9a6fc36629575 Douglas Gilbert 2020-02-27 2057 SG_LOG(3, sfp, "%s: SCSI_IOCTL_PROBE_HOST%s",
e9a6fc36629575 Douglas Gilbert 2020-02-27 2058 __func__, pmlp);
e9a6fc36629575 Douglas Gilbert 2020-02-27 2059 break;
906d15fbd23c12 Christoph Hellwig 2014-10-11 2060 case SG_GET_TRANSFORM:
e9a6fc36629575 Douglas Gilbert 2020-02-27 2061 SG_LOG(3, sfp, "%s: SG_GET_TRANSFORM%s\n", __func__, pmlp);
906d15fbd23c12 Christoph Hellwig 2014-10-11 2062 break;
^1da177e4c3f41 Linus Torvalds 2005-04-16 2063 default:
e9a6fc36629575 Douglas Gilbert 2020-02-27 2064 SG_LOG(3, sfp, "%s: unrecognized ioctl [0x%x]%s\n",
e9a6fc36629575 Douglas Gilbert 2020-02-27 2065 __func__, cmd_in, pmlp);
^1da177e4c3f41 Linus Torvalds 2005-04-16 2066 if (read_only)
e9a6fc36629575 Douglas Gilbert 2020-02-27 2067 return -EPERM; /* don't know, so take safer approach */
906d15fbd23c12 Christoph Hellwig 2014-10-11 2068 break;
^1da177e4c3f41 Linus Torvalds 2005-04-16 2069 }
98dd8630b9e9cb Douglas Gilbert 2020-02-27 2070 result = sg_allow_if_err_recovery(sdp, filp->f_flags & O_NDELAY);
20d8ebcd78db48 Douglas Gilbert 2020-02-27 2071 if (unlikely(result))
906d15fbd23c12 Christoph Hellwig 2014-10-11 2072 return result;
d320a9551e394c Arnd Bergmann 2019-03-15 2073 return -ENOIOCTLCMD;^1da177e4c3f41 Linus Torvalds 2005-04-16 @2074 }
^1da177e4c3f41 Linus Torvalds 2005-04-16 2075
:::::: The code at line 2074 was first introduced by commit :::::: 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 Linux-2.6.12-rc2
:::::: TO: Linus Torvalds <[email protected]> :::::: CC: Linus Torvalds <[email protected]>
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Douglas Gilbert
maintainerPATCH v7 30/38 sg: expand sg_comm_wr_t
In reply to Douglas GilbertThe internal struct sg_comm_wr_t was added when the number of arguments to sg_common_write() became excessive. Expand this idea so multiple calls to sg_fetch_cmnd() can be deferred until a scsi_request object is ready to receive the command. This saves a 252 byte stack allocation on every submit path. Prior to this and a few other changes, the kernel infrastructure was warning about excessive stack usage.
*** Reviewed-by: Hannes Reinecke <[email protected]>
--- drivers/scsi/sg.c | 175 ++++++++++++++++++++++++---------------------- 1 file changed, 90 insertions(+), 85 deletions(-)
drivers/scsi/sg.c +90 -85
@@ -255,35 +255,37 @@ struct sg_device { /* holds the state of each scsi generic device */struct sg_comm_wr_t {/* arguments to sg_common_write() */ int timeout; +int cmd_len;unsigned long frq_bm[1 ];/* see SG_FRQ_* defines above */ union {/* selector is frq_bm.SG_FRQ_IS_V4I */ struct sg_io_hdr *h3p;struct sg_io_v4 *h4p; }; -u8 *cmnd; +struct sg_fd *sfp; +struct file *filp; +const u8 __user *u_cmdp; };/* tasklet or soft irq callback */ static void sg_rq_end_io (struct request *rq,blk_status_t status);/* Declarations of other static functions used before they are defined */ static int sg_proc_init (void ); -static int sg_start_req (struct sg_request *srp,u8 *cmd,int cmd_len, -struct sg_io_v4 *h4p,int dxfer_dir); +static int sg_start_req (struct sg_request *srp,struct sg_comm_wr_t *cwrp, +int dxfer_dir);static void sg_finish_scsi_blk_rq (struct sg_request *srp);static int sg_mk_sgat (struct sg_request *srp,struct sg_fd *sfp,int minlen);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 struct sg_request *sg_common_write (struct sg_comm_wr_t *cwrp);static int sg_read_append (struct sg_request *srp,void __user *outp,int num_xfer);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); -static struct sg_request *sg_add_request (struct sg_fd *sfp,int dxfr_len, -struct sg_comm_wr_t *cwrp); +static struct sg_request *sg_add_request (struct sg_comm_wr_t *cwrp, +int dxfr_len);static void sg_deact_request (struct sg_fd *sfp,struct sg_request *srp);static struct sg_device *sg_get_dev (int min_dev);static void sg_device_destroy (struct kref *kref);
@@ -573,7 +575,6 @@ 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;
@@ -685,9 +686,6 @@ sg_write(struct file *filp, const char __user *p, size_t count, loff_t *ppos) h3p->flags = input_size;/* structure abuse ... */ h3p->pack_id = ohp->pack_id; h3p->usr_ptr =NULL ; - cmnd[0 ] = opcode; -if (copy_from_user (cmnd +1 , p +1 , cmd_size -1 )) -return -EFAULT ;/* * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV, * but it is possible that the app intended SG_DXFER_TO_DEV, because
@@ -699,13 +697,16 @@ sg_write(struct file *filp, const char __user *p, size_t count, loff_t *ppos)"%s: data in/out %d/%d bytes for SCSI command 0x%x-- guessing data in;\n" " program %s not setting count and/or reply_len properly\n" , __func__, ohp->reply_len - (int )SZ_SG_HEADER , - input_size, (unsigned int)cmnd[0], current->comm); + input_size, (unsigned int)opcode, current->comm); } - cwr.frq_bm[0 ] =0 ;/* initial state clear for all req flags */ cwr.h3p = h3p; + cwr.frq_bm[0 ] =0 ; cwr.timeout = sfp->timeout; - cwr.cmnd = cmnd; - srp =sg_common_write (sfp, &cwr); + cwr.cmd_len = cmd_size; + cwr.filp = filp; + cwr.sfp = sfp; + cwr.u_cmdp = p; + srp =sg_common_write (&cwr);return (IS_ERR (srp)) ?PTR_ERR (srp) : (int )count; }
@@ -746,31 +747,29 @@ static int 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;struct sg_request *srp;struct sg_comm_wr_t cwr; -u8 cmnd[SG_MAX_CDB_SIZE ];/* now doing v3 blocking (sync) or non-blocking submission */ if (hp->flags &SG_FLAG_MMAP_IO ) { - res =sg_chk_mmap (sfp, hp->flags, hp->dxfer_len); +int res =sg_chk_mmap (sfp, hp->flags, hp->dxfer_len); +if (res)return res; }/* when v3 seen, allow cmd_q on this fd (def: no cmd_q) */ set_bit (SG_FFD_CMD_Q , sfp->ffd_bm); ul_timeout =msecs_to_jiffies (hp->timeout); - timeout = min_t(unsigned long, ul_timeout, INT_MAX); - res =sg_fetch_cmnd (filp, sfp, hp->cmdp, hp->cmd_len, cmnd); -if (res) -return res; cwr.frq_bm[0 ] =0 ;__assign_bit (SG_FRQ_SYNC_INVOC , cwr.frq_bm, (int )sync); cwr.h3p = hp; - cwr.timeout = timeout; - cwr.cmnd = cmnd; - srp =sg_common_write (sfp, &cwr); + cwr.timeout = min_t(unsigned long, ul_timeout, INT_MAX); + cwr.cmd_len = hp->cmd_len; + cwr.filp = filp; + cwr.sfp = sfp; + cwr.u_cmdp = hp->cmdp; + srp =sg_common_write (&cwr);if (IS_ERR (srp))return PTR_ERR (srp);if (o_srp)
@@ -782,11 +781,10 @@ 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; + int res = 0;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 ;
@@ -802,18 +800,16 @@ sg_submit_v4(struct file *filp, struct sg_fd *sfp, void __user *p,/* 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.filp = filp; + cwr.sfp = sfp; 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); +__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); + cwr.timeout = min_t(unsigned long, ul_timeout, INT_MAX); + cwr.cmd_len = h4p->request_len; + cwr.u_cmdp =cuptr64 (h4p->request); + srp =sg_common_write (&cwr);if (IS_ERR (srp))return PTR_ERR (srp);if (o_srp)
@@ -991,13 +987,14 @@ sg_execute_cmd(struct sg_fd *sfp, struct sg_request *srp)* 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) +sg_common_write (struct sg_comm_wr_t *cwrp) {int res =0 ; -int dxfr_len, dir, cmd_len; +int dxfr_len, dir;int pack_id =SG_PACK_ID_WILDCARD ;u32 rq_flags; -struct sg_device *sdp = sfp->parentdp; +struct sg_fd *fp = cwrp->sfp; +struct sg_device *sdp = fp->parentdp;struct sg_request *srp;struct sg_io_hdr *hi_p;struct sg_io_v4 *h4p;
@@ -1029,31 +1026,27 @@ sg_common_write(struct sg_fd *sfp, struct sg_comm_wr_t *cwrp)if (dxfr_len >=SZ_256M )return ERR_PTR (-EINVAL ); - srp =sg_add_request (sfp, dxfr_len, cwrp); + srp =sg_add_request (cwrp, dxfr_len);if (IS_ERR (srp))return srp; srp->rq_flags = rq_flags; srp->pack_id = pack_id;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; + srp->s_hdr4.out_resid =0 ; }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, h4p, dir); + res =sg_start_req (srp, cwrp, dir);if (res <0 )/* probably out of space --> -ENOMEM */ goto err_out; +SG_LOG (4 , fp,"%s: opcode=0x%02x, cdb_sz=%d, pack_id=%d\n" , __func__, + srp->cmd_opcode, cwrp->cmd_len, pack_id);if (unlikely (SG_IS_DETACHING (sdp))) { res = -ENODEV ; goto err_out;
@@ -1062,17 +1055,17 @@ sg_common_write(struct sg_fd *sfp, struct sg_comm_wr_t *cwrp) res = -EIDRM ;/* this failure unexpected but observed */ goto err_out; } -if (xa_get_mark (&sfp->srp_arr, srp->rq_idx,SG_XA_RQ_FREE )) { -SG_LOG (1 , sfp,"%s: ahhh, request erased!!!\n" , __func__); +if (xa_get_mark (&fp->srp_arr, srp->rq_idx,SG_XA_RQ_FREE )) { +SG_LOG (1 , fp,"%s: ahhh, request erased!!!\n" , __func__); res = -ENODEV ; goto err_out; } srp->rq->timeout = cwrp->timeout; -sg_execute_cmd (sfp, srp); +sg_execute_cmd (fp, srp);return srp; err_out:sg_finish_scsi_blk_rq (srp); -sg_deact_request (sfp, srp); +sg_deact_request (fp, srp);return ERR_PTR (res); }
@@ -1289,8 +1282,8 @@ sg_ctl_ioreceive(struct file *filp, struct sg_fd *sfp, void __user *p) id));if (unlikely (SG_IS_DETACHING (sdp)))return -ENODEV ; -if (res)/* -ERESTARTSYS as signal hit process */ -return res; +if (res) +return res;/* signal --> -ERESTARTSYS */ }/* now srp should be valid */ return sg_receive_v4 (sfp, srp, p, h4p); }
@@ -2736,7 +2729,7 @@ init_sg(void) } sg_sysfs_valid = true; rc =scsi_register_interface (&sg_interface); -if (0 == rc) { +if (rc ==0 ) {sg_proc_init ();return 0 ; }
@@ -2787,11 +2780,10 @@ sg_chk_dio_allowed(struct sg_device *sdp, struct sg_request *srp,return false; } -static void +static inline void sg_set_map_data (const struct sg_scatter_hold *schp,bool up_valid,struct rq_map_data *mdp) { -memset (mdp,0 ,sizeof (*mdp)); mdp->pages = schp->pages; mdp->page_order = schp->page_order; mdp->nr_entries = schp->num_sgat;
@@ -2800,8 +2792,7 @@ 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, -struct sg_io_v4 *h4p,int dxfer_dir) +sg_start_req (struct sg_request *srp,struct sg_comm_wr_t *cwrp,int dxfer_dir) {bool reserved, us_xfer;int res =0 ;
@@ -2811,7 +2802,7 @@ sg_start_req(struct sg_request *srp, u8 *cmd, int cmd_len,void __user *up;struct request *rq;struct scsi_request *scsi_rp; -struct sg_fd *sfp = srp->parentfp; +struct sg_fd *sfp = cwrp->sfp;struct sg_device *sdp;struct sg_scatter_hold *req_schp;struct request_queue *q;
@@ -2821,20 +2812,21 @@ sg_start_req(struct sg_request *srp, u8 *cmd, int cmd_len,struct rq_map_data map_data; sdp = sfp->parentdp; -if (cmd_len >BLK_MAX_CDB ) {/* for longer SCSI cdb_s */ - long_cmdp =kzalloc (cmd_len,GFP_KERNEL ); +if (cwrp->cmd_len >BLK_MAX_CDB ) {/* for longer SCSI cdb_s */ + long_cmdp =kzalloc (cwrp->cmd_len,GFP_KERNEL );if (!long_cmdp)return -ENOMEM ;SG_LOG (5 , sfp,"%s: long_cmdp=0x%pK ++\n" , __func__, long_cmdp); } -if (h4p) { +if (test_bit (SG_FRQ_IS_V4I , srp->frq_bm)) { +struct sg_io_v4 *h4p = cwrp->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 ) { - r0w =READ ; up =uptr64 (h4p->din_xferp); dxfer_len = (int )h4p->din_xfer_len; iov_count = h4p->din_iovec_count;
@@ -2873,10 +2865,19 @@ sg_start_req(struct sg_request *srp, u8 *cmd, int cmd_len, scsi_rp =scsi_req (rq); srp->rq = rq; -if (cmd_len >BLK_MAX_CDB ) +if (cwrp->cmd_len >BLK_MAX_CDB ) scsi_rp->cmd = long_cmdp; -memcpy (scsi_rp->cmd, cmd, cmd_len); - scsi_rp->cmd_len = cmd_len; +if (cwrp->u_cmdp) + res =sg_fetch_cmnd (cwrp->filp, sfp, cwrp->u_cmdp, + cwrp->cmd_len, scsi_rp->cmd); +else + res = -EPROTO ; +if (res) { +kfree (long_cmdp); +return res; + } + scsi_rp->cmd_len = cwrp->cmd_len; + srp->cmd_opcode = scsi_rp->cmd[0 ]; us_xfer = !(srp->rq_flags &SG_FLAG_NO_DXFER );assign_bit (SG_FRQ_NO_US_XFER , srp->frq_bm, !us_xfer); reserved = (sfp->rsv_srp == srp);
@@ -2900,7 +2901,7 @@ sg_start_req(struct sg_request *srp, u8 *cmd, int cmd_len, }if (likely (md)) {/* normal, "indirect" IO */ -if (unlikely ((srp->rq_flags &SG_FLAG_MMAP_IO ))) { +if (unlikely (srp->rq_flags &SG_FLAG_MMAP_IO )) {/* mmap IO must use and fit in reserve request */ if (!reserved || dxfer_len > req_schp->buflen) res = reserved ? -ENOMEM : -EBUSY ;
@@ -2909,7 +2910,7 @@ sg_start_req(struct sg_request *srp, u8 *cmd, int cmd_len, res =sg_mk_sgat (srp, sfp, up_sz); } -if (res) +if (unlikely (res)) goto fini;sg_set_map_data (req_schp, !!up, md);
@@ -3260,7 +3261,7 @@ sg_mk_srp_sgat(struct sg_fd *sfp, bool first, int db_len)return n_srp;if (db_len >0 ) { res =sg_mk_sgat (n_srp, sfp, db_len); -if (res) { +if (unlikely (res)) {kfree (n_srp);return ERR_PTR (res); }
@@ -3313,18 +3314,18 @@ sg_build_reserve(struct sg_fd *sfp, int buflen)* failure returns a negated errno value twisted by ERR_PTR() macro. */ static struct sg_request * -sg_add_request (struct sg_fd *sfp,int dxfr_len,struct sg_comm_wr_t *cwrp) +sg_add_request (struct sg_comm_wr_t *cwrp,int dxfr_len) {bool act_empty = false;bool found = false; -bool mk_new_srp = false; +bool mk_new_srp = true;bool try_harder = false; -int res;int num_inactive =0 ;unsigned long idx, last_idx, iflags; +struct sg_fd *fp = cwrp->sfp;struct sg_request *r_srp =NULL ;/* request to return */ struct sg_request *last_srp =NULL ; -struct xarray *xafp = &sfp->srp_arr; +struct xarray *xafp = &fp->srp_arr;__maybe_unused const char *cp; start_again:
@@ -3378,18 +3379,19 @@ sg_add_request(struct sg_fd *sfp, int dxfr_len, struct sg_comm_wr_t *cwrp) mk_new_srp = true; }if (mk_new_srp) { -bool allow_cmd_q =test_bit (SG_FFD_CMD_Q , sfp->ffd_bm); +bool allow_cmd_q =test_bit (SG_FFD_CMD_Q , fp->ffd_bm); +int res;u32 n_idx;struct xa_limit xal = { .max =0 , .min =0 }; cp ="new" ; -if (!allow_cmd_q &&atomic_read (&sfp->submitted) >0 ) { +if (!allow_cmd_q &&atomic_read (&fp->submitted) >0 ) { r_srp =ERR_PTR (-EDOM ); -SG_LOG (6 , sfp,"%s: trying 2nd req but cmd_q=false\n" , +SG_LOG (6 , fp,"%s: trying 2nd req but cmd_q=false\n" , __func__); goto fini; } - r_srp =sg_mk_srp_sgat (sfp, act_empty, dxfr_len); + r_srp =sg_mk_srp_sgat (fp, act_empty, dxfr_len);if (IS_ERR (r_srp)) {if (!try_harder && dxfr_len <SG_DEF_SECTOR_SZ && num_inactive >0 ) {
@@ -3400,11 +3402,11 @@ sg_add_request(struct sg_fd *sfp, int dxfr_len, struct sg_comm_wr_t *cwrp) }atomic_set (&r_srp->rq_st,SG_RS_BUSY );xa_lock_irqsave (xafp, iflags); - xal.max =atomic_inc_return (&sfp->req_cnt); + xal.max =atomic_inc_return (&fp->req_cnt); res =__xa_alloc (xafp, &n_idx, r_srp, xal,GFP_KERNEL );xa_unlock_irqrestore (xafp, iflags);if (res <0 ) { -SG_LOG (1 , sfp,"%s: xa_alloc() failed, errno=%d\n" , +SG_LOG (1 , fp,"%s: xa_alloc() failed, errno=%d\n" , __func__, -res);sg_remove_sgat (r_srp);kfree (r_srp);
@@ -3413,17 +3415,18 @@ sg_add_request(struct sg_fd *sfp, int dxfr_len, struct sg_comm_wr_t *cwrp) } idx = n_idx; r_srp->rq_idx = idx; - r_srp->parentfp = sfp; -SG_LOG (4 , sfp,"%s: mk_new_srp=0x%pK ++\n" , __func__, r_srp); + r_srp->parentfp = fp; +SG_LOG (4 , fp,"%s: mk_new_srp=0x%pK ++\n" , __func__, r_srp); } r_srp->frq_bm[0 ] = cwrp->frq_bm[0 ];/* assumes <= 32 req flags */ r_srp->sgat_h.dlen = dxfr_len;/* must be <= r_srp->sgat_h.buflen */ r_srp->cmd_opcode =0xff ;/* set invalid opcode (VS), 0x0 is TUR */ fini:if (IS_ERR (r_srp)) -SG_LOG (1 , sfp,"%s: err=%ld\n" , __func__,PTR_ERR (r_srp)); +SG_LOG (1 , fp,"%s: err=%ld\n" , __func__,PTR_ERR (r_srp));if (!IS_ERR (r_srp)) -SG_LOG (4 , sfp,"%s: %s r_srp=0x%pK\n" , __func__, cp, r_srp); +SG_LOG (4 , fp,"%s: %s %sr_srp=0x%pK\n" , __func__, cp, + ((r_srp == fp->rsv_srp) ?"[rsv] " :"" ), r_srp);return r_srp; }
@@ -3478,6 +3481,8 @@ sg_add_sfp(struct sg_device *sdp)__assign_bit (SG_FFD_CMD_Q , sfp->ffd_bm,SG_DEF_COMMAND_Q );__assign_bit (SG_FFD_KEEP_ORPHAN , sfp->ffd_bm,SG_DEF_KEEP_ORPHAN );__assign_bit (SG_FFD_Q_AT_TAIL , sfp->ffd_bm,SG_DEFAULT_Q_AT ); +atomic_set (&sfp->submitted,0 ); +atomic_set (&sfp->waiting,0 );/* * SG_SCATTER_SZ initializes scatter_elem_sz but different value may * be given as driver/module parameter (e.g. 'scatter_elem_sz=8192').
Signature · 1 line
2.25.1
Douglas Gilbert
maintainerPATCH v7 31/38 sg: add sg_iosubmit_v3 and sg_ioreceive_v3 ioctls
In reply to Douglas GilbertAdd ioctl(SG_IOSUBMIT_V3) and ioctl(SG_IORECEIVE_V3). These ioctls are meant to be (almost) drop-in replacements for the write()/read() async version 3 interface. They only accept the version 3 interface.
See the webpage at: http://sg.danny.cz/sg/sg_v40.html
specifically the table in the section titled: "13 SG interface support changes".
If sgv3 is a struct sg_io_hdr object, suitably configured, then
res = write(sg_fd, &sgv3, sizeof(sgv3));
and
res = ioctl(sg_fd, SG_IOSUBMIT_V3, &sgv3);are equivalent. Dito for read() and ioctl(SG_IORECEIVE_V3).
--- drivers/scsi/sg.c | 77 +++++++++++++++++++++++++++++++++++++++++- include/uapi/scsi/sg.h | 6 ++++ 2 files changed, 82 insertions(+), 1 deletion(-)
drivers/scsi/sg.c +76 -1
@@ -835,6 +835,24 @@ sg_ctl_iosubmit(struct file *filp, struct sg_fd *sfp, void __user *p)return -EPERM ; } +static int +sg_ctl_iosubmit_v3 (struct file *filp,struct sg_fd *sfp,void __user *p) +{ +int res; +u8 hdr_store[SZ_SG_IO_V4 ];/* max(v3interface, v4interface) */ +struct sg_io_hdr *h3p = (struct sg_io_hdr *)hdr_store; +struct sg_device *sdp = sfp->parentdp; + + res =sg_allow_if_err_recovery (sdp, (filp->f_flags &O_NONBLOCK )); +if (unlikely (res)) +return res; +if (copy_from_user (h3p, p,SZ_SG_IO_HDR )) +return -EFAULT ; +if (h3p->interface_id =='S' ) +return sg_v3_submit (filp, sfp, h3p, false,NULL ); +return -EPERM ; +} +#if IS_ENABLED (SG_LOG_ACTIVE ) static void sg_rq_state_fail(struct sg_fd *sfp, enum sg_rq_state exp_old_st,
@@ -1224,7 +1242,7 @@ sg_receive_v4(struct sg_fd *sfp, struct sg_request *srp, void __user *p, h4p->din_resid = srp->in_resid; h4p->dout_resid = srp->s_hdr4.out_resid; h4p->usr_ptr = srp->s_hdr4.usr_ptr; - h4p->response = (u64 )srp->s_hdr4.sbp; + h4p->response = (u64 )(unsigned long )srp->s_hdr4.sbp; h4p->request_extra = srp->pack_id;if (p) {if (copy_to_user (p, h4p,SZ_SG_IO_V4 ))
@@ -1288,6 +1306,57 @@ sg_ctl_ioreceive(struct file *filp, struct sg_fd *sfp, void __user *p)return sg_receive_v4 (sfp, srp, p, h4p); } +/* +* Called when ioctl(SG_IORECEIVE_V3) received. Expects a v3 interface. +* Checks if O_NONBLOCK file flag given, if not checks given flags field +* to see if SGV4_FLAG_IMMED is set. Either of these implies non blocking. +* When non-blocking and there is no request waiting, yields EAGAIN; +* otherwise it waits. +*/ +static int +sg_ctl_ioreceive_v3 (struct file *filp,struct sg_fd *sfp,void __user *p) +{ +bool non_block = !!(filp->f_flags &O_NONBLOCK ); +int res; +int pack_id =SG_PACK_ID_WILDCARD ; +u8 v3_holder[SZ_SG_IO_HDR ]; +struct sg_io_hdr *h3p = (struct sg_io_hdr *)v3_holder; +struct sg_device *sdp = sfp->parentdp; +struct sg_request *srp; + + res =sg_allow_if_err_recovery (sdp, non_block); +if (unlikely (res)) +return res; +/* Get first three 32 bit integers: guard, proto+subproto */ +if (copy_from_user (h3p, p,SZ_SG_IO_HDR )) +return -EFAULT ; +/* for v3: interface_id=='S' (in a 32 bit int) */ +if (h3p->interface_id !='S' ) +return -EPERM ; +if (h3p->flags &SGV4_FLAG_IMMED ) + non_block = true;/* set by either this or O_NONBLOCK */ +SG_LOG (3 , sfp,"%s: non_block(+IMMED)=%d\n" , __func__, non_block); + +if (test_bit (SG_FFD_FORCE_PACKID , sfp->ffd_bm)) + pack_id = h3p->pack_id; + + srp =sg_find_srp_by_id (sfp, pack_id); +if (!srp) {/* nothing available so wait on packet or */ +if (unlikely (SG_IS_DETACHING (sdp))) +return -ENODEV ; +if (non_block) +return -EAGAIN ; + res =wait_event_interruptible + (sfp->read_wait, +sg_get_ready_srp (sfp, &srp, pack_id)); +if (unlikely (SG_IS_DETACHING (sdp))) +return -ENODEV ; +if (unlikely (res)) +return res;/* signal --> -ERESTARTSYS */ + }/* now srp should be valid */ +return sg_receive_v3 (sfp, srp,SZ_SG_IO_HDR , p); +} +static int sg_read_v1v2 (void __user *buf,int count,struct sg_fd *sfp,struct sg_request *srp)
@@ -1879,9 +1948,15 @@ sg_ioctl_common(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,case SG_IOSUBMIT :SG_LOG (3 , sfp,"%s: SG_IOSUBMIT\n" , __func__);return sg_ctl_iosubmit (filp, sfp, p); +case SG_IOSUBMIT_V3 : +SG_LOG (3 , sfp,"%s: SG_IOSUBMIT_V3\n" , __func__); +return sg_ctl_iosubmit_v3 (filp, sfp, p);case SG_IORECEIVE :SG_LOG (3 , sfp,"%s: SG_IORECEIVE\n" , __func__);return sg_ctl_ioreceive (filp, sfp, p); +case SG_IORECEIVE_V3 : +SG_LOG (3 , sfp,"%s: SG_IORECEIVE_V3\n" , __func__); +return sg_ctl_ioreceive_v3 (filp, sfp, p);case SG_GET_SCSI_ID :return sg_ctl_scsi_id (sdev, sfp, p);case SG_SET_FORCE_PACK_ID :
include/uapi/scsi/sg.h +6 -0
@@ -357,6 +357,12 @@ struct sg_header {/* Gives some v4 identifying info to driver, receives associated response */ #define SG_IORECEIVE _IOWR(SG_IOCTL_MAGIC_NUM, 0x42, struct sg_io_v4) +/* Submits a v3 interface object to driver */ +#define SG_IOSUBMIT_V3 _IOWR(SG_IOCTL_MAGIC_NUM, 0x45, struct sg_io_hdr) + +/* Gives some v3 identifying info to driver, receives associated response */ +#define SG_IORECEIVE_V3 _IOWR(SG_IOCTL_MAGIC_NUM, 0x46, struct sg_io_hdr) +/* command queuing is always on when the v3 or v4 interface is used */ #define SG_DEF_COMMAND_Q 0
Signature · 1 line
2.25.1
Re: [PATCH v7 31/38] sg: add sg_iosubmit_v3 and sg_ioreceive_v3 ioctls
Quoted 12 lines · Douglas Gilbert
Add
ioctl(SG_IOSUBMIT_V3)andioctl(SG_IORECEIVE_V3). These ioctls are meant to be (almost) drop-in replacements for the write()/read() async version 3 interface. They only accept the version 3 interface.See the webpage at: http://sg.danny.cz/sg/sg_v40.html
specifically the table in the section titled: "13 SG interface support changes".If sgv3 is a struct
sg_io_hdrobject, suitably configured, thenres = write(sg_fd, &sgv3, sizeof(sgv3)); and res = ioctl(sg_fd, SG_IOSUBMIT_V3, &sgv3);
Quoted 8 lines
are equivalent. Dito for
read()andioctl(SG_IORECEIVE_V3).Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 77 +++++++++++++++++++++++++++++++++++++++++- include/uapi/scsi/sg.h | 6 ++++ 2 files changed, 82 insertions(+), 1 deletion(-)Reviewed-by Hannes Reinecke <[email protected]>
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerPATCH v7 32/38 sg: add some __must_hold macros
In reply to Douglas GilbertIn the case of sg_wait_open_event() which calls mutex_unlock on sdp->open_rel_lock and later calls mutex_lock on the same lock; this macro is needed to stop sparse complaining. In other cases it is a reminder to the coder (a precondition).
--- drivers/scsi/sg.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/scsi/sg.c +5 -1
@@ -377,6 +377,7 @@ sg_check_file_access(struct file *filp, const char *caller)static int sg_wait_open_event (struct sg_device *sdp,bool o_excl) + __must_hold(sdp->open_rel_lock) {int res =0 ;
@@ -1744,6 +1745,7 @@ sg_ctl_sg_io(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,*/ static int sg_set_reserved_sz (struct sg_fd *sfp,int want_rsv_sz) + __must_hold(sfp->f_mutex) {bool use_new_srp = false;int res =0 ;
@@ -3719,12 +3721,12 @@ sg_remove_sfp(struct kref *kref)static int sg_idr_max_id (int id,void *p,void *data) +__must_hold (sg_index_lock) {int *k = data;if (*k < id) *k = id; -return 0 ; }
@@ -4027,6 +4029,7 @@ sg_proc_seq_show_devstrs(struct seq_file *s, void *v)/* Writes debug info for one sg_request in obp buffer */ static int sg_proc_debug_sreq (struct sg_request *srp,int to,char *obp,int len) + __must_hold(sfp->srp_arr.xa_lock) {bool is_v3v4, v4, is_dur;int n =0 ;
@@ -4129,6 +4132,7 @@ sg_proc_debug_fd(struct sg_fd *fp, char *obp, int len, unsigned long idx)/* Writes debug info for one sg device (including its sg fds) in obp buffer */ static int sg_proc_debug_sdev (struct sg_device *sdp,char *obp,int len,int *fd_counterp) +__must_hold (sg_index_lock) {int n =0 ;int my_count =0 ;
Signature · 1 line
2.25.1
Re: [PATCH v7 32/38] sg: add some __must_hold macros
Quoted 10 lines · Douglas Gilbert
In the case of
sg_wait_open_event()which callsmutex_unlockonsdp->open_rel_lockand later callsmutex_lockon the same lock; this macro is needed to stop sparse complaining. In other cases it is a reminder to the coder (a precondition).Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerPATCH v7 33/38 sg: move procfs objects to avoid forward decls
In reply to Douglas GilbertMove the procfs related file_operations and seq_operations definitions toward the end of the source file to minimize the need for forward declarations of the functions they name.
--- drivers/scsi/sg.c | 129 +++++++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 71 deletions(-)
drivers/scsi/sg.c +58 -71
@@ -3789,77 +3789,6 @@ sg_rq_st_str(enum sg_rq_state rq_st, bool long_str)#endif #if IS_ENABLED (CONFIG_SCSI_PROC_FS )/* long, almost to end of file */ -static int sg_proc_seq_show_int (struct seq_file *s,void *v); - -static int sg_proc_single_open_adio (struct inode *inode,struct file *filp); -static ssize_t sg_proc_write_adio (struct file *filp,const char __user *buffer, -size_t count,loff_t *off); -static const struct proc_ops adio_proc_ops = { - .proc_open = sg_proc_single_open_adio, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_write = sg_proc_write_adio, - .proc_release = single_release, -}; - -static int sg_proc_single_open_dressz (struct inode *inode,struct file *filp); -static ssize_t sg_proc_write_dressz (struct file *filp, -const char __user *buffer,size_t count,loff_t *off); -static const struct proc_ops dressz_proc_ops = { - .proc_open = sg_proc_single_open_dressz, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_write = sg_proc_write_dressz, - .proc_release = single_release, -}; - -static int sg_proc_seq_show_version (struct seq_file *s,void *v); -static int sg_proc_seq_show_devhdr (struct seq_file *s,void *v); -static int sg_proc_seq_show_dev (struct seq_file *s,void *v); -static void *dev_seq_start (struct seq_file *s,loff_t *pos); -static void *dev_seq_next (struct seq_file *s,void *v,loff_t *pos); -static void dev_seq_stop (struct seq_file *s,void *v); -static const struct seq_operations dev_seq_ops = { - .start = dev_seq_start, - .next = dev_seq_next, - .stop = dev_seq_stop, - .show = sg_proc_seq_show_dev, -}; - -static int sg_proc_seq_show_devstrs (struct seq_file *s,void *v); -static const struct seq_operations devstrs_seq_ops = { - .start = dev_seq_start, - .next = dev_seq_next, - .stop = dev_seq_stop, - .show = sg_proc_seq_show_devstrs, -}; - -static int sg_proc_seq_show_debug (struct seq_file *s,void *v); -static const struct seq_operations debug_seq_ops = { - .start = dev_seq_start, - .next = dev_seq_next, - .stop = dev_seq_stop, - .show = sg_proc_seq_show_debug, -}; - -static int -sg_proc_init (void ) -{ -struct proc_dir_entry *p; - - p =proc_mkdir ("scsi/sg" ,NULL ); -if (!p) -return 1 ; - -proc_create ("allow_dio" ,0644 , p, &adio_proc_ops); -proc_create_seq ("debug" ,0444 , p, &debug_seq_ops); -proc_create ("def_reserved_size" ,0644 , p, &dressz_proc_ops); -proc_create_single ("device_hdr" ,0444 , p, sg_proc_seq_show_devhdr); -proc_create_seq ("devices" ,0444 , p, &dev_seq_ops); -proc_create_seq ("device_strs" ,0444 , p, &devstrs_seq_ops); -proc_create_single ("version" ,0444 , p, sg_proc_seq_show_version); -return 0 ; -}static int sg_last_dev (void )
@@ -4232,6 +4161,64 @@ sg_proc_seq_show_debug(struct seq_file *s, void *v)return 0 ; } +static const struct proc_ops adio_proc_ops = { + .proc_open = sg_proc_single_open_adio, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_write = sg_proc_write_adio, + .proc_release = single_release, +}; + +static const struct proc_ops dressz_proc_ops = { + .proc_open = sg_proc_single_open_dressz, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_write = sg_proc_write_dressz, + .proc_release = single_release, +}; + +static const struct seq_operations dev_seq_ops = { + .start = dev_seq_start, + .next = dev_seq_next, + .stop = dev_seq_stop, + .show = sg_proc_seq_show_dev, +}; + +static const struct seq_operations devstrs_seq_ops = { + .start = dev_seq_start, + .next = dev_seq_next, + .stop = dev_seq_stop, + .show = sg_proc_seq_show_devstrs, +}; + +static const struct seq_operations debug_seq_ops = { + .start = dev_seq_start, + .next = dev_seq_next, + .stop = dev_seq_stop, + .show = sg_proc_seq_show_debug, +}; + +static int +sg_proc_init (void ) +{ +struct proc_dir_entry *p; + + p =proc_mkdir ("scsi/sg" ,NULL ); +if (!p) +return 1 ; + +proc_create ("allow_dio" ,0644 , p, &adio_proc_ops); +proc_create_seq ("debug" ,0444 , p, &debug_seq_ops); +proc_create ("def_reserved_size" ,0644 , p, &dressz_proc_ops); +proc_create_single ("device_hdr" ,0444 , p, sg_proc_seq_show_devhdr); +proc_create_seq ("devices" ,0444 , p, &dev_seq_ops); +proc_create_seq ("device_strs" ,0444 , p, &devstrs_seq_ops); +proc_create_single ("version" ,0444 , p, sg_proc_seq_show_version); +return 0 ; +} + +/* remove_proc_subtree("scsi/sg", NULL) in exit_sg() does cleanup */ +#endif /* CONFIG_SCSI_PROC_FS (~400 lines back) */ module_init (init_sg);
Signature · 1 line
2.25.1
Re: [PATCH v7 33/38] sg: move procfs objects to avoid forward decls
Quoted 9 lines · Douglas Gilbert
Move the procfs related
file_operationsandseq_operationsdefinitions toward the end of the source file to minimize the need for forward declarations of the functions they name.Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 129 +++++++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 71 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerPATCH v7 34/38 sg: protect multiple receivers
In reply to Douglas GilbertIf two threads call ioctl(SG_IORECEIVE) [or read()] on the same file descriptor there is a potential race on the same request response. Use atomic bit operations to make sure only one thread gets each request response. [The other thread will either get another request response or nothing.]
Also make sfp cleanup a bit more robust and report if the number of submitted requests (which are decremented when completed) is other than the expected value of zero.
--- drivers/scsi/sg.c | 54 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 16 deletions(-)
drivers/scsi/sg.c +38 -16
@@ -111,6 +111,7 @@ enum sg_rq_state { /* N.B. sg_rq_state_arr assumes SG_RS_AWAIT_RCV==2 */#define SG_FRQ_DIO_IN_USE 3/* false->indirect_IO,mmap; 1->dio */ #define SG_FRQ_NO_US_XFER 4/* no user space transfer of data */ #define SG_FRQ_DEACT_ORPHAN 7/* not keeping orphan so de-activate */ +#define SG_FRQ_RECEIVING 8/* guard against multiple receivers */ #define SG_FRQ_BLK_PUT_REQ 9/* set when blk_put_request() called */ /* Bit positions (flags) for sg_fd::ffd_bm bitmask follow */
@@ -1290,6 +1291,7 @@ sg_ctl_ioreceive(struct file *filp, struct sg_fd *sfp, void __user *p)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; +try_again: srp =sg_find_srp_by_id (sfp, id);if (!srp) {/* nothing available so wait on packet or */ if (unlikely (SG_IS_DETACHING (sdp)))
@@ -1304,6 +1306,10 @@ sg_ctl_ioreceive(struct file *filp, struct sg_fd *sfp, void __user *p)if (res)return res;/* signal --> -ERESTARTSYS */ }/* now srp should be valid */ +if (test_and_set_bit (SG_FRQ_RECEIVING , srp->frq_bm)) { +cpu_relax (); + goto try_again; + }return sg_receive_v4 (sfp, srp, p, h4p); }
@@ -1340,7 +1346,7 @@ 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);if (!srp) {/* nothing available so wait on packet or */ if (unlikely (SG_IS_DETACHING (sdp)))
@@ -1355,6 +1361,10 @@ sg_ctl_ioreceive_v3(struct file *filp, struct sg_fd *sfp, void __user *p)if (unlikely (res))return res;/* signal --> -ERESTARTSYS */ }/* now srp should be valid */ +if (test_and_set_bit (SG_FRQ_RECEIVING , srp->frq_bm)) { +cpu_relax (); + goto try_again; + }return sg_receive_v3 (sfp, srp,SZ_SG_IO_HDR , p); }
@@ -1507,6 +1517,7 @@ sg_read(struct file *filp, char __user *p, size_t count, loff_t *ppos) want_id = h2p->pack_id; } } +try_again: srp =sg_find_srp_by_id (sfp, want_id);if (!srp) {/* nothing available so wait on packet to arrive or */ if (unlikely (SG_IS_DETACHING (sdp)))
@@ -1522,6 +1533,10 @@ sg_read(struct file *filp, char __user *p, size_t count, loff_t *ppos)return ret;/* otherwise srp should be valid */ } +if (test_and_set_bit (SG_FRQ_RECEIVING , srp->frq_bm)) { +cpu_relax (); + goto try_again; + }if (srp->s_hdr3.interface_id =='\0' ) ret =sg_read_v1v2 (p, (int )count, sfp, srp);else
@@ -3063,32 +3078,33 @@ sg_finish_scsi_blk_rq(struct sg_request *srp)atomic_dec (&sfp->submitted);atomic_dec (&sfp->waiting); } -if (srp->bio) { -bool us_xfer = !test_bit (SG_FRQ_NO_US_XFER , srp->frq_bm); - -if (us_xfer) { - ret =blk_rq_unmap_user (srp->bio); -if (ret) {/* -EINTR (-4) can be ignored */ -SG_LOG (6 , sfp, -"%s: blk_rq_unmap_user() --> %d\n" , - __func__, ret); - } - } - srp->bio =NULL ; - } -/* In worst case READ data returned to user space by this point */ /* Expect blk_put_request(rq) already called in sg_rq_end_io() */ if (unlikely (!test_and_set_bit (SG_FRQ_BLK_PUT_REQ , srp->frq_bm))) {struct request *rq = srp->rq; + srp->rq =NULL ;if (rq) {/* blk_get_request() may have failed */ if (scsi_req (rq))scsi_req_free_cmd (scsi_req (rq)); - srp->rq =NULL ;blk_put_request (rq); } } +if (srp->bio) { +bool us_xfer = !test_bit (SG_FRQ_NO_US_XFER , srp->frq_bm); +struct bio *bio = srp->bio; + + srp->bio =NULL ; +if (us_xfer && bio) { + ret =blk_rq_unmap_user (bio); +if (ret) {/* -EINTR (-4) can be ignored */ +SG_LOG (6 , sfp, +"%s: blk_rq_unmap_user() --> %d\n" , + __func__, ret); + } + } + } +/* In worst case, READ data returned to user space by this point */ } static int
@@ -3522,6 +3538,7 @@ sg_deact_request(struct sg_fd *sfp, struct sg_request *srp)return ; sbp = srp->sense_bp; srp->sense_bp =NULL ; + srp->frq_bm[0 ] =0 ;sg_rq_state_chg (srp,0 ,SG_RS_INACTIVE , true/* force */ , __func__);/* maybe orphaned req, thus never read */ if (sbp)
@@ -3654,6 +3671,7 @@ static void sg_remove_sfp_usercontext(struct work_struct *work) {__maybe_unused int o_count; +int subm;unsigned long idx, iflags;struct sg_device *sdp; struct sg_fd *sfp = container_of(work, struct sg_fd, ew_fd.work);
@@ -3691,6 +3709,10 @@ sg_remove_sfp_usercontext(struct work_struct *work)SG_LOG (6 , sfp,"%s: kfree: srp=%pK --\n" , __func__, srp);kfree (srp); } + subm =atomic_read (&sfp->submitted); +if (subm !=0 ) +SG_LOG (1 , sfp,"%s: expected submitted=0 got %d\n" , + __func__, subm);xa_destroy (xafp);xa_lock_irqsave (xadp, iflags); e_sfp =__xa_erase (xadp, sfp->idx);
Signature · 1 line
2.25.1
Re: [PATCH v7 34/38] sg: protect multiple receivers
Quoted 6 lines · Douglas Gilbert
If two threads call
ioctl(SG_IORECEIVE)[orread()] on the same file descriptor there is a potential race on the same request response. Use atomic bit operations to make sure only one thread gets each request response. [The other thread will either get another request response or nothing.]
Quoted 9 lines
Also make sfp cleanup a bit more robust and report if the number of submitted requests (which are decremented when completed) is other than the expected value of zero.
Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 54 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 16 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerPATCH v7 35/38 sg: first debugfs support
In reply to Douglas GilbertDuplicate the semantics of 'cat /proc/scsi/sg/debug' on 'cat /sys/kernel/debug/scsi_generic/snapshot'. Make code that generates the snapshot conditional on either CONFIG_SCSI_PROC_FS or CONFIG_DEBUG_FS being defined.
Also add snapshot_devs which can be written with a list of comma separated integers corresponding to sg (minor) device numbers. That file can also be read showing that list. Minus one (or any negative number) means accept all when in the first position (the default) or means the end of the list in a later position. When a subsequent
cat /sys/kernel/debug/scsi_generic/snapshot
is performed, only sg device numbers matching an element in that list are output.
--- drivers/scsi/sg.c | 414 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 333 insertions(+), 81 deletions(-)
drivers/scsi/sg.c +333 -81
@@ -44,6 +44,7 @@ static char *sg_version_date = "20190606";#include <linux/cred.h> /* for sg_check_file_access() */ #include <linux/proc_fs.h> #include <linux/xarray.h> +#include <linux/debugfs.h> #include <scsi/scsi.h> #include <scsi/scsi_eh.h>
@@ -68,6 +69,10 @@ static char *sg_version_date = "20190606";#endif #endif +#if IS_ENABLED (CONFIG_SCSI_PROC_FS ) ||IS_ENABLED (CONFIG_DEBUG_FS ) +#define SG_PROC_OR_DEBUG_FS 1 +#endif +/* SG_MAX_CDB_SIZE should be 260 (spc4r37 section 3.1.30) however the type * of sg_io_hdr::cmd_len can only represent 255. All SCSI commands greater * than 16 bytes are "variable length" whose length is a multiple of 4, so:
@@ -271,6 +276,8 @@ struct sg_comm_wr_t { /* arguments to sg_common_write() */static void sg_rq_end_io (struct request *rq,blk_status_t status);/* Declarations of other static functions used before they are defined */ static int sg_proc_init (void ); +static void sg_dfs_init (void ); +static void sg_dfs_exit (void );static int sg_start_req (struct sg_request *srp,struct sg_comm_wr_t *cwrp,int dxfer_dir);static void sg_finish_scsi_blk_rq (struct sg_request *srp);
@@ -340,7 +347,7 @@ static const char *sg_rq_st_str(enum sg_rq_state rq_st, bool long_str); pr_info("sg: sdp or sfp NULL, " fmt, ##a); \ } while (0)#else -#define SG_LOG(depth, sfp, fmt, a...) { } +#define SG_LOG(depth, sfp, fmt, a...) do { } while (0)#endif /* end of CONFIG_SCSI_LOGGING && SG_DEBUG conditional */
@@ -2757,22 +2764,6 @@ sg_remove_device(struct device *cl_dev, struct class_interface *cl_intf)kref_put (&sdp->d_ref, sg_device_destroy); } -module_param_named (scatter_elem_sz, scatter_elem_sz, int,S_IRUGO |S_IWUSR ); -module_param_named (def_reserved_size, def_reserved_size, int, -S_IRUGO |S_IWUSR ); -module_param_named (allow_dio, sg_allow_dio, int,S_IRUGO |S_IWUSR ); - -MODULE_AUTHOR ("Douglas Gilbert" ); -MODULE_DESCRIPTION ("SCSI generic (sg) driver" ); -MODULE_LICENSE ("GPL" ); -MODULE_VERSION (SG_VERSION_STR ); -MODULE_ALIAS_CHARDEV_MAJOR (SCSI_GENERIC_MAJOR ); - -MODULE_PARM_DESC (scatter_elem_sz,"scatter gather element " -"size (default: max(SG_SCATTER_SZ, PAGE_SIZE))" ); -MODULE_PARM_DESC (def_reserved_size,"size of buffer reserved for each fd" ); -MODULE_PARM_DESC (allow_dio,"allow direct I/O (default: 0 (disallow))" ); -static int __init init_sg (void ) {
@@ -2823,6 +2814,7 @@ init_sg(void) rc =scsi_register_interface (&sg_interface);if (rc ==0 ) {sg_proc_init (); +sg_dfs_init ();return 0 ; }class_destroy (sg_sysfs_class);
@@ -2836,17 +2828,10 @@ init_sg(void)return rc; } -#if !IS_ENABLED (CONFIG_SCSI_PROC_FS ) -static int -sg_proc_init (void ) -{ -return 0 ; -} -#endif - static void __exitexit_sg (void ) { +sg_dfs_exit ();if (IS_ENABLED (CONFIG_SCSI_PROC_FS ))remove_proc_subtree ("scsi/sg" ,NULL );scsi_unregister_interface (&sg_interface);
@@ -3301,7 +3286,7 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id)/* here if one of above loops does _not_ find a match */ if (IS_ENABLED (CONFIG_SCSI_PROC_FS )) {if (search_for_1) { -const char *cptp ="pack_id=" ; +__maybe_unused const char *cptp ="pack_id=" ;if (is_bad_st) SG_LOG(1, sfp, "%s: %s%d wrong state: %s\n",
@@ -3741,17 +3726,6 @@ sg_remove_sfp(struct kref *kref)schedule_work (&sfp->ew_fd.work); } -static int -sg_idr_max_id (int id,void *p,void *data) -__must_hold (sg_index_lock) -{ -int *k = data; - -if (*k < id) - *k = id; -return 0 ; -} -/* must be called with sg_index_lock held */ static struct sg_device *sg_lookup_dev (int dev)
@@ -3781,7 +3755,7 @@ sg_get_dev(int dev)return sdp; } -#if IS_ENABLED (CONFIG_SCSI_PROC_FS ) +#if IS_ENABLED (SG_PROC_OR_DEBUG_FS )static const char *sg_rq_st_str (enum sg_rq_state rq_st,bool long_str) {
@@ -3810,7 +3784,35 @@ sg_rq_st_str(enum sg_rq_state rq_st, bool long_str) }#endif -#if IS_ENABLED (CONFIG_SCSI_PROC_FS )/* long, almost to end of file */ +#if IS_ENABLED (SG_PROC_OR_DEBUG_FS ) + +#define SG_SNAPSHOT_DEV_MAX 4 + +/* +* For snapshot_devs array, -1 or two adjacent the same is terminator. +* -1 in first element of first two elements the same implies all. +*/ +static struct sg_dfs_context_t { +struct dentry *dfs_rootdir; +int snapshot_devs[SG_SNAPSHOT_DEV_MAX ]; +} sg_dfs_cxt; + +struct sg_proc_deviter { +loff_t index; +size_t max; +int fd_index; +}; + +static int +sg_idr_max_id (int id,void *p,void *data) +__must_hold (sg_index_lock) +{ +int *k = data; + +if (*k < id) + *k = id; +return 0 ; +}static int sg_last_dev (void )
@@ -3824,6 +3826,41 @@ sg_last_dev(void)return k +1 ;/* origin 1 */ } +static void * +dev_seq_start (struct seq_file *s,loff_t *pos) +{ +struct sg_proc_deviter *it =kzalloc (sizeof (*it),GFP_KERNEL ); + + s->private = it; +if (!it) +return NULL ; + + it->index = *pos; + it->max =sg_last_dev (); +if (it->index >= it->max) +return NULL ; +return it; +} + +static void * +dev_seq_next (struct seq_file *s,void *v,loff_t *pos) +{ +struct sg_proc_deviter *it = s->private; + + *pos = ++it->index; +return (it->index < it->max) ? it :NULL ; +} + +static void +dev_seq_stop (struct seq_file *s,void *v) +{ +kfree (s->private); +} + +#endif /* SG_PROC_OR_DEBUG_FS */ + +#if IS_ENABLED (CONFIG_SCSI_PROC_FS )/* around 100 lines */ +static int sg_proc_seq_show_int (struct seq_file *s,void *v) {
@@ -3837,7 +3874,7 @@ sg_proc_single_open_adio(struct inode *inode, struct file *filp)return single_open (filp, sg_proc_seq_show_int, &sg_allow_dio); } -static ssize_t +static ssize_t sg_proc_write_adio (struct file *filp,const char __user *buffer,size_t count,loff_t *off) {
@@ -3859,7 +3896,7 @@ sg_proc_single_open_dressz(struct inode *inode, struct file *filp)return single_open (filp, sg_proc_seq_show_int, &sg_big_buff); } -static ssize_t +static ssize_t sg_proc_write_dressz (struct file *filp,const char __user *buffer,size_t count,loff_t *off) {
@@ -3894,43 +3931,6 @@ sg_proc_seq_show_devhdr(struct seq_file *s, void *v)return 0 ; } -struct sg_proc_deviter { -loff_t index; -size_t max; -int fd_index; -}; - -static void * -dev_seq_start (struct seq_file *s,loff_t *pos) -{ -struct sg_proc_deviter *it =kzalloc (sizeof (*it),GFP_KERNEL ); - - s->private = it; -if (! it) -return NULL ; - - it->index = *pos; - it->max =sg_last_dev (); -if (it->index >= it->max) -return NULL ; -return it; -} - -static void * -dev_seq_next (struct seq_file *s,void *v,loff_t *pos) -{ -struct sg_proc_deviter *it = s->private; - - *pos = ++it->index; -return (it->index < it->max) ? it :NULL ; -} - -static void -dev_seq_stop (struct seq_file *s,void *v) -{ -kfree (s->private); -} -static int sg_proc_seq_show_dev (struct seq_file *s,void *v) {
@@ -3977,6 +3977,10 @@ sg_proc_seq_show_devstrs(struct seq_file *s, void *v) return 0; } +#endif /* CONFIG_SCSI_PROC_FS (~100 lines back) */ + +#if IS_ENABLED (SG_PROC_OR_DEBUG_FS ) +/* Writes debug info for one sg_request in obp buffer */ static int sg_proc_debug_sreq (struct sg_request *srp,int to,char *obp,int len)
@@ -4119,18 +4123,20 @@ sg_proc_seq_show_debug(struct seq_file *s, void *v)bool found = false;bool trunc = false;const int bp_len =SG_PROC_DEBUG_SZ ; +int j, sd_n;int n =0 ;int k =0 ;unsigned long iflags;struct sg_proc_deviter *it = (struct sg_proc_deviter *)v;struct sg_device *sdp;int *fdi_p; +const int *dev_arr = sg_dfs_cxt.snapshot_devs;char *bp;char *disk_name;char b1[128 ]; b1[0 ] ='\0' ; -if (it && (0 == it->index)) +if (it && it->index ==0 )seq_printf (s,"max_active_device=%d def_reserved_size=%d\n" , (int )it->max, def_reserved_size); fdi_p = it ? &it->fd_index : &k;
@@ -4142,8 +4148,31 @@ sg_proc_seq_show_debug(struct seq_file *s, void *v) }read_lock_irqsave (&sg_index_lock, iflags); sdp = it ?sg_lookup_dev (it->index) :NULL ; -if (NULL == sdp) +if (!sdp) goto skip; + sd_n = dev_arr[0 ]; +if (sd_n !=-1 && sd_n != sdp->index && sd_n != dev_arr[1 ]) { +for (j =1 ; j <SG_SNAPSHOT_DEV_MAX ; ) { + sd_n = dev_arr[j]; +if (sd_n <0 ) + goto skip; + ++j; +if (j >=SG_SNAPSHOT_DEV_MAX ) { +if (sd_n == sdp->index) { + found = true; +break ; + } + }else if (sd_n == dev_arr[j]) { + goto skip; + }else if (sd_n == sdp->index) { + found = true; +break ; + } + } +if (!found) + goto skip; + found = false; + }if (!xa_empty (&sdp->sfp_arr)) { found = true; disk_name = (sdp->disk ? sdp->disk->disk_name :"?_?" );
@@ -4183,6 +4212,10 @@ sg_proc_seq_show_debug(struct seq_file *s, void *v)return 0 ; } +#endif /* SG_PROC_OR_DEBUG_FS */ + +#if IS_ENABLED (CONFIG_SCSI_PROC_FS ) +static const struct proc_ops adio_proc_ops = { .proc_open = sg_proc_single_open_adio, .proc_read = seq_read,
@@ -4241,7 +4274,226 @@ sg_proc_init(void)/* remove_proc_subtree("scsi/sg", NULL) in exit_sg() does cleanup */ -#endif /* CONFIG_SCSI_PROC_FS (~400 lines back) */ +#else + +static int +sg_proc_init (void ) +{ +return 0 ; +} + +#endif /* CONFIG_SCSI_PROC_FS */ + +#if IS_ENABLED (CONFIG_DEBUG_FS ) + +struct sg_dfs_attr { +const char *name; +umode_t mode; +int (*show)(void *d,struct seq_file *m); +ssize_t (*write)(void *d,const char __user *b,size_t s,loff_t *o); +/* Set either .show or .seq_ops. */ +const struct seq_operations *seq_ops; +}; +static int +sg_dfs_snapshot_devs_show (void *data,struct seq_file *m) +{ +bool last; +int k, d; +struct sg_dfs_context_t *ctxp = data; + +for (k =0 ; k <SG_SNAPSHOT_DEV_MAX ; ++k) { + d = ctxp->snapshot_devs[k]; + last = (k +1 ==SG_SNAPSHOT_DEV_MAX ); +if (d <0 ) { +if (k ==0 ) +seq_puts (m,"-1" ); +break ; + } +if (!last && d == ctxp->snapshot_devs[k +1 ]) { +if (k ==0 ) +seq_puts (m,"-1" ); +break ; + } +if (k !=0 ) +seq_puts (m,"," ); +seq_printf (m,"%d" , d); + } +seq_puts (m,"\n" ); +return 0 ; +} + +static ssize_t +sg_dfs_snapshot_devs_write (void *data,const char __user *buf,size_t count, +loff_t *ppos) +{ +bool trailing_comma; +int k, n; +struct sg_dfs_context_t *cxtp = data; +char lbuf[64 ] = { }, *cp, *c2p; + +if (count >=sizeof (lbuf)) { +pr_err ("%s: operation too long\n" , __func__); +return -EINVAL ; + } +if (copy_from_user (lbuf, buf, count)) +return -EFAULT ; +for (k =0 , n =0 , cp = lbuf; k <SG_SNAPSHOT_DEV_MAX ; + ++k, cp = c2p +1 ) { + c2p =strchr (cp,',' ); +if (c2p) + *c2p ='\0' ; + trailing_comma = !!c2p; +/* sscanf is easier to use that this ... */ +if (kstrtoint (cp,10 , cxtp->snapshot_devs + k)) +break ; + ++n; +if (!trailing_comma) +break ; + } +if (n ==0 ) { +return -EINVAL ; + }else if (k >=SG_SNAPSHOT_DEV_MAX && trailing_comma) { +pr_err ("%s: only %d elements in snapshot array\n" , __func__, +SG_SNAPSHOT_DEV_MAX ); +return -EINVAL ; + } +if (n <SG_SNAPSHOT_DEV_MAX ) + cxtp->snapshot_devs[n] =-1 ; +return count; +} + +static int +sg_dfs_show (struct seq_file *m,void *v) +{ +const struct sg_dfs_attr *attr = m->private; +void *data =d_inode (m->file->f_path.dentry->d_parent)->i_private; + +return attr->show (data, m); +} + +static ssize_t +sg_dfs_write (struct file *file,const char __user *buf,size_t count, +loff_t *ppos) +{ +struct seq_file *m = file->private_data; +const struct sg_dfs_attr *attr = m->private; +void *data =d_inode (file->f_path.dentry->d_parent)->i_private; + +/* +* Attributes that only implement .seq_ops are read-only and 'attr' is +* the same with 'data' in this case. +*/ +if (attr == data || !attr->write) +return -EPERM ; +return attr->write (data, buf, count, ppos); +} + +static int +sg_dfs_open (struct inode *inode,struct file *file) +{ +const struct sg_dfs_attr *attr = inode->i_private; +void *data =d_inode (file->f_path.dentry->d_parent)->i_private; +struct seq_file *m; +int ret; + +if (attr->seq_ops) { + ret =seq_open (file, attr->seq_ops); +if (!ret) { + m = file->private_data; + m->private = data; + } +return ret; + } +if (WARN_ON_ONCE (!attr->show)) +return -EPERM ; +return single_open (file, sg_dfs_show, inode->i_private); +} + +static int +sg_dfs_release (struct inode *inode,struct file *file) +{ +const struct sg_dfs_attr *attr = inode->i_private; + +if (attr->show) +return single_release (inode, file); +return seq_release (inode, file); +} + +static const struct file_operations sg_dfs_fops = { + .owner =THIS_MODULE , + .open = sg_dfs_open, + .read = seq_read, + .write = sg_dfs_write, + .llseek = seq_lseek, + .release = sg_dfs_release, +}; + +static void sg_dfs_mk_files (struct dentry *parent,void *data, +const struct sg_dfs_attr *attr) +{ +if (IS_ERR_OR_NULL (parent)) +return ; + +d_inode (parent)->i_private = data; +for (; attr->name; ++attr) +debugfs_create_file (attr->name, attr->mode, parent, + (void *)attr, &sg_dfs_fops); +} + +static const struct seq_operations sg_snapshot_seq_ops = { + .start = dev_seq_start, + .next = dev_seq_next, + .stop = dev_seq_stop, + .show = sg_proc_seq_show_debug, +}; + +static const struct sg_dfs_attr sg_dfs_attrs[] = { + {"snapshot" ,0400 , .seq_ops = &sg_snapshot_seq_ops}, + {"snapshot_devs" ,0600 , sg_dfs_snapshot_devs_show, + sg_dfs_snapshot_devs_write}, + { }, +}; + +static void +sg_dfs_init (void ) +{ +/* create and populate /sys/kernel/debug/scsi_generic directory */ +if (!sg_dfs_cxt.dfs_rootdir) { + sg_dfs_cxt.dfs_rootdir =debugfs_create_dir ("scsi_generic" , +NULL ); +sg_dfs_mk_files (sg_dfs_cxt.dfs_rootdir, &sg_dfs_cxt, + sg_dfs_attrs); + } + sg_dfs_cxt.snapshot_devs[0 ] =-1 ;/* show all sg devices */ +} + +static void +sg_dfs_exit (void ) +{ +debugfs_remove_recursive (sg_dfs_cxt.dfs_rootdir); + sg_dfs_cxt.dfs_rootdir =NULL ; +} + +#else /* not defined: CONFIG_DEBUG_FS */ + +static void sg_dfs_init (void ) {} +static void sg_dfs_exit (void ) {} + +#endif /* CONFIG_DEBUG_FS */ + +module_param_named (scatter_elem_sz, scatter_elem_sz, int,0644 ); +module_param_named (def_reserved_size, def_reserved_size, int,0644 ); +module_param_named (allow_dio, sg_allow_dio, int,0644 ); + +MODULE_AUTHOR ("Douglas Gilbert" ); +MODULE_DESCRIPTION ("SCSI generic (sg) driver" ); +MODULE_LICENSE ("GPL" ); +MODULE_VERSION (SG_VERSION_STR ); +MODULE_ALIAS_CHARDEV_MAJOR (SCSI_GENERIC_MAJOR ); + +MODULE_PARM_DESC (scatter_elem_sz,"scatter gather element size (default: max(SG_SCATTER_SZ, PAGE_SIZE))" ); +MODULE_PARM_DESC (def_reserved_size,"size of buffer reserved for each fd" ); +MODULE_PARM_DESC (allow_dio,"allow direct I/O (default: 0 (disallow))" );module_init (init_sg);module_exit (exit_sg);
Signature · 1 line
2.25.1
Re: [PATCH v7 35/38] sg: first debugfs support
Quoted 15 lines · Douglas Gilbert
Duplicate the semantics of 'cat
/proc/scsi/sg/debug' on 'cat/sys/kernel/debug/scsi_generic/snapshot'. Make code that generates the snapshot conditional on eitherCONFIG_SCSI_PROC_FSorCONFIG_DEBUG_FSbeing defined.Also add
snapshot_devswhich can be written with a list of comma separated integers corresponding to sg (minor) device numbers. That file can also be read showing that list. Minus one (or any negative number) means accept all when in the first position (the default) or means the end of the list in a later position. When a subsequent
cat/sys/kernel/debug/scsi_generic/snapshot
is performed, only sg device numbers matching an element in that list are output.
Quoted 5 lines
Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 414 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 333 insertions(+), 81 deletions(-)
Ah, here it is. Very good.
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerPATCH v7 36/38 sg: rework mmap support
In reply to Douglas GilbertLinux has an issue with mmap-ed multiple pages issued by alloc_pages() [with order > 0]. So when mmap(2) is called if the reserve request's scatter gather (sgat) list is either:
- not big enough, or
- made up of elements of order > 0 (i.e. size > PAGE_SIZE)
then throw away reserve requests sgat list and rebuild it meeting those requirements.
Clean up related code and stop doing mmap+indirect_io.
This new mmap implementation is marginally more flexible (but still compatible with) the production driver. Previously if the user wanted a larger mmap(2) 'length' than the reserve request's size, then they needed to use ioctl(SG_SET_RESERVED_SIZE) to set the new size first. Now mmap(2) called on a sg device node will adjust to the size given by 'length' [mmap's second parameter].
Tweak some SG_LOG() levels to control the amount of debug output.
--- drivers/scsi/sg.c | 150 +++++++++++++++++++++++++++------------------- 1 file changed, 87 insertions(+), 63 deletions(-)
drivers/scsi/sg.c +87 -63
@@ -281,7 +281,8 @@ static void sg_dfs_exit(void);static int sg_start_req (struct sg_request *srp,struct sg_comm_wr_t *cwrp,int dxfer_dir);static void sg_finish_scsi_blk_rq (struct sg_request *srp); -static int sg_mk_sgat (struct sg_request *srp,struct sg_fd *sfp,int minlen); +static int sg_mk_sgat (struct sg_request *srp,struct sg_fd *sfp,int minlen, +bool for_mmap);static int sg_v3_submit (struct file *filp,struct sg_fd *sfp,struct sg_io_hdr *hp,bool sync,struct sg_request **o_srp);
@@ -2240,13 +2241,39 @@ sg_fasync(int fd, struct file *filp, int mode)return fasync_helper (fd, filp, mode, &sfp->async_qp); } +static void +sg_vma_open (struct vm_area_struct *vma) +{ +struct sg_fd *sfp = vma->vm_private_data; + +if (unlikely (!sfp)) { +pr_warn ("%s: sfp null\n" , __func__); +return ; + } +kref_get (&sfp->f_ref); +} + +static void +sg_vma_close (struct vm_area_struct *vma) +{ +struct sg_fd *sfp = vma->vm_private_data; + +if (unlikely (!sfp)) { +pr_warn ("%s: sfp null\n" , __func__); +return ; + } +kref_put (&sfp->f_ref, sg_remove_sfp);/* get in: sg_vma_open() */ +} +/* Note: the error return: VM_FAULT_SIGBUS causes a "bus error" */ static vm_fault_t sg_vma_fault (struct vm_fault *vmf) { -int k, length; -unsigned long offset, len, sa, iflags; +int k, n, length; +int res =VM_FAULT_SIGBUS ; +unsigned long offset;struct vm_area_struct *vma = vmf->vma; +struct page *page;struct sg_scatter_hold *rsv_schp;struct sg_request *srp;struct sg_device *sdp;
@@ -2272,7 +2299,7 @@ sg_vma_fault(struct vm_fault *vmf)SG_LOG (1 , sfp,"%s: srp%s\n" , __func__, nbp); goto out_err; } -xa_lock_irqsave (&sfp->srp_arr, iflags); +mutex_lock (&sfp->f_mutex); rsv_schp = &srp->sgat_h; offset = vmf->pgoff <<PAGE_SHIFT ;if (offset >= (unsigned int )rsv_schp->buflen) {
@@ -2280,44 +2307,37 @@ sg_vma_fault(struct vm_fault *vmf) offset); goto out_err_unlock; } - sa = vma->vm_start; -SG_LOG (3 , sfp,"%s: vm_start=0x%lx, off=%lu\n" , __func__, sa, offset); +SG_LOG (5 , sfp,"%s: vm_start=0x%lx, off=%lu\n" , __func__, + vma->vm_start, offset); length =1 << (PAGE_SHIFT + rsv_schp->page_order); -for (k =0 ; k < rsv_schp->num_sgat && sa < vma->vm_end; ++k) { - len = vma->vm_end - sa; - len =min_t (int, len, (int )length); -if (offset < len) { -struct page *page; -struct page *pp; - - pp = rsv_schp->pages[k]; -xa_unlock_irqrestore (&sfp->srp_arr, iflags); - page =nth_page (pp, offset >>PAGE_SHIFT ); -get_page (page);/* increment page count */ - vmf->page = page; -return 0 ;/* success */ - } - sa += len; - offset -= len; - } + k = (int )offset / length; + n = ((int )offset % length) >>PAGE_SHIFT ; + page =nth_page (rsv_schp->pages[k], n); +get_page (page); + vmf->page = page; + res =0 ; out_err_unlock: -xa_unlock_irqrestore (&sfp->srp_arr, iflags); +mutex_unlock (&sfp->f_mutex); out_err: -return VM_FAULT_SIGBUS ; +return res; }static const struct vm_operations_struct sg_mmap_vm_ops = { .fault = sg_vma_fault, + .open = sg_vma_open, + .close = sg_vma_close, }; -/* Entry point for mmap(2) system call */ +/* +* Entry point for mmap(2) system call. For mmap(2) to work, request's +* scatter gather list needs to be order 0 which it is unlikely to be +* by default. mmap(2) cannot be called more than once per fd. +*/ static int sg_mmap (struct file *filp,struct vm_area_struct *vma) { -int k, length; -int ret =0 ; -unsigned long req_sz, len, sa, iflags; -struct sg_scatter_hold *rsv_schp; +int res =0 ; +unsigned long req_sz;struct sg_fd *sfp;struct sg_request *srp;
@@ -2328,40 +2348,44 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma)pr_warn ("sg: %s: sfp is NULL\n" , __func__);return -ENXIO ; } +mutex_lock (&sfp->f_mutex); req_sz = vma->vm_end - vma->vm_start;SG_LOG (3 , sfp,"%s: vm_start=%pK, len=%d\n" , __func__, (void *)vma->vm_start, (int )req_sz); -if (vma->vm_pgoff) -return -EINVAL ;/* only an offset of 0 accepted */ +if (vma->vm_pgoff) { + res = -EINVAL ;/* only an offset of 0 accepted */ + goto fini; + }/* Check reserve request is inactive and has large enough buffer */ -mutex_lock (&sfp->f_mutex); srp = sfp->rsv_srp; -xa_lock_irqsave (&sfp->srp_arr, iflags);if (SG_RS_ACTIVE (srp)) { - ret = -EBUSY ; - goto out; + res = -EBUSY ; + goto fini; } - rsv_schp = &srp->sgat_h; -if (req_sz > (unsigned long )rsv_schp->buflen) { - ret = -ENOMEM ; - goto out; +if (req_sz >SG_WRITE_COUNT_LIMIT ) {/* sanity check */ + res = -ENOMEM ; + goto fini; } - sa = vma->vm_start; - length =1 << (PAGE_SHIFT + rsv_schp->page_order); -for (k =0 ; k < rsv_schp->num_sgat && sa < vma->vm_end; ++k) { - len = vma->vm_end - sa; - len = min_t(unsigned long, len, (unsigned long)length); - sa += len; +if (test_and_set_bit (SG_FFD_MMAP_CALLED , sfp->ffd_bm)) { +SG_LOG (1 , sfp,"%s: multiple invocations on this fd\n" , + __func__); + res = -EADDRINUSE ; + goto fini; + } +if (srp->sgat_h.page_order >0 || + req_sz > (unsigned long )srp->sgat_h.buflen) { +sg_remove_sgat (srp); + res =sg_mk_sgat (srp, sfp, req_sz, true); +if (res) + goto fini; } - -set_bit (SG_FFD_MMAP_CALLED , sfp->ffd_bm); vma->vm_flags |=VM_IO |VM_DONTEXPAND |VM_DONTDUMP ; vma->vm_private_data = sfp; vma->vm_ops = &sg_mmap_vm_ops; -out: -xa_unlock_irqrestore (&sfp->srp_arr, iflags); +sg_vma_open (vma); +fini:mutex_unlock (&sfp->f_mutex); -return ret; +return res; }/*
@@ -2955,7 +2979,7 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) } scsi_rp->cmd_len = cwrp->cmd_len; srp->cmd_opcode = scsi_rp->cmd[0 ]; - us_xfer = !(srp->rq_flags &SG_FLAG_NO_DXFER ); + us_xfer = !(srp->rq_flags & (SG_FLAG_NO_DXFER |SG_FLAG_MMAP_IO ));assign_bit (SG_FRQ_NO_US_XFER , srp->frq_bm, !us_xfer); reserved = (sfp->rsv_srp == srp); rq->end_io_data = srp;
@@ -2985,7 +3009,7 @@ sg_start_req(struct sg_request *srp, struct sg_comm_wr_t *cwrp, int dxfer_dir) }else if (req_schp->buflen ==0 ) {int up_sz =max_t (int, dxfer_len, sfp->sgat_elem_sz); - res =sg_mk_sgat (srp, sfp, up_sz); + res =sg_mk_sgat (srp, sfp, up_sz, false); }if (unlikely (res)) goto fini;
@@ -3093,14 +3117,14 @@ sg_finish_scsi_blk_rq(struct sg_request *srp) }static int -sg_mk_sgat (struct sg_request *srp,struct sg_fd *sfp,int minlen) +sg_mk_sgat (struct sg_request *srp,struct sg_fd *sfp,int minlen, +bool for_mmap) { -int j, k, rem_sz, order, align_sz; +int j, k, rem_sz, order, align_sz, elem_sz;__maybe_unused int o_order;int m_size = minlen;int rup_sz =0 ;int mx_sgat_elems = sfp->parentdp->max_sgat_elems; -u32 elem_sz;const size_t struct_page_sz =sizeof (struct page *);gfp_t mask_ap =GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN | __GFP_ZERO;gfp_t mask_kz =GFP_ATOMIC | __GFP_NOWARN;
@@ -3113,14 +3137,14 @@ sg_mk_sgat(struct sg_request *srp, struct sg_fd *sfp, int minlen) ++m_size;/* don't remember why */ /* round request up to next highest SG_DEF_SECTOR_SZ byte boundary */ align_sz =ALIGN (m_size,SG_DEF_SECTOR_SZ ); - schp->pages =kcalloc (mx_sgat_elems, struct_page_sz, mask_kz);SG_LOG (4 , sfp,"%s: minlen=%d [sz=%zu, 0x%pK ++]\n" , __func__, minlen, mx_sgat_elems * struct_page_sz, schp->pages);if (unlikely (!schp->pages))return -ENOMEM ; - elem_sz = sfp->sgat_elem_sz;/* power of 2 and >= PAGE_SIZE */ +/* elem_sz must be power of 2 and >= PAGE_SIZE */ + elem_sz = for_mmap ?PAGE_SIZE : sfp->sgat_elem_sz;if (sdp &&unlikely (sdp->device->host->unchecked_isa_dma)) mask_ap |=GFP_DMA ;/* PAGE_SIZE == (1 << PAGE_SHIFT) == (2 ** PAGE_SHIFT) */
@@ -3134,7 +3158,7 @@ sg_mk_sgat(struct sg_request *srp, struct sg_fd *sfp, int minlen) schp->pages[k] =alloc_pages (mask_ap, order);if (!schp->pages[k]) goto err_out; -SG_LOG (5 , sfp,"%s: k=%d, rup_sz=%d [0x%pK ++]\n" , __func__, k, +SG_LOG (6 , sfp,"%s: k=%d, rup_sz=%d [0x%pK ++]\n" , __func__, k, rup_sz, schp->pages[k]); } schp->page_order = order;
@@ -3168,7 +3192,7 @@ sg_remove_sgat_helper(struct sg_fd *sfp, struct sg_scatter_hold *schp)return ;for (k =0 ; k < schp->num_sgat; ++k) { p = schp->pages[k]; -SG_LOG (5 , sfp,"%s: pg[%d]=0x%pK --\n" , __func__, k, p); +SG_LOG (6 , sfp,"%s: pg[%d]=0x%pK --\n" , __func__, k, p);if (unlikely (!p))continue ;__free_pages (p, schp->page_order);
@@ -3299,7 +3323,7 @@ sg_find_srp_by_id(struct sg_fd *sfp, int pack_id) }return NULL ; good: -SG_LOG (6 , sfp,"%s: %s%d found [srp=0x%pK]\n" , __func__,"pack_id=" , +SG_LOG (5 , sfp,"%s: %s%d found [srp=0x%pK]\n" , __func__,"pack_id=" , pack_id, srp);return srp; }
@@ -3338,7 +3362,7 @@ sg_mk_srp_sgat(struct sg_fd *sfp, bool first, int db_len)if (IS_ERR (n_srp))return n_srp;if (db_len >0 ) { - res =sg_mk_sgat (n_srp, sfp, db_len); + res =sg_mk_sgat (n_srp, sfp, db_len, false);if (unlikely (res)) {kfree (n_srp);return ERR_PTR (res);
@@ -3371,7 +3395,7 @@ sg_build_reserve(struct sg_fd *sfp, int buflen) buflen =PAGE_SIZE ; go_out = true; } - res =sg_mk_sgat (srp, sfp, buflen); + res =sg_mk_sgat (srp, sfp, buflen, false);if (res ==0 ) {SG_LOG (4 , sfp,"%s: final buflen=%d, srp=0x%pK ++\n" , __func__, buflen, srp);
Signature · 1 line
2.25.1
Re: [PATCH v7 36/38] sg: rework mmap support
Quoted 16 lines · Douglas Gilbert
Linux has an issue with mmap-ed multiple pages issued by
alloc_pages()[with order > 0]. So when mmap(2) is called if the reserve request's scatter gather (sgat) list is either:
- not big enough, or
- made up of elements of order > 0 (i.e. size >PAGE_SIZE)
then throw away reserve requests sgat list and rebuild it meeting those requirements.
Clean up related code and stop doing mmap+indirect_io.This new mmap implementation is marginally more flexible (but still compatible with) the production driver. Previously if the user wanted a larger mmap(2) 'length' than the reserve request's size, then they needed to use
ioctl(SG_SET_RESERVED_SIZE)to set the new size first. Now mmap(2) called on a sg device node will adjust to the size given by 'length' [mmap's second parameter].
Quoted 8 lines
Tweak some
SG_LOG()levels to control the amount of debug output.Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 150 +++++++++++++++++++++++++++------------------- 1 file changed, 87 insertions(+), 63 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerPATCH v7 37/38 sg: warn v3 write system call users
In reply to Douglas GilbertShould generate one log message per kernel run when the write() system call is used with the sg interface version 3. Due to security concerns suggest that they use ioctl(SG_SUBMIT_v3) instead.
Sg interface version 1 or 2 based code may also be calling write() in this context. There is no easy solution for them (short of upgrading their interface to version 3 or 4), so don't produce a warning suggesting the conversion will be simple.
--- drivers/scsi/sg.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/scsi/sg.c +4 -0
@@ -646,6 +646,10 @@ sg_write(struct file *filp, const char __user *p, size_t count, loff_t *ppos) __func__);return -EPERM ; } +WARN_ONCE (true,"Please use %s instead of write(),\n%s\n" , +"ioctl(SG_SUBMIT_V3)" , +" See: http://sg.danny.cz/sg/sg_v40.html" ); + res =sg_v3_submit (filp, sfp, h3p, false,NULL );return res <0 ? res : (int )count; }
Signature · 1 line
2.25.1
Re: [PATCH v7 37/38] sg: warn v3 write system call users
Quoted 11 lines · Douglas Gilbert
Should generate one log message per kernel run when the
write()system call is used with the sg interface version 3. Due to security concerns suggest that they useioctl(SG_SUBMIT_v3)instead.Sg interface version 1 or 2 based code may also be calling
write()in this context. There is no easy solution for them (short of upgrading their interface to version 3 or 4), so don't produce a warning suggesting the conversion will be simple.
Quoted 5 lines
Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 4 ++++ 1 file changed, 4 insertions(+)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Douglas Gilbert
maintainerPATCH v7 38/38 sg: bump version to 4.0.08
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 | 8 ++++---- include/uapi/scsi/sg.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-)
drivers/scsi/sg.c +4 -4
@@ -8,13 +8,13 @@* Original driver (sg.c): * Copyright (C) 1992 Lawrence Foard * Version 2, 3 and 4 extensions to driver: -* Copyright (C) 1998 - 2019 Douglas Gilbert +* Copyright (C) 1998 - 2020 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 =40008 ;/* [x]xyyzz where [x] empty when x=0 */ +#define SG_VERSION_STR "4.0.08"/* [x]x.[y]y.zz */ +static char *sg_version_date ="20200227" ;#include <linux/module.h>
include/uapi/scsi/sg.h +2 -2
@@ -12,9 +12,9 @@* Copyright (C) 1992 Lawrence Foard * * Later extensions (versions 2, 3 and 4) to driver: -* Copyright (C) 1998 - 2018 Douglas Gilbert +* Copyright (C) 1998 - 2020 Douglas Gilbert * -* Version 4.0.11 (20190502) +* Version 4.0.11 (20200124) * This version is for Linux 4 and 5 series kernels. * * Documentation
Signature · 1 line
2.25.1
Re: [PATCH v7 38/38] sg: bump version to 4.0.08
Quoted 17 lines · Douglas Gilbert
Now that the sg version 4 interface is supported:
- withioctl(SG_IO)for synchronous/blocking use
- withioctl(SG_IOSUBMIT)andioctl(SG_IORECEIVE)for async/non-blocking use
Plus newioctl(SG_IOSUBMIT_V3)andioctl(SG_IORECEIVE_V3)potentially replacewrite()andread()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_fdobject (i.e. a file descriptor). Thesg_requestobjects 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.
Quoted 6 lines
Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/sg.c | 8 ++++---- include/uapi/scsi/sg.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-)Reviewed-by Hannes Reinecke <[email protected]>
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer