blk-throttle.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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. atomic_t 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. /*
  162. * Take the initial reference that will be released on destroy
  163. * This can be thought of a joint reference by cgroup and
  164. * request queue which will be dropped by either request queue
  165. * exit or cgroup deletion path depending on who is exiting first.
  166. */
  167. atomic_set(&tg->ref, 1);
  168. /* Add group onto cgroup list */
  169. sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
  170. blkiocg_add_blkio_group(blkcg, &tg->blkg, (void *)td,
  171. MKDEV(major, minor), BLKIO_POLICY_THROTL);
  172. tg->bps[READ] = blkcg_get_read_bps(blkcg, tg->blkg.dev);
  173. tg->bps[WRITE] = blkcg_get_write_bps(blkcg, tg->blkg.dev);
  174. tg->iops[READ] = blkcg_get_read_iops(blkcg, tg->blkg.dev);
  175. tg->iops[WRITE] = blkcg_get_write_iops(blkcg, tg->blkg.dev);
  176. hlist_add_head(&tg->tg_node, &td->tg_list);
  177. td->nr_undestroyed_grps++;
  178. done:
  179. return tg;
  180. }
  181. static struct throtl_grp * throtl_get_tg(struct throtl_data *td)
  182. {
  183. struct cgroup *cgroup;
  184. struct throtl_grp *tg = NULL;
  185. rcu_read_lock();
  186. cgroup = task_cgroup(current, blkio_subsys_id);
  187. tg = throtl_find_alloc_tg(td, cgroup);
  188. if (!tg)
  189. tg = &td->root_tg;
  190. rcu_read_unlock();
  191. return tg;
  192. }
  193. static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
  194. {
  195. /* Service tree is empty */
  196. if (!root->count)
  197. return NULL;
  198. if (!root->left)
  199. root->left = rb_first(&root->rb);
  200. if (root->left)
  201. return rb_entry_tg(root->left);
  202. return NULL;
  203. }
  204. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  205. {
  206. rb_erase(n, root);
  207. RB_CLEAR_NODE(n);
  208. }
  209. static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
  210. {
  211. if (root->left == n)
  212. root->left = NULL;
  213. rb_erase_init(n, &root->rb);
  214. --root->count;
  215. }
  216. static void update_min_dispatch_time(struct throtl_rb_root *st)
  217. {
  218. struct throtl_grp *tg;
  219. tg = throtl_rb_first(st);
  220. if (!tg)
  221. return;
  222. st->min_disptime = tg->disptime;
  223. }
  224. static void
  225. tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
  226. {
  227. struct rb_node **node = &st->rb.rb_node;
  228. struct rb_node *parent = NULL;
  229. struct throtl_grp *__tg;
  230. unsigned long key = tg->disptime;
  231. int left = 1;
  232. while (*node != NULL) {
  233. parent = *node;
  234. __tg = rb_entry_tg(parent);
  235. if (time_before(key, __tg->disptime))
  236. node = &parent->rb_left;
  237. else {
  238. node = &parent->rb_right;
  239. left = 0;
  240. }
  241. }
  242. if (left)
  243. st->left = &tg->rb_node;
  244. rb_link_node(&tg->rb_node, parent, node);
  245. rb_insert_color(&tg->rb_node, &st->rb);
  246. }
  247. static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  248. {
  249. struct throtl_rb_root *st = &td->tg_service_tree;
  250. tg_service_tree_add(st, tg);
  251. throtl_mark_tg_on_rr(tg);
  252. st->count++;
  253. }
  254. static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  255. {
  256. if (!throtl_tg_on_rr(tg))
  257. __throtl_enqueue_tg(td, tg);
  258. }
  259. static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  260. {
  261. throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
  262. throtl_clear_tg_on_rr(tg);
  263. }
  264. static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  265. {
  266. if (throtl_tg_on_rr(tg))
  267. __throtl_dequeue_tg(td, tg);
  268. }
  269. static void throtl_schedule_next_dispatch(struct throtl_data *td)
  270. {
  271. struct throtl_rb_root *st = &td->tg_service_tree;
  272. /*
  273. * If there are more bios pending, schedule more work.
  274. */
  275. if (!total_nr_queued(td))
  276. return;
  277. BUG_ON(!st->count);
  278. update_min_dispatch_time(st);
  279. if (time_before_eq(st->min_disptime, jiffies))
  280. throtl_schedule_delayed_work(td, 0);
  281. else
  282. throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
  283. }
  284. static inline void
  285. throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  286. {
  287. tg->bytes_disp[rw] = 0;
  288. tg->io_disp[rw] = 0;
  289. tg->slice_start[rw] = jiffies;
  290. tg->slice_end[rw] = jiffies + throtl_slice;
  291. throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
  292. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  293. tg->slice_end[rw], jiffies);
  294. }
  295. static inline void throtl_set_slice_end(struct throtl_data *td,
  296. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  297. {
  298. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  299. }
  300. static inline void throtl_extend_slice(struct throtl_data *td,
  301. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  302. {
  303. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  304. throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
  305. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  306. tg->slice_end[rw], jiffies);
  307. }
  308. /* Determine if previously allocated or extended slice is complete or not */
  309. static bool
  310. throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  311. {
  312. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  313. return 0;
  314. return 1;
  315. }
  316. /* Trim the used slices and adjust slice start accordingly */
  317. static inline void
  318. throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  319. {
  320. unsigned long nr_slices, time_elapsed, io_trim;
  321. u64 bytes_trim, tmp;
  322. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  323. /*
  324. * If bps are unlimited (-1), then time slice don't get
  325. * renewed. Don't try to trim the slice if slice is used. A new
  326. * slice will start when appropriate.
  327. */
  328. if (throtl_slice_used(td, tg, rw))
  329. return;
  330. /*
  331. * A bio has been dispatched. Also adjust slice_end. It might happen
  332. * that initially cgroup limit was very low resulting in high
  333. * slice_end, but later limit was bumped up and bio was dispached
  334. * sooner, then we need to reduce slice_end. A high bogus slice_end
  335. * is bad because it does not allow new slice to start.
  336. */
  337. throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
  338. time_elapsed = jiffies - tg->slice_start[rw];
  339. nr_slices = time_elapsed / throtl_slice;
  340. if (!nr_slices)
  341. return;
  342. tmp = tg->bps[rw] * throtl_slice * nr_slices;
  343. do_div(tmp, HZ);
  344. bytes_trim = tmp;
  345. io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
  346. if (!bytes_trim && !io_trim)
  347. return;
  348. if (tg->bytes_disp[rw] >= bytes_trim)
  349. tg->bytes_disp[rw] -= bytes_trim;
  350. else
  351. tg->bytes_disp[rw] = 0;
  352. if (tg->io_disp[rw] >= io_trim)
  353. tg->io_disp[rw] -= io_trim;
  354. else
  355. tg->io_disp[rw] = 0;
  356. tg->slice_start[rw] += nr_slices * throtl_slice;
  357. throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
  358. " start=%lu end=%lu jiffies=%lu",
  359. rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
  360. tg->slice_start[rw], tg->slice_end[rw], jiffies);
  361. }
  362. static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
  363. struct bio *bio, unsigned long *wait)
  364. {
  365. bool rw = bio_data_dir(bio);
  366. unsigned int io_allowed;
  367. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  368. u64 tmp;
  369. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  370. /* Slice has just started. Consider one slice interval */
  371. if (!jiffy_elapsed)
  372. jiffy_elapsed_rnd = throtl_slice;
  373. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  374. /*
  375. * jiffy_elapsed_rnd should not be a big value as minimum iops can be
  376. * 1 then at max jiffy elapsed should be equivalent of 1 second as we
  377. * will allow dispatch after 1 second and after that slice should
  378. * have been trimmed.
  379. */
  380. tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
  381. do_div(tmp, HZ);
  382. if (tmp > UINT_MAX)
  383. io_allowed = UINT_MAX;
  384. else
  385. io_allowed = tmp;
  386. if (tg->io_disp[rw] + 1 <= io_allowed) {
  387. if (wait)
  388. *wait = 0;
  389. return 1;
  390. }
  391. /* Calc approx time to dispatch */
  392. jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
  393. if (jiffy_wait > jiffy_elapsed)
  394. jiffy_wait = jiffy_wait - jiffy_elapsed;
  395. else
  396. jiffy_wait = 1;
  397. if (wait)
  398. *wait = jiffy_wait;
  399. return 0;
  400. }
  401. static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
  402. struct bio *bio, unsigned long *wait)
  403. {
  404. bool rw = bio_data_dir(bio);
  405. u64 bytes_allowed, extra_bytes, tmp;
  406. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  407. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  408. /* Slice has just started. Consider one slice interval */
  409. if (!jiffy_elapsed)
  410. jiffy_elapsed_rnd = throtl_slice;
  411. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  412. tmp = tg->bps[rw] * jiffy_elapsed_rnd;
  413. do_div(tmp, HZ);
  414. bytes_allowed = tmp;
  415. if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
  416. if (wait)
  417. *wait = 0;
  418. return 1;
  419. }
  420. /* Calc approx time to dispatch */
  421. extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
  422. jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
  423. if (!jiffy_wait)
  424. jiffy_wait = 1;
  425. /*
  426. * This wait time is without taking into consideration the rounding
  427. * up we did. Add that time also.
  428. */
  429. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  430. if (wait)
  431. *wait = jiffy_wait;
  432. return 0;
  433. }
  434. /*
  435. * Returns whether one can dispatch a bio or not. Also returns approx number
  436. * of jiffies to wait before this bio is with-in IO rate and can be dispatched
  437. */
  438. static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
  439. struct bio *bio, unsigned long *wait)
  440. {
  441. bool rw = bio_data_dir(bio);
  442. unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
  443. /*
  444. * Currently whole state machine of group depends on first bio
  445. * queued in the group bio list. So one should not be calling
  446. * this function with a different bio if there are other bios
  447. * queued.
  448. */
  449. BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
  450. /* If tg->bps = -1, then BW is unlimited */
  451. if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
  452. if (wait)
  453. *wait = 0;
  454. return 1;
  455. }
  456. /*
  457. * If previous slice expired, start a new one otherwise renew/extend
  458. * existing slice to make sure it is at least throtl_slice interval
  459. * long since now.
  460. */
  461. if (throtl_slice_used(td, tg, rw))
  462. throtl_start_new_slice(td, tg, rw);
  463. else {
  464. if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
  465. throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
  466. }
  467. if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
  468. && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
  469. if (wait)
  470. *wait = 0;
  471. return 1;
  472. }
  473. max_wait = max(bps_wait, iops_wait);
  474. if (wait)
  475. *wait = max_wait;
  476. if (time_before(tg->slice_end[rw], jiffies + max_wait))
  477. throtl_extend_slice(td, tg, rw, jiffies + max_wait);
  478. return 0;
  479. }
  480. static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
  481. {
  482. bool rw = bio_data_dir(bio);
  483. bool sync = bio->bi_rw & REQ_SYNC;
  484. /* Charge the bio to the group */
  485. tg->bytes_disp[rw] += bio->bi_size;
  486. tg->io_disp[rw]++;
  487. /*
  488. * TODO: This will take blkg->stats_lock. Figure out a way
  489. * to avoid this cost.
  490. */
  491. blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size, rw, sync);
  492. }
  493. static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
  494. struct bio *bio)
  495. {
  496. bool rw = bio_data_dir(bio);
  497. bio_list_add(&tg->bio_lists[rw], bio);
  498. /* Take a bio reference on tg */
  499. throtl_ref_get_tg(tg);
  500. tg->nr_queued[rw]++;
  501. td->nr_queued[rw]++;
  502. throtl_enqueue_tg(td, tg);
  503. }
  504. static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
  505. {
  506. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  507. struct bio *bio;
  508. if ((bio = bio_list_peek(&tg->bio_lists[READ])))
  509. tg_may_dispatch(td, tg, bio, &read_wait);
  510. if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
  511. tg_may_dispatch(td, tg, bio, &write_wait);
  512. min_wait = min(read_wait, write_wait);
  513. disptime = jiffies + min_wait;
  514. /* Update dispatch time */
  515. throtl_dequeue_tg(td, tg);
  516. tg->disptime = disptime;
  517. throtl_enqueue_tg(td, tg);
  518. }
  519. static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
  520. bool rw, struct bio_list *bl)
  521. {
  522. struct bio *bio;
  523. bio = bio_list_pop(&tg->bio_lists[rw]);
  524. tg->nr_queued[rw]--;
  525. /* Drop bio reference on tg */
  526. throtl_put_tg(tg);
  527. BUG_ON(td->nr_queued[rw] <= 0);
  528. td->nr_queued[rw]--;
  529. throtl_charge_bio(tg, bio);
  530. bio_list_add(bl, bio);
  531. bio->bi_rw |= REQ_THROTTLED;
  532. throtl_trim_slice(td, tg, rw);
  533. }
  534. static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
  535. struct bio_list *bl)
  536. {
  537. unsigned int nr_reads = 0, nr_writes = 0;
  538. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  539. unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
  540. struct bio *bio;
  541. /* Try to dispatch 75% READS and 25% WRITES */
  542. while ((bio = bio_list_peek(&tg->bio_lists[READ]))
  543. && tg_may_dispatch(td, tg, bio, NULL)) {
  544. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  545. nr_reads++;
  546. if (nr_reads >= max_nr_reads)
  547. break;
  548. }
  549. while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
  550. && tg_may_dispatch(td, tg, bio, NULL)) {
  551. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  552. nr_writes++;
  553. if (nr_writes >= max_nr_writes)
  554. break;
  555. }
  556. return nr_reads + nr_writes;
  557. }
  558. static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
  559. {
  560. unsigned int nr_disp = 0;
  561. struct throtl_grp *tg;
  562. struct throtl_rb_root *st = &td->tg_service_tree;
  563. while (1) {
  564. tg = throtl_rb_first(st);
  565. if (!tg)
  566. break;
  567. if (time_before(jiffies, tg->disptime))
  568. break;
  569. throtl_dequeue_tg(td, tg);
  570. nr_disp += throtl_dispatch_tg(td, tg, bl);
  571. if (tg->nr_queued[0] || tg->nr_queued[1]) {
  572. tg_update_disptime(td, tg);
  573. throtl_enqueue_tg(td, tg);
  574. }
  575. if (nr_disp >= throtl_quantum)
  576. break;
  577. }
  578. return nr_disp;
  579. }
  580. static void throtl_process_limit_change(struct throtl_data *td)
  581. {
  582. struct throtl_grp *tg;
  583. struct hlist_node *pos, *n;
  584. if (!atomic_read(&td->limits_changed))
  585. return;
  586. throtl_log(td, "limit changed =%d", atomic_read(&td->limits_changed));
  587. /*
  588. * Make sure updates from throtl_update_blkio_group_read_bps() group
  589. * of functions to tg->limits_changed are visible. We do not
  590. * want update td->limits_changed to be visible but update to
  591. * tg->limits_changed not being visible yet on this cpu. Hence
  592. * the read barrier.
  593. */
  594. smp_rmb();
  595. hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
  596. if (throtl_tg_on_rr(tg) && tg->limits_changed) {
  597. throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
  598. " riops=%u wiops=%u", tg->bps[READ],
  599. tg->bps[WRITE], tg->iops[READ],
  600. tg->iops[WRITE]);
  601. tg_update_disptime(td, tg);
  602. tg->limits_changed = false;
  603. }
  604. }
  605. smp_mb__before_atomic_dec();
  606. atomic_dec(&td->limits_changed);
  607. smp_mb__after_atomic_dec();
  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. spin_lock_irq(q->queue_lock);
  617. throtl_process_limit_change(td);
  618. if (!total_nr_queued(td))
  619. goto out;
  620. bio_list_init(&bio_list_on_stack);
  621. throtl_log(td, "dispatch nr_queued=%lu read=%u write=%u",
  622. total_nr_queued(td), td->nr_queued[READ],
  623. td->nr_queued[WRITE]);
  624. nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
  625. if (nr_disp)
  626. throtl_log(td, "bios disp=%u", nr_disp);
  627. throtl_schedule_next_dispatch(td);
  628. out:
  629. spin_unlock_irq(q->queue_lock);
  630. /*
  631. * If we dispatched some requests, unplug the queue to make sure
  632. * immediate dispatch
  633. */
  634. if (nr_disp) {
  635. while((bio = bio_list_pop(&bio_list_on_stack)))
  636. generic_make_request(bio);
  637. blk_unplug(q);
  638. }
  639. return nr_disp;
  640. }
  641. void blk_throtl_work(struct work_struct *work)
  642. {
  643. struct throtl_data *td = container_of(work, struct throtl_data,
  644. throtl_work.work);
  645. struct request_queue *q = td->queue;
  646. throtl_dispatch(q);
  647. }
  648. /* Call with queue lock held */
  649. static void
  650. throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
  651. {
  652. struct delayed_work *dwork = &td->throtl_work;
  653. if (total_nr_queued(td) > 0) {
  654. /*
  655. * We might have a work scheduled to be executed in future.
  656. * Cancel that and schedule a new one.
  657. */
  658. __cancel_delayed_work(dwork);
  659. queue_delayed_work(kthrotld_workqueue, dwork, delay);
  660. throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
  661. delay, jiffies);
  662. }
  663. }
  664. static void
  665. throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
  666. {
  667. /* Something wrong if we are trying to remove same group twice */
  668. BUG_ON(hlist_unhashed(&tg->tg_node));
  669. hlist_del_init(&tg->tg_node);
  670. /*
  671. * Put the reference taken at the time of creation so that when all
  672. * queues are gone, group can be destroyed.
  673. */
  674. throtl_put_tg(tg);
  675. td->nr_undestroyed_grps--;
  676. }
  677. static void throtl_release_tgs(struct throtl_data *td)
  678. {
  679. struct hlist_node *pos, *n;
  680. struct throtl_grp *tg;
  681. hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
  682. /*
  683. * If cgroup removal path got to blk_group first and removed
  684. * it from cgroup list, then it will take care of destroying
  685. * cfqg also.
  686. */
  687. if (!blkiocg_del_blkio_group(&tg->blkg))
  688. throtl_destroy_tg(td, tg);
  689. }
  690. }
  691. static void throtl_td_free(struct throtl_data *td)
  692. {
  693. kfree(td);
  694. }
  695. /*
  696. * Blk cgroup controller notification saying that blkio_group object is being
  697. * delinked as associated cgroup object is going away. That also means that
  698. * no new IO will come in this group. So get rid of this group as soon as
  699. * any pending IO in the group is finished.
  700. *
  701. * This function is called under rcu_read_lock(). key is the rcu protected
  702. * pointer. That means "key" is a valid throtl_data pointer as long as we are
  703. * rcu read lock.
  704. *
  705. * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
  706. * it should not be NULL as even if queue was going away, cgroup deltion
  707. * path got to it first.
  708. */
  709. void throtl_unlink_blkio_group(void *key, struct blkio_group *blkg)
  710. {
  711. unsigned long flags;
  712. struct throtl_data *td = key;
  713. spin_lock_irqsave(td->queue->queue_lock, flags);
  714. throtl_destroy_tg(td, tg_of_blkg(blkg));
  715. spin_unlock_irqrestore(td->queue->queue_lock, flags);
  716. }
  717. /*
  718. * For all update functions, key should be a valid pointer because these
  719. * update functions are called under blkcg_lock, that means, blkg is
  720. * valid and in turn key is valid. queue exit path can not race becuase
  721. * of blkcg_lock
  722. *
  723. * Can not take queue lock in update functions as queue lock under blkcg_lock
  724. * is not allowed. Under other paths we take blkcg_lock under queue_lock.
  725. */
  726. static void throtl_update_blkio_group_read_bps(void *key,
  727. struct blkio_group *blkg, u64 read_bps)
  728. {
  729. struct throtl_data *td = key;
  730. tg_of_blkg(blkg)->bps[READ] = read_bps;
  731. /* Make sure read_bps is updated before setting limits_changed */
  732. smp_wmb();
  733. tg_of_blkg(blkg)->limits_changed = true;
  734. /* Make sure tg->limits_changed is updated before td->limits_changed */
  735. smp_mb__before_atomic_inc();
  736. atomic_inc(&td->limits_changed);
  737. smp_mb__after_atomic_inc();
  738. /* Schedule a work now to process the limit change */
  739. throtl_schedule_delayed_work(td, 0);
  740. }
  741. static void throtl_update_blkio_group_write_bps(void *key,
  742. struct blkio_group *blkg, u64 write_bps)
  743. {
  744. struct throtl_data *td = key;
  745. tg_of_blkg(blkg)->bps[WRITE] = write_bps;
  746. smp_wmb();
  747. tg_of_blkg(blkg)->limits_changed = true;
  748. smp_mb__before_atomic_inc();
  749. atomic_inc(&td->limits_changed);
  750. smp_mb__after_atomic_inc();
  751. throtl_schedule_delayed_work(td, 0);
  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. tg_of_blkg(blkg)->iops[READ] = read_iops;
  758. smp_wmb();
  759. tg_of_blkg(blkg)->limits_changed = true;
  760. smp_mb__before_atomic_inc();
  761. atomic_inc(&td->limits_changed);
  762. smp_mb__after_atomic_inc();
  763. throtl_schedule_delayed_work(td, 0);
  764. }
  765. static void throtl_update_blkio_group_write_iops(void *key,
  766. struct blkio_group *blkg, unsigned int write_iops)
  767. {
  768. struct throtl_data *td = key;
  769. tg_of_blkg(blkg)->iops[WRITE] = write_iops;
  770. smp_wmb();
  771. tg_of_blkg(blkg)->limits_changed = true;
  772. smp_mb__before_atomic_inc();
  773. atomic_inc(&td->limits_changed);
  774. smp_mb__after_atomic_inc();
  775. throtl_schedule_delayed_work(td, 0);
  776. }
  777. static void throtl_shutdown_wq(struct request_queue *q)
  778. {
  779. struct throtl_data *td = q->td;
  780. cancel_delayed_work_sync(&td->throtl_work);
  781. }
  782. static struct blkio_policy_type blkio_policy_throtl = {
  783. .ops = {
  784. .blkio_unlink_group_fn = throtl_unlink_blkio_group,
  785. .blkio_update_group_read_bps_fn =
  786. throtl_update_blkio_group_read_bps,
  787. .blkio_update_group_write_bps_fn =
  788. throtl_update_blkio_group_write_bps,
  789. .blkio_update_group_read_iops_fn =
  790. throtl_update_blkio_group_read_iops,
  791. .blkio_update_group_write_iops_fn =
  792. throtl_update_blkio_group_write_iops,
  793. },
  794. .plid = BLKIO_POLICY_THROTL,
  795. };
  796. int blk_throtl_bio(struct request_queue *q, struct bio **biop)
  797. {
  798. struct throtl_data *td = q->td;
  799. struct throtl_grp *tg;
  800. struct bio *bio = *biop;
  801. bool rw = bio_data_dir(bio), update_disptime = true;
  802. if (bio->bi_rw & REQ_THROTTLED) {
  803. bio->bi_rw &= ~REQ_THROTTLED;
  804. return 0;
  805. }
  806. spin_lock_irq(q->queue_lock);
  807. tg = throtl_get_tg(td);
  808. if (tg->nr_queued[rw]) {
  809. /*
  810. * There is already another bio queued in same dir. No
  811. * need to update dispatch time.
  812. * Still update the disptime if rate limits on this group
  813. * were changed.
  814. */
  815. if (!tg->limits_changed)
  816. update_disptime = false;
  817. else
  818. tg->limits_changed = false;
  819. goto queue_bio;
  820. }
  821. /* Bio is with-in rate limit of group */
  822. if (tg_may_dispatch(td, tg, bio, NULL)) {
  823. throtl_charge_bio(tg, bio);
  824. goto out;
  825. }
  826. queue_bio:
  827. throtl_log_tg(td, tg, "[%c] bio. bdisp=%u sz=%u bps=%llu"
  828. " iodisp=%u iops=%u queued=%d/%d",
  829. rw == READ ? 'R' : 'W',
  830. tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
  831. tg->io_disp[rw], tg->iops[rw],
  832. tg->nr_queued[READ], tg->nr_queued[WRITE]);
  833. throtl_add_bio_tg(q->td, tg, bio);
  834. *biop = NULL;
  835. if (update_disptime) {
  836. tg_update_disptime(td, tg);
  837. throtl_schedule_next_dispatch(td);
  838. }
  839. out:
  840. spin_unlock_irq(q->queue_lock);
  841. return 0;
  842. }
  843. int blk_throtl_init(struct request_queue *q)
  844. {
  845. struct throtl_data *td;
  846. struct throtl_grp *tg;
  847. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  848. if (!td)
  849. return -ENOMEM;
  850. INIT_HLIST_HEAD(&td->tg_list);
  851. td->tg_service_tree = THROTL_RB_ROOT;
  852. atomic_set(&td->limits_changed, 0);
  853. /* Init root group */
  854. tg = &td->root_tg;
  855. INIT_HLIST_NODE(&tg->tg_node);
  856. RB_CLEAR_NODE(&tg->rb_node);
  857. bio_list_init(&tg->bio_lists[0]);
  858. bio_list_init(&tg->bio_lists[1]);
  859. /* Practically unlimited BW */
  860. tg->bps[0] = tg->bps[1] = -1;
  861. tg->iops[0] = tg->iops[1] = -1;
  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);