blk-ioprio.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Block rq-qos policy for assigning an I/O priority class to requests.
  4. *
  5. * Using an rq-qos policy for assigning I/O priority class has two advantages
  6. * over using the ioprio_set() system call:
  7. *
  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.
  13. */
  14. #include <linux/blk-mq.h>
  15. #include <linux/blk_types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include "blk-cgroup.h"
  19. #include "blk-ioprio.h"
  20. #include "blk-rq-qos.h"
  21. /**
  22. * enum prio_policy - I/O priority class policy.
  23. * @POLICY_NO_CHANGE: (default) do not modify the I/O priority class.
  24. * @POLICY_PROMOTE_TO_RT: modify no-IOPRIO_CLASS_RT to IOPRIO_CLASS_RT.
  25. * @POLICY_RESTRICT_TO_BE: modify IOPRIO_CLASS_NONE and IOPRIO_CLASS_RT into
  26. * IOPRIO_CLASS_BE.
  27. * @POLICY_ALL_TO_IDLE: change the I/O priority class into IOPRIO_CLASS_IDLE.
  28. * @POLICY_NONE_TO_RT: an alias for POLICY_PROMOTE_TO_RT.
  29. *
  30. * See also <linux/ioprio.h>.
  31. */
  32. enum prio_policy {
  33. POLICY_NO_CHANGE = 0,
  34. POLICY_PROMOTE_TO_RT = 1,
  35. POLICY_RESTRICT_TO_BE = 2,
  36. POLICY_ALL_TO_IDLE = 3,
  37. POLICY_NONE_TO_RT = 4,
  38. };
  39. static const char *policy_name[] = {
  40. [POLICY_NO_CHANGE] = "no-change",
  41. [POLICY_PROMOTE_TO_RT] = "promote-to-rt",
  42. [POLICY_RESTRICT_TO_BE] = "restrict-to-be",
  43. [POLICY_ALL_TO_IDLE] = "idle",
  44. [POLICY_NONE_TO_RT] = "none-to-rt",
  45. };
  46. static struct blkcg_policy ioprio_policy;
  47. /**
  48. * struct ioprio_blkg - Per (cgroup, request queue) data.
  49. * @pd: blkg_policy_data structure.
  50. */
  51. struct ioprio_blkg {
  52. struct blkg_policy_data pd;
  53. };
  54. /**
  55. * struct ioprio_blkcg - Per cgroup data.
  56. * @cpd: blkcg_policy_data structure.
  57. * @prio_policy: One of the IOPRIO_CLASS_* values. See also <linux/ioprio.h>.
  58. */
  59. struct ioprio_blkcg {
  60. struct blkcg_policy_data cpd;
  61. enum prio_policy prio_policy;
  62. };
  63. static inline struct ioprio_blkg *pd_to_ioprio(struct blkg_policy_data *pd)
  64. {
  65. return pd ? container_of(pd, struct ioprio_blkg, pd) : NULL;
  66. }
  67. static struct ioprio_blkcg *blkcg_to_ioprio_blkcg(struct blkcg *blkcg)
  68. {
  69. return container_of(blkcg_to_cpd(blkcg, &ioprio_policy),
  70. struct ioprio_blkcg, cpd);
  71. }
  72. static struct ioprio_blkcg *
  73. ioprio_blkcg_from_css(struct cgroup_subsys_state *css)
  74. {
  75. return blkcg_to_ioprio_blkcg(css_to_blkcg(css));
  76. }
  77. static struct ioprio_blkcg *ioprio_blkcg_from_bio(struct bio *bio)
  78. {
  79. struct blkg_policy_data *pd = blkg_to_pd(bio->bi_blkg, &ioprio_policy);
  80. if (!pd)
  81. return NULL;
  82. return blkcg_to_ioprio_blkcg(pd->blkg->blkcg);
  83. }
  84. static int ioprio_show_prio_policy(struct seq_file *sf, void *v)
  85. {
  86. struct ioprio_blkcg *blkcg = ioprio_blkcg_from_css(seq_css(sf));
  87. seq_printf(sf, "%s\n", policy_name[blkcg->prio_policy]);
  88. return 0;
  89. }
  90. static ssize_t ioprio_set_prio_policy(struct kernfs_open_file *of, char *buf,
  91. size_t nbytes, loff_t off)
  92. {
  93. struct ioprio_blkcg *blkcg = ioprio_blkcg_from_css(of_css(of));
  94. int ret;
  95. if (off != 0)
  96. return -EIO;
  97. /* kernfs_fop_write_iter() terminates 'buf' with '\0'. */
  98. ret = sysfs_match_string(policy_name, buf);
  99. if (ret < 0)
  100. return ret;
  101. blkcg->prio_policy = ret;
  102. return nbytes;
  103. }
  104. static struct blkg_policy_data *
  105. ioprio_alloc_pd(gfp_t gfp, struct request_queue *q, struct blkcg *blkcg)
  106. {
  107. struct ioprio_blkg *ioprio_blkg;
  108. ioprio_blkg = kzalloc(sizeof(*ioprio_blkg), gfp);
  109. if (!ioprio_blkg)
  110. return NULL;
  111. return &ioprio_blkg->pd;
  112. }
  113. static void ioprio_free_pd(struct blkg_policy_data *pd)
  114. {
  115. struct ioprio_blkg *ioprio_blkg = pd_to_ioprio(pd);
  116. kfree(ioprio_blkg);
  117. }
  118. static struct blkcg_policy_data *ioprio_alloc_cpd(gfp_t gfp)
  119. {
  120. struct ioprio_blkcg *blkcg;
  121. blkcg = kzalloc(sizeof(*blkcg), gfp);
  122. if (!blkcg)
  123. return NULL;
  124. blkcg->prio_policy = POLICY_NO_CHANGE;
  125. return &blkcg->cpd;
  126. }
  127. static void ioprio_free_cpd(struct blkcg_policy_data *cpd)
  128. {
  129. struct ioprio_blkcg *blkcg = container_of(cpd, typeof(*blkcg), cpd);
  130. kfree(blkcg);
  131. }
  132. #define IOPRIO_ATTRS \
  133. { \
  134. .name = "prio.class", \
  135. .seq_show = ioprio_show_prio_policy, \
  136. .write = ioprio_set_prio_policy, \
  137. }, \
  138. { } /* sentinel */
  139. /* cgroup v2 attributes */
  140. static struct cftype ioprio_files[] = {
  141. IOPRIO_ATTRS
  142. };
  143. /* cgroup v1 attributes */
  144. static struct cftype ioprio_legacy_files[] = {
  145. IOPRIO_ATTRS
  146. };
  147. static struct blkcg_policy ioprio_policy = {
  148. .dfl_cftypes = ioprio_files,
  149. .legacy_cftypes = ioprio_legacy_files,
  150. .cpd_alloc_fn = ioprio_alloc_cpd,
  151. .cpd_free_fn = ioprio_free_cpd,
  152. .pd_alloc_fn = ioprio_alloc_pd,
  153. .pd_free_fn = ioprio_free_pd,
  154. };
  155. void blkcg_set_ioprio(struct bio *bio)
  156. {
  157. struct ioprio_blkcg *blkcg = ioprio_blkcg_from_bio(bio);
  158. u16 prio;
  159. if (!blkcg || blkcg->prio_policy == POLICY_NO_CHANGE)
  160. return;
  161. if (blkcg->prio_policy == POLICY_PROMOTE_TO_RT ||
  162. blkcg->prio_policy == POLICY_NONE_TO_RT) {
  163. /*
  164. * For RT threads, the default priority level is 4 because
  165. * task_nice is 0. By promoting non-RT io-priority to RT-class
  166. * and default level 4, those requests that are already
  167. * RT-class but need a higher io-priority can use ioprio_set()
  168. * to achieve this.
  169. */
  170. if (IOPRIO_PRIO_CLASS(bio->bi_ioprio) != IOPRIO_CLASS_RT)
  171. bio->bi_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, 4);
  172. return;
  173. }
  174. /*
  175. * Except for IOPRIO_CLASS_NONE, higher I/O priority numbers
  176. * correspond to a lower priority. Hence, the max_t() below selects
  177. * the lower priority of bi_ioprio and the cgroup I/O priority class.
  178. * If the bio I/O priority equals IOPRIO_CLASS_NONE, the cgroup I/O
  179. * priority is assigned to the bio.
  180. */
  181. prio = max_t(u16, bio->bi_ioprio,
  182. IOPRIO_PRIO_VALUE(blkcg->prio_policy, 0));
  183. if (prio > bio->bi_ioprio)
  184. bio->bi_ioprio = prio;
  185. }
  186. void blk_ioprio_exit(struct gendisk *disk)
  187. {
  188. blkcg_deactivate_policy(disk->queue, &ioprio_policy);
  189. }
  190. int blk_ioprio_init(struct gendisk *disk)
  191. {
  192. return blkcg_activate_policy(disk->queue, &ioprio_policy);
  193. }
  194. static int __init ioprio_init(void)
  195. {
  196. return blkcg_policy_register(&ioprio_policy);
  197. }
  198. static void __exit ioprio_exit(void)
  199. {
  200. blkcg_policy_unregister(&ioprio_policy);
  201. }
  202. module_init(ioprio_init);
  203. module_exit(ioprio_exit);