From patchwork Sat Mar 2 01:19:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579324 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f175.google.com (mail-lj1-f175.google.com [209.85.208.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 F2AA26AB9 for ; Sat, 2 Mar 2024 01:19:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342379; cv=none; b=qJENd7DL8hPxZAG3+EgTa+6VaDz60kiH3TciM63knxKa9OGnIhOb8NZ8Gk6DHml60XzTpZHy+Bt090JmtPw/dSiYA5ttJhcTVZXhMa/bWLFMTatH7f4hFLjBfCuTeR6xnPUqyzwpd2y0SvaCynUbO25Vepu+zJ1Yhb5MSmJCKtU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342379; c=relaxed/simple; bh=u/1jTp2CttH5gxlhCHQfZKsq9FO8kAJbKLWIR91fUBQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Xplq2iGus/1LPDQ3dccqNd1LnKw2Eiex1zxz0yIKA6WExkXc2qX1THArj5X8Pylsxb6k9lcftB89sGZw8rn8wEKvsVGzwwkBGSvEXcFICMiseOqqp9XyDXy/rE7woprUgESX70c7pe7W0yAlqu8g5z5KITgzWaxA8CGjKMR+8Ms= 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=egFPxj/z; arc=none smtp.client-ip=209.85.208.175 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="egFPxj/z" Received: by mail-lj1-f175.google.com with SMTP id 38308e7fff4ca-2d23d301452so28605581fa.1 for ; Fri, 01 Mar 2024 17:19:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342376; x=1709947176; 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=8ghBbmU7JgmzNE0IDgqQ93JxAVATdz4P5/PxXkAkYZ4=; b=egFPxj/zEzpaKUXUrqHU56/kNhb1xfMM2LZnMleI/Zy4yCEvSfBHWTQs2Yyz/KAQ1x QACiHGDTMC5oWTnTDurHBeIwGSxbNQy59KIFoeX/jFGSZ/Ggnyy38D1MJZIwU1u6/F7r 17WyAq2h5biQYibU7VYDMOd/vgjBoMg5awhgkrWC4fvXKUrjdYYYsGAv9M3YhHGGBJEX 10L/yuOh8gSb0r/dlUIUf01YOmZWQhv3zTMJmeBm83Jt83ye+h7mxYflAvwCsRMt6iTx K8abxiXooxuLM1ZbpYWY4VROXhYuP9hrA9oxowF6az3EuX4pl1fktv2Bn76ngdYwcJv9 KAhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342376; x=1709947176; 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=8ghBbmU7JgmzNE0IDgqQ93JxAVATdz4P5/PxXkAkYZ4=; b=RSI2DYl8N7TJyaCBTopM/+eG5u8EXZQ6lZocLDv8uG7sciBuzShbzuWiX16rxqgoeb 8vHACF9WrEi7rv8xGkWUXSrCwtn/JNkgEYgxl9xlmEx3ytB2H8tins7SvjDhqstLRanz e2/9v0EcZwGTdC7OIm2RkfNQ531P3TsYZScw3s0e9UAzUMExkaI98OBYutavWM16Glli /U+9pT+h6v7hMLX3AtW3J9S44yfMYLxFAEPpujePiEZShe607mbhbNfiWhp3yM6K1hZ8 zuz7GaNepcmTIBu5kK4YBXjDnn6XFYzfUq7F77VKP5JSRJHNecUi6u55kVGkB88AIcaY l5mg== X-Gm-Message-State: AOJu0YwC/5gvMPLr9dk5yxIhuoGIIopRsydx2HjiBDCIz91iLhrgIjcr QMLbFxGyM5Nd7D39JYNHSPBy1S+wyBbzdwBasyNU96hqIss9U/l+fRFpkYO3 X-Google-Smtp-Source: AGHT+IF2PpXr5476Y7hicTLds97Adp0NKs91UzW7FpffnBlOfg/n2q7ZvHlZN/aCpKgim9tfC5Ckig== X-Received: by 2002:a2e:860d:0:b0:2d3:1dcc:3b25 with SMTP id a13-20020a2e860d000000b002d31dcc3b25mr2575644lji.10.1709342375808; Fri, 01 Mar 2024 17:19:35 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:35 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 01/15] libbpf: allow version suffixes (___smth) for struct_ops types Date: Sat, 2 Mar 2024 03:19:06 +0200 Message-ID: <20240302011920.15302-2-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 E.g. allow the following struct_ops definitions: struct bpf_testmod_ops___v1 { int (*test)(void); }; struct bpf_testmod_ops___v2 { int (*test)(void); }; SEC(".struct_ops.link") struct bpf_testmod_ops___v1 a = { .test = ... } SEC(".struct_ops.link") struct bpf_testmod_ops___v2 b = { .test = ... } Where both bpf_testmod_ops__v1 and bpf_testmod_ops__v2 would be resolved as 'struct bpf_testmod_ops' from kernel BTF. Acked-by: David Vernet Signed-off-by: Eduard Zingerman --- tools/lib/bpf/libbpf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 6c2979f1b471..e2a4c409980b 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -948,7 +948,7 @@ static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, const char *name, __u32 kind); static int -find_struct_ops_kern_types(struct bpf_object *obj, const char *tname, +find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw, struct module_btf **mod_btf, const struct btf_type **type, __u32 *type_id, const struct btf_type **vtype, __u32 *vtype_id, @@ -958,8 +958,12 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname, const struct btf_member *kern_data_member; struct btf *btf; __s32 kern_vtype_id, kern_type_id; + char tname[256]; __u32 i; + snprintf(tname, sizeof(tname), "%.*s", + (int)bpf_core_essential_name_len(tname_raw), tname_raw); + kern_type_id = find_ksym_btf_id(obj, tname, BTF_KIND_STRUCT, &btf, mod_btf); if (kern_type_id < 0) { From patchwork Sat Mar 2 01:19:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579325 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f175.google.com (mail-lj1-f175.google.com [209.85.208.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 4A6C86FB5 for ; Sat, 2 Mar 2024 01:19:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342381; cv=none; b=awvNXW4tSWIvyt+XB+Q6JmVLThAI2jtXrecMl5frCIZxwGB0usCp6cZ09iPCNP576QZi7M3Gb2BMTESnrZcCEl4C7CGlRdiuUgdwn83b6at1fNNxV+TKn93IM1eyianntJFr/vt+rjXpldbHibmA2IVLp/9E6Sa3Yk14uBiWEOI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342381; c=relaxed/simple; bh=7n3VOltWg2CvMWA84SRE1wH3LRcYRxUITz9dnx52KQ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cHsq0iI0sHL6PWNBCnVZfx9fAO9uBvwcHdL+SS2Q6BhJdIgxEM532AG0Vn1PMS6ZfYt8W3PdRSy7qGMjzIIU9nJoGiKRy/jqKsD20V3PB1Ayh46OLnKoYaf1h1biHLF5R4gZ1/Uo4V5NrETVZdIuD0mwe4pSIkXkFtrpKz3z+V4= 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=VmgZSB9t; arc=none smtp.client-ip=209.85.208.175 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="VmgZSB9t" Received: by mail-lj1-f175.google.com with SMTP id 38308e7fff4ca-2d27184197cso34829091fa.1 for ; Fri, 01 Mar 2024 17:19:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342377; x=1709947177; 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=v11JrOpQ0vOwRfRtr1r+yvwVR9OS5PTJrump/SfFJRM=; b=VmgZSB9tpfIzL6HlU6LKJcgYFDMyeLejeUkmR9sg2jYPKCzPqMJIF4PSuqpGfRecVd pVb/MxSnh7amtzGAUVLJ6LUWPhkprddglBJgukHev1kp2xuKIQpKEJ3lud5Rn2pfr5JQ qI//TdjlBTuXdu2vUDEU8DmPWdV6cvCxskpnZ2I0BxcGwblTGx5w7tl0oNUJYjaCmOqY AQPIJPIOjBo/FhF7NCHfL7ea/vdm/PXPF6syOdDiWnnJYSflK1lKYHfXXPaoZj9T0UkK BeJFUTzyOdLBg8RMe8qL9aM0jSLWzkSrlgAD8jLQZpDx1GWNzAqo3u45W5QxKAZ8Y7rC 0DIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342377; x=1709947177; 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=v11JrOpQ0vOwRfRtr1r+yvwVR9OS5PTJrump/SfFJRM=; b=RJD8pb3z/1Hzsy+j+A3kOhRtveJv4UjOlm730vEh4bNFEGlT2DequyMekifc8huqqK /KzAxmziib/drAOvdSV4JG5aWQG/st7BboG+HnqYQFpNW90AlhiCo9iSHW1iu7w+CEbM eLiSns224XcH+/sbE6G8u8ii2G5G75B7t8ZZ8tuaQV/fbNhzmLMS9uMPDoXL+1JkbY1r TIJcQLROdq5cIEYxiazOFANab/fwJj8VS2zwXRMVLJOk9fOep5dyz5vbW9H0IIjHC/QK KMSqjqL1aF4GqwZbq5Ns/kZbfVaeUWDF05NMSLG2cRp4FaGo18pJGFoKD1M85YuBS4zp 0K3g== X-Gm-Message-State: AOJu0YxnkIre+Gc+8Tn+Pcrr30UcFxr4UpjpTsXUjO7VPTGpclThnry5 bWpuGJZntu07kkFo0dmESgQUopxgXPcsYak8i6sVALdHGxFLVPoA2q+Ejn4t X-Google-Smtp-Source: AGHT+IGpB5wOMHq70nPcyy7vlDPZWnYI3UYcqxyE+oDDYOU7Xps8/8n3gbUwCh9RVn2ppi2poXQXgw== X-Received: by 2002:a2e:7316:0:b0:2d2:5f8b:1382 with SMTP id o22-20020a2e7316000000b002d25f8b1382mr2376977ljc.2.1709342377006; Fri, 01 Mar 2024 17:19:37 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:36 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 02/15] libbpf: tie struct_ops programs to kernel BTF ids, not to local ids Date: Sat, 2 Mar 2024 03:19:07 +0200 Message-ID: <20240302011920.15302-3-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 Enforce the following existing limitation on struct_ops programs based on kernel BTF id instead of program-local BTF id: struct_ops BPF prog can be re-used between multiple .struct_ops & .struct_ops.link as long as it's the same struct_ops struct definition and the same function pointer field This allows reusing same BPF program for versioned struct_ops map definitions, e.g.: SEC("struct_ops/test") int BPF_PROG(foo) { ... } struct some_ops___v1 { int (*test)(void); }; struct some_ops___v2 { int (*test)(void); }; SEC(".struct_ops.link") struct some_ops___v1 a = { .test = foo } SEC(".struct_ops.link") struct some_ops___v2 b = { .test = foo } Signed-off-by: Eduard Zingerman --- tools/lib/bpf/libbpf.c | 49 ++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index e2a4c409980b..2c0cb72bc7a4 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1146,8 +1146,32 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map) if (mod_btf) prog->attach_btf_obj_fd = mod_btf->fd; - prog->attach_btf_id = kern_type_id; - prog->expected_attach_type = kern_member_idx; + + /* if we haven't yet processed this BPF program, record proper + * attach_btf_id and member_idx + */ + if (!prog->attach_btf_id) { + prog->attach_btf_id = kern_type_id; + prog->expected_attach_type = kern_member_idx; + } + + /* struct_ops BPF prog can be re-used between multiple + * .struct_ops & .struct_ops.link as long as it's the + * same struct_ops struct definition and the same + * function pointer field + */ + if (prog->attach_btf_id != kern_type_id) { + pr_warn("struct_ops init_kern %s func ptr %s: invalid reuse of prog %s in sec %s with type %u: attach_btf_id %u != kern_type_id %u\n", + map->name, mname, prog->name, prog->sec_name, prog->type, + prog->attach_btf_id, kern_type_id); + return -EINVAL; + } + if (prog->expected_attach_type != kern_member_idx) { + pr_warn("struct_ops init_kern %s func ptr %s: invalid reuse of prog %s in sec %s with type %u: expected_attach_type %u != kern_member_idx %u\n", + map->name, mname, prog->name, prog->sec_name, prog->type, + prog->expected_attach_type, kern_member_idx); + return -EINVAL; + } st_ops->kern_func_off[i] = kern_data_off + kern_moff; @@ -9428,27 +9452,6 @@ static int bpf_object__collect_st_ops_relos(struct bpf_object *obj, return -EINVAL; } - /* if we haven't yet processed this BPF program, record proper - * attach_btf_id and member_idx - */ - if (!prog->attach_btf_id) { - prog->attach_btf_id = st_ops->type_id; - prog->expected_attach_type = member_idx; - } - - /* struct_ops BPF prog can be re-used between multiple - * .struct_ops & .struct_ops.link as long as it's the - * same struct_ops struct definition and the same - * function pointer field - */ - if (prog->attach_btf_id != st_ops->type_id || - prog->expected_attach_type != member_idx) { - pr_warn("struct_ops reloc %s: cannot use prog %s in sec %s with type %u attach_btf_id %u expected_attach_type %u for func ptr %s\n", - map->name, prog->name, prog->sec_name, prog->type, - prog->attach_btf_id, prog->expected_attach_type, name); - return -EINVAL; - } - st_ops->progs[member_idx] = prog; /* st_ops->data will be exposed to users, being returned by From patchwork Sat Mar 2 01:19:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579326 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f179.google.com (mail-lj1-f179.google.com [209.85.208.179]) (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 445A8566A for ; Sat, 2 Mar 2024 01:19:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342381; cv=none; b=pRjcrZ9/QRXiqYfFkeETKtTpC1QRoEM6yok4zErpO9QNh852PdMwGmqWQoCyZyvox1Alhnh5DMt+s0QftR9tuVnMI4P3Hg+h1MYQeyRPKbzgZAvSjRLxtTDVzeMtUD0pA79BLCrb7IHfYCv9rSTzS6z/vq8iNp9Nt00CQJyN0t0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342381; c=relaxed/simple; bh=MxAP1Mj+92zhg+N4eBULQMbyQMO4cUeudjXhJFMAEm8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YixHGZySwTtBz7Nh/BVsX26rYnUKL4RBwdNfF8L9rRZFQg68/W5RczASLSH43zByGTdUbFlFAKXpSbK+blQlcxK42s4vehn8gwmbi1HvrwoTzBGJGzBr9JrmEdd5S1pWaxcbrqPqL+7ftFahlK9cBUazWm8u5QX426DhgIQyq44= 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=VJ/J88ux; arc=none smtp.client-ip=209.85.208.179 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="VJ/J88ux" Received: by mail-lj1-f179.google.com with SMTP id 38308e7fff4ca-2d275e63590so35414521fa.2 for ; Fri, 01 Mar 2024 17:19:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342378; x=1709947178; 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=ac39i9sTIpkqIzsBXHyyh1hd2HLA5BkCt5GBSAjk6Yo=; b=VJ/J88uxnNZY9/3Q/hDIPtkE63zlZwgLKZ2vQ5SRvKUicIvlXBcYDdG88rlKKfDPHX 0dksCFKJYgO8pRLrPNQUd1UBove4ocYdpH+zOp+9Ic15DmIZUL9SnrzZEZUuUvI4F7vq SyDsqcUOg2SXx7MuJYsgr5803GgFWq178k3ZcB0YuxRjaLKsrYdlBKkfJ87LSbOKaLiT 8iSHSaSOjM/Sb8wht1JKckGj9iOgYJOqyvRa9OxV9EWlPUT3rBtkKZXzMNq+/MftsUjF hcO7ZtaWXLJ6/aX9yQmsG4jFo2GqD43yZJKYOo+xwzCSBqs8TyZhM0KmY0qbB7Z1pEGt dm2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342378; x=1709947178; 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=ac39i9sTIpkqIzsBXHyyh1hd2HLA5BkCt5GBSAjk6Yo=; b=C62SQJjyGSgBq4GWWpvU8nivghanvhH6Pk/q6pcNb4UtgeRrCgHHQl0vig7Fe2oJlq XWTGLT2wviZ50YhIuz5PHCjFZrb92juL7WhpSJcuDR8Y3W4ArbOWylraOUScHgtw9/zX BK92qgxgzNcfcQGOvk6ICyMVCuPLQgP9TZUj1EPiWeYtW+oGeDOTOYgiNZW5WBhGuU9K 7RNPp8lsgaxlNObH/AsrHKvwkPRyJWjSdO2GhfH+CIRTGzr3jYOGF0Nxll6DDQxIaMin w/mubOyy1s9h+eptyMjqlWwF91AfqG9Y6J1+rlk7jvdceHBH4gzQeJ/Kh1r2Vq1G5Q9C mOAQ== X-Gm-Message-State: AOJu0Yw1TCfMvHhsKi/89HHz1JABdVY2onYIoljM0KH3lG17dNpiQlYy 7CsqAo13OcSe6ceM1/I+gZbXH8G3y0NazRCDdJTi7Xbhbpmarzy2QyZDA7Ue X-Google-Smtp-Source: AGHT+IF6bhZ20IaN1HeBs4utrzgkmYgavXdnKFH+q5OXZW1WSu0CrgFijx+cpEVbw64PjVRxQvf6Tg== X-Received: by 2002:a2e:9254:0:b0:2d2:4637:63f with SMTP id v20-20020a2e9254000000b002d24637063fmr2183975ljg.45.1709342378145; Fri, 01 Mar 2024 17:19:38 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:37 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 03/15] libbpf: honor autocreate flag for struct_ops maps Date: Sat, 2 Mar 2024 03:19:08 +0200 Message-ID: <20240302011920.15302-4-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 Skip load steps for struct_ops maps not marked for automatic creation. This should allow to load bpf object in situations like below: SEC("struct_ops/foo") int BPF_PROG(foo) { ... } SEC("struct_ops/bar") int BPF_PROG(bar) { ... } struct test_ops___v1 { int (*foo)(void); }; struct test_ops___v2 { int (*foo)(void); int (*does_not_exist)(void); }; SEC(".struct_ops.link") struct test_ops___v1 map_for_old = { .test_1 = (void *)foo }; SEC(".struct_ops.link") struct test_ops___v2 map_for_new = { .test_1 = (void *)foo, .does_not_exist = (void *)bar }; Suppose program is loaded on old kernel that does not have definition for 'does_not_exist' struct_ops member. After this commit it would be possible to load such object file after the following tweaks: bpf_program__set_autoload(skel->progs.bar, false); bpf_map__set_autocreate(skel->maps.map_for_new, false); Acked-by: David Vernet Signed-off-by: Eduard Zingerman --- tools/lib/bpf/libbpf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 2c0cb72bc7a4..25c452c20d7d 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1209,7 +1209,7 @@ static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj) for (i = 0; i < obj->nr_maps; i++) { map = &obj->maps[i]; - if (!bpf_map__is_struct_ops(map)) + if (!bpf_map__is_struct_ops(map) || !map->autocreate) continue; err = bpf_map__init_kern_struct_ops(map); @@ -8136,7 +8136,7 @@ static int bpf_object_prepare_struct_ops(struct bpf_object *obj) int i; for (i = 0; i < obj->nr_maps; i++) - if (bpf_map__is_struct_ops(&obj->maps[i])) + if (bpf_map__is_struct_ops(&obj->maps[i]) && obj->maps[i].autocreate) bpf_map_prepare_vdata(&obj->maps[i]); return 0; From patchwork Sat Mar 2 01:19:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579327 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f176.google.com (mail-lj1-f176.google.com [209.85.208.176]) (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 8F8A463CB for ; Sat, 2 Mar 2024 01:19:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342383; cv=none; b=Y7+3LXcXHlvLM/47Htnc5sYTB9+/5PDKCCinh7ek1ywp/SVsOthYhYX79lv7umBLJbK5mCmhACzWISiyMVHgWmUMLMjZWEXvGgWVHPBAcPsuo3ws4k8el0XxFaY1XziYm70vZAbYNTh6x1Wrn6DHvwPXoYjhy3axD0A8hPU+0yI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342383; c=relaxed/simple; bh=cEPnyfFWBE9D0OLMyGcbpMzkO4Je1leH8rHBZdxQL3M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Cra5CbkYWxCpIi8sS6a3YbZH3VHG5miRQRE94kxlp7SqwMl6/BhAmC1uen35/BmDoevd40DD3/W7go7M7Mm7dvPoCR5/Ztu4ZaegarDopbTTjEDf3h/pxuN24+ORT84n1QaeReCTdn1QrQiQpwUWsUyUfYkOlh6RWblJZ+XUM3s= 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=hirxtFR4; arc=none smtp.client-ip=209.85.208.176 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="hirxtFR4" Received: by mail-lj1-f176.google.com with SMTP id 38308e7fff4ca-2d21cdbc85bso28743701fa.2 for ; Fri, 01 Mar 2024 17:19:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342379; x=1709947179; 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=uC9tgxkqjgWUVRZpjwzr6XxoNQs3LWLYSDd6/trJreM=; b=hirxtFR4WmthJ0eNkHUrWZIr23mwXwmx56aaguD0KBHAs3BRTZUuZbQ1YEBB+gNiiP Fd8yPK9Yv/a4399Rhbupi0sebMfwAVHMEBoUXKDgsbb2H5yh96dkGpliFeptRKgG1Q9Q cUoI0k5N3qkUil68inqtikxXX7UVerxFpx8lY03uuGoacqGsrUOg98XZ1zChI65nDNbR vPoX3UaJPkX19+7++a5P4sywGqHSJ1vmGrQvWdFNB6+25J+2oOZcpMB53UGVSaRaqpS0 0r4QlHURwHNLnLgT7h3nGNBFGYFNuBKXXZ+2cEj/laaDZsHWuaGSQrGmVzVzFgnWMgJq N+dA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342379; x=1709947179; 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=uC9tgxkqjgWUVRZpjwzr6XxoNQs3LWLYSDd6/trJreM=; b=pk4YpDv++ZvibLAq8e82zGEuOU1tg6oWc5Ml1+2LbzlrYrZKm9giSIOHpw8TpCsYaf 6d6LENmKy9dMWbSS8ul7J6OiyRU8C1FtOnBs1aV++OZB6J4kA03xJJOFT50DE2Md9r3Q mSgS9VQk3oprU9OtC8hHo8cVQi3S8thbXzETxWufFWNEwXmE0CL+aZjltXHLy7oxRd5v XZuTpRAJ8C6pfp6NkRb6+YDIyf6Sp1Q9d/RyMGMCMRGxB8v+5Tkg2SkCeOeNaQ4K3b2P AVe20IiY4HtCxzlGJCBDPFOgh1H6093BHqLv1DuzAGIw+TBRxxXZjAI+wtWBEzsbHGol L7uA== X-Gm-Message-State: AOJu0YxeT7zBZSPJgMjWx2p0NhZsH34cFGwyQfrA/bzyIvrAvTGdqRm/ Wp86Cdw6nOQVSb2GYmRmkHq0DVP777/UmvraVhzxIof762WrqGoOjaU6Cbmj X-Google-Smtp-Source: AGHT+IGDwsRcMl80cox7tlRmbE5Hp/iOrshPsijxzmXPV3cf2wbffYzRT8DqFihAC4bSGTsSSWUODg== X-Received: by 2002:a05:651c:224:b0:2d2:bd9:f100 with SMTP id z4-20020a05651c022400b002d20bd9f100mr2282445ljn.20.1709342379288; Fri, 01 Mar 2024 17:19:39 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:38 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 04/15] selftests/bpf: test struct_ops map definition with type suffix Date: Sat, 2 Mar 2024 03:19:09 +0200 Message-ID: <20240302011920.15302-5-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 Extend struct_ops_module test case to check if it is possible to use '___' suffixes for struct_ops type specification. Acked-by: David Vernet Signed-off-by: Eduard Zingerman --- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 1 + .../bpf/prog_tests/test_struct_ops_module.c | 33 ++++++++++++++----- .../selftests/bpf/progs/struct_ops_module.c | 21 +++++++++++- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c index 098ddd067224..b9ff88e3d463 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -564,6 +564,7 @@ static int bpf_dummy_reg(void *kdata) { struct bpf_testmod_ops *ops = kdata; + ops->test_1(); /* Some test cases (ex. struct_ops_maybe_null) may not have test_2 * initialized, so we need to check for NULL. */ 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 7d6facf46ebb..ee5372c7f2c7 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 @@ -30,11 +30,29 @@ static void check_map_info(struct bpf_map_info *info) close(fd); } +static int attach_ops_and_check(struct struct_ops_module *skel, + struct bpf_map *map, + int expected_test_2_result) +{ + struct bpf_link *link; + + link = bpf_map__attach_struct_ops(map); + ASSERT_OK_PTR(link, "attach_test_mod_1"); + if (!link) + return -1; + + /* test_{1,2}() would be called from bpf_dummy_reg() in bpf_testmod.c */ + ASSERT_EQ(skel->bss->test_1_result, 0xdeadbeef, "test_1_result"); + ASSERT_EQ(skel->bss->test_2_result, expected_test_2_result, "test_2_result"); + + bpf_link__destroy(link); + return 0; +} + static void test_struct_ops_load(void) { struct struct_ops_module *skel; struct bpf_map_info info = {}; - struct bpf_link *link; int err; u32 len; @@ -59,20 +77,17 @@ static void test_struct_ops_load(void) if (!ASSERT_OK(err, "bpf_map_get_info_by_fd")) goto cleanup; - link = bpf_map__attach_struct_ops(skel->maps.testmod_1); - ASSERT_OK_PTR(link, "attach_test_mod_1"); - + check_map_info(&info); /* test_3() will be called from bpf_dummy_reg() in bpf_testmod.c * * In bpf_testmod.c it will pass 4 and 13 (the value of data) to * .test_2. So, the value of test_2_result should be 20 (4 + 13 + * 3). */ - ASSERT_EQ(skel->bss->test_2_result, 20, "check_shadow_variables"); - - bpf_link__destroy(link); - - check_map_info(&info); + if (!attach_ops_and_check(skel, skel->maps.testmod_1, 20)) + goto cleanup; + if (!attach_ops_and_check(skel, skel->maps.testmod_2, 12)) + goto cleanup; cleanup: struct_ops_module__destroy(skel); diff --git a/tools/testing/selftests/bpf/progs/struct_ops_module.c b/tools/testing/selftests/bpf/progs/struct_ops_module.c index 25952fa09348..026cabfa7f1f 100644 --- a/tools/testing/selftests/bpf/progs/struct_ops_module.c +++ b/tools/testing/selftests/bpf/progs/struct_ops_module.c @@ -7,12 +7,14 @@ 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) { - return 0xdeadbeef; + test_1_result = 0xdeadbeef; + return 0; } SEC("struct_ops/test_2") @@ -35,3 +37,20 @@ struct bpf_testmod_ops testmod_1 = { .data = 0x1, }; +SEC("struct_ops/test_2") +void BPF_PROG(test_2_v2, int a, int b) +{ + test_2_result = a * b; +} + +struct bpf_testmod_ops___v2 { + int (*test_1)(void); + void (*test_2)(int a, int b); + int (*test_maybe_null)(int dummy, struct task_struct *task); +}; + +SEC(".struct_ops.link") +struct bpf_testmod_ops___v2 testmod_2 = { + .test_1 = (void *)test_1, + .test_2 = (void *)test_2_v2, +}; From patchwork Sat Mar 2 01:19:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579328 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f169.google.com (mail-lj1-f169.google.com [209.85.208.169]) (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 B255A7464 for ; Sat, 2 Mar 2024 01:19:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342384; cv=none; b=Fm7ai4A/pbPc0ilyTqBJENa3KjwcsRZhplkQNJDmypiLZzyW0fqMuDJExCPcuPdkVSUzf7MdQOhJMxydKXC1dsTOduWRNhEp+1C2T5Pu2kKezPGhlItNqFk4tMvx5toUAsh0MTry/Avfj3xd2KJHI+E/nlPFya+XeedTNWVos8Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342384; c=relaxed/simple; bh=zV/RB5EXo6qwUaRe+c4r7c329aduzNSbkkEXrlo/8L4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Kk3Y8u28WZ+BlBUYA8dvEAsZLEch0UYz+9OiiDzqpcUZbNhoPiCCV8VrGpQIEGueKLcjrlRyu72PQoubfusxdxXP6zV3Cj0HeoyuROz54BpB05+TdiOwLMZKh+tliUIMKzlRUNMaAF9LTHna7gXIELeWB4pW0qSJkVZUY6wDyys= 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=isWawBPd; arc=none smtp.client-ip=209.85.208.169 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="isWawBPd" Received: by mail-lj1-f169.google.com with SMTP id 38308e7fff4ca-2d311081954so26546761fa.2 for ; Fri, 01 Mar 2024 17:19:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342380; x=1709947180; 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=ki6OFj/2RK5ad0QaqWrtytsE2QFidQdKKWOOEY5Z/2E=; b=isWawBPdt2ICtKjc7HmgZPM2hX6l6+NyR+oDycdy7DedPKr2SnVxoSE4MOuY3Qlioj HpTdzGH8O2sD3vYAiPSdl6pVJWSHSh9AOBscvUyqNJg6JP50Yr05j3miotzNWzmYvF9f ULIt6GDE51uQlz96HJubcxraY2qHmK5BM2P/xRtbZw1cj7N0RKEU+G/ixz55eISO+7xn +ANWJ6LvbNhs8uAxDYoAqmnXe9ls4hSRiW1+7Cx9wkRbkiiudgrLz8TN8pSUinmDUFxp oWH/pNFEryUS6cLlaGY6t0/8jFP4DqwPTZAtCuMHiT+giC3X/GxkuG52Ye7MCl6jdh22 6Z/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342380; x=1709947180; 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=ki6OFj/2RK5ad0QaqWrtytsE2QFidQdKKWOOEY5Z/2E=; b=vLCMrLM5sS7WPZUQ7E55VMmjJFodxZeBv5lwJBD+MKH4Ewj0Ja2bbnpCdwVLQG0dzS o3+dgdS3wGEU9qQrdHztH3eb8khQxR9BNyrlOPpMtIiR8DteehhI0LhpyqT1cXHn/0J1 SGnTBOaQu7D7zQvX2uAAzITQ0H25Ou+G7m9OGkrVtqJlULPZ2IcLxvyfBIrr9PY1fUdM bEGTp2UoHcsGzYwDnmtzH18QysbvZ3SKkl/44uSCQwgsnCcFsN0WhvJBqF8wjhx5v7XT Cp/CZtlgC5KPC1508xS8nWvGcgQV7LIHc2ehiJOszO44aAJfPgu9FNc5aNixGN8433V+ 7VMw== X-Gm-Message-State: AOJu0YzuawfFdCq4BJcEsoknoZCUfe74lSiSwAMq364vOJdoKu01gdRh ptJ5LxblM8Le7T45p6XGxuyrJbTC2ptN+GEP+ie95uMWgwbX5cJj0/2tq1l/ X-Google-Smtp-Source: AGHT+IG6aEVx4SSZ1+al5/1POg1gBrwe4nYJaVpWgzZqYms5+svLFp+VRgbdFw5T6cHw3rltGCWSbQ== X-Received: by 2002:a2e:9bc6:0:b0:2d2:3b18:6ffd with SMTP id w6-20020a2e9bc6000000b002d23b186ffdmr2150465ljj.41.1709342380448; Fri, 01 Mar 2024 17:19:40 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:40 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 05/15] selftests/bpf: utility functions to capture libbpf log in test_progs Date: Sat, 2 Mar 2024 03:19:10 +0200 Message-ID: <20240302011920.15302-6-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 Several test_progs tests already capture libbpf log in order to check for some expected output, e.g bpf_tcp_ca.c, kfunc_dynptr_param.c, log_buf.c and a few others. This commit provides a, hopefully, simple API to capture libbpf log w/o necessity to define new print callback in each test: /* Creates a global memstream capturing all output passed to * libbpf_print_fn. * Returns 0 on success, negative value on failure. * On failure the description is printed using PRINT_FAIL and * current test case is marked as fail. */ int start_libbpf_log_capture(void) /* Destroys global memstream created by start_libbpf_log_capture(). * Returns a pointer to captured data which has to be freed. * Returned buffer is null terminated. */ char *stop_libbpf_log_capture(void) The intended usage is as follows: if (start_libbpf_log_capture()) return; use_libbpf(); char *log = stop_libbpf_log_capture(); ASSERT_HAS_SUBSTR(log, "... expected ...", "expected some message"); free(log); As a safety measure, free(start_libbpf_log_capture()) is invoked in the epilogue of the test_progs.c:run_one_test(). Signed-off-by: Eduard Zingerman --- tools/testing/selftests/bpf/test_progs.c | 57 ++++++++++++++++++++++++ tools/testing/selftests/bpf/test_progs.h | 3 ++ 2 files changed, 60 insertions(+) diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c index 808550986f30..698c7387b310 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -683,9 +683,65 @@ static const struct argp_option opts[] = { {}, }; +static FILE *libbpf_capture_stream; + +static struct { + char *buf; + size_t buf_sz; +} libbpf_output_capture; + +/* Creates a global memstream capturing all output passed to libbpf_print_fn. + * Returns 0 on success, negative value on failure. + * On failure the description is printed using PRINT_FAIL and + * current test case is marked as fail. + */ +int start_libbpf_log_capture(void) +{ + if (libbpf_capture_stream) { + PRINT_FAIL("%s: libbpf_capture_stream != NULL\n", __func__); + return -EINVAL; + } + + libbpf_capture_stream = open_memstream(&libbpf_output_capture.buf, + &libbpf_output_capture.buf_sz); + if (!libbpf_capture_stream) { + PRINT_FAIL("%s: open_memstream failed errno=%d\n", __func__, errno); + return -EINVAL; + } + + return 0; +} + +/* Destroys global memstream created by start_libbpf_log_capture(). + * Returns a pointer to captured data which has to be freed. + * Returned buffer is null terminated. + */ +char *stop_libbpf_log_capture(void) +{ + char *buf; + + if (!libbpf_capture_stream) + return NULL; + + fputc(0, libbpf_capture_stream); + fclose(libbpf_capture_stream); + libbpf_capture_stream = NULL; + /* get 'buf' after fclose(), see open_memstream() documentation */ + buf = libbpf_output_capture.buf; + bzero(&libbpf_output_capture, sizeof(libbpf_output_capture)); + return buf; +} + static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) { + if (libbpf_capture_stream) { + va_list args2; + + va_copy(args2, args); + vfprintf(libbpf_capture_stream, format, args2); + } + if (env.verbosity < VERBOSE_VERY && level == LIBBPF_DEBUG) return 0; vfprintf(stdout, format, args); @@ -1081,6 +1137,7 @@ static void run_one_test(int test_num) cleanup_cgroup_environment(); stdio_restore(); + free(stop_libbpf_log_capture()); dump_test_log(test, state, false, false, NULL); } diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h index 80df51244886..0ba5a20b19ba 100644 --- a/tools/testing/selftests/bpf/test_progs.h +++ b/tools/testing/selftests/bpf/test_progs.h @@ -397,6 +397,9 @@ int test__join_cgroup(const char *path); system(cmd); \ }) +int start_libbpf_log_capture(void); +char *stop_libbpf_log_capture(void); + static inline __u64 ptr_to_u64(const void *ptr) { return (__u64) (unsigned long) ptr; From patchwork Sat Mar 2 01:19:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579329 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f173.google.com (mail-lj1-f173.google.com [209.85.208.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 C70217489 for ; Sat, 2 Mar 2024 01:19:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342385; cv=none; b=NuPluXKUzFhM9/jrpAYb/vxchZEtlc0XJeH436EBCQgiq2l2owRFSvU7eyBV6d3QeZ/Pa1bA0YFIsCG4YW67BXwpBeExLEVz3I5ZKt3xr1W3uvDTIGZHh+21IguB+ue3+JSdoXUfUPfsB2I1xJDmRDVvC95ztmaYnUVEB51UacU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342385; c=relaxed/simple; bh=T9Tg69UkPoHBYpyj0IQGWeVNmt0BhhIt5JaJNfhMswQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=levpQub81oSD6NUHoqcihRc55X4mhxMOufCXBwY1b6w0b90bIFVmOUkGieSJDOn779f01abi8aSpItXzcETgljLrRVuzXPiAO2CQVeLzt2bU9PW5ZJkuq9P0AQfXZNSMCmXY5ArPRu/UcjHE1IUkk12M28I/Zgtwz/bWdfcva90= 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=dgCAYCSa; arc=none smtp.client-ip=209.85.208.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="dgCAYCSa" Received: by mail-lj1-f173.google.com with SMTP id 38308e7fff4ca-2d33986dbc0so14929251fa.2 for ; Fri, 01 Mar 2024 17:19:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342381; x=1709947181; 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=HloUQLeyf3X91OgKXgvzmZERUb4RBb98Jjf4ea1gRCQ=; b=dgCAYCSaI4CifMDfbzGYlva4F/TFYHpQfFsj4QbJAa6rceGWHLQM8gwSTUwgzewaKf LeX7YVtfIMv8EpEbI+IVb2r1TLO1D884gAehxnWlhzo8Tri2strB/K4Kpcd2BnaIMd1A cNStKkR79hTpvvZmN8Ws9Emv4+p1mo2h33aq8vcUYMXQXUIVfkklNGYK0hy8MsjDDNSZ 1mPQfMwlQe6b6g4Z6T31uWB7LUb+f0AZRDiKhFwWuNqpwOImwq+bY85LzYy96il6oIp9 eGzt5fa8njlqjCWcJA3dirHOG+wrEoywDjktOGNrJEV54FF37OYxnF+Mq54sJHfZS+Ty /4RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342381; x=1709947181; 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=HloUQLeyf3X91OgKXgvzmZERUb4RBb98Jjf4ea1gRCQ=; b=WzdWXvACcKu/tR7O+1lNUCg/nyKo1LHPGUZPx2tiPoxhW5ZMG7Pd9Z+ccEBSI2tw0V QwJe897AYhTI4om8tNbpPdjBIsEuT+pF/qH9Wn4RaZnCve6edh5qg4gIyt/c4nnvofAL kRgHvuZd1nf1ZP4tCTzOoKZ2CIhDTvUQVMjrHurDK8GGlFECQSYMKSv4d+aWuaVAUxdW 6WFQAHlRD/D0UcFXcfpedlhCiK9u0IOu60Xg0se73grSU1a42yVHcw0lASUGW7mLHTMI kjfsUUmDYwgZgCtIwTkAdPTuIjrDay1UhglGBpFwgQRziliPW1sDpvHukwpJEtIOqrzm svzA== X-Gm-Message-State: AOJu0Yyyolq/c6miUhQN46JcGhudRq+xG0J95WoCumuR+sUOgSZ9KCW0 jt/rHebpzYwhsaQonq4busXmEMitzub62L5t6Mo9kNkULbYI9LPQpSiDveOa X-Google-Smtp-Source: AGHT+IGMmQqnz0jtNPyMIDrVpZe7oVjFnPrHqk3/tlH50EFDiNArZAyReIDQS9v0/imAch/8Rsu5+Q== X-Received: by 2002:a2e:a9a8:0:b0:2d3:72e8:1a66 with SMTP id x40-20020a2ea9a8000000b002d372e81a66mr5343ljq.23.1709342381617; Fri, 01 Mar 2024 17:19:41 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:41 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 06/15] selftests/bpf: bad_struct_ops test Date: Sat, 2 Mar 2024 03:19:11 +0200 Message-ID: <20240302011920.15302-7-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 When loading struct_ops programs kernel requires BTF id of the struct_ops type and member index for attachment point inside that type. This makes it not possible to have same BPF program used in struct_ops maps that have different struct_ops type. Check if libbpf rejects such BPF objects files. Signed-off-by: Eduard Zingerman --- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 24 +++++++++++++ .../selftests/bpf/bpf_testmod/bpf_testmod.h | 4 +++ .../selftests/bpf/prog_tests/bad_struct_ops.c | 35 +++++++++++++++++++ .../selftests/bpf/progs/bad_struct_ops.c | 25 +++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bad_struct_ops.c create mode 100644 tools/testing/selftests/bpf/progs/bad_struct_ops.c diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c index b9ff88e3d463..2de7e80dbb4b 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -610,6 +610,29 @@ struct bpf_struct_ops bpf_bpf_testmod_ops = { .owner = THIS_MODULE, }; +static int bpf_dummy_reg2(void *kdata) +{ + struct bpf_testmod_ops2 *ops = kdata; + + ops->test_1(); + return 0; +} + +static struct bpf_testmod_ops2 __bpf_testmod_ops2 = { + .test_1 = bpf_testmod_test_1, +}; + +struct bpf_struct_ops bpf_testmod_ops2 = { + .verifier_ops = &bpf_testmod_verifier_ops, + .init = bpf_testmod_ops_init, + .init_member = bpf_testmod_ops_init_member, + .reg = bpf_dummy_reg2, + .unreg = bpf_dummy_unreg, + .cfi_stubs = &__bpf_testmod_ops2, + .name = "bpf_testmod_ops2", + .owner = THIS_MODULE, +}; + extern int bpf_fentry_test1(int a); static int bpf_testmod_init(void) @@ -621,6 +644,7 @@ static int bpf_testmod_init(void) ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_testmod_kfunc_set); ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_testmod_kfunc_set); ret = ret ?: register_bpf_struct_ops(&bpf_bpf_testmod_ops, bpf_testmod_ops); + ret = ret ?: register_bpf_struct_ops(&bpf_testmod_ops2, bpf_testmod_ops2); if (ret < 0) return ret; if (bpf_fentry_test1(0) < 0) diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h index 971458acfac3..c51c4eae9ed5 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h @@ -45,4 +45,8 @@ struct bpf_testmod_ops { int data; }; +struct bpf_testmod_ops2 { + int (*test_1)(void); +}; + #endif /* _BPF_TESTMOD_H */ diff --git a/tools/testing/selftests/bpf/prog_tests/bad_struct_ops.c b/tools/testing/selftests/bpf/prog_tests/bad_struct_ops.c new file mode 100644 index 000000000000..9f5dbefa0dd9 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/bad_struct_ops.c @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include "bad_struct_ops.skel.h" + +static void invalid_prog_reuse(void) +{ + struct bad_struct_ops *skel; + char *log = NULL; + int err; + + skel = bad_struct_ops__open(); + if (!ASSERT_OK_PTR(skel, "bad_struct_ops__open")) + return; + + if (start_libbpf_log_capture()) + goto cleanup; + + err = bad_struct_ops__load(skel); + log = stop_libbpf_log_capture(); + ASSERT_ERR(err, "bad_struct_ops__load should fail"); + ASSERT_HAS_SUBSTR(log, + "struct_ops init_kern testmod_2 func ptr test_1: invalid reuse of prog test_1", + "expected init_kern message"); + +cleanup: + free(log); + bad_struct_ops__destroy(skel); +} + +void test_bad_struct_ops(void) +{ + if (test__start_subtest("invalid_prog_reuse")) + invalid_prog_reuse(); +} diff --git a/tools/testing/selftests/bpf/progs/bad_struct_ops.c b/tools/testing/selftests/bpf/progs/bad_struct_ops.c new file mode 100644 index 000000000000..b7e175cd0af0 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bad_struct_ops.c @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include "../bpf_testmod/bpf_testmod.h" + +char _license[] SEC("license") = "GPL"; + +SEC("struct_ops/test_1") +int BPF_PROG(test_1) { return 0; } + +SEC("struct_ops/test_2") +int BPF_PROG(test_2) { return 0; } + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_1 = { + .test_1 = (void *)test_1, + .test_2 = (void *)test_2 +}; + +SEC(".struct_ops.link") +struct bpf_testmod_ops2 testmod_2 = { + .test_1 = (void *)test_1 +}; From patchwork Sat Mar 2 01:19:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579330 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) (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 090DA749F for ; Sat, 2 Mar 2024 01:19:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342386; cv=none; b=l1zsNjA9gr16mS0ilQj/BHwGYMo95JxvWx93z0V7U14zlh+1JasIMlVPXX3nN6eGX2LcE/8+rbJpTXRF8XqriCvOdLhH2fZ9YjUYN53MjZTvmZ8YOPGtMd6wSCRtoM8/i2EuqMNjMgZM8iAB1v2U8uUUfdacib7Tj++b9S0qW3I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342386; c=relaxed/simple; bh=ZHnJiSrs/nymoXm4ZKu7PUP0TfqOApRaIo2hHzhRC6U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eDJjPOwXBg2JQrBaLtCV4G/9s9s/zzf61bz4HMGkLUGVKaqBJPHvBoUHEpcNn3/+Ba1J4Y41LNAAn+mrwtnC4LWjL1F5ejI7qFeOYnBu+Desgfir9qnzlRqwErV3aJW2LGNyxf+JZTj0qxzUnrSM8DYNqYjpMDIulusCXt+eLyI= 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=VnOa8frp; arc=none smtp.client-ip=209.85.208.182 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="VnOa8frp" Received: by mail-lj1-f182.google.com with SMTP id 38308e7fff4ca-2d180d6bd32so29835611fa.1 for ; Fri, 01 Mar 2024 17:19:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342383; x=1709947183; 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=LcgPQmlhhkA8T/cZTKDZ07IXVO71RfmlWQo5IBVKgLo=; b=VnOa8frpfwwlEvmhyPZCrCF3b8hW3K2jS2kDteFtdawGG4EwLxx9nWCMIoWKnT3W9C gidJInD+YT/PYXSDLy4uNXvgGHSiacOK3JELm+7clIKe+2O6rAXtZjentSikjOdOsClh FH9gl7yLmOm6GYMb3r7b5V7Tvg/MCYuvmjQQ8lmjYRI3p5ltOij25Hic7fgfxE2qJ/JB Bg2kv+Yk+fhdaUfUHZ3DJA+5R8D5M0D1LpeDD9T8hYiRNb3QKB5rvIDThAdAm51EErxR xpCNCEthrUYn6MwLLuv7VCVeAW4PPswR1fK02AwlC4QJRto0HgJg5CeUWwDUzPQMe2tu P0dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342383; x=1709947183; 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=LcgPQmlhhkA8T/cZTKDZ07IXVO71RfmlWQo5IBVKgLo=; b=MIkkBhTRFonwWr6CrSU/Ee3IJUQlobEpRnMAm47/8kxLEY+NEbnPUjkneZz3AH5q/2 5SOLwyyqr+eTUeWAq+irk0j7yifymxneiT1HQn5vfY+3RDHcLa0SqJVVNFOjS2vr0tUG wJUt4GPq+cGaCPXSiS1S40nucsbgwCQbWvpdPMxEwxCLK9tOG2jsyLJZuibHoEKzuhiZ gS/PJTIbc6Zbd+5te6s6m8YCiRWB/flRz9tFxfNz6RQKVwaumR4VLyFcSXniUnbAVPqx ykbd63dLz2ohb4ii+Xdz819LrWnlw0IjdHYgCvZLzQkpHfCPqjpg5Wxs/jGgAxST83E5 8gRg== X-Gm-Message-State: AOJu0YyFNiIQLdU0GGpWL5F/2ufi25euHsx0GKRu3SHSuqnIH++lxNIT Kotgmw/LP5uwQwobCeoHUa6PD8GfVx6V1l5a0gGzJ3TfZkIM1EeQKQk9+VZI X-Google-Smtp-Source: AGHT+IFutDRrzYRIDRirLbLKnddTPf90gp4uIQFaED+vlOLtY2CrLN/ohqwm/JbWkoyPm7njsQfdSA== X-Received: by 2002:a2e:9cd4:0:b0:2d2:d0ba:2589 with SMTP id g20-20020a2e9cd4000000b002d2d0ba2589mr2493579ljj.21.1709342382860; Fri, 01 Mar 2024 17:19:42 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:42 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 07/15] selftests/bpf: test autocreate behavior for struct_ops maps Date: Sat, 2 Mar 2024 03:19:12 +0200 Message-ID: <20240302011920.15302-8-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 Check that bpf_map__set_autocreate() can be used to disable automatic creation for struct_ops maps. Signed-off-by: Eduard Zingerman --- .../bpf/prog_tests/struct_ops_autocreate.c | 77 +++++++++++++++++++ .../bpf/progs/struct_ops_autocreate.c | 42 ++++++++++ 2 files changed, 119 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_autocreate.c diff --git a/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c b/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c new file mode 100644 index 000000000000..c67d0b32b9dc --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include "struct_ops_autocreate.skel.h" + +static void cant_load_full_object(void) +{ + struct struct_ops_autocreate *skel; + char *log; + int err; + + skel = struct_ops_autocreate__open(); + if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open")) + return; + + if (start_libbpf_log_capture()) + goto cleanup; + /* The testmod_2 map BTF type (struct bpf_testmod_ops___v2) doesn't + * match the BTF of the actual struct bpf_testmod_ops defined in the + * kernel, so we should fail to load it if we don't disable autocreate + * for that map. + */ + err = struct_ops_autocreate__load(skel); + log = stop_libbpf_log_capture(); + if (!ASSERT_ERR(err, "struct_ops_autocreate__load")) + goto cleanup; + + ASSERT_HAS_SUBSTR(log, "libbpf: struct_ops init_kern", "init_kern message"); + ASSERT_EQ(err, -ENOTSUP, "errno should be ENOTSUP"); + +cleanup: + free(log); + struct_ops_autocreate__destroy(skel); +} + +static void can_load_partial_object(void) +{ + LIBBPF_OPTS(bpf_object_open_opts, opts); + struct struct_ops_autocreate *skel; + struct bpf_link *link = NULL; + int err; + + skel = struct_ops_autocreate__open_opts(&opts); + if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open_opts")) + return; + + err = bpf_program__set_autoload(skel->progs.test_2, false); + if (!ASSERT_OK(err, "bpf_program__set_autoload")) + goto cleanup; + + err = bpf_map__set_autocreate(skel->maps.testmod_2, false); + if (!ASSERT_OK(err, "bpf_map__set_autocreate")) + goto cleanup; + + err = struct_ops_autocreate__load(skel); + if (ASSERT_OK(err, "struct_ops_autocreate__load")) + goto cleanup; + + link = bpf_map__attach_struct_ops(skel->maps.testmod_1); + if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) + goto cleanup; + + /* test_1() would be called from bpf_dummy_reg2() in bpf_testmod.c */ + ASSERT_EQ(skel->bss->test_1_result, 42, "test_1_result"); + +cleanup: + bpf_link__destroy(link); + struct_ops_autocreate__destroy(skel); +} + +void test_struct_ops_autocreate(void) +{ + if (test__start_subtest("cant_load_full_object")) + cant_load_full_object(); + if (test__start_subtest("can_load_partial_object")) + can_load_partial_object(); +} diff --git a/tools/testing/selftests/bpf/progs/struct_ops_autocreate.c b/tools/testing/selftests/bpf/progs/struct_ops_autocreate.c new file mode 100644 index 000000000000..294d48bb8e3c --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_autocreate.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +int test_1_result = 0; + +SEC("struct_ops/test_1") +int BPF_PROG(test_1) +{ + test_1_result = 42; + return 0; +} + +SEC("struct_ops/test_1") +int BPF_PROG(test_2) +{ + return 0; +} + +struct bpf_testmod_ops___v1 { + int (*test_1)(void); +}; + +struct bpf_testmod_ops___v2 { + int (*test_1)(void); + int (*does_not_exist)(void); +}; + +SEC(".struct_ops.link") +struct bpf_testmod_ops___v1 testmod_1 = { + .test_1 = (void *)test_1 +}; + +SEC(".struct_ops.link") +struct bpf_testmod_ops___v2 testmod_2 = { + .test_1 = (void *)test_1, + .does_not_exist = (void *)test_2 +}; From patchwork Sat Mar 2 01:19:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579331 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f173.google.com (mail-lj1-f173.google.com [209.85.208.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 2880379F0 for ; Sat, 2 Mar 2024 01:19:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342387; cv=none; b=R91/TIkC6+5WbdN/yGthq7UG87Zd5sifH4yynPwQRHhNvu/qaF5R92oPaIGaDlG0t14dvuiTnEPiu5CQ6x56OEB2hunn/bcDaLI0K8GOVrPd6niq5SaIWz4oS/PKEiZYZs4CPcIVFbk/C8icsxNnTh5n97aTZepNFOSz8WQMD7U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342387; c=relaxed/simple; bh=nUkIQZPM3LRiGOpMWExTB9RP1/yUYZ9tgkvLPZ1fzy4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=o1q9B0eoUzaNX1ImY2qOWGaGtDeTL7QfxTCPNk6w68zsRCdUKkOfMoLyIoyGPvAYeO3fFU4FlqXdVI9RasAqzzFCqDxVIap3VVxjjwvBbx2wxopiNEKbXE2pApQ4hEvx4OnjuRM4/6kYkKiKKstEgySH7SkIKwW/j5GPHoLyR04= 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=j9FgWaU/; arc=none smtp.client-ip=209.85.208.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="j9FgWaU/" Received: by mail-lj1-f173.google.com with SMTP id 38308e7fff4ca-2d180d6bd32so29835701fa.1 for ; Fri, 01 Mar 2024 17:19:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342384; x=1709947184; 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=UhUPWk3UjTaTqNBC5ATm8e8X0DkmyV8N6srirKnp6dA=; b=j9FgWaU/OSuJkGpZbmLwm3Hn5NCg4xCyds6hWfagJQXWNVWtdD756IfacCDZLfMrvv ZC6z15CEbyTopTqUsIfCkiuMfZVMTIDc5MorYT1lRJIb1IlI78WFzKEfZL/8+QKU88N2 TKcmYPRjuod0vE1RjYNczc197G7IgXG1FwYkPBri8eGPA98ckIKcB8VZUZ7iiFn0pL+X 2+nPFnK7J94ONovZnOEdnDxJ9H6VLZePKBq02GFDWQZ7ypN9PJ6Fs209AXVaCJIdL0ju pq0Uni3ws6z6YwN/Z5V+gQAZ04kgmusNlcraMfqR0wQeU3hMoj9kwngn86nFdQX1biIU Obzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342384; x=1709947184; 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=UhUPWk3UjTaTqNBC5ATm8e8X0DkmyV8N6srirKnp6dA=; b=SVb3u1jzI2aA2aSpdbJz/TqLSsHYasaZphfkpzQ9CsVyJaO7xB668dtYbtHMGaKZoh bAi/KAgZc32FV6UMrc2C8yZVnq9HAYDeJUEtR48BJ/7ev35kngrSI/UlYdlbbLvOk3mS HAFQ3kI3KqIsHGVKG4yZgl4zqpnfVP5RatOVoM1qOYPj5TCJJmjsepU79H9HybLWx+IL yCIj9e6Y4c1sex5JnnjMadH+RMoUHYIk7F53EUkCMOjeVqKz1gx70TiA16FYPXwsvAlA ga2tcyj3d73VHhlm7ilaGWWpApjfpFH7mpAJZud82HtXgX4bWQP3rIHaYm9vy0p6IKpW S6HQ== X-Gm-Message-State: AOJu0YwknMNt8GHD7wp+DG/CVXDWd65tEWsXSZar5koQwVaPaf7Y9y5Y mAh8y40R3TRHPcWXeqnMhpLK+NVCV/fZPDcvPsynr1VffqdZ0xtaF4vUyyxK X-Google-Smtp-Source: AGHT+IGkRas/ZZxHpXWvGAcqywiEbP/UR9GQnRcD5Vlw20dVo4g4OtEAZuT+cYmbQzEn7Hg4YEw1pQ== X-Received: by 2002:a2e:9959:0:b0:2d2:650f:9587 with SMTP id r25-20020a2e9959000000b002d2650f9587mr2623042ljj.13.1709342383996; Fri, 01 Mar 2024 17:19:43 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:43 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 08/15] libbpf: sync progs autoload with maps autocreate for struct_ops maps Date: Sat, 2 Mar 2024 03:19:13 +0200 Message-ID: <20240302011920.15302-9-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 Automatically select which struct_ops programs to load depending on which struct_ops maps are selected for automatic creation. E.g. for the BPF code below: SEC("struct_ops/test_1") int BPF_PROG(foo) { ... } SEC("struct_ops/test_2") int BPF_PROG(bar) { ... } SEC(".struct_ops.link") struct test_ops___v1 A = { .foo = (void *)foo }; SEC(".struct_ops.link") struct test_ops___v2 B = { .foo = (void *)foo, .bar = (void *)bar, }; And the following libbpf API calls: bpf_map__set_autocreate(skel->maps.A, true); bpf_map__set_autocreate(skel->maps.B, false); The autoload would be enabled for program 'foo' and disabled for program 'bar'. To achieve this: - for struct_ops programs referenced from struct_ops maps set autoload property at open() to false; - when creating struct_ops maps set autoload property of referenced programs to true. (Note: struct_ops programs not referenced from any map would have their autoload property set to true by default. If attach_btf_id and expected_attach_type properties would not be specified for such programs manually, the load phase would fail). Signed-off-by: Eduard Zingerman --- tools/lib/bpf/libbpf.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 25c452c20d7d..60d78badfc71 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1151,6 +1151,7 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map) * attach_btf_id and member_idx */ if (!prog->attach_btf_id) { + prog->autoload = true; prog->attach_btf_id = kern_type_id; prog->expected_attach_type = kern_member_idx; } @@ -3187,6 +3188,11 @@ static bool obj_needs_vmlinux_btf(const struct bpf_object *obj) } bpf_object__for_each_program(prog, obj) { + /* Note: struct_ops programs referenced from struct_ops maps + * would have their autoload reset to false after open(), + * but that is fine as corresponding map would trigger + * "needs_vmlinux_btf" anyways. + */ if (!prog->autoload) continue; if (prog_needs_vmlinux_btf(prog)) @@ -9452,6 +9458,12 @@ static int bpf_object__collect_st_ops_relos(struct bpf_object *obj, return -EINVAL; } + /* struct_ops programs autoload is computed depending + * on autocreate property of corresponding maps, + * see bpf_map__init_kern_struct_ops(). + */ + prog->autoload = false; + st_ops->progs[member_idx] = prog; /* st_ops->data will be exposed to users, being returned by From patchwork Sat Mar 2 01:19:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579332 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) (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 4539F6FCB for ; Sat, 2 Mar 2024 01:19:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342389; cv=none; b=kKz/GrULIYJ6WM1JMwlSkykskR5JH040AOqq6vy3nk2ZTHZPhKxVKTmfF2z4e+WQbXq/cayRRrX1djyzglKFWqhTW7ssj9VuB+nnb2lovG+p2Ni0nHHJvjYSqduH1q4229htGDl+VRZC/7VSKyApwUoob5Yr5Kne+Cdb7SDj9oQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342389; c=relaxed/simple; bh=Zstjkfo3bBrEk6tk8nr74zGWRnaol7vk00gw0zqL1lI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gV+/H/0N6QPyRavsX860FF3mxIDnZXF2bf8TPgqAqtHzAFRTE5rsQbMK1uZjdO6wu2ITb57gXAcTgPrHim/QyPvpzldw6JNcHHUYQZTQWqZufXUQsvB8TcipONdzMd+KgQOxBRQlHL5zj86o7eOZggixP3zYRjlS5DKedka+6Bg= 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=mHTZ5TZV; arc=none smtp.client-ip=209.85.208.177 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="mHTZ5TZV" Received: by mail-lj1-f177.google.com with SMTP id 38308e7fff4ca-2d2628e81b8so36043931fa.0 for ; Fri, 01 Mar 2024 17:19:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342385; x=1709947185; 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=G8A22T63G5iQ7IQHgXyef1fcl9DNwEtXo9q+rag5wGk=; b=mHTZ5TZV2a9uB85c58i9fPbBdWlC/W0H4/fufZmdtkLJRXy2no0ekHy+uCGrxs6RDD PPZqeoUfV7NnscN7CNojy+9uAS8vwSNElq7dHFdmx+FFJJRCiSfpWcjy0vNb8ar4kc1R 123VHPNJeROBvyt8bsHUIKU4XINIneTv+nC2Mt9VDiQI72BAjyoGydUPIXeOWy1ZuXLn BcEcFCfBAo+SiWSlnrgKvkDVv4lTCp9tDEicoBpSo5qpsIAYxxIh6AV4j72m3ifzi9kg LlZK1uSP+UWMJsNelAQBvhwJFQJmXmodPV8unWgFvVx8b9G1Clq0CxRBfCCgAUvKDTnC vNeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342385; x=1709947185; 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=G8A22T63G5iQ7IQHgXyef1fcl9DNwEtXo9q+rag5wGk=; b=evez3sO3ifIut/XrhiLfdvfLv7TrWT9TMkrHvRBfN/V1q/4l0KRCAX59+KgP6modAN hdcOpju1DSZ11S/5YzQjvR4yXrM/IgFPsTcRLVg+fP5vqE3WWnCtZVNUegqIy5h6tYwB exWicKRpC74Wf280UHqik/rRYCsnsnRaBwy+HOVWYP9dcLBBgi/vtSAPr2fFkOa0Gn4L itKsMYhNP+Ti5Do1u2SYyBCzvCS9A9SZCrxsw7ZapYez5tRg3N2AN+8ORJ7jYBFZWKym q8DW3TkMi5GCl8Ehnc0hfB7ZXmvYjq4ByEhTHlaIxhMCb9TPui2fGZIQz3OzDDXVl3UE 06Ig== X-Gm-Message-State: AOJu0YxUhZYkhHofdHALiCOaX6BKPOXFAjxnvdox2Kl81CzQ9IIu3ULW e1DQLbrl3r/fBqzAaQ2v7SppwClRJOcBwxvu4aZdMhnUeWhidnysPnnbiS3U X-Google-Smtp-Source: AGHT+IEX/Hhfw82vdEDH1SRoXUzZ/MaOGNRfiemh5ROE0UKQnHJCdO/Ue5h+/+6+2BDwhI0HsNkJIw== X-Received: by 2002:a2e:9bd1:0:b0:2d2:e970:f992 with SMTP id w17-20020a2e9bd1000000b002d2e970f992mr1411737ljj.21.1709342385157; Fri, 01 Mar 2024 17:19:45 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:44 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 09/15] selftests/bpf: verify struct_ops autoload/autocreate sync Date: Sat, 2 Mar 2024 03:19:14 +0200 Message-ID: <20240302011920.15302-10-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 Check that autocreate flag set to false for struct_ops map causes autoload flag set to false for corresponding program. Check that struct_ops program not referenced from any map fails to load. Signed-off-by: Eduard Zingerman --- .../selftests/bpf/prog_tests/bad_struct_ops.c | 32 +++++++++++++++++++ .../bpf/prog_tests/struct_ops_autocreate.c | 11 ++++--- .../selftests/bpf/progs/bad_struct_ops2.c | 14 ++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/bad_struct_ops2.c diff --git a/tools/testing/selftests/bpf/prog_tests/bad_struct_ops.c b/tools/testing/selftests/bpf/prog_tests/bad_struct_ops.c index 9f5dbefa0dd9..6a707213e46b 100644 --- a/tools/testing/selftests/bpf/prog_tests/bad_struct_ops.c +++ b/tools/testing/selftests/bpf/prog_tests/bad_struct_ops.c @@ -2,6 +2,7 @@ #include #include "bad_struct_ops.skel.h" +#include "bad_struct_ops2.skel.h" static void invalid_prog_reuse(void) { @@ -28,8 +29,39 @@ static void invalid_prog_reuse(void) bad_struct_ops__destroy(skel); } +static void unused_program(void) +{ + struct bad_struct_ops2 *skel; + char *log = NULL; + int err; + + skel = bad_struct_ops2__open(); + if (!ASSERT_OK_PTR(skel, "bad_struct_ops2__open")) + return; + + /* struct_ops programs not referenced from any maps are open + * with autoload set to true. + */ + ASSERT_TRUE(bpf_program__autoload(skel->progs.foo), "foo autoload == true"); + + if (start_libbpf_log_capture()) + goto cleanup; + + err = bad_struct_ops2__load(skel); + ASSERT_ERR(err, "bad_struct_ops2__load should fail"); + log = stop_libbpf_log_capture(); + ASSERT_HAS_SUBSTR(log, "prog 'foo': failed to load", + "message about 'foo' failing to load"); + +cleanup: + free(log); + bad_struct_ops2__destroy(skel); +} + void test_bad_struct_ops(void) { if (test__start_subtest("invalid_prog_reuse")) invalid_prog_reuse(); + if (test__start_subtest("unused_program")) + unused_program(); } diff --git a/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c b/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c index c67d0b32b9dc..765b0ec6383a 100644 --- a/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c +++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c @@ -35,18 +35,19 @@ static void cant_load_full_object(void) static void can_load_partial_object(void) { - LIBBPF_OPTS(bpf_object_open_opts, opts); struct struct_ops_autocreate *skel; struct bpf_link *link = NULL; int err; - skel = struct_ops_autocreate__open_opts(&opts); + skel = struct_ops_autocreate__open(); if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open_opts")) return; - err = bpf_program__set_autoload(skel->progs.test_2, false); - if (!ASSERT_OK(err, "bpf_program__set_autoload")) - goto cleanup; + /* struct_ops programs referenced from maps are open with + * autoload set to false. + */ + ASSERT_FALSE(bpf_program__autoload(skel->progs.test_1), "test_1 autoload == false"); + ASSERT_FALSE(bpf_program__autoload(skel->progs.test_2), "test_2 autoload == false"); err = bpf_map__set_autocreate(skel->maps.testmod_2, false); if (!ASSERT_OK(err, "bpf_map__set_autocreate")) diff --git a/tools/testing/selftests/bpf/progs/bad_struct_ops2.c b/tools/testing/selftests/bpf/progs/bad_struct_ops2.c new file mode 100644 index 000000000000..64a95f6be86d --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bad_struct_ops2.c @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +char _license[] SEC("license") = "GPL"; + +/* This is an unused struct_ops program, it lacks corresponding + * struct_ops map, which provides attachment information. + * W/o additional configuration attempt to load such + * BPF object file would fail. + */ +SEC("struct_ops/foo") +void foo(void) {} From patchwork Sat Mar 2 01:19:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579333 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f173.google.com (mail-lj1-f173.google.com [209.85.208.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 6FC518F45 for ; Sat, 2 Mar 2024 01:19:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342390; cv=none; b=MEHz+k9Ra57VnIsQk9sfjc6YaCkjTw/F7K7NWgO417tjKZyiQZOwl25uyHCjTikqm21ukld2TA8Jp/zVU5oi70IWNOwkdBVNyrcyJjek7MJ0MjqVgP0IScZaLWVBXyBLEJOEW/3FOUvRRKiRtYCmhpGFL45sjjnIzTYrD/aRMN8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342390; c=relaxed/simple; bh=0yE/DObzJjc2Zg4crGnGVkVCxyt/u6wvLPD1iui76NI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I4DisN/NkEy9HRn09KVX1irZbq+FwkTx2qW/Fk9MylogqUKfXWh+OaH8R8pWOh0FIitn4ICIAKcNSbMNPf8m0HIh1kw+fh4gHyT0zI55Niq8ZLqW9tbxBq4MD7gVwT4ztbUVycmAWO0oSgAtBuHUXxo6QOccyMWexg7uyk3aYFc= 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=lxYpKYwf; arc=none smtp.client-ip=209.85.208.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="lxYpKYwf" Received: by mail-lj1-f173.google.com with SMTP id 38308e7fff4ca-2d347ddc37eso10175151fa.1 for ; Fri, 01 Mar 2024 17:19:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342386; x=1709947186; 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=Ah3fu713JMkYMRn5NrGfdWczvnLtn2aDDPdwpKWiMs4=; b=lxYpKYwfLqgeAw2M5xqWIWDkVz1i26wFCze1r+fM9BMfIsrPJ263M/G/t58RPOTLJW gy11ThWVJ0pL/U8/FwrsLICipyNdGtubWOgp5JSTW2oFWqDNGJml4+ML1uhcKENC9de9 68/BdaQI/bKEx4FJHgZszRntUodlMpPe+7h00g8pw3Lx0tjGaApWPt8CSP7TLr3UJTTB NPIPvIWEjgpXqu9shIr2Dg7AWF/FUhYmWJWyCQBjlpbCCn3vz4WjQ7u25TD79AvCDX79 L1Dj+R5XuhpwZZRxqefExQri/Ma9VATFt8FH8RMl1xs+reTnBSig3a9WNx5j7K6qfEEe EiCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342386; x=1709947186; 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=Ah3fu713JMkYMRn5NrGfdWczvnLtn2aDDPdwpKWiMs4=; b=q7A8g2NAYKQJFzYGymJuOX+ygNZ+FMh0EAumsarbwTjVeodwIrVeeqPXK8wuh77/i+ ly+ej1xH2LaEZCXDPhBFrY0F3di/fR+CO857BavjIDilg9UL94yFq1eicoipWIZxHuAT 3TQn/OFDkxZH3FaQSURLwAvepoub4TwDXwcSS6ALIsdfGpaTMnuZUiiNzrWlT5IN04oU fEkLfyeWsvDxHbnWE9usX/H3lm0aI1ZojJHj1Y6/9F/THqCgSCo2L7hvK1d9pfJeEIgE QbfQBvTLU4nub3g6o0sCr+JT7ijcJa01GZxF5lXxQoXCU73whdsQCNqUEzaspNU+CdHN j8Wg== X-Gm-Message-State: AOJu0YyE4yRu/KFP9D7xzovrgdURPyhWGm81/ZIjehyl9UFbaMQ/KUty ceratSyxSVdHK4fbQyACM0i5ASFgz2Ord1aafC/nsgrqtW2y/QflyD9ZDx2Z X-Google-Smtp-Source: AGHT+IHdsTWF9a8sdkWi4+7MSb1Rn4gJH5pU2Vspsqt4yrbK/OQPQZC3TfAI/TXNDWvsxDjI/YUMVg== X-Received: by 2002:a2e:98c2:0:b0:2d3:ada:29e9 with SMTP id s2-20020a2e98c2000000b002d30ada29e9mr2292178ljj.12.1709342386280; Fri, 01 Mar 2024 17:19:46 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:45 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 10/15] libbpf: replace elf_state->st_ops_* fields with SEC_ST_OPS sec_type Date: Sat, 2 Mar 2024 03:19:15 +0200 Message-ID: <20240302011920.15302-11-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 The next patch would add two new section names for struct_ops maps. To make working with multiple struct_ops sections more convenient: - remove fields like elf_state->st_ops_{shndx,link_shndx}; - mark section descriptions hosting struct_ops as elf_sec_desc->sec_type == SEC_ST_OPS; After these changes struct_ops sections could be processed uniformly by iterating bpf_object->efile.secs entries. Signed-off-by: Eduard Zingerman --- tools/lib/bpf/libbpf.c | 62 ++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 60d78badfc71..8ecfad091cb5 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -612,6 +612,7 @@ enum sec_type { SEC_BSS, SEC_DATA, SEC_RODATA, + SEC_ST_OPS, }; struct elf_sec_desc { @@ -627,8 +628,6 @@ struct elf_state { Elf *elf; Elf64_Ehdr *ehdr; Elf_Data *symbols; - Elf_Data *st_ops_data; - Elf_Data *st_ops_link_data; size_t shstrndx; /* section index for section name strings */ size_t strtabidx; struct elf_sec_desc *secs; @@ -637,8 +636,7 @@ struct elf_state { __u32 btf_maps_sec_btf_id; int text_shndx; int symbols_shndx; - int st_ops_shndx; - int st_ops_link_shndx; + bool has_st_ops; }; struct usdt_manager; @@ -1222,7 +1220,7 @@ static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj) } static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, - int shndx, Elf_Data *data, __u32 map_flags) + int shndx, Elf_Data *data) { const struct btf_type *type, *datasec; const struct btf_var_secinfo *vsi; @@ -1284,7 +1282,8 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, map->def.key_size = sizeof(int); map->def.value_size = type->size; map->def.max_entries = 1; - map->def.map_flags = map_flags; + map->def.map_flags = strcmp(sec_name, STRUCT_OPS_LINK_SEC) == 0 + ? BPF_F_LINK : 0; map->st_ops = calloc(1, sizeof(*map->st_ops)); if (!map->st_ops) @@ -1319,15 +1318,25 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, static int bpf_object_init_struct_ops(struct bpf_object *obj) { - int err; + const char *sec_name; + int sec_idx, err; - err = init_struct_ops_maps(obj, STRUCT_OPS_SEC, obj->efile.st_ops_shndx, - obj->efile.st_ops_data, 0); - err = err ?: init_struct_ops_maps(obj, STRUCT_OPS_LINK_SEC, - obj->efile.st_ops_link_shndx, - obj->efile.st_ops_link_data, - BPF_F_LINK); - return err; + for (sec_idx = 0; sec_idx < obj->efile.sec_cnt; ++sec_idx) { + struct elf_sec_desc *desc = &obj->efile.secs[sec_idx]; + + if (desc->sec_type != SEC_ST_OPS) + continue; + + sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); + if (!sec_name) + return -LIBBPF_ERRNO__FORMAT; + + err = init_struct_ops_maps(obj, sec_name, sec_idx, desc->data); + if (err) + return err; + } + + return 0; } static struct bpf_object *bpf_object__new(const char *path, @@ -1365,8 +1374,6 @@ static struct bpf_object *bpf_object__new(const char *path, obj->efile.obj_buf = obj_buf; obj->efile.obj_buf_sz = obj_buf_sz; obj->efile.btf_maps_shndx = -1; - obj->efile.st_ops_shndx = -1; - obj->efile.st_ops_link_shndx = -1; obj->kconfig_map_idx = -1; obj->kern_version = get_kernel_version(); @@ -1383,8 +1390,6 @@ static void bpf_object__elf_finish(struct bpf_object *obj) elf_end(obj->efile.elf); obj->efile.elf = NULL; obj->efile.symbols = NULL; - obj->efile.st_ops_data = NULL; - obj->efile.st_ops_link_data = NULL; zfree(&obj->efile.secs); obj->efile.sec_cnt = 0; @@ -2929,14 +2934,13 @@ static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) static bool libbpf_needs_btf(const struct bpf_object *obj) { return obj->efile.btf_maps_shndx >= 0 || - obj->efile.st_ops_shndx >= 0 || - obj->efile.st_ops_link_shndx >= 0 || + obj->efile.has_st_ops || obj->nr_extern > 0; } static bool kernel_needs_btf(const struct bpf_object *obj) { - return obj->efile.st_ops_shndx >= 0 || obj->efile.st_ops_link_shndx >= 0; + return obj->efile.has_st_ops; } static int bpf_object__init_btf(struct bpf_object *obj, @@ -3642,12 +3646,12 @@ static int bpf_object__elf_collect(struct bpf_object *obj) sec_desc->sec_type = SEC_RODATA; sec_desc->shdr = sh; sec_desc->data = data; - } else if (strcmp(name, STRUCT_OPS_SEC) == 0) { - obj->efile.st_ops_data = data; - obj->efile.st_ops_shndx = idx; - } else if (strcmp(name, STRUCT_OPS_LINK_SEC) == 0) { - obj->efile.st_ops_link_data = data; - obj->efile.st_ops_link_shndx = idx; + } else if (strcmp(name, STRUCT_OPS_SEC) == 0 || + strcmp(name, STRUCT_OPS_LINK_SEC) == 0) { + sec_desc->sec_type = SEC_ST_OPS; + sec_desc->shdr = sh; + sec_desc->data = data; + obj->efile.has_st_ops = true; } else { pr_info("elf: skipping unrecognized data section(%d) %s\n", idx, name); @@ -6960,12 +6964,12 @@ static int bpf_object__collect_relos(struct bpf_object *obj) data = sec_desc->data; idx = shdr->sh_info; - if (shdr->sh_type != SHT_REL) { + if (shdr->sh_type != SHT_REL || idx < 0 || idx >= obj->efile.sec_cnt) { pr_warn("internal error at %d\n", __LINE__); return -LIBBPF_ERRNO__INTERNAL; } - if (idx == obj->efile.st_ops_shndx || idx == obj->efile.st_ops_link_shndx) + if (obj->efile.secs[idx].sec_type == SEC_ST_OPS) err = bpf_object__collect_st_ops_relos(obj, shdr, data); else if (idx == obj->efile.btf_maps_shndx) err = bpf_object__collect_map_relos(obj, shdr, data); From patchwork Sat Mar 2 01:19:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579334 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f170.google.com (mail-lj1-f170.google.com [209.85.208.170]) (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 AD232883D for ; Sat, 2 Mar 2024 01:19:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342391; cv=none; b=OmAIekgckKmTUkfpAR0S4VZ12oR+5J4ehU7wW1qhi028V7b3j6SOOWa4xVyFXQUQiKGo1BuWyQ06GHflZ6jwLvmvn4BSGecu+jTsju2b0CjXyEAk/ZJ5CMeO+8HSKT4ihT+3d/66JwJ6bzArTxtBw570WP/FE4sSdSh9SW8bZeM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342391; c=relaxed/simple; bh=cIm4tyYIXVCjczcaRR27PEgHR74b6638+xqZA7CFIsg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nm9j1XwI2cgW1n1SywYce6F7CkSNDHR8qvXwhqTddW5iXfNX/RrZfWwU7E1+U/fE6T8f20ercNlNna/f9Yt5yX86+Y2zjFaMlzyGJ1bWMgzhG13ujW876eT1EgDJjkrgPIXGOWeIHLEKvljaKjTZ3jzocHDwIvrVmb3sbYDJ+8w= 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=IZTjYARx; arc=none smtp.client-ip=209.85.208.170 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="IZTjYARx" Received: by mail-lj1-f170.google.com with SMTP id 38308e7fff4ca-2d269dc3575so23703411fa.1 for ; Fri, 01 Mar 2024 17:19:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342387; x=1709947187; 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=CRJRHaBi8OzFwNPV6geePl2rpmmRsHc2/Z3LyoOlYMk=; b=IZTjYARxGZ8LMSsXi8PxX9plZTkaLK2OCoUNIKdTDIg1cpQRntoEc5PW+WEM4aUFIA u5THQ0Jqj5V55ldT7FCqfXmsVelAZcOhklbdzDjLljidOJt6lYlsyYu1qTCWuAVin/q5 KchZo3KwSGLCBZ1SdtS+DB5PNdATPvbqKB/NDL1DPpcPfsadXe70xS7zZNt3wrow7HdE 7bdtQJRrr7o5nERmgazMXkW5DP9A4AnHZYaOBPBq0VNfVgxXtH0CF9R7CPs/CnR/lemo 1Z0XCpKOBL+DpOPEU5yWiJy6wzh9VsGKqoSL08wowykBOSMzeEQm87+ClabJPro0WJ38 wtGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342387; x=1709947187; 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=CRJRHaBi8OzFwNPV6geePl2rpmmRsHc2/Z3LyoOlYMk=; b=Y1SsYNRBU8utJd7HrAzrCEeNn1pXuNvDevLkBkLUVVTZ79xn9rteaTg2g1v12XT3DF IuHp+VF/QtyGhFNdENGZ4vYSks44iu7dzcMt+ns8S2iWG1GAjhkQvhSyLnvC4zZ2smEf B6tCmeIBxMma4pmNcop38Ap0eeLdqh1V/9fkft6xXNCcYwioBdAPOM1jhTRjO4tX39yd LluoeyVgn22fgfAWKeW78nhA8LRgN3eg/TLMr81RDqez0qAgw07kiVzbprkCYrlsrRzN BPAzh0Kao2SZMS0fopMdHT56wjmAudtskN1pS6Ou4NK0LUFaa18tXLVpTFdEz+dUarOe lOIw== X-Gm-Message-State: AOJu0YwmTmzYqKu69X1zPy3QlB+Uc921j3F3cTwxkV7twG+1MyZ1pWqX caetIHryEFo68f96fLDsUxEg1QvEc7FB+tuwgv0Eksn91XHTCwB7kS72U8Y0 X-Google-Smtp-Source: AGHT+IHednO4NvEkDUJtIC7ZeT4t6ijOQSzRAgbLizr8uLZ+M4RoH109Y6k0sbNlnbgOLIBOoXCALg== X-Received: by 2002:a2e:b602:0:b0:2d2:864c:8467 with SMTP id r2-20020a2eb602000000b002d2864c8467mr1551501ljn.20.1709342387396; Fri, 01 Mar 2024 17:19:47 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:47 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 11/15] libbpf: struct_ops in SEC("?.struct_ops") and SEC("?.struct_ops.link") Date: Sat, 2 Mar 2024 03:19:16 +0200 Message-ID: <20240302011920.15302-12-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 Allow using two new section names for struct_ops maps: - SEC("?.struct_ops") - SEC("?.struct_ops.link") To specify maps that have bpf_map->autocreate == false after open. Signed-off-by: Eduard Zingerman --- tools/lib/bpf/libbpf.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8ecfad091cb5..157d28aea186 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -497,6 +497,8 @@ struct bpf_struct_ops { #define KSYMS_SEC ".ksyms" #define STRUCT_OPS_SEC ".struct_ops" #define STRUCT_OPS_LINK_SEC ".struct_ops.link" +#define OPT_STRUCT_OPS_SEC "?.struct_ops" +#define OPT_STRUCT_OPS_LINK_SEC "?.struct_ops.link" enum libbpf_map_type { LIBBPF_MAP_UNSPEC, @@ -1278,6 +1280,15 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, return -ENOMEM; map->btf_value_type_id = type_id; + /* Follow same convention as for programs autoload: + * SEC("?.struct_ops") means map is not created by default. + */ + if (sec_name[0] == '?') { + map->autocreate = false; + /* from now on forget there was ? in section name */ + sec_name++; + } + map->def.type = BPF_MAP_TYPE_STRUCT_OPS; map->def.key_size = sizeof(int); map->def.value_size = type->size; @@ -3647,7 +3658,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj) sec_desc->shdr = sh; sec_desc->data = data; } else if (strcmp(name, STRUCT_OPS_SEC) == 0 || - strcmp(name, STRUCT_OPS_LINK_SEC) == 0) { + strcmp(name, STRUCT_OPS_LINK_SEC) == 0 || + strcmp(name, OPT_STRUCT_OPS_SEC) == 0 || + strcmp(name, OPT_STRUCT_OPS_LINK_SEC) == 0) { sec_desc->sec_type = SEC_ST_OPS; sec_desc->shdr = sh; sec_desc->data = data; @@ -3667,6 +3680,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj) if (!section_have_execinstr(obj, targ_sec_idx) && strcmp(name, ".rel" STRUCT_OPS_SEC) && strcmp(name, ".rel" STRUCT_OPS_LINK_SEC) && + strcmp(name, ".rel" OPT_STRUCT_OPS_SEC) && + strcmp(name, ".rel" OPT_STRUCT_OPS_LINK_SEC) && strcmp(name, ".rel" MAPS_ELF_SEC)) { pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n", idx, name, targ_sec_idx, From patchwork Sat Mar 2 01:19:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579335 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f174.google.com (mail-lj1-f174.google.com [209.85.208.174]) (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 CDFD7A95E for ; Sat, 2 Mar 2024 01:19:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342392; cv=none; b=FnclIfDMHTe8V/BR2TbzQNqfgnfoE8/LbDzXIN8SLCJ3aAFcsL51An2kCysTZ8TCG5kiNKmye7AGwz/X++8LkXLKX5j9Mm2ksxs8IsZOkdFeK9GORsFjYDIzYQ0RsBj1IeqV6gHGKEaok986OlR34WdbF5Rfa4rxfShE4XVdjK0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342392; c=relaxed/simple; bh=MocsDWsqIiR3iXSCyqpTWSv+2V/Qrzfj2a5lHRCtc0k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aV9GB92LRLOhwdFQ3pEvbv0/KMvtoDqZAA2GoxFh74wQ+ad6dUiflY/6WsVkvXym1yDA8w3acG1lwjEx5OZ3U0vYBr9VfpgzvB3YiDwGL+1o6qLbEniNmI/YOrrC+RA2KSrJwz0tYy93cNQsR+b9XlUA3wHN6qR8yg00KygDBdw= 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=Cbvd7/yu; arc=none smtp.client-ip=209.85.208.174 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="Cbvd7/yu" Received: by mail-lj1-f174.google.com with SMTP id 38308e7fff4ca-2d269dc3575so23703451fa.1 for ; Fri, 01 Mar 2024 17:19:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342388; x=1709947188; 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=12Z2+H+2rtu/sqx0a2yGcy38skHVCPSpGAMA3Zq9sXE=; b=Cbvd7/yuIrXljK42cP7x6DSRCkg/lPGF7o/UsIX3TNTsXLrnG6qvqIfK6dG8wQ0P2Y eJYBsoFsUl77nDAT1w/BXXoVzKUMgovQxeTk6HCSuFdbKidixHYa2N1qFu+yG5le2WTM c7M9Wagu3Eq0Nk/nVS45cWk40oBebwlO80R+/WDPchRJxhw7isI0J63xpc7/zfp8PaI3 9ITmhTqNXlnOQ/h79l0wZjelq1jgBLQVEH3JXTJNKeWmr8hN6ocBjKlFlJUQcU/3F058 ht2qNLRx2CTVIFjjSuCasa2C0daDAs8nTZTFvNp6s/2HQzove71leVuGKn1QiPNSMuh6 LiZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342388; x=1709947188; 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=12Z2+H+2rtu/sqx0a2yGcy38skHVCPSpGAMA3Zq9sXE=; b=gJDL4iSHg1WdVp9UPVjsJTqjeEe1aYVbxGig+vm6YiWzqeSCcsmmO8pzhWscX1HdVH trjsMdUiebNQ3yPvBrdDltQIpSEUfijASTiy5y/GfmXXIeJ8dHbnsKkKpbIyTyFJW5fP YayYZlbyzIv56IF8M+dhaAMSHyTnqeqrgma3zFoj+/chEBcvZQC9F10tTDro9/HI3ZLw 8TMDlEUd2W7phtonG2VeeFsgB4UTAmO0KEtgo5qrvFfDuwZrl1yfZqnFCoYhqjbhF8RB Fxtldx96/CnoJuXbjSFmA1X5Y8/i53/gx02Fckt7KaXZmFsYaCGM4q/CC3JUakW3kuXA Zfvg== X-Gm-Message-State: AOJu0YzzCsyG9krmOhhs4+2qQrp6LbsI7ZrLa3jP/J5kf6dnssuwkjxD 2Is1Y6dlJnYxa6GHTMNHN5EPwajjP25YjdfVkMfxXhTPwPHawRpsv3daft9Z X-Google-Smtp-Source: AGHT+IE1RaWiq03CnecwLkaCrzXZK9j1w75VHJGLjoFqcXpuAB0hVEPk2uqd2iYHINQTXveVFWPEhQ== X-Received: by 2002:a2e:b011:0:b0:2d2:c506:4b46 with SMTP id y17-20020a2eb011000000b002d2c5064b46mr2545639ljk.19.1709342388546; Fri, 01 Mar 2024 17:19:48 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:48 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 12/15] libbpf: rewrite btf datasec names starting from '?' Date: Sat, 2 Mar 2024 03:19:17 +0200 Message-ID: <20240302011920.15302-13-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 Optional struct_ops maps are defined using question mark at the start of the section name, e.g.: SEC("?.struct_ops") struct test_ops optional_map = { ... }; This commit teaches libbpf to detect if kernel allows '?' prefix in datasec names, and if it doesn't then to rewrite such names by removing '?' prefix and adding ".optional" suffix. For example: DATASEC ?.struct_ops -> DATASEC .struct_ops.optional Signed-off-by: Eduard Zingerman --- tools/lib/bpf/features.c | 22 ++++++++++++++++++++++ tools/lib/bpf/libbpf.c | 30 +++++++++++++++++++++++++++++- tools/lib/bpf/libbpf_internal.h | 2 ++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/features.c b/tools/lib/bpf/features.c index 6b0738ad7063..4e783cc7fc4b 100644 --- a/tools/lib/bpf/features.c +++ b/tools/lib/bpf/features.c @@ -147,6 +147,25 @@ static int probe_kern_btf_datasec(int token_fd) strs, sizeof(strs), token_fd)); } +static int probe_kern_btf_qmark_datasec(int token_fd) +{ + static const char strs[] = "\0x\0?.data"; + /* static int a; */ + __u32 types[] = { + /* int */ + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + /* VAR x */ /* [2] */ + BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1), + BTF_VAR_STATIC, + /* DATASEC ?.data */ /* [3] */ + BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4), + BTF_VAR_SECINFO_ENC(2, 0, 4), + }; + + return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types), + strs, sizeof(strs), token_fd)); +} + static int probe_kern_btf_float(int token_fd) { static const char strs[] = "\0float"; @@ -534,6 +553,9 @@ static struct kern_feature_desc { [FEAT_ARG_CTX_TAG] = { "kernel-side __arg_ctx tag", probe_kern_arg_ctx_tag, }, + [FEAT_BTF_QMARK_DATASEC] = { + "BTF DATASEC names starting from '?'", probe_kern_btf_qmark_datasec, + }, }; bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_id) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 157d28aea186..af0bfb987928 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -2828,6 +2828,11 @@ static bool section_have_execinstr(struct bpf_object *obj, int idx) return sh->sh_flags & SHF_EXECINSTR; } +static bool starts_with_qmark(const char *s) +{ + return s && s[0] == '?'; +} + static bool btf_needs_sanitization(struct bpf_object *obj) { bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC); @@ -2837,9 +2842,10 @@ static bool btf_needs_sanitization(struct bpf_object *obj) bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG); bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG); bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64); + bool has_qmark_datasec = kernel_supports(obj, FEAT_BTF_QMARK_DATASEC); return !has_func || !has_datasec || !has_func_global || !has_float || - !has_decl_tag || !has_type_tag || !has_enum64; + !has_decl_tag || !has_type_tag || !has_enum64 || !has_qmark_datasec; } static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) @@ -2851,6 +2857,7 @@ static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG); bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG); bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64); + bool has_qmark_datasec = kernel_supports(obj, FEAT_BTF_QMARK_DATASEC); int enum64_placeholder_id = 0; struct btf_type *t; int i, j, vlen; @@ -2876,6 +2883,8 @@ static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) char *name; name = (char *)btf__name_by_offset(btf, t->name_off); + if (*name == '?') + *name++ = '_'; while (*name) { if (*name == '.') *name = '_'; @@ -2892,6 +2901,25 @@ static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) vt = (void *)btf__type_by_id(btf, v->type); m->name_off = vt->name_off; } + } else if (!has_qmark_datasec && btf_is_datasec(t) && + starts_with_qmark(btf__name_by_offset(btf, t->name_off))) { + /* remove '?' prefix and add '.optional' suffix for + * DATASEC names staring from '?': + * + * DATASEC ?.foo -> DATASEC .foo.optional + */ + const char *name; + char buf[256]; + int str; + + name = btf__name_by_offset(btf, t->name_off); + snprintf(buf, sizeof(buf), "%s.optional", &name[1] /* skip '?' */); + str = btf__add_str(btf, buf); + if (str < 0) + return str; + + t = (struct btf_type *)btf__type_by_id(btf, i); + t->name_off = str; } else if (!has_func && btf_is_func_proto(t)) { /* replace FUNC_PROTO with ENUM */ vlen = btf_vlen(t); diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index ad936ac5e639..864b36177424 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -374,6 +374,8 @@ enum kern_feature_id { FEAT_UPROBE_MULTI_LINK, /* Kernel supports arg:ctx tag (__arg_ctx) for global subprogs natively */ FEAT_ARG_CTX_TAG, + /* Kernel supports '?' at the front of datasec names */ + FEAT_BTF_QMARK_DATASEC, __FEAT_CNT, }; From patchwork Sat Mar 2 01:19:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579336 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) (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 148FEB65E for ; Sat, 2 Mar 2024 01:19:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342393; cv=none; b=ZASq2JOKgzINSfFrElB7btJtHuWv8bkvi2BJL/WEaj+IKnltKBXxed5QrL5elYFSjDrlTX7oaMQYmVwNK1iGxMkH69G16ls23Cj0i8pr8piIdDkmwPM6BfteAsZ6GbeD6ba4XoLHspJoX4icXqT+1TVa4O/ZsyNwU9SG+mxJIQ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342393; c=relaxed/simple; bh=jf+vsu+WTgpWncreUvkd4YwjOt524czFATGH3sZASXw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=boqc97ufbNqMdOxs3Pf74QJBZsYSaUyalcxkHkBWEd7AVVO9/NDMeQum6Ic1rZRaZjM8oQttOxawOjc6pH647VvhR+LVaQQQuhutK0frA/J+Iv68P45ggYfM7nDQzf5y1rQaPqs1SeAn3NEuGgFo482Rknjq/Re2SFy8/lhbg9E= 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=c2zzQrm3; arc=none smtp.client-ip=209.85.208.171 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="c2zzQrm3" Received: by mail-lj1-f171.google.com with SMTP id 38308e7fff4ca-2d22b8801b9so34416431fa.0 for ; Fri, 01 Mar 2024 17:19:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342390; x=1709947190; 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=uoIIx7zO7cBbHKnaF/of+NRm8ozsyHhYrXh6nCT3IOQ=; b=c2zzQrm3dUssBezI6KR8vbtGKb4c2YJEjeM4LNtPQutwFgOwKKTBi4aH9ap2+0UhL4 bm/ifgxiwfYWxhkyAH+LMHH9NpBC+t9sGYv7nNiq3SE7pT4zNrOyLix+/rgFUEYIrF0P Bzubk+dzNbgMCrzJccgd1Lg/DVfVIxmnmbkbv8EjBj92W9rtPSqMuHv8u4tfRGwVDFtj CBVTSUjVXd2z+3PofMnTNSBsv7BW+JZvcVsjeAj4jZ6r5Z+JlEq3skzLAGTagqJclyEE JiA4wIdz8Axyz7I9tf/lAN3ZRPHKT3w0Mcc4wmjMQuD2LqtsGPPqiF44V8ATDEVMEQ7b IwTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342390; x=1709947190; 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=uoIIx7zO7cBbHKnaF/of+NRm8ozsyHhYrXh6nCT3IOQ=; b=vjI446jvP4RCG+27PQ1zINMW3K3KngYxNAREymrclJQDIqCqfl0m9qugVtp1NdmgVj 2Hm92uc+qEmrZUtkY15TgCQhUZy3+di2CL5TcLW1btt4sd6sonk0daQGLZ0q0ubO6VW0 Y3oxXEY46qVw97MVQGUovwfI/EfYdXOr6a145SXgRQ7bOMD6E4W+aceCdxpPluOqlYZC wSHepZw0XxaRY4+5N8Z7HdLdyW8jKHW8Zo5co31cM4BrzjEaEEXA2RjNoJxSvBDz91EW vI/XzRvSvO9f0k56ctkx3OYNODgY6dnsILA/bQ/eZ4fNL1WHd0P3oP3u9F8jQCa7xhw+ 0ROA== X-Gm-Message-State: AOJu0Ywo19xslyuXPIA9HdNXnElHsynsLSsto47Z2XtiEkcVfUXkoT4C UPOptmgAMf159hkkEDPU/j3bIiqP5XczOyg209fMECMyElBaltP7J6ePT4uj X-Google-Smtp-Source: AGHT+IHniwdpCZhLKsblktjUUWQ7ZV6Vp0ANSs5Bbu2pG55014pHnZD78TRkFfAI/VFZEgi5pULvhA== X-Received: by 2002:a2e:8e91:0:b0:2d3:158a:4ce4 with SMTP id z17-20020a2e8e91000000b002d3158a4ce4mr2248827ljk.26.1709342389761; Fri, 01 Mar 2024 17:19:49 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:49 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 13/15] selftests/bpf: test case for SEC("?.struct_ops") Date: Sat, 2 Mar 2024 03:19:18 +0200 Message-ID: <20240302011920.15302-14-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 Check that "?.struct_ops" and "?.struct_ops.link" section names define struct_ops maps with autocreate == false after open. Signed-off-by: Eduard Zingerman --- .../bpf/prog_tests/struct_ops_autocreate.c | 58 +++++++++++++++++-- .../bpf/progs/struct_ops_autocreate.c | 10 ++++ 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c b/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c index 765b0ec6383a..d5295ff2e925 100644 --- a/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c +++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c @@ -33,10 +33,24 @@ static void cant_load_full_object(void) struct_ops_autocreate__destroy(skel); } +static int check_test_1_link(struct struct_ops_autocreate *skel, struct bpf_map *map) +{ + struct bpf_link *link; + int err; + + link = bpf_map__attach_struct_ops(skel->maps.testmod_1); + if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) + return -1; + + /* test_1() would be called from bpf_dummy_reg2() in bpf_testmod.c */ + err = ASSERT_EQ(skel->bss->test_1_result, 42, "test_1_result"); + bpf_link__destroy(link); + return err; +} + static void can_load_partial_object(void) { struct struct_ops_autocreate *skel; - struct bpf_link *link = NULL; int err; skel = struct_ops_autocreate__open(); @@ -57,15 +71,45 @@ static void can_load_partial_object(void) if (ASSERT_OK(err, "struct_ops_autocreate__load")) goto cleanup; - link = bpf_map__attach_struct_ops(skel->maps.testmod_1); - if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) + check_test_1_link(skel, skel->maps.testmod_1); + +cleanup: + struct_ops_autocreate__destroy(skel); +} + +static void optional_maps(void) +{ + struct struct_ops_autocreate *skel; + int err; + + skel = struct_ops_autocreate__open(); + if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open")) + return; + + err = !ASSERT_TRUE(bpf_map__autocreate(skel->maps.testmod_1), + "default autocreate for testmod_1"); + err |= !ASSERT_TRUE(bpf_map__autocreate(skel->maps.testmod_2), + "default autocreate for testmod_2"); + err |= !ASSERT_FALSE(bpf_map__autocreate(skel->maps.optional_map), + "default autocreate for optional_map"); + err |= !ASSERT_FALSE(bpf_map__autocreate(skel->maps.optional_map2), + "default autocreate for optional_map2"); + if (err) goto cleanup; - /* test_1() would be called from bpf_dummy_reg2() in bpf_testmod.c */ - ASSERT_EQ(skel->bss->test_1_result, 42, "test_1_result"); + err = bpf_map__set_autocreate(skel->maps.testmod_1, false); + err |= bpf_map__set_autocreate(skel->maps.testmod_2, false); + err |= bpf_map__set_autocreate(skel->maps.optional_map2, true); + if (!ASSERT_OK(err, "bpf_map__set_autocreate")) + goto cleanup; + + err = struct_ops_autocreate__load(skel); + if (ASSERT_OK(err, "struct_ops_autocreate__load")) + goto cleanup; + + check_test_1_link(skel, skel->maps.optional_map2); cleanup: - bpf_link__destroy(link); struct_ops_autocreate__destroy(skel); } @@ -75,4 +119,6 @@ void test_struct_ops_autocreate(void) cant_load_full_object(); if (test__start_subtest("can_load_partial_object")) can_load_partial_object(); + if (test__start_subtest("optional_maps")) + optional_maps(); } diff --git a/tools/testing/selftests/bpf/progs/struct_ops_autocreate.c b/tools/testing/selftests/bpf/progs/struct_ops_autocreate.c index 294d48bb8e3c..703ad8a2914f 100644 --- a/tools/testing/selftests/bpf/progs/struct_ops_autocreate.c +++ b/tools/testing/selftests/bpf/progs/struct_ops_autocreate.c @@ -40,3 +40,13 @@ struct bpf_testmod_ops___v2 testmod_2 = { .test_1 = (void *)test_1, .does_not_exist = (void *)test_2 }; + +SEC("?.struct_ops") +struct bpf_testmod_ops___v1 optional_map = { + .test_1 = (void *)test_1, +}; + +SEC("?.struct_ops.link") +struct bpf_testmod_ops___v1 optional_map2 = { + .test_1 = (void *)test_1, +}; From patchwork Sat Mar 2 01:19:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579337 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com [209.85.208.181]) (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 E37A1B661 for ; Sat, 2 Mar 2024 01:19:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342394; cv=none; b=ceUPvt+ybUOR9kVC622eC+XdzX7Uo4jxP3kDmmyhOIrI+HJDXM23xZcrF4pKckpq53uJelSqFOxi3P0jpBjQ2dSMedT7s28sIDdiM7XvWL+X50NKTvtz0m+a2xv/RruwE7BDXCSY9FXvUNomIMDEEftiI97mOSusr11v4+YChgo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342394; c=relaxed/simple; bh=vY2eM0UqjsJ93XnGPnegnfX6oPS8A/CrHw+0bW+dkTs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jZ5/uW+YuhTpy2VL7XvQAOTUayXWowPHm9HwR3/+gbErBmKByKnBcJwNHzzfOy8zhax+S4VOnij7ol4IJIl4ahamnPSKu4u8MyZgvYbU6bnedA2XMVrWSJ/OCUPL1oSEbgMiluY1gUGHWTcnky6PmJJW9qgU8Ys2mSMzSdN4pLI= 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=XVwt47pp; arc=none smtp.client-ip=209.85.208.181 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="XVwt47pp" Received: by mail-lj1-f181.google.com with SMTP id 38308e7fff4ca-2d22b8801b9so34416561fa.0 for ; Fri, 01 Mar 2024 17:19:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342391; x=1709947191; 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=DYMlnF4l/D4ErZPFlA4ND9mYkMopZNVa5Qwk1lR+k2Q=; b=XVwt47ppbC9fvnq4hYhdi4P89Uuz3+EIILBZSO5leH4JETPu2AEWq1qRS9Zo9emKhZ d65//AduTBlRewY7K/p58ZjCzCKo/QaAaytvFUiOHFpo8Y1a6JxUE8Fc7+u8SWVUpSoi Paig3VTKVf69JA2zjdsBsQiyCMPrP8NNTPUaCv7p8xEks5X6np7jHohCem0XCEk2XV6y UVdm9AaeyzfwkomePOj5rMqNroQ8bFbyFmL9L0IiSnaH757vN7omblkDqLJflx+ysWM4 ae2AV1CJt+MbKhzmI70ZH7l8LNZX5Qos1H/HrAdnNiqGquY7XOK8mgMS2D89ST4/Jlo7 XjDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342391; x=1709947191; 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=DYMlnF4l/D4ErZPFlA4ND9mYkMopZNVa5Qwk1lR+k2Q=; b=XOAn6Sd41yIcgxGxz864qmNdfbL/be7fcDBkJyG4cpz+EM8U5Nv+79VrvYpa4uzQDa lFGPXIfRmCwY+nHqlMYdDpNwQ7ouQK/NedlW0kz2sLwrPAg22SUf7G2BXFoVJMtEl221 25VSqnII7vbL3aO4s9glMsfzb0br2oxl+IHsqw/laQhZ9RJWtfe9D6T4yWlM1IsWIZIf ou1nHamVnT7+UGFIIJv+o+Pi2/DKQizIqyaEviqcMJebUUYUP4fMYzDhuAdxV+MTc2Yl fWHEk3vgk7nABgdM4DCBg69Y0bmgmeBbQ3qePWsAzW893TLIO5PnWztV/hsqCneWDTLh jtjQ== X-Gm-Message-State: AOJu0Yy5oo8qD7iDx5OR5yciewfSZ0YyRnEOdwhqbMUwTHNWd2OZ58aB 9HSxewgyWF6wozivFmrpHaYQMSoiexAFcWtQGph+5qFVF/YQ9h5TBa9G3uql X-Google-Smtp-Source: AGHT+IGv/gQ4OXq8lzolWzBLgjT9MkQ//36lqK2zLFd5o9eJ8tXew7BxufYt11xBtsNOdpxeYtTo1g== X-Received: by 2002:a2e:9858:0:b0:2d2:c53f:ab18 with SMTP id e24-20020a2e9858000000b002d2c53fab18mr2365095ljj.11.1709342390919; Fri, 01 Mar 2024 17:19:50 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:50 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 14/15] bpf: allow '?' at the beginning of DATASEC names Date: Sat, 2 Mar 2024 03:19:19 +0200 Message-ID: <20240302011920.15302-15-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 Currently kernel does not allow question marks in BTF names. This commit makes an exception, allowing first character of the DATASEC name to be a question mark. The intent is to allow libbpf to use SEC("?.struct_ops") to identify struct_ops maps that are optional, e.g. like in the following BPF code: SEC("?.struct_ops") struct test_ops optional_map = { ... }; Which yields the following BTF: ... [13] DATASEC '?.struct_ops' size=0 vlen=... ... To load such BTF libbpf rewrites DATASEC name before load. After this patch the rewrite won't be necessary. Signed-off-by: Eduard Zingerman --- kernel/bpf/btf.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 6ff0bd1a91d5..a25fb6bce808 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -761,12 +761,13 @@ static bool btf_name_offset_valid(const struct btf *btf, u32 offset) return offset < btf->hdr.str_len; } -static bool __btf_name_char_ok(char c, bool first) +static bool __btf_name_char_ok(char c, bool first, bool allow_qmark) { if ((first ? !isalpha(c) : !isalnum(c)) && c != '_' && - c != '.') + c != '.' && + (allow_qmark && first ? c != '?' : true)) return false; return true; } @@ -783,20 +784,20 @@ static const char *btf_str_by_offset(const struct btf *btf, u32 offset) return NULL; } -static bool __btf_name_valid(const struct btf *btf, u32 offset) +static bool __btf_name_valid(const struct btf *btf, u32 offset, bool allow_qmark) { /* offset must be valid */ const char *src = btf_str_by_offset(btf, offset); const char *src_limit; - if (!__btf_name_char_ok(*src, true)) + if (!__btf_name_char_ok(*src, true, allow_qmark)) return false; /* set a limit on identifier length */ src_limit = src + KSYM_NAME_LEN; src++; while (*src && src < src_limit) { - if (!__btf_name_char_ok(*src, false)) + if (!__btf_name_char_ok(*src, false, false)) return false; src++; } @@ -806,12 +807,12 @@ static bool __btf_name_valid(const struct btf *btf, u32 offset) static bool btf_name_valid_identifier(const struct btf *btf, u32 offset) { - return __btf_name_valid(btf, offset); + return __btf_name_valid(btf, offset, false); } static bool btf_name_valid_section(const struct btf *btf, u32 offset) { - return __btf_name_valid(btf, offset); + return __btf_name_valid(btf, offset, true); } static const char *__btf_name_by_offset(const struct btf *btf, u32 offset) @@ -4481,7 +4482,7 @@ static s32 btf_var_check_meta(struct btf_verifier_env *env, } if (!t->name_off || - !__btf_name_valid(env->btf, t->name_off)) { + !btf_name_valid_identifier(env->btf, t->name_off)) { btf_verifier_log_type(env, t, "Invalid name"); return -EINVAL; } From patchwork Sat Mar 2 01:19:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13579338 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f176.google.com (mail-lj1-f176.google.com [209.85.208.176]) (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 0CBEE6FB5 for ; Sat, 2 Mar 2024 01:19:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342395; cv=none; b=DajOqDC7KcWD0v58emzmmMGy1fW4v2rqWrC4IzrZR5cbnYptiLBDB3skWl8xKde6uhCnkM2dcyIZ4RvWjl8FI1MFeHVW8StPoV+MaGxvMJ8NF1a8I55iuDfohvZgP4TvvbsXrZtr9iGQzeA3YKhX9EuUmxuric0W2bCyMCZ7HLg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709342395; c=relaxed/simple; bh=AYRFrFsme20cwtwcfp7bMqH+JFxsSxvqP14s1SLy364=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VWaecLM7bgrCXKumX28vNZfi4Gnk4APgcOfJqWVMM+Rf7ysXM3tIHk0ejS4900ss6S8F0lAWj05dSYuVT4UHFhbdMEULhQPPwVYEHFtL5EKd6q+2Rdgn7DTAyblYD/wD1tvHfbPM7DCkm0p9M8TDtGhM5Tfy0ffe6G8brvsY4LI= 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=nsbYamJv; arc=none smtp.client-ip=209.85.208.176 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="nsbYamJv" Received: by mail-lj1-f176.google.com with SMTP id 38308e7fff4ca-2d220e39907so36515831fa.1 for ; Fri, 01 Mar 2024 17:19:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709342392; x=1709947192; 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=yUKVRagEjVbWTLTtfvO897pmVhBdhZlyN2rymNdpliI=; b=nsbYamJvxnPBWDit3zGRDvXhrSFPfoeEdZ/D6H1Cyu91oITn5esG49x9YSCsgGpqgI 97mRvNWTHVdOYkQ8XsuMkAFR1+20FwoM2QXFXwSyyA+Fs7whazFZfCaO/x5ElMFdTncC n+vj/cP76CBvc6T2AHaIZPRachz+7ZK/No5IWskPiEMVLuDxeMDge4zH5JkwuIAJYJyi MO4mfFNi35v1HWbBVqesXQwTRr3wa6i9EePN83i2+AsSWx0N6P4uGrhKywiaZBspKbJt FhLImxN1DGZoJe8yEsD8+rElNqUMqeHB/uIUljFXslfG73vMFaAyXDYA0EouPnAhgf6e SPyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709342392; x=1709947192; 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=yUKVRagEjVbWTLTtfvO897pmVhBdhZlyN2rymNdpliI=; b=wKyN4bNMTMgesSrudztF2W7jS1j/2c6WCS2uxKPskF/OTCQg1SOd+YAkufM0iv6263 XPb2RZa4I5gHZCCweiV/VgU7eTRoBh4L+5cM9Uq5k3vfseVq71gkj54/8UZREOlA2hmA 2kVR2zq3WSphJtMG77H2dTc9k5HuWizhDPdsmsM4sfoxNp4DZ3gcCWfKxbZw4R7mp9tJ 6T5SRA1Igidc5kIOwmTQWhpR/KkYzKy1aikhp2daLtYjpgd548dFM6NEKeV4hJqm8Wu3 oysz/hLqtR88S55zfRtguSs6KdMoI8sp5sd3cai5JJQUjVhnbt+DqV4PVPHR5uQgzeb0 Jeuw== X-Gm-Message-State: AOJu0YwTeTeKB3cLB4ou05HWQvD6jZDKZv9U8spZSXtX+TlqvyuTBeYD J6ggpP2UuZZaQy7DwweeGsIC6v8TQ8IlbNB90tPyM6SGJY8UFr58hcOHg9DP X-Google-Smtp-Source: AGHT+IGiYTFrX3u59mp1RmF6S0UmGxCBlBaVwTwj0/hpa41SjCiE6XpKgpVvP1Wj9zcDprMXN4QWTw== X-Received: by 2002:a2e:8612:0:b0:2d2:2c28:f174 with SMTP id a18-20020a2e8612000000b002d22c28f174mr2411478lji.42.1709342392052; Fri, 01 Mar 2024 17:19:52 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id z23-20020a2e9657000000b002d295828d3fsm767386ljh.9.2024.03.01.17.19.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:19:51 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, sinquersw@gmail.com, Eduard Zingerman Subject: [PATCH bpf-next v2 15/15] selftests/bpf: test cases for '?' in BTF names Date: Sat, 2 Mar 2024 03:19:20 +0200 Message-ID: <20240302011920.15302-16-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302011920.15302-1-eddyz87@gmail.com> References: <20240302011920.15302-1-eddyz87@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 Three test cases to verify when '?' is allowed in BTF names: - allowed as first character in DATASEC name; - not allowed as non-first character in DATASEC name; - not allowed in any position in non-DATASEC names. Signed-off-by: Eduard Zingerman --- tools/testing/selftests/bpf/prog_tests/btf.c | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c index 816145bcb647..88c71e3924b9 100644 --- a/tools/testing/selftests/bpf/prog_tests/btf.c +++ b/tools/testing/selftests/bpf/prog_tests/btf.c @@ -3535,6 +3535,49 @@ static struct btf_raw_test raw_tests[] = { .value_type_id = 1, .max_entries = 1, }, +{ + .descr = "datasec: name '?.foo' is ok", + .raw_types = { + /* int */ + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + /* VAR x */ /* [2] */ + BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1), + BTF_VAR_STATIC, + /* DATASEC ?.data */ /* [3] */ + BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4), + BTF_VAR_SECINFO_ENC(2, 0, 4), + BTF_END_RAW, + }, + BTF_STR_SEC("\0x\0?.foo"), +}, +{ + .descr = "datasec: name '.?foo' is not ok", + .raw_types = { + /* int */ + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + /* VAR x */ /* [2] */ + BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1), + BTF_VAR_STATIC, + /* DATASEC ?.data */ /* [3] */ + BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4), + BTF_VAR_SECINFO_ENC(2, 0, 4), + BTF_END_RAW, + }, + BTF_STR_SEC("\0x\0.?foo"), + .err_str = "Invalid name", + .btf_load_err = true, +}, +{ + .descr = "type name '?foo' is not ok", + .raw_types = { + /* union ?foo; */ + BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [1] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0?foo"), + .err_str = "Invalid name", + .btf_load_err = true, +}, { .descr = "float test #1, well-formed", @@ -4363,6 +4406,9 @@ static void do_test_raw(unsigned int test_num) if (err || btf_fd < 0) goto done; + if (!test->map_type) + goto done; + opts.btf_fd = btf_fd; opts.btf_key_type_id = test->key_type_id; opts.btf_value_type_id = test->value_type_id;