iscsi_target_tq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*******************************************************************************
  2. * This file contains the iSCSI Login Thread and Thread Queue functions.
  3. *
  4. * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
  5. *
  6. * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
  7. *
  8. * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. ******************************************************************************/
  20. #include <linux/kthread.h>
  21. #include <linux/list.h>
  22. #include <linux/bitmap.h>
  23. #include "iscsi_target_core.h"
  24. #include "iscsi_target_tq.h"
  25. #include "iscsi_target.h"
  26. static LIST_HEAD(active_ts_list);
  27. static LIST_HEAD(inactive_ts_list);
  28. static DEFINE_SPINLOCK(active_ts_lock);
  29. static DEFINE_SPINLOCK(inactive_ts_lock);
  30. static DEFINE_SPINLOCK(ts_bitmap_lock);
  31. static void iscsi_add_ts_to_active_list(struct iscsi_thread_set *ts)
  32. {
  33. spin_lock(&active_ts_lock);
  34. list_add_tail(&ts->ts_list, &active_ts_list);
  35. iscsit_global->active_ts++;
  36. spin_unlock(&active_ts_lock);
  37. }
  38. extern void iscsi_add_ts_to_inactive_list(struct iscsi_thread_set *ts)
  39. {
  40. spin_lock(&inactive_ts_lock);
  41. list_add_tail(&ts->ts_list, &inactive_ts_list);
  42. iscsit_global->inactive_ts++;
  43. spin_unlock(&inactive_ts_lock);
  44. }
  45. static void iscsi_del_ts_from_active_list(struct iscsi_thread_set *ts)
  46. {
  47. spin_lock(&active_ts_lock);
  48. list_del(&ts->ts_list);
  49. iscsit_global->active_ts--;
  50. spin_unlock(&active_ts_lock);
  51. }
  52. static struct iscsi_thread_set *iscsi_get_ts_from_inactive_list(void)
  53. {
  54. struct iscsi_thread_set *ts;
  55. spin_lock(&inactive_ts_lock);
  56. if (list_empty(&inactive_ts_list)) {
  57. spin_unlock(&inactive_ts_lock);
  58. return NULL;
  59. }
  60. list_for_each_entry(ts, &inactive_ts_list, ts_list)
  61. break;
  62. list_del(&ts->ts_list);
  63. iscsit_global->inactive_ts--;
  64. spin_unlock(&inactive_ts_lock);
  65. return ts;
  66. }
  67. extern int iscsi_allocate_thread_sets(u32 thread_pair_count)
  68. {
  69. int allocated_thread_pair_count = 0, i, thread_id;
  70. struct iscsi_thread_set *ts = NULL;
  71. for (i = 0; i < thread_pair_count; i++) {
  72. ts = kzalloc(sizeof(struct iscsi_thread_set), GFP_KERNEL);
  73. if (!ts) {
  74. pr_err("Unable to allocate memory for"
  75. " thread set.\n");
  76. return allocated_thread_pair_count;
  77. }
  78. /*
  79. * Locate the next available regision in the thread_set_bitmap
  80. */
  81. spin_lock(&ts_bitmap_lock);
  82. thread_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
  83. iscsit_global->ts_bitmap_count, get_order(1));
  84. spin_unlock(&ts_bitmap_lock);
  85. if (thread_id < 0) {
  86. pr_err("bitmap_find_free_region() failed for"
  87. " thread_set_bitmap\n");
  88. kfree(ts);
  89. return allocated_thread_pair_count;
  90. }
  91. ts->thread_id = thread_id;
  92. ts->status = ISCSI_THREAD_SET_FREE;
  93. INIT_LIST_HEAD(&ts->ts_list);
  94. spin_lock_init(&ts->ts_state_lock);
  95. init_completion(&ts->rx_post_start_comp);
  96. init_completion(&ts->tx_post_start_comp);
  97. init_completion(&ts->rx_restart_comp);
  98. init_completion(&ts->tx_restart_comp);
  99. init_completion(&ts->rx_start_comp);
  100. init_completion(&ts->tx_start_comp);
  101. ts->create_threads = 1;
  102. ts->tx_thread = kthread_run(iscsi_target_tx_thread, ts, "%s",
  103. ISCSI_TX_THREAD_NAME);
  104. if (IS_ERR(ts->tx_thread)) {
  105. dump_stack();
  106. pr_err("Unable to start iscsi_target_tx_thread\n");
  107. break;
  108. }
  109. ts->rx_thread = kthread_run(iscsi_target_rx_thread, ts, "%s",
  110. ISCSI_RX_THREAD_NAME);
  111. if (IS_ERR(ts->rx_thread)) {
  112. kthread_stop(ts->tx_thread);
  113. pr_err("Unable to start iscsi_target_rx_thread\n");
  114. break;
  115. }
  116. ts->create_threads = 0;
  117. iscsi_add_ts_to_inactive_list(ts);
  118. allocated_thread_pair_count++;
  119. }
  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;
  123. }
  124. extern void iscsi_deallocate_thread_sets(void)
  125. {
  126. u32 released_count = 0;
  127. struct iscsi_thread_set *ts = NULL;
  128. while ((ts = iscsi_get_ts_from_inactive_list())) {
  129. spin_lock_bh(&ts->ts_state_lock);
  130. ts->status = ISCSI_THREAD_SET_DIE;
  131. spin_unlock_bh(&ts->ts_state_lock);
  132. if (ts->rx_thread) {
  133. send_sig(SIGINT, ts->rx_thread, 1);
  134. kthread_stop(ts->rx_thread);
  135. }
  136. if (ts->tx_thread) {
  137. send_sig(SIGINT, ts->tx_thread, 1);
  138. kthread_stop(ts->tx_thread);
  139. }
  140. /*
  141. * Release this thread_id in the thread_set_bitmap
  142. */
  143. spin_lock(&ts_bitmap_lock);
  144. bitmap_release_region(iscsit_global->ts_bitmap,
  145. ts->thread_id, get_order(1));
  146. spin_unlock(&ts_bitmap_lock);
  147. released_count++;
  148. kfree(ts);
  149. }
  150. if (released_count)
  151. pr_debug("Stopped %d thread set(s) (%d total threads)."
  152. "\n", released_count, released_count * 2);
  153. }
  154. static void iscsi_deallocate_extra_thread_sets(void)
  155. {
  156. u32 orig_count, released_count = 0;
  157. struct iscsi_thread_set *ts = NULL;
  158. orig_count = TARGET_THREAD_SET_COUNT;
  159. while ((iscsit_global->inactive_ts + 1) > orig_count) {
  160. ts = iscsi_get_ts_from_inactive_list();
  161. if (!ts)
  162. break;
  163. spin_lock_bh(&ts->ts_state_lock);
  164. ts->status = ISCSI_THREAD_SET_DIE;
  165. spin_unlock_bh(&ts->ts_state_lock);
  166. if (ts->rx_thread) {
  167. send_sig(SIGINT, ts->rx_thread, 1);
  168. kthread_stop(ts->rx_thread);
  169. }
  170. if (ts->tx_thread) {
  171. send_sig(SIGINT, ts->tx_thread, 1);
  172. kthread_stop(ts->tx_thread);
  173. }
  174. /*
  175. * Release this thread_id in the thread_set_bitmap
  176. */
  177. spin_lock(&ts_bitmap_lock);
  178. bitmap_release_region(iscsit_global->ts_bitmap,
  179. ts->thread_id, get_order(1));
  180. spin_unlock(&ts_bitmap_lock);
  181. released_count++;
  182. kfree(ts);
  183. }
  184. if (released_count) {
  185. pr_debug("Stopped %d thread set(s) (%d total threads)."
  186. "\n", released_count, released_count * 2);
  187. }
  188. }
  189. void iscsi_activate_thread_set(struct iscsi_conn *conn, struct iscsi_thread_set *ts)
  190. {
  191. iscsi_add_ts_to_active_list(ts);
  192. spin_lock_bh(&ts->ts_state_lock);
  193. conn->thread_set = ts;
  194. ts->conn = conn;
  195. spin_unlock_bh(&ts->ts_state_lock);
  196. /*
  197. * Start up the RX thread and wait on rx_post_start_comp. The RX
  198. * Thread will then do the same for the TX Thread in
  199. * iscsi_rx_thread_pre_handler().
  200. */
  201. complete(&ts->rx_start_comp);
  202. wait_for_completion(&ts->rx_post_start_comp);
  203. }
  204. struct iscsi_thread_set *iscsi_get_thread_set(void)
  205. {
  206. int allocate_ts = 0;
  207. struct completion comp;
  208. struct iscsi_thread_set *ts = NULL;
  209. /*
  210. * If no inactive thread set is available on the first call to
  211. * iscsi_get_ts_from_inactive_list(), sleep for a second and
  212. * try again. If still none are available after two attempts,
  213. * allocate a set ourselves.
  214. */
  215. get_set:
  216. ts = iscsi_get_ts_from_inactive_list();
  217. if (!ts) {
  218. if (allocate_ts == 2)
  219. iscsi_allocate_thread_sets(1);
  220. init_completion(&comp);
  221. wait_for_completion_timeout(&comp, 1 * HZ);
  222. allocate_ts++;
  223. goto get_set;
  224. }
  225. ts->delay_inactive = 1;
  226. ts->signal_sent = 0;
  227. ts->thread_count = 2;
  228. init_completion(&ts->rx_restart_comp);
  229. init_completion(&ts->tx_restart_comp);
  230. return ts;
  231. }
  232. void iscsi_set_thread_clear(struct iscsi_conn *conn, u8 thread_clear)
  233. {
  234. struct iscsi_thread_set *ts = NULL;
  235. if (!conn->thread_set) {
  236. pr_err("struct iscsi_conn->thread_set is NULL\n");
  237. return;
  238. }
  239. ts = conn->thread_set;
  240. spin_lock_bh(&ts->ts_state_lock);
  241. ts->thread_clear &= ~thread_clear;
  242. if ((thread_clear & ISCSI_CLEAR_RX_THREAD) &&
  243. (ts->blocked_threads & ISCSI_BLOCK_RX_THREAD))
  244. complete(&ts->rx_restart_comp);
  245. else if ((thread_clear & ISCSI_CLEAR_TX_THREAD) &&
  246. (ts->blocked_threads & ISCSI_BLOCK_TX_THREAD))
  247. complete(&ts->tx_restart_comp);
  248. spin_unlock_bh(&ts->ts_state_lock);
  249. }
  250. void iscsi_set_thread_set_signal(struct iscsi_conn *conn, u8 signal_sent)
  251. {
  252. struct iscsi_thread_set *ts = NULL;
  253. if (!conn->thread_set) {
  254. pr_err("struct iscsi_conn->thread_set is NULL\n");
  255. return;
  256. }
  257. 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);
  261. }
  262. int iscsi_release_thread_set(struct iscsi_conn *conn)
  263. {
  264. int thread_called = 0;
  265. struct iscsi_thread_set *ts = NULL;
  266. if (!conn || !conn->thread_set) {
  267. pr_err("connection or thread set pointer is NULL\n");
  268. BUG();
  269. }
  270. ts = conn->thread_set;
  271. spin_lock_bh(&ts->ts_state_lock);
  272. ts->status = ISCSI_THREAD_SET_RESET;
  273. if (!strncmp(current->comm, ISCSI_RX_THREAD_NAME,
  274. strlen(ISCSI_RX_THREAD_NAME)))
  275. thread_called = ISCSI_RX_THREAD;
  276. else if (!strncmp(current->comm, ISCSI_TX_THREAD_NAME,
  277. strlen(ISCSI_TX_THREAD_NAME)))
  278. thread_called = ISCSI_TX_THREAD;
  279. if (ts->rx_thread && (thread_called == ISCSI_TX_THREAD) &&
  280. (ts->thread_clear & ISCSI_CLEAR_RX_THREAD)) {
  281. if (!(ts->signal_sent & ISCSI_SIGNAL_RX_THREAD)) {
  282. send_sig(SIGINT, ts->rx_thread, 1);
  283. ts->signal_sent |= ISCSI_SIGNAL_RX_THREAD;
  284. }
  285. ts->blocked_threads |= ISCSI_BLOCK_RX_THREAD;
  286. spin_unlock_bh(&ts->ts_state_lock);
  287. wait_for_completion(&ts->rx_restart_comp);
  288. spin_lock_bh(&ts->ts_state_lock);
  289. ts->blocked_threads &= ~ISCSI_BLOCK_RX_THREAD;
  290. }
  291. if (ts->tx_thread && (thread_called == ISCSI_RX_THREAD) &&
  292. (ts->thread_clear & ISCSI_CLEAR_TX_THREAD)) {
  293. if (!(ts->signal_sent & ISCSI_SIGNAL_TX_THREAD)) {
  294. send_sig(SIGINT, ts->tx_thread, 1);
  295. ts->signal_sent |= ISCSI_SIGNAL_TX_THREAD;
  296. }
  297. ts->blocked_threads |= ISCSI_BLOCK_TX_THREAD;
  298. spin_unlock_bh(&ts->ts_state_lock);
  299. wait_for_completion(&ts->tx_restart_comp);
  300. spin_lock_bh(&ts->ts_state_lock);
  301. ts->blocked_threads &= ~ISCSI_BLOCK_TX_THREAD;
  302. }
  303. ts->conn = NULL;
  304. ts->status = ISCSI_THREAD_SET_FREE;
  305. spin_unlock_bh(&ts->ts_state_lock);
  306. return 0;
  307. }
  308. int iscsi_thread_set_force_reinstatement(struct iscsi_conn *conn)
  309. {
  310. struct iscsi_thread_set *ts;
  311. if (!conn->thread_set)
  312. return -1;
  313. ts = conn->thread_set;
  314. spin_lock_bh(&ts->ts_state_lock);
  315. if (ts->status != ISCSI_THREAD_SET_ACTIVE) {
  316. spin_unlock_bh(&ts->ts_state_lock);
  317. return -1;
  318. }
  319. if (ts->tx_thread && (!(ts->signal_sent & ISCSI_SIGNAL_TX_THREAD))) {
  320. send_sig(SIGINT, ts->tx_thread, 1);
  321. ts->signal_sent |= ISCSI_SIGNAL_TX_THREAD;
  322. }
  323. if (ts->rx_thread && (!(ts->signal_sent & ISCSI_SIGNAL_RX_THREAD))) {
  324. send_sig(SIGINT, ts->rx_thread, 1);
  325. ts->signal_sent |= ISCSI_SIGNAL_RX_THREAD;
  326. }
  327. spin_unlock_bh(&ts->ts_state_lock);
  328. return 0;
  329. }
  330. static void iscsi_check_to_add_additional_sets(void)
  331. {
  332. int thread_sets_add;
  333. spin_lock(&inactive_ts_lock);
  334. thread_sets_add = iscsit_global->inactive_ts;
  335. spin_unlock(&inactive_ts_lock);
  336. if (thread_sets_add == 1)
  337. iscsi_allocate_thread_sets(1);
  338. }
  339. static int iscsi_signal_thread_pre_handler(struct iscsi_thread_set *ts)
  340. {
  341. spin_lock_bh(&ts->ts_state_lock);
  342. if ((ts->status == ISCSI_THREAD_SET_DIE) || signal_pending(current)) {
  343. spin_unlock_bh(&ts->ts_state_lock);
  344. return -1;
  345. }
  346. spin_unlock_bh(&ts->ts_state_lock);
  347. return 0;
  348. }
  349. struct iscsi_conn *iscsi_rx_thread_pre_handler(struct iscsi_thread_set *ts)
  350. {
  351. int ret;
  352. spin_lock_bh(&ts->ts_state_lock);
  353. if (ts->create_threads) {
  354. spin_unlock_bh(&ts->ts_state_lock);
  355. goto sleep;
  356. }
  357. flush_signals(current);
  358. if (ts->delay_inactive && (--ts->thread_count == 0)) {
  359. spin_unlock_bh(&ts->ts_state_lock);
  360. iscsi_del_ts_from_active_list(ts);
  361. if (!iscsit_global->in_shutdown)
  362. iscsi_deallocate_extra_thread_sets();
  363. iscsi_add_ts_to_inactive_list(ts);
  364. spin_lock_bh(&ts->ts_state_lock);
  365. }
  366. if ((ts->status == ISCSI_THREAD_SET_RESET) &&
  367. (ts->thread_clear & ISCSI_CLEAR_RX_THREAD))
  368. complete(&ts->rx_restart_comp);
  369. ts->thread_clear &= ~ISCSI_CLEAR_RX_THREAD;
  370. spin_unlock_bh(&ts->ts_state_lock);
  371. sleep:
  372. ret = wait_for_completion_interruptible(&ts->rx_start_comp);
  373. if (ret != 0)
  374. return NULL;
  375. if (iscsi_signal_thread_pre_handler(ts) < 0)
  376. return NULL;
  377. if (!ts->conn) {
  378. pr_err("struct iscsi_thread_set->conn is NULL for"
  379. " thread_id: %d, going back to sleep\n", ts->thread_id);
  380. goto sleep;
  381. }
  382. iscsi_check_to_add_additional_sets();
  383. /*
  384. * The RX Thread starts up the TX Thread and sleeps.
  385. */
  386. ts->thread_clear |= ISCSI_CLEAR_RX_THREAD;
  387. complete(&ts->tx_start_comp);
  388. wait_for_completion(&ts->tx_post_start_comp);
  389. return ts->conn;
  390. }
  391. struct iscsi_conn *iscsi_tx_thread_pre_handler(struct iscsi_thread_set *ts)
  392. {
  393. int ret;
  394. spin_lock_bh(&ts->ts_state_lock);
  395. if (ts->create_threads) {
  396. spin_unlock_bh(&ts->ts_state_lock);
  397. goto sleep;
  398. }
  399. flush_signals(current);
  400. if (ts->delay_inactive && (--ts->thread_count == 0)) {
  401. spin_unlock_bh(&ts->ts_state_lock);
  402. iscsi_del_ts_from_active_list(ts);
  403. if (!iscsit_global->in_shutdown)
  404. iscsi_deallocate_extra_thread_sets();
  405. iscsi_add_ts_to_inactive_list(ts);
  406. spin_lock_bh(&ts->ts_state_lock);
  407. }
  408. if ((ts->status == ISCSI_THREAD_SET_RESET) &&
  409. (ts->thread_clear & ISCSI_CLEAR_TX_THREAD))
  410. complete(&ts->tx_restart_comp);
  411. ts->thread_clear &= ~ISCSI_CLEAR_TX_THREAD;
  412. spin_unlock_bh(&ts->ts_state_lock);
  413. sleep:
  414. ret = wait_for_completion_interruptible(&ts->tx_start_comp);
  415. if (ret != 0)
  416. return NULL;
  417. if (iscsi_signal_thread_pre_handler(ts) < 0)
  418. return NULL;
  419. if (!ts->conn) {
  420. pr_err("struct iscsi_thread_set->conn is NULL for "
  421. " thread_id: %d, going back to sleep\n",
  422. ts->thread_id);
  423. goto sleep;
  424. }
  425. iscsi_check_to_add_additional_sets();
  426. /*
  427. * From the TX thread, up the tx_post_start_comp that the RX Thread is
  428. * sleeping on in iscsi_rx_thread_pre_handler(), then up the
  429. * rx_post_start_comp that iscsi_activate_thread_set() is sleeping on.
  430. */
  431. ts->thread_clear |= ISCSI_CLEAR_TX_THREAD;
  432. complete(&ts->tx_post_start_comp);
  433. complete(&ts->rx_post_start_comp);
  434. spin_lock_bh(&ts->ts_state_lock);
  435. ts->status = ISCSI_THREAD_SET_ACTIVE;
  436. spin_unlock_bh(&ts->ts_state_lock);
  437. return ts->conn;
  438. }
  439. int iscsi_thread_set_init(void)
  440. {
  441. int size;
  442. iscsit_global->ts_bitmap_count = ISCSI_TS_BITMAP_BITS;
  443. size = BITS_TO_LONGS(iscsit_global->ts_bitmap_count) * sizeof(long);
  444. iscsit_global->ts_bitmap = kzalloc(size, GFP_KERNEL);
  445. if (!iscsit_global->ts_bitmap) {
  446. pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
  447. return -ENOMEM;
  448. }
  449. spin_lock_init(&active_ts_lock);
  450. spin_lock_init(&inactive_ts_lock);
  451. spin_lock_init(&ts_bitmap_lock);
  452. INIT_LIST_HEAD(&active_ts_list);
  453. INIT_LIST_HEAD(&inactive_ts_list);
  454. return 0;
  455. }
  456. void iscsi_thread_set_free(void)
  457. {
  458. kfree(iscsit_global->ts_bitmap);
  459. }