[PATCH RFC 7/9] bpf: add BPF_MAP_RECHARGE syscall

Yafang Shao
2022-03-08T13:11:32Z

This patch adds a new bpf syscall BPF_MAP_RECHARGE, which means to
recharge the allocated memory of a bpf map from an offline memcg to
the current memcg.

The recharge methord for each map will be implemented in the follow-up
patches.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
include/linux/bpf.h | 2 ++
include/uapi/linux/bpf.h | 9 +++++++++
kernel/bpf/syscall.c | 19 ++++++++++++++++++-
3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 88449fb..fca274e 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -147,6 +147,8 @@ struct bpf_map_ops {
bpf_callback_t callback_fn,
void *callback_ctx, u64 flags);

+ bool (*map_recharge_memcg)(struct bpf_map *map);
+
/* BTF name and id of struct allocated by map_alloc */
const char * const map_btf_name;
int *map_btf_id;
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index a448b06..290ea67 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -821,6 +821,14 @@ struct bpf_cgroup_storage_key {
* Returns zero on success. On error, -1 is returned and *errno*
* is set appropriately.
*
+ * BPF_MAP_RECHARGE
+ * Description
+ * Recharge bpf memory from an offline memcg
+ *
+ * Return
+ * Returns zero on success. On error, -1 is returned and *errno*
+ * is set appropriately.
+ *
* NOTES
* eBPF objects (maps and programs) can be shared between processes.
*
@@ -875,6 +883,7 @@ enum bpf_cmd {
BPF_ITER_CREATE,
BPF_LINK_DETACH,
BPF_PROG_BIND_MAP,
+ BPF_MAP_RECHARGE,
};

enum bpf_map_type {
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 68fea3b..85456f1 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -1128,7 +1128,6 @@ static int map_lookup_elem(union bpf_attr *attr)
return err;
}

-
#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags

static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
@@ -4621,6 +4620,21 @@ static int bpf_prog_bind_map(union bpf_attr *attr)
return ret;
}

+static int map_recharge_elem(union bpf_attr *attr)
+{
+ int id = attr->map_id;
+ struct bpf_map *map;
+
+ map = bpf_map_idr_find(id);
+ if (IS_ERR(map))
+ return PTR_ERR(map);
+
+ if (map->ops->map_recharge_memcg)
+ map->ops->map_recharge_memcg(map);
+
+ return 0;
+}
+
static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
{
union bpf_attr attr;
@@ -4757,6 +4771,9 @@ static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
case BPF_PROG_BIND_MAP:
err = bpf_prog_bind_map(&attr);
break;
+ case BPF_MAP_RECHARGE:
+ err = map_recharge_elem(&attr);
+ break;
default:
err = -EINVAL;
break;
--
1.8.3.1