blk-cgroup.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  1. /*
  2. * Common Block IO controller cgroup interface
  3. *
  4. * Based on ideas and code from CFQ, CFS and BFQ:
  5. * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
  6. *
  7. * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
  8. * Paolo Valente <paolo.valente@unimore.it>
  9. *
  10. * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
  11. * Nauman Rafique <nauman@google.com>
  12. */
  13. #include <linux/ioprio.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/kdev_t.h>
  16. #include <linux/module.h>
  17. #include <linux/err.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/slab.h>
  20. #include <linux/genhd.h>
  21. #include <linux/delay.h>
  22. #include <linux/atomic.h>
  23. #include "blk-cgroup.h"
  24. #include "blk.h"
  25. #define MAX_KEY_LEN 100
  26. static DEFINE_SPINLOCK(blkio_list_lock);
  27. static LIST_HEAD(blkio_list);
  28. static DEFINE_MUTEX(all_q_mutex);
  29. static LIST_HEAD(all_q_list);
  30. /* List of groups pending per cpu stats allocation */
  31. static DEFINE_SPINLOCK(alloc_list_lock);
  32. static LIST_HEAD(alloc_list);
  33. static void blkio_stat_alloc_fn(struct work_struct *);
  34. static DECLARE_DELAYED_WORK(blkio_stat_alloc_work, blkio_stat_alloc_fn);
  35. struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
  36. EXPORT_SYMBOL_GPL(blkio_root_cgroup);
  37. static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
  38. /* for encoding cft->private value on file */
  39. #define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
  40. /* What policy owns the file, proportional or throttle */
  41. #define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
  42. #define BLKIOFILE_ATTR(val) ((val) & 0xffff)
  43. struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
  44. {
  45. return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
  46. struct blkio_cgroup, css);
  47. }
  48. EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
  49. static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
  50. {
  51. return container_of(task_subsys_state(tsk, blkio_subsys_id),
  52. struct blkio_cgroup, css);
  53. }
  54. struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
  55. {
  56. if (bio && bio->bi_css)
  57. return container_of(bio->bi_css, struct blkio_cgroup, css);
  58. return task_blkio_cgroup(current);
  59. }
  60. EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
  61. static inline void blkio_update_group_weight(struct blkio_group *blkg,
  62. int plid, unsigned int weight)
  63. {
  64. struct blkio_policy_type *blkiop;
  65. list_for_each_entry(blkiop, &blkio_list, list) {
  66. /* If this policy does not own the blkg, do not send updates */
  67. if (blkiop->plid != plid)
  68. continue;
  69. if (blkiop->ops.blkio_update_group_weight_fn)
  70. blkiop->ops.blkio_update_group_weight_fn(blkg->q,
  71. blkg, weight);
  72. }
  73. }
  74. static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid,
  75. u64 bps, int fileid)
  76. {
  77. struct blkio_policy_type *blkiop;
  78. list_for_each_entry(blkiop, &blkio_list, list) {
  79. /* If this policy does not own the blkg, do not send updates */
  80. if (blkiop->plid != plid)
  81. continue;
  82. if (fileid == BLKIO_THROTL_read_bps_device
  83. && blkiop->ops.blkio_update_group_read_bps_fn)
  84. blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
  85. blkg, bps);
  86. if (fileid == BLKIO_THROTL_write_bps_device
  87. && blkiop->ops.blkio_update_group_write_bps_fn)
  88. blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
  89. blkg, bps);
  90. }
  91. }
  92. static inline void blkio_update_group_iops(struct blkio_group *blkg,
  93. int plid, unsigned int iops,
  94. int fileid)
  95. {
  96. struct blkio_policy_type *blkiop;
  97. list_for_each_entry(blkiop, &blkio_list, list) {
  98. /* If this policy does not own the blkg, do not send updates */
  99. if (blkiop->plid != plid)
  100. continue;
  101. if (fileid == BLKIO_THROTL_read_iops_device
  102. && blkiop->ops.blkio_update_group_read_iops_fn)
  103. blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
  104. blkg, iops);
  105. if (fileid == BLKIO_THROTL_write_iops_device
  106. && blkiop->ops.blkio_update_group_write_iops_fn)
  107. blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
  108. blkg,iops);
  109. }
  110. }
  111. #ifdef CONFIG_DEBUG_BLK_CGROUP
  112. /* This should be called with the queue_lock held. */
  113. static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
  114. struct blkio_policy_type *pol,
  115. struct blkio_group *curr_blkg)
  116. {
  117. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  118. if (blkio_blkg_waiting(&pd->stats))
  119. return;
  120. if (blkg == curr_blkg)
  121. return;
  122. pd->stats.start_group_wait_time = sched_clock();
  123. blkio_mark_blkg_waiting(&pd->stats);
  124. }
  125. /* This should be called with the queue_lock held. */
  126. static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
  127. {
  128. unsigned long long now;
  129. if (!blkio_blkg_waiting(stats))
  130. return;
  131. now = sched_clock();
  132. if (time_after64(now, stats->start_group_wait_time))
  133. blkg_stat_add(&stats->group_wait_time,
  134. now - stats->start_group_wait_time);
  135. blkio_clear_blkg_waiting(stats);
  136. }
  137. /* This should be called with the queue_lock held. */
  138. static void blkio_end_empty_time(struct blkio_group_stats *stats)
  139. {
  140. unsigned long long now;
  141. if (!blkio_blkg_empty(stats))
  142. return;
  143. now = sched_clock();
  144. if (time_after64(now, stats->start_empty_time))
  145. blkg_stat_add(&stats->empty_time,
  146. now - stats->start_empty_time);
  147. blkio_clear_blkg_empty(stats);
  148. }
  149. void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
  150. struct blkio_policy_type *pol)
  151. {
  152. struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
  153. lockdep_assert_held(blkg->q->queue_lock);
  154. BUG_ON(blkio_blkg_idling(stats));
  155. stats->start_idle_time = sched_clock();
  156. blkio_mark_blkg_idling(stats);
  157. }
  158. EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
  159. void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
  160. struct blkio_policy_type *pol)
  161. {
  162. struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
  163. lockdep_assert_held(blkg->q->queue_lock);
  164. if (blkio_blkg_idling(stats)) {
  165. unsigned long long now = sched_clock();
  166. if (time_after64(now, stats->start_idle_time))
  167. blkg_stat_add(&stats->idle_time,
  168. now - stats->start_idle_time);
  169. blkio_clear_blkg_idling(stats);
  170. }
  171. }
  172. EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
  173. void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
  174. struct blkio_policy_type *pol)
  175. {
  176. struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
  177. lockdep_assert_held(blkg->q->queue_lock);
  178. blkg_stat_add(&stats->avg_queue_size_sum,
  179. blkg_rwstat_sum(&stats->queued));
  180. blkg_stat_add(&stats->avg_queue_size_samples, 1);
  181. blkio_update_group_wait_time(stats);
  182. }
  183. EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
  184. void blkiocg_set_start_empty_time(struct blkio_group *blkg,
  185. struct blkio_policy_type *pol)
  186. {
  187. struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
  188. lockdep_assert_held(blkg->q->queue_lock);
  189. if (blkg_rwstat_sum(&stats->queued))
  190. return;
  191. /*
  192. * group is already marked empty. This can happen if cfqq got new
  193. * request in parent group and moved to this group while being added
  194. * to service tree. Just ignore the event and move on.
  195. */
  196. if (blkio_blkg_empty(stats))
  197. return;
  198. stats->start_empty_time = sched_clock();
  199. blkio_mark_blkg_empty(stats);
  200. }
  201. EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
  202. void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  203. struct blkio_policy_type *pol,
  204. unsigned long dequeue)
  205. {
  206. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  207. lockdep_assert_held(blkg->q->queue_lock);
  208. blkg_stat_add(&pd->stats.dequeue, dequeue);
  209. }
  210. EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
  211. #else
  212. static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
  213. struct blkio_policy_type *pol,
  214. struct blkio_group *curr_blkg) { }
  215. static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { }
  216. #endif
  217. void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  218. struct blkio_policy_type *pol,
  219. struct blkio_group *curr_blkg, bool direction,
  220. bool sync)
  221. {
  222. struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
  223. int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
  224. lockdep_assert_held(blkg->q->queue_lock);
  225. blkg_rwstat_add(&stats->queued, rw, 1);
  226. blkio_end_empty_time(stats);
  227. blkio_set_start_group_wait_time(blkg, pol, curr_blkg);
  228. }
  229. EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
  230. void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  231. struct blkio_policy_type *pol,
  232. bool direction, bool sync)
  233. {
  234. struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
  235. int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
  236. lockdep_assert_held(blkg->q->queue_lock);
  237. blkg_rwstat_add(&stats->queued, rw, -1);
  238. }
  239. EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
  240. void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  241. struct blkio_policy_type *pol,
  242. unsigned long time,
  243. unsigned long unaccounted_time)
  244. {
  245. struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
  246. lockdep_assert_held(blkg->q->queue_lock);
  247. blkg_stat_add(&stats->time, time);
  248. #ifdef CONFIG_DEBUG_BLK_CGROUP
  249. blkg_stat_add(&stats->unaccounted_time, unaccounted_time);
  250. #endif
  251. }
  252. EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
  253. /*
  254. * should be called under rcu read lock or queue lock to make sure blkg pointer
  255. * is valid.
  256. */
  257. void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  258. struct blkio_policy_type *pol,
  259. uint64_t bytes, bool direction, bool sync)
  260. {
  261. int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
  262. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  263. struct blkio_group_stats_cpu *stats_cpu;
  264. unsigned long flags;
  265. /* If per cpu stats are not allocated yet, don't do any accounting. */
  266. if (pd->stats_cpu == NULL)
  267. return;
  268. /*
  269. * Disabling interrupts to provide mutual exclusion between two
  270. * writes on same cpu. It probably is not needed for 64bit. Not
  271. * optimizing that case yet.
  272. */
  273. local_irq_save(flags);
  274. stats_cpu = this_cpu_ptr(pd->stats_cpu);
  275. blkg_stat_add(&stats_cpu->sectors, bytes >> 9);
  276. blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
  277. blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
  278. local_irq_restore(flags);
  279. }
  280. EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
  281. void blkiocg_update_completion_stats(struct blkio_group *blkg,
  282. struct blkio_policy_type *pol,
  283. uint64_t start_time,
  284. uint64_t io_start_time, bool direction,
  285. bool sync)
  286. {
  287. struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
  288. unsigned long long now = sched_clock();
  289. int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
  290. lockdep_assert_held(blkg->q->queue_lock);
  291. if (time_after64(now, io_start_time))
  292. blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
  293. if (time_after64(io_start_time, start_time))
  294. blkg_rwstat_add(&stats->wait_time, rw,
  295. io_start_time - start_time);
  296. }
  297. EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
  298. /* Merged stats are per cpu. */
  299. void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  300. struct blkio_policy_type *pol,
  301. bool direction, bool sync)
  302. {
  303. struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
  304. int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
  305. lockdep_assert_held(blkg->q->queue_lock);
  306. blkg_rwstat_add(&stats->merged, rw, 1);
  307. }
  308. EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
  309. /*
  310. * Worker for allocating per cpu stat for blk groups. This is scheduled on
  311. * the system_nrt_wq once there are some groups on the alloc_list waiting
  312. * for allocation.
  313. */
  314. static void blkio_stat_alloc_fn(struct work_struct *work)
  315. {
  316. static void *pcpu_stats[BLKIO_NR_POLICIES];
  317. struct delayed_work *dwork = to_delayed_work(work);
  318. struct blkio_group *blkg;
  319. int i;
  320. bool empty = false;
  321. alloc_stats:
  322. for (i = 0; i < BLKIO_NR_POLICIES; i++) {
  323. if (pcpu_stats[i] != NULL)
  324. continue;
  325. pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu);
  326. /* Allocation failed. Try again after some time. */
  327. if (pcpu_stats[i] == NULL) {
  328. queue_delayed_work(system_nrt_wq, dwork,
  329. msecs_to_jiffies(10));
  330. return;
  331. }
  332. }
  333. spin_lock_irq(&blkio_list_lock);
  334. spin_lock(&alloc_list_lock);
  335. /* cgroup got deleted or queue exited. */
  336. if (!list_empty(&alloc_list)) {
  337. blkg = list_first_entry(&alloc_list, struct blkio_group,
  338. alloc_node);
  339. for (i = 0; i < BLKIO_NR_POLICIES; i++) {
  340. struct blkg_policy_data *pd = blkg->pd[i];
  341. if (blkio_policy[i] && pd && !pd->stats_cpu)
  342. swap(pd->stats_cpu, pcpu_stats[i]);
  343. }
  344. list_del_init(&blkg->alloc_node);
  345. }
  346. empty = list_empty(&alloc_list);
  347. spin_unlock(&alloc_list_lock);
  348. spin_unlock_irq(&blkio_list_lock);
  349. if (!empty)
  350. goto alloc_stats;
  351. }
  352. /**
  353. * blkg_free - free a blkg
  354. * @blkg: blkg to free
  355. *
  356. * Free @blkg which may be partially allocated.
  357. */
  358. static void blkg_free(struct blkio_group *blkg)
  359. {
  360. int i;
  361. if (!blkg)
  362. return;
  363. for (i = 0; i < BLKIO_NR_POLICIES; i++) {
  364. struct blkg_policy_data *pd = blkg->pd[i];
  365. if (pd) {
  366. free_percpu(pd->stats_cpu);
  367. kfree(pd);
  368. }
  369. }
  370. kfree(blkg);
  371. }
  372. /**
  373. * blkg_alloc - allocate a blkg
  374. * @blkcg: block cgroup the new blkg is associated with
  375. * @q: request_queue the new blkg is associated with
  376. *
  377. * Allocate a new blkg assocating @blkcg and @q.
  378. */
  379. static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
  380. struct request_queue *q)
  381. {
  382. struct blkio_group *blkg;
  383. int i;
  384. /* alloc and init base part */
  385. blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
  386. if (!blkg)
  387. return NULL;
  388. blkg->q = q;
  389. INIT_LIST_HEAD(&blkg->q_node);
  390. INIT_LIST_HEAD(&blkg->alloc_node);
  391. blkg->blkcg = blkcg;
  392. blkg->refcnt = 1;
  393. cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
  394. for (i = 0; i < BLKIO_NR_POLICIES; i++) {
  395. struct blkio_policy_type *pol = blkio_policy[i];
  396. struct blkg_policy_data *pd;
  397. if (!pol)
  398. continue;
  399. /* alloc per-policy data and attach it to blkg */
  400. pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
  401. q->node);
  402. if (!pd) {
  403. blkg_free(blkg);
  404. return NULL;
  405. }
  406. blkg->pd[i] = pd;
  407. pd->blkg = blkg;
  408. }
  409. /* invoke per-policy init */
  410. for (i = 0; i < BLKIO_NR_POLICIES; i++) {
  411. struct blkio_policy_type *pol = blkio_policy[i];
  412. if (pol)
  413. pol->ops.blkio_init_group_fn(blkg);
  414. }
  415. return blkg;
  416. }
  417. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  418. struct request_queue *q,
  419. bool for_root)
  420. __releases(q->queue_lock) __acquires(q->queue_lock)
  421. {
  422. struct blkio_group *blkg;
  423. WARN_ON_ONCE(!rcu_read_lock_held());
  424. lockdep_assert_held(q->queue_lock);
  425. /*
  426. * This could be the first entry point of blkcg implementation and
  427. * we shouldn't allow anything to go through for a bypassing queue.
  428. * The following can be removed if blkg lookup is guaranteed to
  429. * fail on a bypassing queue.
  430. */
  431. if (unlikely(blk_queue_bypass(q)) && !for_root)
  432. return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
  433. blkg = blkg_lookup(blkcg, q);
  434. if (blkg)
  435. return blkg;
  436. /* blkg holds a reference to blkcg */
  437. if (!css_tryget(&blkcg->css))
  438. return ERR_PTR(-EINVAL);
  439. /*
  440. * Allocate and initialize.
  441. */
  442. blkg = blkg_alloc(blkcg, q);
  443. /* did alloc fail? */
  444. if (unlikely(!blkg)) {
  445. blkg = ERR_PTR(-ENOMEM);
  446. goto out;
  447. }
  448. /* insert */
  449. spin_lock(&blkcg->lock);
  450. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  451. list_add(&blkg->q_node, &q->blkg_list);
  452. spin_unlock(&blkcg->lock);
  453. spin_lock(&alloc_list_lock);
  454. list_add(&blkg->alloc_node, &alloc_list);
  455. /* Queue per cpu stat allocation from worker thread. */
  456. queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0);
  457. spin_unlock(&alloc_list_lock);
  458. out:
  459. return blkg;
  460. }
  461. EXPORT_SYMBOL_GPL(blkg_lookup_create);
  462. /* called under rcu_read_lock(). */
  463. struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  464. struct request_queue *q)
  465. {
  466. struct blkio_group *blkg;
  467. struct hlist_node *n;
  468. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
  469. if (blkg->q == q)
  470. return blkg;
  471. return NULL;
  472. }
  473. EXPORT_SYMBOL_GPL(blkg_lookup);
  474. static void blkg_destroy(struct blkio_group *blkg)
  475. {
  476. struct request_queue *q = blkg->q;
  477. struct blkio_cgroup *blkcg = blkg->blkcg;
  478. lockdep_assert_held(q->queue_lock);
  479. lockdep_assert_held(&blkcg->lock);
  480. /* Something wrong if we are trying to remove same group twice */
  481. WARN_ON_ONCE(list_empty(&blkg->q_node));
  482. WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
  483. list_del_init(&blkg->q_node);
  484. hlist_del_init_rcu(&blkg->blkcg_node);
  485. spin_lock(&alloc_list_lock);
  486. list_del_init(&blkg->alloc_node);
  487. spin_unlock(&alloc_list_lock);
  488. /*
  489. * Put the reference taken at the time of creation so that when all
  490. * queues are gone, group can be destroyed.
  491. */
  492. blkg_put(blkg);
  493. }
  494. /*
  495. * XXX: This updates blkg policy data in-place for root blkg, which is
  496. * necessary across elevator switch and policy registration as root blkgs
  497. * aren't shot down. This broken and racy implementation is temporary.
  498. * Eventually, blkg shoot down will be replaced by proper in-place update.
  499. */
  500. void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
  501. {
  502. struct blkio_policy_type *pol = blkio_policy[plid];
  503. struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
  504. struct blkg_policy_data *pd;
  505. if (!blkg)
  506. return;
  507. kfree(blkg->pd[plid]);
  508. blkg->pd[plid] = NULL;
  509. if (!pol)
  510. return;
  511. pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
  512. WARN_ON_ONCE(!pd);
  513. pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
  514. WARN_ON_ONCE(!pd->stats_cpu);
  515. blkg->pd[plid] = pd;
  516. pd->blkg = blkg;
  517. pol->ops.blkio_init_group_fn(blkg);
  518. }
  519. EXPORT_SYMBOL_GPL(update_root_blkg_pd);
  520. /**
  521. * blkg_destroy_all - destroy all blkgs associated with a request_queue
  522. * @q: request_queue of interest
  523. * @destroy_root: whether to destroy root blkg or not
  524. *
  525. * Destroy blkgs associated with @q. If @destroy_root is %true, all are
  526. * destroyed; otherwise, root blkg is left alone.
  527. */
  528. void blkg_destroy_all(struct request_queue *q, bool destroy_root)
  529. {
  530. struct blkio_group *blkg, *n;
  531. spin_lock_irq(q->queue_lock);
  532. list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
  533. struct blkio_cgroup *blkcg = blkg->blkcg;
  534. /* skip root? */
  535. if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
  536. continue;
  537. spin_lock(&blkcg->lock);
  538. blkg_destroy(blkg);
  539. spin_unlock(&blkcg->lock);
  540. }
  541. spin_unlock_irq(q->queue_lock);
  542. }
  543. EXPORT_SYMBOL_GPL(blkg_destroy_all);
  544. static void blkg_rcu_free(struct rcu_head *rcu_head)
  545. {
  546. blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
  547. }
  548. void __blkg_release(struct blkio_group *blkg)
  549. {
  550. /* release the extra blkcg reference this blkg has been holding */
  551. css_put(&blkg->blkcg->css);
  552. /*
  553. * A group is freed in rcu manner. But having an rcu lock does not
  554. * mean that one can access all the fields of blkg and assume these
  555. * are valid. For example, don't try to follow throtl_data and
  556. * request queue links.
  557. *
  558. * Having a reference to blkg under an rcu allows acess to only
  559. * values local to groups like group stats and group rate limits
  560. */
  561. call_rcu(&blkg->rcu_head, blkg_rcu_free);
  562. }
  563. EXPORT_SYMBOL_GPL(__blkg_release);
  564. static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
  565. {
  566. struct blkg_policy_data *pd = blkg->pd[plid];
  567. int cpu;
  568. if (pd->stats_cpu == NULL)
  569. return;
  570. for_each_possible_cpu(cpu) {
  571. struct blkio_group_stats_cpu *sc =
  572. per_cpu_ptr(pd->stats_cpu, cpu);
  573. blkg_rwstat_reset(&sc->service_bytes);
  574. blkg_rwstat_reset(&sc->serviced);
  575. blkg_stat_reset(&sc->sectors);
  576. }
  577. }
  578. static int
  579. blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
  580. {
  581. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  582. struct blkio_group *blkg;
  583. struct hlist_node *n;
  584. spin_lock(&blkio_list_lock);
  585. spin_lock_irq(&blkcg->lock);
  586. /*
  587. * Note that stat reset is racy - it doesn't synchronize against
  588. * stat updates. This is a debug feature which shouldn't exist
  589. * anyway. If you get hit by a race, retry.
  590. */
  591. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  592. struct blkio_policy_type *pol;
  593. list_for_each_entry(pol, &blkio_list, list) {
  594. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  595. struct blkio_group_stats *stats = &pd->stats;
  596. /* queued stats shouldn't be cleared */
  597. blkg_rwstat_reset(&stats->merged);
  598. blkg_rwstat_reset(&stats->service_time);
  599. blkg_rwstat_reset(&stats->wait_time);
  600. blkg_stat_reset(&stats->time);
  601. #ifdef CONFIG_DEBUG_BLK_CGROUP
  602. blkg_stat_reset(&stats->unaccounted_time);
  603. blkg_stat_reset(&stats->avg_queue_size_sum);
  604. blkg_stat_reset(&stats->avg_queue_size_samples);
  605. blkg_stat_reset(&stats->dequeue);
  606. blkg_stat_reset(&stats->group_wait_time);
  607. blkg_stat_reset(&stats->idle_time);
  608. blkg_stat_reset(&stats->empty_time);
  609. #endif
  610. blkio_reset_stats_cpu(blkg, pol->plid);
  611. }
  612. }
  613. spin_unlock_irq(&blkcg->lock);
  614. spin_unlock(&blkio_list_lock);
  615. return 0;
  616. }
  617. static const char *blkg_dev_name(struct blkio_group *blkg)
  618. {
  619. /* some drivers (floppy) instantiate a queue w/o disk registered */
  620. if (blkg->q->backing_dev_info.dev)
  621. return dev_name(blkg->q->backing_dev_info.dev);
  622. return NULL;
  623. }
  624. /**
  625. * blkcg_print_blkgs - helper for printing per-blkg data
  626. * @sf: seq_file to print to
  627. * @blkcg: blkcg of interest
  628. * @prfill: fill function to print out a blkg
  629. * @pol: policy in question
  630. * @data: data to be passed to @prfill
  631. * @show_total: to print out sum of prfill return values or not
  632. *
  633. * This function invokes @prfill on each blkg of @blkcg if pd for the
  634. * policy specified by @pol exists. @prfill is invoked with @sf, the
  635. * policy data and @data. If @show_total is %true, the sum of the return
  636. * values from @prfill is printed with "Total" label at the end.
  637. *
  638. * This is to be used to construct print functions for
  639. * cftype->read_seq_string method.
  640. */
  641. static void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
  642. u64 (*prfill)(struct seq_file *,
  643. struct blkg_policy_data *, int),
  644. int pol, int data, bool show_total)
  645. {
  646. struct blkio_group *blkg;
  647. struct hlist_node *n;
  648. u64 total = 0;
  649. spin_lock_irq(&blkcg->lock);
  650. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
  651. if (blkg->pd[pol])
  652. total += prfill(sf, blkg->pd[pol], data);
  653. spin_unlock_irq(&blkcg->lock);
  654. if (show_total)
  655. seq_printf(sf, "Total %llu\n", (unsigned long long)total);
  656. }
  657. /**
  658. * __blkg_prfill_u64 - prfill helper for a single u64 value
  659. * @sf: seq_file to print to
  660. * @pd: policy data of interest
  661. * @v: value to print
  662. *
  663. * Print @v to @sf for the device assocaited with @pd.
  664. */
  665. static u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd,
  666. u64 v)
  667. {
  668. const char *dname = blkg_dev_name(pd->blkg);
  669. if (!dname)
  670. return 0;
  671. seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
  672. return v;
  673. }
  674. /**
  675. * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
  676. * @sf: seq_file to print to
  677. * @pd: policy data of interest
  678. * @rwstat: rwstat to print
  679. *
  680. * Print @rwstat to @sf for the device assocaited with @pd.
  681. */
  682. static u64 __blkg_prfill_rwstat(struct seq_file *sf,
  683. struct blkg_policy_data *pd,
  684. const struct blkg_rwstat *rwstat)
  685. {
  686. static const char *rwstr[] = {
  687. [BLKG_RWSTAT_READ] = "Read",
  688. [BLKG_RWSTAT_WRITE] = "Write",
  689. [BLKG_RWSTAT_SYNC] = "Sync",
  690. [BLKG_RWSTAT_ASYNC] = "Async",
  691. };
  692. const char *dname = blkg_dev_name(pd->blkg);
  693. u64 v;
  694. int i;
  695. if (!dname)
  696. return 0;
  697. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  698. seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
  699. (unsigned long long)rwstat->cnt[i]);
  700. v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
  701. seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
  702. return v;
  703. }
  704. static u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd,
  705. int off)
  706. {
  707. return __blkg_prfill_u64(sf, pd,
  708. blkg_stat_read((void *)&pd->stats + off));
  709. }
  710. static u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  711. int off)
  712. {
  713. struct blkg_rwstat rwstat = blkg_rwstat_read((void *)&pd->stats + off);
  714. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  715. }
  716. /* print blkg_stat specified by BLKCG_STAT_PRIV() */
  717. static int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
  718. struct seq_file *sf)
  719. {
  720. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  721. blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat,
  722. BLKCG_STAT_POL(cft->private),
  723. BLKCG_STAT_OFF(cft->private), false);
  724. return 0;
  725. }
  726. /* print blkg_rwstat specified by BLKCG_STAT_PRIV() */
  727. static int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
  728. struct seq_file *sf)
  729. {
  730. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  731. blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat,
  732. BLKCG_STAT_POL(cft->private),
  733. BLKCG_STAT_OFF(cft->private), true);
  734. return 0;
  735. }
  736. static u64 blkg_prfill_cpu_stat(struct seq_file *sf,
  737. struct blkg_policy_data *pd, int off)
  738. {
  739. u64 v = 0;
  740. int cpu;
  741. for_each_possible_cpu(cpu) {
  742. struct blkio_group_stats_cpu *sc =
  743. per_cpu_ptr(pd->stats_cpu, cpu);
  744. v += blkg_stat_read((void *)sc + off);
  745. }
  746. return __blkg_prfill_u64(sf, pd, v);
  747. }
  748. static u64 blkg_prfill_cpu_rwstat(struct seq_file *sf,
  749. struct blkg_policy_data *pd, int off)
  750. {
  751. struct blkg_rwstat rwstat = { }, tmp;
  752. int i, cpu;
  753. for_each_possible_cpu(cpu) {
  754. struct blkio_group_stats_cpu *sc =
  755. per_cpu_ptr(pd->stats_cpu, cpu);
  756. tmp = blkg_rwstat_read((void *)sc + off);
  757. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  758. rwstat.cnt[i] += tmp.cnt[i];
  759. }
  760. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  761. }
  762. /* print per-cpu blkg_stat specified by BLKCG_STAT_PRIV() */
  763. static int blkcg_print_cpu_stat(struct cgroup *cgrp, struct cftype *cft,
  764. struct seq_file *sf)
  765. {
  766. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  767. blkcg_print_blkgs(sf, blkcg, blkg_prfill_cpu_stat,
  768. BLKCG_STAT_POL(cft->private),
  769. BLKCG_STAT_OFF(cft->private), false);
  770. return 0;
  771. }
  772. /* print per-cpu blkg_rwstat specified by BLKCG_STAT_PRIV() */
  773. static int blkcg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
  774. struct seq_file *sf)
  775. {
  776. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  777. blkcg_print_blkgs(sf, blkcg, blkg_prfill_cpu_rwstat,
  778. BLKCG_STAT_POL(cft->private),
  779. BLKCG_STAT_OFF(cft->private), true);
  780. return 0;
  781. }
  782. #ifdef CONFIG_DEBUG_BLK_CGROUP
  783. static u64 blkg_prfill_avg_queue_size(struct seq_file *sf,
  784. struct blkg_policy_data *pd, int off)
  785. {
  786. u64 samples = blkg_stat_read(&pd->stats.avg_queue_size_samples);
  787. u64 v = 0;
  788. if (samples) {
  789. v = blkg_stat_read(&pd->stats.avg_queue_size_sum);
  790. do_div(v, samples);
  791. }
  792. __blkg_prfill_u64(sf, pd, v);
  793. return 0;
  794. }
  795. /* print avg_queue_size */
  796. static int blkcg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft,
  797. struct seq_file *sf)
  798. {
  799. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  800. blkcg_print_blkgs(sf, blkcg, blkg_prfill_avg_queue_size,
  801. BLKIO_POLICY_PROP, 0, false);
  802. return 0;
  803. }
  804. #endif /* CONFIG_DEBUG_BLK_CGROUP */
  805. static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
  806. int fileid, struct blkio_cgroup *blkcg)
  807. {
  808. struct gendisk *disk = NULL;
  809. struct blkio_group *blkg = NULL;
  810. struct blkg_policy_data *pd;
  811. char *s[4], *p, *major_s = NULL, *minor_s = NULL;
  812. unsigned long major, minor;
  813. int i = 0, ret = -EINVAL;
  814. int part;
  815. dev_t dev;
  816. u64 temp;
  817. memset(s, 0, sizeof(s));
  818. while ((p = strsep(&buf, " ")) != NULL) {
  819. if (!*p)
  820. continue;
  821. s[i++] = p;
  822. /* Prevent from inputing too many things */
  823. if (i == 3)
  824. break;
  825. }
  826. if (i != 2)
  827. goto out;
  828. p = strsep(&s[0], ":");
  829. if (p != NULL)
  830. major_s = p;
  831. else
  832. goto out;
  833. minor_s = s[0];
  834. if (!minor_s)
  835. goto out;
  836. if (strict_strtoul(major_s, 10, &major))
  837. goto out;
  838. if (strict_strtoul(minor_s, 10, &minor))
  839. goto out;
  840. dev = MKDEV(major, minor);
  841. if (strict_strtoull(s[1], 10, &temp))
  842. goto out;
  843. disk = get_gendisk(dev, &part);
  844. if (!disk || part)
  845. goto out;
  846. rcu_read_lock();
  847. spin_lock_irq(disk->queue->queue_lock);
  848. blkg = blkg_lookup_create(blkcg, disk->queue, false);
  849. spin_unlock_irq(disk->queue->queue_lock);
  850. if (IS_ERR(blkg)) {
  851. ret = PTR_ERR(blkg);
  852. goto out_unlock;
  853. }
  854. pd = blkg->pd[plid];
  855. switch (plid) {
  856. case BLKIO_POLICY_PROP:
  857. if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
  858. temp > BLKIO_WEIGHT_MAX)
  859. goto out_unlock;
  860. pd->conf.weight = temp;
  861. blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
  862. break;
  863. case BLKIO_POLICY_THROTL:
  864. switch(fileid) {
  865. case BLKIO_THROTL_read_bps_device:
  866. pd->conf.bps[READ] = temp;
  867. blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
  868. break;
  869. case BLKIO_THROTL_write_bps_device:
  870. pd->conf.bps[WRITE] = temp;
  871. blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
  872. break;
  873. case BLKIO_THROTL_read_iops_device:
  874. if (temp > THROTL_IOPS_MAX)
  875. goto out_unlock;
  876. pd->conf.iops[READ] = temp;
  877. blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
  878. break;
  879. case BLKIO_THROTL_write_iops_device:
  880. if (temp > THROTL_IOPS_MAX)
  881. goto out_unlock;
  882. pd->conf.iops[WRITE] = temp;
  883. blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
  884. break;
  885. }
  886. break;
  887. default:
  888. BUG();
  889. }
  890. ret = 0;
  891. out_unlock:
  892. rcu_read_unlock();
  893. out:
  894. put_disk(disk);
  895. /*
  896. * If queue was bypassing, we should retry. Do so after a short
  897. * msleep(). It isn't strictly necessary but queue can be
  898. * bypassing for some time and it's always nice to avoid busy
  899. * looping.
  900. */
  901. if (ret == -EBUSY) {
  902. msleep(10);
  903. return restart_syscall();
  904. }
  905. return ret;
  906. }
  907. static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
  908. const char *buffer)
  909. {
  910. int ret = 0;
  911. char *buf;
  912. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  913. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  914. int fileid = BLKIOFILE_ATTR(cft->private);
  915. buf = kstrdup(buffer, GFP_KERNEL);
  916. if (!buf)
  917. return -ENOMEM;
  918. ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
  919. kfree(buf);
  920. return ret;
  921. }
  922. /* for propio conf */
  923. static u64 blkg_prfill_weight_device(struct seq_file *sf,
  924. struct blkg_policy_data *pd, int off)
  925. {
  926. if (!pd->conf.weight)
  927. return 0;
  928. return __blkg_prfill_u64(sf, pd, pd->conf.weight);
  929. }
  930. static int blkcg_print_weight_device(struct cgroup *cgrp, struct cftype *cft,
  931. struct seq_file *sf)
  932. {
  933. blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp),
  934. blkg_prfill_weight_device, BLKIO_POLICY_PROP, 0,
  935. false);
  936. return 0;
  937. }
  938. static int blkcg_print_weight(struct cgroup *cgrp, struct cftype *cft,
  939. struct seq_file *sf)
  940. {
  941. seq_printf(sf, "%u\n", cgroup_to_blkio_cgroup(cgrp)->weight);
  942. return 0;
  943. }
  944. static int blkcg_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
  945. {
  946. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  947. struct blkio_group *blkg;
  948. struct hlist_node *n;
  949. if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
  950. return -EINVAL;
  951. spin_lock(&blkio_list_lock);
  952. spin_lock_irq(&blkcg->lock);
  953. blkcg->weight = (unsigned int)val;
  954. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  955. struct blkg_policy_data *pd = blkg->pd[BLKIO_POLICY_PROP];
  956. if (pd && !pd->conf.weight)
  957. blkio_update_group_weight(blkg, BLKIO_POLICY_PROP,
  958. blkcg->weight);
  959. }
  960. spin_unlock_irq(&blkcg->lock);
  961. spin_unlock(&blkio_list_lock);
  962. return 0;
  963. }
  964. /* for blk-throttle conf */
  965. #ifdef CONFIG_BLK_DEV_THROTTLING
  966. static u64 blkg_prfill_conf_u64(struct seq_file *sf,
  967. struct blkg_policy_data *pd, int off)
  968. {
  969. u64 v = *(u64 *)((void *)&pd->conf + off);
  970. if (!v)
  971. return 0;
  972. return __blkg_prfill_u64(sf, pd, v);
  973. }
  974. static int blkcg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  975. struct seq_file *sf)
  976. {
  977. int off;
  978. switch (BLKIOFILE_ATTR(cft->private)) {
  979. case BLKIO_THROTL_read_bps_device:
  980. off = offsetof(struct blkio_group_conf, bps[READ]);
  981. break;
  982. case BLKIO_THROTL_write_bps_device:
  983. off = offsetof(struct blkio_group_conf, bps[WRITE]);
  984. break;
  985. case BLKIO_THROTL_read_iops_device:
  986. off = offsetof(struct blkio_group_conf, iops[READ]);
  987. break;
  988. case BLKIO_THROTL_write_iops_device:
  989. off = offsetof(struct blkio_group_conf, iops[WRITE]);
  990. break;
  991. default:
  992. return -EINVAL;
  993. }
  994. blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp),
  995. blkg_prfill_conf_u64, BLKIO_POLICY_THROTL,
  996. off, false);
  997. return 0;
  998. }
  999. #endif
  1000. struct cftype blkio_files[] = {
  1001. {
  1002. .name = "weight_device",
  1003. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1004. BLKIO_PROP_weight_device),
  1005. .read_seq_string = blkcg_print_weight_device,
  1006. .write_string = blkiocg_file_write,
  1007. .max_write_len = 256,
  1008. },
  1009. {
  1010. .name = "weight",
  1011. .read_seq_string = blkcg_print_weight,
  1012. .write_u64 = blkcg_set_weight,
  1013. },
  1014. {
  1015. .name = "time",
  1016. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
  1017. offsetof(struct blkio_group_stats, time)),
  1018. .read_seq_string = blkcg_print_stat,
  1019. },
  1020. {
  1021. .name = "sectors",
  1022. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
  1023. offsetof(struct blkio_group_stats_cpu, sectors)),
  1024. .read_seq_string = blkcg_print_cpu_stat,
  1025. },
  1026. {
  1027. .name = "io_service_bytes",
  1028. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
  1029. offsetof(struct blkio_group_stats_cpu, service_bytes)),
  1030. .read_seq_string = blkcg_print_cpu_rwstat,
  1031. },
  1032. {
  1033. .name = "io_serviced",
  1034. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
  1035. offsetof(struct blkio_group_stats_cpu, serviced)),
  1036. .read_seq_string = blkcg_print_cpu_rwstat,
  1037. },
  1038. {
  1039. .name = "io_service_time",
  1040. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
  1041. offsetof(struct blkio_group_stats, service_time)),
  1042. .read_seq_string = blkcg_print_rwstat,
  1043. },
  1044. {
  1045. .name = "io_wait_time",
  1046. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
  1047. offsetof(struct blkio_group_stats, wait_time)),
  1048. .read_seq_string = blkcg_print_rwstat,
  1049. },
  1050. {
  1051. .name = "io_merged",
  1052. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
  1053. offsetof(struct blkio_group_stats, merged)),
  1054. .read_seq_string = blkcg_print_rwstat,
  1055. },
  1056. {
  1057. .name = "io_queued",
  1058. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
  1059. offsetof(struct blkio_group_stats, queued)),
  1060. .read_seq_string = blkcg_print_rwstat,
  1061. },
  1062. {
  1063. .name = "reset_stats",
  1064. .write_u64 = blkiocg_reset_stats,
  1065. },
  1066. #ifdef CONFIG_BLK_DEV_THROTTLING
  1067. {
  1068. .name = "throttle.read_bps_device",
  1069. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1070. BLKIO_THROTL_read_bps_device),
  1071. .read_seq_string = blkcg_print_conf_u64,
  1072. .write_string = blkiocg_file_write,
  1073. .max_write_len = 256,
  1074. },
  1075. {
  1076. .name = "throttle.write_bps_device",
  1077. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1078. BLKIO_THROTL_write_bps_device),
  1079. .read_seq_string = blkcg_print_conf_u64,
  1080. .write_string = blkiocg_file_write,
  1081. .max_write_len = 256,
  1082. },
  1083. {
  1084. .name = "throttle.read_iops_device",
  1085. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1086. BLKIO_THROTL_read_iops_device),
  1087. .read_seq_string = blkcg_print_conf_u64,
  1088. .write_string = blkiocg_file_write,
  1089. .max_write_len = 256,
  1090. },
  1091. {
  1092. .name = "throttle.write_iops_device",
  1093. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1094. BLKIO_THROTL_write_iops_device),
  1095. .read_seq_string = blkcg_print_conf_u64,
  1096. .write_string = blkiocg_file_write,
  1097. .max_write_len = 256,
  1098. },
  1099. {
  1100. .name = "throttle.io_service_bytes",
  1101. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL,
  1102. offsetof(struct blkio_group_stats_cpu, service_bytes)),
  1103. .read_seq_string = blkcg_print_cpu_rwstat,
  1104. },
  1105. {
  1106. .name = "throttle.io_serviced",
  1107. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL,
  1108. offsetof(struct blkio_group_stats_cpu, serviced)),
  1109. .read_seq_string = blkcg_print_cpu_rwstat,
  1110. },
  1111. #endif /* CONFIG_BLK_DEV_THROTTLING */
  1112. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1113. {
  1114. .name = "avg_queue_size",
  1115. .read_seq_string = blkcg_print_avg_queue_size,
  1116. },
  1117. {
  1118. .name = "group_wait_time",
  1119. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
  1120. offsetof(struct blkio_group_stats, group_wait_time)),
  1121. .read_seq_string = blkcg_print_stat,
  1122. },
  1123. {
  1124. .name = "idle_time",
  1125. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
  1126. offsetof(struct blkio_group_stats, idle_time)),
  1127. .read_seq_string = blkcg_print_stat,
  1128. },
  1129. {
  1130. .name = "empty_time",
  1131. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
  1132. offsetof(struct blkio_group_stats, empty_time)),
  1133. .read_seq_string = blkcg_print_stat,
  1134. },
  1135. {
  1136. .name = "dequeue",
  1137. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
  1138. offsetof(struct blkio_group_stats, dequeue)),
  1139. .read_seq_string = blkcg_print_stat,
  1140. },
  1141. {
  1142. .name = "unaccounted_time",
  1143. .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
  1144. offsetof(struct blkio_group_stats, unaccounted_time)),
  1145. .read_seq_string = blkcg_print_stat,
  1146. },
  1147. #endif
  1148. { } /* terminate */
  1149. };
  1150. /**
  1151. * blkiocg_pre_destroy - cgroup pre_destroy callback
  1152. * @cgroup: cgroup of interest
  1153. *
  1154. * This function is called when @cgroup is about to go away and responsible
  1155. * for shooting down all blkgs associated with @cgroup. blkgs should be
  1156. * removed while holding both q and blkcg locks. As blkcg lock is nested
  1157. * inside q lock, this function performs reverse double lock dancing.
  1158. *
  1159. * This is the blkcg counterpart of ioc_release_fn().
  1160. */
  1161. static int blkiocg_pre_destroy(struct cgroup *cgroup)
  1162. {
  1163. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  1164. spin_lock_irq(&blkcg->lock);
  1165. while (!hlist_empty(&blkcg->blkg_list)) {
  1166. struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
  1167. struct blkio_group, blkcg_node);
  1168. struct request_queue *q = blkg->q;
  1169. if (spin_trylock(q->queue_lock)) {
  1170. blkg_destroy(blkg);
  1171. spin_unlock(q->queue_lock);
  1172. } else {
  1173. spin_unlock_irq(&blkcg->lock);
  1174. cpu_relax();
  1175. spin_lock_irq(&blkcg->lock);
  1176. }
  1177. }
  1178. spin_unlock_irq(&blkcg->lock);
  1179. return 0;
  1180. }
  1181. static void blkiocg_destroy(struct cgroup *cgroup)
  1182. {
  1183. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  1184. if (blkcg != &blkio_root_cgroup)
  1185. kfree(blkcg);
  1186. }
  1187. static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup)
  1188. {
  1189. static atomic64_t id_seq = ATOMIC64_INIT(0);
  1190. struct blkio_cgroup *blkcg;
  1191. struct cgroup *parent = cgroup->parent;
  1192. if (!parent) {
  1193. blkcg = &blkio_root_cgroup;
  1194. goto done;
  1195. }
  1196. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  1197. if (!blkcg)
  1198. return ERR_PTR(-ENOMEM);
  1199. blkcg->weight = BLKIO_WEIGHT_DEFAULT;
  1200. blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
  1201. done:
  1202. spin_lock_init(&blkcg->lock);
  1203. INIT_HLIST_HEAD(&blkcg->blkg_list);
  1204. return &blkcg->css;
  1205. }
  1206. /**
  1207. * blkcg_init_queue - initialize blkcg part of request queue
  1208. * @q: request_queue to initialize
  1209. *
  1210. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  1211. * part of new request_queue @q.
  1212. *
  1213. * RETURNS:
  1214. * 0 on success, -errno on failure.
  1215. */
  1216. int blkcg_init_queue(struct request_queue *q)
  1217. {
  1218. int ret;
  1219. might_sleep();
  1220. ret = blk_throtl_init(q);
  1221. if (ret)
  1222. return ret;
  1223. mutex_lock(&all_q_mutex);
  1224. INIT_LIST_HEAD(&q->all_q_node);
  1225. list_add_tail(&q->all_q_node, &all_q_list);
  1226. mutex_unlock(&all_q_mutex);
  1227. return 0;
  1228. }
  1229. /**
  1230. * blkcg_drain_queue - drain blkcg part of request_queue
  1231. * @q: request_queue to drain
  1232. *
  1233. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  1234. */
  1235. void blkcg_drain_queue(struct request_queue *q)
  1236. {
  1237. lockdep_assert_held(q->queue_lock);
  1238. blk_throtl_drain(q);
  1239. }
  1240. /**
  1241. * blkcg_exit_queue - exit and release blkcg part of request_queue
  1242. * @q: request_queue being released
  1243. *
  1244. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  1245. */
  1246. void blkcg_exit_queue(struct request_queue *q)
  1247. {
  1248. mutex_lock(&all_q_mutex);
  1249. list_del_init(&q->all_q_node);
  1250. mutex_unlock(&all_q_mutex);
  1251. blkg_destroy_all(q, true);
  1252. blk_throtl_exit(q);
  1253. }
  1254. /*
  1255. * We cannot support shared io contexts, as we have no mean to support
  1256. * two tasks with the same ioc in two different groups without major rework
  1257. * of the main cic data structures. For now we allow a task to change
  1258. * its cgroup only if it's the only owner of its ioc.
  1259. */
  1260. static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
  1261. {
  1262. struct task_struct *task;
  1263. struct io_context *ioc;
  1264. int ret = 0;
  1265. /* task_lock() is needed to avoid races with exit_io_context() */
  1266. cgroup_taskset_for_each(task, cgrp, tset) {
  1267. task_lock(task);
  1268. ioc = task->io_context;
  1269. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  1270. ret = -EINVAL;
  1271. task_unlock(task);
  1272. if (ret)
  1273. break;
  1274. }
  1275. return ret;
  1276. }
  1277. static void blkcg_bypass_start(void)
  1278. __acquires(&all_q_mutex)
  1279. {
  1280. struct request_queue *q;
  1281. mutex_lock(&all_q_mutex);
  1282. list_for_each_entry(q, &all_q_list, all_q_node) {
  1283. blk_queue_bypass_start(q);
  1284. blkg_destroy_all(q, false);
  1285. }
  1286. }
  1287. static void blkcg_bypass_end(void)
  1288. __releases(&all_q_mutex)
  1289. {
  1290. struct request_queue *q;
  1291. list_for_each_entry(q, &all_q_list, all_q_node)
  1292. blk_queue_bypass_end(q);
  1293. mutex_unlock(&all_q_mutex);
  1294. }
  1295. struct cgroup_subsys blkio_subsys = {
  1296. .name = "blkio",
  1297. .create = blkiocg_create,
  1298. .can_attach = blkiocg_can_attach,
  1299. .pre_destroy = blkiocg_pre_destroy,
  1300. .destroy = blkiocg_destroy,
  1301. .subsys_id = blkio_subsys_id,
  1302. .base_cftypes = blkio_files,
  1303. .module = THIS_MODULE,
  1304. };
  1305. EXPORT_SYMBOL_GPL(blkio_subsys);
  1306. void blkio_policy_register(struct blkio_policy_type *blkiop)
  1307. {
  1308. struct request_queue *q;
  1309. blkcg_bypass_start();
  1310. spin_lock(&blkio_list_lock);
  1311. BUG_ON(blkio_policy[blkiop->plid]);
  1312. blkio_policy[blkiop->plid] = blkiop;
  1313. list_add_tail(&blkiop->list, &blkio_list);
  1314. spin_unlock(&blkio_list_lock);
  1315. list_for_each_entry(q, &all_q_list, all_q_node)
  1316. update_root_blkg_pd(q, blkiop->plid);
  1317. blkcg_bypass_end();
  1318. }
  1319. EXPORT_SYMBOL_GPL(blkio_policy_register);
  1320. void blkio_policy_unregister(struct blkio_policy_type *blkiop)
  1321. {
  1322. struct request_queue *q;
  1323. blkcg_bypass_start();
  1324. spin_lock(&blkio_list_lock);
  1325. BUG_ON(blkio_policy[blkiop->plid] != blkiop);
  1326. blkio_policy[blkiop->plid] = NULL;
  1327. list_del_init(&blkiop->list);
  1328. spin_unlock(&blkio_list_lock);
  1329. list_for_each_entry(q, &all_q_list, all_q_node)
  1330. update_root_blkg_pd(q, blkiop->plid);
  1331. blkcg_bypass_end();
  1332. }
  1333. EXPORT_SYMBOL_GPL(blkio_policy_unregister);