mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yi Sun <yi.sun@unisoc.com>
To: <sunyibuaa@gmail.com>, <tj@kernel.org>, <jaegeuk@kernel.org>,
	<chao@kernel.org>
Cc: <ebiggers@google.com>, <jiangshanlai@gmail.com>,
	<kent.overstreet@linux.dev>, <linux-kernel@vger.kernel.org>,
	<linux-f2fs-devel@lists.sourceforge.net>, <niuzhiguo84@gmail.com>,
	<Hao_hao.Wang@unisoc.com>, <yunlongxing23@gmail.com>
Subject: [PATCH 1/2] workqueue: add io priority to work_struct
Date: Fri, 21 Jun 2024 14:26:16 +0800	[thread overview]
Message-ID: <20240621062617.595007-2-yi.sun@unisoc.com> (raw)
In-Reply-To: <20240621062617.595007-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 add the basic attribute ioprio to work_struct, and kworker can adjust
its io priority according to this attribute.

Add function set_work_ioprio() to set the io priority of this work.
Add function may_adjust_work_task_ioprio() to adjust kworker's io priority.
Add function restore_work_task_ioprio() to restore kworker's io priority.

Signed-off-by: Yi Sun <yi.sun@unisoc.com>
---
 include/linux/workqueue.h       |  9 ++++++
 include/linux/workqueue_types.h |  6 ++++
 kernel/workqueue.c              | 51 +++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+)

diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index fb3993894536..f6191774b730 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -286,6 +286,9 @@ static inline unsigned int work_static(struct work_struct *work) { return 0; }
 		lockdep_init_map(&(_work)->lockdep_map, "(work_completion)"#_work, (_key), 0); \
 		INIT_LIST_HEAD(&(_work)->entry);			\
 		(_work)->func = (_func);				\
+		(_work)->ioprio = 0;					\
+		(_work)->ori_ioprio = 0;				\
+		(_work)->ioprio_flag = 0;				\
 	} while (0)
 #else
 #define __INIT_WORK_KEY(_work, _func, _onstack, _key)			\
@@ -294,6 +297,9 @@ static inline unsigned int work_static(struct work_struct *work) { return 0; }
 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
 		INIT_LIST_HEAD(&(_work)->entry);			\
 		(_work)->func = (_func);				\
+		(_work)->ioprio = 0;					\
+		(_work)->ori_ioprio = 0;				\
+		(_work)->ioprio_flag = 0;				\
 	} while (0)
 #endif
 
@@ -585,6 +591,9 @@ extern struct work_struct *current_work(void);
 extern bool current_is_workqueue_rescuer(void);
 extern bool workqueue_congested(int cpu, struct workqueue_struct *wq);
 extern unsigned int work_busy(struct work_struct *work);
+extern void set_work_ioprio(struct work_struct *work, unsigned short ioprio);
+extern void may_adjust_work_task_ioprio(struct work_struct *work);
+extern void restore_work_task_ioprio(struct work_struct *work);
 extern __printf(1, 2) void set_worker_desc(const char *fmt, ...);
 extern void print_worker_info(const char *log_lvl, struct task_struct *task);
 extern void show_all_workqueues(void);
diff --git a/include/linux/workqueue_types.h b/include/linux/workqueue_types.h
index 4c38824f3ab4..d9969596bbc3 100644
--- a/include/linux/workqueue_types.h
+++ b/include/linux/workqueue_types.h
@@ -17,6 +17,12 @@ struct work_struct {
 	atomic_long_t data;
 	struct list_head entry;
 	work_func_t func;
+	/* 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;
 #ifdef CONFIG_LOCKDEP
 	struct lockdep_map lockdep_map;
 #endif
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 003474c9a77d..a44a8f92eec2 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -55,6 +55,7 @@
 #include <linux/kvm_para.h>
 #include <linux/delay.h>
 #include <linux/irq_work.h>
+#include <linux/ioprio.h>
 
 #include "workqueue_internal.h"
 
@@ -6025,6 +6026,56 @@ unsigned int work_busy(struct work_struct *work)
 }
 EXPORT_SYMBOL_GPL(work_busy);
 
+/**
+ * set_work_ioprio - set io priority for the current work
+ * @work: the work to be set
+ * @ioprio: desired io priority
+ *
+ * This function can be called after INIT_WORK if the io priority
+ * of the work needs to adjust. And it is recommended to use this
+ * function together with may_adjust_work_task_ioprio() and
+ * restore_work_task_ioprio().
+ */
+void set_work_ioprio(struct work_struct *work, unsigned short ioprio)
+{
+	work->ioprio = ioprio;
+	work->ioprio_flag = 1;
+}
+EXPORT_SYMBOL_GPL(set_work_ioprio);
+
+/**
+ * may_adjust_work_task_ioprio - adjust the io priority of kworker
+ * @work: the work that kworker will do
+ *
+ * It is recommended to use this function together with set_work_ioprio()
+ * and restore_work_task_ioprio().
+ */
+void may_adjust_work_task_ioprio(struct work_struct *work)
+{
+	if (work->ioprio_flag) {
+		work->ori_ioprio = get_current_ioprio();
+		set_task_ioprio(current, work->ioprio);
+	}
+}
+EXPORT_SYMBOL_GPL(may_adjust_work_task_ioprio);
+
+/**
+ * restore_work_task_ioprio - restore the io priority of kworker
+ * @work: the work that kworker just did
+ *
+ * When kworker finishes the work, the original io priority of
+ * kworker should be restored. It is recommended to use this function
+ * together with set_work_ioprio() and may_adjust_work_task_ioprio().
+ */
+void restore_work_task_ioprio(struct work_struct *work)
+{
+	if (work->ioprio_flag) {
+		set_task_ioprio(current, work->ori_ioprio);
+		work->ioprio_flag = 0;
+	}
+}
+EXPORT_SYMBOL_GPL(restore_work_task_ioprio);
+
 /**
  * set_worker_desc - set description for the current work item
  * @fmt: printf-style format string
-- 
2.25.1


  reply	other threads:[~2024-06-21  6:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-21  6:26 [PATCH 0/2] add io priority feature to work and use it in f2fs fsverity work Yi Sun
2024-06-21  6:26 ` Yi Sun [this message]
2024-06-21  8:00   ` [PATCH 1/2] workqueue: add io priority to work_struct Lai Jiangshan
2024-06-21 14:23   ` Kent Overstreet
2024-06-26  4:32   ` kernel test robot
2024-06-26  5:17   ` kernel test robot
2024-06-21  6:26 ` [PATCH 2/2] f2fs: set f2fs verify work io priority 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=20240621062617.595007-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

all inboxes | Powered by JetHome®