blk-cgroup.c 47 KB

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