Commit 920767a9 authored by David S. Miller's avatar David S. Miller

Merge branch 'liquidio-improve-soft-command-response-handling'

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: default avatarDavid S. Miller <davem@davemloft.net>
parents 0927f71d 64fecd3e
......@@ -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);
......@@ -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);
......@@ -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;
......@@ -413,6 +395,7 @@ 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;
}
......@@ -420,6 +403,7 @@ lio_vf_rep_pkt_xmit(struct sk_buff *skb, struct net_device *ndev)
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;
}
......@@ -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;
}
......
......@@ -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.
......
......@@ -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
......@@ -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,
......
......@@ -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)) {
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;
goto out;
}
schedule();
} 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
......
......@@ -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;
......@@ -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. */
......@@ -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);
......
......@@ -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;
}
......@@ -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)
......@@ -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);
......@@ -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;
}
......@@ -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;
}
......@@ -52,20 +52,13 @@ struct octnic_ctrl_pkt {
/** Input queue to use to send this command. */
u64 iq_no;
/** Time to wait for Octeon software to respond to this control command.
* If wait_time is 0, OSI assumes no response is expected.
*/
size_t wait_time;
/** The network device that issued the control command. */
u64 netpndev;
/** Callback function called when the command has been fetched */
octnic_ctrl_pkt_cb_fn_t cb_fn;
u32 status;
u16 *response_code;
struct completion *completion;
u32 sc_status;
};
#define MAX_UDD_SIZE(nctrl) (sizeof((nctrl)->udd))
......
......@@ -409,33 +409,22 @@ lio_process_iq_request_list(struct octeon_device *oct,
else
irh = (struct octeon_instr_irh *)
&sc->cmd.cmd2.irh;
if (irh->rflag) {
/* We're expecting a response from Octeon.
* It's up to lio_process_ordered_list() to
* process sc. Add sc to the ordered soft
* command response list because we expect
* a response from Octeon.
*/
spin_lock_irqsave
(&oct->response_list
[OCTEON_ORDERED_SC_LIST].lock,
flags);
spin_lock_irqsave(&oct->response_list
[OCTEON_ORDERED_SC_LIST].lock, flags);
atomic_inc(&oct->response_list
[OCTEON_ORDERED_SC_LIST].
pending_req_count);
[OCTEON_ORDERED_SC_LIST].pending_req_count);
list_add_tail(&sc->node, &oct->response_list
[OCTEON_ORDERED_SC_LIST].head);
spin_unlock_irqrestore
(&oct->response_list
spin_unlock_irqrestore(&oct->response_list
[OCTEON_ORDERED_SC_LIST].lock,
flags);
} else {
if (sc->callback) {
/* This callback must not sleep */
sc->callback(oct, OCTEON_REQUEST_DONE,
sc->callback_arg);
}
}
break;
default:
dev_err(&oct->pci_dev->dev,
......@@ -755,8 +744,7 @@ int octeon_send_soft_command(struct octeon_device *oct,
len = (u32)ih2->dlengsz;
}
if (sc->wait_time)
sc->timeout = jiffies + sc->wait_time;
sc->expiry_time = jiffies + msecs_to_jiffies(LIO_SC_MAX_TMO_MS);
return (octeon_send_command(oct, sc->iq_no, 1, &sc->cmd, sc,
len, REQTYPE_SOFT_COMMAND));
......@@ -791,11 +779,76 @@ int octeon_setup_sc_buffer_pool(struct octeon_device *oct)
return 0;
}
int octeon_free_sc_done_list(struct octeon_device *oct)
{
struct octeon_response_list *done_sc_list, *zombie_sc_list;
struct octeon_soft_command *sc;
struct list_head *tmp, *tmp2;
spinlock_t *sc_lists_lock; /* lock for response_list */
done_sc_list = &oct->response_list[OCTEON_DONE_SC_LIST];
zombie_sc_list = &oct->response_list[OCTEON_ZOMBIE_SC_LIST];
if (!atomic_read(&done_sc_list->pending_req_count))
return 0;
sc_lists_lock = &oct->response_list[OCTEON_ORDERED_SC_LIST].lock;
spin_lock_bh(sc_lists_lock);
list_for_each_safe(tmp, tmp2, &done_sc_list->head) {
sc = list_entry(tmp, struct octeon_soft_command, node);
if (READ_ONCE(sc->caller_is_done)) {
list_del(&sc->node);
atomic_dec(&done_sc_list->pending_req_count);
if (*sc->status_word == COMPLETION_WORD_INIT) {
/* timeout; move sc to zombie list */
list_add_tail(&sc->node, &zombie_sc_list->head);
atomic_inc(&zombie_sc_list->pending_req_count);
} else {
octeon_free_soft_command(oct, sc);
}
}
}
spin_unlock_bh(sc_lists_lock);
return 0;
}
int octeon_free_sc_zombie_list(struct octeon_device *oct)
{
struct octeon_response_list *zombie_sc_list;
struct octeon_soft_command *sc;
struct list_head *tmp, *tmp2;
spinlock_t *sc_lists_lock; /* lock for response_list */
zombie_sc_list = &oct->response_list[OCTEON_ZOMBIE_SC_LIST];
sc_lists_lock = &oct->response_list[OCTEON_ORDERED_SC_LIST].lock;
spin_lock_bh(sc_lists_lock);
list_for_each_safe(tmp, tmp2, &zombie_sc_list->head) {
list_del(tmp);
atomic_dec(&zombie_sc_list->pending_req_count);
sc = list_entry(tmp, struct octeon_soft_command, node);
octeon_free_soft_command(oct, sc);
}
spin_unlock_bh(sc_lists_lock);
return 0;
}
int octeon_free_sc_buffer_pool(struct octeon_device *oct)
{
struct list_head *tmp, *tmp2;
struct octeon_soft_command *sc;
octeon_free_sc_zombie_list(oct);
spin_lock_bh(&oct->sc_buf_pool.lock);
list_for_each_safe(tmp, tmp2, &oct->sc_buf_pool.head) {
......@@ -824,6 +877,9 @@ struct octeon_soft_command *octeon_alloc_soft_command(struct octeon_device *oct,
struct octeon_soft_command *sc = NULL;
struct list_head *tmp;
if (!rdatasize)
rdatasize = 16;
WARN_ON((offset + datasize + rdatasize + ctxsize) >
SOFT_COMMAND_BUFFER_SIZE);
......
......@@ -69,6 +69,8 @@ int lio_process_ordered_list(struct octeon_device *octeon_dev,
u32 status;
u64 status64;
octeon_free_sc_done_list(octeon_dev);
ordered_sc_list = &octeon_dev->response_list[OCTEON_ORDERED_SC_LIST];
do {
......@@ -111,26 +113,88 @@ int lio_process_ordered_list(struct octeon_device *octeon_dev,
}
}
}
} else if (force_quit || (sc->timeout &&
time_after(jiffies, (unsigned long)sc->timeout))) {
dev_err(&octeon_dev->pci_dev->dev, "%s: cmd failed, timeout (%ld, %ld)\n",
__func__, (long)jiffies, (long)sc->timeout);
} else if (unlikely(force_quit) || (sc->expiry_time &&
time_after(jiffies, (unsigned long)sc->expiry_time))) {
struct octeon_instr_irh *irh =
(struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
dev_err(&octeon_dev->pci_dev->dev, "%s: ", __func__);
dev_err(&octeon_dev->pci_dev->dev,
"cmd %x/%x/%llx/%llx failed, ",
irh->opcode, irh->subcode,
sc->cmd.cmd3.ossp[0], sc->cmd.cmd3.ossp[1]);
dev_err(&octeon_dev->pci_dev->dev,
"timeout (%ld, %ld)\n",
(long)jiffies, (long)sc->expiry_time);
status = OCTEON_REQUEST_TIMEOUT;
}
if (status != OCTEON_REQUEST_PENDING) {
sc->sc_status = status;
/* we have received a response or we have timed out */
/* remove node from linked list */
list_del(&sc->node);
atomic_dec(&octeon_dev->response_list
[OCTEON_ORDERED_SC_LIST].
pending_req_count);
spin_unlock_bh
(&ordered_sc_list->lock);
if (sc->callback)
if (!sc->callback) {
atomic_inc(&octeon_dev->response_list
[OCTEON_DONE_SC_LIST].
pending_req_count);
list_add_tail(&sc->node,
&octeon_dev->response_list
[OCTEON_DONE_SC_LIST].head);
if (unlikely(READ_ONCE(sc->caller_is_done))) {
/* caller does not wait for response
* from firmware
*/
if (status != OCTEON_REQUEST_DONE) {
struct octeon_instr_irh *irh;
irh =
(struct octeon_instr_irh *)
&sc->cmd.cmd3.irh;
dev_dbg
(&octeon_dev->pci_dev->dev,
"%s: sc failed: opcode=%x, ",
__func__, irh->opcode);
dev_dbg
(&octeon_dev->pci_dev->dev,
"subcode=%x, ossp[0]=%llx, ",
irh->subcode,
sc->cmd.cmd3.ossp[0]);
dev_dbg
(&octeon_dev->pci_dev->dev,
"ossp[1]=%llx, status=%d\n",
sc->cmd.cmd3.ossp[1],
status);
}
} else {
complete(&sc->complete);
}
spin_unlock_bh(&ordered_sc_list->lock);
} else {
/* sc with callback function */
if (status == OCTEON_REQUEST_TIMEOUT) {
atomic_inc(&octeon_dev->response_list
[OCTEON_ZOMBIE_SC_LIST].
pending_req_count);
list_add_tail(&sc->node,
&octeon_dev->response_list
[OCTEON_ZOMBIE_SC_LIST].
head);
}
spin_unlock_bh(&ordered_sc_list->lock);
sc->callback(octeon_dev, status,
sc->callback_arg);
/* sc is freed by caller */
}
request_complete++;
......
......@@ -53,7 +53,9 @@ enum {
OCTEON_ORDERED_LIST = 0,
OCTEON_UNORDERED_NONBLOCKING_LIST = 1,
OCTEON_UNORDERED_BLOCKING_LIST = 2,
OCTEON_ORDERED_SC_LIST = 3
OCTEON_ORDERED_SC_LIST = 3,
OCTEON_DONE_SC_LIST = 4,
OCTEON_ZOMBIE_SC_LIST = 5
};
/** Response Order values for a Octeon Request. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment