| @@ -63,6 +63,7 @@ · 62 unmodified lines |
| 63 | 63 | #include <net/sock.h> |
| 64 | 64 | #include <net/af_unix.h> |
| 65 | 65 | #include <net/scm.h> |
| 66 | +#include <net/busy_poll.h> |
| 66 | 67 | #include <linux/anon_inodes.h> |
| 67 | 68 | #include <linux/sched/mm.h> |
| 68 | 69 | #include <linux/uaccess.h> |
| @@ -263,11 +264,18 @@ struct io_rsrc_data { · 194 unmodified lines |
| 263 | 264 | bool quiesce; |
| 264 | 265 | }; |
| 265 | 266 | |
| 267 | +struct io_buffer_list { |
| 268 | + struct list_head list; |
| 269 | + struct list_head buf_list; |
| 270 | + __u16 bgid; |
| 271 | +}; |
| 272 | + |
| 266 | 273 | struct io_buffer { |
| 267 | 274 | struct list_head list; |
| 268 | 275 | __u64 addr; |
| 269 | 276 | __u32 len; |
| 270 | 277 | __u16 bid; |
| 278 | + __u16 bgid; |
| 271 | 279 | }; |
| 272 | 280 | |
| 273 | 281 | struct io_restriction { |
| @@ -326,6 +334,14 @@ struct io_submit_state { · 52 unmodified lines |
| 326 | 334 | struct blk_plug plug; |
| 327 | 335 | }; |
| 328 | 336 | |
| 337 | +struct io_ev_fd { |
| 338 | + struct eventfd_ctx *cq_ev_fd; |
| 339 | + unsigned int eventfd_async: 1; |
| 340 | + struct rcu_head rcu; |
| 341 | +}; |
| 342 | + |
| 343 | +#define IO_BUFFERS_HASH_BITS 5 |
| 344 | + |
| 329 | 345 | struct io_ring_ctx { |
| 330 | 346 | /* const or read-mostly hot data */ |
| 331 | 347 | struct { |
| @@ -335,11 +351,11 @@ struct io_ring_ctx { · 3 unmodified lines |
| 335 | 351 | unsigned int flags; |
| 336 | 352 | unsigned int compat: 1; |
| 337 | 353 | unsigned int drain_next: 1; |
| 338 | | − unsigned int eventfd_async: 1; |
| 339 | 354 | unsigned int restricted: 1; |
| 340 | 355 | unsigned int off_timeout_used: 1; |
| 341 | 356 | unsigned int drain_active: 1; |
| 342 | 357 | unsigned int drain_disabled: 1; |
| 358 | + unsigned int has_evfd: 1; |
| 343 | 359 | } ____cacheline_aligned_in_smp; |
| 344 | 360 | |
| 345 | 361 | /* submission data */ |
| @@ -378,7 +394,9 @@ struct io_ring_ctx { · 32 unmodified lines |
| 378 | 394 | struct list_head timeout_list; |
| 379 | 395 | struct list_head ltimeout_list; |
| 380 | 396 | struct list_head cq_overflow_list; |
| 381 | | − struct xarray io_buffers; |
| 397 | + struct list_head *io_buffers; |
| 398 | + struct list_head io_buffers_cache; |
| 399 | + struct list_head apoll_cache; |
| 382 | 400 | struct xarray personalities; |
| 383 | 401 | u32 pers_next; |
| 384 | 402 | unsigned sq_thread_idle; |
| @@ -395,11 +413,16 @@ struct io_ring_ctx { · 10 unmodified lines |
| 395 | 413 | struct list_head sqd_list; |
| 396 | 414 | |
| 397 | 415 | unsigned long check_cq_overflow; |
| 416 | +#ifdef CONFIG_NET_RX_BUSY_POLL |
| 417 | + /* used to track busy poll napi_id */ |
| 418 | + struct list_head napi_list; |
| 419 | + spinlock_t napi_lock; /* napi_list lock */ |
| 420 | +#endif |
| 398 | 421 | |
| 399 | 422 | struct { |
| 400 | 423 | unsigned cached_cq_tail; |
| 401 | 424 | unsigned cq_entries; |
| 402 | | − struct eventfd_ctx *cq_ev_fd; |
| 425 | + struct io_ev_fd __rcu *io_ev_fd; |
| 403 | 426 | struct wait_queue_head cq_wait; |
| 404 | 427 | unsigned cq_extra; |
| 405 | 428 | atomic_t cq_timeouts; |
| @@ -421,6 +444,8 @@ struct io_ring_ctx { · 15 unmodified lines |
| 421 | 444 | struct hlist_head *cancel_hash; |
| 422 | 445 | unsigned cancel_hash_bits; |
| 423 | 446 | bool poll_multi_queue; |
| 447 | + |
| 448 | + struct list_head io_buffers_comp; |
| 424 | 449 | } ____cacheline_aligned_in_smp; |
| 425 | 450 | |
| 426 | 451 | struct io_restriction restrictions; |
| @@ -436,6 +461,8 @@ struct io_ring_ctx { · 9 unmodified lines |
| 436 | 461 | struct llist_head rsrc_put_llist; |
| 437 | 462 | struct list_head rsrc_ref_list; |
| 438 | 463 | spinlock_t rsrc_ref_lock; |
| 464 | + |
| 465 | + struct list_head io_buffers_pages; |
| 439 | 466 | }; |
| 440 | 467 | |
| 441 | 468 | /* Keep this last, we don't need it for the fast path */ |
| @@ -461,6 +488,11 @@ struct io_ring_ctx { · 19 unmodified lines |
| 461 | 488 | }; |
| 462 | 489 | }; |
| 463 | 490 | |
| 491 | +/* |
| 492 | + * Arbitrary limit, can be raised if need be |
| 493 | + */ |
| 494 | +#define IO_RINGFD_REG_MAX 16 |
| 495 | + |
| 464 | 496 | struct io_uring_task { |
| 465 | 497 | /* submission side */ |
| 466 | 498 | int cached_refs; |
| @@ -476,6 +508,7 @@ struct io_uring_task { · 9 unmodified lines |
| 476 | 508 | struct io_wq_work_list task_list; |
| 477 | 509 | struct io_wq_work_list prior_task_list; |
| 478 | 510 | struct callback_head task_work; |
| 511 | + struct file **registered_rings; |
| 479 | 512 | bool task_running; |
| 480 | 513 | }; |
| 481 | 514 | |
| @@ -690,6 +723,12 @@ struct io_hardlink { · 208 unmodified lines |
| 690 | 723 | int flags; |
| 691 | 724 | }; |
| 692 | 725 | |
| 726 | +struct io_msg { |
| 727 | + struct file *file; |
| 728 | + u64 user_data; |
| 729 | + u32 len; |
| 730 | +}; |
| 731 | + |
| 693 | 732 | struct io_async_connect { |
| 694 | 733 | struct sockaddr_storage address; |
| 695 | 734 | }; |
| @@ -741,6 +780,8 @@ enum { · 45 unmodified lines |
| 741 | 780 | REQ_F_ARM_LTIMEOUT_BIT, |
| 742 | 781 | REQ_F_ASYNC_DATA_BIT, |
| 743 | 782 | REQ_F_SKIP_LINK_CQES_BIT, |
| 783 | + REQ_F_SINGLE_POLL_BIT, |
| 784 | + REQ_F_DOUBLE_POLL_BIT, |
| 744 | 785 | /* keep async read/write and isreg together and in order */ |
| 745 | 786 | REQ_F_SUPPORT_NOWAIT_BIT, |
| 746 | 787 | REQ_F_ISREG_BIT, |
| @@ -799,6 +840,10 @@ enum { · 52 unmodified lines |
| 799 | 840 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), |
| 800 | 841 | /* don't post CQEs while failing linked requests */ |
| 801 | 842 | REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT), |
| 843 | + /* single poll may be active */ |
| 844 | + REQ_F_SINGLE_POLL = BIT(REQ_F_SINGLE_POLL_BIT), |
| 845 | + /* double poll may active */ |
| 846 | + REQ_F_DOUBLE_POLL = BIT(REQ_F_DOUBLE_POLL_BIT), |
| 802 | 847 | }; |
| 803 | 848 | |
| 804 | 849 | struct async_poll { |
| @@ -825,7 +870,7 @@ enum { · 20 unmodified lines |
| 825 | 870 | * NOTE! Each of the iocb union members has the file pointer |
| 826 | 871 | * as the first entry in their struct definition. So you can |
| 827 | 872 | * access the file pointer through any of the sub-structs, |
| 828 | | − * or directly as just 'ki_filp' in this struct. |
| 873 | + * or directly as just 'file' in this struct. |
| 829 | 874 | */ |
| 830 | 875 | struct io_kiocb { |
| 831 | 876 | union { |
| @@ -855,6 +900,7 @@ struct io_kiocb { · 23 unmodified lines |
| 855 | 900 | struct io_mkdir mkdir; |
| 856 | 901 | struct io_symlink symlink; |
| 857 | 902 | struct io_hardlink hardlink; |
| 903 | + struct io_msg msg; |
| 858 | 904 | }; |
| 859 | 905 | |
| 860 | 906 | u8 opcode; |
| @@ -877,6 +923,7 @@ struct io_kiocb { · 16 unmodified lines |
| 877 | 923 | /* used by request caches, completion batching and iopoll */ |
| 878 | 924 | struct io_wq_work_node comp_list; |
| 879 | 925 | atomic_t refs; |
| 926 | + atomic_t poll_refs; |
| 880 | 927 | struct io_kiocb *link; |
| 881 | 928 | struct io_task_work io_task_work; |
| 882 | 929 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| @@ -885,12 +932,11 @@ struct io_kiocb { · 2 unmodified lines |
| 885 | 932 | struct async_poll *apoll; |
| 886 | 933 | /* opcode allocated if it needs to store data for async defer */ |
| 887 | 934 | void *async_data; |
| 888 | | − struct io_wq_work work; |
| 889 | 935 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
| 890 | | − const struct cred *creds; |
| 891 | 936 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
| 892 | 937 | struct io_buffer *kbuf; |
| 893 | | − atomic_t poll_refs; |
| 938 | + const struct cred *creds; |
| 939 | + struct io_wq_work work; |
| 894 | 940 | }; |
| 895 | 941 | |
| 896 | 942 | struct io_tctx_node { |
| @@ -1105,6 +1151,9 @@ static const struct io_op_def io_op_defs[] = { · 208 unmodified lines |
| 1105 | 1151 | [IORING_OP_MKDIRAT] = {}, |
| 1106 | 1152 | [IORING_OP_SYMLINKAT] = {}, |
| 1107 | 1153 | [IORING_OP_LINKAT] = {}, |
| 1154 | + [IORING_OP_MSG_RING] = { |
| 1155 | + .needs_file = 1, |
| 1156 | + }, |
| 1108 | 1157 | }; |
| 1109 | 1158 | |
| 1110 | 1159 | /* requests with any of those set should undergo io_disarm_next() */ |
| @@ -1141,6 +1190,7 @@ static int io_install_fixed_file(struct io_kiocb *req, struct file *file, · 30 unmodified lines |
| 1141 | 1190 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
| 1142 | 1191 | |
| 1143 | 1192 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
| 1193 | +static void io_eventfd_signal(struct io_ring_ctx *ctx); |
| 1144 | 1194 | |
| 1145 | 1195 | static struct kmem_cache *req_cachep; |
| 1146 | 1196 | |
| @@ -1267,36 +1317,88 @@ static inline void io_req_set_rsrc_node(struct io_kiocb *req, · 120 unmodified lines |
| 1267 | 1317 | } |
| 1268 | 1318 | } |
| 1269 | 1319 | |
| 1270 | | −static unsigned int __io_put_kbuf(struct io_kiocb *req) |
| 1320 | +static unsigned int __io_put_kbuf(struct io_kiocb *req, struct list_head *list) |
| 1271 | 1321 | { |
| 1272 | 1322 | struct io_buffer *kbuf = req->kbuf; |
| 1273 | 1323 | unsigned int cflags; |
| 1274 | 1324 | |
| 1275 | | − cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 1276 | | − cflags |= IORING_CQE_F_BUFFER; |
| 1325 | + cflags = IORING_CQE_F_BUFFER | (kbuf->bid << IORING_CQE_BUFFER_SHIFT); |
| 1277 | 1326 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
| 1278 | | − kfree(kbuf); |
| 1327 | + list_add(&kbuf->list, list); |
| 1279 | 1328 | req->kbuf = NULL; |
| 1280 | 1329 | return cflags; |
| 1281 | 1330 | } |
| 1282 | 1331 | |
| 1283 | | −static inline unsigned int io_put_kbuf(struct io_kiocb *req) |
| 1332 | +static inline unsigned int io_put_kbuf_comp(struct io_kiocb *req) |
| 1284 | 1333 | { |
| 1285 | 1334 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
| 1286 | 1335 | return 0; |
| 1287 | | − return __io_put_kbuf(req); |
| 1336 | + return __io_put_kbuf(req, &req->ctx->io_buffers_comp); |
| 1288 | 1337 | } |
| 1289 | 1338 | |
| 1290 | | −static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
| 1339 | +static inline unsigned int io_put_kbuf(struct io_kiocb *req, |
| 1340 | + unsigned issue_flags) |
| 1291 | 1341 | { |
| 1292 | | − bool got = percpu_ref_tryget(ref); |
| 1342 | + unsigned int cflags; |
| 1343 | + |
| 1344 | + if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
| 1345 | + return 0; |
| 1346 | + |
| 1347 | + /* |
| 1348 | + * We can add this buffer back to two lists: |
| 1349 | + * |
| 1350 | + * 1) The io_buffers_cache list. This one is protected by the |
| 1351 | + * ctx->uring_lock. If we already hold this lock, add back to this |
| 1352 | + * list as we can grab it from issue as well. |
| 1353 | + * 2) The io_buffers_comp list. This one is protected by the |
| 1354 | + * ctx->completion_lock. |
| 1355 | + * |
| 1356 | + * We migrate buffers from the comp_list to the issue cache list |
| 1357 | + * when we need one. |
| 1358 | + */ |
| 1359 | + if (issue_flags & IO_URING_F_UNLOCKED) { |
| 1360 | + struct io_ring_ctx *ctx = req->ctx; |
| 1293 | 1361 | |
| 1294 | | − /* already at zero, wait for ->release() */ |
| 1295 | | − if (!got) |
| 1296 | | − wait_for_completion(compl); |
| 1297 | | − percpu_ref_resurrect(ref); |
| 1298 | | − if (got) |
| 1299 | | − percpu_ref_put(ref); |
| 1362 | + spin_lock(&ctx->completion_lock); |
| 1363 | + cflags = __io_put_kbuf(req, &ctx->io_buffers_comp); |
| 1364 | + spin_unlock(&ctx->completion_lock); |
| 1365 | + } else { |
| 1366 | + cflags = __io_put_kbuf(req, &req->ctx->io_buffers_cache); |
| 1367 | + } |
| 1368 | + |
| 1369 | + return cflags; |
| 1370 | +} |
| 1371 | + |
| 1372 | +static struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx, |
| 1373 | + unsigned int bgid) |
| 1374 | +{ |
| 1375 | + struct list_head *hash_list; |
| 1376 | + struct io_buffer_list *bl; |
| 1377 | + |
| 1378 | + hash_list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)]; |
| 1379 | + list_for_each_entry(bl, hash_list, list) |
| 1380 | + if (bl->bgid == bgid || bgid == -1U) |
| 1381 | + return bl; |
| 1382 | + |
| 1383 | + return NULL; |
| 1384 | +} |
| 1385 | + |
| 1386 | +static void io_kbuf_recycle(struct io_kiocb *req) |
| 1387 | +{ |
| 1388 | + struct io_ring_ctx *ctx = req->ctx; |
| 1389 | + struct io_buffer_list *bl; |
| 1390 | + struct io_buffer *buf; |
| 1391 | + |
| 1392 | + if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
| 1393 | + return; |
| 1394 | + |
| 1395 | + lockdep_assert_held(&ctx->uring_lock); |
| 1396 | + |
| 1397 | + buf = req->kbuf; |
| 1398 | + bl = io_buffer_get_list(ctx, buf->bgid); |
| 1399 | + list_add(&buf->list, &bl->buf_list); |
| 1400 | + req->flags &= ~REQ_F_BUFFER_SELECTED; |
| 1401 | + req->kbuf = NULL; |
| 1300 | 1402 | } |
| 1301 | 1403 | |
| 1302 | 1404 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
| @@ -1409,7 +1511,7 @@ static __cold void io_fallback_req_func(struct work_struct *work) · 106 unmodified lines |
| 1409 | 1511 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1410 | 1512 | { |
| 1411 | 1513 | struct io_ring_ctx *ctx; |
| 1412 | | − int hash_bits; |
| 1514 | + int i, hash_bits; |
| 1413 | 1515 | |
| 1414 | 1516 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1415 | 1517 | if (!ctx) |
| @@ -1436,6 +1538,13 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) · 20 unmodified lines |
| 1436 | 1538 | /* set invalid range, so io_import_fixed() fails meeting it */ |
| 1437 | 1539 | ctx->dummy_ubuf->ubuf = -1UL; |
| 1438 | 1540 | |
| 1541 | + ctx->io_buffers = kcalloc(1U << IO_BUFFERS_HASH_BITS, |
| 1542 | + sizeof(struct list_head), GFP_KERNEL); |
| 1543 | + if (!ctx->io_buffers) |
| 1544 | + goto err; |
| 1545 | + for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) |
| 1546 | + INIT_LIST_HEAD(&ctx->io_buffers[i]); |
| 1547 | + |
| 1439 | 1548 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
| 1440 | 1549 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1441 | 1550 | goto err; |
| @@ -1444,14 +1553,17 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) · 2 unmodified lines |
| 1444 | 1553 | init_waitqueue_head(&ctx->sqo_sq_wait); |
| 1445 | 1554 | INIT_LIST_HEAD(&ctx->sqd_list); |
| 1446 | 1555 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
| 1556 | + INIT_LIST_HEAD(&ctx->io_buffers_cache); |
| 1557 | + INIT_LIST_HEAD(&ctx->apoll_cache); |
| 1447 | 1558 | init_completion(&ctx->ref_comp); |
| 1448 | | − xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
| 1449 | 1559 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
| 1450 | 1560 | mutex_init(&ctx->uring_lock); |
| 1451 | 1561 | init_waitqueue_head(&ctx->cq_wait); |
| 1452 | 1562 | spin_lock_init(&ctx->completion_lock); |
| 1453 | 1563 | spin_lock_init(&ctx->timeout_lock); |
| 1454 | 1564 | INIT_WQ_LIST(&ctx->iopoll_list); |
| 1565 | + INIT_LIST_HEAD(&ctx->io_buffers_pages); |
| 1566 | + INIT_LIST_HEAD(&ctx->io_buffers_comp); |
| 1455 | 1567 | INIT_LIST_HEAD(&ctx->defer_list); |
| 1456 | 1568 | INIT_LIST_HEAD(&ctx->timeout_list); |
| 1457 | 1569 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
| @@ -1464,10 +1576,15 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) · 6 unmodified lines |
| 1464 | 1576 | INIT_WQ_LIST(&ctx->locked_free_list); |
| 1465 | 1577 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
| 1466 | 1578 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
| 1579 | +#ifdef CONFIG_NET_RX_BUSY_POLL |
| 1580 | + INIT_LIST_HEAD(&ctx->napi_list); |
| 1581 | + spin_lock_init(&ctx->napi_lock); |
| 1582 | +#endif |
| 1467 | 1583 | return ctx; |
| 1468 | 1584 | err: |
| 1469 | 1585 | kfree(ctx->dummy_ubuf); |
| 1470 | 1586 | kfree(ctx->cancel_hash); |
| 1587 | + kfree(ctx->io_buffers); |
| 1471 | 1588 | kfree(ctx); |
| 1472 | 1589 | return NULL; |
| 1473 | 1590 | } |
| @@ -1610,8 +1727,8 @@ static void io_queue_async_work(struct io_kiocb *req, bool *dont_use) · 136 unmodified lines |
| 1610 | 1727 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) |
| 1611 | 1728 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1612 | 1729 | |
| 1613 | | − trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1614 | | − &req->work, req->flags); |
| 1730 | + trace_io_uring_queue_async_work(ctx, req, req->user_data, req->opcode, req->flags, |
| 1731 | + &req->work, io_wq_is_hashed(&req->work)); |
| 1615 | 1732 | io_wq_enqueue(tctx->io_wq, &req->work); |
| 1616 | 1733 | if (link) |
| 1617 | 1734 | io_queue_linked_timeout(link); |
| @@ -1681,22 +1798,27 @@ static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) · 63 unmodified lines |
| 1681 | 1798 | spin_unlock_irq(&ctx->timeout_lock); |
| 1682 | 1799 | } |
| 1683 | 1800 | |
| 1684 | | −static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
| 1685 | | −{ |
| 1686 | | − if (ctx->off_timeout_used) |
| 1687 | | − io_flush_timeouts(ctx); |
| 1688 | | − if (ctx->drain_active) |
| 1689 | | − io_queue_deferred(ctx); |
| 1690 | | −} |
| 1691 | | − |
| 1692 | 1801 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1693 | 1802 | { |
| 1694 | | − if (unlikely(ctx->off_timeout_used || ctx->drain_active)) |
| 1695 | | − __io_commit_cqring_flush(ctx); |
| 1696 | 1803 | /* order cqe stores with ring update */ |
| 1697 | 1804 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
| 1698 | 1805 | } |
| 1699 | 1806 | |
| 1807 | +static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
| 1808 | +{ |
| 1809 | + if (ctx->off_timeout_used || ctx->drain_active) { |
| 1810 | + spin_lock(&ctx->completion_lock); |
| 1811 | + if (ctx->off_timeout_used) |
| 1812 | + io_flush_timeouts(ctx); |
| 1813 | + if (ctx->drain_active) |
| 1814 | + io_queue_deferred(ctx); |
| 1815 | + io_commit_cqring(ctx); |
| 1816 | + spin_unlock(&ctx->completion_lock); |
| 1817 | + } |
| 1818 | + if (ctx->has_evfd) |
| 1819 | + io_eventfd_signal(ctx); |
| 1820 | +} |
| 1821 | + |
| 1700 | 1822 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1701 | 1823 | { |
| 1702 | 1824 | struct io_rings *r = ctx->rings; |
| @@ -1726,23 +1848,34 @@ static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) · 23 unmodified lines |
| 1726 | 1848 | return &rings->cqes[tail & mask]; |
| 1727 | 1849 | } |
| 1728 | 1850 | |
| 1729 | | −static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1851 | +static void io_eventfd_signal(struct io_ring_ctx *ctx) |
| 1730 | 1852 | { |
| 1731 | | − if (likely(!ctx->cq_ev_fd)) |
| 1732 | | − return false; |
| 1853 | + struct io_ev_fd *ev_fd; |
| 1854 | + |
| 1855 | + rcu_read_lock(); |
| 1856 | + /* |
| 1857 | + * rcu_dereference ctx->io_ev_fd once and use it for both for checking |
| 1858 | + * and eventfd_signal |
| 1859 | + */ |
| 1860 | + ev_fd = rcu_dereference(ctx->io_ev_fd); |
| 1861 | + |
| 1862 | + /* |
| 1863 | + * Check again if ev_fd exists incase an io_eventfd_unregister call |
| 1864 | + * completed between the NULL check of ctx->io_ev_fd at the start of |
| 1865 | + * the function and rcu_read_lock. |
| 1866 | + */ |
| 1867 | + if (unlikely(!ev_fd)) |
| 1868 | + goto out; |
| 1733 | 1869 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1734 | | − return false; |
| 1735 | | − return !ctx->eventfd_async || io_wq_current_is_worker(); |
| 1870 | + goto out; |
| 1871 | + |
| 1872 | + if (!ev_fd->eventfd_async || io_wq_current_is_worker()) |
| 1873 | + eventfd_signal(ev_fd->cq_ev_fd, 1); |
| 1874 | +out: |
| 1875 | + rcu_read_unlock(); |
| 1736 | 1876 | } |
| 1737 | 1877 | |
| 1738 | | −/* |
| 1739 | | − * This should only get called when at least one event has been posted. |
| 1740 | | − * Some applications rely on the eventfd notification count only changing |
| 1741 | | − * IFF a new CQE has been added to the CQ ring. There's no depedency on |
| 1742 | | − * 1:1 relationship between how many times this function is called (and |
| 1743 | | − * hence the eventfd count) and number of CQEs posted to the CQ ring. |
| 1744 | | − */ |
| 1745 | | −static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 1878 | +static inline void io_cqring_wake(struct io_ring_ctx *ctx) |
| 1746 | 1879 | { |
| 1747 | 1880 | /* |
| 1748 | 1881 | * wake_up_all() may seem excessive, but io_wake_function() and |
| @@ -1751,21 +1884,32 @@ static void io_cqring_ev_posted(struct io_ring_ctx *ctx) · 2 unmodified lines |
| 1751 | 1884 | */ |
| 1752 | 1885 | if (wq_has_sleeper(&ctx->cq_wait)) |
| 1753 | 1886 | wake_up_all(&ctx->cq_wait); |
| 1754 | | − if (io_should_trigger_evfd(ctx)) |
| 1755 | | − eventfd_signal(ctx->cq_ev_fd, 1); |
| 1887 | +} |
| 1888 | + |
| 1889 | +/* |
| 1890 | + * This should only get called when at least one event has been posted. |
| 1891 | + * Some applications rely on the eventfd notification count only changing |
| 1892 | + * IFF a new CQE has been added to the CQ ring. There's no depedency on |
| 1893 | + * 1:1 relationship between how many times this function is called (and |
| 1894 | + * hence the eventfd count) and number of CQEs posted to the CQ ring. |
| 1895 | + */ |
| 1896 | +static inline void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 1897 | +{ |
| 1898 | + if (unlikely(ctx->off_timeout_used || ctx->drain_active || |
| 1899 | + ctx->has_evfd)) |
| 1900 | + __io_commit_cqring_flush(ctx); |
| 1901 | + |
| 1902 | + io_cqring_wake(ctx); |
| 1756 | 1903 | } |
| 1757 | 1904 | |
| 1758 | 1905 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1759 | 1906 | { |
| 1760 | | − /* see waitqueue_active() comment */ |
| 1761 | | − smp_mb(); |
| 1907 | + if (unlikely(ctx->off_timeout_used || ctx->drain_active || |
| 1908 | + ctx->has_evfd)) |
| 1909 | + __io_commit_cqring_flush(ctx); |
| 1762 | 1910 | |
| 1763 | | − if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 1764 | | − if (waitqueue_active(&ctx->cq_wait)) |
| 1765 | | − wake_up_all(&ctx->cq_wait); |
| 1766 | | − } |
| 1767 | | − if (io_should_trigger_evfd(ctx)) |
| 1768 | | − eventfd_signal(ctx->cq_ev_fd, 1); |
| 1911 | + if (ctx->flags & IORING_SETUP_SQPOLL) |
| 1912 | + io_cqring_wake(ctx); |
| 1769 | 1913 | } |
| 1770 | 1914 | |
| 1771 | 1915 | /* Returns true if there are no backlogged entries after the flush */ |
| @@ -1905,8 +2049,6 @@ static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, · 133 unmodified lines |
| 1905 | 2049 | { |
| 1906 | 2050 | struct io_uring_cqe *cqe; |
| 1907 | 2051 | |
| 1908 | | − trace_io_uring_complete(ctx, user_data, res, cflags); |
| 1909 | | − |
| 1910 | 2052 | /* |
| 1911 | 2053 | * If we can't get a cq entry, userspace overflowed the |
| 1912 | 2054 | * submission (by quite a lot). Increment the overflow count in |
| @@ -1922,16 +2064,23 @@ static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, · 9 unmodified lines |
| 1922 | 2064 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
| 1923 | 2065 | } |
| 1924 | 2066 | |
| 2067 | +static inline bool __io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
| 2068 | +{ |
| 2069 | + trace_io_uring_complete(req->ctx, req, req->user_data, res, cflags); |
| 2070 | + return __io_fill_cqe(req->ctx, req->user_data, res, cflags); |
| 2071 | +} |
| 2072 | + |
| 1925 | 2073 | static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
| 1926 | 2074 | { |
| 1927 | 2075 | if (!(req->flags & REQ_F_CQE_SKIP)) |
| 1928 | | − __io_fill_cqe(req->ctx, req->user_data, res, cflags); |
| 2076 | + __io_fill_cqe_req(req, res, cflags); |
| 1929 | 2077 | } |
| 1930 | 2078 | |
| 1931 | 2079 | static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, |
| 1932 | 2080 | s32 res, u32 cflags) |
| 1933 | 2081 | { |
| 1934 | 2082 | ctx->cq_extra++; |
| 2083 | + trace_io_uring_complete(ctx, NULL, user_data, res, cflags); |
| 1935 | 2084 | return __io_fill_cqe(ctx, user_data, res, cflags); |
| 1936 | 2085 | } |
| 1937 | 2086 | |
| @@ -1941,7 +2090,7 @@ static void __io_req_complete_post(struct io_kiocb *req, s32 res, · 3 unmodified lines |
| 1941 | 2090 | struct io_ring_ctx *ctx = req->ctx; |
| 1942 | 2091 | |
| 1943 | 2092 | if (!(req->flags & REQ_F_CQE_SKIP)) |
| 1944 | | − __io_fill_cqe(ctx, req->user_data, res, cflags); |
| 2093 | + __io_fill_cqe_req(req, res, cflags); |
| 1945 | 2094 | /* |
| 1946 | 2095 | * If we're the last reference to this request, add to our locked |
| 1947 | 2096 | * free_list cache. |
| @@ -2000,7 +2149,7 @@ static inline void io_req_complete(struct io_kiocb *req, s32 res) · 52 unmodified lines |
| 2000 | 2149 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
| 2001 | 2150 | { |
| 2002 | 2151 | req_set_fail(req); |
| 2003 | | − io_req_complete_post(req, res, 0); |
| 2152 | + io_req_complete_post(req, res, io_put_kbuf(req, 0)); |
| 2004 | 2153 | } |
| 2005 | 2154 | |
| 2006 | 2155 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
| @@ -2183,7 +2332,9 @@ static void io_fail_links(struct io_kiocb *req) · 176 unmodified lines |
| 2183 | 2332 | nxt = link->link; |
| 2184 | 2333 | link->link = NULL; |
| 2185 | 2334 | |
| 2186 | | − trace_io_uring_fail_link(req, link); |
| 2335 | + trace_io_uring_fail_link(req->ctx, req, req->user_data, |
| 2336 | + req->opcode, link); |
| 2337 | + |
| 2187 | 2338 | if (!ignore_cqes) { |
| 2188 | 2339 | link->flags &= ~REQ_F_CQE_SKIP; |
| 2189 | 2340 | io_fill_cqe_req(link, res, 0); |
| @@ -2302,7 +2453,8 @@ static void handle_prev_tw_list(struct io_wq_work_node *node, · 112 unmodified lines |
| 2302 | 2453 | if (likely(*uring_locked)) |
| 2303 | 2454 | req->io_task_work.func(req, uring_locked); |
| 2304 | 2455 | else |
| 2305 | | − __io_req_complete_post(req, req->result, io_put_kbuf(req)); |
| 2456 | + __io_req_complete_post(req, req->result, |
| 2457 | + io_put_kbuf_comp(req)); |
| 2306 | 2458 | node = next; |
| 2307 | 2459 | } while (node); |
| 2308 | 2460 | |
| @@ -2530,8 +2682,16 @@ static void __io_submit_flush_completions(struct io_ring_ctx *ctx) · 221 unmodified lines |
| 2530 | 2682 | comp_list); |
| 2531 | 2683 | |
| 2532 | 2684 | if (!(req->flags & REQ_F_CQE_SKIP)) |
| 2533 | | − __io_fill_cqe(ctx, req->user_data, req->result, |
| 2534 | | − req->cflags); |
| 2685 | + __io_fill_cqe_req(req, req->result, req->cflags); |
| 2686 | + if ((req->flags & REQ_F_POLLED) && req->apoll) { |
| 2687 | + struct async_poll *apoll = req->apoll; |
| 2688 | + |
| 2689 | + if (apoll->double_poll) |
| 2690 | + kfree(apoll->double_poll); |
| 2691 | + list_add(&apoll->poll.wait.entry, |
| 2692 | + &ctx->apoll_cache); |
| 2693 | + req->flags &= ~REQ_F_POLLED; |
| 2694 | + } |
| 2535 | 2695 | } |
| 2536 | 2696 | |
| 2537 | 2697 | io_commit_cqring(ctx); |
| @@ -2653,7 +2813,7 @@ static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) · 115 unmodified lines |
| 2653 | 2813 | if (unlikely(req->flags & REQ_F_CQE_SKIP)) |
| 2654 | 2814 | continue; |
| 2655 | 2815 | |
| 2656 | | − __io_fill_cqe(ctx, req->user_data, req->result, io_put_kbuf(req)); |
| 2816 | + __io_fill_cqe_req(req, req->result, io_put_kbuf(req, 0)); |
| 2657 | 2817 | nr_events++; |
| 2658 | 2818 | } |
| 2659 | 2819 | |
| @@ -2829,14 +2989,14 @@ static bool __io_complete_rw_common(struct io_kiocb *req, long res) · 169 unmodified lines |
| 2829 | 2989 | |
| 2830 | 2990 | static inline void io_req_task_complete(struct io_kiocb *req, bool *locked) |
| 2831 | 2991 | { |
| 2832 | | − unsigned int cflags = io_put_kbuf(req); |
| 2833 | 2992 | int res = req->result; |
| 2834 | 2993 | |
| 2835 | 2994 | if (*locked) { |
| 2836 | | − io_req_complete_state(req, res, cflags); |
| 2995 | + io_req_complete_state(req, res, io_put_kbuf(req, 0)); |
| 2837 | 2996 | io_req_add_compl_list(req); |
| 2838 | 2997 | } else { |
| 2839 | | − io_req_complete_post(req, res, cflags); |
| 2998 | + io_req_complete_post(req, res, |
| 2999 | + io_put_kbuf(req, IO_URING_F_UNLOCKED)); |
| 2840 | 3000 | } |
| 2841 | 3001 | } |
| 2842 | 3002 | |
| @@ -2845,7 +3005,8 @@ static void __io_complete_rw(struct io_kiocb *req, long res, · 2 unmodified lines |
| 2845 | 3005 | { |
| 2846 | 3006 | if (__io_complete_rw_common(req, res)) |
| 2847 | 3007 | return; |
| 2848 | | − __io_req_complete(req, issue_flags, req->result, io_put_kbuf(req)); |
| 3008 | + __io_req_complete(req, issue_flags, req->result, |
| 3009 | + io_put_kbuf(req, issue_flags)); |
| 2849 | 3010 | } |
| 2850 | 3011 | |
| 2851 | 3012 | static void io_complete_rw(struct kiocb *kiocb, long res) |
| @@ -3000,14 +3161,6 @@ static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 148 unmodified lines |
| 3000 | 3161 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; |
| 3001 | 3162 | |
| 3002 | 3163 | kiocb->ki_pos = READ_ONCE(sqe->off); |
| 3003 | | − if (kiocb->ki_pos == -1) { |
| 3004 | | − if (!(file->f_mode & FMODE_STREAM)) { |
| 3005 | | − req->flags |= REQ_F_CUR_POS; |
| 3006 | | − kiocb->ki_pos = file->f_pos; |
| 3007 | | − } else { |
| 3008 | | − kiocb->ki_pos = 0; |
| 3009 | | − } |
| 3010 | | − } |
| 3011 | 3164 | kiocb->ki_flags = iocb_flags(file); |
| 3012 | 3165 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 3013 | 3166 | if (unlikely(ret)) |
| @@ -3074,6 +3227,24 @@ static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) · 60 unmodified lines |
| 3074 | 3227 | } |
| 3075 | 3228 | } |
| 3076 | 3229 | |
| 3230 | +static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req) |
| 3231 | +{ |
| 3232 | + struct kiocb *kiocb = &req->rw.kiocb; |
| 3233 | + bool is_stream = req->file->f_mode & FMODE_STREAM; |
| 3234 | + |
| 3235 | + if (kiocb->ki_pos == -1) { |
| 3236 | + if (!is_stream) { |
| 3237 | + req->flags |= REQ_F_CUR_POS; |
| 3238 | + kiocb->ki_pos = req->file->f_pos; |
| 3239 | + return &kiocb->ki_pos; |
| 3240 | + } else { |
| 3241 | + kiocb->ki_pos = 0; |
| 3242 | + return NULL; |
| 3243 | + } |
| 3244 | + } |
| 3245 | + return is_stream ? NULL : &kiocb->ki_pos; |
| 3246 | +} |
| 3247 | + |
| 3077 | 3248 | static void kiocb_done(struct io_kiocb *req, ssize_t ret, |
| 3078 | 3249 | unsigned int issue_flags) |
| 3079 | 3250 | { |
| @@ -3096,14 +3267,10 @@ static void kiocb_done(struct io_kiocb *req, ssize_t ret, · 16 unmodified lines |
| 3096 | 3267 | |
| 3097 | 3268 | if (req->flags & REQ_F_REISSUE) { |
| 3098 | 3269 | req->flags &= ~REQ_F_REISSUE; |
| 3099 | | − if (io_resubmit_prep(req)) { |
| 3270 | + if (io_resubmit_prep(req)) |
| 3100 | 3271 | io_req_task_queue_reissue(req); |
| 3101 | | − } else { |
| 3102 | | − req_set_fail(req); |
| 3103 | | − req->result = ret; |
| 3104 | | − req->io_task_work.func = io_req_task_complete; |
| 3105 | | − io_req_task_work_add(req, false); |
| 3106 | | − } |
| 3272 | + else |
| 3273 | + io_req_task_queue_fail(req, ret); |
| 3107 | 3274 | } |
| 3108 | 3275 | } |
| 3109 | 3276 | |
| @@ -3201,30 +3368,36 @@ static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) · 91 unmodified lines |
| 3201 | 3368 | mutex_lock(&ctx->uring_lock); |
| 3202 | 3369 | } |
| 3203 | 3370 | |
| 3371 | +static void io_buffer_add_list(struct io_ring_ctx *ctx, |
| 3372 | + struct io_buffer_list *bl, unsigned int bgid) |
| 3373 | +{ |
| 3374 | + struct list_head *list; |
| 3375 | + |
| 3376 | + list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)]; |
| 3377 | + INIT_LIST_HEAD(&bl->buf_list); |
| 3378 | + bl->bgid = bgid; |
| 3379 | + list_add(&bl->list, list); |
| 3380 | +} |
| 3381 | + |
| 3204 | 3382 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 3205 | 3383 | int bgid, unsigned int issue_flags) |
| 3206 | 3384 | { |
| 3207 | 3385 | struct io_buffer *kbuf = req->kbuf; |
| 3208 | | − struct io_buffer *head; |
| 3209 | 3386 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
| 3387 | + struct io_ring_ctx *ctx = req->ctx; |
| 3388 | + struct io_buffer_list *bl; |
| 3210 | 3389 | |
| 3211 | 3390 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 3212 | 3391 | return kbuf; |
| 3213 | 3392 | |
| 3214 | | − io_ring_submit_lock(req->ctx, needs_lock); |
| 3393 | + io_ring_submit_lock(ctx, needs_lock); |
| 3215 | 3394 | |
| 3216 | | − lockdep_assert_held(&req->ctx->uring_lock); |
| 3395 | + lockdep_assert_held(&ctx->uring_lock); |
| 3217 | 3396 | |
| 3218 | | − head = xa_load(&req->ctx->io_buffers, bgid); |
| 3219 | | − if (head) { |
| 3220 | | − if (!list_empty(&head->list)) { |
| 3221 | | − kbuf = list_last_entry(&head->list, struct io_buffer, |
| 3222 | | − list); |
| 3223 | | − list_del(&kbuf->list); |
| 3224 | | − } else { |
| 3225 | | − kbuf = head; |
| 3226 | | − xa_erase(&req->ctx->io_buffers, bgid); |
| 3227 | | − } |
| 3397 | + bl = io_buffer_get_list(ctx, bgid); |
| 3398 | + if (bl && !list_empty(&bl->buf_list)) { |
| 3399 | + kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list); |
| 3400 | + list_del(&kbuf->list); |
| 3228 | 3401 | if (*len > kbuf->len) |
| 3229 | 3402 | *len = kbuf->len; |
| 3230 | 3403 | req->flags |= REQ_F_BUFFER_SELECTED; |
| @@ -3400,6 +3573,7 @@ static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) · 169 unmodified lines |
| 3400 | 3573 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3401 | 3574 | struct file *file = req->file; |
| 3402 | 3575 | ssize_t ret = 0; |
| 3576 | + loff_t *ppos; |
| 3403 | 3577 | |
| 3404 | 3578 | /* |
| 3405 | 3579 | * Don't support polled IO through this interface, and we can't |
| @@ -3412,6 +3586,8 @@ static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) · 6 unmodified lines |
| 3412 | 3586 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) |
| 3413 | 3587 | return -EAGAIN; |
| 3414 | 3588 | |
| 3589 | + ppos = io_kiocb_ppos(kiocb); |
| 3590 | + |
| 3415 | 3591 | while (iov_iter_count(iter)) { |
| 3416 | 3592 | struct iovec iovec; |
| 3417 | 3593 | ssize_t nr; |
| @@ -3425,10 +3601,10 @@ static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) · 7 unmodified lines |
| 3425 | 3601 | |
| 3426 | 3602 | if (rw == READ) { |
| 3427 | 3603 | nr = file->f_op->read(file, iovec.iov_base, |
| 3428 | | − iovec.iov_len, io_kiocb_ppos(kiocb)); |
| 3604 | + iovec.iov_len, ppos); |
| 3429 | 3605 | } else { |
| 3430 | 3606 | nr = file->f_op->write(file, iovec.iov_base, |
| 3431 | | − iovec.iov_len, io_kiocb_ppos(kiocb)); |
| 3607 | + iovec.iov_len, ppos); |
| 3432 | 3608 | } |
| 3433 | 3609 | |
| 3434 | 3610 | if (nr < 0) { |
| @@ -3436,13 +3612,15 @@ static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) · 1 unmodified line |
| 3436 | 3612 | ret = nr; |
| 3437 | 3613 | break; |
| 3438 | 3614 | } |
| 3615 | + ret += nr; |
| 3439 | 3616 | if (!iov_iter_is_bvec(iter)) { |
| 3440 | 3617 | iov_iter_advance(iter, nr); |
| 3441 | 3618 | } else { |
| 3442 | | − req->rw.len -= nr; |
| 3443 | 3619 | req->rw.addr += nr; |
| 3620 | + req->rw.len -= nr; |
| 3621 | + if (!req->rw.len) |
| 3622 | + break; |
| 3444 | 3623 | } |
| 3445 | | − ret += nr; |
| 3446 | 3624 | if (nr != iovec.iov_len) |
| 3447 | 3625 | break; |
| 3448 | 3626 | } |
| @@ -3629,12 +3807,23 @@ static int io_read(struct io_kiocb *req, unsigned int issue_flags) · 180 unmodified lines |
| 3629 | 3807 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
| 3630 | 3808 | struct io_async_rw *rw; |
| 3631 | 3809 | ssize_t ret, ret2; |
| 3810 | + loff_t *ppos; |
| 3632 | 3811 | |
| 3633 | 3812 | if (!req_has_async_data(req)) { |
| 3634 | 3813 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); |
| 3635 | 3814 | if (unlikely(ret < 0)) |
| 3636 | 3815 | return ret; |
| 3637 | 3816 | } else { |
| 3817 | + /* |
| 3818 | + * Safe and required to re-import if we're using provided |
| 3819 | + * buffers, as we dropped the selected one before retry. |
| 3820 | + */ |
| 3821 | + if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3822 | + ret = io_import_iovec(READ, req, &iovec, s, issue_flags); |
| 3823 | + if (unlikely(ret < 0)) |
| 3824 | + return ret; |
| 3825 | + } |
| 3826 | + |
| 3638 | 3827 | rw = req->async_data; |
| 3639 | 3828 | s = &rw->s; |
| 3640 | 3829 | /* |
| @@ -3659,7 +3848,9 @@ static int io_read(struct io_kiocb *req, unsigned int issue_flags) · 18 unmodified lines |
| 3659 | 3848 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3660 | 3849 | } |
| 3661 | 3850 | |
| 3662 | | − ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result); |
| 3851 | + ppos = io_kiocb_update_pos(req); |
| 3852 | + |
| 3853 | + ret = rw_verify_area(READ, req->file, ppos, req->result); |
| 3663 | 3854 | if (unlikely(ret)) { |
| 3664 | 3855 | kfree(iovec); |
| 3665 | 3856 | return ret; |
| @@ -3669,6 +3860,9 @@ static int io_read(struct io_kiocb *req, unsigned int issue_flags) · 3 unmodified lines |
| 3669 | 3860 | |
| 3670 | 3861 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
| 3671 | 3862 | req->flags &= ~REQ_F_REISSUE; |
| 3863 | + /* if we can poll, just do that */ |
| 3864 | + if (req->opcode == IORING_OP_READ && file_can_poll(req->file)) |
| 3865 | + return -EAGAIN; |
| 3672 | 3866 | /* IOPOLL retry should happen for io-wq threads */ |
| 3673 | 3867 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3674 | 3868 | goto done; |
| @@ -3758,6 +3952,7 @@ static int io_write(struct io_kiocb *req, unsigned int issue_flags) · 83 unmodified lines |
| 3758 | 3952 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3759 | 3953 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
| 3760 | 3954 | ssize_t ret, ret2; |
| 3955 | + loff_t *ppos; |
| 3761 | 3956 | |
| 3762 | 3957 | if (!req_has_async_data(req)) { |
| 3763 | 3958 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
| @@ -3788,7 +3983,9 @@ static int io_write(struct io_kiocb *req, unsigned int issue_flags) · 24 unmodified lines |
| 3788 | 3983 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3789 | 3984 | } |
| 3790 | 3985 | |
| 3791 | | − ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result); |
| 3986 | + ppos = io_kiocb_update_pos(req); |
| 3987 | + |
| 3988 | + ret = rw_verify_area(WRITE, req->file, ppos, req->result); |
| 3792 | 3989 | if (unlikely(ret)) |
| 3793 | 3990 | goto out_free; |
| 3794 | 3991 | |
| @@ -4235,6 +4432,45 @@ static int io_nop(struct io_kiocb *req, unsigned int issue_flags) · 440 unmodified lines |
| 4235 | 4432 | return 0; |
| 4236 | 4433 | } |
| 4237 | 4434 | |
| 4435 | +static int io_msg_ring_prep(struct io_kiocb *req, |
| 4436 | + const struct io_uring_sqe *sqe) |
| 4437 | +{ |
| 4438 | + if (unlikely(sqe->addr || sqe->ioprio || sqe->rw_flags || |
| 4439 | + sqe->splice_fd_in || sqe->buf_index || sqe->personality)) |
| 4440 | + return -EINVAL; |
| 4441 | + |
| 4442 | + if (req->file->f_op != &io_uring_fops) |
| 4443 | + return -EBADFD; |
| 4444 | + |
| 4445 | + req->msg.user_data = READ_ONCE(sqe->off); |
| 4446 | + req->msg.len = READ_ONCE(sqe->len); |
| 4447 | + return 0; |
| 4448 | +} |
| 4449 | + |
| 4450 | +static int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags) |
| 4451 | +{ |
| 4452 | + struct io_ring_ctx *target_ctx; |
| 4453 | + struct io_msg *msg = &req->msg; |
| 4454 | + int ret = -EOVERFLOW; |
| 4455 | + bool filled; |
| 4456 | + |
| 4457 | + target_ctx = req->file->private_data; |
| 4458 | + |
| 4459 | + spin_lock(&target_ctx->completion_lock); |
| 4460 | + filled = io_fill_cqe_aux(target_ctx, msg->user_data, msg->len, |
| 4461 | + IORING_CQE_F_MSG); |
| 4462 | + io_commit_cqring(target_ctx); |
| 4463 | + spin_unlock(&target_ctx->completion_lock); |
| 4464 | + |
| 4465 | + if (filled) { |
| 4466 | + io_cqring_ev_posted(target_ctx); |
| 4467 | + ret = 0; |
| 4468 | + } |
| 4469 | + |
| 4470 | + __io_req_complete(req, issue_flags, ret, 0); |
| 4471 | + return 0; |
| 4472 | +} |
| 4473 | + |
| 4238 | 4474 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4239 | 4475 | { |
| 4240 | 4476 | struct io_ring_ctx *ctx = req->ctx; |
| @@ -4458,8 +4694,8 @@ static int io_remove_buffers_prep(struct io_kiocb *req, · 217 unmodified lines |
| 4458 | 4694 | return 0; |
| 4459 | 4695 | } |
| 4460 | 4696 | |
| 4461 | | −static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 4462 | | − int bgid, unsigned nbufs) |
| 4697 | +static int __io_remove_buffers(struct io_ring_ctx *ctx, |
| 4698 | + struct io_buffer_list *bl, unsigned nbufs) |
| 4463 | 4699 | { |
| 4464 | 4700 | unsigned i = 0; |
| 4465 | 4701 | |
| @@ -4468,19 +4704,16 @@ static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, · 2 unmodified lines |
| 4468 | 4704 | return 0; |
| 4469 | 4705 | |
| 4470 | 4706 | /* the head kbuf is the list itself */ |
| 4471 | | − while (!list_empty(&buf->list)) { |
| 4707 | + while (!list_empty(&bl->buf_list)) { |
| 4472 | 4708 | struct io_buffer *nxt; |
| 4473 | 4709 | |
| 4474 | | − nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 4710 | + nxt = list_first_entry(&bl->buf_list, struct io_buffer, list); |
| 4475 | 4711 | list_del(&nxt->list); |
| 4476 | | − kfree(nxt); |
| 4477 | 4712 | if (++i == nbufs) |
| 4478 | 4713 | return i; |
| 4479 | 4714 | cond_resched(); |
| 4480 | 4715 | } |
| 4481 | 4716 | i++; |
| 4482 | | − kfree(buf); |
| 4483 | | − xa_erase(&ctx->io_buffers, bgid); |
| 4484 | 4717 | |
| 4485 | 4718 | return i; |
| 4486 | 4719 | } |
| @@ -4489,7 +4722,7 @@ static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) · 2 unmodified lines |
| 4489 | 4722 | { |
| 4490 | 4723 | struct io_provide_buf *p = &req->pbuf; |
| 4491 | 4724 | struct io_ring_ctx *ctx = req->ctx; |
| 4492 | | − struct io_buffer *head; |
| 4725 | + struct io_buffer_list *bl; |
| 4493 | 4726 | int ret = 0; |
| 4494 | 4727 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
| 4495 | 4728 | |
| @@ -4498,9 +4731,9 @@ static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) · 2 unmodified lines |
| 4498 | 4731 | lockdep_assert_held(&ctx->uring_lock); |
| 4499 | 4732 | |
| 4500 | 4733 | ret = -ENOENT; |
| 4501 | | − head = xa_load(&ctx->io_buffers, p->bgid); |
| 4502 | | − if (head) |
| 4503 | | − ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
| 4734 | + bl = io_buffer_get_list(ctx, p->bgid); |
| 4735 | + if (bl) |
| 4736 | + ret = __io_remove_buffers(ctx, bl, p->nbufs); |
| 4504 | 4737 | if (ret < 0) |
| 4505 | 4738 | req_set_fail(req); |
| 4506 | 4739 | |
| @@ -4545,39 +4778,80 @@ static int io_provide_buffers_prep(struct io_kiocb *req, · 38 unmodified lines |
| 4545 | 4778 | return 0; |
| 4546 | 4779 | } |
| 4547 | 4780 | |
| 4548 | | −static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 4781 | +static int io_refill_buffer_cache(struct io_ring_ctx *ctx) |
| 4782 | +{ |
| 4783 | + struct io_buffer *buf; |
| 4784 | + struct page *page; |
| 4785 | + int bufs_in_page; |
| 4786 | + |
| 4787 | + /* |
| 4788 | + * Completions that don't happen inline (eg not under uring_lock) will |
| 4789 | + * add to ->io_buffers_comp. If we don't have any free buffers, check |
| 4790 | + * the completion list and splice those entries first. |
| 4791 | + */ |
| 4792 | + if (!list_empty_careful(&ctx->io_buffers_comp)) { |
| 4793 | + spin_lock(&ctx->completion_lock); |
| 4794 | + if (!list_empty(&ctx->io_buffers_comp)) { |
| 4795 | + list_splice_init(&ctx->io_buffers_comp, |
| 4796 | + &ctx->io_buffers_cache); |
| 4797 | + spin_unlock(&ctx->completion_lock); |
| 4798 | + return 0; |
| 4799 | + } |
| 4800 | + spin_unlock(&ctx->completion_lock); |
| 4801 | + } |
| 4802 | + |
| 4803 | + /* |
| 4804 | + * No free buffers and no completion entries either. Allocate a new |
| 4805 | + * page worth of buffer entries and add those to our freelist. |
| 4806 | + */ |
| 4807 | + page = alloc_page(GFP_KERNEL_ACCOUNT); |
| 4808 | + if (!page) |
| 4809 | + return -ENOMEM; |
| 4810 | + |
| 4811 | + list_add(&page->lru, &ctx->io_buffers_pages); |
| 4812 | + |
| 4813 | + buf = page_address(page); |
| 4814 | + bufs_in_page = PAGE_SIZE / sizeof(*buf); |
| 4815 | + while (bufs_in_page) { |
| 4816 | + list_add_tail(&buf->list, &ctx->io_buffers_cache); |
| 4817 | + buf++; |
| 4818 | + bufs_in_page--; |
| 4819 | + } |
| 4820 | + |
| 4821 | + return 0; |
| 4822 | +} |
| 4823 | + |
| 4824 | +static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf, |
| 4825 | + struct io_buffer_list *bl) |
| 4549 | 4826 | { |
| 4550 | 4827 | struct io_buffer *buf; |
| 4551 | 4828 | u64 addr = pbuf->addr; |
| 4552 | 4829 | int i, bid = pbuf->bid; |
| 4553 | 4830 | |
| 4554 | 4831 | for (i = 0; i < pbuf->nbufs; i++) { |
| 4555 | | − buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
| 4556 | | − if (!buf) |
| 4832 | + if (list_empty(&ctx->io_buffers_cache) && |
| 4833 | + io_refill_buffer_cache(ctx)) |
| 4557 | 4834 | break; |
| 4558 | | − |
| 4835 | + buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer, |
| 4836 | + list); |
| 4837 | + list_move_tail(&buf->list, &bl->buf_list); |
| 4559 | 4838 | buf->addr = addr; |
| 4560 | 4839 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
| 4561 | 4840 | buf->bid = bid; |
| 4841 | + buf->bgid = pbuf->bgid; |
| 4562 | 4842 | addr += pbuf->len; |
| 4563 | 4843 | bid++; |
| 4564 | | − if (!*head) { |
| 4565 | | − INIT_LIST_HEAD(&buf->list); |
| 4566 | | − *head = buf; |
| 4567 | | − } else { |
| 4568 | | − list_add_tail(&buf->list, &(*head)->list); |
| 4569 | | − } |
| 4570 | 4844 | cond_resched(); |
| 4571 | 4845 | } |
| 4572 | 4846 | |
| 4573 | | − return i ? i : -ENOMEM; |
| 4847 | + return i ? 0 : -ENOMEM; |
| 4574 | 4848 | } |
| 4575 | 4849 | |
| 4576 | 4850 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
| 4577 | 4851 | { |
| 4578 | 4852 | struct io_provide_buf *p = &req->pbuf; |
| 4579 | 4853 | struct io_ring_ctx *ctx = req->ctx; |
| 4580 | | − struct io_buffer *head, *list; |
| 4854 | + struct io_buffer_list *bl; |
| 4581 | 4855 | int ret = 0; |
| 4582 | 4856 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
| 4583 | 4857 | |
| @@ -4585,14 +4859,18 @@ static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) · 1 unmodified line |
| 4585 | 4859 | |
| 4586 | 4860 | lockdep_assert_held(&ctx->uring_lock); |
| 4587 | 4861 | |
| 4588 | | − list = head = xa_load(&ctx->io_buffers, p->bgid); |
| 4589 | | − |
| 4590 | | − ret = io_add_buffers(p, &head); |
| 4591 | | − if (ret >= 0 && !list) { |
| 4592 | | − ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); |
| 4593 | | − if (ret < 0) |
| 4594 | | − __io_remove_buffers(ctx, head, p->bgid, -1U); |
| 4862 | + bl = io_buffer_get_list(ctx, p->bgid); |
| 4863 | + if (unlikely(!bl)) { |
| 4864 | + bl = kmalloc(sizeof(*bl), GFP_KERNEL); |
| 4865 | + if (!bl) { |
| 4866 | + ret = -ENOMEM; |
| 4867 | + goto err; |
| 4868 | + } |
| 4869 | + io_buffer_add_list(ctx, bl, p->bgid); |
| 4595 | 4870 | } |
| 4871 | + |
| 4872 | + ret = io_add_buffers(ctx, p, bl); |
| 4873 | +err: |
| 4596 | 4874 | if (ret < 0) |
| 4597 | 4875 | req_set_fail(req); |
| 4598 | 4876 | /* complete before unlock, IOPOLL may need the lock */ |
| @@ -5184,7 +5462,7 @@ static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) · 585 unmodified lines |
| 5184 | 5462 | if (kmsg->free_iov) |
| 5185 | 5463 | kfree(kmsg->free_iov); |
| 5186 | 5464 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 5187 | | − __io_req_complete(req, issue_flags, ret, io_put_kbuf(req)); |
| 5465 | + __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
| 5188 | 5466 | return 0; |
| 5189 | 5467 | } |
| 5190 | 5468 | |
| @@ -5239,7 +5517,8 @@ static int io_recv(struct io_kiocb *req, unsigned int issue_flags) · 48 unmodified lines |
| 5239 | 5517 | out_free: |
| 5240 | 5518 | req_set_fail(req); |
| 5241 | 5519 | } |
| 5242 | | − __io_req_complete(req, issue_flags, ret, io_put_kbuf(req)); |
| 5520 | + |
| 5521 | + __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
| 5243 | 5522 | return 0; |
| 5244 | 5523 | } |
| 5245 | 5524 | |
| @@ -5258,8 +5537,7 @@ static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 12 unmodified lines |
| 5258 | 5537 | accept->nofile = rlimit(RLIMIT_NOFILE); |
| 5259 | 5538 | |
| 5260 | 5539 | accept->file_slot = READ_ONCE(sqe->file_index); |
| 5261 | | − if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || |
| 5262 | | − (accept->flags & SOCK_CLOEXEC))) |
| 5540 | + if (accept->file_slot && (accept->flags & SOCK_CLOEXEC)) |
| 5263 | 5541 | return -EINVAL; |
| 5264 | 5542 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
| 5265 | 5543 | return -EINVAL; |
| @@ -5399,6 +5677,108 @@ IO_NETOP_FN(send); · 133 unmodified lines |
| 5399 | 5677 | IO_NETOP_FN(recv); |
| 5400 | 5678 | #endif /* CONFIG_NET */ |
| 5401 | 5679 | |
| 5680 | +#ifdef CONFIG_NET_RX_BUSY_POLL |
| 5681 | + |
| 5682 | +#define NAPI_TIMEOUT (60 * SEC_CONVERSION) |
| 5683 | + |
| 5684 | +struct napi_entry { |
| 5685 | + struct list_head list; |
| 5686 | + unsigned int napi_id; |
| 5687 | + unsigned long timeout; |
| 5688 | +}; |
| 5689 | + |
| 5690 | +/* |
| 5691 | + * Add busy poll NAPI ID from sk. |
| 5692 | + */ |
| 5693 | +static void io_add_napi(struct file *file, struct io_ring_ctx *ctx) |
| 5694 | +{ |
| 5695 | + unsigned int napi_id; |
| 5696 | + struct socket *sock; |
| 5697 | + struct sock *sk; |
| 5698 | + struct napi_entry *ne; |
| 5699 | + |
| 5700 | + if (!net_busy_loop_on()) |
| 5701 | + return; |
| 5702 | + |
| 5703 | + sock = sock_from_file(file); |
| 5704 | + if (!sock) |
| 5705 | + return; |
| 5706 | + |
| 5707 | + sk = sock->sk; |
| 5708 | + if (!sk) |
| 5709 | + return; |
| 5710 | + |
| 5711 | + napi_id = READ_ONCE(sk->sk_napi_id); |
| 5712 | + |
| 5713 | + /* Non-NAPI IDs can be rejected */ |
| 5714 | + if (napi_id < MIN_NAPI_ID) |
| 5715 | + return; |
| 5716 | + |
| 5717 | + spin_lock(&ctx->napi_lock); |
| 5718 | + list_for_each_entry(ne, &ctx->napi_list, list) { |
| 5719 | + if (ne->napi_id == napi_id) { |
| 5720 | + ne->timeout = jiffies + NAPI_TIMEOUT; |
| 5721 | + goto out; |
| 5722 | + } |
| 5723 | + } |
| 5724 | + |
| 5725 | + ne = kmalloc(sizeof(*ne), GFP_NOWAIT); |
| 5726 | + if (!ne) |
| 5727 | + goto out; |
| 5728 | + |
| 5729 | + ne->napi_id = napi_id; |
| 5730 | + ne->timeout = jiffies + NAPI_TIMEOUT; |
| 5731 | + list_add_tail(&ne->list, &ctx->napi_list); |
| 5732 | +out: |
| 5733 | + spin_unlock(&ctx->napi_lock); |
| 5734 | +} |
| 5735 | + |
| 5736 | +static inline void io_check_napi_entry_timeout(struct napi_entry *ne) |
| 5737 | +{ |
| 5738 | + if (time_after(jiffies, ne->timeout)) { |
| 5739 | + list_del(&ne->list); |
| 5740 | + kfree(ne); |
| 5741 | + } |
| 5742 | +} |
| 5743 | + |
| 5744 | +/* |
| 5745 | + * Busy poll if globally on and supporting sockets found |
| 5746 | + */ |
| 5747 | +static bool io_napi_busy_loop(struct list_head *napi_list) |
| 5748 | +{ |
| 5749 | + struct napi_entry *ne, *n; |
| 5750 | + |
| 5751 | + list_for_each_entry_safe(ne, n, napi_list, list) { |
| 5752 | + napi_busy_loop(ne->napi_id, NULL, NULL, true, |
| 5753 | + BUSY_POLL_BUDGET); |
| 5754 | + io_check_napi_entry_timeout(ne); |
| 5755 | + } |
| 5756 | + return !list_empty(napi_list); |
| 5757 | +} |
| 5758 | + |
| 5759 | +static void io_free_napi_list(struct io_ring_ctx *ctx) |
| 5760 | +{ |
| 5761 | + spin_lock(&ctx->napi_lock); |
| 5762 | + while (!list_empty(&ctx->napi_list)) { |
| 5763 | + struct napi_entry *ne = |
| 5764 | + list_first_entry(&ctx->napi_list, struct napi_entry, |
| 5765 | + list); |
| 5766 | + |
| 5767 | + list_del(&ne->list); |
| 5768 | + kfree(ne); |
| 5769 | + } |
| 5770 | + spin_unlock(&ctx->napi_lock); |
| 5771 | +} |
| 5772 | +#else |
| 5773 | +static inline void io_add_napi(struct file *file, struct io_ring_ctx *ctx) |
| 5774 | +{ |
| 5775 | +} |
| 5776 | + |
| 5777 | +static inline void io_free_napi_list(struct io_ring_ctx *ctx) |
| 5778 | +{ |
| 5779 | +} |
| 5780 | +#endif /* CONFIG_NET_RX_BUSY_POLL */ |
| 5781 | + |
| 5402 | 5782 | struct io_poll_table { |
| 5403 | 5783 | struct poll_table_struct pt; |
| 5404 | 5784 | struct io_kiocb *req; |
| @@ -5474,8 +5854,12 @@ static inline void io_poll_remove_entry(struct io_poll_iocb *poll) · 69 unmodified lines |
| 5474 | 5854 | |
| 5475 | 5855 | static void io_poll_remove_entries(struct io_kiocb *req) |
| 5476 | 5856 | { |
| 5477 | | − struct io_poll_iocb *poll = io_poll_get_single(req); |
| 5478 | | − struct io_poll_iocb *poll_double = io_poll_get_double(req); |
| 5857 | + /* |
| 5858 | + * Nothing to do if neither of those flags are set. Avoid dipping |
| 5859 | + * into the poll/apoll/double cachelines if we can. |
| 5860 | + */ |
| 5861 | + if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL))) |
| 5862 | + return; |
| 5479 | 5863 | |
| 5480 | 5864 | /* |
| 5481 | 5865 | * While we hold the waitqueue lock and the waitqueue is nonempty, |
| @@ -5493,9 +5877,10 @@ static void io_poll_remove_entries(struct io_kiocb *req) · 11 unmodified lines |
| 5493 | 5877 | * In that case, only RCU prevents the queue memory from being freed. |
| 5494 | 5878 | */ |
| 5495 | 5879 | rcu_read_lock(); |
| 5496 | | − io_poll_remove_entry(poll); |
| 5497 | | − if (poll_double) |
| 5498 | | − io_poll_remove_entry(poll_double); |
| 5880 | + if (req->flags & REQ_F_SINGLE_POLL) |
| 5881 | + io_poll_remove_entry(io_poll_get_single(req)); |
| 5882 | + if (req->flags & REQ_F_DOUBLE_POLL) |
| 5883 | + io_poll_remove_entry(io_poll_get_double(req)); |
| 5499 | 5884 | rcu_read_unlock(); |
| 5500 | 5885 | } |
| 5501 | 5886 | |
| @@ -5527,13 +5912,13 @@ static int io_poll_check_events(struct io_kiocb *req) · 25 unmodified lines |
| 5527 | 5912 | return -ECANCELED; |
| 5528 | 5913 | |
| 5529 | 5914 | if (!req->result) { |
| 5530 | | − struct poll_table_struct pt = { ._key = poll->events }; |
| 5915 | + struct poll_table_struct pt = { ._key = req->cflags }; |
| 5531 | 5916 | |
| 5532 | | − req->result = vfs_poll(req->file, &pt) & poll->events; |
| 5917 | + req->result = vfs_poll(req->file, &pt) & req->cflags; |
| 5533 | 5918 | } |
| 5534 | 5919 | |
| 5535 | 5920 | /* multishot, just fill an CQE and proceed */ |
| 5536 | | − if (req->result && !(poll->events & EPOLLONESHOT)) { |
| 5921 | + if (req->result && !(req->cflags & EPOLLONESHOT)) { |
| 5537 | 5922 | __poll_t mask = mangle_poll(req->result & poll->events); |
| 5538 | 5923 | bool filled; |
| 5539 | 5924 | |
| @@ -5545,6 +5930,7 @@ static int io_poll_check_events(struct io_kiocb *req) · 5 unmodified lines |
| 5545 | 5930 | if (unlikely(!filled)) |
| 5546 | 5931 | return -ECANCELED; |
| 5547 | 5932 | io_cqring_ev_posted(ctx); |
| 5933 | + io_add_napi(req->file, ctx); |
| 5548 | 5934 | } else if (req->result) { |
| 5549 | 5935 | return 0; |
| 5550 | 5936 | } |
| @@ -5603,29 +5989,36 @@ static void io_apoll_task_func(struct io_kiocb *req, bool *locked) · 52 unmodified lines |
| 5603 | 5989 | io_req_complete_failed(req, ret); |
| 5604 | 5990 | } |
| 5605 | 5991 | |
| 5606 | | −static void __io_poll_execute(struct io_kiocb *req, int mask) |
| 5992 | +static void __io_poll_execute(struct io_kiocb *req, int mask, int events) |
| 5607 | 5993 | { |
| 5608 | 5994 | req->result = mask; |
| 5995 | + /* |
| 5996 | + * This is useful for poll that is armed on behalf of another |
| 5997 | + * request, and where the wakeup path could be on a different |
| 5998 | + * CPU. We want to avoid pulling in req->apoll->events for that |
| 5999 | + * case. |
| 6000 | + */ |
| 6001 | + req->cflags = events; |
| 5609 | 6002 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5610 | 6003 | req->io_task_work.func = io_poll_task_func; |
| 5611 | 6004 | else |
| 5612 | 6005 | req->io_task_work.func = io_apoll_task_func; |
| 5613 | 6006 | |
| 5614 | | − trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 6007 | + trace_io_uring_task_add(req->ctx, req, req->user_data, req->opcode, mask); |
| 5615 | 6008 | io_req_task_work_add(req, false); |
| 5616 | 6009 | } |
| 5617 | 6010 | |
| 5618 | | −static inline void io_poll_execute(struct io_kiocb *req, int res) |
| 6011 | +static inline void io_poll_execute(struct io_kiocb *req, int res, int events) |
| 5619 | 6012 | { |
| 5620 | 6013 | if (io_poll_get_ownership(req)) |
| 5621 | | − __io_poll_execute(req, res); |
| 6014 | + __io_poll_execute(req, res, events); |
| 5622 | 6015 | } |
| 5623 | 6016 | |
| 5624 | 6017 | static void io_poll_cancel_req(struct io_kiocb *req) |
| 5625 | 6018 | { |
| 5626 | 6019 | io_poll_mark_cancelled(req); |
| 5627 | 6020 | /* kick tw, which should complete the request */ |
| 5628 | | − io_poll_execute(req, 0); |
| 6021 | + io_poll_execute(req, 0, 0); |
| 5629 | 6022 | } |
| 5630 | 6023 | |
| 5631 | 6024 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| @@ -5639,7 +6032,7 @@ static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, · 7 unmodified lines |
| 5639 | 6032 | if (unlikely(mask & POLLFREE)) { |
| 5640 | 6033 | io_poll_mark_cancelled(req); |
| 5641 | 6034 | /* we have to kick tw in case it's not already */ |
| 5642 | | − io_poll_execute(req, 0); |
| 6035 | + io_poll_execute(req, 0, poll->events); |
| 5643 | 6036 | |
| 5644 | 6037 | /* |
| 5645 | 6038 | * If the waitqueue is being freed early but someone is already |
| @@ -5669,8 +6062,9 @@ static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, · 23 unmodified lines |
| 5669 | 6062 | if (mask && poll->events & EPOLLONESHOT) { |
| 5670 | 6063 | list_del_init(&poll->wait.entry); |
| 5671 | 6064 | poll->head = NULL; |
| 6065 | + req->flags &= ~REQ_F_SINGLE_POLL; |
| 5672 | 6066 | } |
| 5673 | | − __io_poll_execute(req, mask); |
| 6067 | + __io_poll_execute(req, mask, poll->events); |
| 5674 | 6068 | } |
| 5675 | 6069 | return 1; |
| 5676 | 6070 | } |
| @@ -5705,12 +6099,14 @@ static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, · 28 unmodified lines |
| 5705 | 6099 | pt->error = -ENOMEM; |
| 5706 | 6100 | return; |
| 5707 | 6101 | } |
| 6102 | + req->flags |= REQ_F_DOUBLE_POLL; |
| 5708 | 6103 | io_init_poll_iocb(poll, first->events, first->wait.func); |
| 5709 | 6104 | *poll_ptr = poll; |
| 5710 | 6105 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5711 | 6106 | req->flags |= REQ_F_ASYNC_DATA; |
| 5712 | 6107 | } |
| 5713 | 6108 | |
| 6109 | + req->flags |= REQ_F_SINGLE_POLL; |
| 5714 | 6110 | pt->nr_entries++; |
| 5715 | 6111 | poll->head = head; |
| 5716 | 6112 | poll->wait.private = req; |
| @@ -5774,9 +6170,10 @@ static int __io_arm_poll_handler(struct io_kiocb *req, · 57 unmodified lines |
| 5774 | 6170 | /* can't multishot if failed, just queue the event we've got */ |
| 5775 | 6171 | if (unlikely(ipt->error || !ipt->nr_entries)) |
| 5776 | 6172 | poll->events |= EPOLLONESHOT; |
| 5777 | | − __io_poll_execute(req, mask); |
| 6173 | + __io_poll_execute(req, mask, poll->events); |
| 5778 | 6174 | return 0; |
| 5779 | 6175 | } |
| 6176 | + io_add_napi(req->file, req->ctx); |
| 5780 | 6177 | |
| 5781 | 6178 | /* |
| 5782 | 6179 | * Release ownership. If someone tried to queue a tw while it was |
| @@ -5784,7 +6181,7 @@ static int __io_arm_poll_handler(struct io_kiocb *req, · 1 unmodified line |
| 5784 | 6181 | */ |
| 5785 | 6182 | v = atomic_dec_return(&req->poll_refs); |
| 5786 | 6183 | if (unlikely(v & IO_POLL_REF_MASK)) |
| 5787 | | − __io_poll_execute(req, 0); |
| 6184 | + __io_poll_execute(req, 0, poll->events); |
| 5788 | 6185 | return 0; |
| 5789 | 6186 | } |
| 5790 | 6187 | |
| @@ -5803,7 +6200,7 @@ enum { · 12 unmodified lines |
| 5803 | 6200 | IO_APOLL_READY |
| 5804 | 6201 | }; |
| 5805 | 6202 | |
| 5806 | | −static int io_arm_poll_handler(struct io_kiocb *req) |
| 6203 | +static int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags) |
| 5807 | 6204 | { |
| 5808 | 6205 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5809 | 6206 | struct io_ring_ctx *ctx = req->ctx; |
| @@ -5828,9 +6225,16 @@ static int io_arm_poll_handler(struct io_kiocb *req) · 18 unmodified lines |
| 5828 | 6225 | mask |= POLLOUT | POLLWRNORM; |
| 5829 | 6226 | } |
| 5830 | 6227 | |
| 5831 | | − apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5832 | | − if (unlikely(!apoll)) |
| 5833 | | − return IO_APOLL_ABORTED; |
| 6228 | + if (!(issue_flags & IO_URING_F_UNLOCKED) && |
| 6229 | + !list_empty(&ctx->apoll_cache)) { |
| 6230 | + apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, |
| 6231 | + poll.wait.entry); |
| 6232 | + list_del_init(&apoll->poll.wait.entry); |
| 6233 | + } else { |
| 6234 | + apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 6235 | + if (unlikely(!apoll)) |
| 6236 | + return IO_APOLL_ABORTED; |
| 6237 | + } |
| 5834 | 6238 | apoll->double_poll = NULL; |
| 5835 | 6239 | req->apoll = apoll; |
| 5836 | 6240 | req->flags |= REQ_F_POLLED; |
| @@ -5840,7 +6244,7 @@ static int io_arm_poll_handler(struct io_kiocb *req) · 3 unmodified lines |
| 5840 | 6244 | if (ret || ipt.error) |
| 5841 | 6245 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; |
| 5842 | 6246 | |
| 5843 | | − trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
| 6247 | + trace_io_uring_poll_arm(ctx, req, req->user_data, req->opcode, |
| 5844 | 6248 | mask, apoll->poll.events); |
| 5845 | 6249 | return IO_APOLL_OK; |
| 5846 | 6250 | } |
| @@ -5975,7 +6379,7 @@ static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe · 128 unmodified lines |
| 5975 | 6379 | return -EINVAL; |
| 5976 | 6380 | |
| 5977 | 6381 | io_req_set_refcount(req); |
| 5978 | | − poll->events = io_poll_parse_events(sqe, flags); |
| 6382 | + req->cflags = poll->events = io_poll_parse_events(sqe, flags); |
| 5979 | 6383 | return 0; |
| 5980 | 6384 | } |
| 5981 | 6385 | |
| @@ -6092,10 +6496,7 @@ static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) · 110 unmodified lines |
| 6092 | 6496 | |
| 6093 | 6497 | if (IS_ERR(req)) |
| 6094 | 6498 | return PTR_ERR(req); |
| 6095 | | − |
| 6096 | | − req_set_fail(req); |
| 6097 | | − io_fill_cqe_req(req, -ECANCELED, 0); |
| 6098 | | − io_put_req_deferred(req); |
| 6499 | + io_req_task_queue_fail(req, -ECANCELED); |
| 6099 | 6500 | return 0; |
| 6100 | 6501 | } |
| 6101 | 6502 | |
| @@ -6568,6 +6969,8 @@ static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 466 unmodified lines |
| 6568 | 6969 | return io_symlinkat_prep(req, sqe); |
| 6569 | 6970 | case IORING_OP_LINKAT: |
| 6570 | 6971 | return io_linkat_prep(req, sqe); |
| 6972 | + case IORING_OP_MSG_RING: |
| 6973 | + return io_msg_ring_prep(req, sqe); |
| 6571 | 6974 | } |
| 6572 | 6975 | |
| 6573 | 6976 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| @@ -6649,7 +7052,7 @@ fail: · 75 unmodified lines |
| 6649 | 7052 | goto queue; |
| 6650 | 7053 | } |
| 6651 | 7054 | |
| 6652 | | − trace_io_uring_defer(ctx, req, req->user_data); |
| 7055 | + trace_io_uring_defer(ctx, req, req->user_data, req->opcode); |
| 6653 | 7056 | de->req = req; |
| 6654 | 7057 | de->seq = seq; |
| 6655 | 7058 | list_add_tail(&de->list, &ctx->defer_list); |
| @@ -6659,7 +7062,7 @@ fail: · 3 unmodified lines |
| 6659 | 7062 | static void io_clean_op(struct io_kiocb *req) |
| 6660 | 7063 | { |
| 6661 | 7064 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 6662 | | − io_put_kbuf(req); |
| 7065 | + io_put_kbuf_comp(req); |
| 6663 | 7066 | |
| 6664 | 7067 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6665 | 7068 | switch (req->opcode) { |
| @@ -6851,6 +7254,9 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) · 185 unmodified lines |
| 6851 | 7254 | case IORING_OP_LINKAT: |
| 6852 | 7255 | ret = io_linkat(req, issue_flags); |
| 6853 | 7256 | break; |
| 7257 | + case IORING_OP_MSG_RING: |
| 7258 | + ret = io_msg_ring(req, issue_flags); |
| 7259 | + break; |
| 6854 | 7260 | default: |
| 6855 | 7261 | ret = -EINVAL; |
| 6856 | 7262 | break; |
| @@ -6926,7 +7332,7 @@ static void io_wq_submit_work(struct io_wq_work *work) · 69 unmodified lines |
| 6926 | 7332 | continue; |
| 6927 | 7333 | } |
| 6928 | 7334 | |
| 6929 | | − if (io_arm_poll_handler(req) == IO_APOLL_OK) |
| 7335 | + if (io_arm_poll_handler(req, issue_flags) == IO_APOLL_OK) |
| 6930 | 7336 | return; |
| 6931 | 7337 | /* aborted or ready, in either case retry blocking */ |
| 6932 | 7338 | needs_poll = false; |
| @@ -6983,7 +7389,7 @@ static struct file *io_file_get_normal(struct io_ring_ctx *ctx, · 50 unmodified lines |
| 6983 | 7389 | { |
| 6984 | 7390 | struct file *file = fget(fd); |
| 6985 | 7391 | |
| 6986 | | − trace_io_uring_file_get(ctx, fd); |
| 7392 | + trace_io_uring_file_get(ctx, req, req->user_data, fd); |
| 6987 | 7393 | |
| 6988 | 7394 | /* we don't allow fixed io_uring files */ |
| 6989 | 7395 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| @@ -7072,7 +7478,7 @@ static void io_queue_sqe_arm_apoll(struct io_kiocb *req) · 82 unmodified lines |
| 7072 | 7478 | { |
| 7073 | 7479 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
| 7074 | 7480 | |
| 7075 | | − switch (io_arm_poll_handler(req)) { |
| 7481 | + switch (io_arm_poll_handler(req, 0)) { |
| 7076 | 7482 | case IO_APOLL_READY: |
| 7077 | 7483 | io_req_task_queue(req); |
| 7078 | 7484 | break; |
| @@ -7081,8 +7487,12 @@ static void io_queue_sqe_arm_apoll(struct io_kiocb *req) · 2 unmodified lines |
| 7081 | 7487 | * Queued up for async execution, worker will release |
| 7082 | 7488 | * submit reference when the iocb is actually submitted. |
| 7083 | 7489 | */ |
| 7490 | + io_kbuf_recycle(req); |
| 7084 | 7491 | io_queue_async_work(req, NULL); |
| 7085 | 7492 | break; |
| 7493 | + case IO_APOLL_OK: |
| 7494 | + io_kbuf_recycle(req); |
| 7495 | + break; |
| 7086 | 7496 | } |
| 7087 | 7497 | |
| 7088 | 7498 | if (linked_timeout) |
| @@ -7281,7 +7691,7 @@ static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, · 192 unmodified lines |
| 7281 | 7691 | |
| 7282 | 7692 | ret = io_init_req(ctx, req, sqe); |
| 7283 | 7693 | if (unlikely(ret)) { |
| 7284 | | − trace_io_uring_req_failed(sqe, ret); |
| 7694 | + trace_io_uring_req_failed(sqe, ctx, req, ret); |
| 7285 | 7695 | |
| 7286 | 7696 | /* fail even hard links since we don't submit */ |
| 7287 | 7697 | if (link->head) { |
| @@ -7308,7 +7718,7 @@ static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, · 20 unmodified lines |
| 7308 | 7718 | } |
| 7309 | 7719 | |
| 7310 | 7720 | /* don't need @sqe from now on */ |
| 7311 | | − trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
| 7721 | + trace_io_uring_submit_sqe(ctx, req, req->user_data, req->opcode, |
| 7312 | 7722 | req->flags, true, |
| 7313 | 7723 | ctx->flags & IORING_SETUP_SQPOLL); |
| 7314 | 7724 | |
| @@ -7451,8 +7861,14 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) · 136 unmodified lines |
| 7451 | 7861 | } |
| 7452 | 7862 | /* will complete beyond this point, count as submitted */ |
| 7453 | 7863 | submitted++; |
| 7454 | | − if (io_submit_sqe(ctx, req, sqe)) |
| 7455 | | − break; |
| 7864 | + if (io_submit_sqe(ctx, req, sqe)) { |
| 7865 | + /* |
| 7866 | + * Continue submitting even for sqe failure if the |
| 7867 | + * ring was setup with IORING_SETUP_SUBMIT_ALL |
| 7868 | + */ |
| 7869 | + if (!(ctx->flags & IORING_SETUP_SUBMIT_ALL)) |
| 7870 | + break; |
| 7871 | + } |
| 7456 | 7872 | } while (submitted < nr); |
| 7457 | 7873 | |
| 7458 | 7874 | if (unlikely(submitted != nr)) { |
| @@ -7519,7 +7935,13 @@ static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) · 60 unmodified lines |
| 7519 | 7935 | !(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 7520 | 7936 | ret = io_submit_sqes(ctx, to_submit); |
| 7521 | 7937 | mutex_unlock(&ctx->uring_lock); |
| 7522 | | − |
| 7938 | +#ifdef CONFIG_NET_RX_BUSY_POLL |
| 7939 | + spin_lock(&ctx->napi_lock); |
| 7940 | + if (!list_empty(&ctx->napi_list) && |
| 7941 | + io_napi_busy_loop(&ctx->napi_list)) |
| 7942 | + ++ret; |
| 7943 | + spin_unlock(&ctx->napi_lock); |
| 7944 | +#endif |
| 7523 | 7945 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 7524 | 7946 | wake_up(&ctx->sqo_sq_wait); |
| 7525 | 7947 | if (creds) |
| @@ -7650,6 +8072,9 @@ struct io_wait_queue { · 124 unmodified lines |
| 7650 | 8072 | struct io_ring_ctx *ctx; |
| 7651 | 8073 | unsigned cq_tail; |
| 7652 | 8074 | unsigned nr_timeouts; |
| 8075 | +#ifdef CONFIG_NET_RX_BUSY_POLL |
| 8076 | + unsigned busy_poll_to; |
| 8077 | +#endif |
| 7653 | 8078 | }; |
| 7654 | 8079 | |
| 7655 | 8080 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
| @@ -7684,11 +8109,11 @@ static int io_run_task_work_sig(void) · 28 unmodified lines |
| 7684 | 8109 | { |
| 7685 | 8110 | if (io_run_task_work()) |
| 7686 | 8111 | return 1; |
| 7687 | | − if (!signal_pending(current)) |
| 7688 | | − return 0; |
| 7689 | 8112 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
| 7690 | 8113 | return -ERESTARTSYS; |
| 7691 | | − return -EINTR; |
| 8114 | + if (task_sigpending(current)) |
| 8115 | + return -EINTR; |
| 8116 | + return 0; |
| 7692 | 8117 | } |
| 7693 | 8118 | |
| 7694 | 8119 | /* when returns >0, the caller should retry */ |
| @@ -7711,6 +8136,87 @@ static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, · 16 unmodified lines |
| 7711 | 8136 | return 1; |
| 7712 | 8137 | } |
| 7713 | 8138 | |
| 8139 | +#ifdef CONFIG_NET_RX_BUSY_POLL |
| 8140 | +static void io_adjust_busy_loop_timeout(struct timespec64 *ts, |
| 8141 | + struct io_wait_queue *iowq) |
| 8142 | +{ |
| 8143 | + unsigned busy_poll_to = READ_ONCE(sysctl_net_busy_poll); |
| 8144 | + struct timespec64 pollto = ns_to_timespec64(1000 * (s64)busy_poll_to); |
| 8145 | + |
| 8146 | + if (timespec64_compare(ts, &pollto) > 0) { |
| 8147 | + *ts = timespec64_sub(*ts, pollto); |
| 8148 | + iowq->busy_poll_to = busy_poll_to; |
| 8149 | + } else { |
| 8150 | + u64 to = timespec64_to_ns(ts); |
| 8151 | + |
| 8152 | + do_div(to, 1000); |
| 8153 | + iowq->busy_poll_to = to; |
| 8154 | + ts->tv_sec = 0; |
| 8155 | + ts->tv_nsec = 0; |
| 8156 | + } |
| 8157 | +} |
| 8158 | + |
| 8159 | +static inline bool io_busy_loop_timeout(unsigned long start_time, |
| 8160 | + unsigned long bp_usec) |
| 8161 | +{ |
| 8162 | + if (bp_usec) { |
| 8163 | + unsigned long end_time = start_time + bp_usec; |
| 8164 | + unsigned long now = busy_loop_current_time(); |
| 8165 | + |
| 8166 | + return time_after(now, end_time); |
| 8167 | + } |
| 8168 | + return true; |
| 8169 | +} |
| 8170 | + |
| 8171 | +static bool io_busy_loop_end(void *p, unsigned long start_time) |
| 8172 | +{ |
| 8173 | + struct io_wait_queue *iowq = p; |
| 8174 | + |
| 8175 | + return signal_pending(current) || |
| 8176 | + io_should_wake(iowq) || |
| 8177 | + io_busy_loop_timeout(start_time, iowq->busy_poll_to); |
| 8178 | +} |
| 8179 | + |
| 8180 | +static void io_blocking_napi_busy_loop(struct list_head *napi_list, |
| 8181 | + struct io_wait_queue *iowq) |
| 8182 | +{ |
| 8183 | + unsigned long start_time = |
| 8184 | + list_is_singular(napi_list) ? 0 : |
| 8185 | + busy_loop_current_time(); |
| 8186 | + |
| 8187 | + do { |
| 8188 | + if (list_is_singular(napi_list)) { |
| 8189 | + struct napi_entry *ne = |
| 8190 | + list_first_entry(napi_list, |
| 8191 | + struct napi_entry, list); |
| 8192 | + |
| 8193 | + napi_busy_loop(ne->napi_id, io_busy_loop_end, iowq, |
| 8194 | + true, BUSY_POLL_BUDGET); |
| 8195 | + io_check_napi_entry_timeout(ne); |
| 8196 | + break; |
| 8197 | + } |
| 8198 | + } while (io_napi_busy_loop(napi_list) && |
| 8199 | + !io_busy_loop_end(iowq, start_time)); |
| 8200 | +} |
| 8201 | + |
| 8202 | +static void io_putback_napi_list(struct io_ring_ctx *ctx, |
| 8203 | + struct list_head *napi_list) |
| 8204 | +{ |
| 8205 | + struct napi_entry *cne, *lne; |
| 8206 | + |
| 8207 | + spin_lock(&ctx->napi_lock); |
| 8208 | + list_for_each_entry(cne, &ctx->napi_list, list) |
| 8209 | + list_for_each_entry(lne, napi_list, list) |
| 8210 | + if (cne->napi_id == lne->napi_id) { |
| 8211 | + list_del(&lne->list); |
| 8212 | + kfree(lne); |
| 8213 | + break; |
| 8214 | + } |
| 8215 | + list_splice(napi_list, &ctx->napi_list); |
| 8216 | + spin_unlock(&ctx->napi_lock); |
| 8217 | +} |
| 8218 | +#endif /* CONFIG_NET_RX_BUSY_POLL */ |
| 8219 | + |
| 7714 | 8220 | /* |
| 7715 | 8221 | * Wait until events become available, if we don't already have some. The |
| 7716 | 8222 | * application must reap them itself, as they reside on the shared cq ring. |
| @@ -7723,6 +8229,9 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, · 6 unmodified lines |
| 7723 | 8229 | struct io_rings *rings = ctx->rings; |
| 7724 | 8230 | ktime_t timeout = KTIME_MAX; |
| 7725 | 8231 | int ret; |
| 8232 | +#ifdef CONFIG_NET_RX_BUSY_POLL |
| 8233 | + LIST_HEAD(local_napi_list); |
| 8234 | +#endif |
| 7726 | 8235 | |
| 7727 | 8236 | do { |
| 7728 | 8237 | io_cqring_overflow_flush(ctx); |
| @@ -7732,14 +8241,6 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, · 3 unmodified lines |
| 7732 | 8241 | break; |
| 7733 | 8242 | } while (1); |
| 7734 | 8243 | |
| 7735 | | − if (uts) { |
| 7736 | | − struct timespec64 ts; |
| 7737 | | − |
| 7738 | | − if (get_timespec64(&ts, uts)) |
| 7739 | | − return -EFAULT; |
| 7740 | | − timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns()); |
| 7741 | | − } |
| 7742 | | − |
| 7743 | 8244 | if (sig) { |
| 7744 | 8245 | #ifdef CONFIG_COMPAT |
| 7745 | 8246 | if (in_compat_syscall()) |
| @@ -7753,6 +8254,30 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, · 7 unmodified lines |
| 7753 | 8254 | return ret; |
| 7754 | 8255 | } |
| 7755 | 8256 | |
| 8257 | +#ifdef CONFIG_NET_RX_BUSY_POLL |
| 8258 | + iowq.busy_poll_to = 0; |
| 8259 | + if (!(ctx->flags & IORING_SETUP_SQPOLL)) { |
| 8260 | + spin_lock(&ctx->napi_lock); |
| 8261 | + list_splice_init(&ctx->napi_list, &local_napi_list); |
| 8262 | + spin_unlock(&ctx->napi_lock); |
| 8263 | + } |
| 8264 | +#endif |
| 8265 | + if (uts) { |
| 8266 | + struct timespec64 ts; |
| 8267 | + |
| 8268 | + if (get_timespec64(&ts, uts)) |
| 8269 | + return -EFAULT; |
| 8270 | +#ifdef CONFIG_NET_RX_BUSY_POLL |
| 8271 | + if (!list_empty(&local_napi_list)) |
| 8272 | + io_adjust_busy_loop_timeout(&ts, &iowq); |
| 8273 | +#endif |
| 8274 | + timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns()); |
| 8275 | + } |
| 8276 | +#ifdef CONFIG_NET_RX_BUSY_POLL |
| 8277 | + else if (!list_empty(&local_napi_list)) |
| 8278 | + iowq.busy_poll_to = READ_ONCE(sysctl_net_busy_poll); |
| 8279 | +#endif |
| 8280 | + |
| 7756 | 8281 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
| 7757 | 8282 | iowq.wq.private = current; |
| 7758 | 8283 | INIT_LIST_HEAD(&iowq.wq.entry); |
| @@ -7761,6 +8286,12 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, · 2 unmodified lines |
| 7761 | 8286 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
| 7762 | 8287 | |
| 7763 | 8288 | trace_io_uring_cqring_wait(ctx, min_events); |
| 8289 | +#ifdef CONFIG_NET_RX_BUSY_POLL |
| 8290 | + if (iowq.busy_poll_to) |
| 8291 | + io_blocking_napi_busy_loop(&local_napi_list, &iowq); |
| 8292 | + if (!list_empty(&local_napi_list)) |
| 8293 | + io_putback_napi_list(ctx, &local_napi_list); |
| 8294 | +#endif |
| 7764 | 8295 | do { |
| 7765 | 8296 | /* if we can't even flush overflow, don't wait for more */ |
| 7766 | 8297 | if (!io_cqring_overflow_flush(ctx)) { |
| @@ -8749,8 +9280,16 @@ static __cold int io_uring_alloc_task_context(struct task_struct *task, · 982 unmodified lines |
| 8749 | 9280 | if (unlikely(!tctx)) |
| 8750 | 9281 | return -ENOMEM; |
| 8751 | 9282 | |
| 9283 | + tctx->registered_rings = kcalloc(IO_RINGFD_REG_MAX, |
| 9284 | + sizeof(struct file *), GFP_KERNEL); |
| 9285 | + if (unlikely(!tctx->registered_rings)) { |
| 9286 | + kfree(tctx); |
| 9287 | + return -ENOMEM; |
| 9288 | + } |
| 9289 | + |
| 8752 | 9290 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 8753 | 9291 | if (unlikely(ret)) { |
| 9292 | + kfree(tctx->registered_rings); |
| 8754 | 9293 | kfree(tctx); |
| 8755 | 9294 | return ret; |
| 8756 | 9295 | } |
| @@ -8759,6 +9298,7 @@ static __cold int io_uring_alloc_task_context(struct task_struct *task, · 2 unmodified lines |
| 8759 | 9298 | if (IS_ERR(tctx->io_wq)) { |
| 8760 | 9299 | ret = PTR_ERR(tctx->io_wq); |
| 8761 | 9300 | percpu_counter_destroy(&tctx->inflight); |
| 9301 | + kfree(tctx->registered_rings); |
| 8762 | 9302 | kfree(tctx); |
| 8763 | 9303 | return ret; |
| 8764 | 9304 | } |
| @@ -8783,6 +9323,7 @@ void __io_uring_free(struct task_struct *tsk) · 18 unmodified lines |
| 8783 | 9323 | WARN_ON_ONCE(tctx->io_wq); |
| 8784 | 9324 | WARN_ON_ONCE(tctx->cached_refs); |
| 8785 | 9325 | |
| 9326 | + kfree(tctx->registered_rings); |
| 8786 | 9327 | percpu_counter_destroy(&tctx->inflight); |
| 8787 | 9328 | kfree(tctx); |
| 8788 | 9329 | tsk->io_uring = NULL; |
| @@ -9359,33 +9900,55 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, · 570 unmodified lines |
| 9359 | 9900 | return done ? done : err; |
| 9360 | 9901 | } |
| 9361 | 9902 | |
| 9362 | | −static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 9903 | +static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg, |
| 9904 | + unsigned int eventfd_async) |
| 9363 | 9905 | { |
| 9906 | + struct io_ev_fd *ev_fd; |
| 9364 | 9907 | __s32 __user *fds = arg; |
| 9365 | 9908 | int fd; |
| 9366 | 9909 | |
| 9367 | | − if (ctx->cq_ev_fd) |
| 9910 | + ev_fd = rcu_dereference_protected(ctx->io_ev_fd, |
| 9911 | + lockdep_is_held(&ctx->uring_lock)); |
| 9912 | + if (ev_fd) |
| 9368 | 9913 | return -EBUSY; |
| 9369 | 9914 | |
| 9370 | 9915 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 9371 | 9916 | return -EFAULT; |
| 9372 | 9917 | |
| 9373 | | − ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 9374 | | − if (IS_ERR(ctx->cq_ev_fd)) { |
| 9375 | | − int ret = PTR_ERR(ctx->cq_ev_fd); |
| 9918 | + ev_fd = kmalloc(sizeof(*ev_fd), GFP_KERNEL); |
| 9919 | + if (!ev_fd) |
| 9920 | + return -ENOMEM; |
| 9376 | 9921 | |
| 9377 | | − ctx->cq_ev_fd = NULL; |
| 9922 | + ev_fd->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 9923 | + if (IS_ERR(ev_fd->cq_ev_fd)) { |
| 9924 | + int ret = PTR_ERR(ev_fd->cq_ev_fd); |
| 9925 | + kfree(ev_fd); |
| 9378 | 9926 | return ret; |
| 9379 | 9927 | } |
| 9380 | | − |
| 9928 | + ev_fd->eventfd_async = eventfd_async; |
| 9929 | + ctx->has_evfd = true; |
| 9930 | + rcu_assign_pointer(ctx->io_ev_fd, ev_fd); |
| 9381 | 9931 | return 0; |
| 9382 | 9932 | } |
| 9383 | 9933 | |
| 9934 | +static void io_eventfd_put(struct rcu_head *rcu) |
| 9935 | +{ |
| 9936 | + struct io_ev_fd *ev_fd = container_of(rcu, struct io_ev_fd, rcu); |
| 9937 | + |
| 9938 | + eventfd_ctx_put(ev_fd->cq_ev_fd); |
| 9939 | + kfree(ev_fd); |
| 9940 | +} |
| 9941 | + |
| 9384 | 9942 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 9385 | 9943 | { |
| 9386 | | − if (ctx->cq_ev_fd) { |
| 9387 | | − eventfd_ctx_put(ctx->cq_ev_fd); |
| 9388 | | − ctx->cq_ev_fd = NULL; |
| 9944 | + struct io_ev_fd *ev_fd; |
| 9945 | + |
| 9946 | + ev_fd = rcu_dereference_protected(ctx->io_ev_fd, |
| 9947 | + lockdep_is_held(&ctx->uring_lock)); |
| 9948 | + if (ev_fd) { |
| 9949 | + ctx->has_evfd = false; |
| 9950 | + rcu_assign_pointer(ctx->io_ev_fd, NULL); |
| 9951 | + call_rcu(&ev_fd->rcu, io_eventfd_put); |
| 9389 | 9952 | return 0; |
| 9390 | 9953 | } |
| 9391 | 9954 | |
| @@ -9394,11 +9957,28 @@ static int io_eventfd_unregister(struct io_ring_ctx *ctx) · 2 unmodified lines |
| 9394 | 9957 | |
| 9395 | 9958 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 9396 | 9959 | { |
| 9397 | | − struct io_buffer *buf; |
| 9398 | | − unsigned long index; |
| 9960 | + int i; |
| 9961 | + |
| 9962 | + for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) { |
| 9963 | + struct list_head *list = &ctx->io_buffers[i]; |
| 9964 | + |
| 9965 | + while (!list_empty(list)) { |
| 9966 | + struct io_buffer_list *bl; |
| 9967 | + |
| 9968 | + bl = list_first_entry(list, struct io_buffer_list, list); |
| 9969 | + __io_remove_buffers(ctx, bl, -1U); |
| 9970 | + list_del(&bl->list); |
| 9971 | + kfree(bl); |
| 9972 | + } |
| 9973 | + } |
| 9399 | 9974 | |
| 9400 | | − xa_for_each(&ctx->io_buffers, index, buf) |
| 9401 | | − __io_remove_buffers(ctx, buf, index, -1U); |
| 9975 | + while (!list_empty(&ctx->io_buffers_pages)) { |
| 9976 | + struct page *page; |
| 9977 | + |
| 9978 | + page = list_first_entry(&ctx->io_buffers_pages, struct page, lru); |
| 9979 | + list_del_init(&page->lru); |
| 9980 | + __free_page(page); |
| 9981 | + } |
| 9402 | 9982 | } |
| 9403 | 9983 | |
| 9404 | 9984 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
| @@ -9429,6 +10009,18 @@ static void io_wait_rsrc_data(struct io_rsrc_data *data) · 24 unmodified lines |
| 9429 | 10009 | wait_for_completion(&data->done); |
| 9430 | 10010 | } |
| 9431 | 10011 | |
| 10012 | +static void io_flush_apoll_cache(struct io_ring_ctx *ctx) |
| 10013 | +{ |
| 10014 | + struct async_poll *apoll; |
| 10015 | + |
| 10016 | + while (!list_empty(&ctx->apoll_cache)) { |
| 10017 | + apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, |
| 10018 | + poll.wait.entry); |
| 10019 | + list_del(&apoll->poll.wait.entry); |
| 10020 | + kfree(apoll); |
| 10021 | + } |
| 10022 | +} |
| 10023 | + |
| 9432 | 10024 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 9433 | 10025 | { |
| 9434 | 10026 | io_sq_thread_finish(ctx); |
| @@ -9450,8 +10042,9 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) · 15 unmodified lines |
| 9450 | 10042 | __io_sqe_files_unregister(ctx); |
| 9451 | 10043 | if (ctx->rings) |
| 9452 | 10044 | __io_cqring_overflow_flush(ctx, true); |
| 9453 | | − mutex_unlock(&ctx->uring_lock); |
| 9454 | 10045 | io_eventfd_unregister(ctx); |
| 10046 | + io_flush_apoll_cache(ctx); |
| 10047 | + mutex_unlock(&ctx->uring_lock); |
| 9455 | 10048 | io_destroy_buffers(ctx); |
| 9456 | 10049 | if (ctx->sq_creds) |
| 9457 | 10050 | put_cred(ctx->sq_creds); |
| @@ -9483,8 +10076,10 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) · 25 unmodified lines |
| 9483 | 10076 | io_req_caches_free(ctx); |
| 9484 | 10077 | if (ctx->hash_map) |
| 9485 | 10078 | io_wq_put_hash(ctx->hash_map); |
| 10079 | + io_free_napi_list(ctx); |
| 9486 | 10080 | kfree(ctx->cancel_hash); |
| 9487 | 10081 | kfree(ctx->dummy_ubuf); |
| 10082 | + kfree(ctx->io_buffers); |
| 9488 | 10083 | kfree(ctx); |
| 9489 | 10084 | } |
| 9490 | 10085 | |
| @@ -9983,6 +10578,139 @@ void __io_uring_cancel(bool cancel_all) · 492 unmodified lines |
| 9983 | 10578 | io_uring_cancel_generic(cancel_all, NULL); |
| 9984 | 10579 | } |
| 9985 | 10580 | |
| 10581 | +void io_uring_unreg_ringfd(void) |
| 10582 | +{ |
| 10583 | + struct io_uring_task *tctx = current->io_uring; |
| 10584 | + int i; |
| 10585 | + |
| 10586 | + for (i = 0; i < IO_RINGFD_REG_MAX; i++) { |
| 10587 | + if (tctx->registered_rings[i]) { |
| 10588 | + fput(tctx->registered_rings[i]); |
| 10589 | + tctx->registered_rings[i] = NULL; |
| 10590 | + } |
| 10591 | + } |
| 10592 | +} |
| 10593 | + |
| 10594 | +static int io_ring_add_registered_fd(struct io_uring_task *tctx, int fd, |
| 10595 | + int start, int end) |
| 10596 | +{ |
| 10597 | + struct file *file; |
| 10598 | + int offset; |
| 10599 | + |
| 10600 | + for (offset = start; offset < end; offset++) { |
| 10601 | + offset = array_index_nospec(offset, IO_RINGFD_REG_MAX); |
| 10602 | + if (tctx->registered_rings[offset]) |
| 10603 | + continue; |
| 10604 | + |
| 10605 | + file = fget(fd); |
| 10606 | + if (!file) { |
| 10607 | + return -EBADF; |
| 10608 | + } else if (file->f_op != &io_uring_fops) { |
| 10609 | + fput(file); |
| 10610 | + return -EOPNOTSUPP; |
| 10611 | + } |
| 10612 | + tctx->registered_rings[offset] = file; |
| 10613 | + return offset; |
| 10614 | + } |
| 10615 | + |
| 10616 | + return -EBUSY; |
| 10617 | +} |
| 10618 | + |
| 10619 | +/* |
| 10620 | + * Register a ring fd to avoid fdget/fdput for each io_uring_enter() |
| 10621 | + * invocation. User passes in an array of struct io_uring_rsrc_update |
| 10622 | + * with ->data set to the ring_fd, and ->offset given for the desired |
| 10623 | + * index. If no index is desired, application may set ->offset == -1U |
| 10624 | + * and we'll find an available index. Returns number of entries |
| 10625 | + * successfully processed, or < 0 on error if none were processed. |
| 10626 | + */ |
| 10627 | +static int io_ringfd_register(struct io_ring_ctx *ctx, void __user *__arg, |
| 10628 | + unsigned nr_args) |
| 10629 | +{ |
| 10630 | + struct io_uring_rsrc_update __user *arg = __arg; |
| 10631 | + struct io_uring_rsrc_update reg; |
| 10632 | + struct io_uring_task *tctx; |
| 10633 | + int ret, i; |
| 10634 | + |
| 10635 | + if (!nr_args || nr_args > IO_RINGFD_REG_MAX) |
| 10636 | + return -EINVAL; |
| 10637 | + |
| 10638 | + mutex_unlock(&ctx->uring_lock); |
| 10639 | + ret = io_uring_add_tctx_node(ctx); |
| 10640 | + mutex_lock(&ctx->uring_lock); |
| 10641 | + if (ret) |
| 10642 | + return ret; |
| 10643 | + |
| 10644 | + tctx = current->io_uring; |
| 10645 | + for (i = 0; i < nr_args; i++) { |
| 10646 | + int start, end; |
| 10647 | + |
| 10648 | + if (copy_from_user(®, &arg[i], sizeof(reg))) { |
| 10649 | + ret = -EFAULT; |
| 10650 | + break; |
| 10651 | + } |
| 10652 | + |
| 10653 | + if (reg.offset == -1U) { |
| 10654 | + start = 0; |
| 10655 | + end = IO_RINGFD_REG_MAX; |
| 10656 | + } else { |
| 10657 | + if (reg.offset >= IO_RINGFD_REG_MAX) { |
| 10658 | + ret = -EINVAL; |
| 10659 | + break; |
| 10660 | + } |
| 10661 | + start = reg.offset; |
| 10662 | + end = start + 1; |
| 10663 | + } |
| 10664 | + |
| 10665 | + ret = io_ring_add_registered_fd(tctx, reg.data, start, end); |
| 10666 | + if (ret < 0) |
| 10667 | + break; |
| 10668 | + |
| 10669 | + reg.offset = ret; |
| 10670 | + if (copy_to_user(&arg[i], ®, sizeof(reg))) { |
| 10671 | + fput(tctx->registered_rings[reg.offset]); |
| 10672 | + tctx->registered_rings[reg.offset] = NULL; |
| 10673 | + ret = -EFAULT; |
| 10674 | + break; |
| 10675 | + } |
| 10676 | + } |
| 10677 | + |
| 10678 | + return i ? i : ret; |
| 10679 | +} |
| 10680 | + |
| 10681 | +static int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg, |
| 10682 | + unsigned nr_args) |
| 10683 | +{ |
| 10684 | + struct io_uring_rsrc_update __user *arg = __arg; |
| 10685 | + struct io_uring_task *tctx = current->io_uring; |
| 10686 | + struct io_uring_rsrc_update reg; |
| 10687 | + int ret = 0, i; |
| 10688 | + |
| 10689 | + if (!nr_args || nr_args > IO_RINGFD_REG_MAX) |
| 10690 | + return -EINVAL; |
| 10691 | + if (!tctx) |
| 10692 | + return 0; |
| 10693 | + |
| 10694 | + for (i = 0; i < nr_args; i++) { |
| 10695 | + if (copy_from_user(®, &arg[i], sizeof(reg))) { |
| 10696 | + ret = -EFAULT; |
| 10697 | + break; |
| 10698 | + } |
| 10699 | + if (reg.offset >= IO_RINGFD_REG_MAX) { |
| 10700 | + ret = -EINVAL; |
| 10701 | + break; |
| 10702 | + } |
| 10703 | + |
| 10704 | + reg.offset = array_index_nospec(reg.offset, IO_RINGFD_REG_MAX); |
| 10705 | + if (tctx->registered_rings[reg.offset]) { |
| 10706 | + fput(tctx->registered_rings[reg.offset]); |
| 10707 | + tctx->registered_rings[reg.offset] = NULL; |
| 10708 | + } |
| 10709 | + } |
| 10710 | + |
| 10711 | + return i ? i : ret; |
| 10712 | +} |
| 10713 | + |
| 9986 | 10714 | static void *io_uring_validate_mmap_request(struct file *file, |
| 9987 | 10715 | loff_t pgoff, size_t sz) |
| 9988 | 10716 | { |
| @@ -10113,12 +10841,28 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, · 124 unmodified lines |
| 10113 | 10841 | io_run_task_work(); |
| 10114 | 10842 | |
| 10115 | 10843 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
| 10116 | | − IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) |
| 10844 | + IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG | |
| 10845 | + IORING_ENTER_REGISTERED_RING))) |
| 10117 | 10846 | return -EINVAL; |
| 10118 | 10847 | |
| 10119 | | − f = fdget(fd); |
| 10120 | | − if (unlikely(!f.file)) |
| 10121 | | − return -EBADF; |
| 10848 | + /* |
| 10849 | + * Ring fd has been registered via IORING_REGISTER_RING_FDS, we |
| 10850 | + * need only dereference our task private array to find it. |
| 10851 | + */ |
| 10852 | + if (flags & IORING_ENTER_REGISTERED_RING) { |
| 10853 | + struct io_uring_task *tctx = current->io_uring; |
| 10854 | + |
| 10855 | + if (!tctx || fd >= IO_RINGFD_REG_MAX) |
| 10856 | + return -EINVAL; |
| 10857 | + fd = array_index_nospec(fd, IO_RINGFD_REG_MAX); |
| 10858 | + f.file = tctx->registered_rings[fd]; |
| 10859 | + if (unlikely(!f.file)) |
| 10860 | + return -EBADF; |
| 10861 | + } else { |
| 10862 | + f = fdget(fd); |
| 10863 | + if (unlikely(!f.file)) |
| 10864 | + return -EBADF; |
| 10865 | + } |
| 10122 | 10866 | |
| 10123 | 10867 | ret = -EOPNOTSUPP; |
| 10124 | 10868 | if (unlikely(f.file->f_op != &io_uring_fops)) |
| @@ -10192,7 +10936,8 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, · 67 unmodified lines |
| 10192 | 10936 | out: |
| 10193 | 10937 | percpu_ref_put(&ctx->refs); |
| 10194 | 10938 | out_fput: |
| 10195 | | − fdput(f); |
| 10939 | + if (!(flags & IORING_ENTER_REGISTERED_RING)) |
| 10940 | + fdput(f); |
| 10196 | 10941 | return submitted ? submitted : ret; |
| 10197 | 10942 | } |
| 10198 | 10943 | |
| @@ -10610,7 +11355,7 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params) · 411 unmodified lines |
| 10610 | 11355 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
| 10611 | 11356 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
| 10612 | 11357 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 10613 | | − IORING_SETUP_R_DISABLED)) |
| 11358 | + IORING_SETUP_R_DISABLED | IORING_SETUP_SUBMIT_ALL)) |
| 10614 | 11359 | return -EINVAL; |
| 10615 | 11360 | |
| 10616 | 11361 | return io_uring_create(entries, &p, params); |
| @@ -10960,61 +11705,6 @@ err: · 343 unmodified lines |
| 10960 | 11705 | return ret; |
| 10961 | 11706 | } |
| 10962 | 11707 | |
| 10963 | | −static bool io_register_op_must_quiesce(int op) |
| 10964 | | −{ |
| 10965 | | − switch (op) { |
| 10966 | | − case IORING_REGISTER_BUFFERS: |
| 10967 | | − case IORING_UNREGISTER_BUFFERS: |
| 10968 | | − case IORING_REGISTER_FILES: |
| 10969 | | − case IORING_UNREGISTER_FILES: |
| 10970 | | − case IORING_REGISTER_FILES_UPDATE: |
| 10971 | | − case IORING_REGISTER_PROBE: |
| 10972 | | − case IORING_REGISTER_PERSONALITY: |
| 10973 | | − case IORING_UNREGISTER_PERSONALITY: |
| 10974 | | − case IORING_REGISTER_FILES2: |
| 10975 | | − case IORING_REGISTER_FILES_UPDATE2: |
| 10976 | | − case IORING_REGISTER_BUFFERS2: |
| 10977 | | − case IORING_REGISTER_BUFFERS_UPDATE: |
| 10978 | | − case IORING_REGISTER_IOWQ_AFF: |
| 10979 | | − case IORING_UNREGISTER_IOWQ_AFF: |
| 10980 | | − case IORING_REGISTER_IOWQ_MAX_WORKERS: |
| 10981 | | − return false; |
| 10982 | | − default: |
| 10983 | | − return true; |
| 10984 | | − } |
| 10985 | | −} |
| 10986 | | − |
| 10987 | | −static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx) |
| 10988 | | −{ |
| 10989 | | − long ret; |
| 10990 | | − |
| 10991 | | − percpu_ref_kill(&ctx->refs); |
| 10992 | | − |
| 10993 | | − /* |
| 10994 | | − * Drop uring mutex before waiting for references to exit. If another |
| 10995 | | − * thread is currently inside io_uring_enter() it might need to grab the |
| 10996 | | − * uring_lock to make progress. If we hold it here across the drain |
| 10997 | | − * wait, then we can deadlock. It's safe to drop the mutex here, since |
| 10998 | | − * no new references will come in after we've killed the percpu ref. |
| 10999 | | − */ |
| 11000 | | − mutex_unlock(&ctx->uring_lock); |
| 11001 | | − do { |
| 11002 | | − ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ); |
| 11003 | | − if (ret) { |
| 11004 | | − ret = min(0L, ret); |
| 11005 | | − break; |
| 11006 | | − } |
| 11007 | | − |
| 11008 | | − ret = io_run_task_work_sig(); |
| 11009 | | − io_req_caches_free(ctx); |
| 11010 | | − } while (ret >= 0); |
| 11011 | | − mutex_lock(&ctx->uring_lock); |
| 11012 | | − |
| 11013 | | − if (ret) |
| 11014 | | − io_refs_resurrect(&ctx->refs, &ctx->ref_comp); |
| 11015 | | − return ret; |
| 11016 | | −} |
| 11017 | | − |
| 11018 | 11708 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 11019 | 11709 | void __user *arg, unsigned nr_args) |
| 11020 | 11710 | __releases(ctx->uring_lock) |
| @@ -11038,12 +11728,6 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, · 17 unmodified lines |
| 11038 | 11728 | return -EACCES; |
| 11039 | 11729 | } |
| 11040 | 11730 | |
| 11041 | | − if (io_register_op_must_quiesce(opcode)) { |
| 11042 | | − ret = io_ctx_quiesce(ctx); |
| 11043 | | − if (ret) |
| 11044 | | − return ret; |
| 11045 | | − } |
| 11046 | | − |
| 11047 | 11731 | switch (opcode) { |
| 11048 | 11732 | case IORING_REGISTER_BUFFERS: |
| 11049 | 11733 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
| @@ -11067,17 +11751,16 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, · 17 unmodified lines |
| 11067 | 11751 | ret = io_register_files_update(ctx, arg, nr_args); |
| 11068 | 11752 | break; |
| 11069 | 11753 | case IORING_REGISTER_EVENTFD: |
| 11070 | | − case IORING_REGISTER_EVENTFD_ASYNC: |
| 11071 | 11754 | ret = -EINVAL; |
| 11072 | 11755 | if (nr_args != 1) |
| 11073 | 11756 | break; |
| 11074 | | − ret = io_eventfd_register(ctx, arg); |
| 11075 | | − if (ret) |
| 11757 | + ret = io_eventfd_register(ctx, arg, 0); |
| 11758 | + break; |
| 11759 | + case IORING_REGISTER_EVENTFD_ASYNC: |
| 11760 | + ret = -EINVAL; |
| 11761 | + if (nr_args != 1) |
| 11076 | 11762 | break; |
| 11077 | | − if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 11078 | | − ctx->eventfd_async = 1; |
| 11079 | | − else |
| 11080 | | − ctx->eventfd_async = 0; |
| 11763 | + ret = io_eventfd_register(ctx, arg, 1); |
| 11081 | 11764 | break; |
| 11082 | 11765 | case IORING_UNREGISTER_EVENTFD: |
| 11083 | 11766 | ret = -EINVAL; |
| @@ -11144,16 +11827,17 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, · 60 unmodified lines |
| 11144 | 11827 | break; |
| 11145 | 11828 | ret = io_register_iowq_max_workers(ctx, arg); |
| 11146 | 11829 | break; |
| 11830 | + case IORING_REGISTER_RING_FDS: |
| 11831 | + ret = io_ringfd_register(ctx, arg, nr_args); |
| 11832 | + break; |
| 11833 | + case IORING_UNREGISTER_RING_FDS: |
| 11834 | + ret = io_ringfd_unregister(ctx, arg, nr_args); |
| 11835 | + break; |
| 11147 | 11836 | default: |
| 11148 | 11837 | ret = -EINVAL; |
| 11149 | 11838 | break; |
| 11150 | 11839 | } |
| 11151 | 11840 | |
| 11152 | | − if (io_register_op_must_quiesce(opcode)) { |
| 11153 | | − /* bring the ctx back to life */ |
| 11154 | | − percpu_ref_reinit(&ctx->refs); |
| 11155 | | − reinit_completion(&ctx->ref_comp); |
| 11156 | | − } |
| 11157 | 11841 | return ret; |
| 11158 | 11842 | } |
| 11159 | 11843 | |
| @@ -11179,8 +11863,7 @@ SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, · 19 unmodified lines |
| 11179 | 11863 | mutex_lock(&ctx->uring_lock); |
| 11180 | 11864 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 11181 | 11865 | mutex_unlock(&ctx->uring_lock); |
| 11182 | | − trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 11183 | | − ctx->cq_ev_fd != NULL, ret); |
| 11866 | + trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, ret); |
| 11184 | 11867 | out_fput: |
| 11185 | 11868 | fdput(f); |
| 11186 | 11869 | return ret; |