axboe / liburing

Library providing helpers for the Linux kernel io_uring support
MIT License
2.72k stars 393 forks source link

io_uring_get_sqe always return NULL #1056

Closed g2px1 closed 5 months ago

g2px1 commented 5 months ago

I'm running code below under Ubuntu Server 22.04.3 with kernel version 5.15.0-94-generic. Here is simple code to reproduce error:

#include <liburing.h>
#include <liburing/io_uring.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>

#define QUEUE_DEPTH 1

int io_uring_setup(unsigned entries, struct io_uring_params *p) {
  return (int)syscall(__NR_io_uring_setup, entries, p);
}

int main() {
  struct io_uring ring;
  int ret;
  struct io_uring_params p;

  memset(&p, 0, sizeof(p));
  // Initialize io_uring
  ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
  if (ret) {
    perror("io_uring_setup");
    return 1;
  }

  struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
  if (!sqe) {
    fprintf(stderr, "Failed to get SQE\n");
    io_uring_queue_exit(&ring);
    return 1;
  }

  // Setup a no-op operation (does nothing)
  io_uring_prep_nop(sqe);

  // Submit the SQE
  ret = io_uring_submit(&ring);
  if (ret < 0) {
    fprintf(stderr, "Failed to submit SQE: %s\n", strerror(-ret));
    io_uring_queue_exit(&ring);
    return 1;
  }

  // Wait for completion
  struct io_uring_cqe *cqe;
  ret = io_uring_wait_cqe(&ring, &cqe);
  if (ret < 0) {
    fprintf(stderr, "Failed to wait for CQE: %s\n", strerror(-ret));
    io_uring_queue_exit(&ring);
    return 1;
  }

  // Check result
  if (cqe->res < 0) {
    fprintf(stderr, "No-op operation failed: %s\n", strerror(-cqe->res));
    io_uring_cqe_seen(&ring, cqe);
    io_uring_queue_exit(&ring);
    return 1;
  }

  // Mark this CQE as seen
  io_uring_cqe_seen(&ring, cqe);

  printf("SQE operations are supported.\n");

  // Cleanup
  io_uring_queue_exit(&ring);

  return 0;
}
axboe commented 5 months ago

The only reason that could fail is if you have mixed liburing versions installed. Eg the program is compiled with a header that doesn't match the library it's linked with. Maybe you have multiple versions installed, either from source or maybe a distro package is installed alongside one you installed from source?

g2px1 commented 5 months ago

The only reason that could fail is if you have mixed liburing versions installed. Eg the program is compiled with a header that doesn't match the library it's linked with. Maybe you have multiple versions installed, either from source or maybe a distro package is installed alongside one you installed from source?

I’ll check it today, if it’s so, I’ll close the issue

g2px1 commented 5 months ago

The only reason that could fail is if you have mixed liburing versions installed. Eg the program is compiled with a header that doesn't match the library it's linked with. Maybe you have multiple versions installed, either from source or maybe a distro package is installed alongside one you installed from source?

I've tried provided example on several machines running under Ubuntu 22.04.3 and all of them got this issue

xiaguan commented 2 months ago

For my same case, after I tryied to apt remove liburing-dev or sudo make uninstall, install and remove from different source many times. All failed. But then I check the link dir, then I find this file .After I remove all liburing, it still reamins.

/lib/x86_64-linux-gnu/liburing.so.2.1.0

After I remove it, and rerun sudo make isntall. Everything works fine.

axboe commented 2 months ago

@xiaguan that's a good point, I've seen it end up there too as well in testing. Honestly not quite sure where that came from, never really investigated it.