From patchwork Tue Feb 27 20:45: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: 13574393 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 74F1014C5B6 for ; Tue, 27 Feb 2024 20:46:31 +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=1709066793; cv=none; b=VbcqGn0V+Iz7EuHcuyOdwQJ7e1e6oKwqcvJ4MsNqFNcW1QVVLK0eS5mtmzo7Z35hHlryli+jtv2E2t28t1bQchdZBQLROqYIGx9YjHslR0VK69IFDmGZhoMeMYx2Idp5acIYjAguzwzBjv/eGoIznGj1VtVVn5z3/RaZWRHmPX0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709066793; c=relaxed/simple; bh=JZLPYWwur253VYYmx7lHkzSA5g3GXygzBOw0X4jXqh8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IbJnLH8ML5847rA+You/zUy8U5O2MlmUrLDabqtGLYPvFgEb7CnS8a/kWwOqgPlw6amLxN9/5cSJYjyrFp8ZvD04UlH+mmSNQWPsxHkv9Hwt+M4ikCdAHG88VoXr/xhHOj6hV5pjyywOU+3YM92EhKVJGUyJuipxm9qwEv9BuD8= 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=UBpc9xMM; 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="UBpc9xMM" Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a4396b785b8so212871666b.3 for ; Tue, 27 Feb 2024 12:46:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709066789; x=1709671589; 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=ck4ahKqvUsLRPZMERtckLP+GrzJt9TdWlwFGKbWZO2M=; b=UBpc9xMMXv6AukvmoDvfvI/iipAwCW/Zyoa3C1aR5oF/hkszDUxKUGk+pRliKAHyon xHN/D8teqab0gIwptz+9eAUM6PPyI5ISG57H3j/n2gWmMFts0tEvpmXFLtn4GHkYBfZv CJ5UMaA19nPEo0ayjmKP6a6Jh3TNC3a7AvMyLHP0ybhezq/g7j5lfpfTSWLhdX2iqqAo loQV0Q0Zyl3zZI+d+C/Am+y/QqL0hgjxO6bDrh2K82Pl68AhgSwdtV58W0ECbGq+fgGG PZ8k2GUmsoD52HDcDYj/yYthQ8pwFiza75KtAZSWAyWeippvY5ijVbMyNArWlnE52zoj PNXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709066789; x=1709671589; 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=ck4ahKqvUsLRPZMERtckLP+GrzJt9TdWlwFGKbWZO2M=; b=PVWGpiTshV3GQEbq13Go4kJ2PqwwQoer8Bww0NxcySCdV+LLXKFu/nY9MSzlLf4Zk+ x0wLeg7Nh/eUewFpUKDy437w0cDv5htYpRUk085kP8I1g6PqTJQ2d7qotPESM3vhV9k2 MfvM+kPySIgtq0KI7EzdWztAx7z2vINnBo5PcRb4ZTEvgnDzepJXrXgEgB/D+Q9ExQVe kxCPdtdntUxF/hmGhyg/Mr/5pmy3HFDvqZVXGmkq4Nr1tNWV1PDdVwH4coVCNuIwU9Na Pl1FWsGLMh4F46yVH7s/Q3ZP/rMNGVeivtg0Lz4yNIme74t6MDOL2rqNlabiJycbWOvo m3Sw== X-Gm-Message-State: AOJu0YyeWMIC1S58IGle7nYtg/rj6YPTUKLZMP8n74NU5cbafqbwUy0T fgjuRJLEzD/GBY3bAL/WQJi71x0asT5fQf0naacu5LWCzahmzrW6An1HHAD+uOM= X-Google-Smtp-Source: AGHT+IF7xETZyKvrXEJyDgsRy5ofg3V+k51mCMyInKCVeaFG8jVedZYMps1VpBlQ7N9rMQg6qgPwyg== X-Received: by 2002:a17:906:2419:b0:a3e:5adb:cb21 with SMTP id z25-20020a170906241900b00a3e5adbcb21mr6556981eja.59.1709066789618; Tue, 27 Feb 2024 12:46:29 -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 hb13-20020a170906b88d00b00a3d9e6e9983sm1119832ejb.174.2024.02.27.12.46.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 12:46:29 -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, Eduard Zingerman Subject: [PATCH bpf-next v1 1/8] libbpf: allow version suffixes (___smth) for struct_ops types Date: Tue, 27 Feb 2024 22:45:49 +0200 Message-ID: <20240227204556.17524-2-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240227204556.17524-1-eddyz87@gmail.com> References: <20240227204556.17524-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. Signed-off-by: Eduard Zingerman Acked-by: David Vernet --- tools/lib/bpf/libbpf.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 01f407591a92..abe663927013 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, @@ -957,15 +957,21 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname, const struct btf_type *kern_type, *kern_vtype; const struct btf_member *kern_data_member; struct btf *btf; - __s32 kern_vtype_id, kern_type_id; + __s32 kern_vtype_id, kern_type_id, err; + char *tname; __u32 i; + tname = strndup(tname_raw, bpf_core_essential_name_len(tname_raw)); + if (!tname) + return -ENOMEM; + kern_type_id = find_ksym_btf_id(obj, tname, BTF_KIND_STRUCT, &btf, mod_btf); if (kern_type_id < 0) { pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", tname); - return kern_type_id; + err = kern_type_id; + goto err_out; } kern_type = btf__type_by_id(btf, kern_type_id); @@ -979,7 +985,8 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname, if (kern_vtype_id < 0) { pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n", STRUCT_OPS_VALUE_PREFIX, tname); - return kern_vtype_id; + err = kern_vtype_id; + goto err_out; } kern_vtype = btf__type_by_id(btf, kern_vtype_id); @@ -997,7 +1004,8 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname, if (i == btf_vlen(kern_vtype)) { pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n", tname, STRUCT_OPS_VALUE_PREFIX, tname); - return -EINVAL; + err = -EINVAL; + goto err_out; } *type = kern_type; @@ -1007,6 +1015,10 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname, *data_member = kern_data_member; return 0; + +err_out: + free(tname); + return err; } static bool bpf_map__is_struct_ops(const struct bpf_map *map) From patchwork Tue Feb 27 20:45: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: 13574394 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A25014D425 for ; Tue, 27 Feb 2024 20:46:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709066794; cv=none; b=Ici9+qBrYaYDdDzHxbsi3ynuRPhMgK5TfCM6fZ0ukCuZdQYBEiQBRfnORU0AqeSZzQg4+1HvzX59WedBChOn/LfR0hHDW5Uu41ICXnbgTTJ63t6ThaI9Wg1+hajXE3MpTc0c1xw4Nhv7+cG5QVqLguNxUh5vrF9usiW2slnIfZA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709066794; c=relaxed/simple; bh=ZdJM11ZJMQL4kqeHstlcNnl5hl1UkYiPZkVwm1vJIVM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cngrCG44ODPK8flnCx3QfA4+IQUokGf2DNuBmThviFvieTWZsoCxcQGVBm/2zlzLIFy9yqctL5zCyUdV2niMe2NprefcTIufHT9sKLF6YHgOXMZTeA6WWoocc3G8ApZDVYtTNXfuQBWniWH4mfUIiaKaw9jGG0WJ2vQfthdpEwY= 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=Qd34Em+w; arc=none smtp.client-ip=209.85.167.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Qd34Em+w" Received: by mail-lf1-f41.google.com with SMTP id 2adb3069b0e04-51316d6e026so643964e87.1 for ; Tue, 27 Feb 2024 12:46:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709066791; x=1709671591; 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=j00Md19Jw5VmAQzRBHP8AbFIwaIQCyFiwhh2nW0rXis=; b=Qd34Em+wc7yYU9G7tYh3ZnaNuEFaMsbTkxQg4ACnXHdI7zpCgA+mhkJa+A0p20w/IO dQtEUhCEWl9rkp3HXLCOayfB5baYYFHGKcT98RON1fBD3Qj2qUjhip3dx++jxuWUxcVD CxcEEtKc+BjzUNR59UY7GhTkc6ySneMdW23JQDIBSaRyMnUPnNfZ79rkFZHP7gONIGvc VcjepKvyuHlztug5i8kO/5RzP2d55qpsvnhb3xp4NMWjwFcNG9gdy5Ytvf8SYUY96mPL Y328ygMPB70awXv0mLwG79GDPTsh6qtCIgEaNCi+0yWAwFShoGWwkqW5RL//GoqTCVR9 CNWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709066791; x=1709671591; 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=j00Md19Jw5VmAQzRBHP8AbFIwaIQCyFiwhh2nW0rXis=; b=nLxIzBHwKaCrTstWRL+B/2Rb+8Y7roFzaf/QKLwJPopPl3oI7j0Btt5UTmwUZR5/Sg 0btyEAdcI6jOlSoKPaTGJUq0KjnmgWE2AX/jPes/uX5vRC+U8rEFWEywwcOxQw9x6/zl tszu7lN5QMjsIA1IlMKeFjnGrxx3BFEQHimEKkGu2/FPd8fwKwouWE/HQnZ6DAYO8b2T o4df8aamMNRbLhIZksb9DKuLt7mP5tCL/v8AQngBGeqTOmNJn6+S2g31zdGl17CrkKjm UEPuHhX0jbvYMW16kRmuJZTVTnFqsjnDsa+11UJS2oQr6l7+B2RR231DlrM2hVBSIrO6 OqBA== X-Gm-Message-State: AOJu0YxVLLX32+DS5NGkcdEXOOQv0gGiTUoJj0oPjEDTwYRZdTBoJD0C Wl0Kysg3GwmIJyzSWu6dT9iXRQe8WkS5lpCZCeV9pqwmNyETl41SG+HWueK4XRw= X-Google-Smtp-Source: AGHT+IE+plvsJAuBAjP1auTDnyc9POXqssakgs32dKPl0WhlNrOWsdxPRcStKlfn9SpPp1qiTOVdXA== X-Received: by 2002:ac2:4e6e:0:b0:512:a939:3fcc with SMTP id y14-20020ac24e6e000000b00512a9393fccmr6202191lfs.32.1709066790717; Tue, 27 Feb 2024 12:46:30 -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 hb13-20020a170906b88d00b00a3d9e6e9983sm1119832ejb.174.2024.02.27.12.46.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 12:46:30 -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, Eduard Zingerman Subject: [PATCH bpf-next v1 2/8] libbpf: tie struct_ops programs to kernel BTF ids, not to local ids Date: Tue, 27 Feb 2024 22:45:50 +0200 Message-ID: <20240227204556.17524-3-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240227204556.17524-1-eddyz87@gmail.com> References: <20240227204556.17524-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Enforce the following existing limitation on struct_ops programs based on kernel BTF id instead of program-local BTF id: struct_ops BPF prog can be re-used between multiple .struct_ops & .struct_ops.link as long as it's the same struct_ops struct definition and the same function pointer field This allows reusing same BPF program for versioned struct_ops map definitions, e.g.: SEC("struct_ops/test") int BPF_PROG(foo) { ... } struct some_ops___v1 { int (*test)(void); }; struct some_ops___v2 { int (*test)(void); }; SEC(".struct_ops.link") struct some_ops___v1 a = { .test = foo } SEC(".struct_ops.link") struct some_ops___v2 b = { .test = foo } Signed-off-by: Eduard Zingerman --- tools/lib/bpf/libbpf.c | 44 ++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index abe663927013..c239b75d5816 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1134,8 +1134,27 @@ 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 || + prog->expected_attach_type != kern_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, mname); + return -EINVAL; + } st_ops->kern_func_off[i] = kern_data_off + kern_moff; @@ -9409,27 +9428,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; } From patchwork Tue Feb 27 20:45: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: 13574395 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 E197F14C5B6 for ; Tue, 27 Feb 2024 20:46:33 +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=1709066795; cv=none; b=GowQ4iJLvW4LcQKsOqu/s6t/QoDko+9Enw8DDT+LFt63MsYW5M7NBa+2keoMfDhsCPGHA2aQkmSmxH2H4psKC9F5Bvg0+JKbTlG41d+o8hZ+LNn5+kPq40zKI1d79fNoFu0w9bS0PPALIa0y0M7W81nGgIpDzdQ7a3//OMx+Afs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709066795; c=relaxed/simple; bh=VxymEpJKHlGJlVgkg1ah8INJrTbJsBszLUr9EQZI2oA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QrlBPDqBZqli4TJqt1cu7aqZLHTBd0FuwlMdTzjCl/xC6t+r8Ik44VzOVflcWWukemvLNvzU35FDPPI+iRWo0c4Xr1mIhk+rfon+fZq//v3uuKxZfTWE/ykjtwukABsRyr2l4NE7VaWVU9UPiDcocVZwAFvGnU0KVgaBySdfLZo= 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=knMQsf7n; 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="knMQsf7n" Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a43488745bcso396822766b.3 for ; Tue, 27 Feb 2024 12:46:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709066792; x=1709671592; 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=RMVSlIlXMxyBF4/KRZ18vJJTozTm9olW/1R0gUR4hbA=; b=knMQsf7nTUI0LagqpPt5CcSjkDRuyD6WbO8J+PVgCiKcWDnAVATvJYdzjPRT5LSjSQ xSLBhg7EH6TivqHROFyWRrQxarZNvPeHZCcxyJEcxhyVSJdJaCVft6FAAZiYCuUEIhPZ wnKBNplaUIlLmFB/Nzli00OwsBr8m3bS1pydu++e5sCRfgvcegMzhi2PySsg/0BjJyiJ yOJEMVi+XAhch2ZML6XJ+q8n4pIH5dM+DqjdJh79Dz/qXIWA8bOnzXwmnjohDvZqjeks IdwTUujlc/DhDzf8QfkFiAW+OXHVnxSLo8pp8bQ5lLdpQ6oyURgeWrZ1nsFIU+N22Ht4 15xA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709066792; x=1709671592; 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=RMVSlIlXMxyBF4/KRZ18vJJTozTm9olW/1R0gUR4hbA=; b=Daz/LAaIlVJ3dwI0zAXG+rvt11LGMABPSliJRygwokIWAK2jn0FWZmRbW8UsGPiqf6 SHbxPzV8lStDWSc1nKZw0oC0qZ9evHswTTy5OTiRLH0V1o6+WmkBoSh2bVowIN25Np2t Pjd3uITJ+iuNqqYtK9siS6sveo7HxdeDpGEAUDh276T6dZmwJrfPQl4FwHSKYAeh7h6O E5TUjNQ1ziQFDdmirADzesLCXfRvU66XMWpnpKeSCvwH9o+eZSP0K1KyxJVSx5fkdJH7 RoyPVUDu4etJtvjUDplI+LFysNK5lBUv39XOTmjd9MJWSDDty+T5/4gnwDY9XMKf15hZ L1iw== X-Gm-Message-State: AOJu0YyPWNT1IMfS5e0w8CJM+rPwkGlQq1eNyqxncQBSJPZ43LKsjmzX jkBo4EmEyR2GxmvnsvFfiK+uGOQ6kkFrTs3mnnXz1y49Q7VCIpVsB1z5NMGKGhw= X-Google-Smtp-Source: AGHT+IFBYEgmQ1esj2kCZ3WbL0zxkGQsleeEPJ71Vhl+R6+K9fINFZNKeHVjomuP9lm3RsOGI4cIDw== X-Received: by 2002:a17:906:3c05:b0:a3f:db30:8999 with SMTP id h5-20020a1709063c0500b00a3fdb308999mr8335751ejg.4.1709066791816; Tue, 27 Feb 2024 12:46:31 -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 hb13-20020a170906b88d00b00a3d9e6e9983sm1119832ejb.174.2024.02.27.12.46.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 12:46:31 -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, Eduard Zingerman Subject: [PATCH bpf-next v1 3/8] libbpf: honor autocreate flag for struct_ops maps Date: Tue, 27 Feb 2024 22:45:51 +0200 Message-ID: <20240227204556.17524-4-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240227204556.17524-1-eddyz87@gmail.com> References: <20240227204556.17524-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); Signed-off-by: Eduard Zingerman Acked-by: David Vernet --- 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 c239b75d5816..b39d3f2898a1 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1192,7 +1192,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); @@ -8114,7 +8114,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 Tue Feb 27 20:45: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: 13574396 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ed1-f54.google.com (mail-ed1-f54.google.com [209.85.208.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9358134B1 for ; Tue, 27 Feb 2024 20:46:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709066796; cv=none; b=EZzEUenv0ul+0TMZyqYPHNk7xmdIex6VUQymOf+7b6xKtEdSYndAfH8ZqCW6R85/ydPaaMdQc1m9LU638EcDrXOkNP+lKY9d+RlHBJmM+OaZZsIBLKdfaEnPjYm0aZKNxdY2a8xnW1oXGl5BycxoZharQKlAqvTbk73r2Nn1/VI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709066796; c=relaxed/simple; bh=0AuSwfw14n0kaj/ajCbSsEi+4AfinuiC4BPDCRWxqjo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N0n5HMs1G3uZ9me0c8xu5CvXDAESQO18MuG0LwYJP+CsP2uRQPFUrMF8Z4Dmf6o2XGhOpWCIr9tFYq6Eldpy5JG4xL7eDL3pkmt+AHVetlPBh03G2e7hDiq4GBfX6wzOJlzJexSo28OWWs0EGbm8tLMTTGwZ9W/5Jackezy/HbA= 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=KHM63m/v; arc=none smtp.client-ip=209.85.208.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KHM63m/v" Received: by mail-ed1-f54.google.com with SMTP id 4fb4d7f45d1cf-56533e30887so5486180a12.1 for ; Tue, 27 Feb 2024 12:46:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709066793; x=1709671593; 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=tg81NkrF2MG3shbswEhvkPBxiUXCqbyJB/br8WJz2/I=; b=KHM63m/v1yAaTqnSEEqhmZJpz+X6HKWa+c7wUZ+EWB0l8dBcssoTgalcquQjwicE8R plo0ZV39q1nNFnhs8oOT1o/A2iE/GBvmbrwHkBt2a4npwPJz0pbgWTBM29/SeMVs8XOk 6pUaSgXR/1hjiH8bxfEwpnCgmCtLiGn6PlXbVyaKIwh3cWSXASCpVWxJLo1INiBQiGX2 hTG/fNGaL/hvwcNplueWlUGEiH/veP8+orLqE4oAR9gpYnBetyaEyLLKpOrV7Zk9ZzW/ A5eG9HWpshD4+b+bIZ3OQ88l5AyOY6o2Pezf8mLBwRUcKrQ7BOwKrOyauZbsZX+sSf7B J7Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709066793; x=1709671593; 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=tg81NkrF2MG3shbswEhvkPBxiUXCqbyJB/br8WJz2/I=; b=Z+idY6IaVlktAC92hWTB1RwyvFFjfQt97F3p4XpftHXn/MRTCnm3l42xpiFYR88Ema JuBmB3K71oiTzYNtIAZKMEEXp8QyMwCWjqrKgQut9nMPfytj8Ma66EKnrV347wug3suL OuOmGbSoNNWyHq6YJnf36bXKzOf0LkvFfFgsLsF5vuXKG+at2yYPGQxPlLgTat/s0yf7 TwuSqjdBRXRWJWHatFJRJFRQCQ1DlNNMiApoRgKLL3y9LLO3yG8FTnnRLyv/ijc5UU32 u0FedkQKayrd6ARFFrYjFK+ttUrIm6msmBOESAI30be4kF+Zw/iKFypFjekzVapxiqzv ZTJA== X-Gm-Message-State: AOJu0YxI3S2SPLfOI3hjPKRbpKf+1sNoN+QkIJMfR1cZKgw3E5NQi6Ac SbBV2ydkDDRdjB//Lv+SbhMDrG+iTOidsN1rh1JD8O0Em4u8j+90ZMo0JGaQxrI= X-Google-Smtp-Source: AGHT+IFxHQhGm+RgXWhvAbwB+FloHm5JB5I1XCV+ISW62pF1z4NUpW3oYp3BO0d4J8nxlnwMR5kv+A== X-Received: by 2002:a17:906:ecb4:b0:a43:1940:c7f8 with SMTP id qh20-20020a170906ecb400b00a431940c7f8mr5147462ejb.31.1709066792939; Tue, 27 Feb 2024 12:46:32 -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 hb13-20020a170906b88d00b00a3d9e6e9983sm1119832ejb.174.2024.02.27.12.46.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 12:46:32 -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, Eduard Zingerman Subject: [PATCH bpf-next v1 4/8] selftests/bpf: test struct_ops map definition with type suffix Date: Tue, 27 Feb 2024 22:45:52 +0200 Message-ID: <20240227204556.17524-5-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240227204556.17524-1-eddyz87@gmail.com> References: <20240227204556.17524-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. Signed-off-by: Eduard Zingerman Acked-by: David Vernet --- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 1 + .../bpf/prog_tests/test_struct_ops_module.c | 32 +++++++++++++------ .../selftests/bpf/progs/struct_ops_module.c | 21 +++++++++++- 3 files changed, 44 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 66787e99ba1b..0d8437e05f64 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -555,6 +555,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 8d833f0c7580..7bc80d2755f1 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,12 +30,30 @@ 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) { DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts); struct struct_ops_module *skel; struct bpf_map_info info = {}; - struct bpf_link *link; int err; u32 len; @@ -53,15 +71,11 @@ 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"); - - /* test_2() will be called from bpf_dummy_reg() in bpf_testmod.c */ - ASSERT_EQ(skel->bss->test_2_result, 7, "test_2_result"); - - bpf_link__destroy(link); - check_map_info(&info); + if (!attach_ops_and_check(skel, skel->maps.testmod_1, 7)) + 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 b78746b3cef3..e91426dc51af 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") @@ -27,3 +29,20 @@ struct bpf_testmod_ops testmod_1 = { .test_2 = (void *)test_2, }; +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 Tue Feb 27 20:45: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: 13574397 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.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 1EEF314E2E7 for ; Tue, 27 Feb 2024 20:46:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709066797; cv=none; b=lZKnkHfXTdo/jyXnLE6HuA/KVXVgyowCDyB5YM5FO0qFf7pWnyFcgiM6S4t3ly1Sq6T4ie4BvkJ6cEEu6dW1+evGUn+kYeRReN+sbh396YxSC4SnLxMOLcvpmGJU9YCY5JQewEsIka0MPttDR4mZ7yYnpES0dinFePgEU2IGqrg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709066797; c=relaxed/simple; bh=aPHgAlniB8mYH7lkQgh1G1ID2gwOCNzEOk5VdAFGKI4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NlXP0GvbAh4k5wyBuY+QKtlrCkGXNiwfEeCKhDckehvJJlpztyzGLIWjUJHn+A2/bBeiviqVTVklulHkZf56nOZWq0Wno1rc4r6oSGKPNURxtv7x6FtGVzDOgFw+7r2Vj4mz10lK2q7J/BB3s9EyTwXcC2GeikE/rwZm5m8OhuQ= 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=ZnyTI09L; arc=none smtp.client-ip=209.85.208.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="ZnyTI09L" Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-564fd9eea75so6063071a12.3 for ; Tue, 27 Feb 2024 12:46:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709066794; x=1709671594; 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=ugm6wlCTGmAs31wehy3V0RDN9q39yLThBREE/iiVQG0=; b=ZnyTI09LxUqNkYXOda0sr/GqCo/xjeiIo2YF0tpGKMJ8uvWwUKpNYVbHZAc4lxt4Y0 KHkDQ9MWIcFrSBZkKMwgK79TRz2xMf+b5u19LhH1Cr3gUXtVRBKztZWwyaCZ+bgoGTux j4fFGA3jkHyV0m48h5l4o5TQj/hdXkCQKYH/sfwkv4ugyO7ZQnsbFCPNriHlc8MWsYgT 1b7vpEoxLWH1nz/aP/GHE8L92rl57p+DofcDpPpc7Yw7E3le2/WwUWCNHZuZOBjNoy8x +v82FxRG7pbNgDYgqvTiT4BYYlF9ppwh4d/XHxP1JGOVa4BDpfahosW9yGj+A45wlLkU 34mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709066794; x=1709671594; 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=ugm6wlCTGmAs31wehy3V0RDN9q39yLThBREE/iiVQG0=; b=QOjv+s+qoVPZN0Kzu5mBpUDV30HuEj99RCNYNJjgg38aN1Hx1HzbK0pmm/a+q6Vd+r tXFGhOWSZUQwIOXs5lHAvm+WTVwznom1HJT8siSep9js99tIqalCSZDnbLHHiL8fHVQB 3E/aSakcs4creP2lIDFekxZq+vGpULEgjWF8o0s8k2NpGvpYN2EOeF2rXm9003F4t83X cIfVZtD0HgQ1DzYRNnhZn9Fkysr/N2eINf2lubNrzsKpUpZrDeIXK3LdLeZeuoiSV2Sb WpNN1RNYnrOdkBUdhbQFNdxe4H8GxL0kBcVP90DAXqylfu8N3uPXqdePmwWDrtUYwpaP NyQA== X-Gm-Message-State: AOJu0YxS6e3JAEx7SbjF+nJU8up8JhP91DBypRlIstOEytDjUCBqGSmr NrS62Az7Dg053p40Z6Nan84hI9H9wI3901UiuF9BSmF+TzvdGRji6wY1zu0WE40= X-Google-Smtp-Source: AGHT+IFYe9DvHJshCuNGw770tMdZz2v7jqt7BdSGlRfsmrGSQqnXWm2n+GdAj9gI/l+UdPfrC7Xfag== X-Received: by 2002:a17:906:3397:b0:a3f:d797:e6e2 with SMTP id v23-20020a170906339700b00a3fd797e6e2mr6943126eja.28.1709066794210; Tue, 27 Feb 2024 12:46:34 -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 hb13-20020a170906b88d00b00a3d9e6e9983sm1119832ejb.174.2024.02.27.12.46.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 12:46:33 -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, Eduard Zingerman Subject: [PATCH bpf-next v1 5/8] selftests/bpf: bad_struct_ops test Date: Tue, 27 Feb 2024 22:45:53 +0200 Message-ID: <20240227204556.17524-6-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240227204556.17524-1-eddyz87@gmail.com> References: <20240227204556.17524-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net When loading struct_ops programs kernel requires BTF id of the struct_ops type and member index for attachment point inside that type. This makes it not possible to have same BPF program used in struct_ops maps that have different struct_ops type. Check if libbpf rejects such BPF objects files. Signed-off-by: Eduard Zingerman --- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 24 +++++++++++ .../selftests/bpf/bpf_testmod/bpf_testmod.h | 4 ++ .../selftests/bpf/prog_tests/bad_struct_ops.c | 42 +++++++++++++++++++ .../selftests/bpf/progs/bad_struct_ops.c | 17 ++++++++ 4 files changed, 87 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 0d8437e05f64..69f5eb9ad546 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -601,6 +601,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) @@ -612,6 +635,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 c3b0cf788f9f..3183fff7f246 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h @@ -37,4 +37,8 @@ struct bpf_testmod_ops { int (*test_maybe_null)(int dummy, struct task_struct *task); }; +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..9c689db4b05b --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/bad_struct_ops.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include "bad_struct_ops.skel.h" + +#define EXPECTED_MSG "libbpf: struct_ops reloc" + +static libbpf_print_fn_t old_print_cb; +static bool msg_found; + +static int print_cb(enum libbpf_print_level level, const char *fmt, va_list args) +{ + old_print_cb(level, fmt, args); + if (level == LIBBPF_WARN && strncmp(fmt, EXPECTED_MSG, strlen(EXPECTED_MSG)) == 0) + msg_found = true; + + return 0; +} + +static void test_bad_struct_ops(void) +{ + struct bad_struct_ops *skel; + int err; + + old_print_cb = libbpf_set_print(print_cb); + skel = bad_struct_ops__open_and_load(); + err = errno; + libbpf_set_print(old_print_cb); + if (!ASSERT_NULL(skel, "bad_struct_ops__open_and_load")) + return; + + ASSERT_EQ(err, EINVAL, "errno should be EINVAL"); + ASSERT_TRUE(msg_found, "expected message"); + + bad_struct_ops__destroy(skel); +} + +void serial_test_bad_struct_ops(void) +{ + if (test__start_subtest("test_bad_struct_ops")) + test_bad_struct_ops(); +} 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..9c103afbfdb1 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bad_struct_ops.c @@ -0,0 +1,17 @@ +// 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.link") +struct bpf_testmod_ops testmod_1 = { .test_1 = (void *)test_1 }; + +SEC(".struct_ops.link") +struct bpf_testmod_ops2 testmod_2 = { .test_1 = (void *)test_1 }; From patchwork Tue Feb 27 20:45: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: 13574398 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 51076134B1 for ; Tue, 27 Feb 2024 20:46:37 +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=1709066799; cv=none; b=Oo1TJAiCdP5P0X/UvZf38qbeQidYSI3eoq4NLIwhclPckDh8SNhQhM49AJGYpLG6jtU+n4aGIv/FOBhWYAqw8Pvnco3ohKlkVkVUw8Y+yNU1d16eZPQENs3KpuPMSzxg37TNGQ2riRaoiv5ASUt65qO36Gdc4WQ491o1ABVR5ZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709066799; c=relaxed/simple; bh=yx2T0eErEpHQT0ydcArJMHUqBSR4JhQLrDsYp6okSMI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=S7NhmddIxC/FlAZrTkhyHT6tBGsG4CVUM1hvboIjKfNVDPSKNfV6EO55wXe1AsggmFjlc1ME9PHuJZSUMnu3NWLRzzaZS1btXdcldm/MAwBxRCyItK4nrvlDZ63qJX6r7UKeo8Mqhb2uIAmNBVtzC5AZnX+YMXmlqNcyq0jY3o4= 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=k1W1fAG6; 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="k1W1fAG6" Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-a3122b70439so594169666b.3 for ; Tue, 27 Feb 2024 12:46:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709066795; x=1709671595; 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=NufEqf5XNfMeRf8MUUisjrX5VArk6s8sDiv8AT6NiWY=; b=k1W1fAG6KTzc/3BWnE0NkpgnCZg1aANPkJOgLIZdzucH+Gi1XjPwaZB6tnXqa2OSDP aWdrIiNNwnU5nMMqc8BpH548AJ5s/qKBAWzP67nG2JBY845Q7mgBNeramT2o1E8Zz0Yg bqdgKoMY8oL04LIz8Z3mcyFGBIOfORpDMaju8rFHmptYrkkz1p8Kzjv1GSgmSu6mzO9a OMHuAZWoS1CsCfKp8M/5xntn9N+CbZ0UnLbrtTzar4m+ODNhAhJj6jXTY4+LmohF4Uof rW473dmqZK9CvhGf5DbaaoQK5zHru2MoQpog4l5rWIY4rwWGJNqt7maJGDHODtmjxo+a IhVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709066795; x=1709671595; 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=NufEqf5XNfMeRf8MUUisjrX5VArk6s8sDiv8AT6NiWY=; b=IdqYNdRQwOZl1S7PoSp1USweO6egGTrtnfTbOzIaXNQtiKhgyOiN0+s9Nh7srpsrTh 5M9hNku1MDf6kz2mQKuQYUX03vXFphrHfWT6lMebbj27sg1Qy1BTkxvP+QJaNEUbGewJ OteQYyOhxdgKdJ1z9IQYxbZY9aW8nlXReYuBw2R8ptcpejXePdgX3ChtJgQw6oy1FCNr 5xX+ev1k/ldA3w29SVWjzA+jSUqnnagVhPVA8FUuOlbLhVYc1QuukolZkbptcOr1d2Sj OzTGM8iG6mDcRKdlbdvFnIUednm14erTPYIQVaXwY8absPcaFNlrShsNT6uiyohAp08V WgPQ== X-Gm-Message-State: AOJu0YwHcLGqXchPccQgzB9J1Tl92WSikFwzkWH9T1fmronThZ4bazy5 csdY71OovMzxdIjxf4SE43XqXIoDrDh+R6b1Nroaecrw8qPF3aPJrprDU33ZL4I= X-Google-Smtp-Source: AGHT+IET1gJuQtkMad8eJRx9sb6cLMFuB5wTuM2pGqUdXatVwnytzBV94OHnkHc1AWPDWfFm1ck1yQ== X-Received: by 2002:a17:906:48d9:b0:a43:3107:df92 with SMTP id d25-20020a17090648d900b00a433107df92mr5323454ejt.36.1709066795292; Tue, 27 Feb 2024 12:46:35 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id hb13-20020a170906b88d00b00a3d9e6e9983sm1119832ejb.174.2024.02.27.12.46.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 12:46:34 -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, Eduard Zingerman Subject: [PATCH bpf-next v1 6/8] selftests/bpf: test autocreate behavior for struct_ops maps Date: Tue, 27 Feb 2024 22:45:54 +0200 Message-ID: <20240227204556.17524-7-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240227204556.17524-1-eddyz87@gmail.com> References: <20240227204556.17524-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 | 79 +++++++++++++++++++ .../bpf/progs/struct_ops_autocreate.c | 42 ++++++++++ 2 files changed, 121 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..b21b10f94fc2 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include "struct_ops_autocreate.skel.h" + +#define EXPECTED_MSG "libbpf: struct_ops init_kern" + +static libbpf_print_fn_t old_print_cb; +static bool msg_found; + +static int print_cb(enum libbpf_print_level level, const char *fmt, va_list args) +{ + old_print_cb(level, fmt, args); + if (level == LIBBPF_WARN && strncmp(fmt, EXPECTED_MSG, strlen(EXPECTED_MSG)) == 0) + msg_found = true; + + return 0; +} + +static void cant_load_full_object(void) +{ + struct struct_ops_autocreate *skel; + int err; + + old_print_cb = libbpf_set_print(print_cb); + skel = struct_ops_autocreate__open_and_load(); + err = errno; + libbpf_set_print(old_print_cb); + if (!ASSERT_NULL(skel, "struct_ops_autocreate__open_and_load")) + return; + + ASSERT_EQ(err, ENOTSUP, "errno should be ENOTSUP"); + ASSERT_TRUE(msg_found, "expected message"); + + struct_ops_autocreate__destroy(skel); +} + +static void can_load_partial_object(void) +{ + DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts); + struct struct_ops_autocreate *skel; + struct bpf_link *link = NULL; + int err; + + skel = struct_ops_autocreate__open_opts(&opts); + if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open_opts")) + return; + + err = bpf_program__set_autoload(skel->progs.test_2, false); + if (!ASSERT_OK(err, "bpf_program__set_autoload")) + goto cleanup; + + err = bpf_map__set_autocreate(skel->maps.testmod_2, false); + if (!ASSERT_OK(err, "bpf_map__set_autocreate")) + goto cleanup; + + err = struct_ops_autocreate__load(skel); + if (ASSERT_OK(err, "struct_ops_autocreate__load")) + goto cleanup; + + link = bpf_map__attach_struct_ops(skel->maps.testmod_1); + if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) + goto cleanup; + + /* test_1() would be called from bpf_dummy_reg2() in bpf_testmod.c */ + ASSERT_EQ(skel->bss->test_1_result, 42, "test_1_result"); + +cleanup: + bpf_link__destroy(link); + struct_ops_autocreate__destroy(skel); +} + +void serial_test_struct_ops_autocreate(void) +{ + if (test__start_subtest("cant_load_full_object")) + cant_load_full_object(); + if (test__start_subtest("can_load_partial_object")) + can_load_partial_object(); +} diff --git a/tools/testing/selftests/bpf/progs/struct_ops_autocreate.c b/tools/testing/selftests/bpf/progs/struct_ops_autocreate.c new file mode 100644 index 000000000000..294d48bb8e3c --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_autocreate.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +int test_1_result = 0; + +SEC("struct_ops/test_1") +int BPF_PROG(test_1) +{ + test_1_result = 42; + return 0; +} + +SEC("struct_ops/test_1") +int BPF_PROG(test_2) +{ + return 0; +} + +struct bpf_testmod_ops___v1 { + int (*test_1)(void); +}; + +struct bpf_testmod_ops___v2 { + int (*test_1)(void); + int (*does_not_exist)(void); +}; + +SEC(".struct_ops.link") +struct bpf_testmod_ops___v1 testmod_1 = { + .test_1 = (void *)test_1 +}; + +SEC(".struct_ops.link") +struct bpf_testmod_ops___v2 testmod_2 = { + .test_1 = (void *)test_1, + .does_not_exist = (void *)test_2 +}; From patchwork Tue Feb 27 20:45: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: 13574399 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.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 466D814EFDE for ; Tue, 27 Feb 2024 20:46:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709066800; cv=none; b=gN6aafW3+/BzTb9T7iuZqiazt9DZe2EAHQCPCsLuxZQS1pJ3pPqxX20iskGMCcuBsgga2vxq5ziw195y6lChdhMfR1wFLyBMr5mp2ZBhH82FBxmNXtTOdjvqB7CrPreD/cXWXfPsmV5fSr4CLDjw/irYOucBewxHoZAIiqPH+mU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709066800; c=relaxed/simple; bh=U6gtvYNVM1cbFEXOzsGpGLs2llhqqH0x3g5sCyrQbdI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LOVb4XEDp9KHGdbJL11dr2MKCRapzQUPniE0q0BTBynClxx860zJeFjnZQGR4tXLz74Fj1bbZC22JqIpOJtehSyG6o2QHve7znx26+sc30u5ALEohzmzkqesx08bXmvNZd0qrOAQS8sH7LaTACEY7xz7lMVBKYo4vUTJWpLU9+A= 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=i6dMF259; arc=none smtp.client-ip=209.85.208.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="i6dMF259" Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-55a035669d5so8398303a12.2 for ; Tue, 27 Feb 2024 12:46:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709066796; x=1709671596; 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=Nv9d4uG3mkntgWZefbffEpIEK7nJX3evMpRPaACsep0=; b=i6dMF2590hPymMgnuxhcF0Ib0t1GnUfkKqtoIGpH8YtLEefm2LD5rbMVH/3yIVhJGj r/DpMeMGGH2GmatZdqlmlA8R564rgoAAADuJxIvlY6if5GHYXaZxgWOe6qEUlq9r20aP 7Y5Mr1CRMPTuCwQX40x2rI0cE/6VvC+QHQMO79LppdtTFQTigc/PZvWDpTl+y8nrncza LM5DVS+BBgYWQEVwjJgZhbDigEAheKmFEfhuFXlBYzNc9aO5Z4IojaAAX+7hEEpz/vnZ DTd5u80W4Ql9mceitUSx/davxNxRi6XywGMu6CzWBKo2cnPUNtrI2myH3vF7lJrh5xVQ NnHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709066796; x=1709671596; 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=Nv9d4uG3mkntgWZefbffEpIEK7nJX3evMpRPaACsep0=; b=BzoAsDA1MLAs/dKI4fhHZ7ZWgljFKKyQWZCB8h1r4T2tjKlRm7ibEih0mHv+vjYyVJ JYzyJE6ikSQgiP4vac03CmGynrI+64POixXLU6703iUgnWs5ZqHLzpanIRxQ9XSeTMch 9kz3FS2M+uCqGgql4jlbVo7E1tTiI6y6EbH+kIsX0xt2NeyV6Wxx4Kr6VbEONlRuzB0K nKin+Jjhy9gLT8stJ/z7dCJRc5RDrBujZ5J2ABwLjA7vR7uTIudJmIg+D5ZQZdr2E1kZ HRtMMGmhard0kFiLXMpN2WPwk/fmCJxvS4Q9x58ngf5UPsTWwWuuAFTLyXQEPRxA4ahf kY0A== X-Gm-Message-State: AOJu0YzmPKWh3wB309Y2elm8ssXhAJ5FlitBuw5k+XKG1Lg1NOZLvfTi pafqRCmX7CpoxuzpuzRbf1h1s4Is3rPD4JnWbuZygUvVCRrT+p/hkcaMhfCYtI8= X-Google-Smtp-Source: AGHT+IGTTucL+N3lMG+iwPuaEto0NAqTU+Qdh61T3rvUHyYIMrQz8rfj24a8mEh3xCT7gQRrwglZoQ== X-Received: by 2002:a17:906:a3cc:b0:a43:3d07:803a with SMTP id ca12-20020a170906a3cc00b00a433d07803amr5209054ejb.54.1709066796363; Tue, 27 Feb 2024 12:46:36 -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 hb13-20020a170906b88d00b00a3d9e6e9983sm1119832ejb.174.2024.02.27.12.46.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 12:46:36 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, Eduard Zingerman Subject: [PATCH bpf-next v1 7/8] libbpf: sync progs autoload with maps autocreate for struct_ops maps Date: Tue, 27 Feb 2024 22:45:55 +0200 Message-ID: <20240227204556.17524-8-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240227204556.17524-1-eddyz87@gmail.com> References: <20240227204556.17524-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 Make bpf_map__set_autocreate() for struct_ops maps toggle autoload state for referenced programs. 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'. Do not apply such toggling if program autoload state is set by a call to bpf_program__set_autoload(). Signed-off-by: Eduard Zingerman --- tools/lib/bpf/libbpf.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index b39d3f2898a1..1ea3046724f8 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -446,13 +446,18 @@ struct bpf_program { struct bpf_object *obj; int fd; - bool autoload; + bool autoload:1; + bool autoload_user_set:1; bool autoattach; bool sym_global; bool mark_btf_static; enum bpf_prog_type type; enum bpf_attach_type expected_attach_type; int exception_cb_idx; + /* total number of struct_ops maps with autocreate == true + * that reference this program + */ + __u32 struct_ops_refs; int prog_ifindex; __u32 attach_btf_obj_fd; @@ -4509,6 +4514,28 @@ static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info) return 0; } +/* Sync autoload and autocreate state between struct_ops map and + * referenced programs. + */ +static void bpf_map__struct_ops_toggle_progs_autoload(struct bpf_map *map, bool autocreate) +{ + struct bpf_program *prog; + int i; + + for (i = 0; i < btf_vlen(map->st_ops->type); ++i) { + prog = map->st_ops->progs[i]; + + if (!prog || prog->autoload_user_set) + continue; + + if (autocreate) + prog->struct_ops_refs++; + else + prog->struct_ops_refs--; + prog->autoload = prog->struct_ops_refs != 0; + } +} + bool bpf_map__autocreate(const struct bpf_map *map) { return map->autocreate; @@ -4519,6 +4546,9 @@ int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate) if (map->obj->loaded) return libbpf_err(-EBUSY); + if (map->st_ops && map->autocreate != autocreate) + bpf_map__struct_ops_toggle_progs_autoload(map, autocreate); + map->autocreate = autocreate; return 0; } @@ -8801,6 +8831,7 @@ int bpf_program__set_autoload(struct bpf_program *prog, bool autoload) return libbpf_err(-EINVAL); prog->autoload = autoload; + prog->autoload_user_set = 1; return 0; } @@ -9428,6 +9459,8 @@ static int bpf_object__collect_st_ops_relos(struct bpf_object *obj, return -EINVAL; } + if (map->autocreate) + prog->struct_ops_refs++; st_ops->progs[member_idx] = prog; } From patchwork Tue Feb 27 20:45: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: 13574400 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 482C214EFE3 for ; Tue, 27 Feb 2024 20:46:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709066801; cv=none; b=I/8Fygy8GW3QvLq1OzxJA3KRwSuy3PgzYjgfB483FJzXZQfkR45D14Q5LYG7PEd6dBcyGYxs/R40zLx29ZJffRs3igGQgKMQ98b5wX75jwYFgvbZDDTqN3Jqe0u9VSr59xx9Lerf1GsuqsHpcbxmPC1ty8gpd9xNZ1B8nafaHHQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709066801; c=relaxed/simple; bh=AUauZGL5+/rh5GG4zk54y7kpunQscNTEQCjLu4uOP+4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UHTrE+Eb21hKvaRHvXnsidNMw66GYm60SP1oNQZ2yh35g79YhiIz0a3MFKJIuWzDD3b8R4IT14q/3rF0ye9y59gDiZM/PKRotjJS11B/0T/5Q23QdsKgLJlL/09jXRGwPIPm6xpGI27PlxzVl14r9lLfOncqseruRwkzGq5J3Fw= 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=RfZ7P4A3; arc=none smtp.client-ip=209.85.208.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RfZ7P4A3" Received: by mail-ed1-f44.google.com with SMTP id 4fb4d7f45d1cf-565b434f90aso4187469a12.3 for ; Tue, 27 Feb 2024 12:46:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709066797; x=1709671597; 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=liH9xyX1tymvM6hgRgnjazV5iOyyvCduV1ZAdTONLmQ=; b=RfZ7P4A3UKKUr5zV+l0kcDraoJHTNHQoex2wyuaw57iC8YmkD3UVJO3EcYP9sfxRE1 c7WwtEVhqmq+rArP7fN6wbukD86XLKRJssTh8SN8HosHWH8YfhVn9kk3eBadwz2bwlkK JaSqStFCMudHaX7+S6kKlgjWgHuTo8ds32YFYajjol0/r9SC99EuoNSUgAf26AsSTVNK JA6ysMj5cQIiOVcvWndaBhtH1N2Zl28nBAxIvl5ISD3iulFltWDTIRyFDsTGDeTuIkyt DUA1p3vO/PsYWsiLkXd/X2K7f7chRlBFRG8XfuOayPIlprsGkXRMiCo+J3JtUsxtE/lb 1bpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709066797; x=1709671597; 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=liH9xyX1tymvM6hgRgnjazV5iOyyvCduV1ZAdTONLmQ=; b=nJk20jV6KMu7Puy+mGutXQV39PzG5TI4y+w4YjKolUiymKZRRHOQogvEVzIjhHdnMv xdOva0WxQd8OSEY3S8HAI5uVp3l5tgXIrbFaWLdtcPo9cldkYLKsiMwv7lm77Gz5STB+ 2DMZkfVj2Ez/N77/FjsOAJDHBO1pTV3T0mEhCaEAOy+fv6knLEU3xrkleMWcAV2cjXmW 5d5y/SueTmqTnZEK5MPT4oG4b+9UVTo7U5JnhskMbuDYSWJ5cNVyB4+ny+x6ck0kP/Ch o14p20HRUctgHe+jwSRxf4ytT8JwxmTete/hfnBXNTfwSoF4KoyunGyaRn9KJz26FZqB Duhg== X-Gm-Message-State: AOJu0YyjmsaRwVeOjvnQ/hQdznNiJC1a4lNIFkNexKxj0HVgOS3COm+Z OcgKlFzJu6OyolMK0AYJbFjpYY7ZoE3Rsz6uId1LLKat8+vuG30zVml/Ofcg6Lg= X-Google-Smtp-Source: AGHT+IErzoO/LnaIc4qmRfJzZUcsWM01YcbOQiB+OeyYsGD/f7xXLl4ZI7eJxX8pMiwEPh8q07X/tw== X-Received: by 2002:a17:906:565a:b0:a3e:6a25:2603 with SMTP id v26-20020a170906565a00b00a3e6a252603mr7837736ejr.33.1709066797437; Tue, 27 Feb 2024 12:46:37 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id hb13-20020a170906b88d00b00a3d9e6e9983sm1119832ejb.174.2024.02.27.12.46.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 12:46:37 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, void@manifault.com, Eduard Zingerman Subject: [PATCH bpf-next v1 8/8] selftests/bpf: tests for struct_ops autoload/autocreate toggling Date: Tue, 27 Feb 2024 22:45:56 +0200 Message-ID: <20240227204556.17524-9-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240227204556.17524-1-eddyz87@gmail.com> References: <20240227204556.17524-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 Verify automatic interaction between struct_ops map autocreate flag and struct_ops programs autoload flags. Signed-off-by: Eduard Zingerman --- .../bpf/prog_tests/struct_ops_autocreate.c | 65 +++++++++++++++++-- 1 file changed, 61 insertions(+), 4 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 b21b10f94fc2..ace296aae8c4 100644 --- a/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c +++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c @@ -46,10 +46,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; @@ -70,8 +66,69 @@ static void can_load_partial_object(void) struct_ops_autocreate__destroy(skel); } +static void autoload_toggles(void) +{ + DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts); + struct bpf_map *testmod_1, *testmod_2; + struct bpf_program *test_1, *test_2; + struct struct_ops_autocreate *skel; + + skel = struct_ops_autocreate__open_opts(&opts); + if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open_opts")) + return; + + testmod_1 = skel->maps.testmod_1; + testmod_2 = skel->maps.testmod_2; + test_1 = skel->progs.test_1; + test_2 = skel->progs.test_2; + + /* testmod_1 on, testmod_2 on */ + ASSERT_TRUE(bpf_program__autoload(test_1), "autoload(test_1) #1"); + ASSERT_TRUE(bpf_program__autoload(test_2), "autoload(test_2) #1"); + + /* testmod_1 off, testmod_2 on */ + bpf_map__set_autocreate(testmod_1, false); + ASSERT_TRUE(bpf_program__autoload(test_1), "autoload(test_1) #2"); + ASSERT_TRUE(bpf_program__autoload(test_2), "autoload(test_2) #2"); + + /* testmod_1 off, testmod_2 off, + * setting same state several times should not confuse internal state. + */ + bpf_map__set_autocreate(testmod_2, false); + bpf_map__set_autocreate(testmod_2, false); + ASSERT_FALSE(bpf_program__autoload(test_1), "autoload(test_1) #3"); + ASSERT_FALSE(bpf_program__autoload(test_2), "autoload(test_2) #3"); + + /* testmod_1 on, testmod_2 off */ + bpf_map__set_autocreate(testmod_1, true); + bpf_map__set_autocreate(testmod_1, true); + ASSERT_TRUE(bpf_program__autoload(test_1), "autoload(test_1) #4"); + ASSERT_FALSE(bpf_program__autoload(test_2), "autoload(test_2) #4"); + + /* testmod_1 on, testmod_2 on */ + bpf_map__set_autocreate(testmod_2, true); + ASSERT_TRUE(bpf_program__autoload(test_1), "autoload(test_1) #5"); + ASSERT_TRUE(bpf_program__autoload(test_2), "autoload(test_2) #5"); + + /* testmod_1 on, testmod_2 off */ + bpf_map__set_autocreate(testmod_2, false); + ASSERT_TRUE(bpf_program__autoload(test_1), "autoload(test_1) #6"); + ASSERT_FALSE(bpf_program__autoload(test_2), "autoload(test_2) #6"); + + /* setting autoload manually overrides automatic toggling */ + bpf_program__set_autoload(test_2, false); + /* testmod_1 on, testmod_2 off */ + bpf_map__set_autocreate(testmod_2, true); + ASSERT_TRUE(bpf_program__autoload(test_1), "autoload(test_1) #7"); + ASSERT_FALSE(bpf_program__autoload(test_2), "autoload(test_2) #7"); + + struct_ops_autocreate__destroy(skel); +} + void serial_test_struct_ops_autocreate(void) { + if (test__start_subtest("autoload_toggles")) + autoload_toggles(); if (test__start_subtest("cant_load_full_object")) cant_load_full_object(); if (test__start_subtest("can_load_partial_object"))