nvmet_fc: Reduce work_q count
Subsystems (3)
PATCH 1/1 nvmet_fc: Reduce work_q count
From: James Smart <[email protected]>
Instead of a work_q per controller queue, make 1 per cpu and have controller queues post work elements to the work_q.
--- drivers/nvme/target/fc.c | 193 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 142 insertions(+), 51 deletions(-)
drivers/nvme/target/fc.c +142 -51
@@ -20,6 +20,7 @@#include <linux/blk-mq.h> #include <linux/parser.h> #include <linux/random.h> +#include <linux/threads.h> #include <uapi/scsi/fc/fc_fs.h> #include <uapi/scsi/fc/fc_els.h>
@@ -81,6 +82,7 @@ struct nvmet_fc_fcp_iod {u32 offset;enum nvmet_fcp_datadir io_dir;bool active; +bool started;bool abort;bool aborted;bool writedataactive;
@@ -88,12 +90,12 @@ struct nvmet_fc_fcp_iod {struct nvmet_req req;struct work_struct work; -struct work_struct done_work;struct nvmet_fc_tgtport *tgtport;struct nvmet_fc_tgt_queue *queue; -struct list_head fcp_list;/* tgtport->fcp_list */ +struct list_head fcp_list;/* queue->fod_list */ +struct list_head work_list;/* workcpu->work_list */ };struct nvmet_fc_tgtport {
@@ -132,7 +134,6 @@ struct nvmet_fc_tgt_queue {struct nvmet_fc_tgt_assoc *assoc;struct nvmet_fc_fcp_iod *fod;/* array of fcp_iods */ struct list_head fod_list; -struct workqueue_struct *work_q;struct kref ref; }__aligned(sizeof(unsigned long long)) ;
@@ -145,6 +146,20 @@ struct nvmet_fc_tgt_assoc {struct kref ref; }; +enum nvmet_fc_workcpu_flags { +NVMET_FC_CPU_RUNNING = (1 <<0 ), +NVMET_FC_CPU_TERMINATING = (1 <<1 ), +}; + +struct nvmet_fc_work_by_cpu { +struct workqueue_struct *work_q; +struct list_head fod_list; +spinlock_t clock; +int cpu; +bool running; +struct work_struct cpu_work; +}; +static inline int nvmet_fc_iodnum (struct nvmet_fc_ls_iod *iodptr)
@@ -213,10 +228,10 @@ static DEFINE_SPINLOCK(nvmet_fc_tgtlock);static LIST_HEAD (nvmet_fc_target_list);static DEFINE_IDA (nvmet_fc_tgtport_cnt); +static u32 nvmet_fc_cpu_cnt; +static struct nvmet_fc_work_by_cpu nvmet_fc_workcpu[NR_CPUS ];static void nvmet_fc_handle_ls_rqst_work (struct work_struct *work); -static void nvmet_fc_handle_fcp_rqst_work (struct work_struct *work); -static void nvmet_fc_fcp_rqst_op_done_work (struct work_struct *work);static void nvmet_fc_tgt_a_put (struct nvmet_fc_tgt_assoc *assoc);static int nvmet_fc_tgt_a_get (struct nvmet_fc_tgt_assoc *assoc);static void nvmet_fc_tgt_q_put (struct nvmet_fc_tgt_queue *queue);
@@ -417,11 +432,10 @@ nvmet_fc_prep_fcp_iodlist(struct nvmet_fc_tgtport *tgtport,int i;for (i =0 ; i < queue->sqsize; fod++, i++) { -INIT_WORK (&fod->work, nvmet_fc_handle_fcp_rqst_work); -INIT_WORK (&fod->done_work, nvmet_fc_fcp_rqst_op_done_work); fod->tgtport = tgtport; fod->queue = queue; fod->active = false; + fod->started = false; fod->abort = false; fod->aborted = false; fod->fcpreq =NULL ;
@@ -498,6 +512,7 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,spin_lock_irqsave (&queue->qlock, flags);list_add_tail (&fod->fcp_list, &fod->queue->fod_list); fod->active = false; + fod->started = false; fod->abort = false; fod->aborted = false; fod->writedataactive = false;
@@ -558,12 +573,6 @@ nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,if (!nvmet_fc_tgt_a_get (assoc)) goto out_free_queue; - queue->work_q =alloc_workqueue ("ntfc%d.%d.%d" ,0 ,0 , - assoc->tgtport->fc_target_port.port_num, - assoc->a_id, qid); -if (!queue->work_q) - goto out_a_put; - queue->fod = (struct nvmet_fc_fcp_iod *)&queue[1 ]; queue->qid = qid; queue->sqsize = sqsize;
@@ -593,8 +602,6 @@ nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc, out_fail_iodlist:nvmet_fc_destroy_fcp_iodlist (assoc->tgtport, queue); -destroy_workqueue (queue->work_q); -out_a_put:nvmet_fc_tgt_a_put (assoc); out_free_queue:kfree (queue);
@@ -617,8 +624,6 @@ nvmet_fc_tgt_queue_free(struct kref *ref)nvmet_fc_tgt_a_put (queue->assoc); -destroy_workqueue (queue->work_q); -kfree (queue); }
@@ -670,8 +675,6 @@ nvmet_fc_delete_target_queue(struct nvmet_fc_tgt_queue *queue) }spin_unlock_irqrestore (&queue->qlock, flags); -flush_workqueue (queue->work_q); -if (disconnect)nvmet_sq_destroy (&queue->nvme_sq);
@@ -1964,24 +1967,28 @@ nvmet_fc_fod_op_done(struct nvmet_fc_fcp_iod *fod) }static void -nvmet_fc_fcp_rqst_op_done_work (struct work_struct *work) -{ -struct nvmet_fc_fcp_iod *fod = - container_of(work, struct nvmet_fc_fcp_iod, done_work); - -nvmet_fc_fod_op_done (fod); -} - -static void nvmet_fc_xmt_fcp_op_done (struct nvmefc_tgt_fcp_req *fcpreq) {struct nvmet_fc_fcp_iod *fod = fcpreq->nvmet_fc_private;struct nvmet_fc_tgt_queue *queue = fod->queue; - -if (fod->tgtport->ops->target_features &NVMET_FCTGTFEAT_OPDONE_IN_ISR ) -/* context switch so completion is not in ISR context */ -queue_work_on (queue->cpu, queue->work_q, &fod->done_work); -else +struct nvmet_fc_work_by_cpu *workcpu = &nvmet_fc_workcpu[queue->cpu]; +unsigned long flags; +bool running; + +if (fod->tgtport->ops->target_features & +NVMET_FCTGTFEAT_OPDONE_IN_ISR ) { +/* context switch for processing */ + +spin_lock_irqsave (&workcpu->clock, flags); +list_add_tail (&fod->work_list, &workcpu->fod_list); + running = workcpu->running; + workcpu->running = true; +spin_unlock_irqrestore (&workcpu->clock, flags); + +if (!running) +queue_work_on (workcpu->cpu, workcpu->work_q, + &workcpu->cpu_work); + }else nvmet_fc_fod_op_done (fod); }
@@ -2071,6 +2078,7 @@ nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport *tgtport,* layer until we have both based on csn. */ + fod->started = true; fod->fcpreq->done = nvmet_fc_xmt_fcp_op_done; fod->total_length =be32_to_cpu (cmdiu->data_len);
@@ -2146,19 +2154,6 @@ nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport *tgtport,nvmet_fc_abort_op (tgtport, fod); } -/* -* Actual processing routine for received FC-NVME LS Requests from the LLD -*/ -static void -nvmet_fc_handle_fcp_rqst_work (struct work_struct *work) -{ -struct nvmet_fc_fcp_iod *fod = - container_of(work, struct nvmet_fc_fcp_iod, work); -struct nvmet_fc_tgtport *tgtport = fod->tgtport; - -nvmet_fc_handle_fcp_rqst (tgtport, fod); -} -/** * nvmet_fc_rcv_fcp_req - transport entry point called by an LLDD * upon the reception of a NVME FCP CMD IU.
@@ -2188,6 +2183,9 @@ nvmet_fc_rcv_fcp_req(struct nvmet_fc_target_port *target_port,struct nvme_fc_cmd_iu *cmdiu = cmdiubuf;struct nvmet_fc_tgt_queue *queue;struct nvmet_fc_fcp_iod *fod; +struct nvmet_fc_work_by_cpu *workcpu; +unsigned long flags; +bool running;/* validate iu, so the connection id can be used to find the queue */ if ((cmdiubuf_len != sizeof(*cmdiu)) ||
@@ -2225,9 +2223,21 @@ nvmet_fc_rcv_fcp_req(struct nvmet_fc_target_port *target_port, ((queue->qid - 1) % tgtport->ops->max_hw_queues) : 0;memcpy (&fod->cmdiubuf, cmdiubuf, cmdiubuf_len); -if (tgtport->ops->target_features &NVMET_FCTGTFEAT_CMD_IN_ISR ) -queue_work_on (queue->cpu, queue->work_q, &fod->work); -else +if (tgtport->ops->target_features &NVMET_FCTGTFEAT_CMD_IN_ISR ) { +/* context switch for processing */ + + workcpu = &nvmet_fc_workcpu[queue->cpu]; + +spin_lock_irqsave (&workcpu->clock, flags); +list_add_tail (&fod->work_list, &workcpu->fod_list); + running = workcpu->running; + workcpu->running = true; +spin_unlock_irqrestore (&workcpu->clock, flags); + +if (!running) +queue_work_on (workcpu->cpu, workcpu->work_q, + &workcpu->cpu_work); + }else nvmet_fc_handle_fcp_rqst (tgtport, fod);return 0 ;
@@ -2393,13 +2403,17 @@ nvmet_fc_remove_port(struct nvmet_port *port) {struct nvmet_fc_tgtport *tgtport = port->priv;unsigned long flags; +bool matched = false;spin_lock_irqsave (&nvmet_fc_tgtlock, flags);if (tgtport->port == port) { -nvmet_fc_tgtport_put (tgtport); + matched = true; tgtport->port =NULL ; }spin_unlock_irqrestore (&nvmet_fc_tgtlock, flags); + +if (matched) +nvmet_fc_tgtport_put (tgtport); }static struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops = {
@@ -2412,9 +2426,84 @@ static struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops = { .delete_ctrl = nvmet_fc_delete_ctrl, }; +static void +nvmet_fc_do_work_on_cpu (struct work_struct *work) +{ +struct nvmet_fc_work_by_cpu *workcpu = + container_of(work, struct nvmet_fc_work_by_cpu, cpu_work); +struct nvmet_fc_fcp_iod *fod; +unsigned long flags; + +do { +spin_lock_irqsave (&workcpu->clock, flags); + fod =list_first_entry_or_null (&workcpu->fod_list, + struct nvmet_fc_fcp_iod, work_list); +if (fod) +list_del (&fod->work_list); +else + workcpu->running = false; +spin_unlock_irqrestore (&workcpu->clock, flags); +if (fod) { +if (fod->started) +nvmet_fc_fod_op_done (fod); +else +nvmet_fc_handle_fcp_rqst (fod->tgtport, fod); + } + }while (fod); +} + +static int +nvmet_fc_create_threads (void ) +{ +struct nvmet_fc_work_by_cpu *workcpu = nvmet_fc_workcpu; +int i; + + nvmet_fc_cpu_cnt =num_active_cpus (); +for (i =0 ; i < nvmet_fc_cpu_cnt; i++, workcpu++) { + workcpu->work_q =alloc_workqueue ("nvmet-fc-cpu%d" ,0 ,0 , i); +if (!workcpu->work_q) +return -ENOEXEC ; + workcpu->cpu = i; + workcpu->running = false; +spin_lock_init (&workcpu->clock); +INIT_LIST_HEAD (&workcpu->fod_list); +INIT_WORK (&workcpu->cpu_work, nvmet_fc_do_work_on_cpu); + } + +return 0 ; +} + +static void +nvmet_fc_delete_threads (void ) +{ +struct nvmet_fc_work_by_cpu *workcpu = nvmet_fc_workcpu; +int i; + +for (i =0 ; i < nvmet_fc_cpu_cnt; i++, workcpu++) { +/* sanity check - all work should be removed */ +if (!list_empty (&workcpu->fod_list)) +pr_warn ("%s: cpu %d worklist not empty\n" , __func__, i); + +if (workcpu->work_q) +destroy_workqueue (workcpu->work_q); + } +} +static int __init nvmet_fc_init_module (void ) { -return nvmet_register_transport (&nvmet_fc_tgt_fcp_ops); +int ret; + + ret =nvmet_fc_create_threads (); +if (ret) + goto fail; + + ret =nvmet_register_transport (&nvmet_fc_tgt_fcp_ops); +if (!ret) +return 0 ; + +fail: +nvmet_fc_delete_threads (); +return -ENXIO ; } static void __exit nvmet_fc_exit_module(void)
@@ -2425,6 +2514,8 @@ static void __exit nvmet_fc_exit_module(void)nvmet_unregister_transport (&nvmet_fc_tgt_fcp_ops); +nvmet_fc_delete_threads (); +ida_destroy (&nvmet_fc_tgtport_cnt); }
Signature · 1 line
2.9.3
PATCH 1/1 nvmet_fc: Reduce work_q count
Hi James,
Quoted 7 lines · On 04/26/2017 01:22 AM, [email protected] wrote:
From: James Smart <jsmart2021 at gmail.com>
Instead of a
work_qper controller queue, make 1 per cpu and have controller queues post work elements to thework_q.Signed-off-by James Smart <james.smart at broadcom.com>---
[...]
Quoted 19 lines
drivers/nvme/target/fc.c +14
-0+enum nvmet_fc_workcpu_flags { +NVMET_FC_CPU_RUNNING = (1 <<0 ), +NVMET_FC_CPU_TERMINATING = (1 <<1 ), +}; + +struct nvmet_fc_work_by_cpu { +struct workqueue_struct *work_q; +struct list_head fod_list; +spinlock_t clock; +int cpu; +bool running; +struct work_struct cpu_work; +}; +static inline int nvmet_fc_iodnum (struct nvmet_fc_ls_iod *iodptr)
drivers/nvme/target/fc.c static DEFINE_SPINLOCK(nvmet_fc_tgtlock); · line 232
Quoted 5 lines
drivers/nvme/target/fc.c +2
-0@@ -213,10 +228,10 @@ static DEFINE_SPINLOCK(nvmet_fc_tgtlock);static LIST_HEAD (nvmet_fc_target_list);static DEFINE_IDA (nvmet_fc_tgtport_cnt); +static u32 nvmet_fc_cpu_cnt; +static struct nvmet_fc_work_by_cpu nvmet_fc_workcpu[NR_CPUS ];
Is there a reason that prevents you from using DEFINE_PER_CPU() here instead of that array?
[...]
Quoted 15 lines
drivers/nvme/target/fc.c +15
-0+static void +nvmet_fc_do_work_on_cpu (struct work_struct *work) +{ +struct nvmet_fc_work_by_cpu *workcpu = + container_of(work, struct nvmet_fc_work_by_cpu, cpu_work); +struct nvmet_fc_fcp_iod *fod; +unsigned long flags; + + do { + spin_lock_irqsave(&workcpu->clock, flags); + fod = list_first_entry_or_null(&workcpu->fod_list, + struct nvmet_fc_fcp_iod, work_list); + if (fod) + list_del(&fod->work_list); + else
drivers/nvme/target/fc.c static struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops = { · line 2453
Quoted 10 lines
drivers/nvme/target/fc.c +10
-0+ workcpu->running = false; +spin_unlock_irqrestore (&workcpu->clock, flags); +if (fod) { +if (fod->started) +nvmet_fc_fod_op_done (fod); +else +nvmet_fc_handle_fcp_rqst (fod->tgtport, fod); + } + }while (fod); +}
This do {} while(fod) construct looks a bit unusual to me. I do agree using a 'while (!list_empty(&workcpu->fod_list))' loop requires you two list_empty() calls (the 2nd would be in list_first_emtpy_or_null()) but is IMHO a bit more readable. Or am I missing something here?
drivers/nvme/target/fc.c static struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops = { · line 2456
Quoted 3 lines
drivers/nvme/target/fc.c +3
-0+ +static int +nvmet_fc_create_threads (void )
nvmet_fc_create_workqueues()? After all you're creating a workqueue not a kthread.
Signature · 6 lines
Johannes Thumshirn Storage jthumshirn at suse.de +49 911 74053 689 SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N?rnberg GF: Felix Imend?rffer, Jane Smithard, Graham Norton HRB 21284 (AG N?rnberg) Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
PATCH 1/1 nvmet_fc: Reduce work_q count
Quoted 9 lines · Johannes Thumshirn
Hi James,
drivers/nvme/target/fc.c static DEFINE_SPINLOCK(nvmet_fc_tgtlock); · line 232
Quoted 3 lines · On 04/26/2017 01:22 AM, [email protected] wrote:
drivers/nvme/target/fc.c +2
-0+static u32 nvmet_fc_cpu_cnt; +static struct nvmet_fc_work_by_cpu nvmet_fc_workcpu[NR_CPUS ];Is there a reason that prevents you from using
DEFINE_PER_CPU()here instead of that array?
Nope. I'll repost with the change.
Quoted 5 lines
This do {} while(fod) construct looks a bit unusual to me. I do agree using a 'while (!list_empty(&workcpu->fod_list))' loop requires you two
list_empty()calls (the 2nd would be inlist_first_emtpy_or_null()) but is IMHO a bit more readable. Or am I missing something here?
Nope. It's not a big deal so I'll include this as well.
Quoted 7 lines
drivers/nvme/target/fc.c static struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops = { · line 2456
Quoted 4 lines
drivers/nvme/target/fc.c +3
-0+ +static int +nvmet_fc_create_threads (void )
nvmet_fc_create_workqueues()? After all you're creating a workqueue not a kthread.
:) sure.
-- james