Commit fa832fc1 authored by Hou Tao's avatar Hou Tao Committed by Tengda Wu
Browse files

bpf: Handle BPF_EXIST and BPF_NOEXIST for LPM trie

stable inclusion
from stable-v5.10.231
commit adb4a970bfa96dd4bfc4e623a07e3dbefc3c8aab
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/IBFB6E

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=adb4a970bfa96dd4bfc4e623a07e3dbefc3c8aab



--------------------------------

[ Upstream commit eae6a075e9537dd69891cf77ca5a88fa8a28b4a1 ]

Add the currently missing handling for the BPF_EXIST and BPF_NOEXIST
flags. These flags can be specified by users and are relevant since LPM
trie supports exact matches during update.

Fixes: b95a5c4d ("bpf: add a longest prefix match trie map implementation")
Reviewed-by: default avatarToke Høiland-Jørgensen <toke@redhat.com>
Acked-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Signed-off-by: default avatarHou Tao <houtao1@huawei.com>
Link: https://lore.kernel.org/r/20241206110622.1161752-4-houtao@huaweicloud.com


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
Signed-off-by: default avatarTengda Wu <wutengda2@huawei.com>
parent b5c230e2
Loading
Loading
Loading
Loading
+20 −3
Original line number Diff line number Diff line
@@ -364,6 +364,10 @@ static int trie_update_elem(struct bpf_map *map,
	 * simply assign the @new_node to that slot and be done.
	 */
	if (!node) {
		if (flags == BPF_EXIST) {
			ret = -ENOENT;
			goto out;
		}
		rcu_assign_pointer(*slot, new_node);
		goto out;
	}
@@ -372,18 +376,31 @@ static int trie_update_elem(struct bpf_map *map,
	 * which already has the correct data array set.
	 */
	if (node->prefixlen == matchlen) {
		if (!(node->flags & LPM_TREE_NODE_FLAG_IM)) {
			if (flags == BPF_NOEXIST) {
				ret = -EEXIST;
				goto out;
			}
			trie->n_entries--;
		} else if (flags == BPF_EXIST) {
			ret = -ENOENT;
			goto out;
		}

		new_node->child[0] = node->child[0];
		new_node->child[1] = node->child[1];

		if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
			trie->n_entries--;

		rcu_assign_pointer(*slot, new_node);
		kfree_rcu(node, rcu);

		goto out;
	}

	if (flags == BPF_EXIST) {
		ret = -ENOENT;
		goto out;
	}

	/* If the new node matches the prefix completely, it must be inserted
	 * as an ancestor. Simply insert it between @node and *@slot.
	 */