summaryrefslogtreecommitdiffstats
path: root/drivers/target
diff options
context:
space:
mode:
authorMike Christie <mchristi@redhat.com>2017-11-28 13:40:39 -0500
committerNicholas Bellinger <nab@linux-iscsi.org>2018-01-12 18:07:19 -0500
commitaf1dd7ff46824a94da1d90443bd07db2796bd545 (patch)
tree8738bc72ba01cad344c8917ba86472d0a3c2b09a /drivers/target
parentf890f5799a6628fe006ae524e625900186074cdb (diff)
tcmu: don't block submitting context for block waits
This patch has tcmu internally queue cmds if its ring buffer is full. It also makes the TCMU_GLOBAL_MAX_BLOCKS limit a hint instead of a hard limit, so we do not have to add any new locks/atomics in the main IO path except when IO is not running. This fixes the following bugs: 1. We cannot sleep from the submitting context because it might be called from a target recv context. This results in transport level commands timing out. For example if the ring is full, we would sleep, and a iscsi initiator would send a iscsi ping/nop which times out because the target's recv thread is sleeping here. 2. Devices were not fairly scheduled to run when they hit the global limit so they could time out waiting for ring space while others got run. Signed-off-by: Mike Christie <mchristi@redhat.com> Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>
Diffstat (limited to 'drivers/target')
-rw-r--r--drivers/target/target_core_user.c264
1 files changed, 169 insertions, 95 deletions
diff --git a/drivers/target/target_core_user.c b/drivers/target/target_core_user.c
index 2679e4dcd0f1..52fc1d440d23 100644
--- a/drivers/target/target_core_user.c
+++ b/drivers/target/target_core_user.c
@@ -83,7 +83,10 @@
83/* The total size of the ring is 8M + 256K * PAGE_SIZE */ 83/* The total size of the ring is 8M + 256K * PAGE_SIZE */
84#define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE) 84#define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
85 85
86/* Default maximum of the global data blocks(512K * PAGE_SIZE) */ 86/*
87 * Default number of global data blocks(512K * PAGE_SIZE)
88 * when the unmap thread will be started.
89 */
87#define TCMU_GLOBAL_MAX_BLOCKS (512 * 1024) 90#define TCMU_GLOBAL_MAX_BLOCKS (512 * 1024)
88 91
89static u8 tcmu_kern_cmd_reply_supported; 92static u8 tcmu_kern_cmd_reply_supported;
@@ -106,6 +109,7 @@ struct tcmu_nl_cmd {
106struct tcmu_dev { 109struct tcmu_dev {
107 struct list_head node; 110 struct list_head node;
108 struct kref kref; 111 struct kref kref;
112
109 struct se_device se_dev; 113 struct se_device se_dev;
110 114
111 char *name; 115 char *name;
@@ -128,10 +132,9 @@ struct tcmu_dev {
128 size_t data_off; 132 size_t data_off;
129 size_t data_size; 133 size_t data_size;
130 134
131 wait_queue_head_t wait_cmdr;
132 struct mutex cmdr_lock; 135 struct mutex cmdr_lock;
136 struct list_head cmdr_queue;
133 137
134 bool waiting_global;
135 uint32_t dbi_max; 138 uint32_t dbi_max;
136 uint32_t dbi_thresh; 139 uint32_t dbi_thresh;
137 DECLARE_BITMAP(data_bitmap, DATA_BLOCK_BITS); 140 DECLARE_BITMAP(data_bitmap, DATA_BLOCK_BITS);
@@ -160,6 +163,7 @@ struct tcmu_dev {
160struct tcmu_cmd { 163struct tcmu_cmd {
161 struct se_cmd *se_cmd; 164 struct se_cmd *se_cmd;
162 struct tcmu_dev *tcmu_dev; 165 struct tcmu_dev *tcmu_dev;
166 struct list_head cmdr_queue_entry;
163 167
164 uint16_t cmd_id; 168 uint16_t cmd_id;
165 169
@@ -174,7 +178,16 @@ struct tcmu_cmd {
174#define TCMU_CMD_BIT_EXPIRED 0 178#define TCMU_CMD_BIT_EXPIRED 0
175 unsigned long flags; 179 unsigned long flags;
176}; 180};
177 181/*
182 * To avoid dead lock the mutex lock order should always be:
183 *
184 * mutex_lock(&root_udev_mutex);
185 * ...
186 * mutex_lock(&tcmu_dev->cmdr_lock);
187 * mutex_unlock(&tcmu_dev->cmdr_lock);
188 * ...
189 * mutex_unlock(&root_udev_mutex);
190 */
178static DEFINE_MUTEX(root_udev_mutex); 191static DEFINE_MUTEX(root_udev_mutex);
179static LIST_HEAD(root_udev); 192static LIST_HEAD(root_udev);
180 193
@@ -182,7 +195,7 @@ static DEFINE_SPINLOCK(timed_out_udevs_lock);
182static LIST_HEAD(timed_out_udevs); 195static LIST_HEAD(timed_out_udevs);
183 196
184static atomic_t global_db_count = ATOMIC_INIT(0); 197static atomic_t global_db_count = ATOMIC_INIT(0);
185static struct work_struct tcmu_unmap_work; 198static struct delayed_work tcmu_unmap_work;
186 199
187static struct kmem_cache *tcmu_cmd_cache; 200static struct kmem_cache *tcmu_cmd_cache;
188 201
@@ -346,10 +359,8 @@ static inline bool tcmu_get_empty_block(struct tcmu_dev *udev,
346 page = radix_tree_lookup(&udev->data_blocks, dbi); 359 page = radix_tree_lookup(&udev->data_blocks, dbi);
347 if (!page) { 360 if (!page) {
348 if (atomic_add_return(1, &global_db_count) > 361 if (atomic_add_return(1, &global_db_count) >
349 TCMU_GLOBAL_MAX_BLOCKS) { 362 TCMU_GLOBAL_MAX_BLOCKS)
350 atomic_dec(&global_db_count); 363 schedule_delayed_work(&tcmu_unmap_work, 0);
351 return false;
352 }
353 364
354 /* try to get new page from the mm */ 365 /* try to get new page from the mm */
355 page = alloc_page(GFP_KERNEL); 366 page = alloc_page(GFP_KERNEL);
@@ -380,18 +391,11 @@ static bool tcmu_get_empty_blocks(struct tcmu_dev *udev,
380{ 391{
381 int i; 392 int i;
382 393
383 udev->waiting_global = false;
384
385 for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) { 394 for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) {
386 if (!tcmu_get_empty_block(udev, tcmu_cmd)) 395 if (!tcmu_get_empty_block(udev, tcmu_cmd))
387 goto err; 396 return false;
388 } 397 }
389 return true; 398 return true;
390
391err:
392 udev->waiting_global = true;
393 schedule_work(&tcmu_unmap_work);
394 return false;
395} 399}
396 400
397static inline struct page * 401static inline struct page *
@@ -437,6 +441,7 @@ static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
437 if (!tcmu_cmd) 441 if (!tcmu_cmd)
438 return NULL; 442 return NULL;
439 443
444 INIT_LIST_HEAD(&tcmu_cmd->cmdr_queue_entry);
440 tcmu_cmd->se_cmd = se_cmd; 445 tcmu_cmd->se_cmd = se_cmd;
441 tcmu_cmd->tcmu_dev = udev; 446 tcmu_cmd->tcmu_dev = udev;
442 447
@@ -742,6 +747,10 @@ static int tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd)
742 unsigned long tmo = udev->cmd_time_out; 747 unsigned long tmo = udev->cmd_time_out;
743 int cmd_id; 748 int cmd_id;
744 749
750 /*
751 * If it was on the cmdr queue waiting we do not reset the timer
752 * for requeues and when it is finally sent to userspace.
753 */
745 if (tcmu_cmd->cmd_id) 754 if (tcmu_cmd->cmd_id)
746 return 0; 755 return 0;
747 756
@@ -753,13 +762,31 @@ static int tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd)
753 tcmu_cmd->cmd_id = cmd_id; 762 tcmu_cmd->cmd_id = cmd_id;
754 763
755 if (!tmo) 764 if (!tmo)
756 return 0; 765 tmo = TCMU_TIME_OUT;
766
767 pr_debug("allocated cmd %u for dev %s tmo %lu\n", tcmu_cmd->cmd_id,
768 udev->name, tmo / MSEC_PER_SEC);
757 769
758 tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo)); 770 tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo));
759 mod_timer(&udev->timeout, tcmu_cmd->deadline); 771 mod_timer(&udev->timeout, tcmu_cmd->deadline);
760 return 0; 772 return 0;
761} 773}
762 774
775static int add_to_cmdr_queue(struct tcmu_cmd *tcmu_cmd)
776{
777 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
778 int ret;
779
780 ret = tcmu_setup_cmd_timer(tcmu_cmd);
781 if (ret)
782 return ret;
783
784 list_add_tail(&tcmu_cmd->cmdr_queue_entry, &udev->cmdr_queue);
785 pr_debug("adding cmd %u on dev %s to ring space wait queue\n",
786 tcmu_cmd->cmd_id, udev->name);
787 return 0;
788}
789
763/** 790/**
764 * queue_cmd_ring - queue cmd to ring or internally 791 * queue_cmd_ring - queue cmd to ring or internally
765 * @tcmu_cmd: cmd to queue 792 * @tcmu_cmd: cmd to queue
@@ -768,6 +795,7 @@ static int tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd)
768 * Returns: 795 * Returns:
769 * -1 we cannot queue internally or to the ring. 796 * -1 we cannot queue internally or to the ring.
770 * 0 success 797 * 0 success
798 * 1 internally queued to wait for ring memory to free.
771 */ 799 */
772static sense_reason_t queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, int *scsi_err) 800static sense_reason_t queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, int *scsi_err)
773{ 801{
@@ -805,7 +833,8 @@ static sense_reason_t queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, int *scsi_err)
805 base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt); 833 base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt);
806 command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size); 834 command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
807 835
808 mutex_lock(&udev->cmdr_lock); 836 if (!list_empty(&udev->cmdr_queue))
837 goto queue;
809 838
810 mb = udev->mb_addr; 839 mb = udev->mb_addr;
811 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 840 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
@@ -814,42 +843,18 @@ static sense_reason_t queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, int *scsi_err)
814 pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu " 843 pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu "
815 "cmd ring/data area\n", command_size, data_length, 844 "cmd ring/data area\n", command_size, data_length,
816 udev->cmdr_size, udev->data_size); 845 udev->cmdr_size, udev->data_size);
817 mutex_unlock(&udev->cmdr_lock);
818 *scsi_err = TCM_INVALID_CDB_FIELD; 846 *scsi_err = TCM_INVALID_CDB_FIELD;
819 return -1; 847 return -1;
820 } 848 }
821 849
822 while (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) { 850 if (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) {
823 int ret;
824 DEFINE_WAIT(__wait);
825
826 /* 851 /*
827 * Don't leave commands partially setup because the unmap 852 * Don't leave commands partially setup because the unmap
828 * thread might need the blocks to make forward progress. 853 * thread might need the blocks to make forward progress.
829 */ 854 */
830 tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur); 855 tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur);
831 tcmu_cmd_reset_dbi_cur(tcmu_cmd); 856 tcmu_cmd_reset_dbi_cur(tcmu_cmd);
832 857 goto queue;
833 prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE);
834
835 pr_debug("sleeping for ring space\n");
836 mutex_unlock(&udev->cmdr_lock);
837 if (udev->cmd_time_out)
838 ret = schedule_timeout(
839 msecs_to_jiffies(udev->cmd_time_out));
840 else
841 ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT));
842 finish_wait(&udev->wait_cmdr, &__wait);
843 if (!ret) {
844 pr_warn("tcmu: command timed out\n");
845 *scsi_err = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
846 return -1;
847 }
848
849 mutex_lock(&udev->cmdr_lock);
850
851 /* We dropped cmdr_lock, cmd_head is stale */
852 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
853 } 858 }
854 859
855 /* Insert a PAD if end-of-ring space is too small */ 860 /* Insert a PAD if end-of-ring space is too small */
@@ -924,31 +929,39 @@ static sense_reason_t queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, int *scsi_err)
924 929
925 UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size); 930 UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
926 tcmu_flush_dcache_range(mb, sizeof(*mb)); 931 tcmu_flush_dcache_range(mb, sizeof(*mb));
927 mutex_unlock(&udev->cmdr_lock);
928 932
929 /* TODO: only if FLUSH and FUA? */ 933 /* TODO: only if FLUSH and FUA? */
930 uio_event_notify(&udev->uio_info); 934 uio_event_notify(&udev->uio_info);
931 935
932 if (udev->cmd_time_out)
933 mod_timer(&udev->timeout, round_jiffies_up(jiffies +
934 msecs_to_jiffies(udev->cmd_time_out)));
935
936 return 0; 936 return 0;
937
938queue:
939 if (add_to_cmdr_queue(tcmu_cmd)) {
940 *scsi_err = TCM_OUT_OF_RESOURCES;
941 return -1;
942 }
943
944 return 1;
937} 945}
938 946
939static sense_reason_t 947static sense_reason_t
940tcmu_queue_cmd(struct se_cmd *se_cmd) 948tcmu_queue_cmd(struct se_cmd *se_cmd)
941{ 949{
950 struct se_device *se_dev = se_cmd->se_dev;
951 struct tcmu_dev *udev = TCMU_DEV(se_dev);
942 struct tcmu_cmd *tcmu_cmd; 952 struct tcmu_cmd *tcmu_cmd;
943 sense_reason_t scsi_ret; 953 sense_reason_t scsi_ret;
954 int ret;
944 955
945 tcmu_cmd = tcmu_alloc_cmd(se_cmd); 956 tcmu_cmd = tcmu_alloc_cmd(se_cmd);
946 if (!tcmu_cmd) 957 if (!tcmu_cmd)
947 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 958 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
948 959
949 if (queue_cmd_ring(tcmu_cmd, &scsi_ret) < 0) 960 mutex_lock(&udev->cmdr_lock);
961 ret = queue_cmd_ring(tcmu_cmd, &scsi_ret);
962 mutex_unlock(&udev->cmdr_lock);
963 if (ret < 0)
950 tcmu_free_cmd(tcmu_cmd); 964 tcmu_free_cmd(tcmu_cmd);
951
952 return scsi_ret; 965 return scsi_ret;
953} 966}
954 967
@@ -1036,10 +1049,15 @@ static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
1036 handled++; 1049 handled++;
1037 } 1050 }
1038 1051
1039 if (mb->cmd_tail == mb->cmd_head) 1052 if (mb->cmd_tail == mb->cmd_head && list_empty(&udev->cmdr_queue)) {
1040 del_timer(&udev->timeout); /* no more pending cmds */ 1053 del_timer(&udev->timeout);
1041 1054 /*
1042 wake_up(&udev->wait_cmdr); 1055 * not more pending or waiting commands so try to reclaim
1056 * blocks if needed.
1057 */
1058 if (atomic_read(&global_db_count) > TCMU_GLOBAL_MAX_BLOCKS)
1059 schedule_delayed_work(&tcmu_unmap_work, 0);
1060 }
1043 1061
1044 return handled; 1062 return handled;
1045} 1063}
@@ -1047,6 +1065,10 @@ static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
1047static int tcmu_check_expired_cmd(int id, void *p, void *data) 1065static int tcmu_check_expired_cmd(int id, void *p, void *data)
1048{ 1066{
1049 struct tcmu_cmd *cmd = p; 1067 struct tcmu_cmd *cmd = p;
1068 struct tcmu_dev *udev = cmd->tcmu_dev;
1069 u8 scsi_status;
1070 struct se_cmd *se_cmd;
1071 bool is_running;
1050 1072
1051 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) 1073 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
1052 return 0; 1074 return 0;
@@ -1054,10 +1076,27 @@ static int tcmu_check_expired_cmd(int id, void *p, void *data)
1054 if (!time_after(jiffies, cmd->deadline)) 1076 if (!time_after(jiffies, cmd->deadline))
1055 return 0; 1077 return 0;
1056 1078
1057 set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags); 1079 is_running = list_empty(&cmd->cmdr_queue_entry);
1058 target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION); 1080 pr_debug("Timing out cmd %u on dev %s that is %s.\n",
1081 id, udev->name, is_running ? "inflight" : "queued");
1082
1083 se_cmd = cmd->se_cmd;
1059 cmd->se_cmd = NULL; 1084 cmd->se_cmd = NULL;
1060 1085
1086 if (is_running) {
1087 set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
1088 /*
1089 * target_complete_cmd will translate this to LUN COMM FAILURE
1090 */
1091 scsi_status = SAM_STAT_CHECK_CONDITION;
1092 } else {
1093 list_del_init(&cmd->cmdr_queue_entry);
1094
1095 idr_remove(&udev->commands, id);
1096 tcmu_free_cmd(cmd);
1097 scsi_status = SAM_STAT_TASK_SET_FULL;
1098 }
1099 target_complete_cmd(se_cmd, scsi_status);
1061 return 0; 1100 return 0;
1062} 1101}
1063 1102
@@ -1072,7 +1111,7 @@ static void tcmu_device_timedout(struct timer_list *t)
1072 list_add_tail(&udev->timedout_entry, &timed_out_udevs); 1111 list_add_tail(&udev->timedout_entry, &timed_out_udevs);
1073 spin_unlock(&timed_out_udevs_lock); 1112 spin_unlock(&timed_out_udevs_lock);
1074 1113
1075 schedule_work(&tcmu_unmap_work); 1114 schedule_delayed_work(&tcmu_unmap_work, 0);
1076} 1115}
1077 1116
1078static int tcmu_attach_hba(struct se_hba *hba, u32 host_id) 1117static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
@@ -1113,10 +1152,10 @@ static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
1113 udev->hba = hba; 1152 udev->hba = hba;
1114 udev->cmd_time_out = TCMU_TIME_OUT; 1153 udev->cmd_time_out = TCMU_TIME_OUT;
1115 1154
1116 init_waitqueue_head(&udev->wait_cmdr);
1117 mutex_init(&udev->cmdr_lock); 1155 mutex_init(&udev->cmdr_lock);
1118 1156
1119 INIT_LIST_HEAD(&udev->timedout_entry); 1157 INIT_LIST_HEAD(&udev->timedout_entry);
1158 INIT_LIST_HEAD(&udev->cmdr_queue);
1120 idr_init(&udev->commands); 1159 idr_init(&udev->commands);
1121 1160
1122 timer_setup(&udev->timeout, tcmu_device_timedout, 0); 1161 timer_setup(&udev->timeout, tcmu_device_timedout, 0);
@@ -1129,13 +1168,63 @@ static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
1129 return &udev->se_dev; 1168 return &udev->se_dev;
1130} 1169}
1131 1170
1171static bool run_cmdr_queue(struct tcmu_dev *udev)
1172{
1173 struct tcmu_cmd *tcmu_cmd, *tmp_cmd;
1174 LIST_HEAD(cmds);
1175 bool drained = true;
1176 sense_reason_t scsi_ret;
1177 int ret;
1178
1179 if (list_empty(&udev->cmdr_queue))
1180 return true;
1181
1182 pr_debug("running %s's cmdr queue\n", udev->name);
1183
1184 list_splice_init(&udev->cmdr_queue, &cmds);
1185
1186 list_for_each_entry_safe(tcmu_cmd, tmp_cmd, &cmds, cmdr_queue_entry) {
1187 list_del_init(&tcmu_cmd->cmdr_queue_entry);
1188
1189 pr_debug("removing cmd %u on dev %s from queue\n",
1190 tcmu_cmd->cmd_id, udev->name);
1191
1192 ret = queue_cmd_ring(tcmu_cmd, &scsi_ret);
1193 if (ret < 0) {
1194 pr_debug("cmd %u on dev %s failed with %u\n",
1195 tcmu_cmd->cmd_id, udev->name, scsi_ret);
1196
1197 idr_remove(&udev->commands, tcmu_cmd->cmd_id);
1198 /*
1199 * Ignore scsi_ret for now. target_complete_cmd
1200 * drops it.
1201 */
1202 target_complete_cmd(tcmu_cmd->se_cmd,
1203 SAM_STAT_CHECK_CONDITION);
1204 tcmu_free_cmd(tcmu_cmd);
1205 } else if (ret > 0) {
1206 pr_debug("ran out of space during cmdr queue run\n");
1207 /*
1208 * cmd was requeued, so just put all cmds back in
1209 * the queue
1210 */
1211 list_splice_tail(&cmds, &udev->cmdr_queue);
1212 drained = false;
1213 goto done;
1214 }
1215 }
1216done:
1217 return drained;
1218}
1219
1132static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on) 1220static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
1133{ 1221{
1134 struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info); 1222 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
1135 1223
1136 mutex_lock(&tcmu_dev->cmdr_lock); 1224 mutex_lock(&udev->cmdr_lock);
1137 tcmu_handle_completions(tcmu_dev); 1225 tcmu_handle_completions(udev);
1138 mutex_unlock(&tcmu_dev->cmdr_lock); 1226 run_cmdr_queue(udev);
1227 mutex_unlock(&udev->cmdr_lock);
1139 1228
1140 return 0; 1229 return 0;
1141} 1230}
@@ -1531,7 +1620,6 @@ static int tcmu_configure_device(struct se_device *dev)
1531 udev->data_off = CMDR_SIZE; 1620 udev->data_off = CMDR_SIZE;
1532 udev->data_size = DATA_SIZE; 1621 udev->data_size = DATA_SIZE;
1533 udev->dbi_thresh = 0; /* Default in Idle state */ 1622 udev->dbi_thresh = 0; /* Default in Idle state */
1534 udev->waiting_global = false;
1535 1623
1536 /* Initialise the mailbox of the ring buffer */ 1624 /* Initialise the mailbox of the ring buffer */
1537 mb = udev->mb_addr; 1625 mb = udev->mb_addr;
@@ -1977,12 +2065,14 @@ static struct target_backend_ops tcmu_ops = {
1977 .tb_dev_attrib_attrs = NULL, 2065 .tb_dev_attrib_attrs = NULL,
1978}; 2066};
1979 2067
1980
1981static void find_free_blocks(void) 2068static void find_free_blocks(void)
1982{ 2069{
1983 struct tcmu_dev *udev; 2070 struct tcmu_dev *udev;
1984 loff_t off; 2071 loff_t off;
1985 uint32_t start, end, block; 2072 u32 start, end, block, total_freed = 0;
2073
2074 if (atomic_read(&global_db_count) <= TCMU_GLOBAL_MAX_BLOCKS)
2075 return;
1986 2076
1987 mutex_lock(&root_udev_mutex); 2077 mutex_lock(&root_udev_mutex);
1988 list_for_each_entry(udev, &root_udev, node) { 2078 list_for_each_entry(udev, &root_udev, node) {
@@ -1991,8 +2081,8 @@ static void find_free_blocks(void)
1991 /* Try to complete the finished commands first */ 2081 /* Try to complete the finished commands first */
1992 tcmu_handle_completions(udev); 2082 tcmu_handle_completions(udev);
1993 2083
1994 /* Skip the udevs waiting the global pool or in idle */ 2084 /* Skip the udevs in idle */
1995 if (udev->waiting_global || !udev->dbi_thresh) { 2085 if (!udev->dbi_thresh) {
1996 mutex_unlock(&udev->cmdr_lock); 2086 mutex_unlock(&udev->cmdr_lock);
1997 continue; 2087 continue;
1998 } 2088 }
@@ -2001,8 +2091,8 @@ static void find_free_blocks(void)
2001 block = find_last_bit(udev->data_bitmap, end); 2091 block = find_last_bit(udev->data_bitmap, end);
2002 if (block == udev->dbi_max) { 2092 if (block == udev->dbi_max) {
2003 /* 2093 /*
2004 * The last bit is dbi_max, so there is 2094 * The last bit is dbi_max, so it is not possible
2005 * no need to shrink any blocks. 2095 * reclaim any blocks.
2006 */ 2096 */
2007 mutex_unlock(&udev->cmdr_lock); 2097 mutex_unlock(&udev->cmdr_lock);
2008 continue; 2098 continue;
@@ -2022,30 +2112,15 @@ static void find_free_blocks(void)
2022 /* Release the block pages */ 2112 /* Release the block pages */
2023 tcmu_blocks_release(&udev->data_blocks, start, end); 2113 tcmu_blocks_release(&udev->data_blocks, start, end);
2024 mutex_unlock(&udev->cmdr_lock); 2114 mutex_unlock(&udev->cmdr_lock);
2025 }
2026 mutex_unlock(&root_udev_mutex);
2027}
2028 2115
2029static void run_cmdr_queues(void) 2116 total_freed += end - start;
2030{ 2117 pr_debug("Freed %u blocks (total %u) from %s.\n", end - start,
2031 struct tcmu_dev *udev; 2118 total_freed, udev->name);
2032
2033 /*
2034 * Try to wake up the udevs who are waiting
2035 * for the global data block pool.
2036 */
2037 mutex_lock(&root_udev_mutex);
2038 list_for_each_entry(udev, &root_udev, node) {
2039 mutex_lock(&udev->cmdr_lock);
2040 if (!udev->waiting_global) {
2041 mutex_unlock(&udev->cmdr_lock);
2042 break;
2043 }
2044 mutex_unlock(&udev->cmdr_lock);
2045
2046 wake_up(&udev->wait_cmdr);
2047 } 2119 }
2048 mutex_unlock(&root_udev_mutex); 2120 mutex_unlock(&root_udev_mutex);
2121
2122 if (atomic_read(&global_db_count) > TCMU_GLOBAL_MAX_BLOCKS)
2123 schedule_delayed_work(&tcmu_unmap_work, msecs_to_jiffies(5000));
2049} 2124}
2050 2125
2051static void check_timedout_devices(void) 2126static void check_timedout_devices(void)
@@ -2074,7 +2149,6 @@ static void tcmu_unmap_work_fn(struct work_struct *work)
2074{ 2149{
2075 check_timedout_devices(); 2150 check_timedout_devices();
2076 find_free_blocks(); 2151 find_free_blocks();
2077 run_cmdr_queues();
2078} 2152}
2079 2153
2080static int __init tcmu_module_init(void) 2154static int __init tcmu_module_init(void)
@@ -2083,7 +2157,7 @@ static int __init tcmu_module_init(void)
2083 2157
2084 BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0); 2158 BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
2085 2159
2086 INIT_WORK(&tcmu_unmap_work, tcmu_unmap_work_fn); 2160 INIT_DELAYED_WORK(&tcmu_unmap_work, tcmu_unmap_work_fn);
2087 2161
2088 tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache", 2162 tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
2089 sizeof(struct tcmu_cmd), 2163 sizeof(struct tcmu_cmd),
@@ -2146,7 +2220,7 @@ out_free_cache:
2146 2220
2147static void __exit tcmu_module_exit(void) 2221static void __exit tcmu_module_exit(void)
2148{ 2222{
2149 cancel_work_sync(&tcmu_unmap_work); 2223 cancel_delayed_work_sync(&tcmu_unmap_work);
2150 target_backend_unregister(&tcmu_ops); 2224 target_backend_unregister(&tcmu_ops);
2151 kfree(tcmu_attrs); 2225 kfree(tcmu_attrs);
2152 genl_unregister_family(&tcmu_genl_family); 2226 genl_unregister_family(&tcmu_genl_family);