spin_unlock(&wq->hash->wait.lock);
 }
 
-static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
+/*
+ * We can always run the work if the worker is currently the same type as
+ * the work (eg both are bound, or both are unbound). If they are not the
+ * same, only allow it if incrementing the worker count would be allowed.
+ */
+static bool io_worker_can_run_work(struct io_worker *worker,
+                                  struct io_wq_work *work)
+{
+       struct io_wqe_acct *acct;
+
+       if (!(worker->flags & IO_WORKER_F_BOUND) !=
+           !(work->flags & IO_WQ_WORK_UNBOUND))
+               return true;
+
+       /* not the same type, check if we'd go over the limit */
+       acct = io_work_get_acct(worker->wqe, work);
+       return acct->nr_workers < acct->max_workers;
+}
+
+static struct io_wq_work *io_get_next_work(struct io_wqe *wqe,
+                                          struct io_worker *worker,
+                                          bool *stalled)
        __must_hold(wqe->lock)
 {
        struct io_wq_work_node *node, *prev;
 
                work = container_of(node, struct io_wq_work, list);
 
+               if (!io_worker_can_run_work(worker, work))
+                       break;
+
                /* not hashed, can run anytime */
                if (!io_wq_is_hashed(work)) {
                        wq_list_del(&wqe->work_list, node, prev);
                raw_spin_unlock(&wqe->lock);
                io_wait_on_hash(wqe, stall_hash);
                raw_spin_lock(&wqe->lock);
+               *stalled = true;
        }
 
        return NULL;
 
        do {
                struct io_wq_work *work;
+               bool stalled;
 get_next:
                /*
                 * If we got some work, mark us as busy. If we didn't, but
                 * can't make progress, any work completion or insertion will
                 * clear the stalled flag.
                 */
-               work = io_get_next_work(wqe);
+               stalled = false;
+               work = io_get_next_work(wqe, worker, &stalled);
                if (work)
                        __io_worker_busy(wqe, worker, work);
-               else if (!wq_list_empty(&wqe->work_list))
+               else if (stalled)
                        wqe->flags |= IO_WQE_FLAG_STALLED;
 
                raw_spin_unlock_irq(&wqe->lock);