blk-throttle.c 24 KB

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