blk-cgroup.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  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 "blk-cgroup.h"
  23. #include "blk.h"
  24. #define MAX_KEY_LEN 100
  25. static DEFINE_SPINLOCK(blkio_list_lock);
  26. static LIST_HEAD(blkio_list);
  27. static DEFINE_MUTEX(all_q_mutex);
  28. static LIST_HEAD(all_q_list);
  29. struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
  30. EXPORT_SYMBOL_GPL(blkio_root_cgroup);
  31. static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
  32. static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
  33. struct cgroup *);
  34. static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
  35. struct cgroup_taskset *);
  36. static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
  37. struct cgroup_taskset *);
  38. static int blkiocg_pre_destroy(struct cgroup_subsys *, struct cgroup *);
  39. static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
  40. static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
  41. /* for encoding cft->private value on file */
  42. #define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
  43. /* What policy owns the file, proportional or throttle */
  44. #define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
  45. #define BLKIOFILE_ATTR(val) ((val) & 0xffff)
  46. struct cgroup_subsys blkio_subsys = {
  47. .name = "blkio",
  48. .create = blkiocg_create,
  49. .can_attach = blkiocg_can_attach,
  50. .attach = blkiocg_attach,
  51. .pre_destroy = blkiocg_pre_destroy,
  52. .destroy = blkiocg_destroy,
  53. .populate = blkiocg_populate,
  54. .subsys_id = blkio_subsys_id,
  55. .module = THIS_MODULE,
  56. };
  57. EXPORT_SYMBOL_GPL(blkio_subsys);
  58. struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
  59. {
  60. return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
  61. struct blkio_cgroup, css);
  62. }
  63. EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
  64. struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
  65. {
  66. return container_of(task_subsys_state(tsk, blkio_subsys_id),
  67. struct blkio_cgroup, css);
  68. }
  69. EXPORT_SYMBOL_GPL(task_blkio_cgroup);
  70. static inline void blkio_update_group_weight(struct blkio_group *blkg,
  71. int plid, unsigned int weight)
  72. {
  73. struct blkio_policy_type *blkiop;
  74. list_for_each_entry(blkiop, &blkio_list, list) {
  75. /* If this policy does not own the blkg, do not send updates */
  76. if (blkiop->plid != plid)
  77. continue;
  78. if (blkiop->ops.blkio_update_group_weight_fn)
  79. blkiop->ops.blkio_update_group_weight_fn(blkg->q,
  80. blkg, weight);
  81. }
  82. }
  83. static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid,
  84. u64 bps, int fileid)
  85. {
  86. struct blkio_policy_type *blkiop;
  87. list_for_each_entry(blkiop, &blkio_list, list) {
  88. /* If this policy does not own the blkg, do not send updates */
  89. if (blkiop->plid != plid)
  90. continue;
  91. if (fileid == BLKIO_THROTL_read_bps_device
  92. && blkiop->ops.blkio_update_group_read_bps_fn)
  93. blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
  94. blkg, bps);
  95. if (fileid == BLKIO_THROTL_write_bps_device
  96. && blkiop->ops.blkio_update_group_write_bps_fn)
  97. blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
  98. blkg, bps);
  99. }
  100. }
  101. static inline void blkio_update_group_iops(struct blkio_group *blkg,
  102. int plid, unsigned int iops,
  103. int fileid)
  104. {
  105. struct blkio_policy_type *blkiop;
  106. list_for_each_entry(blkiop, &blkio_list, list) {
  107. /* If this policy does not own the blkg, do not send updates */
  108. if (blkiop->plid != plid)
  109. continue;
  110. if (fileid == BLKIO_THROTL_read_iops_device
  111. && blkiop->ops.blkio_update_group_read_iops_fn)
  112. blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
  113. blkg, iops);
  114. if (fileid == BLKIO_THROTL_write_iops_device
  115. && blkiop->ops.blkio_update_group_write_iops_fn)
  116. blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
  117. blkg,iops);
  118. }
  119. }
  120. /*
  121. * Add to the appropriate stat variable depending on the request type.
  122. * This should be called with the blkg->stats_lock held.
  123. */
  124. static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
  125. bool sync)
  126. {
  127. if (direction)
  128. stat[BLKIO_STAT_WRITE] += add;
  129. else
  130. stat[BLKIO_STAT_READ] += add;
  131. if (sync)
  132. stat[BLKIO_STAT_SYNC] += add;
  133. else
  134. stat[BLKIO_STAT_ASYNC] += add;
  135. }
  136. /*
  137. * Decrements the appropriate stat variable if non-zero depending on the
  138. * request type. Panics on value being zero.
  139. * This should be called with the blkg->stats_lock held.
  140. */
  141. static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
  142. {
  143. if (direction) {
  144. BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
  145. stat[BLKIO_STAT_WRITE]--;
  146. } else {
  147. BUG_ON(stat[BLKIO_STAT_READ] == 0);
  148. stat[BLKIO_STAT_READ]--;
  149. }
  150. if (sync) {
  151. BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
  152. stat[BLKIO_STAT_SYNC]--;
  153. } else {
  154. BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
  155. stat[BLKIO_STAT_ASYNC]--;
  156. }
  157. }
  158. #ifdef CONFIG_DEBUG_BLK_CGROUP
  159. /* This should be called with the blkg->stats_lock held. */
  160. static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
  161. struct blkio_policy_type *pol,
  162. struct blkio_group *curr_blkg)
  163. {
  164. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  165. if (blkio_blkg_waiting(&pd->stats))
  166. return;
  167. if (blkg == curr_blkg)
  168. return;
  169. pd->stats.start_group_wait_time = sched_clock();
  170. blkio_mark_blkg_waiting(&pd->stats);
  171. }
  172. /* This should be called with the blkg->stats_lock held. */
  173. static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
  174. {
  175. unsigned long long now;
  176. if (!blkio_blkg_waiting(stats))
  177. return;
  178. now = sched_clock();
  179. if (time_after64(now, stats->start_group_wait_time))
  180. stats->group_wait_time += now - stats->start_group_wait_time;
  181. blkio_clear_blkg_waiting(stats);
  182. }
  183. /* This should be called with the blkg->stats_lock held. */
  184. static void blkio_end_empty_time(struct blkio_group_stats *stats)
  185. {
  186. unsigned long long now;
  187. if (!blkio_blkg_empty(stats))
  188. return;
  189. now = sched_clock();
  190. if (time_after64(now, stats->start_empty_time))
  191. stats->empty_time += now - stats->start_empty_time;
  192. blkio_clear_blkg_empty(stats);
  193. }
  194. void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
  195. struct blkio_policy_type *pol)
  196. {
  197. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  198. unsigned long flags;
  199. spin_lock_irqsave(&blkg->stats_lock, flags);
  200. BUG_ON(blkio_blkg_idling(&pd->stats));
  201. pd->stats.start_idle_time = sched_clock();
  202. blkio_mark_blkg_idling(&pd->stats);
  203. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  204. }
  205. EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
  206. void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
  207. struct blkio_policy_type *pol)
  208. {
  209. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  210. unsigned long flags;
  211. unsigned long long now;
  212. struct blkio_group_stats *stats;
  213. spin_lock_irqsave(&blkg->stats_lock, flags);
  214. stats = &pd->stats;
  215. if (blkio_blkg_idling(stats)) {
  216. now = sched_clock();
  217. if (time_after64(now, stats->start_idle_time))
  218. stats->idle_time += now - stats->start_idle_time;
  219. blkio_clear_blkg_idling(stats);
  220. }
  221. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  222. }
  223. EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
  224. void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
  225. struct blkio_policy_type *pol)
  226. {
  227. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  228. unsigned long flags;
  229. struct blkio_group_stats *stats;
  230. spin_lock_irqsave(&blkg->stats_lock, flags);
  231. stats = &pd->stats;
  232. stats->avg_queue_size_sum +=
  233. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
  234. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
  235. stats->avg_queue_size_samples++;
  236. blkio_update_group_wait_time(stats);
  237. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  238. }
  239. EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
  240. void blkiocg_set_start_empty_time(struct blkio_group *blkg,
  241. struct blkio_policy_type *pol)
  242. {
  243. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  244. unsigned long flags;
  245. struct blkio_group_stats *stats;
  246. spin_lock_irqsave(&blkg->stats_lock, flags);
  247. stats = &pd->stats;
  248. if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
  249. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
  250. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  251. return;
  252. }
  253. /*
  254. * group is already marked empty. This can happen if cfqq got new
  255. * request in parent group and moved to this group while being added
  256. * to service tree. Just ignore the event and move on.
  257. */
  258. if(blkio_blkg_empty(stats)) {
  259. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  260. return;
  261. }
  262. stats->start_empty_time = sched_clock();
  263. blkio_mark_blkg_empty(stats);
  264. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  265. }
  266. EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
  267. void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  268. struct blkio_policy_type *pol,
  269. unsigned long dequeue)
  270. {
  271. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  272. pd->stats.dequeue += dequeue;
  273. }
  274. EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
  275. #else
  276. static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
  277. struct blkio_policy_type *pol,
  278. struct blkio_group *curr_blkg) { }
  279. static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { }
  280. #endif
  281. void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  282. struct blkio_policy_type *pol,
  283. struct blkio_group *curr_blkg, bool direction,
  284. bool sync)
  285. {
  286. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  287. unsigned long flags;
  288. spin_lock_irqsave(&blkg->stats_lock, flags);
  289. blkio_add_stat(pd->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
  290. sync);
  291. blkio_end_empty_time(&pd->stats);
  292. blkio_set_start_group_wait_time(blkg, pol, curr_blkg);
  293. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  294. }
  295. EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
  296. void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  297. struct blkio_policy_type *pol,
  298. bool direction, bool sync)
  299. {
  300. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  301. unsigned long flags;
  302. spin_lock_irqsave(&blkg->stats_lock, flags);
  303. blkio_check_and_dec_stat(pd->stats.stat_arr[BLKIO_STAT_QUEUED],
  304. direction, sync);
  305. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  306. }
  307. EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
  308. void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  309. struct blkio_policy_type *pol,
  310. unsigned long time,
  311. unsigned long unaccounted_time)
  312. {
  313. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  314. unsigned long flags;
  315. spin_lock_irqsave(&blkg->stats_lock, flags);
  316. pd->stats.time += time;
  317. #ifdef CONFIG_DEBUG_BLK_CGROUP
  318. pd->stats.unaccounted_time += unaccounted_time;
  319. #endif
  320. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  321. }
  322. EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
  323. /*
  324. * should be called under rcu read lock or queue lock to make sure blkg pointer
  325. * is valid.
  326. */
  327. void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  328. struct blkio_policy_type *pol,
  329. uint64_t bytes, bool direction, bool sync)
  330. {
  331. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  332. struct blkio_group_stats_cpu *stats_cpu;
  333. unsigned long flags;
  334. /*
  335. * Disabling interrupts to provide mutual exclusion between two
  336. * writes on same cpu. It probably is not needed for 64bit. Not
  337. * optimizing that case yet.
  338. */
  339. local_irq_save(flags);
  340. stats_cpu = this_cpu_ptr(pd->stats_cpu);
  341. u64_stats_update_begin(&stats_cpu->syncp);
  342. stats_cpu->sectors += bytes >> 9;
  343. blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
  344. 1, direction, sync);
  345. blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
  346. bytes, direction, sync);
  347. u64_stats_update_end(&stats_cpu->syncp);
  348. local_irq_restore(flags);
  349. }
  350. EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
  351. void blkiocg_update_completion_stats(struct blkio_group *blkg,
  352. struct blkio_policy_type *pol,
  353. uint64_t start_time,
  354. uint64_t io_start_time, bool direction,
  355. bool sync)
  356. {
  357. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  358. struct blkio_group_stats *stats;
  359. unsigned long flags;
  360. unsigned long long now = sched_clock();
  361. spin_lock_irqsave(&blkg->stats_lock, flags);
  362. stats = &pd->stats;
  363. if (time_after64(now, io_start_time))
  364. blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
  365. now - io_start_time, direction, sync);
  366. if (time_after64(io_start_time, start_time))
  367. blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
  368. io_start_time - start_time, direction, sync);
  369. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  370. }
  371. EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
  372. /* Merged stats are per cpu. */
  373. void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  374. struct blkio_policy_type *pol,
  375. bool direction, bool sync)
  376. {
  377. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  378. struct blkio_group_stats_cpu *stats_cpu;
  379. unsigned long flags;
  380. /*
  381. * Disabling interrupts to provide mutual exclusion between two
  382. * writes on same cpu. It probably is not needed for 64bit. Not
  383. * optimizing that case yet.
  384. */
  385. local_irq_save(flags);
  386. stats_cpu = this_cpu_ptr(pd->stats_cpu);
  387. u64_stats_update_begin(&stats_cpu->syncp);
  388. blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1,
  389. direction, sync);
  390. u64_stats_update_end(&stats_cpu->syncp);
  391. local_irq_restore(flags);
  392. }
  393. EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
  394. /**
  395. * blkg_free - free a blkg
  396. * @blkg: blkg to free
  397. *
  398. * Free @blkg which may be partially allocated.
  399. */
  400. static void blkg_free(struct blkio_group *blkg)
  401. {
  402. struct blkg_policy_data *pd;
  403. if (!blkg)
  404. return;
  405. pd = blkg->pd[blkg->plid];
  406. if (pd) {
  407. free_percpu(pd->stats_cpu);
  408. kfree(pd);
  409. }
  410. kfree(blkg);
  411. }
  412. /**
  413. * blkg_alloc - allocate a blkg
  414. * @blkcg: block cgroup the new blkg is associated with
  415. * @q: request_queue the new blkg is associated with
  416. * @pol: policy the new blkg is associated with
  417. *
  418. * Allocate a new blkg assocating @blkcg and @q for @pol.
  419. *
  420. * FIXME: Should be called with queue locked but currently isn't due to
  421. * percpu stat breakage.
  422. */
  423. static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
  424. struct request_queue *q,
  425. struct blkio_policy_type *pol)
  426. {
  427. struct blkio_group *blkg;
  428. struct blkg_policy_data *pd;
  429. /* alloc and init base part */
  430. blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
  431. if (!blkg)
  432. return NULL;
  433. spin_lock_init(&blkg->stats_lock);
  434. rcu_assign_pointer(blkg->q, q);
  435. blkg->blkcg = blkcg;
  436. blkg->plid = pol->plid;
  437. blkg->refcnt = 1;
  438. cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
  439. /* alloc per-policy data and attach it to blkg */
  440. pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
  441. q->node);
  442. if (!pd) {
  443. blkg_free(blkg);
  444. return NULL;
  445. }
  446. blkg->pd[pol->plid] = pd;
  447. pd->blkg = blkg;
  448. /* broken, read comment in the callsite */
  449. pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
  450. if (!pd->stats_cpu) {
  451. blkg_free(blkg);
  452. return NULL;
  453. }
  454. /* invoke per-policy init */
  455. pol->ops.blkio_init_group_fn(blkg);
  456. return blkg;
  457. }
  458. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  459. struct request_queue *q,
  460. enum blkio_policy_id plid,
  461. bool for_root)
  462. __releases(q->queue_lock) __acquires(q->queue_lock)
  463. {
  464. struct blkio_policy_type *pol = blkio_policy[plid];
  465. struct blkio_group *blkg, *new_blkg;
  466. WARN_ON_ONCE(!rcu_read_lock_held());
  467. lockdep_assert_held(q->queue_lock);
  468. /*
  469. * This could be the first entry point of blkcg implementation and
  470. * we shouldn't allow anything to go through for a bypassing queue.
  471. * The following can be removed if blkg lookup is guaranteed to
  472. * fail on a bypassing queue.
  473. */
  474. if (unlikely(blk_queue_bypass(q)) && !for_root)
  475. return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
  476. blkg = blkg_lookup(blkcg, q, plid);
  477. if (blkg)
  478. return blkg;
  479. /* blkg holds a reference to blkcg */
  480. if (!css_tryget(&blkcg->css))
  481. return ERR_PTR(-EINVAL);
  482. /*
  483. * Allocate and initialize.
  484. *
  485. * FIXME: The following is broken. Percpu memory allocation
  486. * requires %GFP_KERNEL context and can't be performed from IO
  487. * path. Allocation here should inherently be atomic and the
  488. * following lock dancing can be removed once the broken percpu
  489. * allocation is fixed.
  490. */
  491. spin_unlock_irq(q->queue_lock);
  492. rcu_read_unlock();
  493. new_blkg = blkg_alloc(blkcg, q, pol);
  494. rcu_read_lock();
  495. spin_lock_irq(q->queue_lock);
  496. /* did bypass get turned on inbetween? */
  497. if (unlikely(blk_queue_bypass(q)) && !for_root) {
  498. blkg = ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
  499. goto out;
  500. }
  501. /* did someone beat us to it? */
  502. blkg = blkg_lookup(blkcg, q, plid);
  503. if (unlikely(blkg))
  504. goto out;
  505. /* did alloc fail? */
  506. if (unlikely(!new_blkg)) {
  507. blkg = ERR_PTR(-ENOMEM);
  508. goto out;
  509. }
  510. /* insert */
  511. spin_lock(&blkcg->lock);
  512. swap(blkg, new_blkg);
  513. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  514. pol->ops.blkio_link_group_fn(q, blkg);
  515. spin_unlock(&blkcg->lock);
  516. out:
  517. blkg_free(new_blkg);
  518. return blkg;
  519. }
  520. EXPORT_SYMBOL_GPL(blkg_lookup_create);
  521. static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
  522. {
  523. hlist_del_init_rcu(&blkg->blkcg_node);
  524. }
  525. /*
  526. * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
  527. * indicating that blk_group was unhashed by the time we got to it.
  528. */
  529. int blkiocg_del_blkio_group(struct blkio_group *blkg)
  530. {
  531. struct blkio_cgroup *blkcg = blkg->blkcg;
  532. unsigned long flags;
  533. int ret = 1;
  534. spin_lock_irqsave(&blkcg->lock, flags);
  535. if (!hlist_unhashed(&blkg->blkcg_node)) {
  536. __blkiocg_del_blkio_group(blkg);
  537. ret = 0;
  538. }
  539. spin_unlock_irqrestore(&blkcg->lock, flags);
  540. return ret;
  541. }
  542. EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
  543. /* called under rcu_read_lock(). */
  544. struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  545. struct request_queue *q,
  546. enum blkio_policy_id plid)
  547. {
  548. struct blkio_group *blkg;
  549. struct hlist_node *n;
  550. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
  551. if (blkg->q == q && blkg->plid == plid)
  552. return blkg;
  553. return NULL;
  554. }
  555. EXPORT_SYMBOL_GPL(blkg_lookup);
  556. void blkg_destroy_all(struct request_queue *q)
  557. {
  558. struct blkio_policy_type *pol;
  559. while (true) {
  560. bool done = true;
  561. spin_lock(&blkio_list_lock);
  562. spin_lock_irq(q->queue_lock);
  563. /*
  564. * clear_queue_fn() might return with non-empty group list
  565. * if it raced cgroup removal and lost. cgroup removal is
  566. * guaranteed to make forward progress and retrying after a
  567. * while is enough. This ugliness is scheduled to be
  568. * removed after locking update.
  569. */
  570. list_for_each_entry(pol, &blkio_list, list)
  571. if (!pol->ops.blkio_clear_queue_fn(q))
  572. done = false;
  573. spin_unlock_irq(q->queue_lock);
  574. spin_unlock(&blkio_list_lock);
  575. if (done)
  576. break;
  577. msleep(10); /* just some random duration I like */
  578. }
  579. }
  580. static void blkg_rcu_free(struct rcu_head *rcu_head)
  581. {
  582. blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
  583. }
  584. void __blkg_release(struct blkio_group *blkg)
  585. {
  586. /* release the extra blkcg reference this blkg has been holding */
  587. css_put(&blkg->blkcg->css);
  588. /*
  589. * A group is freed in rcu manner. But having an rcu lock does not
  590. * mean that one can access all the fields of blkg and assume these
  591. * are valid. For example, don't try to follow throtl_data and
  592. * request queue links.
  593. *
  594. * Having a reference to blkg under an rcu allows acess to only
  595. * values local to groups like group stats and group rate limits
  596. */
  597. call_rcu(&blkg->rcu_head, blkg_rcu_free);
  598. }
  599. EXPORT_SYMBOL_GPL(__blkg_release);
  600. static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
  601. {
  602. struct blkg_policy_data *pd = blkg->pd[plid];
  603. struct blkio_group_stats_cpu *stats_cpu;
  604. int i, j, k;
  605. /*
  606. * Note: On 64 bit arch this should not be an issue. This has the
  607. * possibility of returning some inconsistent value on 32bit arch
  608. * as 64bit update on 32bit is non atomic. Taking care of this
  609. * corner case makes code very complicated, like sending IPIs to
  610. * cpus, taking care of stats of offline cpus etc.
  611. *
  612. * reset stats is anyway more of a debug feature and this sounds a
  613. * corner case. So I am not complicating the code yet until and
  614. * unless this becomes a real issue.
  615. */
  616. for_each_possible_cpu(i) {
  617. stats_cpu = per_cpu_ptr(pd->stats_cpu, i);
  618. stats_cpu->sectors = 0;
  619. for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
  620. for (k = 0; k < BLKIO_STAT_TOTAL; k++)
  621. stats_cpu->stat_arr_cpu[j][k] = 0;
  622. }
  623. }
  624. static int
  625. blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
  626. {
  627. struct blkio_cgroup *blkcg;
  628. struct blkio_group *blkg;
  629. struct blkio_group_stats *stats;
  630. struct hlist_node *n;
  631. uint64_t queued[BLKIO_STAT_TOTAL];
  632. int i;
  633. #ifdef CONFIG_DEBUG_BLK_CGROUP
  634. bool idling, waiting, empty;
  635. unsigned long long now = sched_clock();
  636. #endif
  637. blkcg = cgroup_to_blkio_cgroup(cgroup);
  638. spin_lock_irq(&blkcg->lock);
  639. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  640. struct blkg_policy_data *pd = blkg->pd[blkg->plid];
  641. spin_lock(&blkg->stats_lock);
  642. stats = &pd->stats;
  643. #ifdef CONFIG_DEBUG_BLK_CGROUP
  644. idling = blkio_blkg_idling(stats);
  645. waiting = blkio_blkg_waiting(stats);
  646. empty = blkio_blkg_empty(stats);
  647. #endif
  648. for (i = 0; i < BLKIO_STAT_TOTAL; i++)
  649. queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
  650. memset(stats, 0, sizeof(struct blkio_group_stats));
  651. for (i = 0; i < BLKIO_STAT_TOTAL; i++)
  652. stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
  653. #ifdef CONFIG_DEBUG_BLK_CGROUP
  654. if (idling) {
  655. blkio_mark_blkg_idling(stats);
  656. stats->start_idle_time = now;
  657. }
  658. if (waiting) {
  659. blkio_mark_blkg_waiting(stats);
  660. stats->start_group_wait_time = now;
  661. }
  662. if (empty) {
  663. blkio_mark_blkg_empty(stats);
  664. stats->start_empty_time = now;
  665. }
  666. #endif
  667. spin_unlock(&blkg->stats_lock);
  668. /* Reset Per cpu stats which don't take blkg->stats_lock */
  669. blkio_reset_stats_cpu(blkg, blkg->plid);
  670. }
  671. spin_unlock_irq(&blkcg->lock);
  672. return 0;
  673. }
  674. static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
  675. char *str, int chars_left, bool diskname_only)
  676. {
  677. snprintf(str, chars_left, "%s", dname);
  678. chars_left -= strlen(str);
  679. if (chars_left <= 0) {
  680. printk(KERN_WARNING
  681. "Possibly incorrect cgroup stat display format");
  682. return;
  683. }
  684. if (diskname_only)
  685. return;
  686. switch (type) {
  687. case BLKIO_STAT_READ:
  688. strlcat(str, " Read", chars_left);
  689. break;
  690. case BLKIO_STAT_WRITE:
  691. strlcat(str, " Write", chars_left);
  692. break;
  693. case BLKIO_STAT_SYNC:
  694. strlcat(str, " Sync", chars_left);
  695. break;
  696. case BLKIO_STAT_ASYNC:
  697. strlcat(str, " Async", chars_left);
  698. break;
  699. case BLKIO_STAT_TOTAL:
  700. strlcat(str, " Total", chars_left);
  701. break;
  702. default:
  703. strlcat(str, " Invalid", chars_left);
  704. }
  705. }
  706. static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
  707. struct cgroup_map_cb *cb, const char *dname)
  708. {
  709. blkio_get_key_name(0, dname, str, chars_left, true);
  710. cb->fill(cb, str, val);
  711. return val;
  712. }
  713. static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,
  714. enum stat_type_cpu type, enum stat_sub_type sub_type)
  715. {
  716. struct blkg_policy_data *pd = blkg->pd[plid];
  717. int cpu;
  718. struct blkio_group_stats_cpu *stats_cpu;
  719. u64 val = 0, tval;
  720. for_each_possible_cpu(cpu) {
  721. unsigned int start;
  722. stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu);
  723. do {
  724. start = u64_stats_fetch_begin(&stats_cpu->syncp);
  725. if (type == BLKIO_STAT_CPU_SECTORS)
  726. tval = stats_cpu->sectors;
  727. else
  728. tval = stats_cpu->stat_arr_cpu[type][sub_type];
  729. } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
  730. val += tval;
  731. }
  732. return val;
  733. }
  734. static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,
  735. struct cgroup_map_cb *cb, const char *dname,
  736. enum stat_type_cpu type)
  737. {
  738. uint64_t disk_total, val;
  739. char key_str[MAX_KEY_LEN];
  740. enum stat_sub_type sub_type;
  741. if (type == BLKIO_STAT_CPU_SECTORS) {
  742. val = blkio_read_stat_cpu(blkg, plid, type, 0);
  743. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb,
  744. dname);
  745. }
  746. for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
  747. sub_type++) {
  748. blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
  749. false);
  750. val = blkio_read_stat_cpu(blkg, plid, type, sub_type);
  751. cb->fill(cb, key_str, val);
  752. }
  753. disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) +
  754. blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE);
  755. blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
  756. false);
  757. cb->fill(cb, key_str, disk_total);
  758. return disk_total;
  759. }
  760. /* This should be called with blkg->stats_lock held */
  761. static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,
  762. struct cgroup_map_cb *cb, const char *dname,
  763. enum stat_type type)
  764. {
  765. struct blkg_policy_data *pd = blkg->pd[plid];
  766. uint64_t disk_total;
  767. char key_str[MAX_KEY_LEN];
  768. enum stat_sub_type sub_type;
  769. if (type == BLKIO_STAT_TIME)
  770. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  771. pd->stats.time, cb, dname);
  772. #ifdef CONFIG_DEBUG_BLK_CGROUP
  773. if (type == BLKIO_STAT_UNACCOUNTED_TIME)
  774. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  775. pd->stats.unaccounted_time, cb, dname);
  776. if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
  777. uint64_t sum = pd->stats.avg_queue_size_sum;
  778. uint64_t samples = pd->stats.avg_queue_size_samples;
  779. if (samples)
  780. do_div(sum, samples);
  781. else
  782. sum = 0;
  783. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  784. sum, cb, dname);
  785. }
  786. if (type == BLKIO_STAT_GROUP_WAIT_TIME)
  787. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  788. pd->stats.group_wait_time, cb, dname);
  789. if (type == BLKIO_STAT_IDLE_TIME)
  790. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  791. pd->stats.idle_time, cb, dname);
  792. if (type == BLKIO_STAT_EMPTY_TIME)
  793. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  794. pd->stats.empty_time, cb, dname);
  795. if (type == BLKIO_STAT_DEQUEUE)
  796. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  797. pd->stats.dequeue, cb, dname);
  798. #endif
  799. for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
  800. sub_type++) {
  801. blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
  802. false);
  803. cb->fill(cb, key_str, pd->stats.stat_arr[type][sub_type]);
  804. }
  805. disk_total = pd->stats.stat_arr[type][BLKIO_STAT_READ] +
  806. pd->stats.stat_arr[type][BLKIO_STAT_WRITE];
  807. blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
  808. false);
  809. cb->fill(cb, key_str, disk_total);
  810. return disk_total;
  811. }
  812. static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
  813. int fileid, struct blkio_cgroup *blkcg)
  814. {
  815. struct gendisk *disk = NULL;
  816. struct blkio_group *blkg = NULL;
  817. struct blkg_policy_data *pd;
  818. char *s[4], *p, *major_s = NULL, *minor_s = NULL;
  819. unsigned long major, minor;
  820. int i = 0, ret = -EINVAL;
  821. int part;
  822. dev_t dev;
  823. u64 temp;
  824. memset(s, 0, sizeof(s));
  825. while ((p = strsep(&buf, " ")) != NULL) {
  826. if (!*p)
  827. continue;
  828. s[i++] = p;
  829. /* Prevent from inputing too many things */
  830. if (i == 3)
  831. break;
  832. }
  833. if (i != 2)
  834. goto out;
  835. p = strsep(&s[0], ":");
  836. if (p != NULL)
  837. major_s = p;
  838. else
  839. goto out;
  840. minor_s = s[0];
  841. if (!minor_s)
  842. goto out;
  843. if (strict_strtoul(major_s, 10, &major))
  844. goto out;
  845. if (strict_strtoul(minor_s, 10, &minor))
  846. goto out;
  847. dev = MKDEV(major, minor);
  848. if (strict_strtoull(s[1], 10, &temp))
  849. goto out;
  850. disk = get_gendisk(dev, &part);
  851. if (!disk || part)
  852. goto out;
  853. rcu_read_lock();
  854. spin_lock_irq(disk->queue->queue_lock);
  855. blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
  856. spin_unlock_irq(disk->queue->queue_lock);
  857. if (IS_ERR(blkg)) {
  858. ret = PTR_ERR(blkg);
  859. goto out_unlock;
  860. }
  861. pd = blkg->pd[plid];
  862. switch (plid) {
  863. case BLKIO_POLICY_PROP:
  864. if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
  865. temp > BLKIO_WEIGHT_MAX)
  866. goto out_unlock;
  867. pd->conf.weight = temp;
  868. blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
  869. break;
  870. case BLKIO_POLICY_THROTL:
  871. switch(fileid) {
  872. case BLKIO_THROTL_read_bps_device:
  873. pd->conf.bps[READ] = temp;
  874. blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
  875. break;
  876. case BLKIO_THROTL_write_bps_device:
  877. pd->conf.bps[WRITE] = temp;
  878. blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
  879. break;
  880. case BLKIO_THROTL_read_iops_device:
  881. if (temp > THROTL_IOPS_MAX)
  882. goto out_unlock;
  883. pd->conf.iops[READ] = temp;
  884. blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
  885. break;
  886. case BLKIO_THROTL_write_iops_device:
  887. if (temp > THROTL_IOPS_MAX)
  888. goto out_unlock;
  889. pd->conf.iops[WRITE] = temp;
  890. blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
  891. break;
  892. }
  893. break;
  894. default:
  895. BUG();
  896. }
  897. ret = 0;
  898. out_unlock:
  899. rcu_read_unlock();
  900. out:
  901. put_disk(disk);
  902. /*
  903. * If queue was bypassing, we should retry. Do so after a short
  904. * msleep(). It isn't strictly necessary but queue can be
  905. * bypassing for some time and it's always nice to avoid busy
  906. * looping.
  907. */
  908. if (ret == -EBUSY) {
  909. msleep(10);
  910. return restart_syscall();
  911. }
  912. return ret;
  913. }
  914. static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
  915. const char *buffer)
  916. {
  917. int ret = 0;
  918. char *buf;
  919. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  920. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  921. int fileid = BLKIOFILE_ATTR(cft->private);
  922. buf = kstrdup(buffer, GFP_KERNEL);
  923. if (!buf)
  924. return -ENOMEM;
  925. ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
  926. kfree(buf);
  927. return ret;
  928. }
  929. static const char *blkg_dev_name(struct blkio_group *blkg)
  930. {
  931. /* some drivers (floppy) instantiate a queue w/o disk registered */
  932. if (blkg->q->backing_dev_info.dev)
  933. return dev_name(blkg->q->backing_dev_info.dev);
  934. return NULL;
  935. }
  936. static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
  937. struct seq_file *m)
  938. {
  939. int plid = BLKIOFILE_POLICY(cft->private);
  940. int fileid = BLKIOFILE_ATTR(cft->private);
  941. struct blkg_policy_data *pd = blkg->pd[plid];
  942. const char *dname = blkg_dev_name(blkg);
  943. int rw = WRITE;
  944. if (!dname)
  945. return;
  946. switch (plid) {
  947. case BLKIO_POLICY_PROP:
  948. if (pd->conf.weight)
  949. seq_printf(m, "%s\t%u\n",
  950. dname, pd->conf.weight);
  951. break;
  952. case BLKIO_POLICY_THROTL:
  953. switch (fileid) {
  954. case BLKIO_THROTL_read_bps_device:
  955. rw = READ;
  956. case BLKIO_THROTL_write_bps_device:
  957. if (pd->conf.bps[rw])
  958. seq_printf(m, "%s\t%llu\n",
  959. dname, pd->conf.bps[rw]);
  960. break;
  961. case BLKIO_THROTL_read_iops_device:
  962. rw = READ;
  963. case BLKIO_THROTL_write_iops_device:
  964. if (pd->conf.iops[rw])
  965. seq_printf(m, "%s\t%u\n",
  966. dname, pd->conf.iops[rw]);
  967. break;
  968. }
  969. break;
  970. default:
  971. BUG();
  972. }
  973. }
  974. /* cgroup files which read their data from policy nodes end up here */
  975. static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
  976. struct seq_file *m)
  977. {
  978. struct blkio_group *blkg;
  979. struct hlist_node *n;
  980. spin_lock_irq(&blkcg->lock);
  981. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
  982. if (BLKIOFILE_POLICY(cft->private) == blkg->plid)
  983. blkio_print_group_conf(cft, blkg, m);
  984. spin_unlock_irq(&blkcg->lock);
  985. }
  986. static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
  987. struct seq_file *m)
  988. {
  989. struct blkio_cgroup *blkcg;
  990. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  991. int name = BLKIOFILE_ATTR(cft->private);
  992. blkcg = cgroup_to_blkio_cgroup(cgrp);
  993. switch(plid) {
  994. case BLKIO_POLICY_PROP:
  995. switch(name) {
  996. case BLKIO_PROP_weight_device:
  997. blkio_read_conf(cft, blkcg, m);
  998. return 0;
  999. default:
  1000. BUG();
  1001. }
  1002. break;
  1003. case BLKIO_POLICY_THROTL:
  1004. switch(name){
  1005. case BLKIO_THROTL_read_bps_device:
  1006. case BLKIO_THROTL_write_bps_device:
  1007. case BLKIO_THROTL_read_iops_device:
  1008. case BLKIO_THROTL_write_iops_device:
  1009. blkio_read_conf(cft, blkcg, m);
  1010. return 0;
  1011. default:
  1012. BUG();
  1013. }
  1014. break;
  1015. default:
  1016. BUG();
  1017. }
  1018. return 0;
  1019. }
  1020. static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
  1021. struct cftype *cft, struct cgroup_map_cb *cb,
  1022. enum stat_type type, bool show_total, bool pcpu)
  1023. {
  1024. struct blkio_group *blkg;
  1025. struct hlist_node *n;
  1026. uint64_t cgroup_total = 0;
  1027. rcu_read_lock();
  1028. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
  1029. const char *dname = blkg_dev_name(blkg);
  1030. int plid = BLKIOFILE_POLICY(cft->private);
  1031. if (!dname || plid != blkg->plid)
  1032. continue;
  1033. if (pcpu) {
  1034. cgroup_total += blkio_get_stat_cpu(blkg, plid,
  1035. cb, dname, type);
  1036. } else {
  1037. spin_lock_irq(&blkg->stats_lock);
  1038. cgroup_total += blkio_get_stat(blkg, plid,
  1039. cb, dname, type);
  1040. spin_unlock_irq(&blkg->stats_lock);
  1041. }
  1042. }
  1043. if (show_total)
  1044. cb->fill(cb, "Total", cgroup_total);
  1045. rcu_read_unlock();
  1046. return 0;
  1047. }
  1048. /* All map kind of cgroup file get serviced by this function */
  1049. static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
  1050. struct cgroup_map_cb *cb)
  1051. {
  1052. struct blkio_cgroup *blkcg;
  1053. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1054. int name = BLKIOFILE_ATTR(cft->private);
  1055. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1056. switch(plid) {
  1057. case BLKIO_POLICY_PROP:
  1058. switch(name) {
  1059. case BLKIO_PROP_time:
  1060. return blkio_read_blkg_stats(blkcg, cft, cb,
  1061. BLKIO_STAT_TIME, 0, 0);
  1062. case BLKIO_PROP_sectors:
  1063. return blkio_read_blkg_stats(blkcg, cft, cb,
  1064. BLKIO_STAT_CPU_SECTORS, 0, 1);
  1065. case BLKIO_PROP_io_service_bytes:
  1066. return blkio_read_blkg_stats(blkcg, cft, cb,
  1067. BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
  1068. case BLKIO_PROP_io_serviced:
  1069. return blkio_read_blkg_stats(blkcg, cft, cb,
  1070. BLKIO_STAT_CPU_SERVICED, 1, 1);
  1071. case BLKIO_PROP_io_service_time:
  1072. return blkio_read_blkg_stats(blkcg, cft, cb,
  1073. BLKIO_STAT_SERVICE_TIME, 1, 0);
  1074. case BLKIO_PROP_io_wait_time:
  1075. return blkio_read_blkg_stats(blkcg, cft, cb,
  1076. BLKIO_STAT_WAIT_TIME, 1, 0);
  1077. case BLKIO_PROP_io_merged:
  1078. return blkio_read_blkg_stats(blkcg, cft, cb,
  1079. BLKIO_STAT_CPU_MERGED, 1, 1);
  1080. case BLKIO_PROP_io_queued:
  1081. return blkio_read_blkg_stats(blkcg, cft, cb,
  1082. BLKIO_STAT_QUEUED, 1, 0);
  1083. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1084. case BLKIO_PROP_unaccounted_time:
  1085. return blkio_read_blkg_stats(blkcg, cft, cb,
  1086. BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
  1087. case BLKIO_PROP_dequeue:
  1088. return blkio_read_blkg_stats(blkcg, cft, cb,
  1089. BLKIO_STAT_DEQUEUE, 0, 0);
  1090. case BLKIO_PROP_avg_queue_size:
  1091. return blkio_read_blkg_stats(blkcg, cft, cb,
  1092. BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
  1093. case BLKIO_PROP_group_wait_time:
  1094. return blkio_read_blkg_stats(blkcg, cft, cb,
  1095. BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
  1096. case BLKIO_PROP_idle_time:
  1097. return blkio_read_blkg_stats(blkcg, cft, cb,
  1098. BLKIO_STAT_IDLE_TIME, 0, 0);
  1099. case BLKIO_PROP_empty_time:
  1100. return blkio_read_blkg_stats(blkcg, cft, cb,
  1101. BLKIO_STAT_EMPTY_TIME, 0, 0);
  1102. #endif
  1103. default:
  1104. BUG();
  1105. }
  1106. break;
  1107. case BLKIO_POLICY_THROTL:
  1108. switch(name){
  1109. case BLKIO_THROTL_io_service_bytes:
  1110. return blkio_read_blkg_stats(blkcg, cft, cb,
  1111. BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
  1112. case BLKIO_THROTL_io_serviced:
  1113. return blkio_read_blkg_stats(blkcg, cft, cb,
  1114. BLKIO_STAT_CPU_SERVICED, 1, 1);
  1115. default:
  1116. BUG();
  1117. }
  1118. break;
  1119. default:
  1120. BUG();
  1121. }
  1122. return 0;
  1123. }
  1124. static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
  1125. {
  1126. struct blkio_group *blkg;
  1127. struct hlist_node *n;
  1128. if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
  1129. return -EINVAL;
  1130. spin_lock(&blkio_list_lock);
  1131. spin_lock_irq(&blkcg->lock);
  1132. blkcg->weight = (unsigned int)val;
  1133. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  1134. struct blkg_policy_data *pd = blkg->pd[blkg->plid];
  1135. if (blkg->plid == plid && !pd->conf.weight)
  1136. blkio_update_group_weight(blkg, plid, blkcg->weight);
  1137. }
  1138. spin_unlock_irq(&blkcg->lock);
  1139. spin_unlock(&blkio_list_lock);
  1140. return 0;
  1141. }
  1142. static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
  1143. struct blkio_cgroup *blkcg;
  1144. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1145. int name = BLKIOFILE_ATTR(cft->private);
  1146. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1147. switch(plid) {
  1148. case BLKIO_POLICY_PROP:
  1149. switch(name) {
  1150. case BLKIO_PROP_weight:
  1151. return (u64)blkcg->weight;
  1152. }
  1153. break;
  1154. default:
  1155. BUG();
  1156. }
  1157. return 0;
  1158. }
  1159. static int
  1160. blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
  1161. {
  1162. struct blkio_cgroup *blkcg;
  1163. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1164. int name = BLKIOFILE_ATTR(cft->private);
  1165. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1166. switch(plid) {
  1167. case BLKIO_POLICY_PROP:
  1168. switch(name) {
  1169. case BLKIO_PROP_weight:
  1170. return blkio_weight_write(blkcg, plid, val);
  1171. }
  1172. break;
  1173. default:
  1174. BUG();
  1175. }
  1176. return 0;
  1177. }
  1178. struct cftype blkio_files[] = {
  1179. {
  1180. .name = "weight_device",
  1181. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1182. BLKIO_PROP_weight_device),
  1183. .read_seq_string = blkiocg_file_read,
  1184. .write_string = blkiocg_file_write,
  1185. .max_write_len = 256,
  1186. },
  1187. {
  1188. .name = "weight",
  1189. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1190. BLKIO_PROP_weight),
  1191. .read_u64 = blkiocg_file_read_u64,
  1192. .write_u64 = blkiocg_file_write_u64,
  1193. },
  1194. {
  1195. .name = "time",
  1196. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1197. BLKIO_PROP_time),
  1198. .read_map = blkiocg_file_read_map,
  1199. },
  1200. {
  1201. .name = "sectors",
  1202. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1203. BLKIO_PROP_sectors),
  1204. .read_map = blkiocg_file_read_map,
  1205. },
  1206. {
  1207. .name = "io_service_bytes",
  1208. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1209. BLKIO_PROP_io_service_bytes),
  1210. .read_map = blkiocg_file_read_map,
  1211. },
  1212. {
  1213. .name = "io_serviced",
  1214. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1215. BLKIO_PROP_io_serviced),
  1216. .read_map = blkiocg_file_read_map,
  1217. },
  1218. {
  1219. .name = "io_service_time",
  1220. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1221. BLKIO_PROP_io_service_time),
  1222. .read_map = blkiocg_file_read_map,
  1223. },
  1224. {
  1225. .name = "io_wait_time",
  1226. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1227. BLKIO_PROP_io_wait_time),
  1228. .read_map = blkiocg_file_read_map,
  1229. },
  1230. {
  1231. .name = "io_merged",
  1232. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1233. BLKIO_PROP_io_merged),
  1234. .read_map = blkiocg_file_read_map,
  1235. },
  1236. {
  1237. .name = "io_queued",
  1238. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1239. BLKIO_PROP_io_queued),
  1240. .read_map = blkiocg_file_read_map,
  1241. },
  1242. {
  1243. .name = "reset_stats",
  1244. .write_u64 = blkiocg_reset_stats,
  1245. },
  1246. #ifdef CONFIG_BLK_DEV_THROTTLING
  1247. {
  1248. .name = "throttle.read_bps_device",
  1249. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1250. BLKIO_THROTL_read_bps_device),
  1251. .read_seq_string = blkiocg_file_read,
  1252. .write_string = blkiocg_file_write,
  1253. .max_write_len = 256,
  1254. },
  1255. {
  1256. .name = "throttle.write_bps_device",
  1257. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1258. BLKIO_THROTL_write_bps_device),
  1259. .read_seq_string = blkiocg_file_read,
  1260. .write_string = blkiocg_file_write,
  1261. .max_write_len = 256,
  1262. },
  1263. {
  1264. .name = "throttle.read_iops_device",
  1265. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1266. BLKIO_THROTL_read_iops_device),
  1267. .read_seq_string = blkiocg_file_read,
  1268. .write_string = blkiocg_file_write,
  1269. .max_write_len = 256,
  1270. },
  1271. {
  1272. .name = "throttle.write_iops_device",
  1273. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1274. BLKIO_THROTL_write_iops_device),
  1275. .read_seq_string = blkiocg_file_read,
  1276. .write_string = blkiocg_file_write,
  1277. .max_write_len = 256,
  1278. },
  1279. {
  1280. .name = "throttle.io_service_bytes",
  1281. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1282. BLKIO_THROTL_io_service_bytes),
  1283. .read_map = blkiocg_file_read_map,
  1284. },
  1285. {
  1286. .name = "throttle.io_serviced",
  1287. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1288. BLKIO_THROTL_io_serviced),
  1289. .read_map = blkiocg_file_read_map,
  1290. },
  1291. #endif /* CONFIG_BLK_DEV_THROTTLING */
  1292. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1293. {
  1294. .name = "avg_queue_size",
  1295. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1296. BLKIO_PROP_avg_queue_size),
  1297. .read_map = blkiocg_file_read_map,
  1298. },
  1299. {
  1300. .name = "group_wait_time",
  1301. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1302. BLKIO_PROP_group_wait_time),
  1303. .read_map = blkiocg_file_read_map,
  1304. },
  1305. {
  1306. .name = "idle_time",
  1307. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1308. BLKIO_PROP_idle_time),
  1309. .read_map = blkiocg_file_read_map,
  1310. },
  1311. {
  1312. .name = "empty_time",
  1313. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1314. BLKIO_PROP_empty_time),
  1315. .read_map = blkiocg_file_read_map,
  1316. },
  1317. {
  1318. .name = "dequeue",
  1319. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1320. BLKIO_PROP_dequeue),
  1321. .read_map = blkiocg_file_read_map,
  1322. },
  1323. {
  1324. .name = "unaccounted_time",
  1325. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1326. BLKIO_PROP_unaccounted_time),
  1327. .read_map = blkiocg_file_read_map,
  1328. },
  1329. #endif
  1330. };
  1331. static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1332. {
  1333. return cgroup_add_files(cgroup, subsys, blkio_files,
  1334. ARRAY_SIZE(blkio_files));
  1335. }
  1336. static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
  1337. struct cgroup *cgroup)
  1338. {
  1339. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  1340. unsigned long flags;
  1341. struct blkio_group *blkg;
  1342. struct request_queue *q;
  1343. struct blkio_policy_type *blkiop;
  1344. rcu_read_lock();
  1345. do {
  1346. spin_lock_irqsave(&blkcg->lock, flags);
  1347. if (hlist_empty(&blkcg->blkg_list)) {
  1348. spin_unlock_irqrestore(&blkcg->lock, flags);
  1349. break;
  1350. }
  1351. blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
  1352. blkcg_node);
  1353. q = rcu_dereference(blkg->q);
  1354. __blkiocg_del_blkio_group(blkg);
  1355. spin_unlock_irqrestore(&blkcg->lock, flags);
  1356. /*
  1357. * This blkio_group is being unlinked as associated cgroup is
  1358. * going away. Let all the IO controlling policies know about
  1359. * this event.
  1360. */
  1361. spin_lock(&blkio_list_lock);
  1362. list_for_each_entry(blkiop, &blkio_list, list) {
  1363. if (blkiop->plid != blkg->plid)
  1364. continue;
  1365. blkiop->ops.blkio_unlink_group_fn(q, blkg);
  1366. }
  1367. spin_unlock(&blkio_list_lock);
  1368. } while (1);
  1369. rcu_read_unlock();
  1370. return 0;
  1371. }
  1372. static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1373. {
  1374. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  1375. if (blkcg != &blkio_root_cgroup)
  1376. kfree(blkcg);
  1377. }
  1378. static struct cgroup_subsys_state *
  1379. blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1380. {
  1381. struct blkio_cgroup *blkcg;
  1382. struct cgroup *parent = cgroup->parent;
  1383. if (!parent) {
  1384. blkcg = &blkio_root_cgroup;
  1385. goto done;
  1386. }
  1387. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  1388. if (!blkcg)
  1389. return ERR_PTR(-ENOMEM);
  1390. blkcg->weight = BLKIO_WEIGHT_DEFAULT;
  1391. done:
  1392. spin_lock_init(&blkcg->lock);
  1393. INIT_HLIST_HEAD(&blkcg->blkg_list);
  1394. return &blkcg->css;
  1395. }
  1396. /**
  1397. * blkcg_init_queue - initialize blkcg part of request queue
  1398. * @q: request_queue to initialize
  1399. *
  1400. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  1401. * part of new request_queue @q.
  1402. *
  1403. * RETURNS:
  1404. * 0 on success, -errno on failure.
  1405. */
  1406. int blkcg_init_queue(struct request_queue *q)
  1407. {
  1408. int ret;
  1409. might_sleep();
  1410. ret = blk_throtl_init(q);
  1411. if (ret)
  1412. return ret;
  1413. mutex_lock(&all_q_mutex);
  1414. INIT_LIST_HEAD(&q->all_q_node);
  1415. list_add_tail(&q->all_q_node, &all_q_list);
  1416. mutex_unlock(&all_q_mutex);
  1417. return 0;
  1418. }
  1419. /**
  1420. * blkcg_drain_queue - drain blkcg part of request_queue
  1421. * @q: request_queue to drain
  1422. *
  1423. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  1424. */
  1425. void blkcg_drain_queue(struct request_queue *q)
  1426. {
  1427. lockdep_assert_held(q->queue_lock);
  1428. blk_throtl_drain(q);
  1429. }
  1430. /**
  1431. * blkcg_exit_queue - exit and release blkcg part of request_queue
  1432. * @q: request_queue being released
  1433. *
  1434. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  1435. */
  1436. void blkcg_exit_queue(struct request_queue *q)
  1437. {
  1438. mutex_lock(&all_q_mutex);
  1439. list_del_init(&q->all_q_node);
  1440. mutex_unlock(&all_q_mutex);
  1441. blk_throtl_exit(q);
  1442. }
  1443. /*
  1444. * We cannot support shared io contexts, as we have no mean to support
  1445. * two tasks with the same ioc in two different groups without major rework
  1446. * of the main cic data structures. For now we allow a task to change
  1447. * its cgroup only if it's the only owner of its ioc.
  1448. */
  1449. static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  1450. struct cgroup_taskset *tset)
  1451. {
  1452. struct task_struct *task;
  1453. struct io_context *ioc;
  1454. int ret = 0;
  1455. /* task_lock() is needed to avoid races with exit_io_context() */
  1456. cgroup_taskset_for_each(task, cgrp, tset) {
  1457. task_lock(task);
  1458. ioc = task->io_context;
  1459. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  1460. ret = -EINVAL;
  1461. task_unlock(task);
  1462. if (ret)
  1463. break;
  1464. }
  1465. return ret;
  1466. }
  1467. static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  1468. struct cgroup_taskset *tset)
  1469. {
  1470. struct task_struct *task;
  1471. struct io_context *ioc;
  1472. cgroup_taskset_for_each(task, cgrp, tset) {
  1473. /* we don't lose anything even if ioc allocation fails */
  1474. ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
  1475. if (ioc) {
  1476. ioc_cgroup_changed(ioc);
  1477. put_io_context(ioc);
  1478. }
  1479. }
  1480. }
  1481. static void blkcg_bypass_start(void)
  1482. __acquires(&all_q_mutex)
  1483. {
  1484. struct request_queue *q;
  1485. mutex_lock(&all_q_mutex);
  1486. list_for_each_entry(q, &all_q_list, all_q_node) {
  1487. blk_queue_bypass_start(q);
  1488. blkg_destroy_all(q);
  1489. }
  1490. }
  1491. static void blkcg_bypass_end(void)
  1492. __releases(&all_q_mutex)
  1493. {
  1494. struct request_queue *q;
  1495. list_for_each_entry(q, &all_q_list, all_q_node)
  1496. blk_queue_bypass_end(q);
  1497. mutex_unlock(&all_q_mutex);
  1498. }
  1499. void blkio_policy_register(struct blkio_policy_type *blkiop)
  1500. {
  1501. blkcg_bypass_start();
  1502. spin_lock(&blkio_list_lock);
  1503. BUG_ON(blkio_policy[blkiop->plid]);
  1504. blkio_policy[blkiop->plid] = blkiop;
  1505. list_add_tail(&blkiop->list, &blkio_list);
  1506. spin_unlock(&blkio_list_lock);
  1507. blkcg_bypass_end();
  1508. }
  1509. EXPORT_SYMBOL_GPL(blkio_policy_register);
  1510. void blkio_policy_unregister(struct blkio_policy_type *blkiop)
  1511. {
  1512. blkcg_bypass_start();
  1513. spin_lock(&blkio_list_lock);
  1514. BUG_ON(blkio_policy[blkiop->plid] != blkiop);
  1515. blkio_policy[blkiop->plid] = NULL;
  1516. list_del_init(&blkiop->list);
  1517. spin_unlock(&blkio_list_lock);
  1518. blkcg_bypass_end();
  1519. }
  1520. EXPORT_SYMBOL_GPL(blkio_policy_unregister);