0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/blk-mq.h>
0016 #include <linux/blk_types.h>
0017 #include <linux/kernel.h>
0018 #include <linux/module.h>
0019 #include "blk-cgroup.h"
0020 #include "blk-ioprio.h"
0021 #include "blk-rq-qos.h"
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 enum prio_policy {
0034 POLICY_NO_CHANGE = 0,
0035 POLICY_NONE_TO_RT = 1,
0036 POLICY_RESTRICT_TO_BE = 2,
0037 POLICY_ALL_TO_IDLE = 3,
0038 };
0039
0040 static const char *policy_name[] = {
0041 [POLICY_NO_CHANGE] = "no-change",
0042 [POLICY_NONE_TO_RT] = "none-to-rt",
0043 [POLICY_RESTRICT_TO_BE] = "restrict-to-be",
0044 [POLICY_ALL_TO_IDLE] = "idle",
0045 };
0046
0047 static struct blkcg_policy ioprio_policy;
0048
0049
0050
0051
0052
0053 struct ioprio_blkg {
0054 struct blkg_policy_data pd;
0055 };
0056
0057
0058
0059
0060
0061
0062 struct ioprio_blkcg {
0063 struct blkcg_policy_data cpd;
0064 enum prio_policy prio_policy;
0065 };
0066
0067 static inline struct ioprio_blkg *pd_to_ioprio(struct blkg_policy_data *pd)
0068 {
0069 return pd ? container_of(pd, struct ioprio_blkg, pd) : NULL;
0070 }
0071
0072 static struct ioprio_blkcg *blkcg_to_ioprio_blkcg(struct blkcg *blkcg)
0073 {
0074 return container_of(blkcg_to_cpd(blkcg, &ioprio_policy),
0075 struct ioprio_blkcg, cpd);
0076 }
0077
0078 static struct ioprio_blkcg *
0079 ioprio_blkcg_from_css(struct cgroup_subsys_state *css)
0080 {
0081 return blkcg_to_ioprio_blkcg(css_to_blkcg(css));
0082 }
0083
0084 static struct ioprio_blkcg *ioprio_blkcg_from_bio(struct bio *bio)
0085 {
0086 struct blkg_policy_data *pd = blkg_to_pd(bio->bi_blkg, &ioprio_policy);
0087
0088 if (!pd)
0089 return NULL;
0090
0091 return blkcg_to_ioprio_blkcg(pd->blkg->blkcg);
0092 }
0093
0094 static int ioprio_show_prio_policy(struct seq_file *sf, void *v)
0095 {
0096 struct ioprio_blkcg *blkcg = ioprio_blkcg_from_css(seq_css(sf));
0097
0098 seq_printf(sf, "%s\n", policy_name[blkcg->prio_policy]);
0099 return 0;
0100 }
0101
0102 static ssize_t ioprio_set_prio_policy(struct kernfs_open_file *of, char *buf,
0103 size_t nbytes, loff_t off)
0104 {
0105 struct ioprio_blkcg *blkcg = ioprio_blkcg_from_css(of_css(of));
0106 int ret;
0107
0108 if (off != 0)
0109 return -EIO;
0110
0111 ret = sysfs_match_string(policy_name, buf);
0112 if (ret < 0)
0113 return ret;
0114 blkcg->prio_policy = ret;
0115 return nbytes;
0116 }
0117
0118 static struct blkg_policy_data *
0119 ioprio_alloc_pd(gfp_t gfp, struct request_queue *q, struct blkcg *blkcg)
0120 {
0121 struct ioprio_blkg *ioprio_blkg;
0122
0123 ioprio_blkg = kzalloc(sizeof(*ioprio_blkg), gfp);
0124 if (!ioprio_blkg)
0125 return NULL;
0126
0127 return &ioprio_blkg->pd;
0128 }
0129
0130 static void ioprio_free_pd(struct blkg_policy_data *pd)
0131 {
0132 struct ioprio_blkg *ioprio_blkg = pd_to_ioprio(pd);
0133
0134 kfree(ioprio_blkg);
0135 }
0136
0137 static struct blkcg_policy_data *ioprio_alloc_cpd(gfp_t gfp)
0138 {
0139 struct ioprio_blkcg *blkcg;
0140
0141 blkcg = kzalloc(sizeof(*blkcg), gfp);
0142 if (!blkcg)
0143 return NULL;
0144 blkcg->prio_policy = POLICY_NO_CHANGE;
0145 return &blkcg->cpd;
0146 }
0147
0148 static void ioprio_free_cpd(struct blkcg_policy_data *cpd)
0149 {
0150 struct ioprio_blkcg *blkcg = container_of(cpd, typeof(*blkcg), cpd);
0151
0152 kfree(blkcg);
0153 }
0154
0155 #define IOPRIO_ATTRS \
0156 { \
0157 .name = "prio.class", \
0158 .seq_show = ioprio_show_prio_policy, \
0159 .write = ioprio_set_prio_policy, \
0160 }, \
0161 { }
0162
0163
0164 static struct cftype ioprio_files[] = {
0165 IOPRIO_ATTRS
0166 };
0167
0168
0169 static struct cftype ioprio_legacy_files[] = {
0170 IOPRIO_ATTRS
0171 };
0172
0173 static struct blkcg_policy ioprio_policy = {
0174 .dfl_cftypes = ioprio_files,
0175 .legacy_cftypes = ioprio_legacy_files,
0176
0177 .cpd_alloc_fn = ioprio_alloc_cpd,
0178 .cpd_free_fn = ioprio_free_cpd,
0179
0180 .pd_alloc_fn = ioprio_alloc_pd,
0181 .pd_free_fn = ioprio_free_pd,
0182 };
0183
0184 void blkcg_set_ioprio(struct bio *bio)
0185 {
0186 struct ioprio_blkcg *blkcg = ioprio_blkcg_from_bio(bio);
0187 u16 prio;
0188
0189 if (!blkcg || blkcg->prio_policy == POLICY_NO_CHANGE)
0190 return;
0191
0192
0193
0194
0195
0196
0197
0198
0199 prio = max_t(u16, bio->bi_ioprio,
0200 IOPRIO_PRIO_VALUE(blkcg->prio_policy, 0));
0201 if (prio > bio->bi_ioprio)
0202 bio->bi_ioprio = prio;
0203 }
0204
0205 void blk_ioprio_exit(struct request_queue *q)
0206 {
0207 blkcg_deactivate_policy(q, &ioprio_policy);
0208 }
0209
0210 int blk_ioprio_init(struct request_queue *q)
0211 {
0212 return blkcg_activate_policy(q, &ioprio_policy);
0213 }
0214
0215 static int __init ioprio_init(void)
0216 {
0217 return blkcg_policy_register(&ioprio_policy);
0218 }
0219
0220 static void __exit ioprio_exit(void)
0221 {
0222 blkcg_policy_unregister(&ioprio_policy);
0223 }
0224
0225 module_init(ioprio_init);
0226 module_exit(ioprio_exit);