xref: /linux/block/blk-ioprio.c (revision 3e9e952bb3139ad1e08f3e1960239c2988ab90c9)
1556910e3SBart Van Assche // SPDX-License-Identifier: GPL-2.0
2556910e3SBart Van Assche /*
3556910e3SBart Van Assche  * Block rq-qos policy for assigning an I/O priority class to requests.
4556910e3SBart Van Assche  *
5556910e3SBart Van Assche  * Using an rq-qos policy for assigning I/O priority class has two advantages
6556910e3SBart Van Assche  * over using the ioprio_set() system call:
7556910e3SBart Van Assche  *
8556910e3SBart Van Assche  * - This policy is cgroup based so it has all the advantages of cgroups.
9556910e3SBart Van Assche  * - While ioprio_set() does not affect page cache writeback I/O, this rq-qos
10556910e3SBart Van Assche  *   controller affects page cache writeback I/O for filesystems that support
11556910e3SBart Van Assche  *   assiociating a cgroup with writeback I/O. See also
12556910e3SBart Van Assche  *   Documentation/admin-guide/cgroup-v2.rst.
13556910e3SBart Van Assche  */
14556910e3SBart Van Assche 
15556910e3SBart Van Assche #include <linux/blk-mq.h>
16556910e3SBart Van Assche #include <linux/blk_types.h>
17556910e3SBart Van Assche #include <linux/kernel.h>
18556910e3SBart Van Assche #include <linux/module.h>
19672fdcf0SMing Lei #include "blk-cgroup.h"
20556910e3SBart Van Assche #include "blk-ioprio.h"
21556910e3SBart Van Assche #include "blk-rq-qos.h"
22556910e3SBart Van Assche 
23556910e3SBart Van Assche /**
24556910e3SBart Van Assche  * enum prio_policy - I/O priority class policy.
25556910e3SBart Van Assche  * @POLICY_NO_CHANGE: (default) do not modify the I/O priority class.
26ddf63516SHou Tao  * @POLICY_PROMOTE_TO_RT: modify no-IOPRIO_CLASS_RT to IOPRIO_CLASS_RT.
27556910e3SBart Van Assche  * @POLICY_RESTRICT_TO_BE: modify IOPRIO_CLASS_NONE and IOPRIO_CLASS_RT into
28556910e3SBart Van Assche  *		IOPRIO_CLASS_BE.
29556910e3SBart Van Assche  * @POLICY_ALL_TO_IDLE: change the I/O priority class into IOPRIO_CLASS_IDLE.
30ddf63516SHou Tao  * @POLICY_NONE_TO_RT: an alias for POLICY_PROMOTE_TO_RT.
31556910e3SBart Van Assche  *
32556910e3SBart Van Assche  * See also <linux/ioprio.h>.
33556910e3SBart Van Assche  */
34556910e3SBart Van Assche enum prio_policy {
35556910e3SBart Van Assche 	POLICY_NO_CHANGE	= 0,
36ddf63516SHou Tao 	POLICY_PROMOTE_TO_RT	= 1,
37556910e3SBart Van Assche 	POLICY_RESTRICT_TO_BE	= 2,
38556910e3SBart Van Assche 	POLICY_ALL_TO_IDLE	= 3,
39ddf63516SHou Tao 	POLICY_NONE_TO_RT	= 4,
40556910e3SBart Van Assche };
41556910e3SBart Van Assche 
42556910e3SBart Van Assche static const char *policy_name[] = {
43556910e3SBart Van Assche 	[POLICY_NO_CHANGE]	= "no-change",
44ddf63516SHou Tao 	[POLICY_PROMOTE_TO_RT]	= "promote-to-rt",
45556910e3SBart Van Assche 	[POLICY_RESTRICT_TO_BE]	= "restrict-to-be",
46556910e3SBart Van Assche 	[POLICY_ALL_TO_IDLE]	= "idle",
47ddf63516SHou Tao 	[POLICY_NONE_TO_RT]	= "none-to-rt",
48556910e3SBart Van Assche };
49556910e3SBart Van Assche 
50556910e3SBart Van Assche static struct blkcg_policy ioprio_policy;
51556910e3SBart Van Assche 
52556910e3SBart Van Assche /**
53556910e3SBart Van Assche  * struct ioprio_blkcg - Per cgroup data.
54556910e3SBart Van Assche  * @cpd: blkcg_policy_data structure.
55556910e3SBart Van Assche  * @prio_policy: One of the IOPRIO_CLASS_* values. See also <linux/ioprio.h>.
56556910e3SBart Van Assche  */
57556910e3SBart Van Assche struct ioprio_blkcg {
58556910e3SBart Van Assche 	struct blkcg_policy_data cpd;
59556910e3SBart Van Assche 	enum prio_policy	 prio_policy;
60556910e3SBart Van Assche };
61556910e3SBart Van Assche 
blkcg_to_ioprio_blkcg(struct blkcg * blkcg)62556910e3SBart Van Assche static struct ioprio_blkcg *blkcg_to_ioprio_blkcg(struct blkcg *blkcg)
63556910e3SBart Van Assche {
64556910e3SBart Van Assche 	return container_of(blkcg_to_cpd(blkcg, &ioprio_policy),
65556910e3SBart Van Assche 			    struct ioprio_blkcg, cpd);
66556910e3SBart Van Assche }
67556910e3SBart Van Assche 
68556910e3SBart Van Assche static struct ioprio_blkcg *
ioprio_blkcg_from_css(struct cgroup_subsys_state * css)69556910e3SBart Van Assche ioprio_blkcg_from_css(struct cgroup_subsys_state *css)
70556910e3SBart Van Assche {
71556910e3SBart Van Assche 	return blkcg_to_ioprio_blkcg(css_to_blkcg(css));
72556910e3SBart Van Assche }
73556910e3SBart Van Assche 
ioprio_show_prio_policy(struct seq_file * sf,void * v)74556910e3SBart Van Assche static int ioprio_show_prio_policy(struct seq_file *sf, void *v)
75556910e3SBart Van Assche {
76556910e3SBart Van Assche 	struct ioprio_blkcg *blkcg = ioprio_blkcg_from_css(seq_css(sf));
77556910e3SBart Van Assche 
78556910e3SBart Van Assche 	seq_printf(sf, "%s\n", policy_name[blkcg->prio_policy]);
79556910e3SBart Van Assche 	return 0;
80556910e3SBart Van Assche }
81556910e3SBart Van Assche 
ioprio_set_prio_policy(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)82556910e3SBart Van Assche static ssize_t ioprio_set_prio_policy(struct kernfs_open_file *of, char *buf,
83556910e3SBart Van Assche 				      size_t nbytes, loff_t off)
84556910e3SBart Van Assche {
85556910e3SBart Van Assche 	struct ioprio_blkcg *blkcg = ioprio_blkcg_from_css(of_css(of));
86556910e3SBart Van Assche 	int ret;
87556910e3SBart Van Assche 
88556910e3SBart Van Assche 	if (off != 0)
89556910e3SBart Van Assche 		return -EIO;
90556910e3SBart Van Assche 	/* kernfs_fop_write_iter() terminates 'buf' with '\0'. */
91556910e3SBart Van Assche 	ret = sysfs_match_string(policy_name, buf);
92556910e3SBart Van Assche 	if (ret < 0)
93556910e3SBart Van Assche 		return ret;
94556910e3SBart Van Assche 	blkcg->prio_policy = ret;
95556910e3SBart Van Assche 	return nbytes;
96556910e3SBart Van Assche }
97556910e3SBart Van Assche 
ioprio_alloc_cpd(gfp_t gfp)98556910e3SBart Van Assche static struct blkcg_policy_data *ioprio_alloc_cpd(gfp_t gfp)
99556910e3SBart Van Assche {
100556910e3SBart Van Assche 	struct ioprio_blkcg *blkcg;
101556910e3SBart Van Assche 
102*69050f8dSKees Cook 	blkcg = kzalloc_obj(*blkcg, gfp);
103556910e3SBart Van Assche 	if (!blkcg)
104556910e3SBart Van Assche 		return NULL;
105556910e3SBart Van Assche 	blkcg->prio_policy = POLICY_NO_CHANGE;
106556910e3SBart Van Assche 	return &blkcg->cpd;
107556910e3SBart Van Assche }
108556910e3SBart Van Assche 
ioprio_free_cpd(struct blkcg_policy_data * cpd)109556910e3SBart Van Assche static void ioprio_free_cpd(struct blkcg_policy_data *cpd)
110556910e3SBart Van Assche {
111556910e3SBart Van Assche 	struct ioprio_blkcg *blkcg = container_of(cpd, typeof(*blkcg), cpd);
112556910e3SBart Van Assche 
113556910e3SBart Van Assche 	kfree(blkcg);
114556910e3SBart Van Assche }
115556910e3SBart Van Assche 
116556910e3SBart Van Assche static struct cftype ioprio_files[] = {
1174a893bdcSMichal Koutný 	{
1184a893bdcSMichal Koutný 		.name		= "prio.class",
1194a893bdcSMichal Koutný 		.seq_show	= ioprio_show_prio_policy,
1204a893bdcSMichal Koutný 		.write		= ioprio_set_prio_policy,
1214a893bdcSMichal Koutný 	},
1224a893bdcSMichal Koutný 	{ } /* sentinel */
123556910e3SBart Van Assche };
124556910e3SBart Van Assche 
125556910e3SBart Van Assche static struct blkcg_policy ioprio_policy = {
126556910e3SBart Van Assche 	.dfl_cftypes	= ioprio_files,
1274a893bdcSMichal Koutný 	.legacy_cftypes = ioprio_files,
128556910e3SBart Van Assche 
129556910e3SBart Van Assche 	.cpd_alloc_fn	= ioprio_alloc_cpd,
130556910e3SBart Van Assche 	.cpd_free_fn	= ioprio_free_cpd,
131556910e3SBart Van Assche };
132556910e3SBart Van Assche 
blkcg_set_ioprio(struct bio * bio)13382b74cacSJan Kara void blkcg_set_ioprio(struct bio *bio)
134556910e3SBart Van Assche {
135d0e92795SYu Kuai 	struct ioprio_blkcg *blkcg = blkcg_to_ioprio_blkcg(bio->bi_blkg->blkcg);
13625c4b5e0SJens Axboe 	u16 prio;
13725c4b5e0SJens Axboe 
13882b74cacSJan Kara 	if (!blkcg || blkcg->prio_policy == POLICY_NO_CHANGE)
13925c4b5e0SJens Axboe 		return;
140556910e3SBart Van Assche 
141ddf63516SHou Tao 	if (blkcg->prio_policy == POLICY_PROMOTE_TO_RT ||
142ddf63516SHou Tao 	    blkcg->prio_policy == POLICY_NONE_TO_RT) {
143ddf63516SHou Tao 		/*
144ddf63516SHou Tao 		 * For RT threads, the default priority level is 4 because
145ddf63516SHou Tao 		 * task_nice is 0. By promoting non-RT io-priority to RT-class
146ddf63516SHou Tao 		 * and default level 4, those requests that are already
147ddf63516SHou Tao 		 * RT-class but need a higher io-priority can use ioprio_set()
148ddf63516SHou Tao 		 * to achieve this.
149ddf63516SHou Tao 		 */
150ddf63516SHou Tao 		if (IOPRIO_PRIO_CLASS(bio->bi_ioprio) != IOPRIO_CLASS_RT)
151ddf63516SHou Tao 			bio->bi_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, 4);
152ddf63516SHou Tao 		return;
153ddf63516SHou Tao 	}
154ddf63516SHou Tao 
155556910e3SBart Van Assche 	/*
156556910e3SBart Van Assche 	 * Except for IOPRIO_CLASS_NONE, higher I/O priority numbers
157556910e3SBart Van Assche 	 * correspond to a lower priority. Hence, the max_t() below selects
158556910e3SBart Van Assche 	 * the lower priority of bi_ioprio and the cgroup I/O priority class.
159f2586544SJan Kara 	 * If the bio I/O priority equals IOPRIO_CLASS_NONE, the cgroup I/O
160f2586544SJan Kara 	 * priority is assigned to the bio.
161556910e3SBart Van Assche 	 */
16225c4b5e0SJens Axboe 	prio = max_t(u16, bio->bi_ioprio,
163556910e3SBart Van Assche 			IOPRIO_PRIO_VALUE(blkcg->prio_policy, 0));
16425c4b5e0SJens Axboe 	if (prio > bio->bi_ioprio)
16525c4b5e0SJens Axboe 		bio->bi_ioprio = prio;
166556910e3SBart Van Assche }
167556910e3SBart Van Assche 
ioprio_init(void)168556910e3SBart Van Assche static int __init ioprio_init(void)
169556910e3SBart Van Assche {
170556910e3SBart Van Assche 	return blkcg_policy_register(&ioprio_policy);
171556910e3SBart Van Assche }
172556910e3SBart Van Assche 
ioprio_exit(void)173556910e3SBart Van Assche static void __exit ioprio_exit(void)
174556910e3SBart Van Assche {
175556910e3SBart Van Assche 	blkcg_policy_unregister(&ioprio_policy);
176556910e3SBart Van Assche }
177556910e3SBart Van Assche 
178556910e3SBart Van Assche module_init(ioprio_init);
179556910e3SBart Van Assche module_exit(ioprio_exit);
180