fix a kmap leak in virtio_console
[linux/fpc-iii.git] / net / core / sysctl_net_core.c
blobcf9cd13509a7d531f9e4a455d2909f5dfb731923
1 /* -*- linux-c -*-
2 * sysctl_net_core.c: sysctl interface to net core subsystem.
4 * Begun April 1, 1996, Mike Shaver.
5 * Added /proc/sys/net/core directory entry (empty =) ). [MS]
6 */
8 #include <linux/mm.h>
9 #include <linux/sysctl.h>
10 #include <linux/module.h>
11 #include <linux/socket.h>
12 #include <linux/netdevice.h>
13 #include <linux/ratelimit.h>
14 #include <linux/vmalloc.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/kmemleak.h>
19 #include <net/ip.h>
20 #include <net/sock.h>
21 #include <net/net_ratelimit.h>
22 #include <net/busy_poll.h>
23 #include <net/pkt_sched.h>
25 static int zero = 0;
26 static int one = 1;
27 static int ushort_max = USHRT_MAX;
29 #ifdef CONFIG_RPS
30 static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
31 void __user *buffer, size_t *lenp, loff_t *ppos)
33 unsigned int orig_size, size;
34 int ret, i;
35 struct ctl_table tmp = {
36 .data = &size,
37 .maxlen = sizeof(size),
38 .mode = table->mode
40 struct rps_sock_flow_table *orig_sock_table, *sock_table;
41 static DEFINE_MUTEX(sock_flow_mutex);
43 mutex_lock(&sock_flow_mutex);
45 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
46 lockdep_is_held(&sock_flow_mutex));
47 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
49 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
51 if (write) {
52 if (size) {
53 if (size > 1<<30) {
54 /* Enforce limit to prevent overflow */
55 mutex_unlock(&sock_flow_mutex);
56 return -EINVAL;
58 size = roundup_pow_of_two(size);
59 if (size != orig_size) {
60 sock_table =
61 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
62 if (!sock_table) {
63 mutex_unlock(&sock_flow_mutex);
64 return -ENOMEM;
67 sock_table->mask = size - 1;
68 } else
69 sock_table = orig_sock_table;
71 for (i = 0; i < size; i++)
72 sock_table->ents[i] = RPS_NO_CPU;
73 } else
74 sock_table = NULL;
76 if (sock_table != orig_sock_table) {
77 rcu_assign_pointer(rps_sock_flow_table, sock_table);
78 if (sock_table)
79 static_key_slow_inc(&rps_needed);
80 if (orig_sock_table) {
81 static_key_slow_dec(&rps_needed);
82 synchronize_rcu();
83 vfree(orig_sock_table);
88 mutex_unlock(&sock_flow_mutex);
90 return ret;
92 #endif /* CONFIG_RPS */
94 #ifdef CONFIG_NET_FLOW_LIMIT
95 static DEFINE_MUTEX(flow_limit_update_mutex);
97 static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
98 void __user *buffer, size_t *lenp,
99 loff_t *ppos)
101 struct sd_flow_limit *cur;
102 struct softnet_data *sd;
103 cpumask_var_t mask;
104 int i, len, ret = 0;
106 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
107 return -ENOMEM;
109 if (write) {
110 ret = cpumask_parse_user(buffer, *lenp, mask);
111 if (ret)
112 goto done;
114 mutex_lock(&flow_limit_update_mutex);
115 len = sizeof(*cur) + netdev_flow_limit_table_len;
116 for_each_possible_cpu(i) {
117 sd = &per_cpu(softnet_data, i);
118 cur = rcu_dereference_protected(sd->flow_limit,
119 lockdep_is_held(&flow_limit_update_mutex));
120 if (cur && !cpumask_test_cpu(i, mask)) {
121 RCU_INIT_POINTER(sd->flow_limit, NULL);
122 synchronize_rcu();
123 kfree(cur);
124 } else if (!cur && cpumask_test_cpu(i, mask)) {
125 cur = kzalloc_node(len, GFP_KERNEL,
126 cpu_to_node(i));
127 if (!cur) {
128 /* not unwinding previous changes */
129 ret = -ENOMEM;
130 goto write_unlock;
132 cur->num_buckets = netdev_flow_limit_table_len;
133 rcu_assign_pointer(sd->flow_limit, cur);
136 write_unlock:
137 mutex_unlock(&flow_limit_update_mutex);
138 } else {
139 char kbuf[128];
141 if (*ppos || !*lenp) {
142 *lenp = 0;
143 goto done;
146 cpumask_clear(mask);
147 rcu_read_lock();
148 for_each_possible_cpu(i) {
149 sd = &per_cpu(softnet_data, i);
150 if (rcu_dereference(sd->flow_limit))
151 cpumask_set_cpu(i, mask);
153 rcu_read_unlock();
155 len = min(sizeof(kbuf) - 1, *lenp);
156 len = cpumask_scnprintf(kbuf, len, mask);
157 if (!len) {
158 *lenp = 0;
159 goto done;
161 if (len < *lenp)
162 kbuf[len++] = '\n';
163 if (copy_to_user(buffer, kbuf, len)) {
164 ret = -EFAULT;
165 goto done;
167 *lenp = len;
168 *ppos += len;
171 done:
172 free_cpumask_var(mask);
173 return ret;
176 static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
177 void __user *buffer, size_t *lenp,
178 loff_t *ppos)
180 unsigned int old, *ptr;
181 int ret;
183 mutex_lock(&flow_limit_update_mutex);
185 ptr = table->data;
186 old = *ptr;
187 ret = proc_dointvec(table, write, buffer, lenp, ppos);
188 if (!ret && write && !is_power_of_2(*ptr)) {
189 *ptr = old;
190 ret = -EINVAL;
193 mutex_unlock(&flow_limit_update_mutex);
194 return ret;
196 #endif /* CONFIG_NET_FLOW_LIMIT */
198 #ifdef CONFIG_NET_SCHED
199 static int set_default_qdisc(struct ctl_table *table, int write,
200 void __user *buffer, size_t *lenp, loff_t *ppos)
202 char id[IFNAMSIZ];
203 struct ctl_table tbl = {
204 .data = id,
205 .maxlen = IFNAMSIZ,
207 int ret;
209 qdisc_get_default(id, IFNAMSIZ);
211 ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
212 if (write && ret == 0)
213 ret = qdisc_set_default(id);
214 return ret;
216 #endif
218 static struct ctl_table net_core_table[] = {
219 #ifdef CONFIG_NET
221 .procname = "wmem_max",
222 .data = &sysctl_wmem_max,
223 .maxlen = sizeof(int),
224 .mode = 0644,
225 .proc_handler = proc_dointvec_minmax,
226 .extra1 = &one,
229 .procname = "rmem_max",
230 .data = &sysctl_rmem_max,
231 .maxlen = sizeof(int),
232 .mode = 0644,
233 .proc_handler = proc_dointvec_minmax,
234 .extra1 = &one,
237 .procname = "wmem_default",
238 .data = &sysctl_wmem_default,
239 .maxlen = sizeof(int),
240 .mode = 0644,
241 .proc_handler = proc_dointvec_minmax,
242 .extra1 = &one,
245 .procname = "rmem_default",
246 .data = &sysctl_rmem_default,
247 .maxlen = sizeof(int),
248 .mode = 0644,
249 .proc_handler = proc_dointvec_minmax,
250 .extra1 = &one,
253 .procname = "dev_weight",
254 .data = &weight_p,
255 .maxlen = sizeof(int),
256 .mode = 0644,
257 .proc_handler = proc_dointvec
260 .procname = "netdev_max_backlog",
261 .data = &netdev_max_backlog,
262 .maxlen = sizeof(int),
263 .mode = 0644,
264 .proc_handler = proc_dointvec
266 #ifdef CONFIG_BPF_JIT
268 .procname = "bpf_jit_enable",
269 .data = &bpf_jit_enable,
270 .maxlen = sizeof(int),
271 .mode = 0644,
272 .proc_handler = proc_dointvec
274 #endif
276 .procname = "netdev_tstamp_prequeue",
277 .data = &netdev_tstamp_prequeue,
278 .maxlen = sizeof(int),
279 .mode = 0644,
280 .proc_handler = proc_dointvec
283 .procname = "message_cost",
284 .data = &net_ratelimit_state.interval,
285 .maxlen = sizeof(int),
286 .mode = 0644,
287 .proc_handler = proc_dointvec_jiffies,
290 .procname = "message_burst",
291 .data = &net_ratelimit_state.burst,
292 .maxlen = sizeof(int),
293 .mode = 0644,
294 .proc_handler = proc_dointvec,
297 .procname = "optmem_max",
298 .data = &sysctl_optmem_max,
299 .maxlen = sizeof(int),
300 .mode = 0644,
301 .proc_handler = proc_dointvec
303 #ifdef CONFIG_RPS
305 .procname = "rps_sock_flow_entries",
306 .maxlen = sizeof(int),
307 .mode = 0644,
308 .proc_handler = rps_sock_flow_sysctl
310 #endif
311 #ifdef CONFIG_NET_FLOW_LIMIT
313 .procname = "flow_limit_cpu_bitmap",
314 .mode = 0644,
315 .proc_handler = flow_limit_cpu_sysctl
318 .procname = "flow_limit_table_len",
319 .data = &netdev_flow_limit_table_len,
320 .maxlen = sizeof(int),
321 .mode = 0644,
322 .proc_handler = flow_limit_table_len_sysctl
324 #endif /* CONFIG_NET_FLOW_LIMIT */
325 #ifdef CONFIG_NET_RX_BUSY_POLL
327 .procname = "busy_poll",
328 .data = &sysctl_net_busy_poll,
329 .maxlen = sizeof(unsigned int),
330 .mode = 0644,
331 .proc_handler = proc_dointvec
334 .procname = "busy_read",
335 .data = &sysctl_net_busy_read,
336 .maxlen = sizeof(unsigned int),
337 .mode = 0644,
338 .proc_handler = proc_dointvec
340 #endif
341 #ifdef CONFIG_NET_SCHED
343 .procname = "default_qdisc",
344 .mode = 0644,
345 .maxlen = IFNAMSIZ,
346 .proc_handler = set_default_qdisc
348 #endif
349 #endif /* CONFIG_NET */
351 .procname = "netdev_budget",
352 .data = &netdev_budget,
353 .maxlen = sizeof(int),
354 .mode = 0644,
355 .proc_handler = proc_dointvec
358 .procname = "warnings",
359 .data = &net_msg_warn,
360 .maxlen = sizeof(int),
361 .mode = 0644,
362 .proc_handler = proc_dointvec
367 static struct ctl_table netns_core_table[] = {
369 .procname = "somaxconn",
370 .data = &init_net.core.sysctl_somaxconn,
371 .maxlen = sizeof(int),
372 .mode = 0644,
373 .extra1 = &zero,
374 .extra2 = &ushort_max,
375 .proc_handler = proc_dointvec_minmax
380 static __net_init int sysctl_core_net_init(struct net *net)
382 struct ctl_table *tbl;
384 net->core.sysctl_somaxconn = SOMAXCONN;
386 tbl = netns_core_table;
387 if (!net_eq(net, &init_net)) {
388 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
389 if (tbl == NULL)
390 goto err_dup;
392 tbl[0].data = &net->core.sysctl_somaxconn;
394 /* Don't export any sysctls to unprivileged users */
395 if (net->user_ns != &init_user_ns) {
396 tbl[0].procname = NULL;
400 net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
401 if (net->core.sysctl_hdr == NULL)
402 goto err_reg;
404 return 0;
406 err_reg:
407 if (tbl != netns_core_table)
408 kfree(tbl);
409 err_dup:
410 return -ENOMEM;
413 static __net_exit void sysctl_core_net_exit(struct net *net)
415 struct ctl_table *tbl;
417 tbl = net->core.sysctl_hdr->ctl_table_arg;
418 unregister_net_sysctl_table(net->core.sysctl_hdr);
419 BUG_ON(tbl == netns_core_table);
420 kfree(tbl);
423 static __net_initdata struct pernet_operations sysctl_core_ops = {
424 .init = sysctl_core_net_init,
425 .exit = sysctl_core_net_exit,
428 static __init int sysctl_core_init(void)
430 register_net_sysctl(&init_net, "net/core", net_core_table);
431 return register_pernet_subsys(&sysctl_core_ops);
434 fs_initcall(sysctl_core_init);