blk-throttle.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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. /* Max dispatch from a group in 1 round */
  13. static int throtl_grp_quantum = 8;
  14. /* Total max dispatch from all groups in one round */
  15. static int throtl_quantum = 32;
  16. /* Throttling is performed over 100ms slice and after that slice is renewed */
  17. static unsigned long throtl_slice = HZ/10; /* 100 ms */
  18. struct throtl_rb_root {
  19. struct rb_root rb;
  20. struct rb_node *left;
  21. unsigned int count;
  22. unsigned long min_disptime;
  23. };
  24. #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
  25. .count = 0, .min_disptime = 0}
  26. #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
  27. struct throtl_grp {
  28. /* List of throtl groups on the request queue*/
  29. struct hlist_node tg_node;
  30. /* active throtl group service_tree member */
  31. struct rb_node rb_node;
  32. /*
  33. * Dispatch time in jiffies. This is the estimated time when group
  34. * will unthrottle and is ready to dispatch more bio. It is used as
  35. * key to sort active groups in service tree.
  36. */
  37. unsigned long disptime;
  38. struct blkio_group blkg;
  39. atomic_t ref;
  40. unsigned int flags;
  41. /* Two lists for READ and WRITE */
  42. struct bio_list bio_lists[2];
  43. /* Number of queued bios on READ and WRITE lists */
  44. unsigned int nr_queued[2];
  45. /* bytes per second rate limits */
  46. uint64_t bps[2];
  47. /* Number of bytes disptached in current slice */
  48. uint64_t bytes_disp[2];
  49. /* When did we start a new slice */
  50. unsigned long slice_start[2];
  51. unsigned long slice_end[2];
  52. };
  53. struct throtl_data
  54. {
  55. /* List of throtl groups */
  56. struct hlist_head tg_list;
  57. /* service tree for active throtl groups */
  58. struct throtl_rb_root tg_service_tree;
  59. struct throtl_grp root_tg;
  60. struct request_queue *queue;
  61. /* Total Number of queued bios on READ and WRITE lists */
  62. unsigned int nr_queued[2];
  63. /*
  64. * number of total undestroyed groups (excluding root group)
  65. */
  66. unsigned int nr_undestroyed_grps;
  67. /* Work for dispatching throttled bios */
  68. struct delayed_work throtl_work;
  69. };
  70. enum tg_state_flags {
  71. THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
  72. };
  73. #define THROTL_TG_FNS(name) \
  74. static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
  75. { \
  76. (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
  77. } \
  78. static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
  79. { \
  80. (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
  81. } \
  82. static inline int throtl_tg_##name(const struct throtl_grp *tg) \
  83. { \
  84. return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
  85. }
  86. THROTL_TG_FNS(on_rr);
  87. #define throtl_log_tg(td, tg, fmt, args...) \
  88. blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
  89. blkg_path(&(tg)->blkg), ##args); \
  90. #define throtl_log(td, fmt, args...) \
  91. blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
  92. static inline struct throtl_grp *tg_of_blkg(struct blkio_group *blkg)
  93. {
  94. if (blkg)
  95. return container_of(blkg, struct throtl_grp, blkg);
  96. return NULL;
  97. }
  98. static inline int total_nr_queued(struct throtl_data *td)
  99. {
  100. return (td->nr_queued[0] + td->nr_queued[1]);
  101. }
  102. static inline struct throtl_grp *throtl_ref_get_tg(struct throtl_grp *tg)
  103. {
  104. atomic_inc(&tg->ref);
  105. return tg;
  106. }
  107. static void throtl_put_tg(struct throtl_grp *tg)
  108. {
  109. BUG_ON(atomic_read(&tg->ref) <= 0);
  110. if (!atomic_dec_and_test(&tg->ref))
  111. return;
  112. kfree(tg);
  113. }
  114. static struct throtl_grp * throtl_find_alloc_tg(struct throtl_data *td,
  115. struct cgroup *cgroup)
  116. {
  117. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  118. struct throtl_grp *tg = NULL;
  119. void *key = td;
  120. struct backing_dev_info *bdi = &td->queue->backing_dev_info;
  121. unsigned int major, minor;
  122. /*
  123. * TODO: Speed up blkiocg_lookup_group() by maintaining a radix
  124. * tree of blkg (instead of traversing through hash list all
  125. * the time.
  126. */
  127. tg = tg_of_blkg(blkiocg_lookup_group(blkcg, key));
  128. /* Fill in device details for root group */
  129. if (tg && !tg->blkg.dev && bdi->dev && dev_name(bdi->dev)) {
  130. sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
  131. tg->blkg.dev = MKDEV(major, minor);
  132. goto done;
  133. }
  134. if (tg)
  135. goto done;
  136. tg = kzalloc_node(sizeof(*tg), GFP_ATOMIC, td->queue->node);
  137. if (!tg)
  138. goto done;
  139. INIT_HLIST_NODE(&tg->tg_node);
  140. RB_CLEAR_NODE(&tg->rb_node);
  141. bio_list_init(&tg->bio_lists[0]);
  142. bio_list_init(&tg->bio_lists[1]);
  143. /*
  144. * Take the initial reference that will be released on destroy
  145. * This can be thought of a joint reference by cgroup and
  146. * request queue which will be dropped by either request queue
  147. * exit or cgroup deletion path depending on who is exiting first.
  148. */
  149. atomic_set(&tg->ref, 1);
  150. /* Add group onto cgroup list */
  151. sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
  152. blkiocg_add_blkio_group(blkcg, &tg->blkg, (void *)td,
  153. MKDEV(major, minor), BLKIO_POLICY_THROTL);
  154. tg->bps[READ] = blkcg_get_read_bps(blkcg, tg->blkg.dev);
  155. tg->bps[WRITE] = blkcg_get_write_bps(blkcg, tg->blkg.dev);
  156. hlist_add_head(&tg->tg_node, &td->tg_list);
  157. td->nr_undestroyed_grps++;
  158. done:
  159. return tg;
  160. }
  161. static struct throtl_grp * throtl_get_tg(struct throtl_data *td)
  162. {
  163. struct cgroup *cgroup;
  164. struct throtl_grp *tg = NULL;
  165. rcu_read_lock();
  166. cgroup = task_cgroup(current, blkio_subsys_id);
  167. tg = throtl_find_alloc_tg(td, cgroup);
  168. if (!tg)
  169. tg = &td->root_tg;
  170. rcu_read_unlock();
  171. return tg;
  172. }
  173. static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
  174. {
  175. /* Service tree is empty */
  176. if (!root->count)
  177. return NULL;
  178. if (!root->left)
  179. root->left = rb_first(&root->rb);
  180. if (root->left)
  181. return rb_entry_tg(root->left);
  182. return NULL;
  183. }
  184. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  185. {
  186. rb_erase(n, root);
  187. RB_CLEAR_NODE(n);
  188. }
  189. static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
  190. {
  191. if (root->left == n)
  192. root->left = NULL;
  193. rb_erase_init(n, &root->rb);
  194. --root->count;
  195. }
  196. static void update_min_dispatch_time(struct throtl_rb_root *st)
  197. {
  198. struct throtl_grp *tg;
  199. tg = throtl_rb_first(st);
  200. if (!tg)
  201. return;
  202. st->min_disptime = tg->disptime;
  203. }
  204. static void
  205. tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
  206. {
  207. struct rb_node **node = &st->rb.rb_node;
  208. struct rb_node *parent = NULL;
  209. struct throtl_grp *__tg;
  210. unsigned long key = tg->disptime;
  211. int left = 1;
  212. while (*node != NULL) {
  213. parent = *node;
  214. __tg = rb_entry_tg(parent);
  215. if (time_before(key, __tg->disptime))
  216. node = &parent->rb_left;
  217. else {
  218. node = &parent->rb_right;
  219. left = 0;
  220. }
  221. }
  222. if (left)
  223. st->left = &tg->rb_node;
  224. rb_link_node(&tg->rb_node, parent, node);
  225. rb_insert_color(&tg->rb_node, &st->rb);
  226. }
  227. static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  228. {
  229. struct throtl_rb_root *st = &td->tg_service_tree;
  230. tg_service_tree_add(st, tg);
  231. throtl_mark_tg_on_rr(tg);
  232. st->count++;
  233. }
  234. static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  235. {
  236. if (!throtl_tg_on_rr(tg))
  237. __throtl_enqueue_tg(td, tg);
  238. }
  239. static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  240. {
  241. throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
  242. throtl_clear_tg_on_rr(tg);
  243. }
  244. static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  245. {
  246. if (throtl_tg_on_rr(tg))
  247. __throtl_dequeue_tg(td, tg);
  248. }
  249. static void throtl_schedule_next_dispatch(struct throtl_data *td)
  250. {
  251. struct throtl_rb_root *st = &td->tg_service_tree;
  252. /*
  253. * If there are more bios pending, schedule more work.
  254. */
  255. if (!total_nr_queued(td))
  256. return;
  257. BUG_ON(!st->count);
  258. update_min_dispatch_time(st);
  259. if (time_before_eq(st->min_disptime, jiffies))
  260. throtl_schedule_delayed_work(td->queue, 0);
  261. else
  262. throtl_schedule_delayed_work(td->queue,
  263. (st->min_disptime - jiffies));
  264. }
  265. static inline void
  266. throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  267. {
  268. tg->bytes_disp[rw] = 0;
  269. tg->slice_start[rw] = jiffies;
  270. tg->slice_end[rw] = jiffies + throtl_slice;
  271. throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
  272. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  273. tg->slice_end[rw], jiffies);
  274. }
  275. static inline void throtl_extend_slice(struct throtl_data *td,
  276. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  277. {
  278. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  279. throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
  280. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  281. tg->slice_end[rw], jiffies);
  282. }
  283. /* Determine if previously allocated or extended slice is complete or not */
  284. static bool
  285. throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  286. {
  287. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  288. return 0;
  289. return 1;
  290. }
  291. /* Trim the used slices and adjust slice start accordingly */
  292. static inline void
  293. throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  294. {
  295. unsigned long nr_slices, bytes_trim, time_elapsed;
  296. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  297. /*
  298. * If bps are unlimited (-1), then time slice don't get
  299. * renewed. Don't try to trim the slice if slice is used. A new
  300. * slice will start when appropriate.
  301. */
  302. if (throtl_slice_used(td, tg, rw))
  303. return;
  304. time_elapsed = jiffies - tg->slice_start[rw];
  305. nr_slices = time_elapsed / throtl_slice;
  306. if (!nr_slices)
  307. return;
  308. bytes_trim = (tg->bps[rw] * throtl_slice * nr_slices)/HZ;
  309. if (!bytes_trim)
  310. return;
  311. if (tg->bytes_disp[rw] >= bytes_trim)
  312. tg->bytes_disp[rw] -= bytes_trim;
  313. else
  314. tg->bytes_disp[rw] = 0;
  315. tg->slice_start[rw] += nr_slices * throtl_slice;
  316. throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%lu"
  317. " start=%lu end=%lu jiffies=%lu",
  318. rw == READ ? 'R' : 'W', nr_slices, bytes_trim,
  319. tg->slice_start[rw], tg->slice_end[rw], jiffies);
  320. }
  321. /*
  322. * Returns whether one can dispatch a bio or not. Also returns approx number
  323. * of jiffies to wait before this bio is with-in IO rate and can be dispatched
  324. */
  325. static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
  326. struct bio *bio, unsigned long *wait)
  327. {
  328. bool rw = bio_data_dir(bio);
  329. u64 bytes_allowed, extra_bytes;
  330. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  331. /*
  332. * Currently whole state machine of group depends on first bio
  333. * queued in the group bio list. So one should not be calling
  334. * this function with a different bio if there are other bios
  335. * queued.
  336. */
  337. BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
  338. /* If tg->bps = -1, then BW is unlimited */
  339. if (tg->bps[rw] == -1) {
  340. if (wait)
  341. *wait = 0;
  342. return 1;
  343. }
  344. /*
  345. * If previous slice expired, start a new one otherwise renew/extend
  346. * existing slice to make sure it is at least throtl_slice interval
  347. * long since now.
  348. */
  349. if (throtl_slice_used(td, tg, rw))
  350. throtl_start_new_slice(td, tg, rw);
  351. else {
  352. if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
  353. throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
  354. }
  355. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  356. /* Slice has just started. Consider one slice interval */
  357. if (!jiffy_elapsed)
  358. jiffy_elapsed_rnd = throtl_slice;
  359. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  360. bytes_allowed = (tg->bps[rw] * jiffies_to_msecs(jiffy_elapsed_rnd))
  361. / MSEC_PER_SEC;
  362. if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
  363. if (wait)
  364. *wait = 0;
  365. return 1;
  366. }
  367. /* Calc approx time to dispatch */
  368. extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
  369. jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
  370. if (!jiffy_wait)
  371. jiffy_wait = 1;
  372. /*
  373. * This wait time is without taking into consideration the rounding
  374. * up we did. Add that time also.
  375. */
  376. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  377. if (wait)
  378. *wait = jiffy_wait;
  379. if (time_before(tg->slice_end[rw], jiffies + jiffy_wait))
  380. throtl_extend_slice(td, tg, rw, jiffies + jiffy_wait);
  381. return 0;
  382. }
  383. static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
  384. {
  385. bool rw = bio_data_dir(bio);
  386. bool sync = bio->bi_rw & REQ_SYNC;
  387. /* Charge the bio to the group */
  388. tg->bytes_disp[rw] += bio->bi_size;
  389. /*
  390. * TODO: This will take blkg->stats_lock. Figure out a way
  391. * to avoid this cost.
  392. */
  393. blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size, rw, sync);
  394. }
  395. static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
  396. struct bio *bio)
  397. {
  398. bool rw = bio_data_dir(bio);
  399. bio_list_add(&tg->bio_lists[rw], bio);
  400. /* Take a bio reference on tg */
  401. throtl_ref_get_tg(tg);
  402. tg->nr_queued[rw]++;
  403. td->nr_queued[rw]++;
  404. throtl_enqueue_tg(td, tg);
  405. }
  406. static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
  407. {
  408. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  409. struct bio *bio;
  410. if ((bio = bio_list_peek(&tg->bio_lists[READ])))
  411. tg_may_dispatch(td, tg, bio, &read_wait);
  412. if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
  413. tg_may_dispatch(td, tg, bio, &write_wait);
  414. min_wait = min(read_wait, write_wait);
  415. disptime = jiffies + min_wait;
  416. /*
  417. * If group is already on active tree, then update dispatch time
  418. * only if it is lesser than existing dispatch time. Otherwise
  419. * always update the dispatch time
  420. */
  421. if (throtl_tg_on_rr(tg) && time_before(disptime, tg->disptime))
  422. return;
  423. /* Update dispatch time */
  424. throtl_dequeue_tg(td, tg);
  425. tg->disptime = disptime;
  426. throtl_enqueue_tg(td, tg);
  427. }
  428. static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
  429. bool rw, struct bio_list *bl)
  430. {
  431. struct bio *bio;
  432. bio = bio_list_pop(&tg->bio_lists[rw]);
  433. tg->nr_queued[rw]--;
  434. /* Drop bio reference on tg */
  435. throtl_put_tg(tg);
  436. BUG_ON(td->nr_queued[rw] <= 0);
  437. td->nr_queued[rw]--;
  438. throtl_charge_bio(tg, bio);
  439. bio_list_add(bl, bio);
  440. bio->bi_rw |= REQ_THROTTLED;
  441. throtl_trim_slice(td, tg, rw);
  442. }
  443. static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
  444. struct bio_list *bl)
  445. {
  446. unsigned int nr_reads = 0, nr_writes = 0;
  447. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  448. unsigned int max_nr_writes = throtl_grp_quantum - nr_reads;
  449. struct bio *bio;
  450. /* Try to dispatch 75% READS and 25% WRITES */
  451. while ((bio = bio_list_peek(&tg->bio_lists[READ]))
  452. && tg_may_dispatch(td, tg, bio, NULL)) {
  453. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  454. nr_reads++;
  455. if (nr_reads >= max_nr_reads)
  456. break;
  457. }
  458. while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
  459. && tg_may_dispatch(td, tg, bio, NULL)) {
  460. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  461. nr_writes++;
  462. if (nr_writes >= max_nr_writes)
  463. break;
  464. }
  465. return nr_reads + nr_writes;
  466. }
  467. static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
  468. {
  469. unsigned int nr_disp = 0;
  470. struct throtl_grp *tg;
  471. struct throtl_rb_root *st = &td->tg_service_tree;
  472. while (1) {
  473. tg = throtl_rb_first(st);
  474. if (!tg)
  475. break;
  476. if (time_before(jiffies, tg->disptime))
  477. break;
  478. throtl_dequeue_tg(td, tg);
  479. nr_disp += throtl_dispatch_tg(td, tg, bl);
  480. if (tg->nr_queued[0] || tg->nr_queued[1]) {
  481. tg_update_disptime(td, tg);
  482. throtl_enqueue_tg(td, tg);
  483. }
  484. if (nr_disp >= throtl_quantum)
  485. break;
  486. }
  487. return nr_disp;
  488. }
  489. /* Dispatch throttled bios. Should be called without queue lock held. */
  490. static int throtl_dispatch(struct request_queue *q)
  491. {
  492. struct throtl_data *td = q->td;
  493. unsigned int nr_disp = 0;
  494. struct bio_list bio_list_on_stack;
  495. struct bio *bio;
  496. spin_lock_irq(q->queue_lock);
  497. if (!total_nr_queued(td))
  498. goto out;
  499. bio_list_init(&bio_list_on_stack);
  500. throtl_log(td, "dispatch nr_queued=%lu read=%u write=%u",
  501. total_nr_queued(td), td->nr_queued[READ],
  502. td->nr_queued[WRITE]);
  503. nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
  504. if (nr_disp)
  505. throtl_log(td, "bios disp=%u", nr_disp);
  506. throtl_schedule_next_dispatch(td);
  507. out:
  508. spin_unlock_irq(q->queue_lock);
  509. /*
  510. * If we dispatched some requests, unplug the queue to make sure
  511. * immediate dispatch
  512. */
  513. if (nr_disp) {
  514. while((bio = bio_list_pop(&bio_list_on_stack)))
  515. generic_make_request(bio);
  516. blk_unplug(q);
  517. }
  518. return nr_disp;
  519. }
  520. void blk_throtl_work(struct work_struct *work)
  521. {
  522. struct throtl_data *td = container_of(work, struct throtl_data,
  523. throtl_work.work);
  524. struct request_queue *q = td->queue;
  525. throtl_dispatch(q);
  526. }
  527. /* Call with queue lock held */
  528. void throtl_schedule_delayed_work(struct request_queue *q, unsigned long delay)
  529. {
  530. struct throtl_data *td = q->td;
  531. struct delayed_work *dwork = &td->throtl_work;
  532. if (total_nr_queued(td) > 0) {
  533. /*
  534. * We might have a work scheduled to be executed in future.
  535. * Cancel that and schedule a new one.
  536. */
  537. __cancel_delayed_work(dwork);
  538. kblockd_schedule_delayed_work(q, dwork, delay);
  539. throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
  540. delay, jiffies);
  541. }
  542. }
  543. EXPORT_SYMBOL(throtl_schedule_delayed_work);
  544. static void
  545. throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
  546. {
  547. /* Something wrong if we are trying to remove same group twice */
  548. BUG_ON(hlist_unhashed(&tg->tg_node));
  549. hlist_del_init(&tg->tg_node);
  550. /*
  551. * Put the reference taken at the time of creation so that when all
  552. * queues are gone, group can be destroyed.
  553. */
  554. throtl_put_tg(tg);
  555. td->nr_undestroyed_grps--;
  556. }
  557. static void throtl_release_tgs(struct throtl_data *td)
  558. {
  559. struct hlist_node *pos, *n;
  560. struct throtl_grp *tg;
  561. hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
  562. /*
  563. * If cgroup removal path got to blk_group first and removed
  564. * it from cgroup list, then it will take care of destroying
  565. * cfqg also.
  566. */
  567. if (!blkiocg_del_blkio_group(&tg->blkg))
  568. throtl_destroy_tg(td, tg);
  569. }
  570. }
  571. static void throtl_td_free(struct throtl_data *td)
  572. {
  573. kfree(td);
  574. }
  575. /*
  576. * Blk cgroup controller notification saying that blkio_group object is being
  577. * delinked as associated cgroup object is going away. That also means that
  578. * no new IO will come in this group. So get rid of this group as soon as
  579. * any pending IO in the group is finished.
  580. *
  581. * This function is called under rcu_read_lock(). key is the rcu protected
  582. * pointer. That means "key" is a valid throtl_data pointer as long as we are
  583. * rcu read lock.
  584. *
  585. * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
  586. * it should not be NULL as even if queue was going away, cgroup deltion
  587. * path got to it first.
  588. */
  589. void throtl_unlink_blkio_group(void *key, struct blkio_group *blkg)
  590. {
  591. unsigned long flags;
  592. struct throtl_data *td = key;
  593. spin_lock_irqsave(td->queue->queue_lock, flags);
  594. throtl_destroy_tg(td, tg_of_blkg(blkg));
  595. spin_unlock_irqrestore(td->queue->queue_lock, flags);
  596. }
  597. static void throtl_update_blkio_group_read_bps (struct blkio_group *blkg,
  598. u64 read_bps)
  599. {
  600. tg_of_blkg(blkg)->bps[READ] = read_bps;
  601. }
  602. static void throtl_update_blkio_group_write_bps (struct blkio_group *blkg,
  603. u64 write_bps)
  604. {
  605. tg_of_blkg(blkg)->bps[WRITE] = write_bps;
  606. }
  607. void throtl_shutdown_timer_wq(struct request_queue *q)
  608. {
  609. struct throtl_data *td = q->td;
  610. cancel_delayed_work_sync(&td->throtl_work);
  611. }
  612. static struct blkio_policy_type blkio_policy_throtl = {
  613. .ops = {
  614. .blkio_unlink_group_fn = throtl_unlink_blkio_group,
  615. .blkio_update_group_read_bps_fn =
  616. throtl_update_blkio_group_read_bps,
  617. .blkio_update_group_write_bps_fn =
  618. throtl_update_blkio_group_write_bps,
  619. },
  620. };
  621. int blk_throtl_bio(struct request_queue *q, struct bio **biop)
  622. {
  623. struct throtl_data *td = q->td;
  624. struct throtl_grp *tg;
  625. struct bio *bio = *biop;
  626. bool rw = bio_data_dir(bio), update_disptime = true;
  627. if (bio->bi_rw & REQ_THROTTLED) {
  628. bio->bi_rw &= ~REQ_THROTTLED;
  629. return 0;
  630. }
  631. spin_lock_irq(q->queue_lock);
  632. tg = throtl_get_tg(td);
  633. if (tg->nr_queued[rw]) {
  634. /*
  635. * There is already another bio queued in same dir. No
  636. * need to update dispatch time.
  637. */
  638. update_disptime = false;
  639. goto queue_bio;
  640. }
  641. /* Bio is with-in rate limit of group */
  642. if (tg_may_dispatch(td, tg, bio, NULL)) {
  643. throtl_charge_bio(tg, bio);
  644. goto out;
  645. }
  646. queue_bio:
  647. throtl_log_tg(td, tg, "[%c] bio. disp=%u sz=%u bps=%llu"
  648. " queued=%d/%d", rw == READ ? 'R' : 'W',
  649. tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
  650. tg->nr_queued[READ], tg->nr_queued[WRITE]);
  651. throtl_add_bio_tg(q->td, tg, bio);
  652. *biop = NULL;
  653. if (update_disptime) {
  654. tg_update_disptime(td, tg);
  655. throtl_schedule_next_dispatch(td);
  656. }
  657. out:
  658. spin_unlock_irq(q->queue_lock);
  659. return 0;
  660. }
  661. int blk_throtl_init(struct request_queue *q)
  662. {
  663. struct throtl_data *td;
  664. struct throtl_grp *tg;
  665. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  666. if (!td)
  667. return -ENOMEM;
  668. INIT_HLIST_HEAD(&td->tg_list);
  669. td->tg_service_tree = THROTL_RB_ROOT;
  670. /* Init root group */
  671. tg = &td->root_tg;
  672. INIT_HLIST_NODE(&tg->tg_node);
  673. RB_CLEAR_NODE(&tg->rb_node);
  674. bio_list_init(&tg->bio_lists[0]);
  675. bio_list_init(&tg->bio_lists[1]);
  676. /* Practically unlimited BW */
  677. tg->bps[0] = tg->bps[1] = -1;
  678. atomic_set(&tg->ref, 1);
  679. INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
  680. rcu_read_lock();
  681. blkiocg_add_blkio_group(&blkio_root_cgroup, &tg->blkg, (void *)td,
  682. 0, BLKIO_POLICY_THROTL);
  683. rcu_read_unlock();
  684. /* Attach throtl data to request queue */
  685. td->queue = q;
  686. q->td = td;
  687. return 0;
  688. }
  689. void blk_throtl_exit(struct request_queue *q)
  690. {
  691. struct throtl_data *td = q->td;
  692. bool wait = false;
  693. BUG_ON(!td);
  694. throtl_shutdown_timer_wq(q);
  695. spin_lock_irq(q->queue_lock);
  696. throtl_release_tgs(td);
  697. blkiocg_del_blkio_group(&td->root_tg.blkg);
  698. /* If there are other groups */
  699. if (td->nr_undestroyed_grps >= 1)
  700. wait = true;
  701. spin_unlock_irq(q->queue_lock);
  702. /*
  703. * Wait for tg->blkg->key accessors to exit their grace periods.
  704. * Do this wait only if there are other undestroyed groups out
  705. * there (other than root group). This can happen if cgroup deletion
  706. * path claimed the responsibility of cleaning up a group before
  707. * queue cleanup code get to the group.
  708. *
  709. * Do not call synchronize_rcu() unconditionally as there are drivers
  710. * which create/delete request queue hundreds of times during scan/boot
  711. * and synchronize_rcu() can take significant time and slow down boot.
  712. */
  713. if (wait)
  714. synchronize_rcu();
  715. throtl_td_free(td);
  716. }
  717. static int __init throtl_init(void)
  718. {
  719. blkio_policy_register(&blkio_policy_throtl);
  720. return 0;
  721. }
  722. module_init(throtl_init);