From patchwork Tue Nov 19 10:15:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Protopopov X-Patchwork-Id: 13879560 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3ED8013DBB1 for ; Tue, 19 Nov 2024 10:13:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732011195; cv=none; b=GO4II3HnJe1eb2/QkDSMf1tRuIFwp+NBagWS7qDZk2ylrhQRAC6hWdduY+tyxSqpODga//pda2kYiwM99t6xDTHwimONU8Z3v0bZUtXsXruMO4kmAYKSfy9jEUu/ay11NWykf+u3L280DWqfATLDgKrswMnahHRBVTMSrtzTIac= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732011195; c=relaxed/simple; bh=G3wa07rZYgN6CVGveQn2J5nSIZh5i+vLZIp5qLRAfWo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=K1GxYChSZe3L2jmok0brDLqLpp+B4SYXbDWgH5K+r5LOYX4x3y3lH6yoxM36jKeWbHVcdYjfrQJ4r7Nn+FAXiu6348ZFUCAhpLzU9NpqejibaPBrvNNHcNbzr2xsTzJKY/ttfppjMvzZy7GoXYFrp3qVWAu907LG8wuxpmV0nFQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=isovalent.com; spf=pass smtp.mailfrom=isovalent.com; dkim=pass (2048-bit key) header.d=isovalent.com header.i=@isovalent.com header.b=e7lMxl6E; arc=none smtp.client-ip=209.85.218.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=isovalent.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=isovalent.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=isovalent.com header.i=@isovalent.com header.b="e7lMxl6E" Received: by mail-ej1-f44.google.com with SMTP id a640c23a62f3a-aa3a79d4d59so123430166b.3 for ; Tue, 19 Nov 2024 02:13:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1732011191; x=1732615991; 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=uoG5JO9syP1TPphV80q7oHvJ3++/Z/0Nk/f2xqd8VJ8=; b=e7lMxl6E0ANSfpl+MFpbI24+gFJkV0cNYvRTEHhtO4OVgsWWzTbNB1Iex+D43vSa6C 7P8HniPU+q4yDLLc/XM+VOcmwhLu/5TVXGMnySznCOV6HExso2+j9AieUjnBnrzB7Kw7 CUtIIpwTuFIdLnntplXkLZ8FLvxjaZBZ0FgM9J2wWV04VoMlRCm2NVcq/Cte+LlAYO4+ 2XzJZ2FhhUr5kAF1M8mlP1YS2jJja4vNt2F7maMqT8HdYnEwzEXlQ8r5PZr6nBdE55M+ tA1KQC/6qM2alS7VIfdEPAmzjnsj+adjdsvc3JVMEre5ZG2IfHkqTn//XXzsy7+6Ts8P OLuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732011191; x=1732615991; 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=uoG5JO9syP1TPphV80q7oHvJ3++/Z/0Nk/f2xqd8VJ8=; b=Sq5WHW0ZPjt8IFaZBWhq4BXaEc+sBSo/dvP9BI76c1aH5zy2RjNkuf4W6BqKWhGGSi P9L1qkcXdGWlAqhGLF8GbBRg7qFUrgG4hfARnIIbJmmvz3lCqIDOIhpyJm5yhaMY3T0z t7dYCAyNOJEg0+tI5RFDr1nVTYZjy6hQly4Hg+CvpmcbO7Z9rqxBeHWAR7TQjP6cjB2H HIGWV0x9QosK1C/9y8b+Ig+sAlWEAu26mD3FrDtcCcnpsyEIfzyzkmJclsCiON3bcshT 1qyMzj+VD3vrz0nH7h5OtkW0j4c8/QjgLJ4RfsWyKhLkkSANqdF2pWxTP7SvmdfBgMx2 OPdQ== X-Gm-Message-State: AOJu0YzXiRqioK5M83pnEn4GprBhdlWqmh5GXUuS/HmT230PVa/SfsMd V48Av4/mBOcZpYANxD2BTfgf6Y9Ka8Ciw8D2ijXqXCuXxBOmQIONBx0l0YVx+UoiTjISWLtDkzv X X-Google-Smtp-Source: AGHT+IG5Znv8naabKvI0Z28q60PkEgTohof//IfJpxdK6tz1td6QkuoD4FbrdCS+W4Mrajzj4YE8BA== X-Received: by 2002:a17:907:2d90:b0:a9a:230b:fb5e with SMTP id a640c23a62f3a-aa4833f66f2mr1568467266b.4.1732011191307; Tue, 19 Nov 2024 02:13:11 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aa20df7eee4sm629003066b.87.2024.11.19.02.13.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2024 02:13:10 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org Cc: Anton Protopopov Subject: [PATCH v2 bpf-next 1/6] bpf: add a __btf_get_by_fd helper Date: Tue, 19 Nov 2024 10:15:47 +0000 Message-Id: <20241119101552.505650-2-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241119101552.505650-1-aspsk@isovalent.com> References: <20241119101552.505650-1-aspsk@isovalent.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 Add a new helper to get a pointer to a struct btf from a file descriptor which doesn't increase a refcount. Signed-off-by: Anton Protopopov --- include/linux/btf.h | 13 +++++++++++++ kernel/bpf/btf.c | 13 ++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index 4214e76c9168..050051a578a8 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -4,6 +4,7 @@ #ifndef _LINUX_BTF_H #define _LINUX_BTF_H 1 +#include #include #include #include @@ -143,6 +144,18 @@ void btf_get(struct btf *btf); void btf_put(struct btf *btf); const struct btf_header *btf_header(const struct btf *btf); int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_sz); + +static inline struct btf *__btf_get_by_fd(struct fd f) +{ + if (fd_empty(f)) + return ERR_PTR(-EBADF); + + if (unlikely(fd_file(f)->f_op != &btf_fops)) + return ERR_PTR(-EINVAL); + + return fd_file(f)->private_data; +} + struct btf *btf_get_by_fd(int fd); int btf_get_info_by_fd(const struct btf *btf, const union bpf_attr *attr, diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index e7a59e6462a9..ad5310fa1d3b 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -7743,17 +7743,12 @@ int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) struct btf *btf_get_by_fd(int fd) { - struct btf *btf; CLASS(fd, f)(fd); + struct btf *btf; - if (fd_empty(f)) - return ERR_PTR(-EBADF); - - if (fd_file(f)->f_op != &btf_fops) - return ERR_PTR(-EINVAL); - - btf = fd_file(f)->private_data; - refcount_inc(&btf->refcnt); + btf = __btf_get_by_fd(f); + if (!IS_ERR(btf)) + refcount_inc(&btf->refcnt); return btf; } From patchwork Tue Nov 19 10:15:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Protopopov X-Patchwork-Id: 13879561 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) (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 4289019D06E for ; Tue, 19 Nov 2024 10:13:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732011196; cv=none; b=HnEnZWB56mfN7efNZ5mxvTF5aNr4YMKQRGwCEOzdSfVLuYh04iTp7FKZbzWf8W3TL1ZZEqu26hJ5iQe161vw9e3A8kbUrlDuNhSj2HgLfvnKxFs5lsQH+scYXQgFQMOn2/k9cJamwvldjNF3n1q1XqoaIQBGyUAZM4pjnfLncQk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732011196; c=relaxed/simple; bh=OpOIqZtqfIYvIxbJ+K68wcnxSuyMbBUxcy0WZzOU8NY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nE/J9o+bBetv5ezmMKeJ/QLAhKJRWEZm6+DUe8BxQyXvAC+LzwBCbtML+rIOrd/2tddVuaw8FYI/mDp9udVVyExn2EHmj13mGOjE5HyXr3RzcssZic5mwcVgE52uoKhWzdoLnZtIXlDh2etQ1YVY+dNpK/dZob2JxAy45dYvEbo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=isovalent.com; spf=pass smtp.mailfrom=isovalent.com; dkim=pass (2048-bit key) header.d=isovalent.com header.i=@isovalent.com header.b=W16Ih5g8; arc=none smtp.client-ip=209.85.218.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=isovalent.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=isovalent.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=isovalent.com header.i=@isovalent.com header.b="W16Ih5g8" Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-a9ec267b879so680612266b.2 for ; Tue, 19 Nov 2024 02:13:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1732011192; x=1732615992; 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=pGLT+E5YHsywRgyQbGHDFcLaKg16gZ+0JL0g/cmKloE=; b=W16Ih5g8PACTX+uKyhGgPFQvZYKXxxiKJr0x8xH0ZQoAgFHJOo65OvUBAFcxgid/xe 8q3adi2JSn4ePh10fg2hYvjkP2qmaqZTN7NFC5/X/HlyByfWNjmwDMaMPGLJ/Vw9f6Ki 09OUHMpSrPc+fq0fa6h72kr1PLsEJ6NGKwdUrsh35ijz2e1Vk+e7d65vZjqVoC1f3A2v fTX/DGMY7rGJ4WMa9HNidaIoVRS7KmlblDGgqAG/Pv8hnVepT9itRUTe9IBCDawtU5/9 loCzR5V0z6LBfIZIsjXCa3EyT5CB1Xxjh5InulGzwCgHCur7sVbqhrmfy8tdIX5SGebG k0Ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732011192; x=1732615992; 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=pGLT+E5YHsywRgyQbGHDFcLaKg16gZ+0JL0g/cmKloE=; b=KDUNBuR5cYZmjzgt3eTkUMfkyeg+J+XQmMuoxRFTRifybzFaDWucxfFx4ZLj5Sb3BZ y/8eyXQOEr0dsONTC3PjmJ0kdzsT2iz6no8pkgD9MP7mSNAhY+FY0lHNfp/xo8zaWI7o vSG3C+u+s6V3K/oh9VQPv1owBol9qNYoUs6s6NPnm/kdE2uHvfhSbDU8+nxar0e+pdlP 9UoexQXYkLDZXMDDhoz4pIpsvcIqkdfwxyGPmHwi26SRaVw3RSkZ2oXZAA7AhEhdFZbD yPuXofV3FO7FG2OMrNs8b4S9ta16vvCmvYgHRW17G4d/5oorYc0SO1ntRsFEo/iWCT8L Us8w== X-Gm-Message-State: AOJu0Yzx1kM+83nrq4Q/quuZul+scNTKZsvOgVmTedNaDGvLFzheFSSV PEtJ/2SBITolFhQaqly2Qc6XSuFs2f5nBg+VuQA7oxuHfKESZTmErcziiu9cfQLqR+j7K2dzhBG v X-Google-Smtp-Source: AGHT+IGyCu6TbuJ2iR8mob0xun0GfS6bK7GZqbL4TyHbt9WDc9wmIlHRh0qVdh39djlH7nYkoZyFVA== X-Received: by 2002:a17:907:9624:b0:a9a:80bd:2920 with SMTP id a640c23a62f3a-aa48354b9ecmr1308083866b.53.1732011192280; Tue, 19 Nov 2024 02:13:12 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aa20df7eee4sm629003066b.87.2024.11.19.02.13.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2024 02:13:11 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org Cc: Anton Protopopov Subject: [PATCH v2 bpf-next 2/6] bpf: move map/prog compatibility checks Date: Tue, 19 Nov 2024 10:15:48 +0000 Message-Id: <20241119101552.505650-3-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241119101552.505650-1-aspsk@isovalent.com> References: <20241119101552.505650-1-aspsk@isovalent.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 Move some inlined map/prog compatibility checks from the resolve_pseudo_ldimm64() function to the dedicated check_map_prog_compatibility() function. Call the latter function from the add_used_map_from_fd() function directly. This simplifies code and optimizes logic a bit, as before these changes the check_map_prog_compatibility() function was executed on every map usage, which doesn't make sense, as it doesn't include any per-instruction checks, only map type vs. prog type. (This patch also simplifies a consequent patch which will call the add_used_map_from_fd() function from another code path.) Signed-off-by: Anton Protopopov --- kernel/bpf/verifier.c | 101 +++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 55 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 1c4ebb326785..8e034a22aa2a 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -19064,6 +19064,12 @@ static bool is_tracing_prog_type(enum bpf_prog_type type) } } +static bool bpf_map_is_cgroup_storage(struct bpf_map *map) +{ + return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || + map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); +} + static int check_map_prog_compatibility(struct bpf_verifier_env *env, struct bpf_map *map, struct bpf_prog *prog) @@ -19142,25 +19148,48 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env, return -EINVAL; } - return 0; -} + if (bpf_map_is_cgroup_storage(map) && + bpf_cgroup_storage_assign(env->prog->aux, map)) { + verbose(env, "only one cgroup storage of each type is allowed\n"); + return -EBUSY; + } -static bool bpf_map_is_cgroup_storage(struct bpf_map *map) -{ - return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || - map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); + if (map->map_type == BPF_MAP_TYPE_ARENA) { + if (env->prog->aux->arena) { + verbose(env, "Only one arena per program\n"); + return -EBUSY; + } + if (!env->allow_ptr_leaks || !env->bpf_capable) { + verbose(env, "CAP_BPF and CAP_PERFMON are required to use arena\n"); + return -EPERM; + } + if (!env->prog->jit_requested) { + verbose(env, "JIT is required to use arena\n"); + return -EOPNOTSUPP; + } + if (!bpf_jit_supports_arena()) { + verbose(env, "JIT doesn't support arena\n"); + return -EOPNOTSUPP; + } + env->prog->aux->arena = (void *)map; + if (!bpf_arena_get_user_vm_start(env->prog->aux->arena)) { + verbose(env, "arena's user address must be set via map_extra or mmap()\n"); + return -EINVAL; + } + } + + return 0; } /* Add map behind fd to used maps list, if it's not already there, and return - * its index. Also set *reused to true if this map was already in the list of - * used maps. + * its index. * Returns <0 on error, or >= 0 index, on success. */ -static int add_used_map_from_fd(struct bpf_verifier_env *env, int fd, bool *reused) +static int add_used_map_from_fd(struct bpf_verifier_env *env, int fd) { CLASS(fd, f)(fd); struct bpf_map *map; - int i; + int i, err; map = __bpf_map_get(f); if (IS_ERR(map)) { @@ -19169,12 +19198,9 @@ static int add_used_map_from_fd(struct bpf_verifier_env *env, int fd, bool *reus } /* check whether we recorded this map already */ - for (i = 0; i < env->used_map_cnt; i++) { - if (env->used_maps[i] == map) { - *reused = true; + for (i = 0; i < env->used_map_cnt; i++) + if (env->used_maps[i] == map) return i; - } - } if (env->used_map_cnt >= MAX_USED_MAPS) { verbose(env, "The total number of maps per program has reached the limit of %u\n", @@ -19182,6 +19208,10 @@ static int add_used_map_from_fd(struct bpf_verifier_env *env, int fd, bool *reus return -E2BIG; } + err = check_map_prog_compatibility(env, map, env->prog); + if (err) + return err; + if (env->prog->sleepable) atomic64_inc(&map->sleepable_refcnt); @@ -19192,7 +19222,6 @@ static int add_used_map_from_fd(struct bpf_verifier_env *env, int fd, bool *reus */ bpf_map_inc(map); - *reused = false; env->used_maps[env->used_map_cnt++] = map; return env->used_map_cnt - 1; @@ -19229,7 +19258,6 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) int map_idx; u64 addr; u32 fd; - bool reused; if (i == insn_cnt - 1 || insn[1].code != 0 || insn[1].dst_reg != 0 || insn[1].src_reg != 0 || @@ -19290,7 +19318,7 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) break; } - map_idx = add_used_map_from_fd(env, fd, &reused); + map_idx = add_used_map_from_fd(env, fd); if (map_idx < 0) return map_idx; map = env->used_maps[map_idx]; @@ -19298,10 +19326,6 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) aux = &env->insn_aux_data[i]; aux->map_index = map_idx; - err = check_map_prog_compatibility(env, map, env->prog); - if (err) - return err; - if (insn[0].src_reg == BPF_PSEUDO_MAP_FD || insn[0].src_reg == BPF_PSEUDO_MAP_IDX) { addr = (unsigned long)map; @@ -19332,39 +19356,6 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) insn[0].imm = (u32)addr; insn[1].imm = addr >> 32; - /* proceed with extra checks only if its newly added used map */ - if (reused) - goto next_insn; - - if (bpf_map_is_cgroup_storage(map) && - bpf_cgroup_storage_assign(env->prog->aux, map)) { - verbose(env, "only one cgroup storage of each type is allowed\n"); - return -EBUSY; - } - if (map->map_type == BPF_MAP_TYPE_ARENA) { - if (env->prog->aux->arena) { - verbose(env, "Only one arena per program\n"); - return -EBUSY; - } - if (!env->allow_ptr_leaks || !env->bpf_capable) { - verbose(env, "CAP_BPF and CAP_PERFMON are required to use arena\n"); - return -EPERM; - } - if (!env->prog->jit_requested) { - verbose(env, "JIT is required to use arena\n"); - return -EOPNOTSUPP; - } - if (!bpf_jit_supports_arena()) { - verbose(env, "JIT doesn't support arena\n"); - return -EOPNOTSUPP; - } - env->prog->aux->arena = (void *)map; - if (!bpf_arena_get_user_vm_start(env->prog->aux->arena)) { - verbose(env, "arena's user address must be set via map_extra or mmap()\n"); - return -EINVAL; - } - } - next_insn: insn++; i++; From patchwork Tue Nov 19 10:15:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Protopopov X-Patchwork-Id: 13879562 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC2771C1F38 for ; Tue, 19 Nov 2024 10:13:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732011197; cv=none; b=jRRIsU6iUmPOyyZ7MZo4jwCCkZa1rSCLwc2wRxNAVWaD9/3jkYvNGBUGL+yrFHWt/liXQp7oSNvdqXunsoCBmC00bQCaMCmzUnqergBE3XJK9ReLgxYFrsvMnbSMkhfyEYIlTbYJiQXKF8eDX855WSjvCv7aZjwbt2xrutotS8Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732011197; c=relaxed/simple; bh=zirssA0mziNJPfvWC+9B0HakpIZiUXZ2ANQBpkBEC24=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=n0p9M94Ab2JEMO0S6IEoCQ1G7OS2VMQkxBSwtfsaSAPVP3OWwei9XjFC3VWRhVvDe2Lrg2ZfTvrEDR7MsVwrJhvzqdzpfxQQB5bWtKnXVelqR4oXPICml5hnbKMJEVqLW3S7f50gzQRU2leo/HvuGlalih3li3eHROTh8oh6E7Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=isovalent.com; spf=pass smtp.mailfrom=isovalent.com; dkim=pass (2048-bit key) header.d=isovalent.com header.i=@isovalent.com header.b=WjPVmjnN; arc=none smtp.client-ip=209.85.208.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=isovalent.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=isovalent.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=isovalent.com header.i=@isovalent.com header.b="WjPVmjnN" Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-5cf6f367f97so5820380a12.0 for ; Tue, 19 Nov 2024 02:13:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1732011193; x=1732615993; 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=WdC16YtzB4SHW0EHay1CU6uRUHGObQ2ODK6yTvLhCUU=; b=WjPVmjnN20be4M51qDtJ+U/wEZBZcV4IjmVRl1SxVDQUzerZXdvTBfqUyKrsuqvDhl N4y6P8SXu3ENetBpnPKl8Dz25HkSkblK5Zu7ZdpM0zQjxO29EAr3ikyBcJWCGQnGaUEl Qek/aJRzmHHG3PF/OnXGn3ozENpA8XpDDGHERiuik9Di0Cd7CouXaZkJBJL3wgk6mIbM 4qJxoE7z2y3HLB4yRXGHit8FLlKLJNl3kM5IND5AeuXIWNsl35AZ3iZeMpmJOf+M83bX ZMsnL450pKjb02i0u8Shs5C+NHO0w/7bQJ2QbriRYlEcyUlkxUOk1iPcV51KQkgp6xkx sQig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732011193; x=1732615993; 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=WdC16YtzB4SHW0EHay1CU6uRUHGObQ2ODK6yTvLhCUU=; b=sRqWR245syfym7m2ob1SWw4SSdlUfMRO78sGiFr1B39Lp9jyPZIgtsvIPPXC/vcVRa Cj2cd/cXSS8l1X2MqSlPcbjyXhiBMb8AaCwtEGrrwYhEYk1OLVFJCiRSSMUavQfhHvFh rE5CcmBh7nyvzvAeQY1krMa6d2VL1BfOQ5XlaXxSG8pUrYvVtVLi4+jTlnTnTghP0OGx JUEhBWnmuFBE4DhP6vo683MqmH5x23owBxO87h5VBLUNm0Q3xRULeITR53FVFNGjshj0 VQ4IQwwE/h0KwdG/rAtlD0ENOUnIk4pdPipCG2KLKDnUBfjqcXnOPL2akukKfra6Amv1 DCUA== X-Gm-Message-State: AOJu0YxO1V9g9U+6AMpP/pcSR6CAtDHX7JPliv9azZi2kzIq3lsAVVK7 mLIEONXioM0riQVjoQKl+3ai48dXDjcUR1dlW001DK5JNcJgYUgTCTYTE4cSA1L+08+vrmOV+ZF Y X-Google-Smtp-Source: AGHT+IEIv4tUHxGo8DtPH2uvr6wpsz1YGqwqf672C+GM6B3Uc+EJaZXJ/gQG1HMSgMVRGFdejd01Jw== X-Received: by 2002:a17:907:d29:b0:a99:fb56:39cc with SMTP id a640c23a62f3a-aa48350996emr1246365466b.38.1732011193354; Tue, 19 Nov 2024 02:13:13 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aa20df7eee4sm629003066b.87.2024.11.19.02.13.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2024 02:13:12 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org Cc: Anton Protopopov Subject: [PATCH v2 bpf-next 3/6] bpf: add fd_array_cnt attribute for prog_load Date: Tue, 19 Nov 2024 10:15:49 +0000 Message-Id: <20241119101552.505650-4-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241119101552.505650-1-aspsk@isovalent.com> References: <20241119101552.505650-1-aspsk@isovalent.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 fd_array attribute of the BPF_PROG_LOAD syscall may contain a set of file descriptors: maps or btfs. This field was introduced as a sparse array. Introduce a new attribute, fd_array_cnt, which, if present, indicates that the fd_array is a continuous array of the corresponding length. If fd_array_cnt is non-zero, then every map in the fd_array will be bound to the program, as if it was used by the program. This functionality is similar to the BPF_PROG_BIND_MAP syscall, but such maps can be used by the verifier during the program load. Signed-off-by: Anton Protopopov --- include/uapi/linux/bpf.h | 10 ++++ kernel/bpf/syscall.c | 2 +- kernel/bpf/verifier.c | 106 ++++++++++++++++++++++++++++----- tools/include/uapi/linux/bpf.h | 10 ++++ 4 files changed, 113 insertions(+), 15 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 4162afc6b5d0..2acf9b336371 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1573,6 +1573,16 @@ union bpf_attr { * If provided, prog_flags should have BPF_F_TOKEN_FD flag set. */ __s32 prog_token_fd; + /* The fd_array_cnt can be used to pass the length of the + * fd_array array. In this case all the [map] file descriptors + * passed in this array will be bound to the program, even if + * the maps are not referenced directly. The functionality is + * similar to the BPF_PROG_BIND_MAP syscall, but maps can be + * used by the verifier during the program load. If provided, + * then the fd_array[0,...,fd_array_cnt-1] is expected to be + * continuous. + */ + __u32 fd_array_cnt; }; struct { /* anonymous struct used by BPF_OBJ_* commands */ diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 58190ca724a2..7e3fbc23c742 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2729,7 +2729,7 @@ static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) } /* last field in 'union bpf_attr' used by this command */ -#define BPF_PROG_LOAD_LAST_FIELD prog_token_fd +#define BPF_PROG_LOAD_LAST_FIELD fd_array_cnt static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) { diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 8e034a22aa2a..a84ba93c0036 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -19181,22 +19181,10 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env, return 0; } -/* Add map behind fd to used maps list, if it's not already there, and return - * its index. - * Returns <0 on error, or >= 0 index, on success. - */ -static int add_used_map_from_fd(struct bpf_verifier_env *env, int fd) +static int add_used_map(struct bpf_verifier_env *env, struct bpf_map *map) { - CLASS(fd, f)(fd); - struct bpf_map *map; int i, err; - map = __bpf_map_get(f); - if (IS_ERR(map)) { - verbose(env, "fd %d is not pointing to valid bpf_map\n", fd); - return PTR_ERR(map); - } - /* check whether we recorded this map already */ for (i = 0; i < env->used_map_cnt; i++) if (env->used_maps[i] == map) @@ -19227,6 +19215,24 @@ static int add_used_map_from_fd(struct bpf_verifier_env *env, int fd) return env->used_map_cnt - 1; } +/* Add map behind fd to used maps list, if it's not already there, and return + * its index. + * Returns <0 on error, or >= 0 index, on success. + */ +static int add_used_map_from_fd(struct bpf_verifier_env *env, int fd) +{ + struct bpf_map *map; + CLASS(fd, f)(fd); + + map = __bpf_map_get(f); + if (IS_ERR(map)) { + verbose(env, "fd %d is not pointing to valid bpf_map\n", fd); + return PTR_ERR(map); + } + + return add_used_map(env, map); +} + /* find and rewrite pseudo imm in ld_imm64 instructions: * * 1. if it accesses map FD, replace it with actual map pointer. @@ -22526,6 +22532,75 @@ struct btf *bpf_get_btf_vmlinux(void) return btf_vmlinux; } +/* + * The add_fd_from_fd_array() is executed only if fd_array_cnt is given. In + * this case expect that every file descriptor in the array is either a map or + * a BTF, or a hole (0). Everything else is considered to be trash. + */ +static int add_fd_from_fd_array(struct bpf_verifier_env *env, int fd) +{ + struct bpf_map *map; + CLASS(fd, f)(fd); + int ret; + + map = __bpf_map_get(f); + if (!IS_ERR(map)) { + ret = add_used_map(env, map); + if (ret < 0) + return ret; + return 0; + } + + if (!IS_ERR(__btf_get_by_fd(f))) + return 0; + + if (!fd) + return 0; + + verbose(env, "fd %d is not pointing to valid bpf_map or btf\n", fd); + return PTR_ERR(map); +} + +static int env_init_fd_array(struct bpf_verifier_env *env, union bpf_attr *attr, bpfptr_t uattr) +{ + int size = sizeof(int) * attr->fd_array_cnt; + int *copy; + int ret; + int i; + + if (attr->fd_array_cnt >= MAX_USED_MAPS) + return -E2BIG; + + env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel); + + /* + * The only difference between old (no fd_array_cnt is given) and new + * APIs is that in the latter case the fd_array is expected to be + * continuous and is scanned for map fds right away + */ + if (!size) + return 0; + + copy = kzalloc(size, GFP_KERNEL); + if (!copy) + return -ENOMEM; + + if (copy_from_bpfptr_offset(copy, env->fd_array, 0, size)) { + ret = -EFAULT; + goto free_copy; + } + + for (i = 0; i < attr->fd_array_cnt; i++) { + ret = add_fd_from_fd_array(env, copy[i]); + if (ret) + goto free_copy; + } + +free_copy: + kfree(copy); + return ret; +} + int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size) { u64 start_time = ktime_get_ns(); @@ -22557,7 +22632,9 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 env->insn_aux_data[i].orig_idx = i; env->prog = *prog; env->ops = bpf_verifier_ops[env->prog->type]; - env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel); + ret = env_init_fd_array(env, attr, uattr); + if (ret) + goto err_free_aux_data; env->allow_ptr_leaks = bpf_allow_ptr_leaks(env->prog->aux->token); env->allow_uninit_stack = bpf_allow_uninit_stack(env->prog->aux->token); @@ -22775,6 +22852,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 err_unlock: if (!is_priv) mutex_unlock(&bpf_verifier_lock); +err_free_aux_data: vfree(env->insn_aux_data); kvfree(env->insn_hist); err_free_env: diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 4162afc6b5d0..2acf9b336371 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1573,6 +1573,16 @@ union bpf_attr { * If provided, prog_flags should have BPF_F_TOKEN_FD flag set. */ __s32 prog_token_fd; + /* The fd_array_cnt can be used to pass the length of the + * fd_array array. In this case all the [map] file descriptors + * passed in this array will be bound to the program, even if + * the maps are not referenced directly. The functionality is + * similar to the BPF_PROG_BIND_MAP syscall, but maps can be + * used by the verifier during the program load. If provided, + * then the fd_array[0,...,fd_array_cnt-1] is expected to be + * continuous. + */ + __u32 fd_array_cnt; }; struct { /* anonymous struct used by BPF_OBJ_* commands */ From patchwork Tue Nov 19 10:15:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Protopopov X-Patchwork-Id: 13879563 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C16821C2334 for ; Tue, 19 Nov 2024 10:13:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732011198; cv=none; b=ShQpUg67nOQwPni1332QkwPKTCrIoH07FkX4r9d11V+nFFHpKPQp3dXQaoVaM2K3K4Wl9jdyRLq0P0UQ1wC7orFFpDMj8KnMfvzd/RTxEZ7rIXJankE2W4y5JC/placpz/CVwhp1j+hSXw6qp9hHfzV55cjg6VBJlJLUIlZOsyo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732011198; c=relaxed/simple; bh=f8Av85VxaQvBl1rqr9M+ANEaYhHy8kFBZs4/XpxHRZk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Jy2f6O1bTKjMpxaoWZViaLlJYfJEmN5Hw6oXCtSixBPb3EIL4TheTx/33Wdfi+oiSJlsR0GyXWiKu1TqmYaBYZwnknrqEuv29nWZ9ZYlIWkscvQdndzs0gz7R2AZgo1beXenfNUCwsL6aRZpFV2zJ+w9qYXh1spkGbK/pTJpMOc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=isovalent.com; spf=pass smtp.mailfrom=isovalent.com; dkim=pass (2048-bit key) header.d=isovalent.com header.i=@isovalent.com header.b=IDDkuMT/; arc=none smtp.client-ip=209.85.218.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=isovalent.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=isovalent.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=isovalent.com header.i=@isovalent.com header.b="IDDkuMT/" Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-aa3a79d4d59so123440366b.3 for ; Tue, 19 Nov 2024 02:13:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1732011195; x=1732615995; 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=cgM7y3e/xxOAPYuVfF9TjNfA1P98tNQ2LygYI7XstEM=; b=IDDkuMT/KyqK7LyLCpeHFk0+c82vh941YfABCa+tVLlIlLsPGdfFI1aQ+y+r3VhoUQ Kw2+NatmRbzhsTALex0+2HGabhbABVy4cKQUIIKWxgtEHN5XyjsJngrenJ/yi2I4QdTS K0nQJxA2PyF2L4ZjBC5aqdryr3DSQmazDEPMxhGn8t4nlIqK/toRu9SWb7ZtbyRZGuJy c+4CY9rtk5pWkKCmMT6F8QKOdiA3KBybzuDZFWs+VgGnJNvoLQGs0VRitsqgjOAm9hRJ 4AJvA695sEC2xgfomAb+v94YhMNNTB4vPySu6ejb+pIR5TbnZlIqDk2yCnnFFHmXHHGV 0yNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732011195; x=1732615995; 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=cgM7y3e/xxOAPYuVfF9TjNfA1P98tNQ2LygYI7XstEM=; b=TPceJRNM61/NiZELMGWsRMTJDXwColbGd0uZ8reD82r6NkJYwhkuq2BjL/5cWq2c7D qyFvI5bmtQff6TmW2iaz3tWJtl/kCCDyCmTsdVc5IXl9nMj8DZBjbdTopF9G8lbKDgmo uRypnGGjc0wYKKkoyCpsIzCAVQG2o9Par+fg2uYHdhO/wMeSpWWhp/wUUv1LDQHoMEjb X62Gb2/dyGh9Kc+WXGOXMQiryzPQU2HcuYYKAu5bgOzfe9NP0d1Cj0JSDOPAcM2/El6L KEdVfwNHX5+w4RBbinQmDYUFjaHdBj1RiMJmQXAjWIl2boBaA3Kfg0bxdvU5G6rcsu76 6d4g== X-Gm-Message-State: AOJu0Yw2sztnSauuE2jhtGf8nOb8Iw0FZk53UUmwAG/ZRSdlOJ+lKirs TfRMJEfryHKLmlD9amqOzdLjUpr3xgrjL4g+SXujtJI7kAXNGZsgtdOjOyvF+31z5e9B24UlQik d X-Google-Smtp-Source: AGHT+IFN0/wm4Y52fdPmFxa6wvXzIgAwfSQIgiF1XhZFSLBhBR3GPr5VyqBYbIoOqlwZMVZh0XZyHg== X-Received: by 2002:a17:906:c10a:b0:a99:5601:7dc1 with SMTP id a640c23a62f3a-aa483551fc8mr1440089866b.49.1732011194710; Tue, 19 Nov 2024 02:13:14 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aa20df7eee4sm629003066b.87.2024.11.19.02.13.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2024 02:13:13 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org Cc: Anton Protopopov Subject: [PATCH v2 bpf-next 4/6] selftests/bpf: Add tests for fd_array_cnt Date: Tue, 19 Nov 2024 10:15:50 +0000 Message-Id: <20241119101552.505650-5-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241119101552.505650-1-aspsk@isovalent.com> References: <20241119101552.505650-1-aspsk@isovalent.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 Add a new set of tests to test the new field in PROG_LOAD-related part of bpf_attr: fd_array_cnt. Add the following test cases: * fd_array_cnt/no-fd-array: program is loaded in a normal way, without any fd_array present * fd_array_cnt/fd-array-ok: pass two extra non-used maps, check that they're bound to the program * fd_array_cnt/fd-array-dup-input: pass a few extra maps, only two of which are unique * fd_array_cnt/fd-array-ref-maps-in-array: pass a map in fd_array which is also referenced from within the program * fd_array_cnt/fd-array-trash-input: pass array with some trash * fd_array_cnt/fd-array-with-holes: pass an array with holes (fd=0) * fd_array_cnt/fd-array-2big: pass too large array All the tests above are using the bpf(2) syscall directly, no libbpf involved. Signed-off-by: Anton Protopopov --- .../selftests/bpf/prog_tests/fd_array.c | 381 ++++++++++++++++++ 1 file changed, 381 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/fd_array.c diff --git a/tools/testing/selftests/bpf/prog_tests/fd_array.c b/tools/testing/selftests/bpf/prog_tests/fd_array.c new file mode 100644 index 000000000000..1b47386e66c3 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/fd_array.c @@ -0,0 +1,381 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +#include +#include +#include + +#include "../test_btf.h" + +static inline int _bpf_map_create(void) +{ + static union bpf_attr attr = { + .map_type = BPF_MAP_TYPE_ARRAY, + .key_size = 4, + .value_size = 8, + .max_entries = 1, + }; + + return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); +} + +static int _btf_create(void) +{ + struct btf_blob { + struct btf_header btf_hdr; + __u32 types[8]; + __u32 str; + } raw_btf = { + .btf_hdr = { + .magic = BTF_MAGIC, + .version = BTF_VERSION, + .hdr_len = sizeof(struct btf_header), + .type_len = sizeof(raw_btf.types), + .str_off = offsetof(struct btf_blob, str) - offsetof(struct btf_blob, types), + .str_len = sizeof(raw_btf.str), + }, + .types = { + /* long */ + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8), /* [1] */ + /* unsigned long */ + BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */ + }, + }; + static union bpf_attr attr = { + .btf_size = sizeof(raw_btf), + }; + + attr.btf = (long)&raw_btf; + + return syscall(__NR_bpf, BPF_BTF_LOAD, &attr, sizeof(attr)); +} + +static bool map_exists(__u32 id) +{ + int fd; + + fd = bpf_map_get_fd_by_id(id); + if (fd >= 0) { + close(fd); + return true; + } + return false; +} + +static inline int bpf_prog_get_map_ids(int prog_fd, __u32 *nr_map_ids, __u32 *map_ids) +{ + __u32 len = sizeof(struct bpf_prog_info); + struct bpf_prog_info info = { + .nr_map_ids = *nr_map_ids, + .map_ids = ptr_to_u64(map_ids), + }; + int err; + + err = bpf_prog_get_info_by_fd(prog_fd, &info, &len); + if (!ASSERT_OK(err, "bpf_prog_get_info_by_fd")) + return -1; + + *nr_map_ids = info.nr_map_ids; + + return 0; +} + +static int __load_test_prog(int map_fd, int *fd_array, int fd_array_cnt) +{ + /* A trivial program which uses one map */ + struct bpf_insn insns[] = { + BPF_LD_MAP_FD(BPF_REG_1, map_fd), + BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0), + BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), + BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8), + BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }; + union bpf_attr attr = { + .prog_type = BPF_PROG_TYPE_XDP, /* we don't care */ + .insns = ptr_to_u64(insns), + .insn_cnt = ARRAY_SIZE(insns), + .license = ptr_to_u64("GPL"), + .fd_array = ptr_to_u64(fd_array), + .fd_array_cnt = fd_array_cnt, + }; + + return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); +} + +static int load_test_prog(int *fd_array, int fd_array_cnt) +{ + int map_fd; + int ret; + + map_fd = _bpf_map_create(); + if (!ASSERT_GE(map_fd, 0, "_bpf_map_create")) + return map_fd; + + ret = __load_test_prog(map_fd, fd_array, fd_array_cnt); + close(map_fd); + + /* switch back to returning the actual value */ + if (ret < 0) + return -errno; + return ret; +} + +static bool check_expected_map_ids(int prog_fd, int expected, __u32 *map_ids, __u32 *nr_map_ids) +{ + int err; + + err = bpf_prog_get_map_ids(prog_fd, nr_map_ids, map_ids); + if (!ASSERT_OK(err, "bpf_prog_get_map_ids")) + return false; + if (!ASSERT_EQ(*nr_map_ids, expected, "unexpected nr_map_ids")) + return false; + + return true; +} + +/* + * Load a program, which uses one map. No fd_array maps are present. + * On return only one map is expected to be bound to prog. + */ +static void check_fd_array_cnt__no_fd_array(void) +{ + __u32 map_ids[16]; + __u32 nr_map_ids; + int prog_fd = -1; + + prog_fd = load_test_prog(NULL, 0); + if (!ASSERT_GE(prog_fd, 0, "BPF_PROG_LOAD")) + return; + nr_map_ids = ARRAY_SIZE(map_ids); + check_expected_map_ids(prog_fd, 1, map_ids, &nr_map_ids); + close(prog_fd); +} + +/* + * Load a program, which uses one map, and pass two extra, non-equal, maps in + * fd_array with fd_array_cnt=2. On return three maps are expected to be bound + * to the program. + */ +static void check_fd_array_cnt__fd_array_ok(void) +{ + int extra_fds[2] = { -1, -1 }; + __u32 map_ids[16]; + __u32 nr_map_ids; + int prog_fd = -1; + + extra_fds[0] = _bpf_map_create(); + if (!ASSERT_GE(extra_fds[0], 0, "_bpf_map_create")) + goto cleanup; + extra_fds[1] = _bpf_map_create(); + if (!ASSERT_GE(extra_fds[1], 0, "_bpf_map_create")) + goto cleanup; + prog_fd = load_test_prog(extra_fds, 2); + if (!ASSERT_GE(prog_fd, 0, "BPF_PROG_LOAD")) + goto cleanup; + nr_map_ids = ARRAY_SIZE(map_ids); + if (!check_expected_map_ids(prog_fd, 3, map_ids, &nr_map_ids)) + goto cleanup; + + /* maps should still exist when original file descriptors are closed */ + close(extra_fds[0]); + close(extra_fds[1]); + if (!ASSERT_EQ(map_exists(map_ids[0]), true, "map_ids[0] should exist")) + goto cleanup; + if (!ASSERT_EQ(map_exists(map_ids[1]), true, "map_ids[1] should exist")) + goto cleanup; + + /* some fds might be invalid, so ignore return codes */ +cleanup: + close(extra_fds[1]); + close(extra_fds[0]); + close(prog_fd); +} + +/* + * Load a program with a few extra maps duplicated in the fd_array. + * After the load maps should only be referenced once. + */ +static void check_fd_array_cnt__duplicated_maps(void) +{ + int extra_fds[4] = { -1, -1, -1, -1 }; + __u32 map_ids[16]; + __u32 nr_map_ids; + int prog_fd = -1; + + extra_fds[0] = extra_fds[2] = _bpf_map_create(); + if (!ASSERT_GE(extra_fds[0], 0, "_bpf_map_create")) + goto cleanup; + extra_fds[1] = extra_fds[3] = _bpf_map_create(); + if (!ASSERT_GE(extra_fds[1], 0, "_bpf_map_create")) + goto cleanup; + prog_fd = load_test_prog(extra_fds, 4); + if (!ASSERT_GE(prog_fd, 0, "BPF_PROG_LOAD")) + goto cleanup; + nr_map_ids = ARRAY_SIZE(map_ids); + if (!check_expected_map_ids(prog_fd, 3, map_ids, &nr_map_ids)) + goto cleanup; + + /* maps should still exist when original file descriptors are closed */ + close(extra_fds[0]); + close(extra_fds[1]); + if (!ASSERT_EQ(map_exists(map_ids[0]), true, "map should exist")) + goto cleanup; + if (!ASSERT_EQ(map_exists(map_ids[1]), true, "map should exist")) + goto cleanup; + + /* some fds might be invalid, so ignore return codes */ +cleanup: + close(extra_fds[1]); + close(extra_fds[0]); + close(prog_fd); +} + +/* + * Check that if maps which are referenced by a program are + * passed in fd_array, then they will be referenced only once + */ +static void check_fd_array_cnt__referenced_maps_in_fd_array(void) +{ + int extra_fds[1] = { -1 }; + __u32 map_ids[16]; + __u32 nr_map_ids; + int prog_fd = -1; + + extra_fds[0] = _bpf_map_create(); + if (!ASSERT_GE(extra_fds[0], 0, "_bpf_map_create")) + goto cleanup; + prog_fd = __load_test_prog(extra_fds[0], extra_fds, 1); + if (!ASSERT_GE(prog_fd, 0, "BPF_PROG_LOAD")) + goto cleanup; + nr_map_ids = ARRAY_SIZE(map_ids); + if (!check_expected_map_ids(prog_fd, 1, map_ids, &nr_map_ids)) + goto cleanup; + + /* map should still exist when original file descriptor is closed */ + close(extra_fds[0]); + if (!ASSERT_EQ(map_exists(map_ids[0]), true, "map should exist")) + goto cleanup; + + /* some fds might be invalid, so ignore return codes */ +cleanup: + close(extra_fds[0]); + close(prog_fd); +} + +/* + * Test that a program with trash in fd_array can't be loaded: + * only map and BTF file descriptors should be accepted. + */ +static void check_fd_array_cnt__fd_array_with_trash(void) +{ + int extra_fds[3] = { -1, -1, -1 }; + int prog_fd = -1; + + extra_fds[0] = _bpf_map_create(); + if (!ASSERT_GE(extra_fds[0], 0, "_bpf_map_create")) + goto cleanup; + extra_fds[1] = _btf_create(); + if (!ASSERT_GE(extra_fds[1], 0, "_btf_create")) + goto cleanup; + + /* trash 1: not a file descriptor */ + extra_fds[2] = 0xbeef; + prog_fd = load_test_prog(extra_fds, 3); + if (!ASSERT_EQ(prog_fd, -EBADF, "prog should have been rejected with -EBADF")) + goto cleanup; + + /* trash 2: not a map or btf */ + extra_fds[2] = socket(AF_INET, SOCK_STREAM, 0); + if (!ASSERT_GE(extra_fds[2], 0, "socket")) + goto cleanup; + + prog_fd = load_test_prog(extra_fds, 3); + if (!ASSERT_EQ(prog_fd, -EINVAL, "prog should have been rejected with -EINVAL")) + goto cleanup; + + /* some fds might be invalid, so ignore return codes */ +cleanup: + close(extra_fds[2]); + close(extra_fds[1]); + close(extra_fds[0]); +} + +/* + * Test that a program with zeroes (= holes) in fd_array can be loaded: + * only map and BTF file descriptors should be accepted. + */ +static void check_fd_array_cnt__fd_array_with_holes(void) +{ + int extra_fds[4] = { -1, -1, -1, -1 }; + int prog_fd = -1; + + extra_fds[0] = _bpf_map_create(); + if (!ASSERT_GE(extra_fds[0], 0, "_bpf_map_create")) + goto cleanup; + /* punch a hole*/ + extra_fds[1] = 0; + extra_fds[2] = _btf_create(); + if (!ASSERT_GE(extra_fds[1], 0, "_btf_create")) + goto cleanup; + /* punch a hole*/ + extra_fds[3] = 0; + + prog_fd = load_test_prog(extra_fds, 4); + ASSERT_GE(prog_fd, 0, "prog with holes should have been loaded"); + + /* some fds might be invalid, so ignore return codes */ +cleanup: + close(extra_fds[2]); + close(extra_fds[0]); + close(prog_fd); +} + +/* + * Test that a program with too big fd_array can't be loaded. + */ +static void check_fd_array_cnt__fd_array_too_big(void) +{ + int extra_fds[65]; + int prog_fd = -1; + int i; + + for (i = 0; i < 65; i++) { + extra_fds[i] = _bpf_map_create(); + if (!ASSERT_GE(extra_fds[i], 0, "_bpf_map_create")) + goto cleanup_fds; + } + + prog_fd = load_test_prog(extra_fds, 65); + ASSERT_EQ(prog_fd, -E2BIG, "prog should have been rejected with -E2BIG"); + +cleanup_fds: + while (i > 0) + close(extra_fds[--i]); +} + +void test_fd_array_cnt(void) +{ + if (test__start_subtest("no-fd-array")) + check_fd_array_cnt__no_fd_array(); + + if (test__start_subtest("fd-array-ok")) + check_fd_array_cnt__fd_array_ok(); + + if (test__start_subtest("fd-array-dup-input")) + check_fd_array_cnt__duplicated_maps(); + + if (test__start_subtest("fd-array-ref-maps-in-array")) + check_fd_array_cnt__referenced_maps_in_fd_array(); + + if (test__start_subtest("fd-array-trash-input")) + check_fd_array_cnt__fd_array_with_trash(); + + if (test__start_subtest("fd-array-with-holes")) + check_fd_array_cnt__fd_array_with_holes(); + + if (test__start_subtest("fd-array-2big")) + check_fd_array_cnt__fd_array_too_big(); +} From patchwork Tue Nov 19 10:15:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Protopopov X-Patchwork-Id: 13879564 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) (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 7FDFF19D06E for ; Tue, 19 Nov 2024 10:13:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732011199; cv=none; b=peMTI9u/YoCfXU1ffF5cGjB5YS5lddrCRWaMgppFOblxKCU/7tqFNDhGyzq3mknpknoRgeXqPLeInIvjhPiTqV8QYWzHOMv3/kFcGBuElYpyAYGS0ipm92ZhIkefvRM3NCXhXbpAH28M1+jGT3KUOkbikWx8QgmwoL9i0NOvsd8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732011199; c=relaxed/simple; bh=+leBIfGTx3qYqG0qr3EMc/+ailKE10I/dQRiyX0z0CA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CmqHhoToGYHUM8uu8cgLu5HEU6lRzDbTWtLL7ERTsy8d0KRbojOmD8gN0Hegq8P/i7JOMiR/hYkpbfp29MO+2CXlcwyVqSyKiI2oFPr0DzVLTWBGVp2SZvHOzb+i71uxDOuU9ikCLNKAkC94D+Zqfl0yLhK6dpzSZH33UpJo7Hk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=isovalent.com; spf=pass smtp.mailfrom=isovalent.com; dkim=pass (2048-bit key) header.d=isovalent.com header.i=@isovalent.com header.b=R9UTcPM8; arc=none smtp.client-ip=209.85.218.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=isovalent.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=isovalent.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=isovalent.com header.i=@isovalent.com header.b="R9UTcPM8" Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-aa1e6ecd353so247151966b.1 for ; Tue, 19 Nov 2024 02:13:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1732011196; x=1732615996; 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=G329gGS4RBx9EPCJNPCJhs4+g77tte1wqZgFhLBI2LE=; b=R9UTcPM8gA0ZOe2mbM+BbpwSuTX7VyXA/mMRpW+wiQ1b9LDrp4tMn/Dd9lfQlYJp1V BTxcvjyz4qRrEGwYwOYZXnEWom9R9djQ/qhjPFX+ygShshSCy1a49fJ7QzAYDiiaUP8G tv0KYDJ7KL2AtcKhkKyNY8+dUA0tM3zeKy8bSoa3iWBMFa864njoNRWu5Y1bEUZNalAj GisYd1uSUbOQ7/2ox8fC+aWM5ENu5lusbTFJntiPCl9JsNlQC3dwcolvI0Z0F2TcNzy9 6FPqGetjbcv+iany8BVPPvk3cTilPv43WqBLAyb7lDLiTEy+IMeCXwtdcE7k7dqUKpVI 8veg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732011196; x=1732615996; 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=G329gGS4RBx9EPCJNPCJhs4+g77tte1wqZgFhLBI2LE=; b=G42s/iHLb4kcUI7vRUHIzr3axvR/0oXyGYUsgGJgJnaQZeYpgZOgd8WOBGlhN1CSy+ hihKSChy1XqsrtOSxjPCAcsq8Aa1h/b4qCcDSZx+E8Ov5uA0lOOUVGf64ZrChUOlMjN/ aQ43cx46xSUZm0IwH3FnTTLPa8k7Nr75fshc/2b/8qZB6POpH8yEpSixIPwKwog9UVmX FazCcthRXXXZ5WjIZEyoQeN89H1EWkQo6+UB9KLjgxjjFi21vav46r7gR0HpjqMbleDL 98sWF+CkAo1z7skUjyyqq/HJzs/FHnK+tJA+g3ekDoaSXpq0UyZyTfCjffdQ7GMHM5pq LNQg== X-Gm-Message-State: AOJu0YyLTh/COrS1kjNKMhCQ6gxR6/X/62CVu6BH6iUS735mm1bdZNNi 8MMi3HpglhYfh8Kw2Wdq+X7juGHjtByERDZuMzUHy7iHzXTeXXUGlmELqx9VNgqDfeIG4pAF9yr z X-Google-Smtp-Source: AGHT+IHJ3MZYqcCV1sUwncobiSFUkCiM7cwd9lBH8dknWp/MRytwCRP+DfXxrhifqDlap4wTQdpRYQ== X-Received: by 2002:a05:6402:13c4:b0:5cf:14fa:d24d with SMTP id 4fb4d7f45d1cf-5cf8fd2c8b2mr21770494a12.22.1732011195759; Tue, 19 Nov 2024 02:13:15 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aa20df7eee4sm629003066b.87.2024.11.19.02.13.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2024 02:13:15 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org Cc: Anton Protopopov , Jiri Olsa Subject: [PATCH v2 bpf-next 5/6] bpf: fix potential error return Date: Tue, 19 Nov 2024 10:15:51 +0000 Message-Id: <20241119101552.505650-6-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241119101552.505650-1-aspsk@isovalent.com> References: <20241119101552.505650-1-aspsk@isovalent.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 bpf_remove_insns() function returns WARN_ON_ONCE(error), where error is a result of bpf_adj_branches(), and thus should be always 0 However, if for any reason it is not 0, then it will be converted to boolean by WARN_ON_ONCE and returned to user space as 1, not an actual error value. Fix this by returning the original err after the WARN check. Signed-off-by: Anton Protopopov Acked-by: Jiri Olsa --- kernel/bpf/core.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 14d9288441f2..a15059918768 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -539,6 +539,8 @@ struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off, int bpf_remove_insns(struct bpf_prog *prog, u32 off, u32 cnt) { + int err; + /* Branch offsets can't overflow when program is shrinking, no need * to call bpf_adj_branches(..., true) here */ @@ -546,7 +548,12 @@ int bpf_remove_insns(struct bpf_prog *prog, u32 off, u32 cnt) sizeof(struct bpf_insn) * (prog->len - off - cnt)); prog->len -= cnt; - return WARN_ON_ONCE(bpf_adj_branches(prog, off, off + cnt, off, false)); + err = bpf_adj_branches(prog, off, off + cnt, off, false); + WARN_ON_ONCE(err); + if (err) + return err; + + return 0; } static void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp) From patchwork Tue Nov 19 10:15:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Protopopov X-Patchwork-Id: 13879565 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f174.google.com (mail-lj1-f174.google.com [209.85.208.174]) (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 DD8731C2DB4 for ; Tue, 19 Nov 2024 10:13:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732011201; cv=none; b=qbJzfgZHg2wT/y7j1KWQDjue9xDJtWqajtuR+JY2c69Np5vQzSDyDoOjc7JHdDygEeTUBZQJo8wKhVH9pxcxMZzhcdjiBCAmY8sQ09KUqFCZdZnbIYFgR5BzqteF3Ipy/OBcUedh4ljXZL+Kf6/gwYPVsvw5HRuk5FZdA7lBhg4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732011201; c=relaxed/simple; bh=5M+9sZ2uOISCSb6KW/FXzpjBYHwIs149KFBIoWPdbV4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Nqh7g2eAbY2LMrWnXKsOGpiIxiSvfmVkDYePaR88+Rj+1FK1fLvbTXKqhFQWB7f4Yf5XUNKxSCA1uQWtyzWAvH4QGrJzOcdBtIR5Sr0qQSQATSVbnRsrVUZ7uP4LyTQ5lmffu0EQKyUI4fEKc8zkbNkvmPFQnJ1dgv0Dc0HfIjE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=isovalent.com; spf=pass smtp.mailfrom=isovalent.com; dkim=pass (2048-bit key) header.d=isovalent.com header.i=@isovalent.com header.b=XggMmK6n; arc=none smtp.client-ip=209.85.208.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=isovalent.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=isovalent.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=isovalent.com header.i=@isovalent.com header.b="XggMmK6n" Received: by mail-lj1-f174.google.com with SMTP id 38308e7fff4ca-2fb5fa911aaso41524101fa.2 for ; Tue, 19 Nov 2024 02:13:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1732011197; x=1732615997; 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=95uLp76JDqCGAntKfWwQIWRGb8VQc95vaaCgVutyp08=; b=XggMmK6nnotAihdXwE23OFGRs/6p7Djhfa6jDXSo8begomuup39mxR5kIiuFJckPxv YD7HWWDalWoXHUJy4H52zMVL42bMU0WeFYdlLbbQZ66B0yTJt9lEQRi84iklLiWnStbJ YabMFIJkiAh7+u/nqGOrRfZgm3tIaKd5FhzMXH/Y4Rknf4EeRgL0piV5dF863+3OcxBk YqlZdYbuFTsTl/MAwLWfsWJ+dm0qQT1YGb+YtkfQgBGiZn9AHtnxBGfr76Hitx95vT13 DLDnMRvSXzJrL+ZRDzWxysQiC8ueM1a8I7b7Ah2J2+xCHgev81ZYjnIbvQd6X/q2u9kd Soiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732011197; x=1732615997; 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=95uLp76JDqCGAntKfWwQIWRGb8VQc95vaaCgVutyp08=; b=BLjBY9PAMQAMZvwKO6GCSRmflr+NPicn3+JNWb0Y7uCqaJXYWo+r+4ej785/ywW2KJ fposTsWJmV4ZTNtQDy/O+o34KBoJNwM+/czNd5lQK63GEeNJO/BGPqraCdEfqTQO1fJC sSd7MUlubePakBJVncHkDJrVUm9Tfd+mzL9kaYoct7DF4AJHKo/C3fYiDVIAphu58htv zYw7d3TgVbeQjB8DLRZmEjYOehShAS/Jiu9O2CGQfQ4pXvRqBUbhK+2bn/ntrvwTR3g6 ASQQ26t/SAuAbtYiLv3dPQsRek7fU51Yh+EryqkmtgpowqQVYxUpTcf3MRsbXB5/n+sC +Etg== X-Gm-Message-State: AOJu0YzIIOmbMIuJIZ3GnctnN8Lui5OiLibFdBl54E/3x08UjEBX30hB P0/N8iW7apAQ3BHOSheK2Xndy7vhpFZkiXzNL9j1L/ssn6uVHd33lR2WA2tq5A26jPSeOGRtTPL b X-Google-Smtp-Source: AGHT+IEcv7FD1KJhVSOoA2npLdTtfQkh/oHfzo0MtgjvXRaXBhWb7TRMalIGetTdQnkzsGhH9IkCEQ== X-Received: by 2002:a05:651c:158d:b0:2f7:5a41:b0b with SMTP id 38308e7fff4ca-2ff606db221mr103930511fa.26.1732011196669; Tue, 19 Nov 2024 02:13:16 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aa20df7eee4sm629003066b.87.2024.11.19.02.13.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2024 02:13:15 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org Cc: Anton Protopopov , Eduard Zingerman Subject: [PATCH v2 bpf-next 6/6] selftest/bpf: replace magic constants by macros Date: Tue, 19 Nov 2024 10:15:52 +0000 Message-Id: <20241119101552.505650-7-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241119101552.505650-1-aspsk@isovalent.com> References: <20241119101552.505650-1-aspsk@isovalent.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 Replace magic constants in a BTF structure initialization code by proper macros, as is done in other similar selftests. Signed-off-by: Anton Protopopov Suggested-by: Eduard Zingerman --- tools/testing/selftests/bpf/progs/syscall.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/bpf/progs/syscall.c b/tools/testing/selftests/bpf/progs/syscall.c index 0f4dfb770c32..b698cc62a371 100644 --- a/tools/testing/selftests/bpf/progs/syscall.c +++ b/tools/testing/selftests/bpf/progs/syscall.c @@ -76,9 +76,9 @@ static int btf_load(void) .magic = BTF_MAGIC, .version = BTF_VERSION, .hdr_len = sizeof(struct btf_header), - .type_len = sizeof(__u32) * 8, - .str_off = sizeof(__u32) * 8, - .str_len = sizeof(__u32), + .type_len = sizeof(raw_btf.types), + .str_off = offsetof(struct btf_blob, str) - offsetof(struct btf_blob, types), + .str_len = sizeof(raw_btf.str), }, .types = { /* long */