* [PATCH liburing] test/buf-ring: add test for buf ring occupying exactly one page
@ 2023-02-18 18:46 Wojciech Lukowicz
2023-02-20 14:45 ` Gabriel Krisman Bertazi
2023-02-22 16:54 ` Jens Axboe
0 siblings, 2 replies; 3+ messages in thread
From: Wojciech Lukowicz @ 2023-02-18 18:46 UTC (permalink / raw)
To: Jens Axboe; +Cc: io-uring, Wojciech Lukowicz
This shows an issue with how the kernel calculates buffer ring sizes
during their registration.
Allocate two pages, register a buf ring fully occupying the first one,
while protecting the second one to make sure it's not used. The
registration should succeed.
mmapping a single page would be a more practical example, but wouldn't
guarantee the issue gets triggered in case the following page happens
to be accessible.
Signed-off-by: Wojciech Lukowicz <[email protected]>
---
This is a failing test, needs the patch I sent earlier.
test/buf-ring.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 54 insertions(+)
diff --git a/test/buf-ring.c b/test/buf-ring.c
index 9d507ac21e32..7615ddad56a9 100644
--- a/test/buf-ring.c
+++ b/test/buf-ring.c
@@ -9,11 +9,13 @@
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
+#include <sys/mman.h>
#include "liburing.h"
#include "helpers.h"
static int no_buf_ring;
+static int pagesize;
/* test trying to register classic group when ring group exists */
static int test_mixed_reg2(int bgid)
@@ -230,6 +232,50 @@ static int test_bad_reg(int bgid)
return !ret;
}
+static int test_full_page_reg(int bgid)
+{
+ struct io_uring ring;
+ int ret;
+ void *ptr;
+ struct io_uring_buf_reg reg = { };
+ int entries = pagesize / sizeof(struct io_uring_buf);
+
+ ret = io_uring_queue_init(1, &ring, 0);
+ if (ret) {
+ fprintf(stderr, "queue init failed %d\n", ret);
+ return 1;
+ }
+
+ ret = posix_memalign(&ptr, pagesize, pagesize * 2);
+ if (ret) {
+ fprintf(stderr, "posix_memalign failed %d\n", ret);
+ goto err;
+ }
+
+ ret = mprotect(ptr + pagesize, pagesize, PROT_NONE);
+ if (ret) {
+ fprintf(stderr, "mprotect failed %d\n", errno);
+ goto err1;
+ }
+
+ reg.ring_addr = (unsigned long) ptr;
+ reg.ring_entries = entries;
+ reg.bgid = bgid;
+
+ ret = io_uring_register_buf_ring(&ring, ®, 0);
+ if (ret)
+ fprintf(stderr, "register buf ring failed %d\n", ret);
+
+ if (mprotect(ptr + pagesize, pagesize, PROT_READ | PROT_WRITE))
+ fprintf(stderr, "reverting mprotect failed %d\n", errno);
+
+err1:
+ free(ptr);
+err:
+ io_uring_queue_exit(&ring);
+ return ret;
+}
+
static int test_one_read(int fd, int bgid, struct io_uring *ring)
{
int ret;
@@ -374,6 +420,8 @@ int main(int argc, char *argv[])
if (argc > 1)
return T_EXIT_SKIP;
+ pagesize = getpagesize();
+
for (i = 0; bgids[i] != -1; i++) {
ret = test_reg_unreg(bgids[i]);
if (ret) {
@@ -406,6 +454,12 @@ int main(int argc, char *argv[])
fprintf(stderr, "test_mixed_reg2 failed\n");
return T_EXIT_FAIL;
}
+
+ ret = test_full_page_reg(bgids[i]);
+ if (ret) {
+ fprintf(stderr, "test_full_page_reg failed\n");
+ return T_EXIT_FAIL;
+ }
}
for (i = 0; !no_buf_ring && entries[i] != -1; i++) {
--
2.30.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH liburing] test/buf-ring: add test for buf ring occupying exactly one page
2023-02-18 18:46 [PATCH liburing] test/buf-ring: add test for buf ring occupying exactly one page Wojciech Lukowicz
@ 2023-02-20 14:45 ` Gabriel Krisman Bertazi
2023-02-22 16:54 ` Jens Axboe
1 sibling, 0 replies; 3+ messages in thread
From: Gabriel Krisman Bertazi @ 2023-02-20 14:45 UTC (permalink / raw)
To: Wojciech Lukowicz; +Cc: Jens Axboe, io-uring
Wojciech Lukowicz <[email protected]> writes:
> This shows an issue with how the kernel calculates buffer ring sizes
> during their registration.
>
> Allocate two pages, register a buf ring fully occupying the first one,
> while protecting the second one to make sure it's not used. The
> registration should succeed.
>
> mmapping a single page would be a more practical example, but wouldn't
> guarantee the issue gets triggered in case the following page happens
> to be accessible.
>
> Signed-off-by: Wojciech Lukowicz <[email protected]>
> ---
> This is a failing test, needs the patch I sent earlier.
Reviewed-by: Gabriel Krisman Bertazi <[email protected]>
--
Gabriel Krisman Bertazi
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH liburing] test/buf-ring: add test for buf ring occupying exactly one page
2023-02-18 18:46 [PATCH liburing] test/buf-ring: add test for buf ring occupying exactly one page Wojciech Lukowicz
2023-02-20 14:45 ` Gabriel Krisman Bertazi
@ 2023-02-22 16:54 ` Jens Axboe
1 sibling, 0 replies; 3+ messages in thread
From: Jens Axboe @ 2023-02-22 16:54 UTC (permalink / raw)
To: Wojciech Lukowicz; +Cc: io-uring
On Sat, 18 Feb 2023 18:46:18 +0000, Wojciech Lukowicz wrote:
> This shows an issue with how the kernel calculates buffer ring sizes
> during their registration.
>
> Allocate two pages, register a buf ring fully occupying the first one,
> while protecting the second one to make sure it's not used. The
> registration should succeed.
>
> [...]
Applied, thanks!
[1/1] test/buf-ring: add test for buf ring occupying exactly one page
(no commit info)
Best regards,
--
Jens Axboe
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-02-22 16:54 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-18 18:46 [PATCH liburing] test/buf-ring: add test for buf ring occupying exactly one page Wojciech Lukowicz
2023-02-20 14:45 ` Gabriel Krisman Bertazi
2023-02-22 16:54 ` Jens Axboe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox