From patchwork Thu Feb 22 22:26:19 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: 13568194 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1AE1973F20 for ; Thu, 22 Feb 2024 22:26:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708640791; cv=none; b=P5GEPtUZDd3ENgCf+0vg9umcYGt1wnaiE7O+9PhaCSONL1U7WKsX8ZQfSFJBTaVE613CSbUa3a5o6C1RqWFApvFF7D+wRsAzscLp2mZuU5hce0wJRif1llFRGCvCp7ExNFI/ae7vGadH7XDHIVy0QhtTz7oTmzdcDt9vl76B13M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708640791; c=relaxed/simple; bh=HWPoxm2exu4cl2asl4FSemXYidKP2WSvF7Ldx5vlU6E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AId8cQIXLNN17eaTDevwBxrSWhfEdoezzbCSU30QO6LgXNdCjuLCsKClPIcxtu+qYK/LzTdIzvOeuC2ADsiPs+7JXWVJlDV2OkJEtcjWDoGRY4LU15gEJ2wnYzT0Rer0bbpZLisxCn2coek3zYtnO4PgI4nMg0NOmy8DTG2uOiQ= 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=NhkI2NhQ; arc=none smtp.client-ip=209.85.128.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NhkI2NhQ" Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-607bfa4c913so2897567b3.3 for ; Thu, 22 Feb 2024 14:26:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708640789; x=1709245589; 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=+/99EYvY07+3bvMFvR7ARAdMavIYR92Oi8Q4MC2C/Go=; b=NhkI2NhQt1IxPNCvMxks1BBFyc8I5A7jqm3oPSFlPZKb5zm6zCmQ7sSbdpZ3rWtYuH KscNvqOoNKYzMZp6aPbJgEbKNXlA6heoXV6h+QpM7ltN7u6Sm5tj1Uv5DRjzIWvTx84y xAWy4/+SNPb5Z9wzZSqJXOSA83laxjXFaWFCxL+jN4PWrcoAL42tv3Dl9ZiBr+E8c1+x Pn5cCPRENjSHQt0N8z61J5HCdjFOCXM1E43GDzK+G03vE8hJpsX9Tx1JSxH3fEc/tSRJ PMg9IDOHoiFdcKyCSXY8yYVgiUnx+MD87uM2gopGJhuHhCLPKz5y00QKt9v4z015ktxk KdVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708640789; x=1709245589; 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=+/99EYvY07+3bvMFvR7ARAdMavIYR92Oi8Q4MC2C/Go=; b=D72xeZhWt5jJbedpMo1NdaNNAWWdnHlzuU+Lqs4jjKjY+3xeABS4M/22ieMjM1mPIp eQ3Z0QmSl0103rWKjtD5x5D4C6CySIHCVQZgMopv82fUOrQSOZ1HU+Sx4Td4OBsdPp7D 6kC2re/HrHyp+mWlDPvaCaJ85zq/3dUxJghKxUOH7pXmfJIcmYCB9/ong+eZAxSKmAxV lBTEnrNpHQzHpXGAiqzrsFAt4SD3xmflyVqbWOSugAHewwOXpcCjs3drxI/OHEUUbDc7 AlK6wfznHKkAFxux6YdbOVDrAstU0fbX3TdX+4GhzfGmH6zlJlhcRiXkI2YY5m3W3DzK 1Ivw== X-Gm-Message-State: AOJu0YzfVNKWAqwQsDuwl7V39vvJYKDiLOYIUFLi0v2Q+Cyx9qImkPuv TV1xFPtsfUDWyTGJBCy9H2bgJwIXIifRkuhvN1nG1AjJH2r1cq2L0MHlVx8G X-Google-Smtp-Source: AGHT+IFg2CfsiMI6O9OA5OdS/HxgbBU+gL/8FIzmahLbaMW283Thp6tcAQyBbm3cZtkOyhP6xKBwaw== X-Received: by 2002:a81:924e:0:b0:608:ab2f:cfd9 with SMTP id j75-20020a81924e000000b00608ab2fcfd9mr506926ywg.11.1708640788719; Thu, 22 Feb 2024 14:26:28 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:34d2:7236:710a:117c]) by smtp.gmail.com with ESMTPSA id e129-20020a0df587000000b00604a2e45cf2sm3280666ywf.140.2024.02.22.14.26.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 14:26:28 -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, quentin@isovalent.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 1/6] libbpf: expose resolve_func_ptr() through libbpf_internal.h. Date: Thu, 22 Feb 2024 14:26:19 -0800 Message-Id: <20240222222624.1163754-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240222222624.1163754-1-thinker.li@gmail.com> References: <20240222222624.1163754-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 | 2 ++ 2 files changed, 3 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..17e6d381da6a 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -234,6 +234,8 @@ 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); +/* This function is exposed to bpftool */ +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 Thu Feb 22 22:26:20 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: 13568195 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f178.google.com (mail-yb1-f178.google.com [209.85.219.178]) (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 695EA137902 for ; Thu, 22 Feb 2024 22:26:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708640792; cv=none; b=WXjeMRdnj1QQvKWVQQkQoIEYJu0Vq/kkmVJLqCb7pN295/js5ZWi+zaeUHIn39KDmNkPM2a0TaPZuQlqFCxgB8emWb/xS216207zAs0AGxPu+zVBbTVuTqn5pHZCN2jDP8aRHuIS/gyJa49eZNrs4wZGXxe5IIyufAGKIODbkm8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708640792; c=relaxed/simple; bh=9pgDJRa9T/5a4i1vhMmQqOlMwwmbYYmxCovw23ewryU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=d+dol9KRv/yA9cqiDZMQSySHIkApz3ZwFOsIe3eul7CZ5+Z58E8DOfvIYedR2FeuXuWRP9lFVSdCBqJ6LKgpgmVloX0e0CVXlmA31zlz0vxJR6zEvps4SZ9oc4nGP62Ry5PDVPlTXp/KAt8U+X/vn7iEhukOyka86D7yPWWSLsc= 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=a/5rSjnP; arc=none smtp.client-ip=209.85.219.178 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="a/5rSjnP" Received: by mail-yb1-f178.google.com with SMTP id 3f1490d57ef6-dcc71031680so209774276.2 for ; Thu, 22 Feb 2024 14:26:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708640790; x=1709245590; 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=a/5rSjnPKAgfydS/BzbQReo6WyWVnawuMZvxJUy6m2LgcGlEqrqhdNU8f+CKuCE1de bm4l7Z3FdtZiGnUxVBb4/F7JZ+HYAJEtQuI2bQ6TrZ95MjtnMrlFubAi5ytx2MQwWtJw qtNqYubC/v2+ZaDog8yMsaU8FZVfp+16g0BdngPe+auH91lmuqiRd+RjEta3aK2ctg7h MwCeBvoB9GZvkM5xvG7y47bDZMep9FXxObtRSeXyxS1OcL3PlHLIBVKUH2R6bFSzrgQh MTEVio8RIBC8BhgR1LSGrc4vZEoHZ57HZFNRXLhuCHECtWYwr7aaWQHZlxrgMmmb210/ 3Uvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708640790; x=1709245590; 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=glOP1dqnjwrns4ZfJ8TbTiIVdPOyv+utOZcekwPVBlZqBNnkMkC/TiZg8eAFjusTQw 9PILyvSy6zYV7R6K1KSsJ7Z5S3ELsyzmifGXvN0V63W+r/sfke3Ev0IriG5clr+y2fqm wUXnVvxoL9Co6Zryo36bOEQco2rSSxCDwNE9SttZ6iG0dZtafsSxRRqzwr3B8g2PS/Jd yPBykKZTzezVqRREQeji3vB+rQBIxmP24c7LDNezFcyct5HbDJlAyWDC2Gqa2trDW0sc HL4M19UWJWzJPM77mSKsTpJmCOcaSmdQyg488CYhxgpFQo/uTPyMBhZxdMYamLasE0di XQTQ== X-Gm-Message-State: AOJu0YzLXlKMM1CSEcRn9dk2VJtaPI4E9BFHBvy/hyw5FqaAwJA8yKrZ A8s7ImhKWYa/MOSyo3xJR3exuvBR3mKjcb/0O/7GhM55DJQ2Hz76o1A9bF6s X-Google-Smtp-Source: AGHT+IHHIdzDN+CVUPDndcYpcPFOgxyfPjZt46lXR1xqN5AejIM0gMTGiWe3fuJfYxirXBmg/j62uQ== X-Received: by 2002:a05:690c:6:b0:608:922:4001 with SMTP id bc6-20020a05690c000600b0060809224001mr568920ywb.5.1708640789895; Thu, 22 Feb 2024 14:26:29 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:34d2:7236:710a:117c]) by smtp.gmail.com with ESMTPSA id e129-20020a0df587000000b00604a2e45cf2sm3280666ywf.140.2024.02.22.14.26.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 14:26:29 -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, quentin@isovalent.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 2/6] libbpf: set btf_value_type_id of struct bpf_map for struct_ops. Date: Thu, 22 Feb 2024 14:26:20 -0800 Message-Id: <20240222222624.1163754-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240222222624.1163754-1-thinker.li@gmail.com> References: <20240222222624.1163754-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 Thu Feb 22 22:26:21 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: 13568196 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7AB2D137912 for ; Thu, 22 Feb 2024 22:26:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708640794; cv=none; b=VeIPprcsL8rKvhH/PDFXAF8OxkOfPOYPmbSfnkPb+mubh9xnevrIidCzc/2nKIGqn2M/IpyCLUuAaq2SNguIsECTdmHbagHx1BeYBi4k562bZwpz2d2B7WA5SYpwTtvzYk2wa+w51vjVj85CcMI0O/qb3mQDu1IMizE8OyBm0oQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708640794; c=relaxed/simple; bh=JYlor7liByBS2LlEYz/edKDIPn+GA8eleujRxigKJPQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uTy/GCVscfaqhLBFtdEOCj8ezyFeBBi26N0wulmpIaGRZolKpjJhbMnBjCkwXhTiB/6H+XY3AIFFwpeeDOfK/x4lTx0pX/v6rrw0Gj1pUKb2KCn/jgtGN+PQYv+9KceSQNQWBc8argzYRRlhfxsewpanZkD9M1R3PkwWy0piw6M= 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=bFZAxIkz; arc=none smtp.client-ip=209.85.128.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bFZAxIkz" Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-607d8506099so3094517b3.0 for ; Thu, 22 Feb 2024 14:26:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708640791; x=1709245591; 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=bFZAxIkzO3fOpSL7Zm79zwPQbqVRoqRg7Kok6I44pJxwjfZuXUbxQZ6VYhmPzP3eBG nW7+PizjqOLP+btuNCkmYljEzVHVaWUhZz06oEMnLwmLT1wI97b2+JaMGpa+w4IjkwjE rOJbRjhx+9bYRvnUUY73iTrdrNkWCiD1K+BKs63UUDf8csv+nDaYOYX0J5ct8UgVWB4t UNhJGylkONA5UT6OHJEVrqrqsGgW0GvUwIVoEVG21wb0lm9oliUXlbzY75V0rxgO9Mu7 0cawedyfu0kKHTtfrdyWnGop0RNhHFk5nG67MuRsyb0G3Iub94zzWEXBX56P2NEKXG2b s1Kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708640791; x=1709245591; 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=ZEDJfSICUDt5yl6MsWfScN5MdqUnNx5gHKKRSqXuur/U1I55CaLlkAmApkWiHVaTqI fCNoKqTBRY7w4oLBU/sKbNEVh+3T648hqtc4truuDQpq89XcXx3liNMpxZzikTWfdmYa KkEvCvI2hDSwUjd2jASLmOI6IlWlU3dXJIGm1s8zWJDMQMD1lMJFLFqm/GfEfTDLkzse iaauD55SykRtiOxxAai+wYFnFkp4UhmWirS9uBt50o7AqmKxV7WWW5YpfSSTGF34eBnp 86KrW2VFnLAbZouYIYb+FWUh8MY/cn77Y+u3yIDu5IjKXi60uDyxpPX+x1Re2OzxL/dR kZqA== X-Gm-Message-State: AOJu0YwDaDJr4kBME8kghG06k02fPBMyRkYD/utn06T1tAT92r3CYRdT YeiWuoOBQ+j12hHfUPpNbFcudFkHnsRR0w3S2YO1RaCO34q9hjfjfToUUUXq X-Google-Smtp-Source: AGHT+IEGhDpv6gtauv4IYtJ72FMmifU1GWzDrnYhK6DFaF7zN22B/oY5Rx6T1uhx3ewk7KphojXTIg== X-Received: by 2002:a05:690c:84:b0:608:b439:4779 with SMTP id be4-20020a05690c008400b00608b4394779mr594049ywb.0.1708640791017; Thu, 22 Feb 2024 14:26:31 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:34d2:7236:710a:117c]) by smtp.gmail.com with ESMTPSA id e129-20020a0df587000000b00604a2e45cf2sm3280666ywf.140.2024.02.22.14.26.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 14:26:30 -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, quentin@isovalent.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 3/6] libbpf: Convert st_ops->data to shadow type. Date: Thu, 22 Feb 2024 14:26:21 -0800 Message-Id: <20240222222624.1163754-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240222222624.1163754-1-thinker.li@gmail.com> References: <20240222222624.1163754-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 Thu Feb 22 22:26:22 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: 13568197 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f182.google.com (mail-yb1-f182.google.com [209.85.219.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 A8476137902 for ; Thu, 22 Feb 2024 22:26:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708640795; cv=none; b=EN59aKioP+ZsGCAJlVap6GJtp7Gv381hJJTir+RI8GIcNNghQOLgTqVdod936TyV7SioJg7+GzMGsmfGeTB2WBz51TxyxNly8gW0x6WnqpLO7nQqEdQJQ+Y8RzHS/qDEALyV6DaIjYISgO8iJupwQSp9MRTxpW59mTMj+PiQJOg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708640795; c=relaxed/simple; bh=myYhlR77GYPMEpdusIYm14ylXipQ4PZJZSgUNWPhjkA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XUCSrg57zrl/CAcAVtRUOTYwbwMnKfNMxQ/0DrnTx+JBXR/3oBwVWIgm1Qx1y4MJ4agFxr+CyqNp5Ua6z4xuaVAeSznepppovLblDlIdO6yyzTZNB9QokYr2byb9EowIGM6NMPWpsu3qcdhlRmat5AW8SHQZYdICVwxKsDs1Zl4= 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=a3cTGwsW; arc=none smtp.client-ip=209.85.219.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="a3cTGwsW" Received: by mail-yb1-f182.google.com with SMTP id 3f1490d57ef6-dcbf82cdf05so200570276.2 for ; Thu, 22 Feb 2024 14:26:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708640792; x=1709245592; 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=R371K0ydisrm+4ePeuSI88lTXvzLnBLcxh/U4PbC+3s=; b=a3cTGwsWeKMS8Jt8HT5bHPk/iHP9CwRPKbgh2OFacIeaky9YwdPHnPa43+epVhmgNE BOccrll0M0aoeMqnUcNLUtHxWMtk3i5vgbRBiJpG7u91K5DVilzHAjnRGgLfSP4YNOVr Ui1kkVaiifYBtnDfGEc+V1HD4VztUjh76/ZOp+4cnpRePdqrwgnBPEIJd4G9+PWhrpH9 cIlfuYnC/UKbmBaXcz5I5iu4ZgCyqz1jrh6epVSiAic25iXSayFHDuisL7aCDE3n1GI7 YKKVA+BobD39wHyy5VKu1VbIdLjSLpf4KxeEyxmX+8I8lyrDthLn1IpLOob5JnXXYNqr gPEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708640792; x=1709245592; 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=R371K0ydisrm+4ePeuSI88lTXvzLnBLcxh/U4PbC+3s=; b=rFxeftJi86a/yfBWl3WLheujWqygBW8CAad/kQOtSCs71OujtoWqyO/O8dpgt6Dhjw +hEXv2a9Q+XYpB2hrhNU/km4NeqDRzeQHBNV+i+FuvUcVVmm1vkW2R5pPzj2oXbCosfa /o1UFwLnoQx8t6R9oHXkL9GZ2fkaUBshp+DV/jP9POfJIwHFVvWiQNO953biZXWAbvi+ zQ3Zdo6LCda6SlnKvh5yfQmbmEs0S3eSbU+FAhhZrFiW0MDu2AWKQTTlQDEdfJEjKJaw BsLLZs+Rb/xZUDnFKgAZPVRe9m8uzWcIpVlp+BExj9GM8CsTVvnHQW8CKVozWZUQ5zNP VG2Q== X-Gm-Message-State: AOJu0Yy+44kaVayjX7kRoEIHuJLAtoLMhPeF9E0l5sbNvtPt96zkTYY8 GyhBUF5CWSlGRsQNEQLUcXGJ+rlCijsphsG6IdZYPPd+aEu3JiNAw/xGnJtx X-Google-Smtp-Source: AGHT+IHSPT6G6BgK76o7714PYcGCZJ4XIHnmp7DzJ5WxJ/Fj+cM1Vkw912Nfar3rfKerrY/FwwXibg== X-Received: by 2002:a05:690c:fc2:b0:608:3cb0:4c97 with SMTP id dg2-20020a05690c0fc200b006083cb04c97mr642376ywb.21.1708640792149; Thu, 22 Feb 2024 14:26:32 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:34d2:7236:710a:117c]) by smtp.gmail.com with ESMTPSA id e129-20020a0df587000000b00604a2e45cf2sm3280666ywf.140.2024.02.22.14.26.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 14:26:31 -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, quentin@isovalent.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 4/6] bpftool: generated shadow variables for struct_ops maps. Date: Thu, 22 Feb 2024 14:26:22 -0800 Message-Id: <20240222222624.1163754-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240222222624.1163754-1-thinker.li@gmail.com> References: <20240222222624.1163754-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, including 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. Reviewed-by: Quentin Monnet Signed-off-by: Kui-Feng Lee --- tools/bpf/bpftool/gen.c | 235 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 234 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index a9334c57e859..a21c92d95401 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -909,6 +909,207 @@ 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; + } + /* All pointer types are unsupported except for + * function pointers. + */ + fallthrough; + + default: + /* Unsupported types + * + * Types other than scalar types and function + * pointers are currently not supported in order to + * prevent conflicts in the generated code caused + * by multiple definitions. For instance, if the + * struct type FOO is used in a struct_ops map, + * bpftool has to generate definitions for FOO, + * which may result in conflicts if FOO is defined + * in different skeleton files. + */ + 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 occupy the same + * space as the original field. However, the user should not access them + * directly. These unsupported fields are also renamed as __padding_* + * . They may be reordered or shifted due to changes in the original struct + * type. Accessing them through the generated names may unintentionally + * corrupt data. + */ +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 +1124,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 +1241,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 +1252,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 +1286,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 +1343,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 +1513,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 +1656,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 +1779,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 Thu Feb 22 22:26:23 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: 13568198 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f177.google.com (mail-yw1-f177.google.com [209.85.128.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 93FB913792A for ; Thu, 22 Feb 2024 22:26:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708640796; cv=none; b=SL2Btlva86KHruBI+xSaPm4c9WPjhmr0nbDJHWZhQgbdyJQMB3xuezRavXTLFQYX9emLzDRgnOphUxoWP8UFBNUBKWy5kZeT6qQKsA89y5b5AC195W/uCjB/QxDHjq+mOh1DQcXpsMp9VX4bBr4CGtP/8KxB4VMZrBSsUeNGlNg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708640796; c=relaxed/simple; bh=xHL0akAEglGOwsUZTmTuC24f6eHkZmXrL9gCcjz+37Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FKsyKRpG2Hz1muByVu7pMgrbHW/RTAXswCl2LHHXtuqOj5vxgs/hSJwDj1bhkQCbc9KpY3OHG43++zzNIHS24+uHN+ZmmBnTRRsExmWxndQRVXlTICJvIR4lXeXVRBlS0VR0CDRX2DJlqElTzdeI4P45llOySez3JNRcDUZCE2M= 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=Uj+nZwzY; arc=none smtp.client-ip=209.85.128.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Uj+nZwzY" Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-6083cabc2f9so2766107b3.1 for ; Thu, 22 Feb 2024 14:26:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708640793; x=1709245593; 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=GXLAX7ArfBFQiuOv1Px8GCmtaMpXT3tYhEK+eN8KW9Q=; b=Uj+nZwzYZXAmHFKkDOb4isRJz+FaP4H+LwKoVT70IkT2vJkOibwZKsO7zCcmYPFn7I lgjPJcC2teaxAqjMigTyTglWRiW1BU+otTlSBXLONkNSjnIk2uR170cVM8066WrloEYt pTTpfRA++fsO9L77xqjaPchwOZyK39AsEpQ099eT7vBo+1aYaP08R6RWLybFqBVdIGrQ jS+5lRsI694j+pzRX4Dp3+yCs2FGs3itJpaAKY9DaYfb4vkznG+3bMO6RVnXulNp4rF7 Q++HX5ESFzq7liL01iD4tgaF22fWmGA9z+BpUjnl45MGMMPf4si5rMIF8qGZ4gDl9d2T KP8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708640793; x=1709245593; 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=GXLAX7ArfBFQiuOv1Px8GCmtaMpXT3tYhEK+eN8KW9Q=; b=uYCH9ffdx2Dcf5fmbktWiwJc85Uw2W0hXkNJkl17hPW6e1IhL3SHERB3govkSfLjTr cu8uvlG0c7ri9dWUiWAjYaebQUV0qUkqVJ74KF5oUfb/UObxT1ro8TW2d8Xc9z/ARk8t FzGLzqzoK6CAP8zUYJ4XeaTnYD9DUqgY0MO7dTt+2zvwnQDpS88qUH2/a7d7Cll550R9 6rMXrR8Cxds3SvlMHmjO7nQ7g2Wif9fV3gZFw5m/0MlY7Cj+4WB2pqT6KOTBbe7osTxg nZ/qopi1VfGoAli5C1H+elHN4F1jDr18c/j1U3IYD0ldJCIagvHWQG2kMVINmM/MWqPF uaqA== X-Gm-Message-State: AOJu0YyVdsUdPuCqS4fiYIZ3nRZz9UtdA0tY9xrVdK2I5nwcjfGCLHq7 1VYYHnfWvioKdUPeY+rgQ2husmYETkKWveXybDFCTm3k/TTlDZzJTSqgHEmg X-Google-Smtp-Source: AGHT+IGEW5DbpSO5evf4OlY+ObZqc/2Ezh5WKHYSL7iQIBFfEbH93rppifTdPc7T5b5gK2zD8DKPVw== X-Received: by 2002:a0d:e808:0:b0:607:d9f7:e884 with SMTP id r8-20020a0de808000000b00607d9f7e884mr482817ywe.4.1708640793287; Thu, 22 Feb 2024 14:26:33 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:34d2:7236:710a:117c]) by smtp.gmail.com with ESMTPSA id e129-20020a0df587000000b00604a2e45cf2sm3280666ywf.140.2024.02.22.14.26.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 14:26:33 -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, quentin@isovalent.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 5/6] bpftool: Add an example for struct_ops map and shadow type. Date: Thu, 22 Feb 2024 14:26:23 -0800 Message-Id: <20240222222624.1163754-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240222222624.1163754-1-thinker.li@gmail.com> References: <20240222222624.1163754-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 The example in bpftool-gen.8 explains how to use the pointer of the shadow type to change the value of a field of a struct_ops map. Signed-off-by: Kui-Feng Lee --- .../bpf/bpftool/Documentation/bpftool-gen.rst | 58 +++++++++++++++++-- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/tools/bpf/bpftool/Documentation/bpftool-gen.rst b/tools/bpf/bpftool/Documentation/bpftool-gen.rst index 5006e724d1bc..62572f5beed9 100644 --- a/tools/bpf/bpftool/Documentation/bpftool-gen.rst +++ b/tools/bpf/bpftool/Documentation/bpftool-gen.rst @@ -257,18 +257,48 @@ EXAMPLES return 0; } -This is example BPF application with two BPF programs and a mix of BPF maps -and global variables. Source code is split across two source code files. +**$ cat example3.bpf.c** + +:: + + #include + #include + #include + /* This header file is provided by the bpf_testmod module. */ + #include "bpf_testmod.h" + + int test_2_result = 0; + + /* bpf_Testmod.ko calls this function, passing a "4" + * and testmod_map->data. + */ + SEC("struct_ops/test_2") + void BPF_PROG(test_2, int a, int b) + { + test_2_result = a + b; + } + + SEC(".struct_ops") + struct bpf_testmod_ops testmod_map = { + .test_2 = (void *)test_2, + .data = 0x1, + }; + +This is example BPF application with three BPF programs and a mix of BPF +maps and global variables. Source code is split across three source code +files. **$ clang --target=bpf -g example1.bpf.c -o example1.bpf.o** **$ clang --target=bpf -g example2.bpf.c -o example2.bpf.o** -**$ bpftool gen object example.bpf.o example1.bpf.o example2.bpf.o** +**$ clang --target=bpf -g example3.bpf.c -o example3.bpf.o** + +**$ bpftool gen object example.bpf.o example1.bpf.o example2.bpf.o example3.bpf.o** -This set of commands compiles *example1.bpf.c* and *example2.bpf.c* -individually and then statically links respective object files into the final -BPF ELF object file *example.bpf.o*. +This set of commands compiles *example1.bpf.c*, *example2.bpf.c* and +*example3.bpf.c* individually and then statically links respective object +files into the final BPF ELF object file *example.bpf.o*. **$ bpftool gen skeleton example.bpf.o name example | tee example.skel.h** @@ -291,7 +321,15 @@ BPF ELF object file *example.bpf.o*. struct bpf_map *data; struct bpf_map *bss; struct bpf_map *my_map; + struct bpf_map *testmod_map; } maps; + struct { + struct { + const struct bpf_program *test_1; + const struct bpf_program *test_2; + int data; + } *testmod_map; + } struct_ops; struct { struct bpf_program *handle_sys_enter; struct bpf_program *handle_sys_exit; @@ -304,6 +342,7 @@ BPF ELF object file *example.bpf.o*. struct { int x; } data; + int test_2_result; } *bss; struct example__data { _Bool global_flag; @@ -342,10 +381,16 @@ BPF ELF object file *example.bpf.o*. skel->rodata->param1 = 128; + /* Change the value through the pointer of shadow type */ + skel->struct_ops.testmod_map->data = 13; + err = example__load(skel); if (err) goto cleanup; + /* The result of the function test_2() */ + printf("test_2_result: %d\n", skel->bss->test_2_result); + err = example__attach(skel); if (err) goto cleanup; @@ -372,6 +417,7 @@ BPF ELF object file *example.bpf.o*. :: + test_2_result: 17 my_map name: my_map sys_enter prog FD: 8 my_static_var: 7 From patchwork Thu Feb 22 22:26:24 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: 13568199 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f178.google.com (mail-yw1-f178.google.com [209.85.128.178]) (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 EFFC3137925 for ; Thu, 22 Feb 2024 22:26:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708640797; cv=none; b=jdz7udXgru5Ih0cTiSgTGOWNtG9ksDunNWd1rOXrOXQi9zbdbet4ALpQFPPyC5wuFMEfSPZRnHu4ybhmZeMk8C9jYifmsN5czFaB3SqO1WB09gid5XDkn+gfEzMxhnCH4jK39hsQ/9lu/SgERBB5QypCNi3F+cr3vv172zAik0k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708640797; c=relaxed/simple; bh=kY+MSlkBXyBkmnvb+NaTneZsRI1XZ8ED3i+XLagur6U=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oa8obt+dMAWUC+lMbg2ltz3v90d6oGrhfdUPhvN2L/TNNz3ttuMmliYecq0AtZdsJ2BYovfXRDS7xQfwYXQ2lCzsMh7maU04JsHlxmnDGqnPd9GUU1CwM24LtNEr/VqpfX58u/+w1vMsyhlWW9b9jHn5eoFqkDPNR5i0B2Tdup4= 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=ZpmBon58; arc=none smtp.client-ip=209.85.128.178 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="ZpmBon58" Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-607c5679842so2779987b3.2 for ; Thu, 22 Feb 2024 14:26:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708640795; x=1709245595; 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=ZpmBon58zK1UX7kbSFOepJ4hs1qja/R8O5L4POFqykg3QH4FJgEVgiNuzj0qQMedme 4unVGL5GSrlavi3/4CpK6BQa1ip0eWpVmUkVkibXIwXl0alL6odAX01QbIntxEDmDqP+ V/EmAsHKF1DaWYSh3BxHTvqT54PTycWgbBiXL0QE4P2EnsK6Tazf+ifEazPQrQw4dxI+ 6qDpKuN0GOUBm5hLkSN8HAalXjZGhju0bOJXKQTclnSCSOH16tjFUSurz74J5Gmt6fdi eXd4tVaPFwvh7maOLhD1OkMhtTW3MiZgZSUSytMjH9johUDe5vKhpK7BFWre1uIy3JUe hMPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708640795; x=1709245595; 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=WQ71jFwsbRP+qJF32Hg7sFEyhRdEZ9stdeQcydxgFWaNIg7pu305PNlsklZFWVxH/b GrQ7W4lHkoPI1jXXIYUyll3rxr+NqeSbKKbHSTmHy/zAf9hB1+7BXO8Lhh+UChxZbO2h 7mKyI+0hxoYRa0G98OzvvN+drbZgcGGnUCbwnwoHn8NSCjSkoXVJ6nNvo1Jfx0BJ8mpN FNKEsz5gTdczsuPAM0xbzqCXqAx5FU+1+IlkaVwEk14pohGVyir3UgZq2CuktDfkNsts 4Dol5LTwikC1wySvOii1m3iDRuA4iixElte2dbZnZfWBVmEfYOnqB16OpHVbgIURylTx 884A== X-Gm-Message-State: AOJu0YzN9n+FQt7uesS9KbnLOsEZuEXlX8qYahLbeyCk/kWjAbej4Imb V4Rqzo2xK3QMHQqVkuG9MSkrlYrK1CEOx9duD/4P8fO1haXp54ewRs29nRf3 X-Google-Smtp-Source: AGHT+IE+Sv3rh+SRxHSqodb9K1iyCLLxOITK7SP6YoyxECdvH5005eWroJRx2bDsMCAqcHN3YsnM/A== X-Received: by 2002:a0d:cb11:0:b0:608:b61c:1a4e with SMTP id n17-20020a0dcb11000000b00608b61c1a4emr207559ywd.15.1708640794528; Thu, 22 Feb 2024 14:26:34 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:34d2:7236:710a:117c]) by smtp.gmail.com with ESMTPSA id e129-20020a0df587000000b00604a2e45cf2sm3280666ywf.140.2024.02.22.14.26.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 14:26:34 -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, quentin@isovalent.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 6/6] selftests/bpf: Test if shadow types work correctly. Date: Thu, 22 Feb 2024 14:26:24 -0800 Message-Id: <20240222222624.1163754-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240222222624.1163754-1-thinker.li@gmail.com> References: <20240222222624.1163754-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, };