@@ -1455,17 +1455,126 @@ static int cgroup_get_rootdir(struct super_block *sb)
14551455 return 0 ;
14561456}
14571457
1458+ static int cgroup_setup_root (struct cgroupfs_root * root )
1459+ {
1460+ LIST_HEAD (tmp_links );
1461+ struct super_block * sb = root -> sb ;
1462+ struct cgroup * root_cgrp = & root -> top_cgroup ;
1463+ struct cgroupfs_root * existing_root ;
1464+ struct css_set * cset ;
1465+ struct inode * inode ;
1466+ const struct cred * cred ;
1467+ int i , ret ;
1468+
1469+ lockdep_assert_held (& cgroup_tree_mutex );
1470+ lockdep_assert_held (& cgroup_mutex );
1471+ BUG_ON (sb -> s_root != NULL );
1472+
1473+ mutex_unlock (& cgroup_mutex );
1474+ mutex_unlock (& cgroup_tree_mutex );
1475+
1476+ ret = cgroup_get_rootdir (sb );
1477+ if (ret ) {
1478+ mutex_lock (& cgroup_tree_mutex );
1479+ mutex_lock (& cgroup_mutex );
1480+ return ret ;
1481+ }
1482+ inode = sb -> s_root -> d_inode ;
1483+
1484+ mutex_lock (& inode -> i_mutex );
1485+ mutex_lock (& cgroup_tree_mutex );
1486+ mutex_lock (& cgroup_mutex );
1487+
1488+ ret = idr_alloc (& root -> cgroup_idr , root_cgrp , 0 , 1 , GFP_KERNEL );
1489+ if (ret < 0 )
1490+ goto out_unlock ;
1491+ root_cgrp -> id = ret ;
1492+
1493+ /* check for name clashes with existing mounts */
1494+ ret = - EBUSY ;
1495+ if (strlen (root -> name ))
1496+ for_each_active_root (existing_root )
1497+ if (!strcmp (existing_root -> name , root -> name ))
1498+ goto out_unlock ;
1499+
1500+ /*
1501+ * We're accessing css_set_count without locking css_set_lock here,
1502+ * but that's OK - it can only be increased by someone holding
1503+ * cgroup_lock, and that's us. The worst that can happen is that we
1504+ * have some link structures left over
1505+ */
1506+ ret = allocate_cgrp_cset_links (css_set_count , & tmp_links );
1507+ if (ret )
1508+ goto out_unlock ;
1509+
1510+ /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1511+ ret = cgroup_init_root_id (root , 2 , 0 );
1512+ if (ret )
1513+ goto out_unlock ;
1514+
1515+ sb -> s_root -> d_fsdata = root_cgrp ;
1516+ root_cgrp -> dentry = sb -> s_root ;
1517+
1518+ /*
1519+ * We're inside get_sb() and will call lookup_one_len() to create
1520+ * the root files, which doesn't work if SELinux is in use. The
1521+ * following cred dancing somehow works around it. See 2ce9738ba
1522+ * ("cgroupfs: use init_cred when populating new cgroupfs mount")
1523+ * for more details.
1524+ */
1525+ cred = override_creds (& init_cred );
1526+
1527+ ret = cgroup_addrm_files (root_cgrp , cgroup_base_files , true);
1528+ if (ret )
1529+ goto rm_base_files ;
1530+
1531+ ret = rebind_subsystems (root , root -> subsys_mask , 0 );
1532+ if (ret )
1533+ goto rm_base_files ;
1534+
1535+ revert_creds (cred );
1536+
1537+ /*
1538+ * There must be no failure case after here, since rebinding takes
1539+ * care of subsystems' refcounts, which are explicitly dropped in
1540+ * the failure exit path.
1541+ */
1542+ list_add (& root -> root_list , & cgroup_roots );
1543+ cgroup_root_count ++ ;
1544+
1545+ /*
1546+ * Link the top cgroup in this hierarchy into all the css_set
1547+ * objects.
1548+ */
1549+ write_lock (& css_set_lock );
1550+ hash_for_each (css_set_table , i , cset , hlist )
1551+ link_css_set (& tmp_links , cset , root_cgrp );
1552+ write_unlock (& css_set_lock );
1553+
1554+ BUG_ON (!list_empty (& root_cgrp -> children ));
1555+ BUG_ON (root -> number_of_cgroups != 1 );
1556+
1557+ ret = 0 ;
1558+ goto out_unlock ;
1559+
1560+ rm_base_files :
1561+ cgroup_addrm_files (& root -> top_cgroup , cgroup_base_files , false);
1562+ revert_creds (cred );
1563+ cgroup_exit_root_id (root );
1564+ out_unlock :
1565+ mutex_unlock (& inode -> i_mutex );
1566+ free_cgrp_cset_links (& tmp_links );
1567+ return ret ;
1568+ }
1569+
14581570static struct dentry * cgroup_mount (struct file_system_type * fs_type ,
14591571 int flags , const char * unused_dev_name ,
14601572 void * data )
14611573{
1462- LIST_HEAD (tmp_links );
14631574 struct super_block * sb = NULL ;
1464- struct inode * inode = NULL ;
14651575 struct cgroupfs_root * root = NULL ;
14661576 struct cgroup_sb_opts opts ;
14671577 struct cgroupfs_root * new_root ;
1468- const struct cred * cred ;
14691578 int ret ;
14701579
14711580 mutex_lock (& cgroup_tree_mutex );
@@ -1502,94 +1611,9 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
15021611 root = sb -> s_fs_info ;
15031612 BUG_ON (!root );
15041613 if (root == opts .new_root ) {
1505- /* We used the new root structure, so this is a new hierarchy */
1506- struct cgroup * root_cgrp = & root -> top_cgroup ;
1507- struct cgroupfs_root * existing_root ;
1508- int i ;
1509- struct css_set * cset ;
1510-
1511- BUG_ON (sb -> s_root != NULL );
1512-
1513- mutex_unlock (& cgroup_mutex );
1514- mutex_unlock (& cgroup_tree_mutex );
1515-
1516- ret = cgroup_get_rootdir (sb );
1614+ ret = cgroup_setup_root (root );
15171615 if (ret )
15181616 goto out_unlock ;
1519- inode = sb -> s_root -> d_inode ;
1520-
1521- mutex_lock (& inode -> i_mutex );
1522- mutex_lock (& cgroup_tree_mutex );
1523- mutex_lock (& cgroup_mutex );
1524-
1525- ret = idr_alloc (& root -> cgroup_idr , root_cgrp , 0 , 1 , GFP_KERNEL );
1526- if (ret < 0 )
1527- goto out_unlock ;
1528- root_cgrp -> id = ret ;
1529-
1530- /* Check for name clashes with existing mounts */
1531- ret = - EBUSY ;
1532- if (strlen (root -> name ))
1533- for_each_active_root (existing_root )
1534- if (!strcmp (existing_root -> name , root -> name ))
1535- goto out_unlock ;
1536-
1537- /*
1538- * We're accessing css_set_count without locking
1539- * css_set_lock here, but that's OK - it can only be
1540- * increased by someone holding cgroup_lock, and
1541- * that's us. The worst that can happen is that we
1542- * have some link structures left over
1543- */
1544- ret = allocate_cgrp_cset_links (css_set_count , & tmp_links );
1545- if (ret )
1546- goto out_unlock ;
1547-
1548- /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1549- ret = cgroup_init_root_id (root , 2 , 0 );
1550- if (ret )
1551- goto out_unlock ;
1552-
1553- sb -> s_root -> d_fsdata = root_cgrp ;
1554- root_cgrp -> dentry = sb -> s_root ;
1555-
1556- /*
1557- * We're inside get_sb() and will call lookup_one_len() to
1558- * create the root files, which doesn't work if SELinux is
1559- * in use. The following cred dancing somehow works around
1560- * it. See 2ce9738ba ("cgroupfs: use init_cred when
1561- * populating new cgroupfs mount") for more details.
1562- */
1563- cred = override_creds (& init_cred );
1564-
1565- ret = cgroup_addrm_files (root_cgrp , cgroup_base_files , true);
1566- if (ret )
1567- goto rm_base_files ;
1568-
1569- ret = rebind_subsystems (root , root -> subsys_mask , 0 );
1570- if (ret )
1571- goto rm_base_files ;
1572-
1573- revert_creds (cred );
1574-
1575- /*
1576- * There must be no failure case after here, since rebinding
1577- * takes care of subsystems' refcounts, which are explicitly
1578- * dropped in the failure exit path.
1579- */
1580-
1581- list_add (& root -> root_list , & cgroup_roots );
1582- cgroup_root_count ++ ;
1583-
1584- /* Link the top cgroup in this hierarchy into all
1585- * the css_set objects */
1586- write_lock (& css_set_lock );
1587- hash_for_each (css_set_table , i , cset , hlist )
1588- link_css_set (& tmp_links , cset , root_cgrp );
1589- write_unlock (& css_set_lock );
1590-
1591- BUG_ON (!list_empty (& root_cgrp -> children ));
1592- BUG_ON (root -> number_of_cgroups != 1 );
15931617 } else {
15941618 /*
15951619 * We re-used an existing hierarchy - the new root (if
@@ -1609,22 +1633,13 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
16091633 }
16101634
16111635 ret = 0 ;
1612- goto out_unlock ;
1613-
1614- rm_base_files :
1615- cgroup_addrm_files (& root -> top_cgroup , cgroup_base_files , false);
1616- revert_creds (cred );
1617- cgroup_exit_root_id (root );
16181636out_unlock :
16191637 mutex_unlock (& cgroup_mutex );
16201638 mutex_unlock (& cgroup_tree_mutex );
1621- if (inode )
1622- mutex_unlock (& inode -> i_mutex );
16231639
16241640 if (ret && !IS_ERR_OR_NULL (sb ))
16251641 deactivate_locked_super (sb );
16261642
1627- free_cgrp_cset_links (& tmp_links );
16281643 kfree (opts .release_agent );
16291644 kfree (opts .name );
16301645
0 commit comments