io_uring: add a CQ ring flag to enable/disable eventfd notification
v2 cover io_uring: add a CQ ring flag to enable/disable eventfd notification
v1 -> v2:
- changed the flag name and behaviour from IORING_CQ_NEED_EVENT to IORING_CQ_EVENTFD_DISABLED [Jens]
The first patch adds the new 'cq_flags' field for the CQ ring. It should be written by the application and read by the kernel.
The second patch adds a new IORING_CQ_EVENTFD_DISABLED flag that can be used by the application to disable/enable eventfd notifications.
This feature can be useful if the application are using eventfd to be notified when requests are completed, but they don't want a notification for every request.
Of course the application can already remove the eventfd from the event loop, but as soon as it adds the eventfd again, it will be notified, even if it has already handled all the completed requests.
The most important use case is when the registered eventfd is used to notify a KVM guest through irqfd and we want a mechanism to enable/disable interrupts.
I also extended liburing API and added a test case here:
https://github.com/stefano-garzarella/liburing/tree/eventfd-disable
Stefano Garzarella (2):
io_uring: add 'cq_flags' field for the CQ ring
io_uring: add IORING_CQ_EVENTFD_DISABLED to the CQ ring flags
fs/io_uring.c | 12 +++++++++++- include/uapi/linux/io_uring.h | 11 ++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-)
Signature · 1 line
2.25.4
PATCH v2 1/2 io_uring: add 'cq_flags' field for the CQ ring
This patch adds the new 'cq_flags' field that should be written by the application and read by the kernel.
This new field is available to the userspace application through 'cq_off.flags'.
We are using 4-bytes previously reserved and set to zero. This means that if the application finds this field to zero, then the new functionality is not supported.
In the next patch we will introduce the first flag available.
--- fs/io_uring.c | 10 +++++++++- include/uapi/linux/io_uring.h | 4 +++- 2 files changed, 12 insertions(+), 2 deletions(-)
fs/io_uring.c +9 -1
@@ -142,7 +142,7 @@ struct io_rings {*/ u32 sq_dropped;/* -* Runtime flags +* Runtime SQ flags * * Written by the kernel, shouldn't be modified by the * application.
@@ -151,6 +151,13 @@ struct io_rings {* for IORING_SQ_NEED_WAKEUP after updating the sq tail. */ u32 sq_flags; +/* +* Runtime CQ flags +* +* Written by the application, shouldn't be modified by the +* kernel. +*/ +u32 cq_flags;/* * Number of completion events lost because the queue was full; * this should be avoided by the application by making sure
@@ -7834,6 +7841,7 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p, p->cq_off.ring_entries = offsetof(struct io_rings , cq_ring_entries); p->cq_off.overflow = offsetof(struct io_rings , cq_overflow); p->cq_off.cqes = offsetof(struct io_rings , cqes); + p->cq_off.flags = offsetof(struct io_rings , cq_flags); p->features =IORING_FEAT_SINGLE_MMAP |IORING_FEAT_NODROP | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
include/uapi/linux/io_uring.h +3 -1
@@ -204,7 +204,9 @@ struct io_cqring_offsets {__u32 ring_entries;__u32 overflow;__u32 cqes; -__u64 resv[2 ]; +__u32 flags; +__u32 resv1; +__u64 resv2; };/*
Signature · 1 line
2.25.4
PATCH v2 2/2 io_uring: add IORING_CQ_EVENTFD_DISABLED to the CQ ring flags
In reply to Stefano GarzarellaThis new flag should be set/clear from the application to disable/enable eventfd notifications when a request is completed and queued to the CQ ring.
Before this patch, notifications were always sent if an eventfd is registered, so IORING_CQ_EVENTFD_DISABLED is not set during the initialization.
It will be up to the application to set the flag after initialization if no notifications are required at the beginning.
---
v1 -> v2:
- changed the flag name and behaviour from IORING_CQ_NEED_EVENT to IORING_CQ_EVENTFD_DISABLED [Jens]
--- fs/io_uring.c | 2 ++ include/uapi/linux/io_uring.h | 7 +++++++ 2 files changed, 9 insertions(+)
fs/io_uring.c +2 -0
@@ -1152,6 +1152,8 @@ static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) {if (!ctx->cq_ev_fd)return false; +if (READ_ONCE (ctx->rings->cq_flags) &IORING_CQ_EVENTFD_DISABLED ) +return false;if (!ctx->eventfd_async)return true;return io_wq_current_is_worker ();
include/uapi/linux/io_uring.h +7 -0
@@ -209,6 +209,13 @@ struct io_cqring_offsets {__u64 resv2; }; +/* +* cq_ring->flags +*/ + +/* disable eventfd notifications */ +#define IORING_CQ_EVENTFD_DISABLED (1U << 0) +/* * io_uring_enter(2) flags */
Signature · 1 line
2.25.4
Quoted 17 lines · Stefano Garzarella
v1 -> v2:
- changed the flag name and behaviour fromIORING_CQ_NEED_EVENTtoIORING_CQ_EVENTFD_DISABLED[Jens]The first patch adds the new '
cq_flags' field for the CQ ring. It should be written by the application and read by the kernel.The second patch adds a new
IORING_CQ_EVENTFD_DISABLEDflag that can be used by the application to disable/enable eventfd notifications.This feature can be useful if the application are using eventfd to be notified when requests are completed, but they don't want a notification for every request.
Of course the application can already remove the eventfd from the event loop, but as soon as it adds the eventfd again, it will be notified, even if it has already handled all the completed requests.
Quoted 3 lines
The most important use case is when the registered eventfd is used to notify a KVM guest through irqfd and we want a mechanism to enable/disable interrupts.
Thanks, applied.
Signature · 1 line
Jens Axboe