Skip to contentSkip to conversation

io_uring: apply worker limits to previous users

PATCH Applied Pavel Begunkov 3 participants 3 messages Oct 21, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 io_uring: apply worker limits to previous users

Another change to the API io-wq worker limitation API added in 5.15, apply the limit to all prior users that already registered a tctx. It may be confusing as it's now, in particular the change covers the following 2 cases:

TASK1 | TASK2
_________________________________________________

ring = create()         |
                        | limit_iowq_workers()
*not limited*           |

TASK1 | TASK2
_________________________________________________

ring = create()         |
                        | issue_requests()
limit_iowq_workers()    |
                        | *not limited*

A note on locking, it's safe to traverse ->tctx_list as we hold ->uring_lock, but do that after dropping sqd->lock to avoid possible problems. It's also safe to access tctx->io_wq there because tasks kill it only after removing themselves from tctx_list, see io_uring_cancel_generic() -> io_uring_clean_tctx()

---
 fs/io_uring.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
fs/io_uring.c +18 -0
index d5cc103224f1..bc18af5e0a93 100644
@@ -10649,7 +10649,9 @@ static int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
 
 static int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
 					void __user *arg)
+	__must_hold(&ctx->uring_lock)
 {
+	struct io_tctx_node *node;
 	struct io_uring_task *tctx = NULL;
 	struct io_sq_data *sqd = NULL;
 	__u32 new_count[2];
@@ -10702,6 +10704,22 @@ static int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
 	if (copy_to_user(arg, new_count, sizeof(new_count)))
 		return -EFAULT;
 
+	/* that's it for SQPOLL, only the SQPOLL task creates requests */
+	if (sqd)
+		return 0;
+
+	/* now propagate the restriction to all registered users */
+	list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
+		struct io_uring_task *tctx = node->task->io_uring;
+
+		if (WARN_ON_ONCE(!tctx->io_wq))
+			continue;
+
+		for (i = 0; i < ARRAY_SIZE(new_count); i++)
+			new_count[i] = ctx->iowq_limits[i];
+		/* ignore errors, it always returns zero anyway */
+		(void)io_wq_max_workers(tctx->io_wq, new_count);
+	}
 	return 0;
 err:
 	if (sqd) {
Signature · 1 line
2.33.1
maintainer Applied
#1 lore raw
Quoted 5 lines · Pavel Begunkov

Another change to the API io-wq worker limitation API added in 5.15, apply the limit to all prior users that already registered a tctx. It may be confusing as it's now, in particular the change covers the following 2 cases:

Quoted 7 lines

TASK1 | TASK2
_________________________________________________

ring = create()         |
                        | limit_iowq_workers()
*not limited*           |

[...]

Applied, thanks!

[1/1] io_uring: apply worker limits to previous users

      commit: 7c810c192d6c18213db7cafc647a46398519094a

Best regards,

Signature · 1 line
Jens Axboe
Reviewed-by on v1 1/1
#2 lore raw
In reply to Pavel Begunkov
Quoted 57 lines · Pavel Begunkov

Another change to the API io-wq worker limitation API added in 5.15, apply the limit to all prior users that already registered a tctx. It may be confusing as it's now, in particular the change covers the following 2 cases:

TASK1 | TASK2
_________________________________________________
ring = create() |
| limit_iowq_workers()
*not limited* |

TASK1 | TASK2
_________________________________________________
ring = create() |
| issue_requests()
limit_iowq_workers() |
| *not limited*

A note on locking, it's safe to traverse ->tctx_list as we hold ->uring_lock, but do that after dropping sqd->lock to avoid possible problems. It's also safe to access tctx->io_wq there because tasks kill it only after removing themselves from tctx_list, see io_uring_cancel_generic() -> io_uring_clean_tctx()

---
  fs/io_uring.c | 18 ++++++++++++++++++
  1 file changed, 18 insertions(+)
fs/io_uring.c +11 -0
index d5cc103224f1..bc18af5e0a93 100644
@@ -10649,7 +10649,9 @@ static int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
 
  static int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
  					void __user *arg)
+	__must_hold(&ctx->uring_lock)
  {
+	struct io_tctx_node *node;
  	struct io_uring_task *tctx = NULL;
  	struct io_sq_data *sqd = NULL;
  	__u32 new_count[2];
@@ -10702,6 +10704,22 @@ static int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
  	if (copy_to_user(arg, new_count, sizeof(new_count)))
  		return -EFAULT;
 
+	/* that's it for SQPOLL, only the SQPOLL task creates requests */
+	if (sqd)
+		return 0;
+
+	/* now propagate the restriction to all registered users */
+	list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
+		struct io_uring_task *tctx = node->task->io_uring;
+
+		if (WARN_ON_ONCE(!tctx->io_wq))

fs/io_uring.c static int io_register_iowq_max_workers(struct io_ring_ctx *ctx, · line 10725

Quoted 10 lines
fs/io_uring.c +7 -0
+			continue;
+
+		for (i = 0; i < ARRAY_SIZE(new_count); i++)
+			new_count[i] = ctx->iowq_limits[i];
+		/* ignore errors, it always returns zero anyway */
+		(void)io_wq_max_workers(tctx->io_wq, new_count);
+	}
  	return 0;
  err:
  	if (sqd) {