All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hao Xu <haoxu.linux@icloud.com>
To: io-uring@vger.kernel.org
Cc: Jens Axboe <axboe@kernel.dk>, Pavel Begunkov <asml.silence@gmail.com>
Subject: [PATCH 03/11] io-wq: add infra data structure for fixed workers
Date: Sun, 15 May 2022 21:12:22 +0800	[thread overview]
Message-ID: <20220515131230.155267-4-haoxu.linux@icloud.com> (raw)
In-Reply-To: <20220515131230.155267-1-haoxu.linux@icloud.com>

From: Hao Xu <haoxu.linux@gmail.com>

From: Hao Xu <howeyxu@tencent.com>

Add data sttructure and basic initialization for fixed worker.

Signed-off-by: Hao Xu <howeyxu@tencent.com>
---
 fs/io-wq.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 94 insertions(+), 11 deletions(-)

diff --git a/fs/io-wq.c b/fs/io-wq.c
index 35ce622f77ba..73fbe62617b7 100644
--- a/fs/io-wq.c
+++ b/fs/io-wq.c
@@ -26,6 +26,7 @@ enum {
 	IO_WORKER_F_RUNNING	= 2,	/* account as running */
 	IO_WORKER_F_FREE	= 4,	/* worker on free list */
 	IO_WORKER_F_BOUND	= 8,	/* is doing bounded work */
+	IO_WORKER_F_FIXED	= 16,	/* is a fixed worker */
 	IO_WORKER_F_EXIT	= 32,	/* worker is exiting */
 };
 
@@ -37,6 +38,68 @@ enum {
 	IO_ACCT_STALLED_BIT	= 0,	/* stalled on hash */
 };
 
+struct io_wqe_acct {
+/*
+ *	union {
+ *		// (1) for wqe->acct (normal worker)
+ *		struct {
+ *			unsigned nr_workers;
+ *			unsigned max_workers;
+ *			struct io_wq_work_list work_list;
+ *		};
+ *		// (2) for wqe->fixed_acct (fixed worker)
+ *		struct {
+ *			unsigned nr_workers;
+ *			unsigned max_workers;
+ *			unsigned nr_fixed;
+ *			unsigned max_works;
+ *			struct io_worker **fixed_workers;
+ *		};
+ *		// (3) for fixed worker's private acct
+ *		struct {
+ *			unsigned nr_works;
+ *			unsigned max_works;
+ *			struct io_wq_work_list work_list;
+ *		};
+ *	};
+ */
+	union {
+		struct {
+			unsigned nr_workers;
+			unsigned max_workers;
+		};
+		struct {
+			unsigned nr_works;
+		};
+	};
+	unsigned max_works;
+	union {
+		struct io_wq_work_list work_list;
+		struct {
+			unsigned nr_fixed;
+			struct io_worker **fixed_workers;
+		};
+	};
+
+	/*
+	 * nr_running is not meaningful for fixed worker
+	 * but still keep the same logic for it for the
+	 * convinence for now. So do nr_workers and
+	 * max_workers.
+	 */
+	atomic_t nr_running;
+	/*
+	 * For 1), it protects the work_list, the other two member nr_workers
+	 * and max_workers are protected by wqe->lock.
+	 * For 2), it protects nr_fixed, max_works, fixed_workers
+	 * For 3), it protects nr_works, max_works and work_list.
+	 */
+	raw_spinlock_t lock;
+	int index;
+	unsigned long flags;
+	bool fixed_worker_registered;
+};
+
 /*
  * One for each thread in a wqe pool
  */
@@ -62,6 +125,8 @@ struct io_worker {
 		struct rcu_head rcu;
 		struct work_struct work;
 	};
+	int index;
+	struct io_wqe_acct acct;
 };
 
 #if BITS_PER_LONG == 64
@@ -72,16 +137,6 @@ struct io_worker {
 
 #define IO_WQ_NR_HASH_BUCKETS	(1u << IO_WQ_HASH_ORDER)
 
-struct io_wqe_acct {
-	unsigned nr_workers;
-	unsigned max_workers;
-	int index;
-	atomic_t nr_running;
-	raw_spinlock_t lock;
-	struct io_wq_work_list work_list;
-	unsigned long flags;
-};
-
 enum {
 	IO_WQ_ACCT_BOUND,
 	IO_WQ_ACCT_UNBOUND,
@@ -94,6 +149,7 @@ enum {
 struct io_wqe {
 	raw_spinlock_t lock;
 	struct io_wqe_acct acct[IO_WQ_ACCT_NR];
+	struct io_wqe_acct fixed_acct[IO_WQ_ACCT_NR];
 
 	int node;
 
@@ -1205,6 +1261,31 @@ struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
 			atomic_set(&acct->nr_running, 0);
 			INIT_WQ_LIST(&acct->work_list);
 			raw_spin_lock_init(&acct->lock);
+
+			acct = &wqe->fixed_acct[i];
+			acct->index = i;
+			INIT_WQ_LIST(&acct->work_list);
+			raw_spin_lock_init(&acct->lock);
+			/*
+			 * nr_running for a fixed worker is meaningless
+			 * for now, init it to 1 to wround around the
+			 * io_wqe_dec_running logic
+			 */
+			atomic_set(&acct->nr_running, 1);
+			/*
+			 * max_workers for a fixed worker is meaningless
+			 * for now, init it so since number of fixed workers
+			 * should be controlled by users.
+			 */
+			acct->max_workers = task_rlimit(current, RLIMIT_NPROC);
+			raw_spin_lock_init(&acct->lock);
+			/*
+			 * For fixed worker, not necessary
+			 * but do it explicitly for clearity
+			 */
+			acct->nr_fixed = 0;
+			acct->max_works = 0;
+			acct->fixed_workers = NULL;
 		}
 		wqe->wq = wq;
 		raw_spin_lock_init(&wqe->lock);
@@ -1287,7 +1368,7 @@ static void io_wq_exit_workers(struct io_wq *wq)
 
 static void io_wq_destroy(struct io_wq *wq)
 {
-	int node;
+	int i, node;
 
 	cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
 
@@ -1299,6 +1380,8 @@ static void io_wq_destroy(struct io_wq *wq)
 		};
 		io_wqe_cancel_pending_work(wqe, &match);
 		free_cpumask_var(wqe->cpu_mask);
+		for (i = 0; i < IO_WQ_ACCT_NR; i++)
+			kfree(wqe->fixed_acct[i].fixed_workers);
 		kfree(wqe);
 	}
 	io_wq_put_hash(wq->hash);
-- 
2.25.1


  parent reply	other threads:[~2022-05-15 13:12 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-15 13:12 [PATCH v4 00/11] fixed worker Hao Xu
2022-05-15 13:12 ` [PATCH 01/11] io-wq: add a worker flag for individual exit Hao Xu
2022-05-15 13:12 ` [PATCH 02/11] io-wq: change argument of create_io_worker() for convienence Hao Xu
2022-05-15 13:12 ` Hao Xu [this message]
2022-05-15 13:12 ` [PATCH 04/11] io-wq: tweak io_get_acct() Hao Xu
2022-05-15 13:12 ` [PATCH 05/11] io-wq: fixed worker initialization Hao Xu
2022-05-15 13:12 ` [PATCH 06/11] io-wq: fixed worker exit Hao Xu
2022-05-15 13:12 ` [PATCH 07/11] io-wq: implement fixed worker logic Hao Xu
2022-05-15 13:12 ` [PATCH 08/11] io-wq: batch the handling of fixed worker private works Hao Xu
2022-05-15 13:12 ` [PATCH 09/11] io_uring: add register fixed worker interface Hao Xu
2022-05-15 13:12 ` [PATCH 10/11] io-wq: add an work list for fixed worker Hao Xu
2022-05-15 13:12 ` [PATCH 11/11] io_uring: cancel works in exec " Hao Xu
2022-05-31  7:05 ` [PATCH v4 00/11] " Hao Xu
2022-05-31  8:46   ` Jens Axboe
2022-05-31  8:55     ` Hao Xu
2022-05-31  9:02       ` Jens Axboe
2022-05-31  9:09         ` Hao Xu
2022-06-27 13:35 [PATCH v5 " Hao Xu
2022-06-27 13:35 ` [PATCH 03/11] io-wq: add infra data structure for fixed workers Hao Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220515131230.155267-4-haoxu.linux@icloud.com \
    --to=haoxu.linux@icloud.com \
    --cc=asml.silence@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=io-uring@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.