Skip to content

Commit 7dd68b3

Browse files
rdnaAlexei Starovoitov
authored andcommitted
bpf: Support replacing cgroup-bpf program in MULTI mode
The common use-case in production is to have multiple cgroup-bpf programs per attach type that cover multiple use-cases. Such programs are attached with BPF_F_ALLOW_MULTI and can be maintained by different people. Order of programs usually matters, for example imagine two egress programs: the first one drops packets and the second one counts packets. If they're swapped the result of counting program will be different. It brings operational challenges with updating cgroup-bpf program(s) attached with BPF_F_ALLOW_MULTI since there is no way to replace a program: * One way to update is to detach all programs first and then attach the new version(s) again in the right order. This introduces an interruption in the work a program is doing and may not be acceptable (e.g. if it's egress firewall); * Another way is attach the new version of a program first and only then detach the old version. This introduces the time interval when two versions of same program are working, what may not be acceptable if a program is not idempotent. It also imposes additional burden on program developers to make sure that two versions of their program can co-exist. Solve the problem by introducing a "replace" mode in BPF_PROG_ATTACH command for cgroup-bpf programs being attached with BPF_F_ALLOW_MULTI flag. This mode is enabled by newly introduced BPF_F_REPLACE attach flag and bpf_attr.replace_bpf_fd attribute to pass fd of the old program to replace That way user can replace any program among those attached with BPF_F_ALLOW_MULTI flag without the problems described above. Details of the new API: * If BPF_F_REPLACE is set but replace_bpf_fd doesn't have valid descriptor of BPF program, BPF_PROG_ATTACH will return corresponding error (EINVAL or EBADF). * If replace_bpf_fd has valid descriptor of BPF program but such a program is not attached to specified cgroup, BPF_PROG_ATTACH will return ENOENT. BPF_F_REPLACE is introduced to make the user intent clear, since replace_bpf_fd alone can't be used for this (its default value, 0, is a valid fd). BPF_F_REPLACE also makes it possible to extend the API in the future (e.g. add BPF_F_BEFORE and BPF_F_AFTER if needed). Signed-off-by: Andrey Ignatov <rdna@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Martin KaFai Lau <kafai@fb.com> Acked-by: Andrii Narkyiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/30cd850044a0057bdfcaaf154b7d2f39850ba813.1576741281.git.rdna@fb.com
1 parent 9fab329 commit 7dd68b3

6 files changed

Lines changed: 54 additions & 9 deletions

File tree

‎include/linux/bpf-cgroup.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ int cgroup_bpf_inherit(struct cgroup *cgrp);
8585
void cgroup_bpf_offline(struct cgroup *cgrp);
8686

8787
int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
88+
struct bpf_prog *replace_prog,
8889
enum bpf_attach_type type, u32 flags);
8990
int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
9091
enum bpf_attach_type type);
@@ -93,7 +94,8 @@ int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
9394

9495
/* Wrapper for __cgroup_bpf_*() protected by cgroup_mutex */
9596
int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
96-
enum bpf_attach_type type, u32 flags);
97+
struct bpf_prog *replace_prog, enum bpf_attach_type type,
98+
u32 flags);
9799
int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
98100
enum bpf_attach_type type, u32 flags);
99101
int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,

‎include/uapi/linux/bpf.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ enum bpf_attach_type {
231231
* When children program makes decision (like picking TCP CA or sock bind)
232232
* parent program has a chance to override it.
233233
*
234+
* With BPF_F_ALLOW_MULTI a new program is added to the end of the list of
235+
* programs for a cgroup. Though it's possible to replace an old program at
236+
* any position by also specifying BPF_F_REPLACE flag and position itself in
237+
* replace_bpf_fd attribute. Old program at this position will be released.
238+
*
234239
* A cgroup with MULTI or OVERRIDE flag allows any attach flags in sub-cgroups.
235240
* A cgroup with NONE doesn't allow any programs in sub-cgroups.
236241
* Ex1:
@@ -249,6 +254,7 @@ enum bpf_attach_type {
249254
*/
250255
#define BPF_F_ALLOW_OVERRIDE (1U << 0)
251256
#define BPF_F_ALLOW_MULTI (1U << 1)
257+
#define BPF_F_REPLACE (1U << 2)
252258

253259
/* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the
254260
* verifier will perform strict alignment checking as if the kernel
@@ -442,6 +448,10 @@ union bpf_attr {
442448
__u32 attach_bpf_fd; /* eBPF program to attach */
443449
__u32 attach_type;
444450
__u32 attach_flags;
451+
__u32 replace_bpf_fd; /* previously attached eBPF
452+
* program to replace if
453+
* BPF_F_REPLACE is used
454+
*/
445455
};
446456

447457
struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */

‎kernel/bpf/cgroup.c‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,17 @@ static int update_effective_progs(struct cgroup *cgrp,
282282
* propagate the change to descendants
283283
* @cgrp: The cgroup which descendants to traverse
284284
* @prog: A program to attach
285+
* @replace_prog: Previously attached program to replace if BPF_F_REPLACE is set
285286
* @type: Type of attach operation
286287
* @flags: Option flags
287288
*
288289
* Must be called with cgroup_mutex held.
289290
*/
290291
int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
292+
struct bpf_prog *replace_prog,
291293
enum bpf_attach_type type, u32 flags)
292294
{
295+
u32 saved_flags = (flags & (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI));
293296
struct list_head *progs = &cgrp->bpf.progs[type];
294297
struct bpf_prog *old_prog = NULL;
295298
struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE],
@@ -298,14 +301,15 @@ int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
298301
enum bpf_cgroup_storage_type stype;
299302
int err;
300303

301-
if ((flags & BPF_F_ALLOW_OVERRIDE) && (flags & BPF_F_ALLOW_MULTI))
304+
if (((flags & BPF_F_ALLOW_OVERRIDE) && (flags & BPF_F_ALLOW_MULTI)) ||
305+
((flags & BPF_F_REPLACE) && !(flags & BPF_F_ALLOW_MULTI)))
302306
/* invalid combination */
303307
return -EINVAL;
304308

305309
if (!hierarchy_allows_attach(cgrp, type))
306310
return -EPERM;
307311

308-
if (!list_empty(progs) && cgrp->bpf.flags[type] != flags)
312+
if (!list_empty(progs) && cgrp->bpf.flags[type] != saved_flags)
309313
/* Disallow attaching non-overridable on top
310314
* of existing overridable in this cgroup.
311315
* Disallow attaching multi-prog if overridable or none
@@ -320,7 +324,12 @@ int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
320324
if (pl->prog == prog)
321325
/* disallow attaching the same prog twice */
322326
return -EINVAL;
327+
if (pl->prog == replace_prog)
328+
replace_pl = pl;
323329
}
330+
if ((flags & BPF_F_REPLACE) && !replace_pl)
331+
/* prog to replace not found for cgroup */
332+
return -ENOENT;
324333
} else if (!list_empty(progs)) {
325334
replace_pl = list_first_entry(progs, typeof(*pl), node);
326335
}
@@ -356,7 +365,7 @@ int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
356365
for_each_cgroup_storage_type(stype)
357366
pl->storage[stype] = storage[stype];
358367

359-
cgrp->bpf.flags[type] = flags;
368+
cgrp->bpf.flags[type] = saved_flags;
360369

361370
err = update_effective_progs(cgrp, type);
362371
if (err)
@@ -522,15 +531,28 @@ int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
522531
int cgroup_bpf_prog_attach(const union bpf_attr *attr,
523532
enum bpf_prog_type ptype, struct bpf_prog *prog)
524533
{
534+
struct bpf_prog *replace_prog = NULL;
525535
struct cgroup *cgrp;
526536
int ret;
527537

528538
cgrp = cgroup_get_from_fd(attr->target_fd);
529539
if (IS_ERR(cgrp))
530540
return PTR_ERR(cgrp);
531541

532-
ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
542+
if ((attr->attach_flags & BPF_F_ALLOW_MULTI) &&
543+
(attr->attach_flags & BPF_F_REPLACE)) {
544+
replace_prog = bpf_prog_get_type(attr->replace_bpf_fd, ptype);
545+
if (IS_ERR(replace_prog)) {
546+
cgroup_put(cgrp);
547+
return PTR_ERR(replace_prog);
548+
}
549+
}
550+
551+
ret = cgroup_bpf_attach(cgrp, prog, replace_prog, attr->attach_type,
533552
attr->attach_flags);
553+
554+
if (replace_prog)
555+
bpf_prog_put(replace_prog);
534556
cgroup_put(cgrp);
535557
return ret;
536558
}

‎kernel/bpf/syscall.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,10 +2073,10 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
20732073
}
20742074
}
20752075

2076-
#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
2076+
#define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
20772077

20782078
#define BPF_F_ATTACH_MASK \
2079-
(BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
2079+
(BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
20802080

20812081
static int bpf_prog_attach(const union bpf_attr *attr)
20822082
{

‎kernel/cgroup/cgroup.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6288,12 +6288,13 @@ void cgroup_sk_free(struct sock_cgroup_data *skcd)
62886288

62896289
#ifdef CONFIG_CGROUP_BPF
62906290
int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
6291-
enum bpf_attach_type type, u32 flags)
6291+
struct bpf_prog *replace_prog, enum bpf_attach_type type,
6292+
u32 flags)
62926293
{
62936294
int ret;
62946295

62956296
mutex_lock(&cgroup_mutex);
6296-
ret = __cgroup_bpf_attach(cgrp, prog, type, flags);
6297+
ret = __cgroup_bpf_attach(cgrp, prog, replace_prog, type, flags);
62976298
mutex_unlock(&cgroup_mutex);
62986299
return ret;
62996300
}

‎tools/include/uapi/linux/bpf.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ enum bpf_attach_type {
231231
* When children program makes decision (like picking TCP CA or sock bind)
232232
* parent program has a chance to override it.
233233
*
234+
* With BPF_F_ALLOW_MULTI a new program is added to the end of the list of
235+
* programs for a cgroup. Though it's possible to replace an old program at
236+
* any position by also specifying BPF_F_REPLACE flag and position itself in
237+
* replace_bpf_fd attribute. Old program at this position will be released.
238+
*
234239
* A cgroup with MULTI or OVERRIDE flag allows any attach flags in sub-cgroups.
235240
* A cgroup with NONE doesn't allow any programs in sub-cgroups.
236241
* Ex1:
@@ -249,6 +254,7 @@ enum bpf_attach_type {
249254
*/
250255
#define BPF_F_ALLOW_OVERRIDE (1U << 0)
251256
#define BPF_F_ALLOW_MULTI (1U << 1)
257+
#define BPF_F_REPLACE (1U << 2)
252258

253259
/* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the
254260
* verifier will perform strict alignment checking as if the kernel
@@ -442,6 +448,10 @@ union bpf_attr {
442448
__u32 attach_bpf_fd; /* eBPF program to attach */
443449
__u32 attach_type;
444450
__u32 attach_flags;
451+
__u32 replace_bpf_fd; /* previously attached eBPF
452+
* program to replace if
453+
* BPF_F_REPLACE is used
454+
*/
445455
};
446456

447457
struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */

0 commit comments

Comments
 (0)