
static int io_sq_thread(void *data)
{
	struct io_sq_data *sqd = data;
	struct io_ring_ctx *ctx;
	unsigned long timeout = 0;
	char buf[TASK_COMM_LEN];
	DEFINE_WAIT(wait);

	sprintf(buf, "iou-sqp-%d", sqd->task_pid);
	set_task_comm(current, buf);
	sqd->thread = current;
	current->pf_io_worker = NULL;

	if (sqd->sq_cpu != -1)
		set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
	else
		set_cpus_allowed_ptr(current, cpu_online_mask);
	current->flags |= PF_NO_SETAFFINITY;

	complete(&sqd->completion);

	wait_for_completion(&sqd->startup);

	while (!io_sq_thread_should_stop(sqd)) {
		int ret;
		bool cap_entries, sqt_spin, needs_sched;

		/*
		 * Any changes to the sqd lists are synchronized through the
		 * thread parking. This synchronizes the thread vs users,
		 * the users are synchronized on the sqd->ctx_lock.
		 */
		if (io_sq_thread_should_park(sqd)) {
			io_sq_thread_parkme(sqd);
			continue;
		}
		if (unlikely(!list_empty(&sqd->ctx_new_list))) {
			io_sqd_init_new(sqd);
			timeout = jiffies + sqd->sq_thread_idle;
		}
		if (fatal_signal_pending(current))
			break;
		sqt_spin = false;
		cap_entries = !list_is_singular(&sqd->ctx_list);
		list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
			ret = __io_sq_thread(ctx, cap_entries);
			if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
				sqt_spin = true;
		}

		if (sqt_spin || !time_after(jiffies, timeout)) {
			io_run_task_work();
			cond_resched();
			if (sqt_spin)
				timeout = jiffies + sqd->sq_thread_idle;
			continue;
		}

		needs_sched = true;
		prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
		list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
			if ((ctx->flags & IORING_SETUP_IOPOLL) &&
			    !list_empty_careful(&ctx->iopoll_list)) {
				needs_sched = false;
				break;
			}
			if (io_sqring_entries(ctx)) {
				needs_sched = false;
				break;
			}
		}

		if (needs_sched && !io_sq_thread_should_park(sqd)) {
			list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
				io_ring_set_wakeup_flag(ctx);

			schedule();
			list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
				io_ring_clear_wakeup_flag(ctx);
		}

		finish_wait(&sqd->wait, &wait);
		timeout = jiffies + sqd->sq_thread_idle;
	}

	list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
		io_uring_cancel_sqpoll(ctx);

	io_run_task_work();

	if (io_sq_thread_should_park(sqd))
		io_sq_thread_parkme(sqd);

	/*
	 * Clear thread under lock so that concurrent parks work correctly
	 */
	complete(&sqd->completion);
	mutex_lock(&sqd->lock);
	sqd->thread = NULL;
	list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
		ctx->sqo_exec = 1;
		io_ring_set_wakeup_flag(ctx);
	}

	complete(&sqd->exited);
	mutex_unlock(&sqd->lock);
	do_exit(0);

static int io_sq_thread(void *data)
{
	struct io_sq_data *sqd = data;
	struct io_ring_ctx *ctx;
	unsigned long timeout = 0;
	char buf[TASK_COMM_LEN];
	DEFINE_WAIT(wait);

	sprintf(buf, "iou-sqp-%d", sqd->task_pid);
	set_task_comm(current, buf);
	sqd->thread = current;
	current->pf_io_worker = NULL;

	if (sqd->sq_cpu != -1)
		set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
	else
		set_cpus_allowed_ptr(current, cpu_online_mask);
	current->flags |= PF_NO_SETAFFINITY;

	complete(&sqd->completion);

	wait_for_completion(&sqd->startup);

	while (!io_sq_thread_should_stop(sqd)) {
		int ret;
		bool cap_entries, sqt_spin, needs_sched;

		 
		if (io_sq_thread_should_park(sqd)) {
			io_sq_thread_parkme(sqd);
			continue;
		}
		if (unlikely(!list_empty(&sqd->ctx_new_list))) {
			io_sqd_init_new(sqd);
			timeout = jiffies + sqd->sq_thread_idle;
		}
		if (fatal_signal_pending(current))
			break;
		sqt_spin = false;
		cap_entries = !list_is_singular(&sqd->ctx_list);
		list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
			ret = __io_sq_thread(ctx, cap_entries);
			if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
				sqt_spin = true;
		}

		if (sqt_spin || !time_after(jiffies, timeout)) {
			io_run_task_work();
			cond_resched();
			if (sqt_spin)
				timeout = jiffies + sqd->sq_thread_idle;
			continue;
		}

		needs_sched = true;
		prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
		list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
			if ((ctx->flags & IORING_SETUP_IOPOLL) &&
			    !list_empty_careful(&ctx->iopoll_list)) {
				needs_sched = false;
				break;
			}
			if (io_sqring_entries(ctx)) {
				needs_sched = false;
				break;
			}
		}

		if (needs_sched && !io_sq_thread_should_park(sqd)) {
			list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
				io_ring_set_wakeup_flag(ctx);

			schedule();
			list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
				io_ring_clear_wakeup_flag(ctx);
		}

		finish_wait(&sqd->wait, &wait);
		timeout = jiffies + sqd->sq_thread_idle;
	}

	list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
		io_uring_cancel_sqpoll(ctx);

	io_run_task_work();

	if (io_sq_thread_should_park(sqd))
		io_sq_thread_parkme(sqd);

	 
	complete(&sqd->completion);
	mutex_lock(&sqd->lock);
	sqd->thread = NULL;
	list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
		ctx->sqo_exec = 1;
		io_ring_set_wakeup_flag(ctx);
	}

	complete(&sqd->exited);
	mutex_unlock(&sqd->lock);
	do_exit(0);