blk-throttle.c 31 KB

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