blk-throttle.c 33 KB

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