File: io_uring_cancelation.7

package info (click to toggle)
liburing 2.14-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,448 kB
  • sloc: ansic: 59,512; sh: 816; makefile: 603; cpp: 32
file content (324 lines) | stat: -rw-r--r-- 8,430 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
.\" Copyright (C) 2025 Jens Axboe <axboe@kernel.dk>
.\" SPDX-License-Identifier: LGPL-2.0-or-later
.\"
.TH io_uring_cancelation 7 "January 18, 2025" "Linux" "Linux Programmer's Manual"
.SH NAME
io_uring_cancelation \- io_uring request cancelation overview
.SH DESCRIPTION
io_uring provides mechanisms to cancel in-flight requests before they
complete naturally. This is useful for implementing timeouts, handling
connection drops, closing connections that go away, or when a request is no
longer needed.
.SS Why cancel requests?
Common scenarios requiring cancelation:
.IP \(bu 2
.B Timeouts:
Cancel a read or accept that has been pending too long
.IP \(bu
.B Connection management:
Cancel pending operations when a connection is closed
.IP \(bu
.B Resource cleanup:
Cancel operations on a file descriptor being closed
.IP \(bu
.B Multishot termination:
Stop a multishot operation that is no longer needed
.SS Basic cancelation
The primary cancelation mechanism is
.B IORING_OP_ASYNC_CANCEL
(set up with
.BR io_uring_prep_cancel (3)
or related functions). By default, it cancels a request matching a
specific
.IR user_data :
.PP
.in +4n
.EX
/* Submit a read with user_data = 1234 */
sqe = io_uring_get_sqe(ring);
io_uring_prep_read(sqe, fd, buf, len, 0);
io_uring_sqe_set_data64(sqe, 1234);
io_uring_submit(ring);

/* Later, cancel it */
sqe = io_uring_get_sqe(ring);
io_uring_prep_cancel64(sqe, 1234, 0);
io_uring_submit(ring);
.EE
.in
.SS Cancelation results
When a cancelation is submitted, two CQEs are generated:
.PP
.B The canceled request's CQE:
.RS 4
.IP \(bu 2
.I res
is set to
.B -ECANCELED
(or occasionally
.B -EINTR
if the operation was already in progress)
.IP \(bu
The
.I user_data
identifies which request was canceled
.RE
.PP
.B The cancel request's CQE:
.RS 4
.IP \(bu 2
.I res
is 0 on success (request was found and canceled)
.IP \(bu
.I res
is
.B -ENOENT
if no matching request was found
.IP \(bu
.I res
is
.B -EALREADY
if the request was found but already completing
.RE
.PP
The order of these CQEs is not guaranteed. The application may receive
the cancel CQE before or after the canceled request's CQE.
.SS Cancelation flags
Various flags modify cancelation behavior:
.PP
.B IORING_ASYNC_CANCEL_ALL
.RS 4
Cancel all matching requests, not just the first one found. The cancel
CQE's
.I res
indicates how many requests were canceled.
.RE
.PP
.B IORING_ASYNC_CANCEL_FD
.RS 4
Match requests by file descriptor instead of
.IR user_data .
Cancels requests operating on the specified fd:
.PP
.in +4n
.EX
io_uring_prep_cancel_fd(sqe, fd, IORING_ASYNC_CANCEL_FD);
.EE
.in
.RE
.PP
.B IORING_ASYNC_CANCEL_ANY
.RS 4
Cancel any single request, ignoring
.I user_data
matching. Useful for draining a ring of all pending requests when
combined with
.BR IORING_ASYNC_CANCEL_ALL .
.RE
.PP
.B IORING_ASYNC_CANCEL_FD_FIXED
.RS 4
The file descriptor is a fixed file (registered file index) rather
than a regular fd.
.RE
.PP
Flags can be combined:
.PP
.in +4n
.EX
/* Cancel all requests on a specific fd */
io_uring_prep_cancel_fd(sqe, fd,
    IORING_ASYNC_CANCEL_FD | IORING_ASYNC_CANCEL_ALL);

/* Cancel all pending requests in the ring */
io_uring_prep_cancel(sqe, NULL,
    IORING_ASYNC_CANCEL_ANY | IORING_ASYNC_CANCEL_ALL);
.EE
.in
.SS Race conditions
Cancelation is inherently racy. Between submitting the cancel request
and the kernel processing it:
.IP \(bu 2
The target request may complete successfully
.IP \(bu
The target request may fail for another reason
.IP \(bu
The target request may already be in a non-cancelable state
.PP
Applications must handle these cases:
.PP
.in +4n
.EX
io_uring_wait_cqe(ring, &cqe);

if (cqe->user_data == cancel_user_data) {
    /* This is the cancel operation's result */
    if (cqe->res == -ENOENT) {
        /* Request already completed or not found */
    } else if (cqe->res == -EALREADY) {
        /* Request was found but completing */
    } else if (cqe->res >= 0) {
        /* Successfully canceled res requests */
    }
} else {
    /* This is the original request's result */
    if (cqe->res == -ECANCELED) {
        /* Request was canceled */
    } else {
        /* Request completed normally (or with error) */
    }
}
.EE
.in
.SS Link timeouts
For timing out a single operation, link timeouts are often simpler
than explicit cancelation. See
.BR io_uring_linked_requests (7)
for details:
.PP
.in +4n
.EX
sqe = io_uring_get_sqe(ring);
io_uring_prep_read(sqe, fd, buf, len, 0);
sqe->flags |= IOSQE_IO_LINK;

sqe = io_uring_get_sqe(ring);
io_uring_prep_link_timeout(sqe, &timeout, 0);
.EE
.in
.PP
The kernel handles the cancelation automatically if the timeout
expires.
.SS Canceling multishot requests
Multishot requests (see
.BR io_uring_multishot (7))
continue generating completions until canceled or an error occurs.
To stop a multishot request:
.PP
.in +4n
.EX
/* Cancel a multishot accept */
io_uring_prep_cancel64(sqe, accept_user_data, 0);
.EE
.in
.PP
After cancelation:
.IP \(bu 2
The multishot generates a final CQE with
.B -ECANCELED
.IP \(bu
The
.B IORING_CQE_F_MORE
flag is not set on this final CQE
.IP \(bu
The cancel CQE indicates success
.SS Canceling by file descriptor
When a file descriptor is closed (either via
.BR close (2)
or
.BR IORING_OP_CLOSE ),
pending requests operating on that fd are
.B not
automatically canceled. This differs from synchronous I/O behavior
and is a common source of confusion.

In synchronous I/O, closing a file descriptor is typically the last
reference to the underlying file, so the close completes any pending
operations. However, io_uring holds its own reference to the file for
each pending request. Closing the application's fd does not release
these references \(em the pending read, recv, or other operation
continues to hold a reference and will not automatically complete or
fail.

If an application expects a pending read on an fd to post a completion
when the fd is closed, that will not happen. The request must be
explicitly canceled:
.PP
.in +4n
.EX
/* Cancel all operations on fd before closing */
sqe = io_uring_get_sqe(ring);
io_uring_prep_cancel_fd(sqe, fd,
    IORING_ASYNC_CANCEL_FD | IORING_ASYNC_CANCEL_ALL);
io_uring_submit(ring);

/* Wait for cancelations, then close */
.EE
.in
.SS Shutdown cancelation
When an io_uring instance is closed (via
.BR io_uring_queue_exit (3)
or closing the ring file descriptor), all pending requests are
automatically canceled. Manual cancelation before shutdown is not
required.

However, if the application needs to ensure all requests are completed
before proceeding (e.g., to process their results or free associated
resources), explicit cancelation can be used:
.PP
.in +4n
.EX
/* Cancel everything */
sqe = io_uring_get_sqe(ring);
io_uring_prep_cancel(sqe, NULL,
    IORING_ASYNC_CANCEL_ANY | IORING_ASYNC_CANCEL_ALL);
io_uring_submit(ring);

/* Wait for all CQEs */
while (pending_count > 0) {
    io_uring_wait_cqe(ring, &cqe);
    pending_count--;
    io_uring_cqe_seen(ring, cqe);
}
.EE
.in
.SS Synchronous cancelation
For cases where the application needs to cancel requests and wait for
the cancelation to complete in a single blocking call,
.BR io_uring_register_sync_cancel (3)
provides a synchronous interface:
.PP
.in +4n
.EX
struct io_uring_sync_cancel_reg reg = {
    .addr = user_data,
    .timeout.tv_sec = 5,
};

ret = io_uring_register_sync_cancel(ring, &reg);
.EE
.in
.PP
This blocks until the matching request is canceled or the timeout
expires. It is useful when the application cannot easily integrate
asynchronous cancelation into its event loop.
.SH NOTES
.IP \(bu 2
Not all operations are cancelable. Operations that have already
been submitted to hardware (e.g., disk I/O in progress) typically
cannot be canceled.
.IP \(bu
Cancelation is asynchronous. The cancel request itself may take
time to complete.
.IP \(bu
When using
.BR IORING_ASYNC_CANCEL_ALL ,
the cancel CQE's
.I res
field contains the count of canceled requests.
.IP \(bu
Fixed files can be canceled using
.B IORING_ASYNC_CANCEL_FD_FIXED
with the file index instead of a regular fd.
.IP \(bu
Poll operations and multishot requests are generally good candidates
for cancelation. Completed disk I/O is not.
.SH SEE ALSO
.BR io_uring (7),
.BR io_uring_linked_requests (7),
.BR io_uring_multishot (7),
.BR io_uring_prep_cancel (3),
.BR io_uring_prep_cancel64 (3),
.BR io_uring_prep_cancel_fd (3),
.BR io_uring_prep_link_timeout (3),
.BR io_uring_register_sync_cancel (3)