From 2d2df09c5ad5b94cfa89c089f4334c83cc888f09 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Sat, 3 Nov 2012 13:22:29 +0000 Subject: [PATCH] scst: Move ioctx manipulation code (merge r4254 from trunk) Move the code for obtaining and releasing an I/O context out of scst_process_active_cmd() in order to improve source code readability. Signed-off-by: Bart Van Assche git-svn-id: http://svn.code.sf.net/p/scst/svn/branches/2.2.x@4570 d57e44dd-8a1f-0410-8b47-8ef2f437770f --- scst/src/scst_targ.c | 123 ++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 48 deletions(-) diff --git a/scst/src/scst_targ.c b/scst/src/scst_targ.c index a7b471ae2..55502fd22 100644 --- a/scst/src/scst_targ.c +++ b/scst/src/scst_targ.c @@ -4074,6 +4074,79 @@ int scst_init_thread(void *arg) return 0; } +/** + * scst_ioctx_get() - Associate an I/O context with a thread. + * + * Associate an I/O context with a thread in such a way that all threads in an + * SCST thread pool share the same I/O context. This greatly improves thread + * pool I/O performance with at least the CFQ scheduler. + * + * Note: A more elegant approach would be to allocate the I/O context in + * scst_init_threads() instead of this function. That approach is only possible + * though after exporting alloc_io_context(). A previous discussion of this + * topic can be found here: http://lkml.org/lkml/2008/12/11/282. + */ +static void scst_ioctx_get(struct scst_cmd_threads *p_cmd_threads) +{ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) + mutex_lock(&p_cmd_threads->io_context_mutex); + + WARN_ON(current->io_context); + + if (p_cmd_threads != &scst_main_cmd_threads) { + /* + * For linked IO contexts io_context might be not NULL while + * io_context 0. + */ + if (p_cmd_threads->io_context == NULL) { +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0) + p_cmd_threads->io_context = get_task_io_context(current, + GFP_KERNEL, NUMA_NO_NODE); +#else + p_cmd_threads->io_context = get_io_context(GFP_KERNEL, -1); +#endif + TRACE_MGMT_DBG("Alloced new IO context %p " + "(p_cmd_threads %p)", p_cmd_threads->io_context, + p_cmd_threads); + /* + * Put the extra reference created by get_io_context() + * because we don't need it. + */ + put_io_context(p_cmd_threads->io_context); + } else { + current->io_context = + ioc_task_link(p_cmd_threads->io_context); + TRACE_MGMT_DBG("Linked IO context %p " + "(p_cmd_threads %p)", p_cmd_threads->io_context, + p_cmd_threads); + } + p_cmd_threads->io_context_refcnt++; + } + + mutex_unlock(&p_cmd_threads->io_context_mutex); +#endif + + smp_wmb(); + p_cmd_threads->io_context_ready = true; + return; +} + +/** + * scst_ioctx_put() - Free I/O context allocated by scst_ioctx_get(). + */ +static void scst_ioctx_put(struct scst_cmd_threads *p_cmd_threads) +{ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) + if (p_cmd_threads != &scst_main_cmd_threads) { + mutex_lock(&p_cmd_threads->io_context_mutex); + if (--p_cmd_threads->io_context_refcnt == 0) + p_cmd_threads->io_context = NULL; + mutex_unlock(&p_cmd_threads->io_context_mutex); + } +#endif + return; +} + /** * scst_process_active_cmd() - process active command * @@ -4296,46 +4369,7 @@ int scst_cmd_thread(void *arg) #endif current->flags |= PF_NOFREEZE; -#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) - mutex_lock(&p_cmd_threads->io_context_mutex); - - WARN_ON(current->io_context); - - if (p_cmd_threads != &scst_main_cmd_threads) { - /* - * For linked IO contexts io_context might be not NULL while - * io_context 0. - */ - if (p_cmd_threads->io_context == NULL) { -#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0) - p_cmd_threads->io_context = get_task_io_context(current, - GFP_KERNEL, NUMA_NO_NODE); -#else - p_cmd_threads->io_context = get_io_context(GFP_KERNEL, -1); -#endif - TRACE_MGMT_DBG("Alloced new IO context %p " - "(p_cmd_threads %p)", - p_cmd_threads->io_context, - p_cmd_threads); - /* - * Put the extra reference created by get_io_context() - * because we don't need it. - */ - put_io_context(p_cmd_threads->io_context); - } else { - current->io_context = ioc_task_link(p_cmd_threads->io_context); - TRACE_MGMT_DBG("Linked IO context %p " - "(p_cmd_threads %p)", p_cmd_threads->io_context, - p_cmd_threads); - } - p_cmd_threads->io_context_refcnt++; - } - - mutex_unlock(&p_cmd_threads->io_context_mutex); -#endif - - smp_wmb(); - p_cmd_threads->io_context_ready = true; + scst_ioctx_get(p_cmd_threads); spin_lock_irq(&p_cmd_threads->cmd_list_lock); while (!kthread_should_stop()) { @@ -4369,14 +4403,7 @@ int scst_cmd_thread(void *arg) } spin_unlock_irq(&p_cmd_threads->cmd_list_lock); -#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) - if (p_cmd_threads != &scst_main_cmd_threads) { - mutex_lock(&p_cmd_threads->io_context_mutex); - if (--p_cmd_threads->io_context_refcnt == 0) - p_cmd_threads->io_context = NULL; - mutex_unlock(&p_cmd_threads->io_context_mutex); - } -#endif + scst_ioctx_put(p_cmd_threads); PRINT_INFO("Processing thread %s (PID %d) finished", current->comm, current->pid);