blk-throttle.c 30 KB

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