From patchwork Tue Feb 27 01:04: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: 13573036 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 B2985468E for ; Tue, 27 Feb 2024 01:04:40 +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=1708995882; cv=none; b=Kd9jS9Qeh3kWtkwv8e9fng15hBCCpS0k24/NwOH6+rMoZWKBdH8yrC7VIHLj+Gb3LnRHnWNmU0XtHeI2x/SLxcoU9EJ9B4gY0DP9MvTTELKRu0ic2jBOCcCdspS5E7T+kUVYRCWcNaiYlK3iKBjAWTSZuSCSReRLEhwxJ3PuWaY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708995882; c=relaxed/simple; bh=+6l0lQnY8omCUzf5OMwxqoI00375s+66fvfjQbAR6LE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=l2t2C5pRZaPkvGtjzV05bXhiege1zznpGwd/KIPfHfmu1XuFJaU1TuRc02xUMWHYb2+R5NcAgRCzKT7nxObjDOGvehoBaF+wiSLgR6+LcU9f9/x9pIOdr90zivNXgqjiA3rEN927IrvBCpU4zDw7JCSD2cHDrttEKrbev9G96/4= 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=G+IhnM50; 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="G+IhnM50" Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-608e3530941so21838347b3.1 for ; Mon, 26 Feb 2024 17:04:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708995879; x=1709600679; 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=IXlFc5ZBorhTOulkewfZwF8BMlwYQd7ekIX0pYnvaM8=; b=G+IhnM50MFRxhMavYAE60QSdf8HNpIToydxf4EMs47bmBiksn4iDxc3x50SzKj9i0P YcAI7C7m/4Be1Yo6UHG0mudblj0Oh8FHvKRvqn3zq1x18VFpkvwSftSCFqMzEPuo0WpK YdAVwwH+pWaE2mdvhXm1seTX31vA83Y4KPZ5viC/287LEc1Y4pS1l+D2NdXO1QCFd6gp xhBkW/XQNaN/POwE0PsLn8J9uWrnmEBhKIMXX8WQvuGhQalvTIZE56tgbsjb+1mIyed+ H20WoCNve1TnBQFjBtvS5cEhr+P1YDvamy+5Ey4BzwUHyIyrCPl52KcQewxnG9PWVV91 yMcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708995879; x=1709600679; 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=IXlFc5ZBorhTOulkewfZwF8BMlwYQd7ekIX0pYnvaM8=; b=CEXtyL8JkeGIBYh154Js2WvNXxsXYPXnCfMjrPoKyAFuhKAwaoz1FY1Liz/AxCKxuE FzeF8wxVFeptK9NBmbKsmQfOSATcVs5rnFPd91BXl4sCyf8NT6+SCqxu+RJjkQ7aYwea OPPlXojKfAUjjOrTSZedsOFLFByMhidVW14k9LTaMXW8pYrtUl5zWWp7LXy+eSqCTxub cp1QVL2b/0uru6A5F+VU1EnYM62uinG4niWYnTBiBK77J5uv5gil9gLxuzRZcz8GpKin KSL54dfoSqJsDSuMb7V7EEVIo+DIW0pv2rxVWGe8gYF2ClB/TUVR9Qm/cQTlku9PwMIy 57qA== X-Gm-Message-State: AOJu0YxWPDSCreO1NbhtXNpO7or2JYG94TBsSArJ90kdvdqCS4lsbSnE S3GejhspaXEABNh+14Wj+gwKoVLzpuaqcX00p7Quxf1It1Gt0IXH6TbWtgmi X-Google-Smtp-Source: AGHT+IFpaXha7NmQhaRYgZIyHXcTtSdayEG61fafa/9VG6VksTSTID+cXIHDSoxv6KksFsKbb0CMpg== X-Received: by 2002:a81:52cc:0:b0:607:74b2:578a with SMTP id g195-20020a8152cc000000b0060774b2578amr846266ywb.6.1708995879273; Mon, 26 Feb 2024 17:04:39 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:5f7:55e:ea3a:9865]) by smtp.gmail.com with ESMTPSA id l141-20020a0de293000000b00607f8df2097sm1458818ywe.104.2024.02.26.17.04.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 17:04:38 -0800 (PST) From: Kui-Feng Lee 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 v5 1/6] libbpf: expose resolve_func_ptr() through libbpf_internal.h. Date: Mon, 26 Feb 2024 17:04:27 -0800 Message-Id: <20240227010432.714127-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227010432.714127-1-thinker.li@gmail.com> References: <20240227010432.714127-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 bpftool is going to reuse this helper function to support shadow types of struct_ops maps. Signed-off-by: Kui-Feng Lee Acked-by: Quentin Monnet --- 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 Tue Feb 27 01:04: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: 13573037 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 847EB46BA for ; Tue, 27 Feb 2024 01:04:41 +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=1708995883; cv=none; b=hyoHylR5ouE3soGoFEsVQZN+Vdz9UiICTgWxlozv7liOYUUPbJPvwuAS6m5FXcft0Drctx3NNdv7/mO9j3Dc9yankvKrxH6JV8FYdZzUrm4tOyA3BqjCO4RxivBPxRydyH6akIvr3SSwNuU2ncXs2a3xqUFeG+qcrH1JcWELIkM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708995883; c=relaxed/simple; bh=CeAyJrK+gvmBgfV2+YuZ++lyP03MR25D8rBYHwFOxcs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Iy2iYszgrZ2Jg4KFz1K+4Lx870hGVDEYSvCIfeIntZT3yBfII63LTm9cCutlV4b/o/he3Y3E07/ggS0h5pPS7PHtdA9/ECpMcH7pa1NtzR5/WM3dt85Sd1YeicbeRr/+gfOZafP+blV5EkeG+edkyPVLeOFHQfYsSTwHmiiNNKU= 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=esBo7zzH; 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="esBo7zzH" Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-608cf2e08f9so31116767b3.0 for ; Mon, 26 Feb 2024 17:04:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708995880; x=1709600680; 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=mE/3uqBHMWR8C9gy7FsW/eYUhdQf9C2bfAKUbxEVGxw=; b=esBo7zzHllRtVjMOw2GvS9s6b1UxD7ExCFnKjfOFQUjrzmo7ouxL5THeNaz5eOC0nd ufoDtpTtu9G5c3856sfIZh6cjDEglNdf0KOnjr00XQSvEGkefkBObgWZWwLu1SE3Npyp Pip1fOm0tlHbdTUFUoWjYHxBiTsuldFW9A6xIpl2+UTYL67+oAkZW8GIWHNMHGbyEtB3 Hg9hUjahkuU2v4+CLUWq+Uj8SMIBJyzvrZTLyiAVZ6mW2VKDr/3Nf5xsuCtx4q8MD8gA yAa0rOycFDsJbZaDIGdPaAbyu0tkgRHAVrWNwI61i3ipgx1p3f6WpzmwQpHgknMEukYW pM9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708995880; x=1709600680; 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=mE/3uqBHMWR8C9gy7FsW/eYUhdQf9C2bfAKUbxEVGxw=; b=MbXIAwDJIRIeQ9gDN0+dF3zzAC+CKdgWlUgRUlBpPKdvViPep091/CpxpjqESl+MM4 rPbWsMn+G0PZ/x1TU4Yu44v4KkKHOQsT/ldYpLx2z4mzjdy4CVmfs3OU4Na74rIESv4f 2kOdm47RjLcX1DhzJzkqVgBZdCZDG3cKiYqqnV+ZI/AjriHsXOpi5Vb/BaPZqiB+bRwp Uq8cLoWt8vHO9XNZrxMIqbon24weIOq3TBlMlBA11tAoclKfYumhD63EQDOWSGPD1Z7b C982mqH+B0JEJPbgnS+26EMl2QOsB6uvtxkQ+mmnDXuaT3nqmkVVoHps4As4u9mhQDwq exCA== X-Gm-Message-State: AOJu0Yxhmaxvl1wnhAkR8rTJYBEUHmWVrh2Khd9R4Gw2eNCjq/1D3kB3 elHJdbt6HcFMxAwsR0JQ/dxWyHBnlDnoyoRnFXvUMb0L+UgYuamSnh28uD1I X-Google-Smtp-Source: AGHT+IHVKtVFVvBl+AxNQcoc4I+j5rzuOu4UvCAsB0LMxVwXLaKX1/O9cMw/Dhf6Mux0ETIJCfnmNA== X-Received: by 2002:a81:9842:0:b0:609:60b:5e46 with SMTP id p63-20020a819842000000b00609060b5e46mr690822ywg.9.1708995880393; Mon, 26 Feb 2024 17:04:40 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:5f7:55e:ea3a:9865]) by smtp.gmail.com with ESMTPSA id l141-20020a0de293000000b00607f8df2097sm1458818ywe.104.2024.02.26.17.04.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 17:04:40 -0800 (PST) From: Kui-Feng Lee 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 v5 2/6] libbpf: set btf_value_type_id of struct bpf_map for struct_ops. Date: Mon, 26 Feb 2024 17:04:28 -0800 Message-Id: <20240227010432.714127-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227010432.714127-1-thinker.li@gmail.com> References: <20240227010432.714127-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 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 Tue Feb 27 01:04: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: 13573038 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 270104A31 for ; Tue, 27 Feb 2024 01:04:42 +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=1708995884; cv=none; b=Ea3taq2aqr1TXgtaP4jKaQkJDm29Bv6Fq/hbGsDslW1ZKLLpL81M9PZqdQqPa1wfgHfuimYkK3/aMlzs0AhceE5jh1WDEI8QxwO86Ckn5uiZcYeVEHrACf4OFFne2RIR7ZGcb3iSPRMeLBm/o3yI8LdFsVWl1/gJw78WCIC7GSw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708995884; c=relaxed/simple; bh=qlzIsVIhJLxgRDzA0yVSTYms/ohIRRDxP4a22qXzz+Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kaYRBdx5Q491UdIlA9I9Z2LAik7HlJkFtVmXCrSuaD+flHQ0c05qL4wXHRBJFlSEM9vJkZFl+AZt3w1xKe6wYI0lHP/yjDk2AhbmmybYB3hFO6NHVeCHAu1oS3REFhphoFuduKwDG+Ns5Fs//9xtOZhAzXZb8K6e6MXG5MBNRws= 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=IodHR3ss; 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="IodHR3ss" Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-6092387bdd0so3194227b3.3 for ; Mon, 26 Feb 2024 17:04:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708995881; x=1709600681; 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=NMqk02IIEeZvjATR0snmBdS3w8x6eoKpCnnb8BcMKSM=; b=IodHR3ssnDwazC8Ij4tqW3JkM03uWv5wkFoRDUiuGEkus1IS1AuaM3qi0v1SoaOvtY +RGRce+RItQMxASl8e72R5cRcRZ4Lssdc4IXwekWE5kMYxkDFbEh8/ci4nERXI0D8jv9 rxyt1nzhkBQLo7n2AAeVgV1swPeHfK+ix+kNsbXBbzlqvNnDo+B0QUjzYEBnWpkzLFbt +uL0FHwhRiydwseEWuqXXYRAy+7dAgop8O2o9qqPV9rL16zASVwvD8mTr4eQEUGsD0S7 6abCJGNevNncr3vCWN2HhZosophnh+vM5jouptDkWcbuX2XQegNCAufIt7BkeJ0iPpL2 ZPUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708995881; x=1709600681; 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=NMqk02IIEeZvjATR0snmBdS3w8x6eoKpCnnb8BcMKSM=; b=JnF0M9tb5qSzbfSgN3Ons5k9R1R3BhMh9YqXbK0ibekOVibln06FlSRXlzhQ0esv+g BPzfw1+7L++DkI95z5JVSiVRGEVB7I6uhq22EHHbvhRYLYHPI1N2lfLI/yCihukRLawf xTAc1NQHtearI3knOF2QjaZMZpgMXi8WPkVUPDj6qsBdHbEX5AIC8S8hrhfY2ZKX988D eA/w7kPHcBXUm+Jcl6QZLOB7INnfgB6mf146lilUm3k+iMPonPqZ26bP2hGWgdoMAsfG PwJPY8W6x8FeqOQzMb8S5E7WHFEjliNzv0x0ZtuiBcUcAyPjjTdYsPufbcLAboJrHHsW KSsA== X-Gm-Message-State: AOJu0YwSRjBlCHS9muJwYVc2YSE7riBPxc9e1lJkKTU5sP5C0KqXsJGv sVVl3cS4/HrY4QurBumI3i8OW3FXt/NTgbSrRdnaiTlePlSjCg8eHmlPofK9 X-Google-Smtp-Source: AGHT+IFa5O5q5I+PNjkPQkkOURTyvdmUcYwrbh1dLVYtTEZNeg6IAYyHXDamsPVlqyJlutMgwF5YzA== X-Received: by 2002:a05:690c:ec8:b0:609:15e2:1e2c with SMTP id cs8-20020a05690c0ec800b0060915e21e2cmr1018490ywb.27.1708995881533; Mon, 26 Feb 2024 17:04:41 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:5f7:55e:ea3a:9865]) by smtp.gmail.com with ESMTPSA id l141-20020a0de293000000b00607f8df2097sm1458818ywe.104.2024.02.26.17.04.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 17:04:41 -0800 (PST) From: Kui-Feng Lee 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 v5 3/6] libbpf: Convert st_ops->data to shadow type. Date: Mon, 26 Feb 2024 17:04:29 -0800 Message-Id: <20240227010432.714127-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227010432.714127-1-thinker.li@gmail.com> References: <20240227010432.714127-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 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 | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 465b50235a01..2d22344fb127 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; @@ -9308,7 +9311,9 @@ static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj, return NULL; } -/* Collect the reloc from ELF and populate the st_ops->progs[] */ +/* Collect the reloc from ELF, populate the st_ops->progs[], and update + * st_ops->data for shadow type. + */ static int bpf_object__collect_st_ops_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Data *data) { @@ -9422,6 +9427,14 @@ static int bpf_object__collect_st_ops_relos(struct bpf_object *obj, } st_ops->progs[member_idx] = prog; + + /* st_ops->data will be expose to users, being returned by + * bpf_map__initial_value() as a pointer to the shadow + * type. All function pointers in the original struct type + * should be converted to a pointer to struct bpf_program + * in the shadow type. + */ + *((struct bpf_program **)(st_ops->data + moff)) = prog; } return 0; @@ -9880,6 +9893,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 Tue Feb 27 01:04:30 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: 13573039 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) (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 2ECEE4C7C for ; Tue, 27 Feb 2024 01:04:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708995886; cv=none; b=KQTd/o74hMlLEEGtZ+7X5OLkduyyw0PfRGdycgPU8wBqH6z+SgfhZ8H5xOe2seVNHOKmBdQBbC0q/x+GESLDYgifHEfbwzlt+QdDa/2Wczf7CJ1rKlTzAt6LM8Fnp+0oAgHfMlLlOzSFhCjOo/YfTcHTN9srDHMMmhdGHB8UvMA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708995886; c=relaxed/simple; bh=mEvLAloVIspqhwIUvzIcrG22pA+JB5eYsnQvK3jUHNM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tAIq+gWDcmDMRAMKdNEuh5hP689Gt7gPiZak6qRYujgfrz++eiX0iVInH1yLmwejxgez/QhLVnU/uIj5iuktO/2urPqnZDnCk9y8CLrF4K+03+BLZahWDGDJ0iliYY2LvmHzS7AX5nzXmUWxRDQ0NPivwZTqbd3tSGE9sY+nK0E= 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=TdjmLo7z; arc=none smtp.client-ip=209.85.128.176 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="TdjmLo7z" Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-608d490a64aso26314817b3.3 for ; Mon, 26 Feb 2024 17:04:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708995883; x=1709600683; 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=Er7aQMtBCvYCPviplDKaMwTF+uVaAe3e+cogFyaQWPQ=; b=TdjmLo7zdkkimwi6Dov84+lpDnJu6nhJYm92el0kHgU31APPjzaO6lUOBpHdG3ne3M 9zXkPltv3XpSY+nr+B02sIgV5WgjGe+f93Bu4aSGPFfMFCzF1+VH9Q9a92LaxkKQx75H nZBGpZnLJbLICJ76pygwvLeeYzbMGMb9i2pLwc31Y00EtC+8uTPI3kodEd2gM2XnEKeL pKZGqZnyF9W3mHXD8mUVs1yClySe2CyobVmeJr6IZg/Qd6KfSDptRGhsMuReIYOutE1R LNaV9tr2n/H8/qNqxt+UV+FVKRSzA2boKY/gZkmEfNenoqrj2MN+uVpYQzcWuVwIld0m Ltew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708995883; x=1709600683; 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=Er7aQMtBCvYCPviplDKaMwTF+uVaAe3e+cogFyaQWPQ=; b=GSHOcklX/xLi4k2YtvU5EG5nR29JXHdXW5Ij0Fv/1ztoG/IUT1+cBZ6Gdi49FNwLBY h+gkZAXH2yg6ajTASD3zyieQY64HQDx84JB3RbGtjycnPWvOMSTkcs0Ty8R/eoRRbRtF UD9jWrV2LWoCaAJG2gLbnhmza61HG2YMrhi4H4Hpc3tqxt0JdjOIUPtk5IC4ajaZQrhI poIX+EX8tDac+dSSBUS15oETzDWV69GLPUoa6EmXLMQfOgJ2nO0/PP9U17hagHkh7Q7l S3xjp3ODH8Pu1seFaXEE9d79l353St3HCC/kODyg8M0fXgmXOfuUnrcQlD628j/g5kt9 r7OA== X-Gm-Message-State: AOJu0YwQFhBH9CmuxI8RXuKSS7i8yVyq7Yz/DZtlBmTpklrJCG6twYgl Y3dIK45Jn298DvttShUUhEPF4Z9YIk+8DJchQ3EnJIyoEd7NSRQuXN5JJbsE X-Google-Smtp-Source: AGHT+IHnJ6c1awfW7YTEq5a+o3H+dqKieA557ACebDBsTy15H+zMsDSFa0Eib23Sf7XMkj10zGBREQ== X-Received: by 2002:a05:690c:fca:b0:608:b9e1:20ea with SMTP id dg10-20020a05690c0fca00b00608b9e120eamr880664ywb.45.1708995882702; Mon, 26 Feb 2024 17:04:42 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:5f7:55e:ea3a:9865]) by smtp.gmail.com with ESMTPSA id l141-20020a0de293000000b00607f8df2097sm1458818ywe.104.2024.02.26.17.04.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 17:04:42 -0800 (PST) From: Kui-Feng Lee 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 v5 4/6] bpftool: generated shadow variables for struct_ops maps. Date: Mon, 26 Feb 2024 17:04:30 -0800 Message-Id: <20240227010432.714127-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227010432.714127-1-thinker.li@gmail.com> References: <20240227010432.714127-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 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 Tue Feb 27 01:04:31 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: 13573040 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 356EB4C8E for ; Tue, 27 Feb 2024 01:04: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=1708995886; cv=none; b=kIN4ZYlNdQgWRaYeBwS56Dk1tCnD6nTZ1QuZhn4xV7aE5I4oXGn66Aem+4OdEpi1R3wByLL7jiEVp2kpNmDVaGWE3y405rXH/7U+2LS22kNNSjVycVMlRsd3gOSaoFa1nvPKVJV/6Yg7hD1eYro8jEUUdd8LO6DWOUn7FGHGcrk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708995886; c=relaxed/simple; bh=kWvE3w/dssfEwW9VwQvUfG+MhBRRVDMss+Cyg3QxDzo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XrzVlGh/ERhe1uay8Kq3Qy0gmFK9qTIPOuEdOpvbyhERwLntY1N8eaGRlRKirAO22X7Qb6osmEmAO26zwWyl+yWxmBPJiCJlOd7dINNvNldSHu1IMFlM0pURkZzGztTv/ZK8nN+g6zhvUy7B1OITpzDpW9I+0Rv4Q7hNh92XyKU= 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=UoMOBCci; 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="UoMOBCci" Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-60915328139so11725887b3.0 for ; Mon, 26 Feb 2024 17:04:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708995884; x=1709600684; 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=SLz74OM2Mxrx78KFBZW+5OCfQUjWPx+I7SN2WTQ9+1I=; b=UoMOBCcief9J0XRJYIbhyfa36mf2lIL/LX/MzfwZW0cMrejiChbT3EFaB9c3gX8iea T+XbxSV3xZH/KzoJ3iaIXyZf4khpN2AxqIpJrKy7LQJGtOtBC94ekEqPunnU72a0Zeka 6LG6KMT/NFRP5Hs9226Lgc44b4GZPxpAPxQi645+4DxPLAK13dRUzgvjTHtaJfiSUvYw PR2EbFuSNo72o9McaqsdIxdGPhGprQ5ioAlK9NFak1UbZvBt+LSxr30gn5xkSNyCOi8a Q/svpfvxFyJkCpGtW5C1+iVvZeHsyZBHqIbPAL2JS5yV8+7EdZYRlbyj+ELvSOhGE0dG URVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708995884; x=1709600684; 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=SLz74OM2Mxrx78KFBZW+5OCfQUjWPx+I7SN2WTQ9+1I=; b=cME647qpRFxDPPEMUhuECzpb8WdWZuzqG25e1GSTeLBbL8QvBuIbbyFlVPn6NxV+j0 BU5o3VNTrCY1cZbspKST3J+IIFr8sL09ztUay+MBPe0IMEugaAWepWmvPhTuq9dd7vae sHfeivA+wKq4F8YJl64eI713jkqstyO+eyLUYIuqTthImRhm8yFhAyVY34iZI1/Q+SCe iyFfpZrx5Wsja/0qdIsmGcrb1FhoTsW9lk1L7f6A8RN+ZQTG8TXMNoTwuviTTFRQsWyE r/eHfEJdxlTrPTer0nJWOxPtdhAoPhFuSYSAEqm6PWHpJCgPwXO0H8PDkclLeoOxi2vS aG4w== X-Gm-Message-State: AOJu0Yxiwbx/P4+z4nchqUS2Ulu6yxcM67V9WJQ1r/HleaPQCIPw5SmU hh+tYbRc33DqTXoMrkkODgb4nSDXegxI9MtaS8fN49YfyPPOM2YxrzQZtVzc X-Google-Smtp-Source: AGHT+IHaUD7UKSHYpt+Q3CMqd7DKyJWeBMjULO2WL76/DPHfT1xKTDzJs5WnOYk+crr6BCsTJLV3IQ== X-Received: by 2002:a81:6c03:0:b0:608:856f:25cf with SMTP id h3-20020a816c03000000b00608856f25cfmr790575ywc.46.1708995883837; Mon, 26 Feb 2024 17:04:43 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:5f7:55e:ea3a:9865]) by smtp.gmail.com with ESMTPSA id l141-20020a0de293000000b00607f8df2097sm1458818ywe.104.2024.02.26.17.04.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 17:04:43 -0800 (PST) From: Kui-Feng Lee 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 v5 5/6] bpftool: Add an example for struct_ops map and shadow type. Date: Mon, 26 Feb 2024 17:04:31 -0800 Message-Id: <20240227010432.714127-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227010432.714127-1-thinker.li@gmail.com> References: <20240227010432.714127-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 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 Reviewed-by: Quentin Monnet --- .../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 Tue Feb 27 01:04:32 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: 13573041 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) (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 456864C92 for ; Tue, 27 Feb 2024 01:04:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708995887; cv=none; b=c8sNvuU2Hd0q87FqoC2ODE45fdQlwTLMH7CpOJWh5SIuLUDEjpMztB8/QZ4wyc7uLGU6VoKV4Vyd0bV06jCUFrKxhjeXcqcWye7oSCajQ2HyO9eoP9q6I/wMFYvOsCs/C6i5DeCtV/WRxBlK99g3y+ESVL1W62Q8zaLOwUbGFlw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708995887; c=relaxed/simple; bh=kn6v7KWUs2y1IgJecF+yvFbM27olTQ2TFVTijy4UfYA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FtXCOfRllR03QjO+ShJoXNXWc+8KT71X/RKfLBX8M/3ixkQVa0lQ1MjkiujQ0sAh5j0TR+/A7KglhwPJ8UaA3ZkWQVhg2KrJCIWMs+YcUFw9G1uTDHiAYGo1YjJkpeh8ts3BoR9fErNw6VRJUWnnpTakEYcYaqwfgRRBsstVUus= 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=OS+qtaTf; arc=none smtp.client-ip=209.85.128.176 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="OS+qtaTf" Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-608e0b87594so21894857b3.1 for ; Mon, 26 Feb 2024 17:04:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708995885; x=1709600685; 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=uD8m4gd8/Bnjn5MvoeiCe8GexY6YWfPcvCxDml5btb8=; b=OS+qtaTfQWXj8iicZlrInQQeGQrOS2fCP8p23az2uZCfyzd4+Jol+T3R4ae3tU98Ax o7B21bvgHK49ToY3L1V25e74DW0qTnYhp3pTrzIRdeb2uuP6UVEso6Cimx3H8nyhgFHA k5ufo05G36wXMQ5Hkvd/ERUR5y/QH7GtHT/Qlcf1Y+cVyx3RCda/nxRhMWp/KtRPHQnV jIsSyLJ9AP+H3oW9tnsFDSGq1wEVxq2E15NcY5Kxb5EnuyRQC/8RXH49CKV7xSNSbkli Lg4TZZaUh4UZBm9RqRRaeHn03tY8xvQv+ZY6cuohRh/s5LXSKlQgHLMUO5lFwUhCzGIW R+ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708995885; x=1709600685; 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=uD8m4gd8/Bnjn5MvoeiCe8GexY6YWfPcvCxDml5btb8=; b=vURdRyOUtzIV02uZP9HSpWMz7A10fPLVMNIlisAnyU5dxLMY2sd+PSvnsiVQpglWj4 LkzHt0nxaDcTEmGFZqbu6TeLHFREA0nEZ+2Z9/hSMgsFKt6xZZ+GCsaYdIecselJl/Bh JlL86A2iQEstXKvH0Dmga75/k17pMj+LDtP+RHZ/dxlWdApaGtRoqzxsZ/BIEDAC0UHs 4K0GtxQ9A5goGE/jzbpPhecn5vn5nIsq7DjRHOA3XGC7bd3E4ss4xIipAlyl58L4snyG sjaOK73/ON3+Vy4EO61r7FC/fCHwn6m0GyxFEbq93TrbFRDId+Q4E52nWYV6utsAHL93 tneg== X-Gm-Message-State: AOJu0YyfkVkkQqYktKHXRDhWGUOjdCzdRVoZRfmLq8hauyg0uvZwppfJ xGtjPddr543Qdh6R1QuepcPBegBNR4JjBDe1H6VT01fEOPsfvVqlH5+bac/f X-Google-Smtp-Source: AGHT+IFYikZHYDXuA9KcsDiaYYUsMQU6J2Dq3bZMQaMZYyPumjT3RpyagdSas9ynXzN6L8oBv9z9YA== X-Received: by 2002:a81:5751:0:b0:608:8f31:244f with SMTP id l78-20020a815751000000b006088f31244fmr669571ywb.51.1708995884994; Mon, 26 Feb 2024 17:04:44 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:5f7:55e:ea3a:9865]) by smtp.gmail.com with ESMTPSA id l141-20020a0de293000000b00607f8df2097sm1458818ywe.104.2024.02.26.17.04.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 17:04:44 -0800 (PST) From: Kui-Feng Lee 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 v5 6/6] selftests/bpf: Test if shadow types work correctly. Date: Mon, 26 Feb 2024 17:04:32 -0800 Message-Id: <20240227010432.714127-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227010432.714127-1-thinker.li@gmail.com> References: <20240227010432.714127-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 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, };