blk-throttle.c 34 KB

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