History

Merge tag 'for-5.19/io_uring-2022-05-22' of git://git.kernel.dk/linux-block

Pull io_uring updates from Jens Axboe: "Here are the main io_uring changes for 5.19. This contains: - Fixes for sparse type warnings (Christoph, Vasily) - Support for multi-shot accept (Hao) - Support for io_uring managed fixed files, rather than always needing the applicationt o manage the indices (me) - Fix for a spurious poll wakeup (Dylan) - CQE overflow fixes (Dylan) - Support more types of cancelations (me) - Support for co-operative task_work signaling, rather than always forcing an IPI (me) - Support for doing poll first when appropriate, rather than always attempting a transfer first (me) - Provided buffer cleanups and support for mapped buffers (me) - Improve how io_uring handles inflight SCM files (Pavel) - Speedups for registered files (Pavel, me) - Organize the completion data in a struct in io_kiocb rather than keep it in separate spots (Pavel) - task_work improvements (Pavel) - Cleanup and optimize the submission path, in general and for handling links (Pavel) - Speedups for registered resource handling (Pavel) - Support sparse buffers and file maps (Pavel, me) - Various fixes and cleanups (Almog, Pavel, me)" * tag 'for-5.19/io_uring-2022-05-22' of git://git.kernel.dk/linux-block: (111 commits) io_uring: fix incorrect __kernel_rwf_t cast io_uring: disallow mixed provided buffer group registrations io_uring: initialize io_buffer_list head when shared ring is unregistered io_uring: add fully sparse buffer registration io_uring: use rcu_dereference in io_close io_uring: consistently use the EPOLL* defines io_uring: make apoll_events a __poll_t io_uring: drop a spurious inline on a forward declaration io_uring: don't use ERR_PTR for user pointers io_uring: use a rwf_t for io_rw.flags io_uring: add support for ring mapped supplied buffers io_uring: add io_pin_pages() helper io_uring: add buffer selection support to IORING_OP_NOP io_uring: fix locking state for empty buffer group io_uring: implement multishot mode for accept io_uring: let fast poll support multishot io_uring: add REQ_F_APOLL_MULTISHOT for requests io_uring: add IORING_ACCEPT_MULTISHOT for accept io_uring: only wake when the correct events are set io_uring: avoid io-wq -EAGAIN looping for !IOPOLL ...

Linus Torvalds committed 4 years ago

Commit 3a166bd2 parents 1e57930 0e7579c8 files · +1813 −1031Browse files

Unified · Split

8 files
  1. fs/io-wq.c +2 −2
  2. fs/io-wq.h +1 −0
  3. fs/io_uring.c +1643 −1018
  4. include/linux/sched/signal.h +11 −2
  5. include/linux/task_work.h +1 −0
  6. include/trace/events/io_uring.h +42 −2
  7. include/uapi/linux/io_uring.h +94 −1
  8. kernel/task_work.c +19 −6
fs/io-wq.c+2−2
@@ -871,7 +871,7 @@ static bool io_wq_for_each_worker(struct io_wqe *wqe, · 870 unmodified lines
871871
872872 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
873873 {
874− set_notify_signal(worker->task);
874+ __set_notify_signal(worker->task);
875875 wake_up_process(worker->task);
876876 return false;
877877 }
@@ -991,7 +991,7 @@ static bool __io_wq_worker_cancel(struct io_worker *worker, · 113 unmodified lines
991991 {
992992 if (work && match->fn(work, match->data)) {
993993 work->flags |= IO_WQ_WORK_CANCEL;
994− set_notify_signal(worker->task);
994+ __set_notify_signal(worker->task);
995995 return true;
996996 }
997997
fs/io-wq.h+1−0
@@ -155,6 +155,7 @@ struct io_wq_work_node *wq_stack_extract(struct io_wq_work_node *stack) · 154 unmodified lines
155155 struct io_wq_work {
156156 struct io_wq_work_node list;
157157 unsigned flags;
158+ int cancel_seq;
158159 };
159160
160161 static inline struct io_wq_work *wq_next_work(struct io_wq_work *work)
fs/io_uring.c+1643−1018
@@ -94,7 +94,7 @@ · 93 unmodified lines
9494 #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8
9595
9696 /* only define max */
97−#define IORING_MAX_FIXED_FILES (1U << 15)
97+#define IORING_MAX_FIXED_FILES (1U << 20)
9898 #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
9999 IORING_REGISTER_LAST + IORING_OP_LAST)
100100
@@ -113,6 +113,11 @@ · 12 unmodified lines
113113 #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \
114114 REQ_F_POLLED | REQ_F_CREDS | REQ_F_ASYNC_DATA)
115115
116+#define IO_REQ_CLEAN_SLOW_FLAGS (REQ_F_REFCOUNT | REQ_F_LINK | REQ_F_HARDLINK |\
117+ IO_REQ_CLEAN_FLAGS)
118+
119+#define IO_APOLL_MULTI_POLLED (REQ_F_APOLL_MULTISHOT | REQ_F_POLLED)
120+
116121 #define IO_TCTX_REFS_CACHE_NR (1U << 10)
117122
118123 struct io_uring {
@@ -166,7 +171,7 @@ struct io_rings { · 47 unmodified lines
166171 * The application needs a full memory barrier before checking
167172 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
168173 */
169− u32 sq_flags;
174+ atomic_t sq_flags;
170175 /*
171176 * Runtime CQ flags
172177 *
@@ -220,6 +225,23 @@ struct io_overflow_cqe { · 47 unmodified lines
220225 struct list_head list;
221226 };
222227
228+/*
229+ * FFS_SCM is only available on 64-bit archs, for 32-bit we just define it as 0
230+ * and define IO_URING_SCM_ALL. For this case, we use SCM for all files as we
231+ * can't safely always dereference the file when the task has exited and ring
232+ * cleanup is done. If a file is tracked and part of SCM, then unix gc on
233+ * process exit may reap it before __io_sqe_files_unregister() is run.
234+ */
235+#define FFS_NOWAIT 0x1UL
236+#define FFS_ISREG 0x2UL
237+#if defined(CONFIG_64BIT)
238+#define FFS_SCM 0x4UL
239+#else
240+#define IO_URING_SCM_ALL
241+#define FFS_SCM 0x0UL
242+#endif
243+#define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG|FFS_SCM)
244+
223245 struct io_fixed_file {
224246 /* file * with additional FFS_* flags */
225247 unsigned long file_ptr;
@@ -237,6 +259,8 @@ struct io_rsrc_put { · 11 unmodified lines
237259
238260 struct io_file_table {
239261 struct io_fixed_file *files;
262+ unsigned long *bitmap;
263+ unsigned int alloc_hint;
240264 };
241265
242266 struct io_rsrc_node {
@@ -261,10 +285,26 @@ struct io_rsrc_data { · 18 unmodified lines
261285 bool quiesce;
262286 };
263287
288+#define IO_BUFFER_LIST_BUF_PER_PAGE (PAGE_SIZE / sizeof(struct io_uring_buf))
264289 struct io_buffer_list {
265− struct list_head list;
266− struct list_head buf_list;
290+ /*
291+ * If ->buf_nr_pages is set, then buf_pages/buf_ring are used. If not,
292+ * then these are classic provided buffers and ->buf_list is used.
293+ */
294+ union {
295+ struct list_head buf_list;
296+ struct {
297+ struct page **buf_pages;
298+ struct io_uring_buf_ring *buf_ring;
299+ };
300+ };
267301 __u16 bgid;
302+
303+ /* below is for ring provided buffers */
304+ __u16 buf_nr_pages;
305+ __u16 nr_entries;
306+ __u32 head;
307+ __u32 mask;
268308 };
269309
270310 struct io_buffer {
@@ -337,7 +377,7 @@ struct io_ev_fd { · 66 unmodified lines
337377 struct rcu_head rcu;
338378 };
339379
340−#define IO_BUFFERS_HASH_BITS 5
380+#define BGID_ARRAY 64
341381
342382 struct io_ring_ctx {
343383 /* const or read-mostly hot data */
@@ -346,6 +386,7 @@ struct io_ring_ctx { · 2 unmodified lines
346386
347387 struct io_rings *rings;
348388 unsigned int flags;
389+ enum task_work_notify_mode notify_method;
349390 unsigned int compat: 1;
350391 unsigned int drain_next: 1;
351392 unsigned int restricted: 1;
@@ -353,6 +394,7 @@ struct io_ring_ctx { · 1 unmodified line
353394 unsigned int drain_active: 1;
354395 unsigned int drain_disabled: 1;
355396 unsigned int has_evfd: 1;
397+ unsigned int syscall_iopoll: 1;
356398 } ____cacheline_aligned_in_smp;
357399
358400 /* submission data */
@@ -382,17 +424,21 @@ struct io_ring_ctx { · 23 unmodified lines
382424 */
383425 struct io_rsrc_node *rsrc_node;
384426 int rsrc_cached_refs;
427+ atomic_t cancel_seq;
385428 struct io_file_table file_table;
386429 unsigned nr_user_files;
387430 unsigned nr_user_bufs;
388431 struct io_mapped_ubuf **user_bufs;
389432
390433 struct io_submit_state submit_state;
434+
435+ struct io_buffer_list *io_bl;
436+ struct xarray io_bl_xa;
437+ struct list_head io_buffers_cache;
438+
391439 struct list_head timeout_list;
392440 struct list_head ltimeout_list;
393441 struct list_head cq_overflow_list;
394− struct list_head *io_buffers;
395− struct list_head io_buffers_cache;
396442 struct list_head apoll_cache;
397443 struct xarray personalities;
398444 u32 pers_next;
@@ -409,9 +455,16 @@ struct io_ring_ctx { · 10 unmodified lines
409455 struct wait_queue_head sqo_sq_wait;
410456 struct list_head sqd_list;
411457
412− unsigned long check_cq_overflow;
458+ unsigned long check_cq;
413459
414460 struct {
461+ /*
462+ * We cache a range of free CQEs we can use, once exhausted it
463+ * should go through a slower range setup, see __io_get_cqe()
464+ */
465+ struct io_uring_cqe *cqe_cached;
466+ struct io_uring_cqe *cqe_sentinel;
467+
415468 unsigned cached_cq_tail;
416469 unsigned cq_entries;
417470 struct io_ev_fd __rcu *io_ev_fd;
@@ -557,6 +610,8 @@ struct io_sync { · 139 unmodified lines
557610 struct io_cancel {
558611 struct file *file;
559612 u64 addr;
613+ u32 flags;
614+ s32 fd;
560615 };
561616
562617 struct io_timeout {
@@ -585,7 +640,7 @@ struct io_rw { · 22 unmodified lines
585640 struct kiocb kiocb;
586641 u64 addr;
587642 u32 len;
588− u32 flags;
643+ rwf_t flags;
589644 };
590645
591646 struct io_connect {
@@ -602,9 +657,9 @@ struct io_sr_msg { · 10 unmodified lines
602657 void __user *buf;
603658 };
604659 int msg_flags;
605− int bgid;
606660 size_t len;
607661 size_t done_io;
662+ unsigned int flags;
608663 };
609664
610665 struct io_open {
@@ -766,6 +821,7 @@ enum { · 155 unmodified lines
766821 REQ_F_NEED_CLEANUP_BIT,
767822 REQ_F_POLLED_BIT,
768823 REQ_F_BUFFER_SELECTED_BIT,
824+ REQ_F_BUFFER_RING_BIT,
769825 REQ_F_COMPLETE_INLINE_BIT,
770826 REQ_F_REISSUE_BIT,
771827 REQ_F_CREDS_BIT,
@@ -776,6 +832,7 @@ enum { · 4 unmodified lines
776832 REQ_F_SINGLE_POLL_BIT,
777833 REQ_F_DOUBLE_POLL_BIT,
778834 REQ_F_PARTIAL_IO_BIT,
835+ REQ_F_APOLL_MULTISHOT_BIT,
779836 /* keep async read/write and isreg together and in order */
780837 REQ_F_SUPPORT_NOWAIT_BIT,
781838 REQ_F_ISREG_BIT,
@@ -816,6 +873,8 @@ enum { · 34 unmodified lines
816873 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
817874 /* buffer already selected */
818875 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
876+ /* buffer selected from ring, needs commit */
877+ REQ_F_BUFFER_RING = BIT(REQ_F_BUFFER_RING_BIT),
819878 /* completion is deferred through io_comp_state */
820879 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
821880 /* caller should reissue async */
@@ -840,6 +899,8 @@ enum { · 18 unmodified lines
840899 REQ_F_DOUBLE_POLL = BIT(REQ_F_DOUBLE_POLL_BIT),
841900 /* request has already done partial IO */
842901 REQ_F_PARTIAL_IO = BIT(REQ_F_PARTIAL_IO_BIT),
902+ /* fast poll multishot mode */
903+ REQ_F_APOLL_MULTISHOT = BIT(REQ_F_APOLL_MULTISHOT_BIT),
843904 };
844905
845906 struct async_poll {
@@ -862,6 +923,21 @@ enum { · 16 unmodified lines
862923 IORING_RSRC_BUFFER = 1,
863924 };
864925
926+struct io_cqe {
927+ __u64 user_data;
928+ __s32 res;
929+ /* fd initially, then cflags for completion */
930+ union {
931+ __u32 flags;
932+ int fd;
933+ };
934+};
935+
936+enum {
937+ IO_CHECK_CQ_OVERFLOW_BIT,
938+ IO_CHECK_CQ_DROPPED_BIT,
939+};
940+
865941 /*
866942 * NOTE! Each of the iocb union members has the file pointer
867943 * as the first entry in their struct definition. So you can
@@ -902,29 +978,40 @@ struct io_kiocb { · 34 unmodified lines
902978 u8 opcode;
903979 /* polled IO has completed */
904980 u8 iopoll_completed;
981+ /*
982+ * Can be either a fixed buffer index, or used with provided buffers.
983+ * For the latter, before issue it points to the buffer group ID,
984+ * and after selection it points to the buffer ID itself.
985+ */
905986 u16 buf_index;
906987 unsigned int flags;
907988
908− u64 user_data;
909− u32 result;
910− /* fd initially, then cflags for completion */
911− union {
912− u32 cflags;
913− int fd;
914− };
989+ struct io_cqe cqe;
915990
916991 struct io_ring_ctx *ctx;
917992 struct task_struct *task;
918993
919− struct percpu_ref *fixed_rsrc_refs;
920− /* store used ubuf, so we can prevent reloading */
921− struct io_mapped_ubuf *imu;
994+ struct io_rsrc_node *rsrc_node;
995+
996+ union {
997+ /* store used ubuf, so we can prevent reloading */
998+ struct io_mapped_ubuf *imu;
999+
1000+ /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
1001+ struct io_buffer *kbuf;
1002+
1003+ /*
1004+ * stores buffer ID for ring provided buffers, valid IFF
1005+ * REQ_F_BUFFER_RING is set.
1006+ */
1007+ struct io_buffer_list *buf_list;
1008+ };
9221009
9231010 union {
9241011 /* used by request caches, completion batching and iopoll */
9251012 struct io_wq_work_node comp_list;
9261013 /* cache ->apoll->events */
927− int apoll_events;
1014+ __poll_t apoll_events;
9281015 };
9291016 atomic_t refs;
9301017 atomic_t poll_refs;
@@ -935,8 +1022,6 @@ struct io_kiocb { · 4 unmodified lines
9351022 struct async_poll *apoll;
9361023 /* opcode allocated if it needs to store data for async defer */
9371024 void *async_data;
938− /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
939− struct io_buffer *kbuf;
9401025 /* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */
9411026 struct io_kiocb *link;
9421027 /* custom credentials, valid IFF REQ_F_CREDS is set */
@@ -956,6 +1041,16 @@ struct io_defer_entry { · 13 unmodified lines
9561041 u32 seq;
9571042 };
9581043
1044+struct io_cancel_data {
1045+ struct io_ring_ctx *ctx;
1046+ union {
1047+ u64 data;
1048+ struct file *file;
1049+ };
1050+ u32 flags;
1051+ int seq;
1052+};
1053+
9591054 struct io_op_def {
9601055 /* needs req->file assigned */
9611056 unsigned needs_file : 1;
@@ -977,12 +1072,20 @@ struct io_op_def { · 15 unmodified lines
9771072 unsigned not_supported : 1;
9781073 /* skip auditing */
9791074 unsigned audit_skip : 1;
1075+ /* supports ioprio */
1076+ unsigned ioprio : 1;
1077+ /* supports iopoll */
1078+ unsigned iopoll : 1;
9801079 /* size of async data needed, if any */
9811080 unsigned short async_size;
9821081 };
9831082
9841083 static const struct io_op_def io_op_defs[] = {
985− [IORING_OP_NOP] = {},
1084+ [IORING_OP_NOP] = {
1085+ .audit_skip = 1,
1086+ .iopoll = 1,
1087+ .buffer_select = 1,
1088+ },
9861089 [IORING_OP_READV] = {
9871090 .needs_file = 1,
9881091 .unbound_nonreg_file = 1,
@@ -991,6 +1094,8 @@ static const struct io_op_def io_op_defs[] = { · 2 unmodified lines
9911094 .needs_async_setup = 1,
9921095 .plug = 1,
9931096 .audit_skip = 1,
1097+ .ioprio = 1,
1098+ .iopoll = 1,
9941099 .async_size = sizeof(struct io_async_rw),
9951100 },
9961101 [IORING_OP_WRITEV] = {
@@ -1001,6 +1106,8 @@ static const struct io_op_def io_op_defs[] = { · 4 unmodified lines
10011106 .needs_async_setup = 1,
10021107 .plug = 1,
10031108 .audit_skip = 1,
1109+ .ioprio = 1,
1110+ .iopoll = 1,
10041111 .async_size = sizeof(struct io_async_rw),
10051112 },
10061113 [IORING_OP_FSYNC] = {
@@ -1013,6 +1120,8 @@ static const struct io_op_def io_op_defs[] = { · 6 unmodified lines
10131120 .pollin = 1,
10141121 .plug = 1,
10151122 .audit_skip = 1,
1123+ .ioprio = 1,
1124+ .iopoll = 1,
10161125 .async_size = sizeof(struct io_async_rw),
10171126 },
10181127 [IORING_OP_WRITE_FIXED] = {
@@ -1022,6 +1131,8 @@ static const struct io_op_def io_op_defs[] = { · 3 unmodified lines
10221131 .pollout = 1,
10231132 .plug = 1,
10241133 .audit_skip = 1,
1134+ .ioprio = 1,
1135+ .iopoll = 1,
10251136 .async_size = sizeof(struct io_async_rw),
10261137 },
10271138 [IORING_OP_POLL_ADD] = {
@@ -1064,6 +1175,7 @@ static const struct io_op_def io_op_defs[] = { · 36 unmodified lines
10641175 .unbound_nonreg_file = 1,
10651176 .pollin = 1,
10661177 .poll_exclusive = 1,
1178+ .ioprio = 1, /* used for flags */
10671179 },
10681180 [IORING_OP_ASYNC_CANCEL] = {
10691181 .audit_skip = 1,
@@ -1086,6 +1198,7 @@ static const struct io_op_def io_op_defs[] = { · 16 unmodified lines
10861198 [IORING_OP_CLOSE] = {},
10871199 [IORING_OP_FILES_UPDATE] = {
10881200 .audit_skip = 1,
1201+ .iopoll = 1,
10891202 },
10901203 [IORING_OP_STATX] = {
10911204 .audit_skip = 1,
@@ -1097,6 +1210,8 @@ static const struct io_op_def io_op_defs[] = { · 5 unmodified lines
10971210 .buffer_select = 1,
10981211 .plug = 1,
10991212 .audit_skip = 1,
1213+ .ioprio = 1,
1214+ .iopoll = 1,
11001215 .async_size = sizeof(struct io_async_rw),
11011216 },
11021217 [IORING_OP_WRITE] = {
@@ -1106,6 +1221,8 @@ static const struct io_op_def io_op_defs[] = { · 3 unmodified lines
11061221 .pollout = 1,
11071222 .plug = 1,
11081223 .audit_skip = 1,
1224+ .ioprio = 1,
1225+ .iopoll = 1,
11091226 .async_size = sizeof(struct io_async_rw),
11101227 },
11111228 [IORING_OP_FADVISE] = {
@@ -1140,9 +1257,11 @@ static const struct io_op_def io_op_defs[] = { · 28 unmodified lines
11401257 },
11411258 [IORING_OP_PROVIDE_BUFFERS] = {
11421259 .audit_skip = 1,
1260+ .iopoll = 1,
11431261 },
11441262 [IORING_OP_REMOVE_BUFFERS] = {
11451263 .audit_skip = 1,
1264+ .iopoll = 1,
11461265 },
11471266 [IORING_OP_TEE] = {
11481267 .needs_file = 1,
@@ -1160,11 +1279,13 @@ static const struct io_op_def io_op_defs[] = { · 11 unmodified lines
11601279 [IORING_OP_LINKAT] = {},
11611280 [IORING_OP_MSG_RING] = {
11621281 .needs_file = 1,
1282+ .iopoll = 1,
11631283 },
11641284 };
11651285
11661286 /* requests with any of those set should undergo io_disarm_next() */
11671287 #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1288+#define IO_REQ_LINK_FLAGS (REQ_F_LINK | REQ_F_HARDLINK)
11681289
11691290 static bool io_disarm_next(struct io_kiocb *req);
11701291 static void io_uring_del_tctx_node(unsigned long index);
@@ -1173,10 +1294,7 @@ static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, · 2 unmodified lines
11731294 bool cancel_all);
11741295 static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
11751296
1176−static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags);
1177−
1178−static void io_put_req(struct io_kiocb *req);
1179−static void io_put_req_deferred(struct io_kiocb *req);
1297+static void __io_req_complete_post(struct io_kiocb *req, s32 res, u32 cflags);
11801298 static void io_dismantle_req(struct io_kiocb *req);
11811299 static void io_queue_linked_timeout(struct io_kiocb *req);
11821300 static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
@@ -1185,10 +1303,10 @@ static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, · 2 unmodified lines
11851303 static void io_clean_op(struct io_kiocb *req);
11861304 static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
11871305 unsigned issue_flags);
1188−static inline struct file *io_file_get_normal(struct io_kiocb *req, int fd);
1306+static struct file *io_file_get_normal(struct io_kiocb *req, int fd);
11891307 static void io_drop_inflight_file(struct io_kiocb *req);
11901308 static bool io_assign_file(struct io_kiocb *req, unsigned int issue_flags);
1191−static void __io_queue_sqe(struct io_kiocb *req);
1309+static void io_queue_sqe(struct io_kiocb *req);
11921310 static void io_rsrc_put_work(struct work_struct *work);
11931311
11941312 static void io_req_task_queue(struct io_kiocb *req);
@@ -1201,6 +1319,7 @@ static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); · 6 unmodified lines
12011319
12021320 static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer);
12031321 static void io_eventfd_signal(struct io_ring_ctx *ctx);
1322+static void io_req_tw_post_queue(struct io_kiocb *req, s32 res, u32 cflags);
12041323
12051324 static struct kmem_cache *req_cachep;
12061325
@@ -1219,6 +1338,42 @@ struct sock *io_uring_get_socket(struct file *file) · 12 unmodified lines
12191338 }
12201339 EXPORT_SYMBOL(io_uring_get_socket);
12211340
1341+#if defined(CONFIG_UNIX)
1342+static inline bool io_file_need_scm(struct file *filp)
1343+{
1344+#if defined(IO_URING_SCM_ALL)
1345+ return true;
1346+#else
1347+ return !!unix_get_socket(filp);
1348+#endif
1349+}
1350+#else
1351+static inline bool io_file_need_scm(struct file *filp)
1352+{
1353+ return false;
1354+}
1355+#endif
1356+
1357+static void io_ring_submit_unlock(struct io_ring_ctx *ctx, unsigned issue_flags)
1358+{
1359+ lockdep_assert_held(&ctx->uring_lock);
1360+ if (issue_flags & IO_URING_F_UNLOCKED)
1361+ mutex_unlock(&ctx->uring_lock);
1362+}
1363+
1364+static void io_ring_submit_lock(struct io_ring_ctx *ctx, unsigned issue_flags)
1365+{
1366+ /*
1367+ * "Normal" inline submissions always hold the uring_lock, since we
1368+ * grab it from the system call. Same is true for the SQPOLL offload.
1369+ * The only exception is when we've detached the request and issue it
1370+ * from an async worker thread, grab the lock for that case.
1371+ */
1372+ if (issue_flags & IO_URING_F_UNLOCKED)
1373+ mutex_lock(&ctx->uring_lock);
1374+ lockdep_assert_held(&ctx->uring_lock);
1375+}
1376+
12221377 static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
12231378 {
12241379 if (!*locked) {
@@ -1280,31 +1435,36 @@ static inline void io_req_set_refcount(struct io_kiocb *req) · 55 unmodified lines
12801435
12811436 #define IO_RSRC_REF_BATCH 100
12821437
1438+static void io_rsrc_put_node(struct io_rsrc_node *node, int nr)
1439+{
1440+ percpu_ref_put_many(&node->refs, nr);
1441+}
1442+
12831443 static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
12841444 struct io_ring_ctx *ctx)
12851445 __must_hold(&ctx->uring_lock)
12861446 {
1287− struct percpu_ref *ref = req->fixed_rsrc_refs;
1447+ struct io_rsrc_node *node = req->rsrc_node;
12881448
1289− if (ref) {
1290− if (ref == &ctx->rsrc_node->refs)
1449+ if (node) {
1450+ if (node == ctx->rsrc_node)
12911451 ctx->rsrc_cached_refs++;
12921452 else
1293− percpu_ref_put(ref);
1453+ io_rsrc_put_node(node, 1);
12941454 }
12951455 }
12961456
1297−static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx)
1457+static inline void io_req_put_rsrc(struct io_kiocb *req)
12981458 {
1299− if (req->fixed_rsrc_refs)
1300− percpu_ref_put(req->fixed_rsrc_refs);
1459+ if (req->rsrc_node)
1460+ io_rsrc_put_node(req->rsrc_node, 1);
13011461 }
13021462
13031463 static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx)
13041464 __must_hold(&ctx->uring_lock)
13051465 {
13061466 if (ctx->rsrc_cached_refs) {
1307− percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs);
1467+ io_rsrc_put_node(ctx->rsrc_node, ctx->rsrc_cached_refs);
13081468 ctx->rsrc_cached_refs = 0;
13091469 }
13101470 }
@@ -1320,8 +1480,8 @@ static inline void io_req_set_rsrc_node(struct io_kiocb *req, · 9 unmodified lines
13201480 struct io_ring_ctx *ctx,
13211481 unsigned int issue_flags)
13221482 {
1323− if (!req->fixed_rsrc_refs) {
1324− req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
1483+ if (!req->rsrc_node) {
1484+ req->rsrc_node = ctx->rsrc_node;
13251485
13261486 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
13271487 lockdep_assert_held(&ctx->uring_lock);
@@ -1329,28 +1489,30 @@ static inline void io_req_set_rsrc_node(struct io_kiocb *req, · 1 unmodified line
13291489 if (unlikely(ctx->rsrc_cached_refs < 0))
13301490 io_rsrc_refs_refill(ctx);
13311491 } else {
1332− percpu_ref_get(req->fixed_rsrc_refs);
1492+ percpu_ref_get(&req->rsrc_node->refs);
13331493 }
13341494 }
13351495 }
13361496
13371497 static unsigned int __io_put_kbuf(struct io_kiocb *req, struct list_head *list)
13381498 {
1339− struct io_buffer *kbuf = req->kbuf;
1340− unsigned int cflags;
1499+ if (req->flags & REQ_F_BUFFER_RING) {
1500+ if (req->buf_list)
1501+ req->buf_list->head++;
1502+ req->flags &= ~REQ_F_BUFFER_RING;
1503+ } else {
1504+ list_add(&req->kbuf->list, list);
1505+ req->flags &= ~REQ_F_BUFFER_SELECTED;
1506+ }
13411507
1342− cflags = IORING_CQE_F_BUFFER | (kbuf->bid << IORING_CQE_BUFFER_SHIFT);
1343− req->flags &= ~REQ_F_BUFFER_SELECTED;
1344− list_add(&kbuf->list, list);
1345− req->kbuf = NULL;
1346− return cflags;
1508+ return IORING_CQE_F_BUFFER | (req->buf_index << IORING_CQE_BUFFER_SHIFT);
13471509 }
13481510
13491511 static inline unsigned int io_put_kbuf_comp(struct io_kiocb *req)
13501512 {
13511513 lockdep_assert_held(&req->ctx->completion_lock);
13521514
1353− if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
1515+ if (!(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)))
13541516 return 0;
13551517 return __io_put_kbuf(req, &req->ctx->io_buffers_comp);
13561518 }
@@ -1360,7 +1522,7 @@ static inline unsigned int io_put_kbuf(struct io_kiocb *req, · 3 unmodified lines
13601522 {
13611523 unsigned int cflags;
13621524
1363− if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
1525+ if (!(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)))
13641526 return 0;
13651527
13661528 /*
@@ -1375,7 +1537,10 @@ static inline unsigned int io_put_kbuf(struct io_kiocb *req, · 8 unmodified lines
13751537 * We migrate buffers from the comp_list to the issue cache list
13761538 * when we need one.
13771539 */
1378− if (issue_flags & IO_URING_F_UNLOCKED) {
1540+ if (req->flags & REQ_F_BUFFER_RING) {
1541+ /* no buffers to recycle for this case */
1542+ cflags = __io_put_kbuf(req, NULL);
1543+ } else if (issue_flags & IO_URING_F_UNLOCKED) {
13791544 struct io_ring_ctx *ctx = req->ctx;
13801545
13811546 spin_lock(&ctx->completion_lock);
@@ -1393,15 +1558,10 @@ static inline unsigned int io_put_kbuf(struct io_kiocb *req, · 11 unmodified lines
13931558 static struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx,
13941559 unsigned int bgid)
13951560 {
1396− struct list_head *hash_list;
1397− struct io_buffer_list *bl;
1398−
1399− hash_list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)];
1400− list_for_each_entry(bl, hash_list, list)
1401− if (bl->bgid == bgid || bgid == -1U)
1402− return bl;
1561+ if (ctx->io_bl && bgid < BGID_ARRAY)
1562+ return &ctx->io_bl[bgid];
14031563
1404− return NULL;
1564+ return xa_load(&ctx->io_bl_xa, bgid);
14051565 }
14061566
14071567 static void io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags)
@@ -1410,25 +1570,33 @@ static void io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags) · 2 unmodified lines
14101570 struct io_buffer_list *bl;
14111571 struct io_buffer *buf;
14121572
1413− if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
1573+ if (!(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)))
14141574 return;
14151575 /* don't recycle if we already did IO to this buffer */
14161576 if (req->flags & REQ_F_PARTIAL_IO)
14171577 return;
1578+ /*
1579+ * We don't need to recycle for REQ_F_BUFFER_RING, we can just clear
1580+ * the flag and hence ensure that bl->head doesn't get incremented.
1581+ * If the tail has already been incremented, hang on to it.
1582+ */
1583+ if (req->flags & REQ_F_BUFFER_RING) {
1584+ if (req->buf_list) {
1585+ req->buf_index = req->buf_list->bgid;
1586+ req->flags &= ~REQ_F_BUFFER_RING;
1587+ }
1588+ return;
1589+ }
14181590
1419− if (issue_flags & IO_URING_F_UNLOCKED)
1420− mutex_lock(&ctx->uring_lock);
1421−
1422− lockdep_assert_held(&ctx->uring_lock);
1591+ io_ring_submit_lock(ctx, issue_flags);
14231592
14241593 buf = req->kbuf;
14251594 bl = io_buffer_get_list(ctx, buf->bgid);
14261595 list_add(&buf->list, &bl->buf_list);
14271596 req->flags &= ~REQ_F_BUFFER_SELECTED;
1428− req->kbuf = NULL;
1597+ req->buf_index = buf->bgid;
14291598
1430− if (issue_flags & IO_URING_F_UNLOCKED)
1431− mutex_unlock(&ctx->uring_lock);
1599+ io_ring_submit_unlock(ctx, issue_flags);
14321600 }
14331601
14341602 static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
@@ -1469,7 +1637,12 @@ static inline void req_set_fail(struct io_kiocb *req) · 34 unmodified lines
14691637 static inline void req_fail_link_node(struct io_kiocb *req, int res)
14701638 {
14711639 req_set_fail(req);
1472− req->result = res;
1640+ req->cqe.res = res;
1641+}
1642+
1643+static inline void io_req_add_to_cache(struct io_kiocb *req, struct io_ring_ctx *ctx)
1644+{
1645+ wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
14731646 }
14741647
14751648 static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref)
@@ -1506,12 +1679,14 @@ static __cold void io_fallback_req_func(struct work_struct *work) · 30 unmodified lines
15061679 static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
15071680 {
15081681 struct io_ring_ctx *ctx;
1509− int i, hash_bits;
1682+ int hash_bits;
15101683
15111684 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
15121685 if (!ctx)
15131686 return NULL;
15141687
1688+ xa_init(&ctx->io_bl_xa);
1689+
15151690 /*
15161691 * Use 5 bits less than the max cq entries, that should give us around
15171692 * 32 entries per hash list if totally full and uniformly spread.
@@ -1533,13 +1708,6 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) · 15 unmodified lines
15331708 /* set invalid range, so io_import_fixed() fails meeting it */
15341709 ctx->dummy_ubuf->ubuf = -1UL;
15351710
1536− ctx->io_buffers = kcalloc(1U << IO_BUFFERS_HASH_BITS,
1537− sizeof(struct list_head), GFP_KERNEL);
1538− if (!ctx->io_buffers)
1539− goto err;
1540− for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++)
1541− INIT_LIST_HEAD(&ctx->io_buffers[i]);
1542−
15431711 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
15441712 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
15451713 goto err;
@@ -1575,7 +1743,8 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) · 29 unmodified lines
15751743 err:
15761744 kfree(ctx->dummy_ubuf);
15771745 kfree(ctx->cancel_hash);
1578− kfree(ctx->io_buffers);
1746+ kfree(ctx->io_bl);
1747+ xa_destroy(&ctx->io_bl_xa);
15791748 kfree(ctx);
15801749 return NULL;
15811750 }
@@ -1599,10 +1768,6 @@ static bool req_need_defer(struct io_kiocb *req, u32 seq) · 17 unmodified lines
15991768 return false;
16001769 }
16011770
1602−#define FFS_NOWAIT 0x1UL
1603−#define FFS_ISREG 0x2UL
1604−#define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG)
1605−
16061771 static inline bool io_req_ffs_set(struct io_kiocb *req)
16071772 {
16081773 return req->flags & REQ_F_FIXED_FILE;
@@ -1629,6 +1794,17 @@ static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) · 20 unmodified lines
16291794 return __io_prep_linked_timeout(req);
16301795 }
16311796
1797+static noinline void __io_arm_ltimeout(struct io_kiocb *req)
1798+{
1799+ io_queue_linked_timeout(__io_prep_linked_timeout(req));
1800+}
1801+
1802+static inline void io_arm_ltimeout(struct io_kiocb *req)
1803+{
1804+ if (unlikely(req->flags & REQ_F_ARM_LTIMEOUT))
1805+ __io_arm_ltimeout(req);
1806+}
1807+
16321808 static void io_prep_async_work(struct io_kiocb *req)
16331809 {
16341810 const struct io_op_def *def = &io_op_defs[req->opcode];
@@ -1641,6 +1817,7 @@ static void io_prep_async_work(struct io_kiocb *req) · 6 unmodified lines
16411817
16421818 req->work.list.next = NULL;
16431819 req->work.flags = 0;
1820+ req->work.cancel_seq = atomic_read(&ctx->cancel_seq);
16441821 if (req->flags & REQ_F_FORCE_ASYNC)
16451822 req->work.flags |= IO_WQ_WORK_CONCURRENT;
16461823
@@ -1672,17 +1849,15 @@ static void io_prep_async_link(struct io_kiocb *req) · 25 unmodified lines
16721849
16731850 static inline void io_req_add_compl_list(struct io_kiocb *req)
16741851 {
1675− struct io_ring_ctx *ctx = req->ctx;
1676− struct io_submit_state *state = &ctx->submit_state;
1852+ struct io_submit_state *state = &req->ctx->submit_state;
16771853
16781854 if (!(req->flags & REQ_F_CQE_SKIP))
1679− ctx->submit_state.flush_cqes = true;
1855+ state->flush_cqes = true;
16801856 wq_list_add_tail(&req->comp_list, &state->compl_reqs);
16811857 }
16821858
1683−static void io_queue_async_work(struct io_kiocb *req, bool *dont_use)
1859+static void io_queue_iowq(struct io_kiocb *req, bool *dont_use)
16841860 {
1685− struct io_ring_ctx *ctx = req->ctx;
16861861 struct io_kiocb *link = io_prep_linked_timeout(req);
16871862 struct io_uring_task *tctx = req->task->io_uring;
16881863
@@ -1702,8 +1877,9 @@ static void io_queue_async_work(struct io_kiocb *req, bool *dont_use) · 13 unmodified lines
17021877 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
17031878 req->work.flags |= IO_WQ_WORK_CANCEL;
17041879
1705− trace_io_uring_queue_async_work(ctx, req, req->user_data, req->opcode, req->flags,
1706− &req->work, io_wq_is_hashed(&req->work));
1880+ trace_io_uring_queue_async_work(req->ctx, req, req->cqe.user_data,
1881+ req->opcode, req->flags, &req->work,
1882+ io_wq_is_hashed(&req->work));
17071883 io_wq_enqueue(tctx->io_wq, &req->work);
17081884 if (link)
17091885 io_queue_linked_timeout(link);
@@ -1721,8 +1897,7 @@ static void io_kill_timeout(struct io_kiocb *req, int status) · 11 unmodified lines
17211897 atomic_set(&req->ctx->cq_timeouts,
17221898 atomic_read(&req->ctx->cq_timeouts) + 1);
17231899 list_del_init(&req->timeout.list);
1724− io_fill_cqe_req(req, status, 0);
1725− io_put_req_deferred(req);
1900+ io_req_tw_post_queue(req, status, 0);
17261901 }
17271902 }
17281903
@@ -1804,21 +1979,38 @@ static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) · 75 unmodified lines
18041979 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
18051980 }
18061981
1807−static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
1982+/*
1983+ * writes to the cq entry need to come after reading head; the
1984+ * control dependency is enough as we're using WRITE_ONCE to
1985+ * fill the cq entry
1986+ */
1987+static noinline struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx)
18081988 {
18091989 struct io_rings *rings = ctx->rings;
1810− unsigned tail, mask = ctx->cq_entries - 1;
1811−
1812− /*
1813− * writes to the cq entry need to come after reading head; the
1814− * control dependency is enough as we're using WRITE_ONCE to
1815− * fill the cq entry
1816− */
1817− if (__io_cqring_events(ctx) == ctx->cq_entries)
1990+ unsigned int off = ctx->cached_cq_tail & (ctx->cq_entries - 1);
1991+ unsigned int free, queued, len;
1992+
1993+ /* userspace may cheat modifying the tail, be safe and do min */
1994+ queued = min(__io_cqring_events(ctx), ctx->cq_entries);
1995+ free = ctx->cq_entries - queued;
1996+ /* we need a contiguous range, limit based on the current array offset */
1997+ len = min(free, ctx->cq_entries - off);
1998+ if (!len)
18181999 return NULL;
18192000
1820− tail = ctx->cached_cq_tail++;
1821− return &rings->cqes[tail & mask];
2001+ ctx->cached_cq_tail++;
2002+ ctx->cqe_cached = &rings->cqes[off];
2003+ ctx->cqe_sentinel = ctx->cqe_cached + len;
2004+ return ctx->cqe_cached++;
2005+}
2006+
2007+static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
2008+{
2009+ if (likely(ctx->cqe_cached < ctx->cqe_sentinel)) {
2010+ ctx->cached_cq_tail++;
2011+ return ctx->cqe_cached++;
2012+ }
2013+ return __io_get_cqe(ctx);
18222014 }
18232015
18242016 static void io_eventfd_signal(struct io_ring_ctx *ctx)
@@ -1915,13 +2107,11 @@ static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) · 90 unmodified lines
19152107
19162108 all_flushed = list_empty(&ctx->cq_overflow_list);
19172109 if (all_flushed) {
1918− clear_bit(0, &ctx->check_cq_overflow);
1919− WRITE_ONCE(ctx->rings->sq_flags,
1920− ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
2110+ clear_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq);
2111+ atomic_andnot(IORING_SQ_CQ_OVERFLOW, &ctx->rings->sq_flags);
19212112 }
19222113
1923− if (posted)
1924− io_commit_cqring(ctx);
2114+ io_commit_cqring(ctx);
19252115 spin_unlock(&ctx->completion_lock);
19262116 if (posted)
19272117 io_cqring_ev_posted(ctx);
@@ -1932,7 +2122,7 @@ static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) · 4 unmodified lines
19322122 {
19332123 bool ret = true;
19342124
1935− if (test_bit(0, &ctx->check_cq_overflow)) {
2125+ if (test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq)) {
19362126 /* iopoll syncs against uring_lock, not completion_lock */
19372127 if (ctx->flags & IORING_SETUP_IOPOLL)
19382128 mutex_lock(&ctx->uring_lock);
@@ -1944,19 +2134,23 @@ static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) · 5 unmodified lines
19442134 return ret;
19452135 }
19462136
1947−/* must to be called somewhat shortly after putting a request */
1948−static inline void io_put_task(struct task_struct *task, int nr)
2137+static void __io_put_task(struct task_struct *task, int nr)
19492138 {
19502139 struct io_uring_task *tctx = task->io_uring;
19512140
1952− if (likely(task == current)) {
1953− tctx->cached_refs += nr;
1954− } else {
1955− percpu_counter_sub(&tctx->inflight, nr);
1956− if (unlikely(atomic_read(&tctx->in_idle)))
1957− wake_up(&tctx->wait);
1958− put_task_struct_many(task, nr);
1959− }
2141+ percpu_counter_sub(&tctx->inflight, nr);
2142+ if (unlikely(atomic_read(&tctx->in_idle)))
2143+ wake_up(&tctx->wait);
2144+ put_task_struct_many(task, nr);
2145+}
2146+
2147+/* must to be called somewhat shortly after putting a request */
2148+static inline void io_put_task(struct task_struct *task, int nr)
2149+{
2150+ if (likely(task == current))
2151+ task->io_uring->cached_refs += nr;
2152+ else
2153+ __io_put_task(task, nr);
19602154 }
19612155
19622156 static void io_task_refs_refill(struct io_uring_task *tctx)
@@ -1995,6 +2189,7 @@ static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, · 32 unmodified lines
19952189 struct io_overflow_cqe *ocqe;
19962190
19972191 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
2192+ trace_io_uring_cqe_overflow(ctx, user_data, res, cflags, ocqe);
19982193 if (!ocqe) {
19992194 /*
20002195 * If we're in ring overflow flush mode, or in task cancel mode,
@@ -2002,12 +2197,12 @@ static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, · 1 unmodified line
20022197 * on the floor.
20032198 */
20042199 io_account_cq_overflow(ctx);
2200+ set_bit(IO_CHECK_CQ_DROPPED_BIT, &ctx->check_cq);
20052201 return false;
20062202 }
20072203 if (list_empty(&ctx->cq_overflow_list)) {
2008− set_bit(0, &ctx->check_cq_overflow);
2009− WRITE_ONCE(ctx->rings->sq_flags,
2010− ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
2204+ set_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq);
2205+ atomic_or(IORING_SQ_CQ_OVERFLOW, &ctx->rings->sq_flags);
20112206
20122207 }
20132208 ocqe->cqe.user_data = user_data;
@@ -2037,16 +2232,32 @@ static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, · 23 unmodified lines
20372232 return io_cqring_event_overflow(ctx, user_data, res, cflags);
20382233 }
20392234
2040−static inline bool __io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags)
2235+static inline bool __io_fill_cqe_req_filled(struct io_ring_ctx *ctx,
2236+ struct io_kiocb *req)
20412237 {
2042− trace_io_uring_complete(req->ctx, req, req->user_data, res, cflags);
2043− return __io_fill_cqe(req->ctx, req->user_data, res, cflags);
2238+ struct io_uring_cqe *cqe;
2239+
2240+ trace_io_uring_complete(req->ctx, req, req->cqe.user_data,
2241+ req->cqe.res, req->cqe.flags);
2242+
2243+ /*
2244+ * If we can't get a cq entry, userspace overflowed the
2245+ * submission (by quite a lot). Increment the overflow count in
2246+ * the ring.
2247+ */
2248+ cqe = io_get_cqe(ctx);
2249+ if (likely(cqe)) {
2250+ memcpy(cqe, &req->cqe, sizeof(*cqe));
2251+ return true;
2252+ }
2253+ return io_cqring_event_overflow(ctx, req->cqe.user_data,
2254+ req->cqe.res, req->cqe.flags);
20442255 }
20452256
2046−static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags)
2257+static inline bool __io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags)
20472258 {
2048− if (!(req->flags & REQ_F_CQE_SKIP))
2049− __io_fill_cqe_req(req, res, cflags);
2259+ trace_io_uring_complete(req->ctx, req, req->cqe.user_data, res, cflags);
2260+ return __io_fill_cqe(req->ctx, req->cqe.user_data, res, cflags);
20502261 }
20512262
20522263 static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data,
@@ -2069,7 +2280,7 @@ static void __io_req_complete_post(struct io_kiocb *req, s32 res, · 16 unmodified lines
20692280 * free_list cache.
20702281 */
20712282 if (req_ref_put_and_test(req)) {
2072− if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
2283+ if (req->flags & IO_REQ_LINK_FLAGS) {
20732284 if (req->flags & IO_DISARM_MASK)
20742285 io_disarm_next(req);
20752286 if (req->link) {
@@ -2077,7 +2288,7 @@ static void __io_req_complete_post(struct io_kiocb *req, s32 res, · 1 unmodified line
20772288 req->link = NULL;
20782289 }
20792290 }
2080− io_req_put_rsrc(req, ctx);
2291+ io_req_put_rsrc(req);
20812292 /*
20822293 * Selected buffer deallocation in io_clean_op() assumes that
20832294 * we don't hold ->completion_lock. Clean them here to avoid
@@ -2106,8 +2317,8 @@ static void io_req_complete_post(struct io_kiocb *req, s32 res, · 22 unmodified lines
21062317 static inline void io_req_complete_state(struct io_kiocb *req, s32 res,
21072318 u32 cflags)
21082319 {
2109− req->result = res;
2110− req->cflags = cflags;
2320+ req->cqe.res = res;
2321+ req->cqe.flags = cflags;
21112322 req->flags |= REQ_F_COMPLETE_INLINE;
21122323 }
21132324
@@ -2131,17 +2342,6 @@ static void io_req_complete_failed(struct io_kiocb *req, s32 res) · 17 unmodified lines
21312342 io_req_complete_post(req, res, io_put_kbuf(req, IO_URING_F_UNLOCKED));
21322343 }
21332344
2134−static void io_req_complete_fail_submit(struct io_kiocb *req)
2135−{
2136− /*
2137− * We don't submit, fail them all, for that replace hardlinks with
2138− * normal links. Extra REQ_F_LINK is tolerated.
2139− */
2140− req->flags &= ~REQ_F_HARDLINK;
2141− req->flags |= REQ_F_LINK;
2142− io_req_complete_failed(req, req->result);
2143−}
2144−
21452345 /*
21462346 * Don't initialise the fields below on every allocation, but do that in
21472347 * advance and keep them valid across allocations.
@@ -2152,7 +2352,7 @@ static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) · 4 unmodified lines
21522352 req->link = NULL;
21532353 req->async_data = NULL;
21542354 /* not necessary, but safer to zero */
2155− req->result = 0;
2355+ req->cqe.res = 0;
21562356 }
21572357
21582358 static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
@@ -2164,19 +2364,9 @@ static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, · 5 unmodified lines
21642364 spin_unlock(&ctx->completion_lock);
21652365 }
21662366
2167−/* Returns true IFF there are requests in the cache */
2168−static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
2367+static inline bool io_req_cache_empty(struct io_ring_ctx *ctx)
21692368 {
2170− struct io_submit_state *state = &ctx->submit_state;
2171−
2172− /*
2173− * If we have more than a batch's worth of requests in our IRQ side
2174− * locked cache, grab the lock and move them over to our submission
2175− * side cache.
2176− */
2177− if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
2178− io_flush_cached_locked_reqs(ctx, state);
2179− return !!state->free_list.next;
2369+ return !ctx->submit_state.free_list.next;
21802370 }
21812371
21822372 /*
@@ -2188,14 +2378,20 @@ static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) · 5 unmodified lines
21882378 static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx)
21892379 __must_hold(&ctx->uring_lock)
21902380 {
2191− struct io_submit_state *state = &ctx->submit_state;
21922381 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
21932382 void *reqs[IO_REQ_ALLOC_BATCH];
2194− struct io_kiocb *req;
21952383 int ret, i;
21962384
2197− if (likely(state->free_list.next || io_flush_cached_reqs(ctx)))
2198− return true;
2385+ /*
2386+ * If we have more than a batch's worth of requests in our IRQ side
2387+ * locked cache, grab the lock and move them over to our submission
2388+ * side cache.
2389+ */
2390+ if (data_race(ctx->locked_free_nr) > IO_COMPL_BATCH) {
2391+ io_flush_cached_locked_reqs(ctx, &ctx->submit_state);
2392+ if (!io_req_cache_empty(ctx))
2393+ return true;
2394+ }
21992395
22002396 ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs);
22012397
@@ -2212,17 +2408,17 @@ static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) · 10 unmodified lines
22122408
22132409 percpu_ref_get_many(&ctx->refs, ret);
22142410 for (i = 0; i < ret; i++) {
2215− req = reqs[i];
2411+ struct io_kiocb *req = reqs[i];
22162412
22172413 io_preinit_req(req, ctx);
2218− wq_stack_add_head(&req->comp_list, &state->free_list);
2414+ io_req_add_to_cache(req, ctx);
22192415 }
22202416 return true;
22212417 }
22222418
22232419 static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx)
22242420 {
2225− if (unlikely(!ctx->submit_state.free_list.next))
2421+ if (unlikely(io_req_cache_empty(ctx)))
22262422 return __io_alloc_req_refill(ctx);
22272423 return true;
22282424 }
@@ -2251,11 +2447,11 @@ static inline void io_dismantle_req(struct io_kiocb *req) · 22 unmodified lines
22512447 io_put_file(req->file);
22522448 }
22532449
2254−static __cold void __io_free_req(struct io_kiocb *req)
2450+static __cold void io_free_req(struct io_kiocb *req)
22552451 {
22562452 struct io_ring_ctx *ctx = req->ctx;
22572453
2258− io_req_put_rsrc(req, ctx);
2454+ io_req_put_rsrc(req);
22592455 io_dismantle_req(req);
22602456 io_put_task(req->task, 1);
22612457
@@ -2273,7 +2469,7 @@ static inline void io_remove_next_linked(struct io_kiocb *req) · 11 unmodified lines
22732469 nxt->link = NULL;
22742470 }
22752471
2276−static bool io_kill_linked_timeout(struct io_kiocb *req)
2472+static struct io_kiocb *io_disarm_linked_timeout(struct io_kiocb *req)
22772473 __must_hold(&req->ctx->completion_lock)
22782474 __must_hold(&req->ctx->timeout_lock)
22792475 {
@@ -2286,13 +2482,10 @@ static bool io_kill_linked_timeout(struct io_kiocb *req) · 6 unmodified lines
22862482 link->timeout.head = NULL;
22872483 if (hrtimer_try_to_cancel(&io->timer) != -1) {
22882484 list_del(&link->timeout.list);
2289− /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */
2290− io_fill_cqe_req(link, -ECANCELED, 0);
2291− io_put_req_deferred(link);
2292− return true;
2485+ return link;
22932486 }
22942487 }
2295− return false;
2488+ return NULL;
22962489 }
22972490
22982491 static void io_fail_links(struct io_kiocb *req)
@@ -2306,19 +2499,19 @@ static void io_fail_links(struct io_kiocb *req) · 7 unmodified lines
23062499 long res = -ECANCELED;
23072500
23082501 if (link->flags & REQ_F_FAIL)
2309− res = link->result;
2502+ res = link->cqe.res;
23102503
23112504 nxt = link->link;
23122505 link->link = NULL;
23132506
2314− trace_io_uring_fail_link(req->ctx, req, req->user_data,
2507+ trace_io_uring_fail_link(req->ctx, req, req->cqe.user_data,
23152508 req->opcode, link);
23162509
2317− if (!ignore_cqes) {
2510+ if (ignore_cqes)
2511+ link->flags |= REQ_F_CQE_SKIP;
2512+ else
23182513 link->flags &= ~REQ_F_CQE_SKIP;
2319− io_fill_cqe_req(link, res, 0);
2320− }
2321− io_put_req_deferred(link);
2514+ __io_req_complete_post(link, res, 0);
23222515 link = nxt;
23232516 }
23242517 }
@@ -2326,25 +2519,27 @@ static void io_fail_links(struct io_kiocb *req) · 1 unmodified line
23262519 static bool io_disarm_next(struct io_kiocb *req)
23272520 __must_hold(&req->ctx->completion_lock)
23282521 {
2522+ struct io_kiocb *link = NULL;
23292523 bool posted = false;
23302524
23312525 if (req->flags & REQ_F_ARM_LTIMEOUT) {
2332− struct io_kiocb *link = req->link;
2333−
2526+ link = req->link;
23342527 req->flags &= ~REQ_F_ARM_LTIMEOUT;
23352528 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
23362529 io_remove_next_linked(req);
2337− /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */
2338− io_fill_cqe_req(link, -ECANCELED, 0);
2339− io_put_req_deferred(link);
2530+ io_req_tw_post_queue(link, -ECANCELED, 0);
23402531 posted = true;
23412532 }
23422533 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
23432534 struct io_ring_ctx *ctx = req->ctx;
23442535
23452536 spin_lock_irq(&ctx->timeout_lock);
2346− posted = io_kill_linked_timeout(req);
2537+ link = io_disarm_linked_timeout(req);
23472538 spin_unlock_irq(&ctx->timeout_lock);
2539+ if (link) {
2540+ posted = true;
2541+ io_req_tw_post_queue(link, -ECANCELED, 0);
2542+ }
23482543 }
23492544 if (unlikely((req->flags & REQ_F_FAIL) &&
23502545 !(req->flags & REQ_F_HARDLINK))) {
@@ -2361,8 +2556,7 @@ static void __io_req_find_next_prep(struct io_kiocb *req) · 10 unmodified lines
23612556
23622557 spin_lock(&ctx->completion_lock);
23632558 posted = io_disarm_next(req);
2364− if (posted)
2365− io_commit_cqring(ctx);
2559+ io_commit_cqring(ctx);
23662560 spin_unlock(&ctx->completion_lock);
23672561 if (posted)
23682562 io_cqring_ev_posted(ctx);
@@ -2372,8 +2566,6 @@ static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) · 3 unmodified lines
23722566 {
23732567 struct io_kiocb *nxt;
23742568
2375− if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
2376− return NULL;
23772569 /*
23782570 * If LINK is set, we have dependent requests in this chain. If we
23792571 * didn't fail this request, queue the first one up, moving any other
@@ -2391,6 +2583,8 @@ static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) · 11 unmodified lines
23912583 {
23922584 if (!ctx)
23932585 return;
2586+ if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
2587+ atomic_andnot(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
23942588 if (*locked) {
23952589 io_submit_flush_completions(ctx);
23962590 mutex_unlock(&ctx->uring_lock);
@@ -2434,7 +2628,7 @@ static void handle_prev_tw_list(struct io_wq_work_node *node, · 37 unmodified lines
24342628 if (likely(*uring_locked))
24352629 req->io_task_work.func(req, uring_locked);
24362630 else
2437− __io_req_complete_post(req, req->result,
2631+ __io_req_complete_post(req, req->cqe.res,
24382632 io_put_kbuf_comp(req));
24392633 node = next;
24402634 } while (node);
@@ -2475,10 +2669,6 @@ static void tctx_task_work(struct callback_head *cb) · 34 unmodified lines
24752669 while (1) {
24762670 struct io_wq_work_node *node1, *node2;
24772671
2478− if (!tctx->task_list.first &&
2479− !tctx->prior_task_list.first && uring_locked)
2480− io_submit_flush_completions(ctx);
2481−
24822672 spin_lock_irq(&tctx->task_lock);
24832673 node1 = tctx->prior_task_list.first;
24842674 node2 = tctx->task_list.first;
@@ -2492,10 +2682,13 @@ static void tctx_task_work(struct callback_head *cb) · 7 unmodified lines
24922682
24932683 if (node1)
24942684 handle_prev_tw_list(node1, &ctx, &uring_locked);
2495−
24962685 if (node2)
24972686 handle_tw_list(node2, &ctx, &uring_locked);
24982687 cond_resched();
2688+
2689+ if (data_race(!tctx->task_list.first) &&
2690+ data_race(!tctx->prior_task_list.first) && uring_locked)
2691+ io_submit_flush_completions(ctx);
24992692 }
25002693
25012694 ctx_flush_and_put(ctx, &uring_locked);
@@ -2508,8 +2701,8 @@ static void tctx_task_work(struct callback_head *cb) · 6 unmodified lines
25082701 static void io_req_task_work_add(struct io_kiocb *req, bool priority)
25092702 {
25102703 struct task_struct *tsk = req->task;
2704+ struct io_ring_ctx *ctx = req->ctx;
25112705 struct io_uring_task *tctx = tsk->io_uring;
2512− enum task_work_notify_mode notify;
25132706 struct io_wq_work_node *node;
25142707 unsigned long flags;
25152708 bool running;
@@ -2532,18 +2725,11 @@ static void io_req_task_work_add(struct io_kiocb *req, bool priority) · 16 unmodified lines
25322725 if (running)
25332726 return;
25342727
2535− /*
2536− * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2537− * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2538− * processing task_work. There's no reliable way to tell if TWA_RESUME
2539− * will do the job.
2540− */
2541− notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
2542− if (likely(!task_work_add(tsk, &tctx->task_work, notify))) {
2543− if (notify == TWA_NONE)
2544− wake_up_process(tsk);
2728+ if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
2729+ atomic_or(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
2730+
2731+ if (likely(!task_work_add(tsk, &tctx->task_work, ctx->notify_method)))
25452732 return;
2546− }
25472733
25482734 spin_lock_irqsave(&tctx->task_lock, flags);
25492735 tctx->task_running = false;
@@ -2559,30 +2745,39 @@ static void io_req_task_work_add(struct io_kiocb *req, bool priority) · 9 unmodified lines
25592745 }
25602746 }
25612747
2562−static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
2748+static void io_req_tw_post(struct io_kiocb *req, bool *locked)
25632749 {
2564− struct io_ring_ctx *ctx = req->ctx;
2750+ io_req_complete_post(req, req->cqe.res, req->cqe.flags);
2751+}
25652752
2753+static void io_req_tw_post_queue(struct io_kiocb *req, s32 res, u32 cflags)
2754+{
2755+ req->cqe.res = res;
2756+ req->cqe.flags = cflags;
2757+ req->io_task_work.func = io_req_tw_post;
2758+ io_req_task_work_add(req, false);
2759+}
2760+
2761+static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
2762+{
25662763 /* not needed for normal modes, but SQPOLL depends on it */
2567− io_tw_lock(ctx, locked);
2568− io_req_complete_failed(req, req->result);
2764+ io_tw_lock(req->ctx, locked);
2765+ io_req_complete_failed(req, req->cqe.res);
25692766 }
25702767
25712768 static void io_req_task_submit(struct io_kiocb *req, bool *locked)
25722769 {
2573− struct io_ring_ctx *ctx = req->ctx;
2574−
2575− io_tw_lock(ctx, locked);
2770+ io_tw_lock(req->ctx, locked);
25762771 /* req->task == current here, checking PF_EXITING is safe */
25772772 if (likely(!(req->task->flags & PF_EXITING)))
2578− __io_queue_sqe(req);
2773+ io_queue_sqe(req);
25792774 else
25802775 io_req_complete_failed(req, -EFAULT);
25812776 }
25822777
25832778 static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
25842779 {
2585− req->result = ret;
2780+ req->cqe.res = ret;
25862781 req->io_task_work.func = io_req_task_cancel;
25872782 io_req_task_work_add(req, false);
25882783 }
@@ -2595,11 +2790,11 @@ static void io_req_task_queue(struct io_kiocb *req) · 6 unmodified lines
25952790
25962791 static void io_req_task_queue_reissue(struct io_kiocb *req)
25972792 {
2598− req->io_task_work.func = io_queue_async_work;
2793+ req->io_task_work.func = io_queue_iowq;
25992794 io_req_task_work_add(req, false);
26002795 }
26012796
2602−static inline void io_queue_next(struct io_kiocb *req)
2797+static void io_queue_next(struct io_kiocb *req)
26032798 {
26042799 struct io_kiocb *nxt = io_req_find_next(req);
26052800
@@ -2607,17 +2802,6 @@ static inline void io_queue_next(struct io_kiocb *req) · 1 unmodified line
26072802 io_req_task_queue(nxt);
26082803 }
26092804
2610−static void io_free_req(struct io_kiocb *req)
2611−{
2612− io_queue_next(req);
2613− __io_free_req(req);
2614−}
2615−
2616−static void io_free_req_work(struct io_kiocb *req, bool *locked)
2617−{
2618− io_free_req(req);
2619−}
2620−
26212805 static void io_free_batch_list(struct io_ring_ctx *ctx,
26222806 struct io_wq_work_node *node)
26232807 __must_hold(&ctx->uring_lock)
@@ -2629,15 +2813,30 @@ static void io_free_batch_list(struct io_ring_ctx *ctx, · 5 unmodified lines
26292813 struct io_kiocb *req = container_of(node, struct io_kiocb,
26302814 comp_list);
26312815
2632− if (unlikely(req->flags & REQ_F_REFCOUNT)) {
2633− node = req->comp_list.next;
2634− if (!req_ref_put_and_test(req))
2635− continue;
2816+ if (unlikely(req->flags & IO_REQ_CLEAN_SLOW_FLAGS)) {
2817+ if (req->flags & REQ_F_REFCOUNT) {
2818+ node = req->comp_list.next;
2819+ if (!req_ref_put_and_test(req))
2820+ continue;
2821+ }
2822+ if ((req->flags & REQ_F_POLLED) && req->apoll) {
2823+ struct async_poll *apoll = req->apoll;
2824+
2825+ if (apoll->double_poll)
2826+ kfree(apoll->double_poll);
2827+ list_add(&apoll->poll.wait.entry,
2828+ &ctx->apoll_cache);
2829+ req->flags &= ~REQ_F_POLLED;
2830+ }
2831+ if (req->flags & IO_REQ_LINK_FLAGS)
2832+ io_queue_next(req);
2833+ if (unlikely(req->flags & IO_REQ_CLEAN_FLAGS))
2834+ io_clean_op(req);
26362835 }
2836+ if (!(req->flags & REQ_F_FIXED_FILE))
2837+ io_put_file(req->file);
26372838
26382839 io_req_put_rsrc_locked(req, ctx);
2639− io_queue_next(req);
2640− io_dismantle_req(req);
26412840
26422841 if (req->task != task) {
26432842 if (task)
@@ -2647,7 +2846,7 @@ static void io_free_batch_list(struct io_ring_ctx *ctx, · 3 unmodified lines
26472846 }
26482847 task_refs++;
26492848 node = req->comp_list.next;
2650− wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
2849+ io_req_add_to_cache(req, ctx);
26512850 } while (node);
26522851
26532852 if (task)
@@ -2667,16 +2866,7 @@ static void __io_submit_flush_completions(struct io_ring_ctx *ctx) · 13 unmodified lines
26672866 comp_list);
26682867
26692868 if (!(req->flags & REQ_F_CQE_SKIP))
2670− __io_fill_cqe_req(req, req->result, req->cflags);
2671− if ((req->flags & REQ_F_POLLED) && req->apoll) {
2672− struct async_poll *apoll = req->apoll;
2673−
2674− if (apoll->double_poll)
2675− kfree(apoll->double_poll);
2676− list_add(&apoll->poll.wait.entry,
2677− &ctx->apoll_cache);
2678− req->flags &= ~REQ_F_POLLED;
2679− }
2869+ __io_fill_cqe_req_filled(ctx, req);
26802870 }
26812871
26822872 io_commit_cqring(ctx);
@@ -2698,23 +2888,18 @@ static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) · 15 unmodified lines
26982888 struct io_kiocb *nxt = NULL;
26992889
27002890 if (req_ref_put_and_test(req)) {
2701− nxt = io_req_find_next(req);
2702− __io_free_req(req);
2891+ if (unlikely(req->flags & IO_REQ_LINK_FLAGS))
2892+ nxt = io_req_find_next(req);
2893+ io_free_req(req);
27032894 }
27042895 return nxt;
27052896 }
27062897
27072898 static inline void io_put_req(struct io_kiocb *req)
27082899 {
2709− if (req_ref_put_and_test(req))
2900+ if (req_ref_put_and_test(req)) {
2901+ io_queue_next(req);
27102902 io_free_req(req);
2711−}
2712−
2713−static inline void io_put_req_deferred(struct io_kiocb *req)
2714−{
2715− if (req_ref_put_and_test(req)) {
2716− req->io_task_work.func = io_free_req_work;
2717− io_req_task_work_add(req, false);
27182903 }
27192904 }
27202905
@@ -2800,7 +2985,7 @@ static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) · 79 unmodified lines
28002985 nr_events++;
28012986 if (unlikely(req->flags & REQ_F_CQE_SKIP))
28022987 continue;
2803− __io_fill_cqe_req(req, req->result, io_put_kbuf(req, 0));
2988+ __io_fill_cqe_req(req, req->cqe.res, io_put_kbuf(req, 0));
28042989 }
28052990
28062991 if (unlikely(!nr_events))
@@ -2846,22 +3031,26 @@ static int io_iopoll_check(struct io_ring_ctx *ctx, long min) · 39 unmodified lines
28463031 {
28473032 unsigned int nr_events = 0;
28483033 int ret = 0;
3034+ unsigned long check_cq;
28493035
2850− /*
2851− * We disallow the app entering submit/complete with polling, but we
2852− * still need to lock the ring to prevent racing with polled issue
2853− * that got punted to a workqueue.
2854− */
2855− mutex_lock(&ctx->uring_lock);
28563036 /*
28573037 * Don't enter poll loop if we already have events pending.
28583038 * If we do, we can potentially be spinning for commands that
28593039 * already triggered a CQE (eg in error).
28603040 */
2861− if (test_bit(0, &ctx->check_cq_overflow))
3041+ check_cq = READ_ONCE(ctx->check_cq);
3042+ if (check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT))
28623043 __io_cqring_overflow_flush(ctx, false);
28633044 if (io_cqring_events(ctx))
2864− goto out;
3045+ return 0;
3046+
3047+ /*
3048+ * Similarly do not spin if we have not informed the user of any
3049+ * dropped CQE.
3050+ */
3051+ if (unlikely(check_cq & BIT(IO_CHECK_CQ_DROPPED_BIT)))
3052+ return -EBADR;
3053+
28653054 do {
28663055 /*
28673056 * If a submit got punted to a workqueue, we can have the
@@ -2891,8 +3080,7 @@ static int io_iopoll_check(struct io_ring_ctx *ctx, long min) · 23 unmodified lines
28913080 nr_events += ret;
28923081 ret = 0;
28933082 } while (nr_events < min && !need_resched());
2894−out:
2895− mutex_unlock(&ctx->uring_lock);
3083+
28963084 return ret;
28973085 }
28983086
@@ -2965,21 +3153,21 @@ static bool __io_complete_rw_common(struct io_kiocb *req, long res) · 66 unmodified lines
29653153 } else {
29663154 fsnotify_access(req->file);
29673155 }
2968− if (unlikely(res != req->result)) {
3156+ if (unlikely(res != req->cqe.res)) {
29693157 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
29703158 io_rw_should_reissue(req)) {
29713159 req->flags |= REQ_F_REISSUE;
29723160 return true;
29733161 }
29743162 req_set_fail(req);
2975− req->result = res;
3163+ req->cqe.res = res;
29763164 }
29773165 return false;
29783166 }
29793167
29803168 static inline void io_req_task_complete(struct io_kiocb *req, bool *locked)
29813169 {
2982− int res = req->result;
3170+ int res = req->cqe.res;
29833171
29843172 if (*locked) {
29853173 io_req_complete_state(req, res, io_put_kbuf(req, 0));
@@ -2995,7 +3183,7 @@ static void __io_complete_rw(struct io_kiocb *req, long res, · 9 unmodified lines
29953183 {
29963184 if (__io_complete_rw_common(req, res))
29973185 return;
2998− __io_req_complete(req, issue_flags, req->result,
3186+ __io_req_complete(req, issue_flags, req->cqe.res,
29993187 io_put_kbuf(req, issue_flags));
30003188 }
30013189
@@ -3005,7 +3193,7 @@ static void io_complete_rw(struct kiocb *kiocb, long res) · 3 unmodified lines
30053193
30063194 if (__io_complete_rw_common(req, res))
30073195 return;
3008− req->result = res;
3196+ req->cqe.res = res;
30093197 req->io_task_work.func = io_req_task_complete;
30103198 io_req_task_work_add(req, !!(req->ctx->flags & IORING_SETUP_SQPOLL));
30113199 }
@@ -3016,12 +3204,12 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) · 4 unmodified lines
30163204
30173205 if (kiocb->ki_flags & IOCB_WRITE)
30183206 kiocb_end_write(req);
3019− if (unlikely(res != req->result)) {
3207+ if (unlikely(res != req->cqe.res)) {
30203208 if (res == -EAGAIN && io_rw_should_reissue(req)) {
30213209 req->flags |= REQ_F_REISSUE;
30223210 return;
30233211 }
3024− req->result = res;
3212+ req->cqe.res = res;
30253213 }
30263214
30273215 /* order with io_iopoll_complete() checking ->iopoll_completed */
@@ -3131,6 +3319,8 @@ static unsigned int io_file_get_flags(struct file *file) · 103 unmodified lines
31313319 res |= FFS_ISREG;
31323320 if (__io_file_supports_nowait(file, mode))
31333321 res |= FFS_NOWAIT;
3322+ if (io_file_need_scm(file))
3323+ res |= FFS_SCM;
31343324 return res;
31353325 }
31363326
@@ -3162,6 +3352,7 @@ static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 25 unmodified lines
31623352 req->rw.addr = READ_ONCE(sqe->addr);
31633353 req->rw.len = READ_ONCE(sqe->len);
31643354 req->rw.flags = READ_ONCE(sqe->rw_flags);
3355+ /* used for fixed read/write too - just read unconditionally */
31653356 req->buf_index = READ_ONCE(sqe->buf_index);
31663357 return 0;
31673358 }
@@ -3310,77 +3501,96 @@ static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, · 142 unmodified lines
33103501 return __io_import_fixed(req, rw, iter, imu);
33113502 }
33123503
3313−static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3504+static int io_buffer_add_list(struct io_ring_ctx *ctx,
3505+ struct io_buffer_list *bl, unsigned int bgid)
33143506 {
3315− if (needs_lock)
3316− mutex_unlock(&ctx->uring_lock);
3317−}
3318−
3319−static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3320−{
3321− /*
3322− * "Normal" inline submissions always hold the uring_lock, since we
3323− * grab it from the system call. Same is true for the SQPOLL offload.
3324− * The only exception is when we've detached the request and issue it
3325− * from an async worker thread, grab the lock for that case.
3326− */
3327− if (needs_lock)
3328− mutex_lock(&ctx->uring_lock);
3329−}
3330−
3331−static void io_buffer_add_list(struct io_ring_ctx *ctx,
3332− struct io_buffer_list *bl, unsigned int bgid)
3333−{
3334− struct list_head *list;
3335−
3336− list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)];
3337− INIT_LIST_HEAD(&bl->buf_list);
33383507 bl->bgid = bgid;
3339− list_add(&bl->list, list);
3508+ if (bgid < BGID_ARRAY)
3509+ return 0;
3510+
3511+ return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
33403512 }
33413513
3342−static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3343− int bgid, unsigned int issue_flags)
3514+static void __user *io_provided_buffer_select(struct io_kiocb *req, size_t *len,
3515+ struct io_buffer_list *bl)
33443516 {
3345− struct io_buffer *kbuf = req->kbuf;
3346− bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
3347− struct io_ring_ctx *ctx = req->ctx;
3348− struct io_buffer_list *bl;
3349−
3350− if (req->flags & REQ_F_BUFFER_SELECTED)
3351− return kbuf;
3517+ if (!list_empty(&bl->buf_list)) {
3518+ struct io_buffer *kbuf;
33523519
3353− io_ring_submit_lock(ctx, needs_lock);
3354−
3355− lockdep_assert_held(&ctx->uring_lock);
3356−
3357− bl = io_buffer_get_list(ctx, bgid);
3358− if (bl && !list_empty(&bl->buf_list)) {
33593520 kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list);
33603521 list_del(&kbuf->list);
33613522 if (*len > kbuf->len)
33623523 *len = kbuf->len;
33633524 req->flags |= REQ_F_BUFFER_SELECTED;
33643525 req->kbuf = kbuf;
3526+ req->buf_index = kbuf->bid;
3527+ return u64_to_user_ptr(kbuf->addr);
3528+ }
3529+ return NULL;
3530+}
3531+
3532+static void __user *io_ring_buffer_select(struct io_kiocb *req, size_t *len,
3533+ struct io_buffer_list *bl,
3534+ unsigned int issue_flags)
3535+{
3536+ struct io_uring_buf_ring *br = bl->buf_ring;
3537+ struct io_uring_buf *buf;
3538+ __u32 head = bl->head;
3539+
3540+ if (unlikely(smp_load_acquire(&br->tail) == head)) {
3541+ io_ring_submit_unlock(req->ctx, issue_flags);
3542+ return NULL;
3543+ }
3544+
3545+ head &= bl->mask;
3546+ if (head < IO_BUFFER_LIST_BUF_PER_PAGE) {
3547+ buf = &br->bufs[head];
33653548 } else {
3366− kbuf = ERR_PTR(-ENOBUFS);
3549+ int off = head & (IO_BUFFER_LIST_BUF_PER_PAGE - 1);
3550+ int index = head / IO_BUFFER_LIST_BUF_PER_PAGE - 1;
3551+ buf = page_address(bl->buf_pages[index]);
3552+ buf += off;
33673553 }
3554+ if (*len > buf->len)
3555+ *len = buf->len;
3556+ req->flags |= REQ_F_BUFFER_RING;
3557+ req->buf_list = bl;
3558+ req->buf_index = buf->bid;
33683559
3369− io_ring_submit_unlock(req->ctx, needs_lock);
3370− return kbuf;
3560+ if (issue_flags & IO_URING_F_UNLOCKED) {
3561+ /*
3562+ * If we came in unlocked, we have no choice but to consume the
3563+ * buffer here. This does mean it'll be pinned until the IO
3564+ * completes. But coming in unlocked means we're in io-wq
3565+ * context, hence there should be no further retry. For the
3566+ * locked case, the caller must ensure to call the commit when
3567+ * the transfer completes (or if we get -EAGAIN and must poll
3568+ * or retry).
3569+ */
3570+ req->buf_list = NULL;
3571+ bl->head++;
3572+ }
3573+ return u64_to_user_ptr(buf->addr);
33713574 }
33723575
3373−static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3374− unsigned int issue_flags)
3576+static void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
3577+ unsigned int issue_flags)
33753578 {
3376− struct io_buffer *kbuf;
3377− u16 bgid;
3579+ struct io_ring_ctx *ctx = req->ctx;
3580+ struct io_buffer_list *bl;
3581+ void __user *ret = NULL;
3582+
3583+ io_ring_submit_lock(req->ctx, issue_flags);
33783584
3379− bgid = req->buf_index;
3380− kbuf = io_buffer_select(req, len, bgid, issue_flags);
3381− if (IS_ERR(kbuf))
3382− return kbuf;
3383− return u64_to_user_ptr(kbuf->addr);
3585+ bl = io_buffer_get_list(ctx, req->buf_index);
3586+ if (likely(bl)) {
3587+ if (bl->buf_nr_pages)
3588+ ret = io_ring_buffer_select(req, len, bl, issue_flags);
3589+ else
3590+ ret = io_provided_buffer_select(req, len, bl);
3591+ }
3592+ io_ring_submit_unlock(req->ctx, issue_flags);
3593+ return ret;
33843594 }
33853595
33863596 #ifdef CONFIG_COMPAT
@@ -3390,7 +3600,7 @@ static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, · 3 unmodified lines
33903600 struct compat_iovec __user *uiov;
33913601 compat_ssize_t clen;
33923602 void __user *buf;
3393− ssize_t len;
3603+ size_t len;
33943604
33953605 uiov = u64_to_user_ptr(req->rw.addr);
33963606 if (!access_ok(uiov, sizeof(*uiov)))
@@ -3401,11 +3611,12 @@ static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, · 4 unmodified lines
34013611 return -EINVAL;
34023612
34033613 len = clen;
3404− buf = io_rw_buffer_select(req, &len, issue_flags);
3405− if (IS_ERR(buf))
3406− return PTR_ERR(buf);
3614+ buf = io_buffer_select(req, &len, issue_flags);
3615+ if (!buf)
3616+ return -ENOBUFS;
3617+ req->rw.addr = (unsigned long) buf;
34073618 iov[0].iov_base = buf;
3408− iov[0].iov_len = (compat_size_t) len;
3619+ req->rw.len = iov[0].iov_len = (compat_size_t) len;
34093620 return 0;
34103621 }
34113622 #endif
@@ -3423,22 +3634,21 @@ static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, · 11 unmodified lines
34233634 len = iov[0].iov_len;
34243635 if (len < 0)
34253636 return -EINVAL;
3426− buf = io_rw_buffer_select(req, &len, issue_flags);
3427− if (IS_ERR(buf))
3428− return PTR_ERR(buf);
3637+ buf = io_buffer_select(req, &len, issue_flags);
3638+ if (!buf)
3639+ return -ENOBUFS;
3640+ req->rw.addr = (unsigned long) buf;
34293641 iov[0].iov_base = buf;
3430− iov[0].iov_len = len;
3642+ req->rw.len = iov[0].iov_len = len;
34313643 return 0;
34323644 }
34333645
34343646 static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
34353647 unsigned int issue_flags)
34363648 {
3437− if (req->flags & REQ_F_BUFFER_SELECTED) {
3438− struct io_buffer *kbuf = req->kbuf;
3439−
3440− iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3441− iov[0].iov_len = kbuf->len;
3649+ if (req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)) {
3650+ iov[0].iov_base = u64_to_user_ptr(req->rw.addr);
3651+ iov[0].iov_len = req->rw.len;
34423652 return 0;
34433653 }
34443654 if (req->rw.len != 1)
@@ -3452,6 +3662,13 @@ static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, · 7 unmodified lines
34523662 return __io_iov_buffer_select(req, iov, issue_flags);
34533663 }
34543664
3665+static inline bool io_do_buffer_select(struct io_kiocb *req)
3666+{
3667+ if (!(req->flags & REQ_F_BUFFER_SELECT))
3668+ return false;
3669+ return !(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING));
3670+}
3671+
34553672 static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req,
34563673 struct io_rw_state *s,
34573674 unsigned int issue_flags)
@@ -3470,18 +3687,15 @@ static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, · 12 unmodified lines
34703687 return NULL;
34713688 }
34723689
3473− /* buffer index only valid with fixed read/write, or buffer select */
3474− if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)))
3475− return ERR_PTR(-EINVAL);
3476−
34773690 buf = u64_to_user_ptr(req->rw.addr);
34783691 sqe_len = req->rw.len;
34793692
34803693 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
3481− if (req->flags & REQ_F_BUFFER_SELECT) {
3482− buf = io_rw_buffer_select(req, &sqe_len, issue_flags);
3483− if (IS_ERR(buf))
3484− return ERR_CAST(buf);
3694+ if (io_do_buffer_select(req)) {
3695+ buf = io_buffer_select(req, &sqe_len, issue_flags);
3696+ if (!buf)
3697+ return ERR_PTR(-ENOBUFS);
3698+ req->rw.addr = (unsigned long) buf;
34853699 req->rw.len = sqe_len;
34863700 }
34873701
@@ -3836,7 +4050,7 @@ static int io_read(struct io_kiocb *req, unsigned int issue_flags) · 348 unmodified lines
38364050 kfree(iovec);
38374051 return ret;
38384052 }
3839− req->result = iov_iter_count(&s->iter);
4053+ req->cqe.res = iov_iter_count(&s->iter);
38404054
38414055 if (force_nonblock) {
38424056 /* If the file doesn't support async, just async punt */
@@ -3852,7 +4066,7 @@ static int io_read(struct io_kiocb *req, unsigned int issue_flags) · 9 unmodified lines
38524066
38534067 ppos = io_kiocb_update_pos(req);
38544068
3855− ret = rw_verify_area(READ, req->file, ppos, req->result);
4069+ ret = rw_verify_area(READ, req->file, ppos, req->cqe.res);
38564070 if (unlikely(ret)) {
38574071 kfree(iovec);
38584072 return ret;
@@ -3874,7 +4088,7 @@ static int io_read(struct io_kiocb *req, unsigned int issue_flags) · 15 unmodified lines
38744088 ret = 0;
38754089 } else if (ret == -EIOCBQUEUED) {
38764090 goto out_free;
3877− } else if (ret == req->result || ret <= 0 || !force_nonblock ||
4091+ } else if (ret == req->cqe.res || ret <= 0 || !force_nonblock ||
38784092 (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) {
38794093 /* read all, failed, already did sync or don't want to retry */
38804094 goto done;
@@ -3964,7 +4178,7 @@ static int io_write(struct io_kiocb *req, unsigned int issue_flags) · 83 unmodified lines
39644178 kfree(iovec);
39654179 return ret;
39664180 }
3967− req->result = iov_iter_count(&s->iter);
4181+ req->cqe.res = iov_iter_count(&s->iter);
39684182
39694183 if (force_nonblock) {
39704184 /* If the file doesn't support async, just async punt */
@@ -3984,7 +4198,7 @@ static int io_write(struct io_kiocb *req, unsigned int issue_flags) · 13 unmodified lines
39844198
39854199 ppos = io_kiocb_update_pos(req);
39864200
3987− ret = rw_verify_area(WRITE, req->file, ppos, req->result);
4201+ ret = rw_verify_area(WRITE, req->file, ppos, req->cqe.res);
39884202 if (unlikely(ret))
39894203 goto out_free;
39904204
@@ -4048,9 +4262,7 @@ static int io_renameat_prep(struct io_kiocb *req, · 57 unmodified lines
40484262 struct io_rename *ren = &req->rename;
40494263 const char __user *oldf, *newf;
40504264
4051− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4052− return -EINVAL;
4053− if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
4265+ if (sqe->buf_index || sqe->splice_fd_in)
40544266 return -EINVAL;
40554267 if (unlikely(req->flags & REQ_F_FIXED_FILE))
40564268 return -EBADF;
@@ -4099,10 +4311,7 @@ static int io_unlinkat_prep(struct io_kiocb *req, · 42 unmodified lines
40994311 struct io_unlink *un = &req->unlink;
41004312 const char __user *fname;
41014313
4102− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4103− return -EINVAL;
4104− if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4105− sqe->splice_fd_in)
4314+ if (sqe->off || sqe->len || sqe->buf_index || sqe->splice_fd_in)
41064315 return -EINVAL;
41074316 if (unlikely(req->flags & REQ_F_FIXED_FILE))
41084317 return -EBADF;
@@ -4148,10 +4357,7 @@ static int io_mkdirat_prep(struct io_kiocb *req, · 39 unmodified lines
41484357 struct io_mkdir *mkd = &req->mkdir;
41494358 const char __user *fname;
41504359
4151− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4152− return -EINVAL;
4153− if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
4154− sqe->splice_fd_in)
4360+ if (sqe->off || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
41554361 return -EINVAL;
41564362 if (unlikely(req->flags & REQ_F_FIXED_FILE))
41574363 return -EBADF;
@@ -4191,10 +4397,7 @@ static int io_symlinkat_prep(struct io_kiocb *req, · 33 unmodified lines
41914397 struct io_symlink *sl = &req->symlink;
41924398 const char __user *oldpath, *newpath;
41934399
4194− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4195− return -EINVAL;
4196− if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index ||
4197− sqe->splice_fd_in)
4400+ if (sqe->len || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
41984401 return -EINVAL;
41994402 if (unlikely(req->flags & REQ_F_FIXED_FILE))
42004403 return -EBADF;
@@ -4240,9 +4443,7 @@ static int io_linkat_prep(struct io_kiocb *req, · 39 unmodified lines
42404443 struct io_hardlink *lnk = &req->hardlink;
42414444 const char __user *oldf, *newf;
42424445
4243− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4244− return -EINVAL;
4245− if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
4446+ if (sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
42464447 return -EINVAL;
42474448 if (unlikely(req->flags & REQ_F_FIXED_FILE))
42484449 return -EBADF;
@@ -4289,9 +4490,7 @@ static int io_shutdown_prep(struct io_kiocb *req, · 40 unmodified lines
42894490 const struct io_uring_sqe *sqe)
42904491 {
42914492 #if defined(CONFIG_NET)
4292− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4293− return -EINVAL;
4294− if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
4493+ if (unlikely(sqe->off || sqe->addr || sqe->rw_flags ||
42954494 sqe->buf_index || sqe->splice_fd_in))
42964495 return -EINVAL;
42974496
@@ -4331,9 +4530,6 @@ static int __io_splice_prep(struct io_kiocb *req, · 33 unmodified lines
43314530 struct io_splice *sp = &req->splice;
43324531 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
43334532
4334− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4335− return -EINVAL;
4336−
43374533 sp->len = READ_ONCE(sqe->len);
43384534 sp->flags = READ_ONCE(sqe->splice_flags);
43394535 if (unlikely(sp->flags & ~valid_flags))
@@ -4432,20 +4628,25 @@ done: · 92 unmodified lines
44324628 */
44334629 static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
44344630 {
4435− struct io_ring_ctx *ctx = req->ctx;
4631+ void __user *buf;
44364632
4437− if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4438− return -EINVAL;
4633+ if (req->flags & REQ_F_BUFFER_SELECT) {
4634+ size_t len = 1;
4635+
4636+ buf = io_buffer_select(req, &len, issue_flags);
4637+ if (!buf)
4638+ return -ENOBUFS;
4639+ }
44394640
4440− __io_req_complete(req, issue_flags, 0, 0);
4641+ __io_req_complete(req, issue_flags, 0, io_put_kbuf(req, issue_flags));
44414642 return 0;
44424643 }
44434644
44444645 static int io_msg_ring_prep(struct io_kiocb *req,
44454646 const struct io_uring_sqe *sqe)
44464647 {
4447− if (unlikely(sqe->addr || sqe->ioprio || sqe->rw_flags ||
4448− sqe->splice_fd_in || sqe->buf_index || sqe->personality))
4648+ if (unlikely(sqe->addr || sqe->rw_flags || sqe->splice_fd_in ||
4649+ sqe->buf_index || sqe->personality))
44494650 return -EINVAL;
44504651
44514652 req->msg.user_data = READ_ONCE(sqe->off);
@@ -4489,12 +4690,7 @@ done: · 37 unmodified lines
44894690
44904691 static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
44914692 {
4492− struct io_ring_ctx *ctx = req->ctx;
4493−
4494− if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4495− return -EINVAL;
4496− if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4497− sqe->splice_fd_in))
4693+ if (unlikely(sqe->addr || sqe->buf_index || sqe->splice_fd_in))
44984694 return -EINVAL;
44994695
45004696 req->sync.flags = READ_ONCE(sqe->fsync_flags);
@@ -4527,10 +4723,7 @@ static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) · 26 unmodified lines
45274723 static int io_fallocate_prep(struct io_kiocb *req,
45284724 const struct io_uring_sqe *sqe)
45294725 {
4530− if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
4531− sqe->splice_fd_in)
4532− return -EINVAL;
4533− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4726+ if (sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in)
45344727 return -EINVAL;
45354728
45364729 req->sync.off = READ_ONCE(sqe->off);
@@ -4561,9 +4754,7 @@ static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe · 24 unmodified lines
45614754 const char __user *fname;
45624755 int ret;
45634756
4564− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4565− return -EINVAL;
4566− if (unlikely(sqe->ioprio || sqe->buf_index))
4757+ if (unlikely(sqe->buf_index))
45674758 return -EINVAL;
45684759 if (unlikely(req->flags & REQ_F_FIXED_FILE))
45694760 return -EBADF;
@@ -4618,6 +4809,61 @@ static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 48 unmodified lines
46184809 return __io_openat_prep(req, sqe);
46194810 }
46204811
4812+static int io_file_bitmap_get(struct io_ring_ctx *ctx)
4813+{
4814+ struct io_file_table *table = &ctx->file_table;
4815+ unsigned long nr = ctx->nr_user_files;
4816+ int ret;
4817+
4818+ if (table->alloc_hint >= nr)
4819+ table->alloc_hint = 0;
4820+
4821+ do {
4822+ ret = find_next_zero_bit(table->bitmap, nr, table->alloc_hint);
4823+ if (ret != nr) {
4824+ table->alloc_hint = ret + 1;
4825+ return ret;
4826+ }
4827+ if (!table->alloc_hint)
4828+ break;
4829+
4830+ nr = table->alloc_hint;
4831+ table->alloc_hint = 0;
4832+ } while (1);
4833+
4834+ return -ENFILE;
4835+}
4836+
4837+static int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
4838+ struct file *file, unsigned int file_slot)
4839+{
4840+ bool alloc_slot = file_slot == IORING_FILE_INDEX_ALLOC;
4841+ struct io_ring_ctx *ctx = req->ctx;
4842+ int ret;
4843+
4844+ if (alloc_slot) {
4845+ io_ring_submit_lock(ctx, issue_flags);
4846+ ret = io_file_bitmap_get(ctx);
4847+ if (unlikely(ret < 0)) {
4848+ io_ring_submit_unlock(ctx, issue_flags);
4849+ return ret;
4850+ }
4851+
4852+ file_slot = ret;
4853+ } else {
4854+ file_slot--;
4855+ }
4856+
4857+ ret = io_install_fixed_file(req, file, issue_flags, file_slot);
4858+ if (alloc_slot) {
4859+ io_ring_submit_unlock(ctx, issue_flags);
4860+ if (!ret)
4861+ return file_slot;
4862+ }
4863+
4864+ return ret;
4865+}
4866+
46214867 static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
46224868 {
46234869 struct open_flags op;
@@ -4673,8 +4919,8 @@ static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) · 49 unmodified lines
46734919 if (!fixed)
46744920 fd_install(ret, file);
46754921 else
4676− ret = io_install_fixed_file(req, file, issue_flags,
4677− req->open.file_slot - 1);
4922+ ret = io_fixed_fd_install(req, issue_flags, file,
4923+ req->open.file_slot);
46784924 err:
46794925 putname(req->open.filename);
46804926 req->flags &= ~REQ_F_NEED_CLEANUP;
@@ -4695,7 +4941,7 @@ static int io_remove_buffers_prep(struct io_kiocb *req, · 14 unmodified lines
46954941 struct io_provide_buf *p = &req->pbuf;
46964942 u64 tmp;
46974943
4698− if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4944+ if (sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
46994945 sqe->splice_fd_in)
47004946 return -EINVAL;
47014947
@@ -4718,6 +4964,20 @@ static int __io_remove_buffers(struct io_ring_ctx *ctx, · 16 unmodified lines
47184964 if (!nbufs)
47194965 return 0;
47204966
4967+ if (bl->buf_nr_pages) {
4968+ int j;
4969+
4970+ i = bl->buf_ring->tail - bl->head;
4971+ for (j = 0; j < bl->buf_nr_pages; j++)
4972+ unpin_user_page(bl->buf_pages[j]);
4973+ kvfree(bl->buf_pages);
4974+ bl->buf_pages = NULL;
4975+ bl->buf_nr_pages = 0;
4976+ /* make sure it's seen as empty */
4977+ INIT_LIST_HEAD(&bl->buf_list);
4978+ return i;
4979+ }
4980+
47214981 /* the head kbuf is the list itself */
47224982 while (!list_empty(&bl->buf_list)) {
47234983 struct io_buffer *nxt;
@@ -4739,22 +4999,23 @@ static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) · 15 unmodified lines
47394999 struct io_ring_ctx *ctx = req->ctx;
47405000 struct io_buffer_list *bl;
47415001 int ret = 0;
4742− bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
4743−
4744− io_ring_submit_lock(ctx, needs_lock);
47455002
4746− lockdep_assert_held(&ctx->uring_lock);
5003+ io_ring_submit_lock(ctx, issue_flags);
47475004
47485005 ret = -ENOENT;
47495006 bl = io_buffer_get_list(ctx, p->bgid);
4750− if (bl)
4751− ret = __io_remove_buffers(ctx, bl, p->nbufs);
5007+ if (bl) {
5008+ ret = -EINVAL;
5009+ /* can't use provide/remove buffers command on mapped buffers */
5010+ if (!bl->buf_nr_pages)
5011+ ret = __io_remove_buffers(ctx, bl, p->nbufs);
5012+ }
47525013 if (ret < 0)
47535014 req_set_fail(req);
47545015
47555016 /* complete before unlock, IOPOLL may need the lock */
47565017 __io_req_complete(req, issue_flags, ret, 0);
4757− io_ring_submit_unlock(ctx, needs_lock);
5018+ io_ring_submit_unlock(ctx, issue_flags);
47585019 return 0;
47595020 }
47605021
@@ -4765,7 +5026,7 @@ static int io_provide_buffers_prep(struct io_kiocb *req, · 4 unmodified lines
47655026 struct io_provide_buf *p = &req->pbuf;
47665027 u64 tmp;
47675028
4768− if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
5029+ if (sqe->rw_flags || sqe->splice_fd_in)
47695030 return -EINVAL;
47705031
47715032 tmp = READ_ONCE(sqe->fd);
@@ -4862,26 +5123,56 @@ static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf, · 90 unmodified lines
48625123 return i ? 0 : -ENOMEM;
48635124 }
48645125
5126+static __cold int io_init_bl_list(struct io_ring_ctx *ctx)
5127+{
5128+ int i;
5129+
5130+ ctx->io_bl = kcalloc(BGID_ARRAY, sizeof(struct io_buffer_list),
5131+ GFP_KERNEL);
5132+ if (!ctx->io_bl)
5133+ return -ENOMEM;
5134+
5135+ for (i = 0; i < BGID_ARRAY; i++) {
5136+ INIT_LIST_HEAD(&ctx->io_bl[i].buf_list);
5137+ ctx->io_bl[i].bgid = i;
5138+ }
5139+
5140+ return 0;
5141+}
5142+
48655143 static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
48665144 {
48675145 struct io_provide_buf *p = &req->pbuf;
48685146 struct io_ring_ctx *ctx = req->ctx;
48695147 struct io_buffer_list *bl;
48705148 int ret = 0;
4871− bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
48725149
4873− io_ring_submit_lock(ctx, needs_lock);
5150+ io_ring_submit_lock(ctx, issue_flags);
48745151
4875− lockdep_assert_held(&ctx->uring_lock);
5152+ if (unlikely(p->bgid < BGID_ARRAY && !ctx->io_bl)) {
5153+ ret = io_init_bl_list(ctx);
5154+ if (ret)
5155+ goto err;
5156+ }
48765157
48775158 bl = io_buffer_get_list(ctx, p->bgid);
48785159 if (unlikely(!bl)) {
4879− bl = kmalloc(sizeof(*bl), GFP_KERNEL);
5160+ bl = kzalloc(sizeof(*bl), GFP_KERNEL);
48805161 if (!bl) {
48815162 ret = -ENOMEM;
48825163 goto err;
48835164 }
4884− io_buffer_add_list(ctx, bl, p->bgid);
5165+ INIT_LIST_HEAD(&bl->buf_list);
5166+ ret = io_buffer_add_list(ctx, bl, p->bgid);
5167+ if (ret) {
5168+ kfree(bl);
5169+ goto err;
5170+ }
5171+ }
5172+ /* can't add buffers via this command for a mapped buffer ring */
5173+ if (bl->buf_nr_pages) {
5174+ ret = -EINVAL;
5175+ goto err;
48855176 }
48865177
48875178 ret = io_add_buffers(ctx, p, bl);
@@ -4890,7 +5181,7 @@ err: · 2 unmodified lines
48905181 req_set_fail(req);
48915182 /* complete before unlock, IOPOLL may need the lock */
48925183 __io_req_complete(req, issue_flags, ret, 0);
4893− io_ring_submit_unlock(ctx, needs_lock);
5184+ io_ring_submit_unlock(ctx, issue_flags);
48945185 return 0;
48955186 }
48965187
@@ -4898,9 +5189,7 @@ static int io_epoll_ctl_prep(struct io_kiocb *req, · 1 unmodified line
48985189 const struct io_uring_sqe *sqe)
48995190 {
49005191 #if defined(CONFIG_EPOLL)
4901− if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
4902− return -EINVAL;
4903− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5192+ if (sqe->buf_index || sqe->splice_fd_in)
49045193 return -EINVAL;
49055194
49065195 req->epoll.epfd = READ_ONCE(sqe->fd);
@@ -4944,9 +5233,7 @@ static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) · 37 unmodified lines
49445233 static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
49455234 {
49465235 #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4947− if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
4948− return -EINVAL;
4949− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5236+ if (sqe->buf_index || sqe->off || sqe->splice_fd_in)
49505237 return -EINVAL;
49515238
49525239 req->madvise.addr = READ_ONCE(sqe->addr);
@@ -4979,9 +5266,7 @@ static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) · 26 unmodified lines
49795266
49805267 static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
49815268 {
4982− if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
4983− return -EINVAL;
4984− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5269+ if (sqe->buf_index || sqe->addr || sqe->splice_fd_in)
49855270 return -EINVAL;
49865271
49875272 req->fadvise.offset = READ_ONCE(sqe->off);
@@ -5017,9 +5302,7 @@ static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 29 unmodified lines
50175302 {
50185303 const char __user *path;
50195304
5020− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5021− return -EINVAL;
5022− if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
5305+ if (sqe->buf_index || sqe->splice_fd_in)
50235306 return -EINVAL;
50245307 if (req->flags & REQ_F_FIXED_FILE)
50255308 return -EBADF;
@@ -5064,10 +5347,7 @@ static int io_statx(struct io_kiocb *req, unsigned int issue_flags) · 38 unmodified lines
50645347
50655348 static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
50665349 {
5067− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5068− return -EINVAL;
5069− if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
5070− sqe->rw_flags || sqe->buf_index)
5350+ if (sqe->off || sqe->addr || sqe->len || sqe->rw_flags || sqe->buf_index)
50715351 return -EINVAL;
50725352 if (req->flags & REQ_F_FIXED_FILE)
50735353 return -EBADF;
@@ -5099,7 +5379,8 @@ static int io_close(struct io_kiocb *req, unsigned int issue_flags) · 25 unmodified lines
50995379 spin_unlock(&files->file_lock);
51005380 goto err;
51015381 }
5102− file = fdt->fd[close->fd];
5382+ file = rcu_dereference_protected(fdt->fd[close->fd],
5383+ lockdep_is_held(&files->file_lock));
51035384 if (!file || file->f_op == &io_uring_fops) {
51045385 spin_unlock(&files->file_lock);
51055386 file = NULL;
@@ -5133,12 +5414,7 @@ err: · 27 unmodified lines
51335414
51345415 static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
51355416 {
5136− struct io_ring_ctx *ctx = req->ctx;
5137−
5138− if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
5139− return -EINVAL;
5140− if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
5141− sqe->splice_fd_in))
5417+ if (unlikely(sqe->addr || sqe->buf_index || sqe->splice_fd_in))
51425418 return -EINVAL;
51435419
51445420 req->sync.off = READ_ONCE(sqe->off);
@@ -5164,6 +5440,13 @@ static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) · 19 unmodified lines
51645440 }
51655441
51665442 #if defined(CONFIG_NET)
5443+static bool io_net_retry(struct socket *sock, int flags)
5444+{
5445+ if (!(flags & MSG_WAITALL))
5446+ return false;
5447+ return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET;
5448+}
5449+
51675450 static int io_setup_async_msg(struct io_kiocb *req,
51685451 struct io_async_msghdr *kmsg)
51695452 {
@@ -5209,13 +5492,16 @@ static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 39 unmodified lines
52095492 {
52105493 struct io_sr_msg *sr = &req->sr_msg;
52115494
5212− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5495+ if (unlikely(sqe->file_index))
52135496 return -EINVAL;
52145497 if (unlikely(sqe->addr2 || sqe->file_index))
52155498 return -EINVAL;
52165499
52175500 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
52185501 sr->len = READ_ONCE(sqe->len);
5502+ sr->flags = READ_ONCE(sqe->addr2);
5503+ if (sr->flags & ~IORING_RECVSEND_POLL_FIRST)
5504+ return -EINVAL;
52195505 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
52205506 if (sr->msg_flags & MSG_DONTWAIT)
52215507 req->flags |= REQ_F_NOWAIT;
@@ -5224,12 +5510,14 @@ static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 2 unmodified lines
52245510 if (req->ctx->compat)
52255511 sr->msg_flags |= MSG_CMSG_COMPAT;
52265512 #endif
5513+ sr->done_io = 0;
52275514 return 0;
52285515 }
52295516
52305517 static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
52315518 {
52325519 struct io_async_msghdr iomsg, *kmsg;
5520+ struct io_sr_msg *sr = &req->sr_msg;
52335521 struct socket *sock;
52345522 unsigned flags;
52355523 int min_ret = 0;
@@ -5248,7 +5536,11 @@ static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) · 12 unmodified lines
52485536 kmsg = &iomsg;
52495537 }
52505538
5251− flags = req->sr_msg.msg_flags;
5539+ if (!(req->flags & REQ_F_POLLED) &&
5540+ (sr->flags & IORING_RECVSEND_POLL_FIRST))
5541+ return io_setup_async_msg(req, kmsg);
5542+
5543+ flags = sr->msg_flags;
52525544 if (issue_flags & IO_URING_F_NONBLOCK)
52535545 flags |= MSG_DONTWAIT;
52545546 if (flags & MSG_WAITALL)
@@ -5261,12 +5553,21 @@ static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) · 6 unmodified lines
52615553 return io_setup_async_msg(req, kmsg);
52625554 if (ret == -ERESTARTSYS)
52635555 ret = -EINTR;
5556+ if (ret > 0 && io_net_retry(sock, flags)) {
5557+ sr->done_io += ret;
5558+ req->flags |= REQ_F_PARTIAL_IO;
5559+ return io_setup_async_msg(req, kmsg);
5560+ }
52645561 req_set_fail(req);
52655562 }
52665563 /* fast path, check for non-NULL to avoid function call */
52675564 if (kmsg->free_iov)
52685565 kfree(kmsg->free_iov);
52695566 req->flags &= ~REQ_F_NEED_CLEANUP;
5567+ if (ret >= 0)
5568+ ret += sr->done_io;
5569+ else if (sr->done_io)
5570+ ret = sr->done_io;
52705571 __io_req_complete(req, issue_flags, ret, 0);
52715572 return 0;
52725573 }
@@ -5281,6 +5582,10 @@ static int io_send(struct io_kiocb *req, unsigned int issue_flags) · 8 unmodified lines
52815582 int min_ret = 0;
52825583 int ret;
52835584
5585+ if (!(req->flags & REQ_F_POLLED) &&
5586+ (sr->flags & IORING_RECVSEND_POLL_FIRST))
5587+ return -EAGAIN;
5588+
52845589 sock = sock_from_file(req->file);
52855590 if (unlikely(!sock))
52865591 return -ENOTSOCK;
@@ -5294,7 +5599,7 @@ static int io_send(struct io_kiocb *req, unsigned int issue_flags) · 7 unmodified lines
52945599 msg.msg_controllen = 0;
52955600 msg.msg_namelen = 0;
52965601
5297− flags = req->sr_msg.msg_flags;
5602+ flags = sr->msg_flags;
52985603 if (issue_flags & IO_URING_F_NONBLOCK)
52995604 flags |= MSG_DONTWAIT;
53005605 if (flags & MSG_WAITALL)
@@ -5307,8 +5612,19 @@ static int io_send(struct io_kiocb *req, unsigned int issue_flags) · 6 unmodified lines
53075612 return -EAGAIN;
53085613 if (ret == -ERESTARTSYS)
53095614 ret = -EINTR;
5615+ if (ret > 0 && io_net_retry(sock, flags)) {
5616+ sr->len -= ret;
5617+ sr->buf += ret;
5618+ sr->done_io += ret;
5619+ req->flags |= REQ_F_PARTIAL_IO;
5620+ return -EAGAIN;
5621+ }
53105622 req_set_fail(req);
53115623 }
5624+ if (ret >= 0)
5625+ ret += sr->done_io;
5626+ else if (sr->done_io)
5627+ ret = sr->done_io;
53125628 __io_req_complete(req, issue_flags, ret, 0);
53135629 return 0;
53145630 }
@@ -5400,14 +5716,6 @@ static int io_recvmsg_copy_hdr(struct io_kiocb *req, · 85 unmodified lines
54005716 return __io_recvmsg_copy_hdr(req, iomsg);
54015717 }
54025718
5403−static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
5404− unsigned int issue_flags)
5405−{
5406− struct io_sr_msg *sr = &req->sr_msg;
5407−
5408− return io_buffer_select(req, &sr->len, sr->bgid, issue_flags);
5409−}
5410−
54115719 static int io_recvmsg_prep_async(struct io_kiocb *req)
54125720 {
54135721 int ret;
@@ -5422,14 +5730,16 @@ static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 8 unmodified lines
54225730 {
54235731 struct io_sr_msg *sr = &req->sr_msg;
54245732
5425− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5733+ if (unlikely(sqe->file_index))
54265734 return -EINVAL;
54275735 if (unlikely(sqe->addr2 || sqe->file_index))
54285736 return -EINVAL;
54295737
54305738 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
54315739 sr->len = READ_ONCE(sqe->len);
5432− sr->bgid = READ_ONCE(sqe->buf_group);
5740+ sr->flags = READ_ONCE(sqe->addr2);
5741+ if (sr->flags & ~IORING_RECVSEND_POLL_FIRST)
5742+ return -EINVAL;
54335743 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
54345744 if (sr->msg_flags & MSG_DONTWAIT)
54355745 req->flags |= REQ_F_NOWAIT;
@@ -5442,19 +5752,11 @@ static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 6 unmodified lines
54425752 return 0;
54435753 }
54445754
5445−static bool io_net_retry(struct socket *sock, int flags)
5446−{
5447− if (!(flags & MSG_WAITALL))
5448− return false;
5449− return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET;
5450−}
5451−
54525755 static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
54535756 {
54545757 struct io_async_msghdr iomsg, *kmsg;
54555758 struct io_sr_msg *sr = &req->sr_msg;
54565759 struct socket *sock;
5457− struct io_buffer *kbuf;
54585760 unsigned flags;
54595761 int ret, min_ret = 0;
54605762 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
@@ -5472,24 +5774,29 @@ static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) · 11 unmodified lines
54725774 kmsg = &iomsg;
54735775 }
54745776
5475− if (req->flags & REQ_F_BUFFER_SELECT) {
5476− kbuf = io_recv_buffer_select(req, issue_flags);
5477− if (IS_ERR(kbuf))
5478− return PTR_ERR(kbuf);
5479− kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
5480− kmsg->fast_iov[0].iov_len = req->sr_msg.len;
5481− iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
5482− 1, req->sr_msg.len);
5777+ if (!(req->flags & REQ_F_POLLED) &&
5778+ (sr->flags & IORING_RECVSEND_POLL_FIRST))
5779+ return io_setup_async_msg(req, kmsg);
5780+
5781+ if (io_do_buffer_select(req)) {
5782+ void __user *buf;
5783+
5784+ buf = io_buffer_select(req, &sr->len, issue_flags);
5785+ if (!buf)
5786+ return -ENOBUFS;
5787+ kmsg->fast_iov[0].iov_base = buf;
5788+ kmsg->fast_iov[0].iov_len = sr->len;
5789+ iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, 1,
5790+ sr->len);
54835791 }
54845792
5485− flags = req->sr_msg.msg_flags;
5793+ flags = sr->msg_flags;
54865794 if (force_nonblock)
54875795 flags |= MSG_DONTWAIT;
54885796 if (flags & MSG_WAITALL)
54895797 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
54905798
5491− ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
5492− kmsg->uaddr, flags);
5799+ ret = __sys_recvmsg_sock(sock, &kmsg->msg, sr->umsg, kmsg->uaddr, flags);
54935800 if (ret < min_ret) {
54945801 if (ret == -EAGAIN && force_nonblock)
54955802 return io_setup_async_msg(req, kmsg);
@@ -5519,28 +5826,32 @@ static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) · 23 unmodified lines
55195826
55205827 static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
55215828 {
5522− struct io_buffer *kbuf;
55235829 struct io_sr_msg *sr = &req->sr_msg;
55245830 struct msghdr msg;
5525− void __user *buf = sr->buf;
55265831 struct socket *sock;
55275832 struct iovec iov;
55285833 unsigned flags;
55295834 int ret, min_ret = 0;
55305835 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
55315836
5837+ if (!(req->flags & REQ_F_POLLED) &&
5838+ (sr->flags & IORING_RECVSEND_POLL_FIRST))
5839+ return -EAGAIN;
5840+
55325841 sock = sock_from_file(req->file);
55335842 if (unlikely(!sock))
55345843 return -ENOTSOCK;
55355844
5536− if (req->flags & REQ_F_BUFFER_SELECT) {
5537− kbuf = io_recv_buffer_select(req, issue_flags);
5538− if (IS_ERR(kbuf))
5539− return PTR_ERR(kbuf);
5540− buf = u64_to_user_ptr(kbuf->addr);
5845+ if (io_do_buffer_select(req)) {
5846+ void __user *buf;
5847+
5848+ buf = io_buffer_select(req, &sr->len, issue_flags);
5849+ if (!buf)
5850+ return -ENOBUFS;
5851+ sr->buf = buf;
55415852 }
55425853
5543− ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
5854+ ret = import_single_range(READ, sr->buf, sr->len, &iov, &msg.msg_iter);
55445855 if (unlikely(ret))
55455856 goto out_free;
55465857
@@ -5551,7 +5862,7 @@ static int io_recv(struct io_kiocb *req, unsigned int issue_flags) · 4 unmodified lines
55515862 msg.msg_iocb = NULL;
55525863 msg.msg_flags = 0;
55535864
5554− flags = req->sr_msg.msg_flags;
5865+ flags = sr->msg_flags;
55555866 if (force_nonblock)
55565867 flags |= MSG_DONTWAIT;
55575868 if (flags & MSG_WAITALL)
@@ -5587,29 +5898,39 @@ out_free: · 29 unmodified lines
55875898 static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
55885899 {
55895900 struct io_accept *accept = &req->accept;
5901+ unsigned flags;
55905902
5591− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5592− return -EINVAL;
5593− if (sqe->ioprio || sqe->len || sqe->buf_index)
5903+ if (sqe->len || sqe->buf_index)
55945904 return -EINVAL;
55955905
55965906 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
55975907 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
55985908 accept->flags = READ_ONCE(sqe->accept_flags);
55995909 accept->nofile = rlimit(RLIMIT_NOFILE);
5910+ flags = READ_ONCE(sqe->ioprio);
5911+ if (flags & ~IORING_ACCEPT_MULTISHOT)
5912+ return -EINVAL;
56005913
56015914 accept->file_slot = READ_ONCE(sqe->file_index);
5602− if (accept->file_slot && (accept->flags & SOCK_CLOEXEC))
5603− return -EINVAL;
5915+ if (accept->file_slot) {
5916+ if (accept->flags & SOCK_CLOEXEC)
5917+ return -EINVAL;
5918+ if (flags & IORING_ACCEPT_MULTISHOT &&
5919+ accept->file_slot != IORING_FILE_INDEX_ALLOC)
5920+ return -EINVAL;
5921+ }
56045922 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
56055923 return -EINVAL;
56065924 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
56075925 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
5926+ if (flags & IORING_ACCEPT_MULTISHOT)
5927+ req->flags |= REQ_F_APOLL_MULTISHOT;
56085928 return 0;
56095929 }
56105930
56115931 static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
56125932 {
5933+ struct io_ring_ctx *ctx = req->ctx;
56135934 struct io_accept *accept = &req->accept;
56145935 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
56155936 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
@@ -5617,6 +5938,7 @@ static int io_accept(struct io_kiocb *req, unsigned int issue_flags) · 1 unmodified line
56175938 struct file *file;
56185939 int ret, fd;
56195940
5941+retry:
56205942 if (!fixed) {
56215943 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
56225944 if (unlikely(fd < 0))
@@ -5628,8 +5950,17 @@ static int io_accept(struct io_kiocb *req, unsigned int issue_flags) · 5 unmodified lines
56285950 if (!fixed)
56295951 put_unused_fd(fd);
56305952 ret = PTR_ERR(file);
5631− if (ret == -EAGAIN && force_nonblock)
5632− return -EAGAIN;
5953+ if (ret == -EAGAIN && force_nonblock) {
5954+ /*
5955+ * if it's multishot and polled, we don't need to
5956+ * return EAGAIN to arm the poll infra since it
5957+ * has already been done
5958+ */
5959+ if ((req->flags & IO_APOLL_MULTI_POLLED) ==
5960+ IO_APOLL_MULTI_POLLED)
5961+ ret = 0;
5962+ return ret;
5963+ }
56335964 if (ret == -ERESTARTSYS)
56345965 ret = -EINTR;
56355966 req_set_fail(req);
@@ -5637,11 +5968,30 @@ static int io_accept(struct io_kiocb *req, unsigned int issue_flags) · 1 unmodified line
56375968 fd_install(fd, file);
56385969 ret = fd;
56395970 } else {
5640− ret = io_install_fixed_file(req, file, issue_flags,
5641− accept->file_slot - 1);
5971+ ret = io_fixed_fd_install(req, issue_flags, file,
5972+ accept->file_slot);
56425973 }
5643− __io_req_complete(req, issue_flags, ret, 0);
5644− return 0;
5974+
5975+ if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
5976+ __io_req_complete(req, issue_flags, ret, 0);
5977+ return 0;
5978+ }
5979+ if (ret >= 0) {
5980+ bool filled;
5981+
5982+ spin_lock(&ctx->completion_lock);
5983+ filled = io_fill_cqe_aux(ctx, req->cqe.user_data, ret,
5984+ IORING_CQE_F_MORE);
5985+ io_commit_cqring(ctx);
5986+ spin_unlock(&ctx->completion_lock);
5987+ if (filled) {
5988+ io_cqring_ev_posted(ctx);
5989+ goto retry;
5990+ }
5991+ ret = -ECANCELED;
5992+ }
5993+
5994+ return ret;
56455995 }
56465996
56475997 static int io_connect_prep_async(struct io_kiocb *req)
@@ -5656,10 +6006,7 @@ static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 8 unmodified lines
56566006 {
56576007 struct io_connect *conn = &req->connect;
56586008
5659− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5660− return -EINVAL;
5661− if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
5662− sqe->splice_fd_in)
6009+ if (sqe->len || sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in)
56636010 return -EINVAL;
56646011
56656012 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
@@ -5782,7 +6129,7 @@ static void io_poll_req_insert(struct io_kiocb *req) · 116 unmodified lines
57826129 struct io_ring_ctx *ctx = req->ctx;
57836130 struct hlist_head *list;
57846131
5785− list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
6132+ list = &ctx->cancel_hash[hash_long(req->cqe.user_data, ctx->cancel_hash_bits)];
57866133 hlist_add_head(&req->hash_node, list);
57876134 }
57886135
@@ -5841,22 +6188,23 @@ static void io_poll_remove_entries(struct io_kiocb *req) · 52 unmodified lines
58416188 rcu_read_unlock();
58426189 }
58436190
6191+static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags);
58446192 /*
58456193 * All poll tw should go through this. Checks for poll events, manages
58466194 * references, does rewait, etc.
58476195 *
58486196 * Returns a negative error on failure. >0 when no action require, which is
58496197 * either spurious wakeup or multishot CQE is served. 0 when it's done with
5850− * the request, then the mask is stored in req->result.
6198+ * the request, then the mask is stored in req->cqe.res.
58516199 */
5852−static int io_poll_check_events(struct io_kiocb *req, bool locked)
6200+static int io_poll_check_events(struct io_kiocb *req, bool *locked)
58536201 {
58546202 struct io_ring_ctx *ctx = req->ctx;
5855− int v;
6203+ int v, ret;
58566204
58576205 /* req->task == current here, checking PF_EXITING is safe */
58586206 if (unlikely(req->task->flags & PF_EXITING))
5859− io_poll_mark_cancelled(req);
6207+ return -ECANCELED;
58606208
58616209 do {
58626210 v = atomic_read(&req->poll_refs);
@@ -5867,32 +6215,46 @@ static int io_poll_check_events(struct io_kiocb *req, bool locked) · 4 unmodified lines
58676215 if (v & IO_POLL_CANCEL_FLAG)
58686216 return -ECANCELED;
58696217
5870− if (!req->result) {
6218+ if (!req->cqe.res) {
58716219 struct poll_table_struct pt = { ._key = req->apoll_events };
58726220 unsigned flags = locked ? 0 : IO_URING_F_UNLOCKED;
58736221
58746222 if (unlikely(!io_assign_file(req, flags)))
58756223 return -EBADF;
5876− req->result = vfs_poll(req->file, &pt) & req->apoll_events;
6224+ req->cqe.res = vfs_poll(req->file, &pt) & req->apoll_events;
58776225 }
58786226
5879− /* multishot, just fill an CQE and proceed */
5880− if (req->result && !(req->apoll_events & EPOLLONESHOT)) {
5881− __poll_t mask = mangle_poll(req->result & req->apoll_events);
6227+ if ((unlikely(!req->cqe.res)))
6228+ continue;
6229+ if (req->apoll_events & EPOLLONESHOT)
6230+ return 0;
6231+
6232+ /* multishot, just fill a CQE and proceed */
6233+ if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
6234+ __poll_t mask = mangle_poll(req->cqe.res &
6235+ req->apoll_events);
58826236 bool filled;
58836237
58846238 spin_lock(&ctx->completion_lock);
5885− filled = io_fill_cqe_aux(ctx, req->user_data, mask,
5886− IORING_CQE_F_MORE);
6239+ filled = io_fill_cqe_aux(ctx, req->cqe.user_data,
6240+ mask, IORING_CQE_F_MORE);
58876241 io_commit_cqring(ctx);
58886242 spin_unlock(&ctx->completion_lock);
5889− if (unlikely(!filled))
5890− return -ECANCELED;
5891− io_cqring_ev_posted(ctx);
5892− } else if (req->result) {
5893− return 0;
6243+ if (filled) {
6244+ io_cqring_ev_posted(ctx);
6245+ continue;
6246+ }
6247+ return -ECANCELED;
58946248 }
58956249
6250+ io_tw_lock(req->ctx, locked);
6251+ if (unlikely(req->task->flags & PF_EXITING))
6252+ return -EFAULT;
6253+ ret = io_issue_sqe(req,
6254+ IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
6255+ if (ret)
6256+ return ret;
6257+
58966258 /*
58976259 * Release all references, retry if someone tried to restart
58986260 * task_work while we were executing it.
@@ -5907,21 +6269,21 @@ static void io_poll_task_func(struct io_kiocb *req, bool *locked) · 8 unmodified lines
59076269 struct io_ring_ctx *ctx = req->ctx;
59086270 int ret;
59096271
5910− ret = io_poll_check_events(req, *locked);
6272+ ret = io_poll_check_events(req, locked);
59116273 if (ret > 0)
59126274 return;
59136275
59146276 if (!ret) {
5915− req->result = mangle_poll(req->result & req->poll.events);
6277+ req->cqe.res = mangle_poll(req->cqe.res & req->poll.events);
59166278 } else {
5917− req->result = ret;
6279+ req->cqe.res = ret;
59186280 req_set_fail(req);
59196281 }
59206282
59216283 io_poll_remove_entries(req);
59226284 spin_lock(&ctx->completion_lock);
59236285 hash_del(&req->hash_node);
5924− __io_req_complete_post(req, req->result, 0);
6286+ __io_req_complete_post(req, req->cqe.res, 0);
59256287 io_commit_cqring(ctx);
59266288 spin_unlock(&ctx->completion_lock);
59276289 io_cqring_ev_posted(ctx);
@@ -5932,7 +6294,7 @@ static void io_apoll_task_func(struct io_kiocb *req, bool *locked) · 4 unmodified lines
59326294 struct io_ring_ctx *ctx = req->ctx;
59336295 int ret;
59346296
5935− ret = io_poll_check_events(req, *locked);
6297+ ret = io_poll_check_events(req, locked);
59366298 if (ret > 0)
59376299 return;
59386300
@@ -5947,9 +6309,9 @@ static void io_apoll_task_func(struct io_kiocb *req, bool *locked) · 8 unmodified lines
59476309 io_req_complete_failed(req, ret);
59486310 }
59496311
5950−static void __io_poll_execute(struct io_kiocb *req, int mask, int events)
6312+static void __io_poll_execute(struct io_kiocb *req, int mask, __poll_t events)
59516313 {
5952− req->result = mask;
6314+ req->cqe.res = mask;
59536315 /*
59546316 * This is useful for poll that is armed on behalf of another
59556317 * request, and where the wakeup path could be on a different
@@ -5962,11 +6324,12 @@ static void __io_poll_execute(struct io_kiocb *req, int mask, int events) · 6 unmodified lines
59626324 else
59636325 req->io_task_work.func = io_apoll_task_func;
59646326
5965− trace_io_uring_task_add(req->ctx, req, req->user_data, req->opcode, mask);
6327+ trace_io_uring_task_add(req->ctx, req, req->cqe.user_data, req->opcode, mask);
59666328 io_req_task_work_add(req, false);
59676329 }
59686330
5969−static inline void io_poll_execute(struct io_kiocb *req, int res, int events)
6331+static inline void io_poll_execute(struct io_kiocb *req, int res,
6332+ __poll_t events)
59706333 {
59716334 if (io_poll_get_ownership(req))
59726335 __io_poll_execute(req, res, events);
@@ -5981,6 +6344,7 @@ static void io_poll_cancel_req(struct io_kiocb *req) · 8 unmodified lines
59816344
59826345 #define wqe_to_req(wait) ((void *)((unsigned long) (wait)->private & ~1))
59836346 #define wqe_is_double(wait) ((unsigned long) (wait)->private & 1)
6347+#define IO_ASYNC_POLL_COMMON (EPOLLONESHOT | EPOLLPRI)
59846348
59856349 static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
59866350 void *key)
@@ -6015,7 +6379,7 @@ static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, · 28 unmodified lines
60156379 }
60166380
60176381 /* for instances that support it check for an event match first */
6018− if (mask && !(mask & poll->events))
6382+ if (mask && !(mask & (poll->events & ~IO_ASYNC_POLL_COMMON)))
60196383 return 0;
60206384
60216385 if (io_poll_get_ownership(req)) {
@@ -6101,6 +6465,7 @@ static int __io_arm_poll_handler(struct io_kiocb *req, · 79 unmodified lines
61016465 int v;
61026466
61036467 INIT_HLIST_NODE(&req->hash_node);
6468+ req->work.cancel_seq = atomic_read(&ctx->cancel_seq);
61046469 io_init_poll_iocb(poll, mask, io_poll_wake);
61056470 poll->file = req->file;
61066471
@@ -6171,28 +6536,34 @@ static int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags) · 64 unmodified lines
61716536 struct io_ring_ctx *ctx = req->ctx;
61726537 struct async_poll *apoll;
61736538 struct io_poll_table ipt;
6174− __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI;
6539+ __poll_t mask = POLLPRI | POLLERR;
61756540 int ret;
61766541
61776542 if (!def->pollin && !def->pollout)
61786543 return IO_APOLL_ABORTED;
6179− if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED))
6544+ if (!file_can_poll(req->file))
61806545 return IO_APOLL_ABORTED;
6546+ if ((req->flags & (REQ_F_POLLED|REQ_F_PARTIAL_IO)) == REQ_F_POLLED)
6547+ return IO_APOLL_ABORTED;
6548+ if (!(req->flags & REQ_F_APOLL_MULTISHOT))
6549+ mask |= EPOLLONESHOT;
61816550
61826551 if (def->pollin) {
6183− mask |= POLLIN | POLLRDNORM;
6552+ mask |= EPOLLIN | EPOLLRDNORM;
61846553
61856554 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
61866555 if ((req->opcode == IORING_OP_RECVMSG) &&
61876556 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
6188− mask &= ~POLLIN;
6557+ mask &= ~EPOLLIN;
61896558 } else {
6190− mask |= POLLOUT | POLLWRNORM;
6559+ mask |= EPOLLOUT | EPOLLWRNORM;
61916560 }
61926561 if (def->poll_exclusive)
61936562 mask |= EPOLLEXCLUSIVE;
6194− if (!(issue_flags & IO_URING_F_UNLOCKED) &&
6195− !list_empty(&ctx->apoll_cache)) {
6563+ if (req->flags & REQ_F_POLLED) {
6564+ apoll = req->apoll;
6565+ } else if (!(issue_flags & IO_URING_F_UNLOCKED) &&
6566+ !list_empty(&ctx->apoll_cache)) {
61966567 apoll = list_first_entry(&ctx->apoll_cache, struct async_poll,
61976568 poll.wait.entry);
61986569 list_del_init(&apoll->poll.wait.entry);
@@ -6212,7 +6583,7 @@ static int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags) · 13 unmodified lines
62126583 if (ret || ipt.error)
62136584 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
62146585
6215− trace_io_uring_poll_arm(ctx, req, req->user_data, req->opcode,
6586+ trace_io_uring_poll_arm(ctx, req, req->cqe.user_data, req->opcode,
62166587 mask, apoll->poll.events);
62176588 return IO_APOLL_OK;
62186589 }
@@ -6245,24 +6616,53 @@ static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, · 26 unmodified lines
62456616 return found;
62466617 }
62476618
6248−static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
6249− bool poll_only)
6619+static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,
6620+ struct io_cancel_data *cd)
62506621 __must_hold(&ctx->completion_lock)
62516622 {
62526623 struct hlist_head *list;
62536624 struct io_kiocb *req;
62546625
6255− list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
6626+ list = &ctx->cancel_hash[hash_long(cd->data, ctx->cancel_hash_bits)];
62566627 hlist_for_each_entry(req, list, hash_node) {
6257− if (sqe_addr != req->user_data)
6628+ if (cd->data != req->cqe.user_data)
62586629 continue;
62596630 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
62606631 continue;
6632+ if (cd->flags & IORING_ASYNC_CANCEL_ALL) {
6633+ if (cd->seq == req->work.cancel_seq)
6634+ continue;
6635+ req->work.cancel_seq = cd->seq;
6636+ }
62616637 return req;
62626638 }
62636639 return NULL;
62646640 }
62656641
6642+static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx,
6643+ struct io_cancel_data *cd)
6644+ __must_hold(&ctx->completion_lock)
6645+{
6646+ struct io_kiocb *req;
6647+ int i;
6648+
6649+ for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
6650+ struct hlist_head *list;
6651+
6652+ list = &ctx->cancel_hash[i];
6653+ hlist_for_each_entry(req, list, hash_node) {
6654+ if (!(cd->flags & IORING_ASYNC_CANCEL_ANY) &&
6655+ req->file != cd->file)
6656+ continue;
6657+ if (cd->seq == req->work.cancel_seq)
6658+ continue;
6659+ req->work.cancel_seq = cd->seq;
6660+ return req;
6661+ }
6662+ }
6663+ return NULL;
6664+}
6665+
62666666 static bool io_poll_disarm(struct io_kiocb *req)
62676667 __must_hold(&ctx->completion_lock)
62686668 {
@@ -6273,12 +6673,15 @@ static bool io_poll_disarm(struct io_kiocb *req) · 4 unmodified lines
62736673 return true;
62746674 }
62756675
6276−static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
6277− bool poll_only)
6676+static int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
62786677 __must_hold(&ctx->completion_lock)
62796678 {
6280− struct io_kiocb *req = io_poll_find(ctx, sqe_addr, poll_only);
6679+ struct io_kiocb *req;
62816680
6681+ if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_ANY))
6682+ req = io_poll_file_find(ctx, cd);
6683+ else
6684+ req = io_poll_find(ctx, false, cd);
62826685 if (!req)
62836686 return -ENOENT;
62846687 io_poll_cancel_req(req);
@@ -6305,9 +6708,7 @@ static int io_poll_update_prep(struct io_kiocb *req, · 20 unmodified lines
63056708 struct io_poll_update *upd = &req->poll_update;
63066709 u32 flags;
63076710
6308− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6309− return -EINVAL;
6310− if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
6711+ if (sqe->buf_index || sqe->splice_fd_in)
63116712 return -EINVAL;
63126713 flags = READ_ONCE(sqe->len);
63136714 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
@@ -6337,9 +6738,7 @@ static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe · 23 unmodified lines
63376738 struct io_poll_iocb *poll = &req->poll;
63386739 u32 flags;
63396740
6340− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6341− return -EINVAL;
6342− if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
6741+ if (sqe->buf_index || sqe->off || sqe->addr)
63436742 return -EINVAL;
63446743 flags = READ_ONCE(sqe->len);
63456744 if (flags & ~IORING_POLL_ADD_MULTI)
@@ -6369,13 +6768,14 @@ static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) · 23 unmodified lines
63696768
63706769 static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
63716770 {
6771+ struct io_cancel_data cd = { .data = req->poll_update.old_user_data, };
63726772 struct io_ring_ctx *ctx = req->ctx;
63736773 struct io_kiocb *preq;
63746774 int ret2, ret = 0;
63756775 bool locked;
63766776
63776777 spin_lock(&ctx->completion_lock);
6378− preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
6778+ preq = io_poll_find(ctx, true, &cd);
63796779 if (!preq || !io_poll_disarm(preq)) {
63806780 spin_unlock(&ctx->completion_lock);
63816781 ret = preq ? -EALREADY : -ENOENT;
@@ -6391,7 +6791,7 @@ static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) · 9 unmodified lines
63916791 preq->poll.events |= IO_POLL_UNMASK;
63926792 }
63936793 if (req->poll_update.update_user_data)
6394− preq->user_data = req->poll_update.new_user_data;
6794+ preq->cqe.user_data = req->poll_update.new_user_data;
63956795
63966796 ret2 = io_poll_add(preq, issue_flags);
63976797 /* successfully updated, don't complete poll request */
@@ -6400,7 +6800,7 @@ static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) · 2 unmodified lines
64006800 }
64016801
64026802 req_set_fail(preq);
6403− preq->result = -ECANCELED;
6803+ preq->cqe.res = -ECANCELED;
64046804 locked = !(issue_flags & IO_URING_F_UNLOCKED);
64056805 io_req_task_complete(preq, &locked);
64066806 out:
@@ -6428,14 +6828,14 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) · 21 unmodified lines
64286828 if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS))
64296829 req_set_fail(req);
64306830
6431− req->result = -ETIME;
6831+ req->cqe.res = -ETIME;
64326832 req->io_task_work.func = io_req_task_complete;
64336833 io_req_task_work_add(req, false);
64346834 return HRTIMER_NORESTART;
64356835 }
64366836
64376837 static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
6438− __u64 user_data)
6838+ struct io_cancel_data *cd)
64396839 __must_hold(&ctx->timeout_lock)
64406840 {
64416841 struct io_timeout_data *io;
@@ -6443,9 +6843,16 @@ static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, · 1 unmodified line
64436843 bool found = false;
64446844
64456845 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
6446− found = user_data == req->user_data;
6447− if (found)
6448− break;
6846+ if (!(cd->flags & IORING_ASYNC_CANCEL_ANY) &&
6847+ cd->data != req->cqe.user_data)
6848+ continue;
6849+ if (cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY)) {
6850+ if (cd->seq == req->work.cancel_seq)
6851+ continue;
6852+ req->work.cancel_seq = cd->seq;
6853+ }
6854+ found = true;
6855+ break;
64496856 }
64506857 if (!found)
64516858 return ERR_PTR(-ENOENT);
@@ -6457,11 +6864,14 @@ static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, · 5 unmodified lines
64576864 return req;
64586865 }
64596866
6460−static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
6867+static int io_timeout_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
64616868 __must_hold(&ctx->completion_lock)
6462− __must_hold(&ctx->timeout_lock)
64636869 {
6464− struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6870+ struct io_kiocb *req;
6871+
6872+ spin_lock_irq(&ctx->timeout_lock);
6873+ req = io_timeout_extract(ctx, cd);
6874+ spin_unlock_irq(&ctx->timeout_lock);
64656875
64666876 if (IS_ERR(req))
64676877 return PTR_ERR(req);
@@ -6494,7 +6904,7 @@ static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, · 26 unmodified lines
64946904 bool found = false;
64956905
64966906 list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) {
6497− found = user_data == req->user_data;
6907+ found = user_data == req->cqe.user_data;
64986908 if (found)
64996909 break;
65006910 }
@@ -6514,7 +6924,8 @@ static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, · 13 unmodified lines
65146924 struct timespec64 *ts, enum hrtimer_mode mode)
65156925 __must_hold(&ctx->timeout_lock)
65166926 {
6517− struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6927+ struct io_cancel_data cd = { .data = user_data, };
6928+ struct io_kiocb *req = io_timeout_extract(ctx, &cd);
65186929 struct io_timeout_data *data;
65196930
65206931 if (IS_ERR(req))
@@ -6534,11 +6945,9 @@ static int io_timeout_remove_prep(struct io_kiocb *req, · 13 unmodified lines
65346945 {
65356946 struct io_timeout_rem *tr = &req->timeout_rem;
65366947
6537− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6538− return -EINVAL;
65396948 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
65406949 return -EINVAL;
6541− if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
6950+ if (sqe->buf_index || sqe->len || sqe->splice_fd_in)
65426951 return -EINVAL;
65436952
65446953 tr->ltimeout = false;
@@ -6579,10 +6988,10 @@ static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) · 34 unmodified lines
65796988 int ret;
65806989
65816990 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
6991+ struct io_cancel_data cd = { .data = tr->addr, };
6992+
65826993 spin_lock(&ctx->completion_lock);
6583− spin_lock_irq(&ctx->timeout_lock);
6584− ret = io_timeout_cancel(ctx, tr->addr);
6585− spin_unlock_irq(&ctx->timeout_lock);
6994+ ret = io_timeout_cancel(ctx, &cd);
65866995 spin_unlock(&ctx->completion_lock);
65876996 } else {
65886997 enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
@@ -6608,10 +7017,7 @@ static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 19 unmodified lines
66087017 unsigned flags;
66097018 u32 off = READ_ONCE(sqe->off);
66107019
6611− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6612− return -EINVAL;
6613− if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
6614− sqe->splice_fd_in)
7020+ if (sqe->buf_index || sqe->len != 1 || sqe->splice_fd_in)
66157021 return -EINVAL;
66167022 if (off && is_timeout_link)
66177023 return -EINVAL;
@@ -6710,30 +7116,42 @@ add: · 92 unmodified lines
67107116 return 0;
67117117 }
67127118
6713−struct io_cancel_data {
6714− struct io_ring_ctx *ctx;
6715− u64 user_data;
6716−};
6717−
67187119 static bool io_cancel_cb(struct io_wq_work *work, void *data)
67197120 {
67207121 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
67217122 struct io_cancel_data *cd = data;
67227123
6723− return req->ctx == cd->ctx && req->user_data == cd->user_data;
7124+ if (req->ctx != cd->ctx)
7125+ return false;
7126+ if (cd->flags & IORING_ASYNC_CANCEL_ANY) {
7127+ ;
7128+ } else if (cd->flags & IORING_ASYNC_CANCEL_FD) {
7129+ if (req->file != cd->file)
7130+ return false;
7131+ } else {
7132+ if (req->cqe.user_data != cd->data)
7133+ return false;
7134+ }
7135+ if (cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY)) {
7136+ if (cd->seq == req->work.cancel_seq)
7137+ return false;
7138+ req->work.cancel_seq = cd->seq;
7139+ }
7140+ return true;
67247141 }
67257142
6726−static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
6727− struct io_ring_ctx *ctx)
7143+static int io_async_cancel_one(struct io_uring_task *tctx,
7144+ struct io_cancel_data *cd)
67287145 {
6729− struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
67307146 enum io_wq_cancel cancel_ret;
67317147 int ret = 0;
7148+ bool all;
67327149
67337150 if (!tctx || !tctx->io_wq)
67347151 return -ENOENT;
67357152
6736− cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
7153+ all = cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY);
7154+ cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, cd, all);
67377155 switch (cancel_ret) {
67387156 case IO_WQ_CANCEL_OK:
67397157 ret = 0;
@@ -6749,14 +7167,14 @@ static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, · 9 unmodified lines
67497167 return ret;
67507168 }
67517169
6752−static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
7170+static int io_try_cancel(struct io_kiocb *req, struct io_cancel_data *cd)
67537171 {
67547172 struct io_ring_ctx *ctx = req->ctx;
67557173 int ret;
67567174
67577175 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
67587176
6759− ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
7177+ ret = io_async_cancel_one(req->task->io_uring, cd);
67607178 /*
67617179 * Fall-through even for -EALREADY, as we may have poll armed
67627180 * that need unarming.
@@ -6765,56 +7183,98 @@ static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) · 2 unmodified lines
67657183 return 0;
67667184
67677185 spin_lock(&ctx->completion_lock);
6768− ret = io_poll_cancel(ctx, sqe_addr, false);
7186+ ret = io_poll_cancel(ctx, cd);
67697187 if (ret != -ENOENT)
67707188 goto out;
6771−
6772− spin_lock_irq(&ctx->timeout_lock);
6773− ret = io_timeout_cancel(ctx, sqe_addr);
6774− spin_unlock_irq(&ctx->timeout_lock);
7189+ if (!(cd->flags & IORING_ASYNC_CANCEL_FD))
7190+ ret = io_timeout_cancel(ctx, cd);
67757191 out:
67767192 spin_unlock(&ctx->completion_lock);
67777193 return ret;
67787194 }
67797195
7196+#define CANCEL_FLAGS (IORING_ASYNC_CANCEL_ALL | IORING_ASYNC_CANCEL_FD | \
7197+ IORING_ASYNC_CANCEL_ANY)
7198+
67807199 static int io_async_cancel_prep(struct io_kiocb *req,
67817200 const struct io_uring_sqe *sqe)
67827201 {
6783− if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6784− return -EINVAL;
6785− if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
7202+ if (unlikely(req->flags & REQ_F_BUFFER_SELECT))
67867203 return -EINVAL;
6787− if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
6788− sqe->splice_fd_in)
7204+ if (sqe->off || sqe->len || sqe->splice_fd_in)
67897205 return -EINVAL;
67907206
67917207 req->cancel.addr = READ_ONCE(sqe->addr);
7208+ req->cancel.flags = READ_ONCE(sqe->cancel_flags);
7209+ if (req->cancel.flags & ~CANCEL_FLAGS)
7210+ return -EINVAL;
7211+ if (req->cancel.flags & IORING_ASYNC_CANCEL_FD) {
7212+ if (req->cancel.flags & IORING_ASYNC_CANCEL_ANY)
7213+ return -EINVAL;
7214+ req->cancel.fd = READ_ONCE(sqe->fd);
7215+ }
7216+
67927217 return 0;
67937218 }
67947219
6795−static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
7220+static int __io_async_cancel(struct io_cancel_data *cd, struct io_kiocb *req,
7221+ unsigned int issue_flags)
67967222 {
6797− struct io_ring_ctx *ctx = req->ctx;
6798− u64 sqe_addr = req->cancel.addr;
6799− bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
7223+ bool all = cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY);
7224+ struct io_ring_ctx *ctx = cd->ctx;
68007225 struct io_tctx_node *node;
6801− int ret;
7226+ int ret, nr = 0;
68027227
6803− ret = io_try_cancel_userdata(req, sqe_addr);
6804− if (ret != -ENOENT)
6805− goto done;
7228+ do {
7229+ ret = io_try_cancel(req, cd);
7230+ if (ret == -ENOENT)
7231+ break;
7232+ if (!all)
7233+ return ret;
7234+ nr++;
7235+ } while (1);
68067236
68077237 /* slow path, try all io-wq's */
6808− io_ring_submit_lock(ctx, needs_lock);
7238+ io_ring_submit_lock(ctx, issue_flags);
68097239 ret = -ENOENT;
68107240 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
68117241 struct io_uring_task *tctx = node->task->io_uring;
68127242
6813− ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
6814− if (ret != -ENOENT)
6815− break;
7243+ ret = io_async_cancel_one(tctx, cd);
7244+ if (ret != -ENOENT) {
7245+ if (!all)
7246+ break;
7247+ nr++;
7248+ }
7249+ }
7250+ io_ring_submit_unlock(ctx, issue_flags);
7251+ return all ? nr : ret;
7252+}
7253+
7254+static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
7255+{
7256+ struct io_cancel_data cd = {
7257+ .ctx = req->ctx,
7258+ .data = req->cancel.addr,
7259+ .flags = req->cancel.flags,
7260+ .seq = atomic_inc_return(&req->ctx->cancel_seq),
7261+ };
7262+ int ret;
7263+
7264+ if (cd.flags & IORING_ASYNC_CANCEL_FD) {
7265+ if (req->flags & REQ_F_FIXED_FILE)
7266+ req->file = io_file_get_fixed(req, req->cancel.fd,
7267+ issue_flags);
7268+ else
7269+ req->file = io_file_get_normal(req, req->cancel.fd);
7270+ if (!req->file) {
7271+ ret = -EBADF;
7272+ goto done;
7273+ }
7274+ cd.file = req->file;
68167275 }
6817− io_ring_submit_unlock(ctx, needs_lock);
7276+
7277+ ret = __io_async_cancel(&cd, req, issue_flags);
68187278 done:
68197279 if (ret < 0)
68207280 req_set_fail(req);
@@ -6827,7 +7287,7 @@ static int io_rsrc_update_prep(struct io_kiocb *req, · 6 unmodified lines
68277287 {
68287288 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
68297289 return -EINVAL;
6830− if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
7290+ if (sqe->rw_flags || sqe->splice_fd_in)
68317291 return -EINVAL;
68327292
68337293 req->rsrc_update.offset = READ_ONCE(sqe->off);
@@ -6841,7 +7301,6 @@ static int io_rsrc_update_prep(struct io_kiocb *req, · 7 unmodified lines
68417301 static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
68427302 {
68437303 struct io_ring_ctx *ctx = req->ctx;
6844− bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
68457304 struct io_uring_rsrc_update2 up;
68467305 int ret;
68477306
@@ -6852,10 +7311,10 @@ static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) · 4 unmodified lines
68527311 up.resv = 0;
68537312 up.resv2 = 0;
68547313
6855− io_ring_submit_lock(ctx, needs_lock);
7314+ io_ring_submit_lock(ctx, issue_flags);
68567315 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
68577316 &up, req->rsrc_update.nr_args);
6858− io_ring_submit_unlock(ctx, needs_lock);
7317+ io_ring_submit_unlock(ctx, issue_flags);
68597318
68607319 if (ret < 0)
68617320 req_set_fail(req);
@@ -6954,7 +7413,7 @@ static int io_req_prep_async(struct io_kiocb *req) · 92 unmodified lines
69547413
69557414 /* assign early for deferred execution for non-fixed file */
69567415 if (def->needs_file && !(req->flags & REQ_F_FIXED_FILE))
6957− req->file = io_file_get_normal(req, req->fd);
7416+ req->file = io_file_get_normal(req, req->cqe.fd);
69587417 if (!def->needs_async_setup)
69597418 return 0;
69607419 if (WARN_ON_ONCE(req_has_async_data(req)))
@@ -6982,9 +7441,10 @@ static int io_req_prep_async(struct io_kiocb *req) · 21 unmodified lines
69827441 static u32 io_get_sequence(struct io_kiocb *req)
69837442 {
69847443 u32 seq = req->ctx->cached_sq_head;
7444+ struct io_kiocb *cur;
69857445
69867446 /* need original cached_sq_head, but it was increased for each req */
6987− io_for_each_link(req, req)
7447+ io_for_each_link(cur, req)
69887448 seq--;
69897449 return seq;
69907450 }
@@ -7027,7 +7487,7 @@ fail: · 36 unmodified lines
70277487 goto queue;
70287488 }
70297489
7030− trace_io_uring_defer(ctx, req, req->user_data, req->opcode);
7490+ trace_io_uring_defer(ctx, req, req->cqe.user_data, req->opcode);
70317491 de->req = req;
70327492 de->seq = seq;
70337493 list_add_tail(&de->list, &ctx->defer_list);
@@ -7111,15 +7571,11 @@ static bool io_assign_file(struct io_kiocb *req, unsigned int issue_flags) · 77 unmodified lines
71117571 return true;
71127572
71137573 if (req->flags & REQ_F_FIXED_FILE)
7114− req->file = io_file_get_fixed(req, req->fd, issue_flags);
7574+ req->file = io_file_get_fixed(req, req->cqe.fd, issue_flags);
71157575 else
7116− req->file = io_file_get_normal(req, req->fd);
7117− if (req->file)
7118− return true;
7576+ req->file = io_file_get_normal(req, req->cqe.fd);
71197577
7120− req_set_fail(req);
7121− req->result = -EBADF;
7122− return false;
7578+ return !!req->file;
71237579 }
71247580
71257581 static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
@@ -7282,7 +7738,6 @@ static void io_wq_submit_work(struct io_wq_work *work) · 156 unmodified lines
72827738 const struct io_op_def *def = &io_op_defs[req->opcode];
72837739 unsigned int issue_flags = IO_URING_F_UNLOCKED;
72847740 bool needs_poll = false;
7285− struct io_kiocb *timeout;
72867741 int ret = 0, err = -ECANCELED;
72877742
72887743 /* one will be dropped by ->io_free_work() after returning to io-wq */
@@ -7291,10 +7746,7 @@ static void io_wq_submit_work(struct io_wq_work *work) · 2 unmodified lines
72917746 else
72927747 req_ref_get(req);
72937748
7294− timeout = io_prep_linked_timeout(req);
7295− if (timeout)
7296− io_queue_linked_timeout(timeout);
7297−
7749+ io_arm_ltimeout(req);
72987750
72997751 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
73007752 if (work->flags & IO_WQ_WORK_CANCEL) {
@@ -7327,6 +7779,8 @@ fail: · 26 unmodified lines
73277779 * wait for request slots on the block side.
73287780 */
73297781 if (!needs_poll) {
7782+ if (!(req->ctx->flags & IORING_SETUP_IOPOLL))
7783+ break;
73307784 cond_resched();
73317785 continue;
73327786 }
@@ -7372,8 +7826,7 @@ static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd, · 39 unmodified lines
73727826 struct file *file = NULL;
73737827 unsigned long file_ptr;
73747828
7375− if (issue_flags & IO_URING_F_UNLOCKED)
7376− mutex_lock(&ctx->uring_lock);
7829+ io_ring_submit_lock(ctx, issue_flags);
73777830
73787831 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
73797832 goto out;
@@ -7384,9 +7837,9 @@ static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd, · 4 unmodified lines
73847837 /* mask in overlapping REQ_F and FFS bits */
73857838 req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT);
73867839 io_req_set_rsrc_node(req, ctx, 0);
7840+ WARN_ON_ONCE(file && !test_bit(fd, ctx->file_table.bitmap));
73877841 out:
7388− if (issue_flags & IO_URING_F_UNLOCKED)
7389− mutex_unlock(&ctx->uring_lock);
7842+ io_ring_submit_unlock(ctx, issue_flags);
73907843 return file;
73917844 }
73927845
@@ -7407,7 +7860,7 @@ static struct file *io_file_get_normal(struct io_kiocb *req, int fd) · 14 unmodified lines
74077860 {
74087861 struct file *file = fget(fd);
74097862
7410− trace_io_uring_file_get(req->ctx, req, req->user_data, fd);
7863+ trace_io_uring_file_get(req->ctx, req, req->cqe.user_data, fd);
74117864
74127865 /* we don't allow fixed io_uring files */
74137866 if (file && file->f_op == &io_uring_fops)
@@ -7421,8 +7874,14 @@ static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) · 7 unmodified lines
74217874 int ret = -ENOENT;
74227875
74237876 if (prev) {
7424− if (!(req->task->flags & PF_EXITING))
7425− ret = io_try_cancel_userdata(req, prev->user_data);
7877+ if (!(req->task->flags & PF_EXITING)) {
7878+ struct io_cancel_data cd = {
7879+ .ctx = req->ctx,
7880+ .data = prev->cqe.user_data,
7881+ };
7882+
7883+ ret = io_try_cancel(req, &cd);
7884+ }
74267885 io_req_complete_post(req, ret ?: -ETIME, 0);
74277886 io_put_req(prev);
74287887 } else {
@@ -7482,10 +7941,17 @@ static void io_queue_linked_timeout(struct io_kiocb *req) · 53 unmodified lines
74827941 io_put_req(req);
74837942 }
74847943
7485−static void io_queue_sqe_arm_apoll(struct io_kiocb *req)
7944+static void io_queue_async(struct io_kiocb *req, int ret)
74867945 __must_hold(&req->ctx->uring_lock)
74877946 {
7488− struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
7947+ struct io_kiocb *linked_timeout;
7948+
7949+ if (ret != -EAGAIN || (req->flags & REQ_F_NOWAIT)) {
7950+ io_req_complete_failed(req, ret);
7951+ return;
7952+ }
7953+
7954+ linked_timeout = io_prep_linked_timeout(req);
74897955
74907956 switch (io_arm_poll_handler(req, 0)) {
74917957 case IO_APOLL_READY:
@@ -7496,7 +7962,7 @@ static void io_queue_sqe_arm_apoll(struct io_kiocb *req) · 4 unmodified lines
74967962 * Queued up for async execution, worker will release
74977963 * submit reference when the iocb is actually submitted.
74987964 */
7499− io_queue_async_work(req, NULL);
7965+ io_queue_iowq(req, NULL);
75007966 break;
75017967 case IO_APOLL_OK:
75027968 break;
@@ -7506,10 +7972,9 @@ static void io_queue_sqe_arm_apoll(struct io_kiocb *req) · 3 unmodified lines
75067972 io_queue_linked_timeout(linked_timeout);
75077973 }
75087974
7509−static inline void __io_queue_sqe(struct io_kiocb *req)
7975+static inline void io_queue_sqe(struct io_kiocb *req)
75107976 __must_hold(&req->ctx->uring_lock)
75117977 {
7512− struct io_kiocb *linked_timeout;
75137978 int ret;
75147979
75157980 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
@@ -7522,22 +7987,23 @@ static inline void __io_queue_sqe(struct io_kiocb *req) · 6 unmodified lines
75227987 * We async punt it if the file wasn't marked NOWAIT, or if the file
75237988 * doesn't support non-blocking read/write attempts
75247989 */
7525− if (likely(!ret)) {
7526− linked_timeout = io_prep_linked_timeout(req);
7527− if (linked_timeout)
7528− io_queue_linked_timeout(linked_timeout);
7529− } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
7530− io_queue_sqe_arm_apoll(req);
7531− } else {
7532− io_req_complete_failed(req, ret);
7533− }
7990+ if (likely(!ret))
7991+ io_arm_ltimeout(req);
7992+ else
7993+ io_queue_async(req, ret);
75347994 }
75357995
75367996 static void io_queue_sqe_fallback(struct io_kiocb *req)
75377997 __must_hold(&req->ctx->uring_lock)
75387998 {
7539− if (req->flags & REQ_F_FAIL) {
7540− io_req_complete_fail_submit(req);
7999+ if (unlikely(req->flags & REQ_F_FAIL)) {
8000+ /*
8001+ * We don't submit, fail them all, for that replace hardlinks
8002+ * with normal links. Extra REQ_F_LINK is tolerated.
8003+ */
8004+ req->flags &= ~REQ_F_HARDLINK;
8005+ req->flags |= REQ_F_LINK;
8006+ io_req_complete_failed(req, req->cqe.res);
75418007 } else if (unlikely(req->ctx->drain_active)) {
75428008 io_drain_req(req);
75438009 } else {
@@ -7546,19 +8012,10 @@ static void io_queue_sqe_fallback(struct io_kiocb *req) · 2 unmodified lines
75468012 if (unlikely(ret))
75478013 io_req_complete_failed(req, ret);
75488014 else
7549− io_queue_async_work(req, NULL);
8015+ io_queue_iowq(req, NULL);
75508016 }
75518017 }
75528018
7553−static inline void io_queue_sqe(struct io_kiocb *req)
7554− __must_hold(&req->ctx->uring_lock)
7555−{
7556− if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL))))
7557− __io_queue_sqe(req);
7558− else
7559− io_queue_sqe_fallback(req);
7560−}
7561−
75628019 /*
75638020 * Check SQE restrictions (opcode and flags).
75648021 *
@@ -7613,9 +8070,9 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, · 48 unmodified lines
76138070 req->opcode = opcode = READ_ONCE(sqe->opcode);
76148071 /* same numerical values with corresponding REQ_F_*, safe to copy */
76158072 req->flags = sqe_flags = READ_ONCE(sqe->flags);
7616− req->user_data = READ_ONCE(sqe->user_data);
8073+ req->cqe.user_data = READ_ONCE(sqe->user_data);
76178074 req->file = NULL;
7618− req->fixed_rsrc_refs = NULL;
8075+ req->rsrc_node = NULL;
76198076 req->task = current;
76208077
76218078 if (unlikely(opcode >= IORING_OP_LAST)) {
@@ -7626,9 +8083,11 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, · 4 unmodified lines
76268083 /* enforce forwards compatibility on users */
76278084 if (sqe_flags & ~SQE_VALID_FLAGS)
76288085 return -EINVAL;
7629− if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
7630− !io_op_defs[opcode].buffer_select)
7631− return -EOPNOTSUPP;
8086+ if (sqe_flags & IOSQE_BUFFER_SELECT) {
8087+ if (!io_op_defs[opcode].buffer_select)
8088+ return -EOPNOTSUPP;
8089+ req->buf_index = READ_ONCE(sqe->buf_group);
8090+ }
76328091 if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS)
76338092 ctx->drain_disabled = true;
76348093 if (sqe_flags & IOSQE_IO_DRAIN) {
@@ -7651,10 +8110,15 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, · 16 unmodified lines
76518110 }
76528111 }
76538112
8113+ if (!io_op_defs[opcode].ioprio && sqe->ioprio)
8114+ return -EINVAL;
8115+ if (!io_op_defs[opcode].iopoll && (ctx->flags & IORING_SETUP_IOPOLL))
8116+ return -EINVAL;
8117+
76548118 if (io_op_defs[opcode].needs_file) {
76558119 struct io_submit_state *state = &ctx->submit_state;
76568120
7657− req->fd = READ_ONCE(sqe->fd);
8121+ req->cqe.fd = READ_ONCE(sqe->fd);
76588122
76598123 /*
76608124 * Plug now if we have more than 2 IO left after this, and the
@@ -7686,7 +8150,44 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, · 25 unmodified lines
76868150 return io_req_prep(req, sqe);
76878151 }
76888152
7689−static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
8153+static __cold int io_submit_fail_init(const struct io_uring_sqe *sqe,
8154+ struct io_kiocb *req, int ret)
8155+{
8156+ struct io_ring_ctx *ctx = req->ctx;
8157+ struct io_submit_link *link = &ctx->submit_state.link;
8158+ struct io_kiocb *head = link->head;
8159+
8160+ trace_io_uring_req_failed(sqe, ctx, req, ret);
8161+
8162+ /*
8163+ * Avoid breaking links in the middle as it renders links with SQPOLL
8164+ * unusable. Instead of failing eagerly, continue assembling the link if
8165+ * applicable and mark the head with REQ_F_FAIL. The link flushing code
8166+ * should find the flag and handle the rest.
8167+ */
8168+ req_fail_link_node(req, ret);
8169+ if (head && !(head->flags & REQ_F_FAIL))
8170+ req_fail_link_node(head, -ECANCELED);
8171+
8172+ if (!(req->flags & IO_REQ_LINK_FLAGS)) {
8173+ if (head) {
8174+ link->last->link = req;
8175+ link->head = NULL;
8176+ req = head;
8177+ }
8178+ io_queue_sqe_fallback(req);
8179+ return ret;
8180+ }
8181+
8182+ if (head)
8183+ link->last->link = req;
8184+ else
8185+ link->head = req;
8186+ link->last = req;
8187+ return 0;
8188+}
8189+
8190+static inline int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
76908191 const struct io_uring_sqe *sqe)
76918192 __must_hold(&ctx->uring_lock)
76928193 {
@@ -7694,35 +8195,11 @@ static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, · 1 unmodified line
76948195 int ret;
76958196
76968197 ret = io_init_req(ctx, req, sqe);
7697− if (unlikely(ret)) {
7698− trace_io_uring_req_failed(sqe, ctx, req, ret);
7699−
7700− /* fail even hard links since we don't submit */
7701− if (link->head) {
7702− /*
7703− * we can judge a link req is failed or cancelled by if
7704− * REQ_F_FAIL is set, but the head is an exception since
7705− * it may be set REQ_F_FAIL because of other req's failure
7706− * so let's leverage req->result to distinguish if a head
7707− * is set REQ_F_FAIL because of its failure or other req's
7708− * failure so that we can set the correct ret code for it.
7709− * init result here to avoid affecting the normal path.
7710− */
7711− if (!(link->head->flags & REQ_F_FAIL))
7712− req_fail_link_node(link->head, -ECANCELED);
7713− } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7714− /*
7715− * the current req is a normal req, we should return
7716− * error and thus break the submittion loop.
7717− */
7718− io_req_complete_failed(req, ret);
7719− return ret;
7720− }
7721− req_fail_link_node(req, ret);
7722− }
8198+ if (unlikely(ret))
8199+ return io_submit_fail_init(sqe, req, ret);
77238200
77248201 /* don't need @sqe from now on */
7725− trace_io_uring_submit_sqe(ctx, req, req->user_data, req->opcode,
8202+ trace_io_uring_submit_sqe(ctx, req, req->cqe.user_data, req->opcode,
77268203 req->flags, true,
77278204 ctx->flags & IORING_SETUP_SQPOLL);
77288205
@@ -7733,29 +8210,32 @@ static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, · 4 unmodified lines
77338210 * submitted sync once the chain is complete. If none of those
77348211 * conditions are true (normal request), then just queue it.
77358212 */
7736− if (link->head) {
7737− struct io_kiocb *head = link->head;
7738−
7739− if (!(req->flags & REQ_F_FAIL)) {
7740− ret = io_req_prep_async(req);
7741− if (unlikely(ret)) {
7742− req_fail_link_node(req, ret);
7743− if (!(head->flags & REQ_F_FAIL))
7744− req_fail_link_node(head, -ECANCELED);
7745− }
7746− }
7747− trace_io_uring_link(ctx, req, head);
8213+ if (unlikely(link->head)) {
8214+ ret = io_req_prep_async(req);
8215+ if (unlikely(ret))
8216+ return io_submit_fail_init(sqe, req, ret);
8217+
8218+ trace_io_uring_link(ctx, req, link->head);
77488219 link->last->link = req;
77498220 link->last = req;
77508221
7751− if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK))
8222+ if (req->flags & IO_REQ_LINK_FLAGS)
77528223 return 0;
7753− /* last request of a link, enqueue the link */
8224+ /* last request of the link, flush it */
8225+ req = link->head;
77548226 link->head = NULL;
7755− req = head;
7756− } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
7757− link->head = req;
7758− link->last = req;
8227+ if (req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL))
8228+ goto fallback;
8229+
8230+ } else if (unlikely(req->flags & (IO_REQ_LINK_FLAGS |
8231+ REQ_F_FORCE_ASYNC | REQ_F_FAIL))) {
8232+ if (req->flags & IO_REQ_LINK_FLAGS) {
8233+ link->head = req;
8234+ link->last = req;
8235+ } else {
8236+fallback:
8237+ io_queue_sqe_fallback(req);
8238+ }
77598239 return 0;
77608240 }
77618241
@@ -7770,8 +8250,8 @@ static void io_submit_state_end(struct io_ring_ctx *ctx) · 8 unmodified lines
77708250 {
77718251 struct io_submit_state *state = &ctx->submit_state;
77728252
7773− if (state->link.head)
7774− io_queue_sqe(state->link.head);
8253+ if (unlikely(state->link.head))
8254+ io_queue_sqe_fallback(state->link.head);
77758255 /* flush only after queuing links as they can generate completions */
77768256 io_submit_flush_completions(ctx);
77778257 if (state->plug_started)
@@ -7839,54 +8319,52 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) · 61 unmodified lines
78398319 __must_hold(&ctx->uring_lock)
78408320 {
78418321 unsigned int entries = io_sqring_entries(ctx);
7842− int submitted = 0;
8322+ unsigned int left;
8323+ int ret;
78438324
78448325 if (unlikely(!entries))
78458326 return 0;
78468327 /* make sure SQ entry isn't read before tail */
7847− nr = min3(nr, ctx->sq_entries, entries);
7848− io_get_task_refs(nr);
8328+ ret = left = min3(nr, ctx->sq_entries, entries);
8329+ io_get_task_refs(left);
8330+ io_submit_state_start(&ctx->submit_state, left);
78498331
7850− io_submit_state_start(&ctx->submit_state, nr);
78518332 do {
78528333 const struct io_uring_sqe *sqe;
78538334 struct io_kiocb *req;
78548335
7855− if (unlikely(!io_alloc_req_refill(ctx))) {
7856− if (!submitted)
7857− submitted = -EAGAIN;
8336+ if (unlikely(!io_alloc_req_refill(ctx)))
78588337 break;
7859− }
78608338 req = io_alloc_req(ctx);
78618339 sqe = io_get_sqe(ctx);
78628340 if (unlikely(!sqe)) {
7863− wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
8341+ io_req_add_to_cache(req, ctx);
78648342 break;
78658343 }
7866− /* will complete beyond this point, count as submitted */
7867− submitted++;
7868− if (io_submit_sqe(ctx, req, sqe)) {
7869− /*
7870− * Continue submitting even for sqe failure if the
7871− * ring was setup with IORING_SETUP_SUBMIT_ALL
7872− */
7873− if (!(ctx->flags & IORING_SETUP_SUBMIT_ALL))
7874− break;
7875− }
7876− } while (submitted < nr);
78778344
7878− if (unlikely(submitted != nr)) {
7879− int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
7880− int unused = nr - ref_used;
8345+ /*
8346+ * Continue submitting even for sqe failure if the
8347+ * ring was setup with IORING_SETUP_SUBMIT_ALL
8348+ */
8349+ if (unlikely(io_submit_sqe(ctx, req, sqe)) &&
8350+ !(ctx->flags & IORING_SETUP_SUBMIT_ALL)) {
8351+ left--;
8352+ break;
8353+ }
8354+ } while (--left);
78818355
7882− current->io_uring->cached_refs += unused;
8356+ if (unlikely(left)) {
8357+ ret -= left;
8358+ /* try again if it submitted nothing and can't allocate a req */
8359+ if (!ret && io_req_cache_empty(ctx))
8360+ ret = -EAGAIN;
8361+ current->io_uring->cached_refs += left;
78838362 }
78848363
78858364 io_submit_state_end(ctx);
78868365 /* Commit SQ ring head once we've consumed and submitted all SQEs */
78878366 io_commit_sqring(ctx);
7888−
7889− return submitted;
8367+ return ret;
78908368 }
78918369
78928370 static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
@@ -7894,23 +8372,6 @@ static inline bool io_sqd_events_pending(struct io_sq_data *sqd) · 1 unmodified line
78948372 return READ_ONCE(sqd->state);
78958373 }
78968374
7897−static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7898−{
7899− /* Tell userspace we may need a wakeup call */
7900− spin_lock(&ctx->completion_lock);
7901− WRITE_ONCE(ctx->rings->sq_flags,
7902− ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
7903− spin_unlock(&ctx->completion_lock);
7904−}
7905−
7906−static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7907−{
7908− spin_lock(&ctx->completion_lock);
7909− WRITE_ONCE(ctx->rings->sq_flags,
7910− ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
7911− spin_unlock(&ctx->completion_lock);
7912−}
7913−
79148375 static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
79158376 {
79168377 unsigned int to_submit;
@@ -8026,8 +8487,8 @@ static int io_sq_thread(void *data) · 109 unmodified lines
80268487 bool needs_sched = true;
80278488
80288489 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
8029− io_ring_set_wakeup_flag(ctx);
8030−
8490+ atomic_or(IORING_SQ_NEED_WAKEUP,
8491+ &ctx->rings->sq_flags);
80318492 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
80328493 !wq_list_empty(&ctx->iopoll_list)) {
80338494 needs_sched = false;
@@ -8038,7 +8499,7 @@ static int io_sq_thread(void *data) · 4 unmodified lines
80388499 * Ensure the store of the wakeup flag is not
80398500 * reordered with the load of the SQ tail
80408501 */
8041− smp_mb();
8502+ smp_mb__after_atomic();
80428503
80438504 if (io_sqring_entries(ctx)) {
80448505 needs_sched = false;
@@ -8052,7 +8513,8 @@ static int io_sq_thread(void *data) · 7 unmodified lines
80528513 mutex_lock(&sqd->lock);
80538514 }
80548515 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
8055− io_ring_clear_wakeup_flag(ctx);
8516+ atomic_andnot(IORING_SQ_NEED_WAKEUP,
8517+ &ctx->rings->sq_flags);
80568518 }
80578519
80588520 finish_wait(&sqd->wait, &wait);
@@ -8062,7 +8524,7 @@ static int io_sq_thread(void *data) · 3 unmodified lines
80628524 io_uring_cancel_generic(true, sqd);
80638525 sqd->thread = NULL;
80648526 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
8065− io_ring_set_wakeup_flag(ctx);
8527+ atomic_or(IORING_SQ_NEED_WAKEUP, &ctx->rings->sq_flags);
80668528 io_run_task_work();
80678529 mutex_unlock(&sqd->lock);
80688530
@@ -8102,7 +8564,8 @@ static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, · 33 unmodified lines
81028564 * Cannot safely flush overflowed CQEs from here, ensure we wake up
81038565 * the task, and the next invocation will do it.
81048566 */
8105− if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
8567+ if (io_should_wake(iowq) ||
8568+ test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &iowq->ctx->check_cq))
81068569 return autoremove_wake_function(curr, mode, wake_flags, key);
81078570 return -1;
81088571 }
@@ -8124,15 +8587,18 @@ static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, · 15 unmodified lines
81248587 ktime_t timeout)
81258588 {
81268589 int ret;
8590+ unsigned long check_cq;
81278591
81288592 /* make sure we run task_work before checking for signals */
81298593 ret = io_run_task_work_sig();
81308594 if (ret || io_should_wake(iowq))
81318595 return ret;
8596+ check_cq = READ_ONCE(ctx->check_cq);
81328597 /* let the caller flush overflows, retry */
8133− if (test_bit(0, &ctx->check_cq_overflow))
8598+ if (check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT))
81348599 return 1;
8135−
8600+ if (unlikely(check_cq & BIT(IO_CHECK_CQ_DROPPED_BIT)))
8601+ return -EBADR;
81368602 if (!schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS))
81378603 return -ETIME;
81388604 return 1;
@@ -8197,10 +8663,10 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, · 58 unmodified lines
81978663 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
81988664 TASK_INTERRUPTIBLE);
81998665 ret = io_cqring_wait_schedule(ctx, &iowq, timeout);
8200− finish_wait(&ctx->cq_wait, &iowq.wq);
82018666 cond_resched();
82028667 } while (ret > 0);
82038668
8669+ finish_wait(&ctx->cq_wait, &iowq.wq);
82048670 restore_saved_sigmask_unless(ret == -EINTR);
82058671
82068672 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
@@ -8438,17 +8904,57 @@ static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) · 231 unmodified lines
84388904 {
84398905 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
84408906 GFP_KERNEL_ACCOUNT);
8441− return !!table->files;
8907+ if (unlikely(!table->files))
8908+ return false;
8909+
8910+ table->bitmap = bitmap_zalloc(nr_files, GFP_KERNEL_ACCOUNT);
8911+ if (unlikely(!table->bitmap)) {
8912+ kvfree(table->files);
8913+ return false;
8914+ }
8915+
8916+ return true;
84428917 }
84438918
84448919 static void io_free_file_tables(struct io_file_table *table)
84458920 {
84468921 kvfree(table->files);
8922+ bitmap_free(table->bitmap);
84478923 table->files = NULL;
8924+ table->bitmap = NULL;
8925+}
8926+
8927+static inline void io_file_bitmap_set(struct io_file_table *table, int bit)
8928+{
8929+ WARN_ON_ONCE(test_bit(bit, table->bitmap));
8930+ __set_bit(bit, table->bitmap);
8931+ if (bit == table->alloc_hint)
8932+ table->alloc_hint++;
8933+}
8934+
8935+static inline void io_file_bitmap_clear(struct io_file_table *table, int bit)
8936+{
8937+ __clear_bit(bit, table->bitmap);
8938+ table->alloc_hint = bit;
84488939 }
84498940
84508941 static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
84518942 {
8943+#if !defined(IO_URING_SCM_ALL)
8944+ int i;
8945+
8946+ for (i = 0; i < ctx->nr_user_files; i++) {
8947+ struct file *file = io_file_from_index(ctx, i);
8948+
8949+ if (!file)
8950+ continue;
8951+ if (io_fixed_file_slot(&ctx->file_table, i)->file_ptr & FFS_SCM)
8952+ continue;
8953+ io_file_bitmap_clear(&ctx->file_table, i);
8954+ fput(file);
8955+ }
8956+#endif
8957+
84528958 #if defined(CONFIG_UNIX)
84538959 if (ctx->ring_sock) {
84548960 struct sock *sock = ctx->ring_sock->sk;
@@ -8457,16 +8963,6 @@ static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) · 2 unmodified lines
84578963 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
84588964 kfree_skb(skb);
84598965 }
8460−#else
8461− int i;
8462−
8463− for (i = 0; i < ctx->nr_user_files; i++) {
8464− struct file *file;
8465−
8466− file = io_file_from_index(ctx, i);
8467− if (file)
8468− fput(file);
8469− }
84708966 #endif
84718967 io_free_file_tables(&ctx->file_table);
84728968 io_rsrc_data_free(ctx->file_data);
@@ -8611,107 +9107,66 @@ static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, · 138 unmodified lines
86119107 return sqd;
86129108 }
86139109
8614−#if defined(CONFIG_UNIX)
86159110 /*
86169111 * Ensure the UNIX gc is aware of our file set, so we are certain that
86179112 * the io_uring can be safely unregistered on process exit, even if we have
8618− * loops in the file referencing.
9113+ * loops in the file referencing. We account only files that can hold other
9114+ * files because otherwise they can't form a loop and so are not interesting
9115+ * for GC.
86199116 */
8620−static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
9117+static int io_scm_file_account(struct io_ring_ctx *ctx, struct file *file)
86219118 {
9119+#if defined(CONFIG_UNIX)
86229120 struct sock *sk = ctx->ring_sock->sk;
9121+ struct sk_buff_head *head = &sk->sk_receive_queue;
86239122 struct scm_fp_list *fpl;
86249123 struct sk_buff *skb;
8625− int i, nr_files;
8626−
8627− fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
8628− if (!fpl)
8629− return -ENOMEM;
8630−
8631− skb = alloc_skb(0, GFP_KERNEL);
8632− if (!skb) {
8633− kfree(fpl);
8634− return -ENOMEM;
8635− }
8636−
8637− skb->sk = sk;
86389124
8639− nr_files = 0;
8640− fpl->user = get_uid(current_user());
8641− for (i = 0; i < nr; i++) {
8642− struct file *file = io_file_from_index(ctx, i + offset);
8643−
8644− if (!file)
8645− continue;
8646− fpl->fp[nr_files] = get_file(file);
8647− unix_inflight(fpl->user, fpl->fp[nr_files]);
8648− nr_files++;
8649− }
8650−
8651− if (nr_files) {
8652− fpl->max = SCM_MAX_FD;
8653− fpl->count = nr_files;
8654− UNIXCB(skb).fp = fpl;
8655− skb->destructor = unix_destruct_scm;
8656− refcount_add(skb->truesize, &sk->sk_wmem_alloc);
8657− skb_queue_head(&sk->sk_receive_queue, skb);
8658−
8659− for (i = 0; i < nr; i++) {
8660− struct file *file = io_file_from_index(ctx, i + offset);
8661−
8662− if (file)
8663− fput(file);
8664− }
8665− } else {
8666− kfree_skb(skb);
8667− free_uid(fpl->user);
8668− kfree(fpl);
8669− }
8670−
8671− return 0;
8672−}
8673−
8674−/*
8675− * If UNIX sockets are enabled, fd passing can cause a reference cycle which
8676− * causes regular reference counting to break down. We rely on the UNIX
8677− * garbage collection to take care of this problem for us.
8678− */
8679−static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8680−{
8681− unsigned left, total;
8682− int ret = 0;
9125+ if (likely(!io_file_need_scm(file)))
9126+ return 0;
86839127
8684− total = 0;
8685− left = ctx->nr_user_files;
8686− while (left) {
8687− unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
9128+ /*
9129+ * See if we can merge this file into an existing skb SCM_RIGHTS
9130+ * file set. If there's no room, fall back to allocating a new skb
9131+ * and filling it in.
9132+ */
9133+ spin_lock_irq(&head->lock);
9134+ skb = skb_peek(head);
9135+ if (skb && UNIXCB(skb).fp->count < SCM_MAX_FD)
9136+ __skb_unlink(skb, head);
9137+ else
9138+ skb = NULL;
9139+ spin_unlock_irq(&head->lock);
86889140
8689− ret = __io_sqe_files_scm(ctx, this_files, total);
8690− if (ret)
8691− break;
8692− left -= this_files;
8693− total += this_files;
8694− }
9141+ if (!skb) {
9142+ fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
9143+ if (!fpl)
9144+ return -ENOMEM;
86959145
8696− if (!ret)
8697− return 0;
9146+ skb = alloc_skb(0, GFP_KERNEL);
9147+ if (!skb) {
9148+ kfree(fpl);
9149+ return -ENOMEM;
9150+ }
86989151
8699− while (total < ctx->nr_user_files) {
8700− struct file *file = io_file_from_index(ctx, total);
9152+ fpl->user = get_uid(current_user());
9153+ fpl->max = SCM_MAX_FD;
9154+ fpl->count = 0;
87019155
8702− if (file)
8703− fput(file);
8704− total++;
9156+ UNIXCB(skb).fp = fpl;
9157+ skb->sk = sk;
9158+ skb->destructor = unix_destruct_scm;
9159+ refcount_add(skb->truesize, &sk->sk_wmem_alloc);
87059160 }
87069161
8707− return ret;
8708−}
8709−#else
8710−static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8711−{
9162+ fpl = UNIXCB(skb).fp;
9163+ fpl->fp[fpl->count++] = get_file(file);
9164+ unix_inflight(fpl->user, file);
9165+ skb_queue_head(head, skb);
9166+ fput(file);
9167+#endif
87129168 return 0;
87139169 }
8714−#endif
87159170
87169171 static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
87179172 {
@@ -8722,6 +9177,11 @@ static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) · 4 unmodified lines
87229177 struct sk_buff *skb;
87239178 int i;
87249179
9180+ if (!io_file_need_scm(file)) {
9181+ fput(file);
9182+ return;
9183+ }
9184+
87259185 __skb_queue_head_init(&list);
87269186
87279187 /*
@@ -8786,15 +9246,17 @@ static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) · 58 unmodified lines
87869246 list_del(&prsrc->list);
87879247
87889248 if (prsrc->tag) {
8789− bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
9249+ if (ctx->flags & IORING_SETUP_IOPOLL)
9250+ mutex_lock(&ctx->uring_lock);
87909251
8791− io_ring_submit_lock(ctx, lock_ring);
87929252 spin_lock(&ctx->completion_lock);
87939253 io_fill_cqe_aux(ctx, prsrc->tag, 0, 0);
87949254 io_commit_cqring(ctx);
87959255 spin_unlock(&ctx->completion_lock);
87969256 io_cqring_ev_posted(ctx);
8797− io_ring_submit_unlock(ctx, lock_ring);
9257+
9258+ if (ctx->flags & IORING_SETUP_IOPOLL)
9259+ mutex_unlock(&ctx->uring_lock);
87989260 }
87999261
88009262 rsrc_data->do_put(ctx, prsrc);
@@ -8848,27 +9310,31 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, · 47 unmodified lines
88489310 if (ret)
88499311 return ret;
88509312
8851− ret = -ENOMEM;
8852− if (!io_alloc_file_tables(&ctx->file_table, nr_args))
8853− goto out_free;
9313+ if (!io_alloc_file_tables(&ctx->file_table, nr_args)) {
9314+ io_rsrc_data_free(ctx->file_data);
9315+ ctx->file_data = NULL;
9316+ return -ENOMEM;
9317+ }
88549318
88559319 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
8856− if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
9320+ struct io_fixed_file *file_slot;
9321+
9322+ if (fds && copy_from_user(&fd, &fds[i], sizeof(fd))) {
88579323 ret = -EFAULT;
8858− goto out_fput;
9324+ goto fail;
88599325 }
88609326 /* allow sparse sets */
8861− if (fd == -1) {
9327+ if (!fds || fd == -1) {
88629328 ret = -EINVAL;
88639329 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
8864− goto out_fput;
9330+ goto fail;
88659331 continue;
88669332 }
88679333
88689334 file = fget(fd);
88699335 ret = -EBADF;
88709336 if (unlikely(!file))
8871− goto out_fput;
9337+ goto fail;
88729338
88739339 /*
88749340 * Don't allow io_uring instances to be registered. If UNIX
@@ -8879,74 +9345,23 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, · 4 unmodified lines
88799345 */
88809346 if (file->f_op == &io_uring_fops) {
88819347 fput(file);
8882− goto out_fput;
9348+ goto fail;
88839349 }
8884− io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
8885− }
8886−
8887− ret = io_sqe_files_scm(ctx);
8888− if (ret) {
8889− __io_sqe_files_unregister(ctx);
8890− return ret;
8891− }
8892−
8893− io_rsrc_node_switch(ctx, NULL);
8894− return ret;
8895−out_fput:
8896− for (i = 0; i < ctx->nr_user_files; i++) {
8897− file = io_file_from_index(ctx, i);
8898− if (file)
9350+ ret = io_scm_file_account(ctx, file);
9351+ if (ret) {
88999352 fput(file);
8900− }
8901− io_free_file_tables(&ctx->file_table);
8902− ctx->nr_user_files = 0;
8903−out_free:
8904− io_rsrc_data_free(ctx->file_data);
8905− ctx->file_data = NULL;
8906− return ret;
8907−}
8908−
8909−static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
8910− int index)
8911−{
8912−#if defined(CONFIG_UNIX)
8913− struct sock *sock = ctx->ring_sock->sk;
8914− struct sk_buff_head *head = &sock->sk_receive_queue;
8915− struct sk_buff *skb;
8916−
8917− /*
8918− * See if we can merge this file into an existing skb SCM_RIGHTS
8919− * file set. If there's no room, fall back to allocating a new skb
8920− * and filling it in.
8921− */
8922− spin_lock_irq(&head->lock);
8923− skb = skb_peek(head);
8924− if (skb) {
8925− struct scm_fp_list *fpl = UNIXCB(skb).fp;
8926−
8927− if (fpl->count < SCM_MAX_FD) {
8928− __skb_unlink(skb, head);
8929− spin_unlock_irq(&head->lock);
8930− fpl->fp[fpl->count] = get_file(file);
8931− unix_inflight(fpl->user, fpl->fp[fpl->count]);
8932− fpl->count++;
8933− spin_lock_irq(&head->lock);
8934− __skb_queue_head(head, skb);
8935− } else {
8936− skb = NULL;
9353+ goto fail;
89379354 }
8938− }
8939− spin_unlock_irq(&head->lock);
8940−
8941− if (skb) {
8942− fput(file);
8943− return 0;
9355+ file_slot = io_fixed_file_slot(&ctx->file_table, i);
9356+ io_fixed_file_set(file_slot, file);
9357+ io_file_bitmap_set(&ctx->file_table, i);
89449358 }
89459359
8946− return __io_sqe_files_scm(ctx, 1, index);
8947−#else
9360+ io_rsrc_node_switch(ctx, NULL);
89489361 return 0;
8949−#endif
9362+fail:
9363+ __io_sqe_files_unregister(ctx);
9364+ return ret;
89509365 }
89519366
89529367 static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
@@ -8970,12 +9385,11 @@ static int io_install_fixed_file(struct io_kiocb *req, struct file *file, · 17 unmodified lines
89709385 unsigned int issue_flags, u32 slot_index)
89719386 {
89729387 struct io_ring_ctx *ctx = req->ctx;
8973− bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
89749388 bool needs_switch = false;
89759389 struct io_fixed_file *file_slot;
89769390 int ret = -EBADF;
89779391
8978− io_ring_submit_lock(ctx, needs_lock);
9392+ io_ring_submit_lock(ctx, issue_flags);
89799393 if (file->f_op == &io_uring_fops)
89809394 goto err;
89819395 ret = -ENXIO;
@@ -9001,22 +9415,20 @@ static int io_install_fixed_file(struct io_kiocb *req, struct file *file, · 19 unmodified lines
90019415 if (ret)
90029416 goto err;
90039417 file_slot->file_ptr = 0;
9418+ io_file_bitmap_clear(&ctx->file_table, slot_index);
90049419 needs_switch = true;
90059420 }
90069421
9007− *io_get_tag_slot(ctx->file_data, slot_index) = 0;
9008− io_fixed_file_set(file_slot, file);
9009− ret = io_sqe_file_register(ctx, file, slot_index);
9010− if (ret) {
9011− file_slot->file_ptr = 0;
9012− goto err;
9422+ ret = io_scm_file_account(ctx, file);
9423+ if (!ret) {
9424+ *io_get_tag_slot(ctx->file_data, slot_index) = 0;
9425+ io_fixed_file_set(file_slot, file);
9426+ io_file_bitmap_set(&ctx->file_table, slot_index);
90139427 }
9014−
9015− ret = 0;
90169428 err:
90179429 if (needs_switch)
90189430 io_rsrc_node_switch(ctx, ctx->file_data);
9019− io_ring_submit_unlock(ctx, needs_lock);
9431+ io_ring_submit_unlock(ctx, issue_flags);
90209432 if (ret)
90219433 fput(file);
90229434 return ret;
@@ -9026,12 +9438,11 @@ static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) · 3 unmodified lines
90269438 {
90279439 unsigned int offset = req->close.file_slot - 1;
90289440 struct io_ring_ctx *ctx = req->ctx;
9029− bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
90309441 struct io_fixed_file *file_slot;
90319442 struct file *file;
90329443 int ret;
90339444
9034− io_ring_submit_lock(ctx, needs_lock);
9445+ io_ring_submit_lock(ctx, issue_flags);
90359446 ret = -ENXIO;
90369447 if (unlikely(!ctx->file_data))
90379448 goto out;
@@ -9054,10 +9465,11 @@ static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) · 16 unmodified lines
90549465 goto out;
90559466
90569467 file_slot->file_ptr = 0;
9468+ io_file_bitmap_clear(&ctx->file_table, offset);
90579469 io_rsrc_node_switch(ctx, ctx->file_data);
90589470 ret = 0;
90599471 out:
9060− io_ring_submit_unlock(ctx, needs_lock);
9472+ io_ring_submit_unlock(ctx, issue_flags);
90619473 return ret;
90629474 }
90639475
@@ -9103,6 +9515,7 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx, · 39 unmodified lines
91039515 if (err)
91049516 break;
91059517 file_slot->file_ptr = 0;
9518+ io_file_bitmap_clear(&ctx->file_table, i);
91069519 needs_switch = true;
91079520 }
91089521 if (fd != -1) {
@@ -9124,14 +9537,14 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx, · 15 unmodified lines
91249537 err = -EBADF;
91259538 break;
91269539 }
9127− *io_get_tag_slot(data, i) = tag;
9128− io_fixed_file_set(file_slot, file);
9129− err = io_sqe_file_register(ctx, file, i);
9540+ err = io_scm_file_account(ctx, file);
91309541 if (err) {
9131− file_slot->file_ptr = 0;
91329542 fput(file);
91339543 break;
91349544 }
9545+ *io_get_tag_slot(data, i) = tag;
9546+ io_fixed_file_set(file_slot, file);
9547+ io_file_bitmap_set(&ctx->file_table, i);
91359548 }
91369549 }
91379550
@@ -9559,30 +9972,18 @@ static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, · 421 unmodified lines
95599972 return ret;
95609973 }
95619974
9562−static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
9563− struct io_mapped_ubuf **pimu,
9564− struct page **last_hpage)
9975+static struct page **io_pin_pages(unsigned long ubuf, unsigned long len,
9976+ int *npages)
95659977 {
9566− struct io_mapped_ubuf *imu = NULL;
9978+ unsigned long start, end, nr_pages;
95679979 struct vm_area_struct **vmas = NULL;
95689980 struct page **pages = NULL;
9569− unsigned long off, start, end, ubuf;
9570− size_t size;
9571− int ret, pret, nr_pages, i;
9572−
9573− if (!iov->iov_base) {
9574− *pimu = ctx->dummy_ubuf;
9575− return 0;
9576− }
9981+ int i, pret, ret = -ENOMEM;
95779982
9578− ubuf = (unsigned long) iov->iov_base;
9579− end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
9983+ end = (ubuf + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
95809984 start = ubuf >> PAGE_SHIFT;
95819985 nr_pages = end - start;
95829986
9583− *pimu = NULL;
9584− ret = -ENOMEM;
9585−
95869987 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
95879988 if (!pages)
95889989 goto done;
@@ -9592,10 +9993,6 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, · 3 unmodified lines
95929993 if (!vmas)
95939994 goto done;
95949995
9595− imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
9596− if (!imu)
9597− goto done;
9598−
95999996 ret = 0;
96009997 mmap_read_lock(current->mm);
96019998 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
@@ -9613,6 +10010,7 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, · 11 unmodified lines
961310010 break;
961410011 }
961510012 }
10013+ *npages = nr_pages;
961610014 } else {
961710015 ret = pret < 0 ? pret : -EFAULT;
961810016 }
@@ -9626,14 +10024,53 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, · 7 unmodified lines
962610024 unpin_user_pages(pages, pret);
962710025 goto done;
962810026 }
10027+ ret = 0;
10028+done:
10029+ kvfree(vmas);
10030+ if (ret < 0) {
10031+ kvfree(pages);
10032+ pages = ERR_PTR(ret);
10033+ }
10034+ return pages;
10035+}
10036+
10037+static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
10038+ struct io_mapped_ubuf **pimu,
10039+ struct page **last_hpage)
10040+{
10041+ struct io_mapped_ubuf *imu = NULL;
10042+ struct page **pages = NULL;
10043+ unsigned long off;
10044+ size_t size;
10045+ int ret, nr_pages, i;
10046+
10047+ if (!iov->iov_base) {
10048+ *pimu = ctx->dummy_ubuf;
10049+ return 0;
10050+ }
10051+
10052+ *pimu = NULL;
10053+ ret = -ENOMEM;
10054+
10055+ pages = io_pin_pages((unsigned long) iov->iov_base, iov->iov_len,
10056+ &nr_pages);
10057+ if (IS_ERR(pages)) {
10058+ ret = PTR_ERR(pages);
10059+ pages = NULL;
10060+ goto done;
10061+ }
10062+
10063+ imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
10064+ if (!imu)
10065+ goto done;
962910066
9630− ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
10067+ ret = io_buffer_account_pin(ctx, pages, nr_pages, imu, last_hpage);
963110068 if (ret) {
9632− unpin_user_pages(pages, pret);
10069+ unpin_user_pages(pages, nr_pages);
963310070 goto done;
963410071 }
963510072
9636− off = ubuf & ~PAGE_MASK;
10073+ off = (unsigned long) iov->iov_base & ~PAGE_MASK;
963710074 size = iov->iov_len;
963810075 for (i = 0; i < nr_pages; i++) {
963910076 size_t vec_len;
@@ -9646,8 +10083,8 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, · 6 unmodified lines
964610083 size -= vec_len;
964710084 }
964810085 /* store original address for later verification */
9649− imu->ubuf = ubuf;
9650− imu->ubuf_end = ubuf + iov->iov_len;
10086+ imu->ubuf = (unsigned long) iov->iov_base;
10087+ imu->ubuf_end = imu->ubuf + iov->iov_len;
965110088 imu->nr_bvecs = nr_pages;
965210089 *pimu = imu;
965310090 ret = 0;
@@ -9655,7 +10092,6 @@ done: · 1 unmodified line
965510092 if (ret)
965610093 kvfree(imu);
965710094 kvfree(pages);
9658− kvfree(vmas);
965910095 return ret;
966010096 }
966110097
@@ -9714,12 +10150,17 @@ static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, · 52 unmodified lines
971410150 }
971510151
971610152 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
9717− ret = io_copy_iov(ctx, &iov, arg, i);
9718− if (ret)
9719− break;
9720− ret = io_buffer_validate(&iov);
9721− if (ret)
9722− break;
10153+ if (arg) {
10154+ ret = io_copy_iov(ctx, &iov, arg, i);
10155+ if (ret)
10156+ break;
10157+ ret = io_buffer_validate(&iov);
10158+ if (ret)
10159+ break;
10160+ } else {
10161+ memset(&iov, 0, sizeof(iov));
10162+ }
10163+
972310164 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
972410165 ret = -EINVAL;
972510166 break;
@@ -9858,19 +10299,19 @@ static int io_eventfd_unregister(struct io_ring_ctx *ctx) · 132 unmodified lines
985810299
985910300 static void io_destroy_buffers(struct io_ring_ctx *ctx)
986010301 {
10302+ struct io_buffer_list *bl;
10303+ unsigned long index;
986110304 int i;
986210305
9863− for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) {
9864− struct list_head *list = &ctx->io_buffers[i];
9865−
9866− while (!list_empty(list)) {
9867− struct io_buffer_list *bl;
10306+ for (i = 0; i < BGID_ARRAY; i++) {
10307+ if (!ctx->io_bl)
10308+ break;
10309+ __io_remove_buffers(ctx, &ctx->io_bl[i], -1U);
10310+ }
986810311
9869− bl = list_first_entry(list, struct io_buffer_list, list);
9870− __io_remove_buffers(ctx, bl, -1U);
9871− list_del(&bl->list);
9872− kfree(bl);
9873− }
10312+ xa_for_each(&ctx->io_bl_xa, index, bl) {
10313+ xa_erase(&ctx->io_bl_xa, bl->bgid);
10314+ __io_remove_buffers(ctx, bl, -1U);
987410315 }
987510316
987610317 while (!list_empty(&ctx->io_buffers_pages)) {
@@ -9890,7 +10331,7 @@ static void io_req_caches_free(struct io_ring_ctx *ctx) · 13 unmodified lines
989010331 mutex_lock(&ctx->uring_lock);
989110332 io_flush_cached_locked_reqs(ctx, state);
989210333
9893− while (state->free_list.next) {
10334+ while (!io_req_cache_empty(ctx)) {
989410335 struct io_wq_work_node *node;
989510336 struct io_kiocb *req;
989610337
@@ -9979,7 +10420,8 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) · 82 unmodified lines
997910420 io_wq_put_hash(ctx->hash_map);
998010421 kfree(ctx->cancel_hash);
998110422 kfree(ctx->dummy_ubuf);
9982− kfree(ctx->io_buffers);
10423+ kfree(ctx->io_bl);
10424+ xa_destroy(&ctx->io_bl_xa);
998310425 kfree(ctx);
998410426 }
998510427
@@ -10010,7 +10452,8 @@ static __poll_t io_uring_poll(struct file *file, poll_table *wait) · 24 unmodified lines
1001010452 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
1001110453 * pushs them to do the flush.
1001210454 */
10013− if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
10455+ if (io_cqring_events(ctx) ||
10456+ test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq))
1001410457 mask |= EPOLLIN | EPOLLRDNORM;
1001510458
1001610459 return mask;
@@ -10142,8 +10585,7 @@ static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, · 125 unmodified lines
1014210585 }
1014310586 }
1014410587 spin_unlock_irq(&ctx->timeout_lock);
10145− if (canceled != 0)
10146− io_commit_cqring(ctx);
10588+ io_commit_cqring(ctx);
1014710589 spin_unlock(&ctx->completion_lock);
1014810590 if (canceled != 0)
1014910591 io_cqring_ev_posted(ctx);
@@ -10163,11 +10605,13 @@ static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) · 13 unmodified lines
1016310605 io_unregister_personality(ctx, index);
1016410606 mutex_unlock(&ctx->uring_lock);
1016510607
10166− io_kill_timeouts(ctx, NULL, true);
10167− io_poll_remove_all(ctx, NULL, true);
10168−
10169− /* if we failed setting up the ctx, we might not have any rings */
10170− io_iopoll_try_reap_events(ctx);
10608+ /* failed during ring init, it couldn't have issued any requests */
10609+ if (ctx->rings) {
10610+ io_kill_timeouts(ctx, NULL, true);
10611+ io_poll_remove_all(ctx, NULL, true);
10612+ /* if we failed setting up the ctx, we might not have any rings */
10613+ io_iopoll_try_reap_events(ctx);
10614+ }
1017110615
1017210616 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
1017310617 /*
@@ -10259,6 +10703,10 @@ static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, · 85 unmodified lines
1025910703 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
1026010704 struct io_uring_task *tctx = task ? task->io_uring : NULL;
1026110705
10706+ /* failed during ring init, it couldn't have issued any requests */
10707+ if (!ctx->rings)
10708+ return;
10709+
1026210710 while (1) {
1026310711 enum io_wq_cancel cret;
1026410712 bool ret = false;
@@ -10704,6 +11152,19 @@ static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) · 439 unmodified lines
1070411152 return 0;
1070511153 }
1070611154
11155+static int io_validate_ext_arg(unsigned flags, const void __user *argp, size_t argsz)
11156+{
11157+ if (flags & IORING_ENTER_EXT_ARG) {
11158+ struct io_uring_getevents_arg arg;
11159+
11160+ if (argsz != sizeof(arg))
11161+ return -EINVAL;
11162+ if (copy_from_user(&arg, argp, sizeof(arg)))
11163+ return -EFAULT;
11164+ }
11165+ return 0;
11166+}
11167+
1070711168 static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
1070811169 struct __kernel_timespec __user **ts,
1070911170 const sigset_t __user **sig)
@@ -10741,7 +11202,6 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, · 31 unmodified lines
1074111202 size_t, argsz)
1074211203 {
1074311204 struct io_ring_ctx *ctx;
10744− int submitted = 0;
1074511205 struct fd f;
1074611206 long ret;
1074711207
@@ -10804,39 +11264,64 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, · 56 unmodified lines
1080411264 if (ret)
1080511265 goto out;
1080611266 }
10807− submitted = to_submit;
11267+ ret = to_submit;
1080811268 } else if (to_submit) {
1080911269 ret = io_uring_add_tctx_node(ctx);
1081011270 if (unlikely(ret))
1081111271 goto out;
10812− mutex_lock(&ctx->uring_lock);
10813− submitted = io_submit_sqes(ctx, to_submit);
10814− mutex_unlock(&ctx->uring_lock);
1081511272
10816− if (submitted != to_submit)
11273+ mutex_lock(&ctx->uring_lock);
11274+ ret = io_submit_sqes(ctx, to_submit);
11275+ if (ret != to_submit) {
11276+ mutex_unlock(&ctx->uring_lock);
1081711277 goto out;
11278+ }
11279+ if ((flags & IORING_ENTER_GETEVENTS) && ctx->syscall_iopoll)
11280+ goto iopoll_locked;
11281+ mutex_unlock(&ctx->uring_lock);
1081811282 }
1081911283 if (flags & IORING_ENTER_GETEVENTS) {
10820− const sigset_t __user *sig;
10821− struct __kernel_timespec __user *ts;
10822−
10823− ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
10824− if (unlikely(ret))
10825− goto out;
11284+ int ret2;
11285+ if (ctx->syscall_iopoll) {
11286+ /*
11287+ * We disallow the app entering submit/complete with
11288+ * polling, but we still need to lock the ring to
11289+ * prevent racing with polled issue that got punted to
11290+ * a workqueue.
11291+ */
11292+ mutex_lock(&ctx->uring_lock);
11293+iopoll_locked:
11294+ ret2 = io_validate_ext_arg(flags, argp, argsz);
11295+ if (likely(!ret2)) {
11296+ min_complete = min(min_complete,
11297+ ctx->cq_entries);
11298+ ret2 = io_iopoll_check(ctx, min_complete);
11299+ }
11300+ mutex_unlock(&ctx->uring_lock);
11301+ } else {
11302+ const sigset_t __user *sig;
11303+ struct __kernel_timespec __user *ts;
11304+
11305+ ret2 = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
11306+ if (likely(!ret2)) {
11307+ min_complete = min(min_complete,
11308+ ctx->cq_entries);
11309+ ret2 = io_cqring_wait(ctx, min_complete, sig,
11310+ argsz, ts);
11311+ }
11312+ }
1082611313
10827− min_complete = min(min_complete, ctx->cq_entries);
11314+ if (!ret) {
11315+ ret = ret2;
1082811316
10829− /*
10830− * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
10831− * space applications don't need to do io completion events
10832− * polling again, they can rely on io_sq_thread to do polling
10833− * work, which can reduce cpu usage and uring_lock contention.
10834− */
10835− if (ctx->flags & IORING_SETUP_IOPOLL &&
10836− !(ctx->flags & IORING_SETUP_SQPOLL)) {
10837− ret = io_iopoll_check(ctx, min_complete);
10838− } else {
10839− ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
11317+ /*
11318+ * EBADR indicates that one or more CQE were dropped.
11319+ * Once the user has been informed we can clear the bit
11320+ * as they are obviously ok with those drops.
11321+ */
11322+ if (unlikely(ret2 == -EBADR))
11323+ clear_bit(IO_CHECK_CQ_DROPPED_BIT,
11324+ &ctx->check_cq);
1084011325 }
1084111326 }
1084211327
@@ -10845,7 +11330,7 @@ out: · 2 unmodified lines
1084511330 out_fput:
1084611331 if (!(flags & IORING_ENTER_REGISTERED_RING))
1084711332 fdput(f);
10848− return submitted ? submitted : ret;
11333+ return ret;
1084911334 }
1085011335
1085111336 #ifdef CONFIG_PROC_FS
@@ -11162,10 +11647,40 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, · 310 unmodified lines
1116211647 ctx = io_ring_ctx_alloc(p);
1116311648 if (!ctx)
1116411649 return -ENOMEM;
11650+
11651+ /*
11652+ * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
11653+ * space applications don't need to do io completion events
11654+ * polling again, they can rely on io_sq_thread to do polling
11655+ * work, which can reduce cpu usage and uring_lock contention.
11656+ */
11657+ if (ctx->flags & IORING_SETUP_IOPOLL &&
11658+ !(ctx->flags & IORING_SETUP_SQPOLL))
11659+ ctx->syscall_iopoll = 1;
11660+
1116511661 ctx->compat = in_compat_syscall();
1116611662 if (!capable(CAP_IPC_LOCK))
1116711663 ctx->user = get_uid(current_user());
1116811664
11665+ /*
11666+ * For SQPOLL, we just need a wakeup, always. For !SQPOLL, if
11667+ * COOP_TASKRUN is set, then IPIs are never needed by the app.
11668+ */
11669+ ret = -EINVAL;
11670+ if (ctx->flags & IORING_SETUP_SQPOLL) {
11671+ /* IPI related flags don't make sense with SQPOLL */
11672+ if (ctx->flags & (IORING_SETUP_COOP_TASKRUN |
11673+ IORING_SETUP_TASKRUN_FLAG))
11674+ goto err;
11675+ ctx->notify_method = TWA_SIGNAL_NO_IPI;
11676+ } else if (ctx->flags & IORING_SETUP_COOP_TASKRUN) {
11677+ ctx->notify_method = TWA_SIGNAL_NO_IPI;
11678+ } else {
11679+ if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
11680+ goto err;
11681+ ctx->notify_method = TWA_SIGNAL;
11682+ }
11683+
1116911684 /*
1117011685 * This is just grabbed for accounting purposes. When a process exits,
1117111686 * the mm is exited and dropped before the files, hence we need to hang
@@ -11263,10 +11778,11 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params) · 91 unmodified lines
1126311778 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
1126411779 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
1126511780 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
11266− IORING_SETUP_R_DISABLED | IORING_SETUP_SUBMIT_ALL))
11781+ IORING_SETUP_R_DISABLED | IORING_SETUP_SUBMIT_ALL |
11782+ IORING_SETUP_COOP_TASKRUN | IORING_SETUP_TASKRUN_FLAG))
1126711783 return -EINVAL;
1126811784
11269− return io_uring_create(entries, &p, params);
11785+ return io_uring_create(entries, &p, params);
1127011786 }
1127111787
1127211788 SYSCALL_DEFINE2(io_uring_setup, u32, entries,
@@ -11479,14 +11995,20 @@ static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, · 206 unmodified lines
1147911995 memset(&rr, 0, sizeof(rr));
1148011996 if (copy_from_user(&rr, arg, size))
1148111997 return -EFAULT;
11482− if (!rr.nr || rr.resv || rr.resv2)
11998+ if (!rr.nr || rr.resv2)
11999+ return -EINVAL;
12000+ if (rr.flags & ~IORING_RSRC_REGISTER_SPARSE)
1148312001 return -EINVAL;
1148412002
1148512003 switch (type) {
1148612004 case IORING_RSRC_FILE:
12005+ if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
12006+ break;
1148712007 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
1148812008 rr.nr, u64_to_user_ptr(rr.tags));
1148912009 case IORING_RSRC_BUFFER:
12010+ if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
12011+ break;
1149012012 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
1149112013 rr.nr, u64_to_user_ptr(rr.tags));
1149212014 }
@@ -11621,6 +12143,85 @@ err: · 128 unmodified lines
1162112143 return ret;
1162212144 }
1162312145
12146+static int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
12147+{
12148+ struct io_uring_buf_ring *br;
12149+ struct io_uring_buf_reg reg;
12150+ struct io_buffer_list *bl;
12151+ struct page **pages;
12152+ int nr_pages;
12153+
12154+ if (copy_from_user(&reg, arg, sizeof(reg)))
12155+ return -EFAULT;
12156+
12157+ if (reg.pad || reg.resv[0] || reg.resv[1] || reg.resv[2])
12158+ return -EINVAL;
12159+ if (!reg.ring_addr)
12160+ return -EFAULT;

126 more lines aren’t shown here.

include/linux/sched/signal.h+11−2
@@ -355,14 +355,23 @@ static inline void clear_notify_signal(void) · 354 unmodified lines
355355 smp_mb__after_atomic();
356356 }
357357
358+/*
359+ * Returns 'true' if kick_process() is needed to force a transition from
360+ * user -> kernel to guarantee expedient run of TWA_SIGNAL based task_work.
361+ */
362+static inline bool __set_notify_signal(struct task_struct *task)
363+{
364+ return !test_and_set_tsk_thread_flag(task, TIF_NOTIFY_SIGNAL) &&
365+ !wake_up_state(task, TASK_INTERRUPTIBLE);
366+}
367+
358368 /*
359369 * Called to break out of interruptible wait loops, and enter the
360370 * exit_to_user_mode_loop().
361371 */
362372 static inline void set_notify_signal(struct task_struct *task)
363373 {
364− if (!test_and_set_tsk_thread_flag(task, TIF_NOTIFY_SIGNAL) &&
365− !wake_up_state(task, TASK_INTERRUPTIBLE))
374+ if (__set_notify_signal(task))
366375 kick_process(task);
367376 }
368377
include/linux/task_work.h+1−0
@@ -17,6 +17,7 @@ enum task_work_notify_mode { · 16 unmodified lines
1717 TWA_NONE,
1818 TWA_RESUME,
1919 TWA_SIGNAL,
20+ TWA_SIGNAL_NO_IPI,
2021 };
2122
2223 static inline bool task_work_pending(struct task_struct *task)
include/trace/events/io_uring.h+42−2
@@ -520,7 +520,7 @@ TRACE_EVENT(io_uring_req_failed, · 519 unmodified lines
520520 __entry->off = sqe->off;
521521 __entry->addr = sqe->addr;
522522 __entry->len = sqe->len;
523− __entry->op_flags = sqe->rw_flags;
523+ __entry->op_flags = sqe->poll32_events;
524524 __entry->buf_index = sqe->buf_index;
525525 __entry->personality = sqe->personality;
526526 __entry->file_index = sqe->file_index;
@@ -530,7 +530,7 @@ TRACE_EVENT(io_uring_req_failed, · 3 unmodified lines
530530 ),
531531
532532 TP_printk("ring %p, req %p, user_data 0x%llx, "
533− "op %d, flags 0x%x, prio=%d, off=%llu, addr=%llu, "
533+ "op %d, flags 0x%x, prio=%d, off=%llu, addr=%llu, "
534534 "len=%u, rw_flags=0x%x, buf_index=%d, "
535535 "personality=%d, file_index=%d, pad=0x%llx/%llx, error=%d",
536536 __entry->ctx, __entry->req, __entry->user_data,
@@ -543,6 +543,46 @@ TRACE_EVENT(io_uring_req_failed, · 6 unmodified lines
543543 (unsigned long long) __entry->pad2, __entry->error)
544544 );
545545
546+
547+/*
548+ * io_uring_cqe_overflow - a CQE overflowed
549+ *
550+ * @ctx: pointer to a ring context structure
551+ * @user_data: user data associated with the request
552+ * @res: CQE result
553+ * @cflags: CQE flags
554+ * @ocqe: pointer to the overflow cqe (if available)
555+ *
556+ */
557+TRACE_EVENT(io_uring_cqe_overflow,
558+
559+ TP_PROTO(void *ctx, unsigned long long user_data, s32 res, u32 cflags,
560+ void *ocqe),
561+
562+ TP_ARGS(ctx, user_data, res, cflags, ocqe),
563+
564+ TP_STRUCT__entry (
565+ __field( void *, ctx )
566+ __field( unsigned long long, user_data )
567+ __field( s32, res )
568+ __field( u32, cflags )
569+ __field( void *, ocqe )
570+ ),
571+
572+ TP_fast_assign(
573+ __entry->ctx = ctx;
574+ __entry->user_data = user_data;
575+ __entry->res = res;
576+ __entry->cflags = cflags;
577+ __entry->ocqe = ocqe;
578+ ),
579+
580+ TP_printk("ring %p, user_data 0x%llx, res %d, flags %x, "
581+ "overflow_cqe %p",
582+ __entry->ctx, __entry->user_data, __entry->res,
583+ __entry->cflags, __entry->ocqe)
584+);
585+
546586 #endif /* _TRACE_IO_URING_H */
547587
548588 /* This part must be outside protection */
include/uapi/linux/io_uring.h+94−1
@@ -63,6 +63,15 @@ struct io_uring_sqe { · 62 unmodified lines
6363 __u64 __pad2[2];
6464 };
6565
66+/*
67+ * If sqe->file_index is set to this for opcodes that instantiate a new
68+ * direct descriptor (like openat/openat2/accept), then io_uring will allocate
69+ * an available direct descriptor instead of having the application pass one
70+ * in. The picked direct descriptor will be returned in cqe->res, or -ENFILE
71+ * if the space is full.
72+ */
73+#define IORING_FILE_INDEX_ALLOC (~0U)
74+
6675 enum {
6776 IOSQE_FIXED_FILE_BIT,
6877 IOSQE_IO_DRAIN_BIT,
@@ -102,6 +111,20 @@ enum { · 33 unmodified lines
102111 #define IORING_SETUP_ATTACH_WQ (1U << 5) /* attach to existing wq */
103112 #define IORING_SETUP_R_DISABLED (1U << 6) /* start with ring disabled */
104113 #define IORING_SETUP_SUBMIT_ALL (1U << 7) /* continue submit on error */
114+/*
115+ * Cooperative task running. When requests complete, they often require
116+ * forcing the submitter to transition to the kernel to complete. If this
117+ * flag is set, work will be done when the task transitions anyway, rather
118+ * than force an inter-processor interrupt reschedule. This avoids interrupting
119+ * a task running in userspace, and saves an IPI.
120+ */
121+#define IORING_SETUP_COOP_TASKRUN (1U << 8)
122+/*
123+ * If COOP_TASKRUN is set, get notified if task work is available for
124+ * running and a kernel transition would be needed to run it. This sets
125+ * IORING_SQ_TASKRUN in the sq ring flags. Not valid with COOP_TASKRUN.
126+ */
127+#define IORING_SETUP_TASKRUN_FLAG (1U << 9)
105128
106129 enum {
107130 IORING_OP_NOP,
@@ -187,6 +210,33 @@ enum { · 79 unmodified lines
187210 #define IORING_POLL_UPDATE_EVENTS (1U << 1)
188211 #define IORING_POLL_UPDATE_USER_DATA (1U << 2)
189212
213+/*
214+ * ASYNC_CANCEL flags.
215+ *
216+ * IORING_ASYNC_CANCEL_ALL Cancel all requests that match the given key
217+ * IORING_ASYNC_CANCEL_FD Key off 'fd' for cancelation rather than the
218+ * request 'user_data'
219+ * IORING_ASYNC_CANCEL_ANY Match any request
220+ */
221+#define IORING_ASYNC_CANCEL_ALL (1U << 0)
222+#define IORING_ASYNC_CANCEL_FD (1U << 1)
223+#define IORING_ASYNC_CANCEL_ANY (1U << 2)
224+
225+/*
226+ * send/sendmsg and recv/recvmsg flags (sqe->addr2)
227+ *
228+ * IORING_RECVSEND_POLL_FIRST If set, instead of first attempting to send
229+ * or receive and arm poll if that yields an
230+ * -EAGAIN result, arm poll upfront and skip
231+ * the initial transfer attempt.
232+ */
233+#define IORING_RECVSEND_POLL_FIRST (1U << 0)
234+
235+/*
236+ * accept flags stored in sqe->ioprio
237+ */
238+#define IORING_ACCEPT_MULTISHOT (1U << 0)
239+
190240 /*
191241 * IO completion data structure (Completion Queue Entry)
192242 */
@@ -236,6 +286,7 @@ struct io_sqring_offsets { · 43 unmodified lines
236286 */
237287 #define IORING_SQ_NEED_WAKEUP (1U << 0) /* needs io_uring_enter wakeup */
238288 #define IORING_SQ_CQ_OVERFLOW (1U << 1) /* CQ ring is overflown */
289+#define IORING_SQ_TASKRUN (1U << 2) /* task should enter the kernel */
239290
240291 struct io_cqring_offsets {
241292 __u32 head;
@@ -333,6 +384,10 @@ enum { · 91 unmodified lines
333384 IORING_REGISTER_RING_FDS = 20,
334385 IORING_UNREGISTER_RING_FDS = 21,
335386
387+ /* register ring based provide buffer group */
388+ IORING_REGISTER_PBUF_RING = 22,
389+ IORING_UNREGISTER_PBUF_RING = 23,
390+
336391 /* this goes last */
337392 IORING_REGISTER_LAST
338393 };
@@ -350,9 +405,15 @@ struct io_uring_files_update { · 11 unmodified lines
350405 __aligned_u64 /* __s32 * */ fds;
351406 };
352407
408+/*
409+ * Register a fully sparse file space, rather than pass in an array of all
410+ * -1 file descriptors.
411+ */
412+#define IORING_RSRC_REGISTER_SPARSE (1U << 0)
413+
353414 struct io_uring_rsrc_register {
354415 __u32 nr;
355− __u32 resv;
416+ __u32 flags;
356417 __u64 resv2;
357418 __aligned_u64 data;
358419 __aligned_u64 tags;
@@ -404,6 +465,38 @@ struct io_uring_restriction { · 45 unmodified lines
404465 __u32 resv2[3];
405466 };
406467
468+struct io_uring_buf {
469+ __u64 addr;
470+ __u32 len;
471+ __u16 bid;
472+ __u16 resv;
473+};
474+
475+struct io_uring_buf_ring {
476+ union {
477+ /*
478+ * To avoid spilling into more pages than we need to, the
479+ * ring tail is overlaid with the io_uring_buf->resv field.
480+ */
481+ struct {
482+ __u64 resv1;
483+ __u32 resv2;
484+ __u16 resv3;
485+ __u16 tail;
486+ };
487+ struct io_uring_buf bufs[0];
488+ };
489+};
490+
491+/* argument for IORING_(UN)REGISTER_PBUF_RING */
492+struct io_uring_buf_reg {
493+ __u64 ring_addr;
494+ __u32 ring_entries;
495+ __u16 bgid;
496+ __u16 pad;
497+ __u64 resv[3];
498+};
499+
407500 /*
408501 * io_uring_restriction->opcode values
409502 */
kernel/task_work.c+19−6
@@ -12,12 +12,22 @@ static struct callback_head work_exited; /* all we need is ->next == NULL */ · 11 unmodified lines
1212 * @notify: how to notify the targeted task
1313 *
1414 * Queue @work for task_work_run() below and notify the @task if @notify
15− * is @TWA_RESUME or @TWA_SIGNAL. @TWA_SIGNAL works like signals, in that the
16− * it will interrupt the targeted task and run the task_work. @TWA_RESUME
17− * work is run only when the task exits the kernel and returns to user mode,
18− * or before entering guest mode. Fails if the @task is exiting/exited and thus
19− * it can't process this @work. Otherwise @work->func() will be called when the
20− * @task goes through one of the aforementioned transitions, or exits.
15+ * is @TWA_RESUME, @TWA_SIGNAL, or @TWA_SIGNAL_NO_IPI.
16+ *
17+ * @TWA_SIGNAL works like signals, in that the it will interrupt the targeted
18+ * task and run the task_work, regardless of whether the task is currently
19+ * running in the kernel or userspace.
20+ * @TWA_SIGNAL_NO_IPI works like @TWA_SIGNAL, except it doesn't send a
21+ * reschedule IPI to force the targeted task to reschedule and run task_work.
22+ * This can be advantageous if there's no strict requirement that the
23+ * task_work be run as soon as possible, just whenever the task enters the
24+ * kernel anyway.
25+ * @TWA_RESUME work is run only when the task exits the kernel and returns to
26+ * user mode, or before entering guest mode.
27+ *
28+ * Fails if the @task is exiting/exited and thus it can't process this @work.
29+ * Otherwise @work->func() will be called when the @task goes through one of
30+ * the aforementioned transitions, or exits.
2131 *
2232 * If the targeted task is exiting, then an error is returned and the work item
2333 * is not queued. It's up to the caller to arrange for an alternative mechanism
@@ -53,6 +63,9 @@ int task_work_add(struct task_struct *task, struct callback_head *work, · 29 unmodified lines
5363 case TWA_SIGNAL:
5464 set_notify_signal(task);
5565 break;
66+ case TWA_SIGNAL_NO_IPI:
67+ __set_notify_signal(task);
68+ break;
5669 default:
5770 WARN_ON_ONCE(1);
5871 break;