Skip to content

Commit 3831897

Browse files
committed
Merge remote-tracking branch 'bpf-next/pr/bpf-sysctl' into sysctl-next
Signed-off-by: Luis Chamberlain <[email protected]>
2 parents 8e4e83b + 2900005 commit 3831897

File tree

2 files changed

+87
-79
lines changed

2 files changed

+87
-79
lines changed

kernel/bpf/syscall.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4908,3 +4908,90 @@ const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
49084908
const struct bpf_prog_ops bpf_syscall_prog_ops = {
49094909
.test_run = bpf_prog_test_run_syscall,
49104910
};
4911+
4912+
#ifdef CONFIG_SYSCTL
4913+
static int bpf_stats_handler(struct ctl_table *table, int write,
4914+
void *buffer, size_t *lenp, loff_t *ppos)
4915+
{
4916+
struct static_key *key = (struct static_key *)table->data;
4917+
static int saved_val;
4918+
int val, ret;
4919+
struct ctl_table tmp = {
4920+
.data = &val,
4921+
.maxlen = sizeof(val),
4922+
.mode = table->mode,
4923+
.extra1 = SYSCTL_ZERO,
4924+
.extra2 = SYSCTL_ONE,
4925+
};
4926+
4927+
if (write && !capable(CAP_SYS_ADMIN))
4928+
return -EPERM;
4929+
4930+
mutex_lock(&bpf_stats_enabled_mutex);
4931+
val = saved_val;
4932+
ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
4933+
if (write && !ret && val != saved_val) {
4934+
if (val)
4935+
static_key_slow_inc(key);
4936+
else
4937+
static_key_slow_dec(key);
4938+
saved_val = val;
4939+
}
4940+
mutex_unlock(&bpf_stats_enabled_mutex);
4941+
return ret;
4942+
}
4943+
4944+
void __weak unpriv_ebpf_notify(int new_state)
4945+
{
4946+
}
4947+
4948+
static int bpf_unpriv_handler(struct ctl_table *table, int write,
4949+
void *buffer, size_t *lenp, loff_t *ppos)
4950+
{
4951+
int ret, unpriv_enable = *(int *)table->data;
4952+
bool locked_state = unpriv_enable == 1;
4953+
struct ctl_table tmp = *table;
4954+
4955+
if (write && !capable(CAP_SYS_ADMIN))
4956+
return -EPERM;
4957+
4958+
tmp.data = &unpriv_enable;
4959+
ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
4960+
if (write && !ret) {
4961+
if (locked_state && unpriv_enable != 1)
4962+
return -EPERM;
4963+
*(int *)table->data = unpriv_enable;
4964+
}
4965+
4966+
unpriv_ebpf_notify(unpriv_enable);
4967+
4968+
return ret;
4969+
}
4970+
4971+
static struct ctl_table bpf_syscall_table[] = {
4972+
{
4973+
.procname = "unprivileged_bpf_disabled",
4974+
.data = &sysctl_unprivileged_bpf_disabled,
4975+
.maxlen = sizeof(sysctl_unprivileged_bpf_disabled),
4976+
.mode = 0644,
4977+
.proc_handler = bpf_unpriv_handler,
4978+
.extra1 = SYSCTL_ZERO,
4979+
.extra2 = SYSCTL_TWO,
4980+
},
4981+
{
4982+
.procname = "bpf_stats_enabled",
4983+
.data = &bpf_stats_enabled_key.key,
4984+
.maxlen = sizeof(bpf_stats_enabled_key),
4985+
.mode = 0644,
4986+
.proc_handler = bpf_stats_handler,
4987+
},
4988+
{ }
4989+
};
4990+
4991+
static int __init bpf_syscall_sysctl_init(void)
4992+
{
4993+
register_sysctl_init("kernel", bpf_syscall_table);
4994+
return 0;
4995+
}
4996+
late_initcall(bpf_syscall_sysctl_init);
4997+
#endif /* CONFIG_SYSCTL */

kernel/sysctl.c

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
#include <linux/binfmts.h>
6363
#include <linux/sched/sysctl.h>
6464
#include <linux/kexec.h>
65-
#include <linux/bpf.h>
6665
#include <linux/mount.h>
6766
#include <linux/userfaultfd_k.h>
6867
#include <linux/latencytop.h>
@@ -139,66 +138,6 @@ static const int max_extfrag_threshold = 1000;
139138

140139
#endif /* CONFIG_SYSCTL */
141140

142-
#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_SYSCTL)
143-
static int bpf_stats_handler(struct ctl_table *table, int write,
144-
void *buffer, size_t *lenp, loff_t *ppos)
145-
{
146-
struct static_key *key = (struct static_key *)table->data;
147-
static int saved_val;
148-
int val, ret;
149-
struct ctl_table tmp = {
150-
.data = &val,
151-
.maxlen = sizeof(val),
152-
.mode = table->mode,
153-
.extra1 = SYSCTL_ZERO,
154-
.extra2 = SYSCTL_ONE,
155-
};
156-
157-
if (write && !capable(CAP_SYS_ADMIN))
158-
return -EPERM;
159-
160-
mutex_lock(&bpf_stats_enabled_mutex);
161-
val = saved_val;
162-
ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
163-
if (write && !ret && val != saved_val) {
164-
if (val)
165-
static_key_slow_inc(key);
166-
else
167-
static_key_slow_dec(key);
168-
saved_val = val;
169-
}
170-
mutex_unlock(&bpf_stats_enabled_mutex);
171-
return ret;
172-
}
173-
174-
void __weak unpriv_ebpf_notify(int new_state)
175-
{
176-
}
177-
178-
static int bpf_unpriv_handler(struct ctl_table *table, int write,
179-
void *buffer, size_t *lenp, loff_t *ppos)
180-
{
181-
int ret, unpriv_enable = *(int *)table->data;
182-
bool locked_state = unpriv_enable == 1;
183-
struct ctl_table tmp = *table;
184-
185-
if (write && !capable(CAP_SYS_ADMIN))
186-
return -EPERM;
187-
188-
tmp.data = &unpriv_enable;
189-
ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
190-
if (write && !ret) {
191-
if (locked_state && unpriv_enable != 1)
192-
return -EPERM;
193-
*(int *)table->data = unpriv_enable;
194-
}
195-
196-
unpriv_ebpf_notify(unpriv_enable);
197-
198-
return ret;
199-
}
200-
#endif /* CONFIG_BPF_SYSCALL && CONFIG_SYSCTL */
201-
202141
/*
203142
* /proc/sys support
204143
*/
@@ -2112,24 +2051,6 @@ static struct ctl_table kern_table[] = {
21122051
.extra2 = SYSCTL_ONE,
21132052
},
21142053
#endif
2115-
#ifdef CONFIG_BPF_SYSCALL
2116-
{
2117-
.procname = "unprivileged_bpf_disabled",
2118-
.data = &sysctl_unprivileged_bpf_disabled,
2119-
.maxlen = sizeof(sysctl_unprivileged_bpf_disabled),
2120-
.mode = 0644,
2121-
.proc_handler = bpf_unpriv_handler,
2122-
.extra1 = SYSCTL_ZERO,
2123-
.extra2 = SYSCTL_TWO,
2124-
},
2125-
{
2126-
.procname = "bpf_stats_enabled",
2127-
.data = &bpf_stats_enabled_key.key,
2128-
.maxlen = sizeof(bpf_stats_enabled_key),
2129-
.mode = 0644,
2130-
.proc_handler = bpf_stats_handler,
2131-
},
2132-
#endif
21332054
#if defined(CONFIG_TREE_RCU)
21342055
{
21352056
.procname = "panic_on_rcu_stall",

0 commit comments

Comments
 (0)