bsg cleanup, part 2
Subsystems (3)
Hi Martin,
this is the next round of bsg cleanups based on the previous scsi ioctl changes. The biggest changes are major simplification of how the bsg nodes are created and found, and a simplification of the interface between the frontend in bsg.c and the two backends.
Diffstat:
block/blk-mq.c | 2 block/bsg-lib.c | 89 +++++------ block/bsg.c | 353 ++++++++++----------------------------------- drivers/scsi/scsi_bsg.c | 72 +++++---- drivers/scsi/scsi_ioctl.c | 63 +++----- drivers/scsi/scsi_priv.h | 11 - drivers/scsi/scsi_scan.c | 2 drivers/scsi/scsi_sysfs.c | 24 ++- include/linux/blkdev.h | 14 - include/linux/bsg-lib.h | 1 include/linux/bsg.h | 31 +-- include/scsi/scsi_device.h | 5 12 files changed, 229 insertions(+), 438 deletions(-)
PATCH 1/4 bsg: simplify device registration
Use the per-device cdev_device_interface to store the bsg data in the char device inode, and thus remove the need to embedd the bsg_class_device structure in the request_queue.
--- block/bsg-lib.c | 11 +- block/bsg.c | 304 +++++++++---------------------------- drivers/scsi/scsi_bsg.c | 5 +- drivers/scsi/scsi_priv.h | 11 +- drivers/scsi/scsi_sysfs.c | 24 ++- include/linux/blkdev.h | 6 - include/linux/bsg-lib.h | 1 + include/linux/bsg.h | 21 +-- include/scsi/scsi_device.h | 2 + 9 files changed, 108 insertions(+), 277 deletions(-)
block/bsg-lib.c +6 -5
@@ -6,6 +6,7 @@* Copyright (C) 2011 Red Hat, Inc. All rights reserved. * Copyright (C) 2011 Mike Christie */ +#include <linux/bsg.h> #include <linux/slab.h> #include <linux/blk-mq.h> #include <linux/delay.h>
@@ -19,6 +20,7 @@struct bsg_set {struct blk_mq_tag_set tag_set; +struct bsg_device *bd;bsg_job_fn *job_fn;bsg_timeout_fn *timeout_fn; };
@@ -327,7 +329,7 @@ void bsg_remove_queue(struct request_queue *q)struct bsg_set *bset = container_of(q->tag_set, struct bsg_set, tag_set); -bsg_unregister_queue (q); +bsg_unregister_queue (bset->bd);blk_cleanup_queue (q);blk_mq_free_tag_set (&bset->tag_set);kfree (bset);
@@ -396,10 +398,9 @@ struct request_queue *bsg_setup_queue(struct device *dev, const char *name, q->queuedata = dev;blk_queue_rq_timeout (q,BLK_DEFAULT_SG_TIMEOUT ); - ret =bsg_register_queue (q, dev, name, &bsg_transport_ops); -if (ret) { -printk (KERN_ERR "%s: bsg interface failed to " -"initialize - register queue\n" , dev->kobj.name); + bset->bd =bsg_register_queue (q, dev, name, &bsg_transport_ops); +if (IS_ERR (bset->bd)) { + ret =PTR_ERR (bset->bd); goto out_cleanup_queue; }
block/bsg.c +72 -232
439 lines not on this page · raw message
drivers/scsi/scsi_bsg.c +3 -2
@@ -89,7 +89,8 @@ static const struct bsg_ops scsi_bsg_ops = { .free_rq = scsi_bsg_free_rq, }; -int scsi_bsg_register_queue (struct request_queue *q,struct device *parent) +struct bsg_device *scsi_bsg_register_queue (struct scsi_device *sdev) { -return bsg_register_queue (q, parent,dev_name (parent), &scsi_bsg_ops); +return bsg_register_queue (sdev->request_queue, &sdev->sdev_gendev, +dev_name (&sdev->sdev_gendev), &scsi_bsg_ops); }
drivers/scsi/scsi_priv.h +2 -9
@@ -7,6 +7,7 @@#include <scsi/scsi_device.h> #include <linux/sbitmap.h> +struct bsg_device ;struct request_queue ;struct request ;struct scsi_cmnd ;
@@ -180,15 +181,7 @@ static inline void scsi_dh_add_device(struct scsi_device *sdev) { }static inline void scsi_dh_release_device (struct scsi_device *sdev) { }#endif -#ifdef CONFIG_BLK_DEV_BSG -int scsi_bsg_register_queue (struct request_queue *q,struct device *parent); -#else -static inline int scsi_bsg_register_queue (struct request_queue *q, -struct device *parent) -{ -return 0 ; -} -#endif +struct bsg_device *scsi_bsg_register_queue (struct scsi_device *sdev);extern int scsi_device_max_queue_depth (struct scsi_device *sdev);
drivers/scsi/scsi_sysfs.c +16 -8
@@ -13,6 +13,7 @@#include <linux/blkdev.h> #include <linux/device.h> #include <linux/pm_runtime.h> +#include <linux/bsg.h> #include <scsi/scsi.h> #include <scsi/scsi_device.h>
@@ -1327,7 +1328,6 @@ static int scsi_target_add(struct scsi_target *starget)int scsi_sysfs_add_sdev (struct scsi_device *sdev) {int error, i; -struct request_queue *rq = sdev->request_queue;struct scsi_target *starget = sdev->sdev_target; error =scsi_target_add (starget);
@@ -1366,12 +1366,19 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev)transport_add_device (&sdev->sdev_gendev); sdev->is_visible =1 ; - error =scsi_bsg_register_queue (rq, &sdev->sdev_gendev); -if (error) -/* we're treating error on bsg register as non-fatal, -* so pretend nothing went wrong */ -sdev_printk (KERN_INFO , sdev, -"Failed to register bsg queue, errno=%d\n" , error); +if (IS_ENABLED (CONFIG_BLK_DEV_BSG )) { + sdev->bsg_dev =scsi_bsg_register_queue (sdev); +if (IS_ERR (sdev->bsg_dev)) { +/* +* We're treating error on bsg register as non-fatal, so +* pretend nothing went wrong. +*/ +sdev_printk (KERN_INFO , sdev, +"Failed to register bsg queue, errno=%d\n" , + error); + sdev->bsg_dev =NULL ; + } + }/* add additional host specific attributes */ if (sdev->host->hostt->sdev_attrs) {
@@ -1433,7 +1440,8 @@ void __scsi_remove_device(struct scsi_device *sdev)sysfs_remove_groups (&sdev->sdev_gendev.kobj, sdev->host->hostt->sdev_groups); -bsg_unregister_queue (sdev->request_queue); +if (IS_ENABLED (CONFIG_BLK_DEV_BSG ) && sdev->bsg_dev) +bsg_unregister_queue (sdev->bsg_dev);device_unregister (&sdev->sdev_dev);transport_remove_device (dev);device_del (dev);
include/linux/blkdev.h +0 -6
@@ -18,7 +18,6 @@#include <linux/bio.h> #include <linux/stringify.h> #include <linux/gfp.h> -#include <linux/bsg.h> #include <linux/smp.h> #include <linux/rcupdate.h> #include <linux/percpu-refcount.h>
@@ -33,7 +32,6 @@ struct elevator_queue;struct blk_trace ;struct request ;struct sg_io_hdr ; -struct bsg_job ;struct blkcg_gq ;struct blk_flush_queue ;struct pr_ops ;
@@ -535,10 +533,6 @@ struct request_queue {int mq_freeze_depth; -#if IS_ENABLED (CONFIG_BLK_DEV_BSG_COMMON ) -struct bsg_class_device bsg_dev; -#endif -#ifdef CONFIG_BLK_DEV_THROTTLING /* Throttle data */ struct throtl_data *td;
include/linux/bsg-lib.h +1 -0
@@ -12,6 +12,7 @@#include <linux/blkdev.h> #include <scsi/scsi_request.h> +struct bsg_job ;struct request ;struct device ;struct scatterlist ;
include/linux/bsg.h +6 -15
@@ -4,10 +4,11 @@#include <uapi/linux/bsg.h> +struct bsg_device ; +struct device ;struct request ;struct request_queue ; -#ifdef CONFIG_BLK_DEV_BSG_COMMONstruct bsg_ops {int (*check_proto)(struct sg_io_v4 *hdr); int (*fill_hdr)(struct request *rq, struct sg_io_v4 *hdr,
@@ -16,19 +17,9 @@ struct bsg_ops { void (*free_rq)(struct request *rq); }; -struct bsg_class_device { -struct device *class_dev; -int minor; -struct request_queue *queue; -const struct bsg_ops *ops; -}; +struct bsg_device *bsg_register_queue (struct request_queue *q, +struct device *parent,const char *name, +const struct bsg_ops *ops); +void bsg_unregister_queue (struct bsg_device *bcd); -int bsg_register_queue (struct request_queue *q,struct device *parent, -const char *name,const struct bsg_ops *ops); -void bsg_unregister_queue (struct request_queue *q); -#else -static inline void bsg_unregister_queue (struct request_queue *q) -{ -} -#endif /* CONFIG_BLK_DEV_BSG_COMMON */ #endif /* _LINUX_BSG_H */
include/scsi/scsi_device.h +2 -0
@@ -10,6 +10,7 @@#include <linux/atomic.h> #include <linux/sbitmap.h> +struct bsg_device ;struct device ;struct request_queue ;struct scsi_cmnd ;
@@ -235,6 +236,7 @@ struct scsi_device {size_t dma_drain_len;void *dma_drain_buf; +struct bsg_device *bsg_dev;unsigned char access_state;struct mutex state_mutex;enum scsi_device_state sdev_state;
Signature · 1 line
2.30.2
PATCH 2/4 block: remove BLK_SCSI_MAX_CMDS
In reply to Christoph HellwigThis was used for the table based scsi passthough permission checking that is gone now.
--- include/linux/blkdev.h | 3 --- 1 file changed, 3 deletions(-)
include/linux/blkdev.h +0 -3
@@ -271,9 +271,6 @@ enum blk_queue_state {#define BLK_TAG_ALLOC_FIFO 0/* allocate starting from 0 */ #define BLK_TAG_ALLOC_RR 1/* allocate starting from last allocated tag */ -#define BLK_SCSI_MAX_CMDS (256) -#define BLK_SCSI_CMD_PER_LONG (BLK_SCSI_MAX_CMDS / (sizeof(long) * 8)) -/* * Zoned block device models (zoned limit). *
Signature · 1 line
2.30.2
PATCH 3/4 block: remove the remaining SG_IO-related fields from struct request_queue
In reply to Christoph HellwigMove the sg_timeout and sg_reserved_size fields into the bsg_device and scsi_device structures as they have nothing to do with generic block I/O. Note that these values are now separate for bsg vs scsi device node access, but that just matches how /dev/sg vs the other nodes has always behaved.
--- block/blk-mq.c | 2 -- block/bsg.c | 13 +++++--- drivers/scsi/scsi_ioctl.c | 63 ++++++++++++++++++-------------------- drivers/scsi/scsi_scan.c | 2 ++ include/linux/blkdev.h | 5 --- include/scsi/scsi_device.h | 3 ++ 6 files changed, 43 insertions(+), 45 deletions(-)
block/blk-mq.c +0 -2
@@ -3298,8 +3298,6 @@ int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, set->map[HCTX_TYPE_POLL].nr_queues)blk_queue_flag_set (QUEUE_FLAG_POLL , q); - q->sg_reserved_size =INT_MAX ; -INIT_DELAYED_WORK (&q->requeue_work, blk_mq_requeue_work);INIT_LIST_HEAD (&q->requeue_list);spin_lock_init (&q->requeue_lock);
block/bsg.c +8 -5
@@ -26,6 +26,8 @@ struct bsg_device {struct device device;struct cdev cdev;int max_queue; +unsigned int timeout; +unsigned int reserved_size; };static inline struct bsg_device *to_bsg_device (struct inode *inode)
@@ -71,7 +73,7 @@ static int bsg_sg_io(struct bsg_device *bd, fmode_t mode, void __user *uarg) rq->timeout =msecs_to_jiffies (hdr.timeout);if (!rq->timeout) - rq->timeout = rq->q->sg_timeout; + rq->timeout = bd->timeout;if (!rq->timeout) rq->timeout =BLK_DEFAULT_SG_TIMEOUT ;if (rq->timeout <BLK_MIN_SG_TIMEOUT )
@@ -161,19 +163,19 @@ static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)case SG_SET_TIMEOUT :if (get_user (val, intp))return -EFAULT ; - q->sg_timeout =clock_t_to_jiffies (val); + bd->timeout =clock_t_to_jiffies (val);return 0 ;case SG_GET_TIMEOUT : -return jiffies_to_clock_t (q->sg_timeout); +return jiffies_to_clock_t (bd->timeout);case SG_GET_RESERVED_SIZE : -return put_user (min (q->sg_reserved_size,queue_max_bytes (q)), +return put_user (min (bd->reserved_size,queue_max_bytes (q)), intp);case SG_SET_RESERVED_SIZE :if (get_user (val, intp))return -EFAULT ;if (val <0 )return -EINVAL ; - q->sg_reserved_size = + bd->reserved_size = min_t(unsigned int, val, queue_max_bytes(q));return 0 ;case SG_EMULATED_HOST :
@@ -219,6 +221,7 @@ struct bsg_device *bsg_register_queue(struct request_queue *q,if (!bd)return ERR_PTR (-ENOMEM ); bd->max_queue =BSG_DEFAULT_CMDS ; + bd->reserved_size =INT_MAX ; bd->queue = q; bd->ops = ops;
drivers/scsi/scsi_ioctl.c +30 -33
177 lines not on this page · raw message
drivers/scsi/scsi_scan.c +2 -0
@@ -267,6 +267,8 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,*/ sdev->borken =1 ; + sdev->sg_reserved_size =INT_MAX ; + q =blk_mq_init_queue (&sdev->host->tag_set);if (IS_ERR (q)) {/* release fn is set up in scsi_sysfs_device_initialise, so
include/linux/blkdev.h +0 -5
@@ -499,11 +499,6 @@ struct request_queue {unsigned int max_active_zones;#endif /* CONFIG_BLK_DEV_ZONED */ -/* -* sg stuff -*/ -unsigned int sg_timeout; -unsigned int sg_reserved_size;int node;struct mutex debugfs_mutex;#ifdef CONFIG_BLK_DEV_IO_TRACE
include/scsi/scsi_device.h +3 -0
@@ -236,6 +236,9 @@ struct scsi_device {size_t dma_drain_len;void *dma_drain_buf; +unsigned int sg_timeout; +unsigned int sg_reserved_size; +struct bsg_device *bsg_dev;unsigned char access_state;struct mutex state_mutex;
Signature · 1 line
2.30.2
PATCH 4/4 bsg: move the whole request execution into the scsi/transport handlers
In reply to Christoph HellwigRemove the amount of indirect calls by making the handler responsible for the entire execution of the request.
--- block/bsg-lib.c | 80 ++++++++++++++++++++--------------------- block/bsg.c | 66 ++++++++-------------------------- drivers/scsi/scsi_bsg.c | 69 +++++++++++++++++++---------------- include/linux/bsg.h | 12 ++----- 4 files changed, 96 insertions(+), 131 deletions(-)
block/bsg-lib.c +39 -41
@@ -25,32 +25,39 @@ struct bsg_set {bsg_timeout_fn *timeout_fn; }; -static int bsg_transport_check_proto (struct sg_io_v4 *hdr) +static int bsg_transport_sg_io_fn (struct request_queue *q,struct sg_io_v4 *hdr, +fmode_t mode,unsigned int timeout) { +struct bsg_job *job; +struct request *rq; +struct bio *bio; +int ret; +if (hdr->protocol !=BSG_PROTOCOL_SCSI || hdr->subprotocol !=BSG_SUB_PROTOCOL_SCSI_TRANSPORT )return -EINVAL ;if (!capable (CAP_SYS_RAWIO ))return -EPERM ; -return 0 ; -} -static int bsg_transport_fill_hdr (struct request *rq,struct sg_io_v4 *hdr, -fmode_t mode) -{ -struct bsg_job *job =blk_mq_rq_to_pdu (rq); -int ret; + rq =blk_get_request (q, hdr->dout_xfer_len ? +REQ_OP_DRV_OUT :REQ_OP_DRV_IN ,0 ); +if (IS_ERR (rq)) +return PTR_ERR (rq); + rq->timeout = timeout; + job =blk_mq_rq_to_pdu (rq); job->request_len = hdr->request_len; job->request =memdup_user (uptr64 (hdr->request), hdr->request_len); -if (IS_ERR (job->request)) -return PTR_ERR (job->request); +if (IS_ERR (job->request)) { + ret =PTR_ERR (job->request); + goto out_put_request; + }if (hdr->dout_xfer_len && hdr->din_xfer_len) { job->bidi_rq =blk_get_request (rq->q,REQ_OP_DRV_IN ,0 );if (IS_ERR (job->bidi_rq)) { ret =PTR_ERR (job->bidi_rq); - goto out; + goto out_free_job_request; } ret = blk_rq_map_user(rq->q, job->bidi_rq, NULL,
@@ -65,20 +72,19 @@ static int bsg_transport_fill_hdr(struct request *rq, struct sg_io_v4 *hdr, job->bidi_bio =NULL ; } -return 0 ; +if (hdr->dout_xfer_len) { + ret =blk_rq_map_user (rq->q, rq,NULL ,uptr64 (hdr->dout_xferp), + hdr->dout_xfer_len,GFP_KERNEL ); + }else if (hdr->din_xfer_len) { + ret =blk_rq_map_user (rq->q, rq,NULL ,uptr64 (hdr->din_xferp), + hdr->din_xfer_len,GFP_KERNEL ); + } -out_free_bidi_rq: -if (job->bidi_rq) -blk_put_request (job->bidi_rq); -out: -kfree (job->request); -return ret; -} +if (ret) + goto out_unmap_bidi_rq; -static int bsg_transport_complete_rq (struct request *rq,struct sg_io_v4 *hdr) -{ -struct bsg_job *job =blk_mq_rq_to_pdu (rq); -int ret =0 ; + bio = rq->bio; +blk_execute_rq (NULL , rq, !(hdr->flags &BSG_FLAG_Q_AT_TAIL ));/* * The assignments below don't make much sense, but are kept for
@@ -121,28 +127,20 @@ static int bsg_transport_complete_rq(struct request *rq, struct sg_io_v4 *hdr) hdr->din_resid =0 ; } -return ret; -} - -static void bsg_transport_free_rq (struct request *rq) -{ -struct bsg_job *job =blk_mq_rq_to_pdu (rq); - -if (job->bidi_rq) { +blk_rq_unmap_user (bio); +out_unmap_bidi_rq: +if (job->bidi_rq)blk_rq_unmap_user (job->bidi_bio); +out_free_bidi_rq: +if (job->bidi_rq)blk_put_request (job->bidi_rq); - } - +out_free_job_request:kfree (job->request); +out_put_request: +blk_put_request (rq); +return ret; } -static const struct bsg_ops bsg_transport_ops = { - .check_proto = bsg_transport_check_proto, - .fill_hdr = bsg_transport_fill_hdr, - .complete_rq = bsg_transport_complete_rq, - .free_rq = bsg_transport_free_rq, -}; -/** * bsg_teardown_job - routine to teardown a bsg job * @kref: kref inside bsg_job that is to be torn down
@@ -398,7 +396,7 @@ struct request_queue *bsg_setup_queue(struct device *dev, const char *name, q->queuedata = dev;blk_queue_rq_timeout (q,BLK_DEFAULT_SG_TIMEOUT ); - bset->bd =bsg_register_queue (q, dev, name, &bsg_transport_ops); + bset->bd =bsg_register_queue (q, dev, name, bsg_transport_sg_io_fn);if (IS_ERR (bset->bd)) { ret =PTR_ERR (bset->bd); goto out_cleanup_queue;
block/bsg.c +15 -51
105 lines not on this page · raw message
drivers/scsi/scsi_bsg.c +39 -30
104 lines not on this page · raw message
include/linux/bsg.h +3 -9
@@ -6,20 +6,14 @@struct bsg_device ;struct device ; -struct request ;struct request_queue ; -struct bsg_ops { -int (*check_proto)(struct sg_io_v4 *hdr); -int (*fill_hdr)(struct request *rq,struct sg_io_v4 *hdr, -fmode_t mode); -int (*complete_rq)(struct request *rq,struct sg_io_v4 *hdr); -void (*free_rq)(struct request *rq); -}; +typedef int (bsg_sg_io_fn )(struct request_queue *,struct sg_io_v4 *hdr, +fmode_t mode,unsigned int timeout);struct bsg_device *bsg_register_queue (struct request_queue *q,struct device *parent,const char *name, -const struct bsg_ops *ops); +bsg_sg_io_fn *sg_io_fn);void bsg_unregister_queue (struct bsg_device *bcd);#endif /* _LINUX_BSG_H */
Signature · 1 line
2.30.2
Martin K. Petersen
AppliedChristoph,
Quoted 4 lines
this is the next round of bsg cleanups based on the previous scsi ioctl changes. The biggest changes are major simplification of how the bsg nodes are created and found, and a simplification of the interface between the frontend in
bsg.cand the two backends.
Applied to 5.15/scsi-staging, thanks!
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering