projects/linux·blob master · refs

117 lines · 3.2 KiB

1// SPDX-License-Identifier: GPL-2.0
2#ifndef IOU_TW_H
3#define IOU_TW_H
4
5#include <linux/sched.h>
6#include <linux/percpu-refcount.h>
7#include <linux/io_uring_types.h>
8
9#include "mpscq.h"
10
11#define IO_LOCAL_TW_DEFAULT_MAX 20
12
13/*
14 * Terminate the request if either of these conditions are true:
15 *
16 * 1) It's being executed by the original task, but that task is marked
17 * with PF_EXITING as it's exiting.
18 * 2) PF_KTHREAD is set, in which case the invoker of the task_work is
19 * our fallback task_work.
20 * 3) The ring has been closed and is going away.
21 */
22static inline bool io_should_terminate_tw(struct io_ring_ctx *ctx)
23{
24 return (current->flags & (PF_EXITING | PF_KTHREAD)) || percpu_ref_is_dying(&ctx->refs);
25}
26
27void io_req_task_work_add_remote(struct io_kiocb *req, unsigned flags);
28void tctx_task_work(struct callback_head *cb);
29void io_tctx_fallback_work(struct work_struct *work);
30int io_run_local_work(struct io_ring_ctx *ctx, int min_events, int max_events);
31int io_run_task_work_sig(struct io_ring_ctx *ctx);
32
33__cold void io_cancel_local_task_work(struct io_ring_ctx *ctx);
34int io_run_local_work_locked(struct io_ring_ctx *ctx, int min_events);
35
36void io_req_local_work_add(struct io_kiocb *req, unsigned flags);
37void io_req_normal_work_add(struct io_kiocb *req);
38void tctx_task_work_run(struct io_uring_task *tctx, unsigned int max_entries, unsigned int *count);
39
40static inline void __io_req_task_work_add(struct io_kiocb *req, unsigned flags)
41{
42 if (req->ctx->flags & IORING_SETUP_DEFER_TASKRUN)
43 io_req_local_work_add(req, flags);
44 else
45 io_req_normal_work_add(req);
46}
47
48static inline void io_req_task_work_add(struct io_kiocb *req)
49{
50 __io_req_task_work_add(req, 0);
51}
52
53static inline int io_run_task_work(void)
54{
55 bool ret = false;
56
57 /*
58 * Always check-and-clear the task_work notification signal. With how
59 * signaling works for task_work, we can find it set with nothing to
60 * run. We need to clear it for that case, like get_signal() does.
61 */
62 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
63 clear_notify_signal();
64 /*
65 * PF_IO_WORKER never returns to userspace, so check here if we have
66 * notify work that needs processing.
67 */
68 if (current->flags & PF_IO_WORKER) {
69 if (test_thread_flag(TIF_NOTIFY_RESUME)) {
70 __set_current_state(TASK_RUNNING);
71 resume_user_mode_work(NULL);
72 }
73 if (current->io_uring) {
74 unsigned int count = 0;
75
76 __set_current_state(TASK_RUNNING);
77 tctx_task_work_run(current->io_uring, UINT_MAX, &count);
78 if (count)
79 ret = true;
80 }
81 }
82 if (task_work_pending(current)) {
83 __set_current_state(TASK_RUNNING);
84 task_work_run();
85 ret = true;
86 }
87
88 return ret;
89}
90
91static inline bool io_local_work_pending(struct io_ring_ctx *ctx)
92{
93 return !mpscq_empty(&ctx->work_list);
94}
95
96static inline bool io_task_work_pending(struct io_ring_ctx *ctx)
97{
98 return task_work_pending(current) || io_local_work_pending(ctx);
99}
100
101static inline void io_tw_lock(struct io_ring_ctx *ctx, io_tw_token_t tw)
102{
103 lockdep_assert_held(&ctx->uring_lock);
104}
105
106static inline bool io_allowed_defer_tw_run(struct io_ring_ctx *ctx)
107{
108 return likely(ctx->submitter_task == current);
109}
110
111static inline bool io_allowed_run_tw(struct io_ring_ctx *ctx)
112{
113 return likely(!(ctx->flags & IORING_SETUP_DEFER_TASKRUN) ||
114 ctx->submitter_task == current);
115}
116
117#endif