blk-throttle.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  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(struct throtl_grp *tg, bool rw)
  534. {
  535. tg->bytes_disp[rw] = 0;
  536. tg->io_disp[rw] = 0;
  537. tg->slice_start[rw] = jiffies;
  538. tg->slice_end[rw] = jiffies + throtl_slice;
  539. throtl_log(&tg->service_queue,
  540. "[%c] new slice start=%lu end=%lu jiffies=%lu",
  541. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  542. tg->slice_end[rw], jiffies);
  543. }
  544. static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
  545. unsigned long jiffy_end)
  546. {
  547. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  548. }
  549. static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
  550. unsigned long jiffy_end)
  551. {
  552. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  553. throtl_log(&tg->service_queue,
  554. "[%c] extend slice start=%lu end=%lu jiffies=%lu",
  555. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  556. tg->slice_end[rw], jiffies);
  557. }
  558. /* Determine if previously allocated or extended slice is complete or not */
  559. static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
  560. {
  561. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  562. return 0;
  563. return 1;
  564. }
  565. /* Trim the used slices and adjust slice start accordingly */
  566. static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
  567. {
  568. unsigned long nr_slices, time_elapsed, io_trim;
  569. u64 bytes_trim, tmp;
  570. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  571. /*
  572. * If bps are unlimited (-1), then time slice don't get
  573. * renewed. Don't try to trim the slice if slice is used. A new
  574. * slice will start when appropriate.
  575. */
  576. if (throtl_slice_used(tg, rw))
  577. return;
  578. /*
  579. * A bio has been dispatched. Also adjust slice_end. It might happen
  580. * that initially cgroup limit was very low resulting in high
  581. * slice_end, but later limit was bumped up and bio was dispached
  582. * sooner, then we need to reduce slice_end. A high bogus slice_end
  583. * is bad because it does not allow new slice to start.
  584. */
  585. throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
  586. time_elapsed = jiffies - tg->slice_start[rw];
  587. nr_slices = time_elapsed / throtl_slice;
  588. if (!nr_slices)
  589. return;
  590. tmp = tg->bps[rw] * throtl_slice * nr_slices;
  591. do_div(tmp, HZ);
  592. bytes_trim = tmp;
  593. io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
  594. if (!bytes_trim && !io_trim)
  595. return;
  596. if (tg->bytes_disp[rw] >= bytes_trim)
  597. tg->bytes_disp[rw] -= bytes_trim;
  598. else
  599. tg->bytes_disp[rw] = 0;
  600. if (tg->io_disp[rw] >= io_trim)
  601. tg->io_disp[rw] -= io_trim;
  602. else
  603. tg->io_disp[rw] = 0;
  604. tg->slice_start[rw] += nr_slices * throtl_slice;
  605. throtl_log(&tg->service_queue,
  606. "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
  607. rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
  608. tg->slice_start[rw], tg->slice_end[rw], jiffies);
  609. }
  610. static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
  611. unsigned long *wait)
  612. {
  613. bool rw = bio_data_dir(bio);
  614. unsigned int io_allowed;
  615. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  616. u64 tmp;
  617. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  618. /* Slice has just started. Consider one slice interval */
  619. if (!jiffy_elapsed)
  620. jiffy_elapsed_rnd = throtl_slice;
  621. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  622. /*
  623. * jiffy_elapsed_rnd should not be a big value as minimum iops can be
  624. * 1 then at max jiffy elapsed should be equivalent of 1 second as we
  625. * will allow dispatch after 1 second and after that slice should
  626. * have been trimmed.
  627. */
  628. tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
  629. do_div(tmp, HZ);
  630. if (tmp > UINT_MAX)
  631. io_allowed = UINT_MAX;
  632. else
  633. io_allowed = tmp;
  634. if (tg->io_disp[rw] + 1 <= io_allowed) {
  635. if (wait)
  636. *wait = 0;
  637. return 1;
  638. }
  639. /* Calc approx time to dispatch */
  640. jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
  641. if (jiffy_wait > jiffy_elapsed)
  642. jiffy_wait = jiffy_wait - jiffy_elapsed;
  643. else
  644. jiffy_wait = 1;
  645. if (wait)
  646. *wait = jiffy_wait;
  647. return 0;
  648. }
  649. static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
  650. unsigned long *wait)
  651. {
  652. bool rw = bio_data_dir(bio);
  653. u64 bytes_allowed, extra_bytes, tmp;
  654. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  655. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  656. /* Slice has just started. Consider one slice interval */
  657. if (!jiffy_elapsed)
  658. jiffy_elapsed_rnd = throtl_slice;
  659. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  660. tmp = tg->bps[rw] * jiffy_elapsed_rnd;
  661. do_div(tmp, HZ);
  662. bytes_allowed = tmp;
  663. if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
  664. if (wait)
  665. *wait = 0;
  666. return 1;
  667. }
  668. /* Calc approx time to dispatch */
  669. extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
  670. jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
  671. if (!jiffy_wait)
  672. jiffy_wait = 1;
  673. /*
  674. * This wait time is without taking into consideration the rounding
  675. * up we did. Add that time also.
  676. */
  677. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  678. if (wait)
  679. *wait = jiffy_wait;
  680. return 0;
  681. }
  682. static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
  683. if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
  684. return 1;
  685. return 0;
  686. }
  687. /*
  688. * Returns whether one can dispatch a bio or not. Also returns approx number
  689. * of jiffies to wait before this bio is with-in IO rate and can be dispatched
  690. */
  691. static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
  692. unsigned long *wait)
  693. {
  694. bool rw = bio_data_dir(bio);
  695. unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
  696. /*
  697. * Currently whole state machine of group depends on first bio
  698. * queued in the group bio list. So one should not be calling
  699. * this function with a different bio if there are other bios
  700. * queued.
  701. */
  702. BUG_ON(tg->service_queue.nr_queued[rw] &&
  703. bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
  704. /* If tg->bps = -1, then BW is unlimited */
  705. if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
  706. if (wait)
  707. *wait = 0;
  708. return 1;
  709. }
  710. /*
  711. * If previous slice expired, start a new one otherwise renew/extend
  712. * existing slice to make sure it is at least throtl_slice interval
  713. * long since now.
  714. */
  715. if (throtl_slice_used(tg, rw))
  716. throtl_start_new_slice(tg, rw);
  717. else {
  718. if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
  719. throtl_extend_slice(tg, rw, jiffies + throtl_slice);
  720. }
  721. if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
  722. tg_with_in_iops_limit(tg, bio, &iops_wait)) {
  723. if (wait)
  724. *wait = 0;
  725. return 1;
  726. }
  727. max_wait = max(bps_wait, iops_wait);
  728. if (wait)
  729. *wait = max_wait;
  730. if (time_before(tg->slice_end[rw], jiffies + max_wait))
  731. throtl_extend_slice(tg, rw, jiffies + max_wait);
  732. return 0;
  733. }
  734. static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
  735. int rw)
  736. {
  737. struct throtl_grp *tg = blkg_to_tg(blkg);
  738. struct tg_stats_cpu *stats_cpu;
  739. unsigned long flags;
  740. /* If per cpu stats are not allocated yet, don't do any accounting. */
  741. if (tg->stats_cpu == NULL)
  742. return;
  743. /*
  744. * Disabling interrupts to provide mutual exclusion between two
  745. * writes on same cpu. It probably is not needed for 64bit. Not
  746. * optimizing that case yet.
  747. */
  748. local_irq_save(flags);
  749. stats_cpu = this_cpu_ptr(tg->stats_cpu);
  750. blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
  751. blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
  752. local_irq_restore(flags);
  753. }
  754. static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
  755. {
  756. bool rw = bio_data_dir(bio);
  757. /* Charge the bio to the group */
  758. tg->bytes_disp[rw] += bio->bi_size;
  759. tg->io_disp[rw]++;
  760. /*
  761. * REQ_THROTTLED is used to prevent the same bio to be throttled
  762. * more than once as a throttled bio will go through blk-throtl the
  763. * second time when it eventually gets issued. Set it when a bio
  764. * is being charged to a tg.
  765. *
  766. * Dispatch stats aren't recursive and each @bio should only be
  767. * accounted by the @tg it was originally associated with. Let's
  768. * update the stats when setting REQ_THROTTLED for the first time
  769. * which is guaranteed to be for the @bio's original tg.
  770. */
  771. if (!(bio->bi_rw & REQ_THROTTLED)) {
  772. bio->bi_rw |= REQ_THROTTLED;
  773. throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size,
  774. bio->bi_rw);
  775. }
  776. }
  777. /**
  778. * throtl_add_bio_tg - add a bio to the specified throtl_grp
  779. * @bio: bio to add
  780. * @qn: qnode to use
  781. * @tg: the target throtl_grp
  782. *
  783. * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
  784. * tg->qnode_on_self[] is used.
  785. */
  786. static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
  787. struct throtl_grp *tg)
  788. {
  789. struct throtl_service_queue *sq = &tg->service_queue;
  790. bool rw = bio_data_dir(bio);
  791. if (!qn)
  792. qn = &tg->qnode_on_self[rw];
  793. /*
  794. * If @tg doesn't currently have any bios queued in the same
  795. * direction, queueing @bio can change when @tg should be
  796. * dispatched. Mark that @tg was empty. This is automatically
  797. * cleaered on the next tg_update_disptime().
  798. */
  799. if (!sq->nr_queued[rw])
  800. tg->flags |= THROTL_TG_WAS_EMPTY;
  801. throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
  802. sq->nr_queued[rw]++;
  803. throtl_enqueue_tg(tg);
  804. }
  805. static void tg_update_disptime(struct throtl_grp *tg)
  806. {
  807. struct throtl_service_queue *sq = &tg->service_queue;
  808. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  809. struct bio *bio;
  810. if ((bio = throtl_peek_queued(&sq->queued[READ])))
  811. tg_may_dispatch(tg, bio, &read_wait);
  812. if ((bio = throtl_peek_queued(&sq->queued[WRITE])))
  813. tg_may_dispatch(tg, bio, &write_wait);
  814. min_wait = min(read_wait, write_wait);
  815. disptime = jiffies + min_wait;
  816. /* Update dispatch time */
  817. throtl_dequeue_tg(tg);
  818. tg->disptime = disptime;
  819. throtl_enqueue_tg(tg);
  820. /* see throtl_add_bio_tg() */
  821. tg->flags &= ~THROTL_TG_WAS_EMPTY;
  822. }
  823. static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
  824. {
  825. struct throtl_service_queue *sq = &tg->service_queue;
  826. struct throtl_service_queue *parent_sq = sq->parent_sq;
  827. struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
  828. struct throtl_grp *tg_to_put = NULL;
  829. struct bio *bio;
  830. /*
  831. * @bio is being transferred from @tg to @parent_sq. Popping a bio
  832. * from @tg may put its reference and @parent_sq might end up
  833. * getting released prematurely. Remember the tg to put and put it
  834. * after @bio is transferred to @parent_sq.
  835. */
  836. bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
  837. sq->nr_queued[rw]--;
  838. throtl_charge_bio(tg, bio);
  839. /*
  840. * If our parent is another tg, we just need to transfer @bio to
  841. * the parent using throtl_add_bio_tg(). If our parent is
  842. * @td->service_queue, @bio is ready to be issued. Put it on its
  843. * bio_lists[] and decrease total number queued. The caller is
  844. * responsible for issuing these bios.
  845. */
  846. if (parent_tg) {
  847. throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
  848. } else {
  849. throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
  850. &parent_sq->queued[rw]);
  851. BUG_ON(tg->td->nr_queued[rw] <= 0);
  852. tg->td->nr_queued[rw]--;
  853. }
  854. throtl_trim_slice(tg, rw);
  855. if (tg_to_put)
  856. blkg_put(tg_to_blkg(tg_to_put));
  857. }
  858. static int throtl_dispatch_tg(struct throtl_grp *tg)
  859. {
  860. struct throtl_service_queue *sq = &tg->service_queue;
  861. unsigned int nr_reads = 0, nr_writes = 0;
  862. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  863. unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
  864. struct bio *bio;
  865. /* Try to dispatch 75% READS and 25% WRITES */
  866. while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
  867. tg_may_dispatch(tg, bio, NULL)) {
  868. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  869. nr_reads++;
  870. if (nr_reads >= max_nr_reads)
  871. break;
  872. }
  873. while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
  874. tg_may_dispatch(tg, bio, NULL)) {
  875. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  876. nr_writes++;
  877. if (nr_writes >= max_nr_writes)
  878. break;
  879. }
  880. return nr_reads + nr_writes;
  881. }
  882. static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
  883. {
  884. unsigned int nr_disp = 0;
  885. while (1) {
  886. struct throtl_grp *tg = throtl_rb_first(parent_sq);
  887. struct throtl_service_queue *sq = &tg->service_queue;
  888. if (!tg)
  889. break;
  890. if (time_before(jiffies, tg->disptime))
  891. break;
  892. throtl_dequeue_tg(tg);
  893. nr_disp += throtl_dispatch_tg(tg);
  894. if (sq->nr_queued[0] || sq->nr_queued[1])
  895. tg_update_disptime(tg);
  896. if (nr_disp >= throtl_quantum)
  897. break;
  898. }
  899. return nr_disp;
  900. }
  901. /**
  902. * throtl_pending_timer_fn - timer function for service_queue->pending_timer
  903. * @arg: the throtl_service_queue being serviced
  904. *
  905. * This timer is armed when a child throtl_grp with active bio's become
  906. * pending and queued on the service_queue's pending_tree and expires when
  907. * the first child throtl_grp should be dispatched. This function
  908. * dispatches bio's from the children throtl_grps to the parent
  909. * service_queue.
  910. *
  911. * If the parent's parent is another throtl_grp, dispatching is propagated
  912. * by either arming its pending_timer or repeating dispatch directly. If
  913. * the top-level service_tree is reached, throtl_data->dispatch_work is
  914. * kicked so that the ready bio's are issued.
  915. */
  916. static void throtl_pending_timer_fn(unsigned long arg)
  917. {
  918. struct throtl_service_queue *sq = (void *)arg;
  919. struct throtl_grp *tg = sq_to_tg(sq);
  920. struct throtl_data *td = sq_to_td(sq);
  921. struct request_queue *q = td->queue;
  922. struct throtl_service_queue *parent_sq;
  923. bool dispatched;
  924. int ret;
  925. spin_lock_irq(q->queue_lock);
  926. again:
  927. parent_sq = sq->parent_sq;
  928. dispatched = false;
  929. while (true) {
  930. throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
  931. sq->nr_queued[READ] + sq->nr_queued[WRITE],
  932. sq->nr_queued[READ], sq->nr_queued[WRITE]);
  933. ret = throtl_select_dispatch(sq);
  934. if (ret) {
  935. throtl_log(sq, "bios disp=%u", ret);
  936. dispatched = true;
  937. }
  938. if (throtl_schedule_next_dispatch(sq, false))
  939. break;
  940. /* this dispatch windows is still open, relax and repeat */
  941. spin_unlock_irq(q->queue_lock);
  942. cpu_relax();
  943. spin_lock_irq(q->queue_lock);
  944. }
  945. if (!dispatched)
  946. goto out_unlock;
  947. if (parent_sq) {
  948. /* @parent_sq is another throl_grp, propagate dispatch */
  949. if (tg->flags & THROTL_TG_WAS_EMPTY) {
  950. tg_update_disptime(tg);
  951. if (!throtl_schedule_next_dispatch(parent_sq, false)) {
  952. /* window is already open, repeat dispatching */
  953. sq = parent_sq;
  954. tg = sq_to_tg(sq);
  955. goto again;
  956. }
  957. }
  958. } else {
  959. /* reached the top-level, queue issueing */
  960. queue_work(kthrotld_workqueue, &td->dispatch_work);
  961. }
  962. out_unlock:
  963. spin_unlock_irq(q->queue_lock);
  964. }
  965. /**
  966. * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
  967. * @work: work item being executed
  968. *
  969. * This function is queued for execution when bio's reach the bio_lists[]
  970. * of throtl_data->service_queue. Those bio's are ready and issued by this
  971. * function.
  972. */
  973. void blk_throtl_dispatch_work_fn(struct work_struct *work)
  974. {
  975. struct throtl_data *td = container_of(work, struct throtl_data,
  976. dispatch_work);
  977. struct throtl_service_queue *td_sq = &td->service_queue;
  978. struct request_queue *q = td->queue;
  979. struct bio_list bio_list_on_stack;
  980. struct bio *bio;
  981. struct blk_plug plug;
  982. int rw;
  983. bio_list_init(&bio_list_on_stack);
  984. spin_lock_irq(q->queue_lock);
  985. for (rw = READ; rw <= WRITE; rw++)
  986. while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
  987. bio_list_add(&bio_list_on_stack, bio);
  988. spin_unlock_irq(q->queue_lock);
  989. if (!bio_list_empty(&bio_list_on_stack)) {
  990. blk_start_plug(&plug);
  991. while((bio = bio_list_pop(&bio_list_on_stack)))
  992. generic_make_request(bio);
  993. blk_finish_plug(&plug);
  994. }
  995. }
  996. static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
  997. struct blkg_policy_data *pd, int off)
  998. {
  999. struct throtl_grp *tg = pd_to_tg(pd);
  1000. struct blkg_rwstat rwstat = { }, tmp;
  1001. int i, cpu;
  1002. for_each_possible_cpu(cpu) {
  1003. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  1004. tmp = blkg_rwstat_read((void *)sc + off);
  1005. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  1006. rwstat.cnt[i] += tmp.cnt[i];
  1007. }
  1008. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  1009. }
  1010. static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
  1011. struct seq_file *sf)
  1012. {
  1013. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  1014. blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
  1015. cft->private, true);
  1016. return 0;
  1017. }
  1018. static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
  1019. int off)
  1020. {
  1021. struct throtl_grp *tg = pd_to_tg(pd);
  1022. u64 v = *(u64 *)((void *)tg + off);
  1023. if (v == -1)
  1024. return 0;
  1025. return __blkg_prfill_u64(sf, pd, v);
  1026. }
  1027. static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
  1028. int off)
  1029. {
  1030. struct throtl_grp *tg = pd_to_tg(pd);
  1031. unsigned int v = *(unsigned int *)((void *)tg + off);
  1032. if (v == -1)
  1033. return 0;
  1034. return __blkg_prfill_u64(sf, pd, v);
  1035. }
  1036. static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  1037. struct seq_file *sf)
  1038. {
  1039. blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
  1040. &blkcg_policy_throtl, cft->private, false);
  1041. return 0;
  1042. }
  1043. static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  1044. struct seq_file *sf)
  1045. {
  1046. blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
  1047. &blkcg_policy_throtl, cft->private, false);
  1048. return 0;
  1049. }
  1050. static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
  1051. bool is_u64)
  1052. {
  1053. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  1054. struct blkg_conf_ctx ctx;
  1055. struct throtl_grp *tg;
  1056. struct throtl_service_queue *sq;
  1057. int ret;
  1058. ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
  1059. if (ret)
  1060. return ret;
  1061. tg = blkg_to_tg(ctx.blkg);
  1062. sq = &tg->service_queue;
  1063. if (!ctx.v)
  1064. ctx.v = -1;
  1065. if (is_u64)
  1066. *(u64 *)((void *)tg + cft->private) = ctx.v;
  1067. else
  1068. *(unsigned int *)((void *)tg + cft->private) = ctx.v;
  1069. throtl_log(&tg->service_queue,
  1070. "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
  1071. tg->bps[READ], tg->bps[WRITE],
  1072. tg->iops[READ], tg->iops[WRITE]);
  1073. /*
  1074. * We're already holding queue_lock and know @tg is valid. Let's
  1075. * apply the new config directly.
  1076. *
  1077. * Restart the slices for both READ and WRITES. It might happen
  1078. * that a group's limit are dropped suddenly and we don't want to
  1079. * account recently dispatched IO with new low rate.
  1080. */
  1081. throtl_start_new_slice(tg, 0);
  1082. throtl_start_new_slice(tg, 1);
  1083. if (tg->flags & THROTL_TG_PENDING) {
  1084. tg_update_disptime(tg);
  1085. throtl_schedule_next_dispatch(sq->parent_sq, true);
  1086. }
  1087. blkg_conf_finish(&ctx);
  1088. return 0;
  1089. }
  1090. static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  1091. const char *buf)
  1092. {
  1093. return tg_set_conf(cgrp, cft, buf, true);
  1094. }
  1095. static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  1096. const char *buf)
  1097. {
  1098. return tg_set_conf(cgrp, cft, buf, false);
  1099. }
  1100. static struct cftype throtl_files[] = {
  1101. {
  1102. .name = "throttle.read_bps_device",
  1103. .private = offsetof(struct throtl_grp, bps[READ]),
  1104. .read_seq_string = tg_print_conf_u64,
  1105. .write_string = tg_set_conf_u64,
  1106. .max_write_len = 256,
  1107. },
  1108. {
  1109. .name = "throttle.write_bps_device",
  1110. .private = offsetof(struct throtl_grp, bps[WRITE]),
  1111. .read_seq_string = tg_print_conf_u64,
  1112. .write_string = tg_set_conf_u64,
  1113. .max_write_len = 256,
  1114. },
  1115. {
  1116. .name = "throttle.read_iops_device",
  1117. .private = offsetof(struct throtl_grp, iops[READ]),
  1118. .read_seq_string = tg_print_conf_uint,
  1119. .write_string = tg_set_conf_uint,
  1120. .max_write_len = 256,
  1121. },
  1122. {
  1123. .name = "throttle.write_iops_device",
  1124. .private = offsetof(struct throtl_grp, iops[WRITE]),
  1125. .read_seq_string = tg_print_conf_uint,
  1126. .write_string = tg_set_conf_uint,
  1127. .max_write_len = 256,
  1128. },
  1129. {
  1130. .name = "throttle.io_service_bytes",
  1131. .private = offsetof(struct tg_stats_cpu, service_bytes),
  1132. .read_seq_string = tg_print_cpu_rwstat,
  1133. },
  1134. {
  1135. .name = "throttle.io_serviced",
  1136. .private = offsetof(struct tg_stats_cpu, serviced),
  1137. .read_seq_string = tg_print_cpu_rwstat,
  1138. },
  1139. { } /* terminate */
  1140. };
  1141. static void throtl_shutdown_wq(struct request_queue *q)
  1142. {
  1143. struct throtl_data *td = q->td;
  1144. cancel_work_sync(&td->dispatch_work);
  1145. }
  1146. static struct blkcg_policy blkcg_policy_throtl = {
  1147. .pd_size = sizeof(struct throtl_grp),
  1148. .cftypes = throtl_files,
  1149. .pd_init_fn = throtl_pd_init,
  1150. .pd_exit_fn = throtl_pd_exit,
  1151. .pd_reset_stats_fn = throtl_pd_reset_stats,
  1152. };
  1153. bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
  1154. {
  1155. struct throtl_data *td = q->td;
  1156. struct throtl_qnode *qn = NULL;
  1157. struct throtl_grp *tg;
  1158. struct throtl_service_queue *sq;
  1159. bool rw = bio_data_dir(bio);
  1160. struct blkcg *blkcg;
  1161. bool throttled = false;
  1162. /* see throtl_charge_bio() */
  1163. if (bio->bi_rw & REQ_THROTTLED)
  1164. goto out;
  1165. /*
  1166. * A throtl_grp pointer retrieved under rcu can be used to access
  1167. * basic fields like stats and io rates. If a group has no rules,
  1168. * just update the dispatch stats in lockless manner and return.
  1169. */
  1170. rcu_read_lock();
  1171. blkcg = bio_blkcg(bio);
  1172. tg = throtl_lookup_tg(td, blkcg);
  1173. if (tg) {
  1174. if (tg_no_rule_group(tg, rw)) {
  1175. throtl_update_dispatch_stats(tg_to_blkg(tg),
  1176. bio->bi_size, bio->bi_rw);
  1177. goto out_unlock_rcu;
  1178. }
  1179. }
  1180. /*
  1181. * Either group has not been allocated yet or it is not an unlimited
  1182. * IO group
  1183. */
  1184. spin_lock_irq(q->queue_lock);
  1185. tg = throtl_lookup_create_tg(td, blkcg);
  1186. if (unlikely(!tg))
  1187. goto out_unlock;
  1188. sq = &tg->service_queue;
  1189. while (true) {
  1190. /* throtl is FIFO - if bios are already queued, should queue */
  1191. if (sq->nr_queued[rw])
  1192. break;
  1193. /* if above limits, break to queue */
  1194. if (!tg_may_dispatch(tg, bio, NULL))
  1195. break;
  1196. /* within limits, let's charge and dispatch directly */
  1197. throtl_charge_bio(tg, bio);
  1198. /*
  1199. * We need to trim slice even when bios are not being queued
  1200. * otherwise it might happen that a bio is not queued for
  1201. * a long time and slice keeps on extending and trim is not
  1202. * called for a long time. Now if limits are reduced suddenly
  1203. * we take into account all the IO dispatched so far at new
  1204. * low rate and * newly queued IO gets a really long dispatch
  1205. * time.
  1206. *
  1207. * So keep on trimming slice even if bio is not queued.
  1208. */
  1209. throtl_trim_slice(tg, rw);
  1210. /*
  1211. * @bio passed through this layer without being throttled.
  1212. * Climb up the ladder. If we''re already at the top, it
  1213. * can be executed directly.
  1214. */
  1215. qn = &tg->qnode_on_parent[rw];
  1216. sq = sq->parent_sq;
  1217. tg = sq_to_tg(sq);
  1218. if (!tg)
  1219. goto out_unlock;
  1220. }
  1221. /* out-of-limit, queue to @tg */
  1222. throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
  1223. rw == READ ? 'R' : 'W',
  1224. tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
  1225. tg->io_disp[rw], tg->iops[rw],
  1226. sq->nr_queued[READ], sq->nr_queued[WRITE]);
  1227. bio_associate_current(bio);
  1228. tg->td->nr_queued[rw]++;
  1229. throtl_add_bio_tg(bio, qn, tg);
  1230. throttled = true;
  1231. /*
  1232. * Update @tg's dispatch time and force schedule dispatch if @tg
  1233. * was empty before @bio. The forced scheduling isn't likely to
  1234. * cause undue delay as @bio is likely to be dispatched directly if
  1235. * its @tg's disptime is not in the future.
  1236. */
  1237. if (tg->flags & THROTL_TG_WAS_EMPTY) {
  1238. tg_update_disptime(tg);
  1239. throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
  1240. }
  1241. out_unlock:
  1242. spin_unlock_irq(q->queue_lock);
  1243. out_unlock_rcu:
  1244. rcu_read_unlock();
  1245. out:
  1246. /*
  1247. * As multiple blk-throtls may stack in the same issue path, we
  1248. * don't want bios to leave with the flag set. Clear the flag if
  1249. * being issued.
  1250. */
  1251. if (!throttled)
  1252. bio->bi_rw &= ~REQ_THROTTLED;
  1253. return throttled;
  1254. }
  1255. /*
  1256. * Dispatch all bios from all children tg's queued on @parent_sq. On
  1257. * return, @parent_sq is guaranteed to not have any active children tg's
  1258. * and all bios from previously active tg's are on @parent_sq->bio_lists[].
  1259. */
  1260. static void tg_drain_bios(struct throtl_service_queue *parent_sq)
  1261. {
  1262. struct throtl_grp *tg;
  1263. while ((tg = throtl_rb_first(parent_sq))) {
  1264. struct throtl_service_queue *sq = &tg->service_queue;
  1265. struct bio *bio;
  1266. throtl_dequeue_tg(tg);
  1267. while ((bio = throtl_peek_queued(&sq->queued[READ])))
  1268. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  1269. while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
  1270. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  1271. }
  1272. }
  1273. /**
  1274. * blk_throtl_drain - drain throttled bios
  1275. * @q: request_queue to drain throttled bios for
  1276. *
  1277. * Dispatch all currently throttled bios on @q through ->make_request_fn().
  1278. */
  1279. void blk_throtl_drain(struct request_queue *q)
  1280. __releases(q->queue_lock) __acquires(q->queue_lock)
  1281. {
  1282. struct throtl_data *td = q->td;
  1283. struct blkcg_gq *blkg;
  1284. struct cgroup *pos_cgrp;
  1285. struct bio *bio;
  1286. int rw;
  1287. queue_lockdep_assert_held(q);
  1288. rcu_read_lock();
  1289. /*
  1290. * Drain each tg while doing post-order walk on the blkg tree, so
  1291. * that all bios are propagated to td->service_queue. It'd be
  1292. * better to walk service_queue tree directly but blkg walk is
  1293. * easier.
  1294. */
  1295. blkg_for_each_descendant_post(blkg, pos_cgrp, td->queue->root_blkg)
  1296. tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
  1297. tg_drain_bios(&td_root_tg(td)->service_queue);
  1298. /* finally, transfer bios from top-level tg's into the td */
  1299. tg_drain_bios(&td->service_queue);
  1300. rcu_read_unlock();
  1301. spin_unlock_irq(q->queue_lock);
  1302. /* all bios now should be in td->service_queue, issue them */
  1303. for (rw = READ; rw <= WRITE; rw++)
  1304. while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
  1305. NULL)))
  1306. generic_make_request(bio);
  1307. spin_lock_irq(q->queue_lock);
  1308. }
  1309. int blk_throtl_init(struct request_queue *q)
  1310. {
  1311. struct throtl_data *td;
  1312. int ret;
  1313. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  1314. if (!td)
  1315. return -ENOMEM;
  1316. INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
  1317. throtl_service_queue_init(&td->service_queue, NULL);
  1318. q->td = td;
  1319. td->queue = q;
  1320. /* activate policy */
  1321. ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
  1322. if (ret)
  1323. kfree(td);
  1324. return ret;
  1325. }
  1326. void blk_throtl_exit(struct request_queue *q)
  1327. {
  1328. BUG_ON(!q->td);
  1329. throtl_shutdown_wq(q);
  1330. blkcg_deactivate_policy(q, &blkcg_policy_throtl);
  1331. kfree(q->td);
  1332. }
  1333. static int __init throtl_init(void)
  1334. {
  1335. kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
  1336. if (!kthrotld_workqueue)
  1337. panic("Failed to create kthrotld\n");
  1338. return blkcg_policy_register(&blkcg_policy_throtl);
  1339. }
  1340. module_init(throtl_init);