From patchwork Wed Mar 13 21:41:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13591906 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f173.google.com (mail-yw1-f173.google.com [209.85.128.173]) (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 17434535CE for ; Wed, 13 Mar 2024 21:41:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710366108; cv=none; b=s2byxLW8ARXiEHnFL9NibfZ5a+f11DO1FEb0RVFsT40hzHPJlzMdmSxqe56AALlNK+J425GLu/2bR849Zvdo2FI61etYpbOZhYdxk+gHYv4WDn/phJarxrrQfWS4SEK3q3/BO1+GD5MxDPhrFEIK/PjdOnHSXiP/gs73NGUKdfU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710366108; c=relaxed/simple; bh=ply/EUFX0DFTlzOppoUbXuC3+JIXUMN5r0124S4PX4o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OFozlBixFYZF8SG5Iivini7jhu4xgE3paIFKPs8MBuISqByErc0xIZTu59sXOXnokCtADZ0yF3qsuQ8JRYsHbW3Dbnn9OgXcUWn1meVRYfjbM13kSAvid0skNvT6RWXj3lGDW7K4GIJGOV7ExLcpX5ot2Gxa1Sj2dyvUjbyaJQY= 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=Wnk4k8od; arc=none smtp.client-ip=209.85.128.173 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="Wnk4k8od" Received: by mail-yw1-f173.google.com with SMTP id 00721157ae682-60a3c48e70fso3924197b3.1 for ; Wed, 13 Mar 2024 14:41:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710366104; x=1710970904; 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=+FypRBUzW9FGcNeJePiI1aw1hm8U6O5hdRlDxrGWkok=; b=Wnk4k8odYTsTJ4CEMckFxB4BH+pUEv1w1BC9udfHK27O9shpYLHKfG81ltS/EAGV5d lb5+8frQQ1IZTuWMDBKe8k7tScOkiuo5/VwS9EJjNP4vDDNPCa/pkx5QbEhmBnid5s5j bKYDNHY6NuQYFq4N3XZNd3AjiIeQr2gNnR7OLKSmoWbFC22LlMIBE/RkDZzgzVRN4IdH /ml10dF1d8KT276e9m64eB+3vTT1N7ZAGOR9twVr3XZrbeoLhYMq17vf8TBiKuGX78Mm d856YRjXmXfEVUyBaBBhaK/1KD2FL+9pV5JRDQ/0/heBzmsDVzaEsfq6ikMNEfDvD9dE Pumg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710366104; x=1710970904; 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=+FypRBUzW9FGcNeJePiI1aw1hm8U6O5hdRlDxrGWkok=; b=Vlr7E6BTYNL+kmucnaTVuBnFEHEM5ORaSHD06+9yUGHoScicwJ+lfVoj+PKW8FUEyD t7nyxEi4cbt1p/PeESroJF+hnFBdyaZq1NfLSsBVppeTYqZpRw5M+yRKvwLn2D21wjpB CazwyEPXw5NlZE+HnBN9H8KSp58R5EgpooZDqr8IWz4W0xsWyb2zI9mPl6sFjvz8d0lL lTkp6jyTg3flkpLWozGFDlCdSZWVKxyM1XhJPJ8FPReLtfI1OLQ5nWgYFH71XwExAKI9 nAzWeOSqJ/lW9B+Ur45JDE8KHPMsBFl1GimZw1EYxBLwSoDP/6uXwEcwHWYjvaILhmmp Bxsw== X-Gm-Message-State: AOJu0YxHFH3B34EdWT0OpQz+WHfRihtvEb2WcHrQEE5E6vBm/GnlJZXR kdKjs+NJCYx0dnh3+ecQ4Ch83W2dNsrFAuPSIP5NGc/o1DyhrmSDbSvEFOC9 X-Google-Smtp-Source: AGHT+IF5Pp2IrWB1CKijVPgK7Mde+EtvAo9iKNBh+Q4OW9UBsOMi+3buU6HIcfZ/OQEmMJM/pNug9Q== X-Received: by 2002:a81:4f82:0:b0:60a:2fd0:8c21 with SMTP id d124-20020a814f82000000b0060a2fd08c21mr3965982ywb.6.1710366104599; Wed, 13 Mar 2024 14:41:44 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:30fd:440:22c1:8047]) by smtp.gmail.com with ESMTPSA id y74-20020a0dd64d000000b00607b3038a7dsm2678950ywd.9.2024.03.13.14.41.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 14:41:44 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v2 1/3] libbpf: Skip zeroed or null fields if not found in the kernel type. Date: Wed, 13 Mar 2024 14:41:37 -0700 Message-Id: <20240313214139.685112-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240313214139.685112-1-thinker.li@gmail.com> References: <20240313214139.685112-1-thinker.li@gmail.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 Accept additional fields of a struct_ops type with all zero values even if these fields are not in the corresponding type in the kernel. This provides a way to be backward compatible. User space programs can use the same map on a machine running an old kernel by clearing fields that do not exist in the kernel. Signed-off-by: Kui-Feng Lee --- tools/lib/bpf/libbpf.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index efab29b8935b..d0ba6eb605f8 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1132,8 +1132,26 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map) const char *mname; mname = btf__name_by_offset(btf, member->name_off); + moff = member->offset / 8; + mdata = data + moff; + msize = btf__resolve_size(btf, member->type); + if (msize < 0) { + pr_warn("struct_ops init_kern %s: failed to resolve the size of member %s\n", + map->name, mname); + return msize; + } + kern_member = find_member_by_name(kern_btf, kern_type, mname); if (!kern_member) { + /* Skip all zeros or null fields if they are not + * presented in the kernel BTF. + */ + if (libbpf_is_mem_zeroed(mdata, msize)) { + pr_info("struct_ops %s: member %s not found in kernel, skipping it as it's set to zero\n", + map->name, mname); + continue; + } + pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n", map->name, mname); return -ENOTSUP; @@ -1147,10 +1165,7 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map) return -ENOTSUP; } - moff = member->offset / 8; kern_moff = kern_member->offset / 8; - - mdata = data + moff; kern_mdata = kern_data + kern_moff; mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id); @@ -1230,9 +1245,8 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map) continue; } - msize = btf__resolve_size(btf, mtype_id); kern_msize = btf__resolve_size(kern_btf, kern_mtype_id); - if (msize < 0 || kern_msize < 0 || msize != kern_msize) { + if (kern_msize < 0 || msize != kern_msize) { pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n", map->name, mname, (ssize_t)msize, (ssize_t)kern_msize); From patchwork Wed Mar 13 21:41:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13591907 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f173.google.com (mail-yw1-f173.google.com [209.85.128.173]) (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 55C4553E3D for ; Wed, 13 Mar 2024 21:41:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710366108; cv=none; b=Wt4YHHRHXAw52QRIOmNojfHyy0kTgToMEqMxvaJGVZs/24lozgI5xS4CdRICp9i2TcZKYkSdUn5p/itNMcbzAK7gsTu3CB2K1e1ntB72i9/z1Mp14DIe14p7/zxdJs+GtiMpKConTpcFGzC8Hr/LaKTt1fttYjFSHmcwwCKoQaw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710366108; c=relaxed/simple; bh=dtLdzQuBbY4kfQP4lL4sE6OJP2osySf8drptIZwprWY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=l61CcU0r8Ybvk7n3OHW9t5wDRWGSYi1rGQKP4OC4BzVMPAbNyonefKhYvtUJswOXasuFpqkyBcoLaUXJaKBIq6/uOU59hXEW86VIjcCujL4TULz44xq3MrMLGFVIA0iAY2KuVjJGALJ8SIV80Hlm5DhUNDar2rTbgcNeTMhjAsQ= 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=D0N9oy5d; arc=none smtp.client-ip=209.85.128.173 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="D0N9oy5d" Received: by mail-yw1-f173.google.com with SMTP id 00721157ae682-60a5a11b59dso4035527b3.0 for ; Wed, 13 Mar 2024 14:41:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710366106; x=1710970906; 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=Q273taNMFteBw9+l51Eedoh4iH5uR4q8TiM120xEQ+4=; b=D0N9oy5dctsGnfjLzqmw/gAwbxUKPE3WGEqKclIvRb8G5zhHEn78mH7zqTWRdThdc1 rtAGeVWJ+TUSNLdfEU0yCjmic6jQpu0PQmQMJNhCxRqNMc5z1JroKdF5prkeRPrYQb8O HUViWR4xoXOOqSl0gjMie1rjZWDy1s5XmBo1PROf4+E8h2zM7/+pg+bfPdk3okM4E9rk deyKNL3R1RYwUepJNVWnY4j+HAhHEF8HA7L9ifeFNeCTg2BBp841tlhbB8xvu4Lnj9fl NclgJDMEdexBC0BRIobiLD50h0JUuILPmbXGzTm3yTQbzA7PXb2dwaBj7oZUb51FT9pE 5EGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710366106; x=1710970906; 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=Q273taNMFteBw9+l51Eedoh4iH5uR4q8TiM120xEQ+4=; b=ahBT57C4N9HR4MFV0ftAtPhUCihK76aV4dew5erkjkyn/cjCrjn9TrfucnPTsTrdYn kRgPpepreqbNf1sxcmouE8+swws85kon/XbZdmCuGJ1D8xFQkhifpJwnMoK6Rb8eCwWE m26wXNOepYWNz8YMLwDuDFiOTWZ/Yl7+DQyMCH9KIAfmGzSSvR9VVcdXyfCocZYX1QaS 35l9cRvggB4jcKO5PssCXgNhOIGI+z1ROime2kdksJ+p1urd8bhrfcIXgpRpjNlR6W3Q VPC1GmoIBmWyjQNGYJQ+RNwxeC0JNpN8lCRJrdN8aEwmWY3UngVyVwgKz/HemDmZ1UXo MytA== X-Gm-Message-State: AOJu0YzFVzSjUIx3ZkKFfJ4knO22NEnMFR/d0BiMT6xqp63WjL1FSCro wUDdsqaVS4xTrMaYIxUlX87UyyWpLcQ644G9GdgUgtR95nRvnSI1oFLk8pvz X-Google-Smtp-Source: AGHT+IEhKYe1pmBuqIMQUmB4vD+SEDeH4iHd3C1WcIaKoFCV1tdRquA7pKszlU/uLV3wVip5JKZEiw== X-Received: by 2002:a81:89c1:0:b0:609:fff3:bad0 with SMTP id z184-20020a8189c1000000b00609fff3bad0mr4651002ywf.7.1710366105726; Wed, 13 Mar 2024 14:41:45 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:30fd:440:22c1:8047]) by smtp.gmail.com with ESMTPSA id y74-20020a0dd64d000000b00607b3038a7dsm2678950ywd.9.2024.03.13.14.41.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 14:41:45 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v2 2/3] selftests/bpf: Ensure libbpf skip all-zeros fields of struct_ops maps. Date: Wed, 13 Mar 2024 14:41:38 -0700 Message-Id: <20240313214139.685112-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240313214139.685112-1-thinker.li@gmail.com> References: <20240313214139.685112-1-thinker.li@gmail.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 A new version of a type may have additional fields that do not exist in older versions. Previously, libbpf would reject struct_ops maps with a new version containing extra fields when running on a machine with an old kernel. However, we have updated libbpf to ignore these fields if their values are all zeros or null in order to provide backward compatibility. Signed-off-by: Kui-Feng Lee --- .../bpf/prog_tests/test_struct_ops_module.c | 47 +++++++++++++++++++ .../selftests/bpf/progs/struct_ops_module.c | 16 ++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c index ee5372c7f2c7..098776d00ab4 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c @@ -93,9 +93,56 @@ static void test_struct_ops_load(void) struct_ops_module__destroy(skel); } +static void test_struct_ops_not_zeroed(void) +{ + struct struct_ops_module *skel; + int err; + + /* zeroed is 0, and zeroed_op is null */ + skel = struct_ops_module__open(); + if (!ASSERT_OK_PTR(skel, "struct_ops_module_open")) + return; + + err = struct_ops_module__load(skel); + ASSERT_OK(err, "struct_ops_module_load"); + + struct_ops_module__destroy(skel); + + /* zeroed is not 0 */ + skel = struct_ops_module__open(); + if (!ASSERT_OK_PTR(skel, "struct_ops_module_open_not_zeroed")) + return; + + /* libbpf should reject the testmod_zeroed since struct + * bpf_testmod_ops in the kernel has no "zeroed" field and the + * value of "zeroed" is non-zero. + */ + skel->struct_ops.testmod_zeroed->zeroed = 0xdeadbeef; + err = struct_ops_module__load(skel); + ASSERT_ERR(err, "struct_ops_module_load_not_zeroed"); + + struct_ops_module__destroy(skel); + + /* zeroed_op is not null */ + skel = struct_ops_module__open(); + if (!ASSERT_OK_PTR(skel, "struct_ops_module_open_not_zeroed_op")) + return; + + /* libbpf should reject the testmod_zeroed since the value of its + * "zeroed_op" is not null. + */ + skel->struct_ops.testmod_zeroed->zeroed_op = skel->progs.test_3; + err = struct_ops_module__load(skel); + ASSERT_ERR(err, "struct_ops_module_load_not_zeroed_op"); + + struct_ops_module__destroy(skel); +} + void serial_test_struct_ops_module(void) { if (test__start_subtest("test_struct_ops_load")) test_struct_ops_load(); + if (test__start_subtest("test_struct_ops_not_zeroed")) + test_struct_ops_not_zeroed(); } diff --git a/tools/testing/selftests/bpf/progs/struct_ops_module.c b/tools/testing/selftests/bpf/progs/struct_ops_module.c index 026cabfa7f1f..86e1e50c5531 100644 --- a/tools/testing/selftests/bpf/progs/struct_ops_module.c +++ b/tools/testing/selftests/bpf/progs/struct_ops_module.c @@ -23,7 +23,7 @@ void BPF_PROG(test_2, int a, int b) test_2_result = a + b; } -SEC("struct_ops/test_3") +SEC("?struct_ops/test_3") int BPF_PROG(test_3, int a, int b) { test_2_result = a + b + 3; @@ -54,3 +54,17 @@ struct bpf_testmod_ops___v2 testmod_2 = { .test_1 = (void *)test_1, .test_2 = (void *)test_2_v2, }; + +struct bpf_testmod_ops___zeroed { + int (*test_1)(void); + void (*test_2)(int a, int b); + int (*test_maybe_null)(int dummy, struct task_struct *task); + void (*zeroed_op)(int a, int b); + int zeroed; +}; + +SEC(".struct_ops.link") +struct bpf_testmod_ops___zeroed testmod_zeroed = { + .test_1 = (void *)test_1, + .test_2 = (void *)test_2_v2, +}; From patchwork Wed Mar 13 21:41:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13591908 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f178.google.com (mail-yw1-f178.google.com [209.85.128.178]) (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 5E31353E2E for ; Wed, 13 Mar 2024 21:41:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710366110; cv=none; b=klVU5lNrodUAMuqTF3Lsm6gqB92mL0B/mmnccDIOhohbFlBF+6wGhfEoUqL7HsunsxH7GxuPSzfb+sPimQ3gCSxutHop3azQStbRszA5JmtZLcN0Hsnx5+8XCTzca0Y4VrFz+DpVJLDiQiM/IAfFtFbYFl2rqBct3YvzW2FeiDE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710366110; c=relaxed/simple; bh=u1+sbSM+7+UEe04xGAhqrujZrrYmcrCFuvWQmciz25Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lauBkz0WWfHa3WmQdx/Lo/QLBN/g60mnxaJPbToQTGsYmXp9nlYoWq7fxOaulu3Qwy5lS+HDWhXT5q2kgiDvDcn82Lelu314eUsu66SvCEag8xHD39jsE6dgdoAG1x30+OnNQrKZ5m7hcItcYhNI6IxGtsAfL0QC/vZPA0yeLco= 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=gBqHvYeX; arc=none smtp.client-ip=209.85.128.178 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="gBqHvYeX" Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-609fc742044so3639067b3.1 for ; Wed, 13 Mar 2024 14:41:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710366107; x=1710970907; 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=7hMREXqXE361hvOJ3Zdr6NNwVC2A8l7mk09b9lG9NMU=; b=gBqHvYeXsVITnE4wiTdPW3jO3xlcN6t3+pNI8cK7sjLUJHnXKqvGz1BFXLawEYxnVU nQjayGxPhCyYqDLnErvk9YhB5UsShg/7HBP4QY7YA3HEnY3/dMIb7jyOx8aoTLyy8DII pClVySFzHoAEHNsEtK1WAv/XgZidSS6XZY8xnn1MNW/rohX4WU/CLTG/jGVupB93o2yd XT0e2axUpUgAoEj+zpnIH5UPQkuk3R2U90IFE+zi1yC1RWwHn6kAbMp3hPVaouyELqBu 38SHdv66wrW6UWOCiVVp0+s1XMhYTMjqG5mR9vDQMK2Tu3L3DeCuRe5OJN1BZarUzKDO 9/dQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710366107; x=1710970907; 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=7hMREXqXE361hvOJ3Zdr6NNwVC2A8l7mk09b9lG9NMU=; b=p5deXTEycdMTm38cPpMxAjyc0tvBTTZesasOhsmAHdeh2pI8oF+mBXcTNJyanm9s+D WZqkyich7/DQqxsGByPBO9LXr8n3gTNxrwJjeN0FIMgGGY+6YdqFNvb8L0pU3JJPc1WH JPB4RzAOe07lbO+btQvTo5tMpE1UZLzkZu9XpX7uAmWwzJcwUfk7aAxLYyBLhClOvMQx ESXLrnVf0eswnxoR8fGxlXzYxXmeSpKGTsceuYbJxecBcc1b2h/6q0Nvp0mPvpcWV9ft eOQiSRknHDlfZZF7XuLp1Z/q6HYbM61ccdePtsUNgTyfwo3V6M8aUbo7vZnr8ioy+pBO MguA== X-Gm-Message-State: AOJu0YwfhaogEHlqI3XSpELXcJzNVNnezke1BB4/vDko8zdILdK06m+t 4viWuJRdGg3FKwoK/CydmtWMNLTEOonreGvJujQWCvnMLu/6lSTQwRUi+KME X-Google-Smtp-Source: AGHT+IFgR3d6HgqWlyHbiQ2UiAgP/2mZEtx2IQmFTaorMP+UbeD/ttRWEU03WJbfYJuv2lgPDANk5Q== X-Received: by 2002:a0d:c945:0:b0:60a:3ad:6872 with SMTP id l66-20020a0dc945000000b0060a03ad6872mr3763204ywd.34.1710366106836; Wed, 13 Mar 2024 14:41:46 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:30fd:440:22c1:8047]) by smtp.gmail.com with ESMTPSA id y74-20020a0dd64d000000b00607b3038a7dsm2678950ywd.9.2024.03.13.14.41.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 14:41:46 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v2 3/3] selftests/bpf: Accept extra arguments if they are not used. Date: Wed, 13 Mar 2024 14:41:39 -0700 Message-Id: <20240313214139.685112-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240313214139.685112-1-thinker.li@gmail.com> References: <20240313214139.685112-1-thinker.li@gmail.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 A struct_ops program can have more arguments than what the corresponding function pointer in the kernel has if the program doesn't access the arguments. For example, a struct_ops operator may have 2 arguments, but your program may defined with 3 or more arguments. It is acceptable as long as the program doesn't use these arguments. Signed-off-by: Kui-Feng Lee --- .../bpf/prog_tests/test_struct_ops_module.c | 56 +++++++++++++++++++ .../bpf/progs/struct_ops_extra_arg.c | 49 ++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_extra_arg.c diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c index 098776d00ab4..a146bf079a60 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c @@ -4,6 +4,7 @@ #include #include "struct_ops_module.skel.h" +#include "struct_ops_extra_arg.skel.h" static void check_map_info(struct bpf_map_info *info) { @@ -138,11 +139,66 @@ static void test_struct_ops_not_zeroed(void) struct_ops_module__destroy(skel); } +/* Handle BPF programs with additional arguments that don't appear in the + * function pointer prototype in the kernel. + */ +static void test_struct_ops_extra_arg(void) +{ + struct struct_ops_extra_arg *skel; + int err; + + /* test_extra_arg() has an extra argument, so testmod_1 should fail + * to load. + * + * Since extra_arg is used in test_extra_arg(), it should be + * rejected by the verifier. + */ + skel = struct_ops_extra_arg__open(); + if (!ASSERT_OK_PTR(skel, "struct_ops_extra_arg_open_extra_arg")) + return; + + err = struct_ops_extra_arg__load(skel); + ASSERT_ERR(err, "struct_ops_extra_arg_load_extra_arg"); + + struct_ops_extra_arg__destroy(skel); + + /* Switch to test_2() */ + skel = struct_ops_extra_arg__open(); + if (!ASSERT_OK_PTR(skel, "struct_ops_extra_arg_open")) + return; + + skel->struct_ops.testmod_1->test_2 = skel->progs.test_2; + + err = struct_ops_extra_arg__load(skel); + ASSERT_OK(err, "struct_ops_extra_arg_load"); + + struct_ops_extra_arg__destroy(skel); + + /* Switch to test_extra_arg_unused() + * + * Since extra_arg is never used, it should be accepted by the + * verifier. The verifier would accept a program with extra + * arguments as long as they are not used. + */ + skel = struct_ops_extra_arg__open(); + if (!ASSERT_OK_PTR(skel, "struct_ops_extra_arg_open_unused")) + return; + + skel->struct_ops.testmod_1->test_2 = skel->progs.test_extra_arg_unused; + + err = struct_ops_extra_arg__load(skel); + ASSERT_OK(err, "struct_ops_extra_arg_load_unused"); + + struct_ops_extra_arg__destroy(skel); +} + void serial_test_struct_ops_module(void) { if (test__start_subtest("test_struct_ops_load")) test_struct_ops_load(); if (test__start_subtest("test_struct_ops_not_zeroed")) test_struct_ops_not_zeroed(); + if (test__start_subtest("test_struct_ops_extra_arg")) + test_struct_ops_extra_arg(); } diff --git a/tools/testing/selftests/bpf/progs/struct_ops_extra_arg.c b/tools/testing/selftests/bpf/progs/struct_ops_extra_arg.c new file mode 100644 index 000000000000..4b73b279ad22 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_extra_arg.c @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ +#include +#include +#include +#include "../bpf_testmod/bpf_testmod.h" + +char _license[] SEC("license") = "GPL"; + +int test_1_result = 0; +int test_2_result = 0; + +SEC("struct_ops/test_1") +int BPF_PROG(test_1) +{ + test_1_result = 0xdeadbeef; + return 0; +} + +SEC("?struct_ops/test_2") +void BPF_PROG(test_2, int a, int b) +{ + test_2_result = a + b; +} + +SEC("?struct_ops/test_extra_arg") +void BPF_PROG(test_extra_arg, int a, int b, int extra_arg) +{ + /* Accessing extra_arg will cause a verifier error since it + * accesses the context data beyond the size of the context. + */ + test_2_result = a + b + extra_arg + 3; +} + +SEC("?struct_ops/test_extra_arg_unused") +void BPF_PROG(test_extra_arg_unused, int a, int b, int extra_arg) +{ + /* The extra_arg is not used, so it should not cause a verifier + * error. + */ + test_2_result = a + b + 3; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_1 = { + .test_1 = (void *)test_1, + .test_2 = (void *)test_extra_arg, + .data = 0x1, +};