Skip to content

Commit b25c786

Browse files
yaskerNicholas Bellinger
authored andcommitted
target/user: Don't free expired command when time out
Which would result in NPE after when userspace connected again. Expired command would be freed either when handling command(by userspace), or when device was tearing down Reviewed-by: Andy Grover <agrover@redhat.com> Signed-off-by: Sheng Yang <sheng@yasker.org> Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>
1 parent 2641864 commit b25c786

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

‎drivers/target/target_core_user.c‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,13 @@ static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *
560560
struct tcmu_dev *udev = cmd->tcmu_dev;
561561

562562
if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
563-
/* cmd has been completed already from timeout, just reclaim data
564-
ring space */
563+
/*
564+
* cmd has been completed already from timeout, just reclaim
565+
* data ring space and free cmd
566+
*/
565567
free_data_area(udev, cmd);
568+
569+
kmem_cache_free(tcmu_cmd_cache, cmd);
566570
return;
567571
}
568572

@@ -976,12 +980,12 @@ static int tcmu_configure_device(struct se_device *dev)
976980
return ret;
977981
}
978982

979-
static int tcmu_check_pending_cmd(int id, void *p, void *data)
983+
static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
980984
{
981-
struct tcmu_cmd *cmd = p;
982-
983-
if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
985+
if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
986+
kmem_cache_free(tcmu_cmd_cache, cmd);
984987
return 0;
988+
}
985989
return -EINVAL;
986990
}
987991

@@ -996,6 +1000,8 @@ static void tcmu_dev_call_rcu(struct rcu_head *p)
9961000
static void tcmu_free_device(struct se_device *dev)
9971001
{
9981002
struct tcmu_dev *udev = TCMU_DEV(dev);
1003+
struct tcmu_cmd *cmd;
1004+
bool all_expired = true;
9991005
int i;
10001006

10011007
del_timer_sync(&udev->timeout);
@@ -1004,10 +1010,13 @@ static void tcmu_free_device(struct se_device *dev)
10041010

10051011
/* Upper layer should drain all requests before calling this */
10061012
spin_lock_irq(&udev->commands_lock);
1007-
i = idr_for_each(&udev->commands, tcmu_check_pending_cmd, NULL);
1013+
idr_for_each_entry(&udev->commands, cmd, i) {
1014+
if (tcmu_check_and_free_pending_cmd(cmd) != 0)
1015+
all_expired = false;
1016+
}
10081017
idr_destroy(&udev->commands);
10091018
spin_unlock_irq(&udev->commands_lock);
1010-
WARN_ON(i);
1019+
WARN_ON(!all_expired);
10111020

10121021
/* Device was configured */
10131022
if (udev->uio_info.uio_dev) {

0 commit comments

Comments
 (0)