Skip to content

Commit 3f77c3d

Browse files
emuslnkuba-moo
authored andcommitted
pds_core: make wait_context part of q_info
Make the wait_context a full part of the q_info struct rather than a stack variable that goes away after pdsc_adminq_post() is done so that the context is still available after the wait loop has given up. There was a case where a slow development firmware caused the adminq request to time out, but then later the FW finally finished the request and sent the interrupt. The handler tried to complete_all() the completion context that had been created on the stack in pdsc_adminq_post() but no longer existed. This caused bad pointer usage, kernel crashes, and much wailing and gnashing of teeth. Fixes: 01ba61b ("pds_core: Add adminq processing and commands") Reviewed-by: Simon Horman <horms@kernel.org> Signed-off-by: Shannon Nelson <shannon.nelson@amd.com> Reviewed-by: Jacob Keller <jacob.e.keller@intel.com> Link: https://patch.msgid.link/20250421174606.3892-5-shannon.nelson@amd.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent f9559d8 commit 3f77c3d

File tree

3 files changed

+18
-24
lines changed

3 files changed

+18
-24
lines changed

drivers/net/ethernet/amd/pds_core/adminq.c

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55

66
#include "core.h"
77

8-
struct pdsc_wait_context {
9-
struct pdsc_qcq *qcq;
10-
struct completion wait_completion;
11-
};
12-
138
static int pdsc_process_notifyq(struct pdsc_qcq *qcq)
149
{
1510
union pds_core_notifyq_comp *comp;
@@ -109,10 +104,10 @@ void pdsc_process_adminq(struct pdsc_qcq *qcq)
109104
q_info = &q->info[q->tail_idx];
110105
q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
111106

112-
/* Copy out the completion data */
113-
memcpy(q_info->dest, comp, sizeof(*comp));
114-
115-
complete_all(&q_info->wc->wait_completion);
107+
if (!completion_done(&q_info->completion)) {
108+
memcpy(q_info->dest, comp, sizeof(*comp));
109+
complete(&q_info->completion);
110+
}
116111

117112
if (cq->tail_idx == cq->num_descs - 1)
118113
cq->done_color = !cq->done_color;
@@ -162,8 +157,7 @@ irqreturn_t pdsc_adminq_isr(int irq, void *data)
162157
static int __pdsc_adminq_post(struct pdsc *pdsc,
163158
struct pdsc_qcq *qcq,
164159
union pds_core_adminq_cmd *cmd,
165-
union pds_core_adminq_comp *comp,
166-
struct pdsc_wait_context *wc)
160+
union pds_core_adminq_comp *comp)
167161
{
168162
struct pdsc_queue *q = &qcq->q;
169163
struct pdsc_q_info *q_info;
@@ -205,9 +199,9 @@ static int __pdsc_adminq_post(struct pdsc *pdsc,
205199
/* Post the request */
206200
index = q->head_idx;
207201
q_info = &q->info[index];
208-
q_info->wc = wc;
209202
q_info->dest = comp;
210203
memcpy(q_info->desc, cmd, sizeof(*cmd));
204+
reinit_completion(&q_info->completion);
211205

212206
dev_dbg(pdsc->dev, "head_idx %d tail_idx %d\n",
213207
q->head_idx, q->tail_idx);
@@ -231,16 +225,13 @@ int pdsc_adminq_post(struct pdsc *pdsc,
231225
union pds_core_adminq_comp *comp,
232226
bool fast_poll)
233227
{
234-
struct pdsc_wait_context wc = {
235-
.wait_completion =
236-
COMPLETION_INITIALIZER_ONSTACK(wc.wait_completion),
237-
};
238228
unsigned long poll_interval = 1;
239229
unsigned long poll_jiffies;
240230
unsigned long time_limit;
241231
unsigned long time_start;
242232
unsigned long time_done;
243233
unsigned long remaining;
234+
struct completion *wc;
244235
int err = 0;
245236
int index;
246237

@@ -250,20 +241,19 @@ int pdsc_adminq_post(struct pdsc *pdsc,
250241
return -ENXIO;
251242
}
252243

253-
wc.qcq = &pdsc->adminqcq;
254-
index = __pdsc_adminq_post(pdsc, &pdsc->adminqcq, cmd, comp, &wc);
244+
index = __pdsc_adminq_post(pdsc, &pdsc->adminqcq, cmd, comp);
255245
if (index < 0) {
256246
err = index;
257247
goto err_out;
258248
}
259249

250+
wc = &pdsc->adminqcq.q.info[index].completion;
260251
time_start = jiffies;
261252
time_limit = time_start + HZ * pdsc->devcmd_timeout;
262253
do {
263254
/* Timeslice the actual wait to catch IO errors etc early */
264255
poll_jiffies = msecs_to_jiffies(poll_interval);
265-
remaining = wait_for_completion_timeout(&wc.wait_completion,
266-
poll_jiffies);
256+
remaining = wait_for_completion_timeout(wc, poll_jiffies);
267257
if (remaining)
268258
break;
269259

@@ -292,9 +282,11 @@ int pdsc_adminq_post(struct pdsc *pdsc,
292282
dev_dbg(pdsc->dev, "%s: elapsed %d msecs\n",
293283
__func__, jiffies_to_msecs(time_done - time_start));
294284

295-
/* Check the results */
296-
if (time_after_eq(time_done, time_limit))
285+
/* Check the results and clear an un-completed timeout */
286+
if (time_after_eq(time_done, time_limit) && !completion_done(wc)) {
297287
err = -ETIMEDOUT;
288+
complete(wc);
289+
}
298290

299291
dev_dbg(pdsc->dev, "read admin queue completion idx %d:\n", index);
300292
dynamic_hex_dump("comp ", DUMP_PREFIX_OFFSET, 16, 1,

drivers/net/ethernet/amd/pds_core/core.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ static void pdsc_q_map(struct pdsc_queue *q, void *base, dma_addr_t base_pa)
167167
q->base = base;
168168
q->base_pa = base_pa;
169169

170-
for (i = 0, cur = q->info; i < q->num_descs; i++, cur++)
170+
for (i = 0, cur = q->info; i < q->num_descs; i++, cur++) {
171171
cur->desc = base + (i * q->desc_size);
172+
init_completion(&cur->completion);
173+
}
172174
}
173175

174176
static void pdsc_cq_map(struct pdsc_cq *cq, void *base, dma_addr_t base_pa)

drivers/net/ethernet/amd/pds_core/core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ struct pdsc_q_info {
9696
unsigned int bytes;
9797
unsigned int nbufs;
9898
struct pdsc_buf_info bufs[PDS_CORE_MAX_FRAGS];
99-
struct pdsc_wait_context *wc;
99+
struct completion completion;
100100
void *dest;
101101
};
102102

0 commit comments

Comments
 (0)