blk-throttle.c 31 KB

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