blk-throttle.c 33 KB

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