From patchwork Wed Feb 21 01:23:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13564836 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C5DF5684 for ; Wed, 21 Feb 2024 01:23:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708478627; cv=none; b=I0sHGOD7ng2btY7AQkhDUPHLQIbtMUsfUPrHVwA0htJzE3PyGFgCwTyB/XhaZmGuZsncRJb+m9qQ8YXo4hgo4PGj7et/EyQspP1tA0ui/BhQffrS9bPGCQVYCtfC+Xzaa0HnYYr58giNcRVELintUhmLwI8FZoLG8kJrFuudLNA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708478627; c=relaxed/simple; bh=XxyBx5VwFWqnv4jib9/VNRcbgnP0Uj3thPHnnZ7MGd0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=D3n39DHgeng2+UtIthrNAhtFh+VXq/2szuDhMYwP5Ikp7v6F7cDChNaIlYIyR3GKKmDoUMT4ubAV/2L0FiL1+C30Fu7WaAft+FtA3lj8ILWScEy6ytdqqYUzgklbcTcPvp0Fm0ws7aLgi5df//ue8rTGDzpPBaZU0F9HMJiP0kI= 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=mJxFJd5n; arc=none smtp.client-ip=209.85.128.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mJxFJd5n" Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-607d8506099so59700667b3.0 for ; Tue, 20 Feb 2024 17:23:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708478624; x=1709083424; 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=iQoqmUR5lihQSWPqil2BNYOwKpcS7L64zovRpaQOkag=; b=mJxFJd5npgu16n2ohyeEiTUhMuEQt/zj58hvcCBUTD3v/LYHbKvXZ83uWWUkKvUpf8 E5qHIsATUjxF0AgK8tQbA6jig33YZyBlkCxUG1zUZtx0gUwwCnTUDfINQkWXSTSyvDYO eGWikFy/odNQIZSrnJuVVKWSdsMCUsDpPoTiAjfJIVPRNf6qr7AHBWYaz8lSyzoGUmoU mraWaNTMNGhM2t/8CJvwHL/ZrhPFBCnzU5lL87/mYAs0TZplBHMIR1/c4ID7o6/YYWSi xDDkVMzA+qwArqiKLmhjtVFZuDgLKSNggMJ3E4ub8l6Mycskt+CO9YpJecLGrutjbhtA K3Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708478624; x=1709083424; 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=iQoqmUR5lihQSWPqil2BNYOwKpcS7L64zovRpaQOkag=; b=oMnlSXgtgbyc/yxhv/UEMjE1PxOtZCjzbSSOT+qgLtuEbT2bZldwINyTikUtvvJxjJ KQmOyTzLGwPvAb6Wo/hypeTUM+hJK66Z3Rh0M1VKNt6NFBiE7JbdGYSpYjbX/eFydG83 0bULvO40/Bk39zaZ5gs0RY2LSkkARaoeIJUa2pxBfT5LHMZr1tBzxK4HioRbU+ZQwWCD 7EsbqdYMz2XfoYL+nDwhRUMFH9cIhsp4JGTK8UH5zN2gqXnPgiklhk1WZqLXxQM3HZKr DyqHKFbEW/W+zWqGBvioZVs4bK0RGA25F/Zu8vW8n7kDeerQ4H1cfc73Uw7p8iSFZOmr cPoA== X-Gm-Message-State: AOJu0YziC1KOBYqhBFw3n5XZ1ViBWoPaZha9gS6mdLKPkJ6U6hOH93uL VgKiMmm7tJDpFua/HA5p1p0puRw47/cxyMdP+M2/tiRD0NE17UXB9M6n+GRW X-Google-Smtp-Source: AGHT+IENKjQx8fkr9A4SJtojsITDvDbzgcw6743mPmTnnTZSr7sNwmelF7rnqAw74DUvKlE+E7r58w== X-Received: by 2002:a81:4317:0:b0:608:7af2:f5f5 with SMTP id q23-20020a814317000000b006087af2f5f5mr910378ywa.50.1708478624505; Tue, 20 Feb 2024 17:23:44 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:26eb:2942:8151:a089]) by smtp.gmail.com with ESMTPSA id j64-20020a0de043000000b00607ef065781sm2396801ywe.138.2024.02.20.17.23.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 17:23:44 -0800 (PST) From: thinker.li@gmail.com To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v3 1/5] libbpf: expose resolve_func_ptr() through libbpf_internal.h. Date: Tue, 20 Feb 2024 17:23:25 -0800 Message-Id: <20240221012329.1387275-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240221012329.1387275-1-thinker.li@gmail.com> References: <20240221012329.1387275-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Kui-Feng Lee bpftool is going to reuse this helper function to support shadow types of struct_ops maps. Signed-off-by: Kui-Feng Lee --- tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf_internal.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 01f407591a92..ef8fd20f33ca 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -2145,7 +2145,7 @@ skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id) return t; } -static const struct btf_type * +const struct btf_type * resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id) { const struct btf_type *t; diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index ad936ac5e639..aec6d57fe5d1 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -234,6 +234,7 @@ struct btf_type; struct btf_type *btf_type_by_id(const struct btf *btf, __u32 type_id); const char *btf_kind_str(const struct btf_type *t); const struct btf_type *skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id); +const struct btf_type *resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id); static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t) { From patchwork Wed Feb 21 01:23:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13564837 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C4F431FD7 for ; Wed, 21 Feb 2024 01:23:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708478628; cv=none; b=EW+X51IqigCp4bf7ahix7UDVbYNbKHwpi99wesc4nuevr2nB9wukrFNeVDNkJHO2Jt5j82WvQqeLr1GTwyTwB51pszLVKTjCmYHo2WSRhBuSRSLSH/Hxs5qcRB7OEMZm/BkTMMO8LkSliKaqeJzY3hCxGqsCgiIkuzPWIXTVRRs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708478628; c=relaxed/simple; bh=9pgDJRa9T/5a4i1vhMmQqOlMwwmbYYmxCovw23ewryU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=f9hBvoesslUk7sNTFXVEkGqgfQ33vPuuLYaX2tponQeDa/7Gj1eRGhNF8fqlYH6o5V7iun7oHTiqnrxLHgCKVhyNKvq0iZApMg9KY4GvJx86v3R1CHZWHjq6296RUUAhcwYVMg/k8wLqnT5oSkiAIYYwWCTfnqmQqe5FYR8n9hM= 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=c9E4aZ5v; arc=none smtp.client-ip=209.85.128.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="c9E4aZ5v" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-607f8482b88so45271707b3.0 for ; Tue, 20 Feb 2024 17:23:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708478625; x=1709083425; 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=pBHML36fUv9H+qGSqsoz0y17RtRNnl1DXEIzsGFdTTM=; b=c9E4aZ5vWCtNnWZXa6DTNzwzMzECae7nynxp54mh0V3ipMP94nTQqr+SGZZc4I9Hjc /Wqycz6iSxjIOCGramoA2nf/RH0xSydN8vcqy4jOMnJYgAVJjqXl9n0biSSnFsI40NGW UA9dfh8+FZUJ+ipNUAowmAAFLvmSq+Uj6HSVvgyc6G5s4nRVA6hSUPKQidzR+Dj+Tlly hJNxznPHmM5rqCZGR7TKxaDcfDZgT1+NyMwA+Fc9ahPxMxMvxMpQKm+AK/jnQuCsBlmw 94ooBiE7huBzrjDxBx0qBGXlg2Xodz+n1kk/yY7lOzIIj+jGZ6iKUatn30a8/tz3NZRG zwUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708478625; x=1709083425; 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=pBHML36fUv9H+qGSqsoz0y17RtRNnl1DXEIzsGFdTTM=; b=qIPbRH5Y22S5K4vDAhSg6ZJlzQNDo2LEK9R50x5JYrCfQAf5hK5Wr6Mm3nuzGqdvPs khS010Z6sQ2a4arcsjUpV+X5kyJ2vgDXE02zmwa4bxvn+aZ1og5hdmT5oc9k+MyIC/RZ 2WKLpeVbL932Xuz622RNcJPmn5sVPvVQy2qxd+hYPf2onNDqg/eEytS7UUwvWyV/yn+5 vmeNE9xkwDNi/P8hLo8yEvQzmgBM2NioC+vFKMB08bLWHjzK7HlyKKG2iOxh4DRQfOK/ YmBKvv/Jwe9BQ3YbEBVR/lVvEf+R3obT7iO+OmTTfs/nDK8wMS89Qz3uhmGey1vidAHs Aj3A== X-Gm-Message-State: AOJu0Yz/96h9w28tKxhxJwkveP7HkJ7eUXxO7Y76JD0BbRApqERjoVEL T8qwt6nhYF2wDXBrIuVM9DiBCF2KA/XQaQ4WqX8fv7OlqBeKJcDVlBlecqLW X-Google-Smtp-Source: AGHT+IFki79F+jMELFfsj0JKNlTl9aJH04+gus6x6tXQkCEDvozqWlSnLbBPK6WNyii8mLSItanJxg== X-Received: by 2002:a81:c252:0:b0:607:9e4b:f0e9 with SMTP id t18-20020a81c252000000b006079e4bf0e9mr17768394ywg.31.1708478625575; Tue, 20 Feb 2024 17:23:45 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:26eb:2942:8151:a089]) by smtp.gmail.com with ESMTPSA id j64-20020a0de043000000b00607ef065781sm2396801ywe.138.2024.02.20.17.23.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 17:23:45 -0800 (PST) From: thinker.li@gmail.com To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v3 2/5] libbpf: set btf_value_type_id of struct bpf_map for struct_ops. Date: Tue, 20 Feb 2024 17:23:26 -0800 Message-Id: <20240221012329.1387275-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240221012329.1387275-1-thinker.li@gmail.com> References: <20240221012329.1387275-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Kui-Feng Lee For a struct_ops map, btf_value_type_id is the type ID of it's struct type. This value is required by bpftool to generate skeleton including pointers of shadow types. The code generator gets the type ID from bpf_map__btf_vaule_type_id() in order to get the type information of the struct type of a map. Signed-off-by: Kui-Feng Lee --- tools/lib/bpf/libbpf.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index ef8fd20f33ca..465b50235a01 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1229,6 +1229,7 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, map->name = strdup(var_name); if (!map->name) return -ENOMEM; + map->btf_value_type_id = type_id; map->def.type = BPF_MAP_TYPE_STRUCT_OPS; map->def.key_size = sizeof(int); @@ -4818,7 +4819,9 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b if (obj->btf && btf__fd(obj->btf) >= 0) { create_attr.btf_fd = btf__fd(obj->btf); create_attr.btf_key_type_id = map->btf_key_type_id; - create_attr.btf_value_type_id = map->btf_value_type_id; + create_attr.btf_value_type_id = + def->type != BPF_MAP_TYPE_STRUCT_OPS ? + map->btf_value_type_id : 0; } if (bpf_map_type__is_map_in_map(def->type)) { From patchwork Wed Feb 21 01:23:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13564838 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2514F443E for ; Wed, 21 Feb 2024 01:23:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708478629; cv=none; b=HOh61Mx8iVWle0kO43KJph019wXiiOLSzW/FonP72Q5eJw0gfNQ5xCW9Ma3/ZOiclwqpQawBvZ2+HyxqkZZroA3Zr1LVh5905dFOpzDHiFaH5eDlusLVtmbxzQaw1XPNBNxsfUur0SurtwVDt78VXJpBtr544Ru90Hos054vjb0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708478629; c=relaxed/simple; bh=JYlor7liByBS2LlEYz/edKDIPn+GA8eleujRxigKJPQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Hwv1zlafjidARfk54tZruqJDu07T8hVzmqsWkGWTgn00b5obZ71Rlt3xqBEev+PfigmSNwb2CIQ9Ve582DyC0rS/VFn3CKApyx+MXq4C1SBRdmnVNiejH+7sejzwCsCcvIL521D2UcnZV2UE/6FzRs2BV6MAUv2o3zt4sYCUksc= 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=Kq3HXVDD; arc=none smtp.client-ip=209.85.128.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Kq3HXVDD" Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-607f8894550so612187b3.1 for ; Tue, 20 Feb 2024 17:23:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708478627; x=1709083427; 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=AtHDSZGnVRHo3SsKsHaqf43fpvkFwz2UT1hdwLb4lMI=; b=Kq3HXVDDtP5PFVM//F6j9D9Mm3JkWNM6a37OXKRHfAg+EqD4y6wfW8VH/ZTVR+v78a ytN6sFmhV3UAWUJhNGo4myet0zvNS2fdevX5FoRAW9HiMBXR/5LlqE4mPQSe4hl7Pl82 /XpFsU0Zak/btTcsyizSuTCGQ0AveyS0/uA12Z7fFtw9TlNcF018bGSNtPdu1Ao6iRW/ IqbQHvXByLxSv90cpO7MUPbZxZGqJGpj0PRarJMaZEOhXEC21ie0EE1Hy0kViK+4sGj1 RbPCumkwnU5I3f40G2sK7PXFTQ5fvT0ie+Nk11a+mnLIcoLrINg2tG38S4eQonP7W9vs TP5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708478627; x=1709083427; 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=AtHDSZGnVRHo3SsKsHaqf43fpvkFwz2UT1hdwLb4lMI=; b=Hz6oOAZI1/4NUco+AYweZKHkHGCENzASLvZ8E8dO2kDOkc8xOeM0A6JMO9eVkfSMtF HdV5X0wxqJmJsBDaDE+KNNsEfQ6jpHCimjwXr9B+NJDJ7cItng6itwWP4AXQzd0KZJYN i8dF1LVLBe/Cem/ABvEeef5dzcAXrxUExLrRtg+H86wlsd8AqTVOEzJYDDSu6AO623Hs SXBTnB8znFHLi0DsP08WwEomZDZDkPsGVsIVZryOmZnk9mp6eAwrHBxopd89gW6B/E40 eZB/K4iheBPybbuASrRHtqBXxvysheuUGHSwD5Xzy8eLsOqoAmOJBnbTau8RvKP4lGLu dC2g== X-Gm-Message-State: AOJu0Yw+J1YoOnZ6hViLy50aHbgMPRL2LNEIH/AHBAlFmZ7IT4ho08iD CMQ/NqZB6Wa/TIpB9RW+03EyTrvkB2SKCWdOwD++Y2keKg4Ppd3zfmF8i8KU X-Google-Smtp-Source: AGHT+IGG+KeAjxFotUr0bjjFkGRXsl7zhQ5bIb9hgUkQjMH3wn9NhRC9qAzaavVNyVX0MBs3t0i5Og== X-Received: by 2002:a81:b243:0:b0:5fb:da77:af07 with SMTP id q64-20020a81b243000000b005fbda77af07mr15135691ywh.32.1708478626649; Tue, 20 Feb 2024 17:23:46 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:26eb:2942:8151:a089]) by smtp.gmail.com with ESMTPSA id j64-20020a0de043000000b00607ef065781sm2396801ywe.138.2024.02.20.17.23.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 17:23:46 -0800 (PST) From: thinker.li@gmail.com To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v3 3/5] libbpf: Convert st_ops->data to shadow type. Date: Tue, 20 Feb 2024 17:23:27 -0800 Message-Id: <20240221012329.1387275-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240221012329.1387275-1-thinker.li@gmail.com> References: <20240221012329.1387275-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Kui-Feng Lee Convert st_ops->data to the shadow type of the struct_ops map. The shadow type of a struct_ops type is a variant of the original struct type providing a way to access/change the values in the maps of the struct_ops type. bpf_map__initial_value() will return st_ops->data for struct_ops types. The skeleton is going to use it as the pointer to the shadow type of the original struct type. One of the main differences between the original struct type and the shadow type is that all function pointers of the shadow type are converted to pointers of struct bpf_program. Users can replace these bpf_program pointers with other BPF programs. The st_ops->progs[] will be updated before updating the value of a map to reflect the changes made by users. Signed-off-by: Kui-Feng Lee --- tools/lib/bpf/libbpf.c | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 465b50235a01..becbb4d81012 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1102,6 +1102,9 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map) if (btf_is_ptr(mtype)) { struct bpf_program *prog; + /* Update the value from the shadow type */ + st_ops->progs[i] = *(struct bpf_program **)mdata; + prog = st_ops->progs[i]; if (!prog) continue; @@ -1172,6 +1175,36 @@ static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj) return 0; } +/* Convert the data of a struct_ops map to shadow type. + * + * The function pointers are replaced with the pointers of bpf_program in + * st_ops->progs[]. + */ +static void struct_ops_convert_shadow(struct bpf_map *map, + const struct btf_type *t) +{ + struct btf *btf = map->obj->btf; + struct bpf_struct_ops *st_ops = map->st_ops; + const struct btf_member *m; + const struct btf_type *mtype; + char *data; + int i; + + data = st_ops->data; + + for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) { + mtype = skip_mods_and_typedefs(btf, m->type, NULL); + + if (btf_kind(mtype) != BTF_KIND_PTR) + continue; + if (!resolve_func_ptr(btf, m->type, NULL)) + continue; + + *((struct bpf_program **)(data + m->offset / 8)) = + st_ops->progs[i]; + } +} + static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, int shndx, Elf_Data *data, __u32 map_flags) { @@ -7531,6 +7564,19 @@ static int bpf_object_init_progs(struct bpf_object *obj, const struct bpf_object return 0; } +/* Convert the data to the shadow type for each struct_ops map. */ +static void bpf_object__init_shadow(struct bpf_object *obj) +{ + struct bpf_map *map; + + bpf_object__for_each_map(map, obj) { + if (!bpf_map__is_struct_ops(map)) + continue; + + struct_ops_convert_shadow(map, map->st_ops->type); + } +} + static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, size_t obj_buf_sz, const struct bpf_object_open_opts *opts) { @@ -7631,6 +7677,7 @@ static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, goto out; bpf_object__elf_finish(obj); + bpf_object__init_shadow(obj); return obj; out: @@ -9880,6 +9927,12 @@ int bpf_map__set_initial_value(struct bpf_map *map, void *bpf_map__initial_value(struct bpf_map *map, size_t *psize) { + if (bpf_map__is_struct_ops(map)) { + if (psize) + *psize = map->def.value_size; + return map->st_ops->data; + } + if (!map->mmaped) return NULL; *psize = map->def.value_size; From patchwork Wed Feb 21 01:23:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13564839 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (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 6259C5CA1 for ; Wed, 21 Feb 2024 01:23:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708478631; cv=none; b=UwpJl9SbpfHIUs3ayOL4w3lwaxLzTmJXm9NdcRttfPSseb8jxwbWPh8XLZX5ALs5OjPCrU92bjMmgmAwm8iQxb/I4pmqrA+3sBGq0886BKp1HZ5mLPqg1eF8Mfzk8ZTK44mCMXHeISa5rcWJ2yyamue/Up9LE/+HZhkf3Ej+jmk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708478631; c=relaxed/simple; bh=Xv4CVaqRkAFvqZOASOMHn4WOwSMEzcm5NfGseX4zwXg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JLBdcyK4LXXZYBhqXpMeW5Ia42vKm+Y75rguq7wuAgfda/lf5q3Z7ls9QAVFczmmB8qa1ifyz6LVUqW59r9lNtws0qGwkgYUq+MONpLv75JSaGzp31biPrrnusJEaJF9SA7951TyteV59vtVzleuEw76rnnO/cSj6U2xD8i1xgE= 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=QWrZ+T8q; arc=none smtp.client-ip=209.85.128.180 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="QWrZ+T8q" Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-6084165494eso24193207b3.1 for ; Tue, 20 Feb 2024 17:23:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708478628; x=1709083428; 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=wQ4yvjmbfK9gTsHxM3zdQuAaDyQDXt43A+y9/rdVBDE=; b=QWrZ+T8qDcauJ/OZ/ou1mM/6hliPcfYnnrky4uuh2IzaLZmZhJyOO1zd1ZYebDrFfH c56mocaJGhgY+8tjPpNJcYiXf5fINm/Y5WUbVeGP60WZEZFBRGNN6wIxy5LfKymwSyap M5BeE0q10ox7TintEIgvMZXjxaTShvYGPrtESb7xxiGLq/d0+SySeHStViADoDfXu2fs 2Jdwl1GMogftwSRKzmy9liWlsQARnqGnyQdxO2cuVhTm1LS6/2bTEcnyHwI+Km9+P0LN 3InuwGku0EQ/HlvU6Zo6GWSurm9VQbUga+ZMkEqWgaGrbH0HGBnti7PjtPPkBw0Rgw7L nBFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708478628; x=1709083428; 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=wQ4yvjmbfK9gTsHxM3zdQuAaDyQDXt43A+y9/rdVBDE=; b=GdE/B7eR5a6IfKpw1fLOTz6uYkHBpdYdH86Ej8trnlgFrKoa4SHkV/eh9MIjN3XH0r tiX2Ao9IBS1ApqAVnBe/sAUmsBAspsGOfsCapVZBMOrc2MxUMxSfyTGFk84hsw43yr7w MyBCEhWZOxuEgZgIaV/1EI973JUAjthpaQiEWI3tYYMP4htISA/74jfBehL+tnPItQgH kv/mY5INCfWPoamMhZjDVLpB6cRoEaYKFATxRPHs7lfxSaZ77m8I66s2qdVjjXJ3dmBx O8NdijSHlIYo3Ntd+utlequ7iucwDHJGCFPm7ufxZchkVw79z+s9Susc9/aiFQAoaTYh vuYQ== X-Gm-Message-State: AOJu0Yx9hLTSMtnw45L2yuoQJCljsalm77xe27EpEPVp0H0pJHzYYT6U dOrLYNyR2KPfyGgPTt6lvHJ3R8lf1+LaQgZvIUVKI2jscbk30X4XQnsp2j0d X-Google-Smtp-Source: AGHT+IEn3n9A+4+SyThXQtqrhHqoPw7cZb2A5whl8Ko2ZiKFIK24f/tk81zLJMqvCa2zgnrQT4D2jQ== X-Received: by 2002:a05:690c:82e:b0:607:9e7e:7d02 with SMTP id by14-20020a05690c082e00b006079e7e7d02mr16117667ywb.35.1708478627751; Tue, 20 Feb 2024 17:23:47 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:26eb:2942:8151:a089]) by smtp.gmail.com with ESMTPSA id j64-20020a0de043000000b00607ef065781sm2396801ywe.138.2024.02.20.17.23.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 17:23:47 -0800 (PST) From: thinker.li@gmail.com To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v3 4/5] bpftool: generated shadow variables for struct_ops maps. Date: Tue, 20 Feb 2024 17:23:28 -0800 Message-Id: <20240221012329.1387275-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240221012329.1387275-1-thinker.li@gmail.com> References: <20240221012329.1387275-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Kui-Feng Lee Declares and defines a pointer of the shadow type for each struct_ops map. The code generator will create an anonymous struct type as the shadow type for each struct_ops map. The shadow type is translated from the original struct type of the map. The user of the skeleton use pointers of them to access the values of struct_ops maps. However, shadow types only supports certain types of fields, such as scalar types and function pointers. Any fields of unsupported types are translated into an array of characters to occupy the space of the original field. Function pointers are translated into pointers of the struct bpf_program. Additionally, padding fields are generated to occupy the space between two consecutive fields. The pointers of shadow types of struct_osp maps are initialized when *__open_opts() in skeletons are called. For a map called FOO, the user can access it through the pointer at skel->struct_ops.FOO. Signed-off-by: Kui-Feng Lee Reviewed-by: Quentin Monnet --- tools/bpf/bpftool/gen.c | 229 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 228 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index a9334c57e859..20c5d5912df7 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -909,6 +909,201 @@ codegen_progs_skeleton(struct bpf_object *obj, size_t prog_cnt, bool populate_li } } +static int walk_st_ops_shadow_vars(struct btf *btf, + const char *ident, + const struct bpf_map *map) +{ + DECLARE_LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts, + .indent_level = 3, + ); + const struct btf_type *map_type, *member_type; + __u32 map_type_id, member_type_id; + __u32 offset, next_offset = 0; + const struct btf_member *m; + const char *member_name; + struct btf_dump *d = NULL; + int i, err = 0; + int size, map_size; + + map_type_id = bpf_map__btf_value_type_id(map); + if (map_type_id == 0) + return -EINVAL; + map_type = btf__type_by_id(btf, map_type_id); + if (!map_type) + return -EINVAL; + + d = btf_dump__new(btf, codegen_btf_dump_printf, NULL, NULL); + if (!d) + return -errno; + + for (i = 0, m = btf_members(map_type); + i < btf_vlen(map_type); + i++, m++) { + member_type = skip_mods_and_typedefs(btf, m->type, + &member_type_id); + if (!member_type) { + err = -EINVAL; + goto out; + } + + member_name = btf__name_by_offset(btf, m->name_off); + if (!member_name) { + err = -EINVAL; + goto out; + } + + offset = m->offset / 8; + if (next_offset != offset) { + printf("\t\t\tchar __padding_%d[%d];\n", + i - 1, offset - next_offset); + } + + switch (btf_kind(member_type)) { + case BTF_KIND_INT: + case BTF_KIND_FLOAT: + case BTF_KIND_ENUM: + case BTF_KIND_ENUM64: + /* scalar type */ + printf("\t\t\t"); + opts.field_name = member_name; + err = btf_dump__emit_type_decl(d, member_type_id, + &opts); + if (err) + goto out; + printf(";\n"); + + size = btf__resolve_size(btf, member_type_id); + if (size < 0) { + err = size; + goto out; + } + + next_offset = offset + size; + break; + + case BTF_KIND_PTR: + if (resolve_func_ptr(btf, m->type, NULL)) { + /* Function pointer */ + printf("\t\t\tconst struct bpf_program *%s;\n", + member_name); + + next_offset = offset + sizeof(void *); + break; + } + fallthrough; + + default: + /* Unsupported types + * + * For unsupported types, we have to generate + * definitions for them in order to support + * them. For example, we need to generate a + * definition for a struct type or a union type. It + * may cause type conflicts without renaming since + * the same type may be defined for several + * skeletons, and the user may include these + * skeletons in the same compile unit. + */ + if (i == btf_vlen(map_type) - 1) { + map_size = btf__resolve_size(btf, map_type_id); + if (map_size < 0) + return -EINVAL; + size = map_size - offset; + } else { + size = (m[1].offset - m->offset) / 8; + } + + printf("\t\t\tchar __padding_%d[%d];\n", i, size); + + next_offset = offset + size; + break; + } + } + +out: + btf_dump__free(d); + + return err; +} + +/* Generate the pointer of the shadow type for a struct_ops map. + * + * This function adds a pointer of the shadow type for a struct_ops map. + * The members of a struct_ops map can be exported through a pointer to a + * shadow type. The user can access these members through the pointer. + * + * A shadow type includes not all members, only members of some types. + * They are scalar types and function pointers. The function pointers are + * translated to the pointer of the struct bpf_program. The scalar types + * are translated to the original type without any modifiers. + * + * Unsupported types will be translated to a char array to take the same + * space of the original field. However, due to handling padding and + * alignments, the user should not access them directly. + */ +static int gen_st_ops_shadow_type(struct btf *btf, const char *ident, + const struct bpf_map *map) +{ + int err; + + printf("\t\tstruct {\n"); + + err = walk_st_ops_shadow_vars(btf, ident, map); + if (err) + return err; + + printf("\t\t} *%s;\n", ident); + + return 0; +} + +static int gen_st_ops_shadow(struct btf *btf, struct bpf_object *obj) +{ + struct bpf_map *map; + char ident[256]; + int err; + + /* Generate the pointers to shadow types of + * struct_ops maps. + */ + printf("\tstruct {\n"); + bpf_object__for_each_map(map, obj) { + if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS) + continue; + if (!get_map_ident(map, ident, sizeof(ident))) + continue; + err = gen_st_ops_shadow_type(btf, ident, map); + if (err) + return err; + } + printf("\t} struct_ops;\n"); + + return 0; +} + +/* Generate the code to initialize the pointers of shadow types. */ +static void gen_st_ops_shadow_init(struct btf *btf, struct bpf_object *obj) +{ + struct bpf_map *map; + char ident[256]; + + /* Initialize the pointers to_ops shadow types of + * struct_ops maps. + */ + bpf_object__for_each_map(map, obj) { + if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS) + continue; + if (!get_map_ident(map, ident, sizeof(ident))) + continue; + codegen("\ + \n\ + obj->struct_ops.%1$s = \n\ + bpf_map__initial_value(obj->maps.%1$s, NULL);\n\ + \n\ + ", ident); + } +} + static int do_skeleton(int argc, char **argv) { char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")]; @@ -923,6 +1118,7 @@ static int do_skeleton(int argc, char **argv) struct bpf_map *map; struct btf *btf; struct stat st; + int st_ops_cnt = 0; if (!REQ_ARGS(1)) { usage(); @@ -1039,6 +1235,8 @@ static int do_skeleton(int argc, char **argv) ); } + btf = bpf_object__btf(obj); + if (map_cnt) { printf("\tstruct {\n"); bpf_object__for_each_map(map, obj) { @@ -1048,8 +1246,15 @@ static int do_skeleton(int argc, char **argv) printf("\t\tstruct bpf_map_desc %s;\n", ident); else printf("\t\tstruct bpf_map *%s;\n", ident); + if (bpf_map__type(map) == BPF_MAP_TYPE_STRUCT_OPS) + st_ops_cnt++; } printf("\t} maps;\n"); + if (st_ops_cnt && btf) { + err = gen_st_ops_shadow(btf, obj); + if (err) + goto out; + } } if (prog_cnt) { @@ -1075,7 +1280,6 @@ static int do_skeleton(int argc, char **argv) printf("\t} links;\n"); } - btf = bpf_object__btf(obj); if (btf) { err = codegen_datasecs(obj, obj_name); if (err) @@ -1133,6 +1337,13 @@ static int do_skeleton(int argc, char **argv) if (err) \n\ goto err_out; \n\ \n\ + ", obj_name); + + if (st_ops_cnt && btf) + gen_st_ops_shadow_init(btf, obj); + + codegen("\ + \n\ return obj; \n\ err_out: \n\ %1$s__destroy(obj); \n\ @@ -1296,6 +1507,7 @@ static int do_subskeleton(int argc, char **argv) struct btf *btf; const struct btf_type *map_type, *var_type; const struct btf_var_secinfo *var; + int st_ops_cnt = 0; struct stat st; if (!REQ_ARGS(1)) { @@ -1438,10 +1650,18 @@ static int do_subskeleton(int argc, char **argv) if (!get_map_ident(map, ident, sizeof(ident))) continue; printf("\t\tstruct bpf_map *%s;\n", ident); + if (bpf_map__type(map) == BPF_MAP_TYPE_STRUCT_OPS) + st_ops_cnt++; } printf("\t} maps;\n"); } + if (st_ops_cnt && btf) { + err = gen_st_ops_shadow(btf, obj); + if (err) + goto out; + } + if (prog_cnt) { printf("\tstruct {\n"); bpf_object__for_each_program(prog, obj) { @@ -1553,6 +1773,13 @@ static int do_subskeleton(int argc, char **argv) if (err) \n\ goto err; \n\ \n\ + "); + + if (st_ops_cnt && btf) + gen_st_ops_shadow_init(btf, obj); + + codegen("\ + \n\ return obj; \n\ err: \n\ %1$s__destroy(obj); \n\ From patchwork Wed Feb 21 01:23:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13564840 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (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 54C3A46A4 for ; Wed, 21 Feb 2024 01:23:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708478632; cv=none; b=SuDA0mjzqlrQfucun1zeMx1yvEX8mddP0xlhcvRkepwNBoMqLhh4xeJKEGbmGUciLdMlv5G0Nury9YFqBm3y3Aaiu2YsMr4OQAvtqvY9fa92hJDNeE1bJpJDakrizYIiIOl1C3Yqb7rdfhoI7PTUKgMtnTNk18cLGhe4jAw3S6c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708478632; c=relaxed/simple; bh=kY+MSlkBXyBkmnvb+NaTneZsRI1XZ8ED3i+XLagur6U=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Cangqc72Qb3nIrOqG/GBZ6TvCVrC1IkM0l+1Lkmt/utMKa5nKFN7V7nGQPTX7GPZ5gkvHeCxHHyjr9HZsocUMQjRCLrzbt+b9/IC69emIipLxMFuUvE3DkhHUl0RihqBFtkekDmWVvCqWj8HxZzyWJrlaiBCp8rRuh50oF1hQ1A= 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=ZRrD3yGt; arc=none smtp.client-ip=209.85.128.180 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="ZRrD3yGt" Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-607c5679842so832577b3.2 for ; Tue, 20 Feb 2024 17:23:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708478629; x=1709083429; 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=gvyBlmzvCQZn7YJtlhRv6cuFksnihPgBjQqmVbCumoc=; b=ZRrD3yGtZo98Xf3JbMUUUqMd/CPQXbYjryFxkfXEUITy/rOxE3DRIwZPusCz2Gw7Ub 2x4RplXU2YzSiKBag3DZcpOYTJhrkgnMoH2SwDRrbYi0Twp97WWTJ7JtUjaXQejoQyqb dd1FxqzwnC4sR9xIJpp1pQuqOg2VAFeY1+7GdLqD0xBnd97EB9aV9LKA1OaqJ3VrDmX4 zAzfYgSmMww/RJZ8xruhPzrA27HRvUjRMVrWohUmNGskHrrqiep/oPGIrykZezjNB8lb jOi2O72MO+7BMiDt+bwVxcgS6tGc+keNG+gVODBYWQHPDwV5I8irYCfigL90r+aVavKX kVeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708478629; x=1709083429; 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=gvyBlmzvCQZn7YJtlhRv6cuFksnihPgBjQqmVbCumoc=; b=sy1TwUH2h3BgqdAzu6gi0iS3yG/oQKRYG2Z+TzwhZVw/7ZJcnboQ6uFqN3mDYM+kOs +3q4B3PnFEu4G9TXzyCNCEIKfp9H6zwpWAmgC84L521slR8ao00Czz8Hh2B6PoiTU6D+ oN6KzsbRq+Qs3FHCRa1kvB3dPKllQYW3WYgVX2RV0c1psv2+HtK5tV7N26K6s04CoVgV M7i0vZ9vNMpdNe/zjA2S/QjpGbeOXoSqkTt56FteEmzEzEU56fy+ciwwfQ4YuCyI/suh PmMjT39m4LV1XfNvIkRSoNrwqIGVYXxXpUfnH5DLwcVsao7tB/GLvpNk2QyfbRDnoUf7 QRVQ== X-Gm-Message-State: AOJu0Ywuxc/684BqJWsyTDsKTFgGcthd3QhHiEoKvC41yXTydDpftQ+7 qeExNHR6G1FZTx2pMz1YhcejIiiz6hpIUPH5HWDW9vo8JbBZD0ZajCA8x5Yq X-Google-Smtp-Source: AGHT+IFgZahGMCpbLqQc3T2Si/LISxsJxS9aTOaGhqOVCNel/kre5aomCauEsBdYgbJRM1fR1KSSTA== X-Received: by 2002:a81:bc51:0:b0:608:7a9c:9a82 with SMTP id b17-20020a81bc51000000b006087a9c9a82mr1053596ywl.47.1708478628851; Tue, 20 Feb 2024 17:23:48 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:26eb:2942:8151:a089]) by smtp.gmail.com with ESMTPSA id j64-20020a0de043000000b00607ef065781sm2396801ywe.138.2024.02.20.17.23.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 17:23:48 -0800 (PST) From: thinker.li@gmail.com To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v3 5/5] selftests/bpf: Test if shadow types work correctly. Date: Tue, 20 Feb 2024 17:23:29 -0800 Message-Id: <20240221012329.1387275-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240221012329.1387275-1-thinker.li@gmail.com> References: <20240221012329.1387275-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Kui-Feng Lee Change the values of fields, including scalar types and function pointers, and check if the struct_ops map works as expected. The test changes the field "test_2" of "testmod_1" from the pointer to test_2() to pointer to test_3() and the field "data" to 13. The function test_2() and test_3() both compute a new value for "test_2_result", but in different way. By checking the value of "test_2_result", it ensures the struct_ops map works as expected with changes through shadow types. Signed-off-by: Kui-Feng Lee --- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 11 ++++++++++- .../selftests/bpf/bpf_testmod/bpf_testmod.h | 8 ++++++++ .../bpf/prog_tests/test_struct_ops_module.c | 19 +++++++++++++++---- .../selftests/bpf/progs/struct_ops_module.c | 8 ++++++++ 4 files changed, 41 insertions(+), 5 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..098ddd067224 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -539,6 +539,15 @@ static int bpf_testmod_ops_init_member(const struct btf_type *t, const struct btf_member *member, void *kdata, const void *udata) { + if (member->offset == offsetof(struct bpf_testmod_ops, data) * 8) { + /* For data fields, this function has to copy it and return + * 1 to indicate that the data has been handled by the + * struct_ops type, or the verifier will reject the map if + * the value of the data field is not zero. + */ + ((struct bpf_testmod_ops *)kdata)->data = ((struct bpf_testmod_ops *)udata)->data; + return 1; + } return 0; } @@ -559,7 +568,7 @@ static int bpf_dummy_reg(void *kdata) * initialized, so we need to check for NULL. */ if (ops->test_2) - ops->test_2(4, 3); + ops->test_2(4, ops->data); return 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..971458acfac3 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h @@ -35,6 +35,14 @@ struct bpf_testmod_ops { void (*test_2)(int a, int b); /* Used to test nullable arguments. */ int (*test_maybe_null)(int dummy, struct task_struct *task); + + /* The following fields are used to test shadow copies. */ + char onebyte; + struct { + int a; + int b; + } unsupported; + int data; }; #endif /* _BPF_TESTMOD_H */ 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..7d6facf46ebb 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 @@ -32,17 +32,23 @@ static void check_map_info(struct bpf_map_info *info) 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; - skel = struct_ops_module__open_opts(&opts); + skel = struct_ops_module__open(); if (!ASSERT_OK_PTR(skel, "struct_ops_module_open")) return; + skel->struct_ops.testmod_1->data = 13; + skel->struct_ops.testmod_1->test_2 = skel->progs.test_3; + /* Since test_2() is not being used, it should be disabled from + * auto-loading, or it will fail to load. + */ + bpf_program__set_autoload(skel->progs.test_2, false); + err = struct_ops_module__load(skel); if (!ASSERT_OK(err, "struct_ops_module_load")) goto cleanup; @@ -56,8 +62,13 @@ static void test_struct_ops_load(void) 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"); + /* 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); diff --git a/tools/testing/selftests/bpf/progs/struct_ops_module.c b/tools/testing/selftests/bpf/progs/struct_ops_module.c index b78746b3cef3..25952fa09348 100644 --- a/tools/testing/selftests/bpf/progs/struct_ops_module.c +++ b/tools/testing/selftests/bpf/progs/struct_ops_module.c @@ -21,9 +21,17 @@ void BPF_PROG(test_2, int a, int b) test_2_result = a + b; } +SEC("struct_ops/test_3") +int BPF_PROG(test_3, int a, int b) +{ + test_2_result = a + b + 3; + return a + b + 3; +} + SEC(".struct_ops.link") struct bpf_testmod_ops testmod_1 = { .test_1 = (void *)test_1, .test_2 = (void *)test_2, + .data = 0x1, };