blk-throttle.c 29 KB

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