1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Copyright (c) 2019 Linus Walleij <linus.walleij@linaro.org>
4 */
5
6 #ifndef LTP_IOPRIO_H
7 #define LTP_IOPRIO_H
8
9 enum {
10 IOPRIO_CLASS_NONE = 0,
11 IOPRIO_CLASS_RT,
12 IOPRIO_CLASS_BE,
13 IOPRIO_CLASS_IDLE,
14 };
15
16 enum {
17 IOPRIO_WHO_PROCESS = 1,
18 IOPRIO_WHO_PGRP,
19 IOPRIO_WHO_USER,
20 };
21
22 /* The I/O scheduler classes have 8 priorities 0..7 except for the IDLE class */
23 #define IOPRIO_PRIO_NUM 8
24
25 #define IOPRIO_CLASS_SHIFT (13)
26 #define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1)
27
28 #define IOPRIO_PRIO_CLASS(data) ((data) >> IOPRIO_CLASS_SHIFT)
29 #define IOPRIO_PRIO_LEVEL(data) ((data) & IOPRIO_PRIO_MASK)
30 #define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data)
31
32 static const char * const to_class_str[] = {
33 [IOPRIO_CLASS_NONE] = "NONE",
34 [IOPRIO_CLASS_RT] = "REALTIME",
35 [IOPRIO_CLASS_BE] = "BEST-EFFORT",
36 [IOPRIO_CLASS_IDLE] = "IDLE"
37 };
38
sys_ioprio_get(int which,int who)39 static inline int sys_ioprio_get(int which, int who)
40 {
41 return tst_syscall(__NR_ioprio_get, which, who);
42 }
43
sys_ioprio_set(int which,int who,int ioprio)44 static inline int sys_ioprio_set(int which, int who, int ioprio)
45 {
46 return tst_syscall(__NR_ioprio_set, which, who, ioprio);
47 }
48
49 /* Priority range from 0 (highest) to 7 (lowest) */
prio_in_range(int prio)50 static inline int prio_in_range(int prio)
51 {
52 if ((prio < 0) || (prio > 7))
53 return 0;
54 return 1;
55 }
56
57 /* Priority range from 0 to 3 using the enum */
class_in_range(int class)58 static inline int class_in_range(int class)
59 {
60 if ((class < IOPRIO_CLASS_NONE) || (class > IOPRIO_CLASS_IDLE))
61 return 0;
62 return 1;
63 }
64
ioprio_check_setting(int class,int prio,int report)65 static inline void ioprio_check_setting(int class, int prio, int report)
66 {
67 int res;
68 int newclass, newprio;
69
70 res = sys_ioprio_get(IOPRIO_WHO_PROCESS, 0);
71 if (res == -1) {
72 tst_res(TFAIL | TTERRNO,
73 "reading back prio failed");
74 return;
75 }
76
77 newclass = IOPRIO_PRIO_CLASS(res);
78 newprio = IOPRIO_PRIO_LEVEL(res);
79 if (newclass != class)
80 tst_res(TFAIL,
81 "wrong class after setting, expected %s got %s",
82 to_class_str[class],
83 to_class_str[newclass]);
84 else if (newprio != prio)
85 tst_res(TFAIL,
86 "wrong prio after setting, expected %d got %d",
87 prio, newprio);
88 else if (report)
89 tst_res(TPASS, "ioprio_set new class %s, new prio %d",
90 to_class_str[newclass],
91 newprio);
92 }
93
94 #endif
95