From: Pavel Begunkov <[email protected]>
To: Jens Axboe <[email protected]>, [email protected]
Subject: [PATCH liburing 1/1] tests: test shmem buffer registration
Date: Wed, 9 Jun 2021 15:27:23 +0100 [thread overview]
Message-ID: <c0bebfd100d860fb055af8edf7e56b8838e92719.1623245732.git.asml.silence@gmail.com> (raw)
Add a simple test registering a chunk of memfd (shmem) memory and doing
some I/O with it.
Signed-off-by: Pavel Begunkov <[email protected]>
---
test/io_uring_register.c | 113 +++++++++++++++++++++++++++++++++++++++
1 file changed, 113 insertions(+)
diff --git a/test/io_uring_register.c b/test/io_uring_register.c
index 1d0981b..53e3987 100644
--- a/test/io_uring_register.c
+++ b/test/io_uring_register.c
@@ -485,6 +485,113 @@ test_poll_ringfd(void)
return status;
}
+static int test_shmem(void)
+{
+ const char pattern = 0xEA;
+ const int len = 4096;
+ struct io_uring_sqe *sqe;
+ struct io_uring_cqe *cqe;
+ struct io_uring ring;
+ struct iovec iov;
+ int memfd, ret, i;
+ char *mem;
+ int pipefd[2] = {-1, -1};
+
+ ret = io_uring_queue_init(8, &ring, 0);
+ if (ret)
+ return 1;
+
+ if (pipe(pipefd)) {
+ perror("pipe");
+ return 1;
+ }
+ memfd = memfd_create("uring-shmem-test", 0);
+ if (memfd < 0) {
+ fprintf(stderr, "memfd_create() failed %i\n", -errno);
+ return 1;
+ }
+ if (ftruncate(memfd, len)) {
+ fprintf(stderr, "can't truncate memfd\n");
+ return 1;
+ }
+ mem = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, 0);
+ if (!mem) {
+ fprintf(stderr, "mmap failed\n");
+ return 1;
+ }
+ for (i = 0; i < len; i++)
+ mem[i] = pattern;
+
+ iov.iov_base = mem;
+ iov.iov_len = len;
+ ret = io_uring_register_buffers(&ring, &iov, 1);
+ if (ret) {
+ if (ret == -EOPNOTSUPP) {
+ fprintf(stdout, "memfd registration isn't supported, "
+ "skip\n");
+ goto out;
+ }
+
+ fprintf(stderr, "buffer reg failed: %d\n", ret);
+ return 1;
+ }
+
+ /* check that we can read and write from/to shmem reg buffer */
+ sqe = io_uring_get_sqe(&ring);
+ io_uring_prep_write_fixed(sqe, pipefd[1], mem, 512, 0, 0);
+ sqe->user_data = 1;
+
+ ret = io_uring_submit(&ring);
+ if (ret != 1) {
+ fprintf(stderr, "submit write failed\n");
+ return 1;
+ }
+ ret = io_uring_wait_cqe(&ring, &cqe);
+ if (ret < 0 || cqe->user_data != 1 || cqe->res != 512) {
+ fprintf(stderr, "reading from shmem failed\n");
+ return 1;
+ }
+ io_uring_cqe_seen(&ring, cqe);
+
+ /* clean it, should be populated with the pattern back from the pipe */
+ memset(mem, 0, 512);
+ sqe = io_uring_get_sqe(&ring);
+ io_uring_prep_read_fixed(sqe, pipefd[0], mem, 512, 0, 0);
+ sqe->user_data = 2;
+
+ ret = io_uring_submit(&ring);
+ if (ret != 1) {
+ fprintf(stderr, "submit write failed\n");
+ return 1;
+ }
+ ret = io_uring_wait_cqe(&ring, &cqe);
+ if (ret < 0 || cqe->user_data != 2 || cqe->res != 512) {
+ fprintf(stderr, "reading from shmem failed\n");
+ return 1;
+ }
+ io_uring_cqe_seen(&ring, cqe);
+
+ for (i = 0; i < 512; i++) {
+ if (mem[i] != pattern) {
+ fprintf(stderr, "data integrity fail\n");
+ return 1;
+ }
+ }
+
+ ret = io_uring_unregister_buffers(&ring);
+ if (ret) {
+ fprintf(stderr, "buffer unreg failed: %d\n", ret);
+ return 1;
+ }
+out:
+ io_uring_queue_exit(&ring);
+ close(pipefd[0]);
+ close(pipefd[1]);
+ munmap(mem, len);
+ close(memfd);
+ return 0;
+}
+
int
main(int argc, char **argv)
{
@@ -541,5 +648,11 @@ main(int argc, char **argv)
else
printf("FAIL\n");
+ ret = test_shmem();
+ if (ret) {
+ fprintf(stderr, "test_shmem() failed\n");
+ status |= 1;
+ }
+
return status;
}
--
2.31.1
next reply other threads:[~2021-06-09 14:27 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-09 14:27 Pavel Begunkov [this message]
2021-06-09 15:12 ` [PATCH liburing 1/1] tests: test shmem buffer registration Jens Axboe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c0bebfd100d860fb055af8edf7e56b8838e92719.1623245732.git.asml.silence@gmail.com \
[email protected] \
[email protected] \
[email protected] \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox