blk-throttle.c 29 KB

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