1 /*******************************************************************************
2 * This file contains the iSCSI Login Thread and Thread Queue functions.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
19 #include <linux/kthread.h>
20 #include <linux/list.h>
21 #include <linux/bitmap.h>
23 #include <target/iscsi/iscsi_target_core.h>
24 #include "iscsi_target_tq.h"
25 #include "iscsi_target.h"
27 static LIST_HEAD(inactive_ts_list);
28 static DEFINE_SPINLOCK(inactive_ts_lock);
29 static DEFINE_SPINLOCK(ts_bitmap_lock);
31 static void iscsi_add_ts_to_inactive_list(struct iscsi_thread_set *ts)
33 if (!list_empty(&ts->ts_list)) {
37 spin_lock(&inactive_ts_lock);
38 list_add_tail(&ts->ts_list, &inactive_ts_list);
39 iscsit_global->inactive_ts++;
40 spin_unlock(&inactive_ts_lock);
43 static struct iscsi_thread_set *iscsi_get_ts_from_inactive_list(void)
45 struct iscsi_thread_set *ts;
47 spin_lock(&inactive_ts_lock);
48 if (list_empty(&inactive_ts_list)) {
49 spin_unlock(&inactive_ts_lock);
53 ts = list_first_entry(&inactive_ts_list, struct iscsi_thread_set, ts_list);
55 list_del_init(&ts->ts_list);
56 iscsit_global->inactive_ts--;
57 spin_unlock(&inactive_ts_lock);
62 int iscsi_allocate_thread_sets(u32 thread_pair_count)
64 int allocated_thread_pair_count = 0, i, thread_id;
65 struct iscsi_thread_set *ts = NULL;
67 for (i = 0; i < thread_pair_count; i++) {
68 ts = kzalloc(sizeof(struct iscsi_thread_set), GFP_KERNEL);
70 pr_err("Unable to allocate memory for"
72 return allocated_thread_pair_count;
75 * Locate the next available regision in the thread_set_bitmap
77 spin_lock(&ts_bitmap_lock);
78 thread_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
79 iscsit_global->ts_bitmap_count, get_order(1));
80 spin_unlock(&ts_bitmap_lock);
82 pr_err("bitmap_find_free_region() failed for"
83 " thread_set_bitmap\n");
85 return allocated_thread_pair_count;
88 ts->thread_id = thread_id;
89 ts->status = ISCSI_THREAD_SET_FREE;
90 INIT_LIST_HEAD(&ts->ts_list);
91 spin_lock_init(&ts->ts_state_lock);
92 init_completion(&ts->rx_restart_comp);
93 init_completion(&ts->tx_restart_comp);
94 init_completion(&ts->rx_start_comp);
95 init_completion(&ts->tx_start_comp);
96 sema_init(&ts->ts_activate_sem, 0);
98 ts->create_threads = 1;
99 ts->tx_thread = kthread_run(iscsi_target_tx_thread, ts, "%s",
100 ISCSI_TX_THREAD_NAME);
101 if (IS_ERR(ts->tx_thread)) {
103 pr_err("Unable to start iscsi_target_tx_thread\n");
107 ts->rx_thread = kthread_run(iscsi_target_rx_thread, ts, "%s",
108 ISCSI_RX_THREAD_NAME);
109 if (IS_ERR(ts->rx_thread)) {
110 kthread_stop(ts->tx_thread);
111 pr_err("Unable to start iscsi_target_rx_thread\n");
114 ts->create_threads = 0;
116 iscsi_add_ts_to_inactive_list(ts);
117 allocated_thread_pair_count++;
120 pr_debug("Spawned %d thread set(s) (%d total threads).\n",
121 allocated_thread_pair_count, allocated_thread_pair_count * 2);
122 return allocated_thread_pair_count;
125 static void iscsi_deallocate_thread_one(struct iscsi_thread_set *ts)
127 spin_lock_bh(&ts->ts_state_lock);
128 ts->status = ISCSI_THREAD_SET_DIE;
131 complete(&ts->rx_start_comp);
132 spin_unlock_bh(&ts->ts_state_lock);
133 kthread_stop(ts->rx_thread);
134 spin_lock_bh(&ts->ts_state_lock);
137 complete(&ts->tx_start_comp);
138 spin_unlock_bh(&ts->ts_state_lock);
139 kthread_stop(ts->tx_thread);
140 spin_lock_bh(&ts->ts_state_lock);
142 spin_unlock_bh(&ts->ts_state_lock);
144 * Release this thread_id in the thread_set_bitmap
146 spin_lock(&ts_bitmap_lock);
147 bitmap_release_region(iscsit_global->ts_bitmap,
148 ts->thread_id, get_order(1));
149 spin_unlock(&ts_bitmap_lock);
154 void iscsi_deallocate_thread_sets(void)
156 struct iscsi_thread_set *ts = NULL;
157 u32 released_count = 0;
159 while ((ts = iscsi_get_ts_from_inactive_list())) {
161 iscsi_deallocate_thread_one(ts);
166 pr_debug("Stopped %d thread set(s) (%d total threads)."
167 "\n", released_count, released_count * 2);
170 static void iscsi_deallocate_extra_thread_sets(void)
172 u32 orig_count, released_count = 0;
173 struct iscsi_thread_set *ts = NULL;
175 orig_count = TARGET_THREAD_SET_COUNT;
177 while ((iscsit_global->inactive_ts + 1) > orig_count) {
178 ts = iscsi_get_ts_from_inactive_list();
182 iscsi_deallocate_thread_one(ts);
187 pr_debug("Stopped %d thread set(s) (%d total threads)."
188 "\n", released_count, released_count * 2);
191 void iscsi_activate_thread_set(struct iscsi_conn *conn, struct iscsi_thread_set *ts)
193 spin_lock_bh(&ts->ts_state_lock);
194 conn->thread_set = ts;
196 ts->status = ISCSI_THREAD_SET_ACTIVE;
197 spin_unlock_bh(&ts->ts_state_lock);
199 complete(&ts->rx_start_comp);
200 complete(&ts->tx_start_comp);
202 down(&ts->ts_activate_sem);
205 struct iscsi_thread_set *iscsi_get_thread_set(void)
207 struct iscsi_thread_set *ts;
210 ts = iscsi_get_ts_from_inactive_list();
212 iscsi_allocate_thread_sets(1);
216 ts->delay_inactive = 1;
218 ts->thread_count = 2;
219 init_completion(&ts->rx_restart_comp);
220 init_completion(&ts->tx_restart_comp);
221 sema_init(&ts->ts_activate_sem, 0);
226 void iscsi_set_thread_clear(struct iscsi_conn *conn, u8 thread_clear)
228 struct iscsi_thread_set *ts = NULL;
230 if (!conn->thread_set) {
231 pr_err("struct iscsi_conn->thread_set is NULL\n");
234 ts = conn->thread_set;
236 spin_lock_bh(&ts->ts_state_lock);
237 ts->thread_clear &= ~thread_clear;
239 if ((thread_clear & ISCSI_CLEAR_RX_THREAD) &&
240 (ts->blocked_threads & ISCSI_BLOCK_RX_THREAD))
241 complete(&ts->rx_restart_comp);
242 else if ((thread_clear & ISCSI_CLEAR_TX_THREAD) &&
243 (ts->blocked_threads & ISCSI_BLOCK_TX_THREAD))
244 complete(&ts->tx_restart_comp);
245 spin_unlock_bh(&ts->ts_state_lock);
248 void iscsi_set_thread_set_signal(struct iscsi_conn *conn, u8 signal_sent)
250 struct iscsi_thread_set *ts = NULL;
252 if (!conn->thread_set) {
253 pr_err("struct iscsi_conn->thread_set is NULL\n");
256 ts = conn->thread_set;
258 spin_lock_bh(&ts->ts_state_lock);
259 ts->signal_sent |= signal_sent;
260 spin_unlock_bh(&ts->ts_state_lock);
263 int iscsi_release_thread_set(struct iscsi_conn *conn)
265 int thread_called = 0;
266 struct iscsi_thread_set *ts = NULL;
268 if (!conn || !conn->thread_set) {
269 pr_err("connection or thread set pointer is NULL\n");
272 ts = conn->thread_set;
274 spin_lock_bh(&ts->ts_state_lock);
275 ts->status = ISCSI_THREAD_SET_RESET;
277 if (!strncmp(current->comm, ISCSI_RX_THREAD_NAME,
278 strlen(ISCSI_RX_THREAD_NAME)))
279 thread_called = ISCSI_RX_THREAD;
280 else if (!strncmp(current->comm, ISCSI_TX_THREAD_NAME,
281 strlen(ISCSI_TX_THREAD_NAME)))
282 thread_called = ISCSI_TX_THREAD;
284 if (ts->rx_thread && (thread_called == ISCSI_TX_THREAD) &&
285 (ts->thread_clear & ISCSI_CLEAR_RX_THREAD)) {
287 if (!(ts->signal_sent & ISCSI_SIGNAL_RX_THREAD)) {
288 send_sig(SIGINT, ts->rx_thread, 1);
289 ts->signal_sent |= ISCSI_SIGNAL_RX_THREAD;
291 ts->blocked_threads |= ISCSI_BLOCK_RX_THREAD;
292 spin_unlock_bh(&ts->ts_state_lock);
293 wait_for_completion(&ts->rx_restart_comp);
294 spin_lock_bh(&ts->ts_state_lock);
295 ts->blocked_threads &= ~ISCSI_BLOCK_RX_THREAD;
297 if (ts->tx_thread && (thread_called == ISCSI_RX_THREAD) &&
298 (ts->thread_clear & ISCSI_CLEAR_TX_THREAD)) {
300 if (!(ts->signal_sent & ISCSI_SIGNAL_TX_THREAD)) {
301 send_sig(SIGINT, ts->tx_thread, 1);
302 ts->signal_sent |= ISCSI_SIGNAL_TX_THREAD;
304 ts->blocked_threads |= ISCSI_BLOCK_TX_THREAD;
305 spin_unlock_bh(&ts->ts_state_lock);
306 wait_for_completion(&ts->tx_restart_comp);
307 spin_lock_bh(&ts->ts_state_lock);
308 ts->blocked_threads &= ~ISCSI_BLOCK_TX_THREAD;
312 ts->status = ISCSI_THREAD_SET_FREE;
313 spin_unlock_bh(&ts->ts_state_lock);
318 int iscsi_thread_set_force_reinstatement(struct iscsi_conn *conn)
320 struct iscsi_thread_set *ts;
322 if (!conn->thread_set)
324 ts = conn->thread_set;
326 spin_lock_bh(&ts->ts_state_lock);
327 if (ts->status != ISCSI_THREAD_SET_ACTIVE) {
328 spin_unlock_bh(&ts->ts_state_lock);
332 if (ts->tx_thread && (!(ts->signal_sent & ISCSI_SIGNAL_TX_THREAD))) {
333 send_sig(SIGINT, ts->tx_thread, 1);
334 ts->signal_sent |= ISCSI_SIGNAL_TX_THREAD;
336 if (ts->rx_thread && (!(ts->signal_sent & ISCSI_SIGNAL_RX_THREAD))) {
337 send_sig(SIGINT, ts->rx_thread, 1);
338 ts->signal_sent |= ISCSI_SIGNAL_RX_THREAD;
340 spin_unlock_bh(&ts->ts_state_lock);
345 static void iscsi_check_to_add_additional_sets(void)
349 spin_lock(&inactive_ts_lock);
350 thread_sets_add = iscsit_global->inactive_ts;
351 spin_unlock(&inactive_ts_lock);
352 if (thread_sets_add == 1)
353 iscsi_allocate_thread_sets(1);
356 static int iscsi_signal_thread_pre_handler(struct iscsi_thread_set *ts)
358 spin_lock_bh(&ts->ts_state_lock);
359 if (ts->status == ISCSI_THREAD_SET_DIE || kthread_should_stop() ||
360 signal_pending(current)) {
361 spin_unlock_bh(&ts->ts_state_lock);
364 spin_unlock_bh(&ts->ts_state_lock);
369 struct iscsi_conn *iscsi_rx_thread_pre_handler(struct iscsi_thread_set *ts)
373 spin_lock_bh(&ts->ts_state_lock);
374 if (ts->create_threads) {
375 spin_unlock_bh(&ts->ts_state_lock);
379 if (ts->status != ISCSI_THREAD_SET_DIE)
380 flush_signals(current);
382 if (ts->delay_inactive && (--ts->thread_count == 0)) {
383 spin_unlock_bh(&ts->ts_state_lock);
385 if (!iscsit_global->in_shutdown)
386 iscsi_deallocate_extra_thread_sets();
388 iscsi_add_ts_to_inactive_list(ts);
389 spin_lock_bh(&ts->ts_state_lock);
392 if ((ts->status == ISCSI_THREAD_SET_RESET) &&
393 (ts->thread_clear & ISCSI_CLEAR_RX_THREAD))
394 complete(&ts->rx_restart_comp);
396 ts->thread_clear &= ~ISCSI_CLEAR_RX_THREAD;
397 spin_unlock_bh(&ts->ts_state_lock);
399 ret = wait_for_completion_interruptible(&ts->rx_start_comp);
403 if (iscsi_signal_thread_pre_handler(ts) < 0)
406 iscsi_check_to_add_additional_sets();
408 spin_lock_bh(&ts->ts_state_lock);
410 pr_err("struct iscsi_thread_set->conn is NULL for"
411 " RX thread_id: %s/%d\n", current->comm, current->pid);
412 spin_unlock_bh(&ts->ts_state_lock);
415 ts->thread_clear |= ISCSI_CLEAR_RX_THREAD;
416 spin_unlock_bh(&ts->ts_state_lock);
418 up(&ts->ts_activate_sem);
423 struct iscsi_conn *iscsi_tx_thread_pre_handler(struct iscsi_thread_set *ts)
427 spin_lock_bh(&ts->ts_state_lock);
428 if (ts->create_threads) {
429 spin_unlock_bh(&ts->ts_state_lock);
433 if (ts->status != ISCSI_THREAD_SET_DIE)
434 flush_signals(current);
436 if (ts->delay_inactive && (--ts->thread_count == 0)) {
437 spin_unlock_bh(&ts->ts_state_lock);
439 if (!iscsit_global->in_shutdown)
440 iscsi_deallocate_extra_thread_sets();
442 iscsi_add_ts_to_inactive_list(ts);
443 spin_lock_bh(&ts->ts_state_lock);
445 if ((ts->status == ISCSI_THREAD_SET_RESET) &&
446 (ts->thread_clear & ISCSI_CLEAR_TX_THREAD))
447 complete(&ts->tx_restart_comp);
449 ts->thread_clear &= ~ISCSI_CLEAR_TX_THREAD;
450 spin_unlock_bh(&ts->ts_state_lock);
452 ret = wait_for_completion_interruptible(&ts->tx_start_comp);
456 if (iscsi_signal_thread_pre_handler(ts) < 0)
459 iscsi_check_to_add_additional_sets();
461 spin_lock_bh(&ts->ts_state_lock);
463 pr_err("struct iscsi_thread_set->conn is NULL for"
464 " TX thread_id: %s/%d\n", current->comm, current->pid);
465 spin_unlock_bh(&ts->ts_state_lock);
468 ts->thread_clear |= ISCSI_CLEAR_TX_THREAD;
469 spin_unlock_bh(&ts->ts_state_lock);
471 up(&ts->ts_activate_sem);
476 int iscsi_thread_set_init(void)
480 iscsit_global->ts_bitmap_count = ISCSI_TS_BITMAP_BITS;
482 size = BITS_TO_LONGS(iscsit_global->ts_bitmap_count) * sizeof(long);
483 iscsit_global->ts_bitmap = kzalloc(size, GFP_KERNEL);
484 if (!iscsit_global->ts_bitmap) {
485 pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
492 void iscsi_thread_set_free(void)
494 kfree(iscsit_global->ts_bitmap);