iscsi_target_tq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*******************************************************************************
  2. * This file contains the iSCSI Login Thread and Thread Queue functions.
  3. *
  4. * (c) Copyright 2007-2013 Datera, Inc.
  5. *
  6. * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
  7. *
  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.
  12. *
  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. ******************************************************************************/
  18. #include <linux/kthread.h>
  19. #include <linux/list.h>
  20. #include <linux/bitmap.h>
  21. #include "iscsi_target_core.h"
  22. #include "iscsi_target_tq.h"
  23. #include "iscsi_target.h"
  24. static LIST_HEAD(inactive_ts_list);
  25. static DEFINE_SPINLOCK(inactive_ts_lock);
  26. static DEFINE_SPINLOCK(ts_bitmap_lock);
  27. static void iscsi_add_ts_to_inactive_list(struct iscsi_thread_set *ts)
  28. {
  29. if (!list_empty(&ts->ts_list)) {
  30. WARN_ON(1);
  31. return;
  32. }
  33. spin_lock(&inactive_ts_lock);
  34. list_add_tail(&ts->ts_list, &inactive_ts_list);
  35. iscsit_global->inactive_ts++;
  36. spin_unlock(&inactive_ts_lock);
  37. }
  38. static struct iscsi_thread_set *iscsi_get_ts_from_inactive_list(void)
  39. {
  40. struct iscsi_thread_set *ts;
  41. spin_lock(&inactive_ts_lock);
  42. if (list_empty(&inactive_ts_list)) {
  43. spin_unlock(&inactive_ts_lock);
  44. return NULL;
  45. }
  46. ts = list_first_entry(&inactive_ts_list, struct iscsi_thread_set, ts_list);
  47. list_del_init(&ts->ts_list);
  48. iscsit_global->inactive_ts--;
  49. spin_unlock(&inactive_ts_lock);
  50. return ts;
  51. }
  52. int iscsi_allocate_thread_sets(u32 thread_pair_count)
  53. {
  54. int allocated_thread_pair_count = 0, i, thread_id;
  55. struct iscsi_thread_set *ts = NULL;
  56. for (i = 0; i < thread_pair_count; i++) {
  57. ts = kzalloc(sizeof(struct iscsi_thread_set), GFP_KERNEL);
  58. if (!ts) {
  59. pr_err("Unable to allocate memory for"
  60. " thread set.\n");
  61. return allocated_thread_pair_count;
  62. }
  63. /*
  64. * Locate the next available regision in the thread_set_bitmap
  65. */
  66. spin_lock(&ts_bitmap_lock);
  67. thread_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
  68. iscsit_global->ts_bitmap_count, get_order(1));
  69. spin_unlock(&ts_bitmap_lock);
  70. if (thread_id < 0) {
  71. pr_err("bitmap_find_free_region() failed for"
  72. " thread_set_bitmap\n");
  73. kfree(ts);
  74. return allocated_thread_pair_count;
  75. }
  76. ts->thread_id = thread_id;
  77. ts->status = ISCSI_THREAD_SET_FREE;
  78. INIT_LIST_HEAD(&ts->ts_list);
  79. spin_lock_init(&ts->ts_state_lock);
  80. init_completion(&ts->rx_restart_comp);
  81. init_completion(&ts->tx_restart_comp);
  82. init_completion(&ts->rx_start_comp);
  83. init_completion(&ts->tx_start_comp);
  84. sema_init(&ts->ts_activate_sem, 0);
  85. ts->create_threads = 1;
  86. ts->tx_thread = kthread_run(iscsi_target_tx_thread, ts, "%s",
  87. ISCSI_TX_THREAD_NAME);
  88. if (IS_ERR(ts->tx_thread)) {
  89. dump_stack();
  90. pr_err("Unable to start iscsi_target_tx_thread\n");
  91. break;
  92. }
  93. ts->rx_thread = kthread_run(iscsi_target_rx_thread, ts, "%s",
  94. ISCSI_RX_THREAD_NAME);
  95. if (IS_ERR(ts->rx_thread)) {
  96. kthread_stop(ts->tx_thread);
  97. pr_err("Unable to start iscsi_target_rx_thread\n");
  98. break;
  99. }
  100. ts->create_threads = 0;
  101. iscsi_add_ts_to_inactive_list(ts);
  102. allocated_thread_pair_count++;
  103. }
  104. pr_debug("Spawned %d thread set(s) (%d total threads).\n",
  105. allocated_thread_pair_count, allocated_thread_pair_count * 2);
  106. return allocated_thread_pair_count;
  107. }
  108. static void iscsi_deallocate_thread_one(struct iscsi_thread_set *ts)
  109. {
  110. spin_lock_bh(&ts->ts_state_lock);
  111. ts->status = ISCSI_THREAD_SET_DIE;
  112. if (ts->rx_thread) {
  113. complete(&ts->rx_start_comp);
  114. spin_unlock_bh(&ts->ts_state_lock);
  115. kthread_stop(ts->rx_thread);
  116. spin_lock_bh(&ts->ts_state_lock);
  117. }
  118. if (ts->tx_thread) {
  119. complete(&ts->tx_start_comp);
  120. spin_unlock_bh(&ts->ts_state_lock);
  121. kthread_stop(ts->tx_thread);
  122. spin_lock_bh(&ts->ts_state_lock);
  123. }
  124. spin_unlock_bh(&ts->ts_state_lock);
  125. /*
  126. * Release this thread_id in the thread_set_bitmap
  127. */
  128. spin_lock(&ts_bitmap_lock);
  129. bitmap_release_region(iscsit_global->ts_bitmap,
  130. ts->thread_id, get_order(1));
  131. spin_unlock(&ts_bitmap_lock);
  132. kfree(ts);
  133. }
  134. void iscsi_deallocate_thread_sets(void)
  135. {
  136. struct iscsi_thread_set *ts = NULL;
  137. u32 released_count = 0;
  138. while ((ts = iscsi_get_ts_from_inactive_list())) {
  139. iscsi_deallocate_thread_one(ts);
  140. released_count++;
  141. }
  142. if (released_count)
  143. pr_debug("Stopped %d thread set(s) (%d total threads)."
  144. "\n", released_count, released_count * 2);
  145. }
  146. static void iscsi_deallocate_extra_thread_sets(void)
  147. {
  148. u32 orig_count, released_count = 0;
  149. struct iscsi_thread_set *ts = NULL;
  150. orig_count = TARGET_THREAD_SET_COUNT;
  151. while ((iscsit_global->inactive_ts + 1) > orig_count) {
  152. ts = iscsi_get_ts_from_inactive_list();
  153. if (!ts)
  154. break;
  155. iscsi_deallocate_thread_one(ts);
  156. released_count++;
  157. }
  158. if (released_count)
  159. pr_debug("Stopped %d thread set(s) (%d total threads)."
  160. "\n", released_count, released_count * 2);
  161. }
  162. void iscsi_activate_thread_set(struct iscsi_conn *conn, struct iscsi_thread_set *ts)
  163. {
  164. spin_lock_bh(&ts->ts_state_lock);
  165. conn->thread_set = ts;
  166. ts->conn = conn;
  167. ts->status = ISCSI_THREAD_SET_ACTIVE;
  168. spin_unlock_bh(&ts->ts_state_lock);
  169. complete(&ts->rx_start_comp);
  170. complete(&ts->tx_start_comp);
  171. down(&ts->ts_activate_sem);
  172. }
  173. struct iscsi_thread_set *iscsi_get_thread_set(void)
  174. {
  175. struct iscsi_thread_set *ts;
  176. get_set:
  177. ts = iscsi_get_ts_from_inactive_list();
  178. if (!ts) {
  179. iscsi_allocate_thread_sets(1);
  180. goto get_set;
  181. }
  182. ts->delay_inactive = 1;
  183. ts->signal_sent = 0;
  184. ts->thread_count = 2;
  185. init_completion(&ts->rx_restart_comp);
  186. init_completion(&ts->tx_restart_comp);
  187. sema_init(&ts->ts_activate_sem, 0);
  188. return ts;
  189. }
  190. void iscsi_set_thread_clear(struct iscsi_conn *conn, u8 thread_clear)
  191. {
  192. struct iscsi_thread_set *ts = NULL;
  193. if (!conn->thread_set) {
  194. pr_err("struct iscsi_conn->thread_set is NULL\n");
  195. return;
  196. }
  197. ts = conn->thread_set;
  198. spin_lock_bh(&ts->ts_state_lock);
  199. ts->thread_clear &= ~thread_clear;
  200. if ((thread_clear & ISCSI_CLEAR_RX_THREAD) &&
  201. (ts->blocked_threads & ISCSI_BLOCK_RX_THREAD))
  202. complete(&ts->rx_restart_comp);
  203. else if ((thread_clear & ISCSI_CLEAR_TX_THREAD) &&
  204. (ts->blocked_threads & ISCSI_BLOCK_TX_THREAD))
  205. complete(&ts->tx_restart_comp);
  206. spin_unlock_bh(&ts->ts_state_lock);
  207. }
  208. void iscsi_set_thread_set_signal(struct iscsi_conn *conn, u8 signal_sent)
  209. {
  210. struct iscsi_thread_set *ts = NULL;
  211. if (!conn->thread_set) {
  212. pr_err("struct iscsi_conn->thread_set is NULL\n");
  213. return;
  214. }
  215. ts = conn->thread_set;
  216. spin_lock_bh(&ts->ts_state_lock);
  217. ts->signal_sent |= signal_sent;
  218. spin_unlock_bh(&ts->ts_state_lock);
  219. }
  220. int iscsi_release_thread_set(struct iscsi_conn *conn)
  221. {
  222. int thread_called = 0;
  223. struct iscsi_thread_set *ts = NULL;
  224. if (!conn || !conn->thread_set) {
  225. pr_err("connection or thread set pointer is NULL\n");
  226. BUG();
  227. }
  228. ts = conn->thread_set;
  229. spin_lock_bh(&ts->ts_state_lock);
  230. ts->status = ISCSI_THREAD_SET_RESET;
  231. if (!strncmp(current->comm, ISCSI_RX_THREAD_NAME,
  232. strlen(ISCSI_RX_THREAD_NAME)))
  233. thread_called = ISCSI_RX_THREAD;
  234. else if (!strncmp(current->comm, ISCSI_TX_THREAD_NAME,
  235. strlen(ISCSI_TX_THREAD_NAME)))
  236. thread_called = ISCSI_TX_THREAD;
  237. if (ts->rx_thread && (thread_called == ISCSI_TX_THREAD) &&
  238. (ts->thread_clear & ISCSI_CLEAR_RX_THREAD)) {
  239. if (!(ts->signal_sent & ISCSI_SIGNAL_RX_THREAD)) {
  240. send_sig(SIGINT, ts->rx_thread, 1);
  241. ts->signal_sent |= ISCSI_SIGNAL_RX_THREAD;
  242. }
  243. ts->blocked_threads |= ISCSI_BLOCK_RX_THREAD;
  244. spin_unlock_bh(&ts->ts_state_lock);
  245. wait_for_completion(&ts->rx_restart_comp);
  246. spin_lock_bh(&ts->ts_state_lock);
  247. ts->blocked_threads &= ~ISCSI_BLOCK_RX_THREAD;
  248. }
  249. if (ts->tx_thread && (thread_called == ISCSI_RX_THREAD) &&
  250. (ts->thread_clear & ISCSI_CLEAR_TX_THREAD)) {
  251. if (!(ts->signal_sent & ISCSI_SIGNAL_TX_THREAD)) {
  252. send_sig(SIGINT, ts->tx_thread, 1);
  253. ts->signal_sent |= ISCSI_SIGNAL_TX_THREAD;
  254. }
  255. ts->blocked_threads |= ISCSI_BLOCK_TX_THREAD;
  256. spin_unlock_bh(&ts->ts_state_lock);
  257. wait_for_completion(&ts->tx_restart_comp);
  258. spin_lock_bh(&ts->ts_state_lock);
  259. ts->blocked_threads &= ~ISCSI_BLOCK_TX_THREAD;
  260. }
  261. ts->conn = NULL;
  262. ts->status = ISCSI_THREAD_SET_FREE;
  263. spin_unlock_bh(&ts->ts_state_lock);
  264. return 0;
  265. }
  266. int iscsi_thread_set_force_reinstatement(struct iscsi_conn *conn)
  267. {
  268. struct iscsi_thread_set *ts;
  269. if (!conn->thread_set)
  270. return -1;
  271. ts = conn->thread_set;
  272. spin_lock_bh(&ts->ts_state_lock);
  273. if (ts->status != ISCSI_THREAD_SET_ACTIVE) {
  274. spin_unlock_bh(&ts->ts_state_lock);
  275. return -1;
  276. }
  277. if (ts->tx_thread && (!(ts->signal_sent & ISCSI_SIGNAL_TX_THREAD))) {
  278. send_sig(SIGINT, ts->tx_thread, 1);
  279. ts->signal_sent |= ISCSI_SIGNAL_TX_THREAD;
  280. }
  281. if (ts->rx_thread && (!(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. spin_unlock_bh(&ts->ts_state_lock);
  286. return 0;
  287. }
  288. static void iscsi_check_to_add_additional_sets(void)
  289. {
  290. int thread_sets_add;
  291. spin_lock(&inactive_ts_lock);
  292. thread_sets_add = iscsit_global->inactive_ts;
  293. spin_unlock(&inactive_ts_lock);
  294. if (thread_sets_add == 1)
  295. iscsi_allocate_thread_sets(1);
  296. }
  297. static int iscsi_signal_thread_pre_handler(struct iscsi_thread_set *ts)
  298. {
  299. spin_lock_bh(&ts->ts_state_lock);
  300. if (ts->status == ISCSI_THREAD_SET_DIE || kthread_should_stop() ||
  301. signal_pending(current)) {
  302. spin_unlock_bh(&ts->ts_state_lock);
  303. return -1;
  304. }
  305. spin_unlock_bh(&ts->ts_state_lock);
  306. return 0;
  307. }
  308. struct iscsi_conn *iscsi_rx_thread_pre_handler(struct iscsi_thread_set *ts)
  309. {
  310. int ret;
  311. spin_lock_bh(&ts->ts_state_lock);
  312. if (ts->create_threads) {
  313. spin_unlock_bh(&ts->ts_state_lock);
  314. goto sleep;
  315. }
  316. if (ts->status != ISCSI_THREAD_SET_DIE)
  317. flush_signals(current);
  318. if (ts->delay_inactive && (--ts->thread_count == 0)) {
  319. spin_unlock_bh(&ts->ts_state_lock);
  320. if (!iscsit_global->in_shutdown)
  321. iscsi_deallocate_extra_thread_sets();
  322. iscsi_add_ts_to_inactive_list(ts);
  323. spin_lock_bh(&ts->ts_state_lock);
  324. }
  325. if ((ts->status == ISCSI_THREAD_SET_RESET) &&
  326. (ts->thread_clear & ISCSI_CLEAR_RX_THREAD))
  327. complete(&ts->rx_restart_comp);
  328. ts->thread_clear &= ~ISCSI_CLEAR_RX_THREAD;
  329. spin_unlock_bh(&ts->ts_state_lock);
  330. sleep:
  331. ret = wait_for_completion_interruptible(&ts->rx_start_comp);
  332. if (ret != 0)
  333. return NULL;
  334. if (iscsi_signal_thread_pre_handler(ts) < 0)
  335. return NULL;
  336. iscsi_check_to_add_additional_sets();
  337. spin_lock_bh(&ts->ts_state_lock);
  338. if (!ts->conn) {
  339. pr_err("struct iscsi_thread_set->conn is NULL for"
  340. " RX thread_id: %s/%d\n", current->comm, current->pid);
  341. spin_unlock_bh(&ts->ts_state_lock);
  342. return NULL;
  343. }
  344. ts->thread_clear |= ISCSI_CLEAR_RX_THREAD;
  345. spin_unlock_bh(&ts->ts_state_lock);
  346. up(&ts->ts_activate_sem);
  347. return ts->conn;
  348. }
  349. struct iscsi_conn *iscsi_tx_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. if (ts->status != ISCSI_THREAD_SET_DIE)
  358. flush_signals(current);
  359. if (ts->delay_inactive && (--ts->thread_count == 0)) {
  360. spin_unlock_bh(&ts->ts_state_lock);
  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_TX_THREAD))
  368. complete(&ts->tx_restart_comp);
  369. ts->thread_clear &= ~ISCSI_CLEAR_TX_THREAD;
  370. spin_unlock_bh(&ts->ts_state_lock);
  371. sleep:
  372. ret = wait_for_completion_interruptible(&ts->tx_start_comp);
  373. if (ret != 0)
  374. return NULL;
  375. if (iscsi_signal_thread_pre_handler(ts) < 0)
  376. return NULL;
  377. iscsi_check_to_add_additional_sets();
  378. spin_lock_bh(&ts->ts_state_lock);
  379. if (!ts->conn) {
  380. pr_err("struct iscsi_thread_set->conn is NULL for"
  381. " TX thread_id: %s/%d\n", current->comm, current->pid);
  382. spin_unlock_bh(&ts->ts_state_lock);
  383. return NULL;
  384. }
  385. ts->thread_clear |= ISCSI_CLEAR_TX_THREAD;
  386. spin_unlock_bh(&ts->ts_state_lock);
  387. up(&ts->ts_activate_sem);
  388. return ts->conn;
  389. }
  390. int iscsi_thread_set_init(void)
  391. {
  392. int size;
  393. iscsit_global->ts_bitmap_count = ISCSI_TS_BITMAP_BITS;
  394. size = BITS_TO_LONGS(iscsit_global->ts_bitmap_count) * sizeof(long);
  395. iscsit_global->ts_bitmap = kzalloc(size, GFP_KERNEL);
  396. if (!iscsit_global->ts_bitmap) {
  397. pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
  398. return -ENOMEM;
  399. }
  400. return 0;
  401. }
  402. void iscsi_thread_set_free(void)
  403. {
  404. kfree(iscsit_global->ts_bitmap);
  405. }