History

bpf: Add flags arg to bpf_dynptr_read and bpf_dynptr_write APIs

Commit 13bbbfbea759 ("bpf: Add bpf_dynptr_read and bpf_dynptr_write") added the bpf_dynptr_write() and bpf_dynptr_read() APIs. However, it will be needed for some dynptr types to pass in flags as well (e.g. when writing to a skb, the user may like to invalidate the hash or recompute the checksum). This patch adds a "u64 flags" arg to the bpf_dynptr_read() and bpf_dynptr_write() APIs before their UAPI signature freezes where we then cannot change them anymore with a 5.19.x released kernel.

Joanne Koong committed 4 years ago

Commit f8d3da41 parent 03261955 files · +29 −19Browse files

Unified · Split

5 files
  1. include/uapi/linux/bpf.h +7 −4
  2. kernel/bpf/helpers.c +8 −4
  3. tools/include/uapi/linux/bpf.h +7 −4
  4. tools/testing/selftests/bpf/progs/dynptr_fail.c +5 −5
  5. tools/testing/selftests/bpf/progs/dynptr_success.c +2 −2
include/uapi/linux/bpf.h+7−4
@@ -5222,22 +5222,25 @@ union bpf_attr { · 5221 unmodified lines
52225222 * Return
52235223 * Nothing. Always succeeds.
52245224 *
5225− * long bpf_dynptr_read(void *dst, u32 len, struct bpf_dynptr *src, u32 offset)
5225+ * long bpf_dynptr_read(void *dst, u32 len, struct bpf_dynptr *src, u32 offset, u64 flags)
52265226 * Description
52275227 * Read *len* bytes from *src* into *dst*, starting from *offset*
52285228 * into *src*.
5229+ * *flags* is currently unused.
52295230 * Return
52305231 * 0 on success, -E2BIG if *offset* + *len* exceeds the length
5231− * of *src*'s data, -EINVAL if *src* is an invalid dynptr.
5232+ * of *src*'s data, -EINVAL if *src* is an invalid dynptr or if
5233+ * *flags* is not 0.
52325234 *
5233− * long bpf_dynptr_write(struct bpf_dynptr *dst, u32 offset, void *src, u32 len)
5235+ * long bpf_dynptr_write(struct bpf_dynptr *dst, u32 offset, void *src, u32 len, u64 flags)
52345236 * Description
52355237 * Write *len* bytes from *src* into *dst*, starting from *offset*
52365238 * into *dst*.
5239+ * *flags* is currently unused.
52375240 * Return
52385241 * 0 on success, -E2BIG if *offset* + *len* exceeds the length
52395242 * of *dst*'s data, -EINVAL if *dst* is an invalid dynptr or if *dst*
5240− * is a read-only dynptr.
5243+ * is a read-only dynptr or if *flags* is not 0.
52415244 *
52425245 * void *bpf_dynptr_data(struct bpf_dynptr *ptr, u32 offset, u32 len)
52435246 * Description
kernel/bpf/helpers.c+8−4
@@ -1497,11 +1497,12 @@ const struct bpf_func_proto bpf_dynptr_from_mem_proto = { · 1496 unmodified lines
14971497 .arg4_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL | MEM_UNINIT,
14981498 };
14991499
1500−BPF_CALL_4(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src, u32, offset)
1500+BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src,
1501+ u32, offset, u64, flags)
15011502 {
15021503 int err;
15031504
1504− if (!src->data)
1505+ if (!src->data || flags)
15051506 return -EINVAL;
15061507
15071508 err = bpf_dynptr_check_off_len(src, offset, len);
@@ -1521,13 +1522,15 @@ const struct bpf_func_proto bpf_dynptr_read_proto = { · 13 unmodified lines
15211522 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
15221523 .arg3_type = ARG_PTR_TO_DYNPTR,
15231524 .arg4_type = ARG_ANYTHING,
1525+ .arg5_type = ARG_ANYTHING,
15241526 };
15251527
1526−BPF_CALL_4(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *, src, u32, len)
1528+BPF_CALL_5(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *, src,
1529+ u32, len, u64, flags)
15271530 {
15281531 int err;
15291532
1530− if (!dst->data || bpf_dynptr_is_rdonly(dst))
1533+ if (!dst->data || flags || bpf_dynptr_is_rdonly(dst))
15311534 return -EINVAL;
15321535
15331536 err = bpf_dynptr_check_off_len(dst, offset, len);
@@ -1547,6 +1550,7 @@ const struct bpf_func_proto bpf_dynptr_write_proto = { · 13 unmodified lines
15471550 .arg2_type = ARG_ANYTHING,
15481551 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
15491552 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1553+ .arg5_type = ARG_ANYTHING,
15501554 };
15511555
15521556 BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len)
tools/include/uapi/linux/bpf.h+7−4
@@ -5222,22 +5222,25 @@ union bpf_attr { · 5221 unmodified lines
52225222 * Return
52235223 * Nothing. Always succeeds.
52245224 *
5225− * long bpf_dynptr_read(void *dst, u32 len, struct bpf_dynptr *src, u32 offset)
5225+ * long bpf_dynptr_read(void *dst, u32 len, struct bpf_dynptr *src, u32 offset, u64 flags)
52265226 * Description
52275227 * Read *len* bytes from *src* into *dst*, starting from *offset*
52285228 * into *src*.
5229+ * *flags* is currently unused.
52295230 * Return
52305231 * 0 on success, -E2BIG if *offset* + *len* exceeds the length
5231− * of *src*'s data, -EINVAL if *src* is an invalid dynptr.
5232+ * of *src*'s data, -EINVAL if *src* is an invalid dynptr or if
5233+ * *flags* is not 0.
52325234 *
5233− * long bpf_dynptr_write(struct bpf_dynptr *dst, u32 offset, void *src, u32 len)
5235+ * long bpf_dynptr_write(struct bpf_dynptr *dst, u32 offset, void *src, u32 len, u64 flags)
52345236 * Description
52355237 * Write *len* bytes from *src* into *dst*, starting from *offset*
52365238 * into *dst*.
5239+ * *flags* is currently unused.
52375240 * Return
52385241 * 0 on success, -E2BIG if *offset* + *len* exceeds the length
52395242 * of *dst*'s data, -EINVAL if *dst* is an invalid dynptr or if *dst*
5240− * is a read-only dynptr.
5243+ * is a read-only dynptr or if *flags* is not 0.
52415244 *
52425245 * void *bpf_dynptr_data(struct bpf_dynptr *ptr, u32 offset, u32 len)
52435246 * Description
tools/testing/selftests/bpf/progs/dynptr_fail.c+5−5
@@ -140,12 +140,12 @@ int use_after_invalid(void *ctx) · 139 unmodified lines
140140
141141 bpf_ringbuf_reserve_dynptr(&ringbuf, sizeof(read_data), 0, &ptr);
142142
143− bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0);
143+ bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0, 0);
144144
145145 bpf_ringbuf_submit_dynptr(&ptr, 0);
146146
147147 /* this should fail */
148− bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0);
148+ bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0, 0);
149149
150150 return 0;
151151 }
@@ -338,7 +338,7 @@ int invalid_helper2(void *ctx) · 186 unmodified lines
338338 get_map_val_dynptr(&ptr);
339339
340340 /* this should fail */
341− bpf_dynptr_read(read_data, sizeof(read_data), (void *)&ptr + 8, 0);
341+ bpf_dynptr_read(read_data, sizeof(read_data), (void *)&ptr + 8, 0, 0);
342342
343343 return 0;
344344 }
@@ -377,7 +377,7 @@ int invalid_write2(void *ctx) · 32 unmodified lines
377377 memcpy((void *)&ptr + 8, &x, sizeof(x));
378378
379379 /* this should fail */
380− bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0);
380+ bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0, 0);
381381
382382 bpf_ringbuf_submit_dynptr(&ptr, 0);
383383
@@ -473,7 +473,7 @@ int invalid_read2(void *ctx) · 89 unmodified lines
473473 get_map_val_dynptr(&ptr);
474474
475475 /* this should fail */
476− bpf_dynptr_read(read_data, sizeof(read_data), (void *)&ptr + 1, 0);
476+ bpf_dynptr_read(read_data, sizeof(read_data), (void *)&ptr + 1, 0, 0);
477477
478478 return 0;
479479 }
tools/testing/selftests/bpf/progs/dynptr_success.c+2−2
@@ -43,10 +43,10 @@ int test_read_write(void *ctx) · 42 unmodified lines
4343 bpf_ringbuf_reserve_dynptr(&ringbuf, sizeof(write_data), 0, &ptr);
4444
4545 /* Write data into the dynptr */
46− err = err ?: bpf_dynptr_write(&ptr, 0, write_data, sizeof(write_data));
46+ err = bpf_dynptr_write(&ptr, 0, write_data, sizeof(write_data), 0);
4747
4848 /* Read the data that was written into the dynptr */
49− err = err ?: bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0);
49+ err = err ?: bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0, 0);
5050
5151 /* Ensure the data we read matches the data we wrote */
5252 for (i = 0; i < sizeof(read_data); i++) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t