From: Yi Sun <yi.sun@unisoc.com>
To: <sunyibuaa@gmail.com>, <tj@kernel.org>, <jiangshanlai@gmail.com>,
<jaegeuk@kernel.org>, <chao@kernel.org>
Cc: <ebiggers@google.com>, <linux-f2fs-devel@lists.sourceforge.net>,
<linux-kernel@vger.kernel.org>, <kent.overstreet@linux.dev>,
<niuzhiguo84@gmail.com>, <Hao_hao.Wang@unisoc.com>,
<yunlongxing23@gmail.com>, <yi.sun@unisoc.com>
Subject: [PATCH v2 1/2] workqueue: new struct io_work
Date: Mon, 1 Jul 2024 15:51:37 +0800 [thread overview]
Message-ID: <20240701075138.1144575-2-yi.sun@unisoc.com> (raw)
In-Reply-To: <20240701075138.1144575-1-yi.sun@unisoc.com>
Many works will go to submit_bio(), and in many cases the io priority
of kworker cannot meet the real-time requirements of this work.
So create a new struct io_work, which contains the io priority that
the kworker thread can adjust its own io priority according to.
And,
new function set_io_work_ioprio() to set the io priority of io work,
new function may_adjust_io_work_task_ioprio() to adjust kworker's io
priority,
new function restore_io_work_task_ioprio() to restore kworker's io
priority.
Signed-off-by: Yi Sun <yi.sun@unisoc.com>
---
include/linux/workqueue.h | 47 ++++++++++++++++++++++++++++++++++++
kernel/workqueue.c | 50 +++++++++++++++++++++++++++++++++++++++
2 files changed, 97 insertions(+)
diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index d9968bfc8eac..4b2cb54a68b2 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -127,6 +127,21 @@ struct rcu_work {
struct workqueue_struct *wq;
};
+/*
+ * If a work may do disk IO, it is recommended to use struct io_work
+ * instead of struct work_struct.
+ */
+struct io_work {
+ struct work_struct work;
+
+ /* If the work does submit_bio, io priority may be needed. */
+ unsigned short ioprio;
+ /* Record kworker's original io priority. */
+ unsigned short ori_ioprio;
+ /* Whether the work has set io priority? */
+ long ioprio_flag;
+};
+
enum wq_affn_scope {
WQ_AFFN_DFL, /* use system default */
WQ_AFFN_CPU, /* one pod per CPU */
@@ -218,6 +233,11 @@ static inline struct rcu_work *to_rcu_work(struct work_struct *work)
return container_of(work, struct rcu_work, work);
}
+static inline struct io_work *to_io_work(struct work_struct *work)
+{
+ return container_of(work, struct io_work, work);
+}
+
struct execute_work {
struct work_struct work;
};
@@ -347,6 +367,18 @@ static inline unsigned int work_static(struct work_struct *work) { return 0; }
#define INIT_RCU_WORK_ONSTACK(_work, _func) \
INIT_WORK_ONSTACK(&(_work)->work, (_func))
+#define INIT_IO_WORK(_work, _func) \
+ do { \
+ INIT_WORK(&(_work)->work, (_func)); \
+ (_work)->ioprio_flag = 0; \
+ } while (0)
+
+#define INIT_IO_WORK_ONSTACK(_work, _func) \
+ do { \
+ INIT_WORK_ONSTACK(&(_work)->work, (_func)); \
+ (_work)->ioprio_flag = 0; \
+ } while (0)
+
/**
* work_pending - Find out whether a work item is currently pending
* @work: The work item in question
@@ -552,6 +584,10 @@ extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
struct delayed_work *dwork, unsigned long delay);
extern bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork);
+extern void set_io_work_ioprio(struct io_work *work, unsigned short ioprio);
+extern void may_adjust_io_work_task_ioprio(struct io_work *work);
+extern void restore_io_work_task_ioprio(struct io_work *work);
+
extern void __flush_workqueue(struct workqueue_struct *wq);
extern void drain_workqueue(struct workqueue_struct *wq);
@@ -636,6 +672,17 @@ static inline bool queue_delayed_work(struct workqueue_struct *wq,
return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
}
+/**
+ * queue_io_work - queue io work on a workqueue
+ * @wq: workqueue to use
+ * @iowork: io work to queue
+ */
+static inline bool queue_io_work(struct workqueue_struct *wq,
+ struct io_work *iowork)
+{
+ return queue_work(wq, &(iowork->work));
+}
+
/**
* mod_delayed_work - modify delay of or queue a delayed work
* @wq: workqueue to use
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 3fbaecfc88c2..a55b74d5f560 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -2652,6 +2652,56 @@ bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork)
}
EXPORT_SYMBOL(queue_rcu_work);
+/**
+ * set_io_work_ioprio - set io priority for the current io work
+ * @iowork: the io work to be set
+ * @ioprio: desired io priority
+ *
+ * This function can be called after INIT_IO_WORK if the io priority
+ * of the io work needs to adjust. And it is recommended to use this
+ * function together with may_adjust_io_work_task_ioprio() and
+ * restore_io_work_task_ioprio().
+ */
+void set_io_work_ioprio(struct io_work *iowork, unsigned short ioprio)
+{
+ iowork->ioprio = ioprio;
+ iowork->ioprio_flag = 1;
+}
+EXPORT_SYMBOL(set_io_work_ioprio);
+
+/**
+ * may_adjust_io_work_task_ioprio - maybe adjust the io priority of kworker
+ * @iowork: the io work that kworker will do
+ *
+ * It is recommended to use this function together with set_io_work_ioprio()
+ * and restore_io_work_task_ioprio().
+ */
+void may_adjust_io_work_task_ioprio(struct io_work *iowork)
+{
+ if (iowork->ioprio_flag) {
+ iowork->ori_ioprio = get_current_ioprio();
+ set_task_ioprio(current, iowork->ioprio);
+ }
+}
+EXPORT_SYMBOL(may_adjust_io_work_task_ioprio);
+
+/**
+ * restore_io_work_task_ioprio - restore the io priority of kworker
+ * @iowork: the io work that kworker just did
+ *
+ * When kworker finishes the io work, the original io priority of
+ * kworker should be restored. It is recommended to use this function
+ * together with set_io_work_ioprio() and may_adjust_io_work_task_ioprio().
+ */
+void restore_io_work_task_ioprio(struct io_work *iowork)
+{
+ if (iowork->ioprio_flag) {
+ set_task_ioprio(current, iowork->ori_ioprio);
+ iowork->ioprio_flag = 0;
+ }
+}
+EXPORT_SYMBOL(restore_io_work_task_ioprio);
+
static struct worker *alloc_worker(int node)
{
struct worker *worker;
--
2.25.1
next prev parent reply other threads:[~2024-07-01 7:52 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-01 7:51 [PATCH v2 0/2] new struct io_work and use it in f2fs fsverity work Yi Sun
2024-07-01 7:51 ` Yi Sun [this message]
2024-07-01 17:32 ` [PATCH v2 1/2] workqueue: new struct io_work Tejun Heo
2024-07-02 3:53 ` Kent Overstreet
2024-07-02 9:27 ` yi sun
2024-07-02 16:40 ` Tejun Heo
2024-07-02 16:45 ` Kent Overstreet
2024-07-02 3:40 ` kernel test robot
2024-07-02 21:51 ` kernel test robot
2024-07-01 7:51 ` [PATCH v2 2/2] f2fs: set io priority for fsverity work Yi Sun
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=20240701075138.1144575-2-yi.sun@unisoc.com \
--to=yi.sun@unisoc.com \
--cc=Hao_hao.Wang@unisoc.com \
--cc=chao@kernel.org \
--cc=ebiggers@google.com \
--cc=jaegeuk@kernel.org \
--cc=jiangshanlai@gmail.com \
--cc=kent.overstreet@linux.dev \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-kernel@vger.kernel.org \
--cc=niuzhiguo84@gmail.com \
--cc=sunyibuaa@gmail.com \
--cc=tj@kernel.org \
--cc=yunlongxing23@gmail.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox