Skip to content

Commit 9fd9978

Browse files
committed
io_uring: add task fork hook
Called when copy_process() is called to copy state to a new child. Right now this is just a stub, but will be used shortly to properly handle fork'ing of task based io_uring restrictions. Reviewed-by: Christian Brauner (Microsoft) <brauner@kernel.org> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent e7f67c2 commit 9fd9978

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

include/linux/io_uring.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ void __io_uring_free(struct task_struct *tsk);
1212
void io_uring_unreg_ringfd(void);
1313
const char *io_uring_get_opcode(u8 opcode);
1414
bool io_is_uring_fops(struct file *file);
15+
int __io_uring_fork(struct task_struct *tsk);
1516

1617
static inline void io_uring_files_cancel(void)
1718
{
@@ -25,9 +26,16 @@ static inline void io_uring_task_cancel(void)
2526
}
2627
static inline void io_uring_free(struct task_struct *tsk)
2728
{
28-
if (tsk->io_uring)
29+
if (tsk->io_uring || tsk->io_uring_restrict)
2930
__io_uring_free(tsk);
3031
}
32+
static inline int io_uring_fork(struct task_struct *tsk)
33+
{
34+
if (tsk->io_uring_restrict)
35+
return __io_uring_fork(tsk);
36+
37+
return 0;
38+
}
3139
#else
3240
static inline void io_uring_task_cancel(void)
3341
{
@@ -46,6 +54,10 @@ static inline bool io_is_uring_fops(struct file *file)
4654
{
4755
return false;
4856
}
57+
static inline int io_uring_fork(struct task_struct *tsk)
58+
{
59+
return 0;
60+
}
4961
#endif
5062

5163
#endif

include/linux/sched.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,7 @@ struct task_struct {
11901190

11911191
#ifdef CONFIG_IO_URING
11921192
struct io_uring_task *io_uring;
1193+
struct io_restriction *io_uring_restrict;
11931194
#endif
11941195

11951196
/* Namespaces: */

io_uring/tctx.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,18 @@ void __io_uring_free(struct task_struct *tsk)
5454
* node is stored in the xarray. Until that gets sorted out, attempt
5555
* an iteration here and warn if any entries are found.
5656
*/
57-
xa_for_each(&tctx->xa, index, node) {
58-
WARN_ON_ONCE(1);
59-
break;
60-
}
61-
WARN_ON_ONCE(tctx->io_wq);
62-
WARN_ON_ONCE(tctx->cached_refs);
57+
if (tctx) {
58+
xa_for_each(&tctx->xa, index, node) {
59+
WARN_ON_ONCE(1);
60+
break;
61+
}
62+
WARN_ON_ONCE(tctx->io_wq);
63+
WARN_ON_ONCE(tctx->cached_refs);
6364

64-
percpu_counter_destroy(&tctx->inflight);
65-
kfree(tctx);
66-
tsk->io_uring = NULL;
65+
percpu_counter_destroy(&tctx->inflight);
66+
kfree(tctx);
67+
tsk->io_uring = NULL;
68+
}
6769
}
6870

6971
__cold int io_uring_alloc_task_context(struct task_struct *task,
@@ -351,3 +353,8 @@ int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg,
351353

352354
return i ? i : ret;
353355
}
356+
357+
int __io_uring_fork(struct task_struct *tsk)
358+
{
359+
return 0;
360+
}

kernel/fork.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
#include <linux/kasan.h>
9898
#include <linux/scs.h>
9999
#include <linux/io_uring.h>
100+
#include <linux/io_uring_types.h>
100101
#include <linux/bpf.h>
101102
#include <linux/stackprotector.h>
102103
#include <linux/user_events.h>
@@ -2129,6 +2130,10 @@ __latent_entropy struct task_struct *copy_process(
21292130

21302131
#ifdef CONFIG_IO_URING
21312132
p->io_uring = NULL;
2133+
retval = io_uring_fork(p);
2134+
if (unlikely(retval))
2135+
goto bad_fork_cleanup_delayacct;
2136+
retval = -EAGAIN;
21322137
#endif
21332138

21342139
p->default_timer_slack_ns = current->timer_slack_ns;
@@ -2525,6 +2530,7 @@ __latent_entropy struct task_struct *copy_process(
25252530
mpol_put(p->mempolicy);
25262531
#endif
25272532
bad_fork_cleanup_delayacct:
2533+
io_uring_free(p);
25282534
delayacct_tsk_free(p);
25292535
bad_fork_cleanup_count:
25302536
dec_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);

0 commit comments

Comments
 (0)