From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F5C03FA5EC for ; Fri, 24 Apr 2026 19:51:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777060291; cv=none; b=ub++OIInKcdTZFOBn4rPWv7FrQJcJe2Pr6wEDjtfJ+AlgtpVbGk99JsLteaxaqdUCWqjatzfMLpKnqyo8yyWzmOYah0ennA56u9VAeknMAx3IzbIYRRUEAjK0y5xLjxG+mbnimdQmxMhBdN/gwg2mtHFTjkCFY4BgAdduVpJxAs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777060291; c=relaxed/simple; bh=E6AyI7qS2XC8iXiqTyAec4DWAXlCknTXKXcPSh+UcZ4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lhHUP+X4zz3mzHsgzqw/b24wq7n8HGBf1afHCJIxPEF17WLxp9X1PB8oyXTw1jtMkdEW0Dx6IzPfuqPp0IYiDlKNHCAGD0NyuD296QOY9Q8/mtV6DHTT4C4fzBnUPt8PgPKjxou9k1bFkAV8UCJhvZbtvP1xoNYywXfC29tE064= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=B2N7MypT; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="B2N7MypT" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-488e1a8ac40so100670805e9.2 for ; Fri, 24 Apr 2026 12:51:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777060276; x=1777665076; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=aMa51JlC3bUxpWwEwiwmW8nqNbn1OqVFvkx+nRJ3BCg=; b=B2N7MypTqP98ZjXbF4MarifcBLkmOVeYu3XgETL17bIMBb0ypH+tibWu9O7ahuMS+s QBW5h7fmZOClf0eW08Ip/ch5J7qp5/0tzheVV9xX68T7jgMwdARlYbXirSgeF4NMis+j 4TNkSCpQ2++nTKMZVcU0VQYepoQGHSdlnXQj5J5zodrfDems74l2OiUkw8RpdV+eib6T xl+KWjGa23l0hHR4EYOiVRqMfq8TknZmPkpRiogTdpkWAYTwB9ocSRa6KWgwD2m21LjH JlAfhmb/Ee0LYEz+cx+6Czo2GWrS5UgOYlZUYl58TWriFb3Ym6g6zhXphF4d7DAkoFmD WjrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777060276; x=1777665076; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=aMa51JlC3bUxpWwEwiwmW8nqNbn1OqVFvkx+nRJ3BCg=; b=YzjAQpwmUtktlPDxghfftoekQU71jvRJhFZ/KviOqk+5hT9A4srLTN8+xCrX4/qRMS r/Z2dChBv543SKiNtFBrLHMephiYZa6cJNm/oUs9NzddoktRtRWbFflU4PSDtB8zLgPe CqtYGx/JQWsciQIM93rQxtdzmiwNBJYl+V+46QBafKs925mUhFRRizWu+xJuTy2w4VxD lr4uS0GWqRyhvM6z6vyKTrxzM6EBWG7Omkrit9cdGqG4wPkfJhM21cz+9z0WmNBBIVWa aAa+wkJuNulT8bcNKQNOc0siQzM4CbiS+81M2e7h+ixVTmRaNvP8jc92GUu79OV/kzrO kJhw== X-Gm-Message-State: AOJu0YyHFMJEHfrlmnFbJLmkLa0j9j2DA0FpnR7788/HKFQ5Z0FZQNz5 4cy5MHL/F7X0FiFtQ16oEG6GXQTzQZ6/qa6rnRxWIxK3RWU7iq5nDdwD X-Gm-Gg: AeBDieu/6ULF42szTFLHleeAcjt9fI3SCk1hnREoNZAXh6djYtgX8Wh70JCIvo3zMv1 KvGNAuCo/biwIcbfCtRfbye4iXIWnwTRRY6cOpf88lQXAZFAWVzm5492C/LTsvTEV2atUCgpJNG VjpkftPATYBFfDcIXY4ThFX4uqmpl8KYe2xisPkWU2LLmAqKtH/8NyfJn/EGeeYstrnDwVxjina HaxTAsc9JCQ25mvBYjOrkhTImzO0P554Ht4Wv0nDXpZDa7Wo/iYFBq7g//S/+YG0pBt9i6bqN4Y 3O9XrYMRcINAj19EIBymRPa/wkcfnxpCozesPHzn5o8Imor/GIXC8An7LVnqELQkMmMwmn7vv0p lvTTvD4BmxTTZA3qD3ej5YATlIZKhVD2Ox6vaveRO5h4OdnvWkJJ62MU3KDIZz9Rr9YlFHXgGUc GiChkzGfW99ALnKlD40mMwk8I= X-Received: by 2002:a05:600c:1616:b0:489:1cd2:6106 with SMTP id 5b1f17b1804b1-4891cd261f1mr190913885e9.6.1777060275547; Fri, 24 Apr 2026 12:51:15 -0700 (PDT) Received: from localhost ([2a03:2880:30ff:3::]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43fe4e4d525sm58960210f8f.31.2026.04.24.12.51.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Apr 2026 12:51:15 -0700 (PDT) From: Mykyta Yatsenko Date: Fri, 24 Apr 2026 12:50:49 -0700 Subject: [PATCH bpf-next v3 07/10] selftests/bpf: Add basic tests for resizable hash map Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260424-rhash-v3-7-d0fa0ce4379b@meta.com> References: <20260424-rhash-v3-0-d0fa0ce4379b@meta.com> In-Reply-To: <20260424-rhash-v3-0-d0fa0ce4379b@meta.com> To: bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net, kafai@meta.com, kernel-team@meta.com, eddyz87@gmail.com, memxor@gmail.com, herbert@gondor.apana.org.au Cc: Mykyta Yatsenko X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1777060253; l=7551; i=yatsenko@meta.com; s=20260324; h=from:subject:message-id; bh=UJg/12Zfr8jbhnEbcSjAJQj08z8HmWTUhVb5q5dKT9w=; b=ji1C53SsCmRM97yvcA35OgzB6cMctKFrMtjmlnDahpxGktO2MwkqAU3cuPfHBtQEQ9e9qaCOo amlpW00SMs8Boib1eAvLbs6Xn6C/iaQdQcM4wbWv5eOlU9kd7XJcL+T X-Developer-Key: i=yatsenko@meta.com; a=ed25519; pk=1zCUBXUa66KmzfjNsG8YNlMj2ckPdqBPvFq2ww3/YaA= From: Mykyta Yatsenko Test basic map operations (lookup, update, delete) for BPF_MAP_TYPE_RHASH including boundary conditions like duplicate key insertion and deletion of nonexistent keys. Signed-off-by: Mykyta Yatsenko --- tools/testing/selftests/bpf/prog_tests/rhash.c | 64 +++++++ tools/testing/selftests/bpf/progs/rhash.c | 239 +++++++++++++++++++++++++ 2 files changed, 303 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/rhash.c b/tools/testing/selftests/bpf/prog_tests/rhash.c new file mode 100644 index 000000000000..0f6eec4fe1fe --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/rhash.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ +#include +#include +#include +#include "rhash.skel.h" +#include +#include +#include + +static void rhash_run(const char *prog_name) +{ + struct rhash *skel; + struct bpf_program *prog; + LIBBPF_OPTS(bpf_test_run_opts, opts); + int err; + + skel = rhash__open(); + if (!ASSERT_OK_PTR(skel, "rhash__open")) + return; + + prog = bpf_object__find_program_by_name(skel->obj, prog_name); + if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name")) + goto cleanup; + bpf_program__set_autoload(prog, true); + + err = rhash__load(skel); + if (!ASSERT_OK(err, "skel_load")) + goto cleanup; + + err = bpf_prog_test_run_opts(bpf_program__fd(prog), &opts); + if (!ASSERT_OK(err, "prog run")) + goto cleanup; + + if (!ASSERT_OK(skel->bss->err, "bss->err")) + goto cleanup; + +cleanup: + rhash__destroy(skel); +} + +void test_rhash(void) +{ + if (test__start_subtest("test_rhash_lookup_update")) + rhash_run("test_rhash_lookup_update"); + + if (test__start_subtest("test_rhash_update_delete")) + rhash_run("test_rhash_update_delete"); + + if (test__start_subtest("test_rhash_update_elements")) + rhash_run("test_rhash_update_elements"); + + if (test__start_subtest("test_rhash_update_exist")) + rhash_run("test_rhash_update_exist"); + + if (test__start_subtest("test_rhash_update_any")) + rhash_run("test_rhash_update_any"); + + if (test__start_subtest("test_rhash_noexist_duplicate")) + rhash_run("test_rhash_noexist_duplicate"); + + if (test__start_subtest("test_rhash_delete_nonexistent")) + rhash_run("test_rhash_delete_nonexistent"); +} diff --git a/tools/testing/selftests/bpf/progs/rhash.c b/tools/testing/selftests/bpf/progs/rhash.c new file mode 100644 index 000000000000..9d066a22ce18 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/rhash.c @@ -0,0 +1,239 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ + +#include +#include +#include +#include +#include "bpf_misc.h" + +#define ENOENT 2 +#define EEXIST 17 + +char _license[] SEC("license") = "GPL"; + +int err; + +struct elem { + char arr[128]; + int val; +}; + +struct { + __uint(type, BPF_MAP_TYPE_RHASH); + __uint(map_flags, BPF_F_NO_PREALLOC); + __uint(max_entries, 128); + __type(key, int); + __type(value, struct elem); +} rhmap SEC(".maps"); + +SEC("syscall") +int test_rhash_lookup_update(void *ctx) +{ + int key = 5; + struct elem empty = {.val = 3, .arr = {0}}; + struct elem *e; + + err = 1; + e = bpf_map_lookup_elem(&rhmap, &key); + if (e) + return 1; + + err = bpf_map_update_elem(&rhmap, &key, &empty, BPF_NOEXIST); + if (err) + return 1; + + e = bpf_map_lookup_elem(&rhmap, &key); + if (e && e->val == empty.val) + err = 0; + + return 0; +} + +SEC("syscall") +int test_rhash_update_delete(void *ctx) +{ + int key = 6; + struct elem empty = {.val = 4, .arr = {0}}; + struct elem *e; + + err = 1; + e = bpf_map_lookup_elem(&rhmap, &key); + if (e) + return 1; + + err = bpf_map_update_elem(&rhmap, &key, &empty, BPF_NOEXIST); + if (err) + return 2; + + err = bpf_map_delete_elem(&rhmap, &key); + if (err) + return 3; + + e = bpf_map_lookup_elem(&rhmap, &key); + if (e) + return 4; + + err = 0; + return 0; +} + +SEC("syscall") +int test_rhash_update_elements(void *ctx) +{ + int key = 0; + struct elem empty = {.val = 4, .arr = {0}}; + struct elem *e; + int i; + + err = 1; + + for (i = 0; i < 128; ++i) { + key = i; + e = bpf_map_lookup_elem(&rhmap, &key); + if (e) + return 1; + + empty.val = key; + err = bpf_map_update_elem(&rhmap, &key, &empty, BPF_NOEXIST); + if (err) + return 2; + + e = bpf_map_lookup_elem(&rhmap, &key); + if (!e || e->val != key) + return 4; + } + + for (i = 0; i < 128; ++i) { + key = i; + err = bpf_map_delete_elem(&rhmap, &key); + if (err) + return 3; + + e = bpf_map_lookup_elem(&rhmap, &key); + if (e) + return 4; + } + + err = 0; + return 0; +} + +SEC("syscall") +int test_rhash_update_exist(void *ctx) +{ + int key = 10; + struct elem val1 = {.val = 100, .arr = {0}}; + struct elem val2 = {.val = 200, .arr = {0}}; + struct elem *e; + int ret; + + err = 1; + + /* BPF_EXIST on non-existent key should fail with -ENOENT */ + ret = bpf_map_update_elem(&rhmap, &key, &val1, BPF_EXIST); + if (ret != -ENOENT) + return 1; + + /* Insert element first */ + ret = bpf_map_update_elem(&rhmap, &key, &val1, BPF_NOEXIST); + if (ret) + return 2; + + /* Verify initial value */ + e = bpf_map_lookup_elem(&rhmap, &key); + if (!e || e->val != 100) + return 3; + + /* BPF_EXIST on existing key should succeed and update value */ + ret = bpf_map_update_elem(&rhmap, &key, &val2, BPF_EXIST); + if (ret) + return 4; + + /* Verify value was updated */ + e = bpf_map_lookup_elem(&rhmap, &key); + if (!e || e->val != 200) + return 5; + + /* Cleanup */ + bpf_map_delete_elem(&rhmap, &key); + err = 0; + return 0; +} + +SEC("syscall") +int test_rhash_update_any(void *ctx) +{ + int key = 11; + struct elem val1 = {.val = 111, .arr = {0}}; + struct elem val2 = {.val = 222, .arr = {0}}; + struct elem *e; + int ret; + + err = 1; + + /* BPF_ANY on non-existent key should insert */ + ret = bpf_map_update_elem(&rhmap, &key, &val1, BPF_ANY); + if (ret) + return 1; + + e = bpf_map_lookup_elem(&rhmap, &key); + if (!e || e->val != 111) + return 2; + + /* BPF_ANY on existing key should update */ + ret = bpf_map_update_elem(&rhmap, &key, &val2, BPF_ANY); + if (ret) + return 3; + + e = bpf_map_lookup_elem(&rhmap, &key); + if (!e || e->val != 222) + return 4; + + /* Cleanup */ + bpf_map_delete_elem(&rhmap, &key); + err = 0; + return 0; +} + +SEC("syscall") +int test_rhash_noexist_duplicate(void *ctx) +{ + int key = 12; + struct elem val = {.val = 600, .arr = {0}}; + int ret; + + err = 1; + + /* Insert element */ + ret = bpf_map_update_elem(&rhmap, &key, &val, BPF_NOEXIST); + if (ret) + return 1; + + /* Try to insert again with BPF_NOEXIST - should fail with -EEXIST */ + ret = bpf_map_update_elem(&rhmap, &key, &val, BPF_NOEXIST); + if (ret != -EEXIST) + return 2; + + /* Cleanup */ + bpf_map_delete_elem(&rhmap, &key); + err = 0; + return 0; +} + +SEC("syscall") +int test_rhash_delete_nonexistent(void *ctx) +{ + int key = 99999; + int ret; + + err = 1; + + /* Delete non-existent key should return -ENOENT */ + ret = bpf_map_delete_elem(&rhmap, &key); + if (ret != -ENOENT) + return 1; + + err = 0; + return 0; +} -- 2.52.0