Skip to content

Commit 69eaab0

Browse files
anakryikoborkmann
authored andcommitted
btf: extract BTF type size calculation
This pre-patch extracts calculation of amount of space taken by BTF type descriptor for later reuse by btf_dedup functionality. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent a8a1f7d commit 69eaab0

File tree

1 file changed

+46
-52
lines changed

1 file changed

+46
-52
lines changed

tools/lib/bpf/btf.c

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,37 @@ static int btf_parse_str_sec(struct btf *btf)
182182
return 0;
183183
}
184184

185+
static int btf_type_size(struct btf_type *t)
186+
{
187+
int base_size = sizeof(struct btf_type);
188+
__u16 vlen = BTF_INFO_VLEN(t->info);
189+
190+
switch (BTF_INFO_KIND(t->info)) {
191+
case BTF_KIND_FWD:
192+
case BTF_KIND_CONST:
193+
case BTF_KIND_VOLATILE:
194+
case BTF_KIND_RESTRICT:
195+
case BTF_KIND_PTR:
196+
case BTF_KIND_TYPEDEF:
197+
case BTF_KIND_FUNC:
198+
return base_size;
199+
case BTF_KIND_INT:
200+
return base_size + sizeof(__u32);
201+
case BTF_KIND_ENUM:
202+
return base_size + vlen * sizeof(struct btf_enum);
203+
case BTF_KIND_ARRAY:
204+
return base_size + sizeof(struct btf_array);
205+
case BTF_KIND_STRUCT:
206+
case BTF_KIND_UNION:
207+
return base_size + vlen * sizeof(struct btf_member);
208+
case BTF_KIND_FUNC_PROTO:
209+
return base_size + vlen * sizeof(struct btf_param);
210+
default:
211+
pr_debug("Unsupported BTF_KIND:%u\n", BTF_INFO_KIND(t->info));
212+
return -EINVAL;
213+
}
214+
}
215+
185216
static int btf_parse_type_sec(struct btf *btf)
186217
{
187218
struct btf_header *hdr = btf->hdr;
@@ -191,41 +222,13 @@ static int btf_parse_type_sec(struct btf *btf)
191222

192223
while (next_type < end_type) {
193224
struct btf_type *t = next_type;
194-
__u16 vlen = BTF_INFO_VLEN(t->info);
225+
int type_size;
195226
int err;
196227

197-
next_type += sizeof(*t);
198-
switch (BTF_INFO_KIND(t->info)) {
199-
case BTF_KIND_INT:
200-
next_type += sizeof(int);
201-
break;
202-
case BTF_KIND_ARRAY:
203-
next_type += sizeof(struct btf_array);
204-
break;
205-
case BTF_KIND_STRUCT:
206-
case BTF_KIND_UNION:
207-
next_type += vlen * sizeof(struct btf_member);
208-
break;
209-
case BTF_KIND_ENUM:
210-
next_type += vlen * sizeof(struct btf_enum);
211-
break;
212-
case BTF_KIND_FUNC_PROTO:
213-
next_type += vlen * sizeof(struct btf_param);
214-
break;
215-
case BTF_KIND_FUNC:
216-
case BTF_KIND_TYPEDEF:
217-
case BTF_KIND_PTR:
218-
case BTF_KIND_FWD:
219-
case BTF_KIND_VOLATILE:
220-
case BTF_KIND_CONST:
221-
case BTF_KIND_RESTRICT:
222-
break;
223-
default:
224-
pr_debug("Unsupported BTF_KIND:%u\n",
225-
BTF_INFO_KIND(t->info));
226-
return -EINVAL;
227-
}
228-
228+
type_size = btf_type_size(t);
229+
if (type_size < 0)
230+
return type_size;
231+
next_type += type_size;
229232
err = btf_add_type(btf, t);
230233
if (err)
231234
return err;
@@ -252,21 +255,6 @@ static bool btf_type_is_void_or_null(const struct btf_type *t)
252255
return !t || btf_type_is_void(t);
253256
}
254257

255-
static __s64 btf_type_size(const struct btf_type *t)
256-
{
257-
switch (BTF_INFO_KIND(t->info)) {
258-
case BTF_KIND_INT:
259-
case BTF_KIND_STRUCT:
260-
case BTF_KIND_UNION:
261-
case BTF_KIND_ENUM:
262-
return t->size;
263-
case BTF_KIND_PTR:
264-
return sizeof(void *);
265-
default:
266-
return -EINVAL;
267-
}
268-
}
269-
270258
#define MAX_RESOLVE_DEPTH 32
271259

272260
__s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
@@ -280,11 +268,16 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
280268
t = btf__type_by_id(btf, type_id);
281269
for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
282270
i++) {
283-
size = btf_type_size(t);
284-
if (size >= 0)
285-
break;
286-
287271
switch (BTF_INFO_KIND(t->info)) {
272+
case BTF_KIND_INT:
273+
case BTF_KIND_STRUCT:
274+
case BTF_KIND_UNION:
275+
case BTF_KIND_ENUM:
276+
size = t->size;
277+
goto done;
278+
case BTF_KIND_PTR:
279+
size = sizeof(void *);
280+
goto done;
288281
case BTF_KIND_TYPEDEF:
289282
case BTF_KIND_VOLATILE:
290283
case BTF_KIND_CONST:
@@ -308,6 +301,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
308301
if (size < 0)
309302
return -EINVAL;
310303

304+
done:
311305
if (nelems && size > UINT32_MAX / nelems)
312306
return -E2BIG;
313307

0 commit comments

Comments
 (0)