Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cd339431 authored by Roman Gushchin's avatar Roman Gushchin Committed by Daniel Borkmann
Browse files

bpf: introduce the bpf_get_local_storage() helper function



The bpf_get_local_storage() helper function is used
to get a pointer to the bpf local storage from a bpf program.

It takes a pointer to a storage map and flags as arguments.
Right now it accepts only cgroup storage maps, and flags
argument has to be 0. Further it can be extended to support
other types of local storage: e.g. thread local storage etc.

Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 7b5dd2bd
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -788,6 +788,8 @@ extern const struct bpf_func_proto bpf_sock_map_update_proto;
extern const struct bpf_func_proto bpf_sock_hash_update_proto;
extern const struct bpf_func_proto bpf_sock_hash_update_proto;
extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;


extern const struct bpf_func_proto bpf_get_local_storage_proto;

/* Shared helpers among cBPF and eBPF. */
/* Shared helpers among cBPF and eBPF. */
void bpf_user_rnd_init_once(void);
void bpf_user_rnd_init_once(void);
u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
+20 −1
Original line number Original line Diff line number Diff line
@@ -2095,6 +2095,24 @@ union bpf_attr {
 * 	Return
 * 	Return
 * 		A 64-bit integer containing the current cgroup id based
 * 		A 64-bit integer containing the current cgroup id based
 * 		on the cgroup within which the current task is running.
 * 		on the cgroup within which the current task is running.
 *
 * void* get_local_storage(void *map, u64 flags)
 *	Description
 *		Get the pointer to the local storage area.
 *		The type and the size of the local storage is defined
 *		by the *map* argument.
 *		The *flags* meaning is specific for each map type,
 *		and has to be 0 for cgroup local storage.
 *
 *		Depending on the bpf program type, a local storage area
 *		can be shared between multiple instances of the bpf program,
 *		running simultaneously.
 *
 *		A user should care about the synchronization by himself.
 *		For example, by using the BPF_STX_XADD instruction to alter
 *		the shared data.
 *	Return
 *		Pointer to the local storage area.
 */
 */
#define __BPF_FUNC_MAPPER(FN)		\
#define __BPF_FUNC_MAPPER(FN)		\
	FN(unspec),			\
	FN(unspec),			\
@@ -2177,7 +2195,8 @@ union bpf_attr {
	FN(rc_repeat),			\
	FN(rc_repeat),			\
	FN(rc_keydown),			\
	FN(rc_keydown),			\
	FN(skb_cgroup_id),		\
	FN(skb_cgroup_id),		\
	FN(get_current_cgroup_id),
	FN(get_current_cgroup_id),	\
	FN(get_local_storage),


/* integer value in 'imm' field of BPF_CALL instruction selects which helper
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
 * function eBPF program intends to call
 * function eBPF program intends to call
+2 −0
Original line number Original line Diff line number Diff line
@@ -684,6 +684,8 @@ cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
		return &bpf_map_delete_elem_proto;
		return &bpf_map_delete_elem_proto;
	case BPF_FUNC_get_current_uid_gid:
	case BPF_FUNC_get_current_uid_gid:
		return &bpf_get_current_uid_gid_proto;
		return &bpf_get_current_uid_gid_proto;
	case BPF_FUNC_get_local_storage:
		return &bpf_get_local_storage_proto;
	case BPF_FUNC_trace_printk:
	case BPF_FUNC_trace_printk:
		if (capable(CAP_SYS_ADMIN))
		if (capable(CAP_SYS_ADMIN))
			return bpf_get_trace_printk_proto();
			return bpf_get_trace_printk_proto();
+1 −0
Original line number Original line Diff line number Diff line
@@ -1795,6 +1795,7 @@ const struct bpf_func_proto bpf_get_current_comm_proto __weak;
const struct bpf_func_proto bpf_sock_map_update_proto __weak;
const struct bpf_func_proto bpf_sock_map_update_proto __weak;
const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak;
const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak;
const struct bpf_func_proto bpf_get_local_storage_proto __weak;


const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
{
{
+20 −0
Original line number Original line Diff line number Diff line
@@ -193,4 +193,24 @@ const struct bpf_func_proto bpf_get_current_cgroup_id_proto = {
	.gpl_only	= false,
	.gpl_only	= false,
	.ret_type	= RET_INTEGER,
	.ret_type	= RET_INTEGER,
};
};

DECLARE_PER_CPU(void*, bpf_cgroup_storage);

BPF_CALL_2(bpf_get_local_storage, struct bpf_map *, map, u64, flags)
{
	/* map and flags arguments are not used now,
	 * but provide an ability to extend the API
	 * for other types of local storages.
	 * verifier checks that their values are correct.
	 */
	return (unsigned long) this_cpu_read(bpf_cgroup_storage);
}

const struct bpf_func_proto bpf_get_local_storage_proto = {
	.func		= bpf_get_local_storage,
	.gpl_only	= false,
	.ret_type	= RET_PTR_TO_MAP_VALUE,
	.arg1_type	= ARG_CONST_MAP_PTR,
	.arg2_type	= ARG_ANYTHING,
};
#endif
#endif
Loading