iscsi_target_tq.c 14 KB

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