Btrfs: fix list transaction->pending_ordered corruption
[linux/fpc-iii.git] / net / core / sysctl_net_core.c
blobe731c96eac4b44428f5efa38a094234deeceeeb7
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;
28 static int min_sndbuf = SOCK_MIN_SNDBUF;
29 static int min_rcvbuf = SOCK_MIN_RCVBUF;
31 #ifdef CONFIG_RPS
32 static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
33 void __user *buffer, size_t *lenp, loff_t *ppos)
35 unsigned int orig_size, size;
36 int ret, i;
37 struct ctl_table tmp = {
38 .data = &size,
39 .maxlen = sizeof(size),
40 .mode = table->mode
42 struct rps_sock_flow_table *orig_sock_table, *sock_table;
43 static DEFINE_MUTEX(sock_flow_mutex);
45 mutex_lock(&sock_flow_mutex);
47 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
48 lockdep_is_held(&sock_flow_mutex));
49 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
51 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
53 if (write) {
54 if (size) {
55 if (size > 1<<30) {
56 /* Enforce limit to prevent overflow */
57 mutex_unlock(&sock_flow_mutex);
58 return -EINVAL;
60 size = roundup_pow_of_two(size);
61 if (size != orig_size) {
62 sock_table =
63 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
64 if (!sock_table) {
65 mutex_unlock(&sock_flow_mutex);
66 return -ENOMEM;
69 sock_table->mask = size - 1;
70 } else
71 sock_table = orig_sock_table;
73 for (i = 0; i < size; i++)
74 sock_table->ents[i] = RPS_NO_CPU;
75 } else
76 sock_table = NULL;
78 if (sock_table != orig_sock_table) {
79 rcu_assign_pointer(rps_sock_flow_table, sock_table);
80 if (sock_table)
81 static_key_slow_inc(&rps_needed);
82 if (orig_sock_table) {
83 static_key_slow_dec(&rps_needed);
84 synchronize_rcu();
85 vfree(orig_sock_table);
90 mutex_unlock(&sock_flow_mutex);
92 return ret;
94 #endif /* CONFIG_RPS */
96 #ifdef CONFIG_NET_FLOW_LIMIT
97 static DEFINE_MUTEX(flow_limit_update_mutex);
99 static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
100 void __user *buffer, size_t *lenp,
101 loff_t *ppos)
103 struct sd_flow_limit *cur;
104 struct softnet_data *sd;
105 cpumask_var_t mask;
106 int i, len, ret = 0;
108 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
109 return -ENOMEM;
111 if (write) {
112 ret = cpumask_parse_user(buffer, *lenp, mask);
113 if (ret)
114 goto done;
116 mutex_lock(&flow_limit_update_mutex);
117 len = sizeof(*cur) + netdev_flow_limit_table_len;
118 for_each_possible_cpu(i) {
119 sd = &per_cpu(softnet_data, i);
120 cur = rcu_dereference_protected(sd->flow_limit,
121 lockdep_is_held(&flow_limit_update_mutex));
122 if (cur && !cpumask_test_cpu(i, mask)) {
123 RCU_INIT_POINTER(sd->flow_limit, NULL);
124 synchronize_rcu();
125 kfree(cur);
126 } else if (!cur && cpumask_test_cpu(i, mask)) {
127 cur = kzalloc_node(len, GFP_KERNEL,
128 cpu_to_node(i));
129 if (!cur) {
130 /* not unwinding previous changes */
131 ret = -ENOMEM;
132 goto write_unlock;
134 cur->num_buckets = netdev_flow_limit_table_len;
135 rcu_assign_pointer(sd->flow_limit, cur);
138 write_unlock:
139 mutex_unlock(&flow_limit_update_mutex);
140 } else {
141 char kbuf[128];
143 if (*ppos || !*lenp) {
144 *lenp = 0;
145 goto done;
148 cpumask_clear(mask);
149 rcu_read_lock();
150 for_each_possible_cpu(i) {
151 sd = &per_cpu(softnet_data, i);
152 if (rcu_dereference(sd->flow_limit))
153 cpumask_set_cpu(i, mask);
155 rcu_read_unlock();
157 len = min(sizeof(kbuf) - 1, *lenp);
158 len = cpumask_scnprintf(kbuf, len, mask);
159 if (!len) {
160 *lenp = 0;
161 goto done;
163 if (len < *lenp)
164 kbuf[len++] = '\n';
165 if (copy_to_user(buffer, kbuf, len)) {
166 ret = -EFAULT;
167 goto done;
169 *lenp = len;
170 *ppos += len;
173 done:
174 free_cpumask_var(mask);
175 return ret;
178 static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
179 void __user *buffer, size_t *lenp,
180 loff_t *ppos)
182 unsigned int old, *ptr;
183 int ret;
185 mutex_lock(&flow_limit_update_mutex);
187 ptr = table->data;
188 old = *ptr;
189 ret = proc_dointvec(table, write, buffer, lenp, ppos);
190 if (!ret && write && !is_power_of_2(*ptr)) {
191 *ptr = old;
192 ret = -EINVAL;
195 mutex_unlock(&flow_limit_update_mutex);
196 return ret;
198 #endif /* CONFIG_NET_FLOW_LIMIT */
200 #ifdef CONFIG_NET_SCHED
201 static int set_default_qdisc(struct ctl_table *table, int write,
202 void __user *buffer, size_t *lenp, loff_t *ppos)
204 char id[IFNAMSIZ];
205 struct ctl_table tbl = {
206 .data = id,
207 .maxlen = IFNAMSIZ,
209 int ret;
211 qdisc_get_default(id, IFNAMSIZ);
213 ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
214 if (write && ret == 0)
215 ret = qdisc_set_default(id);
216 return ret;
218 #endif
220 static struct ctl_table net_core_table[] = {
221 #ifdef CONFIG_NET
223 .procname = "wmem_max",
224 .data = &sysctl_wmem_max,
225 .maxlen = sizeof(int),
226 .mode = 0644,
227 .proc_handler = proc_dointvec_minmax,
228 .extra1 = &min_sndbuf,
231 .procname = "rmem_max",
232 .data = &sysctl_rmem_max,
233 .maxlen = sizeof(int),
234 .mode = 0644,
235 .proc_handler = proc_dointvec_minmax,
236 .extra1 = &min_rcvbuf,
239 .procname = "wmem_default",
240 .data = &sysctl_wmem_default,
241 .maxlen = sizeof(int),
242 .mode = 0644,
243 .proc_handler = proc_dointvec_minmax,
244 .extra1 = &min_sndbuf,
247 .procname = "rmem_default",
248 .data = &sysctl_rmem_default,
249 .maxlen = sizeof(int),
250 .mode = 0644,
251 .proc_handler = proc_dointvec_minmax,
252 .extra1 = &min_rcvbuf,
255 .procname = "dev_weight",
256 .data = &weight_p,
257 .maxlen = sizeof(int),
258 .mode = 0644,
259 .proc_handler = proc_dointvec
262 .procname = "netdev_max_backlog",
263 .data = &netdev_max_backlog,
264 .maxlen = sizeof(int),
265 .mode = 0644,
266 .proc_handler = proc_dointvec
268 #ifdef CONFIG_BPF_JIT
270 .procname = "bpf_jit_enable",
271 .data = &bpf_jit_enable,
272 .maxlen = sizeof(int),
273 .mode = 0644,
274 .proc_handler = proc_dointvec
276 #endif
278 .procname = "netdev_tstamp_prequeue",
279 .data = &netdev_tstamp_prequeue,
280 .maxlen = sizeof(int),
281 .mode = 0644,
282 .proc_handler = proc_dointvec
285 .procname = "message_cost",
286 .data = &net_ratelimit_state.interval,
287 .maxlen = sizeof(int),
288 .mode = 0644,
289 .proc_handler = proc_dointvec_jiffies,
292 .procname = "message_burst",
293 .data = &net_ratelimit_state.burst,
294 .maxlen = sizeof(int),
295 .mode = 0644,
296 .proc_handler = proc_dointvec,
299 .procname = "optmem_max",
300 .data = &sysctl_optmem_max,
301 .maxlen = sizeof(int),
302 .mode = 0644,
303 .proc_handler = proc_dointvec
305 #ifdef CONFIG_RPS
307 .procname = "rps_sock_flow_entries",
308 .maxlen = sizeof(int),
309 .mode = 0644,
310 .proc_handler = rps_sock_flow_sysctl
312 #endif
313 #ifdef CONFIG_NET_FLOW_LIMIT
315 .procname = "flow_limit_cpu_bitmap",
316 .mode = 0644,
317 .proc_handler = flow_limit_cpu_sysctl
320 .procname = "flow_limit_table_len",
321 .data = &netdev_flow_limit_table_len,
322 .maxlen = sizeof(int),
323 .mode = 0644,
324 .proc_handler = flow_limit_table_len_sysctl
326 #endif /* CONFIG_NET_FLOW_LIMIT */
327 #ifdef CONFIG_NET_RX_BUSY_POLL
329 .procname = "busy_poll",
330 .data = &sysctl_net_busy_poll,
331 .maxlen = sizeof(unsigned int),
332 .mode = 0644,
333 .proc_handler = proc_dointvec
336 .procname = "busy_read",
337 .data = &sysctl_net_busy_read,
338 .maxlen = sizeof(unsigned int),
339 .mode = 0644,
340 .proc_handler = proc_dointvec
342 #endif
343 #ifdef CONFIG_NET_SCHED
345 .procname = "default_qdisc",
346 .mode = 0644,
347 .maxlen = IFNAMSIZ,
348 .proc_handler = set_default_qdisc
350 #endif
351 #endif /* CONFIG_NET */
353 .procname = "netdev_budget",
354 .data = &netdev_budget,
355 .maxlen = sizeof(int),
356 .mode = 0644,
357 .proc_handler = proc_dointvec
360 .procname = "warnings",
361 .data = &net_msg_warn,
362 .maxlen = sizeof(int),
363 .mode = 0644,
364 .proc_handler = proc_dointvec
369 static struct ctl_table netns_core_table[] = {
371 .procname = "somaxconn",
372 .data = &init_net.core.sysctl_somaxconn,
373 .maxlen = sizeof(int),
374 .mode = 0644,
375 .extra1 = &zero,
376 .extra2 = &ushort_max,
377 .proc_handler = proc_dointvec_minmax
382 static __net_init int sysctl_core_net_init(struct net *net)
384 struct ctl_table *tbl;
386 net->core.sysctl_somaxconn = SOMAXCONN;
388 tbl = netns_core_table;
389 if (!net_eq(net, &init_net)) {
390 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
391 if (tbl == NULL)
392 goto err_dup;
394 tbl[0].data = &net->core.sysctl_somaxconn;
396 /* Don't export any sysctls to unprivileged users */
397 if (net->user_ns != &init_user_ns) {
398 tbl[0].procname = NULL;
402 net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
403 if (net->core.sysctl_hdr == NULL)
404 goto err_reg;
406 return 0;
408 err_reg:
409 if (tbl != netns_core_table)
410 kfree(tbl);
411 err_dup:
412 return -ENOMEM;
415 static __net_exit void sysctl_core_net_exit(struct net *net)
417 struct ctl_table *tbl;
419 tbl = net->core.sysctl_hdr->ctl_table_arg;
420 unregister_net_sysctl_table(net->core.sysctl_hdr);
421 BUG_ON(tbl == netns_core_table);
422 kfree(tbl);
425 static __net_initdata struct pernet_operations sysctl_core_ops = {
426 .init = sysctl_core_net_init,
427 .exit = sysctl_core_net_exit,
430 static __init int sysctl_core_init(void)
432 register_net_sysctl(&init_net, "net/core", net_core_table);
433 return register_pernet_subsys(&sysctl_core_ops);
436 fs_initcall(sysctl_core_init);