1 // SPDX-License-Identifier: GPL-2.0
3 * Block rq-qos policy for assigning an I/O priority class to requests.
5 * Using an rq-qos policy for assigning I/O priority class has two advantages
6 * over using the ioprio_set() system call:
8 * - This policy is cgroup based so it has all the advantages of cgroups.
9 * - While ioprio_set() does not affect page cache writeback I/O, this rq-qos
10 * controller affects page cache writeback I/O for filesystems that support
11 * assiociating a cgroup with writeback I/O. See also
12 * Documentation/admin-guide/cgroup-v2.rst.
15 #include <linux/blk-cgroup.h>
16 #include <linux/blk-mq.h>
17 #include <linux/blk_types.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include "blk-ioprio.h"
21 #include "blk-rq-qos.h"
24 * enum prio_policy - I/O priority class policy.
25 * @POLICY_NO_CHANGE: (default) do not modify the I/O priority class.
26 * @POLICY_NONE_TO_RT: modify IOPRIO_CLASS_NONE into IOPRIO_CLASS_RT.
27 * @POLICY_RESTRICT_TO_BE: modify IOPRIO_CLASS_NONE and IOPRIO_CLASS_RT into
29 * @POLICY_ALL_TO_IDLE: change the I/O priority class into IOPRIO_CLASS_IDLE.
31 * See also <linux/ioprio.h>.
35 POLICY_NONE_TO_RT = 1,
36 POLICY_RESTRICT_TO_BE = 2,
37 POLICY_ALL_TO_IDLE = 3,
40 static const char *policy_name[] = {
41 [POLICY_NO_CHANGE] = "no-change",
42 [POLICY_NONE_TO_RT] = "none-to-rt",
43 [POLICY_RESTRICT_TO_BE] = "restrict-to-be",
44 [POLICY_ALL_TO_IDLE] = "idle",
47 static struct blkcg_policy ioprio_policy;
50 * struct ioprio_blkg - Per (cgroup, request queue) data.
51 * @pd: blkg_policy_data structure.
54 struct blkg_policy_data pd;
58 * struct ioprio_blkcg - Per cgroup data.
59 * @cpd: blkcg_policy_data structure.
60 * @prio_policy: One of the IOPRIO_CLASS_* values. See also <linux/ioprio.h>.
63 struct blkcg_policy_data cpd;
64 enum prio_policy prio_policy;
68 static inline struct ioprio_blkg *pd_to_ioprio(struct blkg_policy_data *pd)
70 return pd ? container_of(pd, struct ioprio_blkg, pd) : NULL;
73 static struct ioprio_blkcg *blkcg_to_ioprio_blkcg(struct blkcg *blkcg)
75 return container_of(blkcg_to_cpd(blkcg, &ioprio_policy),
76 struct ioprio_blkcg, cpd);
79 static struct ioprio_blkcg *
80 ioprio_blkcg_from_css(struct cgroup_subsys_state *css)
82 return blkcg_to_ioprio_blkcg(css_to_blkcg(css));
85 static struct ioprio_blkcg *ioprio_blkcg_from_bio(struct bio *bio)
87 struct blkg_policy_data *pd = blkg_to_pd(bio->bi_blkg, &ioprio_policy);
92 return blkcg_to_ioprio_blkcg(pd->blkg->blkcg);
95 static int ioprio_show_prio_policy(struct seq_file *sf, void *v)
97 struct ioprio_blkcg *blkcg = ioprio_blkcg_from_css(seq_css(sf));
99 seq_printf(sf, "%s\n", policy_name[blkcg->prio_policy]);
103 static ssize_t ioprio_set_prio_policy(struct kernfs_open_file *of, char *buf,
104 size_t nbytes, loff_t off)
106 struct ioprio_blkcg *blkcg = ioprio_blkcg_from_css(of_css(of));
111 /* kernfs_fop_write_iter() terminates 'buf' with '\0'. */
112 ret = sysfs_match_string(policy_name, buf);
115 blkcg->prio_policy = ret;
116 blkcg->prio_set = true;
120 static struct blkg_policy_data *
121 ioprio_alloc_pd(gfp_t gfp, struct request_queue *q, struct blkcg *blkcg)
123 struct ioprio_blkg *ioprio_blkg;
125 ioprio_blkg = kzalloc(sizeof(*ioprio_blkg), gfp);
129 return &ioprio_blkg->pd;
132 static void ioprio_free_pd(struct blkg_policy_data *pd)
134 struct ioprio_blkg *ioprio_blkg = pd_to_ioprio(pd);
139 static struct blkcg_policy_data *ioprio_alloc_cpd(gfp_t gfp)
141 struct ioprio_blkcg *blkcg;
143 blkcg = kzalloc(sizeof(*blkcg), gfp);
146 blkcg->prio_policy = POLICY_NO_CHANGE;
150 static void ioprio_free_cpd(struct blkcg_policy_data *cpd)
152 struct ioprio_blkcg *blkcg = container_of(cpd, typeof(*blkcg), cpd);
157 #define IOPRIO_ATTRS \
159 .name = "prio.class", \
160 .seq_show = ioprio_show_prio_policy, \
161 .write = ioprio_set_prio_policy, \
165 /* cgroup v2 attributes */
166 static struct cftype ioprio_files[] = {
170 /* cgroup v1 attributes */
171 static struct cftype ioprio_legacy_files[] = {
175 static struct blkcg_policy ioprio_policy = {
176 .dfl_cftypes = ioprio_files,
177 .legacy_cftypes = ioprio_legacy_files,
179 .cpd_alloc_fn = ioprio_alloc_cpd,
180 .cpd_free_fn = ioprio_free_cpd,
182 .pd_alloc_fn = ioprio_alloc_pd,
183 .pd_free_fn = ioprio_free_pd,
190 static void blkcg_ioprio_track(struct rq_qos *rqos, struct request *rq,
193 struct ioprio_blkcg *blkcg = ioprio_blkcg_from_bio(bio);
196 if (!blkcg->prio_set)
200 * Except for IOPRIO_CLASS_NONE, higher I/O priority numbers
201 * correspond to a lower priority. Hence, the max_t() below selects
202 * the lower priority of bi_ioprio and the cgroup I/O priority class.
203 * If the cgroup policy has been set to POLICY_NO_CHANGE == 0, the
204 * bio I/O priority is not modified. If the bio I/O priority equals
205 * IOPRIO_CLASS_NONE, the cgroup I/O priority is assigned to the bio.
207 prio = max_t(u16, bio->bi_ioprio,
208 IOPRIO_PRIO_VALUE(blkcg->prio_policy, 0));
209 if (prio > bio->bi_ioprio)
210 bio->bi_ioprio = prio;
213 static void blkcg_ioprio_exit(struct rq_qos *rqos)
215 struct blk_ioprio *blkioprio_blkg =
216 container_of(rqos, typeof(*blkioprio_blkg), rqos);
218 blkcg_deactivate_policy(rqos->q, &ioprio_policy);
219 kfree(blkioprio_blkg);
222 static struct rq_qos_ops blkcg_ioprio_ops = {
223 .track = blkcg_ioprio_track,
224 .exit = blkcg_ioprio_exit,
227 int blk_ioprio_init(struct request_queue *q)
229 struct blk_ioprio *blkioprio_blkg;
233 blkioprio_blkg = kzalloc(sizeof(*blkioprio_blkg), GFP_KERNEL);
237 ret = blkcg_activate_policy(q, &ioprio_policy);
239 kfree(blkioprio_blkg);
243 rqos = &blkioprio_blkg->rqos;
244 rqos->id = RQ_QOS_IOPRIO;
245 rqos->ops = &blkcg_ioprio_ops;
249 * Registering the rq-qos policy after activating the blk-cgroup
250 * policy guarantees that ioprio_blkcg_from_bio(bio) != NULL in the
258 static int __init ioprio_init(void)
260 return blkcg_policy_register(&ioprio_policy);
263 static void __exit ioprio_exit(void)
265 blkcg_policy_unregister(&ioprio_policy);
268 module_init(ioprio_init);
269 module_exit(ioprio_exit);