Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / net / core / sysctl_net_core.c
blobf2d0462611c35b16aaca6d060e78e5e5af9aa2a4
1 // SPDX-License-Identifier: GPL-2.0
2 /* -*- linux-c -*-
3 * sysctl_net_core.c: sysctl interface to net core subsystem.
5 * Begun April 1, 1996, Mike Shaver.
6 * Added /proc/sys/net/core directory entry (empty =) ). [MS]
7 */
9 #include <linux/mm.h>
10 #include <linux/sysctl.h>
11 #include <linux/module.h>
12 #include <linux/socket.h>
13 #include <linux/netdevice.h>
14 #include <linux/ratelimit.h>
15 #include <linux/vmalloc.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/kmemleak.h>
20 #include <net/ip.h>
21 #include <net/sock.h>
22 #include <net/net_ratelimit.h>
23 #include <net/busy_poll.h>
24 #include <net/pkt_sched.h>
26 static int zero = 0;
27 static int one = 1;
28 static int two __maybe_unused = 2;
29 static int min_sndbuf = SOCK_MIN_SNDBUF;
30 static int min_rcvbuf = SOCK_MIN_RCVBUF;
31 static int max_skb_frags = MAX_SKB_FRAGS;
33 static int net_msg_warn; /* Unused, but still a sysctl */
35 #ifdef CONFIG_RPS
36 static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
37 void __user *buffer, size_t *lenp, loff_t *ppos)
39 unsigned int orig_size, size;
40 int ret, i;
41 struct ctl_table tmp = {
42 .data = &size,
43 .maxlen = sizeof(size),
44 .mode = table->mode
46 struct rps_sock_flow_table *orig_sock_table, *sock_table;
47 static DEFINE_MUTEX(sock_flow_mutex);
49 mutex_lock(&sock_flow_mutex);
51 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
52 lockdep_is_held(&sock_flow_mutex));
53 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
55 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
57 if (write) {
58 if (size) {
59 if (size > 1<<29) {
60 /* Enforce limit to prevent overflow */
61 mutex_unlock(&sock_flow_mutex);
62 return -EINVAL;
64 size = roundup_pow_of_two(size);
65 if (size != orig_size) {
66 sock_table =
67 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
68 if (!sock_table) {
69 mutex_unlock(&sock_flow_mutex);
70 return -ENOMEM;
72 rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1;
73 sock_table->mask = size - 1;
74 } else
75 sock_table = orig_sock_table;
77 for (i = 0; i < size; i++)
78 sock_table->ents[i] = RPS_NO_CPU;
79 } else
80 sock_table = NULL;
82 if (sock_table != orig_sock_table) {
83 rcu_assign_pointer(rps_sock_flow_table, sock_table);
84 if (sock_table) {
85 static_key_slow_inc(&rps_needed);
86 static_key_slow_inc(&rfs_needed);
88 if (orig_sock_table) {
89 static_key_slow_dec(&rps_needed);
90 static_key_slow_dec(&rfs_needed);
91 synchronize_rcu();
92 vfree(orig_sock_table);
97 mutex_unlock(&sock_flow_mutex);
99 return ret;
101 #endif /* CONFIG_RPS */
103 #ifdef CONFIG_NET_FLOW_LIMIT
104 static DEFINE_MUTEX(flow_limit_update_mutex);
106 static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
107 void __user *buffer, size_t *lenp,
108 loff_t *ppos)
110 struct sd_flow_limit *cur;
111 struct softnet_data *sd;
112 cpumask_var_t mask;
113 int i, len, ret = 0;
115 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
116 return -ENOMEM;
118 if (write) {
119 ret = cpumask_parse_user(buffer, *lenp, mask);
120 if (ret)
121 goto done;
123 mutex_lock(&flow_limit_update_mutex);
124 len = sizeof(*cur) + netdev_flow_limit_table_len;
125 for_each_possible_cpu(i) {
126 sd = &per_cpu(softnet_data, i);
127 cur = rcu_dereference_protected(sd->flow_limit,
128 lockdep_is_held(&flow_limit_update_mutex));
129 if (cur && !cpumask_test_cpu(i, mask)) {
130 RCU_INIT_POINTER(sd->flow_limit, NULL);
131 synchronize_rcu();
132 kfree(cur);
133 } else if (!cur && cpumask_test_cpu(i, mask)) {
134 cur = kzalloc_node(len, GFP_KERNEL,
135 cpu_to_node(i));
136 if (!cur) {
137 /* not unwinding previous changes */
138 ret = -ENOMEM;
139 goto write_unlock;
141 cur->num_buckets = netdev_flow_limit_table_len;
142 rcu_assign_pointer(sd->flow_limit, cur);
145 write_unlock:
146 mutex_unlock(&flow_limit_update_mutex);
147 } else {
148 char kbuf[128];
150 if (*ppos || !*lenp) {
151 *lenp = 0;
152 goto done;
155 cpumask_clear(mask);
156 rcu_read_lock();
157 for_each_possible_cpu(i) {
158 sd = &per_cpu(softnet_data, i);
159 if (rcu_dereference(sd->flow_limit))
160 cpumask_set_cpu(i, mask);
162 rcu_read_unlock();
164 len = min(sizeof(kbuf) - 1, *lenp);
165 len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask));
166 if (!len) {
167 *lenp = 0;
168 goto done;
170 if (len < *lenp)
171 kbuf[len++] = '\n';
172 if (copy_to_user(buffer, kbuf, len)) {
173 ret = -EFAULT;
174 goto done;
176 *lenp = len;
177 *ppos += len;
180 done:
181 free_cpumask_var(mask);
182 return ret;
185 static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
186 void __user *buffer, size_t *lenp,
187 loff_t *ppos)
189 unsigned int old, *ptr;
190 int ret;
192 mutex_lock(&flow_limit_update_mutex);
194 ptr = table->data;
195 old = *ptr;
196 ret = proc_dointvec(table, write, buffer, lenp, ppos);
197 if (!ret && write && !is_power_of_2(*ptr)) {
198 *ptr = old;
199 ret = -EINVAL;
202 mutex_unlock(&flow_limit_update_mutex);
203 return ret;
205 #endif /* CONFIG_NET_FLOW_LIMIT */
207 #ifdef CONFIG_NET_SCHED
208 static int set_default_qdisc(struct ctl_table *table, int write,
209 void __user *buffer, size_t *lenp, loff_t *ppos)
211 char id[IFNAMSIZ];
212 struct ctl_table tbl = {
213 .data = id,
214 .maxlen = IFNAMSIZ,
216 int ret;
218 qdisc_get_default(id, IFNAMSIZ);
220 ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
221 if (write && ret == 0)
222 ret = qdisc_set_default(id);
223 return ret;
225 #endif
227 static int proc_do_dev_weight(struct ctl_table *table, int write,
228 void __user *buffer, size_t *lenp, loff_t *ppos)
230 int ret;
232 ret = proc_dointvec(table, write, buffer, lenp, ppos);
233 if (ret != 0)
234 return ret;
236 dev_rx_weight = weight_p * dev_weight_rx_bias;
237 dev_tx_weight = weight_p * dev_weight_tx_bias;
239 return ret;
242 static int proc_do_rss_key(struct ctl_table *table, int write,
243 void __user *buffer, size_t *lenp, loff_t *ppos)
245 struct ctl_table fake_table;
246 char buf[NETDEV_RSS_KEY_LEN * 3];
248 snprintf(buf, sizeof(buf), "%*phC", NETDEV_RSS_KEY_LEN, netdev_rss_key);
249 fake_table.data = buf;
250 fake_table.maxlen = sizeof(buf);
251 return proc_dostring(&fake_table, write, buffer, lenp, ppos);
254 #ifdef CONFIG_BPF_JIT
255 static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write,
256 void __user *buffer, size_t *lenp,
257 loff_t *ppos)
259 int ret, jit_enable = *(int *)table->data;
260 struct ctl_table tmp = *table;
262 if (write && !capable(CAP_SYS_ADMIN))
263 return -EPERM;
265 tmp.data = &jit_enable;
266 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
267 if (write && !ret) {
268 if (jit_enable < 2 ||
269 (jit_enable == 2 && bpf_dump_raw_ok())) {
270 *(int *)table->data = jit_enable;
271 if (jit_enable == 2)
272 pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n");
273 } else {
274 ret = -EPERM;
277 return ret;
280 # ifdef CONFIG_HAVE_EBPF_JIT
281 static int
282 proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write,
283 void __user *buffer, size_t *lenp,
284 loff_t *ppos)
286 if (!capable(CAP_SYS_ADMIN))
287 return -EPERM;
289 return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
291 # endif
292 #endif
294 static struct ctl_table net_core_table[] = {
295 #ifdef CONFIG_NET
297 .procname = "wmem_max",
298 .data = &sysctl_wmem_max,
299 .maxlen = sizeof(int),
300 .mode = 0644,
301 .proc_handler = proc_dointvec_minmax,
302 .extra1 = &min_sndbuf,
305 .procname = "rmem_max",
306 .data = &sysctl_rmem_max,
307 .maxlen = sizeof(int),
308 .mode = 0644,
309 .proc_handler = proc_dointvec_minmax,
310 .extra1 = &min_rcvbuf,
313 .procname = "wmem_default",
314 .data = &sysctl_wmem_default,
315 .maxlen = sizeof(int),
316 .mode = 0644,
317 .proc_handler = proc_dointvec_minmax,
318 .extra1 = &min_sndbuf,
321 .procname = "rmem_default",
322 .data = &sysctl_rmem_default,
323 .maxlen = sizeof(int),
324 .mode = 0644,
325 .proc_handler = proc_dointvec_minmax,
326 .extra1 = &min_rcvbuf,
329 .procname = "dev_weight",
330 .data = &weight_p,
331 .maxlen = sizeof(int),
332 .mode = 0644,
333 .proc_handler = proc_do_dev_weight,
336 .procname = "dev_weight_rx_bias",
337 .data = &dev_weight_rx_bias,
338 .maxlen = sizeof(int),
339 .mode = 0644,
340 .proc_handler = proc_do_dev_weight,
343 .procname = "dev_weight_tx_bias",
344 .data = &dev_weight_tx_bias,
345 .maxlen = sizeof(int),
346 .mode = 0644,
347 .proc_handler = proc_do_dev_weight,
350 .procname = "netdev_max_backlog",
351 .data = &netdev_max_backlog,
352 .maxlen = sizeof(int),
353 .mode = 0644,
354 .proc_handler = proc_dointvec
357 .procname = "netdev_rss_key",
358 .data = &netdev_rss_key,
359 .maxlen = sizeof(int),
360 .mode = 0444,
361 .proc_handler = proc_do_rss_key,
363 #ifdef CONFIG_BPF_JIT
365 .procname = "bpf_jit_enable",
366 .data = &bpf_jit_enable,
367 .maxlen = sizeof(int),
368 .mode = 0644,
369 .proc_handler = proc_dointvec_minmax_bpf_enable,
370 # ifdef CONFIG_BPF_JIT_ALWAYS_ON
371 .extra1 = &one,
372 .extra2 = &one,
373 # else
374 .extra1 = &zero,
375 .extra2 = &two,
376 # endif
378 # ifdef CONFIG_HAVE_EBPF_JIT
380 .procname = "bpf_jit_harden",
381 .data = &bpf_jit_harden,
382 .maxlen = sizeof(int),
383 .mode = 0600,
384 .proc_handler = proc_dointvec_minmax_bpf_restricted,
385 .extra1 = &zero,
386 .extra2 = &two,
389 .procname = "bpf_jit_kallsyms",
390 .data = &bpf_jit_kallsyms,
391 .maxlen = sizeof(int),
392 .mode = 0600,
393 .proc_handler = proc_dointvec_minmax_bpf_restricted,
394 .extra1 = &zero,
395 .extra2 = &one,
397 # endif
398 #endif
400 .procname = "netdev_tstamp_prequeue",
401 .data = &netdev_tstamp_prequeue,
402 .maxlen = sizeof(int),
403 .mode = 0644,
404 .proc_handler = proc_dointvec
407 .procname = "message_cost",
408 .data = &net_ratelimit_state.interval,
409 .maxlen = sizeof(int),
410 .mode = 0644,
411 .proc_handler = proc_dointvec_jiffies,
414 .procname = "message_burst",
415 .data = &net_ratelimit_state.burst,
416 .maxlen = sizeof(int),
417 .mode = 0644,
418 .proc_handler = proc_dointvec,
421 .procname = "optmem_max",
422 .data = &sysctl_optmem_max,
423 .maxlen = sizeof(int),
424 .mode = 0644,
425 .proc_handler = proc_dointvec
428 .procname = "tstamp_allow_data",
429 .data = &sysctl_tstamp_allow_data,
430 .maxlen = sizeof(int),
431 .mode = 0644,
432 .proc_handler = proc_dointvec_minmax,
433 .extra1 = &zero,
434 .extra2 = &one
436 #ifdef CONFIG_RPS
438 .procname = "rps_sock_flow_entries",
439 .maxlen = sizeof(int),
440 .mode = 0644,
441 .proc_handler = rps_sock_flow_sysctl
443 #endif
444 #ifdef CONFIG_NET_FLOW_LIMIT
446 .procname = "flow_limit_cpu_bitmap",
447 .mode = 0644,
448 .proc_handler = flow_limit_cpu_sysctl
451 .procname = "flow_limit_table_len",
452 .data = &netdev_flow_limit_table_len,
453 .maxlen = sizeof(int),
454 .mode = 0644,
455 .proc_handler = flow_limit_table_len_sysctl
457 #endif /* CONFIG_NET_FLOW_LIMIT */
458 #ifdef CONFIG_NET_RX_BUSY_POLL
460 .procname = "busy_poll",
461 .data = &sysctl_net_busy_poll,
462 .maxlen = sizeof(unsigned int),
463 .mode = 0644,
464 .proc_handler = proc_dointvec_minmax,
465 .extra1 = &zero,
468 .procname = "busy_read",
469 .data = &sysctl_net_busy_read,
470 .maxlen = sizeof(unsigned int),
471 .mode = 0644,
472 .proc_handler = proc_dointvec_minmax,
473 .extra1 = &zero,
475 #endif
476 #ifdef CONFIG_NET_SCHED
478 .procname = "default_qdisc",
479 .mode = 0644,
480 .maxlen = IFNAMSIZ,
481 .proc_handler = set_default_qdisc
483 #endif
484 #endif /* CONFIG_NET */
486 .procname = "netdev_budget",
487 .data = &netdev_budget,
488 .maxlen = sizeof(int),
489 .mode = 0644,
490 .proc_handler = proc_dointvec
493 .procname = "warnings",
494 .data = &net_msg_warn,
495 .maxlen = sizeof(int),
496 .mode = 0644,
497 .proc_handler = proc_dointvec
500 .procname = "max_skb_frags",
501 .data = &sysctl_max_skb_frags,
502 .maxlen = sizeof(int),
503 .mode = 0644,
504 .proc_handler = proc_dointvec_minmax,
505 .extra1 = &one,
506 .extra2 = &max_skb_frags,
509 .procname = "netdev_budget_usecs",
510 .data = &netdev_budget_usecs,
511 .maxlen = sizeof(unsigned int),
512 .mode = 0644,
513 .proc_handler = proc_dointvec_minmax,
514 .extra1 = &zero,
519 static struct ctl_table netns_core_table[] = {
521 .procname = "somaxconn",
522 .data = &init_net.core.sysctl_somaxconn,
523 .maxlen = sizeof(int),
524 .mode = 0644,
525 .extra1 = &zero,
526 .proc_handler = proc_dointvec_minmax
531 static __net_init int sysctl_core_net_init(struct net *net)
533 struct ctl_table *tbl;
535 tbl = netns_core_table;
536 if (!net_eq(net, &init_net)) {
537 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
538 if (tbl == NULL)
539 goto err_dup;
541 tbl[0].data = &net->core.sysctl_somaxconn;
543 /* Don't export any sysctls to unprivileged users */
544 if (net->user_ns != &init_user_ns) {
545 tbl[0].procname = NULL;
549 net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
550 if (net->core.sysctl_hdr == NULL)
551 goto err_reg;
553 return 0;
555 err_reg:
556 if (tbl != netns_core_table)
557 kfree(tbl);
558 err_dup:
559 return -ENOMEM;
562 static __net_exit void sysctl_core_net_exit(struct net *net)
564 struct ctl_table *tbl;
566 tbl = net->core.sysctl_hdr->ctl_table_arg;
567 unregister_net_sysctl_table(net->core.sysctl_hdr);
568 BUG_ON(tbl == netns_core_table);
569 kfree(tbl);
572 static __net_initdata struct pernet_operations sysctl_core_ops = {
573 .init = sysctl_core_net_init,
574 .exit = sysctl_core_net_exit,
577 static __init int sysctl_core_init(void)
579 register_net_sysctl(&init_net, "net/core", net_core_table);
580 return register_pernet_subsys(&sysctl_core_ops);
583 fs_initcall(sysctl_core_init);