Skip to content

Commit 71c6737

Browse files
juyinNobody
authored and
Nobody
committed
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 susbsystem/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 <[email protected]>
1 parent cb1b465 commit 71c6737

File tree

2 files changed

+80
-71
lines changed

2 files changed

+80
-71
lines changed

kernel/bpf/syscall.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4892,3 +4892,83 @@ const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
48924892
const struct bpf_prog_ops bpf_syscall_prog_ops = {
48934893
.test_run = bpf_prog_test_run_syscall,
48944894
};
4895+
4896+
#ifdef CONFIG_SYSCTL
4897+
static int bpf_stats_handler(struct ctl_table *table, int write,
4898+
void *buffer, size_t *lenp, loff_t *ppos)
4899+
{
4900+
struct static_key *key = (struct static_key *)table->data;
4901+
static int saved_val;
4902+
int val, ret;
4903+
struct ctl_table tmp = {
4904+
.data = &val,
4905+
.maxlen = sizeof(val),
4906+
.mode = table->mode,
4907+
.extra1 = SYSCTL_ZERO,
4908+
.extra2 = SYSCTL_ONE,
4909+
};
4910+
4911+
if (write && !capable(CAP_SYS_ADMIN))
4912+
return -EPERM;
4913+
4914+
mutex_lock(&bpf_stats_enabled_mutex);
4915+
val = saved_val;
4916+
ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
4917+
if (write && !ret && val != saved_val) {
4918+
if (val)
4919+
static_key_slow_inc(key);
4920+
else
4921+
static_key_slow_dec(key);
4922+
saved_val = val;
4923+
}
4924+
mutex_unlock(&bpf_stats_enabled_mutex);
4925+
return ret;
4926+
}
4927+
4928+
static int bpf_unpriv_handler(struct ctl_table *table, int write,
4929+
void *buffer, size_t *lenp, loff_t *ppos)
4930+
{
4931+
int ret, unpriv_enable = *(int *)table->data;
4932+
bool locked_state = unpriv_enable == 1;
4933+
struct ctl_table tmp = *table;
4934+
4935+
if (write && !capable(CAP_SYS_ADMIN))
4936+
return -EPERM;
4937+
4938+
tmp.data = &unpriv_enable;
4939+
ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
4940+
if (write && !ret) {
4941+
if (locked_state && unpriv_enable != 1)
4942+
return -EPERM;
4943+
*(int *)table->data = unpriv_enable;
4944+
}
4945+
return ret;
4946+
}
4947+
4948+
static struct ctl_table bpf_syscall_table[] = {
4949+
{
4950+
.procname = "unprivileged_bpf_disabled",
4951+
.data = &sysctl_unprivileged_bpf_disabled,
4952+
.maxlen = sizeof(sysctl_unprivileged_bpf_disabled),
4953+
.mode = 0644,
4954+
.proc_handler = bpf_unpriv_handler,
4955+
.extra1 = SYSCTL_ZERO,
4956+
.extra2 = SYSCTL_TWO,
4957+
},
4958+
{
4959+
.procname = "bpf_stats_enabled",
4960+
.data = &bpf_stats_enabled_key.key,
4961+
.maxlen = sizeof(bpf_stats_enabled_key),
4962+
.mode = 0644,
4963+
.proc_handler = bpf_stats_handler,
4964+
},
4965+
{ }
4966+
};
4967+
4968+
static int __init bpf_syscall_sysctl_init(void)
4969+
{
4970+
register_sysctl_init("kernel", bpf_syscall_table);
4971+
return 0;
4972+
}
4973+
late_initcall(bpf_syscall_sysctl_init);
4974+
#endif /* CONFIG_SYSCTL */

kernel/sysctl.c

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -148,59 +148,6 @@ static const int max_extfrag_threshold = 1000;
148148

149149
#endif /* CONFIG_SYSCTL */
150150

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-
static int bpf_unpriv_handler(struct ctl_table *table, int write,
184-
void *buffer, size_t *lenp, loff_t *ppos)
185-
{
186-
int ret, unpriv_enable = *(int *)table->data;
187-
bool locked_state = unpriv_enable == 1;
188-
struct ctl_table tmp = *table;
189-
190-
if (write && !capable(CAP_SYS_ADMIN))
191-
return -EPERM;
192-
193-
tmp.data = &unpriv_enable;
194-
ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
195-
if (write && !ret) {
196-
if (locked_state && unpriv_enable != 1)
197-
return -EPERM;
198-
*(int *)table->data = unpriv_enable;
199-
}
200-
return ret;
201-
}
202-
#endif /* CONFIG_BPF_SYSCALL && CONFIG_SYSCTL */
203-
204151
/*
205152
* /proc/sys support
206153
*/
@@ -2303,24 +2250,6 @@ static struct ctl_table kern_table[] = {
23032250
.extra2 = SYSCTL_ONE,
23042251
},
23052252
#endif
2306-
#ifdef CONFIG_BPF_SYSCALL
2307-
{
2308-
.procname = "unprivileged_bpf_disabled",
2309-
.data = &sysctl_unprivileged_bpf_disabled,
2310-
.maxlen = sizeof(sysctl_unprivileged_bpf_disabled),
2311-
.mode = 0644,
2312-
.proc_handler = bpf_unpriv_handler,
2313-
.extra1 = SYSCTL_ZERO,
2314-
.extra2 = SYSCTL_TWO,
2315-
},
2316-
{
2317-
.procname = "bpf_stats_enabled",
2318-
.data = &bpf_stats_enabled_key.key,
2319-
.maxlen = sizeof(bpf_stats_enabled_key),
2320-
.mode = 0644,
2321-
.proc_handler = bpf_stats_handler,
2322-
},
2323-
#endif
23242253
#if defined(CONFIG_TREE_RCU)
23252254
{
23262255
.procname = "panic_on_rcu_stall",

0 commit comments

Comments
 (0)