From patchwork Mon Mar 4 22:51:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581330 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 BA8893C28 for ; Mon, 4 Mar 2024 22:52:09 +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=1709592731; cv=none; b=DMYWvZisxJq7M2U09sb131kynKxUiRyTQ79G3egzudge8BuphkKo/im5sUlQoFVlOQzH6bpkj51td2kGkKWz00eNS1W6XgBthfD7oIlnbojEGC9xivC5F+dbMBuJotw6ADG/iFgPe23uQgeWD7vo1vLIJtKWKydV+kiDuPceXNk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592731; c=relaxed/simple; bh=u/1jTp2CttH5gxlhCHQfZKsq9FO8kAJbKLWIR91fUBQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HK7gjA1+sKzyN4epOBR5uXBKo828Tmxcr5Tw2ywsPXeEHsPb2fNl6Y9julAhBSp8k1Ud8ByJSBBFYpuiDcb/CiXULbVGsC7cJ2RpkUV/zZ1Rz1u8/BqKhaMCr0MX4Cy/jHEBuNqKCMuu/MUitMhIzAcXc0fUEfWdVoF0DBEOWao= 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=HvoDchRM; 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="HvoDchRM" Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-a4595bd5f8cso51405766b.0 for ; Mon, 04 Mar 2024 14:52:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592728; x=1710197528; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8ghBbmU7JgmzNE0IDgqQ93JxAVATdz4P5/PxXkAkYZ4=; b=HvoDchRM3kczg3QwlSjRzambSyVb8P6r2b4ykCA2t+zMCtpDliHdgNvoXOPZkEJkNE xCeVse0MZhsnt7wPhb8kDvZeOksnVITNQzko3GbbM8DSWt7O6zdpcAjA7CpG8tM8JnX5 +x5VQD4bvPopDdKC2Fa1ExXXQ2Giejw/yQA/5YlGuJMDWm8Vck4EM4RDQ6w3M6+R+REm WQccWqedMnXZHuH4gx+jeDVvoZCOVGHwP6jwTwhWTxx68iqwkTdRNNm2RP8FRc+XmVUO cQ6fKR/Ty7/iGTzIU8t0yLp1Vay6Donbw5IcLvgF+ou8J9IoCG+0dyuL7vMQVW1k+eQC H2RA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592728; x=1710197528; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8ghBbmU7JgmzNE0IDgqQ93JxAVATdz4P5/PxXkAkYZ4=; b=CrcjWemQCMP2rNUWqWPT1a6OYY/qyW8MlQsZG677bUf9jjpi4Gz5l+mrjQoAjdQep7 KhQTmgBQq570W4pNhyZnrvH4RPTHL8NjzqH24qaDWnhsMRzvfP4SlAgcIZo7cPjcmVVI IyeAced0smgbsgtvjWuNxq2zCrIdpxQFFwrSydp4+hSQ/VTHdvAT5cTqnhbiCCIjhR/E gOx1N6w9ayvdwhaUhkbq3Hjm8E1hEnwJ28W9ZyJsz0ojWHHZt2fBcZXz1e+S9HGp/Fn3 2kqxHWbgu5R0VmGCFgbramJF8hIRhb2II2zY2IgTMGj1ukbBWphvVwIBMmJlXkc4a3b+ 0nwQ== X-Gm-Message-State: AOJu0YwgeFUwQjqlcBgAoZ5jHo5vWiyx4AoXOGtd1ciSvz5qn79gcXCe rqLiNLoAtS9I76afEtP907jPvlj+0MArin8FyxEOOwuvU3zez2o2C38DErXl X-Google-Smtp-Source: AGHT+IGAb4EgNl97zuRuvVQP62LAqq3MLOW0U7WGCOfbBuLgeguI7qKwlwcrmmhk3G8PqTVNQLRNEA== X-Received: by 2002:a17:906:1153:b0:a44:86d4:707e with SMTP id i19-20020a170906115300b00a4486d4707emr6836454eja.29.1709592727684; Mon, 04 Mar 2024 14:52:07 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:07 -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 v3 01/15] libbpf: allow version suffixes (___smth) for struct_ops types Date: Tue, 5 Mar 2024 00:51:42 +0200 Message-ID: <20240304225156.24765-2-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net E.g. allow the following struct_ops definitions: struct bpf_testmod_ops___v1 { int (*test)(void); }; struct bpf_testmod_ops___v2 { int (*test)(void); }; SEC(".struct_ops.link") struct bpf_testmod_ops___v1 a = { .test = ... } SEC(".struct_ops.link") struct bpf_testmod_ops___v2 b = { .test = ... } Where both bpf_testmod_ops__v1 and bpf_testmod_ops__v2 would be resolved as 'struct bpf_testmod_ops' from kernel BTF. Acked-by: David Vernet Signed-off-by: Eduard Zingerman Acked-by: Andrii Nakryiko --- 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 Mon Mar 4 22:51:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581331 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f52.google.com (mail-ej1-f52.google.com [209.85.218.52]) (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 0A5F97C089 for ; Mon, 4 Mar 2024 22:52:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592732; cv=none; b=TQs3wulks757C7C4ppPqsfJ7QwhcI2SBm8kN4HBF1LoOA6ospiLT936AlEO3B/sZTvVJ3E210U5v3Xsa0azApi8dr8Vq2Uew+wxaB6QuERwerCYSIUYbI6ONueKx624Z3N4uZ4n0hCkqJ2R/w5JYDfOS529N3hDPJbxvDxW/uk8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592732; c=relaxed/simple; bh=7n3VOltWg2CvMWA84SRE1wH3LRcYRxUITz9dnx52KQ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dQTh/xHWkH7xohYMyXjwFatPeN8k6x/aM0U6zzP0OOc/NAZEDKR4HlUymMgT9DoJdCqk+1QP7QWxUM6O3Id5rlLNch6LLJuRASj+kRlq/YTW8V4aOSlRYCZmF8woK3qz2Djs7pjDzKGiMtuIIflGFZX29OXJbQ2v8eR5hELeqQE= 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=FqtOv5PT; arc=none smtp.client-ip=209.85.218.52 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="FqtOv5PT" Received: by mail-ej1-f52.google.com with SMTP id a640c23a62f3a-a449c5411e1so453822966b.1 for ; Mon, 04 Mar 2024 14:52:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592729; x=1710197529; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=v11JrOpQ0vOwRfRtr1r+yvwVR9OS5PTJrump/SfFJRM=; b=FqtOv5PTru2tCO224rSDU0hNu3YsjuF5T5iFUZLOtVIaXaivbdnHACZeSY1WFo3stI KbOXuBCJqRlLZ4kFgsYIwHZc6ENfxJXN7hleukHy360UUkihTlJSCRT+ECcV87KYga7t CGlQjicoSsYE0JvgP+hyaczIKwW6oQ46GbrUTbxhApbdsDkK7cvMPiGBF+eeUlxQXeoQ Omf+CSr/vPvyFB69S8aLGtULiGeGIoJIx7h3nIKJS9KoWcYPcwfxcapPueR80ey0b3bG uqUEezCnno4e+cw6FPC5aJY7Gwor0/b1rmcDbYTnWFlrTQghQ74uTlpklQrVXvLQXH31 qPGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592729; x=1710197529; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=v11JrOpQ0vOwRfRtr1r+yvwVR9OS5PTJrump/SfFJRM=; b=ljri8ZZegJ87M1UwoqigLmNd/AVvcF53uePerIRD8yeBQJ9OW8sAnwfTf9PZEOfo+9 0o5jGUSS/wR1PodgMqsQIrdOf6gHHbTa6+xyYmrOnly5OyxuZX/tRsCMK7PtPkv39Uxi IiJ60XaE832y+GTd3JrGdbI4NRZhKxa37jrnv+PhdqlYrcAvvJ8JUeRWVdt48I5my6FZ ZA6pa02vjmnwMXDO/DPWkhHtJ8neElpvwyN/2qccBtWUCvwZqkyieHl0fagAAzvgVWEN b/oZyW3ZzLsTKqHtM6ORCyGPCJNCfP/YzoML3lZfwzd1NAbLpaydIVjy459ArA0vQKDS KleQ== X-Gm-Message-State: AOJu0Yxscg5Z/2jK1+TemzBQkNDp4I/rj1gJiJjHrOQ/CN1/aASi3Qn/ 5+6Ab6fCb2L6TfNDZinbw/WI0sNSR0imajQruOaZRKXkXTDU/xzOr86lmxtN X-Google-Smtp-Source: AGHT+IHsuQtxvXmASDXywZrAG1WDaOMVVkRcxbdOFBNOdfRGUpVFzComBJihgNvo3S6lFag/z10kwQ== X-Received: by 2002:a17:906:e08d:b0:a44:731c:bace with SMTP id gh13-20020a170906e08d00b00a44731cbacemr7592179ejb.35.1709592729121; Mon, 04 Mar 2024 14:52:09 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:08 -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 v3 02/15] libbpf: tie struct_ops programs to kernel BTF ids, not to local ids Date: Tue, 5 Mar 2024 00:51:43 +0200 Message-ID: <20240304225156.24765-3-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Enforce the following existing limitation on struct_ops programs based on kernel BTF id instead of program-local BTF id: struct_ops BPF prog can be re-used between multiple .struct_ops & .struct_ops.link as long as it's the same struct_ops struct definition and the same function pointer field This allows reusing same BPF program for versioned struct_ops map definitions, e.g.: SEC("struct_ops/test") int BPF_PROG(foo) { ... } struct some_ops___v1 { int (*test)(void); }; struct some_ops___v2 { int (*test)(void); }; SEC(".struct_ops.link") struct some_ops___v1 a = { .test = foo } SEC(".struct_ops.link") struct some_ops___v2 b = { .test = foo } Signed-off-by: Eduard Zingerman Acked-by: Andrii Nakryiko --- 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 Mon Mar 4 22:51:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581332 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.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 80B797B3FA for ; Mon, 4 Mar 2024 22:52:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592734; cv=none; b=tJxZz/jLuoHuqDnmFMQ/Iq9wpThcV3ZC30gU7RCayDRHlFkVpCTydQFn2ca2oT7/hz3Deh7VEdgEk/pLLvzRCcLoMsdANthWA6coKunweeoEXz2SS6zSWICBBDAcucORvH6+1Xj3R3cfElb97Q3yhZJOmDuNqZxZpQsFrNopKSI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592734; c=relaxed/simple; bh=MxAP1Mj+92zhg+N4eBULQMbyQMO4cUeudjXhJFMAEm8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=F7Cpb+GJs7tPones4yDa4jLtZ3K1KyZZia2qDc241ERbdqWRm0KKI/PgACEqKakeObpJH7lqw1RX99jSt8QDBh/bO2gX+ST8fj9yceu6xEskhvri4pBabiaIlWgtb7dQd/tGWyy6wLlGmc5NCcFgZHsqSIVKyGp2CUxqCv3Jmz4= 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=g8Ad3FCk; arc=none smtp.client-ip=209.85.218.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="g8Ad3FCk" Received: by mail-ej1-f44.google.com with SMTP id a640c23a62f3a-a26ed1e05c7so852613066b.2 for ; Mon, 04 Mar 2024 14:52:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592730; x=1710197530; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ac39i9sTIpkqIzsBXHyyh1hd2HLA5BkCt5GBSAjk6Yo=; b=g8Ad3FCkR7kShM0zUuG3qavleX8wj3n9ynEK/aTqs2adj5csn1qRNAMJbLCCcvthAb PKZuQqyrU1DilEd0ApXZW9iFO+1KcawvoD6LOvni4+UkwbVCnI+6yRUM1GwU7YqIFkci fD2WzlyupC3z9KCf/Mjn+LB7Uwnf6LLzv3a0B5nqPwp+MA3RmkbrhHBxM1O1FPI3zmLd Qo8sCRvueH+pbPU6UZRnOJtqGakwLMsMhFsOvCSzLdFJRD3PA822GG1pz0lIHq9pj7eu wRfDOgPaJ91FYR3BL9/PVlZLKSQ5qDJVZaK9HcXPKtOSYzkPUJ7XzQPpxoIbYa76Gj+9 +YPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592730; x=1710197530; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ac39i9sTIpkqIzsBXHyyh1hd2HLA5BkCt5GBSAjk6Yo=; b=GY6Bf7mPblkE/UKSA/NQ4XaX+qI+6uJ7fWoxtFVlL0IJEYu2ucIyaK5Sg0kgUkghp2 z/o08m412onnAl07sDIlIO+2LeSPrlaIL9lSKtSZ1T70InpqdMLKvxkMu2CeHhyP5g+A oURDxL6Yot6QoAXq2XxsSDVqXIk9YFx7vD20Zzigs9F6L/AOo18yYB8N6KAEiBgigiCJ IK+2VOOSu36GXtRVHMGZEbBxnps3Kwgn3houPsyrx3YRRSNjUOaMMaqXd91Ny60XvC3N Fd0GxA0nefBQ/Pp5D4v2zD/40/mR4gU2RCq78E8xRwKtbeoWEp28+4EubBLFQzG6p46i 9PlA== X-Gm-Message-State: AOJu0Yxtt1p2zcURmtmcsYt14g3MU1G0/+vkBMKwLeZ6UouXMMLIpuBl ZrO7XgAgCFzGZ+vPz+KvuXEdX9KB/32x9rEbNwCQt7F2vuJ/czbfAAxAMpuc X-Google-Smtp-Source: AGHT+IHixalHglIlXXQ2Lavebv7pP2ccRXU+8qIRYmVQYEcPC21hXjTIIyscOmgnpHW1dEoXtcZn9g== X-Received: by 2002:a17:906:ff53:b0:a43:5bc6:17f3 with SMTP id zo19-20020a170906ff5300b00a435bc617f3mr7589876ejb.6.1709592730507; Mon, 04 Mar 2024 14:52:10 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:10 -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 v3 03/15] libbpf: honor autocreate flag for struct_ops maps Date: Tue, 5 Mar 2024 00:51:44 +0200 Message-ID: <20240304225156.24765-4-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Skip load steps for struct_ops maps not marked for automatic creation. This should allow to load bpf object in situations like below: SEC("struct_ops/foo") int BPF_PROG(foo) { ... } SEC("struct_ops/bar") int BPF_PROG(bar) { ... } struct test_ops___v1 { int (*foo)(void); }; struct test_ops___v2 { int (*foo)(void); int (*does_not_exist)(void); }; SEC(".struct_ops.link") struct test_ops___v1 map_for_old = { .test_1 = (void *)foo }; SEC(".struct_ops.link") struct test_ops___v2 map_for_new = { .test_1 = (void *)foo, .does_not_exist = (void *)bar }; Suppose program is loaded on old kernel that does not have definition for 'does_not_exist' struct_ops member. After this commit it would be possible to load such object file after the following tweaks: bpf_program__set_autoload(skel->progs.bar, false); bpf_map__set_autocreate(skel->maps.map_for_new, false); Acked-by: David Vernet Signed-off-by: Eduard Zingerman --- tools/lib/bpf/libbpf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 2c0cb72bc7a4..25c452c20d7d 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1209,7 +1209,7 @@ static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj) for (i = 0; i < obj->nr_maps; i++) { map = &obj->maps[i]; - if (!bpf_map__is_struct_ops(map)) + if (!bpf_map__is_struct_ops(map) || !map->autocreate) continue; err = bpf_map__init_kern_struct_ops(map); @@ -8136,7 +8136,7 @@ static int bpf_object_prepare_struct_ops(struct bpf_object *obj) int i; for (i = 0; i < obj->nr_maps; i++) - if (bpf_map__is_struct_ops(&obj->maps[i])) + if (bpf_map__is_struct_ops(&obj->maps[i]) && obj->maps[i].autocreate) bpf_map_prepare_vdata(&obj->maps[i]); return 0; From patchwork Mon Mar 4 22:51:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581333 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f52.google.com (mail-ej1-f52.google.com [209.85.218.52]) (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 C3C6E10A1A for ; Mon, 4 Mar 2024 22:52:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592735; cv=none; b=kzpSTGhNSoUF1waJWnDJy00cppyjr5PxTM7K75HylFYKLPUl/Y7BACSjrKiC/NmtSoV+qYShA84jHHBN0j+jvCU4EdZ1/wU5jVvC8FukgN1y7ANGKWFP/W9C/62l9ufFdiwV0dhyVXV3ed/1FLZ2HQITmRZUXJWC5go6TdPy69E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592735; c=relaxed/simple; bh=cEPnyfFWBE9D0OLMyGcbpMzkO4Je1leH8rHBZdxQL3M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GoPqhZ1pNC+T4MmBY0D8P5eSXqNpwR08dUd5XWtalMKlhaZQp6nYC/PTGOtCV0/jSZJ3tu1jqnyR9beA8F+LLS9HdAVfSVe5W911PYs6J9g8wuMB3lU8NX8KoiI3JA1IQMQvAVlyg2UdCBBS7F4A3qqhvFRUQsWQKWtdgjJSjTg= 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=dFgeVij/; arc=none smtp.client-ip=209.85.218.52 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="dFgeVij/" Received: by mail-ej1-f52.google.com with SMTP id a640c23a62f3a-a446b5a08f0so658431166b.1 for ; Mon, 04 Mar 2024 14:52:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592732; x=1710197532; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uC9tgxkqjgWUVRZpjwzr6XxoNQs3LWLYSDd6/trJreM=; b=dFgeVij/XwpPT5+brPH1oKmNn9m+SK0xEO/ZGnfzCUzEpE+a017f+4ASk726qWiPuP cF0z91z2PKz6BKJ6fhfp5f2wj+8v85hT12LkYh6MIHAS3PN0M05WvumaO0nAKXoGUiLI EA3pQjQ8DsTh7PdW2H2WxBvdTAuy6n8Nq6COcoMPhHsvErsF0ZZrGBCDk2ei+IdKo3Ep 2vtO2G7XNq/2ptr4FATV/Nm1eXmqMJCmMVbPjVpl4rxnul1c+hKRNlDXKZ3bp9nJ/6iB rDvoneGQFYf96ZUoeZCQBTrivjAFBy0pBW6yp6uDyKqvuzojZ4ES6fNmTsXgBCs0w+MX HXGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592732; x=1710197532; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uC9tgxkqjgWUVRZpjwzr6XxoNQs3LWLYSDd6/trJreM=; b=Q3IrQuCuaX2XvsEsS3gXGBM4h/ZKMk9vM6xP1/n8EvOR9L2EdUPgEeNmhme5wXpA3+ fhuVa7af84K2wLLTCnn7ate+i9C9Rbnvp17mOTpOKRk69Qa8/oa+af6KmfoYAk0lp3ka T7NMrObw64x07kXjhRmDB32lLLQMQSBj40Lw8tuuEyozBPhaQTq9K0ejmFmCYhjtti1F 5lTiMB6yYTFe9fjxHYHuZB6GYx5jO8W9/KfHFBZtTezno66Nri57s/Yrx06ZasBFwOQ6 lqgKI63Vz9Gua8EjCAZkhheovqU5HqJewp3pJKhvMSctjMdWh/LhPC9+WrXphxYxwoEK zAog== X-Gm-Message-State: AOJu0YwnpVaKe2c5rTO2EdWoTFolk3YAEfJDpgJ8EWFgqIMvICu/6HjK UvDoyM/g7jBGI/bcPfe853XW10/XBpR8fqUKgTQ8yHHwQiMfKNWTTaRIKxdj X-Google-Smtp-Source: AGHT+IH8i3yK8jyuprSr0IIRE6NfxyJGX6G2sxLA/kULkzoodRbr7kpB5qHW9XygrUr3udDpFoW0zg== X-Received: by 2002:a17:906:b85a:b0:a43:fd2c:663b with SMTP id ga26-20020a170906b85a00b00a43fd2c663bmr946939ejb.33.1709592731909; Mon, 04 Mar 2024 14:52:11 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:11 -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 v3 04/15] selftests/bpf: test struct_ops map definition with type suffix Date: Tue, 5 Mar 2024 00:51:45 +0200 Message-ID: <20240304225156.24765-5-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Extend struct_ops_module test case to check if it is possible to use '___' suffixes for struct_ops type specification. Acked-by: David Vernet Signed-off-by: Eduard Zingerman --- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 1 + .../bpf/prog_tests/test_struct_ops_module.c | 33 ++++++++++++++----- .../selftests/bpf/progs/struct_ops_module.c | 21 +++++++++++- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c index 098ddd067224..b9ff88e3d463 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -564,6 +564,7 @@ static int bpf_dummy_reg(void *kdata) { struct bpf_testmod_ops *ops = kdata; + ops->test_1(); /* Some test cases (ex. struct_ops_maybe_null) may not have test_2 * initialized, so we need to check for NULL. */ diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c index 7d6facf46ebb..ee5372c7f2c7 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c @@ -30,11 +30,29 @@ static void check_map_info(struct bpf_map_info *info) close(fd); } +static int attach_ops_and_check(struct struct_ops_module *skel, + struct bpf_map *map, + int expected_test_2_result) +{ + struct bpf_link *link; + + link = bpf_map__attach_struct_ops(map); + ASSERT_OK_PTR(link, "attach_test_mod_1"); + if (!link) + return -1; + + /* test_{1,2}() would be called from bpf_dummy_reg() in bpf_testmod.c */ + ASSERT_EQ(skel->bss->test_1_result, 0xdeadbeef, "test_1_result"); + ASSERT_EQ(skel->bss->test_2_result, expected_test_2_result, "test_2_result"); + + bpf_link__destroy(link); + return 0; +} + static void test_struct_ops_load(void) { struct struct_ops_module *skel; struct bpf_map_info info = {}; - struct bpf_link *link; int err; u32 len; @@ -59,20 +77,17 @@ static void test_struct_ops_load(void) if (!ASSERT_OK(err, "bpf_map_get_info_by_fd")) goto cleanup; - link = bpf_map__attach_struct_ops(skel->maps.testmod_1); - ASSERT_OK_PTR(link, "attach_test_mod_1"); - + check_map_info(&info); /* test_3() will be called from bpf_dummy_reg() in bpf_testmod.c * * In bpf_testmod.c it will pass 4 and 13 (the value of data) to * .test_2. So, the value of test_2_result should be 20 (4 + 13 + * 3). */ - ASSERT_EQ(skel->bss->test_2_result, 20, "check_shadow_variables"); - - bpf_link__destroy(link); - - check_map_info(&info); + if (!attach_ops_and_check(skel, skel->maps.testmod_1, 20)) + goto cleanup; + if (!attach_ops_and_check(skel, skel->maps.testmod_2, 12)) + goto cleanup; cleanup: struct_ops_module__destroy(skel); diff --git a/tools/testing/selftests/bpf/progs/struct_ops_module.c b/tools/testing/selftests/bpf/progs/struct_ops_module.c index 25952fa09348..026cabfa7f1f 100644 --- a/tools/testing/selftests/bpf/progs/struct_ops_module.c +++ b/tools/testing/selftests/bpf/progs/struct_ops_module.c @@ -7,12 +7,14 @@ char _license[] SEC("license") = "GPL"; +int test_1_result = 0; int test_2_result = 0; SEC("struct_ops/test_1") int BPF_PROG(test_1) { - return 0xdeadbeef; + test_1_result = 0xdeadbeef; + return 0; } SEC("struct_ops/test_2") @@ -35,3 +37,20 @@ struct bpf_testmod_ops testmod_1 = { .data = 0x1, }; +SEC("struct_ops/test_2") +void BPF_PROG(test_2_v2, int a, int b) +{ + test_2_result = a * b; +} + +struct bpf_testmod_ops___v2 { + int (*test_1)(void); + void (*test_2)(int a, int b); + int (*test_maybe_null)(int dummy, struct task_struct *task); +}; + +SEC(".struct_ops.link") +struct bpf_testmod_ops___v2 testmod_2 = { + .test_1 = (void *)test_1, + .test_2 = (void *)test_2_v2, +}; From patchwork Mon Mar 4 22:51:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581334 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.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 620797B3FA for ; Mon, 4 Mar 2024 22:52:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592737; cv=none; b=MieywzS0m7+OaBBti6eAeLjhPtVdft0/H4hoscg17MR3z7gBJj3oQ8yg5mzIcNZADrl7VyFqO2q/+Gktm27DUqBSGBfqn8o5AGDAGc84C6e7Y7Yln+RG/h3XvAsiOXDGhCmjQ6AZd6aQx8DD42Fjk/JG0poe5ge6lqBeOIN5HYM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592737; c=relaxed/simple; bh=zV/RB5EXo6qwUaRe+c4r7c329aduzNSbkkEXrlo/8L4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NCfvzeFnNV8KehD1kv8rBKQOShD3fiI7E9w0NMfWewTmP87yVis3CodJLCQ2HwloDzyk/ZwKWVmOMxVR9NdhavHG8jNYJfFtSPr+jKqkoR3+VvdFWY1CVAOY2TBNqHemhKQpbq0JbwL4PbMTsDG+WwV/gBFwbTnadVqjngVA3z0= 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=mB0b8fm6; arc=none smtp.client-ip=209.85.218.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="mB0b8fm6" Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-a4429c556efso687871366b.0 for ; Mon, 04 Mar 2024 14:52:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592733; x=1710197533; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ki6OFj/2RK5ad0QaqWrtytsE2QFidQdKKWOOEY5Z/2E=; b=mB0b8fm6miW8L+Ce5encvsLyBAcu6fJ29eEHyOaxBBgzuLg5xzsIn6noK0Z7CHoK+7 UFB0yWDn8BY60qwR8FqR2vdDwOpqZ0U7l3bTW8A2++1rmxZ2G91Rux7k8sC3qVgNjXPK PtHeNud1ITbYjth2aZOA4Uwee21boazdZnGQFij+RBZX9cmLDtywkPAh73XRoZkI2hol f0vsbgTgtJpEcToEnP1Kc0/2+mZCsewdLjtmawaUDPzl0Wmi8gj9DO7QRr/lDFRXVJdk 8OaRjxreToKfud3yoM36MoPEcdC6GAfg55UKvdYVyhR+MvMU8TQ6GSMLGVX0WiEdEM4F 2MDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592733; x=1710197533; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ki6OFj/2RK5ad0QaqWrtytsE2QFidQdKKWOOEY5Z/2E=; b=Dp/fleSsLnUHqELLz3AG1CUyeqSNeOOA3e34SEfcCgnFa5YqU33a0Nuc7gbzHr10Xl EKXmb33PQ3nbPG2faHdZ2TKl9vwmSM0MWkmX3TBpCyfieOSDps9OhM+FU7ivYCgCAkfy EC8rcqTkJxBN7/+m58RFNSk8E2FBMCbxJDML06+TFK25vCHa5Iv8gr7w/hJx+g9LbInD 1JgIh1ACvixJ1aRIWa5EkpF+egv5tEfqLx+nkFZRnUcXsDO7ZPxHv+6L4tEdbnc5zDMe du1ZxGUfXUuKkoMOywaho5W4w7i9HBys3krcRj9ylqLFmWpMEoqHQ6AlzhTvvEzgPexm LcmA== X-Gm-Message-State: AOJu0YxnsoysLCyXXIkZ/suhJzb6PMTLJonxxd+JkBbux+me472PfxMW 5z+/TzWJJk4Ztg5rdWEfFMKmnJtXYuM+1Zgc5ZjeeV89QiQJDSiC2A84cmXC X-Google-Smtp-Source: AGHT+IF6ZK0SDXC3AUhJHop1L8t35+/RhnrMwrzmNxq3QXHt2ZYUVI6UhogcSbIK7+7QNOonBTBrbg== X-Received: by 2002:a17:906:b80b:b0:a45:1c4b:54eb with SMTP id dv11-20020a170906b80b00b00a451c4b54ebmr3574310ejb.3.1709592733300; Mon, 04 Mar 2024 14:52:13 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:12 -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 v3 05/15] selftests/bpf: utility functions to capture libbpf log in test_progs Date: Tue, 5 Mar 2024 00:51:46 +0200 Message-ID: <20240304225156.24765-6-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Several test_progs tests already capture libbpf log in order to check for some expected output, e.g bpf_tcp_ca.c, kfunc_dynptr_param.c, log_buf.c and a few others. This commit provides a, hopefully, simple API to capture libbpf log w/o necessity to define new print callback in each test: /* Creates a global memstream capturing all output passed to * libbpf_print_fn. * Returns 0 on success, negative value on failure. * On failure the description is printed using PRINT_FAIL and * current test case is marked as fail. */ int start_libbpf_log_capture(void) /* Destroys global memstream created by start_libbpf_log_capture(). * Returns a pointer to captured data which has to be freed. * Returned buffer is null terminated. */ char *stop_libbpf_log_capture(void) The intended usage is as follows: if (start_libbpf_log_capture()) return; use_libbpf(); char *log = stop_libbpf_log_capture(); ASSERT_HAS_SUBSTR(log, "... expected ...", "expected some message"); free(log); As a safety measure, free(start_libbpf_log_capture()) is invoked in the epilogue of the test_progs.c:run_one_test(). Signed-off-by: Eduard Zingerman --- tools/testing/selftests/bpf/test_progs.c | 57 ++++++++++++++++++++++++ tools/testing/selftests/bpf/test_progs.h | 3 ++ 2 files changed, 60 insertions(+) diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c index 808550986f30..698c7387b310 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -683,9 +683,65 @@ static const struct argp_option opts[] = { {}, }; +static FILE *libbpf_capture_stream; + +static struct { + char *buf; + size_t buf_sz; +} libbpf_output_capture; + +/* Creates a global memstream capturing all output passed to libbpf_print_fn. + * Returns 0 on success, negative value on failure. + * On failure the description is printed using PRINT_FAIL and + * current test case is marked as fail. + */ +int start_libbpf_log_capture(void) +{ + if (libbpf_capture_stream) { + PRINT_FAIL("%s: libbpf_capture_stream != NULL\n", __func__); + return -EINVAL; + } + + libbpf_capture_stream = open_memstream(&libbpf_output_capture.buf, + &libbpf_output_capture.buf_sz); + if (!libbpf_capture_stream) { + PRINT_FAIL("%s: open_memstream failed errno=%d\n", __func__, errno); + return -EINVAL; + } + + return 0; +} + +/* Destroys global memstream created by start_libbpf_log_capture(). + * Returns a pointer to captured data which has to be freed. + * Returned buffer is null terminated. + */ +char *stop_libbpf_log_capture(void) +{ + char *buf; + + if (!libbpf_capture_stream) + return NULL; + + fputc(0, libbpf_capture_stream); + fclose(libbpf_capture_stream); + libbpf_capture_stream = NULL; + /* get 'buf' after fclose(), see open_memstream() documentation */ + buf = libbpf_output_capture.buf; + bzero(&libbpf_output_capture, sizeof(libbpf_output_capture)); + return buf; +} + static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) { + if (libbpf_capture_stream) { + va_list args2; + + va_copy(args2, args); + vfprintf(libbpf_capture_stream, format, args2); + } + if (env.verbosity < VERBOSE_VERY && level == LIBBPF_DEBUG) return 0; vfprintf(stdout, format, args); @@ -1081,6 +1137,7 @@ static void run_one_test(int test_num) cleanup_cgroup_environment(); stdio_restore(); + free(stop_libbpf_log_capture()); dump_test_log(test, state, false, false, NULL); } diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h index 80df51244886..0ba5a20b19ba 100644 --- a/tools/testing/selftests/bpf/test_progs.h +++ b/tools/testing/selftests/bpf/test_progs.h @@ -397,6 +397,9 @@ int test__join_cgroup(const char *path); system(cmd); \ }) +int start_libbpf_log_capture(void); +char *stop_libbpf_log_capture(void); + static inline __u64 ptr_to_u64(const void *ptr) { return (__u64) (unsigned long) ptr; From patchwork Mon Mar 4 22:51:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581335 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.47]) (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 D80A110A1A for ; Mon, 4 Mar 2024 22:52:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592738; cv=none; b=RcKgLaVyMlRQQ52FAQeYkqs+jRP2cS2KTpZZYze1vh1kAE2Wkkk0jKJwONXHHrM/E8ptPhTsulGUTCK/LM1CFVtJ8cZvLXANXTJKS1e6dLW7iXp/0sUZtNt/+STRVvpoyOS79vvq8Xb/AwDYnbpH30LIVPtCqK49bJHWJ/XnyS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592738; c=relaxed/simple; bh=T9Tg69UkPoHBYpyj0IQGWeVNmt0BhhIt5JaJNfhMswQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oxbQle2obNwHnLcpetI3XlqggoBhWwRMq0ZpDkAtMKISRE+F19hvvFRexWZ4GIKboaRWo66hVpXcIXJjJETn3G5UynLclCDC2ZutbMRlsqSR4LUf9XITEclBkWMQmkO7m9Wfc2fXGmz7Mh4giKy9sv7l587QWd1cP1YN+li7PHw= 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=R+XGRry+; arc=none smtp.client-ip=209.85.167.47 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="R+XGRry+" Received: by mail-lf1-f47.google.com with SMTP id 2adb3069b0e04-51341a5aafbso1931195e87.0 for ; Mon, 04 Mar 2024 14:52:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592735; x=1710197535; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HloUQLeyf3X91OgKXgvzmZERUb4RBb98Jjf4ea1gRCQ=; b=R+XGRry+rBp+aGbJaIpuLmhhI6oMpuQvnRh9pAG6YQkzdjQ61PZWi9aMDN6d3HPY+6 dO+iRMJcVGKWzCpCVUlBykhThktLLgy8GOyroZJd8LL4K2N5InY0HrlI38Z8NlpqUN0W q1b/zA6pxT2vMgqTozVoqYPLXjWJP98Vy9gL6HxwSlTHiz4mUj/QHqeopDiySY1P9nJs 4EF2axN/89nDni/flHBOKHkA6LCUzt+qBvNPfad+/+hqgMgcV4NMKe0Axc8jnyCLcRXv EQzf3belMb9sydVz27g06y/CzImP3j4XfaxbIoilWnQfxtgEwcJJRmFXswr5g9M+f0PJ F4KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592735; x=1710197535; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HloUQLeyf3X91OgKXgvzmZERUb4RBb98Jjf4ea1gRCQ=; b=DvZs78j52BhyHnEdg8MBZUWW0PriAjMriCLzEjRwRb3Siy4cGq3R5w1SMTsngo4WmZ WqGwqLjRaoEMTteCXe1o+HxyxD1bdRWfa7uUI1pf7bKef/Sj8U4526VShRNbraWalWwe /6IYBHKgHRl4KwsMXNViaRPQWGoDJZRhe0hvRUkGxEqjffGL3DJ+segf07UixqH1y+cT g/OL8oVcygaNdqtqlKsAa4LJZQTE3mhqtZti2JH1vpCJwpg9BOiN48d+Rl6q5tSw9Ctd zRUOv6TGYxvm9fc1Ph7ScX+6m7crFePVCyCH+9hMVRcro7HvDe3pZEyZ6mIm98W71wW9 sSPw== X-Gm-Message-State: AOJu0YzVA35PtWbRXVFDJVXgbkNF3vjOFJbghB87qfBzJDphWG9dpsJY 5EO5VaGEo2whMudEKGcY9znjZTAb3s4+fsjCHaZBqzOBWePT4KWYlPsyElnY X-Google-Smtp-Source: AGHT+IEp0r3rFFPVCj0FcU8IN4rhqHyBnIK4nsNn5D8WCLS5RExw7Enc8YF7YpZD2or3aBGAnHDJ8g== X-Received: by 2002:ac2:43b7:0:b0:513:4847:2c0d with SMTP id t23-20020ac243b7000000b0051348472c0dmr99913lfl.29.1709592734591; Mon, 04 Mar 2024 14:52:14 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:14 -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 v3 06/15] selftests/bpf: bad_struct_ops test Date: Tue, 5 Mar 2024 00:51:47 +0200 Message-ID: <20240304225156.24765-7-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net When loading struct_ops programs kernel requires BTF id of the struct_ops type and member index for attachment point inside that type. This makes it not possible to have same BPF program used in struct_ops maps that have different struct_ops type. Check if libbpf rejects such BPF objects files. Signed-off-by: Eduard Zingerman Acked-by: Andrii Nakryiko --- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 24 +++++++++++++ .../selftests/bpf/bpf_testmod/bpf_testmod.h | 4 +++ .../selftests/bpf/prog_tests/bad_struct_ops.c | 35 +++++++++++++++++++ .../selftests/bpf/progs/bad_struct_ops.c | 25 +++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bad_struct_ops.c create mode 100644 tools/testing/selftests/bpf/progs/bad_struct_ops.c diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c index b9ff88e3d463..2de7e80dbb4b 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -610,6 +610,29 @@ struct bpf_struct_ops bpf_bpf_testmod_ops = { .owner = THIS_MODULE, }; +static int bpf_dummy_reg2(void *kdata) +{ + struct bpf_testmod_ops2 *ops = kdata; + + ops->test_1(); + return 0; +} + +static struct bpf_testmod_ops2 __bpf_testmod_ops2 = { + .test_1 = bpf_testmod_test_1, +}; + +struct bpf_struct_ops bpf_testmod_ops2 = { + .verifier_ops = &bpf_testmod_verifier_ops, + .init = bpf_testmod_ops_init, + .init_member = bpf_testmod_ops_init_member, + .reg = bpf_dummy_reg2, + .unreg = bpf_dummy_unreg, + .cfi_stubs = &__bpf_testmod_ops2, + .name = "bpf_testmod_ops2", + .owner = THIS_MODULE, +}; + extern int bpf_fentry_test1(int a); static int bpf_testmod_init(void) @@ -621,6 +644,7 @@ static int bpf_testmod_init(void) ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_testmod_kfunc_set); ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_testmod_kfunc_set); ret = ret ?: register_bpf_struct_ops(&bpf_bpf_testmod_ops, bpf_testmod_ops); + ret = ret ?: register_bpf_struct_ops(&bpf_testmod_ops2, bpf_testmod_ops2); if (ret < 0) return ret; if (bpf_fentry_test1(0) < 0) diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h index 971458acfac3..c51c4eae9ed5 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h @@ -45,4 +45,8 @@ struct bpf_testmod_ops { int data; }; +struct bpf_testmod_ops2 { + int (*test_1)(void); +}; + #endif /* _BPF_TESTMOD_H */ diff --git a/tools/testing/selftests/bpf/prog_tests/bad_struct_ops.c b/tools/testing/selftests/bpf/prog_tests/bad_struct_ops.c new file mode 100644 index 000000000000..9f5dbefa0dd9 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/bad_struct_ops.c @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include "bad_struct_ops.skel.h" + +static void invalid_prog_reuse(void) +{ + struct bad_struct_ops *skel; + char *log = NULL; + int err; + + skel = bad_struct_ops__open(); + if (!ASSERT_OK_PTR(skel, "bad_struct_ops__open")) + return; + + if (start_libbpf_log_capture()) + goto cleanup; + + err = bad_struct_ops__load(skel); + log = stop_libbpf_log_capture(); + ASSERT_ERR(err, "bad_struct_ops__load should fail"); + ASSERT_HAS_SUBSTR(log, + "struct_ops init_kern testmod_2 func ptr test_1: invalid reuse of prog test_1", + "expected init_kern message"); + +cleanup: + free(log); + bad_struct_ops__destroy(skel); +} + +void test_bad_struct_ops(void) +{ + if (test__start_subtest("invalid_prog_reuse")) + invalid_prog_reuse(); +} diff --git a/tools/testing/selftests/bpf/progs/bad_struct_ops.c b/tools/testing/selftests/bpf/progs/bad_struct_ops.c new file mode 100644 index 000000000000..b7e175cd0af0 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bad_struct_ops.c @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include "../bpf_testmod/bpf_testmod.h" + +char _license[] SEC("license") = "GPL"; + +SEC("struct_ops/test_1") +int BPF_PROG(test_1) { return 0; } + +SEC("struct_ops/test_2") +int BPF_PROG(test_2) { return 0; } + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_1 = { + .test_1 = (void *)test_1, + .test_2 = (void *)test_2 +}; + +SEC(".struct_ops.link") +struct bpf_testmod_ops2 testmod_2 = { + .test_1 = (void *)test_1 +}; From patchwork Mon Mar 4 22:51:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581336 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (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 A32FC7C08C for ; Mon, 4 Mar 2024 22:52:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592739; cv=none; b=KORjbIa0t4I9psKgvt8TskscpJx1HGZXhdQPxcSO+pe3RNlbvjwZwnr/fqSt2xcvnEMGxs4PbP6T57ayFjv4w8E6yOw+UGFeS8UNPYyXtZxwjMljOhecUYlVINLdq/b3aslBJ7/sI6MnRnPBT4nm4L0rcGdDsYKpeM2TxYaWUnw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592739; c=relaxed/simple; bh=QXvEP/c5CZ+qMkyjvMCGVrtnH7EiT8yRzTlaKOqT5e4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QT8Ki04HXNL3W+KgmNmayDYQOyoaLaWA6NEkyfJ9r8/krXGcXfbE2/zOk4QA1iHrPZBukycok7OgBVZ4ih3cOMNEYtPq6lwrzZ4AY6hrcodn85AKiPTb7p424u4+Q9S4HXf5Szw+MHNDqrHN7+1fsLwxNMpiEDroh43Xxh+Vask= 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=IEAs11aY; arc=none smtp.client-ip=209.85.218.47 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="IEAs11aY" Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a293f2280c7so877306266b.1 for ; Mon, 04 Mar 2024 14:52:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592736; x=1710197536; 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=8J+ytjUf9SzelgOKTwnMQGif16G6a4gx4Sx+0P9UUqs=; b=IEAs11aYRu0fd9WqtYaYkc0t7Z0Xqh6Q/aZlnV7igqvx0VbWdH5bI8FuhOYiQcI/ib W78eb+jK7fvdXmKVY1YX1cdbvp9qdQ+fCQ9IWPcQXtrTh5HBHEOZZO/z/NScJkjItg8j qv2YtMRpgdQ2qA5MDXRofTWm5EmcF/yy0PchpB8xK1NxopaxE4svUS8hb6rJ4xlKD7Ya x0Qa8sxvbVNbWXHeK0B/rf9046kXv/X1cxpkzvAlTwDbNvHI8TWKZue8PlwGffV3zVrf WOukMAZJTK6I2XUegEJVrK9DRM7ntjEKdFjOa5CfI3/X0u43AAA6Quz+muXOw6Ax8BSu P6Iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592736; x=1710197536; 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=8J+ytjUf9SzelgOKTwnMQGif16G6a4gx4Sx+0P9UUqs=; b=q448Y1ydZFnbRBLXEsWrimsXWU5TmtPKeJGe+my76PIPcAznAEwRkBgHFBQMY312ir P5kR7ZAlcVoeUovoxpYdOV0DtXWrOHwItdK1MsY8LVMLkn1gE7VThD0nMegtbNWZPvJi dAyBXeXZuK6pSoKHYje+him0fYW7ovRoB/Rg4jzFvImTV6QEtaPQVScXe8rtNktgEWRu PjPlZgVR+HjnCFBKM/AeZw+MgqBbWa/sjWQL6BpDFYS11Lu6R9kPsvpg+CBgJYPgdh1L 73L0owsSHBDVhJanSxAX/eRwq1Goese5sNtUQW0yzAL1pV9jPqJm2JvrS/rSGYFaVrFW 2WvA== X-Gm-Message-State: AOJu0YzNOyEF7zESax4xn1fw3ou8qiNB+kALb5ZyqXne2hT+nWgGJ5Fk Y9m/2F5+4kSTKYN2GHcWv2vzBnSWOZOCf5lnRQAofea5/Wc7sEOKU0cHco4S X-Google-Smtp-Source: AGHT+IGgohPam7TXIaIr+S33KaDBcz4qKamA0IS4LVT8US+z5qkC1vzTeOnIQxJhcADCBctwSvI5Kw== X-Received: by 2002:a17:906:7809:b0:a3f:6513:1489 with SMTP id u9-20020a170906780900b00a3f65131489mr6551981ejm.55.1709592735964; Mon, 04 Mar 2024 14:52:15 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:15 -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 v3 07/15] selftests/bpf: test autocreate behavior for struct_ops maps Date: Tue, 5 Mar 2024 00:51:48 +0200 Message-ID: <20240304225156.24765-8-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-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..883f938d518c --- /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; + 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 Mon Mar 4 22:51:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581337 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 C1D827C6C1 for ; Mon, 4 Mar 2024 22:52:19 +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=1709592741; cv=none; b=WJTRSXefJKF28bGXmWtOurJWXSn3FxfTMOApI8958a7Z+HET+ee5+szDNQYcE4I1HJhpzxWkRXxdi851pyziFUZOr0U4y9vXvT9qlkRWCQNKsLaY+kgU2j3125KIHawpez2qubbMQ+odK2ulULsoVKygCLoFh8iYkehYgYKJ34o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592741; c=relaxed/simple; bh=SGugltStRBDh4geB+tZ1Vw+QYKK4QDoGnF8wuYwyOEA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qQOu6i5AAWtRl9am53CuOBp4Hfhpuro8MftMHM08MY6FSgAduEexskJI6hozr7SoXLpVHuCqOxQWHYioyz0eQKSjjAKmG0pmDSfC7Y1gEy3w1EBVAxzNP49nJ5fwoc/htb3HVHG5UYV2hmlBoPinPHszTbZOJDReU8hC3mJ7i4Q= 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=JNFdK9A5; 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="JNFdK9A5" Received: by mail-lf1-f45.google.com with SMTP id 2adb3069b0e04-51321e71673so5756226e87.2 for ; Mon, 04 Mar 2024 14:52:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592737; x=1710197537; 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=IkFfnJ1OxT0gF8JvrEemyPjOclRJZyg7/H5lWqthZZo=; b=JNFdK9A5UpS4ErOqtGA3f8kuAPZ5lG6rEOHWvcn02gH0nJKWefCwaXjELj/VAnO0VK paxUb/F4dFrE8hu2TwV5GrXI6Vcwdy1+rfSAg66RH4JdUabDqwohRlS39LTcTIP5VeQ2 /2FzlpMyLr9TWyj0BJYjnHsEaSghGvW1XllzenqZehOUt28eWPJurVHtqkY5MoGj8sNi 4emVWUimO9gVtB8Ie9LmYjrxCOlzI0cFyVj5HtJoJgjhWryOi9kgnuHt3PJ6PwAikgVw H4UNK36jBWmCuNDLQrXPAFQQfPWz6ay42f28NYyE8KAjYeffo4P7n2PxeGm/2QQvfUhE FuwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592737; x=1710197537; 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=IkFfnJ1OxT0gF8JvrEemyPjOclRJZyg7/H5lWqthZZo=; b=PUqlC9Gi4L0iI/MxAbO06hOYHz4e88gK08JW4M+i/bYOfnvXvJKDLD/6Fmi3KF9r9e sC301zBs0ly9caVJvsCnhLVaWQqkdWHTFhwiaGRV/VRCGjlGeNpPk17myAd+A7eNJzeV ZoFcobG9N8BBM4FLZLxxXu2yhH3uDlDDzLoz7xnMa8aXh3Z5VDZ74SuCps3fHRWZDyIr Sl8/Ur35PhdWGbgef2HuGJPtLAEX6zayOWxrIQFAWwB0to4tDqVHQPKjm7G8ydrFv/ZN 1GMuap6lCq96VL0lLB4wS2nlm7S2zxJYYsptaaXwmoAGP5b6yltLXFfGz1Y+YzrCm4Jn svgg== X-Gm-Message-State: AOJu0YwZHpWEU5S90MeXlOvf404TvwBfAY5IQTKvYG9Peo0tDo2j8EWu qzEw0WEUqF4gftIF7LGtzynPAX5fFbUtvqXXneSYP05dMwaG7ZOtgy1Z2fAQ X-Google-Smtp-Source: AGHT+IE/EjKOdT/bvWfuKjL59ESKOY6NCGpI4hzrHYs+p+pO0+/T1ivRjHSE6bgrdsXp5GBWFYhiUg== X-Received: by 2002:a05:6512:2037:b0:513:4fc7:2dbd with SMTP id s23-20020a056512203700b005134fc72dbdmr92420lfs.60.1709592737402; Mon, 04 Mar 2024 14:52:17 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:16 -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 v3 08/15] libbpf: sync progs autoload with maps autocreate for struct_ops maps Date: Tue, 5 Mar 2024 00:51:49 +0200 Message-ID: <20240304225156.24765-9-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-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 | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 25c452c20d7d..ce39ae34fec0 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1031,6 +1031,53 @@ 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; + struct bpf_map *map; + int i, j, k, vlen; + struct { + __u8 any_map_autocreate:1; + __u8 used_in_struct_ops_map:1; + } *refs; + + refs = calloc(obj->nr_programs, sizeof(refs[0])); + if (!refs) + return -ENOMEM; + + for (i = 0; i < obj->nr_maps; i++) { + map = &obj->maps[i]; + if (!bpf_map__is_struct_ops(map)) + continue; + + vlen = btf_vlen(map->st_ops->type); + for (j = 0; j < vlen; ++j) { + prog = map->st_ops->progs[j]; + if (!prog) + continue; + + k = prog - obj->programs; + if (k < 0 || k > obj->nr_programs) + continue; + + refs[k].used_in_struct_ops_map = true; + refs[k].any_map_autocreate |= map->autocreate; + } + } + + for (i = 0; i < obj->nr_programs; ++i) + if (refs[i].used_in_struct_ops_map) + obj->programs[i].autoload = refs[i].any_map_autocreate; + + free(refs); + return 0; +} + /* Init the map's fields that depend on kern_btf */ static int bpf_map__init_kern_struct_ops(struct bpf_map *map) { @@ -8163,6 +8210,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 Mon Mar 4 22:51:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581338 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) (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 B7D7D7C085 for ; Mon, 4 Mar 2024 22:52:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592742; cv=none; b=BEo8KID8jS5jq8Vvrs2OEZlDOC8HgVWsSg6RhWICV7i8bcx0Q/kIeN1qVge74AC4KX5mOrtNIDiVlzBvlhjWeP8RAwQAOvel1r2MLF8MM68IC6sO4MU7/QtvUIqMEEB9wIIS9mxYhbW5Cj98KSUDRlNCYlj2NxtQSpuebEeI5Pc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592742; c=relaxed/simple; bh=rCBAnMa2X7fiuBRmf946N8+2iHXidemXOv7C6Bc3PyI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GvkZ6O6q1Pxk+7tbUIeA3YDvCfzm9YV1Ygx4ouxJeBhgOt8vq/fY8SRXrzRnPYUUBB0hXu0yYWDq/m7excpjwwpM3XaT4wLpwgj2t4RVXEzr4wZ9Jx34VcRv1p65m5o2HwaDRbPkEBNIQYhpMNE0OoOsqPXNfp/s22JxAsuub+U= 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=CITUmOZU; arc=none smtp.client-ip=209.85.218.48 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="CITUmOZU" Received: by mail-ej1-f48.google.com with SMTP id a640c23a62f3a-a458b6d9cfeso93144166b.2 for ; Mon, 04 Mar 2024 14:52:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592739; x=1710197539; 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=Hfl/Y1CGjeULVzqRfxvVSVqXbrUox/b7pBNOx/gqXPU=; b=CITUmOZUmJ3YdXPxt4CcTuNn6JWiY7ktF8ZseOrbJ/lFcCB5VVi1/XHMVfC3tkFp4A 3GxHDIXv76IG6scrqvFuYOEu4/d3O/RwlmaGHl/+LioD5y7z/Cvl3fQSZaY0DPo7YTqc g/U5L/m3uk2XEcq8fLSV+VZ1j7aWw9MzzXzJK21Zx3lpzOKBLulFqXkD31DEY+ZGZ4iI OsSfb8xbnGnFaXy3cPUF0nUTF1gPh2/+U9FJTGA69WLX4xnTSyd/TtLVNzQ7MdJnzMSg 9kKXFybGzh/HHhYpRsNMT2WEY5j2sGs3qF4CMd/cKnyT/RCS7zaD0dIgty5vT5n07Vmv 0x6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592739; x=1710197539; 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=Hfl/Y1CGjeULVzqRfxvVSVqXbrUox/b7pBNOx/gqXPU=; b=VPJnw4SN4xKUscg9C6op2479OqGlclx6/dJXIQroz8UCRU3S/8TcWvMAVh0zPsovfW 9MkQa96f/oWhKZR0h7FJ12nZndmnVEFcUgqLcSkm82TTvJMkpH8RBOSv1kdYYqBGiNxV ngnZuczW+81mn8p83ULGVjYFs/kbtkWksvp1BXHoGNxFrB2hwVp5BNsTmf2Vp13fFOoE PdjShraVTnXOFO00HHXJrpUBN80ayrt+N6diLV18M+Sg7F99TwnbMhJEf+IMCrFYJIRN uVRzqzV74LXeSXylBktWVr9hLvtq19US1fvQ9xQZdbQz1KGp1oZgPkf/orFUWydm4KtC AJbQ== X-Gm-Message-State: AOJu0YxR87iBs/YLhR1gw6sdNPk2s305TnU05HW9lvS3jK5ruBQW7k2M JIU9wUYFzzarngiOktBvvHqmOCubl5Jojauugoo0Gjy1L2gQ0UuG/5DTtF3C X-Google-Smtp-Source: AGHT+IE6d01Sjb8vEFyWlJzGgOQwvh6px7e+us0tS7nq5V/CJkWNxiUzBeDl75DdbC1PRPBAFKKjIQ== X-Received: by 2002:a17:906:3941:b0:a3f:ce8:1234 with SMTP id g1-20020a170906394100b00a3f0ce81234mr7166574eje.68.1709592738875; Mon, 04 Mar 2024 14:52:18 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:18 -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 v3 09/15] selftests/bpf: verify struct_ops autoload/autocreate sync Date: Tue, 5 Mar 2024 00:51:50 +0200 Message-ID: <20240304225156.24765-10-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-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 | 43 +++++++++++++++++-- .../selftests/bpf/progs/bad_struct_ops2.c | 14 ++++++ .../bpf/progs/struct_ops_autocreate2.c | 32 ++++++++++++++ 4 files changed, 117 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 883f938d518c..35910cbb9ca4 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,10 +44,6 @@ 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; @@ -67,10 +64,48 @@ 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; + + + 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 Mon Mar 4 22:51:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581339 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 2DBB67C6CC for ; Mon, 4 Mar 2024 22:52:22 +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=1709592743; cv=none; b=fMpOcfRR/eFTmf91yrplygqb43eWu9aT9PdN4d/cldS/XFZHiqg10bBWAMNpQ+t17qlwJ+fhkVwD+uUQEjO9mWYmk1BOIRguSQGPbmjljwF1/qdz2vc/61uFobAFLxHfUAHEwX3mbVISfgxzaVhJlVs59qIEB8KsMhr7wRcfHlQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592743; c=relaxed/simple; bh=rgeR5HXu6PVieluBU162i6nCCXzBbmEaYvdlsL5tCIE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RmyeMyBAib2mnDyQHdqknANhUc1vWUzQdSWdW23lkVQYlIjGRwMUskNszWyBIO5woEV7tM+3n0l4DsLk6AepXMdn6oHTJ0HBuVR8JqHz3TfEeX3QyB4xbW0AlXXvVJxCsOtVHEziK73L7w1iQ3NqWhmRYVlpRiPn240YV5TZ0es= 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=N9m5jBkQ; 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="N9m5jBkQ" Received: by mail-ed1-f42.google.com with SMTP id 4fb4d7f45d1cf-5673b5a356eso32930a12.0 for ; Mon, 04 Mar 2024 14:52:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592740; x=1710197540; 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=WD6I0hZklvWavGPzFQiptXUz9ryuUA4HQRXZZhbveEQ=; b=N9m5jBkQT7oPz4TgM4oJKOKa61kWPH7W9hvZjdKo78n6/fNpdeCFI0kgLCJ3MGHK65 cPtdo7EPPBYiS5pYW3Cp6xjI0BEHohRbNiTqlujLmGsT2sUH32SFNLsVMwXEx2Wn4phE XrZ78kpNaEBgIApIMFn86kdyiI0WsWbfA+VzGgcJcsjTLYe9S2iL366+TlEuqexWSu7S ncMSylHBx1xJqRu0OEFKCPUcATMrrP+0R3wMehxL4nlQrUgm6xX08YDzJZ9NW8aRbwri xuXrH+ZV0KKyoU2k3/5+WnpnvALKeppDeR0G+O1qc5lNYb1I3wXtTwAG6N0ZwgwTKemB nlsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592740; x=1710197540; 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=WD6I0hZklvWavGPzFQiptXUz9ryuUA4HQRXZZhbveEQ=; b=XgbBYDXMFCT1M95AKHFnLoW7wU1Z9IPGt4p+zagYCQeilgUGtwb7iDGOwCt5wTzxhs DDCpjPSKi1rSHsJxOUtIe1B1nmqJcPbOfrY0QYCtrGiPK3tT60rdvfZ5XPBUPanA2yF0 AyVdeMh8fWqZ1tAJcpmq6BG5UXo9OSCNoEp2qop2rwVyIm50Z9274kRzwmdQhiFYQy6s ezX/tYkVsA5yyKRoerRCg+FOH4fPLKwU73kSX+P0QXt9iDlZ0wKa9r4BgeUUsQ/qLtB0 d5Q45AdOuP0j5Dnig1//RxWkC1uSVmYATU5EgWBr0yOC6nJ6lBJuJjont0iBAnlwx+6J OK4w== X-Gm-Message-State: AOJu0YyB/iuBJSIxR5xkhtuET6f0OGc8IMtfzz5BeIjc1YjCCkAhV4Rh zfkUpmpyjPUafdTr4ccC9wI4YfSuSOgcJ2aMj3S0cyCGyrBBQXQUlYJrYQvZ X-Google-Smtp-Source: AGHT+IGb+FrI3+cKGVuARdasJN2MBQYW7adUbqqhUKpRCyhvVwmg8A2f+ZzzeKRuIO/DCEywhavEPA== X-Received: by 2002:a17:906:4108:b0:a45:4416:1fa4 with SMTP id j8-20020a170906410800b00a4544161fa4mr2784010ejk.40.1709592740408; Mon, 04 Mar 2024 14:52:20 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:19 -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 v3 10/15] libbpf: replace elf_state->st_ops_* fields with SEC_ST_OPS sec_type Date: Tue, 5 Mar 2024 00:51:51 +0200 Message-ID: <20240304225156.24765-11-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net The next patch would add two new section names for struct_ops maps. To make working with multiple struct_ops sections more convenient: - remove fields like elf_state->st_ops_{shndx,link_shndx}; - mark section descriptions hosting struct_ops as elf_sec_desc->sec_type == SEC_ST_OPS; After these changes struct_ops sections could be processed uniformly by iterating bpf_object->efile.secs entries. Signed-off-by: Eduard Zingerman Acked-by: Andrii Nakryiko --- tools/lib/bpf/libbpf.c | 62 ++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index ce39ae34fec0..4ef3902e65db 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; @@ -1268,7 +1266,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; @@ -1330,7 +1328,8 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, map->def.key_size = sizeof(int); map->def.value_size = type->size; map->def.max_entries = 1; - map->def.map_flags = map_flags; + map->def.map_flags = strcmp(sec_name, STRUCT_OPS_LINK_SEC) == 0 + ? BPF_F_LINK : 0; map->st_ops = calloc(1, sizeof(*map->st_ops)); if (!map->st_ops) @@ -1365,15 +1364,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, @@ -1411,8 +1420,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(); @@ -1429,8 +1436,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; @@ -2975,14 +2980,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, @@ -3683,12 +3687,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); @@ -7001,12 +7005,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 Mon Mar 4 22:51:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581340 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (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 8C9193C28 for ; Mon, 4 Mar 2024 22:52:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592745; cv=none; b=r7WeP8DioWmf2eC8Tx0Q+IQ9SBScHG2Vu/t0qu8Bo6MNSkcGoNntZvlfVo7Dfj9t0rUAz2H4hWP1751ZUHiTgbo5etcCRjSB/q74B40DHR3w5Ul7bpfIXHG2w2SaGgWth/QYdnw36sX6zGZFJQ7d8Nv229xlE14N2At4hO/B3wU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592745; c=relaxed/simple; bh=mRYN8GtA6AeeXnAhta3PiFUwUUHqt9j+YgCCrPA4m70=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NOWn+DlhUbshvlu2/Vab7jai6GL1zpNTyPRiWzUrio9ide/jq9Zuym8tv83EbNtI31YnWEzpqQje/zsgQvp8YcoXsD5Xb2bs+EVaUeIvMYrVF1MChNkmiq6u2p+wWpdClPPyxOjSRm6XdkglU0p1+C4IDFnLZjmAh/x2noDmLaY= 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=gCXopT6c; arc=none smtp.client-ip=209.85.218.47 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="gCXopT6c" Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a452877ddcaso217444366b.3 for ; Mon, 04 Mar 2024 14:52:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592742; x=1710197542; 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=STdOP0ZXMuxwmEzVVyhK+2jyJZme+i5PFKTMGolq9lE=; b=gCXopT6cdMVBtxx4c2qFmiH7lXvGOPtXx065lAepqYC3HE2Ctgid3SuCos/ftYOYdb lbV4XWYIMSHFs1f6ZWdcCzjBmgCEvW++oP87LN64muhbHe6+Sf3kiz7B0neXSqW/bi0q wvG2a/iviJy4PrG1if4xuLOm6/URBxghawU8vNqFS9nRQyDTqe1qa2Lz84MxD54ezw1f M+kUQd4VnUGJnm62VxBt2cc5xi5EGFRQf7B6R6KAY6hrhT9TXMR5GLTAsZzQSo5ESvWZ RhfXC2ycRQc+9gUAdEKgl0QImBG0bvZSJQb3sJqb/QJGrOOIfn4fHdcrtzhd2pDaE3QN xOQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592742; x=1710197542; 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=STdOP0ZXMuxwmEzVVyhK+2jyJZme+i5PFKTMGolq9lE=; b=GnGJ9BIDVPvf6b4y94FCEJ6QDhniu82ocNRZPdtghskvmoL4pzZ9hv19P8iGd5Zjwk bFOhzdGtWVZ2ubDAsCI+1V/OLrzA1w+dVc8g+aWJSOe3YfcmwVGivFWFNv8F7KghsMko OjC+XtNr2FOotgPmGUnUXrnm13UAPrS7YM6NHzkmRhG/UPH4vNS7Z6l5Pq8B3U5Q3rmG Y9QemLOTKxltBr5ZKMhSJrTLb7ssBPVSCh1lmEWJYM6n2ddLc+U4F7MyD7HCEid6LNRo UEe8kqGNGFzdpp938IFFU9B6cUt0RVq27ATVi6FPpLVXYEnEA/z2X/6F29MIdxrmg+RY 1qqQ== X-Gm-Message-State: AOJu0YwdHcT6P7kPCqA3mrio/pqP/rXjBOZI3RfV7uYBqi7VG3VBpOmE yDAWix/IJNBDNWzh5pN7Ae7DDOnmrKu4NyD7Jqz3MBRq4oznjall12ncOLjm X-Google-Smtp-Source: AGHT+IFR8DWddPzI2l5n/KP3S6eobXIampEjq1SkwvT4O2uz/4X0jrlho2bGgB/BGveskieouWwdsw== X-Received: by 2002:a17:906:a450:b0:a45:a797:75ab with SMTP id cb16-20020a170906a45000b00a45a79775abmr60391ejb.3.1709592741834; Mon, 04 Mar 2024 14:52:21 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:21 -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 v3 11/15] libbpf: struct_ops in SEC("?.struct_ops") and SEC("?.struct_ops.link") Date: Tue, 5 Mar 2024 00:51:52 +0200 Message-ID: <20240304225156.24765-12-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Allow using two new section names for struct_ops maps: - SEC("?.struct_ops") - SEC("?.struct_ops.link") To specify maps that have bpf_map->autocreate == false after open. Signed-off-by: Eduard Zingerman --- tools/lib/bpf/libbpf.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 4ef3902e65db..c0212244bdf7 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -497,6 +497,8 @@ struct bpf_struct_ops { #define KSYMS_SEC ".ksyms" #define STRUCT_OPS_SEC ".struct_ops" #define STRUCT_OPS_LINK_SEC ".struct_ops.link" +#define OPT_STRUCT_OPS_SEC "?.struct_ops" +#define OPT_STRUCT_OPS_LINK_SEC "?.struct_ops.link" enum libbpf_map_type { LIBBPF_MAP_UNSPEC, @@ -1324,6 +1326,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; @@ -3688,7 +3699,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj) sec_desc->shdr = sh; sec_desc->data = data; } else if (strcmp(name, STRUCT_OPS_SEC) == 0 || - strcmp(name, STRUCT_OPS_LINK_SEC) == 0) { + strcmp(name, STRUCT_OPS_LINK_SEC) == 0 || + strcmp(name, OPT_STRUCT_OPS_SEC) == 0 || + strcmp(name, OPT_STRUCT_OPS_LINK_SEC) == 0) { sec_desc->sec_type = SEC_ST_OPS; sec_desc->shdr = sh; sec_desc->data = data; @@ -3708,6 +3721,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj) if (!section_have_execinstr(obj, targ_sec_idx) && strcmp(name, ".rel" STRUCT_OPS_SEC) && strcmp(name, ".rel" STRUCT_OPS_LINK_SEC) && + strcmp(name, ".rel" OPT_STRUCT_OPS_SEC) && + strcmp(name, ".rel" OPT_STRUCT_OPS_LINK_SEC) && strcmp(name, ".rel" MAPS_ELF_SEC)) { pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n", idx, name, targ_sec_idx, From patchwork Mon Mar 4 22:51:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581341 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) (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 5B05E7CF0B for ; Mon, 4 Mar 2024 22:52:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592747; cv=none; b=B/bEeZjxXmk++rfIUWGgbscEfqiEflko4SpPNB4xEYoXYzoFF90a053rerHCg8CXha8iAihKEOMKbBX/Qg3M2vheWfGWMPFji/XjzBGtRj01zIOHdFaE6daKLXULDhS1GEQYYTbsYdN90AjnJLy8dIlXbYMcKAjPbth6RsXBM6w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592747; c=relaxed/simple; bh=UOyotS1BYRsSd5KZlhgQA45wkirr1R6UKLzxX09/m5U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jRSb1pYTyPU/j2413XgXHEpcDvtChQRMcWjLUzZbJ4l827hN8JPmJoL7dUM3X7+XiyArJZrta/C8iZay5zeWqPGyUtL95VP0oYo+I/RagK880vP8t5u1fpSWnH3GitLFcqb/dtypbb1IdWabgD4FYTyaxPVBpPvgF4n7CJpWG7o= 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=MjDox2rE; arc=none smtp.client-ip=209.85.167.48 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="MjDox2rE" Received: by mail-lf1-f48.google.com with SMTP id 2adb3069b0e04-51344bebe2fso1624524e87.2 for ; Mon, 04 Mar 2024 14:52:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592743; x=1710197543; 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=3J5Oq4avgTnUiue5ull73zk/v5Sx9caqkhc4270Z+44=; b=MjDox2rEXD49CdaLePv7bDNGkltAZx7d72cks46blbGmeQgfEgaUEGZ2BEZuuwWb3Y mtal0Vr60tkvqOXeQANmkc34WzC0x3/zteGY2Jja/DSFLQF+SfQ7BtslWiPXEnkEMzqO 20KJEEiGGEA7+dxGzq4XIRhlgPpkkPNnQL8GBg3hVF1txmpENw2iCcSte4AxjYXahvlq gRkMSys9A5ZDw/fSCOCcQqTX7SkIggbI/rEiH0IkN9V2sO+DucWFY4bRPWcjXudPVm+e fX/hXYHpKMufyz9lz7vWEho6q/Qdpjt3lg2v5IKXejlrT+UXV1WNPu5kBqDl40T/xKDA aMvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592743; x=1710197543; 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=3J5Oq4avgTnUiue5ull73zk/v5Sx9caqkhc4270Z+44=; b=M0St60me3qXqog3C2tHGKcIPtJAnbnVsTUIUlcwK/tneyxgSH/z+wgQ8Hw0bhT37fi VtXOpKq11+vuDcLu51NQJuFfBYWKJOUxiY/HLhHeqHEmKx+ZMBpHJk8zy03oMSFOppnq LTElWhhMG7GLNXWIzezhQjVE1qiXkwUDR4Lz9Vc2OwPpAa/WL2NxfCpY5SeIAhHzdHSC 6RP96jQnYFp38hM28AsdNNOOSYH7gaZHc4vm6IhNAu3ThFJd7YFS0tbaUwXWre7+txfR j4MRD86tOYZYlS4OdeKEaWECYVUcI+b7tmppPqkJqdZdEf9Bhwz4Cd8fXrDjwUu1CLcu AaWA== X-Gm-Message-State: AOJu0YxBuyQwAbIVPqlYKKn+lqJN9S77GH86E074tZj12YV1EGjepfAn BKN3rsjydZw2ldl/TT7t3FwiUS2KT/Ak5rtoGxHLENYisLXyGrR+kGJB+IVn X-Google-Smtp-Source: AGHT+IEtEokyrTwum1XfI45OCvMG7I3qQMV8p3hfeqIqeZy0s3TnBIZ2R8YuP3pZDEysXnXiJCfjFg== X-Received: by 2002:a05:6512:546:b0:513:1ff9:2bb4 with SMTP id h6-20020a056512054600b005131ff92bb4mr106072lfl.30.1709592743129; Mon, 04 Mar 2024 14:52:23 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:22 -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 v3 12/15] libbpf: rewrite btf datasec names starting from '?' Date: Tue, 5 Mar 2024 00:51:53 +0200 Message-ID: <20240304225156.24765-13-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Optional struct_ops maps are defined using question mark at the start of the section name, e.g.: SEC("?.struct_ops") struct test_ops optional_map = { ... }; This commit teaches libbpf to detect if kernel allows '?' prefix in datasec names, and if it doesn't then to rewrite such names by removing '?' prefix and adding ".optional" suffix. For example: DATASEC ?.struct_ops -> DATASEC .struct_ops.optional Signed-off-by: Eduard Zingerman --- tools/lib/bpf/features.c | 22 ++++++++++++++++++++++ tools/lib/bpf/libbpf.c | 30 +++++++++++++++++++++++++++++- tools/lib/bpf/libbpf_internal.h | 2 ++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/features.c b/tools/lib/bpf/features.c index 6b0738ad7063..4e783cc7fc4b 100644 --- a/tools/lib/bpf/features.c +++ b/tools/lib/bpf/features.c @@ -147,6 +147,25 @@ static int probe_kern_btf_datasec(int token_fd) strs, sizeof(strs), token_fd)); } +static int probe_kern_btf_qmark_datasec(int token_fd) +{ + static const char strs[] = "\0x\0?.data"; + /* static int a; */ + __u32 types[] = { + /* int */ + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + /* VAR x */ /* [2] */ + BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1), + BTF_VAR_STATIC, + /* DATASEC ?.data */ /* [3] */ + BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4), + BTF_VAR_SECINFO_ENC(2, 0, 4), + }; + + return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types), + strs, sizeof(strs), token_fd)); +} + static int probe_kern_btf_float(int token_fd) { static const char strs[] = "\0float"; @@ -534,6 +553,9 @@ static struct kern_feature_desc { [FEAT_ARG_CTX_TAG] = { "kernel-side __arg_ctx tag", probe_kern_arg_ctx_tag, }, + [FEAT_BTF_QMARK_DATASEC] = { + "BTF DATASEC names starting from '?'", probe_kern_btf_qmark_datasec, + }, }; bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_id) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index c0212244bdf7..cf60291db8fd 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -2874,6 +2874,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); @@ -2883,9 +2888,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) @@ -2897,6 +2903,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; @@ -2922,6 +2929,8 @@ static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) char *name; name = (char *)btf__name_by_offset(btf, t->name_off); + if (*name == '?') + *name++ = '_'; while (*name) { if (*name == '.') *name = '_'; @@ -2938,6 +2947,25 @@ static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) vt = (void *)btf__type_by_id(btf, v->type); m->name_off = vt->name_off; } + } else if (!has_qmark_datasec && btf_is_datasec(t) && + starts_with_qmark(btf__name_by_offset(btf, t->name_off))) { + /* remove '?' prefix and add '.optional' suffix for + * DATASEC names staring from '?': + * + * DATASEC ?.foo -> DATASEC .foo.optional + */ + const char *name; + char buf[256]; + int str; + + name = btf__name_by_offset(btf, t->name_off); + snprintf(buf, sizeof(buf), "%s.optional", &name[1] /* skip '?' */); + str = btf__add_str(btf, buf); + if (str < 0) + return str; + + t = (struct btf_type *)btf__type_by_id(btf, i); + t->name_off = str; } else if (!has_func && btf_is_func_proto(t)) { /* replace FUNC_PROTO with ENUM */ vlen = btf_vlen(t); diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index ad936ac5e639..864b36177424 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -374,6 +374,8 @@ enum kern_feature_id { FEAT_UPROBE_MULTI_LINK, /* Kernel supports arg:ctx tag (__arg_ctx) for global subprogs natively */ FEAT_ARG_CTX_TAG, + /* Kernel supports '?' at the front of datasec names */ + FEAT_BTF_QMARK_DATASEC, __FEAT_CNT, }; From patchwork Mon Mar 4 22:51:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581342 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (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 C31A37C088 for ; Mon, 4 Mar 2024 22:52:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592748; cv=none; b=rMRqyD8j9BDiop8RJjKIMjAt5Ux3F8JlBAIyDeiDgfseDIDGiIqEwIy/jM3HxKnE7UM0GehuE0c7cFQODi7qAJ6t3eTpSUhCC+VXVX6lBLLsVuu+uIBw+8n7SfOljJQt3JYye70WcaYEZ/3TrFHIXKlKLtgIJkDqizYj82h7ozI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592748; c=relaxed/simple; bh=3ucbTocqqvyELdO97R7ddrmWT7k2ekLNsTzOQggLbK0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lss1XUnLpzV+PT8cyWSgwY9jBC2dtjhh09cTRWN+BOxvKYurPZVmgsIjbXdsHMr66b4lhTjwnhTZ50HAWT4HRjP9zrIlIuTneH75bqDWoVbsVhtIwmRjrCCiA6sbavf88DhlfaKbtDP8rlNVEHV8HjnW1b40jbenjqLKZ6vZtkM= 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=GuMyu28x; arc=none smtp.client-ip=209.85.167.43 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="GuMyu28x" Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-512bde3d197so4974540e87.0 for ; Mon, 04 Mar 2024 14:52:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592745; x=1710197545; 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=kW5p4yXSsN4YghlPBvvq93ye5IVZXwFo8TqXZjsdlc0=; b=GuMyu28xxsRts/ThRoKn4VNYn5uB5n94HiAUvpFyexQG8g3uaVwR1G79Oy+v6ahK9T axMIvGQX8KSLUuJ0sljN+Ahu+sH1HW+LuSuUMlseTBW1vFCUm920Uf2Tj8/2KbbP1yYm NU7o5HFxDh5mWgwqO6fH6oBBIMjW3YGPTxldZzTvOorh0xhKuSWvW4wWndioy5sz4PYw t1Hg+s2QCDekwK79i/UWK0xMmILeU4YlUuj5cLAzeekf7xu58bPsVoFpBfYY15XrlZq5 qJPpb7F1jbgM6XMrc6X59mwIVMCC7JbZhurP94jFkpwBZXpM2Ixae/ZR5GX8ABqrtvT7 3aVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592745; x=1710197545; 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=kW5p4yXSsN4YghlPBvvq93ye5IVZXwFo8TqXZjsdlc0=; b=qmln1GXiKVlFjDBXx8HhDoI/Wq/KinoQahAXIwIws54SAB6HEZ8gYvp2oS6dQkGXJK 71Xkj4DdJSLX+Bj9eHntXxgzgWfyqpqbLURLVAzchMxVDrfGZFuecWV4eocXpTADHpYQ GnNJRjM7XculfQfnQTmm71T1sGBBpiV/k3iX29h/R8iiL5DHGoOMt+D86oBcXY90YZG3 t90Ls/neIiFL3sfQEOkapgAdv7HRadRpjFdsJ+VEYKE6rTOuu9hVmN/DdEsuJKKczahN sWhxCyO4W3jzapKBGHorQ96hx/7oIpJb6KlRwYvBMokhnpNDgwL18JgLiNvo5jlkzdec Cdqw== X-Gm-Message-State: AOJu0YzsyLMv5cUOTFvSBRuQWnV/DYNPtZnQpc/BotRMrXf4fu9owWuL 7ECFeZ4zl81sx923EEFx/fPcfvsNWfeleA7kYNz4KMQHu9zupTleYrjMccxS X-Google-Smtp-Source: AGHT+IGj9QCGPeTHAPONF0pgEY66EVZK/ahZUU3Jf7oPjr7Je6OdBDZaU6PW0icMdQgQlsyu2PLitw== X-Received: by 2002:ac2:54a8:0:b0:513:3dcf:35ca with SMTP id w8-20020ac254a8000000b005133dcf35camr100423lfk.38.1709592744509; Mon, 04 Mar 2024 14:52:24 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:24 -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 v3 13/15] selftests/bpf: test case for SEC("?.struct_ops") Date: Tue, 5 Mar 2024 00:51:54 +0200 Message-ID: <20240304225156.24765-14-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Check that "?.struct_ops" and "?.struct_ops.link" section names define struct_ops maps with autocreate == false after open. Signed-off-by: Eduard Zingerman --- .../bpf/prog_tests/struct_ops_autocreate.c | 58 +++++++++++++++++-- .../bpf/progs/struct_ops_autocreate.c | 10 ++++ 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c b/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c index 35910cbb9ca4..a89ba036e2e2 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(); @@ -52,15 +66,45 @@ static void can_load_partial_object(void) if (ASSERT_OK(err, "struct_ops_autocreate__load")) goto cleanup; - link = bpf_map__attach_struct_ops(skel->maps.testmod_1); - if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) + check_test_1_link(skel, skel->maps.testmod_1); + +cleanup: + struct_ops_autocreate__destroy(skel); +} + +static void optional_maps(void) +{ + struct struct_ops_autocreate *skel; + int err; + + skel = struct_ops_autocreate__open(); + if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open")) + return; + + err = !ASSERT_TRUE(bpf_map__autocreate(skel->maps.testmod_1), + "default autocreate for testmod_1"); + err |= !ASSERT_TRUE(bpf_map__autocreate(skel->maps.testmod_2), + "default autocreate for testmod_2"); + err |= !ASSERT_FALSE(bpf_map__autocreate(skel->maps.optional_map), + "default autocreate for optional_map"); + err |= !ASSERT_FALSE(bpf_map__autocreate(skel->maps.optional_map2), + "default autocreate for optional_map2"); + if (err) goto cleanup; - /* test_1() would be called from bpf_dummy_reg2() in bpf_testmod.c */ - ASSERT_EQ(skel->bss->test_1_result, 42, "test_1_result"); + err = bpf_map__set_autocreate(skel->maps.testmod_1, false); + err |= bpf_map__set_autocreate(skel->maps.testmod_2, false); + err |= bpf_map__set_autocreate(skel->maps.optional_map2, true); + if (!ASSERT_OK(err, "bpf_map__set_autocreate")) + goto cleanup; + + err = struct_ops_autocreate__load(skel); + if (ASSERT_OK(err, "struct_ops_autocreate__load")) + goto cleanup; + + check_test_1_link(skel, skel->maps.optional_map2); cleanup: - bpf_link__destroy(link); struct_ops_autocreate__destroy(skel); } @@ -108,4 +152,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 Mon Mar 4 22:51:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581343 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 DEBE67CF0B for ; Mon, 4 Mar 2024 22:52:27 +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=1709592749; cv=none; b=VVqOr1VqDaBO66nGjFXm0WS+Vw+saviRr9c7XDaZs/OFp0xSFZQFEQ0hB9/AOPPXKH88kKKSatP2G5ySVQoh050/Vk21yhmMsyjIR7SggTRBRvWk1Hdgntq2Kh1jFAabBeWJ/ZAPUYlqbXRbHjzU2LYowntYY2ryOv1rHfPOTps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592749; c=relaxed/simple; bh=vY2eM0UqjsJ93XnGPnegnfX6oPS8A/CrHw+0bW+dkTs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Y5P+hIoZZw4qydyQ/H89R+mKBlcJHGvbu39k1SQjCsSCzpUiwoblcb1ESHhS99pSt+O3Dd7Fq48j9CSve/NnJtPLSmFfBTiyRZ2szz4H1qvVOXH75oOzmgsrE7YYLvNcB6n5jClF8mVRMp/qporIgFyAx4A1kkFJloUC0vtUc3U= 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=LZjxDwaO; 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="LZjxDwaO" Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a4515f8e13cso213941166b.3 for ; Mon, 04 Mar 2024 14:52:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592746; x=1710197546; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DYMlnF4l/D4ErZPFlA4ND9mYkMopZNVa5Qwk1lR+k2Q=; b=LZjxDwaOsZ5+UHW2sU1Sk5Ir4++opuzwJljE08nm9cgzKiJzTAr8/L7mgeEHy2QNkd mQl4HwKXNIKd+S1vB/CCTjEwXLodcfN+784QoHgC4rdk3tZVLPN3hDdSlGjUbRhk0jyf e0C3tD72Im7zG/BoLmTq8NV11zjMUEIUPIKd7skP8OZAQGujI/Rch/Fivc6TDTWIoLpm ZnaJayD+vFdB5889gnHiYe0+BeHfmPCPeTIyx/WjeG3xJoW51Knv+vGMa2omu2DMorl/ nifDR6AUR8nm/ltcLYfd0wZFWxhnWvm9cqMtk1LUe0YEZWWwc2Ll7UdlyTbyOU5XFJvg EIJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592746; x=1710197546; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DYMlnF4l/D4ErZPFlA4ND9mYkMopZNVa5Qwk1lR+k2Q=; b=eMOvz5E4gcC1qV0NT8sSnAMfvf7xplRzzI5uWg4GGK60tDu3EURu1jO4SCQILcjbK/ JDA/2AbJouLWWW7fjovXHQMrHFRY9QfEbjMUGL8K5eTieuqHF2RsMVXlDxthD+goMLCz 7Tb9x100URnMXQHt6I6iZbMmjwAtviVKG5RgLqrp/0ChTxp7l+3n8pX+JIqmhpJamhVz 4im1NGmd5jaViPoKTcz5XchL08/ORyQ/czoYcMdcmQ3dPwCqVjmJYB/qeDqpKAC1n+C9 2LJw7L1zdS7h4uTE9Eg2EIH9HvVIva9QkPB7VDlWnN0f58G7r/uQSXOJucMQB32I1DCZ LN8g== X-Gm-Message-State: AOJu0Yx+SdbiKZ+ktQDoe3fzciV9i2e2dEc+rbYSjEPmp0/8AJCrkr0a 46OKvWBSLWq1TfU622ChfsQ14spakm3lCrxe9RvCeNm8D/KYSsTGI8d67hSd X-Google-Smtp-Source: AGHT+IFy6Y0iy4gitInwjXletrvgSd4FHUeuDj35qTM+IHilt7A48ZCuixemtUKDYsT2V6DglzMUhQ== X-Received: by 2002:a17:906:240a:b0:a44:2b1a:e257 with SMTP id z10-20020a170906240a00b00a442b1ae257mr7132256eja.55.1709592745927; Mon, 04 Mar 2024 14:52:25 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:25 -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 v3 14/15] bpf: allow '?' at the beginning of DATASEC names Date: Tue, 5 Mar 2024 00:51:55 +0200 Message-ID: <20240304225156.24765-15-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Currently kernel does not allow question marks in BTF names. This commit makes an exception, allowing first character of the DATASEC name to be a question mark. The intent is to allow libbpf to use SEC("?.struct_ops") to identify struct_ops maps that are optional, e.g. like in the following BPF code: SEC("?.struct_ops") struct test_ops optional_map = { ... }; Which yields the following BTF: ... [13] DATASEC '?.struct_ops' size=0 vlen=... ... To load such BTF libbpf rewrites DATASEC name before load. After this patch the rewrite won't be necessary. Signed-off-by: Eduard Zingerman --- kernel/bpf/btf.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 6ff0bd1a91d5..a25fb6bce808 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -761,12 +761,13 @@ static bool btf_name_offset_valid(const struct btf *btf, u32 offset) return offset < btf->hdr.str_len; } -static bool __btf_name_char_ok(char c, bool first) +static bool __btf_name_char_ok(char c, bool first, bool allow_qmark) { if ((first ? !isalpha(c) : !isalnum(c)) && c != '_' && - c != '.') + c != '.' && + (allow_qmark && first ? c != '?' : true)) return false; return true; } @@ -783,20 +784,20 @@ static const char *btf_str_by_offset(const struct btf *btf, u32 offset) return NULL; } -static bool __btf_name_valid(const struct btf *btf, u32 offset) +static bool __btf_name_valid(const struct btf *btf, u32 offset, bool allow_qmark) { /* offset must be valid */ const char *src = btf_str_by_offset(btf, offset); const char *src_limit; - if (!__btf_name_char_ok(*src, true)) + if (!__btf_name_char_ok(*src, true, allow_qmark)) return false; /* set a limit on identifier length */ src_limit = src + KSYM_NAME_LEN; src++; while (*src && src < src_limit) { - if (!__btf_name_char_ok(*src, false)) + if (!__btf_name_char_ok(*src, false, false)) return false; src++; } @@ -806,12 +807,12 @@ static bool __btf_name_valid(const struct btf *btf, u32 offset) static bool btf_name_valid_identifier(const struct btf *btf, u32 offset) { - return __btf_name_valid(btf, offset); + return __btf_name_valid(btf, offset, false); } static bool btf_name_valid_section(const struct btf *btf, u32 offset) { - return __btf_name_valid(btf, offset); + return __btf_name_valid(btf, offset, true); } static const char *__btf_name_by_offset(const struct btf *btf, u32 offset) @@ -4481,7 +4482,7 @@ static s32 btf_var_check_meta(struct btf_verifier_env *env, } if (!t->name_off || - !__btf_name_valid(env->btf, t->name_off)) { + !btf_name_valid_identifier(env->btf, t->name_off)) { btf_verifier_log_type(env, t, "Invalid name"); return -EINVAL; } From patchwork Mon Mar 4 22:51:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13581344 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.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 2B40B7CF32 for ; Mon, 4 Mar 2024 22:52:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592750; cv=none; b=hY3CrFB6dKnDIhyvKjwj/Lj4ZYuxZ3T2WMdDT4rFGzHF0PtnYyqY089j8+MuEyOEtk8hLfWRyWVlsU4KDhq5f2kRiOxZ+wFcRStz2wtfSql2P7b95RCTIbyumceyCg8NffTsNcdN3gvK82cq5ilT7ajuW3u90P7I73jEhUfpAds= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709592750; c=relaxed/simple; bh=AYRFrFsme20cwtwcfp7bMqH+JFxsSxvqP14s1SLy364=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fdJhThITDb4Kye2dJMLsmDhKzcsE7Owy6Jl6Pp45b8btk8Cnpj+p2okZqUtSEqp0JHgCOLuU9f3gR+T8PW6GhldLWVdzsnnlozmtqOxynJOq02gFWuJvEvCXIffiWhssyGOccCiaChoKZqDBDWByLc20nQj/gnJT6amGFOwpBoY= 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=JpTqgob4; arc=none smtp.client-ip=209.85.218.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="JpTqgob4" Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-a26ed1e05c7so852634866b.2 for ; Mon, 04 Mar 2024 14:52:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709592747; x=1710197547; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=yUKVRagEjVbWTLTtfvO897pmVhBdhZlyN2rymNdpliI=; b=JpTqgob4ckRtOGPOYCnLuOqbLFQZ9UI48/ASPdxUXz04z2wROvhBIl3/sO5j2vRvoe xy9WZ2qnlBHzzQvwZABnV1HVBtCCS1v0C4sqMOeMnruLT+JWq3xQzwzNBFFGW7N9YDl6 wsjDIvJD3nK9MoUzZZEk2h13hiy+OYGUXpLkPt05ae6WrHs8VyNJBDqi72UQa6rgTs9Y cCWILteAVqQG/ubi78scWmBnhnF617DmG1aAVJF81/NsUSbyHqnaY5AFzdIjirKPtF+S 2vlQvETapdqilO364gCDQJSK7tN+LCvwdgZq6beGYbGIkXgp9vGEWuDyZ0x3X/Wkm05T TQag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592747; x=1710197547; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yUKVRagEjVbWTLTtfvO897pmVhBdhZlyN2rymNdpliI=; b=aRK6e696l317/1++guT/aNtTH1+8B2e3JLh10QhnRdTyPwtiKfNOosdCHd+BQdnlIN l95keixHBNlvk55pmB2xZi2gVQy821+3cFmYDx6s8ko1nE0NCbX+Hro3sJXfTK5ywEUQ AxbjfiivtfZ/C3y4FOP2a+o2oy33fn44VrWjm2DFVX9VPst8emCD0tM1R3YkoomId1F4 3/OSLfp2Jpk/jDT8obvgb18pReqNC4HmMo98bPeIOjHh0dL4nqNLZdAIp8eq4N9y+Ykp Kx2CQ+a5yRXe5g6VpxyPU/kfzrWxsMRJ2Po2LfhHwBNabshZgkkOemqVhb05SBIgBcC1 7CTQ== X-Gm-Message-State: AOJu0YzQmQ73qY0CbdZdLyCYrLEQDhDZi1GmDprrQYtfsA0iSYvfj4ot rL99QrxVDBX0wiIfJjNAlKAC9JE5wpJB1lg3EXMwZ3KtnC5gm/cC69LMpeqQ X-Google-Smtp-Source: AGHT+IFGroyHKYBVrvjaM9mVMusS8Oiag0e7cXIIN9LyaWwEpaO71CtxoLynYeeGDr76Ptc1cKN/Lw== X-Received: by 2002:a17:906:8411:b0:a45:817f:24fb with SMTP id n17-20020a170906841100b00a45817f24fbmr1625701ejx.10.1709592747325; Mon, 04 Mar 2024 14:52:27 -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 qx22-20020a170906fcd600b00a4302eb448dsm5315499ejb.134.2024.03.04.14.52.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:52:26 -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 v3 15/15] selftests/bpf: test cases for '?' in BTF names Date: Tue, 5 Mar 2024 00:51:56 +0200 Message-ID: <20240304225156.24765-16-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304225156.24765-1-eddyz87@gmail.com> References: <20240304225156.24765-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Three test cases to verify when '?' is allowed in BTF names: - allowed as first character in DATASEC name; - not allowed as non-first character in DATASEC name; - not allowed in any position in non-DATASEC names. Signed-off-by: Eduard Zingerman --- tools/testing/selftests/bpf/prog_tests/btf.c | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c index 816145bcb647..88c71e3924b9 100644 --- a/tools/testing/selftests/bpf/prog_tests/btf.c +++ b/tools/testing/selftests/bpf/prog_tests/btf.c @@ -3535,6 +3535,49 @@ static struct btf_raw_test raw_tests[] = { .value_type_id = 1, .max_entries = 1, }, +{ + .descr = "datasec: name '?.foo' is ok", + .raw_types = { + /* int */ + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + /* VAR x */ /* [2] */ + BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1), + BTF_VAR_STATIC, + /* DATASEC ?.data */ /* [3] */ + BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4), + BTF_VAR_SECINFO_ENC(2, 0, 4), + BTF_END_RAW, + }, + BTF_STR_SEC("\0x\0?.foo"), +}, +{ + .descr = "datasec: name '.?foo' is not ok", + .raw_types = { + /* int */ + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + /* VAR x */ /* [2] */ + BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1), + BTF_VAR_STATIC, + /* DATASEC ?.data */ /* [3] */ + BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4), + BTF_VAR_SECINFO_ENC(2, 0, 4), + BTF_END_RAW, + }, + BTF_STR_SEC("\0x\0.?foo"), + .err_str = "Invalid name", + .btf_load_err = true, +}, +{ + .descr = "type name '?foo' is not ok", + .raw_types = { + /* union ?foo; */ + BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [1] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0?foo"), + .err_str = "Invalid name", + .btf_load_err = true, +}, { .descr = "float test #1, well-formed", @@ -4363,6 +4406,9 @@ static void do_test_raw(unsigned int test_num) if (err || btf_fd < 0) goto done; + if (!test->map_type) + goto done; + opts.btf_fd = btf_fd; opts.btf_key_type_id = test->key_type_id; opts.btf_value_type_id = test->value_type_id;