Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird readv behavior on 5.5 kernel #52

Closed
twissel opened this issue Jan 7, 2020 · 5 comments
Closed

Weird readv behavior on 5.5 kernel #52

twissel opened this issue Jan 7, 2020 · 5 comments

Comments

@twissel
Copy link

twissel commented Jan 7, 2020

On 5.5 kernel in case if file size is less than iovec size, cqe.res will be equal to 0, On 5.4 kernel cqe.res will contain correct number of bytes read

Sample code:

#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/poll.h>

#include "liburing.h"

#define BUF_SIZE 4096
#define FILE_SIZE 1024

static int create_file(const char *file)
{
	ssize_t ret;
	char *buf;
	int fd;

	buf = malloc(FILE_SIZE);
	memset(buf, 0xaa, FILE_SIZE);

	fd = open(file, O_WRONLY | O_CREAT, 0644);
	if (fd < 0) {
		perror("open file");
		return 1;
	}
	ret = write(fd, buf, FILE_SIZE);
	close(fd);
	return ret != FILE_SIZE;
}

int main(int argc, char* argv[]) {
    int ret, fd;
    struct io_uring ring;
    struct io_uring_sqe *sqe;
    struct io_uring_cqe *cqe;
    struct iovec vec;

    vec.iov_base = malloc(BUF_SIZE);
    vec.iov_len = BUF_SIZE;

    if (create_file(".basic-r")) {
		fprintf(stderr, "file creation failed\n");
		return 1;
	}

    fd = open(".basic-r", O_RDONLY);
	if (fd < 0) {
		perror("file open");
		return 1;
	}

    ret = io_uring_queue_init(32, &ring, 0);
	if (ret)
		return ret;


    sqe = io_uring_get_sqe(&ring);
    if (!sqe) {
			fprintf(stderr, "sqe get failed\n");
			return 1;
	}

    io_uring_prep_readv(sqe, fd, &vec, 1, 0);
    
    ret = io_uring_submit(&ring);
    if (ret != 1) {
        return 1;
    }

    ret = io_uring_wait_cqes(&ring, &cqe, 1, 0, 0);
    if (ret) {
        return 1;
    }

    fprintf(stderr, "cqe res %d", cqe->res);
    io_uring_cqe_seen(&ring, cqe);
    return 0;
}
@axboe
Copy link
Owner

axboe commented Jan 7, 2020

Good catch, let me take a look at that.

@axboe
Copy link
Owner

axboe commented Jan 7, 2020

Adopted the test program as a real test case:

https://git.kernel.dk/cgit/liburing/commit/?id=46f92335793e70363c296b4c997da7a808959a49

@twissel
Copy link
Author

twissel commented Jan 8, 2020

Cool, thanks for the fix

@axboe
Copy link
Owner

axboe commented Jan 9, 2020

No problem, thanks for the report!

@axboe axboe closed this as completed Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants