xref: /OK3568_Linux_fs/kernel/include/linux/ioprio.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef IOPRIO_H
3*4882a593Smuzhiyun #define IOPRIO_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/sched.h>
6*4882a593Smuzhiyun #include <linux/sched/rt.h>
7*4882a593Smuzhiyun #include <linux/iocontext.h>
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  * Gives us 8 prio classes with 13-bits of data for each class
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun #define IOPRIO_CLASS_SHIFT	(13)
13*4882a593Smuzhiyun #define IOPRIO_PRIO_MASK	((1UL << IOPRIO_CLASS_SHIFT) - 1)
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #define IOPRIO_PRIO_CLASS(mask)	((mask) >> IOPRIO_CLASS_SHIFT)
16*4882a593Smuzhiyun #define IOPRIO_PRIO_DATA(mask)	((mask) & IOPRIO_PRIO_MASK)
17*4882a593Smuzhiyun #define IOPRIO_PRIO_VALUE(class, data)	(((class) << IOPRIO_CLASS_SHIFT) | data)
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #define ioprio_valid(mask)	(IOPRIO_PRIO_CLASS((mask)) != IOPRIO_CLASS_NONE)
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun  * These are the io priority groups as implemented by CFQ. RT is the realtime
23*4882a593Smuzhiyun  * class, it always gets premium service. BE is the best-effort scheduling
24*4882a593Smuzhiyun  * class, the default for any process. IDLE is the idle scheduling class, it
25*4882a593Smuzhiyun  * is only served when no one else is using the disk.
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun enum {
28*4882a593Smuzhiyun 	IOPRIO_CLASS_NONE,
29*4882a593Smuzhiyun 	IOPRIO_CLASS_RT,
30*4882a593Smuzhiyun 	IOPRIO_CLASS_BE,
31*4882a593Smuzhiyun 	IOPRIO_CLASS_IDLE,
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun  * 8 best effort priority levels are supported
36*4882a593Smuzhiyun  */
37*4882a593Smuzhiyun #define IOPRIO_BE_NR	(8)
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun enum {
40*4882a593Smuzhiyun 	IOPRIO_WHO_PROCESS = 1,
41*4882a593Smuzhiyun 	IOPRIO_WHO_PGRP,
42*4882a593Smuzhiyun 	IOPRIO_WHO_USER,
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun  * Fallback BE priority
47*4882a593Smuzhiyun  */
48*4882a593Smuzhiyun #define IOPRIO_NORM	(4)
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun  * if process has set io priority explicitly, use that. if not, convert
52*4882a593Smuzhiyun  * the cpu scheduler nice value to an io priority
53*4882a593Smuzhiyun  */
task_nice_ioprio(struct task_struct * task)54*4882a593Smuzhiyun static inline int task_nice_ioprio(struct task_struct *task)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	return (task_nice(task) + 20) / 5;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun  * This is for the case where the task hasn't asked for a specific IO class.
61*4882a593Smuzhiyun  * Check for idle and rt task process, and return appropriate IO class.
62*4882a593Smuzhiyun  */
task_nice_ioclass(struct task_struct * task)63*4882a593Smuzhiyun static inline int task_nice_ioclass(struct task_struct *task)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	if (task->policy == SCHED_IDLE)
66*4882a593Smuzhiyun 		return IOPRIO_CLASS_IDLE;
67*4882a593Smuzhiyun 	else if (task_is_realtime(task))
68*4882a593Smuzhiyun 		return IOPRIO_CLASS_RT;
69*4882a593Smuzhiyun 	else
70*4882a593Smuzhiyun 		return IOPRIO_CLASS_BE;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun  * If the calling process has set an I/O priority, use that. Otherwise, return
75*4882a593Smuzhiyun  * the default I/O priority.
76*4882a593Smuzhiyun  */
get_current_ioprio(void)77*4882a593Smuzhiyun static inline int get_current_ioprio(void)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	struct io_context *ioc = current->io_context;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	if (ioc)
82*4882a593Smuzhiyun 		return ioc->ioprio;
83*4882a593Smuzhiyun 	return IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun  * For inheritance, return the highest of the two given priorities
88*4882a593Smuzhiyun  */
89*4882a593Smuzhiyun extern int ioprio_best(unsigned short aprio, unsigned short bprio);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun extern int set_task_ioprio(struct task_struct *task, int ioprio);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun #ifdef CONFIG_BLOCK
94*4882a593Smuzhiyun extern int ioprio_check_cap(int ioprio);
95*4882a593Smuzhiyun #else
ioprio_check_cap(int ioprio)96*4882a593Smuzhiyun static inline int ioprio_check_cap(int ioprio)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	return -ENOTBLK;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun #endif /* CONFIG_BLOCK */
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun #endif
103