blk-throttle.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. static struct blkcg_policy blkcg_policy_throtl;
  20. /* A workqueue to queue throttle related work */
  21. static struct workqueue_struct *kthrotld_workqueue;
  22. struct throtl_service_queue {
  23. struct throtl_service_queue *parent_sq; /* the parent service_queue */
  24. /*
  25. * Bios queued directly to this service_queue or dispatched from
  26. * children throtl_grp's.
  27. */
  28. struct bio_list bio_lists[2]; /* queued bios [READ/WRITE] */
  29. unsigned int nr_queued[2]; /* number of queued bios */
  30. /*
  31. * RB tree of active children throtl_grp's, which are sorted by
  32. * their ->disptime.
  33. */
  34. struct rb_root pending_tree; /* RB tree of active tgs */
  35. struct rb_node *first_pending; /* first node in the tree */
  36. unsigned int nr_pending; /* # queued in the tree */
  37. unsigned long first_pending_disptime; /* disptime of the first tg */
  38. };
  39. enum tg_state_flags {
  40. THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
  41. THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
  42. };
  43. #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
  44. /* Per-cpu group stats */
  45. struct tg_stats_cpu {
  46. /* total bytes transferred */
  47. struct blkg_rwstat service_bytes;
  48. /* total IOs serviced, post merge */
  49. struct blkg_rwstat serviced;
  50. };
  51. struct throtl_grp {
  52. /* must be the first member */
  53. struct blkg_policy_data pd;
  54. /* active throtl group service_queue member */
  55. struct rb_node rb_node;
  56. /* throtl_data this group belongs to */
  57. struct throtl_data *td;
  58. /* this group's service queue */
  59. struct throtl_service_queue service_queue;
  60. /*
  61. * Dispatch time in jiffies. This is the estimated time when group
  62. * will unthrottle and is ready to dispatch more bio. It is used as
  63. * key to sort active groups in service tree.
  64. */
  65. unsigned long disptime;
  66. unsigned int flags;
  67. /* bytes per second rate limits */
  68. uint64_t bps[2];
  69. /* IOPS limits */
  70. unsigned int iops[2];
  71. /* Number of bytes disptached in current slice */
  72. uint64_t bytes_disp[2];
  73. /* Number of bio's dispatched in current slice */
  74. unsigned int io_disp[2];
  75. /* When did we start a new slice */
  76. unsigned long slice_start[2];
  77. unsigned long slice_end[2];
  78. /* Per cpu stats pointer */
  79. struct tg_stats_cpu __percpu *stats_cpu;
  80. /* List of tgs waiting for per cpu stats memory to be allocated */
  81. struct list_head stats_alloc_node;
  82. };
  83. struct throtl_data
  84. {
  85. /* service tree for active throtl groups */
  86. struct throtl_service_queue service_queue;
  87. struct request_queue *queue;
  88. /* Total Number of queued bios on READ and WRITE lists */
  89. unsigned int nr_queued[2];
  90. /*
  91. * number of total undestroyed groups
  92. */
  93. unsigned int nr_undestroyed_grps;
  94. /* Work for dispatching throttled bios */
  95. struct delayed_work dispatch_work;
  96. };
  97. /* list and work item to allocate percpu group stats */
  98. static DEFINE_SPINLOCK(tg_stats_alloc_lock);
  99. static LIST_HEAD(tg_stats_alloc_list);
  100. static void tg_stats_alloc_fn(struct work_struct *);
  101. static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
  102. static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
  103. {
  104. return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
  105. }
  106. static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
  107. {
  108. return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
  109. }
  110. static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
  111. {
  112. return pd_to_blkg(&tg->pd);
  113. }
  114. static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
  115. {
  116. return blkg_to_tg(td->queue->root_blkg);
  117. }
  118. /**
  119. * sq_to_tg - return the throl_grp the specified service queue belongs to
  120. * @sq: the throtl_service_queue of interest
  121. *
  122. * Return the throtl_grp @sq belongs to. If @sq is the top-level one
  123. * embedded in throtl_data, %NULL is returned.
  124. */
  125. static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
  126. {
  127. if (sq && sq->parent_sq)
  128. return container_of(sq, struct throtl_grp, service_queue);
  129. else
  130. return NULL;
  131. }
  132. /**
  133. * sq_to_td - return throtl_data the specified service queue belongs to
  134. * @sq: the throtl_service_queue of interest
  135. *
  136. * A service_queue can be embeded in either a throtl_grp or throtl_data.
  137. * Determine the associated throtl_data accordingly and return it.
  138. */
  139. static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
  140. {
  141. struct throtl_grp *tg = sq_to_tg(sq);
  142. if (tg)
  143. return tg->td;
  144. else
  145. return container_of(sq, struct throtl_data, service_queue);
  146. }
  147. /**
  148. * throtl_log - log debug message via blktrace
  149. * @sq: the service_queue being reported
  150. * @fmt: printf format string
  151. * @args: printf args
  152. *
  153. * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
  154. * throtl_grp; otherwise, just "throtl".
  155. *
  156. * TODO: this should be made a function and name formatting should happen
  157. * after testing whether blktrace is enabled.
  158. */
  159. #define throtl_log(sq, fmt, args...) do { \
  160. struct throtl_grp *__tg = sq_to_tg((sq)); \
  161. struct throtl_data *__td = sq_to_td((sq)); \
  162. \
  163. (void)__td; \
  164. if ((__tg)) { \
  165. char __pbuf[128]; \
  166. \
  167. blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
  168. blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
  169. } else { \
  170. blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
  171. } \
  172. } while (0)
  173. /*
  174. * Worker for allocating per cpu stat for tgs. This is scheduled on the
  175. * system_wq once there are some groups on the alloc_list waiting for
  176. * allocation.
  177. */
  178. static void tg_stats_alloc_fn(struct work_struct *work)
  179. {
  180. static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
  181. struct delayed_work *dwork = to_delayed_work(work);
  182. bool empty = false;
  183. alloc_stats:
  184. if (!stats_cpu) {
  185. stats_cpu = alloc_percpu(struct tg_stats_cpu);
  186. if (!stats_cpu) {
  187. /* allocation failed, try again after some time */
  188. schedule_delayed_work(dwork, msecs_to_jiffies(10));
  189. return;
  190. }
  191. }
  192. spin_lock_irq(&tg_stats_alloc_lock);
  193. if (!list_empty(&tg_stats_alloc_list)) {
  194. struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
  195. struct throtl_grp,
  196. stats_alloc_node);
  197. swap(tg->stats_cpu, stats_cpu);
  198. list_del_init(&tg->stats_alloc_node);
  199. }
  200. empty = list_empty(&tg_stats_alloc_list);
  201. spin_unlock_irq(&tg_stats_alloc_lock);
  202. if (!empty)
  203. goto alloc_stats;
  204. }
  205. /* init a service_queue, assumes the caller zeroed it */
  206. static void throtl_service_queue_init(struct throtl_service_queue *sq,
  207. struct throtl_service_queue *parent_sq)
  208. {
  209. bio_list_init(&sq->bio_lists[0]);
  210. bio_list_init(&sq->bio_lists[1]);
  211. sq->pending_tree = RB_ROOT;
  212. sq->parent_sq = parent_sq;
  213. }
  214. static void throtl_pd_init(struct blkcg_gq *blkg)
  215. {
  216. struct throtl_grp *tg = blkg_to_tg(blkg);
  217. struct throtl_data *td = blkg->q->td;
  218. unsigned long flags;
  219. throtl_service_queue_init(&tg->service_queue, &td->service_queue);
  220. RB_CLEAR_NODE(&tg->rb_node);
  221. tg->td = td;
  222. tg->bps[READ] = -1;
  223. tg->bps[WRITE] = -1;
  224. tg->iops[READ] = -1;
  225. tg->iops[WRITE] = -1;
  226. /*
  227. * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
  228. * but percpu allocator can't be called from IO path. Queue tg on
  229. * tg_stats_alloc_list and allocate from work item.
  230. */
  231. spin_lock_irqsave(&tg_stats_alloc_lock, flags);
  232. list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
  233. schedule_delayed_work(&tg_stats_alloc_work, 0);
  234. spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
  235. }
  236. static void throtl_pd_exit(struct blkcg_gq *blkg)
  237. {
  238. struct throtl_grp *tg = blkg_to_tg(blkg);
  239. unsigned long flags;
  240. spin_lock_irqsave(&tg_stats_alloc_lock, flags);
  241. list_del_init(&tg->stats_alloc_node);
  242. spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
  243. free_percpu(tg->stats_cpu);
  244. }
  245. static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
  246. {
  247. struct throtl_grp *tg = blkg_to_tg(blkg);
  248. int cpu;
  249. if (tg->stats_cpu == NULL)
  250. return;
  251. for_each_possible_cpu(cpu) {
  252. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  253. blkg_rwstat_reset(&sc->service_bytes);
  254. blkg_rwstat_reset(&sc->serviced);
  255. }
  256. }
  257. static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
  258. struct blkcg *blkcg)
  259. {
  260. /*
  261. * This is the common case when there are no blkcgs. Avoid lookup
  262. * in this case
  263. */
  264. if (blkcg == &blkcg_root)
  265. return td_root_tg(td);
  266. return blkg_to_tg(blkg_lookup(blkcg, td->queue));
  267. }
  268. static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
  269. struct blkcg *blkcg)
  270. {
  271. struct request_queue *q = td->queue;
  272. struct throtl_grp *tg = NULL;
  273. /*
  274. * This is the common case when there are no blkcgs. Avoid lookup
  275. * in this case
  276. */
  277. if (blkcg == &blkcg_root) {
  278. tg = td_root_tg(td);
  279. } else {
  280. struct blkcg_gq *blkg;
  281. blkg = blkg_lookup_create(blkcg, q);
  282. /* if %NULL and @q is alive, fall back to root_tg */
  283. if (!IS_ERR(blkg))
  284. tg = blkg_to_tg(blkg);
  285. else if (!blk_queue_dying(q))
  286. tg = td_root_tg(td);
  287. }
  288. return tg;
  289. }
  290. static struct throtl_grp *
  291. throtl_rb_first(struct throtl_service_queue *parent_sq)
  292. {
  293. /* Service tree is empty */
  294. if (!parent_sq->nr_pending)
  295. return NULL;
  296. if (!parent_sq->first_pending)
  297. parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
  298. if (parent_sq->first_pending)
  299. return rb_entry_tg(parent_sq->first_pending);
  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,
  308. struct throtl_service_queue *parent_sq)
  309. {
  310. if (parent_sq->first_pending == n)
  311. parent_sq->first_pending = NULL;
  312. rb_erase_init(n, &parent_sq->pending_tree);
  313. --parent_sq->nr_pending;
  314. }
  315. static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
  316. {
  317. struct throtl_grp *tg;
  318. tg = throtl_rb_first(parent_sq);
  319. if (!tg)
  320. return;
  321. parent_sq->first_pending_disptime = tg->disptime;
  322. }
  323. static void tg_service_queue_add(struct throtl_grp *tg)
  324. {
  325. struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
  326. struct rb_node **node = &parent_sq->pending_tree.rb_node;
  327. struct rb_node *parent = NULL;
  328. struct throtl_grp *__tg;
  329. unsigned long key = tg->disptime;
  330. int left = 1;
  331. while (*node != NULL) {
  332. parent = *node;
  333. __tg = rb_entry_tg(parent);
  334. if (time_before(key, __tg->disptime))
  335. node = &parent->rb_left;
  336. else {
  337. node = &parent->rb_right;
  338. left = 0;
  339. }
  340. }
  341. if (left)
  342. parent_sq->first_pending = &tg->rb_node;
  343. rb_link_node(&tg->rb_node, parent, node);
  344. rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
  345. }
  346. static void __throtl_enqueue_tg(struct throtl_grp *tg)
  347. {
  348. tg_service_queue_add(tg);
  349. tg->flags |= THROTL_TG_PENDING;
  350. tg->service_queue.parent_sq->nr_pending++;
  351. }
  352. static void throtl_enqueue_tg(struct throtl_grp *tg)
  353. {
  354. if (!(tg->flags & THROTL_TG_PENDING))
  355. __throtl_enqueue_tg(tg);
  356. }
  357. static void __throtl_dequeue_tg(struct throtl_grp *tg)
  358. {
  359. throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
  360. tg->flags &= ~THROTL_TG_PENDING;
  361. }
  362. static void throtl_dequeue_tg(struct throtl_grp *tg)
  363. {
  364. if (tg->flags & THROTL_TG_PENDING)
  365. __throtl_dequeue_tg(tg);
  366. }
  367. /* Call with queue lock held */
  368. static void throtl_schedule_delayed_work(struct throtl_data *td,
  369. unsigned long delay)
  370. {
  371. struct delayed_work *dwork = &td->dispatch_work;
  372. struct throtl_service_queue *sq = &td->service_queue;
  373. mod_delayed_work(kthrotld_workqueue, dwork, delay);
  374. throtl_log(sq, "schedule work. delay=%lu jiffies=%lu", delay, jiffies);
  375. }
  376. static void throtl_schedule_next_dispatch(struct throtl_data *td)
  377. {
  378. struct throtl_service_queue *sq = &td->service_queue;
  379. /* any pending children left? */
  380. if (!sq->nr_pending)
  381. return;
  382. update_min_dispatch_time(sq);
  383. if (time_before_eq(sq->first_pending_disptime, jiffies))
  384. throtl_schedule_delayed_work(td, 0);
  385. else
  386. throtl_schedule_delayed_work(td, sq->first_pending_disptime - jiffies);
  387. }
  388. static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
  389. {
  390. tg->bytes_disp[rw] = 0;
  391. tg->io_disp[rw] = 0;
  392. tg->slice_start[rw] = jiffies;
  393. tg->slice_end[rw] = jiffies + throtl_slice;
  394. throtl_log(&tg->service_queue,
  395. "[%c] new slice start=%lu end=%lu jiffies=%lu",
  396. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  397. tg->slice_end[rw], jiffies);
  398. }
  399. static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
  400. unsigned long jiffy_end)
  401. {
  402. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  403. }
  404. static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
  405. unsigned long jiffy_end)
  406. {
  407. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  408. throtl_log(&tg->service_queue,
  409. "[%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 throtl_slice_used(struct throtl_grp *tg, bool rw)
  415. {
  416. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  417. return 0;
  418. return 1;
  419. }
  420. /* Trim the used slices and adjust slice start accordingly */
  421. static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
  422. {
  423. unsigned long nr_slices, time_elapsed, io_trim;
  424. u64 bytes_trim, tmp;
  425. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  426. /*
  427. * If bps are unlimited (-1), then time slice don't get
  428. * renewed. Don't try to trim the slice if slice is used. A new
  429. * slice will start when appropriate.
  430. */
  431. if (throtl_slice_used(tg, rw))
  432. return;
  433. /*
  434. * A bio has been dispatched. Also adjust slice_end. It might happen
  435. * that initially cgroup limit was very low resulting in high
  436. * slice_end, but later limit was bumped up and bio was dispached
  437. * sooner, then we need to reduce slice_end. A high bogus slice_end
  438. * is bad because it does not allow new slice to start.
  439. */
  440. throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
  441. time_elapsed = jiffies - tg->slice_start[rw];
  442. nr_slices = time_elapsed / throtl_slice;
  443. if (!nr_slices)
  444. return;
  445. tmp = tg->bps[rw] * throtl_slice * nr_slices;
  446. do_div(tmp, HZ);
  447. bytes_trim = tmp;
  448. io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
  449. if (!bytes_trim && !io_trim)
  450. return;
  451. if (tg->bytes_disp[rw] >= bytes_trim)
  452. tg->bytes_disp[rw] -= bytes_trim;
  453. else
  454. tg->bytes_disp[rw] = 0;
  455. if (tg->io_disp[rw] >= io_trim)
  456. tg->io_disp[rw] -= io_trim;
  457. else
  458. tg->io_disp[rw] = 0;
  459. tg->slice_start[rw] += nr_slices * throtl_slice;
  460. throtl_log(&tg->service_queue,
  461. "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
  462. rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
  463. tg->slice_start[rw], tg->slice_end[rw], jiffies);
  464. }
  465. static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
  466. unsigned long *wait)
  467. {
  468. bool rw = bio_data_dir(bio);
  469. unsigned int io_allowed;
  470. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  471. u64 tmp;
  472. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  473. /* Slice has just started. Consider one slice interval */
  474. if (!jiffy_elapsed)
  475. jiffy_elapsed_rnd = throtl_slice;
  476. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  477. /*
  478. * jiffy_elapsed_rnd should not be a big value as minimum iops can be
  479. * 1 then at max jiffy elapsed should be equivalent of 1 second as we
  480. * will allow dispatch after 1 second and after that slice should
  481. * have been trimmed.
  482. */
  483. tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
  484. do_div(tmp, HZ);
  485. if (tmp > UINT_MAX)
  486. io_allowed = UINT_MAX;
  487. else
  488. io_allowed = tmp;
  489. if (tg->io_disp[rw] + 1 <= io_allowed) {
  490. if (wait)
  491. *wait = 0;
  492. return 1;
  493. }
  494. /* Calc approx time to dispatch */
  495. jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
  496. if (jiffy_wait > jiffy_elapsed)
  497. jiffy_wait = jiffy_wait - jiffy_elapsed;
  498. else
  499. jiffy_wait = 1;
  500. if (wait)
  501. *wait = jiffy_wait;
  502. return 0;
  503. }
  504. static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
  505. unsigned long *wait)
  506. {
  507. bool rw = bio_data_dir(bio);
  508. u64 bytes_allowed, extra_bytes, tmp;
  509. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  510. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  511. /* Slice has just started. Consider one slice interval */
  512. if (!jiffy_elapsed)
  513. jiffy_elapsed_rnd = throtl_slice;
  514. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  515. tmp = tg->bps[rw] * jiffy_elapsed_rnd;
  516. do_div(tmp, HZ);
  517. bytes_allowed = tmp;
  518. if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
  519. if (wait)
  520. *wait = 0;
  521. return 1;
  522. }
  523. /* Calc approx time to dispatch */
  524. extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
  525. jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
  526. if (!jiffy_wait)
  527. jiffy_wait = 1;
  528. /*
  529. * This wait time is without taking into consideration the rounding
  530. * up we did. Add that time also.
  531. */
  532. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  533. if (wait)
  534. *wait = jiffy_wait;
  535. return 0;
  536. }
  537. static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
  538. if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
  539. return 1;
  540. return 0;
  541. }
  542. /*
  543. * Returns whether one can dispatch a bio or not. Also returns approx number
  544. * of jiffies to wait before this bio is with-in IO rate and can be dispatched
  545. */
  546. static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
  547. unsigned long *wait)
  548. {
  549. bool rw = bio_data_dir(bio);
  550. unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
  551. /*
  552. * Currently whole state machine of group depends on first bio
  553. * queued in the group bio list. So one should not be calling
  554. * this function with a different bio if there are other bios
  555. * queued.
  556. */
  557. BUG_ON(tg->service_queue.nr_queued[rw] &&
  558. bio != bio_list_peek(&tg->service_queue.bio_lists[rw]));
  559. /* If tg->bps = -1, then BW is unlimited */
  560. if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
  561. if (wait)
  562. *wait = 0;
  563. return 1;
  564. }
  565. /*
  566. * If previous slice expired, start a new one otherwise renew/extend
  567. * existing slice to make sure it is at least throtl_slice interval
  568. * long since now.
  569. */
  570. if (throtl_slice_used(tg, rw))
  571. throtl_start_new_slice(tg, rw);
  572. else {
  573. if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
  574. throtl_extend_slice(tg, rw, jiffies + throtl_slice);
  575. }
  576. if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
  577. tg_with_in_iops_limit(tg, bio, &iops_wait)) {
  578. if (wait)
  579. *wait = 0;
  580. return 1;
  581. }
  582. max_wait = max(bps_wait, iops_wait);
  583. if (wait)
  584. *wait = max_wait;
  585. if (time_before(tg->slice_end[rw], jiffies + max_wait))
  586. throtl_extend_slice(tg, rw, jiffies + max_wait);
  587. return 0;
  588. }
  589. static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
  590. int rw)
  591. {
  592. struct throtl_grp *tg = blkg_to_tg(blkg);
  593. struct tg_stats_cpu *stats_cpu;
  594. unsigned long flags;
  595. /* If per cpu stats are not allocated yet, don't do any accounting. */
  596. if (tg->stats_cpu == NULL)
  597. return;
  598. /*
  599. * Disabling interrupts to provide mutual exclusion between two
  600. * writes on same cpu. It probably is not needed for 64bit. Not
  601. * optimizing that case yet.
  602. */
  603. local_irq_save(flags);
  604. stats_cpu = this_cpu_ptr(tg->stats_cpu);
  605. blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
  606. blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
  607. local_irq_restore(flags);
  608. }
  609. static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
  610. {
  611. bool rw = bio_data_dir(bio);
  612. /* Charge the bio to the group */
  613. tg->bytes_disp[rw] += bio->bi_size;
  614. tg->io_disp[rw]++;
  615. /*
  616. * REQ_THROTTLED is used to prevent the same bio to be throttled
  617. * more than once as a throttled bio will go through blk-throtl the
  618. * second time when it eventually gets issued. Set it when a bio
  619. * is being charged to a tg.
  620. *
  621. * Dispatch stats aren't recursive and each @bio should only be
  622. * accounted by the @tg it was originally associated with. Let's
  623. * update the stats when setting REQ_THROTTLED for the first time
  624. * which is guaranteed to be for the @bio's original tg.
  625. */
  626. if (!(bio->bi_rw & REQ_THROTTLED)) {
  627. bio->bi_rw |= REQ_THROTTLED;
  628. throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size,
  629. bio->bi_rw);
  630. }
  631. }
  632. static void throtl_add_bio_tg(struct bio *bio, struct throtl_grp *tg)
  633. {
  634. struct throtl_service_queue *sq = &tg->service_queue;
  635. bool rw = bio_data_dir(bio);
  636. /*
  637. * If @tg doesn't currently have any bios queued in the same
  638. * direction, queueing @bio can change when @tg should be
  639. * dispatched. Mark that @tg was empty. This is automatically
  640. * cleaered on the next tg_update_disptime().
  641. */
  642. if (!sq->nr_queued[rw])
  643. tg->flags |= THROTL_TG_WAS_EMPTY;
  644. bio_list_add(&sq->bio_lists[rw], bio);
  645. /* Take a bio reference on tg */
  646. blkg_get(tg_to_blkg(tg));
  647. sq->nr_queued[rw]++;
  648. tg->td->nr_queued[rw]++;
  649. throtl_enqueue_tg(tg);
  650. }
  651. static void tg_update_disptime(struct throtl_grp *tg)
  652. {
  653. struct throtl_service_queue *sq = &tg->service_queue;
  654. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  655. struct bio *bio;
  656. if ((bio = bio_list_peek(&sq->bio_lists[READ])))
  657. tg_may_dispatch(tg, bio, &read_wait);
  658. if ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
  659. tg_may_dispatch(tg, bio, &write_wait);
  660. min_wait = min(read_wait, write_wait);
  661. disptime = jiffies + min_wait;
  662. /* Update dispatch time */
  663. throtl_dequeue_tg(tg);
  664. tg->disptime = disptime;
  665. throtl_enqueue_tg(tg);
  666. /* see throtl_add_bio_tg() */
  667. tg->flags &= ~THROTL_TG_WAS_EMPTY;
  668. }
  669. static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
  670. {
  671. struct throtl_service_queue *sq = &tg->service_queue;
  672. struct bio *bio;
  673. bio = bio_list_pop(&sq->bio_lists[rw]);
  674. sq->nr_queued[rw]--;
  675. /* Drop bio reference on blkg */
  676. blkg_put(tg_to_blkg(tg));
  677. BUG_ON(tg->td->nr_queued[rw] <= 0);
  678. tg->td->nr_queued[rw]--;
  679. throtl_charge_bio(tg, bio);
  680. bio_list_add(&sq->parent_sq->bio_lists[rw], bio);
  681. throtl_trim_slice(tg, rw);
  682. }
  683. static int throtl_dispatch_tg(struct throtl_grp *tg)
  684. {
  685. struct throtl_service_queue *sq = &tg->service_queue;
  686. unsigned int nr_reads = 0, nr_writes = 0;
  687. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  688. unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
  689. struct bio *bio;
  690. /* Try to dispatch 75% READS and 25% WRITES */
  691. while ((bio = bio_list_peek(&sq->bio_lists[READ])) &&
  692. tg_may_dispatch(tg, bio, NULL)) {
  693. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  694. nr_reads++;
  695. if (nr_reads >= max_nr_reads)
  696. break;
  697. }
  698. while ((bio = bio_list_peek(&sq->bio_lists[WRITE])) &&
  699. tg_may_dispatch(tg, bio, NULL)) {
  700. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  701. nr_writes++;
  702. if (nr_writes >= max_nr_writes)
  703. break;
  704. }
  705. return nr_reads + nr_writes;
  706. }
  707. static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
  708. {
  709. unsigned int nr_disp = 0;
  710. while (1) {
  711. struct throtl_grp *tg = throtl_rb_first(parent_sq);
  712. struct throtl_service_queue *sq = &tg->service_queue;
  713. if (!tg)
  714. break;
  715. if (time_before(jiffies, tg->disptime))
  716. break;
  717. throtl_dequeue_tg(tg);
  718. nr_disp += throtl_dispatch_tg(tg);
  719. if (sq->nr_queued[0] || sq->nr_queued[1])
  720. tg_update_disptime(tg);
  721. if (nr_disp >= throtl_quantum)
  722. break;
  723. }
  724. return nr_disp;
  725. }
  726. /* work function to dispatch throttled bios */
  727. void blk_throtl_dispatch_work_fn(struct work_struct *work)
  728. {
  729. struct throtl_data *td = container_of(to_delayed_work(work),
  730. struct throtl_data, dispatch_work);
  731. struct throtl_service_queue *sq = &td->service_queue;
  732. struct request_queue *q = td->queue;
  733. unsigned int nr_disp = 0;
  734. struct bio_list bio_list_on_stack;
  735. struct bio *bio;
  736. struct blk_plug plug;
  737. int rw;
  738. spin_lock_irq(q->queue_lock);
  739. bio_list_init(&bio_list_on_stack);
  740. throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
  741. td->nr_queued[READ] + td->nr_queued[WRITE],
  742. td->nr_queued[READ], td->nr_queued[WRITE]);
  743. nr_disp = throtl_select_dispatch(sq);
  744. if (nr_disp) {
  745. for (rw = READ; rw <= WRITE; rw++) {
  746. bio_list_merge(&bio_list_on_stack, &sq->bio_lists[rw]);
  747. bio_list_init(&sq->bio_lists[rw]);
  748. }
  749. throtl_log(sq, "bios disp=%u", nr_disp);
  750. }
  751. throtl_schedule_next_dispatch(td);
  752. spin_unlock_irq(q->queue_lock);
  753. /*
  754. * If we dispatched some requests, unplug the queue to make sure
  755. * immediate dispatch
  756. */
  757. if (nr_disp) {
  758. blk_start_plug(&plug);
  759. while((bio = bio_list_pop(&bio_list_on_stack)))
  760. generic_make_request(bio);
  761. blk_finish_plug(&plug);
  762. }
  763. }
  764. static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
  765. struct blkg_policy_data *pd, int off)
  766. {
  767. struct throtl_grp *tg = pd_to_tg(pd);
  768. struct blkg_rwstat rwstat = { }, tmp;
  769. int i, cpu;
  770. for_each_possible_cpu(cpu) {
  771. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  772. tmp = blkg_rwstat_read((void *)sc + off);
  773. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  774. rwstat.cnt[i] += tmp.cnt[i];
  775. }
  776. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  777. }
  778. static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
  779. struct seq_file *sf)
  780. {
  781. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  782. blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
  783. cft->private, true);
  784. return 0;
  785. }
  786. static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
  787. int off)
  788. {
  789. struct throtl_grp *tg = pd_to_tg(pd);
  790. u64 v = *(u64 *)((void *)tg + off);
  791. if (v == -1)
  792. return 0;
  793. return __blkg_prfill_u64(sf, pd, v);
  794. }
  795. static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
  796. int off)
  797. {
  798. struct throtl_grp *tg = pd_to_tg(pd);
  799. unsigned int v = *(unsigned int *)((void *)tg + off);
  800. if (v == -1)
  801. return 0;
  802. return __blkg_prfill_u64(sf, pd, v);
  803. }
  804. static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  805. struct seq_file *sf)
  806. {
  807. blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
  808. &blkcg_policy_throtl, cft->private, false);
  809. return 0;
  810. }
  811. static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  812. struct seq_file *sf)
  813. {
  814. blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
  815. &blkcg_policy_throtl, cft->private, false);
  816. return 0;
  817. }
  818. static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
  819. bool is_u64)
  820. {
  821. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  822. struct blkg_conf_ctx ctx;
  823. struct throtl_grp *tg;
  824. struct throtl_data *td;
  825. int ret;
  826. ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
  827. if (ret)
  828. return ret;
  829. tg = blkg_to_tg(ctx.blkg);
  830. td = ctx.blkg->q->td;
  831. if (!ctx.v)
  832. ctx.v = -1;
  833. if (is_u64)
  834. *(u64 *)((void *)tg + cft->private) = ctx.v;
  835. else
  836. *(unsigned int *)((void *)tg + cft->private) = ctx.v;
  837. throtl_log(&tg->service_queue,
  838. "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
  839. tg->bps[READ], tg->bps[WRITE],
  840. tg->iops[READ], tg->iops[WRITE]);
  841. /*
  842. * We're already holding queue_lock and know @tg is valid. Let's
  843. * apply the new config directly.
  844. *
  845. * Restart the slices for both READ and WRITES. It might happen
  846. * that a group's limit are dropped suddenly and we don't want to
  847. * account recently dispatched IO with new low rate.
  848. */
  849. throtl_start_new_slice(tg, 0);
  850. throtl_start_new_slice(tg, 1);
  851. if (tg->flags & THROTL_TG_PENDING) {
  852. tg_update_disptime(tg);
  853. throtl_schedule_next_dispatch(td);
  854. }
  855. blkg_conf_finish(&ctx);
  856. return 0;
  857. }
  858. static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  859. const char *buf)
  860. {
  861. return tg_set_conf(cgrp, cft, buf, true);
  862. }
  863. static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  864. const char *buf)
  865. {
  866. return tg_set_conf(cgrp, cft, buf, false);
  867. }
  868. static struct cftype throtl_files[] = {
  869. {
  870. .name = "throttle.read_bps_device",
  871. .private = offsetof(struct throtl_grp, bps[READ]),
  872. .read_seq_string = tg_print_conf_u64,
  873. .write_string = tg_set_conf_u64,
  874. .max_write_len = 256,
  875. },
  876. {
  877. .name = "throttle.write_bps_device",
  878. .private = offsetof(struct throtl_grp, bps[WRITE]),
  879. .read_seq_string = tg_print_conf_u64,
  880. .write_string = tg_set_conf_u64,
  881. .max_write_len = 256,
  882. },
  883. {
  884. .name = "throttle.read_iops_device",
  885. .private = offsetof(struct throtl_grp, iops[READ]),
  886. .read_seq_string = tg_print_conf_uint,
  887. .write_string = tg_set_conf_uint,
  888. .max_write_len = 256,
  889. },
  890. {
  891. .name = "throttle.write_iops_device",
  892. .private = offsetof(struct throtl_grp, iops[WRITE]),
  893. .read_seq_string = tg_print_conf_uint,
  894. .write_string = tg_set_conf_uint,
  895. .max_write_len = 256,
  896. },
  897. {
  898. .name = "throttle.io_service_bytes",
  899. .private = offsetof(struct tg_stats_cpu, service_bytes),
  900. .read_seq_string = tg_print_cpu_rwstat,
  901. },
  902. {
  903. .name = "throttle.io_serviced",
  904. .private = offsetof(struct tg_stats_cpu, serviced),
  905. .read_seq_string = tg_print_cpu_rwstat,
  906. },
  907. { } /* terminate */
  908. };
  909. static void throtl_shutdown_wq(struct request_queue *q)
  910. {
  911. struct throtl_data *td = q->td;
  912. cancel_delayed_work_sync(&td->dispatch_work);
  913. }
  914. static struct blkcg_policy blkcg_policy_throtl = {
  915. .pd_size = sizeof(struct throtl_grp),
  916. .cftypes = throtl_files,
  917. .pd_init_fn = throtl_pd_init,
  918. .pd_exit_fn = throtl_pd_exit,
  919. .pd_reset_stats_fn = throtl_pd_reset_stats,
  920. };
  921. bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
  922. {
  923. struct throtl_data *td = q->td;
  924. struct throtl_grp *tg;
  925. struct throtl_service_queue *sq;
  926. bool rw = bio_data_dir(bio);
  927. struct blkcg *blkcg;
  928. bool throttled = false;
  929. /* see throtl_charge_bio() */
  930. if (bio->bi_rw & REQ_THROTTLED)
  931. goto out;
  932. /*
  933. * A throtl_grp pointer retrieved under rcu can be used to access
  934. * basic fields like stats and io rates. If a group has no rules,
  935. * just update the dispatch stats in lockless manner and return.
  936. */
  937. rcu_read_lock();
  938. blkcg = bio_blkcg(bio);
  939. tg = throtl_lookup_tg(td, blkcg);
  940. if (tg) {
  941. if (tg_no_rule_group(tg, rw)) {
  942. throtl_update_dispatch_stats(tg_to_blkg(tg),
  943. bio->bi_size, bio->bi_rw);
  944. goto out_unlock_rcu;
  945. }
  946. }
  947. /*
  948. * Either group has not been allocated yet or it is not an unlimited
  949. * IO group
  950. */
  951. spin_lock_irq(q->queue_lock);
  952. tg = throtl_lookup_create_tg(td, blkcg);
  953. if (unlikely(!tg))
  954. goto out_unlock;
  955. sq = &tg->service_queue;
  956. /* throtl is FIFO - if other bios are already queued, should queue */
  957. if (sq->nr_queued[rw])
  958. goto queue_bio;
  959. /* Bio is with-in rate limit of group */
  960. if (tg_may_dispatch(tg, bio, NULL)) {
  961. throtl_charge_bio(tg, bio);
  962. /*
  963. * We need to trim slice even when bios are not being queued
  964. * otherwise it might happen that a bio is not queued for
  965. * a long time and slice keeps on extending and trim is not
  966. * called for a long time. Now if limits are reduced suddenly
  967. * we take into account all the IO dispatched so far at new
  968. * low rate and * newly queued IO gets a really long dispatch
  969. * time.
  970. *
  971. * So keep on trimming slice even if bio is not queued.
  972. */
  973. throtl_trim_slice(tg, rw);
  974. goto out_unlock;
  975. }
  976. queue_bio:
  977. throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
  978. rw == READ ? 'R' : 'W',
  979. tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
  980. tg->io_disp[rw], tg->iops[rw],
  981. sq->nr_queued[READ], sq->nr_queued[WRITE]);
  982. bio_associate_current(bio);
  983. throtl_add_bio_tg(bio, tg);
  984. throttled = true;
  985. /* update @tg's dispatch time if @tg was empty before @bio */
  986. if (tg->flags & THROTL_TG_WAS_EMPTY) {
  987. tg_update_disptime(tg);
  988. throtl_schedule_next_dispatch(td);
  989. }
  990. out_unlock:
  991. spin_unlock_irq(q->queue_lock);
  992. out_unlock_rcu:
  993. rcu_read_unlock();
  994. out:
  995. /*
  996. * As multiple blk-throtls may stack in the same issue path, we
  997. * don't want bios to leave with the flag set. Clear the flag if
  998. * being issued.
  999. */
  1000. if (!throttled)
  1001. bio->bi_rw &= ~REQ_THROTTLED;
  1002. return throttled;
  1003. }
  1004. /**
  1005. * blk_throtl_drain - drain throttled bios
  1006. * @q: request_queue to drain throttled bios for
  1007. *
  1008. * Dispatch all currently throttled bios on @q through ->make_request_fn().
  1009. */
  1010. void blk_throtl_drain(struct request_queue *q)
  1011. __releases(q->queue_lock) __acquires(q->queue_lock)
  1012. {
  1013. struct throtl_data *td = q->td;
  1014. struct throtl_service_queue *parent_sq = &td->service_queue;
  1015. struct throtl_grp *tg;
  1016. struct bio *bio;
  1017. int rw;
  1018. queue_lockdep_assert_held(q);
  1019. while ((tg = throtl_rb_first(parent_sq))) {
  1020. struct throtl_service_queue *sq = &tg->service_queue;
  1021. throtl_dequeue_tg(tg);
  1022. while ((bio = bio_list_peek(&sq->bio_lists[READ])))
  1023. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  1024. while ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
  1025. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  1026. }
  1027. spin_unlock_irq(q->queue_lock);
  1028. for (rw = READ; rw <= WRITE; rw++)
  1029. while ((bio = bio_list_pop(&parent_sq->bio_lists[rw])))
  1030. generic_make_request(bio);
  1031. spin_lock_irq(q->queue_lock);
  1032. }
  1033. int blk_throtl_init(struct request_queue *q)
  1034. {
  1035. struct throtl_data *td;
  1036. int ret;
  1037. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  1038. if (!td)
  1039. return -ENOMEM;
  1040. INIT_DELAYED_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
  1041. throtl_service_queue_init(&td->service_queue, NULL);
  1042. q->td = td;
  1043. td->queue = q;
  1044. /* activate policy */
  1045. ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
  1046. if (ret)
  1047. kfree(td);
  1048. return ret;
  1049. }
  1050. void blk_throtl_exit(struct request_queue *q)
  1051. {
  1052. BUG_ON(!q->td);
  1053. throtl_shutdown_wq(q);
  1054. blkcg_deactivate_policy(q, &blkcg_policy_throtl);
  1055. kfree(q->td);
  1056. }
  1057. static int __init throtl_init(void)
  1058. {
  1059. kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
  1060. if (!kthrotld_workqueue)
  1061. panic("Failed to create kthrotld\n");
  1062. return blkcg_policy_register(&blkcg_policy_throtl);
  1063. }
  1064. module_init(throtl_init);