blk-cgroup.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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/kdev_t.h>
  15. #include <linux/module.h>
  16. #include <linux/err.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/slab.h>
  19. #include <linux/genhd.h>
  20. #include <linux/delay.h>
  21. #include <linux/atomic.h>
  22. #include "blk-cgroup.h"
  23. #include "blk.h"
  24. #define MAX_KEY_LEN 100
  25. static DEFINE_MUTEX(blkcg_pol_mutex);
  26. struct blkcg blkcg_root = { .cfq_weight = 2 * CFQ_WEIGHT_DEFAULT,
  27. .cfq_leaf_weight = 2 * CFQ_WEIGHT_DEFAULT, };
  28. EXPORT_SYMBOL_GPL(blkcg_root);
  29. static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
  30. static bool blkcg_policy_enabled(struct request_queue *q,
  31. const struct blkcg_policy *pol)
  32. {
  33. return pol && test_bit(pol->plid, q->blkcg_pols);
  34. }
  35. /**
  36. * blkg_free - free a blkg
  37. * @blkg: blkg to free
  38. *
  39. * Free @blkg which may be partially allocated.
  40. */
  41. static void blkg_free(struct blkcg_gq *blkg)
  42. {
  43. int i;
  44. if (!blkg)
  45. return;
  46. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  47. struct blkcg_policy *pol = blkcg_policy[i];
  48. struct blkg_policy_data *pd = blkg->pd[i];
  49. if (!pd)
  50. continue;
  51. if (pol && pol->pd_exit_fn)
  52. pol->pd_exit_fn(blkg);
  53. kfree(pd);
  54. }
  55. blk_exit_rl(&blkg->rl);
  56. kfree(blkg);
  57. }
  58. /**
  59. * blkg_alloc - allocate a blkg
  60. * @blkcg: block cgroup the new blkg is associated with
  61. * @q: request_queue the new blkg is associated with
  62. * @gfp_mask: allocation mask to use
  63. *
  64. * Allocate a new blkg assocating @blkcg and @q.
  65. */
  66. static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
  67. gfp_t gfp_mask)
  68. {
  69. struct blkcg_gq *blkg;
  70. int i;
  71. /* alloc and init base part */
  72. blkg = kzalloc_node(sizeof(*blkg), gfp_mask, q->node);
  73. if (!blkg)
  74. return NULL;
  75. blkg->q = q;
  76. INIT_LIST_HEAD(&blkg->q_node);
  77. blkg->blkcg = blkcg;
  78. blkg->refcnt = 1;
  79. /* root blkg uses @q->root_rl, init rl only for !root blkgs */
  80. if (blkcg != &blkcg_root) {
  81. if (blk_init_rl(&blkg->rl, q, gfp_mask))
  82. goto err_free;
  83. blkg->rl.blkg = blkg;
  84. }
  85. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  86. struct blkcg_policy *pol = blkcg_policy[i];
  87. struct blkg_policy_data *pd;
  88. if (!blkcg_policy_enabled(q, pol))
  89. continue;
  90. /* alloc per-policy data and attach it to blkg */
  91. pd = kzalloc_node(pol->pd_size, gfp_mask, q->node);
  92. if (!pd)
  93. goto err_free;
  94. blkg->pd[i] = pd;
  95. pd->blkg = blkg;
  96. /* invoke per-policy init */
  97. if (pol->pd_init_fn)
  98. pol->pd_init_fn(blkg);
  99. }
  100. return blkg;
  101. err_free:
  102. blkg_free(blkg);
  103. return NULL;
  104. }
  105. static struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg,
  106. struct request_queue *q, bool update_hint)
  107. {
  108. struct blkcg_gq *blkg;
  109. blkg = rcu_dereference(blkcg->blkg_hint);
  110. if (blkg && blkg->q == q)
  111. return blkg;
  112. /*
  113. * Hint didn't match. Look up from the radix tree. Note that the
  114. * hint can only be updated under queue_lock as otherwise @blkg
  115. * could have already been removed from blkg_tree. The caller is
  116. * responsible for grabbing queue_lock if @update_hint.
  117. */
  118. blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
  119. if (blkg && blkg->q == q) {
  120. if (update_hint) {
  121. lockdep_assert_held(q->queue_lock);
  122. rcu_assign_pointer(blkcg->blkg_hint, blkg);
  123. }
  124. return blkg;
  125. }
  126. return NULL;
  127. }
  128. /**
  129. * blkg_lookup - lookup blkg for the specified blkcg - q pair
  130. * @blkcg: blkcg of interest
  131. * @q: request_queue of interest
  132. *
  133. * Lookup blkg for the @blkcg - @q pair. This function should be called
  134. * under RCU read lock and is guaranteed to return %NULL if @q is bypassing
  135. * - see blk_queue_bypass_start() for details.
  136. */
  137. struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q)
  138. {
  139. WARN_ON_ONCE(!rcu_read_lock_held());
  140. if (unlikely(blk_queue_bypass(q)))
  141. return NULL;
  142. return __blkg_lookup(blkcg, q, false);
  143. }
  144. EXPORT_SYMBOL_GPL(blkg_lookup);
  145. /*
  146. * If @new_blkg is %NULL, this function tries to allocate a new one as
  147. * necessary using %GFP_ATOMIC. @new_blkg is always consumed on return.
  148. */
  149. static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
  150. struct request_queue *q,
  151. struct blkcg_gq *new_blkg)
  152. {
  153. struct blkcg_gq *blkg;
  154. int ret;
  155. WARN_ON_ONCE(!rcu_read_lock_held());
  156. lockdep_assert_held(q->queue_lock);
  157. /* blkg holds a reference to blkcg */
  158. if (!css_tryget(&blkcg->css)) {
  159. ret = -EINVAL;
  160. goto err_free_blkg;
  161. }
  162. /* allocate */
  163. if (!new_blkg) {
  164. new_blkg = blkg_alloc(blkcg, q, GFP_ATOMIC);
  165. if (unlikely(!new_blkg)) {
  166. ret = -ENOMEM;
  167. goto err_put_css;
  168. }
  169. }
  170. blkg = new_blkg;
  171. /* link parent and insert */
  172. if (blkcg_parent(blkcg)) {
  173. blkg->parent = __blkg_lookup(blkcg_parent(blkcg), q, false);
  174. if (WARN_ON_ONCE(!blkg->parent)) {
  175. blkg = ERR_PTR(-EINVAL);
  176. goto err_put_css;
  177. }
  178. blkg_get(blkg->parent);
  179. }
  180. spin_lock(&blkcg->lock);
  181. ret = radix_tree_insert(&blkcg->blkg_tree, q->id, blkg);
  182. if (likely(!ret)) {
  183. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  184. list_add(&blkg->q_node, &q->blkg_list);
  185. }
  186. spin_unlock(&blkcg->lock);
  187. if (!ret)
  188. return blkg;
  189. /* @blkg failed fully initialized, use the usual release path */
  190. blkg_put(blkg);
  191. return ERR_PTR(ret);
  192. err_put_css:
  193. css_put(&blkcg->css);
  194. err_free_blkg:
  195. blkg_free(new_blkg);
  196. return ERR_PTR(ret);
  197. }
  198. /**
  199. * blkg_lookup_create - lookup blkg, try to create one if not there
  200. * @blkcg: blkcg of interest
  201. * @q: request_queue of interest
  202. *
  203. * Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to
  204. * create one. blkg creation is performed recursively from blkcg_root such
  205. * that all non-root blkg's have access to the parent blkg. This function
  206. * should be called under RCU read lock and @q->queue_lock.
  207. *
  208. * Returns pointer to the looked up or created blkg on success, ERR_PTR()
  209. * value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not
  210. * dead and bypassing, returns ERR_PTR(-EBUSY).
  211. */
  212. struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
  213. struct request_queue *q)
  214. {
  215. struct blkcg_gq *blkg;
  216. WARN_ON_ONCE(!rcu_read_lock_held());
  217. lockdep_assert_held(q->queue_lock);
  218. /*
  219. * This could be the first entry point of blkcg implementation and
  220. * we shouldn't allow anything to go through for a bypassing queue.
  221. */
  222. if (unlikely(blk_queue_bypass(q)))
  223. return ERR_PTR(blk_queue_dying(q) ? -EINVAL : -EBUSY);
  224. blkg = __blkg_lookup(blkcg, q, true);
  225. if (blkg)
  226. return blkg;
  227. /*
  228. * Create blkgs walking down from blkcg_root to @blkcg, so that all
  229. * non-root blkgs have access to their parents.
  230. */
  231. while (true) {
  232. struct blkcg *pos = blkcg;
  233. struct blkcg *parent = blkcg_parent(blkcg);
  234. while (parent && !__blkg_lookup(parent, q, false)) {
  235. pos = parent;
  236. parent = blkcg_parent(parent);
  237. }
  238. blkg = blkg_create(pos, q, NULL);
  239. if (pos == blkcg || IS_ERR(blkg))
  240. return blkg;
  241. }
  242. }
  243. EXPORT_SYMBOL_GPL(blkg_lookup_create);
  244. static void blkg_destroy(struct blkcg_gq *blkg)
  245. {
  246. struct blkcg *blkcg = blkg->blkcg;
  247. lockdep_assert_held(blkg->q->queue_lock);
  248. lockdep_assert_held(&blkcg->lock);
  249. /* Something wrong if we are trying to remove same group twice */
  250. WARN_ON_ONCE(list_empty(&blkg->q_node));
  251. WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
  252. radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
  253. list_del_init(&blkg->q_node);
  254. hlist_del_init_rcu(&blkg->blkcg_node);
  255. /*
  256. * Both setting lookup hint to and clearing it from @blkg are done
  257. * under queue_lock. If it's not pointing to @blkg now, it never
  258. * will. Hint assignment itself can race safely.
  259. */
  260. if (rcu_dereference_raw(blkcg->blkg_hint) == blkg)
  261. rcu_assign_pointer(blkcg->blkg_hint, NULL);
  262. /*
  263. * Put the reference taken at the time of creation so that when all
  264. * queues are gone, group can be destroyed.
  265. */
  266. blkg_put(blkg);
  267. }
  268. /**
  269. * blkg_destroy_all - destroy all blkgs associated with a request_queue
  270. * @q: request_queue of interest
  271. *
  272. * Destroy all blkgs associated with @q.
  273. */
  274. static void blkg_destroy_all(struct request_queue *q)
  275. {
  276. struct blkcg_gq *blkg, *n;
  277. lockdep_assert_held(q->queue_lock);
  278. list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
  279. struct blkcg *blkcg = blkg->blkcg;
  280. spin_lock(&blkcg->lock);
  281. blkg_destroy(blkg);
  282. spin_unlock(&blkcg->lock);
  283. }
  284. /*
  285. * root blkg is destroyed. Just clear the pointer since
  286. * root_rl does not take reference on root blkg.
  287. */
  288. q->root_blkg = NULL;
  289. q->root_rl.blkg = NULL;
  290. }
  291. static void blkg_rcu_free(struct rcu_head *rcu_head)
  292. {
  293. blkg_free(container_of(rcu_head, struct blkcg_gq, rcu_head));
  294. }
  295. void __blkg_release(struct blkcg_gq *blkg)
  296. {
  297. /* release the blkcg and parent blkg refs this blkg has been holding */
  298. css_put(&blkg->blkcg->css);
  299. if (blkg->parent)
  300. blkg_put(blkg->parent);
  301. /*
  302. * A group is freed in rcu manner. But having an rcu lock does not
  303. * mean that one can access all the fields of blkg and assume these
  304. * are valid. For example, don't try to follow throtl_data and
  305. * request queue links.
  306. *
  307. * Having a reference to blkg under an rcu allows acess to only
  308. * values local to groups like group stats and group rate limits
  309. */
  310. call_rcu(&blkg->rcu_head, blkg_rcu_free);
  311. }
  312. EXPORT_SYMBOL_GPL(__blkg_release);
  313. /*
  314. * The next function used by blk_queue_for_each_rl(). It's a bit tricky
  315. * because the root blkg uses @q->root_rl instead of its own rl.
  316. */
  317. struct request_list *__blk_queue_next_rl(struct request_list *rl,
  318. struct request_queue *q)
  319. {
  320. struct list_head *ent;
  321. struct blkcg_gq *blkg;
  322. /*
  323. * Determine the current blkg list_head. The first entry is
  324. * root_rl which is off @q->blkg_list and mapped to the head.
  325. */
  326. if (rl == &q->root_rl) {
  327. ent = &q->blkg_list;
  328. /* There are no more block groups, hence no request lists */
  329. if (list_empty(ent))
  330. return NULL;
  331. } else {
  332. blkg = container_of(rl, struct blkcg_gq, rl);
  333. ent = &blkg->q_node;
  334. }
  335. /* walk to the next list_head, skip root blkcg */
  336. ent = ent->next;
  337. if (ent == &q->root_blkg->q_node)
  338. ent = ent->next;
  339. if (ent == &q->blkg_list)
  340. return NULL;
  341. blkg = container_of(ent, struct blkcg_gq, q_node);
  342. return &blkg->rl;
  343. }
  344. static int blkcg_reset_stats(struct cgroup *cgroup, struct cftype *cftype,
  345. u64 val)
  346. {
  347. struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
  348. struct blkcg_gq *blkg;
  349. struct hlist_node *n;
  350. int i;
  351. mutex_lock(&blkcg_pol_mutex);
  352. spin_lock_irq(&blkcg->lock);
  353. /*
  354. * Note that stat reset is racy - it doesn't synchronize against
  355. * stat updates. This is a debug feature which shouldn't exist
  356. * anyway. If you get hit by a race, retry.
  357. */
  358. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  359. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  360. struct blkcg_policy *pol = blkcg_policy[i];
  361. if (blkcg_policy_enabled(blkg->q, pol) &&
  362. pol->pd_reset_stats_fn)
  363. pol->pd_reset_stats_fn(blkg);
  364. }
  365. }
  366. spin_unlock_irq(&blkcg->lock);
  367. mutex_unlock(&blkcg_pol_mutex);
  368. return 0;
  369. }
  370. static const char *blkg_dev_name(struct blkcg_gq *blkg)
  371. {
  372. /* some drivers (floppy) instantiate a queue w/o disk registered */
  373. if (blkg->q->backing_dev_info.dev)
  374. return dev_name(blkg->q->backing_dev_info.dev);
  375. return NULL;
  376. }
  377. /**
  378. * blkcg_print_blkgs - helper for printing per-blkg data
  379. * @sf: seq_file to print to
  380. * @blkcg: blkcg of interest
  381. * @prfill: fill function to print out a blkg
  382. * @pol: policy in question
  383. * @data: data to be passed to @prfill
  384. * @show_total: to print out sum of prfill return values or not
  385. *
  386. * This function invokes @prfill on each blkg of @blkcg if pd for the
  387. * policy specified by @pol exists. @prfill is invoked with @sf, the
  388. * policy data and @data. If @show_total is %true, the sum of the return
  389. * values from @prfill is printed with "Total" label at the end.
  390. *
  391. * This is to be used to construct print functions for
  392. * cftype->read_seq_string method.
  393. */
  394. void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
  395. u64 (*prfill)(struct seq_file *,
  396. struct blkg_policy_data *, int),
  397. const struct blkcg_policy *pol, int data,
  398. bool show_total)
  399. {
  400. struct blkcg_gq *blkg;
  401. struct hlist_node *n;
  402. u64 total = 0;
  403. spin_lock_irq(&blkcg->lock);
  404. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
  405. if (blkcg_policy_enabled(blkg->q, pol))
  406. total += prfill(sf, blkg->pd[pol->plid], data);
  407. spin_unlock_irq(&blkcg->lock);
  408. if (show_total)
  409. seq_printf(sf, "Total %llu\n", (unsigned long long)total);
  410. }
  411. EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
  412. /**
  413. * __blkg_prfill_u64 - prfill helper for a single u64 value
  414. * @sf: seq_file to print to
  415. * @pd: policy private data of interest
  416. * @v: value to print
  417. *
  418. * Print @v to @sf for the device assocaited with @pd.
  419. */
  420. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
  421. {
  422. const char *dname = blkg_dev_name(pd->blkg);
  423. if (!dname)
  424. return 0;
  425. seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
  426. return v;
  427. }
  428. EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
  429. /**
  430. * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
  431. * @sf: seq_file to print to
  432. * @pd: policy private data of interest
  433. * @rwstat: rwstat to print
  434. *
  435. * Print @rwstat to @sf for the device assocaited with @pd.
  436. */
  437. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  438. const struct blkg_rwstat *rwstat)
  439. {
  440. static const char *rwstr[] = {
  441. [BLKG_RWSTAT_READ] = "Read",
  442. [BLKG_RWSTAT_WRITE] = "Write",
  443. [BLKG_RWSTAT_SYNC] = "Sync",
  444. [BLKG_RWSTAT_ASYNC] = "Async",
  445. };
  446. const char *dname = blkg_dev_name(pd->blkg);
  447. u64 v;
  448. int i;
  449. if (!dname)
  450. return 0;
  451. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  452. seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
  453. (unsigned long long)rwstat->cnt[i]);
  454. v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
  455. seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
  456. return v;
  457. }
  458. /**
  459. * blkg_prfill_stat - prfill callback for blkg_stat
  460. * @sf: seq_file to print to
  461. * @pd: policy private data of interest
  462. * @off: offset to the blkg_stat in @pd
  463. *
  464. * prfill callback for printing a blkg_stat.
  465. */
  466. u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
  467. {
  468. return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
  469. }
  470. EXPORT_SYMBOL_GPL(blkg_prfill_stat);
  471. /**
  472. * blkg_prfill_rwstat - prfill callback for blkg_rwstat
  473. * @sf: seq_file to print to
  474. * @pd: policy private data of interest
  475. * @off: offset to the blkg_rwstat in @pd
  476. *
  477. * prfill callback for printing a blkg_rwstat.
  478. */
  479. u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  480. int off)
  481. {
  482. struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
  483. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  484. }
  485. EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
  486. /**
  487. * blkg_conf_prep - parse and prepare for per-blkg config update
  488. * @blkcg: target block cgroup
  489. * @pol: target policy
  490. * @input: input string
  491. * @ctx: blkg_conf_ctx to be filled
  492. *
  493. * Parse per-blkg config update from @input and initialize @ctx with the
  494. * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
  495. * value. This function returns with RCU read lock and queue lock held and
  496. * must be paired with blkg_conf_finish().
  497. */
  498. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  499. const char *input, struct blkg_conf_ctx *ctx)
  500. __acquires(rcu) __acquires(disk->queue->queue_lock)
  501. {
  502. struct gendisk *disk;
  503. struct blkcg_gq *blkg;
  504. unsigned int major, minor;
  505. unsigned long long v;
  506. int part, ret;
  507. if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
  508. return -EINVAL;
  509. disk = get_gendisk(MKDEV(major, minor), &part);
  510. if (!disk || part)
  511. return -EINVAL;
  512. rcu_read_lock();
  513. spin_lock_irq(disk->queue->queue_lock);
  514. if (blkcg_policy_enabled(disk->queue, pol))
  515. blkg = blkg_lookup_create(blkcg, disk->queue);
  516. else
  517. blkg = ERR_PTR(-EINVAL);
  518. if (IS_ERR(blkg)) {
  519. ret = PTR_ERR(blkg);
  520. rcu_read_unlock();
  521. spin_unlock_irq(disk->queue->queue_lock);
  522. put_disk(disk);
  523. /*
  524. * If queue was bypassing, we should retry. Do so after a
  525. * short msleep(). It isn't strictly necessary but queue
  526. * can be bypassing for some time and it's always nice to
  527. * avoid busy looping.
  528. */
  529. if (ret == -EBUSY) {
  530. msleep(10);
  531. ret = restart_syscall();
  532. }
  533. return ret;
  534. }
  535. ctx->disk = disk;
  536. ctx->blkg = blkg;
  537. ctx->v = v;
  538. return 0;
  539. }
  540. EXPORT_SYMBOL_GPL(blkg_conf_prep);
  541. /**
  542. * blkg_conf_finish - finish up per-blkg config update
  543. * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
  544. *
  545. * Finish up after per-blkg config update. This function must be paired
  546. * with blkg_conf_prep().
  547. */
  548. void blkg_conf_finish(struct blkg_conf_ctx *ctx)
  549. __releases(ctx->disk->queue->queue_lock) __releases(rcu)
  550. {
  551. spin_unlock_irq(ctx->disk->queue->queue_lock);
  552. rcu_read_unlock();
  553. put_disk(ctx->disk);
  554. }
  555. EXPORT_SYMBOL_GPL(blkg_conf_finish);
  556. struct cftype blkcg_files[] = {
  557. {
  558. .name = "reset_stats",
  559. .write_u64 = blkcg_reset_stats,
  560. },
  561. { } /* terminate */
  562. };
  563. /**
  564. * blkcg_css_offline - cgroup css_offline callback
  565. * @cgroup: cgroup of interest
  566. *
  567. * This function is called when @cgroup is about to go away and responsible
  568. * for shooting down all blkgs associated with @cgroup. blkgs should be
  569. * removed while holding both q and blkcg locks. As blkcg lock is nested
  570. * inside q lock, this function performs reverse double lock dancing.
  571. *
  572. * This is the blkcg counterpart of ioc_release_fn().
  573. */
  574. static void blkcg_css_offline(struct cgroup *cgroup)
  575. {
  576. struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
  577. spin_lock_irq(&blkcg->lock);
  578. while (!hlist_empty(&blkcg->blkg_list)) {
  579. struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
  580. struct blkcg_gq, blkcg_node);
  581. struct request_queue *q = blkg->q;
  582. if (spin_trylock(q->queue_lock)) {
  583. blkg_destroy(blkg);
  584. spin_unlock(q->queue_lock);
  585. } else {
  586. spin_unlock_irq(&blkcg->lock);
  587. cpu_relax();
  588. spin_lock_irq(&blkcg->lock);
  589. }
  590. }
  591. spin_unlock_irq(&blkcg->lock);
  592. }
  593. static void blkcg_css_free(struct cgroup *cgroup)
  594. {
  595. struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
  596. if (blkcg != &blkcg_root)
  597. kfree(blkcg);
  598. }
  599. static struct cgroup_subsys_state *blkcg_css_alloc(struct cgroup *cgroup)
  600. {
  601. static atomic64_t id_seq = ATOMIC64_INIT(0);
  602. struct blkcg *blkcg;
  603. struct cgroup *parent = cgroup->parent;
  604. if (!parent) {
  605. blkcg = &blkcg_root;
  606. goto done;
  607. }
  608. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  609. if (!blkcg)
  610. return ERR_PTR(-ENOMEM);
  611. blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
  612. blkcg->cfq_leaf_weight = CFQ_WEIGHT_DEFAULT;
  613. blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
  614. done:
  615. spin_lock_init(&blkcg->lock);
  616. INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_ATOMIC);
  617. INIT_HLIST_HEAD(&blkcg->blkg_list);
  618. return &blkcg->css;
  619. }
  620. /**
  621. * blkcg_init_queue - initialize blkcg part of request queue
  622. * @q: request_queue to initialize
  623. *
  624. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  625. * part of new request_queue @q.
  626. *
  627. * RETURNS:
  628. * 0 on success, -errno on failure.
  629. */
  630. int blkcg_init_queue(struct request_queue *q)
  631. {
  632. might_sleep();
  633. return blk_throtl_init(q);
  634. }
  635. /**
  636. * blkcg_drain_queue - drain blkcg part of request_queue
  637. * @q: request_queue to drain
  638. *
  639. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  640. */
  641. void blkcg_drain_queue(struct request_queue *q)
  642. {
  643. lockdep_assert_held(q->queue_lock);
  644. blk_throtl_drain(q);
  645. }
  646. /**
  647. * blkcg_exit_queue - exit and release blkcg part of request_queue
  648. * @q: request_queue being released
  649. *
  650. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  651. */
  652. void blkcg_exit_queue(struct request_queue *q)
  653. {
  654. spin_lock_irq(q->queue_lock);
  655. blkg_destroy_all(q);
  656. spin_unlock_irq(q->queue_lock);
  657. blk_throtl_exit(q);
  658. }
  659. /*
  660. * We cannot support shared io contexts, as we have no mean to support
  661. * two tasks with the same ioc in two different groups without major rework
  662. * of the main cic data structures. For now we allow a task to change
  663. * its cgroup only if it's the only owner of its ioc.
  664. */
  665. static int blkcg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
  666. {
  667. struct task_struct *task;
  668. struct io_context *ioc;
  669. int ret = 0;
  670. /* task_lock() is needed to avoid races with exit_io_context() */
  671. cgroup_taskset_for_each(task, cgrp, tset) {
  672. task_lock(task);
  673. ioc = task->io_context;
  674. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  675. ret = -EINVAL;
  676. task_unlock(task);
  677. if (ret)
  678. break;
  679. }
  680. return ret;
  681. }
  682. struct cgroup_subsys blkio_subsys = {
  683. .name = "blkio",
  684. .css_alloc = blkcg_css_alloc,
  685. .css_offline = blkcg_css_offline,
  686. .css_free = blkcg_css_free,
  687. .can_attach = blkcg_can_attach,
  688. .subsys_id = blkio_subsys_id,
  689. .base_cftypes = blkcg_files,
  690. .module = THIS_MODULE,
  691. /*
  692. * blkio subsystem is utterly broken in terms of hierarchy support.
  693. * It treats all cgroups equally regardless of where they're
  694. * located in the hierarchy - all cgroups are treated as if they're
  695. * right below the root. Fix it and remove the following.
  696. */
  697. .broken_hierarchy = true,
  698. };
  699. EXPORT_SYMBOL_GPL(blkio_subsys);
  700. /**
  701. * blkcg_activate_policy - activate a blkcg policy on a request_queue
  702. * @q: request_queue of interest
  703. * @pol: blkcg policy to activate
  704. *
  705. * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
  706. * bypass mode to populate its blkgs with policy_data for @pol.
  707. *
  708. * Activation happens with @q bypassed, so nobody would be accessing blkgs
  709. * from IO path. Update of each blkg is protected by both queue and blkcg
  710. * locks so that holding either lock and testing blkcg_policy_enabled() is
  711. * always enough for dereferencing policy data.
  712. *
  713. * The caller is responsible for synchronizing [de]activations and policy
  714. * [un]registerations. Returns 0 on success, -errno on failure.
  715. */
  716. int blkcg_activate_policy(struct request_queue *q,
  717. const struct blkcg_policy *pol)
  718. {
  719. LIST_HEAD(pds);
  720. struct blkcg_gq *blkg, *new_blkg;
  721. struct blkg_policy_data *pd, *n;
  722. int cnt = 0, ret;
  723. bool preloaded;
  724. if (blkcg_policy_enabled(q, pol))
  725. return 0;
  726. /* preallocations for root blkg */
  727. new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
  728. if (!new_blkg)
  729. return -ENOMEM;
  730. preloaded = !radix_tree_preload(GFP_KERNEL);
  731. blk_queue_bypass_start(q);
  732. /*
  733. * Make sure the root blkg exists and count the existing blkgs. As
  734. * @q is bypassing at this point, blkg_lookup_create() can't be
  735. * used. Open code it.
  736. */
  737. spin_lock_irq(q->queue_lock);
  738. rcu_read_lock();
  739. blkg = __blkg_lookup(&blkcg_root, q, false);
  740. if (blkg)
  741. blkg_free(new_blkg);
  742. else
  743. blkg = blkg_create(&blkcg_root, q, new_blkg);
  744. rcu_read_unlock();
  745. if (preloaded)
  746. radix_tree_preload_end();
  747. if (IS_ERR(blkg)) {
  748. ret = PTR_ERR(blkg);
  749. goto out_unlock;
  750. }
  751. q->root_blkg = blkg;
  752. q->root_rl.blkg = blkg;
  753. list_for_each_entry(blkg, &q->blkg_list, q_node)
  754. cnt++;
  755. spin_unlock_irq(q->queue_lock);
  756. /* allocate policy_data for all existing blkgs */
  757. while (cnt--) {
  758. pd = kzalloc_node(pol->pd_size, GFP_KERNEL, q->node);
  759. if (!pd) {
  760. ret = -ENOMEM;
  761. goto out_free;
  762. }
  763. list_add_tail(&pd->alloc_node, &pds);
  764. }
  765. /*
  766. * Install the allocated pds. With @q bypassing, no new blkg
  767. * should have been created while the queue lock was dropped.
  768. */
  769. spin_lock_irq(q->queue_lock);
  770. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  771. if (WARN_ON(list_empty(&pds))) {
  772. /* umm... this shouldn't happen, just abort */
  773. ret = -ENOMEM;
  774. goto out_unlock;
  775. }
  776. pd = list_first_entry(&pds, struct blkg_policy_data, alloc_node);
  777. list_del_init(&pd->alloc_node);
  778. /* grab blkcg lock too while installing @pd on @blkg */
  779. spin_lock(&blkg->blkcg->lock);
  780. blkg->pd[pol->plid] = pd;
  781. pd->blkg = blkg;
  782. pol->pd_init_fn(blkg);
  783. spin_unlock(&blkg->blkcg->lock);
  784. }
  785. __set_bit(pol->plid, q->blkcg_pols);
  786. ret = 0;
  787. out_unlock:
  788. spin_unlock_irq(q->queue_lock);
  789. out_free:
  790. blk_queue_bypass_end(q);
  791. list_for_each_entry_safe(pd, n, &pds, alloc_node)
  792. kfree(pd);
  793. return ret;
  794. }
  795. EXPORT_SYMBOL_GPL(blkcg_activate_policy);
  796. /**
  797. * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
  798. * @q: request_queue of interest
  799. * @pol: blkcg policy to deactivate
  800. *
  801. * Deactivate @pol on @q. Follows the same synchronization rules as
  802. * blkcg_activate_policy().
  803. */
  804. void blkcg_deactivate_policy(struct request_queue *q,
  805. const struct blkcg_policy *pol)
  806. {
  807. struct blkcg_gq *blkg;
  808. if (!blkcg_policy_enabled(q, pol))
  809. return;
  810. blk_queue_bypass_start(q);
  811. spin_lock_irq(q->queue_lock);
  812. __clear_bit(pol->plid, q->blkcg_pols);
  813. /* if no policy is left, no need for blkgs - shoot them down */
  814. if (bitmap_empty(q->blkcg_pols, BLKCG_MAX_POLS))
  815. blkg_destroy_all(q);
  816. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  817. /* grab blkcg lock too while removing @pd from @blkg */
  818. spin_lock(&blkg->blkcg->lock);
  819. if (pol->pd_exit_fn)
  820. pol->pd_exit_fn(blkg);
  821. kfree(blkg->pd[pol->plid]);
  822. blkg->pd[pol->plid] = NULL;
  823. spin_unlock(&blkg->blkcg->lock);
  824. }
  825. spin_unlock_irq(q->queue_lock);
  826. blk_queue_bypass_end(q);
  827. }
  828. EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
  829. /**
  830. * blkcg_policy_register - register a blkcg policy
  831. * @pol: blkcg policy to register
  832. *
  833. * Register @pol with blkcg core. Might sleep and @pol may be modified on
  834. * successful registration. Returns 0 on success and -errno on failure.
  835. */
  836. int blkcg_policy_register(struct blkcg_policy *pol)
  837. {
  838. int i, ret;
  839. if (WARN_ON(pol->pd_size < sizeof(struct blkg_policy_data)))
  840. return -EINVAL;
  841. mutex_lock(&blkcg_pol_mutex);
  842. /* find an empty slot */
  843. ret = -ENOSPC;
  844. for (i = 0; i < BLKCG_MAX_POLS; i++)
  845. if (!blkcg_policy[i])
  846. break;
  847. if (i >= BLKCG_MAX_POLS)
  848. goto out_unlock;
  849. /* register and update blkgs */
  850. pol->plid = i;
  851. blkcg_policy[i] = pol;
  852. /* everything is in place, add intf files for the new policy */
  853. if (pol->cftypes)
  854. WARN_ON(cgroup_add_cftypes(&blkio_subsys, pol->cftypes));
  855. ret = 0;
  856. out_unlock:
  857. mutex_unlock(&blkcg_pol_mutex);
  858. return ret;
  859. }
  860. EXPORT_SYMBOL_GPL(blkcg_policy_register);
  861. /**
  862. * blkcg_policy_unregister - unregister a blkcg policy
  863. * @pol: blkcg policy to unregister
  864. *
  865. * Undo blkcg_policy_register(@pol). Might sleep.
  866. */
  867. void blkcg_policy_unregister(struct blkcg_policy *pol)
  868. {
  869. mutex_lock(&blkcg_pol_mutex);
  870. if (WARN_ON(blkcg_policy[pol->plid] != pol))
  871. goto out_unlock;
  872. /* kill the intf files first */
  873. if (pol->cftypes)
  874. cgroup_rm_cftypes(&blkio_subsys, pol->cftypes);
  875. /* unregister and update blkgs */
  876. blkcg_policy[pol->plid] = NULL;
  877. out_unlock:
  878. mutex_unlock(&blkcg_pol_mutex);
  879. }
  880. EXPORT_SYMBOL_GPL(blkcg_policy_unregister);