Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
22e2c507 JA |
2 | #ifndef IOPRIO_H |
3 | #define IOPRIO_H | |
4 | ||
5 | #include <linux/sched.h> | |
36436440 | 6 | #include <linux/sched/rt.h> |
fd0928df | 7 | #include <linux/iocontext.h> |
22e2c507 | 8 | |
06447ae5 | 9 | #include <uapi/linux/ioprio.h> |
c2dea2d1 | 10 | |
e70344c0 DLM |
11 | /* |
12 | * Default IO priority. | |
13 | */ | |
e589f464 | 14 | #define IOPRIO_DEFAULT IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0) |
e70344c0 | 15 | |
a553a835 DLM |
16 | /* |
17 | * Check that a priority value has a valid class. | |
18 | */ | |
19 | static inline bool ioprio_valid(unsigned short ioprio) | |
20 | { | |
21 | unsigned short class = IOPRIO_PRIO_CLASS(ioprio); | |
22 | ||
23 | return class > IOPRIO_CLASS_NONE && class <= IOPRIO_CLASS_IDLE; | |
24 | } | |
25 | ||
598971bf TH |
26 | /* |
27 | * if process has set io priority explicitly, use that. if not, convert | |
28 | * the cpu scheduler nice value to an io priority | |
29 | */ | |
22e2c507 JA |
30 | static inline int task_nice_ioprio(struct task_struct *task) |
31 | { | |
32 | return (task_nice(task) + 20) / 5; | |
33 | } | |
34 | ||
6d63c275 JA |
35 | /* |
36 | * This is for the case where the task hasn't asked for a specific IO class. | |
37 | * Check for idle and rt task process, and return appropriate IO class. | |
38 | */ | |
39 | static inline int task_nice_ioclass(struct task_struct *task) | |
40 | { | |
41 | if (task->policy == SCHED_IDLE) | |
42 | return IOPRIO_CLASS_IDLE; | |
36436440 | 43 | else if (task_is_realtime(task)) |
6d63c275 JA |
44 | return IOPRIO_CLASS_RT; |
45 | else | |
46 | return IOPRIO_CLASS_BE; | |
47 | } | |
48 | ||
893e5d32 | 49 | #ifdef CONFIG_BLOCK |
3b7cb745 JA |
50 | /* |
51 | * If the task has set an I/O priority, use that. Otherwise, return | |
52 | * the default I/O priority. | |
53 | * | |
54 | * Expected to be called for current task or with task_lock() held to keep | |
55 | * io_context stable. | |
56 | */ | |
57 | static inline int __get_task_ioprio(struct task_struct *p) | |
58 | { | |
59 | struct io_context *ioc = p->io_context; | |
60 | int prio; | |
61 | ||
53889bca JA |
62 | if (!ioc) |
63 | return IOPRIO_DEFAULT; | |
64 | ||
3b7cb745 JA |
65 | if (p != current) |
66 | lockdep_assert_held(&p->alloc_lock); | |
3b7cb745 | 67 | |
53889bca | 68 | prio = ioc->ioprio; |
3b7cb745 JA |
69 | if (IOPRIO_PRIO_CLASS(prio) == IOPRIO_CLASS_NONE) |
70 | prio = IOPRIO_PRIO_VALUE(task_nice_ioclass(p), | |
71 | task_nice_ioprio(p)); | |
72 | return prio; | |
73 | } | |
893e5d32 JK |
74 | #else |
75 | static inline int __get_task_ioprio(struct task_struct *p) | |
64845a1d | 76 | { |
893e5d32 JK |
77 | return IOPRIO_DEFAULT; |
78 | } | |
79 | #endif /* CONFIG_BLOCK */ | |
f7eda402 | 80 | |
893e5d32 JK |
81 | static inline int get_current_ioprio(void) |
82 | { | |
83 | return __get_task_ioprio(current); | |
64845a1d DLM |
84 | } |
85 | ||
b3881f74 TT |
86 | extern int set_task_ioprio(struct task_struct *task, int ioprio); |
87 | ||
b0966e7b | 88 | #ifdef CONFIG_BLOCK |
aa434577 | 89 | extern int ioprio_check_cap(int ioprio); |
b0966e7b AM |
90 | #else |
91 | static inline int ioprio_check_cap(int ioprio) | |
92 | { | |
93 | return -ENOTBLK; | |
94 | } | |
95 | #endif /* CONFIG_BLOCK */ | |
aa434577 | 96 | |
22e2c507 | 97 | #endif |