Skip to content

Commit

Permalink
target/user: Don't free expired command when time out
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Sheng Yang authored and Nicholas Bellinger committed Mar 11, 2016
1 parent 2641864 commit b25c786
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions drivers/target/target_core_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,13 @@ static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *
struct tcmu_dev *udev = cmd->tcmu_dev;

if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
/* cmd has been completed already from timeout, just reclaim data
ring space */
/*
* cmd has been completed already from timeout, just reclaim
* data ring space and free cmd
*/
free_data_area(udev, cmd);

kmem_cache_free(tcmu_cmd_cache, cmd);
return;
}

Expand Down Expand Up @@ -976,12 +980,12 @@ static int tcmu_configure_device(struct se_device *dev)
return ret;
}

static int tcmu_check_pending_cmd(int id, void *p, void *data)
static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
{
struct tcmu_cmd *cmd = p;

if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
kmem_cache_free(tcmu_cmd_cache, cmd);
return 0;
}
return -EINVAL;
}

Expand All @@ -996,6 +1000,8 @@ static void tcmu_dev_call_rcu(struct rcu_head *p)
static void tcmu_free_device(struct se_device *dev)
{
struct tcmu_dev *udev = TCMU_DEV(dev);
struct tcmu_cmd *cmd;
bool all_expired = true;
int i;

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

/* Upper layer should drain all requests before calling this */
spin_lock_irq(&udev->commands_lock);
i = idr_for_each(&udev->commands, tcmu_check_pending_cmd, NULL);
idr_for_each_entry(&udev->commands, cmd, i) {
if (tcmu_check_and_free_pending_cmd(cmd) != 0)
all_expired = false;
}
idr_destroy(&udev->commands);
spin_unlock_irq(&udev->commands_lock);
WARN_ON(i);
WARN_ON(!all_expired);

/* Device was configured */
if (udev->uio_info.uio_dev) {
Expand Down

0 comments on commit b25c786

Please sign in to comment.