2021-12-16 02:55:38 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
#ifndef _BPF_CGROUP_DEFS_H
|
|
|
|
#define _BPF_CGROUP_DEFS_H
|
|
|
|
|
|
|
|
#ifdef CONFIG_CGROUP_BPF
|
|
|
|
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/percpu-refcount.h>
|
|
|
|
#include <linux/workqueue.h>
|
|
|
|
|
|
|
|
struct bpf_prog_array;
|
|
|
|
|
2022-06-28 17:43:06 +00:00
|
|
|
#ifdef CONFIG_BPF_LSM
|
2022-06-28 17:43:07 +00:00
|
|
|
/* Maximum number of concurrently attachable per-cgroup LSM hooks. */
|
|
|
|
#define CGROUP_LSM_NUM 10
|
2022-06-28 17:43:06 +00:00
|
|
|
#else
|
|
|
|
#define CGROUP_LSM_NUM 0
|
|
|
|
#endif
|
|
|
|
|
2021-12-16 02:55:38 +00:00
|
|
|
enum cgroup_bpf_attach_type {
|
|
|
|
CGROUP_BPF_ATTACH_TYPE_INVALID = -1,
|
|
|
|
CGROUP_INET_INGRESS = 0,
|
|
|
|
CGROUP_INET_EGRESS,
|
|
|
|
CGROUP_INET_SOCK_CREATE,
|
|
|
|
CGROUP_SOCK_OPS,
|
|
|
|
CGROUP_DEVICE,
|
|
|
|
CGROUP_INET4_BIND,
|
|
|
|
CGROUP_INET6_BIND,
|
|
|
|
CGROUP_INET4_CONNECT,
|
|
|
|
CGROUP_INET6_CONNECT,
|
2023-10-11 18:51:06 +00:00
|
|
|
CGROUP_UNIX_CONNECT,
|
2021-12-16 02:55:38 +00:00
|
|
|
CGROUP_INET4_POST_BIND,
|
|
|
|
CGROUP_INET6_POST_BIND,
|
|
|
|
CGROUP_UDP4_SENDMSG,
|
|
|
|
CGROUP_UDP6_SENDMSG,
|
2023-10-11 18:51:06 +00:00
|
|
|
CGROUP_UNIX_SENDMSG,
|
2021-12-16 02:55:38 +00:00
|
|
|
CGROUP_SYSCTL,
|
|
|
|
CGROUP_UDP4_RECVMSG,
|
|
|
|
CGROUP_UDP6_RECVMSG,
|
2023-10-11 18:51:06 +00:00
|
|
|
CGROUP_UNIX_RECVMSG,
|
2021-12-16 02:55:38 +00:00
|
|
|
CGROUP_GETSOCKOPT,
|
|
|
|
CGROUP_SETSOCKOPT,
|
|
|
|
CGROUP_INET4_GETPEERNAME,
|
|
|
|
CGROUP_INET6_GETPEERNAME,
|
2023-10-11 18:51:06 +00:00
|
|
|
CGROUP_UNIX_GETPEERNAME,
|
2021-12-16 02:55:38 +00:00
|
|
|
CGROUP_INET4_GETSOCKNAME,
|
|
|
|
CGROUP_INET6_GETSOCKNAME,
|
2023-10-11 18:51:06 +00:00
|
|
|
CGROUP_UNIX_GETSOCKNAME,
|
2021-12-16 02:55:38 +00:00
|
|
|
CGROUP_INET_SOCK_RELEASE,
|
2022-06-28 17:43:06 +00:00
|
|
|
CGROUP_LSM_START,
|
|
|
|
CGROUP_LSM_END = CGROUP_LSM_START + CGROUP_LSM_NUM - 1,
|
2021-12-16 02:55:38 +00:00
|
|
|
MAX_CGROUP_BPF_ATTACH_TYPE
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cgroup_bpf {
|
|
|
|
/* array of effective progs in this cgroup */
|
|
|
|
struct bpf_prog_array __rcu *effective[MAX_CGROUP_BPF_ATTACH_TYPE];
|
|
|
|
|
|
|
|
/* attached progs to this cgroup and attach flags
|
|
|
|
* when flags == 0 or BPF_F_ALLOW_OVERRIDE the progs list will
|
|
|
|
* have either zero or one element
|
|
|
|
* when BPF_F_ALLOW_MULTI the list can have up to BPF_CGROUP_MAX_PROGS
|
|
|
|
*/
|
bpf: convert cgroup_bpf.progs to hlist
This lets us reclaim some space to be used by new cgroup lsm slots.
Before:
struct cgroup_bpf {
struct bpf_prog_array * effective[23]; /* 0 184 */
/* --- cacheline 2 boundary (128 bytes) was 56 bytes ago --- */
struct list_head progs[23]; /* 184 368 */
/* --- cacheline 8 boundary (512 bytes) was 40 bytes ago --- */
u32 flags[23]; /* 552 92 */
/* XXX 4 bytes hole, try to pack */
/* --- cacheline 10 boundary (640 bytes) was 8 bytes ago --- */
struct list_head storages; /* 648 16 */
struct bpf_prog_array * inactive; /* 664 8 */
struct percpu_ref refcnt; /* 672 16 */
struct work_struct release_work; /* 688 32 */
/* size: 720, cachelines: 12, members: 7 */
/* sum members: 716, holes: 1, sum holes: 4 */
/* last cacheline: 16 bytes */
};
After:
struct cgroup_bpf {
struct bpf_prog_array * effective[23]; /* 0 184 */
/* --- cacheline 2 boundary (128 bytes) was 56 bytes ago --- */
struct hlist_head progs[23]; /* 184 184 */
/* --- cacheline 5 boundary (320 bytes) was 48 bytes ago --- */
u8 flags[23]; /* 368 23 */
/* XXX 1 byte hole, try to pack */
/* --- cacheline 6 boundary (384 bytes) was 8 bytes ago --- */
struct list_head storages; /* 392 16 */
struct bpf_prog_array * inactive; /* 408 8 */
struct percpu_ref refcnt; /* 416 16 */
struct work_struct release_work; /* 432 72 */
/* size: 504, cachelines: 8, members: 7 */
/* sum members: 503, holes: 1, sum holes: 1 */
/* last cacheline: 56 bytes */
};
Suggested-by: Jakub Sitnicki <jakub@cloudflare.com>
Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com>
Reviewed-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Link: https://lore.kernel.org/r/20220628174314.1216643-3-sdf@google.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2022-06-28 17:43:05 +00:00
|
|
|
struct hlist_head progs[MAX_CGROUP_BPF_ATTACH_TYPE];
|
|
|
|
u8 flags[MAX_CGROUP_BPF_ATTACH_TYPE];
|
2021-12-16 02:55:38 +00:00
|
|
|
|
|
|
|
/* list of cgroup shared storages */
|
|
|
|
struct list_head storages;
|
|
|
|
|
|
|
|
/* temp storage for effective prog array used by prog_attach/detach */
|
|
|
|
struct bpf_prog_array *inactive;
|
|
|
|
|
|
|
|
/* reference counter used to detach bpf programs after cgroup removal */
|
|
|
|
struct percpu_ref refcnt;
|
|
|
|
|
|
|
|
/* cgroup_bpf is released using a work queue */
|
|
|
|
struct work_struct release_work;
|
|
|
|
};
|
|
|
|
|
|
|
|
#else /* CONFIG_CGROUP_BPF */
|
|
|
|
struct cgroup_bpf {};
|
|
|
|
#endif /* CONFIG_CGROUP_BPF */
|
|
|
|
|
|
|
|
#endif
|