blk-throttle.c 28 KB

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