Skip to contentSkip to conversation

add io priority feature to work and use it in f2fs fsverity work

PATCH In review Yi Sun 4 participants 7 messages Jun 21, 2024 – Jun 26, 2024

Subsystems (2)
lore
Share & tools
lore

Yi Sun

author
#0 lore raw

cover add io priority feature to work and use it in f2fs fsverity work

Many works will go to submit_bio(). In many cases, the default io priority of kworker cannot meet the real-time requirements of the work. Such as f2fs_verify_bio work.

If a high-task-priority process reads the verity-file, it is likely that the high-task-priority process will be slowly because the kworker's io priority is too low.

Patch 1: Provide a set of interfaces for temporarily changing the io priority of kworker.
Patch 2: Use the interfaces provided in patch1 to temporarily change the io priority of f2fs_verify_bio work.

Yi Sun (2):
workqueue: add io priority to work_struct
f2fs: set f2fs verify work io priority

 fs/f2fs/data.c                  |  5 ++++
 include/linux/workqueue.h       |  9 ++++++
 include/linux/workqueue_types.h |  6 ++++
 kernel/workqueue.c              | 51 +++++++++++++++++++++++++++++++++
 4 files changed, 71 insertions(+)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/2 workqueue: add io priority to work_struct

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.

---
 include/linux/workqueue.h       |  9 ++++++
 include/linux/workqueue_types.h |  6 ++++
 kernel/workqueue.c              | 51 +++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+)
include/linux/workqueue.h +9 -0
index fb3993894536..f6191774b730 100644
@@ -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);
include/linux/workqueue_types.h +6 -0
index 4c38824f3ab4..d9969596bbc3 100644
@@ -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
kernel/workqueue.c +51 -0

63 lines not on this page · raw message

Signature · 1 line
2.25.1
reviewer
#2 lore raw

Re: [PATCH 1/2] workqueue: add io priority to work_struct

Hello

include/linux/workqueue_types.h struct work_struct { · line 25

Quoted 13 lines · Yi Sun

index 4c38824f3ab4..d9969596bbc3 100644

include/linux/workqueue_types.h +6 -0
@@ -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;

I don't see any ioprio code being integrated into workqueue in your patchset, from which what you need might be:

struct ioprio_work {
       /******* the work item to be scheduled *******/
       struct work_struct work;

       /******* the stuff need for ioprio ******/

       /* 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;
}

And if ioprio needs to be integrated into workqueue, it should be attributes added to the workqueue itself as in the struct workqueue_attrs.

Thanks
Lai

#3 lore raw

Re: [PATCH 1/2] workqueue: add io priority to work_struct

In reply to Yi Sun
Quoted 9 lines · Yi Sun

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.

work_struct!?

there's a lot of task_struct properties we would want work_struct to inherit if we went this route, but it's just not feasible, work_struct should be small and thin.

You're always embedding work_struct into your own struct, I would suggest tracking this yourself - or coming up with a new heavier standard struct that embeds a work_struct and has io path options, there's more than just priority.

Quoted 131 lines
---
 include/linux/workqueue.h       |  9 ++++++
 include/linux/workqueue_types.h |  6 ++++
 kernel/workqueue.c              | 51 +++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+)
include/linux/workqueue.h +9 -0
index fb3993894536..f6191774b730 100644
@@ -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);
include/linux/workqueue_types.h +6 -0
index 4c38824f3ab4..d9969596bbc3 100644
@@ -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
kernel/workqueue.c +51 -0
index 003474c9a77d..a44a8f92eec2 100644
@@ -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
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH 1/2] workqueue: add io priority to work_struct

In reply to Yi Sun
kernel test robot noticed the following build errors:

Hi Yi,

kernel test robot noticed the following build errors:

[auto build test ERROR on tj-wq/for-next]
[also build test ERROR on jaegeuk-f2fs/dev-test jaegeuk-f2fs/dev linus/master v6.10-rc5 next-20240625] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Yi-Sun/workqueue-add-io-priority-to-work_struct/20240625-165935
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tj/wq.git for-next
patch link:    https://lore.kernel.org/r/20240621062617.595007-2-yi.sun%40unisoc.com
patch subject: [PATCH 1/2] workqueue: add io priority to work_struct
config: arm-randconfig-003-20240626 (https://download.01.org/0day-ci/archive/20240626/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240626/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

arm-linux-gnueabi-ld: kernel/workqueue.o: in function `restore_work_task_ioprio':

>> workqueue.c:(.text+0xd2e): undefined reference to `set_task_ioprio'

arm-linux-gnueabi-ld: kernel/workqueue.o: in function `may_adjust_work_task_ioprio':
workqueue.c:(.text+0xd18): undefined reference to `set_task_ioprio'

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#5 lore raw

Re: [PATCH 1/2] workqueue: add io priority to work_struct

In reply to Yi Sun
kernel test robot noticed the following build errors:

Hi Yi,

kernel test robot noticed the following build errors:

[auto build test ERROR on tj-wq/for-next]
[also build test ERROR on jaegeuk-f2fs/dev-test jaegeuk-f2fs/dev linus/master v6.10-rc5 next-20240625] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Yi-Sun/workqueue-add-io-priority-to-work_struct/20240625-165935
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tj/wq.git for-next
patch link:    https://lore.kernel.org/r/20240621062617.595007-2-yi.sun%40unisoc.com
patch subject: [PATCH 1/2] workqueue: add io priority to work_struct
config: csky-randconfig-001-20240626 (https://download.01.org/0day-ci/archive/20240626/[email protected]/config)
compiler: csky-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240626/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

csky-linux-ld: kernel/workqueue.o: in function `may_adjust_work_task_ioprio':
workqueue.c:(.text+0x460): undefined reference to `set_task_ioprio'
csky-linux-ld: kernel/workqueue.o: in function `restore_work_task_ioprio':
workqueue.c:(.text+0x47c): undefined reference to `set_task_ioprio'

>> csky-linux-ld: workqueue.c:(.text+0x4bc): undefined reference to `set_task_ioprio'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author
#6 lore raw

PATCH 2/2 f2fs: set f2fs verify work io priority

In reply to Yi Sun

The io priority of reading the original data of the verity-file and reading hash blocks during verification should be consistent. If the io priority is not set, when a high-task-priority process reads the verity-file, the kworker's io priority may be too low, resulting in delayed reading of hash blocks, which eventually causes the high-task-priority process to response slowly.

---
 fs/f2fs/data.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/f2fs/data.c +5 -0
index b9b0debc6b3d..705a2bdf0848 100644
@@ -172,6 +172,8 @@ static void f2fs_verify_bio(struct work_struct *work)
 	struct bio *bio = ctx->bio;
 	bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS);
 
+	may_adjust_work_task_ioprio(work);
+
 	/*
 	 * fsverity_verify_bio() may call readahead() again, and while verity
 	 * will be disabled for this, decryption and/or decompression may still
@@ -204,6 +206,8 @@ static void f2fs_verify_bio(struct work_struct *work)
 	}
 
 	f2fs_finish_read_bio(bio, true);
+
+	restore_work_task_ioprio(work);
 }
 
 /*
@@ -221,6 +225,7 @@ static void f2fs_verify_and_finish_bio(struct bio *bio, bool in_task)
 
 	if (ctx && (ctx->enabled_steps & STEP_VERITY)) {
 		INIT_WORK(&ctx->work, f2fs_verify_bio);
+		set_work_ioprio(&ctx->work, bio->bi_ioprio);
 		fsverity_enqueue_verify_work(&ctx->work);
 	} else {
 		f2fs_finish_read_bio(bio, in_task);
Signature · 1 line
2.25.1