Skip to content

Commit 5c4eca5

Browse files
juyinKernel Patches Daemon
authored andcommitted
bpf: move bpf sysctls from kernel/sysctl.c to bpf module
We're moving sysctls out of kernel/sysctl.c as its a mess. We already moved all filesystem sysctls out. And with time the goal is to move all sysctls out to their own subsystem/actual user. kernel/sysctl.c has grown to an insane mess and its easy to run into conflicts with it. The effort to move them out is part of this. Signed-off-by: Yan Zhu <zhuyan34@huawei.com>
1 parent 0fb1024 commit 5c4eca5

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>
@@ -148,66 +147,6 @@ static const int max_extfrag_threshold = 1000;
148147

149148
#endif /* CONFIG_SYSCTL */
150149

151-
#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_SYSCTL)
152-
static int bpf_stats_handler(struct ctl_table *table, int write,
153-
void *buffer, size_t *lenp, loff_t *ppos)
154-
{
155-
struct static_key *key = (struct static_key *)table->data;
156-
static int saved_val;
157-
int val, ret;
158-
struct ctl_table tmp = {
159-
.data = &val,
160-
.maxlen = sizeof(val),
161-
.mode = table->mode,
162-
.extra1 = SYSCTL_ZERO,
163-
.extra2 = SYSCTL_ONE,
164-
};
165-
166-
if (write && !capable(CAP_SYS_ADMIN))
167-
return -EPERM;
168-
169-
mutex_lock(&bpf_stats_enabled_mutex);
170-
val = saved_val;
171-
ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
172-
if (write && !ret && val != saved_val) {
173-
if (val)
174-
static_key_slow_inc(key);
175-
else
176-
static_key_slow_dec(key);
177-
saved_val = val;
178-
}
179-
mutex_unlock(&bpf_stats_enabled_mutex);
180-
return ret;
181-
}
182-
183-
void __weak unpriv_ebpf_notify(int new_state)
184-
{
185-
}
186-
187-
static int bpf_unpriv_handler(struct ctl_table *table, int write,
188-
void *buffer, size_t *lenp, loff_t *ppos)
189-
{
190-
int ret, unpriv_enable = *(int *)table->data;
191-
bool locked_state = unpriv_enable == 1;
192-
struct ctl_table tmp = *table;
193-
194-
if (write && !capable(CAP_SYS_ADMIN))
195-
return -EPERM;
196-
197-
tmp.data = &unpriv_enable;
198-
ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
199-
if (write && !ret) {
200-
if (locked_state && unpriv_enable != 1)
201-
return -EPERM;
202-
*(int *)table->data = unpriv_enable;
203-
}
204-
205-
unpriv_ebpf_notify(unpriv_enable);
206-
207-
return ret;
208-
}
209-
#endif /* CONFIG_BPF_SYSCALL && CONFIG_SYSCTL */
210-
211150
/*
212151
* /proc/sys support
213152
*/
@@ -2299,24 +2238,6 @@ static struct ctl_table kern_table[] = {
22992238
.extra2 = SYSCTL_ONE,
23002239
},
23012240
#endif
2302-
#ifdef CONFIG_BPF_SYSCALL
2303-
{
2304-
.procname = "unprivileged_bpf_disabled",
2305-
.data = &sysctl_unprivileged_bpf_disabled,
2306-
.maxlen = sizeof(sysctl_unprivileged_bpf_disabled),
2307-
.mode = 0644,
2308-
.proc_handler = bpf_unpriv_handler,
2309-
.extra1 = SYSCTL_ZERO,
2310-
.extra2 = SYSCTL_TWO,
2311-
},
2312-
{
2313-
.procname = "bpf_stats_enabled",
2314-
.data = &bpf_stats_enabled_key.key,
2315-
.maxlen = sizeof(bpf_stats_enabled_key),
2316-
.mode = 0644,
2317-
.proc_handler = bpf_stats_handler,
2318-
},
2319-
#endif
23202241
#if defined(CONFIG_TREE_RCU)
23212242
{
23222243
.procname = "panic_on_rcu_stall",

0 commit comments

Comments
 (0)