blk-cgroup.c 43 KB

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