blk-cgroup.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  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. 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_check_dev_num(dev_t dev)
  675. {
  676. int part = 0;
  677. struct gendisk *disk;
  678. disk = get_gendisk(dev, &part);
  679. if (!disk || part)
  680. return -ENODEV;
  681. return 0;
  682. }
  683. static int blkio_policy_parse_and_set(char *buf,
  684. struct blkio_policy_node *newpn, enum blkio_policy_id plid, int fileid)
  685. {
  686. char *s[4], *p, *major_s = NULL, *minor_s = NULL;
  687. int ret;
  688. unsigned long major, minor, temp;
  689. int i = 0;
  690. dev_t dev;
  691. u64 bps, iops;
  692. memset(s, 0, sizeof(s));
  693. while ((p = strsep(&buf, " ")) != NULL) {
  694. if (!*p)
  695. continue;
  696. s[i++] = p;
  697. /* Prevent from inputing too many things */
  698. if (i == 3)
  699. break;
  700. }
  701. if (i != 2)
  702. return -EINVAL;
  703. p = strsep(&s[0], ":");
  704. if (p != NULL)
  705. major_s = p;
  706. else
  707. return -EINVAL;
  708. minor_s = s[0];
  709. if (!minor_s)
  710. return -EINVAL;
  711. ret = strict_strtoul(major_s, 10, &major);
  712. if (ret)
  713. return -EINVAL;
  714. ret = strict_strtoul(minor_s, 10, &minor);
  715. if (ret)
  716. return -EINVAL;
  717. dev = MKDEV(major, minor);
  718. ret = blkio_check_dev_num(dev);
  719. if (ret)
  720. return ret;
  721. newpn->dev = dev;
  722. if (s[1] == NULL)
  723. return -EINVAL;
  724. switch (plid) {
  725. case BLKIO_POLICY_PROP:
  726. ret = strict_strtoul(s[1], 10, &temp);
  727. if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
  728. temp > BLKIO_WEIGHT_MAX)
  729. return -EINVAL;
  730. newpn->plid = plid;
  731. newpn->fileid = fileid;
  732. newpn->val.weight = temp;
  733. break;
  734. case BLKIO_POLICY_THROTL:
  735. switch(fileid) {
  736. case BLKIO_THROTL_read_bps_device:
  737. case BLKIO_THROTL_write_bps_device:
  738. ret = strict_strtoull(s[1], 10, &bps);
  739. if (ret)
  740. return -EINVAL;
  741. newpn->plid = plid;
  742. newpn->fileid = fileid;
  743. newpn->val.bps = bps;
  744. break;
  745. case BLKIO_THROTL_read_iops_device:
  746. case BLKIO_THROTL_write_iops_device:
  747. ret = strict_strtoull(s[1], 10, &iops);
  748. if (ret)
  749. return -EINVAL;
  750. if (iops > THROTL_IOPS_MAX)
  751. return -EINVAL;
  752. newpn->plid = plid;
  753. newpn->fileid = fileid;
  754. newpn->val.iops = (unsigned int)iops;
  755. break;
  756. }
  757. break;
  758. default:
  759. BUG();
  760. }
  761. return 0;
  762. }
  763. unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
  764. dev_t dev)
  765. {
  766. struct blkio_policy_node *pn;
  767. pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP,
  768. BLKIO_PROP_weight_device);
  769. if (pn)
  770. return pn->val.weight;
  771. else
  772. return blkcg->weight;
  773. }
  774. EXPORT_SYMBOL_GPL(blkcg_get_weight);
  775. uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg, dev_t dev)
  776. {
  777. struct blkio_policy_node *pn;
  778. pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
  779. BLKIO_THROTL_read_bps_device);
  780. if (pn)
  781. return pn->val.bps;
  782. else
  783. return -1;
  784. }
  785. uint64_t blkcg_get_write_bps(struct blkio_cgroup *blkcg, dev_t dev)
  786. {
  787. struct blkio_policy_node *pn;
  788. pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
  789. BLKIO_THROTL_write_bps_device);
  790. if (pn)
  791. return pn->val.bps;
  792. else
  793. return -1;
  794. }
  795. unsigned int blkcg_get_read_iops(struct blkio_cgroup *blkcg, dev_t dev)
  796. {
  797. struct blkio_policy_node *pn;
  798. pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
  799. BLKIO_THROTL_read_iops_device);
  800. if (pn)
  801. return pn->val.iops;
  802. else
  803. return -1;
  804. }
  805. unsigned int blkcg_get_write_iops(struct blkio_cgroup *blkcg, dev_t dev)
  806. {
  807. struct blkio_policy_node *pn;
  808. pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
  809. BLKIO_THROTL_write_iops_device);
  810. if (pn)
  811. return pn->val.iops;
  812. else
  813. return -1;
  814. }
  815. /* Checks whether user asked for deleting a policy rule */
  816. static bool blkio_delete_rule_command(struct blkio_policy_node *pn)
  817. {
  818. switch(pn->plid) {
  819. case BLKIO_POLICY_PROP:
  820. if (pn->val.weight == 0)
  821. return 1;
  822. break;
  823. case BLKIO_POLICY_THROTL:
  824. switch(pn->fileid) {
  825. case BLKIO_THROTL_read_bps_device:
  826. case BLKIO_THROTL_write_bps_device:
  827. if (pn->val.bps == 0)
  828. return 1;
  829. break;
  830. case BLKIO_THROTL_read_iops_device:
  831. case BLKIO_THROTL_write_iops_device:
  832. if (pn->val.iops == 0)
  833. return 1;
  834. }
  835. break;
  836. default:
  837. BUG();
  838. }
  839. return 0;
  840. }
  841. static void blkio_update_policy_rule(struct blkio_policy_node *oldpn,
  842. struct blkio_policy_node *newpn)
  843. {
  844. switch(oldpn->plid) {
  845. case BLKIO_POLICY_PROP:
  846. oldpn->val.weight = newpn->val.weight;
  847. break;
  848. case BLKIO_POLICY_THROTL:
  849. switch(newpn->fileid) {
  850. case BLKIO_THROTL_read_bps_device:
  851. case BLKIO_THROTL_write_bps_device:
  852. oldpn->val.bps = newpn->val.bps;
  853. break;
  854. case BLKIO_THROTL_read_iops_device:
  855. case BLKIO_THROTL_write_iops_device:
  856. oldpn->val.iops = newpn->val.iops;
  857. }
  858. break;
  859. default:
  860. BUG();
  861. }
  862. }
  863. /*
  864. * Some rules/values in blkg have changed. Propagate those to respective
  865. * policies.
  866. */
  867. static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg,
  868. struct blkio_group *blkg, struct blkio_policy_node *pn)
  869. {
  870. unsigned int weight, iops;
  871. u64 bps;
  872. switch(pn->plid) {
  873. case BLKIO_POLICY_PROP:
  874. weight = pn->val.weight ? pn->val.weight :
  875. blkcg->weight;
  876. blkio_update_group_weight(blkg, weight);
  877. break;
  878. case BLKIO_POLICY_THROTL:
  879. switch(pn->fileid) {
  880. case BLKIO_THROTL_read_bps_device:
  881. case BLKIO_THROTL_write_bps_device:
  882. bps = pn->val.bps ? pn->val.bps : (-1);
  883. blkio_update_group_bps(blkg, bps, pn->fileid);
  884. break;
  885. case BLKIO_THROTL_read_iops_device:
  886. case BLKIO_THROTL_write_iops_device:
  887. iops = pn->val.iops ? pn->val.iops : (-1);
  888. blkio_update_group_iops(blkg, iops, pn->fileid);
  889. break;
  890. }
  891. break;
  892. default:
  893. BUG();
  894. }
  895. }
  896. /*
  897. * A policy node rule has been updated. Propagate this update to all the
  898. * block groups which might be affected by this update.
  899. */
  900. static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg,
  901. struct blkio_policy_node *pn)
  902. {
  903. struct blkio_group *blkg;
  904. struct hlist_node *n;
  905. spin_lock(&blkio_list_lock);
  906. spin_lock_irq(&blkcg->lock);
  907. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  908. if (pn->dev != blkg->dev || pn->plid != blkg->plid)
  909. continue;
  910. blkio_update_blkg_policy(blkcg, blkg, pn);
  911. }
  912. spin_unlock_irq(&blkcg->lock);
  913. spin_unlock(&blkio_list_lock);
  914. }
  915. static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
  916. const char *buffer)
  917. {
  918. int ret = 0;
  919. char *buf;
  920. struct blkio_policy_node *newpn, *pn;
  921. struct blkio_cgroup *blkcg;
  922. int keep_newpn = 0;
  923. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  924. int fileid = BLKIOFILE_ATTR(cft->private);
  925. buf = kstrdup(buffer, GFP_KERNEL);
  926. if (!buf)
  927. return -ENOMEM;
  928. newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
  929. if (!newpn) {
  930. ret = -ENOMEM;
  931. goto free_buf;
  932. }
  933. ret = blkio_policy_parse_and_set(buf, newpn, plid, fileid);
  934. if (ret)
  935. goto free_newpn;
  936. blkcg = cgroup_to_blkio_cgroup(cgrp);
  937. spin_lock_irq(&blkcg->lock);
  938. pn = blkio_policy_search_node(blkcg, newpn->dev, plid, fileid);
  939. if (!pn) {
  940. if (!blkio_delete_rule_command(newpn)) {
  941. blkio_policy_insert_node(blkcg, newpn);
  942. keep_newpn = 1;
  943. }
  944. spin_unlock_irq(&blkcg->lock);
  945. goto update_io_group;
  946. }
  947. if (blkio_delete_rule_command(newpn)) {
  948. blkio_policy_delete_node(pn);
  949. spin_unlock_irq(&blkcg->lock);
  950. goto update_io_group;
  951. }
  952. spin_unlock_irq(&blkcg->lock);
  953. blkio_update_policy_rule(pn, newpn);
  954. update_io_group:
  955. blkio_update_policy_node_blkg(blkcg, newpn);
  956. free_newpn:
  957. if (!keep_newpn)
  958. kfree(newpn);
  959. free_buf:
  960. kfree(buf);
  961. return ret;
  962. }
  963. static void
  964. blkio_print_policy_node(struct seq_file *m, struct blkio_policy_node *pn)
  965. {
  966. switch(pn->plid) {
  967. case BLKIO_POLICY_PROP:
  968. if (pn->fileid == BLKIO_PROP_weight_device)
  969. seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
  970. MINOR(pn->dev), pn->val.weight);
  971. break;
  972. case BLKIO_POLICY_THROTL:
  973. switch(pn->fileid) {
  974. case BLKIO_THROTL_read_bps_device:
  975. case BLKIO_THROTL_write_bps_device:
  976. seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev),
  977. MINOR(pn->dev), pn->val.bps);
  978. break;
  979. case BLKIO_THROTL_read_iops_device:
  980. case BLKIO_THROTL_write_iops_device:
  981. seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
  982. MINOR(pn->dev), pn->val.iops);
  983. break;
  984. }
  985. break;
  986. default:
  987. BUG();
  988. }
  989. }
  990. /* cgroup files which read their data from policy nodes end up here */
  991. static void blkio_read_policy_node_files(struct cftype *cft,
  992. struct blkio_cgroup *blkcg, struct seq_file *m)
  993. {
  994. struct blkio_policy_node *pn;
  995. if (!list_empty(&blkcg->policy_list)) {
  996. spin_lock_irq(&blkcg->lock);
  997. list_for_each_entry(pn, &blkcg->policy_list, node) {
  998. if (!pn_matches_cftype(cft, pn))
  999. continue;
  1000. blkio_print_policy_node(m, pn);
  1001. }
  1002. spin_unlock_irq(&blkcg->lock);
  1003. }
  1004. }
  1005. static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
  1006. struct seq_file *m)
  1007. {
  1008. struct blkio_cgroup *blkcg;
  1009. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1010. int name = BLKIOFILE_ATTR(cft->private);
  1011. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1012. switch(plid) {
  1013. case BLKIO_POLICY_PROP:
  1014. switch(name) {
  1015. case BLKIO_PROP_weight_device:
  1016. blkio_read_policy_node_files(cft, blkcg, m);
  1017. return 0;
  1018. default:
  1019. BUG();
  1020. }
  1021. break;
  1022. case BLKIO_POLICY_THROTL:
  1023. switch(name){
  1024. case BLKIO_THROTL_read_bps_device:
  1025. case BLKIO_THROTL_write_bps_device:
  1026. case BLKIO_THROTL_read_iops_device:
  1027. case BLKIO_THROTL_write_iops_device:
  1028. blkio_read_policy_node_files(cft, blkcg, m);
  1029. return 0;
  1030. default:
  1031. BUG();
  1032. }
  1033. break;
  1034. default:
  1035. BUG();
  1036. }
  1037. return 0;
  1038. }
  1039. static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
  1040. struct cftype *cft, struct cgroup_map_cb *cb,
  1041. enum stat_type type, bool show_total, bool pcpu)
  1042. {
  1043. struct blkio_group *blkg;
  1044. struct hlist_node *n;
  1045. uint64_t cgroup_total = 0;
  1046. rcu_read_lock();
  1047. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
  1048. if (blkg->dev) {
  1049. if (!cftype_blkg_same_policy(cft, blkg))
  1050. continue;
  1051. if (pcpu)
  1052. cgroup_total += blkio_get_stat_cpu(blkg, cb,
  1053. blkg->dev, type);
  1054. else {
  1055. spin_lock_irq(&blkg->stats_lock);
  1056. cgroup_total += blkio_get_stat(blkg, cb,
  1057. blkg->dev, type);
  1058. spin_unlock_irq(&blkg->stats_lock);
  1059. }
  1060. }
  1061. }
  1062. if (show_total)
  1063. cb->fill(cb, "Total", cgroup_total);
  1064. rcu_read_unlock();
  1065. return 0;
  1066. }
  1067. /* All map kind of cgroup file get serviced by this function */
  1068. static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
  1069. struct cgroup_map_cb *cb)
  1070. {
  1071. struct blkio_cgroup *blkcg;
  1072. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1073. int name = BLKIOFILE_ATTR(cft->private);
  1074. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1075. switch(plid) {
  1076. case BLKIO_POLICY_PROP:
  1077. switch(name) {
  1078. case BLKIO_PROP_time:
  1079. return blkio_read_blkg_stats(blkcg, cft, cb,
  1080. BLKIO_STAT_TIME, 0, 0);
  1081. case BLKIO_PROP_sectors:
  1082. return blkio_read_blkg_stats(blkcg, cft, cb,
  1083. BLKIO_STAT_CPU_SECTORS, 0, 1);
  1084. case BLKIO_PROP_io_service_bytes:
  1085. return blkio_read_blkg_stats(blkcg, cft, cb,
  1086. BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
  1087. case BLKIO_PROP_io_serviced:
  1088. return blkio_read_blkg_stats(blkcg, cft, cb,
  1089. BLKIO_STAT_CPU_SERVICED, 1, 1);
  1090. case BLKIO_PROP_io_service_time:
  1091. return blkio_read_blkg_stats(blkcg, cft, cb,
  1092. BLKIO_STAT_SERVICE_TIME, 1, 0);
  1093. case BLKIO_PROP_io_wait_time:
  1094. return blkio_read_blkg_stats(blkcg, cft, cb,
  1095. BLKIO_STAT_WAIT_TIME, 1, 0);
  1096. case BLKIO_PROP_io_merged:
  1097. return blkio_read_blkg_stats(blkcg, cft, cb,
  1098. BLKIO_STAT_CPU_MERGED, 1, 1);
  1099. case BLKIO_PROP_io_queued:
  1100. return blkio_read_blkg_stats(blkcg, cft, cb,
  1101. BLKIO_STAT_QUEUED, 1, 0);
  1102. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1103. case BLKIO_PROP_unaccounted_time:
  1104. return blkio_read_blkg_stats(blkcg, cft, cb,
  1105. BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
  1106. case BLKIO_PROP_dequeue:
  1107. return blkio_read_blkg_stats(blkcg, cft, cb,
  1108. BLKIO_STAT_DEQUEUE, 0, 0);
  1109. case BLKIO_PROP_avg_queue_size:
  1110. return blkio_read_blkg_stats(blkcg, cft, cb,
  1111. BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
  1112. case BLKIO_PROP_group_wait_time:
  1113. return blkio_read_blkg_stats(blkcg, cft, cb,
  1114. BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
  1115. case BLKIO_PROP_idle_time:
  1116. return blkio_read_blkg_stats(blkcg, cft, cb,
  1117. BLKIO_STAT_IDLE_TIME, 0, 0);
  1118. case BLKIO_PROP_empty_time:
  1119. return blkio_read_blkg_stats(blkcg, cft, cb,
  1120. BLKIO_STAT_EMPTY_TIME, 0, 0);
  1121. #endif
  1122. default:
  1123. BUG();
  1124. }
  1125. break;
  1126. case BLKIO_POLICY_THROTL:
  1127. switch(name){
  1128. case BLKIO_THROTL_io_service_bytes:
  1129. return blkio_read_blkg_stats(blkcg, cft, cb,
  1130. BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
  1131. case BLKIO_THROTL_io_serviced:
  1132. return blkio_read_blkg_stats(blkcg, cft, cb,
  1133. BLKIO_STAT_CPU_SERVICED, 1, 1);
  1134. default:
  1135. BUG();
  1136. }
  1137. break;
  1138. default:
  1139. BUG();
  1140. }
  1141. return 0;
  1142. }
  1143. static int blkio_weight_write(struct blkio_cgroup *blkcg, u64 val)
  1144. {
  1145. struct blkio_group *blkg;
  1146. struct hlist_node *n;
  1147. struct blkio_policy_node *pn;
  1148. if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
  1149. return -EINVAL;
  1150. spin_lock(&blkio_list_lock);
  1151. spin_lock_irq(&blkcg->lock);
  1152. blkcg->weight = (unsigned int)val;
  1153. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  1154. pn = blkio_policy_search_node(blkcg, blkg->dev,
  1155. BLKIO_POLICY_PROP, BLKIO_PROP_weight_device);
  1156. if (pn)
  1157. continue;
  1158. blkio_update_group_weight(blkg, blkcg->weight);
  1159. }
  1160. spin_unlock_irq(&blkcg->lock);
  1161. spin_unlock(&blkio_list_lock);
  1162. return 0;
  1163. }
  1164. static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
  1165. struct blkio_cgroup *blkcg;
  1166. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1167. int name = BLKIOFILE_ATTR(cft->private);
  1168. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1169. switch(plid) {
  1170. case BLKIO_POLICY_PROP:
  1171. switch(name) {
  1172. case BLKIO_PROP_weight:
  1173. return (u64)blkcg->weight;
  1174. }
  1175. break;
  1176. default:
  1177. BUG();
  1178. }
  1179. return 0;
  1180. }
  1181. static int
  1182. blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
  1183. {
  1184. struct blkio_cgroup *blkcg;
  1185. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1186. int name = BLKIOFILE_ATTR(cft->private);
  1187. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1188. switch(plid) {
  1189. case BLKIO_POLICY_PROP:
  1190. switch(name) {
  1191. case BLKIO_PROP_weight:
  1192. return blkio_weight_write(blkcg, val);
  1193. }
  1194. break;
  1195. default:
  1196. BUG();
  1197. }
  1198. return 0;
  1199. }
  1200. struct cftype blkio_files[] = {
  1201. {
  1202. .name = "weight_device",
  1203. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1204. BLKIO_PROP_weight_device),
  1205. .read_seq_string = blkiocg_file_read,
  1206. .write_string = blkiocg_file_write,
  1207. .max_write_len = 256,
  1208. },
  1209. {
  1210. .name = "weight",
  1211. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1212. BLKIO_PROP_weight),
  1213. .read_u64 = blkiocg_file_read_u64,
  1214. .write_u64 = blkiocg_file_write_u64,
  1215. },
  1216. {
  1217. .name = "time",
  1218. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1219. BLKIO_PROP_time),
  1220. .read_map = blkiocg_file_read_map,
  1221. },
  1222. {
  1223. .name = "sectors",
  1224. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1225. BLKIO_PROP_sectors),
  1226. .read_map = blkiocg_file_read_map,
  1227. },
  1228. {
  1229. .name = "io_service_bytes",
  1230. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1231. BLKIO_PROP_io_service_bytes),
  1232. .read_map = blkiocg_file_read_map,
  1233. },
  1234. {
  1235. .name = "io_serviced",
  1236. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1237. BLKIO_PROP_io_serviced),
  1238. .read_map = blkiocg_file_read_map,
  1239. },
  1240. {
  1241. .name = "io_service_time",
  1242. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1243. BLKIO_PROP_io_service_time),
  1244. .read_map = blkiocg_file_read_map,
  1245. },
  1246. {
  1247. .name = "io_wait_time",
  1248. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1249. BLKIO_PROP_io_wait_time),
  1250. .read_map = blkiocg_file_read_map,
  1251. },
  1252. {
  1253. .name = "io_merged",
  1254. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1255. BLKIO_PROP_io_merged),
  1256. .read_map = blkiocg_file_read_map,
  1257. },
  1258. {
  1259. .name = "io_queued",
  1260. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1261. BLKIO_PROP_io_queued),
  1262. .read_map = blkiocg_file_read_map,
  1263. },
  1264. {
  1265. .name = "reset_stats",
  1266. .write_u64 = blkiocg_reset_stats,
  1267. },
  1268. #ifdef CONFIG_BLK_DEV_THROTTLING
  1269. {
  1270. .name = "throttle.read_bps_device",
  1271. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1272. BLKIO_THROTL_read_bps_device),
  1273. .read_seq_string = blkiocg_file_read,
  1274. .write_string = blkiocg_file_write,
  1275. .max_write_len = 256,
  1276. },
  1277. {
  1278. .name = "throttle.write_bps_device",
  1279. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1280. BLKIO_THROTL_write_bps_device),
  1281. .read_seq_string = blkiocg_file_read,
  1282. .write_string = blkiocg_file_write,
  1283. .max_write_len = 256,
  1284. },
  1285. {
  1286. .name = "throttle.read_iops_device",
  1287. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1288. BLKIO_THROTL_read_iops_device),
  1289. .read_seq_string = blkiocg_file_read,
  1290. .write_string = blkiocg_file_write,
  1291. .max_write_len = 256,
  1292. },
  1293. {
  1294. .name = "throttle.write_iops_device",
  1295. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1296. BLKIO_THROTL_write_iops_device),
  1297. .read_seq_string = blkiocg_file_read,
  1298. .write_string = blkiocg_file_write,
  1299. .max_write_len = 256,
  1300. },
  1301. {
  1302. .name = "throttle.io_service_bytes",
  1303. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1304. BLKIO_THROTL_io_service_bytes),
  1305. .read_map = blkiocg_file_read_map,
  1306. },
  1307. {
  1308. .name = "throttle.io_serviced",
  1309. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1310. BLKIO_THROTL_io_serviced),
  1311. .read_map = blkiocg_file_read_map,
  1312. },
  1313. #endif /* CONFIG_BLK_DEV_THROTTLING */
  1314. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1315. {
  1316. .name = "avg_queue_size",
  1317. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1318. BLKIO_PROP_avg_queue_size),
  1319. .read_map = blkiocg_file_read_map,
  1320. },
  1321. {
  1322. .name = "group_wait_time",
  1323. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1324. BLKIO_PROP_group_wait_time),
  1325. .read_map = blkiocg_file_read_map,
  1326. },
  1327. {
  1328. .name = "idle_time",
  1329. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1330. BLKIO_PROP_idle_time),
  1331. .read_map = blkiocg_file_read_map,
  1332. },
  1333. {
  1334. .name = "empty_time",
  1335. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1336. BLKIO_PROP_empty_time),
  1337. .read_map = blkiocg_file_read_map,
  1338. },
  1339. {
  1340. .name = "dequeue",
  1341. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1342. BLKIO_PROP_dequeue),
  1343. .read_map = blkiocg_file_read_map,
  1344. },
  1345. {
  1346. .name = "unaccounted_time",
  1347. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1348. BLKIO_PROP_unaccounted_time),
  1349. .read_map = blkiocg_file_read_map,
  1350. },
  1351. #endif
  1352. };
  1353. static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1354. {
  1355. return cgroup_add_files(cgroup, subsys, blkio_files,
  1356. ARRAY_SIZE(blkio_files));
  1357. }
  1358. static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1359. {
  1360. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  1361. unsigned long flags;
  1362. struct blkio_group *blkg;
  1363. void *key;
  1364. struct blkio_policy_type *blkiop;
  1365. struct blkio_policy_node *pn, *pntmp;
  1366. rcu_read_lock();
  1367. do {
  1368. spin_lock_irqsave(&blkcg->lock, flags);
  1369. if (hlist_empty(&blkcg->blkg_list)) {
  1370. spin_unlock_irqrestore(&blkcg->lock, flags);
  1371. break;
  1372. }
  1373. blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
  1374. blkcg_node);
  1375. key = rcu_dereference(blkg->key);
  1376. __blkiocg_del_blkio_group(blkg);
  1377. spin_unlock_irqrestore(&blkcg->lock, flags);
  1378. /*
  1379. * This blkio_group is being unlinked as associated cgroup is
  1380. * going away. Let all the IO controlling policies know about
  1381. * this event.
  1382. */
  1383. spin_lock(&blkio_list_lock);
  1384. list_for_each_entry(blkiop, &blkio_list, list) {
  1385. if (blkiop->plid != blkg->plid)
  1386. continue;
  1387. blkiop->ops.blkio_unlink_group_fn(key, blkg);
  1388. }
  1389. spin_unlock(&blkio_list_lock);
  1390. } while (1);
  1391. list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
  1392. blkio_policy_delete_node(pn);
  1393. kfree(pn);
  1394. }
  1395. free_css_id(&blkio_subsys, &blkcg->css);
  1396. rcu_read_unlock();
  1397. if (blkcg != &blkio_root_cgroup)
  1398. kfree(blkcg);
  1399. }
  1400. static struct cgroup_subsys_state *
  1401. blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1402. {
  1403. struct blkio_cgroup *blkcg;
  1404. struct cgroup *parent = cgroup->parent;
  1405. if (!parent) {
  1406. blkcg = &blkio_root_cgroup;
  1407. goto done;
  1408. }
  1409. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  1410. if (!blkcg)
  1411. return ERR_PTR(-ENOMEM);
  1412. blkcg->weight = BLKIO_WEIGHT_DEFAULT;
  1413. done:
  1414. spin_lock_init(&blkcg->lock);
  1415. INIT_HLIST_HEAD(&blkcg->blkg_list);
  1416. INIT_LIST_HEAD(&blkcg->policy_list);
  1417. return &blkcg->css;
  1418. }
  1419. /*
  1420. * We cannot support shared io contexts, as we have no mean to support
  1421. * two tasks with the same ioc in two different groups without major rework
  1422. * of the main cic data structures. For now we allow a task to change
  1423. * its cgroup only if it's the only owner of its ioc.
  1424. */
  1425. static int blkiocg_can_attach(struct cgroup_subsys *subsys,
  1426. struct cgroup *cgroup, struct task_struct *tsk,
  1427. bool threadgroup)
  1428. {
  1429. struct io_context *ioc;
  1430. int ret = 0;
  1431. /* task_lock() is needed to avoid races with exit_io_context() */
  1432. task_lock(tsk);
  1433. ioc = tsk->io_context;
  1434. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  1435. ret = -EINVAL;
  1436. task_unlock(tsk);
  1437. return ret;
  1438. }
  1439. static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
  1440. struct cgroup *prev, struct task_struct *tsk,
  1441. bool threadgroup)
  1442. {
  1443. struct io_context *ioc;
  1444. task_lock(tsk);
  1445. ioc = tsk->io_context;
  1446. if (ioc)
  1447. ioc->cgroup_changed = 1;
  1448. task_unlock(tsk);
  1449. }
  1450. void blkio_policy_register(struct blkio_policy_type *blkiop)
  1451. {
  1452. spin_lock(&blkio_list_lock);
  1453. list_add_tail(&blkiop->list, &blkio_list);
  1454. spin_unlock(&blkio_list_lock);
  1455. }
  1456. EXPORT_SYMBOL_GPL(blkio_policy_register);
  1457. void blkio_policy_unregister(struct blkio_policy_type *blkiop)
  1458. {
  1459. spin_lock(&blkio_list_lock);
  1460. list_del_init(&blkiop->list);
  1461. spin_unlock(&blkio_list_lock);
  1462. }
  1463. EXPORT_SYMBOL_GPL(blkio_policy_unregister);
  1464. static int __init init_cgroup_blkio(void)
  1465. {
  1466. return cgroup_load_subsys(&blkio_subsys);
  1467. }
  1468. static void __exit exit_cgroup_blkio(void)
  1469. {
  1470. cgroup_unload_subsys(&blkio_subsys);
  1471. }
  1472. module_init(init_cgroup_blkio);
  1473. module_exit(exit_cgroup_blkio);
  1474. MODULE_LICENSE("GPL");