blk-throttle.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /*
  2. * Interface for controlling IO bandwidth on a request queue
  3. *
  4. * Copyright (C) 2010 Vivek Goyal <vgoyal@redhat.com>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/bio.h>
  10. #include <linux/blktrace_api.h>
  11. #include "blk-cgroup.h"
  12. #include "blk.h"
  13. /* Max dispatch from a group in 1 round */
  14. static int throtl_grp_quantum = 8;
  15. /* Total max dispatch from all groups in one round */
  16. static int throtl_quantum = 32;
  17. /* Throttling is performed over 100ms slice and after that slice is renewed */
  18. static unsigned long throtl_slice = HZ/10; /* 100 ms */
  19. static struct blkio_policy_type blkio_policy_throtl;
  20. /* A workqueue to queue throttle related work */
  21. static struct workqueue_struct *kthrotld_workqueue;
  22. static void throtl_schedule_delayed_work(struct throtl_data *td,
  23. unsigned long delay);
  24. struct throtl_rb_root {
  25. struct rb_root rb;
  26. struct rb_node *left;
  27. unsigned int count;
  28. unsigned long min_disptime;
  29. };
  30. #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
  31. .count = 0, .min_disptime = 0}
  32. #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
  33. struct throtl_grp {
  34. /* List of throtl groups on the request queue*/
  35. struct hlist_node tg_node;
  36. /* active throtl group service_tree member */
  37. struct rb_node rb_node;
  38. /*
  39. * Dispatch time in jiffies. This is the estimated time when group
  40. * will unthrottle and is ready to dispatch more bio. It is used as
  41. * key to sort active groups in service tree.
  42. */
  43. unsigned long disptime;
  44. unsigned int flags;
  45. /* Two lists for READ and WRITE */
  46. struct bio_list bio_lists[2];
  47. /* Number of queued bios on READ and WRITE lists */
  48. unsigned int nr_queued[2];
  49. /* bytes per second rate limits */
  50. uint64_t bps[2];
  51. /* IOPS limits */
  52. unsigned int iops[2];
  53. /* Number of bytes disptached in current slice */
  54. uint64_t bytes_disp[2];
  55. /* Number of bio's dispatched in current slice */
  56. unsigned int io_disp[2];
  57. /* When did we start a new slice */
  58. unsigned long slice_start[2];
  59. unsigned long slice_end[2];
  60. /* Some throttle limits got updated for the group */
  61. int limits_changed;
  62. };
  63. struct throtl_data
  64. {
  65. /* List of throtl groups */
  66. struct hlist_head tg_list;
  67. /* service tree for active throtl groups */
  68. struct throtl_rb_root tg_service_tree;
  69. struct throtl_grp *root_tg;
  70. struct request_queue *queue;
  71. /* Total Number of queued bios on READ and WRITE lists */
  72. unsigned int nr_queued[2];
  73. /*
  74. * number of total undestroyed groups
  75. */
  76. unsigned int nr_undestroyed_grps;
  77. /* Work for dispatching throttled bios */
  78. struct delayed_work throtl_work;
  79. int limits_changed;
  80. };
  81. static inline struct throtl_grp *blkg_to_tg(struct blkio_group *blkg)
  82. {
  83. return blkg_to_pdata(blkg, &blkio_policy_throtl);
  84. }
  85. static inline struct blkio_group *tg_to_blkg(struct throtl_grp *tg)
  86. {
  87. return pdata_to_blkg(tg, &blkio_policy_throtl);
  88. }
  89. enum tg_state_flags {
  90. THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
  91. };
  92. #define THROTL_TG_FNS(name) \
  93. static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
  94. { \
  95. (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
  96. } \
  97. static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
  98. { \
  99. (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
  100. } \
  101. static inline int throtl_tg_##name(const struct throtl_grp *tg) \
  102. { \
  103. return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
  104. }
  105. THROTL_TG_FNS(on_rr);
  106. #define throtl_log_tg(td, tg, fmt, args...) \
  107. blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
  108. blkg_path(tg_to_blkg(tg)), ##args); \
  109. #define throtl_log(td, fmt, args...) \
  110. blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
  111. static inline unsigned int total_nr_queued(struct throtl_data *td)
  112. {
  113. return td->nr_queued[0] + td->nr_queued[1];
  114. }
  115. static void throtl_init_blkio_group(struct blkio_group *blkg)
  116. {
  117. struct throtl_grp *tg = blkg_to_tg(blkg);
  118. INIT_HLIST_NODE(&tg->tg_node);
  119. RB_CLEAR_NODE(&tg->rb_node);
  120. bio_list_init(&tg->bio_lists[0]);
  121. bio_list_init(&tg->bio_lists[1]);
  122. tg->limits_changed = false;
  123. tg->bps[READ] = -1;
  124. tg->bps[WRITE] = -1;
  125. tg->iops[READ] = -1;
  126. tg->iops[WRITE] = -1;
  127. }
  128. static void throtl_link_blkio_group(struct request_queue *q,
  129. struct blkio_group *blkg)
  130. {
  131. struct throtl_data *td = q->td;
  132. struct throtl_grp *tg = blkg_to_tg(blkg);
  133. hlist_add_head(&tg->tg_node, &td->tg_list);
  134. td->nr_undestroyed_grps++;
  135. }
  136. static struct
  137. throtl_grp *throtl_lookup_tg(struct throtl_data *td, struct blkio_cgroup *blkcg)
  138. {
  139. /*
  140. * This is the common case when there are no blkio cgroups.
  141. * Avoid lookup in this case
  142. */
  143. if (blkcg == &blkio_root_cgroup)
  144. return td->root_tg;
  145. return blkg_to_tg(blkg_lookup(blkcg, td->queue, BLKIO_POLICY_THROTL));
  146. }
  147. static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
  148. struct blkio_cgroup *blkcg)
  149. {
  150. struct request_queue *q = td->queue;
  151. struct throtl_grp *tg = NULL;
  152. /*
  153. * This is the common case when there are no blkio cgroups.
  154. * Avoid lookup in this case
  155. */
  156. if (blkcg == &blkio_root_cgroup) {
  157. tg = td->root_tg;
  158. } else {
  159. struct blkio_group *blkg;
  160. blkg = blkg_lookup_create(blkcg, q, BLKIO_POLICY_THROTL, false);
  161. /* if %NULL and @q is alive, fall back to root_tg */
  162. if (!IS_ERR(blkg))
  163. tg = blkg_to_tg(blkg);
  164. else if (!blk_queue_dead(q))
  165. tg = td->root_tg;
  166. }
  167. return tg;
  168. }
  169. static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
  170. {
  171. /* Service tree is empty */
  172. if (!root->count)
  173. return NULL;
  174. if (!root->left)
  175. root->left = rb_first(&root->rb);
  176. if (root->left)
  177. return rb_entry_tg(root->left);
  178. return NULL;
  179. }
  180. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  181. {
  182. rb_erase(n, root);
  183. RB_CLEAR_NODE(n);
  184. }
  185. static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
  186. {
  187. if (root->left == n)
  188. root->left = NULL;
  189. rb_erase_init(n, &root->rb);
  190. --root->count;
  191. }
  192. static void update_min_dispatch_time(struct throtl_rb_root *st)
  193. {
  194. struct throtl_grp *tg;
  195. tg = throtl_rb_first(st);
  196. if (!tg)
  197. return;
  198. st->min_disptime = tg->disptime;
  199. }
  200. static void
  201. tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
  202. {
  203. struct rb_node **node = &st->rb.rb_node;
  204. struct rb_node *parent = NULL;
  205. struct throtl_grp *__tg;
  206. unsigned long key = tg->disptime;
  207. int left = 1;
  208. while (*node != NULL) {
  209. parent = *node;
  210. __tg = rb_entry_tg(parent);
  211. if (time_before(key, __tg->disptime))
  212. node = &parent->rb_left;
  213. else {
  214. node = &parent->rb_right;
  215. left = 0;
  216. }
  217. }
  218. if (left)
  219. st->left = &tg->rb_node;
  220. rb_link_node(&tg->rb_node, parent, node);
  221. rb_insert_color(&tg->rb_node, &st->rb);
  222. }
  223. static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  224. {
  225. struct throtl_rb_root *st = &td->tg_service_tree;
  226. tg_service_tree_add(st, tg);
  227. throtl_mark_tg_on_rr(tg);
  228. st->count++;
  229. }
  230. static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  231. {
  232. if (!throtl_tg_on_rr(tg))
  233. __throtl_enqueue_tg(td, tg);
  234. }
  235. static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  236. {
  237. throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
  238. throtl_clear_tg_on_rr(tg);
  239. }
  240. static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  241. {
  242. if (throtl_tg_on_rr(tg))
  243. __throtl_dequeue_tg(td, tg);
  244. }
  245. static void throtl_schedule_next_dispatch(struct throtl_data *td)
  246. {
  247. struct throtl_rb_root *st = &td->tg_service_tree;
  248. /*
  249. * If there are more bios pending, schedule more work.
  250. */
  251. if (!total_nr_queued(td))
  252. return;
  253. BUG_ON(!st->count);
  254. update_min_dispatch_time(st);
  255. if (time_before_eq(st->min_disptime, jiffies))
  256. throtl_schedule_delayed_work(td, 0);
  257. else
  258. throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
  259. }
  260. static inline void
  261. throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  262. {
  263. tg->bytes_disp[rw] = 0;
  264. tg->io_disp[rw] = 0;
  265. tg->slice_start[rw] = jiffies;
  266. tg->slice_end[rw] = jiffies + throtl_slice;
  267. throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
  268. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  269. tg->slice_end[rw], jiffies);
  270. }
  271. static inline void throtl_set_slice_end(struct throtl_data *td,
  272. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  273. {
  274. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  275. }
  276. static inline void throtl_extend_slice(struct throtl_data *td,
  277. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  278. {
  279. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  280. throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
  281. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  282. tg->slice_end[rw], jiffies);
  283. }
  284. /* Determine if previously allocated or extended slice is complete or not */
  285. static bool
  286. throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  287. {
  288. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  289. return 0;
  290. return 1;
  291. }
  292. /* Trim the used slices and adjust slice start accordingly */
  293. static inline void
  294. throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  295. {
  296. unsigned long nr_slices, time_elapsed, io_trim;
  297. u64 bytes_trim, tmp;
  298. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  299. /*
  300. * If bps are unlimited (-1), then time slice don't get
  301. * renewed. Don't try to trim the slice if slice is used. A new
  302. * slice will start when appropriate.
  303. */
  304. if (throtl_slice_used(td, tg, rw))
  305. return;
  306. /*
  307. * A bio has been dispatched. Also adjust slice_end. It might happen
  308. * that initially cgroup limit was very low resulting in high
  309. * slice_end, but later limit was bumped up and bio was dispached
  310. * sooner, then we need to reduce slice_end. A high bogus slice_end
  311. * is bad because it does not allow new slice to start.
  312. */
  313. throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
  314. time_elapsed = jiffies - tg->slice_start[rw];
  315. nr_slices = time_elapsed / throtl_slice;
  316. if (!nr_slices)
  317. return;
  318. tmp = tg->bps[rw] * throtl_slice * nr_slices;
  319. do_div(tmp, HZ);
  320. bytes_trim = tmp;
  321. io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
  322. if (!bytes_trim && !io_trim)
  323. return;
  324. if (tg->bytes_disp[rw] >= bytes_trim)
  325. tg->bytes_disp[rw] -= bytes_trim;
  326. else
  327. tg->bytes_disp[rw] = 0;
  328. if (tg->io_disp[rw] >= io_trim)
  329. tg->io_disp[rw] -= io_trim;
  330. else
  331. tg->io_disp[rw] = 0;
  332. tg->slice_start[rw] += nr_slices * throtl_slice;
  333. throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
  334. " start=%lu end=%lu jiffies=%lu",
  335. rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
  336. tg->slice_start[rw], tg->slice_end[rw], jiffies);
  337. }
  338. static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
  339. struct bio *bio, unsigned long *wait)
  340. {
  341. bool rw = bio_data_dir(bio);
  342. unsigned int io_allowed;
  343. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  344. u64 tmp;
  345. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  346. /* Slice has just started. Consider one slice interval */
  347. if (!jiffy_elapsed)
  348. jiffy_elapsed_rnd = throtl_slice;
  349. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  350. /*
  351. * jiffy_elapsed_rnd should not be a big value as minimum iops can be
  352. * 1 then at max jiffy elapsed should be equivalent of 1 second as we
  353. * will allow dispatch after 1 second and after that slice should
  354. * have been trimmed.
  355. */
  356. tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
  357. do_div(tmp, HZ);
  358. if (tmp > UINT_MAX)
  359. io_allowed = UINT_MAX;
  360. else
  361. io_allowed = tmp;
  362. if (tg->io_disp[rw] + 1 <= io_allowed) {
  363. if (wait)
  364. *wait = 0;
  365. return 1;
  366. }
  367. /* Calc approx time to dispatch */
  368. jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
  369. if (jiffy_wait > jiffy_elapsed)
  370. jiffy_wait = jiffy_wait - jiffy_elapsed;
  371. else
  372. jiffy_wait = 1;
  373. if (wait)
  374. *wait = jiffy_wait;
  375. return 0;
  376. }
  377. static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
  378. struct bio *bio, unsigned long *wait)
  379. {
  380. bool rw = bio_data_dir(bio);
  381. u64 bytes_allowed, extra_bytes, tmp;
  382. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  383. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  384. /* Slice has just started. Consider one slice interval */
  385. if (!jiffy_elapsed)
  386. jiffy_elapsed_rnd = throtl_slice;
  387. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  388. tmp = tg->bps[rw] * jiffy_elapsed_rnd;
  389. do_div(tmp, HZ);
  390. bytes_allowed = tmp;
  391. if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
  392. if (wait)
  393. *wait = 0;
  394. return 1;
  395. }
  396. /* Calc approx time to dispatch */
  397. extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
  398. jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
  399. if (!jiffy_wait)
  400. jiffy_wait = 1;
  401. /*
  402. * This wait time is without taking into consideration the rounding
  403. * up we did. Add that time also.
  404. */
  405. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  406. if (wait)
  407. *wait = jiffy_wait;
  408. return 0;
  409. }
  410. static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
  411. if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
  412. return 1;
  413. return 0;
  414. }
  415. /*
  416. * Returns whether one can dispatch a bio or not. Also returns approx number
  417. * of jiffies to wait before this bio is with-in IO rate and can be dispatched
  418. */
  419. static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
  420. struct bio *bio, unsigned long *wait)
  421. {
  422. bool rw = bio_data_dir(bio);
  423. unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
  424. /*
  425. * Currently whole state machine of group depends on first bio
  426. * queued in the group bio list. So one should not be calling
  427. * this function with a different bio if there are other bios
  428. * queued.
  429. */
  430. BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
  431. /* If tg->bps = -1, then BW is unlimited */
  432. if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
  433. if (wait)
  434. *wait = 0;
  435. return 1;
  436. }
  437. /*
  438. * If previous slice expired, start a new one otherwise renew/extend
  439. * existing slice to make sure it is at least throtl_slice interval
  440. * long since now.
  441. */
  442. if (throtl_slice_used(td, tg, rw))
  443. throtl_start_new_slice(td, tg, rw);
  444. else {
  445. if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
  446. throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
  447. }
  448. if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
  449. && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
  450. if (wait)
  451. *wait = 0;
  452. return 1;
  453. }
  454. max_wait = max(bps_wait, iops_wait);
  455. if (wait)
  456. *wait = max_wait;
  457. if (time_before(tg->slice_end[rw], jiffies + max_wait))
  458. throtl_extend_slice(td, tg, rw, jiffies + max_wait);
  459. return 0;
  460. }
  461. static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
  462. {
  463. bool rw = bio_data_dir(bio);
  464. bool sync = rw_is_sync(bio->bi_rw);
  465. /* Charge the bio to the group */
  466. tg->bytes_disp[rw] += bio->bi_size;
  467. tg->io_disp[rw]++;
  468. blkiocg_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, rw, sync);
  469. }
  470. static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
  471. struct bio *bio)
  472. {
  473. bool rw = bio_data_dir(bio);
  474. bio_list_add(&tg->bio_lists[rw], bio);
  475. /* Take a bio reference on tg */
  476. blkg_get(tg_to_blkg(tg));
  477. tg->nr_queued[rw]++;
  478. td->nr_queued[rw]++;
  479. throtl_enqueue_tg(td, tg);
  480. }
  481. static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
  482. {
  483. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  484. struct bio *bio;
  485. if ((bio = bio_list_peek(&tg->bio_lists[READ])))
  486. tg_may_dispatch(td, tg, bio, &read_wait);
  487. if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
  488. tg_may_dispatch(td, tg, bio, &write_wait);
  489. min_wait = min(read_wait, write_wait);
  490. disptime = jiffies + min_wait;
  491. /* Update dispatch time */
  492. throtl_dequeue_tg(td, tg);
  493. tg->disptime = disptime;
  494. throtl_enqueue_tg(td, tg);
  495. }
  496. static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
  497. bool rw, struct bio_list *bl)
  498. {
  499. struct bio *bio;
  500. bio = bio_list_pop(&tg->bio_lists[rw]);
  501. tg->nr_queued[rw]--;
  502. /* Drop bio reference on blkg */
  503. blkg_put(tg_to_blkg(tg));
  504. BUG_ON(td->nr_queued[rw] <= 0);
  505. td->nr_queued[rw]--;
  506. throtl_charge_bio(tg, bio);
  507. bio_list_add(bl, bio);
  508. bio->bi_rw |= REQ_THROTTLED;
  509. throtl_trim_slice(td, tg, rw);
  510. }
  511. static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
  512. struct bio_list *bl)
  513. {
  514. unsigned int nr_reads = 0, nr_writes = 0;
  515. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  516. unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
  517. struct bio *bio;
  518. /* Try to dispatch 75% READS and 25% WRITES */
  519. while ((bio = bio_list_peek(&tg->bio_lists[READ]))
  520. && tg_may_dispatch(td, tg, bio, NULL)) {
  521. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  522. nr_reads++;
  523. if (nr_reads >= max_nr_reads)
  524. break;
  525. }
  526. while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
  527. && tg_may_dispatch(td, tg, bio, NULL)) {
  528. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  529. nr_writes++;
  530. if (nr_writes >= max_nr_writes)
  531. break;
  532. }
  533. return nr_reads + nr_writes;
  534. }
  535. static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
  536. {
  537. unsigned int nr_disp = 0;
  538. struct throtl_grp *tg;
  539. struct throtl_rb_root *st = &td->tg_service_tree;
  540. while (1) {
  541. tg = throtl_rb_first(st);
  542. if (!tg)
  543. break;
  544. if (time_before(jiffies, tg->disptime))
  545. break;
  546. throtl_dequeue_tg(td, tg);
  547. nr_disp += throtl_dispatch_tg(td, tg, bl);
  548. if (tg->nr_queued[0] || tg->nr_queued[1]) {
  549. tg_update_disptime(td, tg);
  550. throtl_enqueue_tg(td, tg);
  551. }
  552. if (nr_disp >= throtl_quantum)
  553. break;
  554. }
  555. return nr_disp;
  556. }
  557. static void throtl_process_limit_change(struct throtl_data *td)
  558. {
  559. struct throtl_grp *tg;
  560. struct hlist_node *pos, *n;
  561. if (!td->limits_changed)
  562. return;
  563. xchg(&td->limits_changed, false);
  564. throtl_log(td, "limits changed");
  565. hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
  566. if (!tg->limits_changed)
  567. continue;
  568. if (!xchg(&tg->limits_changed, false))
  569. continue;
  570. throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
  571. " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
  572. tg->iops[READ], tg->iops[WRITE]);
  573. /*
  574. * Restart the slices for both READ and WRITES. It
  575. * might happen that a group's limit are dropped
  576. * suddenly and we don't want to account recently
  577. * dispatched IO with new low rate
  578. */
  579. throtl_start_new_slice(td, tg, 0);
  580. throtl_start_new_slice(td, tg, 1);
  581. if (throtl_tg_on_rr(tg))
  582. tg_update_disptime(td, tg);
  583. }
  584. }
  585. /* Dispatch throttled bios. Should be called without queue lock held. */
  586. static int throtl_dispatch(struct request_queue *q)
  587. {
  588. struct throtl_data *td = q->td;
  589. unsigned int nr_disp = 0;
  590. struct bio_list bio_list_on_stack;
  591. struct bio *bio;
  592. struct blk_plug plug;
  593. spin_lock_irq(q->queue_lock);
  594. throtl_process_limit_change(td);
  595. if (!total_nr_queued(td))
  596. goto out;
  597. bio_list_init(&bio_list_on_stack);
  598. throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
  599. total_nr_queued(td), td->nr_queued[READ],
  600. td->nr_queued[WRITE]);
  601. nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
  602. if (nr_disp)
  603. throtl_log(td, "bios disp=%u", nr_disp);
  604. throtl_schedule_next_dispatch(td);
  605. out:
  606. spin_unlock_irq(q->queue_lock);
  607. /*
  608. * If we dispatched some requests, unplug the queue to make sure
  609. * immediate dispatch
  610. */
  611. if (nr_disp) {
  612. blk_start_plug(&plug);
  613. while((bio = bio_list_pop(&bio_list_on_stack)))
  614. generic_make_request(bio);
  615. blk_finish_plug(&plug);
  616. }
  617. return nr_disp;
  618. }
  619. void blk_throtl_work(struct work_struct *work)
  620. {
  621. struct throtl_data *td = container_of(work, struct throtl_data,
  622. throtl_work.work);
  623. struct request_queue *q = td->queue;
  624. throtl_dispatch(q);
  625. }
  626. /* Call with queue lock held */
  627. static void
  628. throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
  629. {
  630. struct delayed_work *dwork = &td->throtl_work;
  631. /* schedule work if limits changed even if no bio is queued */
  632. if (total_nr_queued(td) || td->limits_changed) {
  633. /*
  634. * We might have a work scheduled to be executed in future.
  635. * Cancel that and schedule a new one.
  636. */
  637. __cancel_delayed_work(dwork);
  638. queue_delayed_work(kthrotld_workqueue, dwork, delay);
  639. throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
  640. delay, jiffies);
  641. }
  642. }
  643. static void
  644. throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
  645. {
  646. /* Something wrong if we are trying to remove same group twice */
  647. BUG_ON(hlist_unhashed(&tg->tg_node));
  648. hlist_del_init(&tg->tg_node);
  649. /*
  650. * Put the reference taken at the time of creation so that when all
  651. * queues are gone, group can be destroyed.
  652. */
  653. blkg_put(tg_to_blkg(tg));
  654. td->nr_undestroyed_grps--;
  655. }
  656. static bool throtl_release_tgs(struct throtl_data *td, bool release_root)
  657. {
  658. struct hlist_node *pos, *n;
  659. struct throtl_grp *tg;
  660. bool empty = true;
  661. hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
  662. /* skip root? */
  663. if (!release_root && tg == td->root_tg)
  664. continue;
  665. /*
  666. * If cgroup removal path got to blk_group first and removed
  667. * it from cgroup list, then it will take care of destroying
  668. * cfqg also.
  669. */
  670. if (!blkiocg_del_blkio_group(tg_to_blkg(tg)))
  671. throtl_destroy_tg(td, tg);
  672. else
  673. empty = false;
  674. }
  675. return empty;
  676. }
  677. /*
  678. * Blk cgroup controller notification saying that blkio_group object is being
  679. * delinked as associated cgroup object is going away. That also means that
  680. * no new IO will come in this group. So get rid of this group as soon as
  681. * any pending IO in the group is finished.
  682. *
  683. * This function is called under rcu_read_lock(). @q is the rcu protected
  684. * pointer. That means @q is a valid request_queue pointer as long as we
  685. * are rcu read lock.
  686. *
  687. * @q was fetched from blkio_group under blkio_cgroup->lock. That means
  688. * it should not be NULL as even if queue was going away, cgroup deltion
  689. * path got to it first.
  690. */
  691. void throtl_unlink_blkio_group(struct request_queue *q,
  692. struct blkio_group *blkg)
  693. {
  694. unsigned long flags;
  695. spin_lock_irqsave(q->queue_lock, flags);
  696. throtl_destroy_tg(q->td, blkg_to_tg(blkg));
  697. spin_unlock_irqrestore(q->queue_lock, flags);
  698. }
  699. static bool throtl_clear_queue(struct request_queue *q)
  700. {
  701. lockdep_assert_held(q->queue_lock);
  702. /*
  703. * Clear tgs but leave the root one alone. This is necessary
  704. * because root_tg is expected to be persistent and safe because
  705. * blk-throtl can never be disabled while @q is alive. This is a
  706. * kludge to prepare for unified blkg. This whole function will be
  707. * removed soon.
  708. */
  709. return throtl_release_tgs(q->td, false);
  710. }
  711. static void throtl_update_blkio_group_common(struct throtl_data *td,
  712. struct throtl_grp *tg)
  713. {
  714. xchg(&tg->limits_changed, true);
  715. xchg(&td->limits_changed, true);
  716. /* Schedule a work now to process the limit change */
  717. throtl_schedule_delayed_work(td, 0);
  718. }
  719. /*
  720. * For all update functions, @q should be a valid pointer because these
  721. * update functions are called under blkcg_lock, that means, blkg is
  722. * valid and in turn @q is valid. queue exit path can not race because
  723. * of blkcg_lock
  724. *
  725. * Can not take queue lock in update functions as queue lock under blkcg_lock
  726. * is not allowed. Under other paths we take blkcg_lock under queue_lock.
  727. */
  728. static void throtl_update_blkio_group_read_bps(struct request_queue *q,
  729. struct blkio_group *blkg, u64 read_bps)
  730. {
  731. struct throtl_grp *tg = blkg_to_tg(blkg);
  732. tg->bps[READ] = read_bps;
  733. throtl_update_blkio_group_common(q->td, tg);
  734. }
  735. static void throtl_update_blkio_group_write_bps(struct request_queue *q,
  736. struct blkio_group *blkg, u64 write_bps)
  737. {
  738. struct throtl_grp *tg = blkg_to_tg(blkg);
  739. tg->bps[WRITE] = write_bps;
  740. throtl_update_blkio_group_common(q->td, tg);
  741. }
  742. static void throtl_update_blkio_group_read_iops(struct request_queue *q,
  743. struct blkio_group *blkg, unsigned int read_iops)
  744. {
  745. struct throtl_grp *tg = blkg_to_tg(blkg);
  746. tg->iops[READ] = read_iops;
  747. throtl_update_blkio_group_common(q->td, tg);
  748. }
  749. static void throtl_update_blkio_group_write_iops(struct request_queue *q,
  750. struct blkio_group *blkg, unsigned int write_iops)
  751. {
  752. struct throtl_grp *tg = blkg_to_tg(blkg);
  753. tg->iops[WRITE] = write_iops;
  754. throtl_update_blkio_group_common(q->td, tg);
  755. }
  756. static void throtl_shutdown_wq(struct request_queue *q)
  757. {
  758. struct throtl_data *td = q->td;
  759. cancel_delayed_work_sync(&td->throtl_work);
  760. }
  761. static struct blkio_policy_type blkio_policy_throtl = {
  762. .ops = {
  763. .blkio_init_group_fn = throtl_init_blkio_group,
  764. .blkio_link_group_fn = throtl_link_blkio_group,
  765. .blkio_unlink_group_fn = throtl_unlink_blkio_group,
  766. .blkio_clear_queue_fn = throtl_clear_queue,
  767. .blkio_update_group_read_bps_fn =
  768. throtl_update_blkio_group_read_bps,
  769. .blkio_update_group_write_bps_fn =
  770. throtl_update_blkio_group_write_bps,
  771. .blkio_update_group_read_iops_fn =
  772. throtl_update_blkio_group_read_iops,
  773. .blkio_update_group_write_iops_fn =
  774. throtl_update_blkio_group_write_iops,
  775. },
  776. .plid = BLKIO_POLICY_THROTL,
  777. .pdata_size = sizeof(struct throtl_grp),
  778. };
  779. bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
  780. {
  781. struct throtl_data *td = q->td;
  782. struct throtl_grp *tg;
  783. bool rw = bio_data_dir(bio), update_disptime = true;
  784. struct blkio_cgroup *blkcg;
  785. bool throttled = false;
  786. if (bio->bi_rw & REQ_THROTTLED) {
  787. bio->bi_rw &= ~REQ_THROTTLED;
  788. goto out;
  789. }
  790. /*
  791. * A throtl_grp pointer retrieved under rcu can be used to access
  792. * basic fields like stats and io rates. If a group has no rules,
  793. * just update the dispatch stats in lockless manner and return.
  794. */
  795. rcu_read_lock();
  796. blkcg = task_blkio_cgroup(current);
  797. tg = throtl_lookup_tg(td, blkcg);
  798. if (tg) {
  799. if (tg_no_rule_group(tg, rw)) {
  800. blkiocg_update_dispatch_stats(tg_to_blkg(tg),
  801. bio->bi_size, rw,
  802. rw_is_sync(bio->bi_rw));
  803. goto out_unlock_rcu;
  804. }
  805. }
  806. /*
  807. * Either group has not been allocated yet or it is not an unlimited
  808. * IO group
  809. */
  810. spin_lock_irq(q->queue_lock);
  811. tg = throtl_lookup_create_tg(td, blkcg);
  812. if (unlikely(!tg))
  813. goto out_unlock;
  814. if (tg->nr_queued[rw]) {
  815. /*
  816. * There is already another bio queued in same dir. No
  817. * need to update dispatch time.
  818. */
  819. update_disptime = false;
  820. goto queue_bio;
  821. }
  822. /* Bio is with-in rate limit of group */
  823. if (tg_may_dispatch(td, tg, bio, NULL)) {
  824. throtl_charge_bio(tg, bio);
  825. /*
  826. * We need to trim slice even when bios are not being queued
  827. * otherwise it might happen that a bio is not queued for
  828. * a long time and slice keeps on extending and trim is not
  829. * called for a long time. Now if limits are reduced suddenly
  830. * we take into account all the IO dispatched so far at new
  831. * low rate and * newly queued IO gets a really long dispatch
  832. * time.
  833. *
  834. * So keep on trimming slice even if bio is not queued.
  835. */
  836. throtl_trim_slice(td, tg, rw);
  837. goto out_unlock;
  838. }
  839. queue_bio:
  840. throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
  841. " iodisp=%u iops=%u queued=%d/%d",
  842. rw == READ ? 'R' : 'W',
  843. tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
  844. tg->io_disp[rw], tg->iops[rw],
  845. tg->nr_queued[READ], tg->nr_queued[WRITE]);
  846. throtl_add_bio_tg(q->td, tg, bio);
  847. throttled = true;
  848. if (update_disptime) {
  849. tg_update_disptime(td, tg);
  850. throtl_schedule_next_dispatch(td);
  851. }
  852. out_unlock:
  853. spin_unlock_irq(q->queue_lock);
  854. out_unlock_rcu:
  855. rcu_read_unlock();
  856. out:
  857. return throttled;
  858. }
  859. /**
  860. * blk_throtl_drain - drain throttled bios
  861. * @q: request_queue to drain throttled bios for
  862. *
  863. * Dispatch all currently throttled bios on @q through ->make_request_fn().
  864. */
  865. void blk_throtl_drain(struct request_queue *q)
  866. __releases(q->queue_lock) __acquires(q->queue_lock)
  867. {
  868. struct throtl_data *td = q->td;
  869. struct throtl_rb_root *st = &td->tg_service_tree;
  870. struct throtl_grp *tg;
  871. struct bio_list bl;
  872. struct bio *bio;
  873. WARN_ON_ONCE(!queue_is_locked(q));
  874. bio_list_init(&bl);
  875. while ((tg = throtl_rb_first(st))) {
  876. throtl_dequeue_tg(td, tg);
  877. while ((bio = bio_list_peek(&tg->bio_lists[READ])))
  878. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
  879. while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
  880. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
  881. }
  882. spin_unlock_irq(q->queue_lock);
  883. while ((bio = bio_list_pop(&bl)))
  884. generic_make_request(bio);
  885. spin_lock_irq(q->queue_lock);
  886. }
  887. int blk_throtl_init(struct request_queue *q)
  888. {
  889. struct throtl_data *td;
  890. struct blkio_group *blkg;
  891. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  892. if (!td)
  893. return -ENOMEM;
  894. INIT_HLIST_HEAD(&td->tg_list);
  895. td->tg_service_tree = THROTL_RB_ROOT;
  896. td->limits_changed = false;
  897. INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
  898. q->td = td;
  899. td->queue = q;
  900. /* alloc and init root group. */
  901. rcu_read_lock();
  902. spin_lock_irq(q->queue_lock);
  903. blkg = blkg_lookup_create(&blkio_root_cgroup, q, BLKIO_POLICY_THROTL,
  904. true);
  905. if (!IS_ERR(blkg))
  906. td->root_tg = blkg_to_tg(blkg);
  907. spin_unlock_irq(q->queue_lock);
  908. rcu_read_unlock();
  909. if (!td->root_tg) {
  910. kfree(td);
  911. return -ENOMEM;
  912. }
  913. return 0;
  914. }
  915. void blk_throtl_exit(struct request_queue *q)
  916. {
  917. struct throtl_data *td = q->td;
  918. bool wait = false;
  919. BUG_ON(!td);
  920. throtl_shutdown_wq(q);
  921. spin_lock_irq(q->queue_lock);
  922. throtl_release_tgs(td, true);
  923. /* If there are other groups */
  924. if (td->nr_undestroyed_grps > 0)
  925. wait = true;
  926. spin_unlock_irq(q->queue_lock);
  927. /*
  928. * Wait for tg_to_blkg(tg)->q accessors to exit their grace periods.
  929. * Do this wait only if there are other undestroyed groups out
  930. * there (other than root group). This can happen if cgroup deletion
  931. * path claimed the responsibility of cleaning up a group before
  932. * queue cleanup code get to the group.
  933. *
  934. * Do not call synchronize_rcu() unconditionally as there are drivers
  935. * which create/delete request queue hundreds of times during scan/boot
  936. * and synchronize_rcu() can take significant time and slow down boot.
  937. */
  938. if (wait)
  939. synchronize_rcu();
  940. /*
  941. * Just being safe to make sure after previous flush if some body did
  942. * update limits through cgroup and another work got queued, cancel
  943. * it.
  944. */
  945. throtl_shutdown_wq(q);
  946. kfree(q->td);
  947. }
  948. static int __init throtl_init(void)
  949. {
  950. kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
  951. if (!kthrotld_workqueue)
  952. panic("Failed to create kthrotld\n");
  953. blkio_policy_register(&blkio_policy_throtl);
  954. return 0;
  955. }
  956. module_init(throtl_init);