iscsi_target_tq.c 13 KB

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