From: Yonghong Song <yhs@meta.com>
To: Martin KaFai Lau <martin.lau@linux.dev>, Yonghong Song <yhs@fb.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
kernel-team@fb.com, bpf@vger.kernel.org,
Martin KaFai Lau <martin.lau@kernel.org>
Subject: Re: [PATCH bpf-next v5 6/7] selftests/bpf: Add tests for bpf_rcu_read_lock()
Date: Tue, 15 Nov 2022 21:33:21 -0800 [thread overview]
Message-ID: <e2b010e4-9e6e-c949-36b7-5da275c4cd03@meta.com> (raw)
In-Reply-To: <7bd272c8-6e28-cdcd-6728-a78a71f6b0d3@linux.dev>
On 11/14/22 10:50 PM, Martin KaFai Lau wrote:
> On 11/11/22 8:58 AM, Yonghong Song wrote:
>> diff --git a/tools/testing/selftests/bpf/progs/rcu_read_lock.c
>> b/tools/testing/selftests/bpf/progs/rcu_read_lock.c
>> new file mode 100644
>> index 000000000000..c11b4f8f9a9d
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/progs/rcu_read_lock.c
>> @@ -0,0 +1,355 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
>> +
>> +#include "vmlinux.h"
>> +#include <bpf/bpf_helpers.h>
>> +#include <bpf/bpf_tracing.h>
>> +#include "bpf_tracing_net.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");
>> +
>> +struct {
>> + __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
>> + __uint(map_flags, BPF_F_NO_PREALLOC);
>> + __type(key, int);
>> + __type(value, long);
>> +} map_b SEC(".maps");
>> +
>> +__u32 user_data, key_serial, target_pid = 0;
>> +__u64 flags, result = 0;
>> +
>> +struct bpf_key *bpf_lookup_user_key(__u32 serial, __u64 flags) __ksym;
>> +void bpf_key_put(struct bpf_key *key) __ksym;
>> +void bpf_rcu_read_lock(void) __ksym;
>> +void bpf_rcu_read_unlock(void) __ksym;
>> +
>> +SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
>> +int cgrp_succ(void *ctx)
>> +{
>> + struct task_struct *task;
>> + struct css_set *cgroups;
>> + struct cgroup *dfl_cgrp;
>> + long init_val = 2;
>> + long *ptr;
>> +
>> + task = bpf_get_current_task_btf();
>> + if (task->pid != target_pid)
>> + return 0;
>> +
>> + bpf_rcu_read_lock();
>> + cgroups = task->cgroups;
>> + dfl_cgrp = cgroups->dfl_cgrp;
>> + bpf_rcu_read_unlock();
>
> Outside of the rcu section, "cgroups" could have been gone. Is it
> possible that "dfl_cgrp" could be gone together with "cgroups"?
In this particular case, looks like the best is indeed
to get a reference for dfl_cgrp before doing bpf_rcu_read_unlock.
But to decide whether a particular non-rcu pointer needs
reference or not needs kernel internal knowledge for
that particular context.
So right now, the approach is to take the approach
for generic ptr_to_btf_id approach. That is, the
non-rcu pointer after rcu pointer tracing is 'trusted'.
The user can always increase bpf_rcu_read_lock() region
by himself to cover the case if the non-rcu pointer
is untrusted.
>
>> + ptr = bpf_cgrp_storage_get(&map_a, dfl_cgrp, &init_val,
>> + BPF_LOCAL_STORAGE_GET_F_CREATE);
>> + if (!ptr)
>> + return 0;
>> + ptr = bpf_cgrp_storage_get(&map_a, dfl_cgrp, 0, 0);
>> + if (!ptr)
>> + return 0;
>> + result = *ptr;
>> + return 0;
>> +}
>> +
>
> [ ... ]
>
>> +SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
>> +int miss_unlock(void *ctx)
>> +{
>> + struct task_struct *task;
>> + struct css_set *cgroups;
>> + struct cgroup *dfl_cgrp;
>> +
>> + /* missing bpf_rcu_read_unlock() */
>> + bpf_rcu_read_lock();
>
>
>> + task = bpf_get_current_task_btf();
>> + bpf_rcu_read_lock();
>
> One of the bpf_rcu_read_lock() needs to be removed. Otherwise, I think
> the verifier will error on the nested rcu read lock first instead of
> testing the missing unlock case here.
Thanks, will do.
>
>> + cgroups = task->cgroups;
>> + bpf_rcu_read_unlock();
>> + dfl_cgrp = cgroups->dfl_cgrp;
>> + (void)bpf_cgrp_storage_get(&map_a, dfl_cgrp, 0,
>> + BPF_LOCAL_STORAGE_GET_F_CREATE);
>> + return 0;
>> +}
>
next prev parent reply other threads:[~2022-11-16 5:33 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-11 16:57 [PATCH bpf-next v5 0/7] bpf: Add bpf_rcu_read_lock() support Yonghong Song
2022-11-11 16:57 ` [PATCH bpf-next v5 1/7] compiler_types: Define __rcu as __attribute__((btf_type_tag("rcu"))) Yonghong Song
2022-11-11 16:57 ` [PATCH bpf-next v5 2/7] bpf: Abstract out functions to check sleepable helpers Yonghong Song
2022-11-11 16:57 ` [PATCH bpf-next v5 3/7] bpf: Add kfunc bpf_rcu_read_lock/unlock() Yonghong Song
2022-11-11 16:57 ` [PATCH bpf-next v5 4/7] bpf: Add bpf_rcu_read_lock() verifier support Yonghong Song
2022-11-11 16:58 ` [PATCH bpf-next v5 5/7] bpf: Enable sleeptable support for cgrp local storage Yonghong Song
2022-11-11 16:58 ` [PATCH bpf-next v5 6/7] selftests/bpf: Add tests for bpf_rcu_read_lock() Yonghong Song
2022-11-15 6:50 ` Martin KaFai Lau
2022-11-16 5:33 ` Yonghong Song [this message]
2022-11-11 16:58 ` [PATCH bpf-next v5 7/7] selftests/bpf: Add rcu_read_lock test to s390x/aarch64 deny lists Yonghong Song
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e2b010e4-9e6e-c949-36b7-5da275c4cd03@meta.com \
--to=yhs@meta.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
--cc=martin.lau@kernel.org \
--cc=martin.lau@linux.dev \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox