brintos

brintos / linux-shallow public Read only

0
0
Text · 1.0 KiB · ea2dbb8 Raw
48 lines · c
1// SPDX-License-Identifier: GPL-2.02 3#include "vmlinux.h"4#include <bpf/bpf_helpers.h>5#include <bpf/bpf_tracing.h>6 7char _license[] SEC("license") = "GPL";8 9#ifndef EBUSY10#define EBUSY 1611#endif12 13extern bool CONFIG_PREEMPT __kconfig __weak;14int nr_get_errs = 0;15int nr_del_errs = 0;16 17struct {18	__uint(type, BPF_MAP_TYPE_TASK_STORAGE);19	__uint(map_flags, BPF_F_NO_PREALLOC);20	__type(key, int);21	__type(value, int);22} task_storage SEC(".maps");23 24SEC("lsm.s/socket_post_create")25int BPF_PROG(socket_post_create, struct socket *sock, int family, int type,26	     int protocol, int kern)27{28	struct task_struct *task;29	int ret, zero = 0;30	int *value;31 32	if (!CONFIG_PREEMPT)33		return 0;34 35	task = bpf_get_current_task_btf();36	value = bpf_task_storage_get(&task_storage, task, &zero,37				     BPF_LOCAL_STORAGE_GET_F_CREATE);38	if (!value)39		__sync_fetch_and_add(&nr_get_errs, 1);40 41	ret = bpf_task_storage_delete(&task_storage,42				      bpf_get_current_task_btf());43	if (ret == -EBUSY)44		__sync_fetch_and_add(&nr_del_errs, 1);45 46	return 0;47}48