blk-cgroup.c 42 KB

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