Skip to content

bpf: Expand the usage scenarios of bpf_kptr_xchg

Chengkaitao bpf linux-kselftest lkml 9 versions – Applied

Status from this message: This series was applied to bpf/bpf-next.git (master)

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v7 Latest posting 2/25 patches 0 repliesApplied 5/5
  2. v7 posting 1/25 patches 0 repliesSuperseded
  3. v65 patches 1 replySuperseded
Earlier versions · 6
VersionPostedPatchesRepliesStatus
v53 patches1 replySuperseded
v43 patches3 repliesSuperseded
v3 posting 2/23 patches0 repliesSuperseded
v3 posting 1/22 of 3 patches0 repliesSuperseded
v23 patches1 replySuperseded
v11 patch1 replySuperseded

Changes in v7 from the cover letter

- Add a comma to the variable declaration in enum bpf_reg_type
- Modify the prefixes

Patches in v7 posting 2/2 All 5 applied

#Subject and filesSizeRepliesApplied
1/5bpf: allow calling bpf_kptr_xchg while holding a lockkernel/bpf/verifier.c+2 -10 repliesApplied
2/5bpf: allow using bpf_kptr_xchg even if the NON_OWN_REF flag is setkernel/bpf/verifier.c+3 -10 repliesApplied
3/5selftests/bpf: Add supplementary tests for bpf_kptr_xchgtools/testing/selftests/bpf/prog_tests/rbtree.c tools/testing/selftests/bpf/progs/bpf_misc.h tools/testing/selftests/bpf/progs/rbtree_search_kptr.c+177 -00 repliesApplied
4/5bpf: allow using bpf_kptr_xchg even if the MEM_RCU flag is setkernel/bpf/verifier.c+2 -00 repliesApplied
5/5selftests/bpf: Add test case for rbtree nodes that contain both bpf_refcount and kptr fields.tools/testing/selftests/bpf/progs/rbtree_search_kptr.c+123 -00 repliesApplied

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v7 yet.

Read the v7 thread

Cover letter [PATCH RESEND v7 0/5] bpf: Expand the usage scenarios of bpf_kptr_xchg

From: Kaitao Cheng <[email protected]>

When using bpf_kptr_xchg, we triggered the following error:
31: (85) call bpf_kptr_xchg#194
function calls are not allowed while holding a lock
bpf_kptr_xchg can now be used in lock-held contexts, so we extended its usage scope in [patch 1/5].

When writing test cases using bpf_kptr_xchg and bpf_rbtree_*, the following approach must be followed:

	bpf_spin_lock(&lock);
	rb_n = bpf_rbtree_root(&root);
	while (rb_n && can_loop) {
		rb_n = bpf_rbtree_remove(&root, rb_n);
		if (!rb_n)
			goto fail;

		tnode = container_of(rb_n, struct tree_node, node);
		node_data = bpf_kptr_xchg(&tnode->node_data, NULL);
		if (!node_data)
			goto fail;

		data = node_data->data;
		/* use data to do something */

		node_data = bpf_kptr_xchg(&tnode->node_data, node_data);
		if (node_data)
			goto fail;

		bpf_rbtree_add(&root, rb_n, less);

		if (lookup_key < tnode->key)
			rb_n = bpf_rbtree_left(&root, rb_n);
		else
			rb_n = bpf_rbtree_right(&root, rb_n);
	}
	bpf_spin_unlock(&lock);

The above illustrates a lock-remove-read-add-unlock workflow, which exhibits lower performance. To address this, we introduced support for a streamlined lock-read-unlock operation in [patch 2/5] and [patch 4/5].

Changes in v7:
- Add a comma to the variable declaration in enum bpf_reg_type
- Modify the prefixes
Changes in v6:
- allow using bpf_kptr_xchg even if the MEM_RCU flag is set
- Add test case
Changes in v5:
- add lastname
Changes in v4:
- Fix the dead logic issue in the test case
Changes in v3:
- Fix compilation errors
Changes in v2:
- Allow using bpf_kptr_xchg even if the NON_OWN_REF flag is set
- Add test case

Link to v6:
https://lore.kernel.org/all/[email protected]/
Link to v5:
https://lore.kernel.org/all/[email protected]/
Link to v4:
https://lore.kernel.org/all/[email protected]/
Link to V3:
https://lore.kernel.org/all/[email protected]/
Link to V2:
https://lore.kernel.org/all/[email protected]/
Link to V1:
https://lore.kernel.org/all/[email protected]/

Kaitao Cheng (5):
bpf: allow calling bpf_kptr_xchg while holding a lock
bpf: allow using bpf_kptr_xchg even if the NON_OWN_REF flag is set selftests/bpf: Add supplementary tests for bpf_kptr_xchg
bpf: allow using bpf_kptr_xchg even if the MEM_RCU flag is set selftests/bpf: Add test case for rbtree nodes that contain both
bpf_refcount and kptr fields.

 kernel/bpf/verifier.c                         |   9 +-
 .../testing/selftests/bpf/prog_tests/rbtree.c |   6 +
 tools/testing/selftests/bpf/progs/bpf_misc.h  |   4 +
 .../selftests/bpf/progs/rbtree_search_kptr.c  | 290 ++++++++++++++++++
 4 files changed, 307 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/rbtree_search_kptr.c
Signature · 1 line
2.50.1 (Apple Git-155)