blk-throttle.c 32 KB

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