Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
lxc
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
lxc
Commits
dcbb9e99
Unverified
Commit
dcbb9e99
authored
Jan 30, 2021
by
Christian Brauner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cgroups: bpf fixes
Signed-off-by:
Christian Brauner
<
christian.brauner@ubuntu.com
>
parent
0e24c560
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
46 deletions
+50
-46
cgfsng.c
src/lxc/cgroups/cgfsng.c
+11
-11
cgroup2_devices.c
src/lxc/cgroups/cgroup2_devices.c
+39
-35
No files found.
src/lxc/cgroups/cgfsng.c
View file @
dcbb9e99
...
@@ -2963,12 +2963,12 @@ __cgfsng_ops static bool cgfsng_setup_limits(struct cgroup_ops *ops,
...
@@ -2963,12 +2963,12 @@ __cgfsng_ops static bool cgfsng_setup_limits(struct cgroup_ops *ops,
__cgfsng_ops
static
bool
cgfsng_devices_activate
(
struct
cgroup_ops
*
ops
,
struct
lxc_handler
*
handler
)
__cgfsng_ops
static
bool
cgfsng_devices_activate
(
struct
cgroup_ops
*
ops
,
struct
lxc_handler
*
handler
)
{
{
#ifdef HAVE_STRUCT_BPF_CGROUP_DEV_CTX
#ifdef HAVE_STRUCT_BPF_CGROUP_DEV_CTX
__do_bpf_program_free
struct
bpf_program
*
devices
=
NULL
;
__do_bpf_program_free
struct
bpf_program
*
prog
=
NULL
;
int
ret
;
int
ret
;
struct
lxc_conf
*
conf
;
struct
lxc_conf
*
conf
;
struct
hierarchy
*
unified
;
struct
hierarchy
*
unified
;
struct
lxc_list
*
it
;
struct
lxc_list
*
it
;
struct
bpf_program
*
devices
_old
;
struct
bpf_program
*
prog
_old
;
if
(
!
ops
)
if
(
!
ops
)
return
ret_set_errno
(
false
,
ENOENT
);
return
ret_set_errno
(
false
,
ENOENT
);
...
@@ -2988,18 +2988,18 @@ __cgfsng_ops static bool cgfsng_devices_activate(struct cgroup_ops *ops, struct
...
@@ -2988,18 +2988,18 @@ __cgfsng_ops static bool cgfsng_devices_activate(struct cgroup_ops *ops, struct
!
unified
->
container_full_path
||
lxc_list_empty
(
&
conf
->
devices
))
!
unified
->
container_full_path
||
lxc_list_empty
(
&
conf
->
devices
))
return
true
;
return
true
;
devices
=
bpf_program_new
(
BPF_PROG_TYPE_CGROUP_DEVICE
);
prog
=
bpf_program_new
(
BPF_PROG_TYPE_CGROUP_DEVICE
);
if
(
!
devices
)
if
(
!
prog
)
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to create new bpf program"
);
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to create new bpf program"
);
ret
=
bpf_program_init
(
devices
);
ret
=
bpf_program_init
(
prog
);
if
(
ret
)
if
(
ret
)
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to initialize bpf program"
);
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to initialize bpf program"
);
lxc_list_for_each
(
it
,
&
conf
->
devices
)
{
lxc_list_for_each
(
it
,
&
conf
->
devices
)
{
struct
device_item
*
cur
=
it
->
elem
;
struct
device_item
*
cur
=
it
->
elem
;
ret
=
bpf_program_append_device
(
devices
,
cur
);
ret
=
bpf_program_append_device
(
prog
,
cur
);
if
(
ret
)
if
(
ret
)
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to add new rule to bpf device program: type %c, major %d, minor %d, access %s, allow %d, global_rule %d"
,
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to add new rule to bpf device program: type %c, major %d, minor %d, access %s, allow %d, global_rule %d"
,
cur
->
type
,
cur
->
type
,
...
@@ -3017,20 +3017,20 @@ __cgfsng_ops static bool cgfsng_devices_activate(struct cgroup_ops *ops, struct
...
@@ -3017,20 +3017,20 @@ __cgfsng_ops static bool cgfsng_devices_activate(struct cgroup_ops *ops, struct
cur
->
global_rule
);
cur
->
global_rule
);
}
}
ret
=
bpf_program_finalize
(
devices
);
ret
=
bpf_program_finalize
(
prog
);
if
(
ret
)
if
(
ret
)
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to finalize bpf program"
);
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to finalize bpf program"
);
ret
=
bpf_program_cgroup_attach
(
devices
,
BPF_CGROUP_DEVICE
,
ret
=
bpf_program_cgroup_attach
(
prog
,
BPF_CGROUP_DEVICE
,
unified
->
container_limit_path
,
unified
->
container_limit_path
,
BPF_F_ALLOW_MULTI
);
BPF_F_ALLOW_MULTI
);
if
(
ret
)
if
(
ret
)
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to attach bpf program"
);
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to attach bpf program"
);
/* Replace old bpf program. */
/* Replace old bpf program. */
devices
_old
=
move_ptr
(
ops
->
cgroup2_devices
);
prog
_old
=
move_ptr
(
ops
->
cgroup2_devices
);
ops
->
cgroup2_devices
=
move_ptr
(
devices
);
ops
->
cgroup2_devices
=
move_ptr
(
prog
);
devices
=
move_ptr
(
devices
_old
);
prog
=
move_ptr
(
prog
_old
);
#endif
#endif
return
true
;
return
true
;
}
}
...
...
src/lxc/cgroups/cgroup2_devices.c
View file @
dcbb9e99
...
@@ -179,7 +179,7 @@ struct bpf_program *bpf_program_new(uint32_t prog_type)
...
@@ -179,7 +179,7 @@ struct bpf_program *bpf_program_new(uint32_t prog_type)
prog
=
zalloc
(
sizeof
(
struct
bpf_program
));
prog
=
zalloc
(
sizeof
(
struct
bpf_program
));
if
(
!
prog
)
if
(
!
prog
)
return
NULL
;
return
ret_set_errno
(
NULL
,
ENOMEM
)
;
prog
->
prog_type
=
prog_type
;
prog
->
prog_type
=
prog_type
;
prog
->
kernel_fd
=
-
EBADF
;
prog
->
kernel_fd
=
-
EBADF
;
...
@@ -194,19 +194,19 @@ struct bpf_program *bpf_program_new(uint32_t prog_type)
...
@@ -194,19 +194,19 @@ struct bpf_program *bpf_program_new(uint32_t prog_type)
int
bpf_program_init
(
struct
bpf_program
*
prog
)
int
bpf_program_init
(
struct
bpf_program
*
prog
)
{
{
const
struct
bpf_insn
pre_insn
[]
=
{
const
struct
bpf_insn
pre_insn
[]
=
{
/* load device type to r2 */
/* load device type to r2 */
BPF_LDX_MEM
(
BPF_W
,
BPF_REG_2
,
BPF_REG_1
,
offsetof
(
struct
bpf_cgroup_dev_ctx
,
access_type
)),
BPF_LDX_MEM
(
BPF_W
,
BPF_REG_2
,
BPF_REG_1
,
offsetof
(
struct
bpf_cgroup_dev_ctx
,
access_type
)),
BPF_ALU32_IMM
(
BPF_AND
,
BPF_REG_2
,
0xFFFF
),
BPF_ALU32_IMM
(
BPF_AND
,
BPF_REG_2
,
0xFFFF
),
/* load access type to r3 */
/* load access type to r3 */
BPF_LDX_MEM
(
BPF_W
,
BPF_REG_3
,
BPF_REG_1
,
offsetof
(
struct
bpf_cgroup_dev_ctx
,
access_type
)),
BPF_LDX_MEM
(
BPF_W
,
BPF_REG_3
,
BPF_REG_1
,
offsetof
(
struct
bpf_cgroup_dev_ctx
,
access_type
)),
BPF_ALU32_IMM
(
BPF_RSH
,
BPF_REG_3
,
16
),
BPF_ALU32_IMM
(
BPF_RSH
,
BPF_REG_3
,
16
),
/* load major number to r4 */
/* load major number to r4 */
BPF_LDX_MEM
(
BPF_W
,
BPF_REG_4
,
BPF_REG_1
,
offsetof
(
struct
bpf_cgroup_dev_ctx
,
major
)),
BPF_LDX_MEM
(
BPF_W
,
BPF_REG_4
,
BPF_REG_1
,
offsetof
(
struct
bpf_cgroup_dev_ctx
,
major
)),
/* load minor number to r5 */
/* load minor number to r5 */
BPF_LDX_MEM
(
BPF_W
,
BPF_REG_5
,
BPF_REG_1
,
offsetof
(
struct
bpf_cgroup_dev_ctx
,
minor
)),
BPF_LDX_MEM
(
BPF_W
,
BPF_REG_5
,
BPF_REG_1
,
offsetof
(
struct
bpf_cgroup_dev_ctx
,
minor
)),
};
};
if
(
!
prog
)
if
(
!
prog
)
...
@@ -253,9 +253,9 @@ int bpf_program_append_device(struct bpf_program *prog, struct device_item *devi
...
@@ -253,9 +253,9 @@ int bpf_program_append_device(struct bpf_program *prog, struct device_item *devi
if
(
!
bpf_device_all_access
(
access_mask
))
{
if
(
!
bpf_device_all_access
(
access_mask
))
{
struct
bpf_insn
ins
[]
=
{
struct
bpf_insn
ins
[]
=
{
BPF_MOV32_REG
(
BPF_REG_1
,
BPF_REG_3
),
BPF_MOV32_REG
(
BPF_REG_1
,
BPF_REG_3
),
BPF_ALU32_IMM
(
BPF_AND
,
BPF_REG_1
,
access_mask
),
BPF_ALU32_IMM
(
BPF_AND
,
BPF_REG_1
,
access_mask
),
BPF_JMP_REG
(
BPF_JNE
,
BPF_REG_1
,
BPF_REG_3
,
jump_nr
--
),
BPF_JMP_REG
(
BPF_JNE
,
BPF_REG_1
,
BPF_REG_3
,
jump_nr
--
),
};
};
ret
=
bpf_program_add_instructions
(
prog
,
ins
,
ARRAY_SIZE
(
ins
));
ret
=
bpf_program_add_instructions
(
prog
,
ins
,
ARRAY_SIZE
(
ins
));
...
@@ -265,7 +265,7 @@ int bpf_program_append_device(struct bpf_program *prog, struct device_item *devi
...
@@ -265,7 +265,7 @@ int bpf_program_append_device(struct bpf_program *prog, struct device_item *devi
if
(
device_type
>
0
)
{
if
(
device_type
>
0
)
{
struct
bpf_insn
ins
[]
=
{
struct
bpf_insn
ins
[]
=
{
BPF_JMP_IMM
(
BPF_JNE
,
BPF_REG_2
,
device_type
,
jump_nr
--
),
BPF_JMP_IMM
(
BPF_JNE
,
BPF_REG_2
,
device_type
,
jump_nr
--
),
};
};
ret
=
bpf_program_add_instructions
(
prog
,
ins
,
ARRAY_SIZE
(
ins
));
ret
=
bpf_program_add_instructions
(
prog
,
ins
,
ARRAY_SIZE
(
ins
));
...
@@ -275,7 +275,7 @@ int bpf_program_append_device(struct bpf_program *prog, struct device_item *devi
...
@@ -275,7 +275,7 @@ int bpf_program_append_device(struct bpf_program *prog, struct device_item *devi
if
(
device
->
major
>=
0
)
{
if
(
device
->
major
>=
0
)
{
struct
bpf_insn
ins
[]
=
{
struct
bpf_insn
ins
[]
=
{
BPF_JMP_IMM
(
BPF_JNE
,
BPF_REG_4
,
device
->
major
,
jump_nr
--
),
BPF_JMP_IMM
(
BPF_JNE
,
BPF_REG_4
,
device
->
major
,
jump_nr
--
),
};
};
ret
=
bpf_program_add_instructions
(
prog
,
ins
,
ARRAY_SIZE
(
ins
));
ret
=
bpf_program_add_instructions
(
prog
,
ins
,
ARRAY_SIZE
(
ins
));
...
@@ -285,7 +285,7 @@ int bpf_program_append_device(struct bpf_program *prog, struct device_item *devi
...
@@ -285,7 +285,7 @@ int bpf_program_append_device(struct bpf_program *prog, struct device_item *devi
if
(
device
->
minor
>=
0
)
{
if
(
device
->
minor
>=
0
)
{
struct
bpf_insn
ins
[]
=
{
struct
bpf_insn
ins
[]
=
{
BPF_JMP_IMM
(
BPF_JNE
,
BPF_REG_5
,
device
->
minor
,
jump_nr
--
),
BPF_JMP_IMM
(
BPF_JNE
,
BPF_REG_5
,
device
->
minor
,
jump_nr
--
),
};
};
ret
=
bpf_program_add_instructions
(
prog
,
ins
,
ARRAY_SIZE
(
ins
));
ret
=
bpf_program_add_instructions
(
prog
,
ins
,
ARRAY_SIZE
(
ins
));
...
@@ -323,7 +323,7 @@ int bpf_program_finalize(struct bpf_program *prog)
...
@@ -323,7 +323,7 @@ int bpf_program_finalize(struct bpf_program *prog)
static
int
bpf_program_load_kernel
(
struct
bpf_program
*
prog
,
char
*
log_buf
,
static
int
bpf_program_load_kernel
(
struct
bpf_program
*
prog
,
char
*
log_buf
,
__u32
log_size
,
__u32
log_level
)
__u32
log_size
,
__u32
log_level
)
{
{
union
bpf_attr
attr
;
union
bpf_attr
*
attr
;
if
((
log_size
!=
0
&&
!
log_buf
)
||
(
log_size
==
0
&&
log_buf
))
if
((
log_size
!=
0
&&
!
log_buf
)
||
(
log_size
==
0
&&
log_buf
))
return
ret_errno
(
EINVAL
);
return
ret_errno
(
EINVAL
);
...
@@ -333,7 +333,7 @@ static int bpf_program_load_kernel(struct bpf_program *prog, char *log_buf,
...
@@ -333,7 +333,7 @@ static int bpf_program_load_kernel(struct bpf_program *prog, char *log_buf,
return
0
;
return
0
;
}
}
attr
=
(
union
bpf_attr
){
attr
=
&
(
union
bpf_attr
){
.
prog_type
=
prog
->
prog_type
,
.
prog_type
=
prog
->
prog_type
,
.
insns
=
PTR_TO_UINT64
(
prog
->
instructions
),
.
insns
=
PTR_TO_UINT64
(
prog
->
instructions
),
.
insn_cnt
=
prog
->
n_instructions
,
.
insn_cnt
=
prog
->
n_instructions
,
...
@@ -343,7 +343,7 @@ static int bpf_program_load_kernel(struct bpf_program *prog, char *log_buf,
...
@@ -343,7 +343,7 @@ static int bpf_program_load_kernel(struct bpf_program *prog, char *log_buf,
.
log_size
=
log_size
,
.
log_size
=
log_size
,
};
};
prog
->
kernel_fd
=
bpf
(
BPF_PROG_LOAD
,
&
attr
,
sizeof
(
attr
));
prog
->
kernel_fd
=
bpf
(
BPF_PROG_LOAD
,
attr
,
sizeof
(
*
attr
));
if
(
prog
->
kernel_fd
<
0
)
if
(
prog
->
kernel_fd
<
0
)
return
log_error_errno
(
-
1
,
errno
,
"Failed to load bpf program: %s"
,
return
log_error_errno
(
-
1
,
errno
,
"Failed to load bpf program: %s"
,
log_buf
?:
"(null)"
);
log_buf
?:
"(null)"
);
...
@@ -357,7 +357,7 @@ int bpf_program_cgroup_attach(struct bpf_program *prog, int type,
...
@@ -357,7 +357,7 @@ int bpf_program_cgroup_attach(struct bpf_program *prog, int type,
{
{
__do_close
int
fd
=
-
EBADF
;
__do_close
int
fd
=
-
EBADF
;
__do_free
char
*
copy
=
NULL
;
__do_free
char
*
copy
=
NULL
;
union
bpf_attr
attr
;
union
bpf_attr
*
attr
;
int
ret
;
int
ret
;
if
(
!
path
||
!
prog
)
if
(
!
path
||
!
prog
)
...
@@ -389,14 +389,14 @@ int bpf_program_cgroup_attach(struct bpf_program *prog, int type,
...
@@ -389,14 +389,14 @@ int bpf_program_cgroup_attach(struct bpf_program *prog, int type,
if
(
fd
<
0
)
if
(
fd
<
0
)
return
log_error_errno
(
-
1
,
errno
,
"Failed to open cgroup path %s"
,
path
);
return
log_error_errno
(
-
1
,
errno
,
"Failed to open cgroup path %s"
,
path
);
attr
=
(
union
bpf_attr
){
attr
=
&
(
union
bpf_attr
){
.
attach_type
=
type
,
.
attach_type
=
type
,
.
target_fd
=
fd
,
.
target_fd
=
fd
,
.
attach_bpf_fd
=
prog
->
kernel_fd
,
.
attach_bpf_fd
=
prog
->
kernel_fd
,
.
attach_flags
=
flags
,
.
attach_flags
=
flags
,
};
};
ret
=
bpf
(
BPF_PROG_ATTACH
,
&
attr
,
sizeof
(
attr
));
ret
=
bpf
(
BPF_PROG_ATTACH
,
attr
,
sizeof
(
*
attr
));
if
(
ret
<
0
)
if
(
ret
<
0
)
return
log_error_errno
(
-
1
,
errno
,
"Failed to attach bpf program"
);
return
log_error_errno
(
-
1
,
errno
,
"Failed to attach bpf program"
);
...
@@ -425,15 +425,15 @@ int bpf_program_cgroup_detach(struct bpf_program *prog)
...
@@ -425,15 +425,15 @@ int bpf_program_cgroup_detach(struct bpf_program *prog)
return
log_error_errno
(
-
1
,
errno
,
"Failed to open attach cgroup %s"
,
return
log_error_errno
(
-
1
,
errno
,
"Failed to open attach cgroup %s"
,
prog
->
attached_path
);
prog
->
attached_path
);
}
else
{
}
else
{
union
bpf_attr
attr
;
union
bpf_attr
*
attr
;
attr
=
(
union
bpf_attr
){
attr
=
&
(
union
bpf_attr
){
.
attach_type
=
prog
->
attached_type
,
.
attach_type
=
prog
->
attached_type
,
.
target_fd
=
fd
,
.
target_fd
=
fd
,
.
attach_bpf_fd
=
prog
->
kernel_fd
,
.
attach_bpf_fd
=
prog
->
kernel_fd
,
};
};
ret
=
bpf
(
BPF_PROG_DETACH
,
&
attr
,
sizeof
(
attr
));
ret
=
bpf
(
BPF_PROG_DETACH
,
attr
,
sizeof
(
*
attr
));
if
(
ret
<
0
)
if
(
ret
<
0
)
return
log_error_errno
(
-
1
,
errno
,
"Failed to detach bpf program from cgroup %s"
,
return
log_error_errno
(
-
1
,
errno
,
"Failed to detach bpf program from cgroup %s"
,
prog
->
attached_path
);
prog
->
attached_path
);
...
@@ -523,8 +523,8 @@ bool bpf_devices_cgroup_supported(void)
...
@@ -523,8 +523,8 @@ bool bpf_devices_cgroup_supported(void)
{
{
__do_bpf_program_free
struct
bpf_program
*
prog
=
NULL
;
__do_bpf_program_free
struct
bpf_program
*
prog
=
NULL
;
const
struct
bpf_insn
dummy
[]
=
{
const
struct
bpf_insn
dummy
[]
=
{
BPF_MOV64_IMM
(
BPF_REG_0
,
1
),
BPF_MOV64_IMM
(
BPF_REG_0
,
1
),
BPF_EXIT_INSN
(),
BPF_EXIT_INSN
(),
};
};
int
ret
;
int
ret
;
...
@@ -536,6 +536,10 @@ bool bpf_devices_cgroup_supported(void)
...
@@ -536,6 +536,10 @@ bool bpf_devices_cgroup_supported(void)
if
(
!
prog
)
if
(
!
prog
)
return
log_trace
(
false
,
"Failed to allocate new bpf device cgroup program"
);
return
log_trace
(
false
,
"Failed to allocate new bpf device cgroup program"
);
ret
=
bpf_program_init
(
prog
);
if
(
ret
)
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to initialize bpf program"
);
ret
=
bpf_program_add_instructions
(
prog
,
dummy
,
ARRAY_SIZE
(
dummy
));
ret
=
bpf_program_add_instructions
(
prog
,
dummy
,
ARRAY_SIZE
(
dummy
));
if
(
ret
<
0
)
if
(
ret
<
0
)
return
log_trace
(
false
,
"Failed to add new instructions to bpf device cgroup program"
);
return
log_trace
(
false
,
"Failed to add new instructions to bpf device cgroup program"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment