Skip to content

Commit 8d2ca1d

Browse files
strssndktndavem330
authored andcommitted
ipv6: avoid high order memory allocations for /proc/net/ipv6_route
Dumping routes on a system with lots rt6_infos in the fibs causes up to 11-order allocations in seq_file (which fail). While we could switch there to vmalloc we could just implement the streaming interface for /proc/net/ipv6_route. This patch switches /proc/net/ipv6_route from single_open_net to seq_open_net. loff_t *pos tracks dst entries. Also kill never used struct rt6_proc_arg and now unused function fib6_clean_all_ro. Cc: Ben Greear <greearb@candelatech.com> Cc: Patrick McHardy <kaber@trash.net> Cc: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org> Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 6229ed1 commit 8d2ca1d

3 files changed

Lines changed: 175 additions & 68 deletions

File tree

‎include/net/ip6_fib.h‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,6 @@ struct fib6_node *fib6_locate(struct fib6_node *root,
280280
const struct in6_addr *daddr, int dst_len,
281281
const struct in6_addr *saddr, int src_len);
282282

283-
void fib6_clean_all_ro(struct net *net,
284-
int (*func)(struct rt6_info *, void *arg),
285-
int prune, void *arg);
286-
287283
void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
288284
int prune, void *arg);
289285

@@ -299,6 +295,8 @@ void fib6_gc_cleanup(void);
299295

300296
int fib6_init(void);
301297

298+
int ipv6_route_open(struct inode *inode, struct file *file);
299+
302300
#ifdef CONFIG_IPV6_MULTIPLE_TABLES
303301
int fib6_rules_init(void);
304302
void fib6_rules_cleanup(void);

‎net/ipv6/ip6_fib.c‎

Lines changed: 172 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,25 +1529,6 @@ static void fib6_clean_tree(struct net *net, struct fib6_node *root,
15291529
fib6_walk(&c.w);
15301530
}
15311531

1532-
void fib6_clean_all_ro(struct net *net, int (*func)(struct rt6_info *, void *arg),
1533-
int prune, void *arg)
1534-
{
1535-
struct fib6_table *table;
1536-
struct hlist_head *head;
1537-
unsigned int h;
1538-
1539-
rcu_read_lock();
1540-
for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
1541-
head = &net->ipv6.fib_table_hash[h];
1542-
hlist_for_each_entry_rcu(table, head, tb6_hlist) {
1543-
read_lock_bh(&table->tb6_lock);
1544-
fib6_clean_tree(net, &table->tb6_root,
1545-
func, prune, arg);
1546-
read_unlock_bh(&table->tb6_lock);
1547-
}
1548-
}
1549-
rcu_read_unlock();
1550-
}
15511532
void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
15521533
int prune, void *arg)
15531534
{
@@ -1782,3 +1763,175 @@ void fib6_gc_cleanup(void)
17821763
unregister_pernet_subsys(&fib6_net_ops);
17831764
kmem_cache_destroy(fib6_node_kmem);
17841765
}
1766+
1767+
#ifdef CONFIG_PROC_FS
1768+
1769+
struct ipv6_route_iter {
1770+
struct seq_net_private p;
1771+
struct fib6_walker_t w;
1772+
loff_t skip;
1773+
struct fib6_table *tbl;
1774+
};
1775+
1776+
static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1777+
{
1778+
struct rt6_info *rt = v;
1779+
struct ipv6_route_iter *iter = seq->private;
1780+
1781+
seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1782+
1783+
#ifdef CONFIG_IPV6_SUBTREES
1784+
seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1785+
#else
1786+
seq_puts(seq, "00000000000000000000000000000000 00 ");
1787+
#endif
1788+
if (rt->rt6i_flags & RTF_GATEWAY)
1789+
seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1790+
else
1791+
seq_puts(seq, "00000000000000000000000000000000");
1792+
1793+
seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1794+
rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1795+
rt->dst.__use, rt->rt6i_flags,
1796+
rt->dst.dev ? rt->dst.dev->name : "");
1797+
iter->w.leaf = NULL;
1798+
return 0;
1799+
}
1800+
1801+
static int ipv6_route_yield(struct fib6_walker_t *w)
1802+
{
1803+
struct ipv6_route_iter *iter = w->args;
1804+
1805+
if (!iter->skip)
1806+
return 1;
1807+
1808+
do {
1809+
iter->w.leaf = iter->w.leaf->dst.rt6_next;
1810+
iter->skip--;
1811+
if (!iter->skip && iter->w.leaf)
1812+
return 1;
1813+
} while (iter->w.leaf);
1814+
1815+
return 0;
1816+
}
1817+
1818+
static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1819+
{
1820+
memset(&iter->w, 0, sizeof(iter->w));
1821+
iter->w.func = ipv6_route_yield;
1822+
iter->w.root = &iter->tbl->tb6_root;
1823+
iter->w.state = FWS_INIT;
1824+
iter->w.node = iter->w.root;
1825+
iter->w.args = iter;
1826+
INIT_LIST_HEAD(&iter->w.lh);
1827+
fib6_walker_link(&iter->w);
1828+
}
1829+
1830+
static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1831+
struct net *net)
1832+
{
1833+
unsigned int h;
1834+
struct hlist_node *node;
1835+
1836+
if (tbl) {
1837+
h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1838+
node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1839+
} else {
1840+
h = 0;
1841+
node = NULL;
1842+
}
1843+
1844+
while (!node && h < FIB6_TABLE_HASHSZ) {
1845+
node = rcu_dereference_bh(
1846+
hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1847+
}
1848+
return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1849+
}
1850+
1851+
static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1852+
{
1853+
int r;
1854+
struct rt6_info *n;
1855+
struct net *net = seq_file_net(seq);
1856+
struct ipv6_route_iter *iter = seq->private;
1857+
1858+
if (!v)
1859+
goto iter_table;
1860+
1861+
n = ((struct rt6_info *)v)->dst.rt6_next;
1862+
if (n) {
1863+
++*pos;
1864+
return n;
1865+
}
1866+
1867+
iter_table:
1868+
read_lock(&iter->tbl->tb6_lock);
1869+
r = fib6_walk_continue(&iter->w);
1870+
read_unlock(&iter->tbl->tb6_lock);
1871+
if (r > 0) {
1872+
if (v)
1873+
++*pos;
1874+
return iter->w.leaf;
1875+
} else if (r < 0) {
1876+
fib6_walker_unlink(&iter->w);
1877+
return NULL;
1878+
}
1879+
fib6_walker_unlink(&iter->w);
1880+
1881+
iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
1882+
if (!iter->tbl)
1883+
return NULL;
1884+
1885+
ipv6_route_seq_setup_walk(iter);
1886+
goto iter_table;
1887+
}
1888+
1889+
static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
1890+
__acquires(RCU_BH)
1891+
{
1892+
struct net *net = seq_file_net(seq);
1893+
struct ipv6_route_iter *iter = seq->private;
1894+
1895+
rcu_read_lock_bh();
1896+
iter->tbl = ipv6_route_seq_next_table(NULL, net);
1897+
iter->skip = *pos;
1898+
1899+
if (iter->tbl) {
1900+
ipv6_route_seq_setup_walk(iter);
1901+
return ipv6_route_seq_next(seq, NULL, pos);
1902+
} else {
1903+
return NULL;
1904+
}
1905+
}
1906+
1907+
static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
1908+
{
1909+
struct fib6_walker_t *w = &iter->w;
1910+
return w->node && !(w->state == FWS_U && w->node == w->root);
1911+
}
1912+
1913+
static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
1914+
__releases(RCU_BH)
1915+
{
1916+
struct ipv6_route_iter *iter = seq->private;
1917+
1918+
if (ipv6_route_iter_active(iter))
1919+
fib6_walker_unlink(&iter->w);
1920+
1921+
rcu_read_unlock_bh();
1922+
}
1923+
1924+
static const struct seq_operations ipv6_route_seq_ops = {
1925+
.start = ipv6_route_seq_start,
1926+
.next = ipv6_route_seq_next,
1927+
.stop = ipv6_route_seq_stop,
1928+
.show = ipv6_route_seq_show
1929+
};
1930+
1931+
int ipv6_route_open(struct inode *inode, struct file *file)
1932+
{
1933+
return seq_open_net(inode, file, &ipv6_route_seq_ops,
1934+
sizeof(struct ipv6_route_iter));
1935+
}
1936+
1937+
#endif /* CONFIG_PROC_FS */

‎net/ipv6/route.c‎

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,56 +2800,12 @@ static int ip6_route_dev_notify(struct notifier_block *this,
28002800

28012801
#ifdef CONFIG_PROC_FS
28022802

2803-
struct rt6_proc_arg
2804-
{
2805-
char *buffer;
2806-
int offset;
2807-
int length;
2808-
int skip;
2809-
int len;
2810-
};
2811-
2812-
static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2813-
{
2814-
struct seq_file *m = p_arg;
2815-
2816-
seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2817-
2818-
#ifdef CONFIG_IPV6_SUBTREES
2819-
seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2820-
#else
2821-
seq_puts(m, "00000000000000000000000000000000 00 ");
2822-
#endif
2823-
if (rt->rt6i_flags & RTF_GATEWAY) {
2824-
seq_printf(m, "%pi6", &rt->rt6i_gateway);
2825-
} else {
2826-
seq_puts(m, "00000000000000000000000000000000");
2827-
}
2828-
seq_printf(m, " %08x %08x %08x %08x %8s\n",
2829-
rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2830-
rt->dst.__use, rt->rt6i_flags,
2831-
rt->dst.dev ? rt->dst.dev->name : "");
2832-
return 0;
2833-
}
2834-
2835-
static int ipv6_route_show(struct seq_file *m, void *v)
2836-
{
2837-
struct net *net = (struct net *)m->private;
2838-
fib6_clean_all_ro(net, rt6_info_route, 0, m);
2839-
return 0;
2840-
}
2841-
2842-
static int ipv6_route_open(struct inode *inode, struct file *file)
2843-
{
2844-
return single_open_net(inode, file, ipv6_route_show);
2845-
}
2846-
28472803
static const struct file_operations ipv6_route_proc_fops = {
28482804
.owner = THIS_MODULE,
28492805
.open = ipv6_route_open,
28502806
.read = seq_read,
28512807
.llseek = seq_lseek,
2852-
.release = single_release_net,
2808+
.release = seq_release_net,
28532809
};
28542810

28552811
static int rt6_stats_seq_show(struct seq_file *seq, void *v)

0 commit comments

Comments
 (0)