Skip to content

Commit

Permalink
test/poll-race-mshot: fix missing init of buffer ring
Browse files Browse the repository at this point in the history
It's critical to initialize the ring before use, obviously... As is also
documented.

Outside of that, intialize the buffer written. This doesn't really
matter, but it's a nice thing to do.

Use the appropriate helpers to add buffers to the ring, rather than
maintain a private mask.

Finally, free the ring and buffers when done. This usually doesn't matter
in test cases, but we are calling these thousands of times. Better to
free the memory than bloat the usage.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
  • Loading branch information
axboe committed Feb 17, 2023
1 parent 3d4dbe3 commit e301444
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions test/poll-race-mshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <sys/socket.h>

#include "liburing.h"
#include "helpers.h"

#define NREQS 64
#define BR_MASK (NREQS - 1)
#define BUF_SIZE 64

static int no_buf_ring;
Expand All @@ -31,6 +31,7 @@ static void *thread(void *data)
char buf[BUF_SIZE];
int ret, i, fd;

memset(buf, 0x5a, BUF_SIZE);
pthread_barrier_wait(&d->barrier);
fd = d->fd;
for (i = 0; i < NREQS; i++) {
Expand Down Expand Up @@ -64,11 +65,12 @@ static int test(struct io_uring *ring, struct data *d)

d->fd = fd[1];

if (posix_memalign((void *) &buf, 16384, BUF_SIZE * NREQS))
if (posix_memalign((void **) &buf, 16384, BUF_SIZE * NREQS))
return T_EXIT_FAIL;
if (posix_memalign((void *) &br, 16384, 4096))
if (posix_memalign((void **) &br, 16384, sizeof(struct io_uring_buf) * NREQS))
return T_EXIT_FAIL;

io_uring_buf_ring_init(br);
reg.ring_addr = (unsigned long) br;
reg.ring_entries = NREQS;
reg.bgid = 1;
Expand All @@ -85,7 +87,8 @@ static int test(struct io_uring *ring, struct data *d)

ptr = buf;
for (i = 0; i < NREQS; i++) {
io_uring_buf_ring_add(br, ptr, BUF_SIZE, i + 1, BR_MASK, i);
io_uring_buf_ring_add(br, ptr, BUF_SIZE, i + 1,
io_uring_buf_ring_mask(NREQS), i);
ptr += BUF_SIZE;
}
io_uring_buf_ring_advance(br, NREQS);
Expand Down Expand Up @@ -139,6 +142,8 @@ static int test(struct io_uring *ring, struct data *d)
} while (i < NREQS);

pthread_join(t, &ret2);
free(buf);
free(br);
close(fd[0]);
close(fd[1]);
return T_EXIT_PASS;
Expand All @@ -164,9 +169,10 @@ static int test_mshot(struct io_uring *ring, struct data *d)

if (posix_memalign((void *) &buf, 16384, BUF_SIZE * NREQS))
return T_EXIT_FAIL;
if (posix_memalign((void *) &br, 16384, 4096))
if (posix_memalign((void *) &br, 16384, sizeof(struct io_uring_buf) * NREQS))
return T_EXIT_FAIL;

io_uring_buf_ring_init(br);
reg.ring_addr = (unsigned long) br;
reg.ring_entries = NREQS;
reg.bgid = 1;
Expand All @@ -179,7 +185,8 @@ static int test_mshot(struct io_uring *ring, struct data *d)

ptr = buf;
for (i = 0; i < NREQS; i++) {
io_uring_buf_ring_add(br, ptr, BUF_SIZE, i + 1, BR_MASK, i);
io_uring_buf_ring_add(br, ptr, BUF_SIZE, i + 1,
io_uring_buf_ring_mask(NREQS), i);
ptr += BUF_SIZE;
}
io_uring_buf_ring_advance(br, NREQS);
Expand Down Expand Up @@ -238,6 +245,8 @@ static int test_mshot(struct io_uring *ring, struct data *d)
}

pthread_join(t, &ret2);
free(buf);
free(br);
close(fd[0]);
close(fd[1]);
return T_EXIT_PASS;
Expand Down

0 comments on commit e301444

Please sign in to comment.