100 Commits
Author SHA1 Message Date
Jeff Moyer 57ff2f3218 io_uring/kbuf: check if target buffer list is still legacy on recycle
This is a backport from Ming.  Here is Ming's changelog:

JIRA: https://redhat.atlassian.net/browse/RHEL-161185
Conflicts: io_uring/kbuf.c
  - Upstream uses kfree(buf) to free the orphaned buffer; downstream
    allocates io_buffer from the io_buf_cachep slab cache, so
    kmem_cache_free(io_buf_cachep, buf) is used instead.
  - Upstream removed the req->buf_index assignment entirely; downstream
    still needs it for the next buffer selection, so it is kept but moved
    before the conditional to avoid use-after-free on buf->bgid.
  - Upstream has bl->nbufs++ tracking which does not exist downstream;
    dropped from the backport.

commit c2c185be5c85d37215397c8e8781abf0a69bec1f
Author: Jens Axboe <axboe@kernel.dk>
Date:   Thu Mar 12 08:59:25 2026 -0600

    io_uring/kbuf: check if target buffer list is still legacy on recycle

    There's a gap between when the buffer was grabbed and when it
    potentially gets recycled, where if the list is empty, someone
    could've upgraded it to a ring provided type. The legacy recycling
    doesn't check if the buffer_list still exists and is of the right
    type. Add those checks.

    Cc: stable@vger.kernel.org
    Reported-by: Keenan Dong <keenanat2000@gmail.com>
    Fixes: c7fb19428d67 ("io_uring: add support for ring mapped supplied buffers")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-04-30 20:07:57 -04:00
Jeff Moyer 9f6cd18ef1 io_uring: graduate to full support
JIRA: https://issues.redhat.com/browse/RHEL-120699
Upstream status: RHEL-only

Remove the call to mark_tech_preview.

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-03-09 11:55:21 -04:00
Jeff Moyer c4777841ef io_uring/kbuf: always use READ_ONCE() to read ring provided buffer lengths [partial]
JIRA: https://issues.redhat.com/browse/RHEL-141266
Conflicts:  We already backported this commit, but missed the portions
of the kbuf handling in kbuf.h.  This brings in those changes in a way
that aligns with the upstream code as it exists today.  This should aid
reviewers in verifying correctness.
Upstream-status: RHEL only

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-19 19:39:25 -05:00
Jeff Moyer e0de8c1954 io_uring: fix filename leak in __io_openat_prep()
JIRA: https://issues.redhat.com/browse/RHEL-141266

commit b14fad555302a2104948feaff70503b64c80ac01
Author: Prithvi Tambewagh <activprithvi@gmail.com>
Date:   Thu Dec 25 12:58:29 2025 +0530

    io_uring: fix filename leak in __io_openat_prep()
    
     __io_openat_prep() allocates a struct filename using getname(). However,
    for the condition of the file being installed in the fixed file table as
    well as having O_CLOEXEC flag set, the function returns early. At that
    point, the request doesn't have REQ_F_NEED_CLEANUP flag set. Due to this,
    the memory for the newly allocated struct filename is not cleaned up,
    causing a memory leak.
    
    Fix this by setting the REQ_F_NEED_CLEANUP for the request just after the
    successful getname() call, so that when the request is torn down, the
    filename will be cleaned up, along with other resources needing cleanup.
    
    Reported-by: syzbot+00e61c43eb5e4740438f@syzkaller.appspotmail.com
    Closes: https://syzkaller.appspot.com/bug?extid=00e61c43eb5e4740438f
    Tested-by: syzbot+00e61c43eb5e4740438f@syzkaller.appspotmail.com
    Cc: stable@vger.kernel.org
    Signed-off-by: Prithvi Tambewagh <activprithvi@gmail.com>
    Fixes: b9445598d8c6 ("io_uring: openat directly into fixed fd table")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 13:10:36 -05:00
Jeff Moyer b02004677b io_uring: fix min_wait wakeups for SQPOLL
JIRA: https://issues.redhat.com/browse/RHEL-141266

commit e15cb2200b934e507273510ba6bc747d5cde24a3
Author: Jens Axboe <axboe@kernel.dk>
Date:   Tue Dec 9 13:25:23 2025 -0700

    io_uring: fix min_wait wakeups for SQPOLL
    
    Using min_wait, two timeouts are given:
    
    1) The min_wait timeout, within which up to 'wait_nr' events are
       waited for.
    2) The overall long timeout, which is entered if no events are generated
       in the min_wait window.
    
    If the min_wait has expired, any event being posted must wake the task.
    For SQPOLL, that isn't the case, as it won't trigger the io_has_work()
    condition, as it will have already processed the task_work that happened
    when an event was posted. This causes any event to trigger post the
    min_wait to not always cause the waiting application to wakeup, and
    instead it will wait until the overall timeout has expired. This can be
    shown in a test case that has a 1 second min_wait, with a 5 second
    overall wait, even if an event triggers after 1.5 seconds:
    
    axboe@m2max-kvm /d/iouring-mre (master)> zig-out/bin/iouring
    info: MIN_TIMEOUT supported: true, features: 0x3ffff
    info: Testing: min_wait=1000ms, timeout=5s, wait_nr=4
    info: 1 cqes in 5000.2ms
    
    where the expected result should be:
    
    axboe@m2max-kvm /d/iouring-mre (master)> zig-out/bin/iouring
    info: MIN_TIMEOUT supported: true, features: 0x3ffff
    info: Testing: min_wait=1000ms, timeout=5s, wait_nr=4
    info: 1 cqes in 1500.3ms
    
    When the min_wait timeout triggers, reset the number of completions
    needed to wake the task. This should ensure that any future events will
    wake the task, regardless of how many events it originally wanted to
    wait for.
    
    Reported-by: Tip ten Brink <tip@tenbrinkmeijs.com>
    Cc: stable@vger.kernel.org
    Fixes: 1100c4a2656d ("io_uring: add support for batch wait timeout")
    Link: https://github.com/axboe/liburing/issues/1477
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 13:10:34 -05:00
Jeff Moyer 0654c2baa5 io_uring/kbuf: use READ_ONCE() for userspace-mapped memory
JIRA: https://issues.redhat.com/browse/RHEL-141266

Conflicts: RHEL does not have the incremental buffer commit introduced
in cf9536e550dd2 ("io_uring/kbuf: enable bundles for incrementally
consumed buffers"), so that hunk is elided.  There are other
differences due to missing commits 5fda51255439a ("io_uring/kbuf:
switch to storing struct io_buffer_list locally") and ab6559bdbb08f
("io_uring/kbuf: introduce struct io_br_sel").

commit 78385c7299f7514697d196b3233a91bd5e485591
Author: Caleb Sander Mateos <csander@purestorage.com>
Date:   Thu Dec 4 15:43:31 2025 -0700

    io_uring/kbuf: use READ_ONCE() for userspace-mapped memory
    
    The struct io_uring_buf elements in a buffer ring are in a memory region
    accessible from userspace. A malicious/buggy userspace program could
    therefore write to them at any time, so they should be accessed with
    READ_ONCE() in the kernel. Commit 98b6fa62c84f ("io_uring/kbuf: always
    use READ_ONCE() to read ring provided buffer lengths") already switched
    the reads of the len field to READ_ONCE(). Do the same for bid and addr.
    
    Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
    Fixes: c7fb19428d67 ("io_uring: add support for ring mapped supplied buffers")
    Cc: Joanne Koong <joannelkoong@gmail.com>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 12:00:18 -05:00
Jeff Moyer c5cbd4968b io_uring/rsrc: fix lost entries after cloned range
JIRA: https://issues.redhat.com/browse/RHEL-141266

commit 525916ce496615f531091855604eab9ca573b195
Author: Joanne Koong <joannelkoong@gmail.com>
Date:   Thu Dec 4 13:51:16 2025 -0800

    io_uring/rsrc: fix lost entries after cloned range
    
    When cloning with node replacements (IORING_REGISTER_DST_REPLACE),
    destination entries after the cloned range are not copied over.
    
    Add logic to copy them over to the new destination table.
    
    Fixes: c1329532d5aa ("io_uring/rsrc: allow cloning with node replacements")
    Cc: stable@vger.kernel.org
    Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 12:00:18 -05:00
Jeff Moyer d9740e280a io_uring/poll: correctly handle io_poll_add() return value on update
JIRA: https://issues.redhat.com/browse/RHEL-141266

commit 84230ad2d2afbf0c44c32967e525c0ad92e26b4e
Author: Jens Axboe <axboe@kernel.dk>
Date:   Mon Dec 1 13:25:22 2025 -0700

    io_uring/poll: correctly handle io_poll_add() return value on update
    
    When the core of io_uring was updated to handle completions
    consistently and with fixed return codes, the POLL_REMOVE opcode
    with updates got slightly broken. If a POLL_ADD is pending and
    then POLL_REMOVE is used to update the events of that request, if that
    update causes the POLL_ADD to now trigger, then that completion is lost
    and a CQE is never posted.
    
    Additionally, ensure that if an update does cause an existing POLL_ADD
    to complete, that the completion value isn't always overwritten with
    -ECANCELED. For that case, whatever io_poll_add() set the value to
    should just be retained.
    
    Cc: stable@vger.kernel.org
    Fixes: 97b388d70b53 ("io_uring: handle completions in the core")
    Reported-by: syzbot+641eec6b7af1f62f2b99@syzkaller.appspotmail.com
    Tested-by: syzbot+641eec6b7af1f62f2b99@syzkaller.appspotmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 12:00:18 -05:00
Jeff Moyer 3ec7546fbf io_uring: use WRITE_ONCE for user shared memory
JIRA: https://issues.redhat.com/browse/RHEL-141266

commit 93e197e524b14d185d011813b72773a1a49d932d
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Thu Nov 6 12:58:19 2025 +0000

    io_uring: use WRITE_ONCE for user shared memory
    
    IORING_SETUP_NO_MMAP rings remain user accessible even before the ctx
    setup is finalised, so use WRITE_ONCE consistently when initialising
    rings.
    
    Fixes: 03d89a2de25bb ("io_uring: support for user allocated memory for rings/sqes")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 12:00:17 -05:00
Jeff Moyer 5836cc7a66 io_uring: correct __must_hold annotation in io_install_fixed_file
JIRA: https://issues.redhat.com/browse/RHEL-141266

commit c5efc6a0b3940381d67887302ddb87a5cf623685
Author: Alok Tiwari <alok.a.tiwari@oracle.com>
Date:   Thu Oct 23 04:55:24 2025 -0700

    io_uring: correct __must_hold annotation in io_install_fixed_file
    
    The __must_hold annotation references &req->ctx->uring_lock, but req
    is not in scope in io_install_fixed_file. This change updates the
    annotation to reference the correct ctx->uring_lock.
    improving code clarity.
    
    Fixes: f110ed8498af ("io_uring: split out fixed file installation and removal")
    Signed-off-by: Alok Tiwari <alok.a.tiwari@oracle.com>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 12:00:17 -05:00
Jeff Moyer b3f121e275 io_uring/sqpoll: be smarter on when to update the stime usage
JIRA: https://issues.redhat.com/browse/RHEL-141266

commit a94e0657269c5b8e1a90b17aa2c048b3d276e16d
Author: Jens Axboe <axboe@kernel.dk>
Date:   Tue Oct 21 11:44:39 2025 -0600

    io_uring/sqpoll: be smarter on when to update the stime usage
    
    The current approach is a bit naive, and hence calls the time querying
    way too often. Only start the "doing work" timer when there's actual
    work to do, and then use that information to terminate (and account) the
    work time once done. This greatly reduces the frequency of these calls,
    when they cannot have changed anyway.
    
    Running a basic random reader that is setup to use SQPOLL, a profile
    before this change shows these as the top cycle consumers:
    
    +   32.60%  iou-sqp-1074  [kernel.kallsyms]  [k] thread_group_cputime_adjusted
    +   19.97%  iou-sqp-1074  [kernel.kallsyms]  [k] thread_group_cputime
    +   12.20%  io_uring      io_uring           [.] submitter_uring_fn
    +    4.13%  iou-sqp-1074  [kernel.kallsyms]  [k] getrusage
    +    2.45%  iou-sqp-1074  [kernel.kallsyms]  [k] io_submit_sqes
    +    2.18%  iou-sqp-1074  [kernel.kallsyms]  [k] __pi_memset_generic
    +    2.09%  iou-sqp-1074  [kernel.kallsyms]  [k] cputime_adjust
    
    and after this change, top of profile looks as follows:
    
    +   36.23%  io_uring     io_uring           [.] submitter_uring_fn
    +   23.26%  iou-sqp-819  [kernel.kallsyms]  [k] io_sq_thread
    +   10.14%  iou-sqp-819  [kernel.kallsyms]  [k] io_sq_tw
    +    6.52%  iou-sqp-819  [kernel.kallsyms]  [k] tctx_task_work_run
    +    4.82%  iou-sqp-819  [kernel.kallsyms]  [k] nvme_submit_cmds.part.0
    +    2.91%  iou-sqp-819  [kernel.kallsyms]  [k] io_submit_sqes
    [...]
         0.02%  iou-sqp-819  [kernel.kallsyms]  [k] cputime_adjust
    
    where it's spending the cycles on things that actually matter.
    
    Reported-by: Fengnan Chang <changfengnan@bytedance.com>
    Cc: stable@vger.kernel.org
    Fixes: 3fcb9d17206e ("io_uring/sqpoll: statistics of the true utilization of sq threads")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 12:00:17 -05:00
Jeff Moyer 57a02079d2 io_uring/sqpoll: switch away from getrusage() for CPU accounting
JIRA: https://issues.redhat.com/browse/RHEL-141266
Conflicts: Contextual differences due to missing commit 3a3f61ce5e0b4
("exec: Make sure task->comm is always NUL-terminated").

commit 8ac9b0d33e5c0a995338ee5f25fe1b6ff7d97f65
Author: Jens Axboe <axboe@kernel.dk>
Date:   Tue Oct 21 07:16:08 2025 -0600

    io_uring/sqpoll: switch away from getrusage() for CPU accounting
    
    getrusage() does a lot more than what the SQPOLL accounting needs, the
    latter only cares about (and uses) the stime. Rather than do a full
    RUSAGE_SELF summation, just query the used stime instead.
    
    Cc: stable@vger.kernel.org
    Fixes: 3fcb9d17206e ("io_uring/sqpoll: statistics of the true utilization of sq threads")
    Reviewed-by: Gabriel Krisman Bertazi <krisman@suse.de>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 12:00:17 -05:00
Jeff Moyer d3f8b96536 io_uring: fix incorrect io_kiocb reference in io_link_skb
JIRA: https://issues.redhat.com/browse/RHEL-141266

commit 2c139a47eff8de24e3350dadb4c9d5e3426db826
Author: Yang Xiuwei <yangxiuwei@kylinos.cn>
Date:   Fri Sep 19 17:03:52 2025 +0800

    io_uring: fix incorrect io_kiocb reference in io_link_skb
    
    In io_link_skb function, there is a bug where prev_notif is incorrectly
    assigned using 'nd' instead of 'prev_nd'. This causes the context
    validation check to compare the current notification with itself instead
    of comparing it with the previous notification.
    
    Fix by using the correct prev_nd parameter when obtaining prev_notif.
    
    Signed-off-by: Yang Xiuwei <yangxiuwei@kylinos.cn>
    Reviewed-by: Pavel Begunkov <asml.silence@gmail.com>
    Fixes: 6fe4220912d19 ("io_uring/notif: implement notification stacking")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 12:00:17 -05:00
Jeff Moyer bc9d4addb5 io_uring: include dying ring in task_work "should cancel" state
JIRA: https://issues.redhat.com/browse/RHEL-141266

commit 3539b1467e94336d5854ebf976d9627bfb65d6c3
Author: Jens Axboe <axboe@kernel.dk>
Date:   Thu Sep 18 10:21:14 2025 -0600

    io_uring: include dying ring in task_work "should cancel" state
    
    When running task_work for an exiting task, rather than perform the
    issue retry attempt, the task_work is canceled. However, this isn't
    done for a ring that has been closed. This can lead to requests being
    successfully completed post the ring being closed, which is somewhat
    confusing and surprising to an application.
    
    Rather than just check the task exit state, also include the ring
    ref state in deciding whether or not to terminate a given request when
    run from task_work.
    
    Cc: stable@vger.kernel.org # 6.1+
    Link: https://github.com/axboe/liburing/discussions/1459
    Reported-by: Benedek Thaler <thaler@thaler.hu>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 12:00:17 -05:00
Jeff Moyer b9b48a4039 io_uring/net: commit partial buffers on retry
JIRA: https://issues.redhat.com/browse/RHEL-141266

commit 41b70df5b38bc80967d2e0ed55cc3c3896bba781
Author: Jens Axboe <axboe@kernel.dk>
Date:   Tue Aug 12 08:30:11 2025 -0600

    io_uring/net: commit partial buffers on retry
    
    Ring provided buffers are potentially only valid within the single
    execution context in which they were acquired. io_uring deals with this
    and invalidates them on retry. But on the networking side, if
    MSG_WAITALL is set, or if the socket is of the streaming type and too
    little was processed, then it will hang on to the buffer rather than
    recycle or commit it. This is problematic for two reasons:
    
    1) If someone unregisters the provided buffer ring before a later retry,
       then the req->buf_list will no longer be valid.
    
    2) If multiple sockers are using the same buffer group, then multiple
       receives can consume the same memory. This can cause data corruption
       in the application, as either receive could land in the same
       userspace buffer.
    
    Fix this by disallowing partial retries from pinning a provided buffer
    across multiple executions, if ring provided buffers are used.
    
    Cc: stable@vger.kernel.org
    Reported-by: pt x <superman.xpt@gmail.com>
    Fixes: c56e022c0a27 ("io_uring: add support for user mapped provided buffer ring")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 12:00:08 -05:00
Jeff Moyer 076057c8d8 io_uring: finish IOU_OK -> IOU_COMPLETE transition
JIRA: https://issues.redhat.com/browse/RHEL-141266
Conflicts: RHEL does not have epoll_wait or pipe support, so those
hunks are elided.

commit 8bb9d6ccd36062d16baa707b759809e1f494017e
Author: Jens Axboe <axboe@kernel.dk>
Date:   Thu May 8 14:48:33 2025 -0600

    io_uring: finish IOU_OK -> IOU_COMPLETE transition
    
    IOU_COMPLETE is more descriptive, in that it explicitly says that the
    return value means "please post a completion for this request". This
    patch completes the transition from IOU_OK to IOU_COMPLETE, replacing
    existing IOU_OK users.
    
    This is a purely mechanical change.
    
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 11:59:08 -05:00
Jeff Moyer 232f932876 io_uring: unify STOP_MULTISHOT with IOU_OK
JIRA: https://issues.redhat.com/browse/RHEL-141266
Conflicts: RHEL does not have recvzc support, so those hunks are
elided.  RHEL is also missing commit 185523ebc8534 ("io_uring/net:
canonise accept mshot handling"), which changes context in the
io_accept hunk.

commit 5027d02452c982bdc7b36205c66466ebd7e6ee17
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Sat Mar 8 17:19:33 2025 +0000

    io_uring: unify STOP_MULTISHOT with IOU_OK
    
    IOU_OK means that the request ownership is now handed back to core
    io_uring and it has to complete it using the result provided in
    req->cqe. Same is true for multishot and IOU_STOP_MULTISHOT.
    
    Rename it into IOU_COMPLETE to avoid confusion and use for both modes.
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/e6a5b2edb0eb9558acb1c8f1db38ac45fee95491.1741453534.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 11:38:34 -05:00
Jeff Moyer 7f86d0b0a6 io_uring: return -EAGAIN to continue multishot
JIRA: https://issues.redhat.com/browse/RHEL-141266
Conflicts: RHEL does not yet have io_recvzc support, added via upstream
commit 11ed914bbf948 ("io_uring/zcrx: add io_recvzc request"), so the
hunk touching that function was dropped.

commit 7a9dcb05f5501b07a2ef7d0ef743f4f17e9f3055
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Sat Mar 8 17:19:32 2025 +0000

    io_uring: return -EAGAIN to continue multishot
    
    Multishot errors can be mapped 1:1 to normal errors, but there are not
    identical. It leads to a peculiar situation where all multishot requests
    has to check in what context they're run and return different codes.
    
    Unify them starting with EAGAIN / IOU_ISSUE_SKIP_COMPLETE(EIOCBQUEUED)
    pair, which mean that core io_uring still owns the request and it should
    be retried. In case of multishot it's naturally just continues to poll,
    otherwise it might poll, use iowq or do any other kind of allowed
    blocking. Introduce IOU_RETRY aliased to -EAGAIN for that.
    
    Apart from obvious upsides, multishot can now also check for misuse of
    IOU_ISSUE_SKIP_COMPLETE.
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/da117b79ce72ecc3ab488c744e29fae9ba54e23b.1741453534.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2026-01-14 10:35:06 -05:00
Jeff Moyer bcf53451a9 io_uring: fix incorrect unlikely() usage in io_waitid_prep()
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 4ec703ec0c384a2199808c4eb2e9037236285a8d
Author: Alok Tiwari <alok.a.tiwari@oracle.com>
Date:   Sat Oct 18 12:32:54 2025 -0700

    io_uring: fix incorrect unlikely() usage in io_waitid_prep()
    
    The negation operator is incorrectly placed outside the unlikely()
    macro:
    
        if (!unlikely(iwa))
    
    This inverts the compiler branch prediction hint, marking the NULL case
    as likely instead of unlikely. The intent is to indicate that allocation
    failures are rare, consistent with common kernel patterns.
    
     Moving the negation inside unlikely():
    
        if (unlikely(!iwa))
    
    Fixes: 2b4fc4cd43f2 ("io_uring/waitid: setup async data in the prep handler")
    Signed-off-by: Alok Tiwari <alok.a.tiwari@oracle.com>
    Reviewed-by: Gabriel Krisman Bertazi <krisman@suse.de>
    Reviewed-by: Caleb Sander Mateos <csander@purestorage.com>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:33:38 -04:00
Jeff Moyer 8235bf402a io_uring: protect mem region deregistration
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit be7cab44ed099566c605a8dac686c3254db01b35
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Wed Oct 15 13:07:23 2025 +0100

    io_uring: protect mem region deregistration
    
    io_create_region_mmap_safe() protects publishing of a region against
    concurrent mmap calls, however we should also protect against it when
    removing a region. There is a gap io_register_mem_region() where it
    safely publishes a region, but then copy_to_user goes wrong and it
    unsafely frees the region.
    
    Cc: stable@vger.kernel.org
    Fixes: 087f997870a94 ("io_uring/memmap: implement mmap for regions")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:33:28 -04:00
Jeff Moyer aec6b960a1 Revert "io_uring/rw: drop -EOPNOTSUPP check in __io_complete_rw_common()"
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 927069c4ac2cd1a37efa468596fb5b8f86db9df0
Author: Jens Axboe <axboe@kernel.dk>
Date:   Mon Oct 13 12:05:31 2025 -0600

    Revert "io_uring/rw: drop -EOPNOTSUPP check in __io_complete_rw_common()"
    
    This reverts commit 90bfb28d5fa8127a113a140c9791ea0b40ab156a.
    
    Kevin reports that this commit causes an issue for him with LVM
    snapshots, most likely because of turning off NOWAIT support while a
    snapshot is being created. This makes -EOPNOTSUPP bubble back through
    the completion handler, where io_uring read/write handling should just
    retry it.
    
    Reinstate the previous check removed by the referenced commit.
    
    Cc: stable@vger.kernel.org
    Fixes: 90bfb28d5fa8 ("io_uring/rw: drop -EOPNOTSUPP check in __io_complete_rw_common()")
    Reported-by: Salvatore Bonaccorso <carnil@debian.org>
    Reported-by: Kevin Lumik <kevin@xf.ee>
    Link: https://lore.kernel.org/io-uring/cceb723c-051b-4de2-9a4c-4aa82e1619ee@kernel.dk/
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:33:13 -04:00
Jeff Moyer 5490540d58 io_uring/msg_ring: kill alloc_cache for io_kiocb allocations
JIRA: https://issues.redhat.com/browse/RHEL-105612
Conflicts: RHEL does not have commit 3a4689ac109f1 ("io_uring/cmd: add
iovec cache for commands"), which results in a context difference.

commit df8922afc37aa2111ca79a216653a629146763ad
Author: Jens Axboe <axboe@kernel.dk>
Date:   Thu Sep 18 13:59:15 2025 -0600

    io_uring/msg_ring: kill alloc_cache for io_kiocb allocations
    
    A recent commit:
    
    fc582cd26e88 ("io_uring/msg_ring: ensure io_kiocb freeing is deferred for RCU")
    
    fixed an issue with not deferring freeing of io_kiocb structs that
    msg_ring allocates to after the current RCU grace period. But this only
    covers requests that don't end up in the allocation cache. If a request
    goes into the alloc cache, it can get reused before it is sane to do so.
    A recent syzbot report would seem to indicate that there's something
    there, however it may very well just be because of the KASAN poisoning
    that the alloc_cache handles manually.
    
    Rather than attempt to make the alloc_cache sane for that use case, just
    drop the usage of the alloc_cache for msg_ring request payload data.
    
    Fixes: 50cf5f3842af ("io_uring/msg_ring: add an alloc cache for io_kiocb entries")
    Link: https://lore.kernel.org/io-uring/68cc2687.050a0220.139b6.0005.GAE@google.com/
    Reported-by: syzbot+baa2e0f4e02df602583e@syzkaller.appspotmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:31 -04:00
Jeff Moyer 9c77a18428 io_uring: rename the data cmd cache
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 575e7b0629d4bd485517c40ff20676180476f5f9
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Wed Mar 19 06:12:47 2025 +0000

    io_uring: rename the data cmd cache
    
    Pick a more descriptive name for the cmd async data cache.
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/20250319061251.21452-2-sidong.yang@furiosa.ai
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:31 -04:00
Jeff Moyer 02af8d4ebe io_uring: deduplicate caches deallocation
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 40b991837f3293317c9845b549e10600e9d54611
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Fri Jan 31 17:27:02 2025 +0000

    io_uring: deduplicate caches deallocation
    
    Add a function that frees all ring caches since we already have two
    spots repeating the same thing and it's easy to miss it and change only
    one of them.
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/b6b0125677c58bdff99eda91ab320137406e8562.1738342562.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:31 -04:00
Jeff Moyer 8034003529 io_uring/kbuf: always use READ_ONCE() to read ring provided buffer lengths
JIRA: https://issues.redhat.com/browse/RHEL-105612
Conflicts: RHEL does not have commit cf9536e550dd2 ("io_uring/kbuf:
enable bundles for incrementally consumed buffers"), so that hunk is
elided.

commit 98b6fa62c84f2e129161e976a5b9b3cb4ccd117b
Author: Jens Axboe <axboe@kernel.dk>
Date:   Wed Aug 27 15:27:30 2025 -0600

    io_uring/kbuf: always use READ_ONCE() to read ring provided buffer lengths
    
    Since the buffers are mapped from userspace, it is prudent to use
    READ_ONCE() to read the value into a local variable, and use that for
    any other actions taken. Having a stable read of the buffer length
    avoids worrying about it changing after checking, or being read multiple
    times.
    
    Similarly, the buffer may well change in between it being picked and
    being committed. Ensure the looping for incremental ring buffer commit
    stops if it hits a zero sized buffer, as no further progress can be made
    at that point.
    
    Fixes: ae98dbf43d75 ("io_uring/kbuf: add support for incremental buffer consumption")
    Link: https://lore.kernel.org/io-uring/tencent_000C02641F6250C856D0C26228DE29A3D30A@qq.com/
    Reported-by: Qingyue Zhang <chunzhennn@qq.com>
    Reported-by: Suoxing Zhang <aftern00n@qq.com>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:31 -04:00
Jeff Moyer 76632ef268 fs: add a FMODE_ flag to indicate IOCB_HAS_METADATA availability
JIRA: https://issues.redhat.com/browse/RHEL-105612
Conflicts: Upstream reworked the bit definitions.  I assigned the first
available bit to FMODE_HAS_METADATA (0x800000).

commit d072148a8631f102de60ed5a3a827e85d09d24f0
Author: Christoph Hellwig <hch@lst.de>
Date:   Tue Aug 19 10:25:00 2025 +0200

    fs: add a FMODE_ flag to indicate IOCB_HAS_METADATA availability
    
    Currently the kernel will happily route io_uring requests with metadata
    to file operations that don't support it.  Add a FMODE_ flag to guard
    that.
    
    Fixes: 4de2ce04c862 ("fs: introduce IOCB_HAS_METADATA for metadata")
    Signed-off-by: Christoph Hellwig <hch@lst.de>
    Link: https://lore.kernel.org/20250819082517.2038819-2-hch@lst.de
    Signed-off-by: Christian Brauner <brauner@kernel.org>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:12 -04:00
Jeff Moyer 3534dd1862 io_uring/net: always use current transfer count for buffer put
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 51a4598ad5d9eb6be4ec9ba65bbfdf0ac302eb2e
Author: Jens Axboe <axboe@kernel.dk>
Date:   Fri Jun 20 07:41:21 2025 -0600

    io_uring/net: always use current transfer count for buffer put
    
    A previous fix corrected the retry condition for when to continue a
    current bundle, but it missed that the current (not the total) transfer
    count also applies to the buffer put. If not, then for incrementally
    consumed buffer rings repeated completions on the same request may end
    up over consuming.
    
    Reported-by: Roy Tang (ErgoniaTrading) <royonia@ergonia.io>
    Cc: stable@vger.kernel.org
    Fixes: 3a08988123c8 ("io_uring/net: only retry recv bundle for a full transfer")
    Link: https://github.com/axboe/liburing/issues/1423
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:12 -04:00
Jeff Moyer ce5094e9b3 io_uring/memmap: cast nr_pages to size_t before shifting
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 33503c083fda048c77903460ac0429e1e2c0e341
Author: Jens Axboe <axboe@kernel.dk>
Date:   Fri Aug 8 06:35:14 2025 -0600

    io_uring/memmap: cast nr_pages to size_t before shifting
    
    If the allocated size exceeds UINT_MAX, then it's necessary to cast
    the mr->nr_pages value to size_t to prevent it from overflowing. In
    practice this isn't much of a concern as the required memory size will
    have been validated upfront, and accounted to the user. And > 4GB sizes
    will be necessary to make the lack of a cast a problem, which greatly
    exceeds normal user locked_vm settings that are generally in the kb to
    mb range. However, if root is used, then accounting isn't done, and
    then it's possible to hit this issue.
    
    Link: https://lore.kernel.org/all/6895b298.050a0220.7f033.0059.GAE@google.com/
    Cc: stable@vger.kernel.org
    Reported-by: syzbot+23727438116feb13df15@syzkaller.appspotmail.com
    Fixes: 087f997870a9 ("io_uring/memmap: implement mmap for regions")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:12 -04:00
Jeff Moyer 7b51092c85 io_uring: fix breakage in EXPERT menu
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit d1fbe1ebf4a12cabd7945335d5e47718cb2bef99
Author: Randy Dunlap <rdunlap@infradead.org>
Date:   Sat Jul 19 18:04:56 2025 -0700

    io_uring: fix breakage in EXPERT menu
    
    Add a dependency for IO_URING for the GCOV_PROFILE_URING symbol.
    
    Without this patch the EXPERT config menu ends with
    "Enable IO uring support" and the menu prompts for
    GCOV_PROFILE_URING and IO_URING_MOCK_FILE are not subordinate to it.
    This causes all of the EXPERT Kconfig options that follow
    GCOV_PROFILE_URING to be display in the "upper" menu (General setup),
    just following the EXPERT menu.
    
    Fixes: 1802656ef890 ("io_uring: add GCOV_PROFILE_URING Kconfig option")
    Signed-off-by: Randy Dunlap <rdunlap@infradead.org>
    Cc: Jens Axboe <axboe@kernel.dk>
    Cc: Andrew Morton <akpm@linux-foundation.org>
    Cc: Masahiro Yamada <masahiroy@kernel.org>
    Cc: io-uring@vger.kernel.org
    Link: https://lore.kernel.org/r/20250720010456.2945344-1-rdunlap@infradead.org
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:12 -04:00
Jeff Moyer 5f6c624633 io_uring/poll: fix POLLERR handling
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit c7cafd5b81cc07fb402e3068d134c21e60ea688c
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Wed Jul 16 17:20:17 2025 +0100

    io_uring/poll: fix POLLERR handling
    
    8c8492ca64e7 ("io_uring/net: don't retry connect operation on EPOLLERR")
    is a little dirty hack that
    1) wrongfully assumes that POLLERR equals to a failed request, which
    breaks all POLLERR users, e.g. all error queue recv interfaces.
    2) deviates the connection request behaviour from connect(2), and
    3) racy and solved at a wrong level.
    
    Nothing can be done with 2) now, and 3) is beyond the scope of the
    patch. At least solve 1) by moving the hack out of generic poll handling
    into io_connect().
    
    Cc: stable@vger.kernel.org
    Fixes: 8c8492ca64e79 ("io_uring/net: don't retry connect operation on EPOLLERR")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/3dc89036388d602ebd84c28e5042e457bdfc952b.1752682444.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:12 -04:00
Jeff Moyer f1020ad480 io_uring/msg_ring: ensure io_kiocb freeing is deferred for RCU
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit fc582cd26e888b0652bc1494f252329453fd3b23
Author: Jens Axboe <axboe@kernel.dk>
Date:   Tue Jul 8 11:00:32 2025 -0600

    io_uring/msg_ring: ensure io_kiocb freeing is deferred for RCU
    
    syzbot reports that defer/local task_work adding via msg_ring can hit
    a request that has been freed:
    
    CPU: 1 UID: 0 PID: 19356 Comm: iou-wrk-19354 Not tainted 6.16.0-rc4-syzkaller-00108-g17bbde2e1716 #0 PREEMPT(full)
    Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/07/2025
    Call Trace:
     <TASK>
     dump_stack_lvl+0x189/0x250 lib/dump_stack.c:120
     print_address_description mm/kasan/report.c:408 [inline]
     print_report+0xd2/0x2b0 mm/kasan/report.c:521
     kasan_report+0x118/0x150 mm/kasan/report.c:634
     io_req_local_work_add io_uring/io_uring.c:1184 [inline]
     __io_req_task_work_add+0x589/0x950 io_uring/io_uring.c:1252
     io_msg_remote_post io_uring/msg_ring.c:103 [inline]
     io_msg_data_remote io_uring/msg_ring.c:133 [inline]
     __io_msg_ring_data+0x820/0xaa0 io_uring/msg_ring.c:151
     io_msg_ring_data io_uring/msg_ring.c:173 [inline]
     io_msg_ring+0x134/0xa00 io_uring/msg_ring.c:314
     __io_issue_sqe+0x17e/0x4b0 io_uring/io_uring.c:1739
     io_issue_sqe+0x165/0xfd0 io_uring/io_uring.c:1762
     io_wq_submit_work+0x6e9/0xb90 io_uring/io_uring.c:1874
     io_worker_handle_work+0x7cd/0x1180 io_uring/io-wq.c:642
     io_wq_worker+0x42f/0xeb0 io_uring/io-wq.c:696
     ret_from_fork+0x3fc/0x770 arch/x86/kernel/process.c:148
     ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
     </TASK>
    
    which is supposed to be safe with how requests are allocated. But msg
    ring requests alloc and free on their own, and hence must defer freeing
    to a sane time.
    
    Add an rcu_head and use kfree_rcu() in both spots where requests are
    freed. Only the one in io_msg_tw_complete() is strictly required as it
    has been visible on the other ring, but use it consistently in the other
    spot as well.
    
    This should not cause any other issues outside of KASAN rightfully
    complaining about it.
    
    Link: https://lore.kernel.org/io-uring/686cd2ea.a00a0220.338033.0007.GAE@google.com/
    Reported-by: syzbot+54cbbfb4db9145d26fc2@syzkaller.appspotmail.com
    Cc: stable@vger.kernel.org
    Fixes: 0617bb500bfa ("io_uring/msg_ring: improve handling of target CQE posting")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:12 -04:00
Jeff Moyer 6fd07a0a17 io_uring/rw: cast rw->flags assignment to rwf_t
JIRA: https://issues.redhat.com/browse/RHEL-105612
Conflicts: context differences.

commit 825aea662b492571877b32aeeae13689fd9fbee4
Author: Jens Axboe <axboe@kernel.dk>
Date:   Mon Jul 7 16:46:30 2025 -0600

    io_uring/rw: cast rw->flags assignment to rwf_t
    
    kernel test robot reports that a recent change of the sqe->rw_flags
    field throws a sparse warning on 32-bit archs:
    
    >> io_uring/rw.c:291:19: sparse: sparse: incorrect type in assignment (different base types) @@     expected restricted __kernel_rwf_t [usertype] flags @@     got unsigned int @@
       io_uring/rw.c:291:19: sparse:     expected restricted __kernel_rwf_t [usertype] flags
       io_uring/rw.c:291:19: sparse:     got unsigned int
    
    Force cast it to rwf_t to silence that new sparse warning.
    
    Fixes: cf73d9970ea4 ("io_uring: don't use int for ABI")
    Reported-by: kernel test robot <lkp@intel.com>
    Closes: https://lore.kernel.org/oe-kbuild-all/202507032211.PwSNPNSP-lkp@intel.com/
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:12 -04:00
Jeff Moyer 2af1fe4cf0 io_uring: don't use int for ABI
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit cf73d9970ea4f8cace5d8f02d2565a2723003112
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Wed Jul 2 21:31:54 2025 +0100

    io_uring: don't use int for ABI
    
    __kernel_rwf_t is defined as int, the actual size of which is
    implementation defined. It won't go well if some compiler / archs
    ever defines it as i64, so replace it with __u32, hoping that
    there is no one using i16 for it.
    
    Cc: stable@vger.kernel.org
    Fixes: 2b188cc1bb ("Add io_uring IO interface")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/47c666c4ee1df2018863af3a2028af18feef11ed.1751412511.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:11 -04:00
Jeff Moyer b1b4e690fa io_uring/kbuf: flag partial buffer mappings
JIRA: https://issues.redhat.com/browse/RHEL-105612
Conflicts: context differences due to missing upstream series
https://lore.kernel.org/all/cover.1743437358.git.asml.silence@gmail.com/.

commit 178b8ff66ff827c41b4fa105e9aabb99a0b5c537
Author: Jens Axboe <axboe@kernel.dk>
Date:   Thu Jun 26 12:17:48 2025 -0600

    io_uring/kbuf: flag partial buffer mappings
    
    A previous commit aborted mapping more for a non-incremental ring for
    bundle peeking, but depending on where in the process this peeking
    happened, it would not necessarily prevent a retry by the user. That can
    create gaps in the received/read data.
    
    Add struct buf_sel_arg->partial_map, which can pass this information
    back. The networking side can then map that to internal state and use it
    to gate retry as well.
    
    Since this necessitates a new flag, change io_sr_msg->retry to a
    retry_flags member, and store both the retry and partial map condition
    in there.
    
    Cc: stable@vger.kernel.org
    Fixes: 26ec15e4b0c1 ("io_uring/kbuf: don't truncate end buffer for multiple buffer peeks")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:11 -04:00
Jeff Moyer 40dd9b2b12 io_uring/net: mark iov as dynamically allocated even for single segments
JIRA: https://issues.redhat.com/browse/RHEL-105612
Conflicts: RHEL does not have commit be7052a4b5a85 ("io_uring/net:
convert to struct iou_vec").  Fix up the patch to apply on our older
version of the code.

commit 9a709b7e98e6fa51600b5f2d24c5068efa6d39de
Author: Jens Axboe <axboe@kernel.dk>
Date:   Wed Jun 25 10:17:06 2025 -0600

    io_uring/net: mark iov as dynamically allocated even for single segments
    
    A bigger array of vecs could've been allocated, but
    io_ring_buffers_peek() still decided to cap the mapped range depending
    on how much data was available. Hence don't rely on the segment count
    to know if the request should be marked as needing cleanup, always
    check upfront if the iov array is different than the fast_iov array.
    
    Fixes: 26ec15e4b0c1 ("io_uring/kbuf: don't truncate end buffer for multiple buffer peeks")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:11 -04:00
Jeff Moyer f1e475bbf8 io_uring/rsrc: don't rely on user vaddr alignment
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 3a3c6d61577dbb23c09df3e21f6f9eda1ecd634b
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Tue Jun 24 14:40:34 2025 +0100

    io_uring/rsrc: don't rely on user vaddr alignment
    
    There is no guaranteed alignment for user pointers, however the
    calculation of an offset of the first page into a folio after coalescing
    uses some weird bit mask logic, get rid of it.
    
    Cc: stable@vger.kernel.org
    Reported-by: David Hildenbrand <david@redhat.com>
    Fixes: a8edbb424b139 ("io_uring/rsrc: enable multi-hugepage buffer coalescing")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/io-uring/e387b4c78b33f231105a601d84eefd8301f57954.1750771718.git.asml.silence@gmail.com/
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:11 -04:00
Jeff Moyer 776b391a8a io_uring/rsrc: fix folio unpinning
JIRA: https://issues.redhat.com/browse/RHEL-105612
Conflicts: This patchset pulls in fixes on top of 6.14.  This particular
change does not apply cleanly because there were other changes in this
area that are not worth pulling into a fixes patchset.  The fix-up is
pretty straight-forward.

commit 5afb4bf9fc62d828647647ec31745083637132e4
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Tue Jun 24 14:40:33 2025 +0100

    io_uring/rsrc: fix folio unpinning
    
    syzbot complains about an unmapping failure:
    
    [  108.070381][   T14] kernel BUG at mm/gup.c:71!
    [  108.070502][   T14] Internal error: Oops - BUG: 00000000f2000800 [#1]  SMP
    [  108.123672][   T14] Hardware name: QEMU KVM Virtual Machine, BIOS edk2-20250221-8.fc42 02/21/2025
    [  108.127458][   T14] Workqueue: iou_exit io_ring_exit_work
    [  108.174205][   T14] Call trace:
    [  108.175649][   T14]  sanity_check_pinned_pages+0x7cc/0x7d0 (P)
    [  108.178138][   T14]  unpin_user_page+0x80/0x10c
    [  108.180189][   T14]  io_release_ubuf+0x84/0xf8
    [  108.182196][   T14]  io_free_rsrc_node+0x250/0x57c
    [  108.184345][   T14]  io_rsrc_data_free+0x148/0x298
    [  108.186493][   T14]  io_sqe_buffers_unregister+0x84/0xa0
    [  108.188991][   T14]  io_ring_ctx_free+0x48/0x480
    [  108.191057][   T14]  io_ring_exit_work+0x764/0x7d8
    [  108.193207][   T14]  process_one_work+0x7e8/0x155c
    [  108.195431][   T14]  worker_thread+0x958/0xed8
    [  108.197561][   T14]  kthread+0x5fc/0x75c
    [  108.199362][   T14]  ret_from_fork+0x10/0x20
    
    We can pin a tail page of a folio, but then io_uring will try to unpin
    the head page of the folio. While it should be fine in terms of keeping
    the page actually alive, mm folks say it's wrong and triggers a debug
    warning. Use unpin_user_folio() instead of unpin_user_page*.
    
    Cc: stable@vger.kernel.org
    Debugged-by: David Hildenbrand <david@redhat.com>
    Reported-by: syzbot+1d335893772467199ab6@syzkaller.appspotmail.com
    Closes: https://lkml.kernel.org/r/683f1551.050a0220.55ceb.0017.GAE@google.com
    Fixes: a8edbb424b139 ("io_uring/rsrc: enable multi-hugepage buffer coalescing")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/io-uring/a28b0f87339ac2acf14a645dad1e95bbcbf18acd.1750771718.git.asml.silence@gmail.com/
    [axboe: adapt to current tree, massage commit message]
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:11 -04:00
Jeff Moyer eb8557bb38 io_uring: fix potential page leak in io_sqe_buffer_register()
JIRA: https://issues.redhat.com/browse/RHEL-105612
Conflicts: There are context differences due to upstream commit
ed9f3112a8a8f ("io_uring: cache nodes and mapped buffers").

commit e1c75831f682eef0f68b35723437146ed86070b1
Author: Penglei Jiang <superman.xpt@gmail.com>
Date:   Tue Jun 17 09:56:44 2025 -0700

    io_uring: fix potential page leak in io_sqe_buffer_register()
    
    If allocation of the 'imu' fails, then the existing pages aren't
    unpinned in the error path. This is mostly a theoretical issue,
    requiring fault injection to hit.
    
    Move unpin_user_pages() to unified error handling to fix the page leak
    issue.
    
    Fixes: d8c2237d0aa9 ("io_uring: add io_pin_pages() helper")
    Signed-off-by: Penglei Jiang <superman.xpt@gmail.com>
    Link: https://lore.kernel.org/r/20250617165644.79165-1-superman.xpt@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:11 -04:00
Jeff Moyer a8cb90f05d io_uring/sqpoll: don't put task_struct on tctx setup failure
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit f2320f1dd6f6f82cb2c7aff23a12bab537bdea89
Author: Jens Axboe <axboe@kernel.dk>
Date:   Tue Jun 17 06:43:18 2025 -0600

    io_uring/sqpoll: don't put task_struct on tctx setup failure
    
    A recent commit moved the error handling of sqpoll thread and tctx
    failures into the thread itself, as part of fixing an issue. However, it
    missed that tctx allocation may also fail, and that
    io_sq_offload_create() does its own error handling for the task_struct
    in that case.
    
    Remove the manual task putting in io_sq_offload_create(), as
    io_sq_thread() will notice that the tctx did not get setup and hence it
    should put itself and exit.
    
    Reported-by: syzbot+763e12bbf004fb1062e4@syzkaller.appspotmail.com
    Fixes: ac0b8b327a56 ("io_uring: fix use-after-free of sq->thread in __io_uring_show_fdinfo()")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:11 -04:00
Jeff Moyer 6923d5c1d2 io_uring: fix task leak issue in io_wq_create()
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 89465d923bda180299e69ee2800aab84ad0ba689
Author: Penglei Jiang <superman.xpt@gmail.com>
Date:   Sun Jun 15 09:39:06 2025 -0700

    io_uring: fix task leak issue in io_wq_create()
    
    Add missing put_task_struct() in the error path
    
    Cc: stable@vger.kernel.org
    Fixes: 0f8baa3c9802 ("io-wq: fully initialize wqe before calling cpuhp_state_add_instance_nocalls()")
    Signed-off-by: Penglei Jiang <superman.xpt@gmail.com>
    Link: https://lore.kernel.org/r/20250615163906.2367-1-superman.xpt@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:11 -04:00
Jeff Moyer 8543f44ca0 io_uring/rsrc: validate buffer count with offset for cloning
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 1d27f11bf02b38c431e49a17dee5c10a2b4c2e28
Author: Jens Axboe <axboe@kernel.dk>
Date:   Sun Jun 15 08:09:14 2025 -0600

    io_uring/rsrc: validate buffer count with offset for cloning
    
    syzbot reports that it can trigger a WARN_ON() for kmalloc() attempt
    that's too big:
    
    WARNING: CPU: 0 PID: 6488 at mm/slub.c:5024 __kvmalloc_node_noprof+0x520/0x640 mm/slub.c:5024
    Modules linked in:
    CPU: 0 UID: 0 PID: 6488 Comm: syz-executor312 Not tainted 6.15.0-rc7-syzkaller-gd7fa1af5b33e #0 PREEMPT
    Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/07/2025
    pstate: 20400005 (nzCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
    pc : __kvmalloc_node_noprof+0x520/0x640 mm/slub.c:5024
    lr : __do_kmalloc_node mm/slub.c:-1 [inline]
    lr : __kvmalloc_node_noprof+0x3b4/0x640 mm/slub.c:5012
    sp : ffff80009cfd7a90
    x29: ffff80009cfd7ac0 x28: ffff0000dd52a120 x27: 0000000000412dc0
    x26: 0000000000000178 x25: ffff7000139faf70 x24: 0000000000000000
    x23: ffff800082f4cea8 x22: 00000000ffffffff x21: 000000010cd004a8
    x20: ffff0000d75816c0 x19: ffff0000dd52a000 x18: 00000000ffffffff
    x17: ffff800092f39000 x16: ffff80008adbe9e4 x15: 0000000000000005
    x14: 1ffff000139faf1c x13: 0000000000000000 x12: 0000000000000000
    x11: ffff7000139faf21 x10: 0000000000000003 x9 : ffff80008f27b938
    x8 : 0000000000000002 x7 : 0000000000000000 x6 : 0000000000000000
    x5 : 00000000ffffffff x4 : 0000000000400dc0 x3 : 0000000200000000
    x2 : 000000010cd004a8 x1 : ffff80008b3ebc40 x0 : 0000000000000001
    Call trace:
     __kvmalloc_node_noprof+0x520/0x640 mm/slub.c:5024 (P)
     kvmalloc_array_node_noprof include/linux/slab.h:1065 [inline]
     io_rsrc_data_alloc io_uring/rsrc.c:206 [inline]
     io_clone_buffers io_uring/rsrc.c:1178 [inline]
     io_register_clone_buffers+0x484/0xa14 io_uring/rsrc.c:1287
     __io_uring_register io_uring/register.c:815 [inline]
     __do_sys_io_uring_register io_uring/register.c:926 [inline]
     __se_sys_io_uring_register io_uring/register.c:903 [inline]
     __arm64_sys_io_uring_register+0x42c/0xea8 io_uring/register.c:903
     __invoke_syscall arch/arm64/kernel/syscall.c:35 [inline]
     invoke_syscall+0x98/0x2b8 arch/arm64/kernel/syscall.c:49
     el0_svc_common+0x130/0x23c arch/arm64/kernel/syscall.c:132
     do_el0_svc+0x48/0x58 arch/arm64/kernel/syscall.c:151
     el0_svc+0x58/0x17c arch/arm64/kernel/entry-common.c:767
     el0t_64_sync_handler+0x78/0x108 arch/arm64/kernel/entry-common.c:786
     el0t_64_sync+0x198/0x19c arch/arm64/kernel/entry.S:600
    
    which is due to offset + buffer_count being too large. The registration
    code checks only the total count of buffers, but given that the indexing
    is an array, it should also check offset + count. That can't exceed
    IORING_MAX_REG_BUFFERS either, as there's no way to reach buffers beyond
    that limit.
    
    There's no issue with registrering a table this large, outside of the
    fact that it's pointless to register buffers that cannot be reached, and
    that it can trigger this kmalloc() warning for attempting an allocation
    that is too large.
    
    Cc: stable@vger.kernel.org
    Fixes: b16e920a1909 ("io_uring/rsrc: allow cloning at an offset")
    Reported-by: syzbot+cb4bf3cb653be0d25de8@syzkaller.appspotmail.com
    Link: https://lore.kernel.org/io-uring/684e77bd.a00a0220.279073.0029.GAE@google.com/
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:11 -04:00
Jeff Moyer 03c27993a1 io_uring/kbuf: don't truncate end buffer for multiple buffer peeks
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 26ec15e4b0c1d7b25214d9c0be1d50492e2f006c
Author: Jens Axboe <axboe@kernel.dk>
Date:   Fri Jun 13 11:01:49 2025 -0600

    io_uring/kbuf: don't truncate end buffer for multiple buffer peeks
    
    If peeking a bunch of buffers, normally io_ring_buffers_peek() will
    truncate the end buffer. This isn't optimal as presumably more data will
    be arriving later, and hence it's better to stop with the last full
    buffer rather than truncate the end buffer.
    
    Cc: stable@vger.kernel.org
    Fixes: 35c8711c8fc4 ("io_uring/kbuf: add helpers for getting/peeking multiple buffers")
    Reported-by: Christian Mazakas <christian.mazakas@gmail.com>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:11 -04:00
Jeff Moyer 8aacb9aa76 io_uring: consistently use rcu semantics with sqpoll thread
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit c538f400fae22725580842deb2bef546701b64bd
Author: Keith Busch <kbusch@kernel.org>
Date:   Wed Jun 11 13:53:43 2025 -0700

    io_uring: consistently use rcu semantics with sqpoll thread
    
    The sqpoll thread is dereferenced with rcu read protection in one place,
    so it needs to be annotated as an __rcu type, and should consistently
    use rcu helpers for access and assignment to make sparse happy.
    
    Since most of the accesses occur under the sqd->lock, we can use
    rcu_dereference_protected() without declaring an rcu read section.
    Provide a simple helper to get the thread from a locked context.
    
    Fixes: ac0b8b327a5677d ("io_uring: fix use-after-free of sq->thread in __io_uring_show_fdinfo()")
    Signed-off-by: Keith Busch <kbusch@kernel.org>
    Link: https://lore.kernel.org/r/20250611205343.1821117-1-kbusch@meta.com
    [axboe: fold in fix for register.c]
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:10 -04:00
Jeff Moyer be773ea9c6 io_uring: fix use-after-free of sq->thread in __io_uring_show_fdinfo()
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit ac0b8b327a5677dc6fecdf353d808161525b1ff0
Author: Penglei Jiang <superman.xpt@gmail.com>
Date:   Tue Jun 10 10:18:01 2025 -0700

    io_uring: fix use-after-free of sq->thread in __io_uring_show_fdinfo()
    
    syzbot reports:
    
    BUG: KASAN: slab-use-after-free in getrusage+0x1109/0x1a60
    Read of size 8 at addr ffff88810de2d2c8 by task a.out/304
    
    CPU: 0 UID: 0 PID: 304 Comm: a.out Not tainted 6.16.0-rc1 #1 PREEMPT(voluntary)
    Hardware name: QEMU Ubuntu 24.04 PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
    Call Trace:
     <TASK>
     dump_stack_lvl+0x53/0x70
     print_report+0xd0/0x670
     ? __pfx__raw_spin_lock_irqsave+0x10/0x10
     ? getrusage+0x1109/0x1a60
     kasan_report+0xce/0x100
     ? getrusage+0x1109/0x1a60
     getrusage+0x1109/0x1a60
     ? __pfx_getrusage+0x10/0x10
     __io_uring_show_fdinfo+0x9fe/0x1790
     ? ksys_read+0xf7/0x1c0
     ? do_syscall_64+0xa4/0x260
     ? vsnprintf+0x591/0x1100
     ? __pfx___io_uring_show_fdinfo+0x10/0x10
     ? __pfx_vsnprintf+0x10/0x10
     ? mutex_trylock+0xcf/0x130
     ? __pfx_mutex_trylock+0x10/0x10
     ? __pfx_show_fd_locks+0x10/0x10
     ? io_uring_show_fdinfo+0x57/0x80
     io_uring_show_fdinfo+0x57/0x80
     seq_show+0x38c/0x690
     seq_read_iter+0x3f7/0x1180
     ? inode_set_ctime_current+0x160/0x4b0
     seq_read+0x271/0x3e0
     ? __pfx_seq_read+0x10/0x10
     ? __pfx__raw_spin_lock+0x10/0x10
     ? __mark_inode_dirty+0x402/0x810
     ? selinux_file_permission+0x368/0x500
     ? file_update_time+0x10f/0x160
     vfs_read+0x177/0xa40
     ? __pfx___handle_mm_fault+0x10/0x10
     ? __pfx_vfs_read+0x10/0x10
     ? mutex_lock+0x81/0xe0
     ? __pfx_mutex_lock+0x10/0x10
     ? fdget_pos+0x24d/0x4b0
     ksys_read+0xf7/0x1c0
     ? __pfx_ksys_read+0x10/0x10
     ? do_user_addr_fault+0x43b/0x9c0
     do_syscall_64+0xa4/0x260
     entry_SYSCALL_64_after_hwframe+0x77/0x7f
    RIP: 0033:0x7f0f74170fc9
    Code: 00 c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 8
    RSP: 002b:00007fffece049e8 EFLAGS: 00000206 ORIG_RAX: 0000000000000000
    RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007f0f74170fc9
    RDX: 0000000000001000 RSI: 00007fffece049f0 RDI: 0000000000000004
    RBP: 00007fffece05ad0 R08: 0000000000000000 R09: 00007fffece04d90
    R10: 0000000000000000 R11: 0000000000000206 R12: 00005651720a1100
    R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
     </TASK>
    
    Allocated by task 298:
     kasan_save_stack+0x33/0x60
     kasan_save_track+0x14/0x30
     __kasan_slab_alloc+0x6e/0x70
     kmem_cache_alloc_node_noprof+0xe8/0x330
     copy_process+0x376/0x5e00
     create_io_thread+0xab/0xf0
     io_sq_offload_create+0x9ed/0xf20
     io_uring_setup+0x12b0/0x1cc0
     do_syscall_64+0xa4/0x260
     entry_SYSCALL_64_after_hwframe+0x77/0x7f
    
    Freed by task 22:
     kasan_save_stack+0x33/0x60
     kasan_save_track+0x14/0x30
     kasan_save_free_info+0x3b/0x60
     __kasan_slab_free+0x37/0x50
     kmem_cache_free+0xc4/0x360
     rcu_core+0x5ff/0x19f0
     handle_softirqs+0x18c/0x530
     run_ksoftirqd+0x20/0x30
     smpboot_thread_fn+0x287/0x6c0
     kthread+0x30d/0x630
     ret_from_fork+0xef/0x1a0
     ret_from_fork_asm+0x1a/0x30
    
    Last potentially related work creation:
     kasan_save_stack+0x33/0x60
     kasan_record_aux_stack+0x8c/0xa0
     __call_rcu_common.constprop.0+0x68/0x940
     __schedule+0xff2/0x2930
     __cond_resched+0x4c/0x80
     mutex_lock+0x5c/0xe0
     io_uring_del_tctx_node+0xe1/0x2b0
     io_uring_clean_tctx+0xb7/0x160
     io_uring_cancel_generic+0x34e/0x760
     do_exit+0x240/0x2350
     do_group_exit+0xab/0x220
     __x64_sys_exit_group+0x39/0x40
     x64_sys_call+0x1243/0x1840
     do_syscall_64+0xa4/0x260
     entry_SYSCALL_64_after_hwframe+0x77/0x7f
    
    The buggy address belongs to the object at ffff88810de2cb00
     which belongs to the cache task_struct of size 3712
    The buggy address is located 1992 bytes inside of
     freed 3712-byte region [ffff88810de2cb00, ffff88810de2d980)
    
    which is caused by the task_struct pointed to by sq->thread being
    released while it is being used in the function
    __io_uring_show_fdinfo(). Holding ctx->uring_lock does not prevent ehre
    relase or exit of sq->thread.
    
    Fix this by assigning and looking up ->thread under RCU, and grabbing a
    reference to the task_struct. This ensures that it cannot get released
    while fdinfo is using it.
    
    Reported-by: syzbot+531502bbbe51d2f769f4@syzkaller.appspotmail.com
    Closes: https://lore.kernel.org/all/682b06a5.a70a0220.3849cf.00b3.GAE@google.com
    Fixes: 3fcb9d17206e ("io_uring/sqpoll: statistics of the true utilization of sq threads")
    Signed-off-by: Penglei Jiang <superman.xpt@gmail.com>
    Link: https://lore.kernel.org/r/20250610171801.70960-1-superman.xpt@gmail.com
    [axboe: massage commit message]
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:19:10 -04:00
Jeff Moyer a063a7941b io_uring/net: only consider msg_inq if larger than 1
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 2c7f023219966777be0687e15b57689894304cd3
Author: Jens Axboe <axboe@kernel.dk>
Date:   Wed May 28 13:45:44 2025 -0600

    io_uring/net: only consider msg_inq if larger than 1
    
    Currently retry and general validity of msg_inq is gated on it being
    larger than zero, but it's entirely possible for this to be slightly
    inaccurate. In particular, if FIN is received, it'll return 1.
    
    Just use larger than 1 as the check. This covers both the FIN case, and
    at the same time, it doesn't make much sense to retry a recv immediately
    if there's even just a single 1 byte of valid data in the socket.
    
    Leave the SOCK_NONEMPTY flagging when larger than 0 still, as an app may
    use that for the final receive.
    
    Cc: stable@vger.kernel.org
    Reported-by: Christian Mazakas <christian.mazakas@gmail.com>
    Fixes: 7c71a0af81ba ("io_uring/net: improve recv bundles")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:47 -04:00
Jeff Moyer a93aba6642 io_uring/net: only retry recv bundle for a full transfer
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 3a08988123c868dbfdd054541b1090fb891fa49e
Author: Jens Axboe <axboe@kernel.dk>
Date:   Wed May 21 18:51:49 2025 -0600

    io_uring/net: only retry recv bundle for a full transfer
    
    If a shorter than assumed transfer was seen, a partial buffer will have
    been filled. For that case it isn't sane to attempt to fill more into
    the bundle before posting a completion, as that will cause a gap in
    the received data.
    
    Check if the iterator has hit zero and only allow to continue a bundle
    operation if that is the case.
    
    Also ensure that for putting finished buffers, only the current transfer
    is accounted. Otherwise too many buffers may be put for a short transfer.
    
    Link: https://github.com/axboe/liburing/issues/1409
    Cc: stable@vger.kernel.org
    Fixes: 7c71a0af81ba ("io_uring/net: improve recv bundles")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:47 -04:00
Jeff Moyer 04447b92e8 io_uring: fix overflow resched cqe reordering
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit a7d755ed9ce9738af3db602eb29d32774a180bc7
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Sat May 17 13:27:37 2025 +0100

    io_uring: fix overflow resched cqe reordering
    
    Leaving the CQ critical section in the middle of a overflow flushing
    can cause cqe reordering since the cache cq pointers are reset and any
    new cqe emitters that might get called in between are not going to be
    forced into io_cqe_cache_refill().
    
    Fixes: eac2ca2d682f9 ("io_uring: check if we need to reschedule during overflow flush")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/90ba817f1a458f091f355f407de1c911d2b93bbf.1747483784.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:47 -04:00
Jeff Moyer 952fe3f127 io_uring/fdinfo: grab ctx->uring_lock around io_uring_show_fdinfo()
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit d871198ee431d90f5308d53998c1ba1d5db5619a
Author: Jens Axboe <axboe@kernel.dk>
Date:   Tue May 13 15:02:23 2025 -0600

    io_uring/fdinfo: grab ctx->uring_lock around io_uring_show_fdinfo()
    
    Not everything requires locking in there, which is why the 'has_lock'
    variable exists. But enough does that it's a bit unwieldy to manage.
    Wrap the whole thing in a ->uring_lock trylock, and just return
    with no output if we fail to grab it. The existing trylock() will
    already have greatly diminished utility/output for the failure case.
    
    This fixes an issue with reading the SQE fields, if the ring is being
    actively resized at the same time.
    
    Reported-by: Jann Horn <jannh@google.com>
    Fixes: 79cfe9e59c2a ("io_uring/register: add IORING_REGISTER_RESIZE_RINGS")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:47 -04:00
Jeff Moyer b1d6896d6b io_uring/kbuf: account ring io_buffer_list memory
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 475a8d30371604a6363da8e304a608a5959afc40
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Tue May 13 18:26:46 2025 +0100

    io_uring/kbuf: account ring io_buffer_list memory
    
    Follow the non-ringed pbuf struct io_buffer_list allocations and account
    it against the memcg. There is low chance of that being an actual
    problem as ring provided buffer should either pin user memory or
    allocate it, which is already accounted.
    
    Cc: stable@vger.kernel.org # 6.1
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/3985218b50d341273cafff7234e1a7e6d0db9808.1747150490.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:47 -04:00
Jeff Moyer 0270ba461f io_uring/memmap: don't use page_address() on a highmem page
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit f446c6311e86618a1f81eb576b56a6266307238f
Author: Jens Axboe <axboe@kernel.dk>
Date:   Mon May 12 09:06:06 2025 -0600

    io_uring/memmap: don't use page_address() on a highmem page
    
    For older/32-bit systems with highmem, don't assume that the pages in
    a mapped region are always going to be mapped. If io_region_init_ptr()
    finds that the pages are coalescable, also check if the first page is
    a HighMem page or not. If it is, fall through to the usual vmap()
    mapping rather than attempt to get the unmapped page address.
    
    Cc: stable@vger.kernel.org
    Fixes: c4d0ac1c1567 ("io_uring/memmap: optimise single folio regions")
    Link: https://lore.kernel.org/all/681fe2fb.050a0220.f2294.001a.GAE@google.com/
    Reported-by: syzbot+5b8c4abafcb1d791ccfc@syzkaller.appspotmail.com
    Link: https://lore.kernel.org/all/681fed0a.050a0220.f2294.001c.GAE@google.com/
    Reported-by: syzbot+6456a99dfdc2e78c4feb@syzkaller.appspotmail.com
    Tested-by: syzbot+6456a99dfdc2e78c4feb@syzkaller.appspotmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:47 -04:00
Jeff Moyer 6189e5c450 io_uring/uring_cmd: fix hybrid polling initialization issue
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 63166b815dc163b2e46426cecf707dc5923d6d13
Author: hexue <xue01.he@samsung.com>
Date:   Mon May 12 13:20:25 2025 +0800

    io_uring/uring_cmd: fix hybrid polling initialization issue
    
    Modify the check for whether the timer is initialized during IO transfer
    when passthrough is used with hybrid polling, to ensure that it's always
    setup correctly.
    
    Cc: stable@vger.kernel.org
    Fixes: 01ee194d1aba ("io_uring: add support for hybrid IOPOLL")
    Signed-off-by: hexue <xue01.he@samsung.com>
    Link: https://lore.kernel.org/r/20250512052025.293031-1-xue01.he@samsung.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:47 -04:00
Jeff Moyer e1550cb057 io_uring: account drain memory to cgroup
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit f979c20547e72568e3c793bc92c7522bc3166246
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Fri May 9 12:12:47 2025 +0100

    io_uring: account drain memory to cgroup
    
    Account drain allocations against memcg. It's not a big problem as each
    such allocation is paired with a request, which is accounted, but it's
    nicer to follow the limits more closely.
    
    Cc: stable@vger.kernel.org # 6.1
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/f8dfdbd755c41fd9c75d12b858af07dfba5bbb68.1746788718.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:47 -04:00
Jeff Moyer 64217b2ca1 io_uring/sqpoll: Increase task_work submission batch size
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 92835cebab120f8a5f023a26a792a2ac3f816c4f
Author: Gabriel Krisman Bertazi <krisman@suse.de>
Date:   Thu May 8 14:12:03 2025 -0400

    io_uring/sqpoll: Increase task_work submission batch size
    
    Our QA team reported a 10%-23%, throughput reduction on an io_uring
    sqpoll testcase doing IO to a null_blk, that I traced back to a
    reduction of the device submission queue depth utilization. It turns out
    that, after commit af5d68f8892f ("io_uring/sqpoll: manage task_work
    privately"), we capped the number of task_work entries that can be
    completed from a single spin of sqpoll to only 8 entries, before the
    sqpoll goes around to (potentially) sleep.  While this cap doesn't drive
    the submission side directly, it impacts the completion behavior, which
    affects the number of IO queued by fio per sqpoll cycle on the
    submission side, and io_uring ends up seeing less ios per sqpoll cycle.
    As a result, block layer plugging is less effective, and we see more
    time spent inside the block layer in profilings charts, and increased
    submission latency measured by fio.
    
    There are other places that have increased overhead once sqpoll sleeps
    more often, such as the sqpoll utilization calculation.  But, in this
    microbenchmark, those were not representative enough in perf charts, and
    their removal didn't yield measurable changes in throughput.  The major
    overhead comes from the fact we plug less, and less often, when submitting
    to the block layer.
    
    My benchmark is:
    
    fio --ioengine=io_uring --direct=1 --iodepth=128 --runtime=300 --bs=4k \
        --invalidate=1 --time_based  --ramp_time=10 --group_reporting=1 \
        --filename=/dev/nullb0 --name=RandomReads-direct-nullb-sqpoll-4k-1 \
        --rw=randread --numjobs=1 --sqthread_poll
    
    In one machine, tested on top of Linux 6.15-rc1, we have the following
    baseline:
      READ: bw=4994MiB/s (5236MB/s), 4994MiB/s-4994MiB/s (5236MB/s-5236MB/s), io=439GiB (471GB), run=90001-90001msec
    
    With this patch:
      READ: bw=5762MiB/s (6042MB/s), 5762MiB/s-5762MiB/s (6042MB/s-6042MB/s), io=506GiB (544GB), run=90001-90001msec
    
    which is a 15% improvement in measured bandwidth.  The average
    submission latency is noticeably lowered too.  As measured by
    fio:
    
    Baseline:
       lat (usec): min=20, max=241, avg=99.81, stdev=3.38
    Patched:
       lat (usec): min=26, max=226, avg=86.48, stdev=4.82
    
    If we look at blktrace, we can also see the plugging behavior is
    improved. In the baseline, we end up limited to plugging 8 requests in
    the block layer regardless of the device queue depth size, while after
    patching we can drive more io, and we manage to utilize the full device
    queue.
    
    In the baseline, after a stabilization phase, an ordinary submission
    looks like:
      254,0    1    49942     0.016028795  5977  U   N [iou-sqp-5976] 7
    
    After patching, I see consistently more requests per unplug.
      254,0    1     4996     0.001432872  3145  U   N [iou-sqp-3144] 32
    
    Ideally, the cap size would at least be the deep enough to fill the
    device queue, but we can't predict that behavior, or assume all IO goes
    to a single device, and thus can't guess the ideal batch size.  We also
    don't want to let the tw run unbounded, though I'm not sure it would
    really be a problem.  Instead, let's just give it a more sensible value
    that will allow for more efficient batching.  I've tested with different
    cap values, and initially proposed to increase the cap to 1024.  Jens
    argued it is too big of a bump and I observed that, with 32, I'm no
    longer able to observe this bottleneck in any of my machines.
    
    Fixes: af5d68f8892f ("io_uring/sqpoll: manage task_work privately")
    Signed-off-by: Gabriel Krisman Bertazi <krisman@suse.de>
    Link: https://lore.kernel.org/r/20250508181203.3785544-1-krisman@suse.de
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:47 -04:00
Jeff Moyer 28a909f6d4 io_uring: always arm linked timeouts prior to issue
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit b53e523261bf058ea4a518b482222e7a277b186b
Author: Jens Axboe <axboe@kernel.dk>
Date:   Sun May 4 08:06:28 2025 -0600

    io_uring: always arm linked timeouts prior to issue
    
    There are a few spots where linked timeouts are armed, and not all of
    them adhere to the pre-arm, attempt issue, post-arm pattern. This can
    be problematic if the linked request returns that it will trigger a
    callback later, and does so before the linked timeout is fully armed.
    
    Consolidate all the linked timeout handling into __io_issue_sqe(),
    rather than have it spread throughout the various issue entry points.
    
    Cc: stable@vger.kernel.org
    Link: https://github.com/axboe/liburing/issues/1390
    Reported-by: Chase Hiltz <chase@path.net>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:47 -04:00
Jeff Moyer 0292c13100 io_uring: fix 'sync' handling of io_fallback_tw()
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit edd43f4d6f50ec3de55a0c9e9df6348d1da51965
Author: Jens Axboe <axboe@kernel.dk>
Date:   Thu Apr 24 10:28:14 2025 -0600

    io_uring: fix 'sync' handling of io_fallback_tw()
    
    A previous commit added a 'sync' parameter to io_fallback_tw(), which if
    true, means the caller wants to wait on the fallback thread handling it.
    But the logic is somewhat messed up, ensure that ctxs are swapped and
    flushed appropriately.
    
    Cc: stable@vger.kernel.org
    Fixes: dfbe5561ae93 ("io_uring: flush offloaded and delayed task_work on exit")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:46 -04:00
Jeff Moyer e04d0ea9f7 io_uring/kbuf: reject zero sized provided buffers
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit cf960726eb65e8d0bfecbcce6cf95f47b1ffa6cc
Author: Jens Axboe <axboe@kernel.dk>
Date:   Mon Apr 7 07:51:23 2025 -0600

    io_uring/kbuf: reject zero sized provided buffers
    
    This isn't fixing a real issue, but there's also zero point in going
    through group and buffer setup, when the buffers are going to be
    rejected once attempted to get used.
    
    Cc: stable@vger.kernel.org
    Reported-by: syzbot+58928048fd1416f1457c@syzkaller.appspotmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:46 -04:00
Jeff Moyer f8c9275319 io_uring: don't post tag CQEs on file/buffer registration failure
JIRA: https://issues.redhat.com/browse/RHEL-105612
Conflicts: contextual differences only.

commit ab6005f3912fff07330297aba08922d2456dcede
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Fri Apr 4 15:46:34 2025 +0100

    io_uring: don't post tag CQEs on file/buffer registration failure
    
    Buffer / file table registration is all or nothing, if it fails all
    resources we might have partially registered are dropped and the table
    is killed. If that happens, it doesn't make sense to post any rsrc tag
    CQEs. That would be confusing to the application, which should not need
    to handle that case.
    
    Cc: stable@vger.kernel.org
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Fixes: 7029acd8a9503 ("io_uring/rsrc: get rid of per-ring io_rsrc_node list")
    Link: https://lore.kernel.org/r/c514446a8dcb0197cddd5d4ba8f6511da081cf1f.1743777957.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:46 -04:00
Jeff Moyer 5e2c56126a io_uring/net: account memory for zc sendmsg
JIRA: https://issues.redhat.com/browse/RHEL-105612
Conflicts: There was some code churn upstream that I chose not to include
just for this fix.

commit 04491732fc996305e1de80255d64ed6d1c472df5
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Thu Mar 27 15:02:20 2025 +0000

    io_uring/net: account memory for zc sendmsg
    
    Account pinned pages for IORING_OP_SENDMSG_ZC, just as we for
    IORING_OP_SEND_ZC and net/ does for MSG_ZEROCOPY.
    
    Fixes: 493108d95f146 ("io_uring/net: zerocopy sendmsg")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/4f00f67ca6ac8e8ed62343ae92b5816b1e0c9c4b.1743086313.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:46 -04:00
Jeff Moyer d96b55cd99 io_uring/net: fix io_req_post_cqe abuse by send bundle
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 6889ae1b4df1579bcdffef023e2ea9a982565dff
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Thu Mar 27 09:57:27 2025 +0000

    io_uring/net: fix io_req_post_cqe abuse by send bundle
    
    [  114.987980][ T5313] WARNING: CPU: 6 PID: 5313 at io_uring/io_uring.c:872 io_req_post_cqe+0x12e/0x4f0
    [  114.991597][ T5313] RIP: 0010:io_req_post_cqe+0x12e/0x4f0
    [  115.001880][ T5313] Call Trace:
    [  115.002222][ T5313]  <TASK>
    [  115.007813][ T5313]  io_send+0x4fe/0x10f0
    [  115.009317][ T5313]  io_issue_sqe+0x1a6/0x1740
    [  115.012094][ T5313]  io_wq_submit_work+0x38b/0xed0
    [  115.013223][ T5313]  io_worker_handle_work+0x62a/0x1600
    [  115.013876][ T5313]  io_wq_worker+0x34f/0xdf0
    
    As the comment states, io_req_post_cqe() should only be used by
    multishot requests, i.e. REQ_F_APOLL_MULTISHOT, which bundled sends are
    not. Add a flag signifying whether a request wants to post multiple
    CQEs. Eventually REQ_F_APOLL_MULTISHOT should imply the new flag, but
    that's left out for simplicity.
    
    Cc: stable@vger.kernel.org
    Fixes: a05d1f625c7aa ("io_uring/net: support bundles for send")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/8b611dbb54d1cd47a88681f5d38c84d0c02bc563.1743067183.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:46 -04:00
Jeff Moyer c12c01467d io_uring: fix retry handling off iowq
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 3f0cb8de56b9a5c052a9e43fa548856926059810
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Mon Mar 24 15:32:32 2025 +0000

    io_uring: fix retry handling off iowq
    
    io_req_complete_post() doesn't handle reissue and if called with a
    REQ_F_REISSUE request it might post extra unexpected completions. Fix it
    by pushing into flush_completion via task work.
    
    Fixes: d803d123948fe ("io_uring/rw: handle -EAGAIN retry at IO completion time")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/badb3d7e462881e7edbfcc2be6301090b07dbe53.1742829388.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:46 -04:00
Jeff Moyer 62129fca4a io_uring/net: only import send_zc buffer once
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 8e3100fcc5cbba03518b8b5c059624aba5c29d50
Author: Caleb Sander Mateos <csander@purestorage.com>
Date:   Fri Mar 21 12:48:17 2025 -0600

    io_uring/net: only import send_zc buffer once
    
    io_send_zc() guards its call to io_send_zc_import() with if (!done_io)
    in an attempt to avoid calling it redundantly on the same req. However,
    if the initial non-blocking issue returns -EAGAIN, done_io will stay 0.
    This causes the subsequent issue to unnecessarily re-import the buffer.
    
    Add an explicit flag "imported" to io_sr_msg to track if its buffer has
    already been imported. Clear the flag in io_send_zc_prep(). Call
    io_send_zc_import() and set the flag in io_send_zc() if it is unset.
    
    Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
    Fixes: 54cdcca05abd ("io_uring/net: switch io_send() and io_send_zc() to using io_async_msghdr")
    Link: https://lore.kernel.org/r/20250321184819.3847386-2-csander@purestorage.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:18:33 -04:00
Jeff Moyer d08952971d io_uring: Remove unused declaration io_alloc_async_data()
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 30c970354ce2a4c6ad3a4c70040accd34082f477
Author: Yue Haibing <yuehaibing@huawei.com>
Date:   Wed Mar 5 09:34:54 2025 +0800

    io_uring: Remove unused declaration io_alloc_async_data()
    
    Commit ef623a647f42 ("io_uring: Move old async data allocation helper
    to header") leave behind this unused declaration.
    
    Signed-off-by: Yue Haibing <yuehaibing@huawei.com>
    Link: https://lore.kernel.org/r/20250305013454.3635021-1-yuehaibing@huawei.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:17:33 -04:00
Jeff Moyer ae0dfbc7f9 io_uring: make io_poll_issue() sturdier
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit c457eed55d80bc06c2c55cd5d7a4646f102db5d4
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Sun Feb 23 17:22:31 2025 +0000

    io_uring: make io_poll_issue() sturdier
    
    io_poll_issue() forwards the call to io_issue_sqe() and thus inherits
    some of the handling. That's not particularly failure resistant, as for
    example returning an innocently looking IOU_OK from a multishot issue
    will lead to severe bugs.
    
    Reimplement io_poll_issue() without io_issue_sqe()'s request completion
    logic. Remove extra checks as we know that req->file is already set,
    linked timeout are armed, and iopoll is not supported. Also cover it
    with warnings for now.
    
    The patch should be useful by itself, but it's also preparing the
    codebase for other future clean ups.
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/3096d7b1026d9a52426a598bdfc8d9d324555545.1740331076.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:16:33 -04:00
Jeff Moyer c751b9fb67 io_uring/net: fix accept multishot handling
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit f6a89bf5278d6e15016a736db67043560d1b50d5
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Sun Feb 23 17:22:29 2025 +0000

    io_uring/net: fix accept multishot handling
    
    REQ_F_APOLL_MULTISHOT doesn't guarantee it's executed from the multishot
    context, so a multishot accept may get executed inline, fail
    io_req_post_cqe(), and ask the core code to kill the request with
    -ECANCELED by returning IOU_STOP_MULTISHOT even when a socket has been
    accepted and installed.
    
    Cc: stable@vger.kernel.org
    Fixes: 390ed29b5e425 ("io_uring: add IORING_ACCEPT_MULTISHOT for accept")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/51c6deb01feaa78b08565ca8f24843c017f5bc80.1740331076.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:15:33 -04:00
Jeff Moyer 4e7c937a19 io_uring: use lockless_cq flag in io_req_complete_post()
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 62aa9805d123165102273eb277f776aaca908e0e
Author: Caleb Sander Mateos <csander@purestorage.com>
Date:   Tue Feb 11 17:51:18 2025 -0700

    io_uring: use lockless_cq flag in io_req_complete_post()
    
    io_uring_create() computes ctx->lockless_cq as:
    ctx->task_complete || (ctx->flags & IORING_SETUP_IOPOLL)
    
    So use it to simplify that expression in io_req_complete_post().
    
    Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
    Reviewed-by: Li Zetao <lizetao1@huawei.com>
    Link: https://lore.kernel.org/r/20250212005119.3433005-1-csander@purestorage.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:14:33 -04:00
Jeff Moyer 272a49f6b2 io_uring: introduce type alias for io_tw_state
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit bcf8a0293a019bb0c4aebafdebe9a1e7a923249a
Author: Caleb Sander Mateos <csander@purestorage.com>
Date:   Sun Feb 16 19:25:04 2025 -0700

    io_uring: introduce type alias for io_tw_state
    
    In preparation for changing how io_tw_state is passed, introduce a type
    alias io_tw_token_t for struct io_tw_state *. This allows for changing
    the representation in one place, without having to update the many
    functions that just forward their struct io_tw_state * argument.
    
    Also add a comment to struct io_tw_state to explain its purpose.
    
    Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
    Link: https://lore.kernel.org/r/20250217022511.1150145-1-csander@purestorage.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:13:33 -04:00
Jeff Moyer 2fe9340aa4 io_uring/net: improve recv bundles
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 7c71a0af81ba72de9b2c501065e4e718aba9a271
Author: Jens Axboe <axboe@kernel.dk>
Date:   Sat Feb 8 10:50:34 2025 -0700

    io_uring/net: improve recv bundles
    
    Current recv bundles are only supported for multishot receives, and
    additionally they also always post at least 2 CQEs if more data is
    available than what a buffer will hold. This happens because the initial
    bundle recv will do a single buffer, and then do the rest of what is in
    the socket as a followup receive. As shown in a test program, if 1k
    buffers are available and 32k is available to receive in the socket,
    you'd get the following completions:
    
    bundle=1, mshot=0
    cqe res 1024
    cqe res 1024
    [...]
    cqe res 1024
    
    bundle=1, mshot=1
    cqe res 1024
    cqe res 31744
    
    where bundle=1 && mshot=0 will post 32 1k completions, and bundle=1 &&
    mshot=1 will post a 1k completion and then a 31k completion.
    
    To support bundle recv without multishot, it's possible to simply retry
    the recv immediately and post a single completion, rather than split it
    into two completions. With the below patch, the same test looks as
    follows:
    
    bundle=1, mshot=0
    cqe res 32768
    
    bundle=1, mshot=1
    cqe res 32768
    
    where mshot=0 works fine for bundles, and both of them post just a
    single 32k completion rather than split it into separate completions.
    Posting fewer completions is always a nice win, and not needing
    multishot for proper bundle efficiency is nice for cases that can't
    necessarily use multishot.
    
    Reported-by: Norman Maurer <norman_maurer@apple.com>
    Link: https://lore.kernel.org/r/184f9f92-a682-4205-a15d-89e18f664502@kernel.dk
    Fixes: 2f9c9515bdfd ("io_uring/net: support bundles for recv")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:12:33 -04:00
Jeff Moyer 45355de581 io_uring: check for iowq alloc_workqueue failure
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 7215469659cb9751a9bf80e43b24a48749004d26
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Fri Jan 31 17:28:21 2025 +0000

    io_uring: check for iowq alloc_workqueue failure
    
    alloc_workqueue() can fail even during init in io_uring_init(), check
    the result and panic if anything went wrong.
    
    Fixes: 73eaa2b583493 ("io_uring: use private workqueue for exit work")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/3a046063902f888f66151f89fa42f84063b9727b.1738343083.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:11:33 -04:00
Jeff Moyer 23f5d50f93 io_uring/io-wq: do not use bogus hash value
JIRA: https://issues.redhat.com/browse/RHEL-105612
Conflicts: RHEL does not have commit 6ee78354eaa60 ("io_uring/io-wq:
cache work->flags in variable"), and I didn't pull it in as it was
part of a larger patch series.

commit 486ba4d84d62e92716cd395c4b1612b8ce70a257
Author: Max Kellermann <max.kellermann@ionos.com>
Date:   Tue Jan 28 14:39:24 2025 +0100

    io_uring/io-wq: do not use bogus hash value
    
    Previously, the `hash` variable was initialized with `-1` and only
    updated by io_get_next_work() if the current work was hashed.  Commit
    60cf46ae60 ("io-wq: hash dependent work") changed this to always
    call io_get_work_hash() even if the work was not hashed.  This caused
    the `hash != -1U` check to always be true, adding some overhead for
    the `hash->wait` code.
    
    This patch fixes the regression by checking the `IO_WQ_WORK_HASHED`
    flag.
    
    Perf diff for a flood of `IORING_OP_NOP` with `IOSQE_ASYNC`:
    
        38.55%     -1.57%  [kernel.kallsyms]  [k] queued_spin_lock_slowpath
         6.86%     -0.72%  [kernel.kallsyms]  [k] io_worker_handle_work
         0.10%     +0.67%  [kernel.kallsyms]  [k] put_prev_entity
         1.96%     +0.59%  [kernel.kallsyms]  [k] io_nop_prep
         3.31%     -0.51%  [kernel.kallsyms]  [k] try_to_wake_up
         7.18%     -0.47%  [kernel.kallsyms]  [k] io_wq_free_work
    
    Fixes: 60cf46ae60 ("io-wq: hash dependent work")
    Cc: Pavel Begunkov <asml.silence@gmail.com>
    Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
    Link: https://lore.kernel.org/r/20250128133927.3989681-6-max.kellermann@ionos.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:10:33 -04:00
Jeff Moyer 7bbc7350fb fs/aio: Check IOCB_AIO_RW before the struct aio_kiocb conversion
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 961ebd120565cb60cebe21cb634fbc456022db4a
Author: Bart Van Assche <bvanassche@acm.org>
Date:   Mon Mar 4 15:57:15 2024 -0800

    fs/aio: Check IOCB_AIO_RW before the struct aio_kiocb conversion
    
    The first kiocb_set_cancel_fn() argument may point at a struct kiocb
    that is not embedded inside struct aio_kiocb. With the current code,
    depending on the compiler, the req->ki_ctx read happens either before
    the IOCB_AIO_RW test or after that test. Move the req->ki_ctx read such
    that it is guaranteed that the IOCB_AIO_RW test happens first.
    
    Reported-by: Eric Biggers <ebiggers@kernel.org>
    Cc: Benjamin LaHaise <ben@communityfibre.ca>
    Cc: Eric Biggers <ebiggers@google.com>
    Cc: Christoph Hellwig <hch@lst.de>
    Cc: Avi Kivity <avi@scylladb.com>
    Cc: Sandeep Dhavale <dhavale@google.com>
    Cc: Jens Axboe <axboe@kernel.dk>
    Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
    Cc: Kent Overstreet <kent.overstreet@linux.dev>
    Cc: stable@vger.kernel.org
    Fixes: b820de741ae4 ("fs/aio: Restrict kiocb_set_cancel_fn() to I/O submitted via libaio")
    Signed-off-by: Bart Van Assche <bvanassche@acm.org>
    Link: https://lore.kernel.org/r/20240304235715.3790858-1-bvanassche@acm.org
    Reviewed-by: Jens Axboe <axboe@kernel.dk>
    Reviewed-by: Eric Biggers <ebiggers@google.com>
    Signed-off-by: Christian Brauner <brauner@kernel.org>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:09:33 -04:00
Jeff Moyer cfe597e07d io_uring/net: fix sendzc double notif flush
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 67c007d6c12da3e456c005083696c20d4498ae72
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Sat Mar 22 11:47:27 2025 +0000

    io_uring/net: fix sendzc double notif flush
    
    refcount_t: underflow; use-after-free.
    WARNING: CPU: 0 PID: 5823 at lib/refcount.c:28 refcount_warn_saturate+0x15a/0x1d0 lib/refcount.c:28
    RIP: 0010:refcount_warn_saturate+0x15a/0x1d0 lib/refcount.c:28
    Call Trace:
     <TASK>
     io_notif_flush io_uring/notif.h:40 [inline]
     io_send_zc_cleanup+0x121/0x170 io_uring/net.c:1222
     io_clean_op+0x58c/0x9a0 io_uring/io_uring.c:406
     io_free_batch_list io_uring/io_uring.c:1429 [inline]
     __io_submit_flush_completions+0xc16/0xd20 io_uring/io_uring.c:1470
     io_submit_flush_completions io_uring/io_uring.h:159 [inline]
    
    Before the blamed commit, sendzc relied on io_req_msg_cleanup() to clear
    REQ_F_NEED_CLEANUP, so after the following snippet the request will
    never hit the core io_uring cleanup path.
    
    io_notif_flush();
    io_req_msg_cleanup();
    
    The easiest fix is to null the notification. io_send_zc_cleanup() can
    still be called after, but it's tolerated.
    
    Reported-by: syzbot+cf285a028ffba71b2ef5@syzkaller.appspotmail.com
    Tested-by: syzbot+cf285a028ffba71b2ef5@syzkaller.appspotmail.com
    Fixes: cc34d8330e036 ("io_uring/net: don't clear REQ_F_NEED_CLEANUP unconditionally")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/e1306007458b8891c88c4f20c966a17595f766b0.1742643795.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:33 -04:00
Jeff Moyer 6bbd4be5ff io_uring/net: don't clear REQ_F_NEED_CLEANUP unconditionally
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit cc34d8330e036b6bffa88db9ea537bae6b03948f
Author: Jens Axboe <axboe@kernel.dk>
Date:   Thu Mar 20 12:25:12 2025 -0600

    io_uring/net: don't clear REQ_F_NEED_CLEANUP unconditionally
    
    io_req_msg_cleanup() relies on the fact that io_netmsg_recycle() will
    always fully recycle, but that may not be the case if the msg cache
    was already full. To ensure that normal cleanup always gets run,
    let io_netmsg_recycle() deal with clearing the relevant cleanup flags,
    as it knows exactly when that should be done.
    
    Cc: stable@vger.kernel.org
    Reported-by: David Wei <dw@davidwei.uk>
    Fixes: 75191341785e ("io_uring/net: add iovec recycling")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:33 -04:00
Jeff Moyer e05e5ba19b io_uring/rw: ensure reissue path is correctly handled for IOPOLL
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit bcb0fda3c2da9fe4721d3e73d80e778c038e7d27
Author: Jens Axboe <axboe@kernel.dk>
Date:   Wed Mar 5 14:03:34 2025 -0700

    io_uring/rw: ensure reissue path is correctly handled for IOPOLL
    
    The IOPOLL path posts CQEs when the io_kiocb is marked as completed,
    so it cannot rely on the usual retry that non-IOPOLL requests do for
    read/write requests.
    
    If -EAGAIN is received and the request should be retried, go through
    the normal completion path and let the normal flush logic catch it and
    reissue it, like what is done for !IOPOLL reads or writes.
    
    Fixes: d803d123948f ("io_uring/rw: handle -EAGAIN retry at IO completion time")
    Reported-by: John Garry <john.g.garry@oracle.com>
    Link: https://lore.kernel.org/io-uring/2b43ccfa-644d-4a09-8f8f-39ad71810f41@oracle.com/
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:32 -04:00
Jeff Moyer 206141ff80 io_uring/net: save msg_control for compat
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 6ebf05189dfc6d0d597c99a6448a4d1064439a18
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Tue Feb 25 15:59:02 2025 +0000

    io_uring/net: save msg_control for compat
    
    Match the compat part of io_sendmsg_copy_hdr() with its counterpart and
    save msg_control.
    
    Fixes: c55978024d123 ("io_uring/net: move receive multishot out of the generic msghdr path")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/2a8418821fe83d3b64350ad2b3c0303e9b732bbd.1740498502.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:32 -04:00
Jeff Moyer bf9c613bbb io_uring/rw: clean up mshot forced sync mode
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 4614de748e78a295ee9b1f54ca87280b101fbdf0
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Wed Feb 19 01:33:40 2025 +0000

    io_uring/rw: clean up mshot forced sync mode
    
    Move code forcing synchronous execution of multishot read requests out
    a more generic __io_read().
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/4ad7b928c776d1ad59addb9fff64ef2d1fc474d5.1739919038.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:32 -04:00
Jeff Moyer 8c48700a07 io_uring/rw: move ki_complete init into prep
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 74f3e875268f1ce2dd01029c29560263212077df
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Wed Feb 19 01:33:39 2025 +0000

    io_uring/rw: move ki_complete init into prep
    
    Initialise ki_complete during request prep stage, we'll depend on it not
    being reset during issue in the following patch.
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/817624086bd5f0448b08c80623399919fda82f34.1739919038.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:32 -04:00
Jeff Moyer 3b9f50218e io_uring/rw: don't directly use ki_complete
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 4e43133c6f2319d3e205ea986c507b25d9b41e64
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Wed Feb 19 01:33:38 2025 +0000

    io_uring/rw: don't directly use ki_complete
    
    We want to avoid checking ->ki_complete directly in the io_uring
    completion path. Fortunately we have only two callback the selection
    of which depend on the ring constant flags, i.e. IOPOLL, so use that
    to infer the function.
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/4eb4bdab8cbcf5bc87083f7047edc81e920ab83c.1739919038.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:32 -04:00
Jeff Moyer a631499a08 io_uring/rw: forbid multishot async reads
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 67b0025d19f99fb9fbb8b62e6975553c183f3a16
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Wed Feb 19 01:33:37 2025 +0000

    io_uring/rw: forbid multishot async reads
    
    At the moment we can't sanely handle queuing an async request from a
    multishot context, so disable them. It shouldn't matter as pollable
    files / socekts don't normally do async.
    
    Patching it in __io_read() is not the cleanest way, but it's simpler
    than other options, so let's fix it there and clean up on top.
    
    Cc: stable@vger.kernel.org
    Reported-by: chase xd <sl1589472800@gmail.com>
    Fixes: fc68fcda04910 ("io_uring/rw: add support for IORING_OP_READ_MULTISHOT")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/7d51732c125159d17db4fe16f51ec41b936973f8.1739919038.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:32 -04:00
Jeff Moyer 70424169fd io_uring/rsrc: remove unused constants
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit fb3331f53e3cb1f1505f918f4f33bb0a3a231e4f
Author: Caleb Sander Mateos <csander@purestorage.com>
Date:   Tue Feb 18 20:34:43 2025 -0700

    io_uring/rsrc: remove unused constants
    
    IO_NODE_ALLOC_CACHE_MAX has been unused since commit fbbb8e991d86
    ("io_uring/rsrc: get rid of io_rsrc_node allocation cache") removed the
    rsrc_node_cache.
    
    IO_RSRC_TAG_TABLE_SHIFT and IO_RSRC_TAG_TABLE_MASK have been unused
    since commit 7029acd8a950 ("io_uring/rsrc: get rid of per-ring
    io_rsrc_node list") removed the separate tag table for registered nodes.
    
    Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
    Reviewed-by: Li Zetao <lizetao1@huawei.com>
    Link: https://lore.kernel.org/r/20250219033444.2020136-1-csander@purestorage.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:32 -04:00
Jeff Moyer 89e3559368 io_uring: fix spelling error in uapi io_uring.h
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 1fc61eeefe10d9996d2b875214d89f0909d03417
Author: Jens Axboe <axboe@kernel.dk>
Date:   Tue Feb 18 16:47:40 2025 -0700

    io_uring: fix spelling error in uapi io_uring.h
    
    This is obviously not that important, but when changes are synced back
    from the kernel to liburing, the codespell CI ends up erroring because
    of this misspelling. Let's just correct it and avoid this biting us
    again on an import.
    
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:32 -04:00
Jeff Moyer ec63bbebe2 io-wq: backoff when retrying worker creation
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 13918315c5dc5a515926c8799042ea6885c2b734
Author: Uday Shankar <ushankar@purestorage.com>
Date:   Sat Feb 8 13:42:13 2025 -0700

    io-wq: backoff when retrying worker creation
    
    When io_uring submission goes async for the first time on a given task,
    we'll try to create a worker thread to handle the submission. Creating
    this worker thread can fail due to various transient conditions, such as
    an outstanding signal in the forking thread, so we have retry logic with
    a limit of 3 retries. However, this retry logic appears to be too
    aggressive/fast - we've observed a thread blowing through the retry
    limit while having the same outstanding signal the whole time. Here's an
    excerpt of some tracing that demonstrates the issue:
    
    First, signal 26 is generated for the process. It ends up getting routed
    to thread 92942.
    
     0)   cbd-92284    /* signal_generate: sig=26 errno=0 code=-2 comm=psblkdASD pid=92934 grp=1 res=0 */
    
    This causes create_io_thread in the signalled thread to fail with
    ERESTARTNOINTR, and thus a retry is queued.
    
    13) task_th-92942  /* io_uring_queue_async_work: ring 000000007325c9ae, request 0000000080c96d8e, user_data 0x0, opcode URING_CMD, flags 0x8240001, normal queue, work 000000006e96dd3f */
    13) task_th-92942  io_wq_enqueue() {
    13) task_th-92942    _raw_spin_lock();
    13) task_th-92942    io_wq_activate_free_worker();
    13) task_th-92942    _raw_spin_lock();
    13) task_th-92942    create_io_worker() {
    13) task_th-92942      __kmalloc_cache_noprof();
    13) task_th-92942      __init_swait_queue_head();
    13) task_th-92942      kprobe_ftrace_handler() {
    13) task_th-92942        get_kprobe();
    13) task_th-92942        aggr_pre_handler() {
    13) task_th-92942          pre_handler_kretprobe();
    13) task_th-92942          /* create_enter: (create_io_thread+0x0/0x50) fn=0xffffffff8172c0e0 arg=0xffff888996bb69c0 node=-1 */
    13) task_th-92942        } /* aggr_pre_handler */
    ...
    13) task_th-92942        } /* copy_process */
    13) task_th-92942      } /* create_io_thread */
    13) task_th-92942      kretprobe_rethook_handler() {
    13) task_th-92942        /* create_exit: (create_io_worker+0x8a/0x1a0 <- create_io_thread) arg1=0xfffffffffffffdff */
    13) task_th-92942      } /* kretprobe_rethook_handler */
    13) task_th-92942    queue_work_on() {
    ...
    
    The CPU is then handed to a kworker to process the queued retry:
    
    ------------------------------------------
     13) task_th-92942  => kworker-54154
    ------------------------------------------
    13) kworker-54154  io_workqueue_create() {
    13) kworker-54154    io_queue_worker_create() {
    13) kworker-54154      task_work_add() {
    13) kworker-54154        wake_up_state() {
    13) kworker-54154          try_to_wake_up() {
    13) kworker-54154            _raw_spin_lock_irqsave();
    13) kworker-54154            _raw_spin_unlock_irqrestore();
    13) kworker-54154          } /* try_to_wake_up */
    13) kworker-54154        } /* wake_up_state */
    13) kworker-54154        kick_process();
    13) kworker-54154      } /* task_work_add */
    13) kworker-54154    } /* io_queue_worker_create */
    13) kworker-54154  } /* io_workqueue_create */
    
    And then we immediately switch back to the original task to try creating
    a worker again. This fails, because the original task still hasn't
    handled its signal.
    
    -----------------------------------------
     13) kworker-54154  => task_th-92942
    ------------------------------------------
    13) task_th-92942  create_worker_cont() {
    13) task_th-92942    kprobe_ftrace_handler() {
    13) task_th-92942      get_kprobe();
    13) task_th-92942      aggr_pre_handler() {
    13) task_th-92942        pre_handler_kretprobe();
    13) task_th-92942        /* create_enter: (create_io_thread+0x0/0x50) fn=0xffffffff8172c0e0 arg=0xffff888996bb69c0 node=-1 */
    13) task_th-92942      } /* aggr_pre_handler */
    13) task_th-92942    } /* kprobe_ftrace_handler */
    13) task_th-92942    create_io_thread() {
    13) task_th-92942      copy_process() {
    13) task_th-92942        task_active_pid_ns();
    13) task_th-92942        _raw_spin_lock_irq();
    13) task_th-92942        recalc_sigpending();
    13) task_th-92942        _raw_spin_lock_irq();
    13) task_th-92942      } /* copy_process */
    13) task_th-92942    } /* create_io_thread */
    13) task_th-92942    kretprobe_rethook_handler() {
    13) task_th-92942      /* create_exit: (create_worker_cont+0x35/0x1b0 <- create_io_thread) arg1=0xfffffffffffffdff */
    13) task_th-92942    } /* kretprobe_rethook_handler */
    13) task_th-92942    io_worker_release();
    13) task_th-92942    queue_work_on() {
    13) task_th-92942      clear_pending_if_disabled();
    13) task_th-92942      __queue_work() {
    13) task_th-92942      } /* __queue_work */
    13) task_th-92942    } /* queue_work_on */
    13) task_th-92942  } /* create_worker_cont */
    
    The pattern repeats another couple times until we blow through the retry
    counter, at which point we give up. All outstanding work is canceled,
    and the io_uring command which triggered all this is failed with
    ECANCELED:
    
    13) task_th-92942  io_acct_cancel_pending_work() {
    ...
    13) task_th-92942  /* io_uring_complete: ring 000000007325c9ae, req 0000000080c96d8e, user_data 0x0, result -125, cflags 0x0 extra1 0 extra2 0  */
    
    Finally, the task gets around to processing its outstanding signal 26,
    but it's too late.
    
    13) task_th-92942  /* signal_deliver: sig=26 errno=0 code=-2 sa_handler=59566a0 sa_flags=14000000 */
    
    Try to address this issue by adding a small scaling delay when retrying
    worker creation. This should give the forking thread time to handle its
    signal in the above case. This isn't a particularly satisfying solution,
    as sufficiently paradoxical scheduling would still have us hitting the
    same issue, and I'm open to suggestions for something better. But this
    is likely to prevent this (already rare) issue from hitting in practice.
    
    Signed-off-by: Uday Shankar <ushankar@purestorage.com>
    Link: https://lore.kernel.org/r/20250208-wq_retry-v2-1-4f6f5041d303@purestorage.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:32 -04:00
Jeff Moyer 455ed0e215 io_uring/uring_cmd: unconditionally copy SQEs at prep time
JIRA: https://issues.redhat.com/browse/RHEL-105612
Conflicts: Patch application order is different in RHEL.  We already
have commit eaf72f7b414f5 ("io_uring/uring_cmd: cleanup struct
io_uring_cmd_data layout"), which changes context.

commit d6211ebbdaa541af197b50b8dd8f22642ce0b87f
Author: Jens Axboe <axboe@kernel.dk>
Date:   Thu Feb 13 08:24:23 2025 -0700

    io_uring/uring_cmd: unconditionally copy SQEs at prep time
    
    This isn't generally necessary, but conditions have been observed where
    SQE data is accessed from the original SQE after prep has been done and
    outside of the initial issue. Opcode prep handlers must ensure that any
    SQE related data is stable beyond the prep phase, but uring_cmd is a bit
    special in how it handles the SQE which makes it susceptible to reading
    stale data. If the application has reused the SQE before the original
    completes, then that can lead to data corruption.
    
    Down the line we can relax this again once uring_cmd has been sanitized
    a bit, and avoid unnecessarily copying the SQE.
    
    Fixes: 5eff57fa9f3a ("io_uring/uring_cmd: defer SQE copying until it's needed")
    Reported-by: Caleb Sander Mateos <csander@purestorage.com>
    Reviewed-by: Caleb Sander Mateos <csander@purestorage.com>
    Reviewed-by: Li Zetao <lizetao1@huawei.com>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:32 -04:00
Jeff Moyer 1d775678c0 io_uring/waitid: setup async data in the prep handler
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 2b4fc4cd43f28e9e39179c8702e6ee821258584d
Author: Jens Axboe <axboe@kernel.dk>
Date:   Wed Feb 12 15:52:54 2025 -0700

    io_uring/waitid: setup async data in the prep handler
    
    This is the idiomatic way that opcodes should setup their async data,
    so that it's always valid inside ->issue() without issue needing to
    do that.
    
    Fixes: f31ecf671ddc4 ("io_uring: add IORING_OP_WAITID support")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:31 -04:00
Jeff Moyer 2b0cf19b28 io_uring/uring_cmd: remove dead req_has_async_data() check
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 0edf1283a9d1419a2095b4fcdd95c11ac00a191c
Author: Jens Axboe <axboe@kernel.dk>
Date:   Wed Feb 12 14:05:11 2025 -0700

    io_uring/uring_cmd: remove dead req_has_async_data() check
    
    Any uring_cmd always has async data allocated now, there's no reason to
    check and clear a cached copy of the SQE.
    
    Fixes: d10f19dff56e ("io_uring/uring_cmd: switch to always allocating async data")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:31 -04:00
Jeff Moyer 1679541e86 io_uring/uring_cmd: switch sqe to async_data on EAGAIN
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit e663da62ba8672aaa66843f1af8b20e3bb1a0515
Author: Caleb Sander Mateos <csander@purestorage.com>
Date:   Wed Feb 12 13:45:46 2025 -0700

    io_uring/uring_cmd: switch sqe to async_data on EAGAIN
    
    5eff57fa9f3a ("io_uring/uring_cmd: defer SQE copying until it's needed")
    moved the unconditional memcpy() of the uring_cmd SQE to async_data
    to 2 cases when the request goes async:
    - If REQ_F_FORCE_ASYNC is set to force the initial issue to go async
    - If ->uring_cmd() returns -EAGAIN in the initial non-blocking issue
    
    Unlike the REQ_F_FORCE_ASYNC case, in the EAGAIN case, io_uring_cmd()
    copies the SQE to async_data but neglects to update the io_uring_cmd's
    sqe field to point to async_data. As a result, sqe still points to the
    slot in the userspace-mapped SQ. At the end of io_submit_sqes(), the
    kernel advances the SQ head index, allowing userspace to reuse the slot
    for a new SQE. If userspace reuses the slot before the io_uring worker
    reissues the original SQE, the io_uring_cmd's SQE will be corrupted.
    
    Introduce a helper io_uring_cmd_cache_sqes() to copy the original SQE to
    the io_uring_cmd's async_data and point sqe there. Use it for both the
    REQ_F_FORCE_ASYNC and EAGAIN cases. This ensures the uring_cmd doesn't
    read from the SQ slot after it has been returned to userspace.
    
    Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
    Fixes: 5eff57fa9f3a ("io_uring/uring_cmd: defer SQE copying until it's needed")
    Link: https://lore.kernel.org/r/20250212204546.3751645-3-csander@purestorage.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:31 -04:00
Jeff Moyer 7f02483f65 io_uring/uring_cmd: don't assume io_uring_cmd_data layout
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 34cae91215c6f65bed2a124fb9283da6ec0b8dd9
Author: Caleb Sander Mateos <csander@purestorage.com>
Date:   Wed Feb 12 13:45:45 2025 -0700

    io_uring/uring_cmd: don't assume io_uring_cmd_data layout
    
    eaf72f7b414f ("io_uring/uring_cmd: cleanup struct io_uring_cmd_data
    layout") removed most of the places assuming struct io_uring_cmd_data
    has sqes as its first field. However, the EAGAIN case in io_uring_cmd()
    still compares ioucmd->sqe to the struct io_uring_cmd_data pointer using
    a void * cast. Since fa3595523d72 ("io_uring: get rid of alloc cache
    init_once handling"), sqes is no longer io_uring_cmd_data's first field.
    As a result, the pointers will always compare unequal and memcpy() may
    be called with the same source and destination.
    
    Replace the incorrect void * cast with the address of the sqes field.
    
    Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
    Fixes: eaf72f7b414f ("io_uring/uring_cmd: cleanup struct io_uring_cmd_data layout")
    Link: https://lore.kernel.org/r/20250212204546.3751645-2-csander@purestorage.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:31 -04:00
Jeff Moyer f096647a08 io_uring/kbuf: reallocate buf lists on upgrade
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 8802766324e1f5d414a81ac43365c20142e85603
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Wed Feb 12 13:46:46 2025 +0000

    io_uring/kbuf: reallocate buf lists on upgrade
    
    IORING_REGISTER_PBUF_RING can reuse an old struct io_buffer_list if it
    was created for legacy selected buffer and has been emptied. It violates
    the requirement that most of the field should stay stable after publish.
    Always reallocate it instead.
    
    Cc: stable@vger.kernel.org
    Reported-by: Pumpkin Chang <pumpkin@devco.re>
    Fixes: 2fcabce2d7d34 ("io_uring: disallow mixed provided buffer group registrations")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:31 -04:00
Jeff Moyer 8e520de6de io_uring/waitid: don't abuse io_tw_state
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 06521ac0485effdcc9c792cb0b40ed8e6f2f5fb8
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Wed Feb 12 13:33:24 2025 +0000

    io_uring/waitid: don't abuse io_tw_state
    
    struct io_tw_state is managed by core io_uring, and opcode handling code
    must never try to cheat and create their own instances, it's plain
    incorrect.
    
    io_waitid_complete() attempts exactly that outside of the task work
    context, and even though the ring is locked, there would be no one to
    reap the requests from the defer completion list. It only works now
    because luckily it's called before io_uring_try_cancel_uring_cmd(),
    which flushes completions.
    
    Fixes: f31ecf671ddc4 ("io_uring: add IORING_OP_WAITID support")
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:31 -04:00
Jeff Moyer da5ee9a2ea io_uring/net: don't retry connect operation on EPOLLERR
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 8c8492ca64e79c6e0f433e8c9d2bcbd039ef83d0
Author: Jens Axboe <axboe@kernel.dk>
Date:   Thu Jan 30 08:40:29 2025 -0700

    io_uring/net: don't retry connect operation on EPOLLERR
    
    If a socket is shutdown before the connection completes, POLLERR is set
    in the poll mask. However, connect ignores this as it doesn't know, and
    attempts the connection again. This may lead to a bogus -ETIMEDOUT
    result, where it should have noticed the POLLERR and just returned
    -ECONNRESET instead.
    
    Have the poll logic check for whether or not POLLERR is set in the mask,
    and if so, mark the request as failed. Then connect can appropriately
    fail the request rather than retry it.
    
    Reported-by: Sergey Galas <ssgalas@cloud.ru>
    Cc: stable@vger.kernel.org
    Link: https://github.com/axboe/liburing/discussions/1335
    Fixes: 3fb1bd688172 ("io_uring/net: handle -EINPROGRESS correct for IORING_OP_CONNECT")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:31 -04:00
Jeff Moyer 8832ab68c0 io_uring/rw: simplify io_rw_recycle()
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit d1fdab8c06791945d9454fb430951533eba9e175
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Tue Jan 28 20:56:16 2025 +0000

    io_uring/rw: simplify io_rw_recycle()
    
    Instead of freeing iovecs in case of IO_URING_F_UNLOCKED in
    io_rw_recycle(), leave it be and rely on the core io_uring code to
    call io_readv_writev_cleanup() later. This way the iovec will get
    recycled and we can clean up io_rw_recycle() and kill
    io_rw_iovec_free().
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Reviewed-by: Gabriel Krisman Bertazi <krisman@suse.de>
    Link: https://lore.kernel.org/r/14f83b112eb40078bea18e15d77a4f99fc981a44.1738087204.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:31 -04:00
Jeff Moyer 3867962b08 io_uring: remove !KASAN guards from cache free
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 0d124578fed92cadeaca47d734da782beacdc1a7
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Tue Jan 28 20:56:15 2025 +0000

    io_uring: remove !KASAN guards from cache free
    
    Test setups (with KASAN) will avoid !KASAN sections, and so it's not
    testing paths that would be exercised otherwise. That's bad as to be
    sure that your code works you now have to specifically test both KASAN
    and !KASAN configs.
    
    Remove !CONFIG_KASAN guards from io_netmsg_cache_free() and
    io_rw_cache_free(). The free functions should always be getting valid
    entries, and even though for KASAN iovecs should already be cleared,
    that's better than skipping the chunks completely.
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Reviewed-by: Gabriel Krisman Bertazi <krisman@suse.de>
    Link: https://lore.kernel.org/r/d6078a51c7137a243f9d00849bc3daa660873209.1738087204.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:31 -04:00
Jeff Moyer ef6bf18bde io_uring/net: extract io_send_select_buffer()
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 86e62354eef16993834be5bd218d38ec96c47f16
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Tue Jan 28 20:56:14 2025 +0000

    io_uring/net: extract io_send_select_buffer()
    
    Extract a helper out of io_send() for provided buffer selection to
    improve readability as it has grown to take too many lines.
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Reviewed-by: Gabriel Krisman Bertazi <krisman@suse.de>
    Link: https://lore.kernel.org/r/26a769cdabd61af7f40c5d88a22469c5ad071796.1738087204.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:31 -04:00
Jeff Moyer 501b462cac io_uring/net: clean io_msg_copy_hdr()
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 2b350f756b7acf84afab31d65ce6e3d496213ae5
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Tue Jan 28 20:56:13 2025 +0000

    io_uring/net: clean io_msg_copy_hdr()
    
    Put msg->msg_iov into a local variable in io_msg_copy_hdr(), it reads
    better and clearly shows the used types.
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Reviewed-by: Gabriel Krisman Bertazi <krisman@suse.de>
    Link: https://lore.kernel.org/r/6a5d4f7a96b10e571d6128be010166b3aaf7afd5.1738087204.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:30 -04:00
Jeff Moyer 9cbc2cdb28 io_uring/net: make io_net_vec_assign() return void
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit fefcb0dcd02fd34f808e91b13ce25f9847e52eb9
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Tue Jan 28 20:56:12 2025 +0000

    io_uring/net: make io_net_vec_assign() return void
    
    io_net_vec_assign() can only return 0 and it doesn't make sense for it
    to fail, so make it return void.
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Reviewed-by: Gabriel Krisman Bertazi <krisman@suse.de>
    Link: https://lore.kernel.org/r/7c1a2390c99e17d3ae4e8562063e572d3cdeb164.1738087204.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:30 -04:00
Jeff Moyer b0b6b90d21 io_uring: add alloc_cache.c
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit d19af0e9366298aa60afc0fb51ffcbd6205edcee
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Tue Jan 28 20:56:11 2025 +0000

    io_uring: add alloc_cache.c
    
    Avoid inlining all and everything from alloc_cache.h and move cold bits
    into a new file.
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Reviewed-by: Gabriel Krisman Bertazi <krisman@suse.de>
    Link: https://lore.kernel.org/r/06984c6cd58e703f7cfae5ab3067912f9f635a06.1738087204.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:30 -04:00
Jeff Moyer fffe897580 io_uring: dont ifdef io_alloc_cache_kasan()
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 16ac51a0a7aa051fd3b82fa077597488b5572d41
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Tue Jan 28 20:56:10 2025 +0000

    io_uring: dont ifdef io_alloc_cache_kasan()
    
    Use IS_ENABLED in io_alloc_cache_kasan() so at least it gets compile
    tested without KASAN.
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Reviewed-by: Gabriel Krisman Bertazi <krisman@suse.de>
    Link: https://lore.kernel.org/r/35e53e83f6e16478dca0028a64a6cc905dc764d3.1738087204.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:30 -04:00
Jeff Moyer e2cc6b1454 io_uring: include all deps for alloc_cache.h
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 299276502d41cd86376f47b7e087d017eaa0f914
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Tue Jan 28 20:56:09 2025 +0000

    io_uring: include all deps for alloc_cache.h
    
    alloc_cache.h uses types it doesn't declare and thus depends on the
    order in which it's included. Make it self contained and pull all needed
    definitions.
    
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Reviewed-by: Gabriel Krisman Bertazi <krisman@suse.de>
    Link: https://lore.kernel.org/r/39569f3d5b250b4fe78bb609d57f67d3736ebcc4.1738087204.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:30 -04:00
Jeff Moyer 1f93e73072 io_uring: fix multishots with selected buffers
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit d63b0e8a628e62ca85a0f7915230186bb92f8bb4
Author: Pavel Begunkov <asml.silence@gmail.com>
Date:   Tue Jan 28 00:55:24 2025 +0000

    io_uring: fix multishots with selected buffers
    
    We do io_kbuf_recycle() when arming a poll but every iteration of a
    multishot can grab more buffers, which is why we need to flush the kbuf
    ring state before continuing with waiting.
    
    Cc: stable@vger.kernel.org
    Fixes: b3fdea6ecb55c ("io_uring: multishot recv")
    Reported-by: Muhammad Ramdhan <ramdhan@starlabs.sg>
    Reported-by: Bing-Jhong Billy Jheng <billy@starlabs.sg>
    Reported-by: Jacob Soo <jacob.soo@starlabs.sg>
    Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
    Link: https://lore.kernel.org/r/1bfc9990fe435f1fc6152ca9efeba5eb3e68339c.1738025570.git.asml.silence@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:30 -04:00
Jeff Moyer 7278b9d8cd io_uring/register: use atomic_read/write for sq_flags migration
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit a23ad06bfee5e51cd9e51aebf11401e7b4b5d00a
Author: Jens Axboe <axboe@kernel.dk>
Date:   Fri Jan 24 14:32:25 2025 -0700

    io_uring/register: use atomic_read/write for sq_flags migration
    
    A previous commit changed all of the migration from the old to the new
    ring for resizing to use READ/WRITE_ONCE. However, ->sq_flags is an
    atomic_t, and while most archs won't complain on this, some will indeed
    flag this:
    
    io_uring/register.c:554:9: sparse: sparse: cast to non-scalar
    io_uring/register.c:554:9: sparse: sparse: cast from non-scalar
    
    Just use atomic_set/atomic_read for handling this case.
    
    Reported-by: kernel test robot <lkp@intel.com>
    Closes: https://lore.kernel.org/oe-kbuild-all/202501242000.A2sKqaCL-lkp@intel.com/
    Fixes: 2c5aae129f42 ("io_uring/register: document io_register_resize_rings() shared mem usage")
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:30 -04:00
Jeff Moyer ca4c960b5e futex: Pass in task to futex_queue()
JIRA: https://issues.redhat.com/browse/RHEL-105612

commit 5e0e02f0d7e52cfc8b1adfc778dd02181d8b47b4
Author: Jens Axboe <axboe@kernel.dk>
Date:   Wed Jan 15 09:05:15 2025 -0700

    futex: Pass in task to futex_queue()
    
    futex_queue() -> __futex_queue() uses 'current' as the task to store in
    the struct futex_q->task field. This is fine for synchronous usage of
    the futex infrastructure, but it's not always correct when used by
    io_uring where the task doing the initial futex_queue() might not be
    available later on. This doesn't lead to any issues currently, as the
    io_uring side doesn't support PI futexes, but it does leave a
    potentially dangling pointer which is never a good idea.
    
    Have futex_queue() take a task_struct argument, and have the regular
    callers pass in 'current' for that. Meanwhile io_uring can just pass in
    NULL, as the task should never be used off that path. In theory
    req->tctx->task could be used here, but there's no point populating it
    with a task field that will never be used anyway.
    
    Reported-by: Jann Horn <jannh@google.com>
    Signed-off-by: Jens Axboe <axboe@kernel.dk>
    Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
    Link: https://lore.kernel.org/all/22484a23-542c-4003-b721-400688a0d055@kernel.dk

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2025-10-27 13:08:30 -04:00