From patchwork Fri Feb 28 00:33:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Tsalapatis X-Patchwork-Id: 13995419 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) (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 E20638BE7 for ; Fri, 28 Feb 2025 00:33:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740702826; cv=none; b=pwEJ25YN03H4l4HvlL8Duww6RAyQF4XaBVESYegoFCcw5RtTbGIwP2zYPe5NQKvIpDS7Azm/qo8LUJpyN7PmOfI1HFMj4fOIHd9JxwhJTNnzxOiVeeNPkiKGubl6Nno15u95OIvorx5Y87qdMQt+N0IadotPYJ1fjvdbY7QpzfM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740702826; c=relaxed/simple; bh=czd0CByd+8HifzhjxvYqMMnpTsWQfVduvuLxjch2RVs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sAOZNEbItMnekviDFphfuvpzpFgZeOOlsO5f9jSCuLldq9kP95Lj0ZhNIpF2BYTm0++KxBoTZcDrL/7X32v5eMlthaA3PD0sMFKKqwGodFvbMMyqbm7A+8JejUuF4c8Z0+l2wFavW6dS1o94JBj7XpY6LQwfLaMqT0hGNHxMa1Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=etsalapatis.com; spf=pass smtp.mailfrom=etsalapatis.com; dkim=pass (2048-bit key) header.d=etsalapatis-com.20230601.gappssmtp.com header.i=@etsalapatis-com.20230601.gappssmtp.com header.b=SLjOX+Et; arc=none smtp.client-ip=209.85.160.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=etsalapatis.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=etsalapatis.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=etsalapatis-com.20230601.gappssmtp.com header.i=@etsalapatis-com.20230601.gappssmtp.com header.b="SLjOX+Et" Received: by mail-qt1-f175.google.com with SMTP id d75a77b69052e-47208e35f9cso18490231cf.3 for ; Thu, 27 Feb 2025 16:33:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=etsalapatis-com.20230601.gappssmtp.com; s=20230601; t=1740702823; x=1741307623; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zIRIYcVb6AgPHTCemUirgFfxyD51WIA6BHz/E7hx2+4=; b=SLjOX+EtpMX17d5lwiBfoN7BAymP3D+Mf3azuaXzq1ORyVw/ts3FvU6Lm7yMBXiBds 0HdQNpfaJSRxJCLe/5e0u3NB35c/8HI5MzbWW9CI09fMmeVW4jvjven4xFu9TRFTpac0 YGg2vTD49FCcCE3JwzUUWIaBFiCfd8G8aSFkfwMwn3Q9AnPnp3669qEn5+l02kS7g6kR Wpuc3fZ9H1iCEg5V7BhiAAKrvfmOhK1rdClHuoO7IKJbxSCcrPiXjJwFRoUmDE0KK0+6 GIy74Z1CsTank6lUesUJLNYJPbXyyrS8kj+XvPjz03RgYDtI0Obn904o0GkPX2Sxl/WW 6Ziw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740702823; x=1741307623; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zIRIYcVb6AgPHTCemUirgFfxyD51WIA6BHz/E7hx2+4=; b=CUoamlHUeaLUXKy3BfX3axLDEaikGYmj+qP+juy5OePMIJ4zed9Lbq+vcQRatoU2yv vnPrO/tcLujt3fMMw4j7VDZL0iVqbVho2BaaEL0XD4E/lnX9+e5z/pEiW9PaMQaDTOzC 0gmVZW/V44qFUb/FH6yOJFVthYNNcu778xzzumRuUVsWnoPOL3bqLqja4R8sGQdn5JEm T0WhG42MSrRS+NlZvJpoDMEjlxykG4C/ebRj68lmPOXYP5mpkj+8SjIfdz0WGP/q4qUE kpk7X5gThUsrmJ0ntP335lPOJPvgH7PmwKvcL0oCiP0Jx2nw1Gr8lM7CDFasOPCAJbov sQoQ== X-Gm-Message-State: AOJu0YzMytEDnpTAI7/o0UonN6UcsQmr9BXLCYGGoa0PesB2aoYtLFKn C4/VSC3QlKlIffmYRWk+tbkO/3Ma/peiQ4OzXBoKtuCSSHj7vuOhUWYVBZ/jfejjt1NPdz1udPj xzlkvdQ== X-Gm-Gg: ASbGncsnZ3UiUIqaeKLQ8GR9efA7Lm2GTLYvVZBwfaynxZXUUC5ZzniFciLcI+W8CAn iWnBiGo1+rO+mOhixgdL/WhgR+O3+sl/3sLgVTI5D+8fhoCHnYrcTlqIDCCT75uJBzydkRZn+jG idZp7DZxnZ8HjrW/8WaTJNQsWVKgZfzabiZYvP2nTyzZOGjBXR4vXbxLiYjy+vTpLj20FEWPw5U jRW2O5R36GIuFqGTocDbm9LcfgDtBM2jy9wB7BDdeIPvqMNGewn3Pjgrk7ryIwAPYOlVLyxC7n8 nC6KJnzTmYr1r+EndAYiSTU= X-Google-Smtp-Source: AGHT+IFHH0Hz/Yf8EvRrkbuAKgCdqbbbyTkPViAx7buhpnsBfHPB+Jjb4wcDTSWu2zFrVOLM7VwbZw== X-Received: by 2002:ad4:5f0a:0:b0:6e6:5efa:4e01 with SMTP id 6a1803df08f44-6e8a0d04565mr23695806d6.20.1740702822704; Thu, 27 Feb 2025 16:33:42 -0800 (PST) Received: from boreas.. ([140.174.215.88]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7c36fef5beesm174769085a.32.2025.02.27.16.33.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Feb 2025 16:33:42 -0800 (PST) From: Emil Tsalapatis To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.de, eddyz87@gmail.com, yonghong.song@linux.dev, Emil Tsalapatis Subject: [PATCH 1/2] bpf: add kfunc for populating cpumask bits Date: Thu, 27 Feb 2025 19:33:20 -0500 Message-ID: <20250228003321.1409285-2-emil@etsalapatis.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250228003321.1409285-1-emil@etsalapatis.com> References: <20250228003321.1409285-1-emil@etsalapatis.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add a helper kfunc that sets the bitmap of a bpf_cpumask from BPF memory. Signed-off-by: Emil Tsalapatis (Meta) --- kernel/bpf/cpumask.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/kernel/bpf/cpumask.c b/kernel/bpf/cpumask.c index cfa1c18e3a48..a13839b3595f 100644 --- a/kernel/bpf/cpumask.c +++ b/kernel/bpf/cpumask.c @@ -420,6 +420,26 @@ __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) return cpumask_weight(cpumask); } +/** + * bpf_cpumask_fill() - Populate the CPU mask from the contents of + * a BPF memory region. + * + * @cpumask: The cpumask being populated. + * @src: The BPF memory holding the bit pattern. + * @src__sz: Length of the BPF memory region in bytes. + * + */ +__bpf_kfunc int bpf_cpumask_fill(struct cpumask *cpumask, void *src, size_t src__sz) +{ + /* The memory region must be large enough to populate the entire CPU mask. */ + if (src__sz < BITS_TO_BYTES(nr_cpu_ids)) + return -EACCES; + + bitmap_copy(cpumask_bits(cpumask), src, nr_cpu_ids); + + return 0; +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(cpumask_kfunc_btf_ids) @@ -448,6 +468,7 @@ BTF_ID_FLAGS(func, bpf_cpumask_copy, KF_RCU) BTF_ID_FLAGS(func, bpf_cpumask_any_distribute, KF_RCU) BTF_ID_FLAGS(func, bpf_cpumask_any_and_distribute, KF_RCU) BTF_ID_FLAGS(func, bpf_cpumask_weight, KF_RCU) +BTF_ID_FLAGS(func, bpf_cpumask_fill, KF_RCU) BTF_KFUNCS_END(cpumask_kfunc_btf_ids) static const struct btf_kfunc_id_set cpumask_kfunc_set = { From patchwork Fri Feb 28 00:33:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Tsalapatis X-Patchwork-Id: 13995421 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-qv1-f49.google.com (mail-qv1-f49.google.com [209.85.219.49]) (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 0E62C276D0E for ; Fri, 28 Feb 2025 00:33:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740702828; cv=none; b=qWuAHyniJshLoKDEMq6GFZ/ro+FrC/wPgwyxpBLGs0RVLy2mOM+uNMAyeeBa+iEIa023xvCGo/oUKpKKIyZGTGwRn6S4cfwjMM8OTkNONaDegCInjdfOfmnElZSfpElMN5m+571idJX3U/zM5Arp+vpvvHnj96cqVhRIVFJlbcE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740702828; c=relaxed/simple; bh=+tAf7u/TOny6/E/kZLhCXOeYEH6uKeF3f+9OtyqAX6I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YzkjCqEAc7HpS3OzLCAeY1hE23cLf2lVzD9EERJp42xR4AyZXkf//RyVfjdecjfyjrbh/GIGvUYIvNfTqgJzJOkG2+DjTKSwXzDdnRR536WUt5UzXgmGkiSGFyw4Dqbv2qG/9Yx3G7eGvKR5OShLSIRgAfIMbbo0mSrqjgfUaj0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=etsalapatis.com; spf=pass smtp.mailfrom=etsalapatis.com; dkim=pass (2048-bit key) header.d=etsalapatis-com.20230601.gappssmtp.com header.i=@etsalapatis-com.20230601.gappssmtp.com header.b=yOSagiNl; arc=none smtp.client-ip=209.85.219.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=etsalapatis.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=etsalapatis.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=etsalapatis-com.20230601.gappssmtp.com header.i=@etsalapatis-com.20230601.gappssmtp.com header.b="yOSagiNl" Received: by mail-qv1-f49.google.com with SMTP id 6a1803df08f44-6dd15d03eacso15707886d6.0 for ; Thu, 27 Feb 2025 16:33:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=etsalapatis-com.20230601.gappssmtp.com; s=20230601; t=1740702825; x=1741307625; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SIlQs9gMH3GKjLGlfRe+Sf9VX2opG0S7ZOahb6Lf2Ik=; b=yOSagiNlQWi6I8R4MTrs1TjDaUMPMRcdMaHssftglL3aoFjiXEFZkqrvcpsR6ypG6F /oqTnzCgu833k/2G7hfAUXcVZNnCSSKyC70SIS1T8hULMBBgr8ocRRWVzrki87E6SQS/ 4oIhvEWgOIFa72wUDyn4uKe4aX8JTFmUmaV7DukH2vRQASM2jygm+PEqXqpprTVTpE1s yS0VcFMOLuBYQ5FMEtHkq+x4kgdcyVFkrvg0rmCTqlooOP+vJaj8jpzw7QOpTPGk8/01 BwYWSRowmBP3q5rANbJEbHnkVed/5sncn9DO0Fg5e2Kuy9FtsGecRk0mrZUSD59wvsHT YImQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740702825; x=1741307625; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SIlQs9gMH3GKjLGlfRe+Sf9VX2opG0S7ZOahb6Lf2Ik=; b=qh8EoU5QEYMCB3Zap4E5yAPsjp/BTZklHHid1WZH3dzDxV+IQGOrCfgPkXW8kOfhjl 9iczKeFhzzylHhEWbJHyDcoTI21oFwqTM1zZ1uSaA4IjNkmRzbxu+s1fLji3JseSSlHW pCvs1PKY4jaNV5ZAAmJVfcx++B+lI6hZi6qzn/PndZu6/RvsZ6UmYs/Z4nRF6+BjFtc9 AO1ij4qS2UhSaivniIVmEWFc5z1Uwf5qly80TYLd7Eko/++6mqFq6hJUAychKijfPogX T9oSsBTfuyF9e3tAaxb9C1AbyBo9wDyK8SeYGMBhK1iO4Uiz+8/Bnl6bYbyjuFuxfNDm 4XNA== X-Gm-Message-State: AOJu0YxgdO8UqjkapzXDrShtkLcDW08X+KfQDK8xMtoZ9lUi8Z9QknUV 9NaW7MRxgPZTFYX8oGgyalWK6OHYyb1CFpOb8vfcxsK8Vh4x2oY5Sfl2TfwzTRAyq5kWiapObUQ ROILoxQ== X-Gm-Gg: ASbGnctYqV0Uds6nLfRC9s4HJ2OQfXJeq4qMIb+wzXuRFqPFjs+VROwHkS58dOPDreL Q9LeqwnV/wFD07bziRbdes5vo5dfZSTO/PlSdbW6TN7/GhvugZVSHnB7qSac6q/YaKc2WJZ+5Ba sig8ADl+MxBzhMpIw9WjaYR6qChp4CiSRit5kKk+wUbLFwUlIIYO8lK/h7n4ighGrN7jd7JlA7O kjMauq9SC1OKKwHA2/YgJK6JuN09hXNKW+OJDJljGX8vDTwT0M4bUW/qnb7HoQxgTKJ+CIK/r67 ZCdkdyvvmqTKst3+Dd3BnDY= X-Google-Smtp-Source: AGHT+IHcBvsomVnHzNNdyYUP40PyzjKJhjmGzUKJe2PP+dg4JpanNYCoJ1UziQclKb+qvn2U1IQNXA== X-Received: by 2002:a05:6214:508c:b0:6e4:5f2b:9f15 with SMTP id 6a1803df08f44-6e8959ea24emr72926276d6.14.1740702823412; Thu, 27 Feb 2025 16:33:43 -0800 (PST) Received: from boreas.. ([140.174.215.88]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7c36fef5beesm174769085a.32.2025.02.27.16.33.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Feb 2025 16:33:43 -0800 (PST) From: Emil Tsalapatis To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.de, eddyz87@gmail.com, yonghong.song@linux.dev, Emil Tsalapatis Subject: [PATCH 2/2] selftests: bpf: add bpf_cpumask_fill selftests Date: Thu, 27 Feb 2025 19:33:21 -0500 Message-ID: <20250228003321.1409285-3-emil@etsalapatis.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250228003321.1409285-1-emil@etsalapatis.com> References: <20250228003321.1409285-1-emil@etsalapatis.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add selftests for the bpf_cpumask_fill helper that sets a bpf_cpumask to a bit pattern provided by a BPF program. Signed-off-by: Emil Tsalapatis (Meta) --- .../selftests/bpf/prog_tests/verifier.c | 2 + .../selftests/bpf/progs/cpumask_success.c | 23 ++++++ .../selftests/bpf/progs/verifier_cpumask.c | 77 +++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_cpumask.c diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c index 8a0e1ff8a2dc..4dd95e93bd7e 100644 --- a/tools/testing/selftests/bpf/prog_tests/verifier.c +++ b/tools/testing/selftests/bpf/prog_tests/verifier.c @@ -23,6 +23,7 @@ #include "verifier_cgroup_storage.skel.h" #include "verifier_const.skel.h" #include "verifier_const_or.skel.h" +#include "verifier_cpumask.skel.h" #include "verifier_ctx.skel.h" #include "verifier_ctx_sk_msg.skel.h" #include "verifier_d_path.skel.h" @@ -155,6 +156,7 @@ void test_verifier_cgroup_skb(void) { RUN(verifier_cgroup_skb); } void test_verifier_cgroup_storage(void) { RUN(verifier_cgroup_storage); } void test_verifier_const(void) { RUN(verifier_const); } void test_verifier_const_or(void) { RUN(verifier_const_or); } +void test_verifier_cpumask(void) { RUN(verifier_cpumask); } void test_verifier_ctx(void) { RUN(verifier_ctx); } void test_verifier_ctx_sk_msg(void) { RUN(verifier_ctx_sk_msg); } void test_verifier_d_path(void) { RUN(verifier_d_path); } diff --git a/tools/testing/selftests/bpf/progs/cpumask_success.c b/tools/testing/selftests/bpf/progs/cpumask_success.c index 80ee469b0b60..f252aa2f3090 100644 --- a/tools/testing/selftests/bpf/progs/cpumask_success.c +++ b/tools/testing/selftests/bpf/progs/cpumask_success.c @@ -770,3 +770,26 @@ int BPF_PROG(test_refcount_null_tracking, struct task_struct *task, u64 clone_fl bpf_cpumask_release(mask2); return 0; } + +SEC("syscall") +__success +int BPF_PROG(test_fill_reject_small_mask) +{ + struct bpf_cpumask *local; + u8 toofewbits; + int ret; + + local = create_cpumask(); + if (!local) + return 0; + + /* The kfunc should prevent this operation */ + ret = bpf_cpumask_fill((struct cpumask *)local, &toofewbits, sizeof(toofewbits)); + if (ret != -EACCES) + err = 2; + + bpf_cpumask_release(local); + + return 0; +} + diff --git a/tools/testing/selftests/bpf/progs/verifier_cpumask.c b/tools/testing/selftests/bpf/progs/verifier_cpumask.c new file mode 100644 index 000000000000..bb84dd36beac --- /dev/null +++ b/tools/testing/selftests/bpf/progs/verifier_cpumask.c @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include +#include +#include +#include "bpf_misc.h" + +#include "cpumask_common.h" + +#define CPUMASK_TEST_MASKLEN (8 * sizeof(u64)) + +u64 bits[CPUMASK_TEST_MASKLEN]; + +SEC("syscall") +__success +int BPF_PROG(test_cpumask_fill) +{ + struct bpf_cpumask *mask; + int ret; + + mask = bpf_cpumask_create(); + if (!mask) { + err = 1; + return 0; + } + + ret = bpf_cpumask_fill((struct cpumask *)mask, bits, CPUMASK_TEST_MASKLEN); + if (!ret) + err = 2; + + if (mask) + bpf_cpumask_release(mask); + + return 0; +} + +SEC("syscall") +__description("bpf_cpumask_fill: invalid cpumask target") +__failure __msg("type=scalar expected=fp") +int BPF_PROG(test_cpumask_fill_cpumask_invalid) +{ + struct bpf_cpumask *invalid = (struct bpf_cpumask *)0x123456; + int ret; + + ret = bpf_cpumask_fill((struct cpumask *)invalid, bits, CPUMASK_TEST_MASKLEN); + if (!ret) + err = 2; + + return 0; +} + +SEC("syscall") +__description("bpf_cpumask_fill: invalid cpumask source") +__failure __msg("leads to invalid memory access") +int BPF_PROG(test_cpumask_fill_bpf_invalid) +{ + void *garbage = (void *)0x123456; + struct bpf_cpumask *local; + int ret; + + local = create_cpumask(); + if (!local) { + err = 1; + return 0; + } + + ret = bpf_cpumask_fill((struct cpumask *)local, garbage, CPUMASK_TEST_MASKLEN); + if (!ret) + err = 2; + + bpf_cpumask_release(local); + + return 0; +} + +char _license[] SEC("license") = "GPL";