blk-throttle.c 30 KB

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