blk-throttle.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  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. /*
  23. * To implement hierarchical throttling, throtl_grps form a tree and bios
  24. * are dispatched upwards level by level until they reach the top and get
  25. * issued. When dispatching bios from the children and local group at each
  26. * level, if the bios are dispatched into a single bio_list, there's a risk
  27. * of a local or child group which can queue many bios at once filling up
  28. * the list starving others.
  29. *
  30. * To avoid such starvation, dispatched bios are queued separately
  31. * according to where they came from. When they are again dispatched to
  32. * the parent, they're popped in round-robin order so that no single source
  33. * hogs the dispatch window.
  34. *
  35. * throtl_qnode is used to keep the queued bios separated by their sources.
  36. * Bios are queued to throtl_qnode which in turn is queued to
  37. * throtl_service_queue and then dispatched in round-robin order.
  38. *
  39. * It's also used to track the reference counts on blkg's. A qnode always
  40. * belongs to a throtl_grp and gets queued on itself or the parent, so
  41. * incrementing the reference of the associated throtl_grp when a qnode is
  42. * queued and decrementing when dequeued is enough to keep the whole blkg
  43. * tree pinned while bios are in flight.
  44. */
  45. struct throtl_qnode {
  46. struct list_head node; /* service_queue->queued[] */
  47. struct bio_list bios; /* queued bios */
  48. struct throtl_grp *tg; /* tg this qnode belongs to */
  49. };
  50. struct throtl_service_queue {
  51. struct throtl_service_queue *parent_sq; /* the parent service_queue */
  52. /*
  53. * Bios queued directly to this service_queue or dispatched from
  54. * children throtl_grp's.
  55. */
  56. struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */
  57. unsigned int nr_queued[2]; /* number of queued bios */
  58. /*
  59. * RB tree of active children throtl_grp's, which are sorted by
  60. * their ->disptime.
  61. */
  62. struct rb_root pending_tree; /* RB tree of active tgs */
  63. struct rb_node *first_pending; /* first node in the tree */
  64. unsigned int nr_pending; /* # queued in the tree */
  65. unsigned long first_pending_disptime; /* disptime of the first tg */
  66. struct timer_list pending_timer; /* fires on first_pending_disptime */
  67. };
  68. enum tg_state_flags {
  69. THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
  70. THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
  71. };
  72. #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
  73. /* Per-cpu group stats */
  74. struct tg_stats_cpu {
  75. /* total bytes transferred */
  76. struct blkg_rwstat service_bytes;
  77. /* total IOs serviced, post merge */
  78. struct blkg_rwstat serviced;
  79. };
  80. struct throtl_grp {
  81. /* must be the first member */
  82. struct blkg_policy_data pd;
  83. /* active throtl group service_queue member */
  84. struct rb_node rb_node;
  85. /* throtl_data this group belongs to */
  86. struct throtl_data *td;
  87. /* this group's service queue */
  88. struct throtl_service_queue service_queue;
  89. /*
  90. * qnode_on_self is used when bios are directly queued to this
  91. * throtl_grp so that local bios compete fairly with bios
  92. * dispatched from children. qnode_on_parent is used when bios are
  93. * dispatched from this throtl_grp into its parent and will compete
  94. * with the sibling qnode_on_parents and the parent's
  95. * qnode_on_self.
  96. */
  97. struct throtl_qnode qnode_on_self[2];
  98. struct throtl_qnode qnode_on_parent[2];
  99. /*
  100. * Dispatch time in jiffies. This is the estimated time when group
  101. * will unthrottle and is ready to dispatch more bio. It is used as
  102. * key to sort active groups in service tree.
  103. */
  104. unsigned long disptime;
  105. unsigned int flags;
  106. /* bytes per second rate limits */
  107. uint64_t bps[2];
  108. /* IOPS limits */
  109. unsigned int iops[2];
  110. /* Number of bytes disptached in current slice */
  111. uint64_t bytes_disp[2];
  112. /* Number of bio's dispatched in current slice */
  113. unsigned int io_disp[2];
  114. /* When did we start a new slice */
  115. unsigned long slice_start[2];
  116. unsigned long slice_end[2];
  117. /* Per cpu stats pointer */
  118. struct tg_stats_cpu __percpu *stats_cpu;
  119. /* List of tgs waiting for per cpu stats memory to be allocated */
  120. struct list_head stats_alloc_node;
  121. };
  122. struct throtl_data
  123. {
  124. /* service tree for active throtl groups */
  125. struct throtl_service_queue service_queue;
  126. struct request_queue *queue;
  127. /* Total Number of queued bios on READ and WRITE lists */
  128. unsigned int nr_queued[2];
  129. /*
  130. * number of total undestroyed groups
  131. */
  132. unsigned int nr_undestroyed_grps;
  133. /* Work for dispatching throttled bios */
  134. struct work_struct dispatch_work;
  135. };
  136. /* list and work item to allocate percpu group stats */
  137. static DEFINE_SPINLOCK(tg_stats_alloc_lock);
  138. static LIST_HEAD(tg_stats_alloc_list);
  139. static void tg_stats_alloc_fn(struct work_struct *);
  140. static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
  141. static void throtl_pending_timer_fn(unsigned long arg);
  142. static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
  143. {
  144. return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
  145. }
  146. static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
  147. {
  148. return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
  149. }
  150. static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
  151. {
  152. return pd_to_blkg(&tg->pd);
  153. }
  154. static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
  155. {
  156. return blkg_to_tg(td->queue->root_blkg);
  157. }
  158. /**
  159. * sq_to_tg - return the throl_grp the specified service queue belongs to
  160. * @sq: the throtl_service_queue of interest
  161. *
  162. * Return the throtl_grp @sq belongs to. If @sq is the top-level one
  163. * embedded in throtl_data, %NULL is returned.
  164. */
  165. static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
  166. {
  167. if (sq && sq->parent_sq)
  168. return container_of(sq, struct throtl_grp, service_queue);
  169. else
  170. return NULL;
  171. }
  172. /**
  173. * sq_to_td - return throtl_data the specified service queue belongs to
  174. * @sq: the throtl_service_queue of interest
  175. *
  176. * A service_queue can be embeded in either a throtl_grp or throtl_data.
  177. * Determine the associated throtl_data accordingly and return it.
  178. */
  179. static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
  180. {
  181. struct throtl_grp *tg = sq_to_tg(sq);
  182. if (tg)
  183. return tg->td;
  184. else
  185. return container_of(sq, struct throtl_data, service_queue);
  186. }
  187. /**
  188. * throtl_log - log debug message via blktrace
  189. * @sq: the service_queue being reported
  190. * @fmt: printf format string
  191. * @args: printf args
  192. *
  193. * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
  194. * throtl_grp; otherwise, just "throtl".
  195. *
  196. * TODO: this should be made a function and name formatting should happen
  197. * after testing whether blktrace is enabled.
  198. */
  199. #define throtl_log(sq, fmt, args...) do { \
  200. struct throtl_grp *__tg = sq_to_tg((sq)); \
  201. struct throtl_data *__td = sq_to_td((sq)); \
  202. \
  203. (void)__td; \
  204. if ((__tg)) { \
  205. char __pbuf[128]; \
  206. \
  207. blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
  208. blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
  209. } else { \
  210. blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
  211. } \
  212. } while (0)
  213. /*
  214. * Worker for allocating per cpu stat for tgs. This is scheduled on the
  215. * system_wq once there are some groups on the alloc_list waiting for
  216. * allocation.
  217. */
  218. static void tg_stats_alloc_fn(struct work_struct *work)
  219. {
  220. static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
  221. struct delayed_work *dwork = to_delayed_work(work);
  222. bool empty = false;
  223. alloc_stats:
  224. if (!stats_cpu) {
  225. stats_cpu = alloc_percpu(struct tg_stats_cpu);
  226. if (!stats_cpu) {
  227. /* allocation failed, try again after some time */
  228. schedule_delayed_work(dwork, msecs_to_jiffies(10));
  229. return;
  230. }
  231. }
  232. spin_lock_irq(&tg_stats_alloc_lock);
  233. if (!list_empty(&tg_stats_alloc_list)) {
  234. struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
  235. struct throtl_grp,
  236. stats_alloc_node);
  237. swap(tg->stats_cpu, stats_cpu);
  238. list_del_init(&tg->stats_alloc_node);
  239. }
  240. empty = list_empty(&tg_stats_alloc_list);
  241. spin_unlock_irq(&tg_stats_alloc_lock);
  242. if (!empty)
  243. goto alloc_stats;
  244. }
  245. static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
  246. {
  247. INIT_LIST_HEAD(&qn->node);
  248. bio_list_init(&qn->bios);
  249. qn->tg = tg;
  250. }
  251. /**
  252. * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
  253. * @bio: bio being added
  254. * @qn: qnode to add bio to
  255. * @queued: the service_queue->queued[] list @qn belongs to
  256. *
  257. * Add @bio to @qn and put @qn on @queued if it's not already on.
  258. * @qn->tg's reference count is bumped when @qn is activated. See the
  259. * comment on top of throtl_qnode definition for details.
  260. */
  261. static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
  262. struct list_head *queued)
  263. {
  264. bio_list_add(&qn->bios, bio);
  265. if (list_empty(&qn->node)) {
  266. list_add_tail(&qn->node, queued);
  267. blkg_get(tg_to_blkg(qn->tg));
  268. }
  269. }
  270. /**
  271. * throtl_peek_queued - peek the first bio on a qnode list
  272. * @queued: the qnode list to peek
  273. */
  274. static struct bio *throtl_peek_queued(struct list_head *queued)
  275. {
  276. struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
  277. struct bio *bio;
  278. if (list_empty(queued))
  279. return NULL;
  280. bio = bio_list_peek(&qn->bios);
  281. WARN_ON_ONCE(!bio);
  282. return bio;
  283. }
  284. /**
  285. * throtl_pop_queued - pop the first bio form a qnode list
  286. * @queued: the qnode list to pop a bio from
  287. * @tg_to_put: optional out argument for throtl_grp to put
  288. *
  289. * Pop the first bio from the qnode list @queued. After popping, the first
  290. * qnode is removed from @queued if empty or moved to the end of @queued so
  291. * that the popping order is round-robin.
  292. *
  293. * When the first qnode is removed, its associated throtl_grp should be put
  294. * too. If @tg_to_put is NULL, this function automatically puts it;
  295. * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
  296. * responsible for putting it.
  297. */
  298. static struct bio *throtl_pop_queued(struct list_head *queued,
  299. struct throtl_grp **tg_to_put)
  300. {
  301. struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
  302. struct bio *bio;
  303. if (list_empty(queued))
  304. return NULL;
  305. bio = bio_list_pop(&qn->bios);
  306. WARN_ON_ONCE(!bio);
  307. if (bio_list_empty(&qn->bios)) {
  308. list_del_init(&qn->node);
  309. if (tg_to_put)
  310. *tg_to_put = qn->tg;
  311. else
  312. blkg_put(tg_to_blkg(qn->tg));
  313. } else {
  314. list_move_tail(&qn->node, queued);
  315. }
  316. return bio;
  317. }
  318. /* init a service_queue, assumes the caller zeroed it */
  319. static void throtl_service_queue_init(struct throtl_service_queue *sq,
  320. struct throtl_service_queue *parent_sq)
  321. {
  322. INIT_LIST_HEAD(&sq->queued[0]);
  323. INIT_LIST_HEAD(&sq->queued[1]);
  324. sq->pending_tree = RB_ROOT;
  325. sq->parent_sq = parent_sq;
  326. setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
  327. (unsigned long)sq);
  328. }
  329. static void throtl_service_queue_exit(struct throtl_service_queue *sq)
  330. {
  331. del_timer_sync(&sq->pending_timer);
  332. }
  333. static void throtl_pd_init(struct blkcg_gq *blkg)
  334. {
  335. struct throtl_grp *tg = blkg_to_tg(blkg);
  336. struct throtl_data *td = blkg->q->td;
  337. unsigned long flags;
  338. int rw;
  339. throtl_service_queue_init(&tg->service_queue, &td->service_queue);
  340. for (rw = READ; rw <= WRITE; rw++) {
  341. throtl_qnode_init(&tg->qnode_on_self[rw], tg);
  342. throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
  343. }
  344. RB_CLEAR_NODE(&tg->rb_node);
  345. tg->td = td;
  346. tg->bps[READ] = -1;
  347. tg->bps[WRITE] = -1;
  348. tg->iops[READ] = -1;
  349. tg->iops[WRITE] = -1;
  350. /*
  351. * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
  352. * but percpu allocator can't be called from IO path. Queue tg on
  353. * tg_stats_alloc_list and allocate from work item.
  354. */
  355. spin_lock_irqsave(&tg_stats_alloc_lock, flags);
  356. list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
  357. schedule_delayed_work(&tg_stats_alloc_work, 0);
  358. spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
  359. }
  360. static void throtl_pd_exit(struct blkcg_gq *blkg)
  361. {
  362. struct throtl_grp *tg = blkg_to_tg(blkg);
  363. unsigned long flags;
  364. spin_lock_irqsave(&tg_stats_alloc_lock, flags);
  365. list_del_init(&tg->stats_alloc_node);
  366. spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
  367. free_percpu(tg->stats_cpu);
  368. throtl_service_queue_exit(&tg->service_queue);
  369. }
  370. static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
  371. {
  372. struct throtl_grp *tg = blkg_to_tg(blkg);
  373. int cpu;
  374. if (tg->stats_cpu == NULL)
  375. return;
  376. for_each_possible_cpu(cpu) {
  377. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  378. blkg_rwstat_reset(&sc->service_bytes);
  379. blkg_rwstat_reset(&sc->serviced);
  380. }
  381. }
  382. static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
  383. struct blkcg *blkcg)
  384. {
  385. /*
  386. * This is the common case when there are no blkcgs. Avoid lookup
  387. * in this case
  388. */
  389. if (blkcg == &blkcg_root)
  390. return td_root_tg(td);
  391. return blkg_to_tg(blkg_lookup(blkcg, td->queue));
  392. }
  393. static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
  394. struct blkcg *blkcg)
  395. {
  396. struct request_queue *q = td->queue;
  397. struct throtl_grp *tg = NULL;
  398. /*
  399. * This is the common case when there are no blkcgs. Avoid lookup
  400. * in this case
  401. */
  402. if (blkcg == &blkcg_root) {
  403. tg = td_root_tg(td);
  404. } else {
  405. struct blkcg_gq *blkg;
  406. blkg = blkg_lookup_create(blkcg, q);
  407. /* if %NULL and @q is alive, fall back to root_tg */
  408. if (!IS_ERR(blkg))
  409. tg = blkg_to_tg(blkg);
  410. else if (!blk_queue_dying(q))
  411. tg = td_root_tg(td);
  412. }
  413. return tg;
  414. }
  415. static struct throtl_grp *
  416. throtl_rb_first(struct throtl_service_queue *parent_sq)
  417. {
  418. /* Service tree is empty */
  419. if (!parent_sq->nr_pending)
  420. return NULL;
  421. if (!parent_sq->first_pending)
  422. parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
  423. if (parent_sq->first_pending)
  424. return rb_entry_tg(parent_sq->first_pending);
  425. return NULL;
  426. }
  427. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  428. {
  429. rb_erase(n, root);
  430. RB_CLEAR_NODE(n);
  431. }
  432. static void throtl_rb_erase(struct rb_node *n,
  433. struct throtl_service_queue *parent_sq)
  434. {
  435. if (parent_sq->first_pending == n)
  436. parent_sq->first_pending = NULL;
  437. rb_erase_init(n, &parent_sq->pending_tree);
  438. --parent_sq->nr_pending;
  439. }
  440. static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
  441. {
  442. struct throtl_grp *tg;
  443. tg = throtl_rb_first(parent_sq);
  444. if (!tg)
  445. return;
  446. parent_sq->first_pending_disptime = tg->disptime;
  447. }
  448. static void tg_service_queue_add(struct throtl_grp *tg)
  449. {
  450. struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
  451. struct rb_node **node = &parent_sq->pending_tree.rb_node;
  452. struct rb_node *parent = NULL;
  453. struct throtl_grp *__tg;
  454. unsigned long key = tg->disptime;
  455. int left = 1;
  456. while (*node != NULL) {
  457. parent = *node;
  458. __tg = rb_entry_tg(parent);
  459. if (time_before(key, __tg->disptime))
  460. node = &parent->rb_left;
  461. else {
  462. node = &parent->rb_right;
  463. left = 0;
  464. }
  465. }
  466. if (left)
  467. parent_sq->first_pending = &tg->rb_node;
  468. rb_link_node(&tg->rb_node, parent, node);
  469. rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
  470. }
  471. static void __throtl_enqueue_tg(struct throtl_grp *tg)
  472. {
  473. tg_service_queue_add(tg);
  474. tg->flags |= THROTL_TG_PENDING;
  475. tg->service_queue.parent_sq->nr_pending++;
  476. }
  477. static void throtl_enqueue_tg(struct throtl_grp *tg)
  478. {
  479. if (!(tg->flags & THROTL_TG_PENDING))
  480. __throtl_enqueue_tg(tg);
  481. }
  482. static void __throtl_dequeue_tg(struct throtl_grp *tg)
  483. {
  484. throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
  485. tg->flags &= ~THROTL_TG_PENDING;
  486. }
  487. static void throtl_dequeue_tg(struct throtl_grp *tg)
  488. {
  489. if (tg->flags & THROTL_TG_PENDING)
  490. __throtl_dequeue_tg(tg);
  491. }
  492. /* Call with queue lock held */
  493. static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
  494. unsigned long expires)
  495. {
  496. mod_timer(&sq->pending_timer, expires);
  497. throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
  498. expires - jiffies, jiffies);
  499. }
  500. /**
  501. * throtl_schedule_next_dispatch - schedule the next dispatch cycle
  502. * @sq: the service_queue to schedule dispatch for
  503. * @force: force scheduling
  504. *
  505. * Arm @sq->pending_timer so that the next dispatch cycle starts on the
  506. * dispatch time of the first pending child. Returns %true if either timer
  507. * is armed or there's no pending child left. %false if the current
  508. * dispatch window is still open and the caller should continue
  509. * dispatching.
  510. *
  511. * If @force is %true, the dispatch timer is always scheduled and this
  512. * function is guaranteed to return %true. This is to be used when the
  513. * caller can't dispatch itself and needs to invoke pending_timer
  514. * unconditionally. Note that forced scheduling is likely to induce short
  515. * delay before dispatch starts even if @sq->first_pending_disptime is not
  516. * in the future and thus shouldn't be used in hot paths.
  517. */
  518. static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
  519. bool force)
  520. {
  521. /* any pending children left? */
  522. if (!sq->nr_pending)
  523. return true;
  524. update_min_dispatch_time(sq);
  525. /* is the next dispatch time in the future? */
  526. if (force || time_after(sq->first_pending_disptime, jiffies)) {
  527. throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
  528. return true;
  529. }
  530. /* tell the caller to continue dispatching */
  531. return false;
  532. }
  533. static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
  534. bool rw, unsigned long start)
  535. {
  536. tg->bytes_disp[rw] = 0;
  537. tg->io_disp[rw] = 0;
  538. /*
  539. * Previous slice has expired. We must have trimmed it after last
  540. * bio dispatch. That means since start of last slice, we never used
  541. * that bandwidth. Do try to make use of that bandwidth while giving
  542. * credit.
  543. */
  544. if (time_after_eq(start, tg->slice_start[rw]))
  545. tg->slice_start[rw] = start;
  546. tg->slice_end[rw] = jiffies + throtl_slice;
  547. throtl_log(&tg->service_queue,
  548. "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
  549. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  550. tg->slice_end[rw], jiffies);
  551. }
  552. static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
  553. {
  554. tg->bytes_disp[rw] = 0;
  555. tg->io_disp[rw] = 0;
  556. tg->slice_start[rw] = jiffies;
  557. tg->slice_end[rw] = jiffies + throtl_slice;
  558. throtl_log(&tg->service_queue,
  559. "[%c] new slice start=%lu end=%lu jiffies=%lu",
  560. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  561. tg->slice_end[rw], jiffies);
  562. }
  563. static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
  564. unsigned long jiffy_end)
  565. {
  566. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  567. }
  568. static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
  569. unsigned long jiffy_end)
  570. {
  571. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  572. throtl_log(&tg->service_queue,
  573. "[%c] extend slice start=%lu end=%lu jiffies=%lu",
  574. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  575. tg->slice_end[rw], jiffies);
  576. }
  577. /* Determine if previously allocated or extended slice is complete or not */
  578. static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
  579. {
  580. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  581. return 0;
  582. return 1;
  583. }
  584. /* Trim the used slices and adjust slice start accordingly */
  585. static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
  586. {
  587. unsigned long nr_slices, time_elapsed, io_trim;
  588. u64 bytes_trim, tmp;
  589. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  590. /*
  591. * If bps are unlimited (-1), then time slice don't get
  592. * renewed. Don't try to trim the slice if slice is used. A new
  593. * slice will start when appropriate.
  594. */
  595. if (throtl_slice_used(tg, rw))
  596. return;
  597. /*
  598. * A bio has been dispatched. Also adjust slice_end. It might happen
  599. * that initially cgroup limit was very low resulting in high
  600. * slice_end, but later limit was bumped up and bio was dispached
  601. * sooner, then we need to reduce slice_end. A high bogus slice_end
  602. * is bad because it does not allow new slice to start.
  603. */
  604. throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
  605. time_elapsed = jiffies - tg->slice_start[rw];
  606. nr_slices = time_elapsed / throtl_slice;
  607. if (!nr_slices)
  608. return;
  609. tmp = tg->bps[rw] * throtl_slice * nr_slices;
  610. do_div(tmp, HZ);
  611. bytes_trim = tmp;
  612. io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
  613. if (!bytes_trim && !io_trim)
  614. return;
  615. if (tg->bytes_disp[rw] >= bytes_trim)
  616. tg->bytes_disp[rw] -= bytes_trim;
  617. else
  618. tg->bytes_disp[rw] = 0;
  619. if (tg->io_disp[rw] >= io_trim)
  620. tg->io_disp[rw] -= io_trim;
  621. else
  622. tg->io_disp[rw] = 0;
  623. tg->slice_start[rw] += nr_slices * throtl_slice;
  624. throtl_log(&tg->service_queue,
  625. "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
  626. rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
  627. tg->slice_start[rw], tg->slice_end[rw], jiffies);
  628. }
  629. static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
  630. unsigned long *wait)
  631. {
  632. bool rw = bio_data_dir(bio);
  633. unsigned int io_allowed;
  634. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  635. u64 tmp;
  636. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  637. /* Slice has just started. Consider one slice interval */
  638. if (!jiffy_elapsed)
  639. jiffy_elapsed_rnd = throtl_slice;
  640. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  641. /*
  642. * jiffy_elapsed_rnd should not be a big value as minimum iops can be
  643. * 1 then at max jiffy elapsed should be equivalent of 1 second as we
  644. * will allow dispatch after 1 second and after that slice should
  645. * have been trimmed.
  646. */
  647. tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
  648. do_div(tmp, HZ);
  649. if (tmp > UINT_MAX)
  650. io_allowed = UINT_MAX;
  651. else
  652. io_allowed = tmp;
  653. if (tg->io_disp[rw] + 1 <= io_allowed) {
  654. if (wait)
  655. *wait = 0;
  656. return 1;
  657. }
  658. /* Calc approx time to dispatch */
  659. jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
  660. if (jiffy_wait > jiffy_elapsed)
  661. jiffy_wait = jiffy_wait - jiffy_elapsed;
  662. else
  663. jiffy_wait = 1;
  664. if (wait)
  665. *wait = jiffy_wait;
  666. return 0;
  667. }
  668. static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
  669. unsigned long *wait)
  670. {
  671. bool rw = bio_data_dir(bio);
  672. u64 bytes_allowed, extra_bytes, tmp;
  673. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  674. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  675. /* Slice has just started. Consider one slice interval */
  676. if (!jiffy_elapsed)
  677. jiffy_elapsed_rnd = throtl_slice;
  678. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  679. tmp = tg->bps[rw] * jiffy_elapsed_rnd;
  680. do_div(tmp, HZ);
  681. bytes_allowed = tmp;
  682. if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
  683. if (wait)
  684. *wait = 0;
  685. return 1;
  686. }
  687. /* Calc approx time to dispatch */
  688. extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
  689. jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
  690. if (!jiffy_wait)
  691. jiffy_wait = 1;
  692. /*
  693. * This wait time is without taking into consideration the rounding
  694. * up we did. Add that time also.
  695. */
  696. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  697. if (wait)
  698. *wait = jiffy_wait;
  699. return 0;
  700. }
  701. static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
  702. if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
  703. return 1;
  704. return 0;
  705. }
  706. /*
  707. * Returns whether one can dispatch a bio or not. Also returns approx number
  708. * of jiffies to wait before this bio is with-in IO rate and can be dispatched
  709. */
  710. static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
  711. unsigned long *wait)
  712. {
  713. bool rw = bio_data_dir(bio);
  714. unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
  715. /*
  716. * Currently whole state machine of group depends on first bio
  717. * queued in the group bio list. So one should not be calling
  718. * this function with a different bio if there are other bios
  719. * queued.
  720. */
  721. BUG_ON(tg->service_queue.nr_queued[rw] &&
  722. bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
  723. /* If tg->bps = -1, then BW is unlimited */
  724. if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
  725. if (wait)
  726. *wait = 0;
  727. return 1;
  728. }
  729. /*
  730. * If previous slice expired, start a new one otherwise renew/extend
  731. * existing slice to make sure it is at least throtl_slice interval
  732. * long since now.
  733. */
  734. if (throtl_slice_used(tg, rw))
  735. throtl_start_new_slice(tg, rw);
  736. else {
  737. if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
  738. throtl_extend_slice(tg, rw, jiffies + throtl_slice);
  739. }
  740. if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
  741. tg_with_in_iops_limit(tg, bio, &iops_wait)) {
  742. if (wait)
  743. *wait = 0;
  744. return 1;
  745. }
  746. max_wait = max(bps_wait, iops_wait);
  747. if (wait)
  748. *wait = max_wait;
  749. if (time_before(tg->slice_end[rw], jiffies + max_wait))
  750. throtl_extend_slice(tg, rw, jiffies + max_wait);
  751. return 0;
  752. }
  753. static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
  754. int rw)
  755. {
  756. struct throtl_grp *tg = blkg_to_tg(blkg);
  757. struct tg_stats_cpu *stats_cpu;
  758. unsigned long flags;
  759. /* If per cpu stats are not allocated yet, don't do any accounting. */
  760. if (tg->stats_cpu == NULL)
  761. return;
  762. /*
  763. * Disabling interrupts to provide mutual exclusion between two
  764. * writes on same cpu. It probably is not needed for 64bit. Not
  765. * optimizing that case yet.
  766. */
  767. local_irq_save(flags);
  768. stats_cpu = this_cpu_ptr(tg->stats_cpu);
  769. blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
  770. blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
  771. local_irq_restore(flags);
  772. }
  773. static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
  774. {
  775. bool rw = bio_data_dir(bio);
  776. /* Charge the bio to the group */
  777. tg->bytes_disp[rw] += bio->bi_size;
  778. tg->io_disp[rw]++;
  779. /*
  780. * REQ_THROTTLED is used to prevent the same bio to be throttled
  781. * more than once as a throttled bio will go through blk-throtl the
  782. * second time when it eventually gets issued. Set it when a bio
  783. * is being charged to a tg.
  784. *
  785. * Dispatch stats aren't recursive and each @bio should only be
  786. * accounted by the @tg it was originally associated with. Let's
  787. * update the stats when setting REQ_THROTTLED for the first time
  788. * which is guaranteed to be for the @bio's original tg.
  789. */
  790. if (!(bio->bi_rw & REQ_THROTTLED)) {
  791. bio->bi_rw |= REQ_THROTTLED;
  792. throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size,
  793. bio->bi_rw);
  794. }
  795. }
  796. /**
  797. * throtl_add_bio_tg - add a bio to the specified throtl_grp
  798. * @bio: bio to add
  799. * @qn: qnode to use
  800. * @tg: the target throtl_grp
  801. *
  802. * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
  803. * tg->qnode_on_self[] is used.
  804. */
  805. static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
  806. struct throtl_grp *tg)
  807. {
  808. struct throtl_service_queue *sq = &tg->service_queue;
  809. bool rw = bio_data_dir(bio);
  810. if (!qn)
  811. qn = &tg->qnode_on_self[rw];
  812. /*
  813. * If @tg doesn't currently have any bios queued in the same
  814. * direction, queueing @bio can change when @tg should be
  815. * dispatched. Mark that @tg was empty. This is automatically
  816. * cleaered on the next tg_update_disptime().
  817. */
  818. if (!sq->nr_queued[rw])
  819. tg->flags |= THROTL_TG_WAS_EMPTY;
  820. throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
  821. sq->nr_queued[rw]++;
  822. throtl_enqueue_tg(tg);
  823. }
  824. static void tg_update_disptime(struct throtl_grp *tg)
  825. {
  826. struct throtl_service_queue *sq = &tg->service_queue;
  827. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  828. struct bio *bio;
  829. if ((bio = throtl_peek_queued(&sq->queued[READ])))
  830. tg_may_dispatch(tg, bio, &read_wait);
  831. if ((bio = throtl_peek_queued(&sq->queued[WRITE])))
  832. tg_may_dispatch(tg, bio, &write_wait);
  833. min_wait = min(read_wait, write_wait);
  834. disptime = jiffies + min_wait;
  835. /* Update dispatch time */
  836. throtl_dequeue_tg(tg);
  837. tg->disptime = disptime;
  838. throtl_enqueue_tg(tg);
  839. /* see throtl_add_bio_tg() */
  840. tg->flags &= ~THROTL_TG_WAS_EMPTY;
  841. }
  842. static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
  843. struct throtl_grp *parent_tg, bool rw)
  844. {
  845. if (throtl_slice_used(parent_tg, rw)) {
  846. throtl_start_new_slice_with_credit(parent_tg, rw,
  847. child_tg->slice_start[rw]);
  848. }
  849. }
  850. static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
  851. {
  852. struct throtl_service_queue *sq = &tg->service_queue;
  853. struct throtl_service_queue *parent_sq = sq->parent_sq;
  854. struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
  855. struct throtl_grp *tg_to_put = NULL;
  856. struct bio *bio;
  857. /*
  858. * @bio is being transferred from @tg to @parent_sq. Popping a bio
  859. * from @tg may put its reference and @parent_sq might end up
  860. * getting released prematurely. Remember the tg to put and put it
  861. * after @bio is transferred to @parent_sq.
  862. */
  863. bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
  864. sq->nr_queued[rw]--;
  865. throtl_charge_bio(tg, bio);
  866. /*
  867. * If our parent is another tg, we just need to transfer @bio to
  868. * the parent using throtl_add_bio_tg(). If our parent is
  869. * @td->service_queue, @bio is ready to be issued. Put it on its
  870. * bio_lists[] and decrease total number queued. The caller is
  871. * responsible for issuing these bios.
  872. */
  873. if (parent_tg) {
  874. throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
  875. start_parent_slice_with_credit(tg, parent_tg, rw);
  876. } else {
  877. throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
  878. &parent_sq->queued[rw]);
  879. BUG_ON(tg->td->nr_queued[rw] <= 0);
  880. tg->td->nr_queued[rw]--;
  881. }
  882. throtl_trim_slice(tg, rw);
  883. if (tg_to_put)
  884. blkg_put(tg_to_blkg(tg_to_put));
  885. }
  886. static int throtl_dispatch_tg(struct throtl_grp *tg)
  887. {
  888. struct throtl_service_queue *sq = &tg->service_queue;
  889. unsigned int nr_reads = 0, nr_writes = 0;
  890. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  891. unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
  892. struct bio *bio;
  893. /* Try to dispatch 75% READS and 25% WRITES */
  894. while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
  895. tg_may_dispatch(tg, bio, NULL)) {
  896. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  897. nr_reads++;
  898. if (nr_reads >= max_nr_reads)
  899. break;
  900. }
  901. while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
  902. tg_may_dispatch(tg, bio, NULL)) {
  903. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  904. nr_writes++;
  905. if (nr_writes >= max_nr_writes)
  906. break;
  907. }
  908. return nr_reads + nr_writes;
  909. }
  910. static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
  911. {
  912. unsigned int nr_disp = 0;
  913. while (1) {
  914. struct throtl_grp *tg = throtl_rb_first(parent_sq);
  915. struct throtl_service_queue *sq = &tg->service_queue;
  916. if (!tg)
  917. break;
  918. if (time_before(jiffies, tg->disptime))
  919. break;
  920. throtl_dequeue_tg(tg);
  921. nr_disp += throtl_dispatch_tg(tg);
  922. if (sq->nr_queued[0] || sq->nr_queued[1])
  923. tg_update_disptime(tg);
  924. if (nr_disp >= throtl_quantum)
  925. break;
  926. }
  927. return nr_disp;
  928. }
  929. /**
  930. * throtl_pending_timer_fn - timer function for service_queue->pending_timer
  931. * @arg: the throtl_service_queue being serviced
  932. *
  933. * This timer is armed when a child throtl_grp with active bio's become
  934. * pending and queued on the service_queue's pending_tree and expires when
  935. * the first child throtl_grp should be dispatched. This function
  936. * dispatches bio's from the children throtl_grps to the parent
  937. * service_queue.
  938. *
  939. * If the parent's parent is another throtl_grp, dispatching is propagated
  940. * by either arming its pending_timer or repeating dispatch directly. If
  941. * the top-level service_tree is reached, throtl_data->dispatch_work is
  942. * kicked so that the ready bio's are issued.
  943. */
  944. static void throtl_pending_timer_fn(unsigned long arg)
  945. {
  946. struct throtl_service_queue *sq = (void *)arg;
  947. struct throtl_grp *tg = sq_to_tg(sq);
  948. struct throtl_data *td = sq_to_td(sq);
  949. struct request_queue *q = td->queue;
  950. struct throtl_service_queue *parent_sq;
  951. bool dispatched;
  952. int ret;
  953. spin_lock_irq(q->queue_lock);
  954. again:
  955. parent_sq = sq->parent_sq;
  956. dispatched = false;
  957. while (true) {
  958. throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
  959. sq->nr_queued[READ] + sq->nr_queued[WRITE],
  960. sq->nr_queued[READ], sq->nr_queued[WRITE]);
  961. ret = throtl_select_dispatch(sq);
  962. if (ret) {
  963. throtl_log(sq, "bios disp=%u", ret);
  964. dispatched = true;
  965. }
  966. if (throtl_schedule_next_dispatch(sq, false))
  967. break;
  968. /* this dispatch windows is still open, relax and repeat */
  969. spin_unlock_irq(q->queue_lock);
  970. cpu_relax();
  971. spin_lock_irq(q->queue_lock);
  972. }
  973. if (!dispatched)
  974. goto out_unlock;
  975. if (parent_sq) {
  976. /* @parent_sq is another throl_grp, propagate dispatch */
  977. if (tg->flags & THROTL_TG_WAS_EMPTY) {
  978. tg_update_disptime(tg);
  979. if (!throtl_schedule_next_dispatch(parent_sq, false)) {
  980. /* window is already open, repeat dispatching */
  981. sq = parent_sq;
  982. tg = sq_to_tg(sq);
  983. goto again;
  984. }
  985. }
  986. } else {
  987. /* reached the top-level, queue issueing */
  988. queue_work(kthrotld_workqueue, &td->dispatch_work);
  989. }
  990. out_unlock:
  991. spin_unlock_irq(q->queue_lock);
  992. }
  993. /**
  994. * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
  995. * @work: work item being executed
  996. *
  997. * This function is queued for execution when bio's reach the bio_lists[]
  998. * of throtl_data->service_queue. Those bio's are ready and issued by this
  999. * function.
  1000. */
  1001. void blk_throtl_dispatch_work_fn(struct work_struct *work)
  1002. {
  1003. struct throtl_data *td = container_of(work, struct throtl_data,
  1004. dispatch_work);
  1005. struct throtl_service_queue *td_sq = &td->service_queue;
  1006. struct request_queue *q = td->queue;
  1007. struct bio_list bio_list_on_stack;
  1008. struct bio *bio;
  1009. struct blk_plug plug;
  1010. int rw;
  1011. bio_list_init(&bio_list_on_stack);
  1012. spin_lock_irq(q->queue_lock);
  1013. for (rw = READ; rw <= WRITE; rw++)
  1014. while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
  1015. bio_list_add(&bio_list_on_stack, bio);
  1016. spin_unlock_irq(q->queue_lock);
  1017. if (!bio_list_empty(&bio_list_on_stack)) {
  1018. blk_start_plug(&plug);
  1019. while((bio = bio_list_pop(&bio_list_on_stack)))
  1020. generic_make_request(bio);
  1021. blk_finish_plug(&plug);
  1022. }
  1023. }
  1024. static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
  1025. struct blkg_policy_data *pd, int off)
  1026. {
  1027. struct throtl_grp *tg = pd_to_tg(pd);
  1028. struct blkg_rwstat rwstat = { }, tmp;
  1029. int i, cpu;
  1030. for_each_possible_cpu(cpu) {
  1031. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  1032. tmp = blkg_rwstat_read((void *)sc + off);
  1033. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  1034. rwstat.cnt[i] += tmp.cnt[i];
  1035. }
  1036. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  1037. }
  1038. static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
  1039. struct seq_file *sf)
  1040. {
  1041. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  1042. blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
  1043. cft->private, true);
  1044. return 0;
  1045. }
  1046. static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
  1047. int off)
  1048. {
  1049. struct throtl_grp *tg = pd_to_tg(pd);
  1050. u64 v = *(u64 *)((void *)tg + off);
  1051. if (v == -1)
  1052. return 0;
  1053. return __blkg_prfill_u64(sf, pd, v);
  1054. }
  1055. static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
  1056. int off)
  1057. {
  1058. struct throtl_grp *tg = pd_to_tg(pd);
  1059. unsigned int v = *(unsigned int *)((void *)tg + off);
  1060. if (v == -1)
  1061. return 0;
  1062. return __blkg_prfill_u64(sf, pd, v);
  1063. }
  1064. static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  1065. struct seq_file *sf)
  1066. {
  1067. blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
  1068. &blkcg_policy_throtl, cft->private, false);
  1069. return 0;
  1070. }
  1071. static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  1072. struct seq_file *sf)
  1073. {
  1074. blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
  1075. &blkcg_policy_throtl, cft->private, false);
  1076. return 0;
  1077. }
  1078. static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
  1079. bool is_u64)
  1080. {
  1081. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  1082. struct blkg_conf_ctx ctx;
  1083. struct throtl_grp *tg;
  1084. struct throtl_service_queue *sq;
  1085. int ret;
  1086. ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
  1087. if (ret)
  1088. return ret;
  1089. tg = blkg_to_tg(ctx.blkg);
  1090. sq = &tg->service_queue;
  1091. if (!ctx.v)
  1092. ctx.v = -1;
  1093. if (is_u64)
  1094. *(u64 *)((void *)tg + cft->private) = ctx.v;
  1095. else
  1096. *(unsigned int *)((void *)tg + cft->private) = ctx.v;
  1097. throtl_log(&tg->service_queue,
  1098. "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
  1099. tg->bps[READ], tg->bps[WRITE],
  1100. tg->iops[READ], tg->iops[WRITE]);
  1101. /*
  1102. * We're already holding queue_lock and know @tg is valid. Let's
  1103. * apply the new config directly.
  1104. *
  1105. * Restart the slices for both READ and WRITES. It might happen
  1106. * that a group's limit are dropped suddenly and we don't want to
  1107. * account recently dispatched IO with new low rate.
  1108. */
  1109. throtl_start_new_slice(tg, 0);
  1110. throtl_start_new_slice(tg, 1);
  1111. if (tg->flags & THROTL_TG_PENDING) {
  1112. tg_update_disptime(tg);
  1113. throtl_schedule_next_dispatch(sq->parent_sq, true);
  1114. }
  1115. blkg_conf_finish(&ctx);
  1116. return 0;
  1117. }
  1118. static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  1119. const char *buf)
  1120. {
  1121. return tg_set_conf(cgrp, cft, buf, true);
  1122. }
  1123. static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  1124. const char *buf)
  1125. {
  1126. return tg_set_conf(cgrp, cft, buf, false);
  1127. }
  1128. static struct cftype throtl_files[] = {
  1129. {
  1130. .name = "throttle.read_bps_device",
  1131. .private = offsetof(struct throtl_grp, bps[READ]),
  1132. .read_seq_string = tg_print_conf_u64,
  1133. .write_string = tg_set_conf_u64,
  1134. .max_write_len = 256,
  1135. },
  1136. {
  1137. .name = "throttle.write_bps_device",
  1138. .private = offsetof(struct throtl_grp, bps[WRITE]),
  1139. .read_seq_string = tg_print_conf_u64,
  1140. .write_string = tg_set_conf_u64,
  1141. .max_write_len = 256,
  1142. },
  1143. {
  1144. .name = "throttle.read_iops_device",
  1145. .private = offsetof(struct throtl_grp, iops[READ]),
  1146. .read_seq_string = tg_print_conf_uint,
  1147. .write_string = tg_set_conf_uint,
  1148. .max_write_len = 256,
  1149. },
  1150. {
  1151. .name = "throttle.write_iops_device",
  1152. .private = offsetof(struct throtl_grp, iops[WRITE]),
  1153. .read_seq_string = tg_print_conf_uint,
  1154. .write_string = tg_set_conf_uint,
  1155. .max_write_len = 256,
  1156. },
  1157. {
  1158. .name = "throttle.io_service_bytes",
  1159. .private = offsetof(struct tg_stats_cpu, service_bytes),
  1160. .read_seq_string = tg_print_cpu_rwstat,
  1161. },
  1162. {
  1163. .name = "throttle.io_serviced",
  1164. .private = offsetof(struct tg_stats_cpu, serviced),
  1165. .read_seq_string = tg_print_cpu_rwstat,
  1166. },
  1167. { } /* terminate */
  1168. };
  1169. static void throtl_shutdown_wq(struct request_queue *q)
  1170. {
  1171. struct throtl_data *td = q->td;
  1172. cancel_work_sync(&td->dispatch_work);
  1173. }
  1174. static struct blkcg_policy blkcg_policy_throtl = {
  1175. .pd_size = sizeof(struct throtl_grp),
  1176. .cftypes = throtl_files,
  1177. .pd_init_fn = throtl_pd_init,
  1178. .pd_exit_fn = throtl_pd_exit,
  1179. .pd_reset_stats_fn = throtl_pd_reset_stats,
  1180. };
  1181. bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
  1182. {
  1183. struct throtl_data *td = q->td;
  1184. struct throtl_qnode *qn = NULL;
  1185. struct throtl_grp *tg;
  1186. struct throtl_service_queue *sq;
  1187. bool rw = bio_data_dir(bio);
  1188. struct blkcg *blkcg;
  1189. bool throttled = false;
  1190. /* see throtl_charge_bio() */
  1191. if (bio->bi_rw & REQ_THROTTLED)
  1192. goto out;
  1193. /*
  1194. * A throtl_grp pointer retrieved under rcu can be used to access
  1195. * basic fields like stats and io rates. If a group has no rules,
  1196. * just update the dispatch stats in lockless manner and return.
  1197. */
  1198. rcu_read_lock();
  1199. blkcg = bio_blkcg(bio);
  1200. tg = throtl_lookup_tg(td, blkcg);
  1201. if (tg) {
  1202. if (tg_no_rule_group(tg, rw)) {
  1203. throtl_update_dispatch_stats(tg_to_blkg(tg),
  1204. bio->bi_size, bio->bi_rw);
  1205. goto out_unlock_rcu;
  1206. }
  1207. }
  1208. /*
  1209. * Either group has not been allocated yet or it is not an unlimited
  1210. * IO group
  1211. */
  1212. spin_lock_irq(q->queue_lock);
  1213. tg = throtl_lookup_create_tg(td, blkcg);
  1214. if (unlikely(!tg))
  1215. goto out_unlock;
  1216. sq = &tg->service_queue;
  1217. while (true) {
  1218. /* throtl is FIFO - if bios are already queued, should queue */
  1219. if (sq->nr_queued[rw])
  1220. break;
  1221. /* if above limits, break to queue */
  1222. if (!tg_may_dispatch(tg, bio, NULL))
  1223. break;
  1224. /* within limits, let's charge and dispatch directly */
  1225. throtl_charge_bio(tg, bio);
  1226. /*
  1227. * We need to trim slice even when bios are not being queued
  1228. * otherwise it might happen that a bio is not queued for
  1229. * a long time and slice keeps on extending and trim is not
  1230. * called for a long time. Now if limits are reduced suddenly
  1231. * we take into account all the IO dispatched so far at new
  1232. * low rate and * newly queued IO gets a really long dispatch
  1233. * time.
  1234. *
  1235. * So keep on trimming slice even if bio is not queued.
  1236. */
  1237. throtl_trim_slice(tg, rw);
  1238. /*
  1239. * @bio passed through this layer without being throttled.
  1240. * Climb up the ladder. If we''re already at the top, it
  1241. * can be executed directly.
  1242. */
  1243. qn = &tg->qnode_on_parent[rw];
  1244. sq = sq->parent_sq;
  1245. tg = sq_to_tg(sq);
  1246. if (!tg)
  1247. goto out_unlock;
  1248. }
  1249. /* out-of-limit, queue to @tg */
  1250. throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
  1251. rw == READ ? 'R' : 'W',
  1252. tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
  1253. tg->io_disp[rw], tg->iops[rw],
  1254. sq->nr_queued[READ], sq->nr_queued[WRITE]);
  1255. bio_associate_current(bio);
  1256. tg->td->nr_queued[rw]++;
  1257. throtl_add_bio_tg(bio, qn, tg);
  1258. throttled = true;
  1259. /*
  1260. * Update @tg's dispatch time and force schedule dispatch if @tg
  1261. * was empty before @bio. The forced scheduling isn't likely to
  1262. * cause undue delay as @bio is likely to be dispatched directly if
  1263. * its @tg's disptime is not in the future.
  1264. */
  1265. if (tg->flags & THROTL_TG_WAS_EMPTY) {
  1266. tg_update_disptime(tg);
  1267. throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
  1268. }
  1269. out_unlock:
  1270. spin_unlock_irq(q->queue_lock);
  1271. out_unlock_rcu:
  1272. rcu_read_unlock();
  1273. out:
  1274. /*
  1275. * As multiple blk-throtls may stack in the same issue path, we
  1276. * don't want bios to leave with the flag set. Clear the flag if
  1277. * being issued.
  1278. */
  1279. if (!throttled)
  1280. bio->bi_rw &= ~REQ_THROTTLED;
  1281. return throttled;
  1282. }
  1283. /*
  1284. * Dispatch all bios from all children tg's queued on @parent_sq. On
  1285. * return, @parent_sq is guaranteed to not have any active children tg's
  1286. * and all bios from previously active tg's are on @parent_sq->bio_lists[].
  1287. */
  1288. static void tg_drain_bios(struct throtl_service_queue *parent_sq)
  1289. {
  1290. struct throtl_grp *tg;
  1291. while ((tg = throtl_rb_first(parent_sq))) {
  1292. struct throtl_service_queue *sq = &tg->service_queue;
  1293. struct bio *bio;
  1294. throtl_dequeue_tg(tg);
  1295. while ((bio = throtl_peek_queued(&sq->queued[READ])))
  1296. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  1297. while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
  1298. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  1299. }
  1300. }
  1301. /**
  1302. * blk_throtl_drain - drain throttled bios
  1303. * @q: request_queue to drain throttled bios for
  1304. *
  1305. * Dispatch all currently throttled bios on @q through ->make_request_fn().
  1306. */
  1307. void blk_throtl_drain(struct request_queue *q)
  1308. __releases(q->queue_lock) __acquires(q->queue_lock)
  1309. {
  1310. struct throtl_data *td = q->td;
  1311. struct blkcg_gq *blkg;
  1312. struct cgroup *pos_cgrp;
  1313. struct bio *bio;
  1314. int rw;
  1315. queue_lockdep_assert_held(q);
  1316. rcu_read_lock();
  1317. /*
  1318. * Drain each tg while doing post-order walk on the blkg tree, so
  1319. * that all bios are propagated to td->service_queue. It'd be
  1320. * better to walk service_queue tree directly but blkg walk is
  1321. * easier.
  1322. */
  1323. blkg_for_each_descendant_post(blkg, pos_cgrp, td->queue->root_blkg)
  1324. tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
  1325. tg_drain_bios(&td_root_tg(td)->service_queue);
  1326. /* finally, transfer bios from top-level tg's into the td */
  1327. tg_drain_bios(&td->service_queue);
  1328. rcu_read_unlock();
  1329. spin_unlock_irq(q->queue_lock);
  1330. /* all bios now should be in td->service_queue, issue them */
  1331. for (rw = READ; rw <= WRITE; rw++)
  1332. while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
  1333. NULL)))
  1334. generic_make_request(bio);
  1335. spin_lock_irq(q->queue_lock);
  1336. }
  1337. int blk_throtl_init(struct request_queue *q)
  1338. {
  1339. struct throtl_data *td;
  1340. int ret;
  1341. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  1342. if (!td)
  1343. return -ENOMEM;
  1344. INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
  1345. throtl_service_queue_init(&td->service_queue, NULL);
  1346. q->td = td;
  1347. td->queue = q;
  1348. /* activate policy */
  1349. ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
  1350. if (ret)
  1351. kfree(td);
  1352. return ret;
  1353. }
  1354. void blk_throtl_exit(struct request_queue *q)
  1355. {
  1356. BUG_ON(!q->td);
  1357. throtl_shutdown_wq(q);
  1358. blkcg_deactivate_policy(q, &blkcg_policy_throtl);
  1359. kfree(q->td);
  1360. }
  1361. static int __init throtl_init(void)
  1362. {
  1363. kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
  1364. if (!kthrotld_workqueue)
  1365. panic("Failed to create kthrotld\n");
  1366. return blkcg_policy_register(&blkcg_policy_throtl);
  1367. }
  1368. module_init(throtl_init);