aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sysctl.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-03-23 21:08:58 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-03-23 21:08:58 -0400
commitf1d38e423a697b7aa06e12d3ca4753bcc1aa3531 (patch)
tree1cbfd86070f724d5ffe53146d4c67edf14cccf98 /kernel/sysctl.c
parentdae430c6f6e5d0b98c238c340a41a39e221e8940 (diff)
parent4e474a00d7ff746ed177ddae14fa8b2d4bad7a00 (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/sysctl
Pull sysctl updates from Eric Biederman: - Rewrite of sysctl for speed and clarity. Insert/remove/Lookup in sysctl are all now O(NlogN) operations, and are no longer bottlenecks in the process of adding and removing network devices. sysctl is now focused on being a filesystem instead of system call and the code can all be found in fs/proc/proc_sysctl.c. Hopefully this means the code is now approachable. Much thanks is owed to Lucian Grinjincu for keeping at this until something was found that was usable. - The recent proc_sys_poll oops found by the fuzzer during hibernation is fixed. * git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/sysctl: (36 commits) sysctl: protect poll() in entries that may go away sysctl: Don't call sysctl_follow_link unless we are a link. sysctl: Comments to make the code clearer. sysctl: Correct error return from get_subdir sysctl: An easier to read version of find_subdir sysctl: fix memset parameters in setup_sysctl_set() sysctl: remove an unused variable sysctl: Add register_sysctl for normal sysctl users sysctl: Index sysctl directories with rbtrees. sysctl: Make the header lists per directory. sysctl: Move sysctl_check_dups into insert_header sysctl: Modify __register_sysctl_paths to take a set instead of a root and an nsproxy sysctl: Replace root_list with links between sysctl_table_sets. sysctl: Add sysctl_print_dir and use it in get_subdir sysctl: Stop requiring explicit management of sysctl directories sysctl: Add a root pointer to ctl_table_set sysctl: Rewrite proc_sys_readdir in terms of first_entry and next_entry sysctl: Rewrite proc_sys_lookup introducing find_entry and lookup_entry. sysctl: Normalize the root_table data structure. sysctl: Factor out insert_header and erase_header ...
Diffstat (limited to 'kernel/sysctl.c')
-rw-r--r--kernel/sysctl.c501
1 files changed, 3 insertions, 498 deletions
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 11d53046b905..d48ff4fd44c3 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -193,20 +193,6 @@ static int sysrq_sysctl_handler(ctl_table *table, int write,
193 193
194#endif 194#endif
195 195
196static struct ctl_table root_table[];
197static struct ctl_table_root sysctl_table_root;
198static struct ctl_table_header root_table_header = {
199 {{.count = 1,
200 .ctl_table = root_table,
201 .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
202 .root = &sysctl_table_root,
203 .set = &sysctl_table_root.default_set,
204};
205static struct ctl_table_root sysctl_table_root = {
206 .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
207 .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
208};
209
210static struct ctl_table kern_table[]; 196static struct ctl_table kern_table[];
211static struct ctl_table vm_table[]; 197static struct ctl_table vm_table[];
212static struct ctl_table fs_table[]; 198static struct ctl_table fs_table[];
@@ -223,7 +209,7 @@ int sysctl_legacy_va_layout;
223 209
224/* The default sysctl tables: */ 210/* The default sysctl tables: */
225 211
226static struct ctl_table root_table[] = { 212static struct ctl_table sysctl_base_table[] = {
227 { 213 {
228 .procname = "kernel", 214 .procname = "kernel",
229 .mode = 0555, 215 .mode = 0555,
@@ -1560,490 +1546,12 @@ static struct ctl_table dev_table[] = {
1560 { } 1546 { }
1561}; 1547};
1562 1548
1563static DEFINE_SPINLOCK(sysctl_lock); 1549int __init sysctl_init(void)
1564
1565/* called under sysctl_lock */
1566static int use_table(struct ctl_table_header *p)
1567{
1568 if (unlikely(p->unregistering))
1569 return 0;
1570 p->used++;
1571 return 1;
1572}
1573
1574/* called under sysctl_lock */
1575static void unuse_table(struct ctl_table_header *p)
1576{
1577 if (!--p->used)
1578 if (unlikely(p->unregistering))
1579 complete(p->unregistering);
1580}
1581
1582/* called under sysctl_lock, will reacquire if has to wait */
1583static void start_unregistering(struct ctl_table_header *p)
1584{
1585 /*
1586 * if p->used is 0, nobody will ever touch that entry again;
1587 * we'll eliminate all paths to it before dropping sysctl_lock
1588 */
1589 if (unlikely(p->used)) {
1590 struct completion wait;
1591 init_completion(&wait);
1592 p->unregistering = &wait;
1593 spin_unlock(&sysctl_lock);
1594 wait_for_completion(&wait);
1595 spin_lock(&sysctl_lock);
1596 } else {
1597 /* anything non-NULL; we'll never dereference it */
1598 p->unregistering = ERR_PTR(-EINVAL);
1599 }
1600 /*
1601 * do not remove from the list until nobody holds it; walking the
1602 * list in do_sysctl() relies on that.
1603 */
1604 list_del_init(&p->ctl_entry);
1605}
1606
1607void sysctl_head_get(struct ctl_table_header *head)
1608{
1609 spin_lock(&sysctl_lock);
1610 head->count++;
1611 spin_unlock(&sysctl_lock);
1612}
1613
1614void sysctl_head_put(struct ctl_table_header *head)
1615{
1616 spin_lock(&sysctl_lock);
1617 if (!--head->count)
1618 kfree_rcu(head, rcu);
1619 spin_unlock(&sysctl_lock);
1620}
1621
1622struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
1623{
1624 if (!head)
1625 BUG();
1626 spin_lock(&sysctl_lock);
1627 if (!use_table(head))
1628 head = ERR_PTR(-ENOENT);
1629 spin_unlock(&sysctl_lock);
1630 return head;
1631}
1632
1633void sysctl_head_finish(struct ctl_table_header *head)
1634{
1635 if (!head)
1636 return;
1637 spin_lock(&sysctl_lock);
1638 unuse_table(head);
1639 spin_unlock(&sysctl_lock);
1640}
1641
1642static struct ctl_table_set *
1643lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
1644{
1645 struct ctl_table_set *set = &root->default_set;
1646 if (root->lookup)
1647 set = root->lookup(root, namespaces);
1648 return set;
1649}
1650
1651static struct list_head *
1652lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
1653{
1654 struct ctl_table_set *set = lookup_header_set(root, namespaces);
1655 return &set->list;
1656}
1657
1658struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
1659 struct ctl_table_header *prev)
1660{
1661 struct ctl_table_root *root;
1662 struct list_head *header_list;
1663 struct ctl_table_header *head;
1664 struct list_head *tmp;
1665
1666 spin_lock(&sysctl_lock);
1667 if (prev) {
1668 head = prev;
1669 tmp = &prev->ctl_entry;
1670 unuse_table(prev);
1671 goto next;
1672 }
1673 tmp = &root_table_header.ctl_entry;
1674 for (;;) {
1675 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
1676
1677 if (!use_table(head))
1678 goto next;
1679 spin_unlock(&sysctl_lock);
1680 return head;
1681 next:
1682 root = head->root;
1683 tmp = tmp->next;
1684 header_list = lookup_header_list(root, namespaces);
1685 if (tmp != header_list)
1686 continue;
1687
1688 do {
1689 root = list_entry(root->root_list.next,
1690 struct ctl_table_root, root_list);
1691 if (root == &sysctl_table_root)
1692 goto out;
1693 header_list = lookup_header_list(root, namespaces);
1694 } while (list_empty(header_list));
1695 tmp = header_list->next;
1696 }
1697out:
1698 spin_unlock(&sysctl_lock);
1699 return NULL;
1700}
1701
1702struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
1703{
1704 return __sysctl_head_next(current->nsproxy, prev);
1705}
1706
1707void register_sysctl_root(struct ctl_table_root *root)
1708{
1709 spin_lock(&sysctl_lock);
1710 list_add_tail(&root->root_list, &sysctl_table_root.root_list);
1711 spin_unlock(&sysctl_lock);
1712}
1713
1714/*
1715 * sysctl_perm does NOT grant the superuser all rights automatically, because
1716 * some sysctl variables are readonly even to root.
1717 */
1718
1719static int test_perm(int mode, int op)
1720{
1721 if (!current_euid())
1722 mode >>= 6;
1723 else if (in_egroup_p(0))
1724 mode >>= 3;
1725 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
1726 return 0;
1727 return -EACCES;
1728}
1729
1730int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
1731{
1732 int mode;
1733
1734 if (root->permissions)
1735 mode = root->permissions(root, current->nsproxy, table);
1736 else
1737 mode = table->mode;
1738
1739 return test_perm(mode, op);
1740}
1741
1742static void sysctl_set_parent(struct ctl_table *parent, struct ctl_table *table)
1743{
1744 for (; table->procname; table++) {
1745 table->parent = parent;
1746 if (table->child)
1747 sysctl_set_parent(table, table->child);
1748 }
1749}
1750
1751static __init int sysctl_init(void)
1752{ 1550{
1753 sysctl_set_parent(NULL, root_table); 1551 register_sysctl_table(sysctl_base_table);
1754#ifdef CONFIG_SYSCTL_SYSCALL_CHECK
1755 sysctl_check_table(current->nsproxy, root_table);
1756#endif
1757 return 0; 1552 return 0;
1758} 1553}
1759 1554
1760core_initcall(sysctl_init);
1761
1762static struct ctl_table *is_branch_in(struct ctl_table *branch,
1763 struct ctl_table *table)
1764{
1765 struct ctl_table *p;
1766 const char *s = branch->procname;
1767
1768 /* branch should have named subdirectory as its first element */
1769 if (!s || !branch->child)
1770 return NULL;
1771
1772 /* ... and nothing else */
1773 if (branch[1].procname)
1774 return NULL;
1775
1776 /* table should contain subdirectory with the same name */
1777 for (p = table; p->procname; p++) {
1778 if (!p->child)
1779 continue;
1780 if (p->procname && strcmp(p->procname, s) == 0)
1781 return p;
1782 }
1783 return NULL;
1784}
1785
1786/* see if attaching q to p would be an improvement */
1787static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
1788{
1789 struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
1790 struct ctl_table *next;
1791 int is_better = 0;
1792 int not_in_parent = !p->attached_by;
1793
1794 while ((next = is_branch_in(by, to)) != NULL) {
1795 if (by == q->attached_by)
1796 is_better = 1;
1797 if (to == p->attached_by)
1798 not_in_parent = 1;
1799 by = by->child;
1800 to = next->child;
1801 }
1802
1803 if (is_better && not_in_parent) {
1804 q->attached_by = by;
1805 q->attached_to = to;
1806 q->parent = p;
1807 }
1808}
1809
1810/**
1811 * __register_sysctl_paths - register a sysctl hierarchy
1812 * @root: List of sysctl headers to register on
1813 * @namespaces: Data to compute which lists of sysctl entries are visible
1814 * @path: The path to the directory the sysctl table is in.
1815 * @table: the top-level table structure
1816 *
1817 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1818 * array. A completely 0 filled entry terminates the table.
1819 *
1820 * The members of the &struct ctl_table structure are used as follows:
1821 *
1822 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1823 * enter a sysctl file
1824 *
1825 * data - a pointer to data for use by proc_handler
1826 *
1827 * maxlen - the maximum size in bytes of the data
1828 *
1829 * mode - the file permissions for the /proc/sys file, and for sysctl(2)
1830 *
1831 * child - a pointer to the child sysctl table if this entry is a directory, or
1832 * %NULL.
1833 *
1834 * proc_handler - the text handler routine (described below)
1835 *
1836 * de - for internal use by the sysctl routines
1837 *
1838 * extra1, extra2 - extra pointers usable by the proc handler routines
1839 *
1840 * Leaf nodes in the sysctl tree will be represented by a single file
1841 * under /proc; non-leaf nodes will be represented by directories.
1842 *
1843 * sysctl(2) can automatically manage read and write requests through
1844 * the sysctl table. The data and maxlen fields of the ctl_table
1845 * struct enable minimal validation of the values being written to be
1846 * performed, and the mode field allows minimal authentication.
1847 *
1848 * There must be a proc_handler routine for any terminal nodes
1849 * mirrored under /proc/sys (non-terminals are handled by a built-in
1850 * directory handler). Several default handlers are available to
1851 * cover common cases -
1852 *
1853 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1854 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1855 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1856 *
1857 * It is the handler's job to read the input buffer from user memory
1858 * and process it. The handler should return 0 on success.
1859 *
1860 * This routine returns %NULL on a failure to register, and a pointer
1861 * to the table header on success.
1862 */
1863struct ctl_table_header *__register_sysctl_paths(
1864 struct ctl_table_root *root,
1865 struct nsproxy *namespaces,
1866 const struct ctl_path *path, struct ctl_table *table)
1867{
1868 struct ctl_table_header *header;
1869 struct ctl_table *new, **prevp;
1870 unsigned int n, npath;
1871 struct ctl_table_set *set;
1872
1873 /* Count the path components */
1874 for (npath = 0; path[npath].procname; ++npath)
1875 ;
1876
1877 /*
1878 * For each path component, allocate a 2-element ctl_table array.
1879 * The first array element will be filled with the sysctl entry
1880 * for this, the second will be the sentinel (procname == 0).
1881 *
1882 * We allocate everything in one go so that we don't have to
1883 * worry about freeing additional memory in unregister_sysctl_table.
1884 */
1885 header = kzalloc(sizeof(struct ctl_table_header) +
1886 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
1887 if (!header)
1888 return NULL;
1889
1890 new = (struct ctl_table *) (header + 1);
1891
1892 /* Now connect the dots */
1893 prevp = &header->ctl_table;
1894 for (n = 0; n < npath; ++n, ++path) {
1895 /* Copy the procname */
1896 new->procname = path->procname;
1897 new->mode = 0555;
1898
1899 *prevp = new;
1900 prevp = &new->child;
1901
1902 new += 2;
1903 }
1904 *prevp = table;
1905 header->ctl_table_arg = table;
1906
1907 INIT_LIST_HEAD(&header->ctl_entry);
1908 header->used = 0;
1909 header->unregistering = NULL;
1910 header->root = root;
1911 sysctl_set_parent(NULL, header->ctl_table);
1912 header->count = 1;
1913#ifdef CONFIG_SYSCTL_SYSCALL_CHECK
1914 if (sysctl_check_table(namespaces, header->ctl_table)) {
1915 kfree(header);
1916 return NULL;
1917 }
1918#endif
1919 spin_lock(&sysctl_lock);
1920 header->set = lookup_header_set(root, namespaces);
1921 header->attached_by = header->ctl_table;
1922 header->attached_to = root_table;
1923 header->parent = &root_table_header;
1924 for (set = header->set; set; set = set->parent) {
1925 struct ctl_table_header *p;
1926 list_for_each_entry(p, &set->list, ctl_entry) {
1927 if (p->unregistering)
1928 continue;
1929 try_attach(p, header);
1930 }
1931 }
1932 header->parent->count++;
1933 list_add_tail(&header->ctl_entry, &header->set->list);
1934 spin_unlock(&sysctl_lock);
1935
1936 return header;
1937}
1938
1939/**
1940 * register_sysctl_table_path - register a sysctl table hierarchy
1941 * @path: The path to the directory the sysctl table is in.
1942 * @table: the top-level table structure
1943 *
1944 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1945 * array. A completely 0 filled entry terminates the table.
1946 *
1947 * See __register_sysctl_paths for more details.
1948 */
1949struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1950 struct ctl_table *table)
1951{
1952 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1953 path, table);
1954}
1955
1956/**
1957 * register_sysctl_table - register a sysctl table hierarchy
1958 * @table: the top-level table structure
1959 *
1960 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1961 * array. A completely 0 filled entry terminates the table.
1962 *
1963 * See register_sysctl_paths for more details.
1964 */
1965struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1966{
1967 static const struct ctl_path null_path[] = { {} };
1968
1969 return register_sysctl_paths(null_path, table);
1970}
1971
1972/**
1973 * unregister_sysctl_table - unregister a sysctl table hierarchy
1974 * @header: the header returned from register_sysctl_table
1975 *
1976 * Unregisters the sysctl table and all children. proc entries may not
1977 * actually be removed until they are no longer used by anyone.
1978 */
1979void unregister_sysctl_table(struct ctl_table_header * header)
1980{
1981 might_sleep();
1982
1983 if (header == NULL)
1984 return;
1985
1986 spin_lock(&sysctl_lock);
1987 start_unregistering(header);
1988 if (!--header->parent->count) {
1989 WARN_ON(1);
1990 kfree_rcu(header->parent, rcu);
1991 }
1992 if (!--header->count)
1993 kfree_rcu(header, rcu);
1994 spin_unlock(&sysctl_lock);
1995}
1996
1997int sysctl_is_seen(struct ctl_table_header *p)
1998{
1999 struct ctl_table_set *set = p->set;
2000 int res;
2001 spin_lock(&sysctl_lock);
2002 if (p->unregistering)
2003 res = 0;
2004 else if (!set->is_seen)
2005 res = 1;
2006 else
2007 res = set->is_seen(set);
2008 spin_unlock(&sysctl_lock);
2009 return res;
2010}
2011
2012void setup_sysctl_set(struct ctl_table_set *p,
2013 struct ctl_table_set *parent,
2014 int (*is_seen)(struct ctl_table_set *))
2015{
2016 INIT_LIST_HEAD(&p->list);
2017 p->parent = parent ? parent : &sysctl_table_root.default_set;
2018 p->is_seen = is_seen;
2019}
2020
2021#else /* !CONFIG_SYSCTL */
2022struct ctl_table_header *register_sysctl_table(struct ctl_table * table)
2023{
2024 return NULL;
2025}
2026
2027struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
2028 struct ctl_table *table)
2029{
2030 return NULL;
2031}
2032
2033void unregister_sysctl_table(struct ctl_table_header * table)
2034{
2035}
2036
2037void setup_sysctl_set(struct ctl_table_set *p,
2038 struct ctl_table_set *parent,
2039 int (*is_seen)(struct ctl_table_set *))
2040{
2041}
2042
2043void sysctl_head_put(struct ctl_table_header *head)
2044{
2045}
2046
2047#endif /* CONFIG_SYSCTL */ 1555#endif /* CONFIG_SYSCTL */
2048 1556
2049/* 1557/*
@@ -3009,6 +2517,3 @@ EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
3009EXPORT_SYMBOL(proc_dostring); 2517EXPORT_SYMBOL(proc_dostring);
3010EXPORT_SYMBOL(proc_doulongvec_minmax); 2518EXPORT_SYMBOL(proc_doulongvec_minmax);
3011EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax); 2519EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
3012EXPORT_SYMBOL(register_sysctl_table);
3013EXPORT_SYMBOL(register_sysctl_paths);
3014EXPORT_SYMBOL(unregister_sysctl_table);