forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bpf: Add sleepable prog tests for cgrp local storage
Add three tests for cgrp local storage support for sleepable progs. Two tests can load and run properly, one for cgroup_iter, another for passing current->cgroups->dfl_cgrp to bpf_cgrp_storage_get() helper. One test has bpf_rcu_read_lock() and failed to load. Signed-off-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
- Loading branch information
1 parent
2c40d97
commit 41d76c7
Showing
2 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */ | ||
|
||
#include "bpf_iter.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
#include "bpf_misc.h" | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_CGRP_STORAGE); | ||
__uint(map_flags, BPF_F_NO_PREALLOC); | ||
__type(key, int); | ||
__type(value, long); | ||
} map_a SEC(".maps"); | ||
|
||
__u32 target_pid; | ||
__u64 cgroup_id; | ||
|
||
void bpf_rcu_read_lock(void) __ksym; | ||
void bpf_rcu_read_unlock(void) __ksym; | ||
|
||
SEC("?iter.s/cgroup") | ||
int cgroup_iter(struct bpf_iter__cgroup *ctx) | ||
{ | ||
struct seq_file *seq = ctx->meta->seq; | ||
struct cgroup *cgrp = ctx->cgroup; | ||
long *ptr; | ||
|
||
if (cgrp == NULL) | ||
return 0; | ||
|
||
ptr = bpf_cgrp_storage_get(&map_a, cgrp, 0, | ||
BPF_LOCAL_STORAGE_GET_F_CREATE); | ||
if (ptr) | ||
cgroup_id = cgrp->kn->id; | ||
return 0; | ||
} | ||
|
||
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") | ||
int no_rcu_lock(void *ctx) | ||
{ | ||
struct task_struct *task; | ||
struct cgroup *cgrp; | ||
long *ptr; | ||
|
||
task = bpf_get_current_task_btf(); | ||
if (task->pid != target_pid) | ||
return 0; | ||
|
||
/* ptr_to_btf_id semantics. should work. */ | ||
cgrp = task->cgroups->dfl_cgrp; | ||
ptr = bpf_cgrp_storage_get(&map_a, cgrp, 0, | ||
BPF_LOCAL_STORAGE_GET_F_CREATE); | ||
if (ptr) | ||
cgroup_id = cgrp->kn->id; | ||
return 0; | ||
} | ||
|
||
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") | ||
int yes_rcu_lock(void *ctx) | ||
{ | ||
struct task_struct *task; | ||
struct cgroup *cgrp; | ||
long *ptr; | ||
|
||
task = bpf_get_current_task_btf(); | ||
if (task->pid != target_pid) | ||
return 0; | ||
|
||
bpf_rcu_read_lock(); | ||
cgrp = task->cgroups->dfl_cgrp; | ||
/* cgrp is untrusted and cannot pass to bpf_cgrp_storage_get() helper. */ | ||
ptr = bpf_cgrp_storage_get(&map_a, cgrp, 0, BPF_LOCAL_STORAGE_GET_F_CREATE); | ||
if (ptr) | ||
cgroup_id = cgrp->kn->id; | ||
bpf_rcu_read_unlock(); | ||
return 0; | ||
} |