blk-throttle.c 31 KB

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