From patchwork Wed Mar 6 10:45: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: 13583856 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) (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 8BAF72AD0E for ; Wed, 6 Mar 2024 10:45:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721945; cv=none; b=XDxyzUNnqWlXB/zY3ItTKLpcWz4vdBsx/dTOwUv+dMMyn03QJPi0vZntryrg+q+oDUR6r/DHuFiGyT/jwC9TcGmvDN3lQFyIWocwQPVwg2Y55Vr8uRwEn5BIKsTcy8VGrjziBj9JuQvgBPyyZdktuiCDV4+bGAfDr2YZuOpBdOM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721945; c=relaxed/simple; bh=G+Gb9raI3k69bKdJDWiSzbs64l3Ub9N6DUOBU+P4+qU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UzPN4pjJSuIfwvGqfPX8TE27TqlnHTcml7/n8x36vigNfn7QXUOQSDXwJxUV3TI0z7SL98+4seH80rLXQ7n4eXD151RZ3pVJ7UdFCSqIA+O7j7Nn6xxcHc5wLjSeHlEl3x/O7w8tm6lXxmNnGSXnbDgPHcYUkHn2MFi5M8CPPUA= 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=eBqvRybC; arc=none smtp.client-ip=209.85.208.53 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="eBqvRybC" Received: by mail-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-564372fb762so8573058a12.0 for ; Wed, 06 Mar 2024 02:45:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721941; x=1710326741; 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=jr9F8RUG1nG10Rr3cII66qRWYW8JOv0GcLrRadN8seU=; b=eBqvRybCoCCQPG6Kcm01uubvm9GdSjRWH4/CvzUbMmvpY+myBbCCI/UKRe4GbW+nGw HUWsF5teOh3PRUPTBvnO2ZLaYHekma0sWoes7ksSnw8DoR8lk1wbAyZBnfM/cVNwGLjZ lLyk8/9kBFxyiPyr5b3kmaimo1eWdYHA82jH9qQBCRw9pPcm8WLVTX/SIGQmWpnj4EvF PbaE5MyZK/lxstzBS6Cl/lP5k1ltJ8kxge0iFoXm7EYvhY0N5azcVHLj/4khATlDn5L9 smYSh+0JuknV6oZzQFjz0/3WZAX0y/iQry0VLL7nvNPQMTo8XZdplvwEhUDxLOWj9GVf k1/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721941; x=1710326741; 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=jr9F8RUG1nG10Rr3cII66qRWYW8JOv0GcLrRadN8seU=; b=ZSgUfRn0l1gvgrK3/Vbmxx2yDQlbgeAZAkOKrE5z7Fv7h4opsn66KrAwlmYGTsUK/a oG3pxckMjg9j+S82g+wGc/BEpkNMqMLKfqMmMUbpt2C6bWoiD+ebo+GF9jVyLtl8AELw OJRgC7074x6SL5NcwN+nkEQKraSfI95P0O7VVP5D6Dm6wPGZKnHsaRWfKlE2pNJpFZcZ MCPSDaZ6WNLquw9x3Bw6hcrASHNO3Z0itOJyc8qm45JlRLQj36F4H2fZuSASZ5K8AofD ABOL+vFU2PZJ2pK6ufiMj+HevJuf/EA5vHWS0fjifvw0c1BZ6HCkVMJods6ye05C0Ite qGaw== X-Gm-Message-State: AOJu0YyRGxpIGF6MRlMIeQf1TENrAQjDoE8I8a8RL+MtH2vGpxgSMCBV j1yNtX31rwkoQho1d4osawvPFipRmVwxzBxCwHERCtgVa4zfWtDZa/3bUABf X-Google-Smtp-Source: AGHT+IFhQ1SMEB51ezlARdw9jdpeNxDwxMJLX/g+pgSHUKCFm3d3SMLrIQeOQmvhgrlYvBnFmg4KTQ== X-Received: by 2002:a17:906:d156:b0:a3f:2d9c:c66b with SMTP id br22-20020a170906d15600b00a3f2d9cc66bmr10626804ejb.22.1709721941384; Wed, 06 Mar 2024 02:45: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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45: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 v4 01/15] libbpf: allow version suffixes (___smth) for struct_ops types Date: Wed, 6 Mar 2024 12:45:15 +0200 Message-ID: <20240306104529.6453-2-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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 Acked-by: Andrii Nakryiko 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 Wed Mar 6 10:45: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: 13583858 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) (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 A89995F862 for ; Wed, 6 Mar 2024 10:45:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721946; cv=none; b=oQHiox75ZQMiFec8Xt08/cgbGoeq10Tw6QGNgwOi3Qr6bBKm/Ko7Shs1ObFYlEPGUfLLmMLIrtRFCRsdgKS87raJo75Q7UzEzOO2SZRhUg6/P3Ps4BGQvWtyzrC6758NqZMqbP6cmY8blvE0VrX5LTQg0yukf3MYMdHHzbC/bEo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721946; c=relaxed/simple; bh=AlwMdNFDJS0zfuU+xc0pIGyGfGCAdfWXrniIcad1Ym4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SV/+r2kypw1oDWlYjHbgrvMAcip+UBuVypBfzXZaqbiMDp5N5kWzhIVS9VkAsD3uBr6q5upb7WqDOCGDt0+ItUE0wT6x8mC4lfjWJc254jVBoDp3dF5A9Uv0lk/N6phi4NXMklDrhsiUO0JGbym4+2QjaHWVfm8EQj/uRX3zIxg= 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=h6yRk0Fi; arc=none smtp.client-ip=209.85.218.54 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="h6yRk0Fi" Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-a3ddc13bbb3so127236766b.0 for ; Wed, 06 Mar 2024 02:45:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721943; x=1710326743; 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=TLXKB51WZ7Tdq/MZem0e2IjeQIkn0MywkjVRYEGvqSM=; b=h6yRk0FiZRgQniMFe3slKT1S1dOaLcHGlciHH5AW2JKhaqUdlQIOyNotMntlh99YZH DcgfUPO2kKGVP0Wl8jP0ic1tktMko/grKDtCKjdTxlxNauzv33HaGJAZ/9LAm1P72QJn UHvkxta5f8un8ICTpuG+uQBJX+Baymr6yuHiDPZ8b9IEGMqXUP1cm4pE1f4/eU28OiKA z7stYOVGjbWfQij1k/3YUG+uYUUqG056vzogaB+de/5erMrjJNG5ZRoo2dbIJxJTwaGm rWQuBKKemibpNYPmkXCNxbHNkx/yqsuXW+Bt4pPhXKYJm5Apa6UVqkbxZpPkkLLrUW4e 9Fpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721943; x=1710326743; 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=TLXKB51WZ7Tdq/MZem0e2IjeQIkn0MywkjVRYEGvqSM=; b=O6bpbHmKjfx6BZtbKTgqDSUrLoEMxY6pC66nSAWE4BgmG0ofqwB4cDRckZLMIAGAhe 2nt8LHRW/J5FApEG4O1N9uKAuV2CTnSmtW5NzDBL5mvGUks7NWcTrfrc0UQ1lfIv3dav EHjEMLpZe1SFdkQB3i9e0KD2A2Vd7vbp2OKy1SMy1LBfE61o8ygjDeE0HhiwG1WzpnBc +RE71EntGDn3J6YWerungyjZk/+pydSeZd5tf+bQhKFDgXZrPhRNGUZjwc0W7ffQoFDF ScN0uwk8ca15SQFJlOMemnVnpEBTRxMj9ZTISxSizjcL6f3A8BUWuUI21L4UMRRT77hy yCag== X-Gm-Message-State: AOJu0YyRBVAqa119p1YKRZtwcbIE0AnwsZgb5SU770xwCh4soBuADmAB /7D8S7H2wSaO3N+as9JJRuluaa40dAg3SBmc635FOgAC+ntBFMPGcx9QfwzW X-Google-Smtp-Source: AGHT+IEOd6UC3s+YZkm7Nd5u1zhgVV5zOoRkzSAWlCG6dP+6ORk22h/xQLS6R8LS/8PRpBEtAZ3R+g== X-Received: by 2002:a17:906:1511:b0:a44:144e:3463 with SMTP id b17-20020a170906151100b00a44144e3463mr5756337ejd.7.1709721942514; Wed, 06 Mar 2024 02:45: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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45: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 v4 02/15] libbpf: tie struct_ops programs to kernel BTF ids, not to local ids Date: Wed, 6 Mar 2024 12:45:16 +0200 Message-ID: <20240306104529.6453-3-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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 } Acked-by: Andrii Nakryiko 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 Wed Mar 6 10:45: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: 13583859 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) (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 D854E605B1 for ; Wed, 6 Mar 2024 10:45:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721947; cv=none; b=OIoMP/4sRgdDUDUaDwVElFn+esTd9nmBOjgxeKkT6hCuUcCAeoBv4y4lVQXk+THCJLKTUVyYAQek9CnQhTY7KCSqgrTEmardKkQLatTkwocVjtBPHJkXzrYEbuBHcxTUIsz8C3JytdE2mAiYZtrlwLZn544qS2dNMWRFnJl9tPA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721947; c=relaxed/simple; bh=2kOHAempn5pz0xL9loA2uYivlISfYuYhenGFOFmFKJw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Wppxism63XIhCkwlUgRf/74CPhbUG0OOm/zv93eknsJMr7tkxgoVMcD0gns+GOZwqoeG8YyzRB6D1IGPwFU9tZhPrq0GBec59BGbuuS8jJzyKlGak89hM42cv5c2qKsDYFXlFVHX+mYleeABZYnASuNAx6X3fqxB0idD3hzVSrQ= 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=Ye9DxsAr; arc=none smtp.client-ip=209.85.218.54 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="Ye9DxsAr" Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-a45bb2a9c20so57566566b.0 for ; Wed, 06 Mar 2024 02:45:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721944; x=1710326744; 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=vu+CRpIdKn1mMAIY+aQXp2RuVcGDqVY0wuARmCwSDS0=; b=Ye9DxsArGP2Kw2OnStNGvhFVuwlTUbUxXAsUqtW+7DP6GpbzqCNBAsoAWf5MpRVs8E +czxPilRae7BBhMzYExpGQhfceCWmFUWiFSG+aI5Aevlg1wIv5uqZ0SkYEd5zhdk0RT6 qfa84GqNAHNbwGRxlIWNDxFTWOMutlmd4ClUINzJO04ZJzgksNb7HeuEAAZe15h/dcUU 6eQnFvFn7AgN2HGtTsebh8CuCUypQSAOx/gIqn3tAc7p6iVycLDBa334x6tsSsgGUxdB eTEc1yO6VOAZfUpgOKmoEm5WMPsX4Fzmk8UFcxxJ/13XJMcyoipyYT+IVKv9tG9px+BY TsQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721944; x=1710326744; 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=vu+CRpIdKn1mMAIY+aQXp2RuVcGDqVY0wuARmCwSDS0=; b=oSj3rOq1mFO2EaiSZYHH+vtHjRQe/Sin6lr7pgAGoorQXV8GrfrQ19Xg8TPT6xI2Rn oKiOsleO4woCnkrdf6WjToObh9L0qiAkhPGvpy/lX4PlouT3/voE/AzjzUHvt/TooJ/1 fZc80cKvPxqhvUwK9ehyb5a3KRwlCn9Eg9n3gD9vDx08xYvVR5J/vMAwvxYSHNrAivRm YDotj1bj20uj9goiVTsqD+Kd2OcFYDZVfujdwMuBzb/vjT0Qhm+sfpkF3Y3nZaEDE6lf tzyXUVXdmh5qC3wcQyCCc5m9vgb8dpLdzUmPqp+DaJTCw0XTkRLdFkK32KDbNPR60jvT yIbw== X-Gm-Message-State: AOJu0YysjSQWHmxwJdi+aD6iQB+OSYGClKqhfVH3N1h43KbsPIozsSK/ 2MzbQOVv4EPk/UMo5rgCBJpizfNIqLZ1bMgIzI8RibXiBOarvAJ8Y5ppME3U X-Google-Smtp-Source: AGHT+IH9pdEIHrBcVfoZgfDbY6TvkAcZm8rSCsCyQA/WyCYZOMlnGsEqVcakt/qzilA2g+jhH1xtdw== X-Received: by 2002:a17:906:cf88:b0:a45:373:d0a with SMTP id um8-20020a170906cf8800b00a4503730d0amr7913679ejb.32.1709721943623; Wed, 06 Mar 2024 02:45: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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45: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 v4 03/15] libbpf: honor autocreate flag for struct_ops maps Date: Wed, 6 Mar 2024 12:45:17 +0200 Message-ID: <20240306104529.6453-4-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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 | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 2c0cb72bc7a4..1fb9a4f237b4 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1212,6 +1212,9 @@ static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj) if (!bpf_map__is_struct_ops(map)) continue; + if (!map->autocreate) + continue; + err = bpf_map__init_kern_struct_ops(map); if (err) return err; @@ -8133,11 +8136,20 @@ static void bpf_map_prepare_vdata(const struct bpf_map *map) static int bpf_object_prepare_struct_ops(struct bpf_object *obj) { + struct bpf_map *map; int i; - for (i = 0; i < obj->nr_maps; i++) - if (bpf_map__is_struct_ops(&obj->maps[i])) - bpf_map_prepare_vdata(&obj->maps[i]); + for (i = 0; i < obj->nr_maps; i++) { + map = &obj->maps[i]; + + if (!bpf_map__is_struct_ops(map)) + continue; + + if (!map->autocreate) + continue; + + bpf_map_prepare_vdata(map); + } return 0; } From patchwork Wed Mar 6 10:45: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: 13583860 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) (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 BB5CE605BC for ; Wed, 6 Mar 2024 10:45:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721948; cv=none; b=Fkm+jOGFOSRVxyVaQU2fIEtFgEVqknoBeq8ICuGTH/xw1M7aasbm8vdKac4xhAqlplWZ0ZvpemRr/lI+9Wp0Bnwku0gV+lty7DSxnzEykwOsWI/i2cp+jMlE8vV6+xelkGhpPNusneEm9BwU4gLHYtKE1aKkvfzgKEjtMbUF4z4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721948; c=relaxed/simple; bh=PnHswEzkTKLyrkFqxRvFfq3V7JCNVSzhJrf8FimWERg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B37dQI22jfCUn0gt5waGSwysRGwdoWEKutyBLPzl9ujNqFPkjKC5F3uodxZdlRIP2JhZKqLZI2sOX/MBM3SS04koq5J4nTwoaqvlQRiEH4glwMCJa78PMWvgqnEiE38ndvz3amtLJMazMiRgS8DL025CH9K4w/lsiqTslEfdkjw= 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=eiYPeswe; arc=none smtp.client-ip=209.85.218.54 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="eiYPeswe" Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-a2f22bfb4e6so248283166b.0 for ; Wed, 06 Mar 2024 02:45:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721945; x=1710326745; 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=WObSvZSCuqPl1NZLtLA+BpcdBmw/EUeTAbS78t3Bs6k=; b=eiYPeswe2KAKVeU2seZeb+j+myGsYpj9Dg9qO6qLNBDmZ467/jwzq3ZnR3t4mmjyQv EWJmuEd7AXxWakVHgXJcH1m8pxJDgfPoEUifC8Movuvp4Cl2zGtGzJwcbSua35QNCTEs aiq/h2bRju+CGJwkn+jWPVa9479vkk31P9BSPrN3lv5ceMoii/O23PNVURSs/34+MNrl y5znh5nIIQLmVqNIpHU0lw9EV8A0JG7CgNsonRIWTmxLZpivkt6BwDmVU8ZLaW8WY0C6 yZACs2g5yGrB9YJEO3ahFy058/zjeIcB5ujbqthM1xuKtVMHB25YS+OSd6XVnkJCVA58 hW0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721945; x=1710326745; 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=WObSvZSCuqPl1NZLtLA+BpcdBmw/EUeTAbS78t3Bs6k=; b=B49uyd/xwTq6zyCXQVg9ttC2lotuacdxMFK8Kxt/fNBpR2zHF/agVnFgDxzvwFeq2z aL65JvqcJpYeNyuJ3TOBMLWjZIKsgQFX9DcHAibfosuvcZdKUath43+OcR6/uwzW84gG pfMdHTU/oAo78ZnHWVf7HOZ3IjwBPTRHx7vQQEc1xArvUNjsBBdfUUkiRZjzZGnNthDR 7igy1/XtF2hn4UHoZ7ldWaVRB6eCqbm9EiVI35fG76Mx7FKcvYvkE6M+NJ18MTRwU6R2 XE5iMpk25oWkl5qMOjN3C7bMvwHcVEqN/RM78MRYjAwyVeUDJr9CGTDbpqrRYK3AKcfs oXhg== X-Gm-Message-State: AOJu0YykW4q0rTRJpMEwQCUEdi4aPUGQ9ERpNczfamahEAiroWplLFdl olMUt+Yw1HoiIJgGIaQBGHSBsv04wgofuDqstFVIhxUocuE+2Qux2w6iOh4J X-Google-Smtp-Source: AGHT+IGghzYB1ZXvUpUzcyW5Wrz64RrccRETh+Xk5SDzihjkhiKWm9vvb9k5R0tsI1dZhL+x/smZ2Q== X-Received: by 2002:a17:906:4c58:b0:a45:ad29:725c with SMTP id d24-20020a1709064c5800b00a45ad29725cmr2539759ejw.62.1709721944882; Wed, 06 Mar 2024 02:45:44 -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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45: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 v4 04/15] selftests/bpf: test struct_ops map definition with type suffix Date: Wed, 6 Mar 2024 12:45:18 +0200 Message-ID: <20240306104529.6453-5-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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 | 2 ++ .../bpf/prog_tests/test_struct_ops_module.c | 33 ++++++++++++++----- .../selftests/bpf/progs/struct_ops_module.c | 21 +++++++++++- 3 files changed, 46 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..b9b488d6d53a 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -564,6 +564,8 @@ static int bpf_dummy_reg(void *kdata) { struct bpf_testmod_ops *ops = kdata; + if (ops->test_1) + 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 Wed Mar 6 10:45: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: 13583861 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) (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 175D4605CA for ; Wed, 6 Mar 2024 10:45:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721949; cv=none; b=H9xC6evu0pj7fYyjIPavlCwVy7/vMQQsPwgd0LLnf9yj5ZKBJayPLexNye33GQWrEHaJJ3AxgR928Ti6BNvJFsceLWWjbhq9Pp+f/wEuoLpGJciKDwXBpkeC0MDdu7VM3fxkIjIklS1vlo/FZFZqe2/Cyp2peK9JJfcUluCJ/n8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721949; c=relaxed/simple; bh=560LnpXJyKDnc+o4x0AOeZKY76rsVdOyjpCP/vzD98o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KgAZKEHZGTgtxETMb0W+mRpwmofHTakoJl0xDFzljMWdEiFKuI8HHjaNAVI6raDarLyt0zy3CMGM/5TBmvl7znfVv6pPV7PYq83DKkXMGvZHXSZ4p++vquennvMEzTm1iwf7yGLDe97MzFyiL1+s7N+VLj2qAf7KtNdUHjWUw68= 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=gyGmCYJZ; arc=none smtp.client-ip=209.85.208.53 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="gyGmCYJZ" Received: by mail-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-56781070f38so1210599a12.0 for ; Wed, 06 Mar 2024 02:45:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721946; x=1710326746; 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=e69/kZxMiYRBsVrYW+fBLTtqbjlgUyrdHfF7WeGA9BA=; b=gyGmCYJZqcs0tCgWJY0GEuFBU/f0gGlNyxYjwLeeZ75MjVVxkKP63/E8n+cg/81VjG h0v5g59nvxFQzRkq5CQ4lx/CKpl3vH8oD2PBwOPi533D5V9tjsxwLJrJvoiZsp+INHrz +F3PpnOdGxh/mm7LhJ9noa7AbqCtscJyywUqtjKG3h8S47VR8lABsMEcGoJBQjWWET1O 9LeLrQhaCz6Vu5sjHlQ50qx+1EKa6Q2nuw5+VKOc2HhOw/JdLZI8AVl0BugZfAyIRCZu DXHAfEao0CpDHvKjZl7cgd4Szh7TmJf01aPtolLga6NoXEyvY+N81qx4w7RjRPsMcCaZ fd3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721946; x=1710326746; 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=e69/kZxMiYRBsVrYW+fBLTtqbjlgUyrdHfF7WeGA9BA=; b=uGENYso/PKI2A/7SV/tofoKOTbgIAwrvQVhh++1rMZVAilKEA5S1jsJTvMso9YhRU7 XIskrMkLQd2XDiKqqhDpMhLhJW7njY99epfoUBOpU+7yoFBDcfQRmyNEXUgsQdmPvRgi L1zF/+2jt7ewhZooTKQfe3mjVrBp+h8cah4QcfQvNef57jraHxHpeTbg1WyWvpGKpJ4J KLzw++P/Nbe9zJCTiSidjwxH9umr+pKk0vD4xPU/EjzR+miHF9khiddG9XG5nZvTsJAE na65Qntf5EzVJuBWChLL/7q9u7QPVHSQrhT759GSVmQTo7h1JBdNzT0R5KR7QNjqoDKi O/Jg== X-Gm-Message-State: AOJu0YzuH5vsBv+zfdRQk3pe78pmri9zwVnK4LveLBgGoClZX3K/qLbE Mbuaimc/3kIFFJpLgYUmprvFgB7QC6+cdiCrLgSRED3yc/evz1CM200aXNsl X-Google-Smtp-Source: AGHT+IE40B4TjvD7zV2IeQ93vEQpvXdFAkQATM0WdHpkHI2wgGVon6p6BxX8lAFf8JIVwcMXCD9VcQ== X-Received: by 2002:a17:906:8cc:b0:a45:7f61:8940 with SMTP id o12-20020a17090608cc00b00a457f618940mr5537893eje.18.1709721945948; Wed, 06 Mar 2024 02:45: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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45: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 v4 05/15] selftests/bpf: utility functions to capture libbpf log in test_progs Date: Wed, 6 Mar 2024 12:45:19 +0200 Message-ID: <20240306104529.6453-6-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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 INFO and WARN level 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 | 59 ++++++++++++++++++++++++ tools/testing/selftests/bpf/test_progs.h | 3 ++ 2 files changed, 62 insertions(+) diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c index 808550986f30..89ff704e9dad 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -683,11 +683,69 @@ 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 INFO and WARN level 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; + memset(&libbpf_output_capture, 0, 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 && level != LIBBPF_DEBUG) { + 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); return 0; } @@ -1081,6 +1139,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 Wed Mar 6 10:45: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: 13583862 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) (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 E94E0605D9 for ; Wed, 6 Mar 2024 10:45:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721950; cv=none; b=Cow3jzItA0R1nkJfX3wDqvXG/FeSzAZFAJeDG+/JfnIqGLr7uNTS4EvZL0r6/KujQMukR3VTmR4XP4Nbb+fdTHbm7CJSndAE45pVUH0IoTzCbJbZ3H3FfT9OYJ3DKZLeFDerDx42rXpkpnJ8XVXMaS7jSaJd1LC60norgYkptWs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721950; c=relaxed/simple; bh=1iOMjIDBisCaf2/gUeBMwvH+IKTk0Wc9C91o2m9GN90=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iYXVO2OCFLYjVMvimSDDDOFCtR4TfE3KUeTsbckEiTedbaQqdC1anZ7foNvayeW+0Rvs6s/6YQfYd5b4vOtGlRofgHCWHAeBXigm066zn2ROtyb5/iGLIAGaB7WpuhCfokgUG2kPvEi1b8Q24r8l4xW+fMxS2c8DHBapd9Lmuy0= 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=aU2+ZMgm; arc=none smtp.client-ip=209.85.208.44 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="aU2+ZMgm" Received: by mail-ed1-f44.google.com with SMTP id 4fb4d7f45d1cf-56715a6aa55so3979169a12.2 for ; Wed, 06 Mar 2024 02:45:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721947; x=1710326747; 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=MebKaQUWBNfrYV+FUUP3D2chpFGoR/EqlF1UJnbSX/k=; b=aU2+ZMgmxLxvjwDrEiD687XgrUpWgwcyYPzDsbsWyE7EhdvMTrF2SDfPnnxUlAJ7ls 1KQj+NTXi63bdlpbH8vylAs5/2/gEGAdFnAN16GZoKpnvz6tobWmMXeY02coB87fC8ls Bc6pR5uJ6Qf9QibciOCzrDbMNsaDoIxFkAnr69+wcFbSumE91yJ9NTS0Gc275f3LbrIU QtZQ23RaU1aTVBUrvsXN7GZV5mglZL3XRGNUmrqNACSdo1UbjCe9Eitjlnvh/KObo128 v2jDmvyxs4fU+cIWpYN2tQf0SazYCu/gbp7NSjaqKZlI++5a2sR1aklVqypLh+H/DH6K fK9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721947; x=1710326747; 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=MebKaQUWBNfrYV+FUUP3D2chpFGoR/EqlF1UJnbSX/k=; b=AbA/c9LM/6M48EaUreEEwQ6fYfkzsUf0d+O0bMylbkapN/9XSGCAc2EAttJuTaDPUx xhz0H/XUNfsqNt0JkHIltAKWeLKlFrM7E03wzkCBLFCROBbYtGBhM03eu4nxoqnbBMV0 PZdWy1GSUOv5uI4PF/DnuB4t8R37UBwypnPODxGMhpnqaHhSZGiBlohNce+1VMJHPRW2 HWc4AneWqagCuixgMqOF+bjMWPULzCnFX5rs/oScvbJM3hyngjQZ3gv5keSxRE1oGyeM jc80z7wW4fkP7UhFQMJ3ae/UrZ4eCQzMe1N4f4BR+kuI8HxEpVj5FD4OTkGd5L3yc18m /GNw== X-Gm-Message-State: AOJu0YwNLXxkr50AZlLW4PM8VcPxd172HIMtEi4KN8w6wBl4MHLAFil2 DXBeDaTdX/oBFoBz7KHaqHMPiX8s7e3u28TkaSzr2g04BJKCALqCSr4fXVW2 X-Google-Smtp-Source: AGHT+IGJG/xdUg0MmAPWeBgl/2SfgsCQpFECKzosr5WMkpNMisl3CkKRiwGT0sledZUDRftQUneivQ== X-Received: by 2002:a17:906:27db:b0:a43:dd85:e171 with SMTP id k27-20020a17090627db00b00a43dd85e171mr9588561ejc.69.1709721947030; Wed, 06 Mar 2024 02:45: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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45:46 -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 v4 06/15] selftests/bpf: bad_struct_ops test Date: Wed, 6 Mar 2024 12:45:20 +0200 Message-ID: <20240306104529.6453-7-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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 impossible to use same BPF program in several struct_ops maps that have different struct_ops type. Check if libbpf rejects such BPF objects files. Acked-by: Andrii Nakryiko 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 b9b488d6d53a..39ad96a18123 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -611,6 +611,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) @@ -622,6 +645,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 622d24e29d35..23fa1872ee67 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h @@ -89,4 +89,8 @@ struct bpf_testmod_ops { int (*tramp_40)(int value); }; +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 Wed Mar 6 10:45:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13583863 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (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 195E06086C for ; Wed, 6 Mar 2024 10:45:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721951; cv=none; b=kl98U00hPevamfNSjnCo2xnS5LoPOQhh2cT2qz4ly/zDqR0VKAaIrGjJuSn83AXuloCzTHerK/2bpV3LzUhsh4vs1l3ccrwZpKBeXZnfgFzI6KpnHfQvjk1PfHRkvYsWe76/hlltW3djkcMQrMEXNn9wYJbvhFMB5qF/BPvgWtY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721951; c=relaxed/simple; bh=d3p2s7QmzxToDK96TFHJQ1qZN4aIgJhdEigNkUD5eww=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AVxDTqNlIUYIpfeYtws/Lx6GGAuLoCGVwmeG7igaNXAuxn8kZZjH03d7QshisI/rhKh5iAlnyzKQqIu/dffG9a7IjXp/BceqLUM8TYnOZaaEl25b4tCZMNc8dmFNb46SlmfhYtR7pr8A69Sit4blpwaVa066GKuTmqTg/irHYZI= 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=MwvK8NB6; arc=none smtp.client-ip=209.85.218.41 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="MwvK8NB6" Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-a458850dbddso314686866b.0 for ; Wed, 06 Mar 2024 02:45:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721948; x=1710326748; 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=HcjgCbM6tI4PtrjhlfgHXYswgkyt3LDoGkG3c2uEf/I=; b=MwvK8NB6guaKaWduK3e9mg+ZAc4zKquBOTTACn7ztPZqdR3wqFAfyI8kOxF8Lw0Qgt ATeVx4L7UwsgHTGEXfKScgGbFaRKJKB9jAuFrqd8XbJtE0AnqNFmFA4lvh0HnTCtKrTT XSkcnLuyNDWX7CHws+yqPa/CVd0Ws3h3AVWm34piym+9PAVGY8SJVlJTbrJGPH/0bJLh u+icRK0lQ8FVOJJFZqH8pYREcGa+H4Px8ViDoPbGTK6MPf6vLi0wbMVSNalc2xhCrjCp 8JB33xWQ86OmIAdjQbcW5c1U3IiTXXKWFHVu4g3YhrT0lbSlj8q8Jy6Ux43WHgJbID3q 3IkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721948; x=1710326748; 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=HcjgCbM6tI4PtrjhlfgHXYswgkyt3LDoGkG3c2uEf/I=; b=BE9j5ypOF1D3hQCb5uQE48X291mOzEJqS6TJwXH5GuWRc575EhPY7qcnD79tWUHms+ GVug+hFpZHlolWUzSm+l6rBpj7yv0oRsbFQZ5PHQ4luFuXbbevAXovsVdt1F5cQi//dj ADjlCC0XmsQWp8ci6kjI2D2MiQ0p65SD5V7iQQJ4d+Z0mywzMoPR990sQ5cH2VcEXCYQ r02cPsmVR4odwXQnvO9yT995P35cOHChfbWYXoffpWvKle1U4eCcVB2BihTSAF2dv5rS 29vVE70VWeKg8EN8asMWX/BDsQB9Fd+ubDlDl+UVqVmvFcDDFuAmgKKB/GN3vrYxBli+ 4k8Q== X-Gm-Message-State: AOJu0YwJl4aamRdCyZza02LxwUhVyDr6hRmTk9DC/7TJdd/WlHlIAY19 gqLnbc2KxaYdZgy0+thmgWkG8HrD+xY8irrneud1H7EKUDdEQBCitxZXT3I9 X-Google-Smtp-Source: AGHT+IFLDt1IvMYPrqAZ/kfmqoVdbLskdVSLF8dz2gAp4FdkAC37wkw7Mdwdgf8mAXIe7LDYq2zwtw== X-Received: by 2002:a17:906:f9c9:b0:a44:3db:6649 with SMTP id lj9-20020a170906f9c900b00a4403db6649mr9628372ejb.58.1709721948113; Wed, 06 Mar 2024 02:45: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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45: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 v4 07/15] selftests/bpf: test autocreate behavior for struct_ops maps Date: Wed, 6 Mar 2024 12:45:21 +0200 Message-ID: <20240306104529.6453-8-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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 | 76 +++++++++++++++++++ .../bpf/progs/struct_ops_autocreate.c | 42 ++++++++++ 2 files changed, 118 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..a5c8bb9fcb2c --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c @@ -0,0 +1,76 @@ +// 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 = NULL; + 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) +{ + struct struct_ops_autocreate *skel; + struct bpf_link *link = NULL; + int err; + + 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; + + 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..9a951ee6f55c --- /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 Wed Mar 6 10:45:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13583864 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (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 9DBBE605D9 for ; Wed, 6 Mar 2024 10:45:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721953; cv=none; b=OoL3d+lJUz9NRhmseooXmHm+viyNGUZR8FV0BbOqJ0k5V7piPT543yb2ZKSoq5BZYfnw3prnIUUtGX4dvGYBYrWrGPwiu12TybV+STRasl8tR5/phb3LeO46N3V8fXDaWg7k4mSmm9hyJCfW+OOi7XwB8msW04Ij/+1q0Oz9uDg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721953; c=relaxed/simple; bh=A1SfXuTio4KSbs6b4XtpubbI6FmW0LNaFpy56FN93KQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Z9UiiAzmbmO7xB0hK+oBpHeZm8kP58YP+6UIumILedoYJSIVI5mMqJ7o6ohMcSxwaufuGReOpfUWwmKxkwEq8mJlZ+26boMVg7YyAZQAQ5W6IvJNCsqOZoAWGXYYRjXLIXdoMY1aBvfSThIbY2B0hmITkCJ+4wY0lgbXL/tbfhc= 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=mKzzdPAr; arc=none smtp.client-ip=209.85.167.45 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="mKzzdPAr" Received: by mail-lf1-f45.google.com with SMTP id 2adb3069b0e04-51323dfce59so5943889e87.3 for ; Wed, 06 Mar 2024 02:45:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721949; x=1710326749; 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=47GfzjYGZTHpEJubLYjHLgWE8gjKnQhjDWwL4r9PGc0=; b=mKzzdPAr8HDkmJztw7fqqYubL105X1+pofXn3qot97pW6Ddb56OnKKVwZXlMhvH5Fw hvlpG2R99eu3Ac7iLi08poEeoykoyALX9CmtOAoPo0l469aD4k/ErGcfD8EdlwFtpWWf R/TNgbWhYPu79x4oHUJGGWWvkr5a5PGm9D0PXbHtr3cXbbYhAlBSRWTcKzJXpobttz88 0TVhnLdUslZfVDvDrEW0UhTcH1wJLqM1pQUZwy85D3XlR9UBBBOjXRWGnbYTOCiuHpBD K1jnE4Xt6dV7yOd7y1Ek8kDUhwGucxm/irxKyaDKd14ZGoC+WKEB8d1XuZs0X7XQ78GV dbpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721949; x=1710326749; 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=47GfzjYGZTHpEJubLYjHLgWE8gjKnQhjDWwL4r9PGc0=; b=aBZIudOeGxovUO4TdUHAOR0qi0wdXc2xm4nFvXcwLZZGNVNcWEHp7EYB0q/31rJhU4 +6PGFCGqSnkdEOi9QlZU1D/XM2NYwHWScmj3w1U6EueW3/Oo2fq2+LnuAm/EVkRxpAd8 LGhnYV7ki5LhGFaLNPo0NrCSSyzihHX22VR7bX5amj62r/OiG/FvFP8LH1goCGNrnpRG AAyh8NAola4LNmDp238KT+15aMRnI7SOcMYT7aN3AxgdA5oKSR5IAtsNCCF8bh4KdeOf W9cGG5wEm0iOP+mhw4i4IeQ7vOrKHlywFHLRqobwX4mL/9O7yBbAEdb64/C7TENKCfcJ jkPQ== X-Gm-Message-State: AOJu0YxcbIhnD4DmFsTCRzDwSx65gjFzCav2GDakZepvJgVdSbXcXleP eQMoVCOTx2VF38VtIwgxRqE/vxYp6cJKQ+8CHzX06zI+SZ3Nbv3lKIPzzArC X-Google-Smtp-Source: AGHT+IFvQ1RgzhQnroD+d9sge1EwhqofShFH5ovqG0lzGmJplxojemZqs35aYiK9K0AAVzJhIYek3Q== X-Received: by 2002:a05:6512:312f:b0:513:58c7:371f with SMTP id p15-20020a056512312f00b0051358c7371fmr1932446lfd.65.1709721949211; Wed, 06 Mar 2024 02:45: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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45: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 v4 08/15] libbpf: sync progs autoload with maps autocreate for struct_ops maps Date: Wed, 6 Mar 2024 12:45:22 +0200 Message-ID: <20240306104529.6453-9-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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'. During load, for each struct_ops program P, referenced from some struct_ops map M: - set P.autoload = true if M.autocreate is true for some M; - set P.autoload = false if M.autocreate is false for all M; - don't change P.autoload, if P is not referenced from any map. Do this after bpf_object__init_kern_struct_ops_maps() to make sure that shadow vars assignment is done. Signed-off-by: Eduard Zingerman --- tools/lib/bpf/libbpf.c | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 1fb9a4f237b4..595c287d84ef 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1031,6 +1031,48 @@ static bool is_valid_st_ops_program(struct bpf_object *obj, return false; } +/* For each struct_ops program P, referenced from some struct_ops map M, + * enable P.autoload if there are Ms for which M.autocreate is true, + * disable P.autoload if for all Ms M.autocreate is false. + * Don't change P.autoload for programs that are not referenced from any maps. + */ +static int bpf_object_adjust_struct_ops_autoload(struct bpf_object *obj) +{ + struct bpf_program *prog, *slot_prog; + struct bpf_map *map; + int i, j, k, vlen; + + for (i = 0; i < obj->nr_programs; ++i) { + int should_load = false; + int use_cnt = 0; + + prog = &obj->programs[i]; + if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) + continue; + + for (j = 0; j < obj->nr_maps; ++j) { + map = &obj->maps[j]; + if (!bpf_map__is_struct_ops(map)) + continue; + + vlen = btf_vlen(map->st_ops->type); + for (k = 0; k < vlen; ++k) { + slot_prog = map->st_ops->progs[k]; + if (prog != slot_prog) + continue; + + use_cnt++; + if (map->autocreate) + should_load = true; + } + } + if (use_cnt) + prog->autoload = should_load; + } + + return 0; +} + /* Init the map's fields that depend on kern_btf */ static int bpf_map__init_kern_struct_ops(struct bpf_map *map) { @@ -8175,6 +8217,7 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch err = err ? : bpf_object__resolve_externs(obj, obj->kconfig); err = err ? : bpf_object__sanitize_maps(obj); err = err ? : bpf_object__init_kern_struct_ops_maps(obj); + err = err ? : bpf_object_adjust_struct_ops_autoload(obj); err = err ? : bpf_object__relocate(obj, obj->btf_custom_path ? : target_btf_path); err = err ? : bpf_object__sanitize_and_load_btf(obj); err = err ? : bpf_object__create_maps(obj); From patchwork Wed Mar 6 10:45:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13583865 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) (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 3285360887 for ; Wed, 6 Mar 2024 10:45:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721953; cv=none; b=I3tu8mHWsx4AqrorAJro3w4yoTee3PJNYN2buesya2RnlMW0F2Irf7qFm2xT4+Q7U+WLq2AsoLIyTjvmuGmQfiuKkWEk50LktnxHkrj5ZbPPYIPhardngEzQRtGafO2bcFOcZDze6S3MmLTs2BcQfSLLUvlwQbmINMF7Rjb9lO4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721953; c=relaxed/simple; bh=G4ecxg7LSoDP8MJVLH4UV2xdOkweAMnKQ77b3nVDSQ4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BFTfmxj7WtGXBpvd418/hSRu+argCuNX5+NnDLLVV9v8cC+T5PvpfvgRbOGezLppkTkS42beaht4NQVnSGD9w/i9WnQJbhabM/buO/rRBCJavt2Xb9hwPa9heHZIAre3EE7ilF1cNGHfKKuc6qJWye+wqg5zg+ijnmYzy4imwS8= 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=YjS8o/rL; arc=none smtp.client-ip=209.85.218.50 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="YjS8o/rL" Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-a3f893ad5f4so971477866b.2 for ; Wed, 06 Mar 2024 02:45:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721950; x=1710326750; 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=l7Tp1+0QPXm9bAZ+co/csJxjcRsh7mBUoZj4MMBt58o=; b=YjS8o/rLl1BdP1LoXpfh0MWYwLilYJtZPaBbPeCOwHfV2stVOennz/UE2uNj2E4meY nfFpbkomiaqx2yygQv1/B1Zf/jUD2IJ+b/C8u4KkzM+J+EaT2A7PX2Q3xksQQDyG5M5i 5jPlqNeLN8InHNhOHQm6fqhMm9I62cPT2PgE9tLLi0JK5tYBEql8iNRbNzAbxCmSaqUs rJ1FgTjpHqE1yA4en79iHvJ7PH8njXeJsAdwnUC8nP1hhqJcnLQroqT7wjWGxSDHX/Yp 9pDleh2+/ODkwj/X5nZgUI1dDEd2ddu++gr7vdFiiv/9d71Im1LhJilo3R+49L0cIAPZ +/ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721950; x=1710326750; 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=l7Tp1+0QPXm9bAZ+co/csJxjcRsh7mBUoZj4MMBt58o=; b=eDfcWmWimPEAcuMPQcFoZfDUfRlWSvFnnGZgahpTgxfS90kSrx8PKfQui33GaRkv46 NUXF54rPDvtNMep2GArlxpUpAYU7xXxbB3RBjRDQ1j+M4FMCIj9DFHzP5xnev/GuuWqT C7D+K79rnnppRl5Fy6yaIstYbSRCubAkrvzHf3px0X6+4C9m8nPI9dc//LAfHDbd5EWi Ijje/ElCBuPHudSDynbQZHIXKtoNBm0tfFMfucI24dTzy6uBqzVLaZak7jv6UFSYy9Fq o38nO1LYj9D86x/Pv3m8P9csKDU+x6ZkGF48+/D4lR+wjm/NEttDx/SqKBTBo46ZhXNu z8CQ== X-Gm-Message-State: AOJu0Yw2PnCv6DkkvjpOJkl/S989HFg1cqXoc6Ja+LWVqWlTfdvsc24l Q+PoGgzT4nrLLSCHL+3qZ9nv9Cx4BFmal712Cry9XxYrL81MwNq12t5RAei6 X-Google-Smtp-Source: AGHT+IEENWkkyQZSUlrGBITDlHZpJub1P5JJAfzyw/XOK2EYw1OZzJgCg9gQf8/RDxr3PBKcNHcV1Q== X-Received: by 2002:a17:906:248b:b0:a45:ad00:eade with SMTP id e11-20020a170906248b00b00a45ad00eademr2711628ejb.57.1709721950389; Wed, 06 Mar 2024 02:45: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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45: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 v4 09/15] selftests/bpf: verify struct_ops autoload/autocreate sync Date: Wed, 6 Mar 2024 12:45:23 +0200 Message-ID: <20240306104529.6453-10-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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 flags of struct_ops map cause autoload of struct_ops corresponding programs: - when struct_ops program is referenced only from a map for which autocreate is set to false, that program should not be loaded; - when struct_ops program with autoload == false is set to be used from a map with autocreate == true using shadow var, that program should be loaded; - when struct_ops program is not referenced from any map object load should fail. Signed-off-by: Eduard Zingerman --- .../selftests/bpf/prog_tests/bad_struct_ops.c | 32 ++++++++++++ .../bpf/prog_tests/struct_ops_autocreate.c | 51 +++++++++++++++++-- .../selftests/bpf/progs/bad_struct_ops2.c | 14 +++++ .../bpf/progs/struct_ops_autocreate2.c | 32 ++++++++++++ 4 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/bad_struct_ops2.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_autocreate2.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 a5c8bb9fcb2c..78948dc6445c 100644 --- a/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c +++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c @@ -2,6 +2,7 @@ #include #include "struct_ops_autocreate.skel.h" +#include "struct_ops_autocreate2.skel.h" static void cant_load_full_object(void) { @@ -43,18 +44,20 @@ static void can_load_partial_object(void) 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; + ASSERT_TRUE(bpf_program__autoload(skel->progs.test_1), "test_1 default autoload"); + ASSERT_TRUE(bpf_program__autoload(skel->progs.test_2), "test_2 default autoload"); + err = struct_ops_autocreate__load(skel); if (ASSERT_OK(err, "struct_ops_autocreate__load")) goto cleanup; + ASSERT_TRUE(bpf_program__autoload(skel->progs.test_1), "test_1 actual autoload"); + ASSERT_FALSE(bpf_program__autoload(skel->progs.test_2), "test_2 actual autoload"); + link = bpf_map__attach_struct_ops(skel->maps.testmod_1); if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) goto cleanup; @@ -67,10 +70,50 @@ static void can_load_partial_object(void) struct_ops_autocreate__destroy(skel); } +/* Swap test_mod1->test_1 program from 'bar' to 'foo' using shadow vars. + * test_mod1 load should enable autoload for 'foo'. + */ +static void autoload_and_shadow_vars(void) +{ + struct struct_ops_autocreate2 *skel = NULL; + struct bpf_link *link = NULL; + int err; + + skel = struct_ops_autocreate2__open(); + if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open_opts")) + return; + + ASSERT_FALSE(bpf_program__autoload(skel->progs.foo), "foo default autoload"); + ASSERT_FALSE(bpf_program__autoload(skel->progs.bar), "bar default autoload"); + + /* loading map testmod_1 would switch foo's autoload to true */ + skel->struct_ops.testmod_1->test_1 = skel->progs.foo; + + err = struct_ops_autocreate2__load(skel); + if (ASSERT_OK(err, "struct_ops_autocreate__load")) + goto cleanup; + + ASSERT_TRUE(bpf_program__autoload(skel->progs.foo), "foo actual autoload"); + ASSERT_FALSE(bpf_program__autoload(skel->progs.bar), "bar actual autoload"); + + 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 */ + err = ASSERT_EQ(skel->bss->test_1_result, 42, "test_1_result"); + +cleanup: + bpf_link__destroy(link); + struct_ops_autocreate2__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(); + if (test__start_subtest("autoload_and_shadow_vars")) + autoload_and_shadow_vars(); } 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) {} diff --git a/tools/testing/selftests/bpf/progs/struct_ops_autocreate2.c b/tools/testing/selftests/bpf/progs/struct_ops_autocreate2.c new file mode 100644 index 000000000000..6049d9c902d3 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_autocreate2.c @@ -0,0 +1,32 @@ +// 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(foo) +{ + test_1_result = 42; + return 0; +} + +SEC("?struct_ops/test_1") +int BPF_PROG(bar) +{ + test_1_result = 24; + return 0; +} + +struct bpf_testmod_ops { + int (*test_1)(void); +}; + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_1 = { + .test_1 = (void *)bar +}; From patchwork Wed Mar 6 10:45:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13583866 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ed1-f42.google.com (mail-ed1-f42.google.com [209.85.208.42]) (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 6EA15604A7 for ; Wed, 6 Mar 2024 10:45:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721955; cv=none; b=kqUMsYETfUn4NfKrFAHi4afaD7OM94j1EPdH2xuxwdje8iZxIOyY2nuEy+h3zSHzDqsXUQ2ssIE0UcrzA86/My9W/bfAvxj0haIk93HrpbrggtHo7ucg/PrMYM/cLKWjvLdC5FQfVhYXPAgxZ0vHvKBnc8vUGvqcl6dcq/IGeSw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721955; c=relaxed/simple; bh=liT2Bf2pYMcGiLNQzN3/ZVAPOpSd4cYypNOz1rx5Ruo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=urFoMPJew8kscG06SZSpgav9ub170ntAyavzDjMCeUWZmLQ13TdbMLfT07e+z6qalJSTS6seGqFp0l58HmANjY/MT/2mXfPaOP25l4ogDXpMDKrMrwgxa0gvV8WNYred+k6yVTtSknhjuL4UN2MD15j2bwxn4aXQ9QS+KOsLGjA= 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=DJO40U//; arc=none smtp.client-ip=209.85.208.42 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="DJO40U//" Received: by mail-ed1-f42.google.com with SMTP id 4fb4d7f45d1cf-567fbbd723cso222547a12.3 for ; Wed, 06 Mar 2024 02:45:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721951; x=1710326751; 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=JD4CwcZkWrmekfCA2TcuuBFUVuij7VezMqcO0//4liQ=; b=DJO40U//y3FobJHDqfHXEd034p0jFramAnbftN1VVeLmnf1V2t6OlTMd+UQl2qiBwt sDZ9KTFzlvhyA1QEhucUbjPS6z8hTRZzt9N8uOZCVi05S8X5+y82T/MBkvhaLxlI5iql tkH8Hy4fny8I/lTxceDfAuzJVBAm9fP3znmV5AjqKJRpSRolKssnNNYe3vS9OVKaiGvc ArNuhcrkfV4RBHaYHeMoa7+959CbTJJPUr9Cl9SzIbt5gyzCHa39CM3wVn2oZ1hvoTB8 s142MegH3YVM1DX791RTz3wvn+TNPGXBgi1fXovYXFRJvoWxv5ewFcrS9t08076Z+Uap DnUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721951; x=1710326751; 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=JD4CwcZkWrmekfCA2TcuuBFUVuij7VezMqcO0//4liQ=; b=U4XfHOLoMDkLcCDkldCuG3oxqM4Us9d+QB5h4qBwiu/7tSwJ6omvIvjT+x+haZmIs4 M0OtOpUSlFJy5qP6s/BXMq/tO/JnbtM+KevBryP+kxP1X4aHwh4ToG/DctnXWuEfaNQ/ 6p597W/IzZ8A87dRpOTB7z3joaaUwSNo3OdD/yN8X9NA4vzLlerCJWVlHokpu8F+nCf+ Ee5h/mQ8ONaHvMWsw139ipog2Cq6I7vQjDPzxoKJsuJ1//NbWvKcaD/iqEwO2TM0i4DH ZU6MalLzaoC1EiXVueTHcwxxjzlCMmoeLmzIp8AM4R1fNkA2K1nPjJzVVPro3mOXTHKY 8WFQ== X-Gm-Message-State: AOJu0YzFC6ifbqR2bXhsxhFWlYoexIW0+0b0opdqUL68iDe9Voa/lRSm qpaU8OnlkCNvRFD+DqoKm6KdT76iVSB+f5goLAdln61QuDbaN7VXvuK5ZFW4 X-Google-Smtp-Source: AGHT+IHzD6NUj8aLdE/2DC0NbrDALtmfkuI4JxElnyjTmKC+OP3dFWyontdry8wyWT5t/+4Fhqiwjw== X-Received: by 2002:a17:906:714f:b0:a3f:4596:c3c8 with SMTP id z15-20020a170906714f00b00a3f4596c3c8mr9509729ejj.53.1709721951408; Wed, 06 Mar 2024 02:45:51 -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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45: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 v4 10/15] libbpf: replace elf_state->st_ops_* fields with SEC_ST_OPS sec_type Date: Wed, 6 Mar 2024 12:45:24 +0200 Message-ID: <20240306104529.6453-11-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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. Acked-by: Andrii Nakryiko Signed-off-by: Eduard Zingerman --- tools/lib/bpf/libbpf.c | 61 ++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 595c287d84ef..cff72de42bf4 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; @@ -1266,7 +1264,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; @@ -1328,7 +1326,7 @@ 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) @@ -1363,15 +1361,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, @@ -1409,8 +1417,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(); @@ -1427,8 +1433,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; @@ -2973,14 +2977,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, @@ -3681,12 +3684,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); @@ -6999,12 +7002,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 Wed Mar 6 10:45:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13583867 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BCF4F60887 for ; Wed, 6 Mar 2024 10:45:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721956; cv=none; b=tcnB1yPvCGydQAICoVHPosKTwwJ9ed1YdtrX6y1YFkUI0qCNJP/1mF0myTSfLIk+6oyRO9Laq5BgKkjEU67EIotSmeTcNcmG97/u3vfIWAmzCBAvHvbqfdJOlNNqtk/Sr6pSLZJbokMXi+aC5mVNFt/guqj9yxPHwqESLLLp+II= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721956; c=relaxed/simple; bh=vFK0/Gi72ctLPZvfLiE2fSIeO440e+Xuwy2HI5yPo8I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=epub1AO9kXpYht6ZXBXl6Rzar3CTr7NdjW9IzKnLGGV9kO4Q1C4TTNOdTry8mPArlG7nrJWV8NHRjfZy9Gr1CibpBzOObOmRP6RSr7MjhShdjnGorC7wBUMs5ShPhtuu75ZCXHD/0F3EeIPQUAvM0D8+vPHyb0j82bEM09nNM9k= 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=S8bFMbZZ; arc=none smtp.client-ip=209.85.167.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S8bFMbZZ" Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-512e39226efso6733878e87.0 for ; Wed, 06 Mar 2024 02:45:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721952; x=1710326752; 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=B38VbkNQ9IyC3AD4wqb11aaztk716mYb2bs5TwzwrxE=; b=S8bFMbZZosu2uxQNWxtuHH6+VtVPZmj0E9gBao3Pis86JsYiRbcVrJim3mBB3ihTqR 0C4ZyG+C3CZbbbYs7RTh87gE5sO/Pe4eE4mOtVGJ6yiyflj4agZJtpqUpopoPAU/MsM1 7FHuWYcFczS6bz0XN+DnCidOLXSF6s3rid0PmqjGhEL1GjtBuSi/Zu5z0VBPRxN86d66 eBypni31IjE32NevCjy5hGc1tqPtIzEdbGh3rMHco50x7Vjyin34koOuf8II+fgLEPLC DNgr5F+88RBuZmemHXa6j5dlqaCVV4tAyc63cdFtMiCv4v8w2jaPbkPYLwKAOXKIulqL 97ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721952; x=1710326752; 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=B38VbkNQ9IyC3AD4wqb11aaztk716mYb2bs5TwzwrxE=; b=Z+wSlMwxR+eF9slprG4BsaTDMG4b+Q39OzlQzCV2hu3tuiztKtAhFnz7FAQKlUIJEU 4RxJBwnwJvxDJpNifkBIwSy3i03v47PUysnAZZYhmVaWgSm5gnG4R9tQB96/pqG0R3ck mNSZ0K92mdUmojSycOQIQqc+3rznU1Of5/iTEBAwhTwDDQZXjyZaVdROUmA4AVITwIzD o6TimnJ8AVN9LjjzePB1pRcwa+annCSgcNA6DjrJJcDqOwxLIhRdW02ykXs2NM+Izmc8 YpB0s6uijqeTdZ0XclVCu2K0KjAzGQB1uueWzWhsyfHcifLDqeii1+mMXlo65zRhvUyQ 7BNQ== X-Gm-Message-State: AOJu0Yx594uQk/ZHJxD9tIoOHzPnRCnapP05MBj3KHPVQ3em3G035tY7 gHdZmkHDcgE7ReMrh1kX2gE8FVexlsSuGPfzwW/qC239LrjjBylbHBP6B6yf X-Google-Smtp-Source: AGHT+IG/iEuBETYtx5pURvmIh2Nu+K9p5/gjXGX1DeBwPvcYQ8eGot+SSbP6cu+N3wVfp61egnoXoQ== X-Received: by 2002:ac2:554b:0:b0:513:2858:6a67 with SMTP id l11-20020ac2554b000000b0051328586a67mr2487625lfk.63.1709721952555; Wed, 06 Mar 2024 02:45: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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45:52 -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 v4 11/15] libbpf: struct_ops in SEC("?.struct_ops") / SEC("?.struct_ops.link") Date: Wed, 6 Mar 2024 12:45:25 +0200 Message-ID: <20240306104529.6453-12-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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 | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index cff72de42bf4..ec0f508b853d 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1322,6 +1322,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; @@ -3685,7 +3694,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, "?" 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; @@ -3705,6 +3716,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?" STRUCT_OPS_SEC) && + strcmp(name, ".rel?" 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 Wed Mar 6 10:45:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13583868 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 98EBC6088D for ; Wed, 6 Mar 2024 10:45:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721957; cv=none; b=iMPiXvDr7feXm6msv3VQoZebRoMT3I/foY6v/sbtrml/4nU7AGgyPHrpxEdYdl6ifkkHPccuFIO1U/l00mfW03VrhGxDeX+gtilY3xQW7xDNWMFaasBJ39x/BeVDdNnQm0Gdi9NFI2sNrJXg8fmeMm8/m7ZAZm3xaFRftzIMoXk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721957; c=relaxed/simple; bh=tZnEqdsal/o/rqDi6IjS14FUtucitKe1FHu6ezz6Lwc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n/BfS9czuDI1rQtOECqOdxZZGNZCNoT5cJqyWVEep+55aCKOS/BQhSIRMzwXuCUDtiq0oNgSyCNDaX0mUF8tAvFxywoz5A3pciFPzUQ7PO6UhWyHE+YfjE2eMn4Z63WrucoxaVpt0Av6jkR4ohPoxaFz+cneNdlMoRcNsmcOdGs= 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=Mms3iSyh; arc=none smtp.client-ip=209.85.218.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Mms3iSyh" Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a4429c556efso889892066b.0 for ; Wed, 06 Mar 2024 02:45:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721954; x=1710326754; 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=GHgo3qNc7kN9i7+sdJw0Q+Ih9OYuF4pyHBnj+/ISHjw=; b=Mms3iSyhTiRA6V+lYcLbafgBRQjYLmhbgCvpdWmoNvzhZIrdLFjKyedXpUeLroDaTD ghHi32u3uHiLPRibTRK1dfNlV+A0rZukTj23irnCiUoq0SY03D1+FHqxyrxj/6HHaaZe npY6ZjBg4QVVbq3sX+QEaFh+rclRSA04crR1j+7S6q2RbTBb/McIZgR8KdqacgOmCQ28 6lRSEnksI94h1dcN0dPFbtrJ3wgy39XFNV/QklIc4xCAEjIuH7ddujL6/95WP8Pr3rrN HoEh20cmom7/wAvclsWvJZllf6AlLt+kB1B2+lj9kEfRgcGFxXShSy25MzRDi04X4mfI jxSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721954; x=1710326754; 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=GHgo3qNc7kN9i7+sdJw0Q+Ih9OYuF4pyHBnj+/ISHjw=; b=uxCg2qHR3R9CGakk5nmHT6b8BFaU1khWKW4yO7OwyCL+wcNEul1WV+vD9SXOH7ShHh 31Dac/AwnxM1rjj7BFfNw64H/8af7K8thnLsz7Mwj3RQ7buuy3dNghqAvrGwp84pndYk DPWNzJ/0JwbeQ0iYyXlfKyvrpv6ysD31aSiB4hMISI4tZRVgpKlMtl1yAX03GI5AGsdY 1mgWxgElOM5yRxJV4FgrjuyPyi8oP5uw/tUePlU+Zk9KABhaDqdyuUJOgiA0DZprlcl8 F6z7rhicugdwpLNLt3sGoA8m3e5/52pszWrKnGF3HkvN5Y0jCiyzx2aydsVrTEo4Z3Zh 0jZA== X-Gm-Message-State: AOJu0YwvgaRvtsBClfTEaMVNofPdSM0zgilJte0C/Vu63nW0ssDG5Bs+ fQQycD7I9pDQKizfZDDXDHBySUV8xDAsuEY3LfeVqmvlFj4+FDccbjDnt8ia X-Google-Smtp-Source: AGHT+IF6JnEvqQ9ZmKxkQpw4iX3L3ZYt3bCDPWLaeOl9DSEHAlRqteB7jbTVKt05JIC9KvDdDzEGYQ== X-Received: by 2002:a17:906:b24a:b0:a45:a845:7f91 with SMTP id ce10-20020a170906b24a00b00a45a8457f91mr2721473ejb.45.1709721953684; Wed, 06 Mar 2024 02:45:53 -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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45:53 -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 v4 12/15] libbpf: rewrite btf datasec names starting from '?' Date: Wed, 6 Mar 2024 12:45:26 +0200 Message-ID: <20240306104529.6453-13-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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 replacing '?' with '_', e.g.: DATASEC ?.struct_ops -> DATASEC _.struct_ops Signed-off-by: Eduard Zingerman --- tools/lib/bpf/features.c | 22 ++++++++++++++++++++++ tools/lib/bpf/libbpf.c | 19 +++++++++++++++++-- tools/lib/bpf/libbpf_internal.h | 2 ++ 3 files changed, 41 insertions(+), 2 deletions(-) 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 ec0f508b853d..672fca94ff53 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -2869,6 +2869,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); @@ -2878,9 +2883,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) @@ -2892,6 +2898,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; @@ -2918,7 +2925,7 @@ static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) name = (char *)btf__name_by_offset(btf, t->name_off); while (*name) { - if (*name == '.') + if (*name == '.' || *name == '?') *name = '_'; name++; } @@ -2933,6 +2940,14 @@ 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))) { + /* replace '?' prefix with '_' for DATASEC names */ + char *name; + + name = (char *)btf__name_by_offset(btf, t->name_off); + if (name[0] == '?') + name[0] = '_'; } 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 Wed Mar 6 10:45:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13583869 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (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 A7658605B3 for ; Wed, 6 Mar 2024 10:45:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721958; cv=none; b=NPErN81H9vLKNUp+cJqnaFli0t/ZaOg94LLN6hHONEL/2zOf4Sc5wWthEf1TTzmto7gNcr05khzflYpBMMir8fmc3E+7l9/2uFAOQyQryOuf3lqDFR0OKLGq+O79yAoZFxCFDB+O9T+NC1vpxEnp11m8ZYaGZ6rTRq5lrm5D55I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721958; c=relaxed/simple; bh=rqgFbIeNuDVsN10Y3Wc/XO8EnZl5RUPoZprVYF/DQc8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ARv8iwEBDEX+qeDGMMITq9SqL1r+cH240Dbn8C7NKYT0x2BuX07y3spw1n9J4CzVU/3vmqTVv76w2m3tYpV0XjqIDvS4q5PehrVz/ie7jXxk2Djc3z9q9NMqCW3z9MQk+YjK1R6otuOEmdFZWGo7h29mWb2Raq6kvH8GWl6Swn8= 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=ix2Xd/SZ; arc=none smtp.client-ip=209.85.218.51 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="ix2Xd/SZ" Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-a3ddc13bbb3so127266966b.0 for ; Wed, 06 Mar 2024 02:45:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721955; x=1710326755; 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=sT8tEW8RnySdDbuoriCWllNRutWRJvAqN1r7dcUmjdE=; b=ix2Xd/SZfnh9bO+Xwx8RcfbdLRtTqlb4ZVVyxTIp4OZqfXMUd5Zqe7WdQkwE0JsSQy /H+MK4Z4yAwSq3bGZha7bl2ADTexsp6OPZmFN6+x/zObq2RgTnE2TGr6G7TSTuxV5YKN +Gbl8/rANEoFiWzOhC2SdSaKAuRd3BWKpnahsEN2xcv0c8cvNqZSvq95YV3al78++BKP KKtfFT7nu/ymOcd8arbHrQSJ1bOzWqCahw1RcUjLTy/dng5GPv6UJ66EHisYh0E/7FvV z6umUYJSFMUgIzt5W0JJ5XjsOvopEner+r/Ob1ite2xgarN8Njdt7gNGyKyLh/r1J+DS n3Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721955; x=1710326755; 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=sT8tEW8RnySdDbuoriCWllNRutWRJvAqN1r7dcUmjdE=; b=eMw7NINH3IhhnZoBH2dOKeMXfnPM7zvn7c1Qh+zlh07McNdOHidG+IfA1qrsT+TiAw zYxaRNJn32vWRA7VPVCVuSfABgBQHewXabyj1UYT+1FeS03RYcyzAcVJP8QaC+LD0db4 Wro7dVw9TO0n0eU7cbSxqpoC8aT6MURI6npYx8A7Guxe5H1MPLMjuSpa7KXBlQvUa+Oa i/u3ycxa5URyTFYI8nLvhMNKYWa4s77C7OO22KgKmGXzPILucfveFYbIBlNpXnrRBSNY ZtFhcbDCI5+c4T7HC6NYh+0oUz8eMjjqN57COwK7Lp38+a+W7U6k8TF+gvPnw+jJWnn0 BsIg== X-Gm-Message-State: AOJu0YxSf+gNCqczv4nRMlS3mafdQFKShWY7cBXjS0IH1n7NDFdTpkd3 yqXmLgwfxK0SogNi5ny3Uk/nQMGpwEPrLNKcNn2UKLdnLrR/gkOYdZcafMM4 X-Google-Smtp-Source: AGHT+IFInlQzz0qIUa2Ds5TTKIIkya00rik6bD0nLjKdoDxfJHA9R4Y93T2P2TYQ1mW7nbpxNX6Erg== X-Received: by 2002:a17:906:ca5b:b0:a45:163a:c08e with SMTP id jx27-20020a170906ca5b00b00a45163ac08emr5775241ejb.0.1709721954899; Wed, 06 Mar 2024 02:45:54 -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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45:54 -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 v4 13/15] selftests/bpf: test case for SEC("?.struct_ops") Date: Wed, 6 Mar 2024 12:45:27 +0200 Message-ID: <20240306104529.6453-14-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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 | 52 ++++++++++++++++--- .../bpf/progs/struct_ops_autocreate.c | 10 ++++ 2 files changed, 56 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 78948dc6445c..a5cc593c1e1d 100644 --- a/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c +++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c @@ -34,10 +34,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(); @@ -58,15 +72,39 @@ static void can_load_partial_object(void) ASSERT_TRUE(bpf_program__autoload(skel->progs.test_1), "test_1 actual autoload"); ASSERT_FALSE(bpf_program__autoload(skel->progs.test_2), "test_2 actual autoload"); - 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; + + ASSERT_TRUE(bpf_map__autocreate(skel->maps.testmod_1), "testmod_1 autocreate"); + ASSERT_TRUE(bpf_map__autocreate(skel->maps.testmod_2), "testmod_2 autocreate"); + ASSERT_FALSE(bpf_map__autocreate(skel->maps.optional_map), "optional_map autocreate"); + ASSERT_FALSE(bpf_map__autocreate(skel->maps.optional_map2), "optional_map2 autocreate"); + + 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; - /* 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 = 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); } @@ -116,4 +154,6 @@ void test_struct_ops_autocreate(void) can_load_partial_object(); if (test__start_subtest("autoload_and_shadow_vars")) autoload_and_shadow_vars(); + 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 9a951ee6f55c..ba10c3896213 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 Wed Mar 6 10:45:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13583870 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) (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 8772560893 for ; Wed, 6 Mar 2024 10:45:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721959; cv=none; b=jBjK3wxrhbTzF2+pmbmZ3VDMG4ynIApYpyfYoX9B/OgAODz5DI1U71O6LzkUKsAfEXVOlX+JEV16Msy/Jdf7yfZyFTJj8iJlXIa1e2/mpCH4gr6luZMbx/OMfnXBU4dT2p4y0lJScerIkl0d+XY9lh9U7JoBXsab48+9Ru+MoFw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721959; c=relaxed/simple; bh=c626uGZGz6bvkWSfIGAs2RDjqVJ0je76+hAvK5HHS2Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D+DLhznQowsIVzl0dTS9ux6aOwZdqZFnwiywRSS9I1FYVzzce/cGWqoloTzjaTJmJJosD3RMxEEJU11pT3Y0ouywoDSzMAsa7QM/Ssl6tPXZW2uqbWqD7m0wbBsIuQu8/eRK1i+QnvFt5ZulMr3twdQDtJUoe1etyDu03nOUfzY= 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=couvARLh; arc=none smtp.client-ip=209.85.208.44 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="couvARLh" Received: by mail-ed1-f44.google.com with SMTP id 4fb4d7f45d1cf-565c6cf4819so1254578a12.1 for ; Wed, 06 Mar 2024 02:45:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721956; x=1710326756; 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=wzOUaWEMM76cWVANb2OxVF5f/AqcuB1wbqQNTxjWF78=; b=couvARLhQgH4jfg5nrb7ho6pCvqaU7/c6p3ikE8hnkgK13EzPV6k1REYvlBK129il6 A+zwWOWTLgZhcYtGADNjRsTwg1P+ChZUta0WPn1cLnBF7c7ROI/fxIK+uy8kUpiqnSwi UHoKhGccTQgMW5J4BVGZT9hH38XSPBJCdeOfpD0L3R7nCNWMSzICspgAGixT0Iz9/BTN xvvfQLLpkucyuouDwiTbjXB/KkSrNOq1kmujCW+taENOx3r8adwO3uTqpLyRepCX7FjW 2xizIyTjzHg24Sl4DLiYoeHRrVjAxYszcYGJGiSFYqYhSL5RMzk5T08CbJlH9wd2MlRz aljA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721956; x=1710326756; 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=wzOUaWEMM76cWVANb2OxVF5f/AqcuB1wbqQNTxjWF78=; b=g6ED5n06l68ldcPLUs9Aum/K89Ww0Ch6i2y6VU2uRh8mJOPwfOKYRtRqY46K+3PQG6 IxSuoY9Bro3QmvzuIKxilRn3IyX8kdj4Otw2MyHYlqnwHTr9h3HFVnabA8C6kb69J8h3 xbb55tT81dI4++e24zdNZxVf97y0Fknb5LaPY+XjwmojtfVEqc39nY+1YJFok/FhQbsM c9bICPrJ4X4xG2CMtdqaIZGEz3yTXVJn5DWI3iNyT8ZxwW5lVBj8B+V5Z63jVe9Ji7Jp 9wHevl3iQkF5bVSnVU+IsqZPJBFOyhYtqAi43+NK2N2+26FYC3dSEXB+3HZKDXV/eGM1 IisQ== X-Gm-Message-State: AOJu0Yxypi2sC4XJw5od5Vfg6gwawvGAEsxHMiKD04pvcUmpVUW+s5jn WXIHXpSIcN7KXVJ1VmmdoMr/C7VhC4iHY87wUU/XAxtBlQWWllj8zdU2hKQy X-Google-Smtp-Source: AGHT+IF8mFF9vKid5Auy4JklvLlGMlW7SPTJbEzRGbCJoKOMNX4oLz/caOOMXIvLkhFjG+QqmW3TOQ== X-Received: by 2002:a17:906:793:b0:a43:75ad:6d56 with SMTP id l19-20020a170906079300b00a4375ad6d56mr5995575ejc.18.1709721955869; Wed, 06 Mar 2024 02:45:55 -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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45:55 -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 v4 14/15] bpf: allow all printable characters in BTF DATASEC names Date: Wed, 6 Mar 2024 12:45:28 +0200 Message-ID: <20240306104529.6453-15-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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 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 | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 6ff0bd1a91d5..170d017e8e4a 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -809,9 +809,23 @@ static bool btf_name_valid_identifier(const struct btf *btf, u32 offset) return __btf_name_valid(btf, offset); } +/* Allow any printable character in DATASEC names */ static bool btf_name_valid_section(const struct btf *btf, u32 offset) { - return __btf_name_valid(btf, offset); + /* offset must be valid */ + const char *src = btf_str_by_offset(btf, offset); + const char *src_limit; + + /* set a limit on identifier length */ + src_limit = src + KSYM_NAME_LEN; + src++; + while (*src && src < src_limit) { + if (!isprint(*src)) + return false; + src++; + } + + return !*src; } static const char *__btf_name_by_offset(const struct btf *btf, u32 offset) From patchwork Wed Mar 6 10:45:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13583871 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 0FD306089F for ; Wed, 6 Mar 2024 10:45:58 +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=1709721960; cv=none; b=SbdbcEV96yi4kvhEn7b4RXJAqneODZbJXD5bNiI5SFA0G9o8vD0SH6tkTyI7yOKLcaOcnLnX9V0wZPmMCCTP0U2dI5cVfsXc7Rr7225VaUP7SrGTgtWSA2p4PwQBtN2ub4+xuEVCpv8+AQkTO6XDT6D01XYtz0nHOp1YjEKZWJo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709721960; c=relaxed/simple; bh=qq8YY5adS+/Dj50g3xdUaXj0dVPqjGcPBsacABXu0nA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oV+b2BxOnQvGbcceLiMO6sAOeVJeK7MTY75QNuP7S81WFsAV5/g+1vLvkIbNlsi+fcUJI0gnxmAykcsChgqjYgvA7z/4iutEb4EAQW95IzSgv0Akt7n+R0bZb6elmGwIM6Jfd02APHHKfDYYOdkhy4hTioaqJCwzSo/Njw/RJKk= 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=X9/5xrti; 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="X9/5xrti" Received: by mail-lj1-f177.google.com with SMTP id 38308e7fff4ca-2d3b93e992aso43270041fa.1 for ; Wed, 06 Mar 2024 02:45:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709721957; x=1710326757; 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=zV5iorLT4bPhioadrT3z62waasJmp7yXKBZpS5NIkKQ=; b=X9/5xrtiybAt3g+9wqQSJ79XgBQhj33yhfYqXSZMF6RZ9/GxaYtYcaGAF0kEW8sz5w YlBYeVxJV69XYOEV5Uzd9Kq5x2Hh5BT2WMaQU/sw3pcrYp6nNJw74hgCjws+dU4vo+h6 cKjHwhsDllc5PLUEAPkWQKSxZjAfhFm5d2LW7Z5dWnosCArAEyZKR9+IxRXx7UWUh8Ik CMIY+4YKx1Zg0s6HSYkOXVa9pbl2AUWeUDphUJZd6JpkYFIoGfNsGV9s449hsJqkcWEH V2AKZ1gfCD8sep0w4AXGPlmpJJPJG+ccp5G1uBfUV+e4olD4HjawgMO0Y8/j+W+rrf7u PhoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709721957; x=1710326757; 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=zV5iorLT4bPhioadrT3z62waasJmp7yXKBZpS5NIkKQ=; b=ay9Ukvgh7abJj5aeojy1C+c5VmMQ4LMt8/Yr29ZmkLBrC/zvkPRIc8PAdcoL3thudG jgmm8mXZB2Lr2jzKz7CaRaS/BuSTR3xgKbOv3THqJVnlArcEhsTm+Qyy19WaZgPPNK4a rPxixsedtRjscIzZ+/bOEO2UU+4DCwW/fL73fKzuoWuC6151v8YOH8KR32VY5cMOtOkm DkLI+jsmMKx9FafdWuVOAIopm7J9umtIBv6PqPJt414px5cILiTtYxFx19o/MtiGENGh ddrt+WIptO9JjtjFW0PiZhR2JHCKAnOFun9+eigLpuBgsPhBmhDavQWM9b4uaI5Ve25L BpIQ== X-Gm-Message-State: AOJu0YxcNquH8SPlt7SfkV3sz2jwm9zeXJHwPWFs9u0ME8d4IXIT4LOg gwmVqY+FWtUzHPDQOJ5Syk7DT5s0DREN4NewLJoujpciCJrfAmBCHz5A8dQp X-Google-Smtp-Source: AGHT+IE4M9KzztvTNKt/DJlHSfxkT37cNtdjM/4inIWBHQV/hkXDC9Q8dlNyRiW8jVvqe3ynfIyX4w== X-Received: by 2002:a05:6512:3593:b0:513:5541:e5d9 with SMTP id m19-20020a056512359300b005135541e5d9mr2103567lfr.18.1709721956841; Wed, 06 Mar 2024 02:45:56 -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 h20-20020a170906591400b00a3d5efc65e0sm7002450ejq.91.2024.03.06.02.45.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:45:56 -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 v4 15/15] selftests/bpf: test cases for '?' in BTF names Date: Wed, 6 Mar 2024 12:45:29 +0200 Message-ID: <20240306104529.6453-16-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240306104529.6453-1-eddyz87@gmail.com> References: <20240306104529.6453-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 Two test cases to verify that '?' and other printable characters are allowed in BTF DATASEC names: - DATASEC with name "?.foo bar:buz" should be accepted; - type with name "?foo" should be rejected. Signed-off-by: Eduard Zingerman --- tools/testing/selftests/bpf/prog_tests/btf.c | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c index 816145bcb647..00965a6e83bb 100644 --- a/tools/testing/selftests/bpf/prog_tests/btf.c +++ b/tools/testing/selftests/bpf/prog_tests/btf.c @@ -3535,6 +3535,32 @@ static struct btf_raw_test raw_tests[] = { .value_type_id = 1, .max_entries = 1, }, +{ + .descr = "datasec: name '?.foo bar:buz' 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 bar:buz"), +}, +{ + .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 +4389,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;