Skip to content

Commit

Permalink
Merge branch 'liquidio-improve-soft-command-response-handling'
Browse files Browse the repository at this point in the history
Weilin Chang says:

====================
liquidio: improve soft command/response handling

Change soft command handling to fix the possible race condition when the
process handles a response of a soft command that was already freed by an
application which got timeout for this request.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
David S. Miller committed Aug 30, 2018
2 parents 0927f71 + 64fecd3 commit 920767a
Show file tree
Hide file tree
Showing 14 changed files with 627 additions and 802 deletions.
232 changes: 54 additions & 178 deletions drivers/net/ethernet/cavium/liquidio/lio_core.c

Large diffs are not rendered by default.

256 changes: 77 additions & 179 deletions drivers/net/ethernet/cavium/liquidio/lio_ethtool.c

Large diffs are not rendered by default.

307 changes: 138 additions & 169 deletions drivers/net/ethernet/cavium/liquidio/lio_main.c

Large diffs are not rendered by default.

194 changes: 83 additions & 111 deletions drivers/net/ethernet/cavium/liquidio/lio_vf_main.c

Large diffs are not rendered by default.

47 changes: 16 additions & 31 deletions drivers/net/ethernet/cavium/liquidio/lio_vf_rep.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,25 @@ static const struct net_device_ops lio_vf_rep_ndev_ops = {
.ndo_change_mtu = lio_vf_rep_change_mtu,
};

static void
lio_vf_rep_send_sc_complete(struct octeon_device *oct,
u32 status, void *ptr)
{
struct octeon_soft_command *sc = (struct octeon_soft_command *)ptr;
struct lio_vf_rep_sc_ctx *ctx =
(struct lio_vf_rep_sc_ctx *)sc->ctxptr;
struct lio_vf_rep_resp *resp =
(struct lio_vf_rep_resp *)sc->virtrptr;

if (status != OCTEON_REQUEST_TIMEOUT && READ_ONCE(resp->status))
WRITE_ONCE(resp->status, 0);

complete(&ctx->complete);
}

static int
lio_vf_rep_send_soft_command(struct octeon_device *oct,
void *req, int req_size,
void *resp, int resp_size)
{
int tot_resp_size = sizeof(struct lio_vf_rep_resp) + resp_size;
int ctx_size = sizeof(struct lio_vf_rep_sc_ctx);
struct octeon_soft_command *sc = NULL;
struct lio_vf_rep_resp *rep_resp;
struct lio_vf_rep_sc_ctx *ctx;
void *sc_req;
int err;

sc = (struct octeon_soft_command *)
octeon_alloc_soft_command(oct, req_size,
tot_resp_size, ctx_size);
tot_resp_size, 0);
if (!sc)
return -ENOMEM;

ctx = (struct lio_vf_rep_sc_ctx *)sc->ctxptr;
memset(ctx, 0, ctx_size);
init_completion(&ctx->complete);
init_completion(&sc->complete);
sc->sc_status = OCTEON_REQUEST_PENDING;

sc_req = (struct lio_vf_rep_req *)sc->virtdptr;
memcpy(sc_req, req, req_size);
Expand All @@ -98,23 +79,24 @@ lio_vf_rep_send_soft_command(struct octeon_device *oct,
sc->iq_no = 0;
octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
OPCODE_NIC_VF_REP_CMD, 0, 0, 0);
sc->callback = lio_vf_rep_send_sc_complete;
sc->callback_arg = sc;
sc->wait_time = LIO_VF_REP_REQ_TMO_MS;

err = octeon_send_soft_command(oct, sc);
if (err == IQ_SEND_FAILED)
goto free_buff;

wait_for_completion_timeout(&ctx->complete,
msecs_to_jiffies
(2 * LIO_VF_REP_REQ_TMO_MS));
err = wait_for_sc_completion_timeout(oct, sc, 0);
if (err)
return err;

err = READ_ONCE(rep_resp->status) ? -EBUSY : 0;
if (err)
dev_err(&oct->pci_dev->dev, "VF rep send config failed\n");

if (resp)
else if (resp)
memcpy(resp, (rep_resp + 1), resp_size);

WRITE_ONCE(sc->caller_is_done, true);
return err;

free_buff:
octeon_free_soft_command(oct, sc);

Expand Down Expand Up @@ -404,7 +386,7 @@ lio_vf_rep_pkt_xmit(struct sk_buff *skb, struct net_device *ndev)
}

sc = (struct octeon_soft_command *)
octeon_alloc_soft_command(oct, 0, 0, 0);
octeon_alloc_soft_command(oct, 0, 16, 0);
if (!sc) {
dev_err(&oct->pci_dev->dev, "VF rep: Soft command alloc failed\n");
goto xmit_failed;
Expand All @@ -413,13 +395,15 @@ lio_vf_rep_pkt_xmit(struct sk_buff *skb, struct net_device *ndev)
/* Multiple buffers are not used for vf_rep packets. */
if (skb_shinfo(skb)->nr_frags != 0) {
dev_err(&oct->pci_dev->dev, "VF rep: nr_frags != 0. Dropping packet\n");
octeon_free_soft_command(oct, sc);
goto xmit_failed;
}

sc->dmadptr = dma_map_single(&oct->pci_dev->dev,
skb->data, skb->len, DMA_TO_DEVICE);
if (dma_mapping_error(&oct->pci_dev->dev, sc->dmadptr)) {
dev_err(&oct->pci_dev->dev, "VF rep: DMA mapping failed\n");
octeon_free_soft_command(oct, sc);
goto xmit_failed;
}

Expand All @@ -440,6 +424,7 @@ lio_vf_rep_pkt_xmit(struct sk_buff *skb, struct net_device *ndev)
if (status == IQ_SEND_FAILED) {
dma_unmap_single(&oct->pci_dev->dev, sc->dmadptr,
sc->datasize, DMA_TO_DEVICE);
octeon_free_soft_command(oct, sc);
goto xmit_failed;
}

Expand Down
3 changes: 2 additions & 1 deletion drivers/net/ethernet/cavium/liquidio/octeon_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,10 @@ struct octeon_config {
#define MAX_BAR1_IOREMAP_SIZE (16 * OCTEON_BAR1_ENTRY_SIZE)

/* Response lists - 1 ordered, 1 unordered-blocking, 1 unordered-nonblocking
* 1 process done list, 1 zombie lists(timeouted sc list)
* NoResponse Lists are now maintained with each IQ. (Dec' 2007).
*/
#define MAX_RESPONSE_LISTS 4
#define MAX_RESPONSE_LISTS 6

/* Opcode hash bits. The opcode is hashed on the lower 6-bits to lookup the
* dispatch table.
Expand Down
12 changes: 10 additions & 2 deletions drivers/net/ethernet/cavium/liquidio/octeon_iq.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,19 @@ struct octeon_soft_command {
u32 ctxsize;

/** Time out and callback */
size_t wait_time;
size_t timeout;
size_t expiry_time;
u32 iq_no;
void (*callback)(struct octeon_device *, u32, void *);
void *callback_arg;

int caller_is_done;
u32 sc_status;
struct completion complete;
};

/* max timeout (in milli sec) for soft request */
#define LIO_SC_MAX_TMO_MS 60000

/** Maximum number of buffers to allocate into soft command buffer pool
*/
#define MAX_SOFT_COMMAND_BUFFERS 256
Expand All @@ -319,6 +325,8 @@ struct octeon_sc_buffer_pool {
(((octeon_dev_ptr)->instr_queue[iq_no]->stats.field) += count)

int octeon_setup_sc_buffer_pool(struct octeon_device *oct);
int octeon_free_sc_done_list(struct octeon_device *oct);
int octeon_free_sc_zombie_list(struct octeon_device *oct);
int octeon_free_sc_buffer_pool(struct octeon_device *oct);
struct octeon_soft_command *
octeon_alloc_soft_command(struct octeon_device *oct,
Expand Down
94 changes: 59 additions & 35 deletions drivers/net/ethernet/cavium/liquidio/octeon_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,46 +146,70 @@ static inline int octeon_map_pci_barx(struct octeon_device *oct,
return 1;
}

/* input parameter:
* sc: pointer to a soft request
* timeout: milli sec which an application wants to wait for the
response of the request.
* 0: the request will wait until its response gets back
* from the firmware within LIO_SC_MAX_TMO_MS milli sec.
* It the response does not return within
* LIO_SC_MAX_TMO_MS milli sec, lio_process_ordered_list()
* will move the request to zombie response list.
*
* return value:
* 0: got the response from firmware for the sc request.
* errno -EINTR: user abort the command.
* errno -ETIME: user spefified timeout value has been expired.
* errno -EBUSY: the response of the request does not return in
* resonable time (LIO_SC_MAX_TMO_MS).
* the sc wll be move to zombie response list by
* lio_process_ordered_list()
*
* A request with non-zero return value, the sc->caller_is_done
* will be marked 1.
* When getting a request with zero return value, the requestor
* should mark sc->caller_is_done with 1 after examing the
* response of sc.
* lio_process_ordered_list() will free the soft command on behalf
* of the soft command requestor.
* This is to fix the possible race condition of both timeout process
* and lio_process_ordered_list()/callback function to free a
* sc strucutre.
*/
static inline int
sleep_cond(wait_queue_head_t *wait_queue, int *condition)
wait_for_sc_completion_timeout(struct octeon_device *oct_dev,
struct octeon_soft_command *sc,
unsigned long timeout)
{
int errno = 0;
wait_queue_entry_t we;

init_waitqueue_entry(&we, current);
add_wait_queue(wait_queue, &we);
while (!(READ_ONCE(*condition))) {
set_current_state(TASK_INTERRUPTIBLE);
if (signal_pending(current)) {
errno = -EINTR;
goto out;
}
schedule();
long timeout_jiff;

if (timeout)
timeout_jiff = msecs_to_jiffies(timeout);
else
timeout_jiff = MAX_SCHEDULE_TIMEOUT;

timeout_jiff =
wait_for_completion_interruptible_timeout(&sc->complete,
timeout_jiff);
if (timeout_jiff == 0) {
dev_err(&oct_dev->pci_dev->dev, "%s: sc is timeout\n",
__func__);
WRITE_ONCE(sc->caller_is_done, true);
errno = -ETIME;
} else if (timeout_jiff == -ERESTARTSYS) {
dev_err(&oct_dev->pci_dev->dev, "%s: sc is interrupted\n",
__func__);
WRITE_ONCE(sc->caller_is_done, true);
errno = -EINTR;
} else if (sc->sc_status == OCTEON_REQUEST_TIMEOUT) {
dev_err(&oct_dev->pci_dev->dev, "%s: sc has fatal timeout\n",
__func__);
WRITE_ONCE(sc->caller_is_done, true);
errno = -EBUSY;
}
out:
set_current_state(TASK_RUNNING);
remove_wait_queue(wait_queue, &we);
return errno;
}

/* Gives up the CPU for a timeout period.
* Check that the condition is not true before we go to sleep for a
* timeout period.
*/
static inline void
sleep_timeout_cond(wait_queue_head_t *wait_queue,
int *condition,
int timeout)
{
wait_queue_entry_t we;

init_waitqueue_entry(&we, current);
add_wait_queue(wait_queue, &we);
set_current_state(TASK_INTERRUPTIBLE);
if (!(*condition))
schedule_timeout(timeout);
set_current_state(TASK_RUNNING);
remove_wait_queue(wait_queue, &we);
return errno;
}

#ifndef ROUNDUP4
Expand Down
16 changes: 0 additions & 16 deletions drivers/net/ethernet/cavium/liquidio/octeon_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@
#define LIO_IFSTATE_RX_TIMESTAMP_ENABLED 0x08
#define LIO_IFSTATE_RESETTING 0x10

struct liquidio_if_cfg_context {
u32 octeon_id;
wait_queue_head_t wc;
int cond;
};

struct liquidio_if_cfg_resp {
u64 rh;
struct liquidio_if_cfg_info cfg_info;
Expand Down Expand Up @@ -87,12 +81,6 @@ struct oct_nic_seapi_resp {
u64 status;
};

struct liquidio_nic_seapi_ctl_context {
int octeon_id;
u32 status;
struct completion complete;
};

/** LiquidIO per-interface network private data */
struct lio {
/** State of the interface. Rx/Tx happens only in the RUNNING state. */
Expand Down Expand Up @@ -234,10 +222,6 @@ int lio_wait_for_clean_oq(struct octeon_device *oct);
*/
void liquidio_set_ethtool_ops(struct net_device *netdev);

void lio_if_cfg_callback(struct octeon_device *oct,
u32 status __attribute__((unused)),
void *buf);

void lio_delete_glists(struct lio *lio);

int lio_setup_glists(struct octeon_device *oct, struct lio *lio, int num_qs);
Expand Down
59 changes: 26 additions & 33 deletions drivers/net/ethernet/cavium/liquidio/octeon_nic.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ octeon_alloc_soft_command_resp(struct octeon_device *oct,
else
sc->cmd.cmd2.rptr = sc->dmarptr;

sc->wait_time = 1000;
sc->timeout = jiffies + sc->wait_time;
sc->expiry_time = jiffies + msecs_to_jiffies(LIO_SC_MAX_TMO_MS);

return sc;
}
Expand All @@ -92,29 +91,6 @@ int octnet_send_nic_data_pkt(struct octeon_device *oct,
ndata->reqtype);
}

static void octnet_link_ctrl_callback(struct octeon_device *oct,
u32 status,
void *sc_ptr)
{
struct octeon_soft_command *sc = (struct octeon_soft_command *)sc_ptr;
struct octnic_ctrl_pkt *nctrl;

nctrl = (struct octnic_ctrl_pkt *)sc->ctxptr;

/* Call the callback function if status is zero (meaning OK) or status
* contains a firmware status code bigger than zero (meaning the
* firmware is reporting an error).
* If no response was expected, status is OK if the command was posted
* successfully.
*/
if ((!status || status > FIRMWARE_STATUS_CODE(0)) && nctrl->cb_fn) {
nctrl->status = status;
nctrl->cb_fn(nctrl);
}

octeon_free_soft_command(oct, sc);
}

static inline struct octeon_soft_command
*octnic_alloc_ctrl_pkt_sc(struct octeon_device *oct,
struct octnic_ctrl_pkt *nctrl)
Expand All @@ -127,17 +103,14 @@ static inline struct octeon_soft_command
uddsize = (u32)(nctrl->ncmd.s.more * 8);

datasize = OCTNET_CMD_SIZE + uddsize;
rdatasize = (nctrl->wait_time) ? 16 : 0;
rdatasize = 16;

sc = (struct octeon_soft_command *)
octeon_alloc_soft_command(oct, datasize, rdatasize,
sizeof(struct octnic_ctrl_pkt));
octeon_alloc_soft_command(oct, datasize, rdatasize, 0);

if (!sc)
return NULL;

memcpy(sc->ctxptr, nctrl, sizeof(struct octnic_ctrl_pkt));

data = (u8 *)sc->virtdptr;

memcpy(data, &nctrl->ncmd, OCTNET_CMD_SIZE);
Expand All @@ -154,9 +127,8 @@ static inline struct octeon_soft_command
octeon_prepare_soft_command(oct, sc, OPCODE_NIC, OPCODE_NIC_CMD,
0, 0, 0);

sc->callback = octnet_link_ctrl_callback;
sc->callback_arg = sc;
sc->wait_time = nctrl->wait_time;
init_completion(&sc->complete);
sc->sc_status = OCTEON_REQUEST_PENDING;

return sc;
}
Expand Down Expand Up @@ -199,5 +171,26 @@ octnet_send_nic_ctrl_pkt(struct octeon_device *oct,
}

spin_unlock_bh(&oct->cmd_resp_wqlock);

switch (nctrl->ncmd.s.cmd) {
/* caller holds lock, can not sleep */
case OCTNET_CMD_CHANGE_DEVFLAGS:
case OCTNET_CMD_SET_MULTI_LIST:
case OCTNET_CMD_SET_UC_LIST:
WRITE_ONCE(sc->caller_is_done, true);
return retval;
}

retval = wait_for_sc_completion_timeout(oct, sc, 0);
if (retval)
return (retval);

nctrl->sc_status = sc->sc_status;
retval = nctrl->sc_status;
if (nctrl->cb_fn)
nctrl->cb_fn(nctrl);

WRITE_ONCE(sc->caller_is_done, true);

return retval;
}
Loading

0 comments on commit 920767a

Please sign in to comment.