blk-cgroup.c 39 KB

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