Skip to content

Commit 264e181

Browse files
povikmarcan
authored andcommitted
afk: Fix ringbuf space checks in afk_epic_tx
Redo the checks to make sure we never cause rptr==wptr, because that signifies an empty buffer, not a full one. Signed-off-by: Martin Povišer <[email protected]>
1 parent 07ea5a9 commit 264e181

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

src/afk.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,16 @@ static int afk_epic_tx(afk_epic_ep_t *epic, u32 channel, u32 type, void *data, s
275275
u32 rptr = rb->hdr->rptr;
276276
u32 wptr = rb->hdr->wptr;
277277
struct afk_qe *hdr = rb->buf + wptr;
278+
size_t buf_advance = ALIGN_UP(sizeof(struct afk_qe) + size, 1 << BLOCK_SHIFT);
278279

279-
if (wptr < rptr && (wptr + sizeof(struct afk_qe) > rptr)) {
280-
printf("EPIC: TX ring buffer is full\n");
281-
return -1;
280+
if (wptr < rptr && buf_advance >= rptr - wptr)
281+
goto buffer_full;
282+
if (wptr >= rptr) {
283+
bool fits_above_wptr =
284+
(buf_advance < rb->bufsz - wptr) || (buf_advance == rb->bufsz - wptr && rptr != 0);
285+
286+
if (!fits_above_wptr && buf_advance >= rptr)
287+
goto buffer_full;
282288
}
283289

284290
hdr->magic = QE_MAGIC;
@@ -289,20 +295,11 @@ static int afk_epic_tx(afk_epic_ep_t *epic, u32 channel, u32 type, void *data, s
289295
wptr += sizeof(struct afk_qe);
290296

291297
if (size > rb->bufsz - wptr) {
292-
if (rptr < sizeof(struct afk_qe)) {
293-
printf("EPIC: TX ring buffer is full\n");
294-
return -1;
295-
}
296298
*(struct afk_qe *)rb->buf = *hdr;
297299
hdr = rb->buf;
298300
wptr = sizeof(struct afk_qe);
299301
}
300302

301-
if (wptr < rptr && (wptr + size > rptr)) {
302-
printf("EPIC: TX ring buffer is full\n");
303-
return -1;
304-
}
305-
306303
wptr += size;
307304
wptr = ALIGN_UP(wptr, 1 << BLOCK_SHIFT);
308305

@@ -323,6 +320,10 @@ static int afk_epic_tx(afk_epic_ep_t *epic, u32 channel, u32 type, void *data, s
323320
}
324321

325322
return 1;
323+
324+
buffer_full:
325+
printf("EPIC: TX ring buffer is full\n");
326+
return -1;
326327
}
327328

328329
static void afk_epic_rx_ack(afk_epic_ep_t *epic)

0 commit comments

Comments
 (0)