blk-throttle.c 33 KB

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