iscsi_target_tq.c 13 KB

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