Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions src/unix/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,22 @@ static unsigned int next_power_of_two(unsigned int val) {
return val;
}

/* The index of the watcher in loop->watchers, or -1. The descriptor may be already closed:
* then the index is found by a search. */
static int uv__io_slot(uv_loop_t* loop, uv__io_t* w) {
int slot;
unsigned int i;

slot = uv__kernel_fd(w->fd);
if (slot >= 0 && (unsigned) slot < loop->nwatchers && loop->watchers[slot] == w)
return slot;
for (i = 0; i < loop->nwatchers; i++)
if (loop->watchers[i] == w)
return i;
return -1;
}


static int maybe_resize(uv_loop_t* loop, unsigned int len) {
uv__io_t** watchers;
void* fake_watcher_list;
Expand Down Expand Up @@ -952,6 +968,7 @@ void uv__io_init(uv__io_t* w, uv__io_cb_t cb, int fd) {

int uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
int err;
int slot;

assert(uv__io_cb_get(w) >= UV__AHAFS_EVENT);
assert(uv__io_cb_get(w) <= UV__UDP_IO);
Expand All @@ -960,8 +977,12 @@ int uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
assert(w->fd >= 0);
assert(w->fd < INT_MAX);

slot = uv__kernel_fd(w->fd);
if (slot < 0)
return UV_EBADF;

w->pevents |= events;
err = maybe_resize(loop, w->fd + 1);
err = maybe_resize(loop, slot + 1);
if (err)
return err;

Expand All @@ -977,8 +998,8 @@ int uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
if (uv__queue_empty(&w->watcher_queue))
uv__queue_insert_tail(&loop->watcher_queue, &w->watcher_queue);

if (loop->watchers[w->fd] == NULL) {
loop->watchers[w->fd] = w;
if (loop->watchers[slot] == NULL) {
loop->watchers[slot] = w;
loop->nfds++;
}

Expand All @@ -1003,6 +1024,8 @@ int uv__io_init_start(uv_loop_t* loop,


void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
int slot;

assert(0 == (events & ~(POLLIN | POLLOUT | UV__POLLRDHUP | UV__POLLPRI)));
assert(0 != events);

Expand All @@ -1012,7 +1035,7 @@ void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
assert(w->fd >= 0);

/* Happens when uv__io_stop() is called on a handle that was never started. */
if ((unsigned) w->fd >= loop->nwatchers)
if (loop->nwatchers == 0)
return;

w->pevents &= ~events;
Expand All @@ -1022,9 +1045,10 @@ void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
uv__queue_init(&w->watcher_queue);
w->events = 0;

if (w == loop->watchers[w->fd]) {
slot = uv__io_slot(loop, w);
if (slot >= 0) {
assert(loop->nfds > 0);
loop->watchers[w->fd] = NULL;
loop->watchers[slot] = NULL;
loop->nfds--;
}
return;
Expand Down Expand Up @@ -1104,7 +1128,8 @@ void uv__io_poll_check(uv_loop_t* loop, sigset_t* pset) {
}

int uv__fd_exists(uv_loop_t* loop, int fd) {
return (unsigned) fd < loop->nwatchers && loop->watchers[fd] != NULL;
int slot = uv__kernel_fd(fd);
return slot >= 0 && (unsigned) slot < loop->nwatchers && loop->watchers[slot] != NULL;
}


Expand Down
13 changes: 13 additions & 0 deletions src/unix/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@
#include <fcntl.h> /* O_CLOEXEC and O_NONBLOCK, if supported. */
#include <stdio.h>
#include <errno.h>

/* ClickHouse: with its musl, the file descriptors visible to the program are a pseudorandom
* encoding of the kernel descriptors (see contrib/musl/src/internal/fdmap.h in ClickHouse).
* The table of watchers is indexed by the kernel descriptor, which is small, and the kernel
* reads the descriptors of io_uring submission entries directly. uv__kernel_fd returns -1 if
* the descriptor is not open. */
#if defined(USE_MUSL)
int __musl_fd_to_kernel(int fd);
int __musl_fd_from_kernel(int kernel_fd);
#define uv__kernel_fd(fd) __musl_fd_to_kernel(fd)
#else
#define uv__kernel_fd(fd) (fd)
#endif
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
Expand Down
29 changes: 20 additions & 9 deletions src/unix/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ int uv__iou_fs_close(uv_loop_t* loop, uv_fs_t* req) {
if (sqe == NULL)
return 0;

sqe->fd = req->file;
sqe->fd = uv__kernel_fd(req->file);
sqe->opcode = UV__IORING_OP_CLOSE;

uv__iou_submit(iou);
Expand All @@ -935,7 +935,7 @@ int uv__iou_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req) {
if (sqe == NULL)
return 0;

sqe->fd = req->file;
sqe->fd = uv__kernel_fd(req->file);
sqe->off = req->off;
sqe->opcode = UV__IORING_OP_FTRUNCATE;
uv__iou_submit(iou);
Expand All @@ -958,7 +958,7 @@ int uv__iou_fs_fsync_or_fdatasync(uv_loop_t* loop,
/* Little known fact: setting seq->off and seq->len turns
* it into an asynchronous sync_file_range() operation.
*/
sqe->fd = req->file;
sqe->fd = uv__kernel_fd(req->file);
sqe->fsync_flags = fsync_flags;
sqe->opcode = UV__IORING_OP_FSYNC;

Expand Down Expand Up @@ -1124,7 +1124,7 @@ int uv__iou_fs_read_or_write(uv_loop_t* loop,
return 0;

sqe->addr = (uintptr_t) req->bufs;
sqe->fd = req->file;
sqe->fd = uv__kernel_fd(req->file);
sqe->len = req->nbufs;
sqe->off = req->off < 0 ? -1 : req->off;
sqe->opcode = is_read ? UV__IORING_OP_READV : UV__IORING_OP_WRITEV;
Expand Down Expand Up @@ -1165,7 +1165,7 @@ int uv__iou_fs_statx(uv_loop_t* loop,

if (is_fstat) {
sqe->addr = (uintptr_t) "";
sqe->fd = req->file;
sqe->fd = uv__kernel_fd(req->file);
sqe->statx_flags |= 0x1000; /* AT_EMPTY_PATH */
}

Expand Down Expand Up @@ -1279,6 +1279,13 @@ static void uv__poll_io_uring(uv_loop_t* loop, struct uv__iou* iou) {
case UV_FS_STAT:
uv__iou_fs_statx_post(req);
break;
#if defined(USE_MUSL)
case UV_FS_OPEN:
/* The kernel returns a kernel descriptor, see uv__kernel_fd. */
if (req->result >= 0)
req->result = __musl_fd_from_kernel(req->result);
break;
#endif
default: /* Squelch -Wswitch warnings. */
break;
}
Expand Down Expand Up @@ -1342,9 +1349,9 @@ static void uv__epoll_ctl_prep(int epollfd,

memset(sqe, 0, sizeof(*sqe));
sqe->addr = (uintptr_t) pe;
sqe->fd = epollfd;
sqe->fd = uv__kernel_fd(epollfd);
sqe->len = op;
sqe->off = fd;
sqe->off = uv__kernel_fd(fd);
sqe->opcode = UV__IORING_OP_EPOLL_CTL;
sqe->user_data = op | slot << 2 | (int64_t) fd << 32;

Expand Down Expand Up @@ -1435,6 +1442,7 @@ void uv__io_poll(uv_loop_t* loop, int timeout) {
struct epoll_event e;
struct uv__iou* ctl;
struct uv__iou* iou;
int slot;
int real_timeout;
struct uv__queue* q;
uv__io_t* w;
Expand Down Expand Up @@ -1579,9 +1587,12 @@ void uv__io_poll(uv_loop_t* loop, int timeout) {
}

assert(fd >= 0);
assert((unsigned) fd < loop->nwatchers);

w = loop->watchers[fd];
/* The descriptor may be closed or reused by another watcher, see uv__kernel_fd. */
slot = uv__kernel_fd(fd);
w = slot >= 0 && (unsigned) slot < loop->nwatchers ? loop->watchers[slot] : NULL;
if (w != NULL && w->fd != fd)
w = NULL;

if (w == NULL) {
/* File descriptor that we've stopped watching, disarm it.
Expand Down
2 changes: 1 addition & 1 deletion src/unix/poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ int uv_poll_start(uv_poll_t* handle, int pevents, uv_poll_cb poll_cb) {
w = &handle->io_watcher;

if (uv__fd_exists(handle->loop, w->fd))
if (watchers[w->fd] != w)
if (watchers[uv__kernel_fd(w->fd)] != w)
return UV_EEXIST;

uv__poll_stop(handle);
Expand Down
Loading