History

block: move the bio cgroup associatation helpers to blk-cgroup.c

Keep the cgroup code together.

Christoph Hellwig committed 6 years ago

Commit 28fc5911 parent a18b9b13 files · +101 −107Browse files

Unified · Split

3 files
  1. block/bio.c +0 −75
  2. block/blk-cgroup.c +101 −2
  3. include/linux/blk-cgroup.h +0 −30
block/bio.c+0−75
@@ -1627,81 +1627,6 @@ int bioset_init_from_src(struct bio_set *bs, struct bio_set *src) · 1626 unmodified lines
1627}1627}
1628EXPORT_SYMBOL(bioset_init_from_src);1628EXPORT_SYMBOL(bioset_init_from_src);
1629 1629
1630#ifdef CONFIG_BLK_CGROUP
1631/**
1632 * bio_associate_blkg_from_css - associate a bio with a specified css
1633 * @bio: target bio
1634 * @css: target css
1635 *
1636 * Associate @bio with the blkg found by combining the css's blkg and the
1637 * request_queue of the @bio. An association failure is handled by walking up
1638 * the blkg tree. Therefore, the blkg associated can be anything between @blkg
1639 * and q->root_blkg. This situation only happens when a cgroup is dying and
1640 * then the remaining bios will spill to the closest alive blkg.
1641 *
1642 * A reference will be taken on the blkg and will be released when @bio is
1643 * freed.
1644 */
1645void bio_associate_blkg_from_css(struct bio *bio,
1646 struct cgroup_subsys_state *css)
1647{
1648 struct request_queue *q = bio->bi_disk->queue;
1649 struct blkcg_gq *blkg = q->root_blkg;
1650
1651 if (bio->bi_blkg)
1652 blkg_put(bio->bi_blkg);
1653
1654 rcu_read_lock();
1655 if (css && css->parent)
1656 blkg = blkg_lookup_create(css_to_blkcg(css), q);
1657 bio->bi_blkg = blkg_tryget_closest(blkg);
1658 rcu_read_unlock();
1659}
1660EXPORT_SYMBOL_GPL(bio_associate_blkg_from_css);
1661
1662/**
1663 * bio_associate_blkg - associate a bio with a blkg
1664 * @bio: target bio
1665 *
1666 * Associate @bio with the blkg found from the bio's css and request_queue.
1667 * If one is not found, bio_lookup_blkg() creates the blkg. If a blkg is
1668 * already associated, the css is reused and association redone as the
1669 * request_queue may have changed.
1670 */
1671void bio_associate_blkg(struct bio *bio)
1672{
1673 struct cgroup_subsys_state *css;
1674
1675 rcu_read_lock();
1676
1677 if (bio->bi_blkg)
1678 css = &bio_blkcg(bio)->css;
1679 else
1680 css = blkcg_css();
1681
1682 bio_associate_blkg_from_css(bio, css);
1683
1684 rcu_read_unlock();
1685}
1686EXPORT_SYMBOL_GPL(bio_associate_blkg);
1687
1688/**
1689 * bio_clone_blkg_association - clone blkg association from src to dst bio
1690 * @dst: destination bio
1691 * @src: source bio
1692 */
1693void bio_clone_blkg_association(struct bio *dst, struct bio *src)
1694{
1695 if (src->bi_blkg) {
1696 if (dst->bi_blkg)
1697 blkg_put(dst->bi_blkg);
1698 blkg_get(src->bi_blkg);
1699 dst->bi_blkg = src->bi_blkg;
1700 }
1701}
1702EXPORT_SYMBOL_GPL(bio_clone_blkg_association);
1703#endif /* CONFIG_BLK_CGROUP */
1704
1705static void __init biovec_init_slabs(void)1630static void __init biovec_init_slabs(void)
1706{1631{
1707 int i;1632 int i;
block/blk-cgroup.c+101−2
@@ -328,7 +328,7 @@ err_free_blkg: · 327 unmodified lines
328 * Returns the blkg or the closest blkg if blkg_create() fails as it walks328 * Returns the blkg or the closest blkg if blkg_create() fails as it walks
329 * down from root.329 * down from root.
330 */330 */
331struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,331static struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
332 struct request_queue *q)332 struct request_queue *q)
333{333{
334 struct blkcg_gq *blkg;334 struct blkcg_gq *blkg;
@@ -377,7 +377,7 @@ struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg, · 42 unmodified lines
377 * This looks up or creates the blkg representing the unique pair377 * This looks up or creates the blkg representing the unique pair
378 * of the blkcg and the request_queue.378 * of the blkcg and the request_queue.
379 */379 */
380struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,380static struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
381 struct request_queue *q)381 struct request_queue *q)
382{382{
383 struct blkcg_gq *blkg = blkg_lookup(blkcg, q);383 struct blkcg_gq *blkg = blkg_lookup(blkcg, q);
@@ -1727,6 +1727,105 @@ void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta) · 1343 unmodified lines
1727 atomic64_add(delta, &blkg->delay_nsec);1727 atomic64_add(delta, &blkg->delay_nsec);
1728}1728}
1729 1729
1730/**
1731 * blkg_tryget_closest - try and get a blkg ref on the closet blkg
1732 * @blkg: blkg to get
1733 *
1734 * This needs to be called rcu protected. As the failure mode here is to walk
1735 * up the blkg tree, this ensure that the blkg->parent pointers are always
1736 * valid. This returns the blkg that it ended up taking a reference on or %NULL
1737 * if no reference was taken.
1738 */
1739static inline struct blkcg_gq *blkg_tryget_closest(struct blkcg_gq *blkg)
1740{
1741 struct blkcg_gq *ret_blkg = NULL;
1742
1743 WARN_ON_ONCE(!rcu_read_lock_held());
1744
1745 while (blkg) {
1746 if (blkg_tryget(blkg)) {
1747 ret_blkg = blkg;
1748 break;
1749 }
1750 blkg = blkg->parent;
1751 }
1752
1753 return ret_blkg;
1754}
1755
1756/**
1757 * bio_associate_blkg_from_css - associate a bio with a specified css
1758 * @bio: target bio
1759 * @css: target css
1760 *
1761 * Associate @bio with the blkg found by combining the css's blkg and the
1762 * request_queue of the @bio. An association failure is handled by walking up
1763 * the blkg tree. Therefore, the blkg associated can be anything between @blkg
1764 * and q->root_blkg. This situation only happens when a cgroup is dying and
1765 * then the remaining bios will spill to the closest alive blkg.
1766 *
1767 * A reference will be taken on the blkg and will be released when @bio is
1768 * freed.
1769 */
1770void bio_associate_blkg_from_css(struct bio *bio,
1771 struct cgroup_subsys_state *css)
1772{
1773 struct request_queue *q = bio->bi_disk->queue;
1774 struct blkcg_gq *blkg = q->root_blkg;
1775
1776 if (bio->bi_blkg)
1777 blkg_put(bio->bi_blkg);
1778
1779 rcu_read_lock();
1780 if (css && css->parent)
1781 blkg = blkg_lookup_create(css_to_blkcg(css), q);
1782 bio->bi_blkg = blkg_tryget_closest(blkg);
1783 rcu_read_unlock();
1784}
1785EXPORT_SYMBOL_GPL(bio_associate_blkg_from_css);
1786
1787/**
1788 * bio_associate_blkg - associate a bio with a blkg
1789 * @bio: target bio
1790 *
1791 * Associate @bio with the blkg found from the bio's css and request_queue.
1792 * If one is not found, bio_lookup_blkg() creates the blkg. If a blkg is
1793 * already associated, the css is reused and association redone as the
1794 * request_queue may have changed.
1795 */
1796void bio_associate_blkg(struct bio *bio)
1797{
1798 struct cgroup_subsys_state *css;
1799
1800 rcu_read_lock();
1801
1802 if (bio->bi_blkg)
1803 css = &bio_blkcg(bio)->css;
1804 else
1805 css = blkcg_css();
1806
1807 bio_associate_blkg_from_css(bio, css);
1808
1809 rcu_read_unlock();
1810}
1811EXPORT_SYMBOL_GPL(bio_associate_blkg);
1812
1813/**
1814 * bio_clone_blkg_association - clone blkg association from src to dst bio
1815 * @dst: destination bio
1816 * @src: source bio
1817 */
1818void bio_clone_blkg_association(struct bio *dst, struct bio *src)
1819{
1820 if (src->bi_blkg) {
1821 if (dst->bi_blkg)
1822 blkg_put(dst->bi_blkg);
1823 blkg_get(src->bi_blkg);
1824 dst->bi_blkg = src->bi_blkg;
1825 }
1826}
1827EXPORT_SYMBOL_GPL(bio_clone_blkg_association);
1828
1730static int __init blkcg_init(void)1829static int __init blkcg_init(void)
1731{1830{
1732 blkcg_punt_bio_wq = alloc_workqueue("blkcg_punt_bio",1831 blkcg_punt_bio_wq = alloc_workqueue("blkcg_punt_bio",
include/linux/blk-cgroup.h+0−30
@@ -183,10 +183,6 @@ extern bool blkcg_debug_stats; · 182 unmodified lines
183 183
184struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,184struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
185 struct request_queue *q, bool update_hint);185 struct request_queue *q, bool update_hint);
186struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
187 struct request_queue *q);
188struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
189 struct request_queue *q);
190int blkcg_init_queue(struct request_queue *q);186int blkcg_init_queue(struct request_queue *q);
191void blkcg_exit_queue(struct request_queue *q);187void blkcg_exit_queue(struct request_queue *q);
192 188
@@ -480,32 +476,6 @@ static inline bool blkg_tryget(struct blkcg_gq *blkg) · 287 unmodified lines
480 return blkg && percpu_ref_tryget(&blkg->refcnt);476 return blkg && percpu_ref_tryget(&blkg->refcnt);
481}477}
482 478
483/**
484 * blkg_tryget_closest - try and get a blkg ref on the closet blkg
485 * @blkg: blkg to get
486 *
487 * This needs to be called rcu protected. As the failure mode here is to walk
488 * up the blkg tree, this ensure that the blkg->parent pointers are always
489 * valid. This returns the blkg that it ended up taking a reference on or %NULL
490 * if no reference was taken.
491 */
492static inline struct blkcg_gq *blkg_tryget_closest(struct blkcg_gq *blkg)
493{
494 struct blkcg_gq *ret_blkg = NULL;
495
496 WARN_ON_ONCE(!rcu_read_lock_held());
497
498 while (blkg) {
499 if (blkg_tryget(blkg)) {
500 ret_blkg = blkg;
501 break;
502 }
503 blkg = blkg->parent;
504 }
505
506 return ret_blkg;
507}
508
509/**479/**
510 * blkg_put - put a blkg reference480 * blkg_put - put a blkg reference
511 * @blkg: blkg to put481 * @blkg: blkg to put