From: Martin KaFai Lau <kafai@fb.com>
To: <bpf@vger.kernel.org>
Cc: <linux-kernel@vger.kernel.org>, Song Liu <songliubraving@fb.com>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Paul Turner <pjt@google.com>, Jann Horn <jannh@google.com>,
Hao Luo <haoluo@google.com>
Subject: Re: [PATCH bpf-next v3 1/9] bpf: Implement task local storage
Date: Wed, 4 Nov 2020 14:17:58 -0800 [thread overview]
Message-ID: <20201104221758.jr537hm7psfyvqlv@kafai-mbp.dhcp.thefacebook.com> (raw)
In-Reply-To: <20201104220814.quq7jzpeb4wcyffv@kafai-mbp.dhcp.thefacebook.com>
Please ignore this reply which has missed some recipients.
On Wed, Nov 04, 2020 at 02:08:14PM -0800, Martin KaFai Lau wrote:
> On Wed, Nov 04, 2020 at 05:44:45PM +0100, KP Singh wrote:
> [ ... ]
>
> > +static void *bpf_pid_task_storage_lookup_elem(struct bpf_map *map, void *key)
> > +{
> > + struct bpf_local_storage_data *sdata;
> > + struct task_struct *task;
> > + unsigned int f_flags;
> > + struct pid *pid;
> > + int fd, err;
> > +
> > + fd = *(int *)key;
> > + pid = pidfd_get_pid(fd, &f_flags);
> > + if (IS_ERR(pid))
> > + return ERR_CAST(pid);
> > +
> > + /* We should be in an RCU read side critical section, it should be safe
> > + * to call pid_task.
> > + */
> > + WARN_ON_ONCE(!rcu_read_lock_held());
> > + task = pid_task(pid, PIDTYPE_PID);
> > + if (!task) {
> > + err = -ENOENT;
> > + goto out;
> > + }
> > +
> > + sdata = task_storage_lookup(task, map, true);
> > + put_pid(pid);
> > + return sdata ? sdata->data : NULL;
> > +out:
> > + put_pid(pid);
> > + return ERR_PTR(err);
> > +}
> > +
> > +static int bpf_pid_task_storage_update_elem(struct bpf_map *map, void *key,
> > + void *value, u64 map_flags)
> > +{
> > + struct bpf_local_storage_data *sdata;
> > + struct task_struct *task;
> > + unsigned int f_flags;
> > + struct pid *pid;
> > + int fd, err;
> > +
> > + fd = *(int *)key;
> > + pid = pidfd_get_pid(fd, &f_flags);
> > + if (IS_ERR(pid))
> > + return PTR_ERR(pid);
> > +
> > + /* We should be in an RCU read side critical section, it should be safe
> > + * to call pid_task.
> > + */
> > + WARN_ON_ONCE(!rcu_read_lock_held());
> > + task = pid_task(pid, PIDTYPE_PID);
> > + if (!task) {
> > + err = -ENOENT;
> > + goto out;
> > + }
> > +
> > + sdata = bpf_local_storage_update(
> > + task, (struct bpf_local_storage_map *)map, value, map_flags);
> It seems the task is protected by rcu here and the task may be going away.
> Is it ok?
>
> or the following comment in the later "BPF_CALL_4(bpf_task_storage_get, ...)"
> is no longer valid?
> /* This helper must only called from where the task is guaranteed
> * to have a refcount and cannot be freed.
> */
>
> > +
> > + err = PTR_ERR_OR_ZERO(sdata);
> > +out:
> > + put_pid(pid);
> > + return err;
> > +}
> > +
>
> [ ... ]
>
> > +BPF_CALL_4(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *,
> > + task, void *, value, u64, flags)
> > +{
> > + struct bpf_local_storage_data *sdata;
> > +
> > + if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE))
> > + return (unsigned long)NULL;
> > +
> > + /* explicitly check that the task_storage_ptr is not
> > + * NULL as task_storage_lookup returns NULL in this case and
> > + * bpf_local_storage_update expects the owner to have a
> > + * valid storage pointer.
> > + */
> > + if (!task_storage_ptr(task))
> > + return (unsigned long)NULL;
> > +
> > + sdata = task_storage_lookup(task, map, true);
> > + if (sdata)
> > + return (unsigned long)sdata->data;
> > +
> > + /* This helper must only called from where the task is guaranteed
> > + * to have a refcount and cannot be freed.
> > + */
> > + if (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) {
> > + sdata = bpf_local_storage_update(
> > + task, (struct bpf_local_storage_map *)map, value,
> > + BPF_NOEXIST);
> > + return IS_ERR(sdata) ? (unsigned long)NULL :
> > + (unsigned long)sdata->data;
> > + }
> > +
> > + return (unsigned long)NULL;
> > +}
> > +
>
> [ ... ]
>
> > diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> > index 8f50c9c19f1b..f3fe9f53f93c 100644
> > --- a/kernel/bpf/syscall.c
> > +++ b/kernel/bpf/syscall.c
> > @@ -773,7 +773,8 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf,
> > map->map_type != BPF_MAP_TYPE_ARRAY &&
> > map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
> > map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
> > - map->map_type != BPF_MAP_TYPE_INODE_STORAGE)
> > + map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
> > + map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
> This is to enable spin lock support in a map's value. Without peeking
> patch 5, I was confused a bit here. It seems patch 5 was missed when
> inode storage was added.
>
> > return -ENOTSUPP;
> > if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
> > map->value_size) {
next prev parent reply other threads:[~2020-11-04 22:19 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-04 16:44 [PATCH bpf-next v3 0/9] Implement task_local_storage KP Singh
2020-11-04 16:44 ` [PATCH bpf-next v3 1/9] bpf: Implement task local storage KP Singh
2020-11-04 22:08 ` Martin KaFai Lau
2020-11-04 22:17 ` Martin KaFai Lau [this message]
2020-11-04 22:15 ` Martin KaFai Lau
2020-11-04 22:54 ` KP Singh
2020-11-04 16:44 ` [PATCH bpf-next v3 2/9] libbpf: Add support for " KP Singh
2020-11-04 22:20 ` Martin KaFai Lau
2020-11-04 16:44 ` [PATCH bpf-next v3 3/9] bpftool: " KP Singh
2020-11-04 16:44 ` [PATCH bpf-next v3 4/9] bpf: Implement get_current_task_btf and RET_PTR_TO_BTF_ID KP Singh
2020-11-04 16:44 ` [PATCH bpf-next v3 5/9] bpf: Allow LSM programs to use bpf spin locks KP Singh
2020-11-04 22:35 ` Martin KaFai Lau
2020-11-04 22:44 ` KP Singh
2020-11-04 16:44 ` [PATCH bpf-next v3 6/9] bpf: Fix tests for local_storage KP Singh
2020-11-04 16:44 ` [PATCH bpf-next v3 7/9] bpf: Update selftests for local_storage to use vmlinux.h KP Singh
2020-11-04 16:44 ` [PATCH bpf-next v3 8/9] bpf: Add tests for task_local_storage KP Singh
2020-11-04 16:44 ` [PATCH bpf-next v3 9/9] bpf: Exercise syscall operations for inode and sk storage KP Singh
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=20201104221758.jr537hm7psfyvqlv@kafai-mbp.dhcp.thefacebook.com \
--to=kafai@fb.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=jannh@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=pjt@google.com \
--cc=songliubraving@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;
as well as URLs for NNTP newsgroup(s).