iscsi_target_tq.c 13 KB

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