blk-cgroup.c 43 KB

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